The most comprehensive iOS breakpoint debugging

As a programmer, coding ability is the basis for survival. Coding is the withdrawal of personal logic. As the saying goes, "a wise man will make a mistake if he thinks about it." Almost no one can guarantee that his program is completely correct. Therefore, in programming work, code debugging tools are extremely important. The debugging tool artifact lldb is integrated in Xcode, which can easily find hidden errors in the code. This article gives a comprehensive introduction to it.

Adding a breakpoint in Xcode is very simple, click on the code line on the left, and a blue label appears, which means adding a breakpoint:

640?wx_fmt=other

 

Next, look at the promotion operation of the breakpoint:

1 ctrl + breakpoint/right-click breakpoint, bring up the breakpoint edit menu:

 

640?wx_fmt=other

Click Edit Breakpoint to bring up the submenu for breakpoint editing:

 

640?wx_fmt=other

 

The Condition at position 1 inputs a Boolean expression to control the triggering condition of the breakpoint; the
position at 2 indicates the number of execution times of ignoring the breakpoint;
the position at 3 is used to add additional actions when the breakpoint is triggered, which is divided into six types: AppleScript, Capture GPU Frame, Debugger Command, Log Message, Shell Command and Sound;
3.1 添加 Log 输出信息:

640?wx_fmt=other

 

The parameters are described as follows:

3.2 添加 lldb 命令:

 

lldb command

640?wx_fmt=other

Commonly used commands include:
1 po: similar to the po command in Console;
2 bt: function call stack information;
3 expression: modify variable value; p / e for short
4 script: enter python script program;
5 facebook supports python language based on lldb Supported features, open source its own more detailed debug project: chisel
6 br / breakpoint list: list all breakpoints;
7 thread backtrace: get the call stack of the current thread;
8 thread list: list all threads;
9 target stop -hook :
9.1 在每次 stop 的时候去执行一些命令;例如:
(lldb) target stop-hook add -o "frame variable”, execute the frame variable command every time it stops;
9.2 target stop-hook list: 列出 hook 的所有断点;
9.3 target stop-hook del 编号: 删除 hook 的断点;
10 command line implements control debugging:

 

c/continue: continue 
next/n: next 
step/s: enter 
finish/f: jump out

You can refer to the Xcode graphical interface to understand:

640?wx_fmt=other

 

1 Enable, disable breakpoint debugging
2 Continue execution
3 Single step execution
4 Enter function
5 Jump out of function
6 UI debugging
7 Select thread

11 thread return: control function return value, directly skip this function;

Add other types of breakpoints:

 

1 Swift Error Breakpoint

Swift global breakpoints are similar to Exception Breakpoint breakpoints.

2 Exception Breakpoint

Objective-C global breakpoints. Add a global breakpoint, for example, when the array is out of bounds, the following information will be printed:

 

***** Terminating app due to uncaught exception 'NSRangeException'**, **reason: '*** -**[**__NSArray0 objectAtIndex:**]**: index 1 beyond bounds for empty NSArray'**

But it cannot accurately locate the specific array position. After adding a global breakpoint, the breakpoint can stop exactly at the out-of-bounds array  .

3 OpenGL ES Error Breakpoint

OpenGL is a unified interface based on C language for 2D/3D graphics programming, which is compatible on desktop windows, Mac, Linux/Unix. OpenGL ES is the version on OpenGL embedded devices, that is, the programming specification for Android/iPhone, other embedded and other mobile devices. (In addition, there is a corresponding WebGL on the web)

Now the OpenGL version currently supported on the iOS platform is available
OpenGL1.0/OpenGL2.0/OpenGL3.0. When using the OpenGL class library in the cocoa framework to draw 2D/3D images, you can use this option to add breakpoints.

4 Symbolic Breakpoint

Add breakpoints based on characters. For example, add the following breakpoint:

640?wx_fmt=other

Breakpoints will be added automatically at all functions named btn_leftClick in the project.

5 Constraint Error Breakpoint

After adding the breakpoint, using Autolayout to deliberately set the constraint violation, nothing was printed. This type of breakpoint is rarely used.

6 Test Failure Breakpoint

Unit test global breakpoints. When added, when a unit test XCAssert assertion fails, stay at the function. At this time, you can use the lldb command p to force the modification condition to meet the assertion, and then continue debugging.

The above is the introduction of various commands that can be applied in iOS code debugging. Among them, the lldb command in the debugging process is introduced in detail. The global breakpoints are classified and introduced, basically covering the scenarios used in daily programming. For more detailed command parameters in lldb and Constraint Error Breakpoint detailed usage, please refer to Apple's official documentation. If you have any questions during use, please leave a message to communicate.

Reference:
1 iOS Xcode Breakpoint (Breakpoint) Debugging 
2 iOS-Breakpoint Setting (Debugger) 
3 Official lldb Documentation
4 iOS Debugging
5 OpenGLES_ProgrammingGuide

Guess you like

Origin blog.csdn.net/KLong27/article/details/106081378