blob: ffba2b085f68099294139fb337fccfca9a94cb6c [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
mostang.com!davidmbf070a92002-12-19 07:16:50 +000033/* Platform-independent libunwind-internal declarations. */
34
mostang.com!davidm15693e22003-03-19 19:25:18 +000035#include <sys/types.h> /* HP-UX needs this before include of pthread.h */
36
mostang.com!davidmbf070a92002-12-19 07:16:50 +000037#include <assert.h>
mostang.com!davidmbf070a92002-12-19 07:16:50 +000038#include <libunwind.h>
mostang.com!davidm9003fd22003-03-06 06:14:36 +000039#include <pthread.h>
40#include <signal.h>
mostang.com!davidmbf070a92002-12-19 07:16:50 +000041
mostang.com!davidma04f38e2003-04-03 07:59:15 +000042#ifdef HAVE_ENDIAN_H
43# include <endian.h>
44#else
45# define __LITTLE_ENDIAN 1234
46# define __BIG_ENDIAN 4321
47# if defined(__hpux)
hp.com!davidm039accf2003-10-11 01:05:18 +000048# define __BYTE_ORDER __BIG_ENDIAN
mostang.com!davidma04f38e2003-04-03 07:59:15 +000049# else
50# error Host has unknown byte-order.
51# endif
52#endif
53
mostang.com!davidmbf070a92002-12-19 07:16:50 +000054#ifdef __GNUC__
hp.com!davidm04dd29b2003-11-27 06:52:54 +000055# define UNUSED __attribute__((unused))
mostang.com!davidmf5265f52003-02-08 10:10:59 +000056# if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
57# define HIDDEN __attribute__((visibility ("hidden")))
58# else
59# define HIDDEN
60# endif
hp.com!davidma45c8882003-02-15 03:10:30 +000061# if (__GNUC__ >= 3)
62# define likely(x) __builtin_expect ((x), 1)
63# define unlikely(x) __builtin_expect ((x), 0)
64# else
65# define likely(x) (x)
66# define unlikely(x) (x)
67# endif
mostang.com!davidmbf070a92002-12-19 07:16:50 +000068#else
hp.com!davidm04dd29b2003-11-27 06:52:54 +000069# define UNUSED
mostang.com!davidmbf070a92002-12-19 07:16:50 +000070# define HIDDEN
hp.com!davidma45c8882003-02-15 03:10:30 +000071# define likely(x) (x)
72# define unlikely(x) (x)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000073#endif
74
75#ifdef DEBUG
76# define UNW_DEBUG 1
hp.com!davidm04dd29b2003-11-27 06:52:54 +000077#else
78# define UNW_DEBUG 0
mostang.com!davidmbf070a92002-12-19 07:16:50 +000079#endif
80
81#if UNW_DEBUG
82# include <stdio.h>
83# define debug(level,format...) \
hp.com!davidma45c8882003-02-15 03:10:30 +000084 do { if (tdep_debug_level > level) fprintf (stderr, format); } while (0)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000085# define dprintf(format...) \
hp.com!davidma45c8882003-02-15 03:10:30 +000086 fprintf (stderr, format)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000087# ifdef __GNUC__
hp.com!davidm90e83932003-11-24 21:37:22 +000088# undef inline
hp.com!davidm04dd29b2003-11-27 06:52:54 +000089# define inline UNUSED
mostang.com!davidmbf070a92002-12-19 07:16:50 +000090# endif
91#else
92# define debug(level,format...)
93# define dprintf(format...)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000094#endif
95
mostang.com!davidm313653f2003-01-21 08:08:32 +000096#define NELEMS(a) (sizeof (a) / sizeof ((a)[0]))
97
mostang.com!davidmbf070a92002-12-19 07:16:50 +000098/* Make it easy to write thread-safe code which may or may not be
99 linked against libpthread. The macros below can be used
100 unconditionally and if -lpthread is around, they'll call the
101 corresponding routines otherwise, they do nothing. */
102
hp.com!davidm1d443a62003-03-29 07:32:50 +0000103#pragma weak pthread_mutex_init
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000104#pragma weak pthread_mutex_lock
105#pragma weak pthread_mutex_unlock
106
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000107#define mutex_init(l) (pthread_mutex_init ? pthread_mutex_init ((l), 0) : 0)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000108#define mutex_lock(l) (pthread_mutex_lock ? pthread_mutex_lock (l) : 0)
109#define mutex_unlock(l) (pthread_mutex_unlock ? pthread_mutex_unlock (l) : 0)
110
hp.com!davidm90e83932003-11-24 21:37:22 +0000111#ifdef HAVE_ATOMIC_OPS_H
112# include <atomic_ops.h>
113# define cmpxchg_ptr(_ptr,_o,_n) AO_compare_and_swap((AO_T *)_ptr, \
114 (AO_T) (_o), \
115 (AO_T) (_n))
116# define fetch_and_add1(_ptr) AO_fetch_and_add1(_ptr)
hp.com!davidm04dd29b2003-11-27 06:52:54 +0000117 /* GCC 3.2.0 on HP-UX crashes on cmpxchg_ptr() */
118# if !(defined(__hpux) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
119# define HAVE_CMPXCHG
120# endif
hp.com!davidm90e83932003-11-24 21:37:22 +0000121# define HAVE_FETCH_AND_ADD1
122#else
123# ifdef HAVE_IA64INTRIN_H
124# include <ia64intrin.h>
mostang.com!davidm28fe6a62003-03-27 04:29:07 +0000125 /*
126 * ecc v7.0 is broken: it's missing __sync_val_compare_and_swap()
127 * even though the ia64 ABI requires it. Work around it:
128 */
hp.com!davidm90e83932003-11-24 21:37:22 +0000129# ifndef __sync_val_compare_and_swap
130# define __sync_val_compare_and_swap(x,y,z) \
mostang.com!davidm28fe6a62003-03-27 04:29:07 +0000131 _InterlockedCompareExchange64_rel(x,y,z)
hp.com!davidm90e83932003-11-24 21:37:22 +0000132# endif
mostang.com!davidm28fe6a62003-03-27 04:29:07 +0000133
hp.com!davidm90e83932003-11-24 21:37:22 +0000134# define cmpxchg_ptr(_ptr,_o,_n) \
mostang.com!davidm271d7062003-03-28 07:43:22 +0000135 ((void *) __sync_val_compare_and_swap((volatile long *) (_ptr), \
hp.com!davidm90e83932003-11-24 21:37:22 +0000136 (long) (_o), (long) (_n)) \
hp.com!davidme01fb3b2003-11-25 20:13:21 +0000137 == (_o))
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 */