blob: 7309234759b9b8828874ea177097a4e5bf3a67da [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
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of libunwind.
6
7Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
26/* This files contains libunwind-internal definitions which are
27 subject to frequent change and are not to be exposed to
28 libunwind-users. */
29
30#ifndef libunwind_i_h
31#define libunwind_i_h
32
33#ifdef HAVE_CONFIG_H
34# include "config.h"
35#endif
36
37#ifdef HAVE___THREAD
38 /* For now, turn off per-thread caching. It uses up too much TLS
39 memory per thread even when the thread never uses libunwind at
40 all. */
41# undef HAVE___THREAD
42#endif
43
44/* Platform-independent libunwind-internal declarations. */
45
46#include <sys/types.h> /* HP-UX needs this before include of pthread.h */
47
48#include <assert.h>
49#include <libunwind.h>
50#include <pthread.h>
51#include <signal.h>
52#include <stdlib.h>
53#include <string.h>
54#include <unistd.h>
55
56#ifdef HAVE_ENDIAN_H
57# include <endian.h>
58#else
59# define __LITTLE_ENDIAN 1234
60# define __BIG_ENDIAN 4321
61# if defined(__hpux)
62# define __BYTE_ORDER __BIG_ENDIAN
63# else
64# error Host has unknown byte-order.
65# endif
66#endif
67
68#ifdef __GNUC__
69# define UNUSED __attribute__((unused))
70# define NORETURN __attribute__((noreturn))
71# define ALIAS(name) __attribute__((alias (#name)))
72# if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
73# define ALWAYS_INLINE inline __attribute__((always_inline))
74# define HIDDEN __attribute__((visibility ("hidden")))
75# define PROTECTED __attribute__((visibility ("protected")))
76# else
77# define ALWAYS_INLINE
78# define HIDDEN
79# define PROTECTED
80# endif
81# if (__GNUC__ >= 3)
82# define likely(x) __builtin_expect ((x), 1)
83# define unlikely(x) __builtin_expect ((x), 0)
84# else
85# define likely(x) (x)
86# define unlikely(x) (x)
87# endif
88#else
89# define ALWAYS_INLINE
90# define UNUSED
91# define NORETURN
92# define ALIAS(name)
93# define HIDDEN
94# define PROTECTED
95# define likely(x) (x)
96# define unlikely(x) (x)
97#endif
98
99#ifdef DEBUG
100# define UNW_DEBUG 1
101#else
102# define UNW_DEBUG 0
103#endif
104
105#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
106
107/* Make it easy to write thread-safe code which may or may not be
108 linked against libpthread. The macros below can be used
109 unconditionally and if -lpthread is around, they'll call the
110 corresponding routines otherwise, they do nothing. */
111
112#pragma weak pthread_mutex_init
113#pragma weak pthread_mutex_lock
114#pragma weak pthread_mutex_unlock
115
116#define mutex_init(l) \
117 (pthread_mutex_init != 0 ? pthread_mutex_init ((l), 0) : 0)
118#define mutex_lock(l) \
119 (pthread_mutex_lock != 0 ? pthread_mutex_lock (l) : 0)
120#define mutex_unlock(l) \
121 (pthread_mutex_unlock != 0 ? pthread_mutex_unlock (l) : 0)
122
123#ifdef HAVE_ATOMIC_OPS_H
124# include <atomic_ops.h>
125static inline int
126cmpxchg_ptr (void *addr, void *old, void *new)
127{
128 union
129 {
130 void *vp;
131 AO_t *aop;
132 }
133 u;
134
135 u.vp = addr;
136 return AO_compare_and_swap(u.aop, (AO_t) old, (AO_t) new);
137}
138# define fetch_and_add1(_ptr) AO_fetch_and_add1(_ptr)
139 /* GCC 3.2.0 on HP-UX crashes on cmpxchg_ptr() */
140# if !(defined(__hpux) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
141# define HAVE_CMPXCHG
142# endif
143# define HAVE_FETCH_AND_ADD1
144#else
145# ifdef HAVE_IA64INTRIN_H
146# include <ia64intrin.h>
147static inline int
148cmpxchg_ptr (void *addr, void *old, void *new)
149{
150 union
151 {
152 void *vp;
153 long *vlp;
154 }
155 u;
156
157 u.vp = addr;
158 return __sync_bool_compare_and_swap(u.vlp, (long) old, (long) new);
159}
160# define fetch_and_add1(_ptr) __sync_fetch_and_add(_ptr, 1)
161# define HAVE_CMPXCHG
162# define HAVE_FETCH_AND_ADD1
163# endif
164#endif
165#define atomic_read(ptr) (*(ptr))
166
167#define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
168#define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
169
170#define unwi_full_mask UNWI_ARCH_OBJ(full_mask)
171
172/* Type of a mask that can be used to inhibit preemption. At the
173 userlevel, preemption is caused by signals and hence sigset_t is
174 appropriate. In constrast, the Linux kernel uses "unsigned long"
175 to hold the processor "flags" instead. */
176typedef sigset_t intrmask_t;
177
178extern intrmask_t unwi_full_mask;
179
180#define define_lock(name) \
181 pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
182#define lock_init(l) mutex_init (l)
183#define lock_acquire(l,m) \
184do { \
185 sigprocmask (SIG_SETMASK, &unwi_full_mask, &(m)); \
186 mutex_lock (l); \
187} while (0)
188#define lock_release(l,m) \
189do { \
190 mutex_unlock (l); \
191 sigprocmask (SIG_SETMASK, &(m), NULL); \
192} while (0)
193
194#define SOS_MEMORY_SIZE 16384 /* see src/mi/mempool.c */
195
196#define GET_MEMORY(mem, size_in_bytes) \
197do { \
198 /* Hopefully, mmap() goes straight through to a system call stub... */ \
199 mem = mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, \
200 -1, 0); \
201 if (mem == MAP_FAILED) \
202 mem = NULL; \
203} while (0)
204
205#define unwi_find_dynamic_proc_info UNWI_OBJ(find_dynamic_proc_info)
206#define unwi_extract_dynamic_proc_info UNWI_OBJ(extract_dynamic_proc_info)
207#define unwi_put_dynamic_unwind_info UNWI_OBJ(put_dynamic_unwind_info)
208#define unwi_dyn_remote_find_proc_info UNWI_OBJ(dyn_remote_find_proc_info)
209#define unwi_dyn_remote_put_unwind_info UNWI_OBJ(dyn_remote_put_unwind_info)
210#define unwi_dyn_validate_cache UNWI_OBJ(dyn_validate_cache)
211
212extern int unwi_find_dynamic_proc_info (unw_addr_space_t as,
213 unw_word_t ip,
214 unw_proc_info_t *pi,
215 int need_unwind_info, void *arg);
216extern int unwi_extract_dynamic_proc_info (unw_addr_space_t as,
217 unw_word_t ip,
218 unw_proc_info_t *pi,
219 unw_dyn_info_t *di,
220 int need_unwind_info,
221 void *arg);
222extern void unwi_put_dynamic_unwind_info (unw_addr_space_t as,
223 unw_proc_info_t *pi, void *arg);
224
225/* These handle the remote (cross-address-space) case of accessing
226 dynamic unwind info. */
227
228extern int unwi_dyn_remote_find_proc_info (unw_addr_space_t as,
229 unw_word_t ip,
230 unw_proc_info_t *pi,
231 int need_unwind_info,
232 void *arg);
233extern void unwi_dyn_remote_put_unwind_info (unw_addr_space_t as,
234 unw_proc_info_t *pi,
235 void *arg);
236extern int unwi_dyn_validate_cache (unw_addr_space_t as, void *arg);
237
238extern unw_dyn_info_list_t _U_dyn_info_list;
239extern pthread_mutex_t _U_dyn_info_list_lock;
240
241#if UNW_DEBUG
242#define unwi_debug_level UNWI_ARCH_OBJ(debug_level)
243extern long unwi_debug_level;
244
245# include <stdio.h>
246# define Debug(level,format...) \
247do { \
248 if (unwi_debug_level >= level) \
249 { \
250 int _n = level; \
251 if (_n > 16) \
252 _n = 16; \
253 fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__); \
254 fprintf (stderr, format); \
255 } \
256} while (0)
257# define dprintf(format...) fprintf (stderr, format)
258# ifdef __GNUC__
259# undef inline
260# define inline UNUSED
261# endif
262#else
263# define Debug(level,format...)
264# define dprintf(format...)
265#endif
266
267static ALWAYS_INLINE void
268print_error (const char *string)
269{
270 write (2, string, strlen (string));
271}
272
273#define mi_init UNWI_ARCH_OBJ(mi_init)
274
275extern void mi_init (void); /* machine-independent initializations */
276extern unw_word_t _U_dyn_info_list_addr (void);
277
278/* This is needed/used by ELF targets only. */
279
280struct elf_image
281 {
282 void *image; /* pointer to mmap'd image */
283 size_t size; /* (file-) size of the image */
284 };
285
286#include "tdep/libunwind_i.h"
287
288#endif /* libunwind_i_h */