Imported Upstream version 1.3.1
[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 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 #else
88 #error Fix me
89 #endif
90   if ((unsigned) reg >= ARRAY_SIZE (_UPT_reg_offset))
91     return -UNW_EBADREG;
92
93   if (ptrace(PT_GETFPREGS, pid, (caddr_t)&fpreg, 0) == -1)
94           return -UNW_EBADREG;
95   if (write) {
96 #if defined(__amd64__)
97           memcpy(&fpreg.fpr_xacc[reg], val, sizeof(unw_fpreg_t));
98 #elif defined(__i386__)
99           memcpy(&fpreg.fpr_acc[reg], val, sizeof(unw_fpreg_t));
100 #elif defined(__arm__)
101           memcpy(&fpreg.fpr[reg], val, sizeof(unw_fpreg_t));
102 #else
103 #error Fix me
104 #endif
105           if (ptrace(PT_SETFPREGS, pid, (caddr_t)&fpreg, 0) == -1)
106                   return -UNW_EBADREG;
107   } else
108 #if defined(__amd64__)
109           memcpy(val, &fpreg.fpr_xacc[reg], sizeof(unw_fpreg_t));
110 #elif defined(__i386__)
111           memcpy(val, &fpreg.fpr_acc[reg], sizeof(unw_fpreg_t));
112 #elif defined(__arm__)
113           memcpy(val, &fpreg.fpr[reg], sizeof(unw_fpreg_t));
114 #else
115 #error Fix me
116 #endif
117   return 0;
118 }
119 #else
120 #error Fix me
121 #endif