blob: 30d5b794fea51b30604675c8f29a36e572b48263 [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__
mostang.com!davidmf5265f52003-02-08 10:10:59 +000055# if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
56# define HIDDEN __attribute__((visibility ("hidden")))
57# else
58# define HIDDEN
59# endif
hp.com!davidma45c8882003-02-15 03:10:30 +000060# if (__GNUC__ >= 3)
61# define likely(x) __builtin_expect ((x), 1)
62# define unlikely(x) __builtin_expect ((x), 0)
63# else
64# define likely(x) (x)
65# define unlikely(x) (x)
66# endif
mostang.com!davidmbf070a92002-12-19 07:16:50 +000067#else
68# define HIDDEN
hp.com!davidma45c8882003-02-15 03:10:30 +000069# define likely(x) (x)
70# define unlikely(x) (x)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000071#endif
72
73#ifdef DEBUG
74# define UNW_DEBUG 1
75#endif
76
77#if UNW_DEBUG
78# include <stdio.h>
79# define debug(level,format...) \
hp.com!davidma45c8882003-02-15 03:10:30 +000080 do { if (tdep_debug_level > level) fprintf (stderr, format); } while (0)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000081# define dprintf(format...) \
hp.com!davidma45c8882003-02-15 03:10:30 +000082 fprintf (stderr, format)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000083# ifdef __GNUC__
hp.com!davidm90e83932003-11-24 21:37:22 +000084# undef inline
mostang.com!davidmbf070a92002-12-19 07:16:50 +000085# define inline __attribute__ ((unused))
86# endif
87#else
88# define debug(level,format...)
89# define dprintf(format...)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000090#endif
91
mostang.com!davidm313653f2003-01-21 08:08:32 +000092#define NELEMS(a) (sizeof (a) / sizeof ((a)[0]))
93
mostang.com!davidmbf070a92002-12-19 07:16:50 +000094/* Make it easy to write thread-safe code which may or may not be
95 linked against libpthread. The macros below can be used
96 unconditionally and if -lpthread is around, they'll call the
97 corresponding routines otherwise, they do nothing. */
98
hp.com!davidm1d443a62003-03-29 07:32:50 +000099#pragma weak pthread_mutex_init
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000100#pragma weak pthread_mutex_lock
101#pragma weak pthread_mutex_unlock
102
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000103#define mutex_init(l) (pthread_mutex_init ? pthread_mutex_init ((l), 0) : 0)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000104#define mutex_lock(l) (pthread_mutex_lock ? pthread_mutex_lock (l) : 0)
105#define mutex_unlock(l) (pthread_mutex_unlock ? pthread_mutex_unlock (l) : 0)
106
hp.com!davidm90e83932003-11-24 21:37:22 +0000107#ifdef HAVE_ATOMIC_OPS_H
108# include <atomic_ops.h>
109# define cmpxchg_ptr(_ptr,_o,_n) AO_compare_and_swap((AO_T *)_ptr, \
110 (AO_T) (_o), \
111 (AO_T) (_n))
112# define fetch_and_add1(_ptr) AO_fetch_and_add1(_ptr)
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000113# define HAVE_CMPXCHG
hp.com!davidm90e83932003-11-24 21:37:22 +0000114# define HAVE_FETCH_AND_ADD1
115#else
116# ifdef HAVE_IA64INTRIN_H
117# include <ia64intrin.h>
mostang.com!davidm28fe6a62003-03-27 04:29:07 +0000118 /*
119 * ecc v7.0 is broken: it's missing __sync_val_compare_and_swap()
120 * even though the ia64 ABI requires it. Work around it:
121 */
hp.com!davidm90e83932003-11-24 21:37:22 +0000122# ifndef __sync_val_compare_and_swap
123# define __sync_val_compare_and_swap(x,y,z) \
mostang.com!davidm28fe6a62003-03-27 04:29:07 +0000124 _InterlockedCompareExchange64_rel(x,y,z)
hp.com!davidm90e83932003-11-24 21:37:22 +0000125# endif
mostang.com!davidm28fe6a62003-03-27 04:29:07 +0000126
hp.com!davidm90e83932003-11-24 21:37:22 +0000127# define cmpxchg_ptr(_ptr,_o,_n) \
mostang.com!davidm271d7062003-03-28 07:43:22 +0000128 ((void *) __sync_val_compare_and_swap((volatile long *) (_ptr), \
hp.com!davidm90e83932003-11-24 21:37:22 +0000129 (long) (_o), (long) (_n)) \
hp.com!davidme01fb3b2003-11-25 20:13:21 +0000130 == (_o))
hp.com!davidm90e83932003-11-24 21:37:22 +0000131# define fetch_and_add1(_ptr) __sync_fetch_and_add(_ptr, 1)
132# define HAVE_CMPXCHG
133# define HAVE_FETCH_AND_ADD1
134# endif
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000135#endif
136
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000137#define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
138#define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
139
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000140#define unwi_full_sigmask UNWI_ARCH_OBJ(full_sigmask)
141
142extern sigset_t unwi_full_sigmask;
143
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000144extern int UNWI_OBJ(find_dynamic_proc_info) (unw_addr_space_t as,
145 unw_word_t ip,
146 unw_proc_info_t *pi,
147 int need_unwind_info, void *arg);
148extern int UNWI_ARCH_OBJ(extract_dynamic_proc_info) (unw_addr_space_t as,
149 unw_word_t ip,
150 unw_proc_info_t *pi,
151 unw_dyn_info_t *di,
152 int need_unwind_info,
153 void *arg);
154extern void UNWI_OBJ(put_dynamic_unwind_info) (unw_addr_space_t as,
155 unw_proc_info_t *pi, void *arg);
156extern int UNWI_ARCH_OBJ(dyn_remote_find_proc_info) (unw_addr_space_t as,
157 unw_word_t ip,
158 unw_proc_info_t *pi,
159 unw_word_t *generation,
160 int need_unwind_info,
161 void *arg);
162extern void UNWI_ARCH_OBJ(dyn_remote_put_unwind_info) (unw_addr_space_t as,
163 unw_proc_info_t *pi,
164 void *arg);
mostang.com!davidme20ecc62003-02-22 08:19:43 +0000165extern int UNWI_OBJ(get_proc_name) (unw_addr_space_t as, unw_word_t ip,
166 char *buf, size_t buf_len,
167 unw_word_t *offp, void *arg);
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000168
169#define unwi_find_dynamic_proc_info(as,ip,pi,n,arg) \
170 UNWI_OBJ(find_dynamic_proc_info)(as, ip, pi, n, arg)
171
172#define unwi_extract_dynamic_proc_info(as,ip,pi,di,n,arg) \
173 UNWI_ARCH_OBJ(extract_dynamic_proc_info)(as, ip, pi, di, n, arg)
174
175#define unwi_put_dynamic_unwind_info(as,pi,arg) \
176 UNWI_OBJ(put_dynamic_unwind_info)(as, pi, arg)
177
178/* These handle the remote (cross-address-space) case of accessing
179 dynamic unwind info. */
180
181#define unwi_dyn_remote_find_proc_info(as,i,p,g,n,arg) \
182 UNWI_ARCH_OBJ(dyn_remote_find_proc_info)(as, i, p, g, n, arg)
183
184#define unwi_dyn_remote_put_unwind_info(as,p,arg) \
185 UNWI_ARCH_OBJ(dyn_remote_put_unwind_info)(as, p, arg)
186
mostang.com!davidme20ecc62003-02-22 08:19:43 +0000187#define unwi_get_proc_name(as,ip,b,s,o,arg) \
188 UNWI_OBJ(get_proc_name)(as, ip, b, s, o, arg)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000189
190extern unw_dyn_info_list_t _U_dyn_info_list;
191extern pthread_mutex_t _U_dyn_info_list_lock;
192
193#define WSIZE (sizeof (unw_word_t))
194
195static inline int
196fetch8 (unw_addr_space_t as, unw_accessors_t *a,
197 unw_word_t *addr, int8_t *valp, void *arg)
198{
199 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
200 int ret;
201
202 *addr += 1;
203
204 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
205
206#if __BYTE_ORDER == __LITTLE_ENDIAN
207 val >>= 8*off;
208#else
209 val >>= 8*(WSIZE - 1 - off);
210#endif
211 *valp = val & 0xff;
212 return ret;
213}
214
215static inline int
216fetch16 (unw_addr_space_t as, unw_accessors_t *a,
217 unw_word_t *addr, int16_t *valp, void *arg)
218{
219 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
220 int ret;
221
222 assert ((off & 0x1) == 0);
223
224 *addr += 2;
225
226 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
227
228#if __BYTE_ORDER == __LITTLE_ENDIAN
229 val >>= 8*off;
230#else
231 val >>= 8*(WSIZE - 2 - off);
232#endif
233 *valp = val & 0xffff;
234 return ret;
235}
236
237static inline int
238fetch32 (unw_addr_space_t as, unw_accessors_t *a,
239 unw_word_t *addr, int32_t *valp, void *arg)
240{
241 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
242 int ret;
243
244 assert ((off & 0x3) == 0);
245
246 *addr += 4;
247
248 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
249
250#if __BYTE_ORDER == __LITTLE_ENDIAN
251 val >>= 8*off;
252#else
253 val >>= 8*(WSIZE - 4 - off);
254#endif
255 *valp = val & 0xffffffff;
256 return ret;
257}
258
259static inline int
260fetchw (unw_addr_space_t as, unw_accessors_t *a,
261 unw_word_t *addr, unw_word_t *valp, void *arg)
262{
263 int ret;
264
265 ret = (*a->access_mem) (as, *addr, valp, 0, arg);
266 *addr += WSIZE;
267 return ret;
268}
269
mostang.com!davidm7e268d22003-01-23 10:04:09 +0000270#define mi_init UNWI_ARCH_OBJ(mi_init)
271
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000272extern void mi_init (void); /* machine-independent initializations */
273
mostang.com!davidmf5265f52003-02-08 10:10:59 +0000274/* This is needed/used by ELF targets only. */
275
276struct elf_image
277 {
278 void *image; /* pointer to mmap'd image */
279 size_t size; /* (file-) size of the image */
280 };
281
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000282#endif /* internal_h */