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

Screenshots and introductions of Apple Watch built-in apps and third-party apps

Apple Watch excels at quick check of information ...

Who says dads aren't careful when taking care of their kids? Look at this!

The common tern (Sterna hirundo), also known as C...

Squall line is coming! Thunder, lightning, gale and hail are coming!

In recent years, Longyan, Fujian has experienced ...

Will suppliers refuse Apple orders?

According to reports, Apple asked Taiwan's do...

A complete set of live streaming script templates

First put the gains of reading this article in fr...

4 entry points to teach you how to operate vertical products!

Today we are going to talk about vertical product...

A day in the life of a technical manager at Google

[[154079]] I shamelessly wrote this article about...

6 carefully selected black technology apps that can make your phone omnipotent

Here I would like to share 6 carefully selected b...

Apple iOS's closed system has created an unknown gray industry chain

At the end of 2015, with the imprisonment of Xu, ...

Practical methodology to improve SEM bidding promotion effect by 70%!

1. Introduction to SEM bidding Here, I need to sa...