PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > print stack...
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
print stack...

Réponse
 
LinkBack Outils de la discussion
Vieux 16/10/2007, 18h55   #1
call_me_anything
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut print stack...

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)

  Réponse avec citation
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
Vieux 16/10/2007, 20h49   #3
Gianni Mariani
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: print stack... -ot-

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)



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.
  Réponse avec citation
Vieux 16/10/2007, 20h57   #4
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: print stack...

On Oct 16, 7:55 pm, call_me_anything <sgiitne...@gmail.com> 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)


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:james.kanze@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

  Réponse avec citation
Vieux 16/10/2007, 21h03   #5
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: print stack...

On Oct 16, 8:20 pm, red floyd <no.s...@here.dude> wrote:
> call_me_anything wrote:


[...]
> > 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:


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: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();
> }


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:james.kanze@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

  Réponse avec citation
Vieux 17/10/2007, 07h56   #6
red floyd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: print stack...

James Kanze wrote:
> On Oct 16, 8:20 pm, red floyd <no.s...@here.dude> wrote:
>> call_me_anything wrote:

>
> [...]
>>> 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)

>[redacted]
> 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.
>


1. You're right, it does require the TRACE macro and skips those that don't.

2. You're right. I wrote the original version years ago in a single
threaded environment. However, if I *really* wanted to be pedantic, I'd
note that multithreaded environments are OT in comp.lang.c++... :-).
  Réponse avec citation
Vieux 17/10/2007, 08h17   #7
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: print stack...

On Oct 17, 8:56 am, red floyd <no.s...@here.dude> wrote:
> James Kanze wrote:
> > On Oct 16, 8:20 pm, red floyd <no.s...@here.dude> wrote:
> >> call_me_anything wrote:


> > [...]
> >>> 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)

> >[redacted]
> > 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.


> 1. You're right, it does require the TRACE macro and skips
> those that don't.


> 2. You're right. I wrote the original version years ago in a
> single threaded environment. However, if I *really* wanted to
> be pedantic, I'd note that multithreaded environments are OT
> in comp.lang.c++... :-).


Neither of which, of course, means that it isn't useful. As
long as you know and understand its limitations. It's a lot,
lot simpler than the stack walkback that I use, which requires a
new implementation for every platform.

--
James Kanze (GABI Software) email:james.kanze@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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h20.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15757 seconds with 15 queries