blob: 9d48923e8b1b9196537b16a74e3efd4cbd9eeb7c [file] [log] [blame]
hp.com!davidm76166fb2002-04-05 23:37:55 +00001/* libunwind - a platform-independent unwind library
2 Copyright (C) 2001-2002 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of libunwind.
6
mostang.com!davidmaca38432002-11-16 03:25:36 +00007Permission 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:
hp.com!davidm76166fb2002-04-05 23:37:55 +000014
mostang.com!davidmaca38432002-11-16 03:25:36 +000015The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
hp.com!davidm76166fb2002-04-05 23:37:55 +000017
mostang.com!davidmaca38432002-11-16 03:25:36 +000018THE 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. */
hp.com!davidm76166fb2002-04-05 23:37:55 +000025
26#define UNW_PASTE2(x,y) x##y
27#define UNW_PASTE(x,y) UNW_PASTE2(x,y)
28#define UNW_OBJ(fn) UNW_PASTE(UNW_PREFIX, fn)
mostang.com!davidm50d7a152002-12-03 08:19:58 +000029#define UNW_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_U,UNW_TARGET),_), fn)
hp.com!davidm76166fb2002-04-05 23:37:55 +000030
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +000031
hp.com!davidm76166fb2002-04-05 23:37:55 +000032#ifdef UNW_LOCAL_ONLY
33# define UNW_PREFIX UNW_PASTE(UNW_PASTE(_UL,UNW_TARGET),_)
34#else /* !UNW_LOCAL_ONLY */
35# define UNW_PREFIX UNW_PASTE(UNW_PASTE(_U,UNW_TARGET),_)
36#endif /* !UNW_LOCAL_ONLY */
37
38typedef unw_tdep_word_t unw_word_t;
39
40/* This needs to be big enough to accommodate the unwind state of any
41 architecture, while leaving some slack for future expansion.
42 Changing this value will require recompiling all users of this
43 library. */
44#define UNW_STATE_LEN 127
45
46/* Error codes. The unwind routines return the *negated* values of
47 these error codes on error and a non-negative value on success. */
48typedef enum
49 {
50 UNW_ESUCCESS = 0, /* no error */
51 UNW_EUNSPEC, /* unspecified (general) error */
52 UNW_ENOMEM, /* out of memory */
53 UNW_EBADREG, /* bad register number */
54 UNW_EREADONLYREG, /* attempt to write read-only register */
55 UNW_ESTOPUNWIND, /* stop unwinding */
56 UNW_EINVALIDIP, /* invalid IP */
57 UNW_EBADFRAME, /* bad frame */
mostang.com!davidm50d7a152002-12-03 08:19:58 +000058 UNW_EINVAL, /* unsupported operation or bad value */
hp.com!davidm76166fb2002-04-05 23:37:55 +000059 UNW_EBADVERSION, /* unwind info has unsupported version */
60 UNW_ENOINFO /* no unwind info found */
61 }
62unw_error_t;
63
64/* The following enum defines the indices for a couple of
65 (pseudo-)registers which have the same meaning across all
66 platforms. (RO) means read-only. (RW) means read-write. General
67 registers (aka "integer registers") are expected to start with
68 index 0. The number of such registers is architecture-dependent.
69 The remaining indices can be used as an architecture sees fit. The
70 last valid register index is given by UNW_REG_LAST. */
71typedef enum
72 {
73 UNW_REG_IP = UNW_TDEP_IP, /* (rw) instruction pointer (pc) */
74 UNW_REG_SP = UNW_TDEP_SP, /* (ro) stack pointer */
hp.com!davidm76166fb2002-04-05 23:37:55 +000075 UNW_REG_LAST = UNW_TDEP_LAST_REG
76 }
77unw_frame_regnum_t;
78
mostang.com!davidm16f21892002-11-09 07:59:02 +000079typedef enum
80 {
81 UNW_CACHE_NONE, /* no caching */
82 UNW_CACHE_GLOBAL, /* shared global cache */
83 UNW_CACHE_PER_THREAD /* per-thread caching */
84 }
85unw_caching_policy_t;
86
hp.com!davidm76166fb2002-04-05 23:37:55 +000087typedef int unw_regnum_t;
88
89/* The unwind cursor starts at the youngest (most deeply nested) frame
90 and is used to track the frame state as the unwinder steps from
91 frame to frame. It is safe to make (shallow) copies of variables
92 of this type. */
93typedef struct unw_cursor
94 {
95 unw_word_t opaque[UNW_STATE_LEN];
96 }
97unw_cursor_t;
98
99/* This type encapsulates the entire (preserved) machine-state. */
100typedef unw_tdep_context_t unw_context_t;
101
102/* unw_getcontext() fills the unw_context_t pointed to by UC with the
103 machine state as it exists at the call-site. For implementation
104 reasons, this needs to be a target-dependent macro. It's easiest
105 to think of unw_getcontext() as being identical to getcontext(). */
106#define unw_getcontext(uc) unw_tdep_getcontext(uc)
107
108/* We will assume that "long double" is sufficiently large and aligned
109 to hold the contents of a floating-point register. Note that the
110 fp register format is not usually the same format as a "long
111 double". Instead, the content of unw_fpreg_t should be manipulated
112 only through the "raw.bits" member. */
113typedef union
114 {
115 struct { unw_word_t bits[1]; } raw;
116 long double dummy; /* dummy to force 16-byte alignment */
117 }
118unw_fpreg_t;
119
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000120typedef struct unw_addr_space *unw_addr_space_t;
121
mostang.com!davidm50d7a152002-12-03 08:19:58 +0000122typedef struct unw_proc_info
123 {
124 unw_word_t start_ip; /* first IP covered by this procedure */
125 unw_word_t end_ip; /* first IP NOT covered by this procedure */
126 unw_word_t lsda; /* address of lang.-spec. data area (if any) */
127 unw_word_t handler; /* optional personality routine */
128 unw_word_t gp; /* global-pointer value for this procedure */
129 unw_word_t flags; /* misc. flags */
mostang.com!davidm50d7a152002-12-03 08:19:58 +0000130
131 int format; /* unwind-info format (arch-specific) */
mostang.com!davidmb6251b02002-12-12 09:17:41 +0000132 int unwind_info_size; /* size of the informat (if applicable) */
mostang.com!davidm50d7a152002-12-03 08:19:58 +0000133 void *unwind_info; /* unwind-info (arch-specific) */
mostang.com!davidm50d7a152002-12-03 08:19:58 +0000134 }
135unw_proc_info_t;
136
hp.com!davidm76166fb2002-04-05 23:37:55 +0000137/* These are backend callback routines that provide access to the
138 state of a "remote" process. This can be used, for example, to
139 unwind another process through the ptrace() interface. */
140typedef struct unw_accessors
141 {
mostang.com!davidm50d7a152002-12-03 08:19:58 +0000142 /* Look up the unwind info associated with instruction-pointer IP.
143 On success, the routine fills in the PROC_INFO structure. */
144 int (*find_proc_info) (unw_addr_space_t as, unw_word_t ip,
mostang.com!davidmb6251b02002-12-12 09:17:41 +0000145 unw_proc_info_t *proc_info,
146 int need_unwind_info,
147 void *arg);
hp.com!davidm76166fb2002-04-05 23:37:55 +0000148
mostang.com!davidmb6251b02002-12-12 09:17:41 +0000149 /* Release any resources (e.g., memory) that were allocated for
150 the unwind info returned in by a previous call to
151 find_proc_info() with NEED_UNWIND_INFO set to 1. */
152 void (*put_unwind_info) (unw_addr_space_t as, unw_proc_info_t *proc_info,
153 void *arg);
154
155 /* Return the list-head of the dynamically registered unwind
156 info. */
157 int (*get_dyn_info_list_addr) (unw_addr_space_t as,
158 unw_word_t *dyn_info_list_addr,
159 void *arg);
160
161 /* Access aligned word at address ADDR. The value is returned
162 according to the endianness of the host (e.g., if the host is
163 little-endian and the target is big-endian, access_mem() needs
164 to byte-swap the value before returning it). */
mostang.com!davidm50d7a152002-12-03 08:19:58 +0000165 int (*access_mem) (unw_addr_space_t as, unw_word_t addr,
166 unw_word_t *val, int write, void *arg);
hp.com!davidm76166fb2002-04-05 23:37:55 +0000167
168 /* Access register number REG at address ADDR. */
mostang.com!davidm50d7a152002-12-03 08:19:58 +0000169 int (*access_reg) (unw_addr_space_t as, unw_regnum_t reg,
170 unw_word_t *val, int write, void *arg);
hp.com!davidm76166fb2002-04-05 23:37:55 +0000171
172 /* Access register number REG at address ADDR. */
mostang.com!davidm50d7a152002-12-03 08:19:58 +0000173 int (*access_fpreg) (unw_addr_space_t as, unw_regnum_t reg,
174 unw_fpreg_t *val, int write, void *arg);
hp.com!davidm76166fb2002-04-05 23:37:55 +0000175
mostang.com!davidm50d7a152002-12-03 08:19:58 +0000176 int (*resume) (unw_addr_space_t as, unw_cursor_t *c, void *arg);
hp.com!davidm76166fb2002-04-05 23:37:55 +0000177 }
178unw_accessors_t;
179
180typedef enum unw_save_loc_type
181 {
182 UNW_SLT_NONE, /* register is not saved ("not an l-value") */
183 UNW_SLT_MEMORY, /* register has been saved in memory */
184 UNW_SLT_REG /* register has been saved in (another) register */
185 }
186unw_save_loc_type_t;
187
188typedef struct unw_save_loc
189 {
190 unw_save_loc_type_t type;
191 union
192 {
193 unw_word_t addr; /* valid if type==UNW_SLT_MEMORY */
194 unw_regnum_t regnum; /* valid if type==UNW_SLT_REG */
195 }
196 u;
197 unw_tdep_save_loc_t extra; /* target-dependent additional information */
198 }
199unw_save_loc_t;
200
mostang.com!davidm50d7a152002-12-03 08:19:58 +0000201#include <libunwind-dynamic.h>
202
hp.com!davidm76166fb2002-04-05 23:37:55 +0000203/* These routines work both for local and remote unwinding. */
204
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000205extern unw_addr_space_t UNW_OBJ(local_addr_space);
206
mostang.com!davidmb6251b02002-12-12 09:17:41 +0000207extern unw_addr_space_t UNW_OBJ(create_addr_space) (unw_accessors_t *a,
208 int byte_order);
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000209extern void UNW_OBJ(destroy_addr_space) (unw_addr_space_t as);
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000210extern unw_accessors_t *UNW_ARCH_OBJ(get_accessors) (unw_addr_space_t as);
211extern void UNW_ARCH_OBJ(flush_cache)(unw_addr_space_t as,
212 unw_word_t lo, unw_word_t hi);
213extern int UNW_ARCH_OBJ(set_caching_policy)(unw_addr_space_t as,
214 unw_caching_policy_t policy);
215extern const char *UNW_ARCH_OBJ(regname) (int regnum);
216
hp.com!davidm76166fb2002-04-05 23:37:55 +0000217extern int UNW_OBJ(init_local) (unw_cursor_t *c, ucontext_t *u);
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000218extern int UNW_OBJ(init_remote) (unw_cursor_t *c, unw_addr_space_t as,
219 void *as_arg);
hp.com!davidm76166fb2002-04-05 23:37:55 +0000220extern int UNW_OBJ(step) (unw_cursor_t *c);
221extern int UNW_OBJ(resume) (unw_cursor_t *c);
mostang.com!davidm50d7a152002-12-03 08:19:58 +0000222extern int UNW_OBJ(get_proc_info) (unw_cursor_t *c, unw_proc_info_t *pi);
hp.com!davidm76166fb2002-04-05 23:37:55 +0000223extern int UNW_OBJ(get_reg) (unw_cursor_t *c, int regnum, unw_word_t *valp);
224extern int UNW_OBJ(set_reg) (unw_cursor_t *c, int regnum, unw_word_t val);
225extern int UNW_OBJ(get_fpreg) (unw_cursor_t *c, int regnum, unw_fpreg_t *val);
226extern int UNW_OBJ(set_fpreg) (unw_cursor_t *c, int regnum, unw_fpreg_t val);
227extern int UNW_OBJ(get_save_loc) (unw_cursor_t *c, int regnum,
228 unw_save_loc_t *loc);
229extern int UNW_OBJ(is_signal_frame) (unw_cursor_t *c);
mostang.com!davidmb6251b02002-12-12 09:17:41 +0000230extern int UNW_OBJ(get_proc_name) (unw_cursor_t *c, char *buf, size_t buf_len);
hp.com!davidm76166fb2002-04-05 23:37:55 +0000231
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000232#define unw_local_addr_space UNW_OBJ(local_addr_space)
233
234/* Create a new address space (in addition to the default
mostang.com!davidmb6251b02002-12-12 09:17:41 +0000235 local_addr_space). BYTE_ORDER can be 0 to select the default
236 byte-order or one of the byte-order values defined by <endian.h>
237 (e.g., __LITLE_ENDIAN or __BIG_ENDIAN). The default byte-order is
238 either implied by the target architecture (e.g., x86 is always
239 little-endian) or is select based on the byte-order of the host.
240
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000241 This routine is NOT signal-safe. */
mostang.com!davidmb6251b02002-12-12 09:17:41 +0000242#define unw_create_addr_space(a,b) UNW_OBJ(create_addr_space)(a,b)
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000243
244/* Destroy an address space.
245 This routine is NOT signal-safe. */
246#define unw_destroy_addr_space(as) UNW_OBJ(destroy_addr_space)(as)
247
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000248/* Retrieve a pointer to the accessors structure associated with
249 address space AS.
250 This routine is signal-safe. */
251#define unw_get_accessors(as) UNW_ARCH_OBJ(get_accessors)(as)
252
hp.com!davidm76166fb2002-04-05 23:37:55 +0000253/* Initialize cursor C such that unwinding starts at the point
254 represented by the context U. Returns zero on success, negative
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000255 value on failure.
256 This routine is signal-safe. */
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000257#define unw_init_local(c,u) UNW_OBJ(init_local)(c, u)
hp.com!davidm76166fb2002-04-05 23:37:55 +0000258
259/* Initialize cursor C such that it accesses the unwind target through
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000260 accessors A.
261 This routine is signal-safe. */
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000262#define unw_init_remote(c,a,arg) UNW_OBJ(init_remote)(c, a, arg)
hp.com!davidm76166fb2002-04-05 23:37:55 +0000263
264/* Move cursor up by one step (up meaning toward earlier, less deeply
265 nested frames). Returns positive number if there are more frames
266 to unwind, 0 if last frame has been reached, negative number in
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000267 case of an error.
268 This routine is signal-safe. */
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000269#define unw_step(c) UNW_OBJ(step)(c)
hp.com!davidm76166fb2002-04-05 23:37:55 +0000270
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000271/* Resume execution at the point identified by the cursor.
272 This routine is signal-safe. */
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000273#define unw_resume(c) UNW_OBJ(resume)(c)
hp.com!davidm76166fb2002-04-05 23:37:55 +0000274
mostang.com!davidm50d7a152002-12-03 08:19:58 +0000275/* Return the proc-info associated with the cursor.
276 This routine is signal-safe. */
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000277#define unw_get_proc_info(c,p) UNW_OBJ(get_proc_info)(c,p)
mostang.com!davidm50d7a152002-12-03 08:19:58 +0000278
hp.com!davidm76166fb2002-04-05 23:37:55 +0000279/* Register accessor routines. Return zero on success, negative value
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000280 on failure.
281 These routines are signal-safe. */
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000282#define unw_get_reg(c,r,v) UNW_OBJ(get_reg)(c,r,v)
283#define unw_set_reg(c,r,v) UNW_OBJ(set_reg)(c,r,v)
hp.com!davidm76166fb2002-04-05 23:37:55 +0000284
285/* Floating-point accessor routines. Return zero on success, negative
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000286 value on failure.
287 These routines are signal-safe. */
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000288#define unw_get_fpreg(c,r,v) UNW_OBJ(get_fpreg)(c,r,v)
289#define unw_set_fpreg(c,r,v) UNW_OBJ(set_fpreg)(c,r,v)
hp.com!davidm76166fb2002-04-05 23:37:55 +0000290
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000291/* Get the save-location of register R.
292 This routine is signal-safe. */
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000293#define unw_get_save_loc(c,r,l) UNW_OBJ(get_save_loc)(c,r,l)
hp.com!davidm76166fb2002-04-05 23:37:55 +0000294
295/* Return 1 if register number R is a floating-point register, zero
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000296 otherwise.
297 This routine is signal-safe. */
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000298#define unw_is_fpreg(r) unw_tdep_is_fpreg(r)
hp.com!davidm76166fb2002-04-05 23:37:55 +0000299
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000300/* Returns non-zero value if the cursor points to a signal frame.
301 This routine is signal-safe. */
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000302#define unw_is_signal_frame(c) UNW_OBJ(is_signal_frame)(c)
mostang.com!davidm58142c02002-04-12 05:02:40 +0000303
mostang.com!davidmb6251b02002-12-12 09:17:41 +0000304/* Return the name of the procedure that created the frame identified
305 by the cursor. The returned string is ASCII NUL terminated. If the
306 string buffer is too small to store the entire name, the first
307 portion of the string that can fit is stored in the buffer (along
308 with a terminating NUL character) and -UNW_ENOMEM is returned. If
309 no name can be determined, -UNW_ENOINFO is returned. */
310#define unw_get_proc_name(c,s,l) UNW_OBJ(get_proc_name)(c, s, l)
311
mostang.com!davidm58142c02002-04-12 05:02:40 +0000312/* Returns the canonical register name of register R. R must be in
313 the range from 0 to UNW_REG_LAST. Like all other unwind routines,
314 this one is re-entrant (i.e., the returned string must be a string
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000315 constant.
316 This routine is signal-safe. */
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000317#define unw_regname(r) UNW_ARCH_OBJ(regname)(r)
mostang.com!davidm16f21892002-11-09 07:59:02 +0000318
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000319/* Sets the caching policy of address space AS. Caching can be
320 disabled completely by setting the policy to UNW_CACHE_NONE. With
321 UNW_CACHE_GLOBAL, there is a single cache that is shared across all
322 threads. With UNW_CACHE_PER_THREAD, each thread gets its own
323 cache, which can improve performance thanks to less locking and
324 better locality. By default, UNW_CACHE_GLOBAL is in effect.
325 This routine is NOT signal-safe. */
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000326#define unw_set_caching_policy(as, p) UNW_ARCH_OBJ(set_caching_policy)(as, p)
mostang.com!davidm16f21892002-11-09 07:59:02 +0000327
328/* Flush all caches (global, per-thread, or any other caches that
mostang.com!davidm87bc2e32002-11-16 06:50:04 +0000329 might exist) in address-space AS of information at least relating
330 to the address-range LO to HI (non-inclusive). LO and HI are only
331 a performance hint and the function is allowed to over-flush (i.e.,
332 flush more than the requested address-range). Furthermore, if LO
333 and HI are both 0, the entire address-range is flushed. This
334 function must be called if any of unwind information might have
335 changed (e.g., because a library might have been removed via a call
mostang.com!davidm50d7a152002-12-03 08:19:58 +0000336 to dlclose()).
337 This routine is signal-safe. */
mostang.com!davidm20a6c1a2002-12-19 07:16:50 +0000338#define unw_flush_cache(as,lo,hi) UNW_ARCH_OBJ(flush_cache)(as, lo, hi)