uwx.h revision 160157
1/*
2Copyright (c) 2003-2006 Hewlett-Packard Development Company, L.P.
3Permission is hereby granted, free of charge, to any person
4obtaining a copy of this software and associated documentation
5files (the "Software"), to deal in the Software without
6restriction, including without limitation the rights to use,
7copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the
9Software is furnished to do so, subject to the following
10conditions:
11
12The above copyright notice and this permission notice shall be
13included in all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22OTHER DEALINGS IN THE SOFTWARE.
23*/
24
25#ifndef __UWX_INCLUDED
26#define __UWX_INCLUDED 1
27
28#include <stdlib.h>
29#include <inttypes.h>
30
31#if defined(__cplusplus)
32#define __EXTERN_C extern "C"
33#else
34#define __EXTERN_C extern
35#endif
36
37#define UWX_VERSION 3		/* Version id for callback interfaces */
38
39/* Unwind environment structure (opaque) */
40struct uwx_env;
41
42/* Symbol Cache for uwx_find_symbol (opaque) */
43struct uwx_symbol_cache;
44
45/* Allocate and free callbacks */
46typedef void *(*alloc_cb)(size_t size);
47typedef void (*free_cb)(void *ptr);
48__EXTERN_C int uwx_register_alloc_cb(alloc_cb alloc, free_cb free);
49
50/* Allocate and initialize an unwind environment */
51__EXTERN_C struct uwx_env *uwx_init(void);
52
53/* Free an unwind environment */
54__EXTERN_C int uwx_free(struct uwx_env *env);
55
56/* Put unwind express into cross-process mode */
57__EXTERN_C int uwx_set_remote(struct uwx_env *env, int is_big_endian_target);
58
59/* Put unwind express into reduced-context mode (no floating-point regs) */
60__EXTERN_C int uwx_set_nofr(struct uwx_env *env);
61
62/* Copy-in callback */
63typedef int (*copyin_cb)(
64    int request,		/* request code (see below) */
65    char *loc,			/* local (destination) address */
66    uint64_t rem,		/* remote (source) address */
67    int len,			/* number of bytes to copy */
68    intptr_t tok);		/* callback token */
69
70/* Lookup IP callback */
71typedef int (*lookupip_cb)(
72    int request,		/* request code (see below) */
73    uint64_t ip,		/* IP of current frame */
74    intptr_t tok,		/* callback token */
75    uint64_t **vecp);		/* parameter vector (in/out) */
76
77/* Register copy-in and lookup IP callbacks */
78__EXTERN_C int uwx_register_callbacks(
79    struct uwx_env *env,	/* unwind environment */
80    intptr_t tok,		/* callback token */
81    copyin_cb copyin,		/* copy-in callback */
82    lookupip_cb lookupip);	/* lookup IP callback */
83
84/* Initialize a context with the basic info needed to start an unwind */
85__EXTERN_C int uwx_init_context(
86    struct uwx_env *env,	/* unwind environment */
87    uint64_t ip,		/* IP (instruction pointer) */
88    uint64_t sp,		/* SP (stack pointer) */
89    uint64_t bsp,		/* BSP (backing store pointer) */
90    uint64_t cfm);		/* CFM (current frame marker) */
91
92/* Set the value of a specific register in the current context (non fp) */
93__EXTERN_C int uwx_set_reg(
94    struct uwx_env *env,	/* unwind environment */
95    int regid,			/* register id (see below) */
96    uint64_t val);		/* register value */
97
98/* Set the value of a floating-point register in the current context */
99__EXTERN_C int uwx_set_fr(
100    struct uwx_env *env,	/* unwind environment */
101    int regid,			/* register id (see below) */
102    uint64_t *val);		/* register value (ptr to 16 bytes) */
103				/*   (memory spill format) */
104
105/* Initialize the unwind history */
106__EXTERN_C int uwx_init_history(struct uwx_env *env);
107
108/* Step one frame */
109__EXTERN_C int uwx_step(struct uwx_env *env);
110
111/* Get module name and text base, if available, for current frame */
112__EXTERN_C int uwx_get_module_info(
113    struct uwx_env *env,	/* unwind environment */
114    char **modp,		/* load module name (out)  */
115    uint64_t *text_base);	/* base address of text segment (out)  */
116
117/* Get function start address for current frame */
118__EXTERN_C int uwx_get_funcstart(
119    struct uwx_env *env,	/* unwind environment */
120    uint64_t *funcstart);	/* function start address (out)  */
121
122/* Get symbol information, if available, for current frame */
123__EXTERN_C int uwx_get_sym_info(
124    struct uwx_env *env,	/* unwind environment */
125    char **modp,		/* load module name (out)  */
126    char **symp,		/* function name (out)  */
127    uint64_t *offsetp);		/* offset from start of function (out)  */
128
129/* Get symbol information, given module name and IP */
130__EXTERN_C int uwx_find_symbol(
131    struct uwx_env *env,	/* unwind environment */
132    struct uwx_symbol_cache **cachep,
133				/* ptr to symbol cache ptr (in/out) */
134    char *mod,			/* load module name */
135    uint64_t relip,		/* IP, relative to text segment  */
136    char **symp,		/* function name (out) */
137    uint64_t *offsetp);		/* offset from start of function (out) */
138
139/* Release memory used by symbol cache */
140__EXTERN_C void uwx_release_symbol_cache(
141    struct uwx_env *env,	/* unwind environment */
142    struct uwx_symbol_cache *symbol_cache);
143				/* symbol cache ptr */
144
145/* Get the value of a register from the current context */
146__EXTERN_C int uwx_get_reg(
147    struct uwx_env *env,	/* unwind environment */
148    int regid,			/* register id (see below) */
149    uint64_t *valp);		/* register value (out) */
150
151/* Get the NaT bit of a GR from the current context */
152__EXTERN_C int uwx_get_nat(
153    struct uwx_env *env,	/* unwind environment */
154    int regid,			/* register id (see below) */
155    int *natp);			/* NaT value (out: 0 or 1) */
156
157/* Get the spill location for a register in the current context */
158__EXTERN_C int uwx_get_spill_loc(
159    struct uwx_env *env,	/* unwind environment */
160    int regid,			/* register id (see below) */
161    uint64_t *dispp);		/* disposition code (see below) (out) */
162
163/* Get the ABI context code (if uwx_step returned UWX_ABI_FRAME) */
164__EXTERN_C int uwx_get_abi_context_code(struct uwx_env *env);
165
166/* Increment/Decrement the bsp by a number of slots */
167/* (accounts for NaT collections) */
168__EXTERN_C uint64_t uwx_add_to_bsp(uint64_t bsp, int nslots);
169
170/* Return status codes for uwx_ APIs */
171#define UWX_OK			0
172#define UWX_BOTTOM		1	/* Hit bottom of stack */
173#define UWX_ABI_FRAME		2	/* Hit ABI-dependent frame */
174#define UWX_ERR_NOENV		(-1)	/* No uwx_env allocated */
175#define UWX_ERR_IPNOTFOUND	(-2)	/* Lookup IP c/b returned NOTFOUND */
176#define UWX_ERR_LOOKUPERR	(-3)	/* Lookup IP c/b returned ERR */
177#define UWX_ERR_BADKEY		(-4)	/* Bad result vector key */
178#define UWX_ERR_COPYIN_UTBL	(-5)	/* Error reading unwind table */
179#define UWX_ERR_COPYIN_UINFO	(-6)	/* Error reading unwind info */
180#define UWX_ERR_COPYIN_MSTK	(-7)	/* Error reading memory stack */
181#define UWX_ERR_COPYIN_RSTK	(-8)	/* Error reading register stack */
182#define UWX_ERR_COPYIN_REG	(-9)	/* Error reading context register */
183#define UWX_ERR_NOUENTRY	(-10)	/* No unwind table entry for ip */
184#define UWX_ERR_NOUDESC		(-11)	/* No unwind descriptor covers ip */
185#define UWX_ERR_BADUDESC	(-12)	/* Bad unwind descriptor */
186#define UWX_ERR_NOMEM		(-13)	/* Out of memory */
187#define UWX_ERR_PROLOG_UF	(-14)	/* Prologue underflow */
188#define UWX_ERR_UNDEFLABEL	(-15)	/* Undefined label in copy_state */
189#define UWX_ERR_BADREGID	(-16)	/* Bad register identifier */
190#define UWX_ERR_CANTUNWIND	(-17)	/* Can't unwind */
191#define UWX_ERR_NOCALLBACKS	(-18)	/* No callbacks registered */
192#define UWX_ERR_NOCONTEXT	(-19)	/* Context not initialized */
193#define UWX_ERR_UCACCESS	(-20)	/* Failure in libuca */
194#define UWX_ERR_NOSYM		(-21)	/* Symbol not found */
195
196/* Request codes for copyin callback */
197#define UWX_COPYIN_UINFO	1	/* Reading unwind info */
198#define UWX_COPYIN_MSTACK	2	/* Reading memory stack */
199#define UWX_COPYIN_RSTACK	3	/* Reading RSE backing store */
200#define UWX_COPYIN_REG		4	/* Reading initial register state */
201
202/* Request codes for lookup IP callback */
203#define UWX_LKUP_LOOKUP		1	/* Lookup IP */
204#define UWX_LKUP_FREE		2	/* Free result vector */
205#define UWX_LKUP_SYMBOLS	3	/* Lookup symbolic information */
206#define UWX_LKUP_MODULE		4	/* Get module name */
207
208/* Return status codes for lookup IP callback */
209#define UWX_LKUP_NOTFOUND	0	/* IP not found */
210#define UWX_LKUP_ERR		1	/* Other error */
211#define UWX_LKUP_UTABLE		2	/* Returned ref to unwind table */
212#define UWX_LKUP_FDESC		3	/* Returned frame description */
213#define UWX_LKUP_SYMINFO	4	/* Returned symbolic information */
214#define UWX_LKUP_REMAP		5	/* Returned remapped IP */
215#define UWX_LKUP_UINFO		6	/* Returned unw info block ptr */
216
217/* The lookup IP callback receives a parameter vector, and returns */
218/* one on success. This vector is a series of key/value pairs; each */
219/* even-numbered slot is a key, and each odd-numbered slot is a */
220/* corresponding value. The vector is terminated by a pair whose */
221/* key is 0. */
222#define UWX_KEY_END		0	/* End of vector */
223
224/* Keys passed to lookup IP callback */
225#define UWX_KEY_PREDS		1	/* Predicate registers */
226#define UWX_KEY_VERSION		2	/* Version id of unwind engine */
227/* UWX_KEY_FUNCSTART (below) may also be passed with the UWX_LKUP_SYMINFO */
228/* request. */
229
230/* Keys returned with UWX_LKUP_UTABLE */
231/* These key/value pairs describe the unwind table corresponding */
232/* to the load module in which the current IP resides. */
233#define UWX_KEY_TBASE		1	/* Base address of text seg */
234#define UWX_KEY_UFLAGS		2	/* Unwind flags (optional) */
235#define UWX_KEY_USTART		3	/* Base of unwind tbl */
236#define UWX_KEY_UEND		4	/* End of unwind tbl */
237#define UWX_KEY_GP		7	/* GP value for module */
238
239/* Keys returned with UWX_LKUP_FDESC */
240/* These key/value pairs describe the state of the frame at the */
241/* given IP. They are typically used for dynamically-generated code. */
242/* If UWX_KEY_CONTEXT is returned, it must be the only key returned. */
243/* Use UWX_KEY_GP for the module's gp value. */
244#define UWX_KEY_FSIZE		1			/* Frame size */
245#define UWX_KEY_SPILL(reg_id)	(2 | ((reg_id) << 4))	/* Reg spilled */
246#define UWX_KEY_CONTEXT		3 			/* ABI-dep. context */
247
248/* Keys returned with UWX_LKUP_REMAP */
249#define UWX_KEY_NEWIP		5	/* Remapped IP */
250
251/* Keys returned with UWX_LKUP_UINFO */
252/* Use UWX_KEY_GP for the module's gp value. */
253/* Use UWX_KEY_FUNCSTART for the start address of the function */
254/* Use UWX_KEY_UFLAGS for the unwind flags (optional) */
255#define UWX_KEY_UINFO 		6	/* Address of unwind info block */
256
257/* Keys returned with UWX_LKUP_SYMINFO */
258/* These keys may be returned with UWX_LKUP_FDESC or UWX_LKUP_UINFO, */
259/* if the information is cheap to obtain. */
260/* Use UWX_KEY_TBASE for the base of the text segment */
261#define UWX_KEY_MODULE		17	/* Name of load module */
262#define UWX_KEY_FUNC		18	/* Name of function */
263#define UWX_KEY_FUNCSTART	19	/* Address of start of function */
264
265/* Register identifiers */
266/* For use in UWX_LKUP_FDESC result vectors and context access APIs. */
267/* "no spill info": These regs aren't spilled directly, so */
268/*    result vectors must not describe these registers. */
269/*    The result vector must describe the related register or */
270/*    pseudo register instead (ip:rp, sp:psp, bsp/cfm:pfs). */
271/* "pseudo register": Not a machine register, but treated as */
272/*    one for unwind purposes. */
273#define UWX_REG_IP		0	/* ip (no spill info) */
274#define UWX_REG_SP		1	/* sp (no spill info) */
275#define UWX_REG_BSP		2	/* ar.bsp (no spill info) */
276#define UWX_REG_CFM		3	/* cfm (no spill info) */
277#define UWX_REG_RP		4	/* rp (pseudo-register) */
278#define UWX_REG_PSP		5	/* psp (pseudo-register) */
279#define UWX_REG_PFS		6	/* pfs (pseudo-register) */
280#define UWX_REG_PREDS		7	/* p0 - p63 */
281#define UWX_REG_PRIUNAT		8	/* primary unat (pseudo-register) */
282#define UWX_REG_AR_BSPSTORE	9	/* ar.bspstore */
283#define UWX_REG_AR_RNAT		10	/* ar.rnat */
284#define UWX_REG_AR_UNAT		11	/* ar.unat */
285#define UWX_REG_AR_FPSR		12	/* ar.fpsr */
286#define UWX_REG_AR_LC		13	/* ar.lc */
287#define UWX_REG_AR_PFS		14	/* ar.pfs */
288#define UWX_REG_GP		15	/* gp (pseudo-register) */
289#define UWX_REG_GR(gr)		(0x100 | (gr))
290#define UWX_REG_FR(fr)		(0x200 | (fr))
291#define UWX_REG_BR(br)		(0x300 | (br))
292
293/* for backwards compatibility with previous releases... */
294#define UWX_REG_BSPSTORE	UWX_REG_AR_BSPSTORE
295#define UWX_REG_RNAT		UWX_REG_AR_RNAT
296#define UWX_REG_UNAT		UWX_REG_AR_UNAT
297#define UWX_REG_FPSR		UWX_REG_AR_FPSR
298#define UWX_REG_LC		UWX_REG_AR_LC
299
300/* Values corresponding to UWX_KEY_SPILL keys indicate the disposition */
301/* of the spilled register -- either in the memory stack or in another */
302/* register. The PSP register may also have a disposition of "SPPLUS", */
303/* indicating that its value is SP plus a fixed constant. */
304#define UWX_DISP_NONE		0		/* Not spilled */
305#define UWX_DISP_SPPLUS(k)	(1 | (k))	/* PSP = SP+constant */
306#define UWX_DISP_SPREL(disp)	(2 | (disp))	/* Spilled at [SP+disp] */
307#define UWX_DISP_PSPREL(disp)	(3 | (disp))	/* Spilled at [PSP+16-disp] */
308#define UWX_DISP_REG(reg)	(4 | ((reg) << 4)) /* Saved to another reg. */
309
310/* The uwx_get_spill_loc() routine returns a spill location for a */
311/* given register in the current context. It will return a disposition */
312/* code of UWX_DISP_NONE, UWX_DISP_REG(reg), or one of the following */
313/* to indicate that the spilled value can be found in the memory */
314/* stack or the register stack backing store. */
315#define UWX_DISP_MSTK(addr)	(5 | (addr))	/* Spilled in mem. stack */
316#define UWX_DISP_RSTK(addr)	(6 | (addr))	/* Spilled in reg. stack */
317
318/* Extract the disposition code, offset, address, or register id */
319/* from a disposition returned from uwx_get_spill_loc(). */
320/* Compare the extracted disp code against UWX_DISP_REG(0), etc. */
321#define UWX_GET_DISP_CODE(disp)		((int)(disp) & 0x07)
322#define UWX_GET_DISP_OFFSET(disp)	((disp) & ~(uint64_t)0x07)
323#define UWX_GET_DISP_ADDR(disp)		((disp) & ~(uint64_t)0x07)
324#define UWX_GET_DISP_REGID(disp)	((int)(disp) >> 4)
325
326#undef __EXTERN_C
327
328#if defined(__cplusplus)
329
330class UnwindExpress {
331
332public:
333
334    UnwindExpress() {
335	env = uwx_init();
336    }
337
338    ~UnwindExpress() {
339	if (env != 0)
340	    uwx_free(env);
341	env = 0;
342    }
343
344    int init_context(uint64_t ip, uint64_t sp, uint64_t bsp, uint64_t cfm) {
345	return uwx_init_context(env, ip, sp, bsp, cfm);
346    }
347
348    int init_history() {
349	return uwx_init_history(env);
350    }
351
352    int set_reg(int regid, uint64_t val) {
353	return uwx_set_reg(env, regid, val);
354    }
355
356    int set_fr(int regid, uint64_t *valp) {
357	return uwx_set_fr(env, regid, valp);
358    }
359
360    int step() {
361	return uwx_step(env);
362    }
363
364    int get_module_info(char **modp, uint64_t *text_base_p) {
365	return uwx_get_module_info(env, modp, text_base_p);
366    }
367
368    int get_funcstart(uint64_t *funcstart) {
369	return uwx_get_funcstart(env, funcstart);
370    }
371
372    int get_sym_info(char **modp, char **symp, uint64_t *offsetp) {
373	return uwx_get_sym_info(env, modp, symp, offsetp);
374    }
375
376    int find_symbol(struct uwx_symbol_cache **cachep,
377		char *mod, uint64_t relip, char **symp, uint64_t *offsetp) {
378	return uwx_find_symbol(env, cachep, mod, relip, symp, offsetp);
379    }
380
381    void release_symbol_cache(struct uwx_symbol_cache *symbol_cache) {
382	uwx_release_symbol_cache(env, symbol_cache);
383    }
384
385    int get_reg(int regid, uint64_t *valp) {
386	return uwx_get_reg(env, regid, valp);
387    }
388
389    int get_nat(int regid, int *natp) {
390	return uwx_get_nat(env, regid, natp);
391    }
392
393    int get_spill_loc(int regid, uint64_t *dispp) {
394	return uwx_get_spill_loc(env, regid, dispp);
395    }
396
397    int get_abi_context_code() {
398	return uwx_get_abi_context_code(env);
399    }
400
401    struct uwx_env *get_env() {
402	return env;
403    }
404
405protected:
406
407    struct uwx_env *env;
408
409};
410
411#endif /* __cplusplus */
412
413#endif /* __UWX_INCLUDED */
414