1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2003 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4 Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
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 "_UPT_internal.h"
28
29 #if HAVE_DECL_PTRACE_POKEUSER || HAVE_TTRACE
30 int
_UPT_access_fpreg(unw_addr_space_t as,unw_regnum_t reg,unw_fpreg_t * val,int write,void * arg)31 _UPT_access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
32 int write, void *arg)
33 {
34 unw_word_t *wp = (unw_word_t *) val;
35 struct UPT_info *ui = arg;
36 pid_t pid = ui->pid;
37 int i;
38
39 if ((unsigned) reg >= ARRAY_SIZE (_UPT_reg_offset))
40 return -UNW_EBADREG;
41
42 errno = 0;
43 if (write)
44 for (i = 0; i < (int) (sizeof (*val) / sizeof (wp[i])); ++i)
45 {
46 #ifdef HAVE_TTRACE
47 # warning No support for ttrace() yet.
48 #else
49 ptrace (PTRACE_POKEUSER, pid, _UPT_reg_offset[reg] + i * sizeof(wp[i]),
50 wp[i]);
51 #endif
52 if (errno)
53 return -UNW_EBADREG;
54 }
55 else
56 for (i = 0; i < (int) (sizeof (*val) / sizeof (wp[i])); ++i)
57 {
58 #ifdef HAVE_TTRACE
59 # warning No support for ttrace() yet.
60 #else
61 wp[i] = ptrace (PTRACE_PEEKUSER, pid,
62 _UPT_reg_offset[reg] + i * sizeof(wp[i]), 0);
63 #endif
64 if (errno)
65 return -UNW_EBADREG;
66 }
67 return 0;
68 }
69 #elif HAVE_DECL_PT_GETFPREGS
70 int
_UPT_access_fpreg(unw_addr_space_t as,unw_regnum_t reg,unw_fpreg_t * val,int write,void * arg)71 _UPT_access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
72 int write, void *arg)
73 {
74 struct UPT_info *ui = arg;
75 pid_t pid = ui->pid;
76 fpregset_t fpreg;
77
78 #if defined(__amd64__)
79 if (1) /* XXXKIB */
80 return -UNW_EBADREG;
81 #elif defined(__i386__)
82 if ((unsigned) reg < UNW_X86_ST0 || (unsigned) reg > UNW_X86_ST7)
83 return -UNW_EBADREG;
84 #elif defined(__arm__)
85 if ((unsigned) reg < UNW_ARM_F0 || (unsigned) reg > UNW_ARM_F7)
86 return -UNW_EBADREG;
87 #elif defined(__aarch64__)
88 if ((unsigned) reg < UNW_AARCH64_V0 || (unsigned) reg > UNW_AARCH64_V31)
89 return -UNW_EBADREG;
90 #elif defined(__powerpc64__)
91 if ((unsigned) reg < UNW_PPC64_F0 || (unsigned) reg > UNW_PPC64_F31)
92 return -UNW_EBADREG;
93 #else
94 #error Fix me
95 #endif
96 if ((unsigned) reg >= ARRAY_SIZE (_UPT_reg_offset))
97 return -UNW_EBADREG;
98
99 if (ptrace(PT_GETFPREGS, pid, (caddr_t)&fpreg, 0) == -1)
100 return -UNW_EBADREG;
101 if (write) {
102 #if defined(__amd64__)
103 memcpy(&fpreg.fpr_xacc[reg], val, sizeof(unw_fpreg_t));
104 #elif defined(__i386__)
105 memcpy(&fpreg.fpr_acc[reg], val, sizeof(unw_fpreg_t));
106 #elif defined(__arm__)
107 memcpy(&fpreg.fpr[reg], val, sizeof(unw_fpreg_t));
108 #elif defined(__aarch64__)
109 memcpy(&fpreg.fp_q[reg], val, sizeof(unw_fpreg_t));
110 #elif defined(__powerpc64__)
111 memcpy(&fpreg.fpreg[reg], val, sizeof(unw_fpreg_t));
112 #else
113 #error Fix me
114 #endif
115 if (ptrace(PT_SETFPREGS, pid, (caddr_t)&fpreg, 0) == -1)
116 return -UNW_EBADREG;
117 } else
118 #if defined(__amd64__)
119 memcpy(val, &fpreg.fpr_xacc[reg], sizeof(unw_fpreg_t));
120 #elif defined(__i386__)
121 memcpy(val, &fpreg.fpr_acc[reg], sizeof(unw_fpreg_t));
122 #elif defined(__arm__)
123 memcpy(val, &fpreg.fpr[reg], sizeof(unw_fpreg_t));
124 #elif defined(__aarch64__)
125 memcpy(val, &fpreg.fp_q[reg], sizeof(unw_fpreg_t));
126 #elif defined(__powerpc64__)
127 memcpy(val, &fpreg.fpreg[reg], sizeof(unw_fpreg_t));
128 #else
129 #error Fix me
130 #endif
131 return 0;
132 }
133 #else
134 #error Fix me
135 #endif
136