blob: 8e72acafbaaba0503f40d1efaa6e397cfa63dee9 [file] [log] [blame]
mostang.com!davidmbf070a92002-12-19 07:16:50 +00001/* libunwind - a platform-independent unwind library
mostang.com!davidm313653f2003-01-21 08:08:32 +00002 Copyright (C) 2001-2003 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>
mostang.com!davidmbf070a92002-12-19 07:16:50 +000048
mostang.com!davidma04f38e2003-04-03 07:59:15 +000049#ifdef HAVE_ENDIAN_H
50# include <endian.h>
51#else
52# define __LITTLE_ENDIAN 1234
53# define __BIG_ENDIAN 4321
54# if defined(__hpux)
hp.com!davidm039accf2003-10-11 01:05:18 +000055# define __BYTE_ORDER __BIG_ENDIAN
mostang.com!davidma04f38e2003-04-03 07:59:15 +000056# else
57# error Host has unknown byte-order.
58# endif
59#endif
60
mostang.com!davidmbf070a92002-12-19 07:16:50 +000061#ifdef __GNUC__
hp.com!davidm04dd29b2003-11-27 06:52:54 +000062# define UNUSED __attribute__((unused))
mostang.com!davidmf5265f52003-02-08 10:10:59 +000063# if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
hp.com!davidm1334cae2003-12-04 07:30:39 +000064# define ALWAYS_INLINE __attribute__((always_inline))
mostang.com!davidmf5265f52003-02-08 10:10:59 +000065# define HIDDEN __attribute__((visibility ("hidden")))
66# else
hp.com!davidm1334cae2003-12-04 07:30:39 +000067# define ALWAYS_INLINE
mostang.com!davidmf5265f52003-02-08 10:10:59 +000068# define HIDDEN
69# endif
hp.com!davidma45c8882003-02-15 03:10:30 +000070# if (__GNUC__ >= 3)
71# define likely(x) __builtin_expect ((x), 1)
72# define unlikely(x) __builtin_expect ((x), 0)
73# else
74# define likely(x) (x)
75# define unlikely(x) (x)
76# endif
mostang.com!davidmbf070a92002-12-19 07:16:50 +000077#else
hp.com!davidm1334cae2003-12-04 07:30:39 +000078# define ALWAYS_INLINE
hp.com!davidm04dd29b2003-11-27 06:52:54 +000079# define UNUSED
mostang.com!davidmbf070a92002-12-19 07:16:50 +000080# define HIDDEN
hp.com!davidma45c8882003-02-15 03:10:30 +000081# define likely(x) (x)
82# define unlikely(x) (x)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000083#endif
84
85#ifdef DEBUG
86# define UNW_DEBUG 1
hp.com!davidm04dd29b2003-11-27 06:52:54 +000087#else
88# define UNW_DEBUG 0
mostang.com!davidmbf070a92002-12-19 07:16:50 +000089#endif
90
91#if UNW_DEBUG
92# include <stdio.h>
hp.com!davidm7d3de042003-12-20 11:32:43 +000093# define debug(level,format...) \
94do { \
95 if (tdep_debug_level > level) fprintf (stderr, format); \
96} while (0)
97# define Debug(level,format...) \
98do { \
99 if (tdep_debug_level > level) \
100 { \
101 int _n = level; \
102 if (_n > 16) \
103 _n = 16; \
104 fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__); \
105 fprintf (stderr, format); \
106 } \
107} while (0)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000108# define dprintf(format...) \
hp.com!davidma45c8882003-02-15 03:10:30 +0000109 fprintf (stderr, format)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000110# ifdef __GNUC__
hp.com!davidm90e83932003-11-24 21:37:22 +0000111# undef inline
hp.com!davidm04dd29b2003-11-27 06:52:54 +0000112# define inline UNUSED
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000113# endif
114#else
115# define debug(level,format...)
116# define dprintf(format...)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000117#endif
118
mostang.com!davidm313653f2003-01-21 08:08:32 +0000119#define NELEMS(a) (sizeof (a) / sizeof ((a)[0]))
120
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000121/* Make it easy to write thread-safe code which may or may not be
122 linked against libpthread. The macros below can be used
123 unconditionally and if -lpthread is around, they'll call the
124 corresponding routines otherwise, they do nothing. */
125
hp.com!davidm1d443a62003-03-29 07:32:50 +0000126#pragma weak pthread_mutex_init
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000127#pragma weak pthread_mutex_lock
128#pragma weak pthread_mutex_unlock
129
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000130#define mutex_init(l) (pthread_mutex_init ? pthread_mutex_init ((l), 0) : 0)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000131#define mutex_lock(l) (pthread_mutex_lock ? pthread_mutex_lock (l) : 0)
132#define mutex_unlock(l) (pthread_mutex_unlock ? pthread_mutex_unlock (l) : 0)
133
hp.com!davidm90e83932003-11-24 21:37:22 +0000134#ifdef HAVE_ATOMIC_OPS_H
135# include <atomic_ops.h>
136# define cmpxchg_ptr(_ptr,_o,_n) AO_compare_and_swap((AO_T *)_ptr, \
137 (AO_T) (_o), \
138 (AO_T) (_n))
139# define fetch_and_add1(_ptr) AO_fetch_and_add1(_ptr)
hp.com!davidm04dd29b2003-11-27 06:52:54 +0000140 /* GCC 3.2.0 on HP-UX crashes on cmpxchg_ptr() */
141# if !(defined(__hpux) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
142# define HAVE_CMPXCHG
143# endif
hp.com!davidm90e83932003-11-24 21:37:22 +0000144# define HAVE_FETCH_AND_ADD1
145#else
146# ifdef HAVE_IA64INTRIN_H
147# include <ia64intrin.h>
hp.com!davidm90e83932003-11-24 21:37:22 +0000148# define cmpxchg_ptr(_ptr,_o,_n) \
mostang.com!davidmbcdedf62003-12-05 06:42:28 +0000149 (__sync_bool_compare_and_swap((volatile long *) (_ptr), \
150 (long) (_o), (long) (_n)))
hp.com!davidm90e83932003-11-24 21:37:22 +0000151# define fetch_and_add1(_ptr) __sync_fetch_and_add(_ptr, 1)
152# define HAVE_CMPXCHG
153# define HAVE_FETCH_AND_ADD1
154# endif
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000155#endif
156
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000157#define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
158#define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
159
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000160#define unwi_full_sigmask UNWI_ARCH_OBJ(full_sigmask)
161
162extern sigset_t unwi_full_sigmask;
163
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000164extern int UNWI_OBJ(find_dynamic_proc_info) (unw_addr_space_t as,
165 unw_word_t ip,
166 unw_proc_info_t *pi,
167 int need_unwind_info, void *arg);
hp.com!davidm7d3de042003-12-20 11:32:43 +0000168extern int UNWI_OBJ(extract_dynamic_proc_info) (unw_addr_space_t as,
169 unw_word_t ip,
170 unw_proc_info_t *pi,
171 unw_dyn_info_t *di,
172 int need_unwind_info,
173 void *arg);
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000174extern void UNWI_OBJ(put_dynamic_unwind_info) (unw_addr_space_t as,
175 unw_proc_info_t *pi, void *arg);
hp.com!davidm7d3de042003-12-20 11:32:43 +0000176extern int UNWI_OBJ(dyn_remote_find_proc_info) (unw_addr_space_t as,
177 unw_word_t ip,
178 unw_proc_info_t *pi,
179 unw_word_t *generation,
180 int need_unwind_info,
181 void *arg);
182extern void UNWI_OBJ(dyn_remote_put_unwind_info) (unw_addr_space_t as,
183 unw_proc_info_t *pi,
184 void *arg);
mostang.com!davidme20ecc62003-02-22 08:19:43 +0000185extern int UNWI_OBJ(get_proc_name) (unw_addr_space_t as, unw_word_t ip,
186 char *buf, size_t buf_len,
187 unw_word_t *offp, void *arg);
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000188
189#define unwi_find_dynamic_proc_info(as,ip,pi,n,arg) \
190 UNWI_OBJ(find_dynamic_proc_info)(as, ip, pi, n, arg)
191
192#define unwi_extract_dynamic_proc_info(as,ip,pi,di,n,arg) \
hp.com!davidm7d3de042003-12-20 11:32:43 +0000193 UNWI_OBJ(extract_dynamic_proc_info)(as, ip, pi, di, n, arg)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000194
195#define unwi_put_dynamic_unwind_info(as,pi,arg) \
196 UNWI_OBJ(put_dynamic_unwind_info)(as, pi, arg)
197
198/* These handle the remote (cross-address-space) case of accessing
199 dynamic unwind info. */
200
201#define unwi_dyn_remote_find_proc_info(as,i,p,g,n,arg) \
hp.com!davidm7d3de042003-12-20 11:32:43 +0000202 UNWI_OBJ(dyn_remote_find_proc_info)(as, i, p, g, n, arg)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000203
204#define unwi_dyn_remote_put_unwind_info(as,p,arg) \
hp.com!davidm7d3de042003-12-20 11:32:43 +0000205 UNWI_OBJ(dyn_remote_put_unwind_info)(as, p, arg)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000206
mostang.com!davidme20ecc62003-02-22 08:19:43 +0000207#define unwi_get_proc_name(as,ip,b,s,o,arg) \
208 UNWI_OBJ(get_proc_name)(as, ip, b, s, o, arg)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000209
210extern unw_dyn_info_list_t _U_dyn_info_list;
211extern pthread_mutex_t _U_dyn_info_list_lock;
212
213#define WSIZE (sizeof (unw_word_t))
214
215static inline int
216fetch8 (unw_addr_space_t as, unw_accessors_t *a,
217 unw_word_t *addr, int8_t *valp, void *arg)
218{
219 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
220 int ret;
221
222 *addr += 1;
223
224 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
225
226#if __BYTE_ORDER == __LITTLE_ENDIAN
227 val >>= 8*off;
228#else
229 val >>= 8*(WSIZE - 1 - off);
230#endif
231 *valp = val & 0xff;
232 return ret;
233}
234
235static inline int
236fetch16 (unw_addr_space_t as, unw_accessors_t *a,
237 unw_word_t *addr, int16_t *valp, void *arg)
238{
239 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
240 int ret;
241
242 assert ((off & 0x1) == 0);
243
244 *addr += 2;
245
246 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
247
248#if __BYTE_ORDER == __LITTLE_ENDIAN
249 val >>= 8*off;
250#else
251 val >>= 8*(WSIZE - 2 - off);
252#endif
253 *valp = val & 0xffff;
254 return ret;
255}
256
257static inline int
258fetch32 (unw_addr_space_t as, unw_accessors_t *a,
259 unw_word_t *addr, int32_t *valp, void *arg)
260{
261 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
262 int ret;
263
264 assert ((off & 0x3) == 0);
265
266 *addr += 4;
267
268 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
269
270#if __BYTE_ORDER == __LITTLE_ENDIAN
271 val >>= 8*off;
272#else
273 val >>= 8*(WSIZE - 4 - off);
274#endif
275 *valp = val & 0xffffffff;
276 return ret;
277}
278
279static inline int
280fetchw (unw_addr_space_t as, unw_accessors_t *a,
281 unw_word_t *addr, unw_word_t *valp, void *arg)
282{
283 int ret;
284
285 ret = (*a->access_mem) (as, *addr, valp, 0, arg);
286 *addr += WSIZE;
287 return ret;
288}
289
mostang.com!davidm7e268d22003-01-23 10:04:09 +0000290#define mi_init UNWI_ARCH_OBJ(mi_init)
291
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000292extern void mi_init (void); /* machine-independent initializations */
293
mostang.com!davidmf5265f52003-02-08 10:10:59 +0000294/* This is needed/used by ELF targets only. */
295
296struct elf_image
297 {
298 void *image; /* pointer to mmap'd image */
299 size_t size; /* (file-) size of the image */
300 };
301
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000302#endif /* internal_h */