By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,161 Members | 1,132 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,161 IT Pros & Developers. It's quick & easy.

print stack...

P: n/a
Here is a code to have a debug printf :

#ifdef DEBUG
#define DEBUG_printf(...)
{printf("[%s]:",__FUNCTION__);printf(__VA_ARGS__);printf("\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 help 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)

Oct 16 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
call_me_anything wrote:
Here is a code to have a debug printf :

#ifdef DEBUG
#define DEBUG_printf(...)
{printf("[%s]:",__FUNCTION__);printf(__VA_ARGS__);printf("\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 help 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::ostream& dump(std::ostream& 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();

}
Oct 16 '07 #2

P: n/a
call_me_anything wrote:
Here is a code to have a debug printf :

#ifdef DEBUG
#define DEBUG_printf(...)
{printf("[%s]:",__FUNCTION__);printf(__VA_ARGS__);printf("\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 help 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)

There are platform specific mechanisms that allow you to get a stack
trace. The Austria C++ "netcabletv" alpha contains a stack trace API
that works on win32 and linux.
Oct 16 '07 #3

P: n/a
On Oct 16, 7:55 pm, call_me_anything <sgiitne...@gmail.comwrote:
Here is a code to have a debug printf :
#ifdef DEBUG
#define DEBUG_printf(...)
{printf("[%s]:",__FUNCTION__);printf(__VA_ARGS__);printf("\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 help 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)
Nothing standard. In fact, any solution will be very platform
dependent. (My site has a stack trace class with
implementations for i80x86, 64 bit AMD, and 32 and 64 bit
Sparcs. But you still have to pay attention---changing the
compiler options can change it as well. Still, it might be a
start.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 16 '07 #4

P: n/a
On Oct 16, 8:20 pm, red floyd <no.s...@here.dudewrote:
call_me_anything wrote:
[...]
Is there something similar, which can print the current stack ?
I mean some C/C++ API which can help 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:
For what platform? Which compiler? What compile options?

It's very, very implementation dependent.
#include <ostream>
class stack_tracer
{
public:
stack_tracer(const char *where) : where_(where), next_(top())
{
top() = this;
}
~stack_tracer()
{
top() = next_;
}
static std::ostream& dump(std::ostream& 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();
}
Your implementation has two major problems: it requires that
every function use the TRACE macro, it skips functions which
don't use the macro; and it fails radically in a multi-thread
environment.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 16 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.