简体   繁体   中英

URLSession dataTask symbolic breakpoint at specific URL

I have an API wrapper, which is logging all URLs requested by URLSession . I need to breakpoint at specific URL call, for a possibility to easily find a place in UI, from where API call is initialized.

There is no problem to stop at any URLSession dataTask . I'm doing this with lldb command br set -F '-[__NSCFURLSessionTask resume]' :

添加符号断点

Then the program stops:

在此处输入图片说明

So, the question:

Is there any way to obtain URL from context on the screenshot above, match it with provided URL, and continue if URLs doesn't match?

Or maybe some other ideas ( lldb python script for example)?

Let's start with figuring out the instance pointer value at the breakpoint. Since an Objective-C selector is being sent inside the Swift .resume() call we must fallback to the actual implementation of:

objc_msgSend(receiver, selector, arg1, arg2, ...)

Without going into too much details we rely on MacOS/iOS ABI specifying which cpu registers are used for passing arguments. We're interested specifically in the 1st argument to find the receiver instance pointer value.

Private class __NSCFURLSessionTask happens to have following method:

- (NSURL*)currentRequest_URL;

@JimIngham pointed out an amazing shortcut to accomplish this with universal argument synonyms being $arg1 $arg2 ... in lldb . (notice that selector's arg1 arg2 would be lldb 's $arg3 $arg4 respectively) This simplifies it to:

po [$arg1 currentRequest_URL] 
https://www.google.com

In case of x86 32bit Simulator which would correspond to devices lower than iPhone 5S you would use instead:

(lldb) x/x $esp+4
     0xbff9e050: 0x7874dd70
(lldb) po [0x7874dd70 currentRequest_URL]
    https://www.google.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM