blob: 8bfbf6541a6529f021ec46dac6bc20ad02f22fd3 [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
42#ifdef __GNUC__
mostang.com!davidmf5265f52003-02-08 10:10:59 +000043# if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
44# define HIDDEN __attribute__((visibility ("hidden")))
45# else
46# define HIDDEN
47# endif
hp.com!davidma45c8882003-02-15 03:10:30 +000048# if (__GNUC__ >= 3)
49# define likely(x) __builtin_expect ((x), 1)
50# define unlikely(x) __builtin_expect ((x), 0)
51# else
52# define likely(x) (x)
53# define unlikely(x) (x)
54# endif
mostang.com!davidmbf070a92002-12-19 07:16:50 +000055#else
56# define HIDDEN
hp.com!davidma45c8882003-02-15 03:10:30 +000057# define likely(x) (x)
58# define unlikely(x) (x)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000059#endif
60
61#ifdef DEBUG
62# define UNW_DEBUG 1
63#endif
64
65#if UNW_DEBUG
66# include <stdio.h>
67# define debug(level,format...) \
hp.com!davidma45c8882003-02-15 03:10:30 +000068 do { if (tdep_debug_level > level) fprintf (stderr, format); } while (0)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000069# define dprintf(format...) \
hp.com!davidma45c8882003-02-15 03:10:30 +000070 fprintf (stderr, format)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000071# ifdef __GNUC__
72# define inline __attribute__ ((unused))
73# endif
74#else
75# define debug(level,format...)
76# define dprintf(format...)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000077#endif
78
mostang.com!davidm313653f2003-01-21 08:08:32 +000079#define NELEMS(a) (sizeof (a) / sizeof ((a)[0]))
80
mostang.com!davidmbf070a92002-12-19 07:16:50 +000081/* Make it easy to write thread-safe code which may or may not be
82 linked against libpthread. The macros below can be used
83 unconditionally and if -lpthread is around, they'll call the
84 corresponding routines otherwise, they do nothing. */
85
hp.com!davidm1d443a62003-03-29 07:32:50 +000086#pragma weak pthread_mutex_init
mostang.com!davidmbf070a92002-12-19 07:16:50 +000087#pragma weak pthread_mutex_lock
88#pragma weak pthread_mutex_unlock
89
mostang.com!davidm9003fd22003-03-06 06:14:36 +000090#define mutex_init(l) (pthread_mutex_init ? pthread_mutex_init ((l), 0) : 0)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000091#define mutex_lock(l) (pthread_mutex_lock ? pthread_mutex_lock (l) : 0)
92#define mutex_unlock(l) (pthread_mutex_unlock ? pthread_mutex_unlock (l) : 0)
93
mostang.com!davidm15693e22003-03-19 19:25:18 +000094#ifdef HAVE_IA64INTRIN_H
mostang.com!davidm9003fd22003-03-06 06:14:36 +000095# define HAVE_CMPXCHG
96# include <ia64intrin.h>
mostang.com!davidm28fe6a62003-03-27 04:29:07 +000097 /*
98 * ecc v7.0 is broken: it's missing __sync_val_compare_and_swap()
99 * even though the ia64 ABI requires it. Work around it:
100 */
101# ifndef __sync_val_compare_and_swap
102# define __sync_val_compare_and_swap(x,y,z) \
103 _InterlockedCompareExchange64_rel(x,y,z)
104# endif
105
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000106# define cmpxchg_ptr(_ptr,_o,_n) \
mostang.com!davidm271d7062003-03-28 07:43:22 +0000107 ((void *) __sync_val_compare_and_swap((volatile long *) (_ptr), \
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000108 (long) (_o), (long) (_n)))
109#endif
110
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000111#define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
112#define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
113
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000114#define unwi_full_sigmask UNWI_ARCH_OBJ(full_sigmask)
115
116extern sigset_t unwi_full_sigmask;
117
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000118extern int UNWI_OBJ(find_dynamic_proc_info) (unw_addr_space_t as,
119 unw_word_t ip,
120 unw_proc_info_t *pi,
121 int need_unwind_info, void *arg);
122extern int UNWI_ARCH_OBJ(extract_dynamic_proc_info) (unw_addr_space_t as,
123 unw_word_t ip,
124 unw_proc_info_t *pi,
125 unw_dyn_info_t *di,
126 int need_unwind_info,
127 void *arg);
128extern void UNWI_OBJ(put_dynamic_unwind_info) (unw_addr_space_t as,
129 unw_proc_info_t *pi, void *arg);
130extern int UNWI_ARCH_OBJ(dyn_remote_find_proc_info) (unw_addr_space_t as,
131 unw_word_t ip,
132 unw_proc_info_t *pi,
133 unw_word_t *generation,
134 int need_unwind_info,
135 void *arg);
136extern void UNWI_ARCH_OBJ(dyn_remote_put_unwind_info) (unw_addr_space_t as,
137 unw_proc_info_t *pi,
138 void *arg);
mostang.com!davidme20ecc62003-02-22 08:19:43 +0000139extern int UNWI_OBJ(get_proc_name) (unw_addr_space_t as, unw_word_t ip,
140 char *buf, size_t buf_len,
141 unw_word_t *offp, void *arg);
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000142
143#define unwi_find_dynamic_proc_info(as,ip,pi,n,arg) \
144 UNWI_OBJ(find_dynamic_proc_info)(as, ip, pi, n, arg)
145
146#define unwi_extract_dynamic_proc_info(as,ip,pi,di,n,arg) \
147 UNWI_ARCH_OBJ(extract_dynamic_proc_info)(as, ip, pi, di, n, arg)
148
149#define unwi_put_dynamic_unwind_info(as,pi,arg) \
150 UNWI_OBJ(put_dynamic_unwind_info)(as, pi, arg)
151
152/* These handle the remote (cross-address-space) case of accessing
153 dynamic unwind info. */
154
155#define unwi_dyn_remote_find_proc_info(as,i,p,g,n,arg) \
156 UNWI_ARCH_OBJ(dyn_remote_find_proc_info)(as, i, p, g, n, arg)
157
158#define unwi_dyn_remote_put_unwind_info(as,p,arg) \
159 UNWI_ARCH_OBJ(dyn_remote_put_unwind_info)(as, p, arg)
160
mostang.com!davidme20ecc62003-02-22 08:19:43 +0000161#define unwi_get_proc_name(as,ip,b,s,o,arg) \
162 UNWI_OBJ(get_proc_name)(as, ip, b, s, o, arg)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000163
164extern unw_dyn_info_list_t _U_dyn_info_list;
165extern pthread_mutex_t _U_dyn_info_list_lock;
166
167#define WSIZE (sizeof (unw_word_t))
168
169static inline int
170fetch8 (unw_addr_space_t as, unw_accessors_t *a,
171 unw_word_t *addr, int8_t *valp, void *arg)
172{
173 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
174 int ret;
175
176 *addr += 1;
177
178 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
179
180#if __BYTE_ORDER == __LITTLE_ENDIAN
181 val >>= 8*off;
182#else
183 val >>= 8*(WSIZE - 1 - off);
184#endif
185 *valp = val & 0xff;
186 return ret;
187}
188
189static inline int
190fetch16 (unw_addr_space_t as, unw_accessors_t *a,
191 unw_word_t *addr, int16_t *valp, void *arg)
192{
193 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
194 int ret;
195
196 assert ((off & 0x1) == 0);
197
198 *addr += 2;
199
200 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
201
202#if __BYTE_ORDER == __LITTLE_ENDIAN
203 val >>= 8*off;
204#else
205 val >>= 8*(WSIZE - 2 - off);
206#endif
207 *valp = val & 0xffff;
208 return ret;
209}
210
211static inline int
212fetch32 (unw_addr_space_t as, unw_accessors_t *a,
213 unw_word_t *addr, int32_t *valp, void *arg)
214{
215 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
216 int ret;
217
218 assert ((off & 0x3) == 0);
219
220 *addr += 4;
221
222 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
223
224#if __BYTE_ORDER == __LITTLE_ENDIAN
225 val >>= 8*off;
226#else
227 val >>= 8*(WSIZE - 4 - off);
228#endif
229 *valp = val & 0xffffffff;
230 return ret;
231}
232
233static inline int
234fetchw (unw_addr_space_t as, unw_accessors_t *a,
235 unw_word_t *addr, unw_word_t *valp, void *arg)
236{
237 int ret;
238
239 ret = (*a->access_mem) (as, *addr, valp, 0, arg);
240 *addr += WSIZE;
241 return ret;
242}
243
mostang.com!davidm7e268d22003-01-23 10:04:09 +0000244#define mi_init UNWI_ARCH_OBJ(mi_init)
245
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000246extern void mi_init (void); /* machine-independent initializations */
247
mostang.com!davidmf5265f52003-02-08 10:10:59 +0000248/* This is needed/used by ELF targets only. */
249
250struct elf_image
251 {
252 void *image; /* pointer to mmap'd image */
253 size_t size; /* (file-) size of the image */
254 };
255
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000256#endif /* internal_h */