Skip to content
systemcoding.dev
Debugging

Run commands when a breakpoint hits with breakpoint command add

Attach commands to an LLDB breakpoint with breakpoint command add, ending the list with DONE, to log or continue without stopping.

1 min read
Summarize in AI

Like GDB's commands, breakpoint command add stores commands that run when a breakpoint hits. In an interactive session it reads lines until you type DONE. Ending the list with continue makes the breakpoint a tracepoint: it logs and keeps going.

Text
(lldb) breakpoint set -n handle_request
Breakpoint 1: where = app`handle_request + 4 at srv.c:52:5, address = 0x0000000100003e40
(lldb) breakpoint command add 1
Enter your debugger command(s).  Type 'DONE' to end.
> frame variable fd
> bt 3
> continue
> DONE
(lldb) run
(int) fd = 4
  * frame #0: 0x0000000100003e40 app`handle_request(fd=4) at srv.c:52:5
    frame #1: 0x0000000100003f10 app`serve + 40 at srv.c:80:9
(int) fd = 5

Without the final continue the process stops after the commands run. The commands can be LLDB commands, or a Python function with -s python -F module.func, which receives the frame and the breakpoint location and returns False to keep running.

For a one-line version there is breakpoint command add -o 'p fd', which takes a single command. Use breakpoint command list 1 to see what is stored and breakpoint command delete 1 to remove it. A common mistake is typing continue outside a breakpoint command block, where it resumes at once. Each hit costs a stop and resume, so a breakpoint on a hot function slows the program a lot. See breakpoint commands, LLDB 19.

Share

More snippets

Systems Programming

Get the page size with sysconf(_SC_PAGESIZE)

Read the memory page size at run time with sysconf(_SC_PAGESIZE) instead of hard-coding 4096, which is wrong on arm64 kernels built with 16K or 64K pages.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.