Thursday, July 24, 2008

Tracing a Daemon

I've recently needed to run a daemon under strace, in an attempt to figure out what was making it fail.

Tracing a running process is simple enough - just attach strace to it:
strace -p <pid>
(replace <pid> with the process id). But in this case I wanted to start the daemon under strace, which is a bit tricky.

A daemon is typically (always?) started from an init script located in /etc/init.d/ via a program called start-stop-daemon, e.g.
start-stop-daemon --start --exec $DAEMON -- $ARGS

where $DAEMON is the executable being launched and $ARGS are its command line arguments (optional).

The idea is to replace that line in the init script with something else that launches an strace-d daemon, that can later be stopped with start-stop-daemon. I've tried various combinations of start-stop-daemon, strace and $DAEMON, before I hit the following incantation:
start-stop-daemon --oknodo --start --exec $DAEMON --background --startas /usr/bin/strace -- -f -o /tmp/$NAME.strace $DAEMON $ARGS

This will trace the executable and any of its forked child processes (-f) to a file named /tmp/$NAME.strace.

Note that a daemon may be started in several places in the init script ("start" and "restart").

No comments:

Post a Comment