Discussion:
Stack trace from C program
(too old to reply)
Sad Clouds
2010-05-25 11:43:49 UTC
Permalink
Hi, is there a sane way of generating a stack trace from withink a C
program on *BSD?

For example, Solaris has walkcontext() and printstack() functions. It
doesn't look like there is anything similar on NetBSD.

Is there a portable way of implementing those functions, or does it
heavily depend on machine architecture and operating system?

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Wouter Klouwen
2010-05-25 12:14:51 UTC
Permalink
Post by Sad Clouds
Hi, is there a sane way of generating a stack trace from withink a C
program on *BSD?
For example, Solaris has walkcontext() and printstack() functions. It
doesn't look like there is anything similar on NetBSD.
glibc also has something to print a stack trace, but NetBSD has it's own
libc.

See: http://www.linuxjournal.com/article/6391
Post by Sad Clouds
Is there a portable way of implementing those functions, or does it
heavily depend on machine architecture and operating system?
Basically, no and yes. It is very compiler, libc and machine architecture
dependent.
The LJ article has a brief discussion on why exactly it's an extremely non
portable thing to do.

--Wouter

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Sad Clouds
2010-05-25 12:35:14 UTC
Permalink
On Tue, 25 May 2010 12:14:51 +0000
Post by Wouter Klouwen
Basically, no and yes. It is very compiler, libc and machine
architecture dependent.
The LJ article has a brief discussion on why exactly it's an
extremely non portable thing to do.
--Wouter
OK, thanks for the link. I think I'll go with #ifdef in my code and
call backtrace functions on platforms that support it, i.e. Linux or
Solaris.

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Jeremy C. Reed
2010-05-25 12:59:00 UTC
Permalink
Post by Sad Clouds
Hi, is there a sane way of generating a stack trace from withink a C
program on *BSD?
For example, Solaris has walkcontext() and printstack() functions. It
doesn't look like there is anything similar on NetBSD.
Is there a portable way of implementing those functions, or does it
heavily depend on machine architecture and operating system?
You may want to look at the recent BIND 9 source:
lib/isc/backtrace.c
lib/isc/include/isc/backtrace.h
(even in NetBSD src/external/bsd/bind/dist/)

It has comments and embedded docs explaining it. It provides examples
using backtrace() and gcc's Unwind_Backtrace() (on x86_64 or IA64). So
yes it is machine dependent.

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Aleksey Cheusov
2010-05-25 13:46:44 UTC
Permalink
Post by Sad Clouds
Hi, is there a sane way of generating a stack trace from withink a C
program on *BSD?
Have a look at wip/libstacktrace library.
It has the API similar to glibc's backtrace(3).
It works fine on {Free,Net,Open,DF,Mir}BSD, Solaris and Linux.

At the moment it depends on gcc's extensions:
__builtin_return_address and __builtin_frame_address.
--
Best regards, Aleksey Cheusov.

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Continue reading on narkive:
Loading...