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