When a program runs, it stores its execution information in the call stack. Each time a method is called, a new stack frame is added to the top of the call stack. Each stack frame contains the parameters passed to the method, information about the local variables of the method, and information about the address to jump to when the method returns.

When the program stops at a breakpoint, the debugger checks the state of the current stack frame. Stack state is a good way to analyze method behavior and how methods interact with other parts of the program.

Gets information about the current stack frame

frame info

Through the frame Info command, you can obtain the position of the code corresponding to the current frame stack, including the source file information and line number information.

(lldb) frame info
frame #0: 0x00000001000034cf Greeter`Greeter.greet(name="Mei", self=0x0000000100205bf0) at Greeter.swift:9:12
Copy the code

Checking variable information

frame variable(f v)

Through the frame Variable directive. Retrieves all variable information on the current frame stack.

(lldb) frame variable
(String) name = "Anton"
(Greeter.Greeter) self = 0x0000000100502920 {
  acquaintances = ([0] = "Anton")
}
Copy the code

Execute expression

expression

Executing expressions is a powerful feature of LLDB. With this expression, you can change the stored variable information, thus changing the results of the program.

(lldb) expression -- acquaintances.insert("Mei")
(lldb) expression -- acquaintances.remove("Anton")
(String?) $R1 = "Anton"
Copy the code

frame variable(f v)expressionThe comparison of

frame variable (f v) expression -- (p) expression -O -- (po)
You don’t actually run the code The code actually runs The code actually runs
Output in LLDB format Output in LLDB format Use custom formats (such as Swift’sdebugDescriptionIf no custom format is available, LLDB format will be used for output

Expression Runs expressions, which may affect the program. For safe and fast debugging, use frame variable

Get the thread call stack

The call stack shows the current method call structure. With Thread Backtrace (BT), you can follow the call stack to see how the program changes step by step to its current state by calling methods.

(lldb) thread backtrace
* thread #1: tid = 0x1288be3, 0x0000000100001a98 Greeter`Greeter.hasMet(name="Anton", self=0x0000000101200190) -> Bool + 24 at Greeter.swift:5, queue = 'com.apple.main-thread', stop reason = step in
    frame #0: 0x0000000100001a98 Greeter`Greeter.hasMet(name="Anton", self=0x0000000101200190) -> Bool + 24 at Greeter.swift:5
  * frame #1: 0x0000000100001be4 Greeter`Greeter.greet(name="Anton", self=0x0000000101200190) -> () + 84 at Greeter.swift:9
    frame #2: 0x00000001000019eb Greeter`main + 155 at Greeter.swift:20
    frame #3: 0x00007fff949d05ad libdyld.dylib`start + 1
    frame #4: 0x00007fff949d05ad libdyld.dylib`start + 1
Copy the code

You can pass in an integer parameter to control the number of frames displayed

The call stack of all threads can be obtained by using Thread Backtrace all

Get thread list

thread list

(lldb) thread list
Process 96461 stopped
* thread #1: tid = 0x1384af1, 0x000000010000111f main`sayHello() -> () + 15 at main.swift:2, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
Copy the code

Resources: Examining the Call Stack