I am debugging a program that is short-lived. That is, it won't stop and wait for the lldb/gdb debugger to attach. So how can I attach lldb debugger to this kind of process?
2 Answers
The traditional approach is to have a sleep loop, or if you have access to the source, to put a busyloop like
int wait_for_debugger = 1
while (wait_for_debugger)
;
Then you attach to the process and set wait_for_debugger to 0 (e.g. p wait_for_debugger = 0) and continue the process.
lldb has a --waitfor option to attach to a process. This has lldb repeatedly poll the process table looking for a new instance of that process name and attaches to it. The process will get to execute a little bit but the polling frequency is high enough that it usually catches things early enough. This is the process launch --waitfor --name procname command, or pr la -w -n processname for short.
Comments
So how can I attach lldb debugger to this kind of process?
Attach the debugger to the parent process, and set follow-fork-mode child. According to the docs, "The new process is debugged after a fork. The parent process runs unimpeded". See 4.11 Debugging Forks in the GDB manual.
gdb