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.
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.
(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 = 5Without 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.