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