1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2003-2005 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4 Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
6 This file is part of libunwind.
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:
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
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. */
27 #include "_UPT_internal.h"
31 # ifdef HAVE_ASM_PTRACE_OFFSETS_H
32 # include <asm/ptrace_offsets.h>
34 # include "tdep-ia64/rse.h"
37 #if HAVE_DECL_PTRACE_GETREGSET
39 _UPT_access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val,
42 struct UPT_info *ui = arg;
44 gregset_t regs = { 0 };
45 struct iovec iovec = { 0 };
48 iovec.iov_base = ®s;
49 iovec.iov_len = sizeof(regs);
51 Debug(16, "using getregset: reg: %s [%u], val: %lx, write: %u\n",
52 unw_regname(reg), (unsigned int)reg, (unsigned long)*val, write);
55 Debug(16, "%s [%u] <- %lx\n",
56 unw_regname (reg), (unsigned int)reg, (unsigned long)*val);
58 if ((unsigned int)reg >= ARRAY_SIZE (_UPT_reg_offset))
64 r = (char *)®s + _UPT_reg_offset[reg];
65 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iovec) == -1)
69 memcpy(r, val, sizeof(unw_word_t));
70 if (ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &iovec) == -1)
73 memcpy(val, r, sizeof(unw_word_t));
78 Debug(1, "bad register %s [%u] (error: %s)\n",
79 unw_regname(reg), reg, strerror (errno));
82 #elif HAVE_DECL_PTRACE_POKEUSER || HAVE_TTRACE
84 _UPT_access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val,
87 struct UPT_info *ui = arg;
91 Debug(16, "using pokeuser: reg: %s [%u], val: %lx, write: %d\n", unw_regname(reg), (unsigned) reg, (long) val, write);
94 Debug (16, "%s <- %lx\n", unw_regname (reg), (long) *val);
98 if ((unsigned) reg - UNW_IA64_NAT < 32)
100 unsigned long nat_bits, mask;
102 /* The Linux ptrace represents the statc NaT bits as a single word. */
103 mask = ((unw_word_t) 1) << (reg - UNW_IA64_NAT);
106 # warning No support for ttrace() yet.
108 nat_bits = ptrace (PTRACE_PEEKUSER, pid, PT_NAT_BITS, 0);
120 # warning No support for ttrace() yet.
123 ptrace (PTRACE_POKEUSER, pid, PT_NAT_BITS, nat_bits);
133 case UNW_IA64_GR + 0:
141 unsigned long ip, psr;
143 /* distribute bundle-addr. & slot-number across PT_IIP & PT_IPSR. */
145 # warning No support for ttrace() yet.
148 psr = ptrace (PTRACE_PEEKUSER, pid, PT_CR_IPSR, 0);
155 psr = (psr & ~0x3UL << 41) | (*val & 0x3);
157 # warning No support for ttrace() yet.
160 ptrace (PTRACE_POKEUSER, pid, PT_CR_IIP, ip);
161 ptrace (PTRACE_POKEUSER, pid, PT_CR_IPSR, psr);
169 # warning No support for ttrace() yet.
172 ip = ptrace (PTRACE_PEEKUSER, pid, PT_CR_IIP, 0);
176 *val = ip + ((psr >> 41) & 0x3);
181 case UNW_IA64_AR_BSPSTORE:
182 reg = UNW_IA64_AR_BSP;
185 case UNW_IA64_AR_BSP:
188 unsigned long sof, cfm, bsp;
191 # warning No support for ttrace() yet.
193 /* Account for the fact that ptrace() expects bsp to point
194 _after_ the current register frame. */
196 cfm = ptrace (PTRACE_PEEKUSER, pid, PT_CFM, 0);
204 bsp = rse_skip_regs (*val, sof);
206 # warning No support for ttrace() yet.
209 ptrace (PTRACE_POKEUSER, pid, PT_AR_BSP, bsp);
217 # warning No support for ttrace() yet.
220 bsp = ptrace (PTRACE_PEEKUSER, pid, PT_AR_BSP, 0);
224 *val = rse_skip_regs (bsp, -sof);
230 /* If we change CFM, we need to adjust ptrace's notion of bsp
231 accordingly, so that the real bsp remains unchanged. */
234 unsigned long new_sof, old_sof, cfm, bsp;
237 # warning No support for ttrace() yet.
240 bsp = ptrace (PTRACE_PEEKUSER, pid, PT_AR_BSP, 0);
241 cfm = ptrace (PTRACE_PEEKUSER, pid, PT_CFM, 0);
245 old_sof = (cfm & 0x7f);
246 new_sof = (*val & 0x7f);
247 if (old_sof != new_sof)
249 bsp = rse_skip_regs (bsp, -old_sof + new_sof);
251 # warning No support for ttrace() yet.
254 ptrace (PTRACE_POKEUSER, pid, PT_AR_BSP, 0);
260 # warning No support for ttrace() yet.
263 ptrace (PTRACE_POKEUSER, pid, PT_CFM, *val);
271 #endif /* End of IA64 */
273 if ((unsigned) reg >= ARRAY_SIZE (_UPT_reg_offset))
276 Debug(2, "register out of range: >= %zu / %zu\n", sizeof(_UPT_reg_offset), sizeof(_UPT_reg_offset[0]));
283 # warning No support for ttrace() yet.
287 ptrace (PTRACE_POKEUSER, pid, _UPT_reg_offset[reg], *val);
290 Debug(16, "ptrace PEEKUSER pid: %lu , reg: %lu , offs: %lu\n", (unsigned long)pid, (unsigned long)reg,
291 (unsigned long)_UPT_reg_offset[reg]);
293 *val = ptrace (PTRACE_PEEKUSER, pid, _UPT_reg_offset[reg], 0);
297 Debug(2, "ptrace failure\n");
303 #ifdef UNW_TARGET_IA64
308 Debug (16, "%s[%u] -> %lx\n", unw_regname (reg), (unsigned) reg, (long) *val);
313 Debug (1, "bad register %s [%u] (error: %s)\n", unw_regname(reg), reg, strerror (errno));
316 #elif HAVE_DECL_PT_GETREGS
318 _UPT_access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val,
319 int write, void *arg)
321 struct UPT_info *ui = arg;
327 Debug(16, "using getregs: reg: %s [%u], val: %lx, write: %u\n", unw_regname(reg), (unsigned) reg, (long) val, write);
330 Debug (16, "%s [%u] <- %lx\n", unw_regname (reg), (unsigned) reg, (long) *val);
332 if ((unsigned) reg >= ARRAY_SIZE (_UPT_reg_offset))
337 r = (char *)®s + _UPT_reg_offset[reg];
338 if (ptrace(PT_GETREGS, pid, (caddr_t)®s, 0) == -1)
341 memcpy(r, val, sizeof(unw_word_t));
342 if (ptrace(PT_SETREGS, pid, (caddr_t)®s, 0) == -1)
345 memcpy(val, r, sizeof(unw_word_t));
349 Debug (1, "bad register %s [%u] (error: %s)\n", unw_regname(reg), reg, strerror (errno));