homeip.net!davidm | 588192d | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 1 | /* libunwind - a platform-independent unwind library |
| 2 | Copyright (C) 2002 Hewlett-Packard Co |
David Mosberger-Tang | e6b9f35 | 2007-08-22 13:02:09 -0600 | [diff] [blame] | 3 | Copyright (C) 2007 David Mosberger-Tang |
| 4 | Contributed by David Mosberger-Tang <dmosberger@gmail.com> |
homeip.net!davidm | 588192d | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of libunwind. |
| 7 | |
| 8 | Permission is hereby granted, free of charge, to any person obtaining |
| 9 | a copy of this software and associated documentation files (the |
| 10 | "Software"), to deal in the Software without restriction, including |
| 11 | without limitation the rights to use, copy, modify, merge, publish, |
| 12 | distribute, sublicense, and/or sell copies of the Software, and to |
| 13 | permit persons to whom the Software is furnished to do so, subject to |
| 14 | the following conditions: |
| 15 | |
| 16 | The above copyright notice and this permission notice shall be |
| 17 | included in all copies or substantial portions of the Software. |
| 18 | |
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 23 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 24 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 25 | WITH 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. */ |
| 35 | PROTECTED unw_addr_space_t unw_local_addr_space; |
| 36 | |
| 37 | #else /* !UNW_REMOTE_ONLY */ |
| 38 | |
| 39 | static struct unw_addr_space local_addr_space; |
| 40 | |
| 41 | PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space; |
| 42 | |
| 43 | static inline void * |
| 44 | uc_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 | |
| 76 | HIDDEN void * |
| 77 | tdep_uc_addr (ucontext_t *uc, int reg) |
| 78 | { |
| 79 | return uc_addr (uc, reg); |
| 80 | } |
| 81 | |
| 82 | # endif /* UNW_LOCAL_ONLY */ |
| 83 | |
| 84 | HIDDEN 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 | |
| 91 | static void |
| 92 | put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg) |
| 93 | { |
| 94 | /* it's a no-op */ |
| 95 | } |
| 96 | |
| 97 | static int |
| 98 | get_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 Sharma | ff0ae70 | 2009-03-16 11:06:26 -0700 | [diff] [blame] | 105 | #define PAGE_SIZE 4096 |
| 106 | #define PAGE_START(a) ((a) & ~(PAGE_SIZE-1)) |
| 107 | |
| 108 | /* Cache of already validated addresses */ |
| 109 | #define NLGA 4 |
| 110 | static unw_word_t last_good_addr[NLGA]; |
| 111 | static int lga_victim; |
| 112 | |
| 113 | static int |
| 114 | validate_mem (unw_word_t addr) |
| 115 | { |
| 116 | int i, victim; |
| 117 | |
| 118 | addr = PAGE_START(addr); |
| 119 | |
Paul Pluzhnikov | 0cf76ed | 2009-12-01 13:59:45 -0800 | [diff] [blame] | 120 | if (addr == 0) |
| 121 | return -1; |
| 122 | |
Arun Sharma | ff0ae70 | 2009-03-16 11:06:26 -0700 | [diff] [blame] | 123 | for (i = 0; i < NLGA; i++) |
| 124 | { |
| 125 | if (last_good_addr[i] && (addr == last_good_addr[i])) |
| 126 | return 0; |
| 127 | } |
| 128 | |
Arun Sharma | 3468a6b | 2010-02-23 10:35:47 -0800 | [diff] [blame^] | 129 | if (msync ((void *) addr, 1, MS_ASYNC) == -1) |
Arun Sharma | ff0ae70 | 2009-03-16 11:06:26 -0700 | [diff] [blame] | 130 | return -1; |
| 131 | |
| 132 | victim = lga_victim; |
| 133 | for (i = 0; i < NLGA; i++) { |
| 134 | if (!last_good_addr[victim]) { |
| 135 | last_good_addr[victim++] = addr; |
| 136 | return 0; |
| 137 | } |
| 138 | victim = (victim + 1) % NLGA; |
| 139 | } |
| 140 | |
| 141 | /* All slots full. Evict the victim. */ |
| 142 | last_good_addr[victim] = addr; |
| 143 | victim = (victim + 1) % NLGA; |
| 144 | lga_victim = victim; |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
homeip.net!davidm | 588192d | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 149 | static int |
| 150 | access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write, |
| 151 | void *arg) |
| 152 | { |
| 153 | if (write) |
| 154 | { |
| 155 | Debug (16, "mem[%x] <- %x\n", addr, *val); |
| 156 | *(unw_word_t *) addr = *val; |
| 157 | } |
| 158 | else |
| 159 | { |
Arun Sharma | ff0ae70 | 2009-03-16 11:06:26 -0700 | [diff] [blame] | 160 | /* validate address */ |
| 161 | const struct cursor *c = (const struct cursor *)arg; |
| 162 | if (c && c->validate && validate_mem(addr)) |
| 163 | return -1; |
homeip.net!davidm | 588192d | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 164 | *val = *(unw_word_t *) addr; |
| 165 | Debug (16, "mem[%x] -> %x\n", addr, *val); |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int |
| 171 | access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write, |
| 172 | void *arg) |
| 173 | { |
| 174 | unw_word_t *addr; |
Arun Sharma | ff0ae70 | 2009-03-16 11:06:26 -0700 | [diff] [blame] | 175 | ucontext_t *uc = ((struct cursor *)arg)->uc; |
homeip.net!davidm | 588192d | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 176 | |
| 177 | if (unw_is_fpreg (reg)) |
| 178 | goto badreg; |
| 179 | |
homeip.net!davidm | 588192d | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 180 | if (!(addr = uc_addr (uc, reg))) |
| 181 | goto badreg; |
| 182 | |
| 183 | if (write) |
| 184 | { |
| 185 | *(unw_word_t *) addr = *val; |
| 186 | Debug (12, "%s <- %x\n", unw_regname (reg), *val); |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | *val = *(unw_word_t *) addr; |
| 191 | Debug (12, "%s -> %x\n", unw_regname (reg), *val); |
| 192 | } |
| 193 | return 0; |
| 194 | |
| 195 | badreg: |
| 196 | Debug (1, "bad register number %u\n", reg); |
| 197 | return -UNW_EBADREG; |
| 198 | } |
| 199 | |
| 200 | static int |
| 201 | access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val, |
| 202 | int write, void *arg) |
| 203 | { |
Arun Sharma | ff0ae70 | 2009-03-16 11:06:26 -0700 | [diff] [blame] | 204 | ucontext_t *uc = ((struct cursor *)arg)->uc; |
homeip.net!davidm | 588192d | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 205 | unw_fpreg_t *addr; |
| 206 | |
| 207 | if (!unw_is_fpreg (reg)) |
| 208 | goto badreg; |
| 209 | |
| 210 | if (!(addr = uc_addr (uc, reg))) |
| 211 | goto badreg; |
| 212 | |
| 213 | if (write) |
| 214 | { |
| 215 | Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg), |
| 216 | ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]); |
| 217 | *(unw_fpreg_t *) addr = *val; |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | *val = *(unw_fpreg_t *) addr; |
| 222 | Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg), |
| 223 | ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]); |
| 224 | } |
| 225 | return 0; |
| 226 | |
| 227 | badreg: |
| 228 | Debug (1, "bad register number %u\n", reg); |
| 229 | /* attempt to access a non-preserved register */ |
| 230 | return -UNW_EBADREG; |
| 231 | } |
| 232 | |
| 233 | static int |
| 234 | get_static_proc_name (unw_addr_space_t as, unw_word_t ip, |
| 235 | char *buf, size_t buf_len, unw_word_t *offp, |
| 236 | void *arg) |
| 237 | { |
David Mosberger-Tang | e6b9f35 | 2007-08-22 13:02:09 -0600 | [diff] [blame] | 238 | return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp); |
homeip.net!davidm | 588192d | 2004-08-17 15:34:28 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | HIDDEN void |
| 242 | x86_local_addr_space_init (void) |
| 243 | { |
| 244 | memset (&local_addr_space, 0, sizeof (local_addr_space)); |
| 245 | local_addr_space.caching_policy = UNW_CACHE_GLOBAL; |
| 246 | local_addr_space.acc.find_proc_info = dwarf_find_proc_info; |
| 247 | local_addr_space.acc.put_unwind_info = put_unwind_info; |
| 248 | local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr; |
| 249 | local_addr_space.acc.access_mem = access_mem; |
| 250 | local_addr_space.acc.access_reg = access_reg; |
| 251 | local_addr_space.acc.access_fpreg = access_fpreg; |
| 252 | local_addr_space.acc.resume = x86_local_resume; |
| 253 | local_addr_space.acc.get_proc_name = get_static_proc_name; |
| 254 | unw_flush_cache (&local_addr_space, 0, 0); |
| 255 | } |
| 256 | |
| 257 | #endif /* !UNW_REMOTE_ONLY */ |