 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
jlom Guest
|
Posted: Sat Aug 16, 2003 8:04 am Post subject: stack trace info in exceptions |
|
|
I'm writing unmanaged code in VC++.NET 2003, and thought that the
existence of std::exception (and subclasses thereof) was going to
provide some access to the stack snapshot at the time the exception is
thrown.
However, there doesn't seem to be anything in those classes that allows
me to do that.
While I've realized that most likely I'll never be able to achieve the
same basic functionality of Java exceptions's printStackTrace if I don't
make symbolic info available (which is not going to happen in production
releases), I'm still quite curious to understand how I could at least
start approaching that goal.
Thanks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Sat Aug 16, 2003 1:45 pm Post subject: Re: stack trace info in exceptions |
|
|
jlom wrote:
| Quote: |
I'm writing unmanaged code in VC++.NET 2003, and thought that the
existence of std::exception (and subclasses thereof) was going to
provide some access to the stack snapshot at the time the exception is
thrown.
However, there doesn't seem to be anything in those classes that allows
me to do that.
While I've realized that most likely I'll never be able to achieve the
same basic functionality of Java exceptions's printStackTrace if I don't
make symbolic info available (which is not going to happen in production
releases), I'm still quite curious to understand how I could at least
start approaching that goal.
|
Unlike Java, C++ does not put a debugger into your application.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Anderson Guest
|
Posted: Sun Aug 17, 2003 12:00 am Post subject: Re: stack trace info in exceptions |
|
|
I once wrote a class: FuncTrace, to do something similar to what you are
looking for.
In the simplest form the object constructor take a string as a parameter
that it save for reporting.
In the destructor you verify if the destruction is due to exception by
checking uncaught_exception().
if so you can use the string saved in the constructor to build a stack
strace.
Now in every function, the first object constructed should be FuncTrace with
the fonction name the parameter.
Some compiler offers function names as a macro.
Danderson
"jlom" <noone (AT) nowhere (DOT) net> a écrit dans le message de news:
[email]3F3E2000.7080200 (AT) nowhere (DOT) net[/email]...
| Quote: |
I'm writing unmanaged code in VC++.NET 2003, and thought that the
existence of std::exception (and subclasses thereof) was going to
provide some access to the stack snapshot at the time the exception is
thrown.
However, there doesn't seem to be anything in those classes that allows
me to do that.
While I've realized that most likely I'll never be able to achieve the
same basic functionality of Java exceptions's printStackTrace if I don't
make symbolic info available (which is not going to happen in production
releases), I'm still quite curious to understand how I could at least
start approaching that goal.
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Eugene Gershnik Guest
|
Posted: Sun Aug 17, 2003 11:48 am Post subject: Re: stack trace info in exceptions |
|
|
jlom wrote:
| Quote: | I'm writing unmanaged code in VC++.NET 2003, and thought that the
existence of std::exception (and subclasses thereof) was going to
provide some access to the stack snapshot at the time the exception
is thrown.
However, there doesn't seem to be anything in those classes that
allows me to do that.
|
It is possible on your particular compiler and platform. To provide a short
answer here, you need to redefine or intercept the compiler-provided
function used to throw exceptions. For more details ask this question on one
of Microsoft's Visual C++ newsgroups or look at
http://www.windevnet.com/documents/s=7547/win0212a/
| Quote: | While I've realized that most likely I'll never be able to achieve
the same basic functionality of Java exceptions's printStackTrace
if I don't make symbolic info available (which is not going to
happen in production releases), I'm still quite curious to
understand how I could at least start approaching that goal.
|
You are right that it is not possible to achieve the _exact_ functionality
of Java exceptions but from practical point of view all you need is to have
all the information required to diagnose a problem.
Eugene
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Aaron Bentley Guest
|
Posted: Mon Aug 18, 2003 10:47 am Post subject: Re: stack trace info in exceptions |
|
|
jlom wrote:
| Quote: | While I've realized that most likely I'll never be able to achieve the
same basic functionality of Java exceptions's printStackTrace if I don't
make symbolic info available (which is not going to happen in production
releases), I'm still quite curious to understand how I could at least
start approaching that goal.
In our project, we have a Context class and a ContextStack Singleton. |
As a variation on "Resource Acquisition Is Instantiation", the Context
constructor registers the object with the ContextStack, and the
destructor deregisters the object with the ContextStack.
The Context object has no other operations.
void randfunc()
{
Context cx1("randfunc()");
for (i=0; i<5; ++i)
{
Context cx2("Loop 1");
}
}
Since cx1 is created at the beginning of randfunc(), it stays in the
stack for the duration of the function. cx2 is constructed and
destroyed during each loop, so it is registered and destroyed 5 times.
This is flexible, but it requires you to scatter Context objects like
breadcrumbs throughout your code.
Aaron
--
Aaron Bentley
www.aaronbentley.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joshua Lehrer Guest
|
Posted: Tue Aug 19, 2003 12:03 am Post subject: Re: stack trace info in exceptions |
|
|
Aaron Bentley <aaron.bentley (AT) utoronto (DOT) ca> wrote
| Quote: | jlom wrote:
While I've realized that most likely I'll never be able to achieve the
same basic functionality of Java exceptions's printStackTrace if I don't
make symbolic info available (which is not going to happen in production
releases), I'm still quite curious to understand how I could at least
start approaching that goal.
In our project, we have a Context class and a ContextStack Singleton.
As a variation on "Resource Acquisition Is Instantiation", the Context
constructor registers the object with the ContextStack, and the
destructor deregisters the object with the ContextStack.
The Context object has no other operations.
void randfunc()
{
Context cx1("randfunc()");
for (i=0; i<5; ++i)
{
Context cx2("Loop 1");
}
}
Since cx1 is created at the beginning of randfunc(), it stays in the
stack for the duration of the function. cx2 is constructed and
destroyed during each loop, so it is registered and destroyed 5 times.
This is flexible, but it requires you to scatter Context objects like
breadcrumbs throughout your code.
|
May I suggest a way to make these even more useful? You can use these
context objects to do debug printing along the way. You can pass a
debug level to the constructor, and it only prints if some global
debugging level exceeds the level of that stamp. Thus, if the global
value is 0, no debug output is printed, and setting it to a high
number causes all stamps to print.
Second, you can have the constructor use a varargs signature, and work
like printf:
void randfunc()
{
Context cx1(1,"randfunc()");
for (i=0; i<5; ++i)
{
Context cx2(2,"Loop %d",i);
}
}
Finally, you can set conditional breakpoints in your code by causing
the context object's constructor to signal the debugger when a given
context object is reached.
We also use context-like objects to control the name of the currently
running thread. That way, when a thread crashes, we know what the
last context object was that was hit.
joshua lehrer
factset research systems
NYSE:FDS
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Tue Aug 19, 2003 12:09 am Post subject: Re: stack trace info in exceptions |
|
|
In article <3F3E2000.7080200 (AT) nowhere (DOT) net>, jlom wrote:
| Quote: |
I'm writing unmanaged code in VC++.NET 2003, and thought that the
existence of std::exception (and subclasses thereof) was going to
provide some access to the stack snapshot at the time the exception is
thrown.
However, there doesn't seem to be anything in those classes that allows
me to do that.
While I've realized that most likely I'll never be able to achieve the
same basic functionality of Java exceptions's printStackTrace if I don't
make symbolic info available (which is not going to happen in production
releases),
|
If you're happy to release Java bytecode with symbolic information (not
that you have a lot of choice), why not C++ code?
| Quote: | I'm still quite curious to understand how I could at least start
approaching that goal.
|
I assume you want stack backtraces for uncaught exceptions, not for all
exceptions. This should be possible but VC++ won't do it because it
unwinds the stack while searching for an exception handler, so by the
time it detects an unhandled exception this information is lost.
If you want to get stack backtraces in can't-happen cases, use assert()
not exceptions. This should abort the program without unwinding the
stack. On a machine without a just-in-time debugger installed, the "Dr
Watson" program should save a dump of the program state which can be
sent to the developer. (On Windows 9x, Dr Watson must be started
beforehand and will save only partial information.) You can load the
dump file into Visual Studio and see the full program state, not just
the stack trace.
Caveats:
1. You need to build every release with symbolic debugging information
and keep that information even if you don't distribute it.
2. This doesn't work so well with Windows 9x.
3. Optimisations result in an unintuitive ordering of operations and
can obscure the call stack, so it may be hard to interpret a dump.
(But the same applies to interactive debugging.)
For information on Dr Watson, see:
<http://support.microsoft.com/default.aspx?scid=kb;en-us;246084>
<http://support.microsoft.com/default.aspx?scid=kb;en-us;275481>
<http://support.microsoft.com/default.aspx?scid=kb;en-us;308538>
Similarly under Unix, a failed assertion normally results in a core
dump (dump file named "core" after an old memory technology).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Shannon Barber Guest
|
Posted: Tue Aug 19, 2003 12:13 am Post subject: Re: stack trace info in exceptions |
|
|
jlom <noone (AT) nowhere (DOT) net> wrote
| Quote: | I'm writing unmanaged code in VC++.NET 2003, and thought that the
existence of std::exception (and subclasses thereof) was going to
provide some access to the stack snapshot at the time the exception is
thrown.
However, there doesn't seem to be anything in those classes that allows
me to do that.
While I've realized that most likely I'll never be able to achieve the
same basic functionality of Java exceptions's printStackTrace if I don't
make symbolic info available (which is not going to happen in production
releases), I'm still quite curious to understand how I could at least
start approaching that goal.
|
It's not as nice as a stack dump, but it's easy to pick-up the line,
file, and code that generates the error using a macro:
#define MKH_THROWONFAILURE(expr)
{
int error=0;
if( (error=(expr)) != 0)
throw MKH::System::Exception(MKH_ERROR(error), #expr);
}
#define MKH_ERROR(err) MKH::System::Error(err, __LINE__, __FILE__)
struct Exception : std::exception
{
Exception(const Error& err, const char* code)
{
make_msg(err, desc);
}
//...
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Markus Werle Guest
|
Posted: Tue Aug 19, 2003 12:38 pm Post subject: Re: stack trace info in exceptions |
|
|
jlom wrote:
| Quote: |
I'm writing unmanaged code in VC++.NET 2003, and thought that the
existence of std::exception (and subclasses thereof) was going to
provide some access to the stack snapshot at the time the exception is
thrown.
However, there doesn't seem to be anything in those classes that allows
me to do that.
While I've realized that most likely I'll never be able to achieve the
same basic functionality of Java exceptions's printStackTrace if I don't
make symbolic info available (which is not going to happen in production
releases), I'm still quite curious to understand how I could at least
start approaching that goal
|
On linux with gdb as debugger one can use
break __cxa_throw (must be the first command!)
and then at the breakpoint the stack is still available
via backtrace
(until gdb supports "catch throw", which was promised to me by
one of the developers and maybe is already in the newest version)
I am pretty sure that similar functionalities are available
with other debuggers on Your desaster platform
(or run it through cygwin://gdb if possible).
If not and if nothing hints at a solution to come then it
is time to say hello to some new platform -
but then You need to manage your code of course ...
Markus
--
Build your own Expression Template Library with Daixtrose!
Visit http://daixtrose.sourceforge.net/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|