blob: a2492c94f1155e4292f03d52de522e3fdaf49604 [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__
84# define inline __attribute__ ((unused))
85# endif
86#else
87# define debug(level,format...)
88# define dprintf(format...)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000089#endif
90
mostang.com!davidm313653f2003-01-21 08:08:32 +000091#define NELEMS(a) (sizeof (a) / sizeof ((a)[0]))
92
mostang.com!davidmbf070a92002-12-19 07:16:50 +000093/* Make it easy to write thread-safe code which may or may not be
94 linked against libpthread. The macros below can be used
95 unconditionally and if -lpthread is around, they'll call the
96 corresponding routines otherwise, they do nothing. */
97
hp.com!davidm1d443a62003-03-29 07:32:50 +000098#pragma weak pthread_mutex_init
mostang.com!davidmbf070a92002-12-19 07:16:50 +000099#pragma weak pthread_mutex_lock
100#pragma weak pthread_mutex_unlock
101
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000102#define mutex_init(l) (pthread_mutex_init ? pthread_mutex_init ((l), 0) : 0)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000103#define mutex_lock(l) (pthread_mutex_lock ? pthread_mutex_lock (l) : 0)
104#define mutex_unlock(l) (pthread_mutex_unlock ? pthread_mutex_unlock (l) : 0)
105
mostang.com!davidm15693e22003-03-19 19:25:18 +0000106#ifdef HAVE_IA64INTRIN_H
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000107# define HAVE_CMPXCHG
108# include <ia64intrin.h>
mostang.com!davidm28fe6a62003-03-27 04:29:07 +0000109 /*
110 * ecc v7.0 is broken: it's missing __sync_val_compare_and_swap()
111 * even though the ia64 ABI requires it. Work around it:
112 */
113# ifndef __sync_val_compare_and_swap
114# define __sync_val_compare_and_swap(x,y,z) \
115 _InterlockedCompareExchange64_rel(x,y,z)
116# endif
117
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000118# define cmpxchg_ptr(_ptr,_o,_n) \
mostang.com!davidm271d7062003-03-28 07:43:22 +0000119 ((void *) __sync_val_compare_and_swap((volatile long *) (_ptr), \
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000120 (long) (_o), (long) (_n)))
121#endif
122
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000123#define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
124#define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
125
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000126#define unwi_full_sigmask UNWI_ARCH_OBJ(full_sigmask)
127
128extern sigset_t unwi_full_sigmask;
129
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000130extern int UNWI_OBJ(find_dynamic_proc_info) (unw_addr_space_t as,
131 unw_word_t ip,
132 unw_proc_info_t *pi,
133 int need_unwind_info, void *arg);
134extern int UNWI_ARCH_OBJ(extract_dynamic_proc_info) (unw_addr_space_t as,
135 unw_word_t ip,
136 unw_proc_info_t *pi,
137 unw_dyn_info_t *di,
138 int need_unwind_info,
139 void *arg);
140extern void UNWI_OBJ(put_dynamic_unwind_info) (unw_addr_space_t as,
141 unw_proc_info_t *pi, void *arg);
142extern int UNWI_ARCH_OBJ(dyn_remote_find_proc_info) (unw_addr_space_t as,
143 unw_word_t ip,
144 unw_proc_info_t *pi,
145 unw_word_t *generation,
146 int need_unwind_info,
147 void *arg);
148extern void UNWI_ARCH_OBJ(dyn_remote_put_unwind_info) (unw_addr_space_t as,
149 unw_proc_info_t *pi,
150 void *arg);
mostang.com!davidme20ecc62003-02-22 08:19:43 +0000151extern int UNWI_OBJ(get_proc_name) (unw_addr_space_t as, unw_word_t ip,
152 char *buf, size_t buf_len,
153 unw_word_t *offp, void *arg);
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000154
155#define unwi_find_dynamic_proc_info(as,ip,pi,n,arg) \
156 UNWI_OBJ(find_dynamic_proc_info)(as, ip, pi, n, arg)
157
158#define unwi_extract_dynamic_proc_info(as,ip,pi,di,n,arg) \
159 UNWI_ARCH_OBJ(extract_dynamic_proc_info)(as, ip, pi, di, n, arg)
160
161#define unwi_put_dynamic_unwind_info(as,pi,arg) \
162 UNWI_OBJ(put_dynamic_unwind_info)(as, pi, arg)
163
164/* These handle the remote (cross-address-space) case of accessing
165 dynamic unwind info. */
166
167#define unwi_dyn_remote_find_proc_info(as,i,p,g,n,arg) \
168 UNWI_ARCH_OBJ(dyn_remote_find_proc_info)(as, i, p, g, n, arg)
169
170#define unwi_dyn_remote_put_unwind_info(as,p,arg) \
171 UNWI_ARCH_OBJ(dyn_remote_put_unwind_info)(as, p, arg)
172
mostang.com!davidme20ecc62003-02-22 08:19:43 +0000173#define unwi_get_proc_name(as,ip,b,s,o,arg) \
174 UNWI_OBJ(get_proc_name)(as, ip, b, s, o, arg)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000175
176extern unw_dyn_info_list_t _U_dyn_info_list;
177extern pthread_mutex_t _U_dyn_info_list_lock;
178
179#define WSIZE (sizeof (unw_word_t))
180
181static inline int
182fetch8 (unw_addr_space_t as, unw_accessors_t *a,
183 unw_word_t *addr, int8_t *valp, void *arg)
184{
185 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
186 int ret;
187
188 *addr += 1;
189
190 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
191
192#if __BYTE_ORDER == __LITTLE_ENDIAN
193 val >>= 8*off;
194#else
195 val >>= 8*(WSIZE - 1 - off);
196#endif
197 *valp = val & 0xff;
198 return ret;
199}
200
201static inline int
202fetch16 (unw_addr_space_t as, unw_accessors_t *a,
203 unw_word_t *addr, int16_t *valp, void *arg)
204{
205 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
206 int ret;
207
208 assert ((off & 0x1) == 0);
209
210 *addr += 2;
211
212 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
213
214#if __BYTE_ORDER == __LITTLE_ENDIAN
215 val >>= 8*off;
216#else
217 val >>= 8*(WSIZE - 2 - off);
218#endif
219 *valp = val & 0xffff;
220 return ret;
221}
222
223static inline int
224fetch32 (unw_addr_space_t as, unw_accessors_t *a,
225 unw_word_t *addr, int32_t *valp, void *arg)
226{
227 unw_word_t val, aligned_addr = *addr & -WSIZE, off = *addr - aligned_addr;
228 int ret;
229
230 assert ((off & 0x3) == 0);
231
232 *addr += 4;
233
234 ret = (*a->access_mem) (as, aligned_addr, &val, 0, arg);
235
236#if __BYTE_ORDER == __LITTLE_ENDIAN
237 val >>= 8*off;
238#else
239 val >>= 8*(WSIZE - 4 - off);
240#endif
241 *valp = val & 0xffffffff;
242 return ret;
243}
244
245static inline int
246fetchw (unw_addr_space_t as, unw_accessors_t *a,
247 unw_word_t *addr, unw_word_t *valp, void *arg)
248{
249 int ret;
250
251 ret = (*a->access_mem) (as, *addr, valp, 0, arg);
252 *addr += WSIZE;
253 return ret;
254}
255
mostang.com!davidm7e268d22003-01-23 10:04:09 +0000256#define mi_init UNWI_ARCH_OBJ(mi_init)
257
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000258extern void mi_init (void); /* machine-independent initializations */
259
mostang.com!davidmf5265f52003-02-08 10:10:59 +0000260/* This is needed/used by ELF targets only. */
261
262struct elf_image
263 {
264 void *image; /* pointer to mmap'd image */
265 size_t size; /* (file-) size of the image */
266 };
267
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000268#endif /* internal_h */