• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 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 static void
put_unwind_info(unw_addr_space_t as,unw_proc_info_t * proc_info,void * arg)62 put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
63 {
64   /* it's a no-op */
65 }
66 
67 static int
get_dyn_info_list_addr(unw_addr_space_t as,unw_word_t * dyn_info_list_addr,void * arg)68 get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
69                         void *arg)
70 {
71 #ifndef UNW_LOCAL_ONLY
72 # pragma weak _U_dyn_info_list_addr
73   if (!_U_dyn_info_list_addr)
74     return -UNW_ENOINFO;
75 #endif
76   // Access the `_U_dyn_info_list` from `LOCAL_ONLY` library, i.e. libunwind.so.
77   *dyn_info_list_addr = _U_dyn_info_list_addr ();
78   return 0;
79 }
80 
81 static int
access_mem(unw_addr_space_t as,unw_word_t addr,unw_word_t * val,int write,void * arg)82 access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
83             void *arg)
84 {
85   if (write)
86     {
87       Debug (16, "mem[%x] <- %x\n", addr, *val);
88       *(unw_word_t *) addr = *val;
89     }
90   else
91     {
92       *val = *(unw_word_t *) addr;
93       Debug (16, "mem[%x] -> %x\n", addr, *val);
94     }
95   return 0;
96 }
97 
98 static int
access_reg(unw_addr_space_t as,unw_regnum_t reg,unw_word_t * val,int write,void * arg)99 access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
100             void *arg)
101 {
102   unw_word_t *addr;
103   ucontext_t *uc = arg;
104 
105   if (unw_is_fpreg (reg))
106     goto badreg;
107 
108   if (!(addr = uc_addr (uc, reg)))
109     goto badreg;
110 
111   if (write)
112     {
113       *(unw_word_t *) addr = *val;
114       Debug (12, "%s <- %x\n", unw_regname (reg), *val);
115     }
116   else
117     {
118       *val = *(unw_word_t *) addr;
119       Debug (12, "%s -> %x\n", unw_regname (reg), *val);
120     }
121   return 0;
122 
123  badreg:
124   Debug (1, "bad register number %u\n", reg);
125   return -UNW_EBADREG;
126 }
127 
128 static int
access_fpreg(unw_addr_space_t as,unw_regnum_t reg,unw_fpreg_t * val,int write,void * arg)129 access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
130               int write, void *arg)
131 {
132   ucontext_t *uc = arg;
133   unw_fpreg_t *addr;
134 
135   if (!unw_is_fpreg (reg))
136     goto badreg;
137 
138   if (!(addr = uc_addr (uc, reg)))
139     goto badreg;
140 
141   if (write)
142     {
143       Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
144              ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
145       *(unw_fpreg_t *) addr = *val;
146     }
147   else
148     {
149       *val = *(unw_fpreg_t *) addr;
150       Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
151              ((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
152     }
153   return 0;
154 
155  badreg:
156   Debug (1, "bad register number %u\n", reg);
157   /* attempt to access a non-preserved register */
158   return -UNW_EBADREG;
159 }
160 
161 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)162 get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
163                       char *buf, size_t buf_len, unw_word_t *offp,
164                       void *arg)
165 {
166   return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
167 }
168 
169 HIDDEN void
sh_local_addr_space_init(void)170 sh_local_addr_space_init (void)
171 {
172   memset (&local_addr_space, 0, sizeof (local_addr_space));
173   local_addr_space.caching_policy = UNWI_DEFAULT_CACHING_POLICY;
174   local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
175   local_addr_space.acc.put_unwind_info = put_unwind_info;
176   local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
177   local_addr_space.acc.access_mem = access_mem;
178   local_addr_space.acc.access_reg = access_reg;
179   local_addr_space.acc.access_fpreg = access_fpreg;
180   local_addr_space.acc.resume = sh_local_resume;
181   local_addr_space.acc.get_proc_name = get_static_proc_name;
182   unw_flush_cache (&local_addr_space, 0, 0);
183 }
184 
185 #endif /* !UNW_REMOTE_ONLY */
186