|
From: LRN <lr...@gm...> - 2011-05-01 21:27:34
|
On 02.05.2011 0:12, TSalm wrote: > Le 01/05/2011 00:26, K. Frank a écrit : >> Hi TSalm! >> >> On Sat, Apr 30, 2011 at 5:55 PM, TSalm<TS...@fr...> wrote: >>> Hi, >>> >>> Is there away to catch an exception and display his stacktrace ? >> I believe that the answer is no, if I understand your question. >> >> Ordinary, built-in c++ exceptions do not take a snapshot of the >> stack or build a stacktrace as they unwind the stack. This is >> by design, as c++ doesn't want to impose on you the cost of >> building and maintaining this stacktrace. >> >> The stack gets unwound between where you throw the exception >> and where you catch it. This can be inconvenient when debugging, >> because if your code throws an exception deep down in some nested >> function calls, seeing the stacktrace (at the time the exception was >> thrown) can help make the bug apparent. (I think java exceptions do >> give you the stacktrace, and this can be very helpful.) >> >> When I'm doing development and debugging, I generally put in assets. >> These cause the program to core dump (or send control back to the >> debugger) right at the point where the error is detected (i.e., the assert >> fails), and then you can look at the state of the stack when the error >> occurred (unless your bug has corrupted the stack...). > I understand. On an other side, why the -g switch doesn't insert > something to manage this stacktrace when an exception occurs ? > There's a difference between C++ exceptions, Windows structured exceptions and segfaults. mingw-backtrace can only hook itself up to segfaults. DrMingw probably too. For C++ exception magic you'd want boost. Dunno, never used it. You DO have the means to get a stack trace. Now you only need to catch an exception, and then generate the trace. I do not know a way to insert a catch-all for any exception that could happen anywhere, so you'd have to catch exceptions manually. And if you catch an exception too late somewhere up the stack - well, you won't get the full stack, just as TSalm told you. Probably. I'm not a C++ guru. |