Discussion: print stack...
Afficher un message
Vieux 16/10/2007, 19h20   #2
red floyd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: print stack...

call_me_anything wrote:
> Here is a code to have a debug printf :
>
> #ifdef DEBUG
> #define DEBUG_printf(...)
> {printf("[%s]:",__FUNCTION__)rintf(__VA_ARGS__)rintf("\n"); }
> #else
> #define DEBUG
> #endif
>
> int main () {
> DEBUG_printf ("%d %s", 5, "abc");
> }
>
> Is there something similar, which can print the current stack ?
> I mean some C/C++ API which can me get any info related to the
> current stack of functions.
> Thats is required for debugging.
> (Please do not suggest gdb... I want something similar to gdb stack
> traces but that should print stack info everytime I compile with DEBUG
> on)
>


Not built in, but it's fairly easy to add:


#include <ostream>
class stack_tracer
{
public:
stack_tracer(const char *where) : where_(where), next_(top())
{
top() = this;
}
~stack_tracer()
{
top() = next_;
}
static std:stream& dump(std:stream& os)
{
for (stack_tracer* curr = top();
curr != NULL;
curr = curr->next_)
{
os << curr->where_ << '\n';
}
return os;
}
private:
static stack_tracer*& top()
{
static stack_tracer* top_ = 0;
return top_;
}
stack_tracer *next_;
const char *where_;
};

#define TRACE3(mark, ln, txt) stack_tracer mark ## ln ## _(txt)
#define TRACE2(ln, txt) TRACE3(st_, ln , txt)
#define TRACE(txt) TRACE2(__LINE__,txt)

#include <iostream>

int f()
{
TRACE("f");
if (true)
{
TRACE("If block in f()");
stack_tracer::dump(std::cout) << std::endl;
}
}

int main()
{
TRACE("main");
stack_tracer::dump(std::cout) << std::endl;
f();

}
  Réponse avec citation
 
Page generated in 0,05024 seconds with 9 queries