1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2001-2004 Hewlett-Packard Co
3	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#define UNW_VERSION_MAJOR	@PKG_MAJOR@
27#define UNW_VERSION_MINOR	@PKG_MINOR@
28#define UNW_VERSION_EXTRA	@PKG_EXTRA@
29
30#define UNW_VERSION_CODE(maj,min)	(((maj) << 16) | (min))
31#define UNW_VERSION	UNW_VERSION_CODE(UNW_VERSION_MAJOR, UNW_VERSION_MINOR)
32
33#ifdef __sun
34// On SmartOS, gcc fails with the following error:
35//
36// ../include/libunwind-common.h:43:41: error: expected identifier or '(' before numeric constant
37// # define UNW_PREFIX UNW_PASTE(UNW_PASTE(_U,UNW_TARGET),_)
38//                                         ^
39//
40// workaround is to undefine _U explicitly.
41// see https://github.com/libunwind/libunwind/issues/118 for more details.
42//
43#undef _U
44#endif
45
46#define UNW_PASTE2(x,y)	x##y
47#define UNW_PASTE(x,y)	UNW_PASTE2(x,y)
48#define UNW_OBJ(fn)	UNW_PASTE(UNW_PREFIX, fn)
49#define UNW_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_U,UNW_TARGET),_), fn)
50
51#ifdef UNW_LOCAL_ONLY
52# define UNW_PREFIX	UNW_PASTE(UNW_PASTE(_UL,UNW_TARGET),_)
53#else /* !UNW_LOCAL_ONLY */
54# define UNW_PREFIX	UNW_PASTE(UNW_PASTE(_U,UNW_TARGET),_)
55#endif /* !UNW_LOCAL_ONLY */
56
57/* Error codes.  The unwind routines return the *negated* values of
58   these error codes on error and a non-negative value on success.  */
59typedef enum
60  {
61    UNW_ESUCCESS = 0,		/* no error */
62    UNW_EUNSPEC,		/* unspecified (general) error */
63    UNW_ENOMEM,			/* out of memory */
64    UNW_EBADREG,		/* bad register number */
65    UNW_EREADONLYREG,		/* attempt to write read-only register */
66    UNW_ESTOPUNWIND,		/* stop unwinding */
67    UNW_EINVALIDIP,		/* invalid IP */
68    UNW_EBADFRAME,		/* bad frame */
69    UNW_EINVAL,			/* unsupported operation or bad value */
70    UNW_EBADVERSION,		/* unwind info has unsupported version */
71    UNW_ENOINFO			/* no unwind info found */
72  }
73unw_error_t;
74
75/* The following enum defines the indices for a couple of
76   (pseudo-)registers which have the same meaning across all
77   platforms.  (RO) means read-only.  (RW) means read-write.  General
78   registers (aka "integer registers") are expected to start with
79   index 0.  The number of such registers is architecture-dependent.
80   The remaining indices can be used as an architecture sees fit.  The
81   last valid register index is given by UNW_REG_LAST.  */
82typedef enum
83  {
84    UNW_REG_IP = UNW_TDEP_IP,		/* (rw) instruction pointer (pc) */
85    UNW_REG_SP = UNW_TDEP_SP,		/* (ro) stack pointer */
86    UNW_REG_EH = UNW_TDEP_EH,		/* (rw) exception-handling reg base */
87    UNW_REG_LAST = UNW_TDEP_LAST_REG
88  }
89unw_frame_regnum_t;
90
91/* Number of exception-handler argument registers: */
92#define UNW_NUM_EH_REGS		UNW_TDEP_NUM_EH_REGS
93
94typedef enum
95  {
96    UNW_CACHE_NONE,			/* no caching */
97    UNW_CACHE_GLOBAL,			/* shared global cache */
98    UNW_CACHE_PER_THREAD		/* per-thread caching */
99  }
100unw_caching_policy_t;
101
102typedef enum
103  {
104    UNW_INIT_SIGNAL_FRAME = 1,          /* We know this is a signal frame */
105  }
106unw_init_local2_flags_t;
107
108typedef int unw_regnum_t;
109
110/* The unwind cursor starts at the youngest (most deeply nested) frame
111   and is used to track the frame state as the unwinder steps from
112   frame to frame.  It is safe to make (shallow) copies of variables
113   of this type.  */
114typedef struct unw_cursor
115  {
116    unw_word_t opaque[UNW_TDEP_CURSOR_LEN];
117  }
118unw_cursor_t;
119
120/* This type encapsulates the entire (preserved) machine-state.  */
121typedef unw_tdep_context_t unw_context_t;
122
123/* unw_getcontext() fills the unw_context_t pointed to by UC with the
124   machine state as it exists at the call-site.  For implementation
125   reasons, this needs to be a target-dependent macro.  It's easiest
126   to think of unw_getcontext() as being identical to getcontext(). */
127#define unw_getcontext(uc)		unw_tdep_getcontext(uc)
128
129/* Return 1 if register number R is a floating-point register, zero
130   otherwise.
131   This routine is signal-safe.  */
132#define unw_is_fpreg(r)			unw_tdep_is_fpreg(r)
133
134typedef unw_tdep_fpreg_t unw_fpreg_t;
135
136typedef struct unw_addr_space *unw_addr_space_t;
137
138/* Each target may define it's own set of flags, but bits 0-15 are
139   reserved for general libunwind-use.  */
140#define UNW_PI_FLAG_FIRST_TDEP_BIT	16
141/* The information comes from a .debug_frame section.  */
142#define UNW_PI_FLAG_DEBUG_FRAME	32
143
144typedef struct unw_proc_info
145  {
146    unw_word_t start_ip;	/* first IP covered by this procedure */
147    unw_word_t end_ip;		/* first IP NOT covered by this procedure */
148#if defined(NEED_LAST_IP)
149    unw_word_t last_ip;		/* first IP that could begin another procedure */
150#endif
151    unw_word_t lsda;		/* address of lang.-spec. data area (if any) */
152    unw_word_t handler;		/* optional personality routine */
153    unw_word_t gp;		/* global-pointer value for this procedure */
154    unw_word_t flags;		/* misc. flags */
155
156    int format;			/* unwind-info format (arch-specific) */
157    int unwind_info_size;	/* size of the information (if applicable) */
158    void *unwind_info;		/* unwind-info (arch-specific) */
159    unw_tdep_proc_info_t extra;	/* target-dependent auxiliary proc-info */
160  }
161unw_proc_info_t;
162
163typedef int (*unw_reg_states_callback)(void *token,
164				       void *reg_states_data,
165				       size_t reg_states_data_size,
166				       unw_word_t start_ip, unw_word_t end_ip);
167
168/* These are backend callback routines that provide access to the
169   state of a "remote" process.  This can be used, for example, to
170   unwind another process through the ptrace() interface.  */
171typedef struct unw_accessors
172  {
173    /* Look up the unwind info associated with instruction-pointer IP.
174       On success, the routine fills in the PROC_INFO structure.  */
175    int (*find_proc_info) (unw_addr_space_t, unw_word_t, unw_proc_info_t *,
176			   int, void *);
177
178    /* Release any resources (e.g., memory) that were allocated for
179       the unwind info returned in by a previous call to
180       find_proc_info() with NEED_UNWIND_INFO set to 1.  */
181    void (*put_unwind_info) (unw_addr_space_t, unw_proc_info_t *, void *);
182
183    /* Return the list-head of the dynamically registered unwind
184       info.  */
185    int (*get_dyn_info_list_addr) (unw_addr_space_t, unw_word_t *, void *);
186
187    /* Access aligned word at address ADDR.  The value is returned
188       according to the endianness of the host (e.g., if the host is
189       little-endian and the target is big-endian, access_mem() needs
190       to byte-swap the value before returning it).  */
191    int (*access_mem) (unw_addr_space_t, unw_word_t, unw_word_t *, int,
192		       void *);
193
194    /* Access register number REG at address ADDR.  */
195    int (*access_reg) (unw_addr_space_t, unw_regnum_t, unw_word_t *, int,
196		       void *);
197
198    /* Access register number REG at address ADDR.  */
199    int (*access_fpreg) (unw_addr_space_t, unw_regnum_t,
200			 unw_fpreg_t *, int, void *);
201
202    int (*resume) (unw_addr_space_t, unw_cursor_t *, void *);
203
204    /* Optional call back to obtain the name of a (static) procedure.
205       Dynamically generated procedures are handled automatically by
206       libunwind.  This callback is optional and may be set to
207       NULL.  */
208    int (*get_proc_name) (unw_addr_space_t, unw_word_t, char *, size_t,
209			  unw_word_t *, void *);
210  }
211unw_accessors_t;
212
213typedef enum unw_save_loc_type
214  {
215    UNW_SLT_NONE,	/* register is not saved ("not an l-value") */
216    UNW_SLT_MEMORY,	/* register has been saved in memory */
217    UNW_SLT_REG		/* register has been saved in (another) register */
218  }
219unw_save_loc_type_t;
220
221typedef struct unw_save_loc
222  {
223    unw_save_loc_type_t type;
224    union
225      {
226	unw_word_t addr;	/* valid if type==UNW_SLT_MEMORY */
227	unw_regnum_t regnum;	/* valid if type==UNW_SLT_REG */
228      }
229    u;
230    unw_tdep_save_loc_t extra;	/* target-dependent additional information */
231  }
232unw_save_loc_t;
233
234/* These routines work both for local and remote unwinding.  */
235
236#define unw_local_addr_space	UNW_OBJ(local_addr_space)
237#define unw_create_addr_space	UNW_OBJ(create_addr_space)
238#define unw_destroy_addr_space	UNW_OBJ(destroy_addr_space)
239#define unw_get_accessors	UNW_ARCH_OBJ(get_accessors)
240#define unw_get_accessors_int	UNW_ARCH_OBJ(get_accessors_int)
241#define unw_init_local		UNW_OBJ(init_local)
242#define unw_init_local2	        UNW_OBJ(init_local2)
243#define unw_init_remote		UNW_OBJ(init_remote)
244#define unw_step		UNW_OBJ(step)
245#define unw_resume		UNW_OBJ(resume)
246#define unw_get_proc_info	UNW_OBJ(get_proc_info)
247#define unw_get_proc_info_by_ip	UNW_OBJ(get_proc_info_by_ip)
248#define unw_reg_states_iterate  UNW_OBJ(reg_states_iterate)
249#define unw_apply_reg_state     UNW_OBJ(apply_reg_state)
250#define unw_get_reg		UNW_OBJ(get_reg)
251#define unw_set_reg		UNW_OBJ(set_reg)
252#define unw_get_fpreg		UNW_OBJ(get_fpreg)
253#define unw_set_fpreg		UNW_OBJ(set_fpreg)
254#define unw_get_save_loc	UNW_OBJ(get_save_loc)
255#define unw_is_signal_frame	UNW_OBJ(is_signal_frame)
256#define unw_get_proc_name	UNW_OBJ(get_proc_name)
257#define unw_set_caching_policy	UNW_OBJ(set_caching_policy)
258#define unw_set_cache_size	UNW_OBJ(set_cache_size)
259#define unw_regname		UNW_ARCH_OBJ(regname)
260#define unw_flush_cache		UNW_ARCH_OBJ(flush_cache)
261#define unw_strerror		UNW_ARCH_OBJ(strerror)
262
263extern unw_addr_space_t unw_create_addr_space (unw_accessors_t *, int);
264extern void unw_destroy_addr_space (unw_addr_space_t);
265extern unw_accessors_t *unw_get_accessors (unw_addr_space_t);
266extern unw_accessors_t *unw_get_accessors_int (unw_addr_space_t);
267extern void unw_flush_cache (unw_addr_space_t, unw_word_t, unw_word_t);
268extern int unw_set_caching_policy (unw_addr_space_t, unw_caching_policy_t);
269extern int unw_set_cache_size (unw_addr_space_t, size_t, int);
270extern const char *unw_regname (unw_regnum_t);
271
272extern int unw_init_local (unw_cursor_t *, unw_context_t *);
273extern int unw_init_local2 (unw_cursor_t *, unw_context_t *, int);
274extern int unw_init_remote (unw_cursor_t *, unw_addr_space_t, void *);
275extern int unw_step (unw_cursor_t *);
276extern int unw_resume (unw_cursor_t *);
277extern int unw_get_proc_info (unw_cursor_t *, unw_proc_info_t *);
278extern int unw_get_proc_info_by_ip (unw_addr_space_t, unw_word_t,
279				    unw_proc_info_t *, void *);
280extern int unw_reg_states_iterate (unw_cursor_t *, unw_reg_states_callback, void *);
281extern int unw_apply_reg_state (unw_cursor_t *, void *);
282extern int unw_get_reg (unw_cursor_t *, int, unw_word_t *);
283extern int unw_set_reg (unw_cursor_t *, int, unw_word_t);
284extern int unw_get_fpreg (unw_cursor_t *, int, unw_fpreg_t *);
285extern int unw_set_fpreg (unw_cursor_t *, int, unw_fpreg_t);
286extern int unw_get_save_loc (unw_cursor_t *, int, unw_save_loc_t *);
287extern int unw_is_signal_frame (unw_cursor_t *);
288extern int unw_get_proc_name (unw_cursor_t *, char *, size_t, unw_word_t *);
289extern const char *unw_strerror (int);
290extern int unw_backtrace (void **, int);
291
292extern unw_addr_space_t unw_local_addr_space;
293