问题
I have done it like this but it always give the error:
error: use of undeclared identifier 'self'
error: 1 errors parsing expression
回答1:
I don't believe that you can access any symbolic information from a breakpoint inside the UIKit framework, because there is no debugging information available. If you set a breakpoint and allow the debugger to open you will see you are just presented with some assembler code and a set of registers.
You can set a breakpoint for a specific UIViewController class in your application -
You could add a thin shim UIViewController subclass and then subclass all of your view controllers from it. This would then allow you to specify a single symbolic breakpoint in the shim -
#import "BreakPointingViewController.h"
@implementation BreakPointingViewController
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
@end
Then add the symbolic breakpoint in -[BreakPointingViewController viewWillAppear:]
回答2:
If you know a little about how ObjC method dispatch works, you can also do this by printing the appropriate argument. There are always two artificial arguments to an ObjC method call, the first of which is the "self" variable, the second the "selector"; and then explicit arguments follow.
On arm, arm64 and x86_64 where arguments are passed in registers, it's easy to get your hands on these values by looking in the appropriate registers, even without debug info.
To make this even easier, lldb defines the register aliases "$arg1", "$arg2" which are just other names for the registers that the current ABI uses to pass in the first, second, etc arguments. So if you've stopped at the beginning of a function (so the arguments are still in the registers by which they were passed into the function) you can use these names in an ABI independent way.
The point of that somewhat long-winded discussion is that at the beginning of an objc method call, $arg1 will hold the self parameter. So you can put:
po $arg1
in your breakpoint command, and that will work in place of
po self
in cases where you have no debug information.
Note, the reason that I stress that $arg1 is not the value of the first argument but the argument passing register is if, having stopped, you try to go up the stack and print self in some higher frame using this same trick, it will fail. The incoming arguments to already executing frames are almost never left in the incoming registers (after all, those registers had to be reused to call the younger functions.) But this will work at the beginning of execution of the function.
来源:https://stackoverflow.com/questions/25235271/how-to-log-out-self-when-add-a-symbol-breakpoint-at-uiviewcontroller-viewwilla