stack trace
Martin T. wrote:
Thomas Beckmann wrote:
...
And yes the standard library uses them to signal precondition
violations. The point was that debugging a program that uses C++
exceptions to protect preconditions is very difficult. (The MSVC
debugger does an excellent job in this regard though). The Java
exceptions seemed esp designed to provide stack traces, making them
far easier to use in spotting precondition violations.
Why can not I have stack traces in C++? The debugger shows them....
Shame that. Used to think that too. :)
However, at runtime of release code, there is no symbolic information
anymore and all a stacktrace could provide would be a bunch of
addresses. (Well you can use these for post mortem analysis from a
core-dump, but not during runtime to get any useful info.)
Oh. And if you have optimized code the stack trace might not even be
useful then, since it does no longer correspond to the logical stack of
the code ... (Well, at least on MSVC - and that debugger seems to be
quite good - debugging optimized code is horrible.)
While it would be nice I don't think it would be possible without rather
fat standardized reflection features for C++ and we're simply not going
to get these, imho.
I used to have a class like this:
class stack_trace
{
private:
static stack_trace* call_stack_;
stack_trace* next_;
const char* where_;
stack_trace();
stack_trace& operator=(const stack_trace&);
stack_trace(const stack_trace&);
public:
stack_trace(const char *where)
: where_(where)
{
next_ = call_stack_;
call_stack_ = this;
}
~stack_trace()
{
call_stack_ = call_stack_->next;
}
static void dump_stack(std::ostream& os = std::cout)
{
for (stack_trace* p = call_stack_; p != NULL; p = p->next_)
os << p->where_ << "\n";
}
}
stack_trace* stack_trace::call_stack_ = NULL;
Then I could declare a stack_trace variable wherever I needed.
E.g.:
void f()
{
stack_trace("f");
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]