2004-08-19 15:39:10 +02:00
|
|
|
/* libunwind - a platform-independent unwind library
|
2005-05-03 11:13:17 +02:00
|
|
|
Copyright (C) 2002-2004 Hewlett-Packard Co
|
2004-08-19 15:39:10 +02:00
|
|
|
Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
|
|
|
|
|
|
|
|
Modified for x86_64 by Max Asbock <masbock@us.ibm.com>
|
|
|
|
|
|
|
|
This file is part of libunwind.
|
|
|
|
|
|
|
|
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. */
|
|
|
|
|
|
|
|
#include "unwind_i.h"
|
|
|
|
#include <signal.h>
|
2010-03-07 20:53:01 +01:00
|
|
|
|
2010-05-03 16:47:28 +02:00
|
|
|
/* Recognise PLT entries such as:
|
|
|
|
3bdf0: ff 25 e2 49 13 00 jmpq *0x1349e2(%rip)
|
|
|
|
3bdf6: 68 ae 03 00 00 pushq $0x3ae
|
|
|
|
3bdfb: e9 00 c5 ff ff jmpq 38300 <_init+0x18> */
|
|
|
|
static int
|
|
|
|
is_plt_entry (struct dwarf_cursor *c)
|
|
|
|
{
|
|
|
|
unw_word_t w0, w1;
|
|
|
|
unw_accessors_t *a;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
a = unw_get_accessors (c->as);
|
|
|
|
if ((ret = (*a->access_mem) (c->as, c->ip, &w0, 0, c->as_arg)) < 0
|
|
|
|
|| (ret = (*a->access_mem) (c->as, c->ip + 8, &w1, 0, c->as_arg)) < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = (((w0 & 0xffff) == 0x25ff)
|
|
|
|
&& (((w0 >> 48) & 0xff) == 0x68)
|
|
|
|
&& (((w1 >> 24) & 0xff) == 0xe9));
|
|
|
|
|
|
|
|
Debug (14, "ip=0x%lx => 0x%016lx 0x%016lx, ret = %d\n", c->ip, w0, w1, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-08-19 15:39:10 +02:00
|
|
|
PROTECTED int
|
|
|
|
unw_step (unw_cursor_t *cursor)
|
|
|
|
{
|
|
|
|
struct cursor *c = (struct cursor *) cursor;
|
|
|
|
int ret, i;
|
|
|
|
|
2011-03-19 10:00:48 +01:00
|
|
|
#if CONSERVATIVE_CHECKS
|
|
|
|
int val = c->validate;
|
|
|
|
c->validate = 1;
|
|
|
|
#endif
|
|
|
|
|
2010-05-03 16:47:28 +02:00
|
|
|
Debug (1, "(cursor=%p, ip=0x%016lx, cfa=0x%016lx)\n",
|
|
|
|
c, c->dwarf.ip, c->dwarf.cfa);
|
2005-05-03 11:13:17 +02:00
|
|
|
|
2004-08-20 13:23:15 +02:00
|
|
|
/* Try DWARF-based unwinding... */
|
2010-04-20 23:13:09 +02:00
|
|
|
c->sigcontext_format = X86_64_SCF_NONE;
|
2004-08-20 13:23:15 +02:00
|
|
|
ret = dwarf_step (&c->dwarf);
|
2004-08-19 15:39:10 +02:00
|
|
|
|
2011-03-19 10:00:48 +01:00
|
|
|
#if CONSERVATIVE_CHECKS
|
|
|
|
c->validate = val;
|
|
|
|
#endif
|
|
|
|
|
2005-05-03 11:13:17 +02:00
|
|
|
if (ret < 0 && ret != -UNW_ENOINFO)
|
|
|
|
{
|
|
|
|
Debug (2, "returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2004-08-20 13:23:15 +02:00
|
|
|
|
|
|
|
if (likely (ret >= 0))
|
2004-08-19 15:39:10 +02:00
|
|
|
{
|
2004-08-20 13:23:15 +02:00
|
|
|
/* x86_64 ABI specifies that end of call-chain is marked with a
|
|
|
|
NULL RBP. */
|
|
|
|
if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP]))
|
|
|
|
c->dwarf.ip = 0;
|
2004-08-19 15:39:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-05-03 11:13:17 +02:00
|
|
|
/* DWARF failed. There isn't much of a usable frame-chain on x86-64,
|
|
|
|
but we do need to handle two special-cases:
|
|
|
|
|
|
|
|
(i) signal trampoline: Old kernels and older libcs don't
|
|
|
|
export the vDSO needed to get proper unwind info for the
|
|
|
|
trampoline. Recognize that case by looking at the code
|
|
|
|
and filling in things by hand.
|
|
|
|
|
|
|
|
(ii) PLT (shared-library) call-stubs: PLT stubs are invoked
|
|
|
|
via CALLQ. Try this for all non-signal trampoline
|
|
|
|
code. */
|
|
|
|
|
|
|
|
unw_word_t prev_ip = c->dwarf.ip, prev_cfa = c->dwarf.cfa;
|
2004-08-19 15:39:10 +02:00
|
|
|
struct dwarf_loc rbp_loc, rsp_loc, rip_loc;
|
|
|
|
|
2008-06-16 22:35:53 +02:00
|
|
|
/* We could get here because of missing/bad unwind information.
|
|
|
|
Validate all addresses before dereferencing. */
|
|
|
|
c->validate = 1;
|
|
|
|
|
2004-08-20 13:23:15 +02:00
|
|
|
Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
|
|
|
|
|
2010-04-05 15:25:36 +02:00
|
|
|
if (unw_is_signal_frame (cursor))
|
2004-08-20 13:23:15 +02:00
|
|
|
{
|
2010-04-05 01:17:32 +02:00
|
|
|
ret = unw_handle_signal_frame(cursor);
|
2004-08-20 13:23:15 +02:00
|
|
|
if (ret < 0)
|
2005-05-03 11:13:17 +02:00
|
|
|
{
|
2010-04-05 01:17:32 +02:00
|
|
|
Debug (2, "returning 0\n");
|
|
|
|
return 0;
|
2005-05-03 11:13:17 +02:00
|
|
|
}
|
2004-08-20 13:23:15 +02:00
|
|
|
}
|
2010-05-03 16:47:28 +02:00
|
|
|
else if (is_plt_entry (&c->dwarf))
|
|
|
|
{
|
2011-03-19 10:00:48 +01:00
|
|
|
/* Like regular frame, CFA = RSP+8, RA = [CFA-8], no regs saved. */
|
2010-05-03 16:47:28 +02:00
|
|
|
Debug (2, "found plt entry\n");
|
2011-03-19 10:00:48 +01:00
|
|
|
c->frame_info.cfa_reg_offset = 8;
|
|
|
|
c->frame_info.cfa_reg_rsp = -1;
|
|
|
|
c->frame_info.frame_type = UNW_X86_64_FRAME_STANDARD;
|
2010-05-03 16:47:28 +02:00
|
|
|
c->dwarf.loc[RIP] = DWARF_LOC (c->dwarf.cfa, 0);
|
|
|
|
c->dwarf.cfa += 8;
|
|
|
|
}
|
2010-04-20 23:13:18 +02:00
|
|
|
else if (DWARF_IS_NULL_LOC (c->dwarf.loc[RBP]))
|
|
|
|
{
|
|
|
|
for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
|
|
|
|
c->dwarf.loc[i] = DWARF_NULL_LOC;
|
|
|
|
}
|
2004-08-20 13:23:15 +02:00
|
|
|
else
|
|
|
|
{
|
2005-05-03 11:13:17 +02:00
|
|
|
unw_word_t rbp;
|
2004-08-20 13:23:15 +02:00
|
|
|
|
2005-05-03 11:13:17 +02:00
|
|
|
ret = dwarf_get (&c->dwarf, c->dwarf.loc[RBP], &rbp);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2010-04-20 23:13:18 +02:00
|
|
|
Debug (2, "returning %d [RBP=0x%lx]\n", ret,
|
|
|
|
DWARF_GET_LOC (c->dwarf.loc[RBP]));
|
2005-05-03 11:13:17 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rbp)
|
|
|
|
{
|
|
|
|
/* Looks like we may have reached the end of the call-chain. */
|
|
|
|
rbp_loc = DWARF_NULL_LOC;
|
|
|
|
rsp_loc = DWARF_NULL_LOC;
|
|
|
|
rip_loc = DWARF_NULL_LOC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-19 10:00:48 +01:00
|
|
|
unw_word_t rbp1 = 0;
|
2006-07-27 06:04:40 +02:00
|
|
|
rbp_loc = DWARF_LOC(rbp, 0);
|
2005-05-03 11:13:17 +02:00
|
|
|
rsp_loc = DWARF_NULL_LOC;
|
2006-07-27 06:04:40 +02:00
|
|
|
rip_loc = DWARF_LOC (rbp + 8, 0);
|
|
|
|
ret = dwarf_get (&c->dwarf, rbp_loc, &rbp1);
|
2011-03-19 10:00:48 +01:00
|
|
|
Debug (1, "[RBP=0x%lx] = 0x%lx (cfa = 0x%lx) -> 0x%lx\n",
|
|
|
|
(unsigned long) DWARF_GET_LOC (c->dwarf.loc[RBP]),
|
|
|
|
rbp, c->dwarf.cfa, rbp1);
|
|
|
|
|
|
|
|
/* Heuristic to determine incorrect guess. For RBP to be a
|
|
|
|
valid frame it needs to be above current CFA, but don't
|
|
|
|
let it go more than a little. Note that we can't deduce
|
|
|
|
anything about new RBP (rbp1) since it may not be a frame
|
|
|
|
pointer in the frame above. Just check we get the value. */
|
|
|
|
if (ret < 0
|
|
|
|
|| rbp <= c->dwarf.cfa
|
|
|
|
|| (rbp - c->dwarf.cfa) > 0x4000)
|
|
|
|
{
|
|
|
|
rip_loc = DWARF_NULL_LOC;
|
|
|
|
rbp_loc = DWARF_NULL_LOC;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->frame_info.frame_type = UNW_X86_64_FRAME_GUESSED;
|
|
|
|
c->frame_info.cfa_reg_rsp = 0;
|
|
|
|
c->frame_info.cfa_reg_offset = 16;
|
|
|
|
c->frame_info.rbp_cfa_offset = -16;
|
2006-07-27 06:06:48 +02:00
|
|
|
c->dwarf.cfa += 16;
|
2005-05-03 11:13:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark all registers unsaved */
|
|
|
|
for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
|
|
|
|
c->dwarf.loc[i] = DWARF_NULL_LOC;
|
2010-04-05 01:17:32 +02:00
|
|
|
|
|
|
|
c->dwarf.loc[RBP] = rbp_loc;
|
|
|
|
c->dwarf.loc[RSP] = rsp_loc;
|
|
|
|
c->dwarf.loc[RIP] = rip_loc;
|
2004-08-20 13:23:15 +02:00
|
|
|
}
|
2004-08-19 15:39:10 +02:00
|
|
|
|
|
|
|
c->dwarf.ret_addr_column = RIP;
|
|
|
|
|
2010-05-03 16:47:28 +02:00
|
|
|
if (!DWARF_IS_NULL_LOC (c->dwarf.loc[RIP]))
|
2004-08-19 15:39:10 +02:00
|
|
|
{
|
|
|
|
ret = dwarf_get (&c->dwarf, c->dwarf.loc[RIP], &c->dwarf.ip);
|
2006-07-27 06:04:40 +02:00
|
|
|
Debug (1, "Frame Chain [RIP=0x%Lx] = 0x%Lx\n",
|
|
|
|
(unsigned long long) DWARF_GET_LOC (c->dwarf.loc[RIP]),
|
|
|
|
(unsigned long long) c->dwarf.ip);
|
2004-08-19 15:39:10 +02:00
|
|
|
if (ret < 0)
|
2005-05-03 11:13:17 +02:00
|
|
|
{
|
|
|
|
Debug (2, "returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2004-08-19 15:39:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
c->dwarf.ip = 0;
|
2005-05-03 11:13:17 +02:00
|
|
|
|
|
|
|
if (c->dwarf.ip == prev_ip && c->dwarf.cfa == prev_cfa)
|
|
|
|
return -UNW_EBADFRAME;
|
2004-08-19 15:39:10 +02:00
|
|
|
}
|
2005-05-03 11:13:17 +02:00
|
|
|
ret = (c->dwarf.ip == 0) ? 0 : 1;
|
|
|
|
Debug (2, "returning %d\n", ret);
|
|
|
|
return ret;
|
2004-08-19 15:39:10 +02:00
|
|
|
}
|