Assuming you have appropriate permissions, you can have gdb attach to any process. You can do it on the command line with:
gdb /path/to/binary _pid_
or from within gdb with the attach command:
attach _pid_
So, once your daemon has started, you can use either of these techniques to attach to the final PID your daemon is running as. Attaching gdb stops the process which you are tracing so you will need to issue a "continue" to restart it.
I don't know a direct way to get gdb to run arbitrary commands when the program crashes. Here is one workaround I can think of:
- Create and register a signal handlers for SIGSEGV.
- Tell gdb not to stop on that signal (
handle SIGSEGV nostop
)
- Set a breakpoint at the first line of your signal handler.
- Assign commands to the breakpoint from step 3