blob: a395d6884a56c155fb145f65b22e9e2ff293a7dc [file] [log] [blame]
hp.com!davidmd5ab8982005-05-20 11:28:16 +00001/* libunwind - a platform-independent unwind library
2 Copyright (C) 2001-2005 Hewlett-Packard Co
David Mosberger-Tange6b9f352007-08-22 13:02:09 -06003 Copyright (C) 2007 David Mosberger-Tang
4 Contributed by David Mosberger-Tang <dmosberger@gmail.com>
hp.com!davidmd5ab8982005-05-20 11:28:16 +00005
6This file is part of libunwind.
7
8Permission is hereby granted, free of charge, to any person obtaining
9a copy of this software and associated documentation files (the
10"Software"), to deal in the Software without restriction, including
11without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense, and/or sell copies of the Software, and to
13permit persons to whom the Software is furnished to do so, subject to
14the following conditions:
15
16The above copyright notice and this permission notice shall be
17included in all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25WITH 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#ifdef HAVE_CONFIG_H
35# include "config.h"
36#endif
37
38#ifdef HAVE___THREAD
39 /* For now, turn off per-thread caching. It uses up too much TLS
40 memory per thread even when the thread never uses libunwind at
41 all. */
42# undef HAVE___THREAD
43#endif
44
45/* Platform-independent libunwind-internal declarations. */
46
47#include <sys/types.h> /* HP-UX needs this before include of pthread.h */
48
49#include <assert.h>
50#include <libunwind.h>
51#include <pthread.h>
52#include <signal.h>
53#include <stdlib.h>
54#include <string.h>
55#include <unistd.h>
Ken Werner2c865b62011-10-20 16:51:45 +000056#include <sys/mman.h>
Ken Werner91494b72011-10-25 15:19:49 +000057#include <elf.h>
hp.com!davidmd5ab8982005-05-20 11:28:16 +000058
Konstantin Belousov905034c2010-03-06 00:41:37 +020059#if defined(HAVE_ENDIAN_H)
hp.com!davidmd5ab8982005-05-20 11:28:16 +000060# include <endian.h>
Konstantin Belousov905034c2010-03-06 00:41:37 +020061#elif defined(HAVE_SYS_ENDIAN_H)
62# include <sys/endian.h>
hp.com!davidmd5ab8982005-05-20 11:28:16 +000063#else
64# define __LITTLE_ENDIAN 1234
65# define __BIG_ENDIAN 4321
66# if defined(__hpux)
67# define __BYTE_ORDER __BIG_ENDIAN
68# else
69# error Host has unknown byte-order.
70# endif
71#endif
72
73#ifdef __GNUC__
74# define UNUSED __attribute__((unused))
75# define NORETURN __attribute__((noreturn))
76# define ALIAS(name) __attribute__((alias (#name)))
77# if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
78# define ALWAYS_INLINE inline __attribute__((always_inline))
79# define HIDDEN __attribute__((visibility ("hidden")))
80# define PROTECTED __attribute__((visibility ("protected")))
81# else
82# define ALWAYS_INLINE
83# define HIDDEN
84# define PROTECTED
85# endif
86# if (__GNUC__ >= 3)
87# define likely(x) __builtin_expect ((x), 1)
88# define unlikely(x) __builtin_expect ((x), 0)
89# else
90# define likely(x) (x)
91# define unlikely(x) (x)
92# endif
93#else
94# define ALWAYS_INLINE
95# define UNUSED
96# define NORETURN
97# define ALIAS(name)
98# define HIDDEN
99# define PROTECTED
100# define likely(x) (x)
101# define unlikely(x) (x)
102#endif
103
104#ifdef DEBUG
105# define UNW_DEBUG 1
106#else
107# define UNW_DEBUG 0
108#endif
109
110#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
111
112/* Make it easy to write thread-safe code which may or may not be
113 linked against libpthread. The macros below can be used
114 unconditionally and if -lpthread is around, they'll call the
115 corresponding routines otherwise, they do nothing. */
116
117#pragma weak pthread_mutex_init
118#pragma weak pthread_mutex_lock
119#pragma weak pthread_mutex_unlock
120
121#define mutex_init(l) \
122 (pthread_mutex_init != 0 ? pthread_mutex_init ((l), 0) : 0)
123#define mutex_lock(l) \
124 (pthread_mutex_lock != 0 ? pthread_mutex_lock (l) : 0)
125#define mutex_unlock(l) \
126 (pthread_mutex_unlock != 0 ? pthread_mutex_unlock (l) : 0)
127
128#ifdef HAVE_ATOMIC_OPS_H
129# include <atomic_ops.h>
130static inline int
131cmpxchg_ptr (void *addr, void *old, void *new)
132{
133 union
134 {
135 void *vp;
136 AO_t *aop;
137 }
138 u;
139
140 u.vp = addr;
141 return AO_compare_and_swap(u.aop, (AO_t) old, (AO_t) new);
142}
143# define fetch_and_add1(_ptr) AO_fetch_and_add1(_ptr)
144 /* GCC 3.2.0 on HP-UX crashes on cmpxchg_ptr() */
145# if !(defined(__hpux) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
146# define HAVE_CMPXCHG
147# endif
148# define HAVE_FETCH_AND_ADD1
149#else
150# ifdef HAVE_IA64INTRIN_H
151# include <ia64intrin.h>
152static inline int
153cmpxchg_ptr (void *addr, void *old, void *new)
154{
155 union
156 {
157 void *vp;
158 long *vlp;
159 }
160 u;
161
162 u.vp = addr;
163 return __sync_bool_compare_and_swap(u.vlp, (long) old, (long) new);
164}
165# define fetch_and_add1(_ptr) __sync_fetch_and_add(_ptr, 1)
166# define HAVE_CMPXCHG
167# define HAVE_FETCH_AND_ADD1
168# endif
169#endif
170#define atomic_read(ptr) (*(ptr))
171
172#define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
173#define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
174
175#define unwi_full_mask UNWI_ARCH_OBJ(full_mask)
176
177/* Type of a mask that can be used to inhibit preemption. At the
178 userlevel, preemption is caused by signals and hence sigset_t is
179 appropriate. In constrast, the Linux kernel uses "unsigned long"
180 to hold the processor "flags" instead. */
181typedef sigset_t intrmask_t;
182
183extern intrmask_t unwi_full_mask;
184
Arun Sharma491d5762009-10-16 14:01:50 -0700185/* Silence compiler warnings about variables which are used only if libunwind
186 is configured in a certain way */
187static inline void mark_as_used(void *v) {
188}
189
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700190#if defined(CONFIG_BLOCK_SIGNALS)
Arun Sharmaaf9daf62009-10-15 19:29:49 -0700191# define SIGPROCMASK(how, new_mask, old_mask) \
192 sigprocmask((how), (new_mask), (old_mask))
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700193#else
Arun Sharma4ab26bc2009-10-16 15:52:44 -0700194# define SIGPROCMASK(how, new_mask, old_mask) mark_as_used(old_mask)
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700195#endif
196
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000197#define define_lock(name) \
198 pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
199#define lock_init(l) mutex_init (l)
200#define lock_acquire(l,m) \
201do { \
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700202 SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &(m)); \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000203 mutex_lock (l); \
204} while (0)
205#define lock_release(l,m) \
206do { \
207 mutex_unlock (l); \
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700208 SIGPROCMASK (SIG_SETMASK, &(m), NULL); \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000209} while (0)
210
211#define SOS_MEMORY_SIZE 16384 /* see src/mi/mempool.c */
212
Konstantin Belousov8ccebc92010-03-06 16:23:24 +0200213#ifndef MAP_ANONYMOUS
214# define MAP_ANONYMOUS MAP_ANON
215#endif
Arun Sharmac0a9d0c2011-01-23 18:06:51 -0800216#define GET_MEMORY(mem, size) \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000217do { \
218 /* Hopefully, mmap() goes straight through to a system call stub... */ \
219 mem = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, \
220 -1, 0); \
221 if (mem == MAP_FAILED) \
222 mem = NULL; \
223} while (0)
224
225#define unwi_find_dynamic_proc_info UNWI_OBJ(find_dynamic_proc_info)
226#define unwi_extract_dynamic_proc_info UNWI_OBJ(extract_dynamic_proc_info)
227#define unwi_put_dynamic_unwind_info UNWI_OBJ(put_dynamic_unwind_info)
228#define unwi_dyn_remote_find_proc_info UNWI_OBJ(dyn_remote_find_proc_info)
229#define unwi_dyn_remote_put_unwind_info UNWI_OBJ(dyn_remote_put_unwind_info)
230#define unwi_dyn_validate_cache UNWI_OBJ(dyn_validate_cache)
231
232extern int unwi_find_dynamic_proc_info (unw_addr_space_t as,
233 unw_word_t ip,
234 unw_proc_info_t *pi,
235 int need_unwind_info, void *arg);
236extern int unwi_extract_dynamic_proc_info (unw_addr_space_t as,
237 unw_word_t ip,
238 unw_proc_info_t *pi,
239 unw_dyn_info_t *di,
240 int need_unwind_info,
241 void *arg);
242extern void unwi_put_dynamic_unwind_info (unw_addr_space_t as,
243 unw_proc_info_t *pi, void *arg);
244
245/* These handle the remote (cross-address-space) case of accessing
246 dynamic unwind info. */
247
248extern int unwi_dyn_remote_find_proc_info (unw_addr_space_t as,
249 unw_word_t ip,
250 unw_proc_info_t *pi,
251 int need_unwind_info,
252 void *arg);
253extern void unwi_dyn_remote_put_unwind_info (unw_addr_space_t as,
254 unw_proc_info_t *pi,
255 void *arg);
256extern int unwi_dyn_validate_cache (unw_addr_space_t as, void *arg);
257
258extern unw_dyn_info_list_t _U_dyn_info_list;
259extern pthread_mutex_t _U_dyn_info_list_lock;
260
261#if UNW_DEBUG
262#define unwi_debug_level UNWI_ARCH_OBJ(debug_level)
263extern long unwi_debug_level;
264
265# include <stdio.h>
266# define Debug(level,format...) \
267do { \
268 if (unwi_debug_level >= level) \
269 { \
270 int _n = level; \
271 if (_n > 16) \
272 _n = 16; \
273 fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__); \
274 fprintf (stderr, format); \
275 } \
276} while (0)
Arun Sharmaec53de82009-03-16 18:42:27 +0000277# define Dprintf(format...) fprintf (stderr, format)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000278# ifdef __GNUC__
279# undef inline
280# define inline UNUSED
281# endif
282#else
283# define Debug(level,format...)
Arun Sharmaec53de82009-03-16 18:42:27 +0000284# define Dprintf(format...)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000285#endif
286
Arun Sharma491d5762009-10-16 14:01:50 -0700287static ALWAYS_INLINE int
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000288print_error (const char *string)
289{
Arun Sharma491d5762009-10-16 14:01:50 -0700290 return write (2, string, strlen (string));
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000291}
292
293#define mi_init UNWI_ARCH_OBJ(mi_init)
294
295extern void mi_init (void); /* machine-independent initializations */
296extern unw_word_t _U_dyn_info_list_addr (void);
297
298/* This is needed/used by ELF targets only. */
299
300struct elf_image
301 {
302 void *image; /* pointer to mmap'd image */
303 size_t size; /* (file-) size of the image */
304 };
305
Arun Sharma25ee9f82012-03-12 18:45:08 -0700306struct elf_dyn_info
307 {
308 struct elf_image ei;
309 unw_dyn_info_t di_cache;
310 unw_dyn_info_t di_debug; /* additional table info for .debug_frame */
311#if UNW_TARGET_IA64
312 unw_dyn_info_t ktab;
313#endif
314#if UNW_TARGET_ARM
315 unw_dyn_info_t di_arm; /* additional table info for .ARM.exidx */
316#endif
317 };
318
Tommi Rantala18c26d42012-08-12 21:24:35 +0300319static inline void invalidate_edi (struct elf_dyn_info *edi)
Arun Sharma25ee9f82012-03-12 18:45:08 -0700320{
321 if (edi->ei.image)
322 munmap (edi->ei.image, edi->ei.size);
323 memset (edi, 0, sizeof (*edi));
324 edi->di_cache.format = -1;
325 edi->di_debug.format = -1;
326#if UNW_TARGET_ARM
327 edi->di_arm.format = -1;
328#endif
329}
330
331
Lassi Tuuraae5c1f22011-04-17 20:33:09 -0700332/* Provide a place holder for architecture to override for fast access
333 to memory when known not to need to validate and know the access
334 will be local to the process. A suitable override will improve
335 unw_tdep_trace() performance in particular. */
336#define ACCESS_MEM_FAST(ret,validate,cur,addr,to) \
337 do { (ret) = dwarf_get ((cur), DWARF_MEM_LOC ((cur), (addr)), &(to)); } \
338 while (0)
339
Ken Werner91494b72011-10-25 15:19:49 +0000340/* Define GNU and processor specific values for the Phdr p_type field in case
341 they aren't defined by <elf.h>. */
342#ifndef PT_GNU_EH_FRAME
343# define PT_GNU_EH_FRAME 0x6474e550
344#endif /* !PT_GNU_EH_FRAME */
345#ifndef PT_ARM_EXIDX
346# define PT_ARM_EXIDX 0x70000001 /* ARM unwind segment */
347#endif /* !PT_ARM_EXIDX */
348
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000349#include "tdep/libunwind_i.h"
350
David Mosberger-Tange6b9f352007-08-22 13:02:09 -0600351#ifndef tdep_get_func_addr
352# define tdep_get_func_addr(as,addr,v) (*(v) = addr, 0)
353#endif
354
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000355#endif /* libunwind_i_h */