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 5 This file is part of libunwind. 6 7 Permission is hereby granted, free of charge, to any person obtaining 8 a copy of this software and associated documentation files (the 9 "Software"), to deal in the Software without restriction, including 10 without limitation the rights to use, copy, modify, merge, publish, 11 distribute, sublicense, and/or sell copies of the Software, and to 12 permit persons to whom the Software is furnished to do so, subject to 13 the following conditions: 14 15 The above copyright notice and this permission notice shall be 16 included in all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 25 26 /* Add For Cache MAP And ELF */ 27 #include <sys/types.h> 28 /* Add For Cache MAP And ELF */ 29 30 #define UNW_VERSION_MAJOR 1 31 #define UNW_VERSION_MINOR 3.1 32 #define UNW_VERSION_EXTRA 33 34 #define UNW_VERSION_CODE(maj, min) (((maj) << 16) | (min)) 35 #define UNW_VERSION UNW_VERSION_CODE(UNW_VERSION_MAJOR, UNW_VERSION_MINOR) 36 37 #define UNW_PASTE2(x, y) x##y 38 #define UNW_PASTE(x, y) UNW_PASTE2(x, y) 39 #define UNW_OBJ(fn) UNW_PASTE(UNW_PREFIX, fn) 40 #define UNW_ARCH_OBJ(fn) UNW_PASTE(UNW_PASTE(UNW_PASTE(_U, UNW_TARGET), _), fn) 41 42 #ifdef UNW_LOCAL_ONLY 43 #define UNW_PREFIX UNW_PASTE(UNW_PASTE(_UL, UNW_TARGET), _) 44 #else /* !UNW_LOCAL_ONLY */ 45 #define UNW_PREFIX UNW_PASTE(UNW_PASTE(_U, UNW_TARGET), _) 46 #endif /* !UNW_LOCAL_ONLY */ 47 48 /* Error codes. The unwind routines return the *negated* values of 49 these error codes on error and a non-negative value on success. */ 50 typedef enum 51 { 52 UNW_ESUCCESS = 0, /* no error */ 53 UNW_EUNSPEC, /* unspecified (general) error */ 54 UNW_ENOMEM, /* out of memory */ 55 UNW_EBADREG, /* bad register number */ 56 UNW_EREADONLYREG, /* attempt to write read-only register */ 57 UNW_ESTOPUNWIND, /* stop unwinding */ 58 UNW_EINVALIDIP, /* invalid IP */ 59 UNW_EBADFRAME, /* bad frame */ 60 UNW_EINVAL, /* unsupported operation or bad value */ 61 UNW_EBADVERSION, /* unwind info has unsupported version */ 62 UNW_ENOINFO /* no unwind info found */ 63 } unw_error_t; 64 65 /* The following enum defines the indices for a couple of 66 (pseudo-)registers which have the same meaning across all 67 platforms. (RO) means read-only. (RW) means read-write. General 68 registers (aka "integer registers") are expected to start with 69 index 0. The number of such registers is architecture-dependent. 70 The remaining indices can be used as an architecture sees fit. The 71 last valid register index is given by UNW_REG_LAST. */ 72 typedef enum 73 { 74 UNW_REG_IP = UNW_TDEP_IP, /* (rw) instruction pointer (pc) */ 75 UNW_REG_SP = UNW_TDEP_SP, /* (ro) stack pointer */ 76 UNW_REG_EH = UNW_TDEP_EH, /* (rw) exception-handling reg base */ 77 UNW_REG_LAST = UNW_TDEP_LAST_REG 78 } unw_frame_regnum_t; 79 80 /* Number of exception-handler argument registers: */ 81 #define UNW_NUM_EH_REGS UNW_TDEP_NUM_EH_REGS 82 83 typedef enum 84 { 85 UNW_CACHE_NONE, /* no caching */ 86 UNW_CACHE_GLOBAL, /* shared global cache */ 87 UNW_CACHE_PER_THREAD /* per-thread caching */ 88 } unw_caching_policy_t; 89 90 typedef enum 91 { 92 UNW_INIT_SIGNAL_FRAME = 1, /* We know this is a signal frame */ 93 } unw_init_local2_flags_t; 94 95 typedef int unw_regnum_t; 96 97 /* The unwind cursor starts at the youngest (most deeply nested) frame 98 and is used to track the frame state as the unwinder steps from 99 frame to frame. It is safe to make (shallow) copies of variables 100 of this type. */ 101 typedef struct unw_cursor 102 { 103 unw_word_t opaque[UNW_TDEP_CURSOR_LEN]; 104 } unw_cursor_t; 105 106 /* This type encapsulates the entire (preserved) machine-state. */ 107 typedef unw_tdep_context_t unw_context_t; 108 109 /* unw_getcontext() fills the unw_context_t pointed to by UC with the 110 machine state as it exists at the call-site. For implementation 111 reasons, this needs to be a target-dependent macro. It's easiest 112 to think of unw_getcontext() as being identical to getcontext(). */ 113 #define unw_getcontext(uc) unw_tdep_getcontext(uc) 114 115 /* Return 1 if register number R is a floating-point register, zero 116 otherwise. 117 This routine is signal-safe. */ 118 #define unw_is_fpreg(r) unw_tdep_is_fpreg(r) 119 120 typedef unw_tdep_fpreg_t unw_fpreg_t; 121 122 typedef struct unw_addr_space *unw_addr_space_t; 123 124 /* Each target may define it's own set of flags, but bits 0-15 are 125 reserved for general libunwind-use. */ 126 #define UNW_PI_FLAG_FIRST_TDEP_BIT 16 127 /* The information comes from a .debug_frame section. */ 128 #define UNW_PI_FLAG_DEBUG_FRAME 32 129 130 typedef struct unw_proc_info 131 { 132 unw_word_t start_ip; /* first IP covered by this procedure */ 133 unw_word_t end_ip; /* first IP NOT covered by this procedure */ 134 #if defined(NEED_LAST_IP) 135 unw_word_t last_ip; /* first IP that could begin another procedure */ 136 #endif 137 unw_word_t lsda; /* address of lang.-spec. data area (if any) */ 138 unw_word_t handler; /* optional personality routine */ 139 unw_word_t gp; /* global-pointer value for this procedure */ 140 unw_word_t flags; /* misc. flags */ 141 142 int format; /* unwind-info format (arch-specific) */ 143 int unwind_info_size; /* size of the information (if applicable) */ 144 void *unwind_info; /* unwind-info (arch-specific) */ 145 unw_tdep_proc_info_t extra; /* target-dependent auxiliary proc-info */ 146 } unw_proc_info_t; 147 148 typedef int (*unw_reg_states_callback)(void *token, 149 void *reg_states_data, 150 size_t reg_states_data_size, 151 unw_word_t start_ip, unw_word_t end_ip); 152 153 /* These are backend callback routines that provide access to the 154 state of a "remote" process. This can be used, for example, to 155 unwind another process through the ptrace() interface. */ 156 typedef struct unw_accessors 157 { 158 /* Look up the unwind info associated with instruction-pointer IP. 159 On success, the routine fills in the PROC_INFO structure. */ 160 int (*find_proc_info)(unw_addr_space_t, unw_word_t, unw_proc_info_t *, 161 int, void *); 162 163 /* Release any resources (e.g., memory) that were allocated for 164 the unwind info returned in by a previous call to 165 find_proc_info() with NEED_UNWIND_INFO set to 1. */ 166 void (*put_unwind_info)(unw_addr_space_t, unw_proc_info_t *, void *); 167 168 /* Return the list-head of the dynamically registered unwind 169 info. */ 170 int (*get_dyn_info_list_addr)(unw_addr_space_t, unw_word_t *, void *); 171 172 /* Access aligned word at address ADDR. The value is returned 173 according to the endianness of the host (e.g., if the host is 174 little-endian and the target is big-endian, access_mem() needs 175 to byte-swap the value before returning it). */ 176 int (*access_mem)(unw_addr_space_t, unw_word_t, unw_word_t *, int, 177 void *); 178 179 /* Access register number REG at address ADDR. */ 180 int (*access_reg)(unw_addr_space_t, unw_regnum_t, unw_word_t *, int, 181 void *); 182 183 /* Access register number REG at address ADDR. */ 184 int (*access_fpreg)(unw_addr_space_t, unw_regnum_t, 185 unw_fpreg_t *, int, void *); 186 187 int (*resume)(unw_addr_space_t, unw_cursor_t *, void *); 188 189 /* Optional call back to obtain the name of a (static) procedure. 190 Dynamically generated procedures are handled automatically by 191 libunwind. This callback is optional and may be set to 192 NULL. */ 193 int (*get_proc_name)(unw_addr_space_t, unw_word_t, char *, size_t, 194 unw_word_t *, void *); 195 } unw_accessors_t; 196 197 typedef enum unw_save_loc_type 198 { 199 UNW_SLT_NONE, /* register is not saved ("not an l-value") */ 200 UNW_SLT_MEMORY, /* register has been saved in memory */ 201 UNW_SLT_REG /* register has been saved in (another) register */ 202 } unw_save_loc_type_t; 203 204 typedef struct unw_save_loc 205 { 206 unw_save_loc_type_t type; 207 union { 208 unw_word_t addr; /* valid if type==UNW_SLT_MEMORY */ 209 unw_regnum_t regnum; /* valid if type==UNW_SLT_REG */ 210 } u; 211 unw_tdep_save_loc_t extra; /* target-dependent additional information */ 212 } unw_save_loc_t; 213 214 /* Add For Cache MAP And ELF */ 215 typedef struct unw_map_cursor 216 { 217 void *map_list; 218 void *cur_map; 219 } 220 unw_map_cursor_t; 221 222 typedef struct unw_map 223 { 224 unw_word_t start; 225 unw_word_t end; 226 char *path; 227 int flags; 228 } 229 unw_map_t; 230 /* Add For Cache MAP And ELF */ 231 232 /* These routines work both for local and remote unwinding. */ 233 234 #define unw_local_addr_space UNW_OBJ(local_addr_space) 235 #define unw_create_addr_space UNW_OBJ(create_addr_space) 236 #define unw_destroy_addr_space UNW_OBJ(destroy_addr_space) 237 #define unw_get_accessors UNW_ARCH_OBJ(get_accessors) 238 #define unw_get_accessors_int UNW_ARCH_OBJ(get_accessors_int) 239 #define unw_init_local UNW_OBJ(init_local) 240 #define unw_init_local2 UNW_OBJ(init_local2) 241 #define unw_init_remote UNW_OBJ(init_remote) 242 #define unw_step UNW_OBJ(step) 243 #define unw_resume UNW_OBJ(resume) 244 #define unw_get_proc_info UNW_OBJ(get_proc_info) 245 #define unw_get_proc_info_by_ip UNW_OBJ(get_proc_info_by_ip) 246 #define unw_reg_states_iterate UNW_OBJ(reg_states_iterate) 247 #define unw_apply_reg_state UNW_OBJ(apply_reg_state) 248 #define unw_get_reg UNW_OBJ(get_reg) 249 #define unw_set_reg UNW_OBJ(set_reg) 250 #define unw_get_fpreg UNW_OBJ(get_fpreg) 251 #define unw_set_fpreg UNW_OBJ(set_fpreg) 252 #define unw_get_save_loc UNW_OBJ(get_save_loc) 253 #define unw_is_signal_frame UNW_OBJ(is_signal_frame) 254 #define unw_handle_signal_frame UNW_OBJ(handle_signal_frame) 255 #define unw_get_proc_name UNW_OBJ(get_proc_name) 256 #define unw_set_caching_policy UNW_OBJ(set_caching_policy) 257 #define unw_set_cache_size UNW_OBJ(set_cache_size) 258 #define unw_regname UNW_ARCH_OBJ(regname) 259 #define unw_flush_cache UNW_ARCH_OBJ(flush_cache) 260 #define unw_strerror UNW_ARCH_OBJ(strerror) 261 262 #define unw_init_local_addr_space UNW_OBJ(init_local_addr_space) 263 #define unw_init_local_with_as UNW_OBJ(init_local_with_as) 264 265 extern unw_addr_space_t unw_create_addr_space(unw_accessors_t *, int); 266 extern void unw_destroy_addr_space(unw_addr_space_t); 267 extern unw_accessors_t *unw_get_accessors(unw_addr_space_t); 268 extern unw_accessors_t *unw_get_accessors_int(unw_addr_space_t); 269 extern void unw_flush_cache(unw_addr_space_t, unw_word_t, unw_word_t); 270 extern int unw_set_caching_policy(unw_addr_space_t, unw_caching_policy_t); 271 extern int unw_set_cache_size(unw_addr_space_t, size_t, int); 272 extern const char *unw_regname(unw_regnum_t); 273 274 extern int unw_init_local(unw_cursor_t *, unw_context_t *); 275 extern int unw_init_local2(unw_cursor_t *, unw_context_t *, int); 276 extern int unw_init_remote(unw_cursor_t *, unw_addr_space_t, void *); 277 extern int unw_step(unw_cursor_t *); 278 extern int unw_resume(unw_cursor_t *); 279 extern int unw_get_proc_info(unw_cursor_t *, unw_proc_info_t *); 280 extern int unw_get_proc_info_by_ip(unw_addr_space_t, unw_word_t, 281 unw_proc_info_t *, void *); 282 extern int unw_reg_states_iterate(unw_cursor_t *, unw_reg_states_callback, void *); 283 extern int unw_apply_reg_state(unw_cursor_t *, void *); 284 extern int unw_get_reg(unw_cursor_t *, int, unw_word_t *); 285 extern int unw_set_reg(unw_cursor_t *, int, unw_word_t); 286 extern int unw_get_fpreg(unw_cursor_t *, int, unw_fpreg_t *); 287 extern int unw_set_fpreg(unw_cursor_t *, int, unw_fpreg_t); 288 extern int unw_get_save_loc(unw_cursor_t *, int, unw_save_loc_t *); 289 extern int unw_is_signal_frame(unw_cursor_t *); 290 extern int unw_handle_signal_frame(unw_cursor_t *); 291 extern int unw_get_proc_name(unw_cursor_t *, char *, size_t, unw_word_t *); 292 extern const char *unw_strerror(int); 293 extern int unw_backtrace(void **, int); 294 295 extern void unw_map_set (unw_addr_space_t, unw_map_cursor_t *); 296 extern void unw_map_cursor_reset (unw_map_cursor_t *); 297 extern int unw_map_cursor_create (unw_map_cursor_t *, pid_t); 298 extern void unw_map_cursor_destroy (unw_map_cursor_t *); 299 extern int unw_map_cursor_get (unw_map_cursor_t *, unw_map_t *); 300 301 extern unw_addr_space_t unw_local_addr_space; 302