blob: 9b95161ae36e79ff76aa5d492f010bdf7c2ce6ec [file] [log] [blame]
mostang.com!davidmbf070a92002-12-19 07:16:50 +00001/* libunwind - a platform-independent unwind library
mostang.com!davidmc1bbbb22004-01-20 23:32:28 +00002 Copyright (C) 2001-2004 Hewlett-Packard Co
mostang.com!davidmbf070a92002-12-19 07:16:50 +00003 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#ifndef internal_h
27#define internal_h
28
mostang.com!davidm15693e22003-03-19 19:25:18 +000029#ifdef HAVE_CONFIG_H
30# include "config.h"
31#endif
32
hp.com!davidm1334cae2003-12-04 07:30:39 +000033#ifdef HAVE___THREAD
34 /* For now, turn off per-thread caching. It uses up too much TLS
35 memory per thread even when the thread never uses libunwind at
36 all. */
37# undef HAVE___THREAD
38#endif
39
mostang.com!davidmbf070a92002-12-19 07:16:50 +000040/* Platform-independent libunwind-internal declarations. */
41
mostang.com!davidm15693e22003-03-19 19:25:18 +000042#include <sys/types.h> /* HP-UX needs this before include of pthread.h */
43
mostang.com!davidmbf070a92002-12-19 07:16:50 +000044#include <assert.h>
mostang.com!davidmbf070a92002-12-19 07:16:50 +000045#include <libunwind.h>
mostang.com!davidm9003fd22003-03-06 06:14:36 +000046#include <pthread.h>
47#include <signal.h>
hp.com!davidm23024e22004-01-03 10:50:24 +000048#include <string.h>
49#include <unistd.h>
mostang.com!davidmbf070a92002-12-19 07:16:50 +000050
mostang.com!davidma04f38e2003-04-03 07:59:15 +000051#ifdef HAVE_ENDIAN_H
52# include <endian.h>
53#else
54# define __LITTLE_ENDIAN 1234
55# define __BIG_ENDIAN 4321
56# if defined(__hpux)
hp.com!davidm039accf2003-10-11 01:05:18 +000057# define __BYTE_ORDER __BIG_ENDIAN
mostang.com!davidma04f38e2003-04-03 07:59:15 +000058# else
59# error Host has unknown byte-order.
60# endif
61#endif
62
mostang.com!davidmbf070a92002-12-19 07:16:50 +000063#ifdef __GNUC__
hp.com!davidm04dd29b2003-11-27 06:52:54 +000064# define UNUSED __attribute__((unused))
hp.com!davidm23024e22004-01-03 10:50:24 +000065# define NORETURN __attribute__((noreturn))
mostang.com!davidmf5265f52003-02-08 10:10:59 +000066# if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
hp.com!davidm1334cae2003-12-04 07:30:39 +000067# define ALWAYS_INLINE __attribute__((always_inline))
mostang.com!davidmf5265f52003-02-08 10:10:59 +000068# define HIDDEN __attribute__((visibility ("hidden")))
hp.com!davidm23024e22004-01-03 10:50:24 +000069# define PROTECTED __attribute__((visibility ("protected")))
mostang.com!davidmf5265f52003-02-08 10:10:59 +000070# else
hp.com!davidm1334cae2003-12-04 07:30:39 +000071# define ALWAYS_INLINE
mostang.com!davidmf5265f52003-02-08 10:10:59 +000072# define HIDDEN
hp.com!davidm23024e22004-01-03 10:50:24 +000073# define PROTECTED
mostang.com!davidmf5265f52003-02-08 10:10:59 +000074# endif
hp.com!davidma45c8882003-02-15 03:10:30 +000075# if (__GNUC__ >= 3)
76# define likely(x) __builtin_expect ((x), 1)
77# define unlikely(x) __builtin_expect ((x), 0)
78# else
79# define likely(x) (x)
80# define unlikely(x) (x)
81# endif
mostang.com!davidmbf070a92002-12-19 07:16:50 +000082#else
hp.com!davidm1334cae2003-12-04 07:30:39 +000083# define ALWAYS_INLINE
hp.com!davidm04dd29b2003-11-27 06:52:54 +000084# define UNUSED
hp.com!davidm23024e22004-01-03 10:50:24 +000085# define NORETURN
mostang.com!davidmbf070a92002-12-19 07:16:50 +000086# define HIDDEN
hp.com!davidm23024e22004-01-03 10:50:24 +000087# define PROTECTED
hp.com!davidma45c8882003-02-15 03:10:30 +000088# define likely(x) (x)
89# define unlikely(x) (x)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000090#endif
91
92#ifdef DEBUG
93# define UNW_DEBUG 1
hp.com!davidm04dd29b2003-11-27 06:52:54 +000094#else
95# define UNW_DEBUG 0
mostang.com!davidmbf070a92002-12-19 07:16:50 +000096#endif
97
98#if UNW_DEBUG
99# include <stdio.h>
hp.com!davidm7d3de042003-12-20 11:32:43 +0000100# define debug(level,format...) \
101do { \
102 if (tdep_debug_level > level) fprintf (stderr, format); \
103} while (0)
104# define Debug(level,format...) \
105do { \
106 if (tdep_debug_level > level) \
107 { \
108 int _n = level; \
109 if (_n > 16) \
110 _n = 16; \
111 fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__); \
112 fprintf (stderr, format); \
113 } \
114} while (0)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000115# define dprintf(format...) \
hp.com!davidma45c8882003-02-15 03:10:30 +0000116 fprintf (stderr, format)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000117# ifdef __GNUC__
hp.com!davidm90e83932003-11-24 21:37:22 +0000118# undef inline
hp.com!davidm04dd29b2003-11-27 06:52:54 +0000119# define inline UNUSED
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000120# endif
121#else
122# define debug(level,format...)
123# define dprintf(format...)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000124#endif
125
mostang.com!davidm313653f2003-01-21 08:08:32 +0000126#define NELEMS(a) (sizeof (a) / sizeof ((a)[0]))
127
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000128/* Make it easy to write thread-safe code which may or may not be
129 linked against libpthread. The macros below can be used
130 unconditionally and if -lpthread is around, they'll call the
131 corresponding routines otherwise, they do nothing. */
132
hp.com!davidm1d443a62003-03-29 07:32:50 +0000133#pragma weak pthread_mutex_init
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000134#pragma weak pthread_mutex_lock
135#pragma weak pthread_mutex_unlock
136
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000137#define mutex_init(l) (pthread_mutex_init ? pthread_mutex_init ((l), 0) : 0)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000138#define mutex_lock(l) (pthread_mutex_lock ? pthread_mutex_lock (l) : 0)
139#define mutex_unlock(l) (pthread_mutex_unlock ? pthread_mutex_unlock (l) : 0)
140
hp.com!davidm90e83932003-11-24 21:37:22 +0000141#ifdef HAVE_ATOMIC_OPS_H
142# include <atomic_ops.h>
143# define cmpxchg_ptr(_ptr,_o,_n) AO_compare_and_swap((AO_T *)_ptr, \
144 (AO_T) (_o), \
145 (AO_T) (_n))
146# define fetch_and_add1(_ptr) AO_fetch_and_add1(_ptr)
hp.com!davidm04dd29b2003-11-27 06:52:54 +0000147 /* GCC 3.2.0 on HP-UX crashes on cmpxchg_ptr() */
148# if !(defined(__hpux) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
149# define HAVE_CMPXCHG
150# endif
hp.com!davidm90e83932003-11-24 21:37:22 +0000151# define HAVE_FETCH_AND_ADD1
152#else
153# ifdef HAVE_IA64INTRIN_H
154# include <ia64intrin.h>
hp.com!davidm90e83932003-11-24 21:37:22 +0000155# define cmpxchg_ptr(_ptr,_o,_n) \
mostang.com!davidmbcdedf62003-12-05 06:42:28 +0000156 (__sync_bool_compare_and_swap((volatile long *) (_ptr), \
157 (long) (_o), (long) (_n)))
hp.com!davidm90e83932003-11-24 21:37:22 +0000158# define fetch_and_add1(_ptr) __sync_fetch_and_add(_ptr, 1)
159# define HAVE_CMPXCHG
160# define HAVE_FETCH_AND_ADD1
161# endif
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000162#endif
163
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000164#define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
165#define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
166
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000167#define unwi_full_sigmask UNWI_ARCH_OBJ(full_sigmask)
168
169extern sigset_t unwi_full_sigmask;
170
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000171extern int UNWI_OBJ(find_dynamic_proc_info) (unw_addr_space_t as,
172 unw_word_t ip,
173 unw_proc_info_t *pi,
174 int need_unwind_info, void *arg);
hp.com!davidm7d3de042003-12-20 11:32:43 +0000175extern int UNWI_OBJ(extract_dynamic_proc_info) (unw_addr_space_t as,
176 unw_word_t ip,
177 unw_proc_info_t *pi,
178 unw_dyn_info_t *di,
179 int need_unwind_info,
180 void *arg);
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000181extern void UNWI_OBJ(put_dynamic_unwind_info) (unw_addr_space_t as,
182 unw_proc_info_t *pi, void *arg);
hp.com!davidm7d3de042003-12-20 11:32:43 +0000183extern int UNWI_OBJ(dyn_remote_find_proc_info) (unw_addr_space_t as,
184 unw_word_t ip,
185 unw_proc_info_t *pi,
hp.com!davidm7d3de042003-12-20 11:32:43 +0000186 int need_unwind_info,
187 void *arg);
188extern void UNWI_OBJ(dyn_remote_put_unwind_info) (unw_addr_space_t as,
189 unw_proc_info_t *pi,
190 void *arg);
mostang.com!davidmc1bbbb22004-01-20 23:32:28 +0000191extern int UNWI_OBJ(dyn_validate_cache) (unw_addr_space_t as, void *arg);
mostang.com!davidme20ecc62003-02-22 08:19:43 +0000192extern int UNWI_OBJ(get_proc_name) (unw_addr_space_t as, unw_word_t ip,
193 char *buf, size_t buf_len,
194 unw_word_t *offp, void *arg);
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000195
196#define unwi_find_dynamic_proc_info(as,ip,pi,n,arg) \
197 UNWI_OBJ(find_dynamic_proc_info)(as, ip, pi, n, arg)
198
199#define unwi_extract_dynamic_proc_info(as,ip,pi,di,n,arg) \
hp.com!davidm7d3de042003-12-20 11:32:43 +0000200 UNWI_OBJ(extract_dynamic_proc_info)(as, ip, pi, di, n, arg)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000201
202#define unwi_put_dynamic_unwind_info(as,pi,arg) \
203 UNWI_OBJ(put_dynamic_unwind_info)(as, pi, arg)
204
205/* These handle the remote (cross-address-space) case of accessing
206 dynamic unwind info. */
207
hp.com!davidmea78d272004-01-20 01:51:17 +0000208#define unwi_dyn_remote_find_proc_info(as,i,p,n,arg) \
209 UNWI_OBJ(dyn_remote_find_proc_info)(as, i, p, n, arg)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000210
211#define unwi_dyn_remote_put_unwind_info(as,p,arg) \
hp.com!davidm7d3de042003-12-20 11:32:43 +0000212 UNWI_OBJ(dyn_remote_put_unwind_info)(as, p, arg)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000213
mostang.com!davidmc1bbbb22004-01-20 23:32:28 +0000214#define unwi_dyn_validate_cache(as, arg) \
215 UNWI_OBJ(dyn_validate_cache)(as, arg)
216
mostang.com!davidme20ecc62003-02-22 08:19:43 +0000217#define unwi_get_proc_name(as,ip,b,s,o,arg) \
218 UNWI_OBJ(get_proc_name)(as, ip, b, s, o, arg)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000219
220extern unw_dyn_info_list_t _U_dyn_info_list;
221extern pthread_mutex_t _U_dyn_info_list_lock;
222
223#define WSIZE (sizeof (unw_word_t))
224
hp.com!davidm23024e22004-01-03 10:50:24 +0000225static inline ALWAYS_INLINE void
226print_error (const char *string)
227{
228 write (2, string, strlen (string));
229}
230
231#ifdef UNW_LOCAL_ONLY
232
233static inline int
234fetch8 (unw_addr_space_t as, unw_accessors_t *a,
235 unw_word_t *addr, int8_t *valp, void *arg)
236{
237 *valp = *(int8_t *) *addr;
238 *addr += 1;
239 return 0;
240}
241
242static inline int
243fetch16 (unw_addr_space_t as, unw_accessors_t *a,
244 unw_word_t *addr, int16_t *valp, void *arg)
245{
246 *valp = *(int16_t *) *addr;
247 *addr += 2;
248 return 0;
249}
250
251static inline int
252fetch32 (unw_addr_space_t as, unw_accessors_t *a,
253 unw_word_t *addr, int32_t *valp, void *arg)
254{
255 *valp = *(int32_t *) *addr;
256 *addr += 4;
257 return 0;
258}
259
260static inline int
261fetchw (unw_addr_space_t as, unw_accessors_t *a,
262 unw_word_t *addr, unw_word_t *valp, void *arg)
263{
264 *valp = *(unw_word_t *) *addr;
265 *addr += sizeof (unw_word_t);
266 return 0;
267}
268
269#else /* !UNW_LOCAL_ONLY */
270
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000271static inline int
272fetch8 (unw_addr_space_t as, unw_accessors_t *a,
273 unw_word_t *addr, int8_t *valp, void *arg)
274{
275 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
276 int ret;
277
278 *addr += 1;
279
280 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
281
282#if __BYTE_ORDER == __LITTLE_ENDIAN
283 val >>= 8*off;
284#else
285 val >>= 8*(WSIZE - 1 - off);
286#endif
287 *valp = val & 0xff;
288 return ret;
289}
290
291static inline int
292fetch16 (unw_addr_space_t as, unw_accessors_t *a,
293 unw_word_t *addr, int16_t *valp, void *arg)
294{
295 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
296 int ret;
297
298 assert ((off & 0x1) == 0);
299
300 *addr += 2;
301
302 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
303
304#if __BYTE_ORDER == __LITTLE_ENDIAN
305 val >>= 8*off;
306#else
307 val >>= 8*(WSIZE - 2 - off);
308#endif
309 *valp = val & 0xffff;
310 return ret;
311}
312
313static inline int
314fetch32 (unw_addr_space_t as, unw_accessors_t *a,
315 unw_word_t *addr, int32_t *valp, void *arg)
316{
317 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
318 int ret;
319
320 assert ((off & 0x3) == 0);
321
322 *addr += 4;
323
324 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
325
326#if __BYTE_ORDER == __LITTLE_ENDIAN
327 val >>= 8*off;
328#else
329 val >>= 8*(WSIZE - 4 - off);
330#endif
331 *valp = val & 0xffffffff;
332 return ret;
333}
334
335static inline int
336fetchw (unw_addr_space_t as, unw_accessors_t *a,
337 unw_word_t *addr, unw_word_t *valp, void *arg)
338{
339 int ret;
340
341 ret = (*a->access_mem) (as, *addr, valp, 0, arg);
342 *addr += WSIZE;
343 return ret;
344}
345
hp.com!davidm23024e22004-01-03 10:50:24 +0000346#endif /* !UNW_LOCAL_ONLY */
347
mostang.com!davidm7e268d22003-01-23 10:04:09 +0000348#define mi_init UNWI_ARCH_OBJ(mi_init)
349
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000350extern void mi_init (void); /* machine-independent initializations */
hp.com!davidm23024e22004-01-03 10:50:24 +0000351extern unw_word_t _U_dyn_info_list_addr (void);
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000352
mostang.com!davidmf5265f52003-02-08 10:10:59 +0000353/* This is needed/used by ELF targets only. */
354
355struct elf_image
356 {
357 void *image; /* pointer to mmap'd image */
358 size_t size; /* (file-) size of the image */
359 };
360
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000361#endif /* internal_h */