1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2001-2005 Hewlett-Packard Co
3 Copyright (C) 2007 David Mosberger-Tang
4 Contributed by David Mosberger-Tang <dmosberger@gmail.com>
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 /* This files contains libunwind-internal definitions which are
28 subject to frequent change and are not to be exposed to
29 libunwind-users. */
30
31 #ifndef libunwind_i_h
32 #define libunwind_i_h
33
34 #include "config.h"
35
36 /* Platform-independent libunwind-internal declarations. */
37
38 #include <assert.h>
39 #include <elf.h>
40 #include <endian.h>
41 #include <pthread.h>
42 #include <signal.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <sys/mman.h>
47
48 #include <ngunwind/libunwind.h>
49 #include <ngunwind/dwarf.h>
50
51 #include "compiler.h"
52
53 #ifdef DEBUG
54 # define UNW_DEBUG 1
55 #else
56 # define UNW_DEBUG 0
57 #endif
58
59 /* Make it easy to write thread-safe code which may or may not be
60 linked against libpthread. The macros below can be used
61 unconditionally and if -lpthread is around, they'll call the
62 corresponding routines otherwise, they do nothing. */
63
64 #pragma weak pthread_mutex_init
65 #pragma weak pthread_mutex_lock
66 #pragma weak pthread_mutex_unlock
67
68 #define mutex_init(l) \
69 (pthread_mutex_init != NULL ? pthread_mutex_init ((l), NULL) : 0)
70 #define mutex_lock(l) \
71 (pthread_mutex_lock != NULL ? pthread_mutex_lock (l) : 0)
72 #define mutex_unlock(l) \
73 (pthread_mutex_unlock != NULL ? pthread_mutex_unlock (l) : 0)
74
75 #ifdef HAVE_ATOMIC_OPS_H
76 # include <atomic_ops.h>
77 static inline int
cmpxchg_ptr(void * addr,void * old_value,void * new_value)78 cmpxchg_ptr (void *addr, void *old_value, void *new_value)
79 {
80 union
81 {
82 void *vp;
83 AO_t *aop;
84 }
85 u;
86
87 u.vp = addr;
88 return AO_compare_and_swap(u.aop, (AO_t) old_value, (AO_t) new_value);
89 }
90 # define fetch_and_add1(_ptr) AO_fetch_and_add1(_ptr)
91 # define fetch_and_add(_ptr, value) AO_fetch_and_add(_ptr, value)
92 # define HAVE_CMPXCHG
93 # define HAVE_FETCH_AND_ADD
94 #elif defined(HAVE_SYNC_ATOMICS)
95 static inline int
cmpxchg_ptr(void * addr,void * old_value,void * new_value)96 cmpxchg_ptr (void *addr, void *old_value, void *new_value)
97 {
98 union
99 {
100 void *vp;
101 long *vlp;
102 }
103 u;
104
105 u.vp = addr;
106 return __sync_bool_compare_and_swap(u.vlp, (long) old_value, (long) new_value);
107 }
108 # define fetch_and_add1(_ptr) __sync_fetch_and_add(_ptr, 1)
109 # define fetch_and_add(_ptr, value) __sync_fetch_and_add(_ptr, value)
110 # define HAVE_CMPXCHG
111 # define HAVE_FETCH_AND_ADD
112 #endif
113 #define atomic_read(ptr) (*(ptr))
114
115 /* Type of a mask that can be used to inhibit preemption. At the
116 userlevel, preemption is caused by signals and hence sigset_t is
117 appropriate. In constrast, the Linux kernel uses "unsigned long"
118 to hold the processor "flags" instead. */
119 typedef sigset_t intrmask_t;
120
121 extern intrmask_t unwi_full_mask;
122
123 /* Silence compiler warnings about variables which are used only if libunwind
124 is configured in a certain way */
mark_as_used(void * v UNUSED)125 static inline void mark_as_used(void *v UNUSED) {
126 }
127
128 #if defined(CONFIG_BLOCK_SIGNALS)
129 # define SIGPROCMASK(how, new_mask, old_mask) \
130 sigprocmask((how), (new_mask), (old_mask))
131 #else
132 # define SIGPROCMASK(how, new_mask, old_mask) mark_as_used(old_mask)
133 #endif
134
135 /* Prefer adaptive mutexes if available */
136 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
137 #define UNW_PTHREAD_MUTEX_INITIALIZER PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
138 #else
139 #define UNW_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
140 #endif
141
142 #define define_lock(name) \
143 pthread_mutex_t name = UNW_PTHREAD_MUTEX_INITIALIZER
144 #define lock_init(l) mutex_init (l)
145 #define lock_acquire(l,m) \
146 do { \
147 SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &(m)); \
148 mutex_lock (l); \
149 } while (0)
150 #define lock_release(l,m) \
151 do { \
152 mutex_unlock (l); \
153 SIGPROCMASK (SIG_SETMASK, &(m), NULL); \
154 } while (0)
155
156 #define SOS_MEMORY_SIZE 16384 /* see src/mi/mempool.c */
157
158 #ifndef MAP_ANONYMOUS
159 # define MAP_ANONYMOUS MAP_ANON
160 #endif
161 #define GET_MEMORY(mem, size) \
162 do { \
163 /* Hopefully, mmap() goes straight through to a system call stub... */ \
164 mem = mmap (NULL, size, PROT_READ | PROT_WRITE, \
165 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); \
166 if (mem == MAP_FAILED) \
167 mem = NULL; \
168 } while (0)
169
170 extern int unwi_find_dynamic_proc_info (unw_addr_space_t as,
171 unw_word_t ip,
172 unw_proc_info_t *pi,
173 int need_unwind_info, void *arg);
174 extern int unwi_extract_dynamic_proc_info (unw_addr_space_t as,
175 unw_word_t ip,
176 unw_proc_info_t *pi,
177 unw_dyn_info_t *di,
178 int need_unwind_info,
179 void *arg);
180 extern void unwi_put_dynamic_unwind_info (unw_addr_space_t as,
181 unw_proc_info_t *pi, void *arg);
182
183 /* These handle the remote (cross-address-space) case of accessing
184 dynamic unwind info. */
185
186 extern int unwi_dyn_remote_find_proc_info (unw_addr_space_t as,
187 unw_word_t ip,
188 unw_proc_info_t *pi,
189 int need_unwind_info,
190 void *arg);
191 extern void unwi_dyn_remote_put_unwind_info (unw_addr_space_t as,
192 unw_proc_info_t *pi,
193 void *arg);
194 extern int unwi_dyn_validate_cache (unw_addr_space_t as, void *arg);
195
196 extern unw_dyn_info_list_t _U_dyn_info_list;
197 extern pthread_mutex_t _U_dyn_info_list_lock;
198
199 #if UNW_DEBUG
200 extern int unwi_debug_level;
201
202 # include <stdio.h>
203 # define Debug(level,format...) \
204 do { \
205 if (unwi_debug_level >= level) \
206 { \
207 int _n = level; \
208 if (_n > 16) \
209 _n = 16; \
210 fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__); \
211 fprintf (stderr, format); \
212 } \
213 } while (0)
214 # define Dprintf(format...) fprintf (stderr, format)
215 #else
216 # define Debug(level,format...)
217 # define Dprintf(format...)
218 #endif
219
220 extern int unwi_print_error (const char *string);
221
222 extern void mi_init (void); /* machine-independent initializations */
223 extern unw_word_t _U_dyn_info_list_addr (void);
224
225 /* This is needed/used by ELF targets only. */
226
227 struct elf_image
228 {
229 void *image; /* pointer to mmap'd image */
230 size_t size; /* (file-) size of the image */
231 };
232
233 struct elf_dyn_info
234 {
235 struct elf_image ei;
236 unw_dyn_info_t di_cache;
237 unw_dyn_info_t di_debug; /* additional table info for .debug_frame */
238 #if UNW_TARGET_ARM
239 unw_dyn_info_t di_arm; /* additional table info for .ARM.exidx */
240 #endif
241 };
242
243 // as: Address Space
244
245 struct as_contents
246 {
247 void *data;
248 size_t size;
249 };
250
251 struct as_elf_dyn_info
252 {
253 void* arg; /* the "arg" to address space accessors */
254 unw_dyn_info_t di_cache;
255 unw_dyn_info_t di_debug; /* additional table info for .debug_frame */
256
257 /* loaded contents */
258 struct as_contents ehdr;
259 struct as_contents phdr;
260 struct as_contents eh;
261 struct as_contents dyn;
262 };
263
264 extern void unwi_invalidate_edi (struct elf_dyn_info *edi);
265
266 extern void unwi_invalidate_as_edi (struct as_elf_dyn_info *edi);
267
268 extern int unwi_load_as_contents (unw_addr_space_t as, struct as_contents *contents,
269 unw_word_t offset, size_t size, void *arg);
270
271 /* Define GNU and processor specific values for the Phdr p_type field in case
272 they aren't defined by <elf.h>. */
273 #ifndef PT_GNU_EH_FRAME
274 # define PT_GNU_EH_FRAME 0x6474e550
275 #endif /* !PT_GNU_EH_FRAME */
276 #ifndef PT_ARM_EXIDX
277 # define PT_ARM_EXIDX 0x70000001 /* ARM unwind segment */
278 #endif /* !PT_ARM_EXIDX */
279
280 struct unw_addr_space
281 {
282 struct unw_accessors acc;
283 int big_endian;
284 unw_caching_policy_t caching_policy;
285 #ifdef HAVE_ATOMIC_OPS_H
286 AO_t cache_generation;
287 #else
288 uint32_t cache_generation;
289 #endif
290 unw_word_t dyn_generation; /* see dyn-common.h */
291 unw_word_t dyn_info_list_addr; /* (cached) dyn_info_list_addr */
292 struct dwarf_rs_cache global_cache;
293 struct unw_debug_frame_list *debug_frames;
294 };
295
296 typedef struct
297 {
298 unw_word_t virtual_address;
299 int32_t frame_type : 2; /* unw_tdep_frame_type_t classification */
300 int32_t last_frame : 1; /* non-zero if last frame in chain */
301 int32_t cfa_reg_sp : 1; /* cfa dwarf base register is sp vs. fp */
302 int32_t cfa_reg_offset; /* cfa is at this offset from base register value */
303 int32_t fp_cfa_offset; /* fp saved at this offset from cfa (-1 = not saved) */
304 int32_t sp_cfa_offset; /* sp saved at this offset from cfa (-1 = not saved) */
305 #if UNW_TARGET_ARM || UNW_TARGET_AARCH64
306 int32_t lr_cfa_offset; /* lr saved at this offset from cfa (-1 = not saved) */
307 #endif
308 }
309 unw_tdep_frame_t;
310
311 #if defined __arm__
312 # include "tgt_i-arm.h"
313 #elif defined __aarch64__
314 # include "tgt_i-aarch64.h"
315 #elif defined __x86_64__
316 # include "tgt_i-x86_64.h"
317 #else
318 # error "Unsupported arch"
319 #endif
320
321 struct cursor
322 {
323 struct dwarf_cursor dwarf; /* must be first */
324
325 unw_tdep_frame_t frame_info; /* quick tracing assist info */
326
327 /* Format of sigcontext structure and address at which it is stored: */
328 unw_tdep_sigcontext_format_t sigcontext_format;
329 unw_word_t sigcontext_addr;
330
331 int validate;
332 ucontext_t *uc;
333 };
334
335 /* Platforms that support UNW_INFO_FORMAT_TABLE need to define
336 tdep_search_unwind_table. */
337 #if !UNW_TARGET_ARM /* arm has its own version */
338 #define tdep_search_unwind_table dwarf_search_unwind_table
339 #endif
340
341 #define tdep_find_unwind_table dwarf_find_unwind_table
342
343 // These allow target-specific frame handling.
344 // Linux uses this for signal frame handling.
345 #if UNW_TARGET_X86_64 && defined (__linux__)
346 extern void tdep_fetch_frame (struct dwarf_cursor *c, unw_word_t ip,
347 int need_unwind_info);
348 extern void tdep_cache_frame (struct dwarf_cursor *c,
349 struct dwarf_reg_state *rs);
350 extern void tdep_reuse_frame (struct dwarf_cursor *c,
351 struct dwarf_reg_state *rs);
352 #else
353 #define tdep_fetch_frame(c,ip,n) do {} while(0)
354 #define tdep_cache_frame(c,rs) do {} while(0)
355 #define tdep_reuse_frame(c,rs) do {} while(0)
356 #endif
357
358 extern void tdep_stash_frame (struct dwarf_cursor *c,
359 struct dwarf_reg_state *rs);
360
361 #define tdep_find_proc_info(c,ip,n) \
362 (*(c)->as->acc.find_proc_info) ((c)->as, (ip), &(c)->pi, (n), \
363 (c)->as_arg)
364 #define tdep_put_unwind_info(as,pi,arg) \
365 (*(as)->acc.put_unwind_info) ((as), (pi), (arg))
366
367 #define tdep_get_as(c) ((c)->dwarf.as)
368 #define tdep_get_as_arg(c) ((c)->dwarf.as_arg)
369 #define tdep_get_ip(c) ((c)->dwarf.ip)
370 #define tdep_big_endian(as) ((as)->big_endian)
371
372 extern int tdep_init_done;
373
374 extern void tdep_init (void);
375 extern void tdep_init_mem_validate (void);
376 extern int tdep_search_unwind_table (unw_addr_space_t as, unw_word_t ip,
377 unw_dyn_info_t *di, unw_proc_info_t *pi,
378 int need_unwind_info, void *arg);
379 extern int tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
380 unsigned long *segbase, unsigned long *mapoff,
381 char *path, size_t pathlen);
382 extern int tdep_access_reg (struct cursor *c, unw_regnum_t reg,
383 unw_word_t *valp, int write);
384 extern int tdep_access_fpreg (struct cursor *c, unw_regnum_t reg,
385 unw_fpreg_t *valp, int write);
386
387 #ifndef tdep_get_func_addr
388 # define tdep_get_func_addr(as,addr,v) (*(v) = addr, 0)
389 #endif
390
391 #define UNW_ALIGN(x,a) (((x)+(a)-1UL)&~((a)-1UL))
392
393 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
394
395 #endif /* libunwind_i_h */
396