e90ec47d08150f9a10b83a0523913f134a10228f
[platform/upstream/libunwind.git] / src / ptrace / _UPT_access_fpreg.c
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
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
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 ((unsigned) reg >= ARRAY_SIZE (_UPT_reg_offset))
79     return -UNW_EBADREG;
80
81   if (ptrace(PT_GETFPREGS, pid, (caddr_t)&fpreg, 0) == -1)
82           return -UNW_EBADREG;
83   if (write) {
84 #if defined(__amd64__)
85           memcpy(&fpreg.fpr_xacc[reg], val, sizeof(unw_fpreg_t));
86 #elif defined(__i386__)
87           memcpy(&fpreg.fpr_acc[reg], val, sizeof(unw_fpreg_t));
88 #else
89 #error Fix me
90 #endif
91           if (ptrace(PT_SETFPREGS, pid, (caddr_t)&fpreg, 0) == -1)
92                   return -UNW_EBADREG;
93   } else
94 #if defined(__amd64__)
95           memcpy(val, &fpreg.fpr_xacc[reg], sizeof(unw_fpreg_t));
96 #elif defined(__i386__)
97           memcpy(val, &fpreg.fpr_acc[reg], sizeof(unw_fpreg_t));
98 #else
99 #error Fix me
100 #endif
101   return 0;
102 }
103 #else
104 #error Fix me
105 #endif