blob: 42ddd88a14cfbb0144307b44e7f2d219b29899a7 [file] [log] [blame]
mostang.com!davidmbf070a92002-12-19 07:16:50 +00001/* libunwind - a platform-independent unwind library
mostang.com!davidmc1bbbb22004-01-20 23:32:28 +00002 Copyright (C) 2001-2004 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>
hp.com!davidm23024e22004-01-03 10:50:24 +000048#include <string.h>
49#include <unistd.h>
mostang.com!davidmbf070a92002-12-19 07:16:50 +000050
mostang.com!davidma04f38e2003-04-03 07:59:15 +000051#ifdef HAVE_ENDIAN_H
52# include <endian.h>
53#else
54# define __LITTLE_ENDIAN 1234
55# define __BIG_ENDIAN 4321
56# if defined(__hpux)
hp.com!davidm039accf2003-10-11 01:05:18 +000057# define __BYTE_ORDER __BIG_ENDIAN
mostang.com!davidma04f38e2003-04-03 07:59:15 +000058# else
59# error Host has unknown byte-order.
60# endif
61#endif
62
mostang.com!davidmbf070a92002-12-19 07:16:50 +000063#ifdef __GNUC__
hp.com!davidm04dd29b2003-11-27 06:52:54 +000064# define UNUSED __attribute__((unused))
hp.com!davidm23024e22004-01-03 10:50:24 +000065# define NORETURN __attribute__((noreturn))
mostang.com!davidmf5265f52003-02-08 10:10:59 +000066# if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
hp.com!davidm1334cae2003-12-04 07:30:39 +000067# define ALWAYS_INLINE __attribute__((always_inline))
mostang.com!davidmf5265f52003-02-08 10:10:59 +000068# define HIDDEN __attribute__((visibility ("hidden")))
hp.com!davidm23024e22004-01-03 10:50:24 +000069# define PROTECTED __attribute__((visibility ("protected")))
mostang.com!davidmf5265f52003-02-08 10:10:59 +000070# else
hp.com!davidm1334cae2003-12-04 07:30:39 +000071# define ALWAYS_INLINE
mostang.com!davidmf5265f52003-02-08 10:10:59 +000072# define HIDDEN
hp.com!davidm23024e22004-01-03 10:50:24 +000073# define PROTECTED
mostang.com!davidmf5265f52003-02-08 10:10:59 +000074# endif
hp.com!davidma45c8882003-02-15 03:10:30 +000075# if (__GNUC__ >= 3)
76# define likely(x) __builtin_expect ((x), 1)
77# define unlikely(x) __builtin_expect ((x), 0)
78# else
79# define likely(x) (x)
80# define unlikely(x) (x)
81# endif
mostang.com!davidmbf070a92002-12-19 07:16:50 +000082#else
hp.com!davidm1334cae2003-12-04 07:30:39 +000083# define ALWAYS_INLINE
hp.com!davidm04dd29b2003-11-27 06:52:54 +000084# define UNUSED
hp.com!davidm23024e22004-01-03 10:50:24 +000085# define NORETURN
mostang.com!davidmbf070a92002-12-19 07:16:50 +000086# define HIDDEN
hp.com!davidm23024e22004-01-03 10:50:24 +000087# define PROTECTED
hp.com!davidma45c8882003-02-15 03:10:30 +000088# define likely(x) (x)
89# define unlikely(x) (x)
mostang.com!davidmbf070a92002-12-19 07:16:50 +000090#endif
91
92#ifdef DEBUG
93# define UNW_DEBUG 1
hp.com!davidm04dd29b2003-11-27 06:52:54 +000094#else
95# define UNW_DEBUG 0
mostang.com!davidmbf070a92002-12-19 07:16:50 +000096#endif
97
mostang.com!davidm313653f2003-01-21 08:08:32 +000098#define NELEMS(a) (sizeof (a) / sizeof ((a)[0]))
99
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000100/* Make it easy to write thread-safe code which may or may not be
101 linked against libpthread. The macros below can be used
102 unconditionally and if -lpthread is around, they'll call the
103 corresponding routines otherwise, they do nothing. */
104
hp.com!davidm1d443a62003-03-29 07:32:50 +0000105#pragma weak pthread_mutex_init
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000106#pragma weak pthread_mutex_lock
107#pragma weak pthread_mutex_unlock
108
hp.com!davidm9a961842004-04-23 00:12:51 +0000109#define mutex_init(l) \
110 (pthread_mutex_init != 0 ? pthread_mutex_init ((l), 0) : 0)
111#define mutex_lock(l) \
112 (pthread_mutex_lock != 0 ? pthread_mutex_lock (l) : 0)
113#define mutex_unlock(l) \
114 (pthread_mutex_unlock != 0 ? pthread_mutex_unlock (l) : 0)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000115
hp.com!davidm90e83932003-11-24 21:37:22 +0000116#ifdef HAVE_ATOMIC_OPS_H
117# include <atomic_ops.h>
mostang.com!davidm82cfd482004-01-20 23:50:00 +0000118static inline int
119cmpxchg_ptr (void *addr, void *old, void *new)
120{
121 union
122 {
123 void *vp;
124 AO_t *aop;
125 }
126 u;
127
128 u.vp = addr;
129 return AO_compare_and_swap(u.aop, (AO_t) old, (AO_t) new);
130}
hp.com!davidm90e83932003-11-24 21:37:22 +0000131# define fetch_and_add1(_ptr) AO_fetch_and_add1(_ptr)
hp.com!davidm04dd29b2003-11-27 06:52:54 +0000132 /* GCC 3.2.0 on HP-UX crashes on cmpxchg_ptr() */
133# if !(defined(__hpux) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
134# define HAVE_CMPXCHG
135# endif
hp.com!davidm90e83932003-11-24 21:37:22 +0000136# define HAVE_FETCH_AND_ADD1
137#else
138# ifdef HAVE_IA64INTRIN_H
139# include <ia64intrin.h>
mostang.com!davidm82cfd482004-01-20 23:50:00 +0000140static inline int
141cmpxchg_ptr (void *addr, void *old, void *new)
142{
143 union
144 {
145 void *vp;
146 long *vlp;
147 }
148 u;
149
150 u.vp = addr;
151 return __sync_bool_compare_and_swap(u.vlp, (long) old, (long) new);
152}
hp.com!davidm90e83932003-11-24 21:37:22 +0000153# define fetch_and_add1(_ptr) __sync_fetch_and_add(_ptr, 1)
154# define HAVE_CMPXCHG
155# define HAVE_FETCH_AND_ADD1
156# endif
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000157#endif
158
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000159#define UNWI_OBJ(fn) UNW_PASTE(UNW_PREFIX,UNW_PASTE(I,fn))
160#define UNWI_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_UI,UNW_TARGET),_), fn)
161
mostang.com!davidm9003fd22003-03-06 06:14:36 +0000162#define unwi_full_sigmask UNWI_ARCH_OBJ(full_sigmask)
163
164extern sigset_t unwi_full_sigmask;
165
hp.com!davidmb5d64da2004-01-30 00:01:24 +0000166#define unwi_find_dynamic_proc_info UNWI_OBJ(find_dynamic_proc_info)
167#define unwi_extract_dynamic_proc_info UNWI_OBJ(extract_dynamic_proc_info)
168#define unwi_put_dynamic_unwind_info UNWI_OBJ(put_dynamic_unwind_info)
169#define unwi_dyn_remote_find_proc_info UNWI_OBJ(dyn_remote_find_proc_info)
170#define unwi_dyn_remote_put_unwind_info UNWI_OBJ(dyn_remote_put_unwind_info)
171#define unwi_dyn_validate_cache UNWI_OBJ(dyn_validate_cache)
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000172
hp.com!davidmb5d64da2004-01-30 00:01:24 +0000173extern int unwi_find_dynamic_proc_info (unw_addr_space_t as,
174 unw_word_t ip,
175 unw_proc_info_t *pi,
176 int need_unwind_info, void *arg);
177extern int unwi_extract_dynamic_proc_info (unw_addr_space_t as,
178 unw_word_t ip,
179 unw_proc_info_t *pi,
180 unw_dyn_info_t *di,
181 int need_unwind_info,
182 void *arg);
183extern void unwi_put_dynamic_unwind_info (unw_addr_space_t as,
184 unw_proc_info_t *pi, void *arg);
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000185
186/* These handle the remote (cross-address-space) case of accessing
187 dynamic unwind info. */
188
hp.com!davidmb5d64da2004-01-30 00:01:24 +0000189extern int unwi_dyn_remote_find_proc_info (unw_addr_space_t as,
190 unw_word_t ip,
191 unw_proc_info_t *pi,
192 int need_unwind_info,
193 void *arg);
194extern void unwi_dyn_remote_put_unwind_info (unw_addr_space_t as,
195 unw_proc_info_t *pi,
196 void *arg);
197extern int unwi_dyn_validate_cache (unw_addr_space_t as, void *arg);
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000198
199extern unw_dyn_info_list_t _U_dyn_info_list;
200extern pthread_mutex_t _U_dyn_info_list_lock;
201
hp.com!davidmc44d7702004-01-22 08:36:15 +0000202#if UNW_DEBUG
203#define unwi_debug_level UNWI_ARCH_OBJ(debug_level)
204extern long unwi_debug_level;
205
206# include <stdio.h>
hp.com!davidmc44d7702004-01-22 08:36:15 +0000207# define Debug(level,format...) \
208do { \
209 if (unwi_debug_level > level) \
210 { \
211 int _n = level; \
212 if (_n > 16) \
213 _n = 16; \
214 fprintf (stderr, "%*c>%s: ", _n, ' ', __FUNCTION__); \
215 fprintf (stderr, format); \
216 } \
217} while (0)
hp.com!davidm5f45c422004-02-14 04:44:05 +0000218# define dprintf(format...) fprintf (stderr, format)
hp.com!davidmc44d7702004-01-22 08:36:15 +0000219# ifdef __GNUC__
220# undef inline
221# define inline UNUSED
222# endif
223#else
hp.com!davidm5f45c422004-02-14 04:44:05 +0000224# define Debug(level,format...)
hp.com!davidmc44d7702004-01-22 08:36:15 +0000225# define dprintf(format...)
226#endif
227
hp.com!davidm23024e22004-01-03 10:50:24 +0000228static inline ALWAYS_INLINE void
229print_error (const char *string)
230{
231 write (2, string, strlen (string));
232}
233
mostang.com!davidm7e268d22003-01-23 10:04:09 +0000234#define mi_init UNWI_ARCH_OBJ(mi_init)
235
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000236extern void mi_init (void); /* machine-independent initializations */
hp.com!davidm23024e22004-01-03 10:50:24 +0000237extern unw_word_t _U_dyn_info_list_addr (void);
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000238
mostang.com!davidmf5265f52003-02-08 10:10:59 +0000239/* This is needed/used by ELF targets only. */
240
241struct elf_image
242 {
243 void *image; /* pointer to mmap'd image */
244 size_t size; /* (file-) size of the image */
245 };
246
mostang.com!davidmbf070a92002-12-19 07:16:50 +0000247#endif /* internal_h */