f4.
f4.

Reputation: 53

Exceptions stacktrace

What's the best way to implement an exception stack trace?

I found some kind of a solution using uncaught_exception() but it requires to add some code to every function.

I need something working on gcc under linux and windows

Upvotes: 4

Views: 6447

Answers (3)

jschmier
jschmier

Reputation: 15796

I'm not sure that a reliable cross-platform method for unwinding the stack exists.

All the platforms/architectures that I've worked on have offered a way to walk the stack when an exception occurs and match addresses to function names. None of these are portable, but the reporting framework can written to be portable with the actual stack walking code remaining platform-specific (StackWalk on Windows or backtrace on Linux).

You might take a look at the libunwind project. I've never used or looked into this myself, so it may not be what you are looking for.

Upvotes: 4

Jeremy Friesner
Jeremy Friesner

Reputation: 73091

I implemented some code that generates the current stack trace as a string; take a look at the GetStackTrace() function that starts at line 1220 of this file if you are interested. The function works under Linux, MacOS/X, and Windows (note that I borrowed the Windows implementation from here, and that it takes an incredible amount of code to implement this feature under Windows.... bleah)

Upvotes: 2

Anne
Anne

Reputation: 480

I don't think there's a cross-platform way to do it. On windows, look at the StackWalk method; on linux, man backtrace. This will get the information; it's up to you to format it.

Upvotes: 4

Related Questions