A quick summary of BreakPoint and LLDB debugging in Xcode

BreakPoint

Specify where to set BreakPoint

Navigate to the line in the code where you want to pause execution by clicking on the number of lines.

Function points in the figure:

  1. Click to start and cancel all added breakpoints. You can use this function if you want to cancel multiple endpoints at once.

  2. Continue executing the program and pause if there is a breakpoint in subsequent code execution;

  3. Step, which is the equivalent of executing code line by line. :

    • Step over instruction (hold Control)
    • Step over thread (hold Control-Shift)
  4. Jump into the implementation of the function, for example, figure 1 calls a test method, look at the internal implementation logic, click to jump to the implementation of the test method in Figure 2; If you want to view the binary file method implementation, you need to have a binary source file, the corresponding source file path should be consistent with the compile time path, details can refer to the United States iOS project zsource command;

    • Step over Instruction (hold Control), can pry system internal call logic, need to understand some assembly knowledge.
    • Step over thread (hold Control-shift), which normally feels the same as above.

    Figure 1:Figure 2:

  5. Jump function, FIG. 3 is jump firsttestAfter the function, click out of the function and go backtouchesBeganThere’s going to be one-[KKObject test]The output. Figure 3:

  6. You can view the UI interface, view some UI level or location offset problems, UI debugging is much more convenient than before;

  7. Current memory analysis;

  8. You can debug some UI items, such as Dark Mode, text size;

  9. Change location information;

  10. View information about all threads in the current APP (process);

  11. View the current thread call stack, you can switch, view historical data;

BreakPoint of the entire program

  1. Swift Error BreakPoint: Can catch errors thrown by Swift. Swift Error and Exception Breakpoints

  2. Exception BreakPoint: Captures objective-C and C++ exceptions, respectively.

    • ALL: indicates ALL exceptions.
    • Objective-c: Exceptions are limited to OC.
    • C++ : exceptions are limited to C++. To stop on a particular C++ exception, you must specify the exception name.
  3. Symbolic BreakPoint adds a Symbolic BreakPoint. For example to look at UILabel set the text property add a breakpoint:

    • Symbol: Can be filled in directlySelector, such as:setText:, to be exact to the method below a class, the format is:[Class Selector], such as:[UILabel setText:];
    • Module: the library where the Module resides. For example, UILabel is in UIKit and can be filled in hereUIKit, of course, can be omitted;
  4. OPENGL ES Error BreakPoint: It can catch errors thrown by OPENGL ES.

  5. Runtime Issue BreakPoint: Can catch errors when a program is running. Here are the official details:

    Xcode has a tool called Sanitizers to detect several different types of runtime problems:

    • Update UI for non-main thread;
    • Update variables when not thread safe;
    • (61) Your accessing addresses are not safe.
    • Executing executing undefined behavior code that results in undefined behavior
      • Try dividing by zero
      • Attempted to load memory with unaligned pointer
      • Try to dereference a NULL pointer
      • The math operation that causes an integer overflow

    You can turn on the Sanitizers tool in a Scheme configuration to detect these problems in static analysis. When the Sanitizers tool is disabled, the application will crash if it encounters any of these problems, and Xcode may not be able to clearly determine where the problem occurred.

    If the Runtime Issue BreakPoint and Undefined Behavior Sanitizer in Scheme are not configured at the same time, an error will be reported directly. If the Runtime Issue BreakPoint and Undefined Behavior Sanitizer are configured at the same time, an error will be reported after the execution. In this way, data from the abnormal scene can be retained.

  6. Constraint Error BreakPoint: Constraint Error BreakPoint allows you to quickly locate automatic layout errors.

  7. Test Failure BreakPoint: a unit Test /UI Test Failure BreakPoint that can be used to quickly locate the cause of a Test Failure

Cancel deleting BreakPoint

When multiple breakpoints are set in multiple source code files, click the Breakpoint Navigator button in the navigation area of the main window to open Breakpoint Navigator to view and manage all breakpoints.

Click the breakpoints TAB in the breakpoints navigator to quickly navigate to breakpoints in the source editor. Selecting the breakpoint label and pressing Delete will remove the breakpoint from your code. Click the breakpoint icon in the breakpoint navigator to enable or disable it.

Configuration BreakPoint

  1. Specifies the condition under which a breakpoint is paused. For bugs that occur after a certain number of iterations, or under limited conditions that require repetition, it can be cumbersome to pause at a breakpoint and press the Continue button repeatedly until a bug occurs. There are two ways to handle such situations more efficiently in the debugger.
    • For bugs that occur after a certain number of iterations, set the debugger to ignore breakpoints for certain iterations. Hold down the Control key to press the breakpoint, select Edit Breakpoint, and then specify how many times the breakpoint should be ignored before stopping.

    • For errors that occur under finite conditions, set the debugger to pause at a breakpoint when the expression is true. Hold down the Control key to click on the breakpoint, select Edit Breakpoint, and then enter the condition using the variable available in the local scope.

    The debugger evaluates the expression every time a breakpoint is reached during execution and pauses only if the expression is true.

2. Configure the action for the breakpoint. Instead of writing code to log variable values and details about application execution, use breakpoint operations to log messages and execute debugger commands that print variable values to the console. For example, if you have a loop that needs to see every variable in it, you can add a breakpoint after the current variable, and then edit the breakpoint to add an Action and perform the Po operation. I don’t have to use NSLog anymore.

Breakpoint operations can also play sound when the debugger reaches a breakpoint, which is useful for knowing when code executes without pausing. Breakpoint operations can execute AppleScripts or shell scripts to perform useful debugging tasks, such as capturing screen shots or saving some application data for analysis.

To perform an action using breakpoints, hold down the Control key in the source editor or breakpoint navigator to click on a breakpoint, select Edit Breakpoint, click Add Action, select an action and provide any additional information necessary. For example, provide messages for log message operations or commands and parameters for debugger command operations.

To perform multiple actions at the breakpoint, click the Add button (+) to the right of the existing action to add another action. To continue an action without pausing, select the Automatically continue after evaluating Actions option.

LLDB debugging

Common LLDB commands

The LLDB Debugger commands are listed here. If you want to learn more About other commands, read The LLDB Debugger, About LLDB and Xcode in detail

Full command Shorthand command describe
expr -O — [SomeClass returnAnObject] po Print the description of objct, which can also be used to execute code
expression e You can change the value of a variable, or you can use it to execute code
thread backtrace bt Displays a stack backtrace for the current thread
thread backtrace all bt all Displays a stack backtrace for all threads
thread backtrace -c 5 bt 5 Displays the stack backtrace for the first five threads of the current thread
run r Start the process instead of Command + R
breakpoint set –name “-[NSString stringWithFormat:]” b -[NSString stringWithFormat:] Add a breakpoint to an OC method [NSString stringWithFormat:]
breakpoint set –selector count br s -S count Add breakpoints to all count methods in OC, which will also add breakpoints to count in C and C++
breakpoint set –regex regular-expression br s -r regular-expression To add a breakpoint to a method using a regular expression
watchpoint set variable var wa s v var Add an observation breakpoint to the var variable that can be triggered when the value changes
watch set var global Setting a condition for the viewing breakpoint is the first step
watchpoint modify -c ‘(global==5)’ Set a conditional second step for the viewing breakpoint
frame variable fr v View all variables under the current frame
frame variable –no-args fr v -a View local variables under the current frame
frame select 12 f 12 View the current thread 12frame
frame info View the frame selected by the current thread
register read View the current thread register
memory read –size 4 –format x –count 4 0xbffff3c0 x/4xw 0xbffff3c0 Read memory from address 0xbffFF3C0 and display four hex uint32_t values.
image list Lists the main executables and all associated shared libraries
image lookup –address 0x1ec4 im loo -a 0x1ec4 Look for information about the original address in the executable or any shared library

LLDB Displays memory information

In LLDB, the x command is used to print the memory value in x/nfu

format.

Example: x/4gx p, printing the value of 4giant Word (8 byet) before P in hexadecimal format.

  • X: Examine
  • N: number of output units.
  • F: Indicates the output format.
    1. xOutput in hexadecimal format
    2. dOutput in base 10
    3. uOutput in unsigned base 10
    4. oOutput in base 8
    5. tOutput in base 2
    6. aOutput in hexadecimal format
    7. iInstruction format address
    8. cOutput in character format
    9. fOutput in floating-point format
  • U: Indicate the length of a unit.
    1. bIs abyte, one byte
    2. hAre the twobyte(halfword)
    3. wIs fourbyte(word)
    4. gIs eightbyte(giant word)

Reference links:

  1. BreakPoint related
    • Developer.apple.com/documentati…
    • Help.apple.com/xcode/mac/1…
    • Developer.apple.com/documentati…
    • Cocoacasts.com/debugging-a…
  2. LLDB
    • Lldb.llvm.org/status/proj…
    • Southpeak. Making. IO / 2015/01/25 /…
    • Developer.apple.com/library/arc…
    • Wizardforcel. Gitbooks. IO / 100 – GDB – tip…
  3. other
    • Tech.meituan.com/2019/08/08/…