00000000b804e424 +0xbfef0f18 (sp=00000000bf87281c)
0000000008048504 main+0xb (sp=00000000bf872860)
00000000b7f14b86 __libc_start_main+0xe6 (sp=00000000bf872870)
void do_backtrace(pid_t target_pid)
{
unw_word_t ip, sp, start_ip = 0, off;
int n = 0, ret;
unw_proc_info_t pi;
unw_cursor_t c;
char buf[512];
size_t len;
fprintf(stderr, "Target pid is %lu\n", (unsigned long)target_pid);
as = unw_create_addr_space (&_UPT_accessors, 0);
if (!as)
panic ("unw_create_addr_space() failed");
ui = _UPT_create (target_pid);
ret = unw_init_remote (&c, as, ui);
if (ret < 0)
panic ("unw_init_remote() failed: ret=%d\n", ret);
do
{
if ((ret = unw_get_reg (&c, UNW_REG_IP, &ip)) < 0
|| (ret = unw_get_reg (&c, UNW_REG_SP, &sp)) < 0)
panic ("unw_get_reg/unw_get_proc_name() failed: ret=%d\n", ret);
if (n == 0)
start_ip = ip;
buf[0] = '\0';
if (print_names)
unw_get_proc_name (&c, buf, sizeof (buf), &off);
if (off)
{
len = strlen (buf);
if (len >= sizeof (buf) - 32)
len = sizeof (buf) - 32;
sprintf (buf + len, "+0x%lx", (unsigned long)off);
}
printf ("%016lx %-32s (sp=%016lx)\n", (long) ip, buf, (long) sp);
if ((ret = unw_get_proc_info (&c, &pi)) < 0)
panic ("unw_get_proc_info(ip=0x%lx) failed: ret=%d\n", (long) ip, ret);
else printf ("\tproc=%016lx-%016lx\n\thandler=%lx lsda=%lx\n",
(long) pi.start_ip, (long) pi.end_ip,
(long) pi.handler, (long) pi.lsda);
#if UNW_TARGET_IA64
{
unw_word_t bsp;
if ((ret = unw_get_reg (&c, UNW_IA64_BSP, &bsp)) < 0)
panic ("unw_get_reg() failed: ret=%d\n", ret);
else printf (" bsp=%lx", bsp);
}
#endif
ret = unw_step (&c);
if (ret < 0)
{
unw_get_reg (&c, UNW_REG_IP, &ip);
panic ("FAILURE: unw_step() returned %d for ip=%lx (start ip=%lx)\n",
ret, (long) ip, (long) start_ip);
}
if (++n > 64)
{
/* guard against bad unwind info in old libraries... */
panic ("too deeply nested---assuming bogus unwind (start ip=%lx)\n",
(long) start_ip);
break;
}
}
while (ret > 0);
if (ret < 0)
panic ("unwind failed with ret=%d\n", ret);
printf ("\n\n================\n\n");
_UPT_destroy (ui);
}