Common iOS debugging methods: breakpoint debugging

Common iOS debugging methods: breakpoint debugging

Tricks Guide

In the process of iOS project development, static analysis (Analyze), breakpoints (BreakPoint) and console (Console) are often used for code debugging. This article introduces "breakpoint debugging", a common debugging method in Xcode.

[[262842]]

1. Introduction

Xcode's breakpoint function is a common function for iOS developers. Click on the left column in the code editing area to create a breakpoint. Every time the program runs to the breakpoint, it will pause, making it easier for developers to debug. As shown below:

At the same time, developers can use the tools marked with numbers in the figure above to assist breakpoint debugging. The functions of each tool are as follows:

  • Enable/disable breakpoints (click to turn gray, all breakpoints disabled; click again to turn blue, all breakpoints enabled)
  • Continue to execute the program (click to skip this breakpoint and continue to execute the program)
  • Execute the next step (click to execute line 37 of code)
  • Entry method (click to enter -afunction method)
  • Jump out of the method (click inside the -afunction method to return to line 36 of the code)

In addition to the basic usage of breakpoints, developers can also use advanced breakpoint features: edit breakpoints, exception breakpoints, and symbolic breakpoints.

2. Edit breakpoints

Double-click a breakpoint or right-click it and select Edit Breakpoint to open the breakpoint editing page. The page contains four parts: Condition, Ignore, Action, and Options. Let's introduce them one by one.

1. Condition

Condition is a condition. Developers can set the conditions for triggering breakpoints in the Condition input box. For example, in a for loop, you can set the breakpoint to be triggered only when i==5 is satisfied. As shown below:

2. Ignore

Ignore is the number of times to ignore. Developers can set the number of times to ignore this breakpoint in the Igore input box. For example, if Ignore is set to 5, the breakpoint will be triggered only when the breakpoint line is executed for the 6th time.

3. Action

Action is the trigger action. You can add multiple Actions. After the breakpoint is triggered, the set Actions will be executed immediately. Action has 6 execution types, among which the more commonly used ones are Debugger Command and Log message, as shown below:

1) Debugger Command

Debugger Command allows developers to set some LLDB commands to implement functions such as printing objects and modifying variables. As shown below:

2) Log Message

Log Message provides a standard text output format. Developers can set the content to be output after the breakpoint is triggered according to the format. Among them, @exp@ indicates the content of output exp, %B indicates the name of the output breakpoint, and %H indicates the number of times the output breakpoint is executed. As shown below:

4. Options

Options controls whether to automatically continue executing the program after executing the corresponding Actions of the breakpoint. After checking Options, the Debug interface will not be entered after the breakpoint is triggered.

3. Abnormal breakpoints (global breakpoints)

Developers can easily create an exception breakpoint in Xcode. As shown below:

After creating an exception breakpoint, a breakpoint named All Exceptions will be displayed, so it is often called a global breakpoint. When the program throws an exception, the exception breakpoint will be triggered, and most common errors will be located at the corresponding line of code by the breakpoint, which is very convenient for debugging. For example: after setting a global breakpoint, we write a piece of code that exceeds the bounds of an array. When running this code, the breakpoint will be set to the line that exceeds the bounds. As shown below:

When the global breakpoint is removed and the program is run again, the out-of-bounds code is triggered and the breakpoint is set to main.m, but the out-of-bounds line is not located. As shown below:

4. Symbolic breakpoints

Symbolic Breakpoint is a global breakpoint that can be set for a method (function). Developers can easily create a symbolic breakpoint, as shown below:

Compared with ordinary breakpoints, the editing interface of symbolic breakpoints has two more input boxes: Symbol and Module.

1. Symbol

Developers can set breakpoints to trigger methods/functions in the Symbol input box. As shown in the figure below, after setting a method/function in the Symbol, the breakpoint will be triggered when the program is run and the method is executed.

Here we should pay attention to the writing of methods/functions in Symbol:

  • Method (OC method): Use + to indicate a class method, and - to indicate an instance method. Parameter declarations cannot be omitted, and there cannot be a space after :;
  • Function: (C function): Just write the function name.

2. Module

Developers can set the library where the function in the Symbol is located in the Module input box to avoid methods/functions with the same name in different libraries. By default, this field does not need to be filled in.

3. Condition and other functions

The usage of the Condition function in the symbol breakpoint editing page, except for Symbol and Module, is the same as that of ordinary breakpoints. For example, if you set $arg3==nil && $arg4==nil in the Condition input box, the breakpoint will be triggered only when the first and second parameters are both nil. As shown below:

Summarize

Xcode's breakpoint debugging function is very powerful. Developers can make full use of breakpoint debugging and combine it with rich LLDB debugging commands to improve development efficiency to a great extent.

[This article is an original article from 51CTO columnist 360 Technology, WeChat public account "360 Technology (id: qihoo_tech)"]

Click here to read more articles by this author

<<:  Samsung Galaxy Fold review: An imperfect folding screen, a promising new form factor

>>:  iOS 13 will also add a dark mode. Is the era of "everyone in black" coming?

Recommend

Farewell, Skoda

If a car brand with sentiment and history fails t...

Douyin operation: practical tips for Douyin shooting!

1. What is TikTok? Some people say that Douyin is...

The most comprehensive case analysis of event planning and operation strategies

Today I’d like to talk to you about how to run a ...

Five suicide attempts in 20 years: Windows Phone is completely finished

Windows Phone has completely failed. On May 23, Mi...

How to write popular notes on Xiaohongshu?

"Hey, my notes have had poor traffic recentl...

Laxatives are poisonous. How can I poop comfortably?

Constipation is a common problem among adults. Co...

New plant species discovered in Medog, Tibet!

Recently, researchers discovered a new species of...

Discussing WeChat’s O2O path from the collapse of Dingdong Community

This article Heart of Rock puts forward several c...

Android Market - OPPO Application Market ASO Detailed Explanation

What I want to talk about today is the whole oper...

The first course of the iP Evolution Wealth Camp of Tanhuo Video Account

The first issue of Tanhuo’s video account iP Evol...