blob: 488d7d58c205e6559dacf55658f7c8f4fad06860 [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>
hp.com!davidm90e83932003-11-24 21:37:22 +0000135# define cmpxchg_ptr(_ptr,_o,_n) \
mostang.com!davidmbcdedf62003-12-05 06:42:28 +0000136 (__sync_bool_compare_and_swap((volatile long *) (_ptr), \
137 (long) (_o), (long) (_n)))
hp.com!davidm90e83932003-11-24 21:37:22 +0000138# define fetch_and_add1(_ptr) __sync_fetch_and_add(_ptr, 1)
139# define HAVE_CMPXCHG
140# define HAVE_FETCH_AND_ADD1
141# endif
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000142#endif
143
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000144#define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
145#define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
146
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000147#define unwi_full_sigmask UNWI_ARCH_OBJ(full_sigmask)
148
149extern sigset_t unwi_full_sigmask;
150
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000151extern int UNWI_OBJ(find_dynamic_proc_info) (unw_addr_space_t as,
152 unw_word_t ip,
153 unw_proc_info_t *pi,
154 int need_unwind_info, void *arg);
155extern int UNWI_ARCH_OBJ(extract_dynamic_proc_info) (unw_addr_space_t as,
156 unw_word_t ip,
157 unw_proc_info_t *pi,
158 unw_dyn_info_t *di,
159 int need_unwind_info,
160 void *arg);
161extern void UNWI_OBJ(put_dynamic_unwind_info) (unw_addr_space_t as,
162 unw_proc_info_t *pi, void *arg);
163extern int UNWI_ARCH_OBJ(dyn_remote_find_proc_info) (unw_addr_space_t as,
164 unw_word_t ip,
165 unw_proc_info_t *pi,
166 unw_word_t *generation,
167 int need_unwind_info,
168 void *arg);
169extern void UNWI_ARCH_OBJ(dyn_remote_put_unwind_info) (unw_addr_space_t as,
170 unw_proc_info_t *pi,
171 void *arg);
mostang.com!davidme20ecc62003-02-22 08:19:43 +0000172extern int UNWI_OBJ(get_proc_name) (unw_addr_space_t as, unw_word_t ip,
173 char *buf, size_t buf_len,
174 unw_word_t *offp, void *arg);
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000175
176#define unwi_find_dynamic_proc_info(as,ip,pi,n,arg) \
177 UNWI_OBJ(find_dynamic_proc_info)(as, ip, pi, n, arg)
178
179#define unwi_extract_dynamic_proc_info(as,ip,pi,di,n,arg) \
180 UNWI_ARCH_OBJ(extract_dynamic_proc_info)(as, ip, pi, di, n, arg)
181
182#define unwi_put_dynamic_unwind_info(as,pi,arg) \
183 UNWI_OBJ(put_dynamic_unwind_info)(as, pi, arg)
184
185/* These handle the remote (cross-address-space) case of accessing
186 dynamic unwind info. */
187
188#define unwi_dyn_remote_find_proc_info(as,i,p,g,n,arg) \
189 UNWI_ARCH_OBJ(dyn_remote_find_proc_info)(as, i, p, g, n, arg)
190
191#define unwi_dyn_remote_put_unwind_info(as,p,arg) \
192 UNWI_ARCH_OBJ(dyn_remote_put_unwind_info)(as, p, arg)
193
mostang.com!davidme20ecc62003-02-22 08:19:43 +0000194#define unwi_get_proc_name(as,ip,b,s,o,arg) \
195 UNWI_OBJ(get_proc_name)(as, ip, b, s, o, arg)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000196
197extern unw_dyn_info_list_t _U_dyn_info_list;
198extern pthread_mutex_t _U_dyn_info_list_lock;
199
200#define WSIZE (sizeof (unw_word_t))
201
202static inline int
203fetch8 (unw_addr_space_t as, unw_accessors_t *a,
204 unw_word_t *addr, int8_t *valp, void *arg)
205{
206 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
207 int ret;
208
209 *addr += 1;
210
211 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
212
213#if __BYTE_ORDER == __LITTLE_ENDIAN
214 val >>= 8*off;
215#else
216 val >>= 8*(WSIZE - 1 - off);
217#endif
218 *valp = val & 0xff;
219 return ret;
220}
221
222static inline int
223fetch16 (unw_addr_space_t as, unw_accessors_t *a,
224 unw_word_t *addr, int16_t *valp, void *arg)
225{
226 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
227 int ret;
228
229 assert ((off & 0x1) == 0);
230
231 *addr += 2;
232
233 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
234
235#if __BYTE_ORDER == __LITTLE_ENDIAN
236 val >>= 8*off;
237#else
238 val >>= 8*(WSIZE - 2 - off);
239#endif
240 *valp = val & 0xffff;
241 return ret;
242}
243
244static inline int
245fetch32 (unw_addr_space_t as, unw_accessors_t *a,
246 unw_word_t *addr, int32_t *valp, void *arg)
247{
248 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
249 int ret;
250
251 assert ((off & 0x3) == 0);
252
253 *addr += 4;
254
255 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
256
257#if __BYTE_ORDER == __LITTLE_ENDIAN
258 val >>= 8*off;
259#else
260 val >>= 8*(WSIZE - 4 - off);
261#endif
262 *valp = val & 0xffffffff;
263 return ret;
264}
265
266static inline int
267fetchw (unw_addr_space_t as, unw_accessors_t *a,
268 unw_word_t *addr, unw_word_t *valp, void *arg)
269{
270 int ret;
271
272 ret = (*a->access_mem) (as, *addr, valp, 0, arg);
273 *addr += WSIZE;
274 return ret;
275}
276
mostang.com!davidm7e268d22003-01-23 10:04:09 +0000277#define mi_init UNWI_ARCH_OBJ(mi_init)
278
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000279extern void mi_init (void); /* machine-independent initializations */
280
mostang.com!davidmf5265f52003-02-08 10:10:59 +0000281/* This is needed/used by ELF targets only. */
282
283struct elf_image
284 {
285 void *image; /* pointer to mmap'd image */
286 size_t size; /* (file-) size of the image */
287 };
288
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000289#endif /* internal_h */