blob: ac5b0bf2eda95df195704a4e881fcf831e5757f5 [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>
93# define debug(level,format...) \
hp.com!davidma45c8882003-02-15 03:10:30 +000094 do { if (tdep_debug_level > level) fprintf (stderr, format); } while (0)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000095# define dprintf(format...) \
hp.com!davidma45c8882003-02-15 03:10:30 +000096 fprintf (stderr, format)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000097# ifdef __GNUC__
hp.com!davidm90e83932003-11-24 21:37:22 +000098# undef inline
hp.com!davidm04dd29b2003-11-27 06:52:54 +000099# define inline UNUSED
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000100# endif
101#else
102# define debug(level,format...)
103# define dprintf(format...)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000104#endif
105
mostang.com!davidm313653f2003-01-21 08:08:32 +0000106#define NELEMS(a) (sizeof (a) / sizeof ((a)[0]))
107
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000108/* Make it easy to write thread-safe code which may or may not be
109 linked against libpthread. The macros below can be used
110 unconditionally and if -lpthread is around, they'll call the
111 corresponding routines otherwise, they do nothing. */
112
hp.com!davidm1d443a62003-03-29 07:32:50 +0000113#pragma weak pthread_mutex_init
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000114#pragma weak pthread_mutex_lock
115#pragma weak pthread_mutex_unlock
116
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000117#define mutex_init(l) (pthread_mutex_init ? pthread_mutex_init ((l), 0) : 0)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000118#define mutex_lock(l) (pthread_mutex_lock ? pthread_mutex_lock (l) : 0)
119#define mutex_unlock(l) (pthread_mutex_unlock ? pthread_mutex_unlock (l) : 0)
120
hp.com!davidm90e83932003-11-24 21:37:22 +0000121#ifdef HAVE_ATOMIC_OPS_H
122# include <atomic_ops.h>
123# define cmpxchg_ptr(_ptr,_o,_n) AO_compare_and_swap((AO_T *)_ptr, \
124 (AO_T) (_o), \
125 (AO_T) (_n))
126# define fetch_and_add1(_ptr) AO_fetch_and_add1(_ptr)
hp.com!davidm04dd29b2003-11-27 06:52:54 +0000127 /* GCC 3.2.0 on HP-UX crashes on cmpxchg_ptr() */
128# if !(defined(__hpux) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
129# define HAVE_CMPXCHG
130# endif
hp.com!davidm90e83932003-11-24 21:37:22 +0000131# define HAVE_FETCH_AND_ADD1
132#else
133# ifdef HAVE_IA64INTRIN_H
134# include <ia64intrin.h>
mostang.com!davidm28fe6a62003-03-27 04:29:07 +0000135 /*
136 * ecc v7.0 is broken: it's missing __sync_val_compare_and_swap()
137 * even though the ia64 ABI requires it. Work around it:
138 */
hp.com!davidm90e83932003-11-24 21:37:22 +0000139# ifndef __sync_val_compare_and_swap
140# define __sync_val_compare_and_swap(x,y,z) \
mostang.com!davidm28fe6a62003-03-27 04:29:07 +0000141 _InterlockedCompareExchange64_rel(x,y,z)
hp.com!davidm90e83932003-11-24 21:37:22 +0000142# endif
mostang.com!davidm28fe6a62003-03-27 04:29:07 +0000143
hp.com!davidm90e83932003-11-24 21:37:22 +0000144# define cmpxchg_ptr(_ptr,_o,_n) \
mostang.com!davidm271d7062003-03-28 07:43:22 +0000145 ((void *) __sync_val_compare_and_swap((volatile long *) (_ptr), \
hp.com!davidm90e83932003-11-24 21:37:22 +0000146 (long) (_o), (long) (_n)) \
hp.com!davidme01fb3b2003-11-25 20:13:21 +0000147 == (_o))
hp.com!davidm90e83932003-11-24 21:37:22 +0000148# define fetch_and_add1(_ptr) __sync_fetch_and_add(_ptr, 1)
149# define HAVE_CMPXCHG
150# define HAVE_FETCH_AND_ADD1
151# endif
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000152#endif
153
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000154#define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
155#define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
156
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000157#define unwi_full_sigmask UNWI_ARCH_OBJ(full_sigmask)
158
159extern sigset_t unwi_full_sigmask;
160
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000161extern int UNWI_OBJ(find_dynamic_proc_info) (unw_addr_space_t as,
162 unw_word_t ip,
163 unw_proc_info_t *pi,
164 int need_unwind_info, void *arg);
165extern int UNWI_ARCH_OBJ(extract_dynamic_proc_info) (unw_addr_space_t as,
166 unw_word_t ip,
167 unw_proc_info_t *pi,
168 unw_dyn_info_t *di,
169 int need_unwind_info,
170 void *arg);
171extern void UNWI_OBJ(put_dynamic_unwind_info) (unw_addr_space_t as,
172 unw_proc_info_t *pi, void *arg);
173extern int UNWI_ARCH_OBJ(dyn_remote_find_proc_info) (unw_addr_space_t as,
174 unw_word_t ip,
175 unw_proc_info_t *pi,
176 unw_word_t *generation,
177 int need_unwind_info,
178 void *arg);
179extern void UNWI_ARCH_OBJ(dyn_remote_put_unwind_info) (unw_addr_space_t as,
180 unw_proc_info_t *pi,
181 void *arg);
mostang.com!davidme20ecc62003-02-22 08:19:43 +0000182extern int UNWI_OBJ(get_proc_name) (unw_addr_space_t as, unw_word_t ip,
183 char *buf, size_t buf_len,
184 unw_word_t *offp, void *arg);
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000185
186#define unwi_find_dynamic_proc_info(as,ip,pi,n,arg) \
187 UNWI_OBJ(find_dynamic_proc_info)(as, ip, pi, n, arg)
188
189#define unwi_extract_dynamic_proc_info(as,ip,pi,di,n,arg) \
190 UNWI_ARCH_OBJ(extract_dynamic_proc_info)(as, ip, pi, di, n, arg)
191
192#define unwi_put_dynamic_unwind_info(as,pi,arg) \
193 UNWI_OBJ(put_dynamic_unwind_info)(as, pi, arg)
194
195/* These handle the remote (cross-address-space) case of accessing
196 dynamic unwind info. */
197
198#define unwi_dyn_remote_find_proc_info(as,i,p,g,n,arg) \
199 UNWI_ARCH_OBJ(dyn_remote_find_proc_info)(as, i, p, g, n, arg)
200
201#define unwi_dyn_remote_put_unwind_info(as,p,arg) \
202 UNWI_ARCH_OBJ(dyn_remote_put_unwind_info)(as, p, arg)
203
mostang.com!davidme20ecc62003-02-22 08:19:43 +0000204#define unwi_get_proc_name(as,ip,b,s,o,arg) \
205 UNWI_OBJ(get_proc_name)(as, ip, b, s, o, arg)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000206
207extern unw_dyn_info_list_t _U_dyn_info_list;
208extern pthread_mutex_t _U_dyn_info_list_lock;
209
210#define WSIZE (sizeof (unw_word_t))
211
212static inline int
213fetch8 (unw_addr_space_t as, unw_accessors_t *a,
214 unw_word_t *addr, int8_t *valp, void *arg)
215{
216 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
217 int ret;
218
219 *addr += 1;
220
221 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
222
223#if __BYTE_ORDER == __LITTLE_ENDIAN
224 val >>= 8*off;
225#else
226 val >>= 8*(WSIZE - 1 - off);
227#endif
228 *valp = val & 0xff;
229 return ret;
230}
231
232static inline int
233fetch16 (unw_addr_space_t as, unw_accessors_t *a,
234 unw_word_t *addr, int16_t *valp, void *arg)
235{
236 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
237 int ret;
238
239 assert ((off & 0x1) == 0);
240
241 *addr += 2;
242
243 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
244
245#if __BYTE_ORDER == __LITTLE_ENDIAN
246 val >>= 8*off;
247#else
248 val >>= 8*(WSIZE - 2 - off);
249#endif
250 *valp = val & 0xffff;
251 return ret;
252}
253
254static inline int
255fetch32 (unw_addr_space_t as, unw_accessors_t *a,
256 unw_word_t *addr, int32_t *valp, void *arg)
257{
258 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
259 int ret;
260
261 assert ((off & 0x3) == 0);
262
263 *addr += 4;
264
265 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
266
267#if __BYTE_ORDER == __LITTLE_ENDIAN
268 val >>= 8*off;
269#else
270 val >>= 8*(WSIZE - 4 - off);
271#endif
272 *valp = val & 0xffffffff;
273 return ret;
274}
275
276static inline int
277fetchw (unw_addr_space_t as, unw_accessors_t *a,
278 unw_word_t *addr, unw_word_t *valp, void *arg)
279{
280 int ret;
281
282 ret = (*a->access_mem) (as, *addr, valp, 0, arg);
283 *addr += WSIZE;
284 return ret;
285}
286
mostang.com!davidm7e268d22003-01-23 10:04:09 +0000287#define mi_init UNWI_ARCH_OBJ(mi_init)
288
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000289extern void mi_init (void); /* machine-independent initializations */
290
mostang.com!davidmf5265f52003-02-08 10:10:59 +0000291/* This is needed/used by ELF targets only. */
292
293struct elf_image
294 {
295 void *image; /* pointer to mmap'd image */
296 size_t size; /* (file-) size of the image */
297 };
298
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000299#endif /* internal_h */