Monday, April 13, 2009

stacktrace


One of the quickest ways to debug code is to be able to tell how you got to a certain point in your code. So it is useful to be able to dump a backtrace/stacktrace at any point in your code.

I think you can find really good resources elsewhere to understand what a program stack is (for example this or this), so I won´t get into it. As a side note, those two links are some of the best ones I have found to understanding program memory and the difference between program heap and stack.

In C/C++:

This article gives really good information.
In summary, glibc provides backtrace() and backtrace_symbols() to do this quickly.

In JAVA:

Use the very useful Throwable class with your logging.

Example:
Throwable t = new Throwable();
Log.getStackTraceString(t));

No comments: