blob: e1b1dcfca7b2c810c893bb8690702e76c13d5751 [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
Arun Sharmaff0ae702009-03-16 11:06:26 -0700105#define PAGE_SIZE 4096
106#define PAGE_START(a) ((a) & ~(PAGE_SIZE-1))
107
108/* Cache of already validated addresses */
109#define NLGA 4
110static unw_word_t last_good_addr[NLGA];
111static int lga_victim;
112
113static int
114validate_mem (unw_word_t addr)
115{
116 int i, victim;
117
118 addr = PAGE_START(addr);
119
120 for (i = 0; i < NLGA; i++)
121 {
122 if (last_good_addr[i] && (addr == last_good_addr[i]))
123 return 0;
124 }
125
126 if (msync ((void *) addr, 1, MS_SYNC) == -1)
127 return -1;
128
129 victim = lga_victim;
130 for (i = 0; i < NLGA; i++) {
131 if (!last_good_addr[victim]) {
132 last_good_addr[victim++] = addr;
133 return 0;
134 }
135 victim = (victim + 1) % NLGA;
136 }
137
138 /* All slots full. Evict the victim. */
139 last_good_addr[victim] = addr;
140 victim = (victim + 1) % NLGA;
141 lga_victim = victim;
142
143 return 0;
144}
145
homeip.net!davidm588192d2004-08-17 15:34:28 +0000146static int
147access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
148 void *arg)
149{
150 if (write)
151 {
152 Debug (16, "mem[%x] <- %x\n", addr, *val);
153 *(unw_word_t *) addr = *val;
154 }
155 else
156 {
Arun Sharmaff0ae702009-03-16 11:06:26 -0700157 /* validate address */
158 const struct cursor *c = (const struct cursor *)arg;
159 if (c && c->validate && validate_mem(addr))
160 return -1;
homeip.net!davidm588192d2004-08-17 15:34:28 +0000161 *val = *(unw_word_t *) addr;
162 Debug (16, "mem[%x] -> %x\n", addr, *val);
163 }
164 return 0;
165}
166
167static int
168access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
169 void *arg)
170{
171 unw_word_t *addr;
Arun Sharmaff0ae702009-03-16 11:06:26 -0700172 ucontext_t *uc = ((struct cursor *)arg)->uc;
homeip.net!davidm588192d2004-08-17 15:34:28 +0000173
174 if (unw_is_fpreg (reg))
175 goto badreg;
176
homeip.net!davidm588192d2004-08-17 15:34:28 +0000177 if (!(addr = uc_addr (uc, reg)))
178 goto badreg;
179
180 if (write)
181 {
182 *(unw_word_t *) addr = *val;
183 Debug (12, "%s <- %x\n", unw_regname (reg), *val);
184 }
185 else
186 {
187 *val = *(unw_word_t *) addr;
188 Debug (12, "%s -> %x\n", unw_regname (reg), *val);
189 }
190 return 0;
191
192 badreg:
193 Debug (1, "bad register number %u\n", reg);
194 return -UNW_EBADREG;
195}
196
197static int
198access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
199 int write, void *arg)
200{
Arun Sharmaff0ae702009-03-16 11:06:26 -0700201 ucontext_t *uc = ((struct cursor *)arg)->uc;
homeip.net!davidm588192d2004-08-17 15:34:28 +0000202 unw_fpreg_t *addr;
203
204 if (!unw_is_fpreg (reg))
205 goto badreg;
206
207 if (!(addr = uc_addr (uc, reg)))
208 goto badreg;
209
210 if (write)
211 {
212 Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
213 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
214 *(unw_fpreg_t *) addr = *val;
215 }
216 else
217 {
218 *val = *(unw_fpreg_t *) addr;
219 Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
220 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
221 }
222 return 0;
223
224 badreg:
225 Debug (1, "bad register number %u\n", reg);
226 /* attempt to access a non-preserved register */
227 return -UNW_EBADREG;
228}
229
230static int
231get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
232 char *buf, size_t buf_len, unw_word_t *offp,
233 void *arg)
234{
David Mosberger-Tange6b9f352007-08-22 13:02:09 -0600235 return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
homeip.net!davidm588192d2004-08-17 15:34:28 +0000236}
237
238HIDDEN void
239x86_local_addr_space_init (void)
240{
241 memset (&local_addr_space, 0, sizeof (local_addr_space));
242 local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
243 local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
244 local_addr_space.acc.put_unwind_info = put_unwind_info;
245 local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
246 local_addr_space.acc.access_mem = access_mem;
247 local_addr_space.acc.access_reg = access_reg;
248 local_addr_space.acc.access_fpreg = access_fpreg;
249 local_addr_space.acc.resume = x86_local_resume;
250 local_addr_space.acc.get_proc_name = get_static_proc_name;
251 unw_flush_cache (&local_addr_space, 0, 0);
252}
253
254#endif /* !UNW_REMOTE_ONLY */