blob: c1fff6163d6992053b2a76cd0386bb5ba24fc638 [file] [log] [blame]
homeip.net!davidm3dab98e2004-08-17 15:34:28 +00001/* libunwind - a platform-independent unwind library
mostang.com!davidmfa0828a2005-05-03 09:13:17 +00002 Copyright (C) 2002-2004 Hewlett-Packard Co
homeip.net!davidm3dab98e2004-08-17 15:34:28 +00003 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of libunwind.
6
7Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
Konstantin Belousove9cd3002010-03-13 20:28:12 +020026#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif
29
30#include <sys/types.h>
31#include <signal.h>
Konstantin Belousove9cd3002010-03-13 20:28:12 +020032
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000033#include "unwind_i.h"
34#include "offsets.h"
35
36PROTECTED int
37unw_step (unw_cursor_t *cursor)
38{
39 struct cursor *c = (struct cursor *) cursor;
Konstantin Belousove9cd3002010-03-13 20:28:12 +020040 int ret, i, format;
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000041
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000042 Debug (1, "(cursor=%p, ip=0x%08x)\n", c, (unsigned) c->dwarf.ip);
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000043
44 /* Try DWARF-based unwinding... */
45 ret = dwarf_step (&c->dwarf);
46
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000047 if (ret < 0 && ret != -UNW_ENOINFO)
48 {
49 Debug (2, "returning %d\n", ret);
50 return ret;
51 }
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000052
53 if (unlikely (ret < 0))
54 {
55 /* DWARF failed, let's see if we can follow the frame-chain
56 or skip over the signal trampoline. */
57 struct dwarf_loc ebp_loc, eip_loc;
58
Arun Sharmaff0ae702009-03-16 11:06:26 -070059 /* We could get here because of missing/bad unwind information.
60 Validate all addresses before dereferencing. */
61 c->validate = 1;
62
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000063 Debug (13, "dwarf_step() failed (ret=%d), trying frame-chain\n", ret);
64
65 if (unw_is_signal_frame (cursor))
Arun Sharma8e53e622010-04-04 16:17:32 -070066 {
67 ret = unw_handle_signal_frame(cursor);
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000068 if (ret < 0)
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000069 {
70 Debug (2, "returning 0\n");
71 return 0;
72 }
Arun Sharma8e53e622010-04-04 16:17:32 -070073 }
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000074 else
75 {
76 ret = dwarf_get (&c->dwarf, c->dwarf.loc[EBP], &c->dwarf.cfa);
77 if (ret < 0)
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000078 {
79 Debug (2, "returning %d\n", ret);
80 return ret;
81 }
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000082
homeip.net!davidm57426422004-08-19 12:26:11 +000083 Debug (13, "[EBP=0x%x] = 0x%x\n", DWARF_GET_LOC (c->dwarf.loc[EBP]),
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000084 c->dwarf.cfa);
85
86 ebp_loc = DWARF_LOC (c->dwarf.cfa, 0);
87 eip_loc = DWARF_LOC (c->dwarf.cfa + 4, 0);
88 c->dwarf.cfa += 8;
mostang.com!davidmfa0828a2005-05-03 09:13:17 +000089
90 /* Mark all registers unsaved, since we don't know where
91 they are saved (if at all), except for the EBP and
92 EIP. */
93 for (i = 0; i < DWARF_NUM_PRESERVED_REGS; ++i)
94 c->dwarf.loc[i] = DWARF_NULL_LOC;
Arun Sharma8e53e622010-04-04 16:17:32 -070095
96 c->dwarf.loc[EBP] = ebp_loc;
97 c->dwarf.loc[EIP] = eip_loc;
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000098 }
homeip.net!davidm3dab98e2004-08-17 15:34:28 +000099 c->dwarf.ret_addr_column = EIP;
100
101 if (!DWARF_IS_NULL_LOC (c->dwarf.loc[EBP]))
102 {
103 ret = dwarf_get (&c->dwarf, c->dwarf.loc[EIP], &c->dwarf.ip);
104 if (ret < 0)
mostang.com!davidmfa0828a2005-05-03 09:13:17 +0000105 {
106 Debug (2, "returning %d\n", ret);
107 return ret;
108 }
homeip.net!davidm3dab98e2004-08-17 15:34:28 +0000109 }
110 else
111 c->dwarf.ip = 0;
112 }
mostang.com!davidmfa0828a2005-05-03 09:13:17 +0000113 ret = (c->dwarf.ip == 0) ? 0 : 1;
114 Debug (2, "returning %d\n", ret);
115 return ret;
homeip.net!davidm3dab98e2004-08-17 15:34:28 +0000116}