vehomzzz
vehomzzz

Reputation: 44588

How to implement a stacktrace in C++ (from throwing to catch site)?

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?

Upvotes: 1

Views: 1789

Answers (4)

Sebastiaan M
Sebastiaan M

Reputation: 5885

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.

Upvotes: 2

qz-
qz-

Reputation: 1048

As of C++23 std::stacktrace can be used. Prior to that use cpptrace which is simple and portable.

Upvotes: 0

Gunther Piez
Gunther Piez

Reputation: 30439

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

Upvotes: 1

AProgrammer
AProgrammer

Reputation: 52294

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

Upvotes: 1

Related Questions