“If you usually work with non-trivial C sources, you may have
wondered which execution path (that is, which sequence of function
calls) brought you to a certain point in your program. Also, it
would be even more useful if you could have that piece of
information whenever your beautiful, bug-free program suddenly
crashes, and you have no debugger at hand. What is needed is a
stack backtrace and, thanks to a little known feature of the GNU C
library, obtaining it is a fairly easy task.“Before diving into the article, let’s briefly go over how
function calls and parameters pass work in C. In order to prepare
for the function call, parameters are pushed on the stack in
reverse order. Afterwards, the caller’s return address also is
pushed on the stack and the function is called. Finally, the called
function’s entry code creates some more space on the stack for
storage of automatic variables. This layout commonly is called a
stack frame for that particular instance of the function call. When
more function calls are nested, the whole procedure is repeated,
causing the stack to keep growing downwards and building a chain of
stack frames… Thus, at any given point in a program it
theoretically is possible to backtrace the sequence of stack frames
to the originating calling point, up to the main() function (to be
exact, up to the libc function, which calls main() when the process
starts up)…”
Linux Journal: Stack Backtracing Inside Your Program
By
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends, & analysis