generating stack traces on UNIX/Linux systems

This is follow up on the previous post for generating stack trace. The code demonstrated earlier holds good for both Linux & Mac OS X. However retrieving filename and line numbers is non-trivial. There is no API that can do it. But it’s hackable.

Here are few ways of doing it:

  • use addr2line for each address and fetch information, this is overkill as for each frame you will have to execute a process
  • code up something similar to addr2line, but Licenses spoil the party. It’s either GPL or APSL. Both are not permissive licenses
  • Look at what LLDB does, but it has it’s own engine and assumes you are debugging, which you may be not
  • Find and read the symbol files. This for sure will be better that others in terms of performance, but then you have to deal with whole lot of debug binary formats (a.out, STABS, DWARF and what not)

For now I am going to stick with what I have for now, it gives me library and function name with offset. Implementing same on windows is relatively easy (maybe a post on that later).


Related Links (click to expand..)