Why sometimes 'self' isn't available while debugging with lldb?

我只是一个虾纸丫 提交于 2019-12-18 19:14:07

问题


A lot of time (when it's not every time) I got the following error when I try to print objects on lldb. Is there some build/debug configuration to change or is this an error inside lldb?

(lldb) po userLevel
error: warning: Stopped in an Objective-C method, but 'self' isn't available; pretending we are in a generic context
error: use of undeclared identifier 'userLevel'
error: 1 errors parsing expression

I build with llvm and do not strip debug symbols.

Edit: Here is the backtrace:

(lldb) bt
* thread #1: tid = 0x1c03, 0x001169c5 FanCake-Beta`-[KWUserLevelController addPoints:](, _cmd=0x0029187b, points=15) + 179 at KWUserLevelController.m:53, stop reason = step over
    frame #0: 0x001169c5 FanCake-Beta`-[KWUserLevelController addPoints:](, _cmd=0x0029187b, points=15) + 179 at KWUserLevelController.m:53
    frame #1: 0x00112172 FanCake-Beta`-[KWEventRealTimeControllergameEngine:hostedGame:didSucceedIn:withScore:](self=0x0b9d7740, _cmd=0x0027a2a7, engine=0x1be5af40, game=0x1be5a850, completionTime=3.59473554800206, score=0) + 421 at KWEventRealTimeController.m:647
    frame #2: 0x0007189a FanCake-Beta`__35-[KMCatchEmGameEngine animateStep6]_block_invoke197(, finished='\x01') + 257 at KMCatchEmGameEngine.m:214
    frame #3: 0x01990df6 UIKit`-[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 223
    frame #4: 0x01983d66 UIKit`-[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 237
    frame #5: 0x01983f04 UIKit`-[UIViewAnimationState animationDidStop:finished:] + 68
    frame #6: 0x017587d8 QuartzCore`CA::Layer::run_animation_callbacks(void*) + 284
    frame #7: 0x03634014 libdispatch.dylib`_dispatch_client_callout + 14
    frame #8: 0x036247d5 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 296
    frame #9: 0x04737af5 CoreFoundation`__CFRunLoopRun + 1925
    frame #10: 0x04736f44 CoreFoundation`CFRunLoopRunSpecific + 276
    frame #11: 0x04736e1b CoreFoundation`CFRunLoopRunInMode + 123
    frame #12: 0x040367e3 GraphicsServices`GSEventRunModal + 88
    frame #13: 0x04036668 GraphicsServices`GSEventRun + 104
    frame #14: 0x01945ffc UIKit`UIApplicationMain + 1211
    frame #15: 0x000039a8 FanCake-Beta`main(argc=1, argv=0xbffff354) + 94 at main.m:13
    frame #16: 0x00002dc5 FanCake-Beta`start + 53

Edit 2: Same thing when I try to print local vars

(lldb) po currentUser
error: variable not available

If I put some NSLog() in the code, the correct value is printing. But not with the po command.


回答1:


I usually get this error when I have compiler optimization turned on. The compiler will generate code which does not necessarily follow your code logic flow.

Go to your project in the navigator -> Target -> Build settings -> Search for optimization level -> expand optimization level -> select the debug line -> change to none in both columns of your project and target.

Hope this helps.




回答2:


I was having this problem and it went away when I edited my scheme and set the Run build to Debug. I had set it to AdHoc for testing Push Notifications and that apparently makes LLDB unhappy.




回答3:


Because it's optimized away. Let's use an example:

void f(int self) {
  // Here, "self" is live:Its value is used in the call to NSLog()
  NSLog(@"I am %d",self);
  // Here, "self" is dead: Its value is never used.
  // I could do self=0 or self=self*self and nobody would know.
  // An optimizing compiler will typically optimize it away.
  // It might still be on the stack (in the parameters passed to NSLog())
  // but the compiler shouldn't assume this.
  NSLog(@"Another function call: %d", 1);
  // If "self" was on the stack, it will probably now have been overwritten.
}

Compilers do a lot of things to make your code faster/smaller; forgetting about variables which are no longer needed is a very common optimization.




回答4:


As already said in this other question:

In Build Settings, setting Precompile Prefix Header to NO fixed it for me.



来源:https://stackoverflow.com/questions/15265949/why-sometimes-self-isnt-available-while-debugging-with-lldb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!