0

Is the trickery way that we can show the entire stack trace (function+line) for an exception, much like in Java and C#, in C++?

Can we do something with macros to accomplish that for windows and linux-like platforms?

thanks

vehomzzz
  • 42,832
  • 72
  • 186
  • 216
  • 1
    Do you mean a stacktrace for an exception thrown or a general stacktrace (usable for, e.g., log messages etc.)? – sbi Aug 12 '09 at 13:52
  • @sbi, in both cases the answer is the same :-) – AProgrammer Aug 12 '09 at 13:56
  • See http://stackoverflow.com/questions/616653/portable-c-stack-trace-on-exception or http://stackoverflow.com/questions/564177/implementing-a-stack-trace-on-windows. – Cătălin Pitiș Aug 12 '09 at 13:59

3 Answers3

2

Not without either platform specific knowledge or addition of code in each function.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
2

On Windows it can be done using the Windows DbgHelp API, but to get it exactly right requires lots of experimenting and twiddling. See http://msdn.microsoft.com/en-us/library/ms679267(VS.85).aspx for a start. I have no idea how to implement it for other platforms.

Hope this helps a bit.

Regards,

Sebastiaan

Sebastiaan M
  • 5,775
  • 2
  • 27
  • 28
1

If you are running on a platform which uses glibc, you can use the backtrace() functions. This are C functions, but they do work for c++ back traces too. This is of course not portable, but I doubt you will find a portable solution without additional code in every function ;-)

http://www.gnu.org/software/libc/manual/html_node/Backtraces.html

Gunther Piez
  • 29,760
  • 6
  • 71
  • 103