1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2008 CodeSourcery
3 Copyright (C) 2012 Tommi Rantala <tt.rantala@gmail.com>
4
5 This file is part of libunwind.
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "unwind_i.h"
30
31 #ifdef UNW_REMOTE_ONLY
32
33 /* unw_local_addr_space is a NULL pointer in this case. */
34 PROTECTED unw_addr_space_t unw_local_addr_space;
35
36 #else /* !UNW_REMOTE_ONLY */
37
38 static struct unw_addr_space local_addr_space;
39
40 PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
41
42 static inline void *
uc_addr(ucontext_t * uc,int reg)43 uc_addr (ucontext_t *uc, int reg)
44 {
45 if (reg >= UNW_SH_R0 && reg <= UNW_SH_PR)
46 return &uc->uc_mcontext.gregs[reg];
47 else
48 return NULL;
49 }
50
51 # ifdef UNW_LOCAL_ONLY
52
53 HIDDEN void *
tdep_uc_addr(ucontext_t * uc,int reg)54 tdep_uc_addr (ucontext_t *uc, int reg)
55 {
56 return uc_addr (uc, reg);
57 }
58
59 # endif /* UNW_LOCAL_ONLY */
60
61 HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
62
63 /* XXX fix me: there is currently no way to locate the dyn-info list
64 by a remote unwinder. On ia64, this is done via a special
65 unwind-table entry. Perhaps something similar can be done with
66 DWARF2 unwind info. */
67
68 static void
put_unwind_info(unw_addr_space_t as,unw_proc_info_t * proc_info,void * arg)69 put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
70 {
71 /* it's a no-op */
72 }
73
74 static int
get_dyn_info_list_addr(unw_addr_space_t as,unw_word_t * dyn_info_list_addr,void * arg)75 get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
76 void *arg)
77 {
78 *dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
79 return 0;
80 }
81
82 static int
access_mem(unw_addr_space_t as,unw_word_t addr,unw_word_t * val,int write,void * arg)83 access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
84 void *arg)
85 {
86 if (write)
87 {
88 /* ANDROID support update. */
89 #ifdef UNW_LOCAL_ONLY
90 if (map_local_is_writable (addr, sizeof(unw_word_t)))
91 {
92 #endif
93 Debug (16, "mem[%x] <- %x\n", addr, *val);
94 *(unw_word_t *) addr = *val;
95 #ifdef UNW_LOCAL_ONLY
96 }
97 else
98 {
99 Debug (16, "Unwritable memory mem[%x] <- %x\n", addr, *val);
100 return -1;
101 }
102 #endif
103 /* End of ANDROID update. */
104 }
105 else
106 {
107 /* ANDROID support update. */
108 #ifdef UNW_LOCAL_ONLY
109 if (map_local_is_readable (addr, sizeof(unw_word_t)))
110 {
111 #endif
112 *val = *(unw_word_t *) addr;
113 Debug (16, "mem[%x] -> %x\n", addr, *val);
114 #ifdef UNW_LOCAL_ONLY
115 }
116 else
117 {
118 Debug (16, "Unreadable memory mem[%x] -> XXX\n", addr);
119 return -1;
120 }
121 #endif
122 /* End of ANDROID update. */
123 }
124 return 0;
125 }
126
127 static int
access_reg(unw_addr_space_t as,unw_regnum_t reg,unw_word_t * val,int write,void * arg)128 access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
129 void *arg)
130 {
131 unw_word_t *addr;
132 ucontext_t *uc = arg;
133
134 if (unw_is_fpreg (reg))
135 goto badreg;
136
137 if (!(addr = uc_addr (uc, reg)))
138 goto badreg;
139
140 if (write)
141 {
142 *(unw_word_t *) addr = *val;
143 Debug (12, "%s <- %x\n", unw_regname (reg), *val);
144 }
145 else
146 {
147 *val = *(unw_word_t *) addr;
148 Debug (12, "%s -> %x\n", unw_regname (reg), *val);
149 }
150 return 0;
151
152 badreg:
153 Debug (1, "bad register number %u\n", reg);
154 return -UNW_EBADREG;
155 }
156
157 static int
access_fpreg(unw_addr_space_t as,unw_regnum_t reg,unw_fpreg_t * val,int write,void * arg)158 access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
159 int write, void *arg)
160 {
161 ucontext_t *uc = arg;
162 unw_fpreg_t *addr;
163
164 if (!unw_is_fpreg (reg))
165 goto badreg;
166
167 if (!(addr = uc_addr (uc, reg)))
168 goto badreg;
169
170 if (write)
171 {
172 Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
173 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
174 *(unw_fpreg_t *) addr = *val;
175 }
176 else
177 {
178 *val = *(unw_fpreg_t *) addr;
179 Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
180 ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
181 }
182 return 0;
183
184 badreg:
185 Debug (1, "bad register number %u\n", reg);
186 /* attempt to access a non-preserved register */
187 return -UNW_EBADREG;
188 }
189
190 static int
get_static_proc_name(unw_addr_space_t as,unw_word_t ip,char * buf,size_t buf_len,unw_word_t * offp,void * arg)191 get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
192 char *buf, size_t buf_len, unw_word_t *offp,
193 void *arg)
194 {
195 return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp, arg);
196 }
197
198 HIDDEN void
sh_local_addr_space_init(void)199 sh_local_addr_space_init (void)
200 {
201 memset (&local_addr_space, 0, sizeof (local_addr_space));
202 local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
203 local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
204 local_addr_space.acc.put_unwind_info = put_unwind_info;
205 local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
206 local_addr_space.acc.access_mem = access_mem;
207 local_addr_space.acc.access_reg = access_reg;
208 local_addr_space.acc.access_fpreg = access_fpreg;
209 local_addr_space.acc.resume = sh_local_resume;
210 local_addr_space.acc.get_proc_name = get_static_proc_name;
211 unw_flush_cache (&local_addr_space, 0, 0);
212
213 map_local_init ();
214 }
215
216 #endif /* !UNW_REMOTE_ONLY */
217