Getting a stack trace

M

milkyway

Hello out there,

When one gets an error, they can use
fprintf (Stdout, "File %s Line %d\n", __FILE__, __LINE__);

Is there a similar way where on can print out the stack trace?

Any help, hints or advice is appreciated ;-)
 
K

Keith Thompson

milkyway said:
Hello out there,

When one gets an error, they can use
fprintf (Stdout, "File %s Line %d\n", __FILE__, __LINE__);

Is there a similar way where on can print out the stack trace?

Any help, hints or advice is appreciated ;-)

There is no way to do this in portable standard C, unless you keep
track of the information yourself by adding code to each function to
maintain a program-wide stack of trace information.

There may be Unix- or Linux-specific ways to do this. If you want to
discuss them, please consider dropping comp.lang.c from the
newsgroups.
 
M

milkyway

Hi,

Will drop but do not know how to do this. Is there a program to collect
the stack trace under Linux

TIA
 
W

Walter Bright

milkyway said:
Will drop but do not know how to do this. Is there a program to collect
the stack trace under Linux

Yes. Replace the assert(expr) with:

if (!(expr)) *(char*)0=0;

Compile with full debug info. Run the program under gdb. When the seg fault
trips, use the 'bt' command which will give you a stack trace. The same
technique works under Windows.

-Walter
www.digitalmars.com free C, C++, D compilers
 
F

Flash Gordon

milkyway said:
Hi,

Will drop but do not know how to do this.

Send an email to whatever support Google provide and ask them how.
Alternatively read whatever instructions they provide.

Whilst you are about it, learn to quote context (it is possibly and
there has been at least one post to comp.lang.c with instructions) and
complain at google for providing such a broken interface.
> Is there a program to collect
the stack trace under Linux

You will have to hope someone on one of the other groups knows, since
comp.lang.c is not the right place.

Follow ups set to exclude comp.lang.c
 
A

Alexei A. Frounze

milkyway said:
Hello out there,

When one gets an error, they can use
fprintf (Stdout, "File %s Line %d\n", __FILE__, __LINE__);

Is there a similar way where on can print out the stack trace?

Any help, hints or advice is appreciated ;-)

If all you want to know is the function call sequence (not the whole stack
contents) that has led to this situation, then there's a solution: you
create your own stack-like structure, into which you can push either a
number or a string (i.e. char*) and then pop it off. Upon entry to each
function you push that function's ID (number or string) to the stack, upon
return you pop it off. At error you just dump the contents of the stack to
stdout, stderr or elsewhere. The dump will contain the exact call
sequence...

Alex
 
D

David Schwartz

milkyway said:
Hi,

Will drop but do not know how to do this. Is there a program to collect
the stack trace under Linux

TIA

There are glibc-specific ways to do this. Look in execinfo.h for
functions like backtrace, backtrace_symbols, and backtrace_symbols_fd.
Helpful as well will be addr2line and similar tools. Google around, now that
you know you are looking for a 'glibc' feature to provide 'backtrace'.

DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top