Pranav Kasetti
Pranav Kasetti

Reputation: 9915

How to call Swift function with breakpoint from LLDB?

I know a Swift function by name and wanted to call the function from LLDB for debugging purposes.

I tried doing:

expr -- function_with_a_breakpoint()

This runs the function and parses correctly but doesn't hit the breakpoints I made. Ideally, I want to trigger from within LLDB without having to manually trigger the function from the app UI, for example. This would be more convenient.

Upvotes: 4

Views: 860

Answers (1)

Pranav Kasetti
Pranav Kasetti

Reputation: 9915

Swift functions with breakpoints in LLDB 💥

I've added this answer with bonus notes since we usually trigger a Swift function from LLDB when we want to reach a breakpoint.

I enjoy working within LLDB for managing breakpoints because I don't break my flow between Xcode and the console.

Trigger a Function 🔫

The answer to your question is here. We have to set the -i flag to trigger a function from LLDB:

(lldb) expr -i 0 -- function_with_a_breakpoint()

Programmatic breakpoints

We commonly use a function trigger from LLDB to trigger a breakpoint manually.

If we want to set a breakpoint on a function and we know the name:

(lldb) breakpoint set -n function_with_a_breakpoint

Using the file name and line number:

(lldb) breakpoint set --file foo.swift --line 12
// copy the breakpoint number into the modify command below

We can also create the breakpoint as a one-off using the --one-shot flag.

(lldb) breakpoint modify --one-shot 1

Bonus Workflow Notes 🍀

An example flow:

(lldb) run
(lldb) kill
... hit breakpoints...
(lldb) continue

So we could trigger the function after kill in the above example.

Upvotes: 3

Related Questions