Xcode breakpoint don't work inside dispatch_async block
Asked Answered
E

4

6

Our team seriously need some help with the following problem that we are facing as it is preventing us from debugging some of the code inside dispatch_async block.

Hope I will get some help or suggestions on what to do next.

The problem we faced is as below:

We recently hit a strange problem where in Xcode 6, we are not able to break within the dispatch_async block.

- (void)viewDidLoad {

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);    

        dispatch_async(queue, ^{


        BOOL setRtn = TRUE;  //<- break point is set here but never break

        //call to function
        BOOL setFn = some_function();
        });

} 



- (BOOL) some_function()
{
     BOOL test = YES;   <- break point set here, breakpoint break!


     return test;
}

What happens is that when we set a breakpoint within any line in the dispatch_async block, the code never breaks and the break point never works.

However, if we set the breakpoint within the function that is called by the dispatch_async block, the breakpoint works!

Is anyone else facing the same problem?

We are using Xcode 6.1 and is working on IOS8.1

Please help, have been going crazy trying to solve this problem for days.

Ethology answered 8/10, 2014 at 10:16 Comment(0)
E
11

It turns out that it was because of a 3rd party framework New Relic. After removing reference to the New Relic framework, we can then step inside the dispatch_async block.

So for any developer working on project that has a new relic plugin, you can temporary remove reference to the plugin and add it back later when your debugging id complete.

It took us a few days to find out , and we hope that this piece of information is helpful to everyone.

Ethology answered 9/10, 2014 at 5:46 Comment(2)
Any ideas on why this happens with New Relic? Your solution worked, I'm just wondering why this happened in the first place.Electrotype
Instead of removing NR entirely you can just disable the instrumentation for dispatch_async so you still get the rest of the monitoring even in dev. See my answer for more info.Lotze
L
11

As it turns out, Grand Central Dispatch instrumentation can indeed prevent breakpoints within dispatch_async blocks. You can disable the GCD macros in your environment by adding the following to your prefix.pch file:

#ifdef DEBUG
#undef dispatch_async 
#undef dispatch_sync 
#undef dispatch_after 
#undef dispatch_apply 
#undef _dispatch_once 
#endif
Lotze answered 6/8, 2015 at 19:51 Comment(0)
G
1

Depending on the configured build Optimization Level that line can be compiled in such a way that's impossible to break in it (it can be even removed from the compiler output if you don't use that variable in the block).

Try setting the optimization to -O0 in build settings.

Greatcoat answered 8/10, 2014 at 10:43 Comment(5)
We have checked the flag previously and the optimization level had already been set to -O0. Is there any other complier settings that we need to check?Ethology
I've tested in a demo project; with -O0 it stops at the bp, with the most agressive optimization it doesn't stop. I don't know right now of other flags that can affect the breakpoints. Did you clean & build?Greatcoat
We tested on a brand new project as well and the breakpoint works inside dispatch_async. However, for our existing project, even with optimization set to -O0, it doesn't break. Yeap, we did clean and build again, reinstall Xcode, make sure the generate debug symbol is on, and still doesn't work. Anything more that we can do?Ethology
One thing we found is is that if we set the breakpoint directly on the line of dispatch_async, dispatch_async(queue, ^{ <-- break point here, it will break with _85_<function_name>_block_invoke . Does this give you any clue on what happened?Ethology
Not other than that line is not being optimized. As expected.Greatcoat
C
0

I understand it doesn't make much sense, but for me the Clean build folder dance worked with some Swift 4.2 code wrapped inside a DispatchQueue.main.async that didn't stop at the breakpoint; after the clean & rebuild, it now breaks as expected.

I'm using Xcode 10.1 and my Optimization Level Build Setting was already None -O0 as suggested in other answers here.

Consort answered 24/1, 2019 at 5:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.