【iOS】Bug debugging


Preface

It is inevitable for us to have bugs during our coding process. Sometimes it is very energy-consuming to find these bugs. Debugging can help us find bugs in the program. The author hereby writes the following blog to record the efficient method of using Xcode to find bugs in iOS.

1. Locating compilation errors

Xcode will report the problem to us with a red exclamation mark when we compile or after compilation, so we won’t go into details here.
Insert image description here

2. Set and view breakpoints

When writing code, unexpected results often occur. In order to find out the reasons, we can use breakpoints to help us. The author will mainly introduce three types of breakpoints.
Insert image description here

1. File line breakpoint settings

File line breakpoints are relatively simple. We can directly click on the line number in the file.
Insert image description here
Breakpoints can be deleted, prohibited and used.

When we right-click a breakpoint, we can select Edit Breakpoint, which means the breakpoint edit dialog box , in which we can set the breakpointTrigger conditions and ignore times, and add actions
Insert image description here

For example:
Insert image description here
we want to see i8 o'clock situation, you can set i in Condition8, and then we add a breakpoint to it, and we can
Insert image description here
Insert image description here
get the same effect by setting the number of times in Ignore to 8.
Insert image description here
Insert image description here


2. Symbolic breakpoint setting

Xcode has designed several global breakpoints for us. Symbolic breakpoints are also one of our global breakpoints.
Insert image description here
We can see all breakpoints here.

Insert image description here
Open the "+" panel and we can see all global breakpointsInsert image description here

Symbolic BreakpointThis is our symbolic breakpoint,Symbolic breakpoints pause the program when execution reaches a specific symbol.

After setting the symbol breakpoint, you need to edit it and fill in the symbol in Symbol. The symbols include the following:

The name of the method that will suspend all calls to this symbol. For example: , a method of the class that will suspend program execution removeFromSuperviewas long as it is called . For example: C function. For example:removeFromSuperview·
-[MyView1removeFromSuperview]
c_func

The following three breakpoints are symbolic breakpoints.
Insert image description here
Enter the editing mode: select the symbolic breakpoint set in the breakpoint navigation panel, right-click and select Edit Breakpointto enter the editing panel below.
Insert image description here

3.Exception Breakpoint

Our program often crashes directly in the main function.
Insert image description here
When encountering this kind of problem, we need aException Breakpoint

For example, I have an array out-of-bounds problem. When I add this breakpoint, it will automatically help me locate the array location where the problem occurs.
Insert image description here
We can also add it to the action of the breakpoint po $arg1to print out the information about the exception object.
Insert image description here
Here It is recommended to always execute this breakpoint when the program is running, which will help save our time.

4.Constraint Error Breakpoint

An automatic layout constraint error breakpoint can help you quickly locate automatic layout errors, but it is not commonly used and will be used when we use Masonry for automatic layout.
When there are constraint errors between our controls, this breakpoint will be automatically identified, and by looking at the stack we can see which control has the problem.
Insert image description here
Insert image description here
In the above program, the author deliberately _replyconstrained the controls incorrectly. Looking at the stack, I found that It automatically helps us find the control with the wrong constraint


3. Debugging Tools

Xcode provides powerful debugging functions. When the breakpoint is suspended, the debugging interface will be entered.
Insert image description here

This is the debugging toolbar:
Insert image description here
There are many buttons in the debugging toolbar:
for example, when the breakpoint is suspended, click the continue execution button to continue execution .
The single-step skip button is for single-step execution . When encountering methods and functions, it does not enter
single-step entry. The button is to enter the method or function . date = [self.homeModel pastDateForJson:numbersOfLoad];In this code, if you click on the single-step into the program, you will enter pastDateForJsonthe method. If you click on the single-step skip , you will not enter
the single-step out button. After entering the method or function, you want to jump back to the original call. used when

4. Output window

Use the window display button to control both left and right windows (variable viewing window and output window) to be displayed at the same time, or only one of the windows to be displayed. There are three options for the output window - All Output, Debugger Output and Target Output.
When debugging a program, you can execute debugging commands in the Debugger Output window.
The Target Output window displays some output and error messages.
Insert image description here

5. Variable viewing window

Located on the left side of the debugging window, it is used to view the contents of variables and registers . It is the same as the output window and has a variety of options.
Insert image description here

Insert image description here
At the same time, right-clicking on a variable can also perform various operations on the variable.
Insert image description here
For example, if we click on the printf variable, the following output will appear in the output window.
Insert image description here

6. View threads

There are two ways in Xcode to support us in viewing threads.
One is to select the thread drop-down list in the jump bar. After selecting a thread, Xcode will display a stack of code running
Insert image description here
. The other method is to click on the caterpillar to also display the thread. with its stack
Insert image description here

7. LLDB debugging tools

The methods we used above can actually be done using LLDB debugging tools. Here I will briefly talk about a few commonly used LLDB commands.

1.p, po command

p command: View the value of basic data type
po command: View oc object
Insert image description here
Insert image description here

2. expr command

expr command: the full name is expression. It can dynamically modify the value of variables during debugging and print out the results at the same time . Using the expr command to dynamically modify the value of a variable can cover some exception paths during debugging , which is very useful for debugging exception handling code.
Insert image description here

3.bt command

The bt command can view the stack information of the thread, which can also be seen in the Debug Navigator in the navigation area;
bt: prints the current thread stack bt all: prints all thread stacks

May be used when learning multi-threading
Insert image description here

There are some more convenient commands that we can learn by typing help in lldb .
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_72437555/article/details/134607750