1. Xcode has built-in GDB. You can use GDB to debug and debug the command:
1.1 po command: the abbreviation of print object, showing the text description of the object
(Lldb) po [$ eax class]: Address of the output exception object
(Lldb) po [$ eax name]: output the name of this exception.
(Lldb) po [$ eax reason]: This will output the error message: Success:
(Lldb) "po $ eax": Call the "description" method for this object and print it out.
"$ Eax" is a register of the cup. In the case of an exception, this register will contain a pointer to the exception object. Note: $ eax only works in the simulator. If you Debug on the device, you will need to use the "$ r0" register.
1.2 print command: similar to formatting output, which can output different object information
For example: print (char *) [[dic description] cString], (lldb) print (int) [label retainCount]
1.3 info command: we can view the information of the memory address.
1.4 info line * Memory Address: obtains the code line information of the memory address.
1.5 show command: displays information about GDB. For example, show version displays the GDB version.
1.6 bt: displays the function call stack of the current process; "up num": displays the call details; down: returns the stack list; l: displays the detailed code information; p: output value.
2. Add Exception BreakPoint ):
2.1 Add steps:
1. In the bottom-left corner of the breakpoints navigator, click the Add button.
2. Choose Add Exception Breakpoint.
3. Choose the type of exception from the Exception pop-up menu.
4. Choose the phase of the exception handling process at which you want program execution to stop.
5. Click Done.
2.2 Use Cases:
The program crash because of SIGABRT and wants to locate the line that causes the crash.
3. Add Symbolic BreakPoint ):
3.1 timing of breakpoint execution: Symbolic breakpoints stop program execution when a specific function or method starts executing
3.2 add steps:
1. Steps In the bottom-left corner of the breakpoint navigator, click the Add button.
2. Choose Add Symbolic Breakpoint.
3. Enter the symbol name in the Symbol field.
4. Click Done.
3.3 Use Cases:
When you want the system to interrupt a specified condition, set the corresponding breakpoint.
For example:
Objc_exception_throw: Set a breakpoint when the system throws an exception.
-[NSException raise]:
4. Set NSZombieEnabled, MallocStackLogging, nyoutoreleasefreedobjectcheckenabled, and NSDebugEnabled:
4.1 setting method:
1. Product-> Edit Scheme...-> Run...-> EnvironmentVariables.
2. add NSZombieEnabled, set the value with YES
3. add MallocStackLogging, set the value with YES.
4. add NSAID or eleasefreedobjectcheckenabled, set the value with YES.
5. add NSDebugEnabled, set the value with YES.
4.2 Use Cases:
To solve the problem of EXC_BAD_ACCESS, MallocStackLogging is used to enable malloc records (using malloc_history $ {App_PID }$ {Object_instance_addr }).
4.3 notes
NSZombieEnabled can only be used during debugging. Do not forget to remove it during product release because NSZombieEnabled does not actually release the memory of the dealloc object.
5. Override the respondsToSelector method.
5.1 Implementation Method
[Cpp]
# Ifdef _ FOR_DEBUG _
-(BOOL) respondsToSelector :( SEL) aSelector {
Printf ("SELECTOR: % s \ n", [NSStringFromSelector (aSelector) UTF8String]);
Return [super respondsToSelector: aSelector];
}
# Endif
5.2 usage:
The. m or. add the above Code to the mm file (which can be implemented by class), and add-D _ FOR_DEBUG _ to other c flags (remember to add this mark only under Debug Configuration ). In this way, when your program crashes, the Xcode console will accurately record the method of the last running object.
References:
1. Xcode GDB debugging: http://blog.csdn.net/ch_soft/article/details/7005998
2. Some XCode debugging skills: http://blog.csdn.net/kesalin/article/details/7222153
3. About the Breakpoint Navigator: http://developer.apple.com/library/mac/#recipes/xcode_help-breakpoint_navigator/articles/about_breakpoint_navigator.html#//apple_ref/doc/uid/TP40010433-CH6-SW1
4. What should I do when the program crashes part-1: http://article.ityran.com/archives/1006
5. What should I do when the program crashes Part-2: http://article.ityran.com/archives/1143
6. Memory Usage Performance Guidelines: https://developer.apple.com/library/mac/#documentation/performance/Conceptual/ManagingMemory/ManagingMemory.html#//apple_ref/doc/uid/10000160-SW1