blob: abc9e613f8fea3c893e10deede8e42977dbbe0d4 [file] [log] [blame]
homeip.net!davidm588192d2004-08-17 15:34:28 +00001/* libunwind - a platform-independent unwind library
2 Copyright (C) 2002 Hewlett-Packard Co
David Mosberger-Tange6b9f352007-08-22 13:02:09 -06003 Copyright (C) 2007 David Mosberger-Tang
4 Contributed by David Mosberger-Tang <dmosberger@gmail.com>
homeip.net!davidm588192d2004-08-17 15:34:28 +00005
6This file is part of libunwind.
7
8Permission is hereby granted, free of charge, to any person obtaining
9a copy of this software and associated documentation files (the
10"Software"), to deal in the Software without restriction, including
11without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense, and/or sell copies of the Software, and to
13permit persons to whom the Software is furnished to do so, subject to
14the following conditions:
15
16The above copyright notice and this permission notice shall be
17included in all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
26
27#include <stdlib.h>
28#include <string.h>
29
30#include "unwind_i.h"
31
32#ifdef UNW_REMOTE_ONLY
33
34/* unw_local_addr_space is a NULL pointer in this case. */
35PROTECTED unw_addr_space_t unw_local_addr_space;
36
37#else /* !UNW_REMOTE_ONLY */
38
39static struct unw_addr_space local_addr_space;
40
41PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
42
43static inline void *
44uc_addr (ucontext_t *uc, int reg)
45{
46 void *addr;
47
48 switch (reg)
49 {
50 case UNW_X86_GS: addr = &uc->uc_mcontext.gregs[REG_GS]; break;
51 case UNW_X86_FS: addr = &uc->uc_mcontext.gregs[REG_FS]; break;
52 case UNW_X86_ES: addr = &uc->uc_mcontext.gregs[REG_ES]; break;
53 case UNW_X86_DS: addr = &uc->uc_mcontext.gregs[REG_DS]; break;
54 case UNW_X86_EAX: addr = &uc->uc_mcontext.gregs[REG_EAX]; break;
55 case UNW_X86_EBX: addr = &uc->uc_mcontext.gregs[REG_EBX]; break;
56 case UNW_X86_ECX: addr = &uc->uc_mcontext.gregs[REG_ECX]; break;
57 case UNW_X86_EDX: addr = &uc->uc_mcontext.gregs[REG_EDX]; break;
58 case UNW_X86_ESI: addr = &uc->uc_mcontext.gregs[REG_ESI]; break;
59 case UNW_X86_EDI: addr = &uc->uc_mcontext.gregs[REG_EDI]; break;
60 case UNW_X86_EBP: addr = &uc->uc_mcontext.gregs[REG_EBP]; break;
61 case UNW_X86_EIP: addr = &uc->uc_mcontext.gregs[REG_EIP]; break;
62 case UNW_X86_ESP: addr = &uc->uc_mcontext.gregs[REG_ESP]; break;
63 case UNW_X86_TRAPNO: addr = &uc->uc_mcontext.gregs[REG_TRAPNO]; break;
64 case UNW_X86_CS: addr = &uc->uc_mcontext.gregs[REG_CS]; break;
65 case UNW_X86_EFLAGS: addr = &uc->uc_mcontext.gregs[REG_EFL]; break;
66 case UNW_X86_SS: addr = &uc->uc_mcontext.gregs[REG_SS]; break;
67
68 default:
69 addr = NULL;
70 }
71 return addr;
72}
73
74# ifdef UNW_LOCAL_ONLY
75
76HIDDEN void *
77tdep_uc_addr (ucontext_t *uc, int reg)
78{
79 return uc_addr (uc, reg);
80}
81
82# endif /* UNW_LOCAL_ONLY */
83
84HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
85
86/* XXX fix me: there is currently no way to locate the dyn-info list
87 by a remote unwinder. On ia64, this is done via a special
88 unwind-table entry. Perhaps something similar can be done with
89 DWARF2 unwind info. */
90
91static void
92put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
93{
94 /* it's a no-op */
95}
96
97static int
98get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
99 void *arg)
100{
101 *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
102 return 0;
103}
104
105static int
106access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
107 void *arg)
108{
109 if (write)
110 {
111 Debug (16, "mem[%x] <- %x\n", addr, *val);
112 *(unw_word_t *) addr = *val;
113 }
114 else
115 {
116 *val = *(unw_word_t *) addr;
117 Debug (16, "mem[%x] -> %x\n", addr, *val);
118 }
119 return 0;
120}
121
122static int
123access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
124 void *arg)
125{
126 unw_word_t *addr;
127 ucontext_t *uc = arg;
128
129 if (unw_is_fpreg (reg))
130 goto badreg;
131
homeip.net!davidm588192d2004-08-17 15:34:28 +0000132 if (!(addr = uc_addr (uc, reg)))
133 goto badreg;
134
135 if (write)
136 {
137 *(unw_word_t *) addr = *val;
138 Debug (12, "%s <- %x\n", unw_regname (reg), *val);
139 }
140 else
141 {
142 *val = *(unw_word_t *) addr;
143 Debug (12, "%s -> %x\n", unw_regname (reg), *val);
144 }
145 return 0;
146
147 badreg:
148 Debug (1, "bad register number %u\n", reg);
149 return -UNW_EBADREG;
150}
151
152static int
153access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
154 int write, void *arg)
155{
156 ucontext_t *uc = arg;
157 unw_fpreg_t *addr;
158
159 if (!unw_is_fpreg (reg))
160 goto badreg;
161
162 if (!(addr = uc_addr (uc, reg)))
163 goto badreg;
164
165 if (write)
166 {
167 Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
168 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
169 *(unw_fpreg_t *) addr = *val;
170 }
171 else
172 {
173 *val = *(unw_fpreg_t *) addr;
174 Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
175 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
176 }
177 return 0;
178
179 badreg:
180 Debug (1, "bad register number %u\n", reg);
181 /* attempt to access a non-preserved register */
182 return -UNW_EBADREG;
183}
184
185static int
186get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
187 char *buf, size_t buf_len, unw_word_t *offp,
188 void *arg)
189{
David Mosberger-Tange6b9f352007-08-22 13:02:09 -0600190 return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
homeip.net!davidm588192d2004-08-17 15:34:28 +0000191}
192
193HIDDEN void
194x86_local_addr_space_init (void)
195{
196 memset (&local_addr_space, 0, sizeof (local_addr_space));
197 local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
198 local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
199 local_addr_space.acc.put_unwind_info = put_unwind_info;
200 local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
201 local_addr_space.acc.access_mem = access_mem;
202 local_addr_space.acc.access_reg = access_reg;
203 local_addr_space.acc.access_fpreg = access_fpreg;
204 local_addr_space.acc.resume = x86_local_resume;
205 local_addr_space.acc.get_proc_name = get_static_proc_name;
206 unw_flush_cache (&local_addr_space, 0, 0);
207}
208
209#endif /* !UNW_REMOTE_ONLY */