473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Stacktrace code snippet? Like gdb's "bt"

Does anyone know about a code snippet that can be used
to output a backtrace just like gdb's "backtrace" command
does. One that can be called on a segmentation fault.
(without resolving symbol names, just simple list of frames
until the main() stack frame)

Nov 14 '05 #1
3 3223
In article <dx*****************@newsb.telia.net>
eiselekd <ei******@gmx.de> wrote:
Does anyone know about a code snippet that can be used
to output a backtrace just like gdb's "backtrace" command
does. One that can be called on a segmentation fault.
(without resolving symbol names, just simple list of frames
until the main() stack frame)


This cannot be done portably. GDB does it with code that includes
special cases -- as in, code written especially for every single
machine architecture for which gdb does it. There is *some*
code-sharing, e.g., via the "bfd" library, but it really, truly
requires that you write new code every time you port gdb to a new
machine. Sometimes that code is simple, but sometimes it is
horribly complex, and requires cooperation with the compiler.
(In particular, on architectures in which there are different
methods for returning from different function classifications,
one might need to look in different registers or stack locations
or similar depending on the return type and/or "leaf-ness" of
any particular function.)

In a few cases (which one might hope are rare), it cannot be done
at all, because by the time you get something like a "segmentation
fault", the system has removed critical information that the program
that encountered the problem would need. (For a debugger to work
at all on these systems, they have to provide a special, outside-the-
"segmentation-fault" communications channel. Systems with no
debugging facilities at all do exist as well, though.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #2
On Sun, 21 Nov 2004 13:56:09 -0500, eiselekd wrote:
Does anyone know about a code snippet that can be used to output a
backtrace just like gdb's "backtrace" command does. One that can be
called on a segmentation fault. (without resolving symbol names, just
simple list of frames until the main() stack frame)


I think this is what you want:

http://samba.org/ftp/unpacked/junkcode/segv_handler/

But of course I'm obligated to point out that this is *nix specific,
is not ansi C, and is therefore offtopic here.
I use some macros that can build error text in a static buffer. The
result looks like this:

src/module.c:365:module_load: foo.so: cannot open shared object file
src/module.c:405:module_load_all:
src/main.c:114:run:

It's part of my personal "toolbelt" of stuff:

http://www.ioplex.com/~miallen/libmb...ref/msgno.html

I think it's ANSI but it doesn't get called automatically on segv and it
uses variadic macros so you only get the stack trace like effect if you're
using a c library that supports them (e.g. glibc does, MS' does not).

Mike
Nov 14 '05 #3
Found it myself:

#include <stdio.h>
#include <stdlib.h>

#define UNW_LOCAL_ONLY
#include <unwind.h>

void *__libc_stack_end;

struct layout
{
struct layout * next;
void * return_address;
};

int
__backtrace (array, size)
void **array;
int size;
{
/* We assume that all the code is generated with frame pointers set. */
register void *ebp __asm__ ("ebp");
register void *esp __asm__ ("esp");
struct layout *current;
int cnt = 0;

/* We skip the call to this function, it makes no sense to record it. */
current = ((struct layout *) ebp);
while (cnt < size)
{
if ((void *) current < esp || (void *) current > __libc_stack_end)
/* This means the address is out of range. Note that for the
toplevel we see a frame pointer with value NULL which clearly is
out of range. */
break;

array[cnt++] = current->return_address;

current = current->next;
}

return cnt;
}

/* _Unwind_Reason_Code Trace_Fn (struct _Unwind_Context *c, void *arg) */
/* { */
/* printf ("Out\n"); */
/* return _URC_NO_REASON; */
/* } */

/* void print_trace (void) { */

/* _Unwind_Reason_Code r = _Unwind_Backtrace(Trace_Fn,0); */

/* /\*{ */

/* unw_getcontext(&uc); */
/* unw_init_local(&cursor, &uc); */
/* while (unw_step(&cursor) > 0) { */
/* unw_get_reg(&cursor, UNW_REG_IP, &ip); */
/* unw_get_reg(&cursor, UNW_REG_SP, &sp); */
/* printf ("ip = %lx, sp = %lx\n", (long) ip, (long) sp); */
/* }*\/ */
/* } */
/* A dummy function to make the backtrace more interesting. */
void
dummy_function2 (void)
{
void *a[10]; int i = 0;
int cnt = __backtrace (&a, 10);
for (i = 0;i < cnt;i++) {
printf("%x\n",a[i]);
}

}

/* A dummy function to make the backtrace more interesting. */
void
dummy_function1 (void)
{
dummy_function2 ();
}

int main ()
{
/* We assume that all the code is generated with frame pointers set. */
register void *ebp __asm__ ("ebp");
register void *esp __asm__ ("esp");
__libc_stack_end = ((struct layout *) ebp);
dummy_function1 ();
return 0;
}



If glibc is present:
-------------------------------------
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>

/* Obtain a backtrace and print it to stdout. */
void
print_trace (void)
{
void *array[10];
size_t size;
char **strings;
size_t i;

size = backtrace (array, 10);
strings = backtrace_symbols (array, size);

printf ("Obtained %zd stack frames.\n", size);

for (i = 0; i < size; i++)
printf ("%s\n", strings[i]);

free (strings);
}

/* A dummy function to make the backtrace more interesting. */
void
dummy_function (void)
{
print_trace ();
}

int
main (void)
{
dummy_function ();
return 0;
}
-------------------------------------

Chris Torek <no****@torek.net> wrote in message news:<cn*********@news4.newsguy.com>...
In article <dx*****************@newsb.telia.net>
eiselekd <ei******@gmx.de> wrote:
Does anyone know about a code snippet that can be used
to output a backtrace just like gdb's "backtrace" command
does. One that can be called on a segmentation fault.
(without resolving symbol names, just simple list of frames
until the main() stack frame)


This cannot be done portably. GDB does it with code that includes
special cases -- as in, code written especially for every single
machine architecture for which gdb does it. There is *some*
code-sharing, e.g., via the "bfd" library, but it really, truly
requires that you write new code every time you port gdb to a new
machine. Sometimes that code is simple, but sometimes it is
horribly complex, and requires cooperation with the compiler.
(In particular, on architectures in which there are different
methods for returning from different function classifications,
one might need to look in different registers or stack locations
or similar depending on the return type and/or "leaf-ness" of
any particular function.)

In a few cases (which one might hope are rare), it cannot be done
at all, because by the time you get something like a "segmentation
fault", the system has removed critical information that the program
that encountered the problem would need. (For a debugger to work
at all on these systems, they have to provide a special, outside-the-
"segmentation-fault" communications channel. Systems with no
debugging facilities at all do exist as well, though.)

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

57
by: John Howard | last post by:
I've sent several messages over the last year asking about python - Who teaches python? Is python losing steam? etc. I have noticed, eg, the declinng number of books at my local borders. The last...
10
by: Berthold Hoellmann | last post by:
Hello, When I use ./configure --with-thread --with-fpectl --with-signal-module \ --with-pymalloc --enable-shared --with-cxx=g++ make test on 2.3.3 I get
3
by: bt | last post by:
I am just beginning with asp and have gotten an error that I need some help with. I posted a pair of files to an online ASP host server. The files are in the same directory; one is readfile.asp...
9
by: Maksim Kasimov | last post by:
Hello, my programm sometime gives "Segmentation fault" message (no matter how long the programm had run (1 day or 2 weeks). And there is nothing in log-files that can points the problem. My...
3
by: mallyonline | last post by:
I last posted to this group about 6 months ago and was very pleased by the excellent response I got and the great help offered to me. I am back this time with another request for your expert help...
0
by: Aaron Queenan | last post by:
ECMA 334 section 13.4.3 User-defined implicit conversions states that D, the set of types from which user-defined conversion operators will be considered, includes (among other things) the base...
2
by: Gregory S. Williamson | last post by:
Dear peoples, Periodically we are getting runaway postgres processes on our Linux (2.4.21-0.13 on Dell servers), using 7.4 and GIS (0.8 USE_GEOS=1 USE_PROJ=1 USE_STATS=1). All of the queries...
7
by: bookon | last post by:
I was running into the System.Drawing.Image.FromStream "parameter is not valid" on some of the images I was retrieving from a blob column in Sql Server. I thought there were corrupt images as...
1
by: mowsen | last post by:
Hey, simple thing. I try to create a new button which holds a function as "onclick" attribute. I tried different ways, varried here and there but without success. The function in question looks...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.