strace(1) is an amazing tool that you can use to debug processes that went south when nothing else helps.
At it’s core, strace simply runs a command and prints out all system calls executed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
Now, that’s quite a bit of information: you will see each and every system call that’s been run in order to execute your command, with arguments (up to a certain length) and the exit code.
How is this useful? Well, in order to debug a running process you can simply strace it by its pid:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
(note that $!
is the pid of the previous process, it’s just a magic shell variable)
Now we’re talking! Remember that process that inexplicably hangs after running for a couple minutes? Let’s run it, then find its pid and strace it on the fly.
You can even trace child processes and even threads with the -f
option, so
that you can literally follow anything your parent process triggers — just this
week this turned out handy for me since I needed to debug an android app running
on an emulated device, which can be easily done through something like:
1 2 3 4 5 6 7 8 9 |
|
Remember: when you think there’s nothing left to try, strace(1) will always have your back.