blob: 6bbeb3efcd1b2433e1f29910109ad66acf2ed8db [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>
hp.com!davidmd5ab8982005-05-20 11:28:16 +000057
Konstantin Belousov905034c2010-03-06 00:41:37 +020058#if defined(HAVE_ENDIAN_H)
hp.com!davidmd5ab8982005-05-20 11:28:16 +000059# include <endian.h>
Konstantin Belousov905034c2010-03-06 00:41:37 +020060#elif defined(HAVE_SYS_ENDIAN_H)
61# include <sys/endian.h>
hp.com!davidmd5ab8982005-05-20 11:28:16 +000062#else
63# define __LITTLE_ENDIAN 1234
64# define __BIG_ENDIAN 4321
65# if defined(__hpux)
66# define __BYTE_ORDER __BIG_ENDIAN
67# else
68# error Host has unknown byte-order.
69# endif
70#endif
71
72#ifdef __GNUC__
73# define UNUSED __attribute__((unused))
74# define NORETURN __attribute__((noreturn))
75# define ALIAS(name) __attribute__((alias (#name)))
76# if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
77# define ALWAYS_INLINE inline __attribute__((always_inline))
78# define HIDDEN __attribute__((visibility ("hidden")))
79# define PROTECTED __attribute__((visibility ("protected")))
80# else
81# define ALWAYS_INLINE
82# define HIDDEN
83# define PROTECTED
84# endif
85# if (__GNUC__ >= 3)
86# define likely(x) __builtin_expect ((x), 1)
87# define unlikely(x) __builtin_expect ((x), 0)
88# else
89# define likely(x) (x)
90# define unlikely(x) (x)
91# endif
92#else
93# define ALWAYS_INLINE
94# define UNUSED
95# define NORETURN
96# define ALIAS(name)
97# define HIDDEN
98# define PROTECTED
99# define likely(x) (x)
100# define unlikely(x) (x)
101#endif
102
103#ifdef DEBUG
104# define UNW_DEBUG 1
105#else
106# define UNW_DEBUG 0
107#endif
108
109#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
110
111/* Make it easy to write thread-safe code which may or may not be
112 linked against libpthread. The macros below can be used
113 unconditionally and if -lpthread is around, they'll call the
114 corresponding routines otherwise, they do nothing. */
115
116#pragma weak pthread_mutex_init
117#pragma weak pthread_mutex_lock
118#pragma weak pthread_mutex_unlock
119
120#define mutex_init(l) \
121 (pthread_mutex_init != 0 ? pthread_mutex_init ((l), 0) : 0)
122#define mutex_lock(l) \
123 (pthread_mutex_lock != 0 ? pthread_mutex_lock (l) : 0)
124#define mutex_unlock(l) \
125 (pthread_mutex_unlock != 0 ? pthread_mutex_unlock (l) : 0)
126
127#ifdef HAVE_ATOMIC_OPS_H
128# include <atomic_ops.h>
129static inline int
130cmpxchg_ptr (void *addr, void *old, void *new)
131{
132 union
133 {
134 void *vp;
135 AO_t *aop;
136 }
137 u;
138
139 u.vp = addr;
140 return AO_compare_and_swap(u.aop, (AO_t) old, (AO_t) new);
141}
142# define fetch_and_add1(_ptr) AO_fetch_and_add1(_ptr)
143 /* GCC 3.2.0 on HP-UX crashes on cmpxchg_ptr() */
144# if !(defined(__hpux) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
145# define HAVE_CMPXCHG
146# endif
147# define HAVE_FETCH_AND_ADD1
148#else
149# ifdef HAVE_IA64INTRIN_H
150# include <ia64intrin.h>
151static inline int
152cmpxchg_ptr (void *addr, void *old, void *new)
153{
154 union
155 {
156 void *vp;
157 long *vlp;
158 }
159 u;
160
161 u.vp = addr;
162 return __sync_bool_compare_and_swap(u.vlp, (long) old, (long) new);
163}
164# define fetch_and_add1(_ptr) __sync_fetch_and_add(_ptr, 1)
165# define HAVE_CMPXCHG
166# define HAVE_FETCH_AND_ADD1
167# endif
168#endif
169#define atomic_read(ptr) (*(ptr))
170
171#define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
172#define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
173
174#define unwi_full_mask UNWI_ARCH_OBJ(full_mask)
175
176/* Type of a mask that can be used to inhibit preemption. At the
177 userlevel, preemption is caused by signals and hence sigset_t is
178 appropriate. In constrast, the Linux kernel uses "unsigned long"
179 to hold the processor "flags" instead. */
180typedef sigset_t intrmask_t;
181
182extern intrmask_t unwi_full_mask;
183
Arun Sharma491d5762009-10-16 14:01:50 -0700184/* Silence compiler warnings about variables which are used only if libunwind
185 is configured in a certain way */
186static inline void mark_as_used(void *v) {
187}
188
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700189#if defined(CONFIG_BLOCK_SIGNALS)
Arun Sharmaaf9daf62009-10-15 19:29:49 -0700190# define SIGPROCMASK(how, new_mask, old_mask) \
191 sigprocmask((how), (new_mask), (old_mask))
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700192#else
Arun Sharma4ab26bc2009-10-16 15:52:44 -0700193# define SIGPROCMASK(how, new_mask, old_mask) mark_as_used(old_mask)
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700194#endif
195
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000196#define define_lock(name) \
197 pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
198#define lock_init(l) mutex_init (l)
199#define lock_acquire(l,m) \
200do { \
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700201 SIGPROCMASK (SIG_SETMASK, &unwi_full_mask, &(m)); \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000202 mutex_lock (l); \
203} while (0)
204#define lock_release(l,m) \
205do { \
206 mutex_unlock (l); \
Paul Pluzhnikov9aa0d6d2009-09-21 13:04:33 -0700207 SIGPROCMASK (SIG_SETMASK, &(m), NULL); \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000208} while (0)
209
210#define SOS_MEMORY_SIZE 16384 /* see src/mi/mempool.c */
211
Konstantin Belousov8ccebc92010-03-06 16:23:24 +0200212#ifndef MAP_ANONYMOUS
213# define MAP_ANONYMOUS MAP_ANON
214#endif
Arun Sharmac0a9d0c2011-01-23 18:06:51 -0800215#define GET_MEMORY(mem, size) \
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000216do { \
217 /* Hopefully, mmap() goes straight through to a system call stub... */ \
218 mem = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, \
219 -1, 0); \
220 if (mem == MAP_FAILED) \
221 mem = NULL; \
222} while (0)
223
224#define unwi_find_dynamic_proc_info UNWI_OBJ(find_dynamic_proc_info)
225#define unwi_extract_dynamic_proc_info UNWI_OBJ(extract_dynamic_proc_info)
226#define unwi_put_dynamic_unwind_info UNWI_OBJ(put_dynamic_unwind_info)
227#define unwi_dyn_remote_find_proc_info UNWI_OBJ(dyn_remote_find_proc_info)
228#define unwi_dyn_remote_put_unwind_info UNWI_OBJ(dyn_remote_put_unwind_info)
229#define unwi_dyn_validate_cache UNWI_OBJ(dyn_validate_cache)
230
231extern int unwi_find_dynamic_proc_info (unw_addr_space_t as,
232 unw_word_t ip,
233 unw_proc_info_t *pi,
234 int need_unwind_info, void *arg);
235extern int unwi_extract_dynamic_proc_info (unw_addr_space_t as,
236 unw_word_t ip,
237 unw_proc_info_t *pi,
238 unw_dyn_info_t *di,
239 int need_unwind_info,
240 void *arg);
241extern void unwi_put_dynamic_unwind_info (unw_addr_space_t as,
242 unw_proc_info_t *pi, void *arg);
243
244/* These handle the remote (cross-address-space) case of accessing
245 dynamic unwind info. */
246
247extern int unwi_dyn_remote_find_proc_info (unw_addr_space_t as,
248 unw_word_t ip,
249 unw_proc_info_t *pi,
250 int need_unwind_info,
251 void *arg);
252extern void unwi_dyn_remote_put_unwind_info (unw_addr_space_t as,
253 unw_proc_info_t *pi,
254 void *arg);
255extern int unwi_dyn_validate_cache (unw_addr_space_t as, void *arg);
256
257extern unw_dyn_info_list_t _U_dyn_info_list;
258extern pthread_mutex_t _U_dyn_info_list_lock;
259
260#if UNW_DEBUG
261#define unwi_debug_level UNWI_ARCH_OBJ(debug_level)
262extern long unwi_debug_level;
263
264# include <stdio.h>
265# define Debug(level,format...) \
266do { \
267 if (unwi_debug_level >= level) \
268 { \
269 int _n = level; \
270 if (_n > 16) \
271 _n = 16; \
272 fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__); \
273 fprintf (stderr, format); \
274 } \
275} while (0)
Arun Sharmaec53de82009-03-16 18:42:27 +0000276# define Dprintf(format...) fprintf (stderr, format)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000277# ifdef __GNUC__
278# undef inline
279# define inline UNUSED
280# endif
281#else
282# define Debug(level,format...)
Arun Sharmaec53de82009-03-16 18:42:27 +0000283# define Dprintf(format...)
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000284#endif
285
Arun Sharma491d5762009-10-16 14:01:50 -0700286static ALWAYS_INLINE int
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000287print_error (const char *string)
288{
Arun Sharma491d5762009-10-16 14:01:50 -0700289 return write (2, string, strlen (string));
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000290}
291
292#define mi_init UNWI_ARCH_OBJ(mi_init)
293
294extern void mi_init (void); /* machine-independent initializations */
295extern unw_word_t _U_dyn_info_list_addr (void);
296
297/* This is needed/used by ELF targets only. */
298
299struct elf_image
300 {
301 void *image; /* pointer to mmap'd image */
302 size_t size; /* (file-) size of the image */
303 };
304
Lassi Tuuraae5c1f22011-04-17 20:33:09 -0700305/* Provide a place holder for architecture to override for fast access
306 to memory when known not to need to validate and know the access
307 will be local to the process. A suitable override will improve
308 unw_tdep_trace() performance in particular. */
309#define ACCESS_MEM_FAST(ret,validate,cur,addr,to) \
310 do { (ret) = dwarf_get ((cur), DWARF_MEM_LOC ((cur), (addr)), &(to)); } \
311 while (0)
312
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000313#include "tdep/libunwind_i.h"
314
David Mosberger-Tange6b9f352007-08-22 13:02:09 -0600315#ifndef tdep_get_func_addr
316# define tdep_get_func_addr(as,addr,v) (*(v) = addr, 0)
317#endif
318
hp.com!davidmd5ab8982005-05-20 11:28:16 +0000319#endif /* libunwind_i_h */