2002-02-23 21:27:03 +01:00
|
|
|
/* libunwind - a platform-independent unwind library
|
2003-01-21 18:41:20 +01:00
|
|
|
Copyright (C) 2001-2003 Hewlett-Packard Co
|
2002-02-23 21:27:03 +01:00
|
|
|
Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
|
|
|
|
|
|
|
|
This file is part of libunwind.
|
|
|
|
|
2002-11-16 04:25:36 +01:00
|
|
|
Copyright (c) 2002 Hewlett-Packard Co.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
2002-02-23 21:27:03 +01:00
|
|
|
|
2003-03-19 20:25:18 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2003-01-21 18:41:20 +01:00
|
|
|
#include <errno.h>
|
2003-03-19 20:25:18 +01:00
|
|
|
#if HAVE_EXECINFO_H
|
|
|
|
# include <execinfo.h>
|
|
|
|
#else
|
|
|
|
extern int backtrace (void **, int);
|
|
|
|
#endif
|
2002-04-25 08:47:29 +02:00
|
|
|
#include <signal.h>
|
2002-02-23 21:27:03 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2003-01-21 18:41:20 +01:00
|
|
|
#include <string.h>
|
2002-07-18 05:58:34 +02:00
|
|
|
#include <unistd.h>
|
2002-02-23 21:27:03 +01:00
|
|
|
#include <libunwind.h>
|
|
|
|
|
2003-04-22 01:14:14 +02:00
|
|
|
#if UNW_TARGET_X86
|
2003-11-27 08:17:47 +01:00
|
|
|
# define STACK_SIZE (128*1024) /* On x86, SIGSTKSZ is too small */
|
2003-04-22 01:14:14 +02:00
|
|
|
#else
|
|
|
|
# define STACK_SIZE SIGSTKSZ
|
|
|
|
#endif
|
|
|
|
|
2002-02-23 21:27:03 +01:00
|
|
|
#define panic(args...) \
|
|
|
|
{ fprintf (stderr, args); exit (-1); }
|
|
|
|
|
2003-03-19 20:25:18 +01:00
|
|
|
#ifndef HAVE_SIGHANDLER_T
|
|
|
|
typedef RETSIGTYPE (*sighandler_t) (int);
|
|
|
|
#endif
|
|
|
|
|
2002-02-23 21:27:03 +01:00
|
|
|
static void
|
|
|
|
do_backtrace (void)
|
|
|
|
{
|
2003-02-08 11:10:59 +01:00
|
|
|
char buf[512], name[256];
|
|
|
|
unw_word_t ip, sp, off;
|
2002-02-23 21:27:03 +01:00
|
|
|
unw_cursor_t cursor;
|
2002-12-19 08:16:50 +01:00
|
|
|
unw_proc_info_t pi;
|
2002-02-23 21:27:03 +01:00
|
|
|
unw_context_t uc;
|
2002-12-03 09:19:58 +01:00
|
|
|
int ret;
|
2002-02-23 21:27:03 +01:00
|
|
|
|
|
|
|
unw_getcontext (&uc);
|
|
|
|
if (unw_init_local (&cursor, &uc) < 0)
|
|
|
|
panic ("unw_init_local failed!\n");
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
unw_get_reg (&cursor, UNW_REG_IP, &ip);
|
|
|
|
unw_get_reg (&cursor, UNW_REG_SP, &sp);
|
2003-02-08 11:10:59 +01:00
|
|
|
buf[0] = '\0';
|
|
|
|
if (unw_get_proc_name (&cursor, name, sizeof (name), &off) == 0)
|
|
|
|
{
|
|
|
|
if (off)
|
2003-09-25 07:29:14 +02:00
|
|
|
snprintf (buf, sizeof (buf), "<%s+0x%lx>", name, (long) off);
|
2003-02-08 11:10:59 +01:00
|
|
|
else
|
|
|
|
snprintf (buf, sizeof (buf), "<%s>", name);
|
|
|
|
}
|
2002-12-19 08:16:50 +01:00
|
|
|
printf ("%016lx %-32s (sp=%016lx)\n", (long) ip, buf, (long) sp);
|
2002-02-23 21:27:03 +01:00
|
|
|
|
2002-12-19 08:16:50 +01:00
|
|
|
unw_get_proc_info (&cursor, &pi);
|
2003-04-24 22:45:07 +02:00
|
|
|
printf ("\tproc=%lx-%lx\n\thandler=%lx lsda=%lx gp=%lx",
|
2002-12-19 08:16:50 +01:00
|
|
|
(long) pi.start_ip, (long) pi.end_ip,
|
2003-04-24 22:45:07 +02:00
|
|
|
(long) pi.handler, (long) pi.lsda, (long) pi.gp);
|
2002-12-19 08:16:50 +01:00
|
|
|
|
|
|
|
#if UNW_TARGET_IA64
|
2002-02-23 21:27:03 +01:00
|
|
|
{
|
2002-12-03 09:19:58 +01:00
|
|
|
unw_word_t bsp;
|
2002-02-23 21:27:03 +01:00
|
|
|
|
2002-04-02 01:01:22 +02:00
|
|
|
unw_get_reg (&cursor, UNW_IA64_BSP, &bsp);
|
2002-12-19 08:16:50 +01:00
|
|
|
printf (" bsp=%lx", bsp);
|
2002-02-23 21:27:03 +01:00
|
|
|
}
|
2002-12-19 08:16:50 +01:00
|
|
|
#endif
|
|
|
|
printf ("\n");
|
|
|
|
|
2002-12-03 09:19:58 +01:00
|
|
|
ret = unw_step (&cursor);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
unw_get_reg (&cursor, UNW_REG_IP, &ip);
|
2002-12-19 08:16:50 +01:00
|
|
|
printf ("FAILURE: unw_step() returned %d for ip=%lx\n",
|
|
|
|
ret, (long) ip);
|
2002-12-03 09:19:58 +01:00
|
|
|
}
|
2002-02-23 21:27:03 +01:00
|
|
|
}
|
2002-12-03 09:19:58 +01:00
|
|
|
while (ret > 0);
|
2002-02-23 21:27:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
foo (void)
|
|
|
|
{
|
|
|
|
void *buffer[20];
|
|
|
|
int i, n;
|
|
|
|
|
2003-01-21 18:41:20 +01:00
|
|
|
printf ("\texplicit backtrace:\n");
|
2002-02-23 21:27:03 +01:00
|
|
|
do_backtrace ();
|
|
|
|
|
2003-04-24 22:45:07 +02:00
|
|
|
printf ("\n\tvia backtrace():\n");
|
2002-02-23 21:27:03 +01:00
|
|
|
n = backtrace (buffer, 20);
|
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
printf ("[%d] ip=%p\n", i, buffer[i]);
|
|
|
|
}
|
|
|
|
|
2002-07-18 05:58:34 +02:00
|
|
|
void
|
2003-04-24 22:45:07 +02:00
|
|
|
sighandler (int signal, void *siginfo, void *context)
|
2002-04-25 08:47:29 +02:00
|
|
|
{
|
2003-04-24 22:45:07 +02:00
|
|
|
ucontext_t *uc = context;
|
2003-01-21 18:41:20 +01:00
|
|
|
int sp;
|
|
|
|
|
|
|
|
printf ("sighandler: got signal %d, sp=%p", signal, &sp);
|
2002-12-19 08:16:50 +01:00
|
|
|
#if UNW_TARGET_IA64
|
2003-03-19 20:25:18 +01:00
|
|
|
# if defined(__linux__)
|
2003-04-24 22:45:07 +02:00
|
|
|
printf (" @ %lx", uc->uc_mcontext.sc_ip);
|
|
|
|
# else
|
|
|
|
{
|
|
|
|
uint16_t reason;
|
|
|
|
uint64_t ip;
|
|
|
|
|
|
|
|
__uc_get_reason (uc, &reason);
|
|
|
|
__uc_get_ip (uc, &ip);
|
|
|
|
printf (" @ %lx (reason=%d)", ip, reason);
|
|
|
|
}
|
2003-03-19 20:25:18 +01:00
|
|
|
# endif
|
2002-12-19 08:16:50 +01:00
|
|
|
#elif UNW_TARGET_X86
|
2003-11-27 07:52:54 +01:00
|
|
|
printf (" @ %lx", (unsigned long) uc->uc_mcontext.gregs[REG_EIP]);
|
2002-12-19 08:16:50 +01:00
|
|
|
#endif
|
|
|
|
printf ("\n");
|
2002-04-25 08:47:29 +02:00
|
|
|
|
|
|
|
do_backtrace();
|
|
|
|
}
|
|
|
|
|
2002-02-23 21:27:03 +01:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2003-01-21 18:41:20 +01:00
|
|
|
struct sigaction act;
|
|
|
|
stack_t stk;
|
|
|
|
|
|
|
|
printf ("Normal backtrace:\n");
|
2002-02-23 21:27:03 +01:00
|
|
|
foo ();
|
2002-04-25 08:47:29 +02:00
|
|
|
|
2003-04-24 22:45:07 +02:00
|
|
|
memset (&act, 0, sizeof (act));
|
|
|
|
act.sa_handler = (void (*)(int)) sighandler;
|
|
|
|
act.sa_flags = SA_SIGINFO;
|
|
|
|
if (sigaction (SIGTERM, &act, NULL) < 0)
|
|
|
|
panic ("sigaction: %s\n", strerror (errno));
|
|
|
|
|
2003-01-21 18:41:20 +01:00
|
|
|
printf ("\nBacktrace across signal handler:\n");
|
2002-04-25 08:47:29 +02:00
|
|
|
kill (getpid (), SIGTERM);
|
2003-01-21 18:41:20 +01:00
|
|
|
|
2003-04-24 22:45:07 +02:00
|
|
|
printf ("\nBacktrace across signal handler on alternate stack:\n");
|
2003-04-22 01:14:14 +02:00
|
|
|
stk.ss_sp = malloc (STACK_SIZE);
|
2003-01-21 18:41:20 +01:00
|
|
|
if (!stk.ss_sp)
|
|
|
|
panic ("failed to allocate SIGSTKSZ (%u) bytes\n", SIGSTKSZ);
|
2003-04-22 01:14:14 +02:00
|
|
|
stk.ss_size = STACK_SIZE;
|
2003-01-21 18:41:20 +01:00
|
|
|
stk.ss_flags = 0;
|
|
|
|
if (sigaltstack (&stk, NULL) < 0)
|
|
|
|
panic ("sigaltstack: %s\n", strerror (errno));
|
|
|
|
|
|
|
|
memset (&act, 0, sizeof (act));
|
|
|
|
act.sa_handler = (void (*)(int)) sighandler;
|
2003-04-24 22:45:07 +02:00
|
|
|
act.sa_flags = SA_ONSTACK | SA_SIGINFO;
|
2003-01-21 18:41:20 +01:00
|
|
|
if (sigaction (SIGTERM, &act, NULL) < 0)
|
|
|
|
panic ("sigaction: %s\n", strerror (errno));
|
|
|
|
kill (getpid (), SIGTERM);
|
|
|
|
|
2002-02-23 21:27:03 +01:00
|
|
|
return 0;
|
|
|
|
}
|