| 1 | // backtrace.h: |
| 2 | #ifndef BACKTRACE_H |
| 3 | #define BACKTRACE_H |
| 4 | |
| 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | #include <stdio.h> |
| 8 | |
| 9 | #ifdef __cplusplus |
| 10 | extern "C" { |
| 11 | #endif |
| 12 | |
| 13 | /* The backtrace state. This struct is intentionally not defined in |
| 14 | the public interface. */ |
| 15 | |
| 16 | struct backtrace_state; |
| 17 | |
| 18 | /* The type of the error callback argument to backtrace functions. |
| 19 | This function, if not NULL, will be called for certain error cases. |
| 20 | The DATA argument is passed to the function that calls this one. |
| 21 | The MSG argument is an error message. The ERRNUM argument, if |
| 22 | greater than 0, holds an errno value. The MSG buffer may become |
| 23 | invalid after this function returns. |
| 24 | |
| 25 | As a special case, the ERRNUM argument will be passed as -1 if no |
| 26 | debug info can be found for the executable, or if the debug info |
| 27 | exists but has an unsupported version, but the function requires |
| 28 | debug info (e.g., backtrace_full, backtrace_pcinfo). The MSG in |
| 29 | this case will be something along the lines of "no debug info". |
| 30 | Similarly, ERRNUM will be passed as -1 if there is no symbol table, |
| 31 | but the function requires a symbol table (e.g., backtrace_syminfo). |
| 32 | This may be used as a signal that some other approach should be |
| 33 | tried. */ |
| 34 | |
| 35 | typedef void (*backtrace_error_callback) (void *data, const char *msg, |
| 36 | int errnum); |
| 37 | |
| 38 | /* Create state information for the backtrace routines. This must be |
| 39 | called before any of the other routines, and its return value must |
| 40 | be passed to all of the other routines. FILENAME is the path name |
| 41 | of the executable file; if it is NULL the library will try |
| 42 | system-specific path names. If not NULL, FILENAME must point to a |
| 43 | permanent buffer. If THREADED is non-zero the state may be |
| 44 | accessed by multiple threads simultaneously, and the library will |
| 45 | use appropriate atomic operations. If THREADED is zero the state |
| 46 | may only be accessed by one thread at a time. This returns a state |
| 47 | pointer on success, NULL on error. If an error occurs, this will |
| 48 | call the ERROR_CALLBACK routine. |
| 49 | |
| 50 | Calling this function allocates resources that cannot be freed. |
| 51 | There is no backtrace_free_state function. The state is used to |
| 52 | cache information that is expensive to recompute. Programs are |
| 53 | expected to call this function at most once and to save the return |
| 54 | value for all later calls to backtrace functions. */ |
| 55 | |
| 56 | extern struct backtrace_state *backtrace_create_state ( |
| 57 | const char *filename, int threaded, |
| 58 | backtrace_error_callback error_callback, void *data); |
| 59 | |
| 60 | /* The type of the callback argument to the backtrace_full function. |
| 61 | DATA is the argument passed to backtrace_full. PC is the program |
| 62 | counter. FILENAME is the name of the file containing PC, or NULL |
| 63 | if not available. LINENO is the line number in FILENAME containing |
| 64 | PC, or 0 if not available. FUNCTION is the name of the function |
| 65 | containing PC, or NULL if not available. This should return 0 to |
| 66 | continuing tracing. The FILENAME and FUNCTION buffers may become |
| 67 | invalid after this function returns. */ |
| 68 | |
| 69 | typedef int (*backtrace_full_callback) (void *data, uintptr_t pc, |
| 70 | const char *filename, int lineno, |
| 71 | const char *function); |
| 72 | |
| 73 | /* Get a full stack backtrace. SKIP is the number of frames to skip; |
| 74 | passing 0 will start the trace with the function calling |
| 75 | backtrace_full. DATA is passed to the callback routine. If any |
| 76 | call to CALLBACK returns a non-zero value, the stack backtrace |
| 77 | stops, and backtrace returns that value; this may be used to limit |
| 78 | the number of stack frames desired. If all calls to CALLBACK |
| 79 | return 0, backtrace returns 0. The backtrace_full function will |
| 80 | make at least one call to either CALLBACK or ERROR_CALLBACK. This |
| 81 | function requires debug info for the executable. */ |
| 82 | |
| 83 | extern int backtrace_full (struct backtrace_state *state, int skip, |
| 84 | backtrace_full_callback callback, |
| 85 | backtrace_error_callback error_callback, |
| 86 | void *data); |
| 87 | |
| 88 | /* The type of the callback argument to the backtrace_simple function. |
| 89 | DATA is the argument passed to simple_backtrace. PC is the program |
| 90 | counter. This should return 0 to continue tracing. */ |
| 91 | |
| 92 | typedef int (*backtrace_simple_callback) (void *data, uintptr_t pc); |
| 93 | |
| 94 | /* Get a simple backtrace. SKIP is the number of frames to skip, as |
| 95 | in backtrace. DATA is passed to the callback routine. If any call |
| 96 | to CALLBACK returns a non-zero value, the stack backtrace stops, |
| 97 | and backtrace_simple returns that value. Otherwise |
| 98 | backtrace_simple returns 0. The backtrace_simple function will |
| 99 | make at least one call to either CALLBACK or ERROR_CALLBACK. This |
| 100 | function does not require any debug info for the executable. */ |
| 101 | |
| 102 | extern int backtrace_simple (struct backtrace_state *state, int skip, |
| 103 | backtrace_simple_callback callback, |
| 104 | backtrace_error_callback error_callback, |
| 105 | void *data); |
| 106 | |
| 107 | /* Print the current backtrace in a user readable format to a FILE. |
| 108 | SKIP is the number of frames to skip, as in backtrace_full. Any |
| 109 | error messages are printed to stderr. This function requires debug |
| 110 | info for the executable. */ |
| 111 | |
| 112 | extern void backtrace_print (struct backtrace_state *state, int skip, FILE *); |
| 113 | |
| 114 | /* Given PC, a program counter in the current program, call the |
| 115 | callback function with filename, line number, and function name |
| 116 | information. This will normally call the callback function exactly |
| 117 | once. However, if the PC happens to describe an inlined call, and |
| 118 | the debugging information contains the necessary information, then |
| 119 | this may call the callback function multiple times. This will make |
| 120 | at least one call to either CALLBACK or ERROR_CALLBACK. This |
| 121 | returns the first non-zero value returned by CALLBACK, or 0. */ |
| 122 | |
| 123 | extern int backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc, |
| 124 | backtrace_full_callback callback, |
| 125 | backtrace_error_callback error_callback, |
| 126 | void *data); |
| 127 | |
| 128 | /* The type of the callback argument to backtrace_syminfo. DATA and |
| 129 | PC are the arguments passed to backtrace_syminfo. SYMNAME is the |
| 130 | name of the symbol for the corresponding code. SYMVAL is the |
| 131 | value and SYMSIZE is the size of the symbol. SYMNAME will be NULL |
| 132 | if no error occurred but the symbol could not be found. */ |
| 133 | |
| 134 | typedef void (*backtrace_syminfo_callback) (void *data, uintptr_t pc, |
| 135 | const char *symname, |
| 136 | uintptr_t symval, |
| 137 | uintptr_t symsize); |
| 138 | |
| 139 | /* Given ADDR, an address or program counter in the current program, |
| 140 | call the callback information with the symbol name and value |
| 141 | describing the function or variable in which ADDR may be found. |
| 142 | This will call either CALLBACK or ERROR_CALLBACK exactly once. |
| 143 | This returns 1 on success, 0 on failure. This function requires |
| 144 | the symbol table but does not require the debug info. Note that if |
| 145 | the symbol table is present but ADDR could not be found in the |
| 146 | table, CALLBACK will be called with a NULL SYMNAME argument. |
| 147 | Returns 1 on success, 0 on error. */ |
| 148 | |
| 149 | extern int backtrace_syminfo (struct backtrace_state *state, uintptr_t addr, |
| 150 | backtrace_syminfo_callback callback, |
| 151 | backtrace_error_callback error_callback, |
| 152 | void *data); |
| 153 | |
| 154 | #ifdef __cplusplus |
| 155 | } /* End extern "C". */ |
| 156 | #endif |
| 157 | |
| 158 | #endif |
| 159 | // internal.h: |
| 160 | #ifndef BACKTRACE_INTERNAL_H |
| 161 | #define BACKTRACE_INTERNAL_H |
| 162 | |
| 163 | /* We assume that <sys/types.h> and "backtrace.h" have already been |
| 164 | included. */ |
| 165 | |
| 166 | #ifndef GCC_VERSION |
| 167 | # define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__) |
| 168 | #endif |
| 169 | |
| 170 | #if (GCC_VERSION < 2007) |
| 171 | # define __attribute__(x) |
| 172 | #endif |
| 173 | |
| 174 | #ifndef ATTRIBUTE_UNUSED |
| 175 | # define ATTRIBUTE_UNUSED __attribute__ ((__unused__)) |
| 176 | #endif |
| 177 | |
| 178 | #ifndef ATTRIBUTE_MALLOC |
| 179 | # if (GCC_VERSION >= 2096) |
| 180 | # define ATTRIBUTE_MALLOC __attribute__ ((__malloc__)) |
| 181 | # else |
| 182 | # define ATTRIBUTE_MALLOC |
| 183 | # endif |
| 184 | #endif |
| 185 | |
| 186 | #ifdef __has_attribute |
| 187 | # if __has_attribute(fallthrough) |
| 188 | # define ATTRIBUTE_FALLTHROUGH __attribute__ ((fallthrough)) |
| 189 | # endif |
| 190 | #endif |
| 191 | #ifndef ATTRIBUTE_FALLTHROUGH |
| 192 | # if (GCC_VERSION >= 7000) |
| 193 | # define ATTRIBUTE_FALLTHROUGH __attribute__ ((__fallthrough__)) |
| 194 | # else |
| 195 | # define ATTRIBUTE_FALLTHROUGH |
| 196 | # endif |
| 197 | #endif |
| 198 | |
| 199 | #ifndef HAVE_SYNC_FUNCTIONS |
| 200 | |
| 201 | /* Define out the sync functions. These should never be called if |
| 202 | they are not available. */ |
| 203 | |
| 204 | #define __sync_bool_compare_and_swap(A, B, C) (abort(), 1) |
| 205 | #define __sync_lock_test_and_set(A, B) (abort(), 0) |
| 206 | #define __sync_lock_release(A) abort() |
| 207 | |
| 208 | #endif /* !defined (HAVE_SYNC_FUNCTIONS) */ |
| 209 | |
| 210 | #ifdef HAVE_ATOMIC_FUNCTIONS |
| 211 | |
| 212 | /* We have the atomic builtin functions. */ |
| 213 | |
| 214 | #define backtrace_atomic_load_pointer(p) \ |
| 215 | __atomic_load_n ((p), __ATOMIC_ACQUIRE) |
| 216 | #define backtrace_atomic_load_int(p) \ |
| 217 | __atomic_load_n ((p), __ATOMIC_ACQUIRE) |
| 218 | #define backtrace_atomic_store_pointer(p, v) \ |
| 219 | __atomic_store_n ((p), (v), __ATOMIC_RELEASE) |
| 220 | #define backtrace_atomic_store_size_t(p, v) \ |
| 221 | __atomic_store_n ((p), (v), __ATOMIC_RELEASE) |
| 222 | #define backtrace_atomic_store_int(p, v) \ |
| 223 | __atomic_store_n ((p), (v), __ATOMIC_RELEASE) |
| 224 | |
| 225 | #else /* !defined (HAVE_ATOMIC_FUNCTIONS) */ |
| 226 | #ifdef HAVE_SYNC_FUNCTIONS |
| 227 | |
| 228 | /* We have the sync functions but not the atomic functions. Define |
| 229 | the atomic ones in terms of the sync ones. */ |
| 230 | |
| 231 | extern void *backtrace_atomic_load_pointer (void *); |
| 232 | extern int backtrace_atomic_load_int (int *); |
| 233 | extern void backtrace_atomic_store_pointer (void *, void *); |
| 234 | extern void backtrace_atomic_store_size_t (size_t *, size_t); |
| 235 | extern void backtrace_atomic_store_int (int *, int); |
| 236 | |
| 237 | #else /* !defined (HAVE_SYNC_FUNCTIONS) */ |
| 238 | |
| 239 | /* We have neither the sync nor the atomic functions. These will |
| 240 | never be called. */ |
| 241 | |
| 242 | #define backtrace_atomic_load_pointer(p) (abort(), (void *) NULL) |
| 243 | #define backtrace_atomic_load_int(p) (abort(), 0) |
| 244 | #define backtrace_atomic_store_pointer(p, v) abort() |
| 245 | #define backtrace_atomic_store_size_t(p, v) abort() |
| 246 | #define backtrace_atomic_store_int(p, v) abort() |
| 247 | |
| 248 | #endif /* !defined (HAVE_SYNC_FUNCTIONS) */ |
| 249 | #endif /* !defined (HAVE_ATOMIC_FUNCTIONS) */ |
| 250 | |
| 251 | /* The type of the function that collects file/line information. This |
| 252 | is like backtrace_pcinfo. */ |
| 253 | |
| 254 | typedef int (*fileline) (struct backtrace_state *state, uintptr_t pc, |
| 255 | backtrace_full_callback callback, |
| 256 | backtrace_error_callback error_callback, void *data); |
| 257 | |
| 258 | /* The type of the function that collects symbol information. This is |
| 259 | like backtrace_syminfo. */ |
| 260 | |
| 261 | typedef void (*syminfo) (struct backtrace_state *state, uintptr_t pc, |
| 262 | backtrace_syminfo_callback callback, |
| 263 | backtrace_error_callback error_callback, void *data); |
| 264 | |
| 265 | /* What the backtrace state pointer points to. */ |
| 266 | |
| 267 | struct backtrace_state |
| 268 | { |
| 269 | /* The name of the executable. */ |
| 270 | const char *filename; |
| 271 | /* Non-zero if threaded. */ |
| 272 | int threaded; |
| 273 | /* The master lock for fileline_fn, fileline_data, syminfo_fn, |
| 274 | syminfo_data, fileline_initialization_failed and everything the |
| 275 | data pointers point to. */ |
| 276 | void *lock; |
| 277 | /* The function that returns file/line information. */ |
| 278 | fileline fileline_fn; |
| 279 | /* The data to pass to FILELINE_FN. */ |
| 280 | void *fileline_data; |
| 281 | /* The function that returns symbol information. */ |
| 282 | syminfo syminfo_fn; |
| 283 | /* The data to pass to SYMINFO_FN. */ |
| 284 | void *syminfo_data; |
| 285 | /* Whether initializing the file/line information failed. */ |
| 286 | int fileline_initialization_failed; |
| 287 | /* The lock for the freelist. */ |
| 288 | int lock_alloc; |
| 289 | /* The freelist when using mmap. */ |
| 290 | struct backtrace_freelist_struct *freelist; |
| 291 | }; |
| 292 | |
| 293 | /* Open a file for reading. Returns -1 on error. If DOES_NOT_EXIST |
| 294 | is not NULL, *DOES_NOT_EXIST will be set to 0 normally and set to 1 |
| 295 | if the file does not exist. If the file does not exist and |
| 296 | DOES_NOT_EXIST is not NULL, the function will return -1 and will |
| 297 | not call ERROR_CALLBACK. On other errors, or if DOES_NOT_EXIST is |
| 298 | NULL, the function will call ERROR_CALLBACK before returning. */ |
| 299 | extern int backtrace_open (const char *filename, |
| 300 | backtrace_error_callback error_callback, |
| 301 | void *data, |
| 302 | int *does_not_exist); |
| 303 | |
| 304 | /* A view of the contents of a file. This supports mmap when |
| 305 | available. A view will remain in memory even after backtrace_close |
| 306 | is called on the file descriptor from which the view was |
| 307 | obtained. */ |
| 308 | |
| 309 | struct backtrace_view |
| 310 | { |
| 311 | /* The data that the caller requested. */ |
| 312 | const void *data; |
| 313 | /* The base of the view. */ |
| 314 | void *base; |
| 315 | /* The total length of the view. */ |
| 316 | size_t len; |
| 317 | }; |
| 318 | |
| 319 | /* Create a view of SIZE bytes from DESCRIPTOR at OFFSET. Store the |
| 320 | result in *VIEW. Returns 1 on success, 0 on error. */ |
| 321 | extern int backtrace_get_view (struct backtrace_state *state, int descriptor, |
| 322 | off_t offset, uint64_t size, |
| 323 | backtrace_error_callback error_callback, |
| 324 | void *data, struct backtrace_view *view); |
| 325 | |
| 326 | /* Release a view created by backtrace_get_view. */ |
| 327 | extern void backtrace_release_view (struct backtrace_state *state, |
| 328 | struct backtrace_view *view, |
| 329 | backtrace_error_callback error_callback, |
| 330 | void *data); |
| 331 | |
| 332 | /* Close a file opened by backtrace_open. Returns 1 on success, 0 on |
| 333 | error. */ |
| 334 | |
| 335 | extern int backtrace_close (int descriptor, |
| 336 | backtrace_error_callback error_callback, |
| 337 | void *data); |
| 338 | |
| 339 | /* Sort without using memory. */ |
| 340 | |
| 341 | extern void backtrace_qsort (void *base, size_t count, size_t size, |
| 342 | int (*compar) (const void *, const void *)); |
| 343 | |
| 344 | /* Allocate memory. This is like malloc. If ERROR_CALLBACK is NULL, |
| 345 | this does not report an error, it just returns NULL. */ |
| 346 | |
| 347 | extern void *backtrace_alloc (struct backtrace_state *state, size_t size, |
| 348 | backtrace_error_callback error_callback, |
| 349 | void *data) ATTRIBUTE_MALLOC; |
| 350 | |
| 351 | /* Free memory allocated by backtrace_alloc. If ERROR_CALLBACK is |
| 352 | NULL, this does not report an error. */ |
| 353 | |
| 354 | extern void backtrace_free (struct backtrace_state *state, void *mem, |
| 355 | size_t size, |
| 356 | backtrace_error_callback error_callback, |
| 357 | void *data); |
| 358 | |
| 359 | /* A growable vector of some struct. This is used for more efficient |
| 360 | allocation when we don't know the final size of some group of data |
| 361 | that we want to represent as an array. */ |
| 362 | |
| 363 | struct backtrace_vector |
| 364 | { |
| 365 | /* The base of the vector. */ |
| 366 | void *base; |
| 367 | /* The number of bytes in the vector. */ |
| 368 | size_t size; |
| 369 | /* The number of bytes available at the current allocation. */ |
| 370 | size_t alc; |
| 371 | }; |
| 372 | |
| 373 | /* Grow VEC by SIZE bytes. Return a pointer to the newly allocated |
| 374 | bytes. Note that this may move the entire vector to a new memory |
| 375 | location. Returns NULL on failure. */ |
| 376 | |
| 377 | extern void *backtrace_vector_grow (struct backtrace_state *state, size_t size, |
| 378 | backtrace_error_callback error_callback, |
| 379 | void *data, |
| 380 | struct backtrace_vector *vec); |
| 381 | |
| 382 | /* Finish the current allocation on VEC. Prepare to start a new |
| 383 | allocation. The finished allocation will never be freed. Returns |
| 384 | a pointer to the base of the finished entries, or NULL on |
| 385 | failure. */ |
| 386 | |
| 387 | extern void* backtrace_vector_finish (struct backtrace_state *state, |
| 388 | struct backtrace_vector *vec, |
| 389 | backtrace_error_callback error_callback, |
| 390 | void *data); |
| 391 | |
| 392 | /* Release any extra space allocated for VEC. This may change |
| 393 | VEC->base. Returns 1 on success, 0 on failure. */ |
| 394 | |
| 395 | extern int backtrace_vector_release (struct backtrace_state *state, |
| 396 | struct backtrace_vector *vec, |
| 397 | backtrace_error_callback error_callback, |
| 398 | void *data); |
| 399 | |
| 400 | /* Free the space managed by VEC. This will reset VEC. */ |
| 401 | |
| 402 | static inline void |
| 403 | backtrace_vector_free (struct backtrace_state *state, |
| 404 | struct backtrace_vector *vec, |
| 405 | backtrace_error_callback error_callback, void *data) |
| 406 | { |
| 407 | vec->alc += vec->size; |
| 408 | vec->size = 0; |
| 409 | backtrace_vector_release (state, vec, error_callback, data); |
| 410 | } |
| 411 | |
| 412 | /* Read initial debug data from a descriptor, and set the |
| 413 | fileline_data, syminfo_fn, and syminfo_data fields of STATE. |
| 414 | Return the fileln_fn field in *FILELN_FN--this is done this way so |
| 415 | that the synchronization code is only implemented once. This is |
| 416 | called after the descriptor has first been opened. It will close |
| 417 | the descriptor if it is no longer needed. Returns 1 on success, 0 |
| 418 | on error. There will be multiple implementations of this function, |
| 419 | for different file formats. Each system will compile the |
| 420 | appropriate one. */ |
| 421 | |
| 422 | extern int backtrace_initialize (struct backtrace_state *state, |
| 423 | const char *filename, |
| 424 | int descriptor, |
| 425 | backtrace_error_callback error_callback, |
| 426 | void *data, |
| 427 | fileline *fileline_fn); |
| 428 | |
| 429 | /* An enum for the DWARF sections we care about. */ |
| 430 | |
| 431 | enum dwarf_section |
| 432 | { |
| 433 | DEBUG_INFO, |
| 434 | DEBUG_LINE, |
| 435 | DEBUG_ABBREV, |
| 436 | DEBUG_RANGES, |
| 437 | DEBUG_STR, |
| 438 | DEBUG_ADDR, |
| 439 | DEBUG_STR_OFFSETS, |
| 440 | DEBUG_LINE_STR, |
| 441 | DEBUG_RNGLISTS, |
| 442 | |
| 443 | DEBUG_MAX |
| 444 | }; |
| 445 | |
| 446 | /* Data for the DWARF sections we care about. */ |
| 447 | |
| 448 | struct dwarf_sections |
| 449 | { |
| 450 | const unsigned char *data[DEBUG_MAX]; |
| 451 | size_t size[DEBUG_MAX]; |
| 452 | }; |
| 453 | |
| 454 | /* DWARF data read from a file, used for .gnu_debugaltlink. */ |
| 455 | |
| 456 | struct dwarf_data; |
| 457 | |
| 458 | /* The load address mapping. */ |
| 459 | |
| 460 | #if defined(__FDPIC__) && defined(HAVE_DL_ITERATE_PHDR) && (defined(HAVE_LINK_H) || defined(HAVE_SYS_LINK_H)) |
| 461 | |
| 462 | #ifdef HAVE_LINK_H |
| 463 | #include <link.h> |
| 464 | #endif |
| 465 | #ifdef HAVE_SYS_LINK_H |
| 466 | #include <sys/link.h> |
| 467 | #endif |
| 468 | |
| 469 | #define libbacktrace_using_fdpic() (1) |
| 470 | |
| 471 | struct libbacktrace_base_address |
| 472 | { |
| 473 | struct elf32_fdpic_loadaddr m; |
| 474 | }; |
| 475 | |
| 476 | #define libbacktrace_add_base(pc, base) \ |
| 477 | ((uintptr_t) (__RELOC_POINTER ((pc), (base).m))) |
| 478 | |
| 479 | #else /* not _FDPIC__ */ |
| 480 | |
| 481 | #define libbacktrace_using_fdpic() (0) |
| 482 | |
| 483 | struct libbacktrace_base_address |
| 484 | { |
| 485 | uintptr_t m; |
| 486 | }; |
| 487 | |
| 488 | #define libbacktrace_add_base(pc, base) ((pc) + (base).m) |
| 489 | |
| 490 | #endif /* not _FDPIC__ */ |
| 491 | |
| 492 | /* Add file/line information for a DWARF module. */ |
| 493 | |
| 494 | extern int backtrace_dwarf_add (struct backtrace_state *state, |
| 495 | struct libbacktrace_base_address base_address, |
| 496 | const struct dwarf_sections *dwarf_sections, |
| 497 | int is_bigendian, |
| 498 | struct dwarf_data *fileline_altlink, |
| 499 | backtrace_error_callback error_callback, |
| 500 | void *data, fileline *fileline_fn, |
| 501 | struct dwarf_data **fileline_entry); |
| 502 | |
| 503 | /* A data structure to pass to backtrace_syminfo_to_full. */ |
| 504 | |
| 505 | struct backtrace_call_full |
| 506 | { |
| 507 | backtrace_full_callback full_callback; |
| 508 | backtrace_error_callback full_error_callback; |
| 509 | void *full_data; |
| 510 | int ret; |
| 511 | }; |
| 512 | |
| 513 | /* A backtrace_syminfo_callback that can call into a |
| 514 | backtrace_full_callback, used when we have a symbol table but no |
| 515 | debug info. */ |
| 516 | |
| 517 | extern void backtrace_syminfo_to_full_callback (void *data, uintptr_t pc, |
| 518 | const char *symname, |
| 519 | uintptr_t symval, |
| 520 | uintptr_t symsize); |
| 521 | |
| 522 | /* An error callback that corresponds to |
| 523 | backtrace_syminfo_to_full_callback. */ |
| 524 | |
| 525 | extern void backtrace_syminfo_to_full_error_callback (void *, const char *, |
| 526 | int); |
| 527 | |
| 528 | /* A test-only hook for elf_uncompress_zdebug. */ |
| 529 | |
| 530 | extern int backtrace_uncompress_zdebug (struct backtrace_state *, |
| 531 | const unsigned char *compressed, |
| 532 | size_t compressed_size, |
| 533 | backtrace_error_callback, void *data, |
| 534 | unsigned char **uncompressed, |
| 535 | size_t *uncompressed_size); |
| 536 | |
| 537 | /* A test-only hook for elf_zstd_decompress. */ |
| 538 | |
| 539 | extern int backtrace_uncompress_zstd (struct backtrace_state *, |
| 540 | const unsigned char *compressed, |
| 541 | size_t compressed_size, |
| 542 | backtrace_error_callback, void *data, |
| 543 | unsigned char *uncompressed, |
| 544 | size_t uncompressed_size); |
| 545 | |
| 546 | /* A test-only hook for elf_uncompress_lzma. */ |
| 547 | |
| 548 | extern int backtrace_uncompress_lzma (struct backtrace_state *, |
| 549 | const unsigned char *compressed, |
| 550 | size_t compressed_size, |
| 551 | backtrace_error_callback, void *data, |
| 552 | unsigned char **uncompressed, |
| 553 | size_t *uncompressed_size); |
| 554 | |
| 555 | #endif |
| 556 | // filenames.h: |
| 557 | #ifndef GCC_VERSION |
| 558 | # define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__) |
| 559 | #endif |
| 560 | |
| 561 | #if (GCC_VERSION < 2007) |
| 562 | # define __attribute__(x) |
| 563 | #endif |
| 564 | |
| 565 | #ifndef ATTRIBUTE_UNUSED |
| 566 | # define ATTRIBUTE_UNUSED __attribute__ ((__unused__)) |
| 567 | #endif |
| 568 | |
| 569 | #if defined(__MSDOS__) || defined(_WIN32) || defined(__OS2__) || defined (__CYGWIN__) |
| 570 | # define IS_DIR_SEPARATOR(c) ((c) == '/' || (c) == '\\') |
| 571 | # define HAS_DRIVE_SPEC(f) ((f)[0] != '\0' && (f)[1] == ':') |
| 572 | # define IS_ABSOLUTE_PATH(f) (IS_DIR_SEPARATOR((f)[0]) || HAS_DRIVE_SPEC(f)) |
| 573 | #else |
| 574 | # define IS_DIR_SEPARATOR(c) ((c) == '/') |
| 575 | # define IS_ABSOLUTE_PATH(f) (IS_DIR_SEPARATOR((f)[0])) |
| 576 | #endif |
| 577 | // atomic.c: |
| 578 | #include <sys/types.h> |
| 579 | |
| 580 | |
| 581 | /* This file holds implementations of the atomic functions that are |
| 582 | used if the host compiler has the sync functions but not the atomic |
| 583 | functions, as is true of versions of GCC before 4.7. */ |
| 584 | |
| 585 | #if !defined (HAVE_ATOMIC_FUNCTIONS) && defined (HAVE_SYNC_FUNCTIONS) |
| 586 | |
| 587 | /* Do an atomic load of a pointer. */ |
| 588 | |
| 589 | void * |
| 590 | backtrace_atomic_load_pointer (void *arg) |
| 591 | { |
| 592 | void **pp; |
| 593 | void *p; |
| 594 | |
| 595 | pp = (void **) arg; |
| 596 | p = *pp; |
| 597 | while (!__sync_bool_compare_and_swap (pp, p, p)) |
| 598 | p = *pp; |
| 599 | return p; |
| 600 | } |
| 601 | |
| 602 | /* Do an atomic load of an int. */ |
| 603 | |
| 604 | int |
| 605 | backtrace_atomic_load_int (int *p) |
| 606 | { |
| 607 | int i; |
| 608 | |
| 609 | i = *p; |
| 610 | while (!__sync_bool_compare_and_swap (p, i, i)) |
| 611 | i = *p; |
| 612 | return i; |
| 613 | } |
| 614 | |
| 615 | /* Do an atomic store of a pointer. */ |
| 616 | |
| 617 | void |
| 618 | backtrace_atomic_store_pointer (void *arg, void *p) |
| 619 | { |
| 620 | void **pp; |
| 621 | void *old; |
| 622 | |
| 623 | pp = (void **) arg; |
| 624 | old = *pp; |
| 625 | while (!__sync_bool_compare_and_swap (pp, old, p)) |
| 626 | old = *pp; |
| 627 | } |
| 628 | |
| 629 | /* Do an atomic store of a size_t value. */ |
| 630 | |
| 631 | void |
| 632 | backtrace_atomic_store_size_t (size_t *p, size_t v) |
| 633 | { |
| 634 | size_t old; |
| 635 | |
| 636 | old = *p; |
| 637 | while (!__sync_bool_compare_and_swap (p, old, v)) |
| 638 | old = *p; |
| 639 | } |
| 640 | |
| 641 | /* Do an atomic store of a int value. */ |
| 642 | |
| 643 | void |
| 644 | backtrace_atomic_store_int (int *p, int v) |
| 645 | { |
| 646 | int old; |
| 647 | |
| 648 | old = *p; |
| 649 | while (!__sync_bool_compare_and_swap (p, old, v)) |
| 650 | old = *p; |
| 651 | } |
| 652 | |
| 653 | #endif |
| 654 | // dwarf.c: |
| 655 | #include <errno.h> |
| 656 | #include <stdlib.h> |
| 657 | #include <string.h> |
| 658 | #include <sys/types.h> |
| 659 | |
| 660 | |
| 661 | |
| 662 | /* DWARF constants. */ |
| 663 | |
| 664 | enum dwarf_tag { |
| 665 | DW_TAG_entry_point = 0x3, |
| 666 | DW_TAG_compile_unit = 0x11, |
| 667 | DW_TAG_inlined_subroutine = 0x1d, |
| 668 | DW_TAG_subprogram = 0x2e, |
| 669 | DW_TAG_skeleton_unit = 0x4a, |
| 670 | }; |
| 671 | |
| 672 | enum dwarf_form { |
| 673 | DW_FORM_addr = 0x01, |
| 674 | DW_FORM_block2 = 0x03, |
| 675 | DW_FORM_block4 = 0x04, |
| 676 | DW_FORM_data2 = 0x05, |
| 677 | DW_FORM_data4 = 0x06, |
| 678 | DW_FORM_data8 = 0x07, |
| 679 | DW_FORM_string = 0x08, |
| 680 | DW_FORM_block = 0x09, |
| 681 | DW_FORM_block1 = 0x0a, |
| 682 | DW_FORM_data1 = 0x0b, |
| 683 | DW_FORM_flag = 0x0c, |
| 684 | DW_FORM_sdata = 0x0d, |
| 685 | DW_FORM_strp = 0x0e, |
| 686 | DW_FORM_udata = 0x0f, |
| 687 | DW_FORM_ref_addr = 0x10, |
| 688 | DW_FORM_ref1 = 0x11, |
| 689 | DW_FORM_ref2 = 0x12, |
| 690 | DW_FORM_ref4 = 0x13, |
| 691 | DW_FORM_ref8 = 0x14, |
| 692 | DW_FORM_ref_udata = 0x15, |
| 693 | DW_FORM_indirect = 0x16, |
| 694 | DW_FORM_sec_offset = 0x17, |
| 695 | DW_FORM_exprloc = 0x18, |
| 696 | DW_FORM_flag_present = 0x19, |
| 697 | DW_FORM_ref_sig8 = 0x20, |
| 698 | DW_FORM_strx = 0x1a, |
| 699 | DW_FORM_addrx = 0x1b, |
| 700 | DW_FORM_ref_sup4 = 0x1c, |
| 701 | DW_FORM_strp_sup = 0x1d, |
| 702 | DW_FORM_data16 = 0x1e, |
| 703 | DW_FORM_line_strp = 0x1f, |
| 704 | DW_FORM_implicit_const = 0x21, |
| 705 | DW_FORM_loclistx = 0x22, |
| 706 | DW_FORM_rnglistx = 0x23, |
| 707 | DW_FORM_ref_sup8 = 0x24, |
| 708 | DW_FORM_strx1 = 0x25, |
| 709 | DW_FORM_strx2 = 0x26, |
| 710 | DW_FORM_strx3 = 0x27, |
| 711 | DW_FORM_strx4 = 0x28, |
| 712 | DW_FORM_addrx1 = 0x29, |
| 713 | DW_FORM_addrx2 = 0x2a, |
| 714 | DW_FORM_addrx3 = 0x2b, |
| 715 | DW_FORM_addrx4 = 0x2c, |
| 716 | DW_FORM_GNU_addr_index = 0x1f01, |
| 717 | DW_FORM_GNU_str_index = 0x1f02, |
| 718 | DW_FORM_GNU_ref_alt = 0x1f20, |
| 719 | DW_FORM_GNU_strp_alt = 0x1f21 |
| 720 | }; |
| 721 | |
| 722 | enum dwarf_attribute { |
| 723 | DW_AT_sibling = 0x01, |
| 724 | DW_AT_location = 0x02, |
| 725 | DW_AT_name = 0x03, |
| 726 | DW_AT_ordering = 0x09, |
| 727 | DW_AT_subscr_data = 0x0a, |
| 728 | DW_AT_byte_size = 0x0b, |
| 729 | DW_AT_bit_offset = 0x0c, |
| 730 | DW_AT_bit_size = 0x0d, |
| 731 | DW_AT_element_list = 0x0f, |
| 732 | DW_AT_stmt_list = 0x10, |
| 733 | DW_AT_low_pc = 0x11, |
| 734 | DW_AT_high_pc = 0x12, |
| 735 | DW_AT_language = 0x13, |
| 736 | DW_AT_member = 0x14, |
| 737 | DW_AT_discr = 0x15, |
| 738 | DW_AT_discr_value = 0x16, |
| 739 | DW_AT_visibility = 0x17, |
| 740 | DW_AT_import = 0x18, |
| 741 | DW_AT_string_length = 0x19, |
| 742 | DW_AT_common_reference = 0x1a, |
| 743 | DW_AT_comp_dir = 0x1b, |
| 744 | DW_AT_const_value = 0x1c, |
| 745 | DW_AT_containing_type = 0x1d, |
| 746 | DW_AT_default_value = 0x1e, |
| 747 | DW_AT_inline = 0x20, |
| 748 | DW_AT_is_optional = 0x21, |
| 749 | DW_AT_lower_bound = 0x22, |
| 750 | DW_AT_producer = 0x25, |
| 751 | DW_AT_prototyped = 0x27, |
| 752 | DW_AT_return_addr = 0x2a, |
| 753 | DW_AT_start_scope = 0x2c, |
| 754 | DW_AT_bit_stride = 0x2e, |
| 755 | DW_AT_upper_bound = 0x2f, |
| 756 | DW_AT_abstract_origin = 0x31, |
| 757 | DW_AT_accessibility = 0x32, |
| 758 | DW_AT_address_class = 0x33, |
| 759 | DW_AT_artificial = 0x34, |
| 760 | DW_AT_base_types = 0x35, |
| 761 | DW_AT_calling_convention = 0x36, |
| 762 | DW_AT_count = 0x37, |
| 763 | DW_AT_data_member_location = 0x38, |
| 764 | DW_AT_decl_column = 0x39, |
| 765 | DW_AT_decl_file = 0x3a, |
| 766 | DW_AT_decl_line = 0x3b, |
| 767 | DW_AT_declaration = 0x3c, |
| 768 | DW_AT_discr_list = 0x3d, |
| 769 | DW_AT_encoding = 0x3e, |
| 770 | DW_AT_external = 0x3f, |
| 771 | DW_AT_frame_base = 0x40, |
| 772 | DW_AT_friend = 0x41, |
| 773 | DW_AT_identifier_case = 0x42, |
| 774 | DW_AT_macro_info = 0x43, |
| 775 | DW_AT_namelist_items = 0x44, |
| 776 | DW_AT_priority = 0x45, |
| 777 | DW_AT_segment = 0x46, |
| 778 | DW_AT_specification = 0x47, |
| 779 | DW_AT_static_link = 0x48, |
| 780 | DW_AT_type = 0x49, |
| 781 | DW_AT_use_location = 0x4a, |
| 782 | DW_AT_variable_parameter = 0x4b, |
| 783 | DW_AT_virtuality = 0x4c, |
| 784 | DW_AT_vtable_elem_location = 0x4d, |
| 785 | DW_AT_allocated = 0x4e, |
| 786 | DW_AT_associated = 0x4f, |
| 787 | DW_AT_data_location = 0x50, |
| 788 | DW_AT_byte_stride = 0x51, |
| 789 | DW_AT_entry_pc = 0x52, |
| 790 | DW_AT_use_UTF8 = 0x53, |
| 791 | DW_AT_extension = 0x54, |
| 792 | DW_AT_ranges = 0x55, |
| 793 | DW_AT_trampoline = 0x56, |
| 794 | DW_AT_call_column = 0x57, |
| 795 | DW_AT_call_file = 0x58, |
| 796 | DW_AT_call_line = 0x59, |
| 797 | DW_AT_description = 0x5a, |
| 798 | DW_AT_binary_scale = 0x5b, |
| 799 | DW_AT_decimal_scale = 0x5c, |
| 800 | DW_AT_small = 0x5d, |
| 801 | DW_AT_decimal_sign = 0x5e, |
| 802 | DW_AT_digit_count = 0x5f, |
| 803 | DW_AT_picture_string = 0x60, |
| 804 | DW_AT_mutable = 0x61, |
| 805 | DW_AT_threads_scaled = 0x62, |
| 806 | DW_AT_explicit = 0x63, |
| 807 | DW_AT_object_pointer = 0x64, |
| 808 | DW_AT_endianity = 0x65, |
| 809 | DW_AT_elemental = 0x66, |
| 810 | DW_AT_pure = 0x67, |
| 811 | DW_AT_recursive = 0x68, |
| 812 | DW_AT_signature = 0x69, |
| 813 | DW_AT_main_subprogram = 0x6a, |
| 814 | DW_AT_data_bit_offset = 0x6b, |
| 815 | DW_AT_const_expr = 0x6c, |
| 816 | DW_AT_enum_class = 0x6d, |
| 817 | DW_AT_linkage_name = 0x6e, |
| 818 | DW_AT_string_length_bit_size = 0x6f, |
| 819 | DW_AT_string_length_byte_size = 0x70, |
| 820 | DW_AT_rank = 0x71, |
| 821 | DW_AT_str_offsets_base = 0x72, |
| 822 | DW_AT_addr_base = 0x73, |
| 823 | DW_AT_rnglists_base = 0x74, |
| 824 | DW_AT_dwo_name = 0x76, |
| 825 | DW_AT_reference = 0x77, |
| 826 | DW_AT_rvalue_reference = 0x78, |
| 827 | DW_AT_macros = 0x79, |
| 828 | DW_AT_call_all_calls = 0x7a, |
| 829 | DW_AT_call_all_source_calls = 0x7b, |
| 830 | DW_AT_call_all_tail_calls = 0x7c, |
| 831 | DW_AT_call_return_pc = 0x7d, |
| 832 | DW_AT_call_value = 0x7e, |
| 833 | DW_AT_call_origin = 0x7f, |
| 834 | DW_AT_call_parameter = 0x80, |
| 835 | DW_AT_call_pc = 0x81, |
| 836 | DW_AT_call_tail_call = 0x82, |
| 837 | DW_AT_call_target = 0x83, |
| 838 | DW_AT_call_target_clobbered = 0x84, |
| 839 | DW_AT_call_data_location = 0x85, |
| 840 | DW_AT_call_data_value = 0x86, |
| 841 | DW_AT_noreturn = 0x87, |
| 842 | DW_AT_alignment = 0x88, |
| 843 | DW_AT_export_symbols = 0x89, |
| 844 | DW_AT_deleted = 0x8a, |
| 845 | DW_AT_defaulted = 0x8b, |
| 846 | DW_AT_loclists_base = 0x8c, |
| 847 | DW_AT_lo_user = 0x2000, |
| 848 | DW_AT_hi_user = 0x3fff, |
| 849 | DW_AT_MIPS_fde = 0x2001, |
| 850 | DW_AT_MIPS_loop_begin = 0x2002, |
| 851 | DW_AT_MIPS_tail_loop_begin = 0x2003, |
| 852 | DW_AT_MIPS_epilog_begin = 0x2004, |
| 853 | DW_AT_MIPS_loop_unroll_factor = 0x2005, |
| 854 | DW_AT_MIPS_software_pipeline_depth = 0x2006, |
| 855 | DW_AT_MIPS_linkage_name = 0x2007, |
| 856 | DW_AT_MIPS_stride = 0x2008, |
| 857 | DW_AT_MIPS_abstract_name = 0x2009, |
| 858 | DW_AT_MIPS_clone_origin = 0x200a, |
| 859 | DW_AT_MIPS_has_inlines = 0x200b, |
| 860 | DW_AT_HP_block_index = 0x2000, |
| 861 | DW_AT_HP_unmodifiable = 0x2001, |
| 862 | DW_AT_HP_prologue = 0x2005, |
| 863 | DW_AT_HP_epilogue = 0x2008, |
| 864 | DW_AT_HP_actuals_stmt_list = 0x2010, |
| 865 | DW_AT_HP_proc_per_section = 0x2011, |
| 866 | DW_AT_HP_raw_data_ptr = 0x2012, |
| 867 | DW_AT_HP_pass_by_reference = 0x2013, |
| 868 | DW_AT_HP_opt_level = 0x2014, |
| 869 | DW_AT_HP_prof_version_id = 0x2015, |
| 870 | DW_AT_HP_opt_flags = 0x2016, |
| 871 | DW_AT_HP_cold_region_low_pc = 0x2017, |
| 872 | DW_AT_HP_cold_region_high_pc = 0x2018, |
| 873 | DW_AT_HP_all_variables_modifiable = 0x2019, |
| 874 | DW_AT_HP_linkage_name = 0x201a, |
| 875 | DW_AT_HP_prof_flags = 0x201b, |
| 876 | DW_AT_HP_unit_name = 0x201f, |
| 877 | DW_AT_HP_unit_size = 0x2020, |
| 878 | DW_AT_HP_widened_byte_size = 0x2021, |
| 879 | DW_AT_HP_definition_points = 0x2022, |
| 880 | DW_AT_HP_default_location = 0x2023, |
| 881 | DW_AT_HP_is_result_param = 0x2029, |
| 882 | DW_AT_sf_names = 0x2101, |
| 883 | DW_AT_src_info = 0x2102, |
| 884 | DW_AT_mac_info = 0x2103, |
| 885 | DW_AT_src_coords = 0x2104, |
| 886 | DW_AT_body_begin = 0x2105, |
| 887 | DW_AT_body_end = 0x2106, |
| 888 | DW_AT_GNU_vector = 0x2107, |
| 889 | DW_AT_GNU_guarded_by = 0x2108, |
| 890 | DW_AT_GNU_pt_guarded_by = 0x2109, |
| 891 | DW_AT_GNU_guarded = 0x210a, |
| 892 | DW_AT_GNU_pt_guarded = 0x210b, |
| 893 | DW_AT_GNU_locks_excluded = 0x210c, |
| 894 | DW_AT_GNU_exclusive_locks_required = 0x210d, |
| 895 | DW_AT_GNU_shared_locks_required = 0x210e, |
| 896 | DW_AT_GNU_odr_signature = 0x210f, |
| 897 | DW_AT_GNU_template_name = 0x2110, |
| 898 | DW_AT_GNU_call_site_value = 0x2111, |
| 899 | DW_AT_GNU_call_site_data_value = 0x2112, |
| 900 | DW_AT_GNU_call_site_target = 0x2113, |
| 901 | DW_AT_GNU_call_site_target_clobbered = 0x2114, |
| 902 | DW_AT_GNU_tail_call = 0x2115, |
| 903 | DW_AT_GNU_all_tail_call_sites = 0x2116, |
| 904 | DW_AT_GNU_all_call_sites = 0x2117, |
| 905 | DW_AT_GNU_all_source_call_sites = 0x2118, |
| 906 | DW_AT_GNU_macros = 0x2119, |
| 907 | DW_AT_GNU_deleted = 0x211a, |
| 908 | DW_AT_GNU_dwo_name = 0x2130, |
| 909 | DW_AT_GNU_dwo_id = 0x2131, |
| 910 | DW_AT_GNU_ranges_base = 0x2132, |
| 911 | DW_AT_GNU_addr_base = 0x2133, |
| 912 | DW_AT_GNU_pubnames = 0x2134, |
| 913 | DW_AT_GNU_pubtypes = 0x2135, |
| 914 | DW_AT_GNU_discriminator = 0x2136, |
| 915 | DW_AT_GNU_locviews = 0x2137, |
| 916 | DW_AT_GNU_entry_view = 0x2138, |
| 917 | DW_AT_VMS_rtnbeg_pd_address = 0x2201, |
| 918 | DW_AT_use_GNAT_descriptive_type = 0x2301, |
| 919 | DW_AT_GNAT_descriptive_type = 0x2302, |
| 920 | DW_AT_GNU_numerator = 0x2303, |
| 921 | DW_AT_GNU_denominator = 0x2304, |
| 922 | DW_AT_GNU_bias = 0x2305, |
| 923 | DW_AT_upc_threads_scaled = 0x3210, |
| 924 | DW_AT_PGI_lbase = 0x3a00, |
| 925 | DW_AT_PGI_soffset = 0x3a01, |
| 926 | DW_AT_PGI_lstride = 0x3a02, |
| 927 | DW_AT_APPLE_optimized = 0x3fe1, |
| 928 | DW_AT_APPLE_flags = 0x3fe2, |
| 929 | DW_AT_APPLE_isa = 0x3fe3, |
| 930 | DW_AT_APPLE_block = 0x3fe4, |
| 931 | DW_AT_APPLE_major_runtime_vers = 0x3fe5, |
| 932 | DW_AT_APPLE_runtime_class = 0x3fe6, |
| 933 | DW_AT_APPLE_omit_frame_ptr = 0x3fe7, |
| 934 | DW_AT_APPLE_property_name = 0x3fe8, |
| 935 | DW_AT_APPLE_property_getter = 0x3fe9, |
| 936 | DW_AT_APPLE_property_setter = 0x3fea, |
| 937 | DW_AT_APPLE_property_attribute = 0x3feb, |
| 938 | DW_AT_APPLE_objc_complete_type = 0x3fec, |
| 939 | DW_AT_APPLE_property = 0x3fed |
| 940 | }; |
| 941 | |
| 942 | enum dwarf_line_number_op { |
| 943 | DW_LNS_extended_op = 0x0, |
| 944 | DW_LNS_copy = 0x1, |
| 945 | DW_LNS_advance_pc = 0x2, |
| 946 | DW_LNS_advance_line = 0x3, |
| 947 | DW_LNS_set_file = 0x4, |
| 948 | DW_LNS_set_column = 0x5, |
| 949 | DW_LNS_negate_stmt = 0x6, |
| 950 | DW_LNS_set_basic_block = 0x7, |
| 951 | DW_LNS_const_add_pc = 0x8, |
| 952 | DW_LNS_fixed_advance_pc = 0x9, |
| 953 | DW_LNS_set_prologue_end = 0xa, |
| 954 | DW_LNS_set_epilogue_begin = 0xb, |
| 955 | DW_LNS_set_isa = 0xc, |
| 956 | }; |
| 957 | |
| 958 | enum dwarf_extended_line_number_op { |
| 959 | DW_LNE_end_sequence = 0x1, |
| 960 | DW_LNE_set_address = 0x2, |
| 961 | DW_LNE_define_file = 0x3, |
| 962 | DW_LNE_set_discriminator = 0x4, |
| 963 | }; |
| 964 | |
| 965 | enum dwarf_line_number_content_type { |
| 966 | DW_LNCT_path = 0x1, |
| 967 | DW_LNCT_directory_index = 0x2, |
| 968 | DW_LNCT_timestamp = 0x3, |
| 969 | DW_LNCT_size = 0x4, |
| 970 | DW_LNCT_MD5 = 0x5, |
| 971 | DW_LNCT_lo_user = 0x2000, |
| 972 | DW_LNCT_hi_user = 0x3fff |
| 973 | }; |
| 974 | |
| 975 | enum dwarf_range_list_entry { |
| 976 | DW_RLE_end_of_list = 0x00, |
| 977 | DW_RLE_base_addressx = 0x01, |
| 978 | DW_RLE_startx_endx = 0x02, |
| 979 | DW_RLE_startx_length = 0x03, |
| 980 | DW_RLE_offset_pair = 0x04, |
| 981 | DW_RLE_base_address = 0x05, |
| 982 | DW_RLE_start_end = 0x06, |
| 983 | DW_RLE_start_length = 0x07 |
| 984 | }; |
| 985 | |
| 986 | enum dwarf_unit_type { |
| 987 | DW_UT_compile = 0x01, |
| 988 | DW_UT_type = 0x02, |
| 989 | DW_UT_partial = 0x03, |
| 990 | DW_UT_skeleton = 0x04, |
| 991 | DW_UT_split_compile = 0x05, |
| 992 | DW_UT_split_type = 0x06, |
| 993 | DW_UT_lo_user = 0x80, |
| 994 | DW_UT_hi_user = 0xff |
| 995 | }; |
| 996 | |
| 997 | #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN |
| 998 | |
| 999 | /* If strnlen is not declared, provide our own version. */ |
| 1000 | |
| 1001 | static size_t |
| 1002 | xstrnlen (const char *s, size_t maxlen) |
| 1003 | { |
| 1004 | size_t i; |
| 1005 | |
| 1006 | for (i = 0; i < maxlen; ++i) |
| 1007 | if (s[i] == '\0') |
| 1008 | break; |
| 1009 | return i; |
| 1010 | } |
| 1011 | |
| 1012 | #define strnlen xstrnlen |
| 1013 | |
| 1014 | #endif |
| 1015 | |
| 1016 | /* A buffer to read DWARF info. */ |
| 1017 | |
| 1018 | struct dwarf_buf |
| 1019 | { |
| 1020 | /* Buffer name for error messages. */ |
| 1021 | const char *name; |
| 1022 | /* Start of the buffer. */ |
| 1023 | const unsigned char *start; |
| 1024 | /* Next byte to read. */ |
| 1025 | const unsigned char *buf; |
| 1026 | /* The number of bytes remaining. */ |
| 1027 | size_t left; |
| 1028 | /* Whether the data is big-endian. */ |
| 1029 | int is_bigendian; |
| 1030 | /* Error callback routine. */ |
| 1031 | backtrace_error_callback error_callback; |
| 1032 | /* Data for error_callback. */ |
| 1033 | void *data; |
| 1034 | /* Non-zero if we've reported an underflow error. */ |
| 1035 | int reported_underflow; |
| 1036 | }; |
| 1037 | |
| 1038 | /* A single attribute in a DWARF abbreviation. */ |
| 1039 | |
| 1040 | struct attr |
| 1041 | { |
| 1042 | /* The attribute name. */ |
| 1043 | enum dwarf_attribute name; |
| 1044 | /* The attribute form. */ |
| 1045 | enum dwarf_form form; |
| 1046 | /* The attribute value, for DW_FORM_implicit_const. */ |
| 1047 | int64_t val; |
| 1048 | }; |
| 1049 | |
| 1050 | /* A single DWARF abbreviation. */ |
| 1051 | |
| 1052 | struct abbrev |
| 1053 | { |
| 1054 | /* The abbrev code--the number used to refer to the abbrev. */ |
| 1055 | uint64_t code; |
| 1056 | /* The entry tag. */ |
| 1057 | enum dwarf_tag tag; |
| 1058 | /* Non-zero if this abbrev has child entries. */ |
| 1059 | int has_children; |
| 1060 | /* The number of attributes. */ |
| 1061 | size_t num_attrs; |
| 1062 | /* The attributes. */ |
| 1063 | struct attr *attrs; |
| 1064 | }; |
| 1065 | |
| 1066 | /* The DWARF abbreviations for a compilation unit. This structure |
| 1067 | only exists while reading the compilation unit. Most DWARF readers |
| 1068 | seem to a hash table to map abbrev ID's to abbrev entries. |
| 1069 | However, we primarily care about GCC, and GCC simply issues ID's in |
| 1070 | numerical order starting at 1. So we simply keep a sorted vector, |
| 1071 | and try to just look up the code. */ |
| 1072 | |
| 1073 | struct abbrevs |
| 1074 | { |
| 1075 | /* The number of abbrevs in the vector. */ |
| 1076 | size_t num_abbrevs; |
| 1077 | /* The abbrevs, sorted by the code field. */ |
| 1078 | struct abbrev *abbrevs; |
| 1079 | }; |
| 1080 | |
| 1081 | /* The different kinds of attribute values. */ |
| 1082 | |
| 1083 | enum attr_val_encoding |
| 1084 | { |
| 1085 | /* No attribute value. */ |
| 1086 | ATTR_VAL_NONE, |
| 1087 | /* An address. */ |
| 1088 | ATTR_VAL_ADDRESS, |
| 1089 | /* An index into the .debug_addr section, whose value is relative to |
| 1090 | the DW_AT_addr_base attribute of the compilation unit. */ |
| 1091 | ATTR_VAL_ADDRESS_INDEX, |
| 1092 | /* A unsigned integer. */ |
| 1093 | ATTR_VAL_UINT, |
| 1094 | /* A sigd integer. */ |
| 1095 | ATTR_VAL_SINT, |
| 1096 | /* A string. */ |
| 1097 | ATTR_VAL_STRING, |
| 1098 | /* An index into the .debug_str_offsets section. */ |
| 1099 | ATTR_VAL_STRING_INDEX, |
| 1100 | /* An offset to other data in the containing unit. */ |
| 1101 | ATTR_VAL_REF_UNIT, |
| 1102 | /* An offset to other data within the .debug_info section. */ |
| 1103 | ATTR_VAL_REF_INFO, |
| 1104 | /* An offset to other data within the alt .debug_info section. */ |
| 1105 | ATTR_VAL_REF_ALT_INFO, |
| 1106 | /* An offset to data in some other section. */ |
| 1107 | ATTR_VAL_REF_SECTION, |
| 1108 | /* A type signature. */ |
| 1109 | ATTR_VAL_REF_TYPE, |
| 1110 | /* An index into the .debug_rnglists section. */ |
| 1111 | ATTR_VAL_RNGLISTS_INDEX, |
| 1112 | /* A block of data (not represented). */ |
| 1113 | ATTR_VAL_BLOCK, |
| 1114 | /* An expression (not represented). */ |
| 1115 | ATTR_VAL_EXPR, |
| 1116 | }; |
| 1117 | |
| 1118 | /* An attribute value. */ |
| 1119 | |
| 1120 | struct attr_val |
| 1121 | { |
| 1122 | /* How the value is stored in the field u. */ |
| 1123 | enum attr_val_encoding encoding; |
| 1124 | union |
| 1125 | { |
| 1126 | /* ATTR_VAL_ADDRESS*, ATTR_VAL_UINT, ATTR_VAL_REF*. */ |
| 1127 | uint64_t uint; |
| 1128 | /* ATTR_VAL_SINT. */ |
| 1129 | int64_t sint; |
| 1130 | /* ATTR_VAL_STRING. */ |
| 1131 | const char *string; |
| 1132 | /* ATTR_VAL_BLOCK not stored. */ |
| 1133 | } u; |
| 1134 | }; |
| 1135 | |
| 1136 | /* The line number program header. */ |
| 1137 | |
| 1138 | struct line_header |
| 1139 | { |
| 1140 | /* The version of the line number information. */ |
| 1141 | int version; |
| 1142 | /* Address size. */ |
| 1143 | int addrsize; |
| 1144 | /* The minimum instruction length. */ |
| 1145 | unsigned int min_insn_len; |
| 1146 | /* The maximum number of ops per instruction. */ |
| 1147 | unsigned int max_ops_per_insn; |
| 1148 | /* The line base for special opcodes. */ |
| 1149 | int line_base; |
| 1150 | /* The line range for special opcodes. */ |
| 1151 | unsigned int line_range; |
| 1152 | /* The opcode base--the first special opcode. */ |
| 1153 | unsigned int opcode_base; |
| 1154 | /* Opcode lengths, indexed by opcode - 1. */ |
| 1155 | const unsigned char *opcode_lengths; |
| 1156 | /* The number of directory entries. */ |
| 1157 | size_t dirs_count; |
| 1158 | /* The directory entries. */ |
| 1159 | const char **dirs; |
| 1160 | /* The number of filenames. */ |
| 1161 | size_t filenames_count; |
| 1162 | /* The filenames. */ |
| 1163 | const char **filenames; |
| 1164 | }; |
| 1165 | |
| 1166 | /* A format description from a line header. */ |
| 1167 | |
| 1168 | struct line_header_format |
| 1169 | { |
| 1170 | int lnct; /* LNCT code. */ |
| 1171 | enum dwarf_form form; /* Form of entry data. */ |
| 1172 | }; |
| 1173 | |
| 1174 | /* Map a single PC value to a file/line. We will keep a vector of |
| 1175 | these sorted by PC value. Each file/line will be correct from the |
| 1176 | PC up to the PC of the next entry if there is one. We allocate one |
| 1177 | extra entry at the end so that we can use bsearch. */ |
| 1178 | |
| 1179 | struct line |
| 1180 | { |
| 1181 | /* PC. */ |
| 1182 | uintptr_t pc; |
| 1183 | /* File name. Many entries in the array are expected to point to |
| 1184 | the same file name. */ |
| 1185 | const char *filename; |
| 1186 | /* Line number. */ |
| 1187 | int lineno; |
| 1188 | /* Index of the object in the original array read from the DWARF |
| 1189 | section, before it has been sorted. The index makes it possible |
| 1190 | to use Quicksort and maintain stability. */ |
| 1191 | int idx; |
| 1192 | }; |
| 1193 | |
| 1194 | /* A growable vector of line number information. This is used while |
| 1195 | reading the line numbers. */ |
| 1196 | |
| 1197 | struct line_vector |
| 1198 | { |
| 1199 | /* Memory. This is an array of struct line. */ |
| 1200 | struct backtrace_vector vec; |
| 1201 | /* Number of valid mappings. */ |
| 1202 | size_t count; |
| 1203 | }; |
| 1204 | |
| 1205 | /* A function described in the debug info. */ |
| 1206 | |
| 1207 | struct function |
| 1208 | { |
| 1209 | /* The name of the function. */ |
| 1210 | const char *name; |
| 1211 | /* If this is an inlined function, the filename of the call |
| 1212 | site. */ |
| 1213 | const char *caller_filename; |
| 1214 | /* If this is an inlined function, the line number of the call |
| 1215 | site. */ |
| 1216 | int caller_lineno; |
| 1217 | /* Map PC ranges to inlined functions. */ |
| 1218 | struct function_addrs *function_addrs; |
| 1219 | size_t function_addrs_count; |
| 1220 | }; |
| 1221 | |
| 1222 | /* An address range for a function. This maps a PC value to a |
| 1223 | specific function. */ |
| 1224 | |
| 1225 | struct function_addrs |
| 1226 | { |
| 1227 | /* Range is LOW <= PC < HIGH. */ |
| 1228 | uintptr_t low; |
| 1229 | uintptr_t high; |
| 1230 | /* Function for this address range. */ |
| 1231 | struct function *function; |
| 1232 | }; |
| 1233 | |
| 1234 | /* A growable vector of function address ranges. */ |
| 1235 | |
| 1236 | struct function_vector |
| 1237 | { |
| 1238 | /* Memory. This is an array of struct function_addrs. */ |
| 1239 | struct backtrace_vector vec; |
| 1240 | /* Number of address ranges present. */ |
| 1241 | size_t count; |
| 1242 | }; |
| 1243 | |
| 1244 | /* A DWARF compilation unit. This only holds the information we need |
| 1245 | to map a PC to a file and line. */ |
| 1246 | |
| 1247 | struct unit |
| 1248 | { |
| 1249 | /* The first entry for this compilation unit. */ |
| 1250 | const unsigned char *unit_data; |
| 1251 | /* The length of the data for this compilation unit. */ |
| 1252 | size_t unit_data_len; |
| 1253 | /* The offset of UNIT_DATA from the start of the information for |
| 1254 | this compilation unit. */ |
| 1255 | size_t unit_data_offset; |
| 1256 | /* Offset of the start of the compilation unit from the start of the |
| 1257 | .debug_info section. */ |
| 1258 | size_t low_offset; |
| 1259 | /* Offset of the end of the compilation unit from the start of the |
| 1260 | .debug_info section. */ |
| 1261 | size_t high_offset; |
| 1262 | /* DWARF version. */ |
| 1263 | int version; |
| 1264 | /* Whether unit is DWARF64. */ |
| 1265 | int is_dwarf64; |
| 1266 | /* Address size. */ |
| 1267 | int addrsize; |
| 1268 | /* Offset into line number information. */ |
| 1269 | off_t lineoff; |
| 1270 | /* Offset of compilation unit in .debug_str_offsets. */ |
| 1271 | uint64_t str_offsets_base; |
| 1272 | /* Offset of compilation unit in .debug_addr. */ |
| 1273 | uint64_t addr_base; |
| 1274 | /* Offset of compilation unit in .debug_rnglists. */ |
| 1275 | uint64_t rnglists_base; |
| 1276 | /* Primary source file. */ |
| 1277 | const char *filename; |
| 1278 | /* Compilation command working directory. */ |
| 1279 | const char *comp_dir; |
| 1280 | /* Absolute file name, only set if needed. */ |
| 1281 | const char *abs_filename; |
| 1282 | /* The abbreviations for this unit. */ |
| 1283 | struct abbrevs abbrevs; |
| 1284 | |
| 1285 | /* The fields above this point are read in during initialization and |
| 1286 | may be accessed freely. The fields below this point are read in |
| 1287 | as needed, and therefore require care, as different threads may |
| 1288 | try to initialize them simultaneously. */ |
| 1289 | |
| 1290 | /* PC to line number mapping. This is NULL if the values have not |
| 1291 | been read. This is (struct line *) -1 if there was an error |
| 1292 | reading the values. */ |
| 1293 | struct line *lines; |
| 1294 | /* Number of entries in lines. */ |
| 1295 | size_t lines_count; |
| 1296 | /* PC ranges to function. */ |
| 1297 | struct function_addrs *function_addrs; |
| 1298 | size_t function_addrs_count; |
| 1299 | }; |
| 1300 | |
| 1301 | /* An address range for a compilation unit. This maps a PC value to a |
| 1302 | specific compilation unit. Note that we invert the representation |
| 1303 | in DWARF: instead of listing the units and attaching a list of |
| 1304 | ranges, we list the ranges and have each one point to the unit. |
| 1305 | This lets us do a binary search to find the unit. */ |
| 1306 | |
| 1307 | struct unit_addrs |
| 1308 | { |
| 1309 | /* Range is LOW <= PC < HIGH. */ |
| 1310 | uintptr_t low; |
| 1311 | uintptr_t high; |
| 1312 | /* Compilation unit for this address range. */ |
| 1313 | struct unit *u; |
| 1314 | }; |
| 1315 | |
| 1316 | /* A growable vector of compilation unit address ranges. */ |
| 1317 | |
| 1318 | struct unit_addrs_vector |
| 1319 | { |
| 1320 | /* Memory. This is an array of struct unit_addrs. */ |
| 1321 | struct backtrace_vector vec; |
| 1322 | /* Number of address ranges present. */ |
| 1323 | size_t count; |
| 1324 | }; |
| 1325 | |
| 1326 | /* A growable vector of compilation unit pointer. */ |
| 1327 | |
| 1328 | struct unit_vector |
| 1329 | { |
| 1330 | struct backtrace_vector vec; |
| 1331 | size_t count; |
| 1332 | }; |
| 1333 | |
| 1334 | /* The information we need to map a PC to a file and line. */ |
| 1335 | |
| 1336 | struct dwarf_data |
| 1337 | { |
| 1338 | /* The data for the next file we know about. */ |
| 1339 | struct dwarf_data *next; |
| 1340 | /* The data for .gnu_debugaltlink. */ |
| 1341 | struct dwarf_data *altlink; |
| 1342 | /* The base address mapping for this file. */ |
| 1343 | struct libbacktrace_base_address base_address; |
| 1344 | /* A sorted list of address ranges. */ |
| 1345 | struct unit_addrs *addrs; |
| 1346 | /* Number of address ranges in list. */ |
| 1347 | size_t addrs_count; |
| 1348 | /* A sorted list of units. */ |
| 1349 | struct unit **units; |
| 1350 | /* Number of units in the list. */ |
| 1351 | size_t units_count; |
| 1352 | /* The unparsed DWARF debug data. */ |
| 1353 | struct dwarf_sections dwarf_sections; |
| 1354 | /* Whether the data is big-endian or not. */ |
| 1355 | int is_bigendian; |
| 1356 | /* A vector used for function addresses. We keep this here so that |
| 1357 | we can grow the vector as we read more functions. */ |
| 1358 | struct function_vector fvec; |
| 1359 | }; |
| 1360 | |
| 1361 | /* Report an error for a DWARF buffer. */ |
| 1362 | |
| 1363 | static void |
| 1364 | dwarf_buf_error (struct dwarf_buf *buf, const char *msg, int errnum) |
| 1365 | { |
| 1366 | char b[200]; |
| 1367 | |
| 1368 | snprintf (b, sizeof b, "%s in %s at %d", |
| 1369 | msg, buf->name, (int) (buf->buf - buf->start)); |
| 1370 | buf->error_callback (buf->data, b, errnum); |
| 1371 | } |
| 1372 | |
| 1373 | /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on |
| 1374 | error. */ |
| 1375 | |
| 1376 | static int |
| 1377 | require (struct dwarf_buf *buf, size_t count) |
| 1378 | { |
| 1379 | if (buf->left >= count) |
| 1380 | return 1; |
| 1381 | |
| 1382 | if (!buf->reported_underflow) |
| 1383 | { |
| 1384 | dwarf_buf_error (buf, "DWARF underflow", 0); |
| 1385 | buf->reported_underflow = 1; |
| 1386 | } |
| 1387 | |
| 1388 | return 0; |
| 1389 | } |
| 1390 | |
| 1391 | /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on |
| 1392 | error. */ |
| 1393 | |
| 1394 | static int |
| 1395 | advance (struct dwarf_buf *buf, size_t count) |
| 1396 | { |
| 1397 | if (!require (buf, count)) |
| 1398 | return 0; |
| 1399 | buf->buf += count; |
| 1400 | buf->left -= count; |
| 1401 | return 1; |
| 1402 | } |
| 1403 | |
| 1404 | /* Read one zero-terminated string from BUF and advance past the string. */ |
| 1405 | |
| 1406 | static const char * |
| 1407 | read_string (struct dwarf_buf *buf) |
| 1408 | { |
| 1409 | const char *p = (const char *)buf->buf; |
| 1410 | size_t len = strnlen (p, buf->left); |
| 1411 | |
| 1412 | /* - If len == left, we ran out of buffer before finding the zero terminator. |
| 1413 | Generate an error by advancing len + 1. |
| 1414 | - If len < left, advance by len + 1 to skip past the zero terminator. */ |
| 1415 | size_t count = len + 1; |
| 1416 | |
| 1417 | if (!advance (buf, count)) |
| 1418 | return NULL; |
| 1419 | |
| 1420 | return p; |
| 1421 | } |
| 1422 | |
| 1423 | /* Read one byte from BUF and advance 1 byte. */ |
| 1424 | |
| 1425 | static unsigned char |
| 1426 | read_byte (struct dwarf_buf *buf) |
| 1427 | { |
| 1428 | const unsigned char *p = buf->buf; |
| 1429 | |
| 1430 | if (!advance (buf, 1)) |
| 1431 | return 0; |
| 1432 | return p[0]; |
| 1433 | } |
| 1434 | |
| 1435 | /* Read a signed char from BUF and advance 1 byte. */ |
| 1436 | |
| 1437 | static signed char |
| 1438 | read_sbyte (struct dwarf_buf *buf) |
| 1439 | { |
| 1440 | const unsigned char *p = buf->buf; |
| 1441 | |
| 1442 | if (!advance (buf, 1)) |
| 1443 | return 0; |
| 1444 | return (*p ^ 0x80) - 0x80; |
| 1445 | } |
| 1446 | |
| 1447 | /* Read a uint16 from BUF and advance 2 bytes. */ |
| 1448 | |
| 1449 | static uint16_t |
| 1450 | read_uint16 (struct dwarf_buf *buf) |
| 1451 | { |
| 1452 | const unsigned char *p = buf->buf; |
| 1453 | |
| 1454 | if (!advance (buf, 2)) |
| 1455 | return 0; |
| 1456 | if (buf->is_bigendian) |
| 1457 | return ((uint16_t) p[0] << 8) | (uint16_t) p[1]; |
| 1458 | else |
| 1459 | return ((uint16_t) p[1] << 8) | (uint16_t) p[0]; |
| 1460 | } |
| 1461 | |
| 1462 | /* Read a 24 bit value from BUF and advance 3 bytes. */ |
| 1463 | |
| 1464 | static uint32_t |
| 1465 | read_uint24 (struct dwarf_buf *buf) |
| 1466 | { |
| 1467 | const unsigned char *p = buf->buf; |
| 1468 | |
| 1469 | if (!advance (buf, 3)) |
| 1470 | return 0; |
| 1471 | if (buf->is_bigendian) |
| 1472 | return (((uint32_t) p[0] << 16) | ((uint32_t) p[1] << 8) |
| 1473 | | (uint32_t) p[2]); |
| 1474 | else |
| 1475 | return (((uint32_t) p[2] << 16) | ((uint32_t) p[1] << 8) |
| 1476 | | (uint32_t) p[0]); |
| 1477 | } |
| 1478 | |
| 1479 | /* Read a uint32 from BUF and advance 4 bytes. */ |
| 1480 | |
| 1481 | static uint32_t |
| 1482 | read_uint32 (struct dwarf_buf *buf) |
| 1483 | { |
| 1484 | const unsigned char *p = buf->buf; |
| 1485 | |
| 1486 | if (!advance (buf, 4)) |
| 1487 | return 0; |
| 1488 | if (buf->is_bigendian) |
| 1489 | return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16) |
| 1490 | | ((uint32_t) p[2] << 8) | (uint32_t) p[3]); |
| 1491 | else |
| 1492 | return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16) |
| 1493 | | ((uint32_t) p[1] << 8) | (uint32_t) p[0]); |
| 1494 | } |
| 1495 | |
| 1496 | /* Read a uint64 from BUF and advance 8 bytes. */ |
| 1497 | |
| 1498 | static uint64_t |
| 1499 | read_uint64 (struct dwarf_buf *buf) |
| 1500 | { |
| 1501 | const unsigned char *p = buf->buf; |
| 1502 | |
| 1503 | if (!advance (buf, 8)) |
| 1504 | return 0; |
| 1505 | if (buf->is_bigendian) |
| 1506 | return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48) |
| 1507 | | ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32) |
| 1508 | | ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16) |
| 1509 | | ((uint64_t) p[6] << 8) | (uint64_t) p[7]); |
| 1510 | else |
| 1511 | return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48) |
| 1512 | | ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32) |
| 1513 | | ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16) |
| 1514 | | ((uint64_t) p[1] << 8) | (uint64_t) p[0]); |
| 1515 | } |
| 1516 | |
| 1517 | /* Read an offset from BUF and advance the appropriate number of |
| 1518 | bytes. */ |
| 1519 | |
| 1520 | static uint64_t |
| 1521 | read_offset (struct dwarf_buf *buf, int is_dwarf64) |
| 1522 | { |
| 1523 | if (is_dwarf64) |
| 1524 | return read_uint64 (buf); |
| 1525 | else |
| 1526 | return read_uint32 (buf); |
| 1527 | } |
| 1528 | |
| 1529 | /* Read an address from BUF and advance the appropriate number of |
| 1530 | bytes. */ |
| 1531 | |
| 1532 | static uint64_t |
| 1533 | read_address (struct dwarf_buf *buf, int addrsize) |
| 1534 | { |
| 1535 | switch (addrsize) |
| 1536 | { |
| 1537 | case 1: |
| 1538 | return read_byte (buf); |
| 1539 | case 2: |
| 1540 | return read_uint16 (buf); |
| 1541 | case 4: |
| 1542 | return read_uint32 (buf); |
| 1543 | case 8: |
| 1544 | return read_uint64 (buf); |
| 1545 | default: |
| 1546 | dwarf_buf_error (buf, "unrecognized address size", 0); |
| 1547 | return 0; |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | /* Return whether a value is the highest possible address, given the |
| 1552 | address size. */ |
| 1553 | |
| 1554 | static int |
| 1555 | is_highest_address (uint64_t address, int addrsize) |
| 1556 | { |
| 1557 | switch (addrsize) |
| 1558 | { |
| 1559 | case 1: |
| 1560 | return address == (unsigned char) -1; |
| 1561 | case 2: |
| 1562 | return address == (uint16_t) -1; |
| 1563 | case 4: |
| 1564 | return address == (uint32_t) -1; |
| 1565 | case 8: |
| 1566 | return address == (uint64_t) -1; |
| 1567 | default: |
| 1568 | return 0; |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | /* Read an unsigned LEB128 number. */ |
| 1573 | |
| 1574 | static uint64_t |
| 1575 | read_uleb128 (struct dwarf_buf *buf) |
| 1576 | { |
| 1577 | uint64_t ret; |
| 1578 | unsigned int shift; |
| 1579 | int overflow; |
| 1580 | unsigned char b; |
| 1581 | |
| 1582 | ret = 0; |
| 1583 | shift = 0; |
| 1584 | overflow = 0; |
| 1585 | do |
| 1586 | { |
| 1587 | const unsigned char *p; |
| 1588 | |
| 1589 | p = buf->buf; |
| 1590 | if (!advance (buf, 1)) |
| 1591 | return 0; |
| 1592 | b = *p; |
| 1593 | if (shift < 64) |
| 1594 | ret |= ((uint64_t) (b & 0x7f)) << shift; |
| 1595 | else if (!overflow) |
| 1596 | { |
| 1597 | dwarf_buf_error (buf, "LEB128 overflows uint64_t", 0); |
| 1598 | overflow = 1; |
| 1599 | } |
| 1600 | shift += 7; |
| 1601 | } |
| 1602 | while ((b & 0x80) != 0); |
| 1603 | |
| 1604 | return ret; |
| 1605 | } |
| 1606 | |
| 1607 | /* Read a signed LEB128 number. */ |
| 1608 | |
| 1609 | static int64_t |
| 1610 | read_sleb128 (struct dwarf_buf *buf) |
| 1611 | { |
| 1612 | uint64_t val; |
| 1613 | unsigned int shift; |
| 1614 | int overflow; |
| 1615 | unsigned char b; |
| 1616 | |
| 1617 | val = 0; |
| 1618 | shift = 0; |
| 1619 | overflow = 0; |
| 1620 | do |
| 1621 | { |
| 1622 | const unsigned char *p; |
| 1623 | |
| 1624 | p = buf->buf; |
| 1625 | if (!advance (buf, 1)) |
| 1626 | return 0; |
| 1627 | b = *p; |
| 1628 | if (shift < 64) |
| 1629 | val |= ((uint64_t) (b & 0x7f)) << shift; |
| 1630 | else if (!overflow) |
| 1631 | { |
| 1632 | dwarf_buf_error (buf, "signed LEB128 overflows uint64_t", 0); |
| 1633 | overflow = 1; |
| 1634 | } |
| 1635 | shift += 7; |
| 1636 | } |
| 1637 | while ((b & 0x80) != 0); |
| 1638 | |
| 1639 | if ((b & 0x40) != 0 && shift < 64) |
| 1640 | val |= ((uint64_t) -1) << shift; |
| 1641 | |
| 1642 | return (int64_t) val; |
| 1643 | } |
| 1644 | |
| 1645 | /* Return the length of an LEB128 number. */ |
| 1646 | |
| 1647 | static size_t |
| 1648 | leb128_len (const unsigned char *p) |
| 1649 | { |
| 1650 | size_t ret; |
| 1651 | |
| 1652 | ret = 1; |
| 1653 | while ((*p & 0x80) != 0) |
| 1654 | { |
| 1655 | ++p; |
| 1656 | ++ret; |
| 1657 | } |
| 1658 | return ret; |
| 1659 | } |
| 1660 | |
| 1661 | /* Read initial_length from BUF and advance the appropriate number of bytes. */ |
| 1662 | |
| 1663 | static uint64_t |
| 1664 | read_initial_length (struct dwarf_buf *buf, int *is_dwarf64) |
| 1665 | { |
| 1666 | uint64_t len; |
| 1667 | |
| 1668 | len = read_uint32 (buf); |
| 1669 | if (len == 0xffffffff) |
| 1670 | { |
| 1671 | len = read_uint64 (buf); |
| 1672 | *is_dwarf64 = 1; |
| 1673 | } |
| 1674 | else |
| 1675 | *is_dwarf64 = 0; |
| 1676 | |
| 1677 | return len; |
| 1678 | } |
| 1679 | |
| 1680 | /* Free an abbreviations structure. */ |
| 1681 | |
| 1682 | static void |
| 1683 | free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs, |
| 1684 | backtrace_error_callback error_callback, void *data) |
| 1685 | { |
| 1686 | size_t i; |
| 1687 | |
| 1688 | for (i = 0; i < abbrevs->num_abbrevs; ++i) |
| 1689 | backtrace_free (state, abbrevs->abbrevs[i].attrs, |
| 1690 | abbrevs->abbrevs[i].num_attrs * sizeof (struct attr), |
| 1691 | error_callback, data); |
| 1692 | backtrace_free (state, abbrevs->abbrevs, |
| 1693 | abbrevs->num_abbrevs * sizeof (struct abbrev), |
| 1694 | error_callback, data); |
| 1695 | abbrevs->num_abbrevs = 0; |
| 1696 | abbrevs->abbrevs = NULL; |
| 1697 | } |
| 1698 | |
| 1699 | /* Read an attribute value. Returns 1 on success, 0 on failure. If |
| 1700 | the value can be represented as a uint64_t, sets *VAL and sets |
| 1701 | *IS_VALID to 1. We don't try to store the value of other attribute |
| 1702 | forms, because we don't care about them. */ |
| 1703 | |
| 1704 | static int |
| 1705 | read_attribute (enum dwarf_form form, uint64_t implicit_val, |
| 1706 | struct dwarf_buf *buf, int is_dwarf64, int version, |
| 1707 | int addrsize, const struct dwarf_sections *dwarf_sections, |
| 1708 | struct dwarf_data *altlink, struct attr_val *val) |
| 1709 | { |
| 1710 | /* Avoid warnings about val.u.FIELD may be used uninitialized if |
| 1711 | this function is inlined. The warnings aren't valid but can |
| 1712 | occur because the different fields are set and used |
| 1713 | conditionally. */ |
| 1714 | memset (val, 0, sizeof *val); |
| 1715 | |
| 1716 | switch (form) |
| 1717 | { |
| 1718 | case DW_FORM_addr: |
| 1719 | val->encoding = ATTR_VAL_ADDRESS; |
| 1720 | val->u.uint = read_address (buf, addrsize); |
| 1721 | return 1; |
| 1722 | case DW_FORM_block2: |
| 1723 | val->encoding = ATTR_VAL_BLOCK; |
| 1724 | return advance (buf, read_uint16 (buf)); |
| 1725 | case DW_FORM_block4: |
| 1726 | val->encoding = ATTR_VAL_BLOCK; |
| 1727 | return advance (buf, read_uint32 (buf)); |
| 1728 | case DW_FORM_data2: |
| 1729 | val->encoding = ATTR_VAL_UINT; |
| 1730 | val->u.uint = read_uint16 (buf); |
| 1731 | return 1; |
| 1732 | case DW_FORM_data4: |
| 1733 | val->encoding = ATTR_VAL_UINT; |
| 1734 | val->u.uint = read_uint32 (buf); |
| 1735 | return 1; |
| 1736 | case DW_FORM_data8: |
| 1737 | val->encoding = ATTR_VAL_UINT; |
| 1738 | val->u.uint = read_uint64 (buf); |
| 1739 | return 1; |
| 1740 | case DW_FORM_data16: |
| 1741 | val->encoding = ATTR_VAL_BLOCK; |
| 1742 | return advance (buf, 16); |
| 1743 | case DW_FORM_string: |
| 1744 | val->encoding = ATTR_VAL_STRING; |
| 1745 | val->u.string = read_string (buf); |
| 1746 | return val->u.string == NULL ? 0 : 1; |
| 1747 | case DW_FORM_block: |
| 1748 | val->encoding = ATTR_VAL_BLOCK; |
| 1749 | return advance (buf, read_uleb128 (buf)); |
| 1750 | case DW_FORM_block1: |
| 1751 | val->encoding = ATTR_VAL_BLOCK; |
| 1752 | return advance (buf, read_byte (buf)); |
| 1753 | case DW_FORM_data1: |
| 1754 | val->encoding = ATTR_VAL_UINT; |
| 1755 | val->u.uint = read_byte (buf); |
| 1756 | return 1; |
| 1757 | case DW_FORM_flag: |
| 1758 | val->encoding = ATTR_VAL_UINT; |
| 1759 | val->u.uint = read_byte (buf); |
| 1760 | return 1; |
| 1761 | case DW_FORM_sdata: |
| 1762 | val->encoding = ATTR_VAL_SINT; |
| 1763 | val->u.sint = read_sleb128 (buf); |
| 1764 | return 1; |
| 1765 | case DW_FORM_strp: |
| 1766 | { |
| 1767 | uint64_t offset; |
| 1768 | |
| 1769 | offset = read_offset (buf, is_dwarf64); |
| 1770 | if (offset >= dwarf_sections->size[DEBUG_STR]) |
| 1771 | { |
| 1772 | dwarf_buf_error (buf, "DW_FORM_strp out of range", 0); |
| 1773 | return 0; |
| 1774 | } |
| 1775 | val->encoding = ATTR_VAL_STRING; |
| 1776 | val->u.string = |
| 1777 | (const char *) dwarf_sections->data[DEBUG_STR] + offset; |
| 1778 | return 1; |
| 1779 | } |
| 1780 | case DW_FORM_line_strp: |
| 1781 | { |
| 1782 | uint64_t offset; |
| 1783 | |
| 1784 | offset = read_offset (buf, is_dwarf64); |
| 1785 | if (offset >= dwarf_sections->size[DEBUG_LINE_STR]) |
| 1786 | { |
| 1787 | dwarf_buf_error (buf, "DW_FORM_line_strp out of range", 0); |
| 1788 | return 0; |
| 1789 | } |
| 1790 | val->encoding = ATTR_VAL_STRING; |
| 1791 | val->u.string = |
| 1792 | (const char *) dwarf_sections->data[DEBUG_LINE_STR] + offset; |
| 1793 | return 1; |
| 1794 | } |
| 1795 | case DW_FORM_udata: |
| 1796 | val->encoding = ATTR_VAL_UINT; |
| 1797 | val->u.uint = read_uleb128 (buf); |
| 1798 | return 1; |
| 1799 | case DW_FORM_ref_addr: |
| 1800 | val->encoding = ATTR_VAL_REF_INFO; |
| 1801 | if (version == 2) |
| 1802 | val->u.uint = read_address (buf, addrsize); |
| 1803 | else |
| 1804 | val->u.uint = read_offset (buf, is_dwarf64); |
| 1805 | return 1; |
| 1806 | case DW_FORM_ref1: |
| 1807 | val->encoding = ATTR_VAL_REF_UNIT; |
| 1808 | val->u.uint = read_byte (buf); |
| 1809 | return 1; |
| 1810 | case DW_FORM_ref2: |
| 1811 | val->encoding = ATTR_VAL_REF_UNIT; |
| 1812 | val->u.uint = read_uint16 (buf); |
| 1813 | return 1; |
| 1814 | case DW_FORM_ref4: |
| 1815 | val->encoding = ATTR_VAL_REF_UNIT; |
| 1816 | val->u.uint = read_uint32 (buf); |
| 1817 | return 1; |
| 1818 | case DW_FORM_ref8: |
| 1819 | val->encoding = ATTR_VAL_REF_UNIT; |
| 1820 | val->u.uint = read_uint64 (buf); |
| 1821 | return 1; |
| 1822 | case DW_FORM_ref_udata: |
| 1823 | val->encoding = ATTR_VAL_REF_UNIT; |
| 1824 | val->u.uint = read_uleb128 (buf); |
| 1825 | return 1; |
| 1826 | case DW_FORM_indirect: |
| 1827 | { |
| 1828 | uint64_t form; |
| 1829 | |
| 1830 | form = read_uleb128 (buf); |
| 1831 | if (form == DW_FORM_implicit_const) |
| 1832 | { |
| 1833 | dwarf_buf_error (buf, |
| 1834 | "DW_FORM_indirect to DW_FORM_implicit_const", |
| 1835 | 0); |
| 1836 | return 0; |
| 1837 | } |
| 1838 | return read_attribute ((enum dwarf_form) form, 0, buf, is_dwarf64, |
| 1839 | version, addrsize, dwarf_sections, altlink, |
| 1840 | val); |
| 1841 | } |
| 1842 | case DW_FORM_sec_offset: |
| 1843 | val->encoding = ATTR_VAL_REF_SECTION; |
| 1844 | val->u.uint = read_offset (buf, is_dwarf64); |
| 1845 | return 1; |
| 1846 | case DW_FORM_exprloc: |
| 1847 | val->encoding = ATTR_VAL_EXPR; |
| 1848 | return advance (buf, read_uleb128 (buf)); |
| 1849 | case DW_FORM_flag_present: |
| 1850 | val->encoding = ATTR_VAL_UINT; |
| 1851 | val->u.uint = 1; |
| 1852 | return 1; |
| 1853 | case DW_FORM_ref_sig8: |
| 1854 | val->encoding = ATTR_VAL_REF_TYPE; |
| 1855 | val->u.uint = read_uint64 (buf); |
| 1856 | return 1; |
| 1857 | case DW_FORM_strx: case DW_FORM_strx1: case DW_FORM_strx2: |
| 1858 | case DW_FORM_strx3: case DW_FORM_strx4: |
| 1859 | { |
| 1860 | uint64_t offset; |
| 1861 | |
| 1862 | switch (form) |
| 1863 | { |
| 1864 | case DW_FORM_strx: |
| 1865 | offset = read_uleb128 (buf); |
| 1866 | break; |
| 1867 | case DW_FORM_strx1: |
| 1868 | offset = read_byte (buf); |
| 1869 | break; |
| 1870 | case DW_FORM_strx2: |
| 1871 | offset = read_uint16 (buf); |
| 1872 | break; |
| 1873 | case DW_FORM_strx3: |
| 1874 | offset = read_uint24 (buf); |
| 1875 | break; |
| 1876 | case DW_FORM_strx4: |
| 1877 | offset = read_uint32 (buf); |
| 1878 | break; |
| 1879 | default: |
| 1880 | /* This case can't happen. */ |
| 1881 | return 0; |
| 1882 | } |
| 1883 | val->encoding = ATTR_VAL_STRING_INDEX; |
| 1884 | val->u.uint = offset; |
| 1885 | return 1; |
| 1886 | } |
| 1887 | case DW_FORM_addrx: case DW_FORM_addrx1: case DW_FORM_addrx2: |
| 1888 | case DW_FORM_addrx3: case DW_FORM_addrx4: |
| 1889 | { |
| 1890 | uint64_t offset; |
| 1891 | |
| 1892 | switch (form) |
| 1893 | { |
| 1894 | case DW_FORM_addrx: |
| 1895 | offset = read_uleb128 (buf); |
| 1896 | break; |
| 1897 | case DW_FORM_addrx1: |
| 1898 | offset = read_byte (buf); |
| 1899 | break; |
| 1900 | case DW_FORM_addrx2: |
| 1901 | offset = read_uint16 (buf); |
| 1902 | break; |
| 1903 | case DW_FORM_addrx3: |
| 1904 | offset = read_uint24 (buf); |
| 1905 | break; |
| 1906 | case DW_FORM_addrx4: |
| 1907 | offset = read_uint32 (buf); |
| 1908 | break; |
| 1909 | default: |
| 1910 | /* This case can't happen. */ |
| 1911 | return 0; |
| 1912 | } |
| 1913 | val->encoding = ATTR_VAL_ADDRESS_INDEX; |
| 1914 | val->u.uint = offset; |
| 1915 | return 1; |
| 1916 | } |
| 1917 | case DW_FORM_ref_sup4: |
| 1918 | val->encoding = ATTR_VAL_REF_SECTION; |
| 1919 | val->u.uint = read_uint32 (buf); |
| 1920 | return 1; |
| 1921 | case DW_FORM_ref_sup8: |
| 1922 | val->encoding = ATTR_VAL_REF_SECTION; |
| 1923 | val->u.uint = read_uint64 (buf); |
| 1924 | return 1; |
| 1925 | case DW_FORM_implicit_const: |
| 1926 | val->encoding = ATTR_VAL_UINT; |
| 1927 | val->u.uint = implicit_val; |
| 1928 | return 1; |
| 1929 | case DW_FORM_loclistx: |
| 1930 | /* We don't distinguish this from DW_FORM_sec_offset. It |
| 1931 | * shouldn't matter since we don't care about loclists. */ |
| 1932 | val->encoding = ATTR_VAL_REF_SECTION; |
| 1933 | val->u.uint = read_uleb128 (buf); |
| 1934 | return 1; |
| 1935 | case DW_FORM_rnglistx: |
| 1936 | val->encoding = ATTR_VAL_RNGLISTS_INDEX; |
| 1937 | val->u.uint = read_uleb128 (buf); |
| 1938 | return 1; |
| 1939 | case DW_FORM_GNU_addr_index: |
| 1940 | val->encoding = ATTR_VAL_REF_SECTION; |
| 1941 | val->u.uint = read_uleb128 (buf); |
| 1942 | return 1; |
| 1943 | case DW_FORM_GNU_str_index: |
| 1944 | val->encoding = ATTR_VAL_REF_SECTION; |
| 1945 | val->u.uint = read_uleb128 (buf); |
| 1946 | return 1; |
| 1947 | case DW_FORM_GNU_ref_alt: |
| 1948 | val->u.uint = read_offset (buf, is_dwarf64); |
| 1949 | if (altlink == NULL) |
| 1950 | { |
| 1951 | val->encoding = ATTR_VAL_NONE; |
| 1952 | return 1; |
| 1953 | } |
| 1954 | val->encoding = ATTR_VAL_REF_ALT_INFO; |
| 1955 | return 1; |
| 1956 | case DW_FORM_strp_sup: case DW_FORM_GNU_strp_alt: |
| 1957 | { |
| 1958 | uint64_t offset; |
| 1959 | |
| 1960 | offset = read_offset (buf, is_dwarf64); |
| 1961 | if (altlink == NULL) |
| 1962 | { |
| 1963 | val->encoding = ATTR_VAL_NONE; |
| 1964 | return 1; |
| 1965 | } |
| 1966 | if (offset >= altlink->dwarf_sections.size[DEBUG_STR]) |
| 1967 | { |
| 1968 | dwarf_buf_error (buf, "DW_FORM_strp_sup out of range", 0); |
| 1969 | return 0; |
| 1970 | } |
| 1971 | val->encoding = ATTR_VAL_STRING; |
| 1972 | val->u.string = |
| 1973 | (const char *) altlink->dwarf_sections.data[DEBUG_STR] + offset; |
| 1974 | return 1; |
| 1975 | } |
| 1976 | default: |
| 1977 | dwarf_buf_error (buf, "unrecognized DWARF form", -1); |
| 1978 | return 0; |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | /* If we can determine the value of a string attribute, set *STRING to |
| 1983 | point to the string. Return 1 on success, 0 on error. If we don't |
| 1984 | know the value, we consider that a success, and we don't change |
| 1985 | *STRING. An error is only reported for some sort of out of range |
| 1986 | offset. */ |
| 1987 | |
| 1988 | static int |
| 1989 | resolve_string (const struct dwarf_sections *dwarf_sections, int is_dwarf64, |
| 1990 | int is_bigendian, uint64_t str_offsets_base, |
| 1991 | const struct attr_val *val, |
| 1992 | backtrace_error_callback error_callback, void *data, |
| 1993 | const char **string) |
| 1994 | { |
| 1995 | switch (val->encoding) |
| 1996 | { |
| 1997 | case ATTR_VAL_STRING: |
| 1998 | *string = val->u.string; |
| 1999 | return 1; |
| 2000 | |
| 2001 | case ATTR_VAL_STRING_INDEX: |
| 2002 | { |
| 2003 | uint64_t offset; |
| 2004 | struct dwarf_buf offset_buf; |
| 2005 | |
| 2006 | offset = val->u.uint * (is_dwarf64 ? 8 : 4) + str_offsets_base; |
| 2007 | if (offset + (is_dwarf64 ? 8 : 4) |
| 2008 | > dwarf_sections->size[DEBUG_STR_OFFSETS]) |
| 2009 | { |
| 2010 | error_callback (data, "DW_FORM_strx value out of range", 0); |
| 2011 | return 0; |
| 2012 | } |
| 2013 | |
| 2014 | offset_buf.name = ".debug_str_offsets"; |
| 2015 | offset_buf.start = dwarf_sections->data[DEBUG_STR_OFFSETS]; |
| 2016 | offset_buf.buf = dwarf_sections->data[DEBUG_STR_OFFSETS] + offset; |
| 2017 | offset_buf.left = dwarf_sections->size[DEBUG_STR_OFFSETS] - offset; |
| 2018 | offset_buf.is_bigendian = is_bigendian; |
| 2019 | offset_buf.error_callback = error_callback; |
| 2020 | offset_buf.data = data; |
| 2021 | offset_buf.reported_underflow = 0; |
| 2022 | |
| 2023 | offset = read_offset (&offset_buf, is_dwarf64); |
| 2024 | if (offset >= dwarf_sections->size[DEBUG_STR]) |
| 2025 | { |
| 2026 | dwarf_buf_error (&offset_buf, |
| 2027 | "DW_FORM_strx offset out of range", |
| 2028 | 0); |
| 2029 | return 0; |
| 2030 | } |
| 2031 | *string = (const char *) dwarf_sections->data[DEBUG_STR] + offset; |
| 2032 | return 1; |
| 2033 | } |
| 2034 | |
| 2035 | default: |
| 2036 | return 1; |
| 2037 | } |
| 2038 | } |
| 2039 | |
| 2040 | /* Set *ADDRESS to the real address for a ATTR_VAL_ADDRESS_INDEX. |
| 2041 | Return 1 on success, 0 on error. */ |
| 2042 | |
| 2043 | static int |
| 2044 | resolve_addr_index (const struct dwarf_sections *dwarf_sections, |
| 2045 | uint64_t addr_base, int addrsize, int is_bigendian, |
| 2046 | uint64_t addr_index, |
| 2047 | backtrace_error_callback error_callback, void *data, |
| 2048 | uintptr_t *address) |
| 2049 | { |
| 2050 | uint64_t offset; |
| 2051 | struct dwarf_buf addr_buf; |
| 2052 | |
| 2053 | offset = addr_index * addrsize + addr_base; |
| 2054 | if (offset + addrsize > dwarf_sections->size[DEBUG_ADDR]) |
| 2055 | { |
| 2056 | error_callback (data, "DW_FORM_addrx value out of range", 0); |
| 2057 | return 0; |
| 2058 | } |
| 2059 | |
| 2060 | addr_buf.name = ".debug_addr"; |
| 2061 | addr_buf.start = dwarf_sections->data[DEBUG_ADDR]; |
| 2062 | addr_buf.buf = dwarf_sections->data[DEBUG_ADDR] + offset; |
| 2063 | addr_buf.left = dwarf_sections->size[DEBUG_ADDR] - offset; |
| 2064 | addr_buf.is_bigendian = is_bigendian; |
| 2065 | addr_buf.error_callback = error_callback; |
| 2066 | addr_buf.data = data; |
| 2067 | addr_buf.reported_underflow = 0; |
| 2068 | |
| 2069 | *address = (uintptr_t) read_address (&addr_buf, addrsize); |
| 2070 | return 1; |
| 2071 | } |
| 2072 | |
| 2073 | /* Compare a unit offset against a unit for bsearch. */ |
| 2074 | |
| 2075 | static int |
| 2076 | units_search (const void *vkey, const void *ventry) |
| 2077 | { |
| 2078 | const size_t *key = (const size_t *) vkey; |
| 2079 | const struct unit *entry = *((const struct unit *const *) ventry); |
| 2080 | size_t offset; |
| 2081 | |
| 2082 | offset = *key; |
| 2083 | if (offset < entry->low_offset) |
| 2084 | return -1; |
| 2085 | else if (offset >= entry->high_offset) |
| 2086 | return 1; |
| 2087 | else |
| 2088 | return 0; |
| 2089 | } |
| 2090 | |
| 2091 | /* Find a unit in PU containing OFFSET. */ |
| 2092 | |
| 2093 | static struct unit * |
| 2094 | find_unit (struct unit **pu, size_t units_count, size_t offset) |
| 2095 | { |
| 2096 | struct unit **u; |
| 2097 | u = bsearch (&offset, pu, units_count, sizeof (struct unit *), units_search); |
| 2098 | return u == NULL ? NULL : *u; |
| 2099 | } |
| 2100 | |
| 2101 | /* Compare function_addrs for qsort. When ranges are nested, make the |
| 2102 | smallest one sort last. */ |
| 2103 | |
| 2104 | static int |
| 2105 | function_addrs_compare (const void *v1, const void *v2) |
| 2106 | { |
| 2107 | const struct function_addrs *a1 = (const struct function_addrs *) v1; |
| 2108 | const struct function_addrs *a2 = (const struct function_addrs *) v2; |
| 2109 | |
| 2110 | if (a1->low < a2->low) |
| 2111 | return -1; |
| 2112 | if (a1->low > a2->low) |
| 2113 | return 1; |
| 2114 | if (a1->high < a2->high) |
| 2115 | return 1; |
| 2116 | if (a1->high > a2->high) |
| 2117 | return -1; |
| 2118 | return strcmp (a1->function->name, a2->function->name); |
| 2119 | } |
| 2120 | |
| 2121 | /* Compare a PC against a function_addrs for bsearch. We always |
| 2122 | allocate an entra entry at the end of the vector, so that this |
| 2123 | routine can safely look at the next entry. Note that if there are |
| 2124 | multiple ranges containing PC, which one will be returned is |
| 2125 | unpredictable. We compensate for that in dwarf_fileline. */ |
| 2126 | |
| 2127 | static int |
| 2128 | function_addrs_search (const void *vkey, const void *ventry) |
| 2129 | { |
| 2130 | const uintptr_t *key = (const uintptr_t *) vkey; |
| 2131 | const struct function_addrs *entry = (const struct function_addrs *) ventry; |
| 2132 | uintptr_t pc; |
| 2133 | |
| 2134 | pc = *key; |
| 2135 | if (pc < entry->low) |
| 2136 | return -1; |
| 2137 | else if (pc > (entry + 1)->low) |
| 2138 | return 1; |
| 2139 | else |
| 2140 | return 0; |
| 2141 | } |
| 2142 | |
| 2143 | /* Add a new compilation unit address range to a vector. This is |
| 2144 | called via add_ranges. Returns 1 on success, 0 on failure. */ |
| 2145 | |
| 2146 | static int |
| 2147 | add_unit_addr (struct backtrace_state *state, void *rdata, |
| 2148 | uintptr_t lowpc, uintptr_t highpc, |
| 2149 | backtrace_error_callback error_callback, void *data, |
| 2150 | void *pvec) |
| 2151 | { |
| 2152 | struct unit *u = (struct unit *) rdata; |
| 2153 | struct unit_addrs_vector *vec = (struct unit_addrs_vector *) pvec; |
| 2154 | struct unit_addrs *p; |
| 2155 | |
| 2156 | /* Try to merge with the last entry. */ |
| 2157 | if (vec->count > 0) |
| 2158 | { |
| 2159 | p = (struct unit_addrs *) vec->vec.base + (vec->count - 1); |
| 2160 | if ((lowpc == p->high || lowpc == p->high + 1) |
| 2161 | && u == p->u) |
| 2162 | { |
| 2163 | if (highpc > p->high) |
| 2164 | p->high = highpc; |
| 2165 | return 1; |
| 2166 | } |
| 2167 | } |
| 2168 | |
| 2169 | p = ((struct unit_addrs *) |
| 2170 | backtrace_vector_grow (state, sizeof (struct unit_addrs), |
| 2171 | error_callback, data, &vec->vec)); |
| 2172 | if (p == NULL) |
| 2173 | return 0; |
| 2174 | |
| 2175 | p->low = lowpc; |
| 2176 | p->high = highpc; |
| 2177 | p->u = u; |
| 2178 | |
| 2179 | ++vec->count; |
| 2180 | |
| 2181 | return 1; |
| 2182 | } |
| 2183 | |
| 2184 | /* Compare unit_addrs for qsort. When ranges are nested, make the |
| 2185 | smallest one sort last. */ |
| 2186 | |
| 2187 | static int |
| 2188 | unit_addrs_compare (const void *v1, const void *v2) |
| 2189 | { |
| 2190 | const struct unit_addrs *a1 = (const struct unit_addrs *) v1; |
| 2191 | const struct unit_addrs *a2 = (const struct unit_addrs *) v2; |
| 2192 | |
| 2193 | if (a1->low < a2->low) |
| 2194 | return -1; |
| 2195 | if (a1->low > a2->low) |
| 2196 | return 1; |
| 2197 | if (a1->high < a2->high) |
| 2198 | return 1; |
| 2199 | if (a1->high > a2->high) |
| 2200 | return -1; |
| 2201 | if (a1->u->lineoff < a2->u->lineoff) |
| 2202 | return -1; |
| 2203 | if (a1->u->lineoff > a2->u->lineoff) |
| 2204 | return 1; |
| 2205 | return 0; |
| 2206 | } |
| 2207 | |
| 2208 | /* Compare a PC against a unit_addrs for bsearch. We always allocate |
| 2209 | an entry entry at the end of the vector, so that this routine can |
| 2210 | safely look at the next entry. Note that if there are multiple |
| 2211 | ranges containing PC, which one will be returned is unpredictable. |
| 2212 | We compensate for that in dwarf_fileline. */ |
| 2213 | |
| 2214 | static int |
| 2215 | unit_addrs_search (const void *vkey, const void *ventry) |
| 2216 | { |
| 2217 | const uintptr_t *key = (const uintptr_t *) vkey; |
| 2218 | const struct unit_addrs *entry = (const struct unit_addrs *) ventry; |
| 2219 | uintptr_t pc; |
| 2220 | |
| 2221 | pc = *key; |
| 2222 | if (pc < entry->low) |
| 2223 | return -1; |
| 2224 | else if (pc > (entry + 1)->low) |
| 2225 | return 1; |
| 2226 | else |
| 2227 | return 0; |
| 2228 | } |
| 2229 | |
| 2230 | /* Fill in overlapping ranges as needed. This is a subroutine of |
| 2231 | resolve_unit_addrs_overlap. */ |
| 2232 | |
| 2233 | static int |
| 2234 | resolve_unit_addrs_overlap_walk (struct backtrace_state *state, |
| 2235 | size_t *pfrom, size_t *pto, |
| 2236 | struct unit_addrs *enclosing, |
| 2237 | struct unit_addrs_vector *old_vec, |
| 2238 | backtrace_error_callback error_callback, |
| 2239 | void *data, |
| 2240 | struct unit_addrs_vector *new_vec) |
| 2241 | { |
| 2242 | struct unit_addrs *old_addrs; |
| 2243 | size_t old_count; |
| 2244 | struct unit_addrs *new_addrs; |
| 2245 | size_t from; |
| 2246 | size_t to; |
| 2247 | |
| 2248 | old_addrs = (struct unit_addrs *) old_vec->vec.base; |
| 2249 | old_count = old_vec->count; |
| 2250 | new_addrs = (struct unit_addrs *) new_vec->vec.base; |
| 2251 | |
| 2252 | for (from = *pfrom, to = *pto; from < old_count; from++, to++) |
| 2253 | { |
| 2254 | /* If we are in the scope of a larger range that can no longer |
| 2255 | cover any further ranges, return back to the caller. */ |
| 2256 | |
| 2257 | if (enclosing != NULL |
| 2258 | && enclosing->high <= old_addrs[from].low) |
| 2259 | { |
| 2260 | *pfrom = from; |
| 2261 | *pto = to; |
| 2262 | return 1; |
| 2263 | } |
| 2264 | |
| 2265 | new_addrs[to] = old_addrs[from]; |
| 2266 | |
| 2267 | /* If we are in scope of a larger range, fill in any gaps |
| 2268 | between this entry and the next one. |
| 2269 | |
| 2270 | There is an extra entry at the end of the vector, so it's |
| 2271 | always OK to refer to from + 1. */ |
| 2272 | |
| 2273 | if (enclosing != NULL |
| 2274 | && enclosing->high > old_addrs[from].high |
| 2275 | && old_addrs[from].high < old_addrs[from + 1].low) |
| 2276 | { |
| 2277 | void *grew; |
| 2278 | size_t new_high; |
| 2279 | |
| 2280 | grew = backtrace_vector_grow (state, sizeof (struct unit_addrs), |
| 2281 | error_callback, data, &new_vec->vec); |
| 2282 | if (grew == NULL) |
| 2283 | return 0; |
| 2284 | new_addrs = (struct unit_addrs *) new_vec->vec.base; |
| 2285 | to++; |
| 2286 | new_addrs[to].low = old_addrs[from].high; |
| 2287 | new_high = old_addrs[from + 1].low; |
| 2288 | if (enclosing->high < new_high) |
| 2289 | new_high = enclosing->high; |
| 2290 | new_addrs[to].high = new_high; |
| 2291 | new_addrs[to].u = enclosing->u; |
| 2292 | } |
| 2293 | |
| 2294 | /* If this range has a larger scope than the next one, use it to |
| 2295 | fill in any gaps. */ |
| 2296 | |
| 2297 | if (old_addrs[from].high > old_addrs[from + 1].high) |
| 2298 | { |
| 2299 | *pfrom = from + 1; |
| 2300 | *pto = to + 1; |
| 2301 | if (!resolve_unit_addrs_overlap_walk (state, pfrom, pto, |
| 2302 | &old_addrs[from], old_vec, |
| 2303 | error_callback, data, new_vec)) |
| 2304 | return 0; |
| 2305 | from = *pfrom; |
| 2306 | to = *pto; |
| 2307 | |
| 2308 | /* Undo the increment the loop is about to do. */ |
| 2309 | from--; |
| 2310 | to--; |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | if (enclosing == NULL) |
| 2315 | { |
| 2316 | struct unit_addrs *pa; |
| 2317 | |
| 2318 | /* Add trailing entry. */ |
| 2319 | |
| 2320 | pa = ((struct unit_addrs *) |
| 2321 | backtrace_vector_grow (state, sizeof (struct unit_addrs), |
| 2322 | error_callback, data, &new_vec->vec)); |
| 2323 | if (pa == NULL) |
| 2324 | return 0; |
| 2325 | pa->low = 0; |
| 2326 | --pa->low; |
| 2327 | pa->high = pa->low; |
| 2328 | pa->u = NULL; |
| 2329 | |
| 2330 | new_vec->count = to; |
| 2331 | } |
| 2332 | |
| 2333 | return 1; |
| 2334 | } |
| 2335 | |
| 2336 | /* It is possible for the unit_addrs list to contain overlaps, as in |
| 2337 | |
| 2338 | 10: low == 10, high == 20, unit 1 |
| 2339 | 11: low == 12, high == 15, unit 2 |
| 2340 | 12: low == 20, high == 30, unit 1 |
| 2341 | |
| 2342 | In such a case, for pc == 17, a search using units_addr_search will |
| 2343 | return entry 11. However, pc == 17 doesn't fit in that range. We |
| 2344 | actually want range 10. |
| 2345 | |
| 2346 | It seems that in general we might have an arbitrary number of |
| 2347 | ranges in between 10 and 12. |
| 2348 | |
| 2349 | To handle this we look for cases where range R1 is followed by |
| 2350 | range R2 such that R2 is a strict subset of R1. In such cases we |
| 2351 | insert a new range R3 following R2 that fills in the remainder of |
| 2352 | the address space covered by R1. That lets a relatively simple |
| 2353 | search find the correct range. |
| 2354 | |
| 2355 | These overlaps can occur because of the range merging we do in |
| 2356 | add_unit_addr. When the linker de-duplicates functions, it can |
| 2357 | leave behind an address range that refers to the address range of |
| 2358 | the retained duplicate. If the retained duplicate address range is |
| 2359 | merged with others, then after sorting we can see overlapping |
| 2360 | address ranges. |
| 2361 | |
| 2362 | See https://github.com/ianlancetaylor/libbacktrace/issues/137. */ |
| 2363 | |
| 2364 | static int |
| 2365 | resolve_unit_addrs_overlap (struct backtrace_state *state, |
| 2366 | backtrace_error_callback error_callback, |
| 2367 | void *data, struct unit_addrs_vector *addrs_vec) |
| 2368 | { |
| 2369 | struct unit_addrs *addrs; |
| 2370 | size_t count; |
| 2371 | int found; |
| 2372 | struct unit_addrs *entry; |
| 2373 | size_t i; |
| 2374 | struct unit_addrs_vector new_vec; |
| 2375 | void *grew; |
| 2376 | size_t from; |
| 2377 | size_t to; |
| 2378 | |
| 2379 | addrs = (struct unit_addrs *) addrs_vec->vec.base; |
| 2380 | count = addrs_vec->count; |
| 2381 | |
| 2382 | if (count == 0) |
| 2383 | return 1; |
| 2384 | |
| 2385 | /* Optimistically assume that overlaps are rare. */ |
| 2386 | found = 0; |
| 2387 | entry = addrs; |
| 2388 | for (i = 0; i < count - 1; i++) |
| 2389 | { |
| 2390 | if (entry->low < (entry + 1)->low |
| 2391 | && entry->high > (entry + 1)->high) |
| 2392 | { |
| 2393 | found = 1; |
| 2394 | break; |
| 2395 | } |
| 2396 | entry++; |
| 2397 | } |
| 2398 | if (!found) |
| 2399 | return 1; |
| 2400 | |
| 2401 | memset (&new_vec, 0, sizeof new_vec); |
| 2402 | grew = backtrace_vector_grow (state, |
| 2403 | count * sizeof (struct unit_addrs), |
| 2404 | error_callback, data, &new_vec.vec); |
| 2405 | if (grew == NULL) |
| 2406 | return 0; |
| 2407 | |
| 2408 | from = 0; |
| 2409 | to = 0; |
| 2410 | resolve_unit_addrs_overlap_walk (state, &from, &to, NULL, addrs_vec, |
| 2411 | error_callback, data, &new_vec); |
| 2412 | backtrace_vector_free (state, &addrs_vec->vec, error_callback, data); |
| 2413 | *addrs_vec = new_vec; |
| 2414 | |
| 2415 | return 1; |
| 2416 | } |
| 2417 | |
| 2418 | /* Sort the line vector by PC. We want a stable sort here to maintain |
| 2419 | the order of lines for the same PC values. Since the sequence is |
| 2420 | being sorted in place, their addresses cannot be relied on to |
| 2421 | maintain stability. That is the purpose of the index member. */ |
| 2422 | |
| 2423 | static int |
| 2424 | line_compare (const void *v1, const void *v2) |
| 2425 | { |
| 2426 | const struct line *ln1 = (const struct line *) v1; |
| 2427 | const struct line *ln2 = (const struct line *) v2; |
| 2428 | |
| 2429 | if (ln1->pc < ln2->pc) |
| 2430 | return -1; |
| 2431 | else if (ln1->pc > ln2->pc) |
| 2432 | return 1; |
| 2433 | else if (ln1->idx < ln2->idx) |
| 2434 | return -1; |
| 2435 | else if (ln1->idx > ln2->idx) |
| 2436 | return 1; |
| 2437 | else |
| 2438 | return 0; |
| 2439 | } |
| 2440 | |
| 2441 | /* Find a PC in a line vector. We always allocate an extra entry at |
| 2442 | the end of the lines vector, so that this routine can safely look |
| 2443 | at the next entry. Note that when there are multiple mappings for |
| 2444 | the same PC value, this will return the last one. */ |
| 2445 | |
| 2446 | static int |
| 2447 | line_search (const void *vkey, const void *ventry) |
| 2448 | { |
| 2449 | const uintptr_t *key = (const uintptr_t *) vkey; |
| 2450 | const struct line *entry = (const struct line *) ventry; |
| 2451 | uintptr_t pc; |
| 2452 | |
| 2453 | pc = *key; |
| 2454 | if (pc < entry->pc) |
| 2455 | return -1; |
| 2456 | else if (pc >= (entry + 1)->pc) |
| 2457 | return 1; |
| 2458 | else |
| 2459 | return 0; |
| 2460 | } |
| 2461 | |
| 2462 | /* Sort the abbrevs by the abbrev code. This function is passed to |
| 2463 | both qsort and bsearch. */ |
| 2464 | |
| 2465 | static int |
| 2466 | abbrev_compare (const void *v1, const void *v2) |
| 2467 | { |
| 2468 | const struct abbrev *a1 = (const struct abbrev *) v1; |
| 2469 | const struct abbrev *a2 = (const struct abbrev *) v2; |
| 2470 | |
| 2471 | if (a1->code < a2->code) |
| 2472 | return -1; |
| 2473 | else if (a1->code > a2->code) |
| 2474 | return 1; |
| 2475 | else |
| 2476 | { |
| 2477 | /* This really shouldn't happen. It means there are two |
| 2478 | different abbrevs with the same code, and that means we don't |
| 2479 | know which one lookup_abbrev should return. */ |
| 2480 | return 0; |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | /* Read the abbreviation table for a compilation unit. Returns 1 on |
| 2485 | success, 0 on failure. */ |
| 2486 | |
| 2487 | static int |
| 2488 | read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset, |
| 2489 | const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size, |
| 2490 | int is_bigendian, backtrace_error_callback error_callback, |
| 2491 | void *data, struct abbrevs *abbrevs) |
| 2492 | { |
| 2493 | struct dwarf_buf abbrev_buf; |
| 2494 | struct dwarf_buf count_buf; |
| 2495 | size_t num_abbrevs; |
| 2496 | |
| 2497 | abbrevs->num_abbrevs = 0; |
| 2498 | abbrevs->abbrevs = NULL; |
| 2499 | |
| 2500 | if (abbrev_offset >= dwarf_abbrev_size) |
| 2501 | { |
| 2502 | error_callback (data, "abbrev offset out of range", 0); |
| 2503 | return 0; |
| 2504 | } |
| 2505 | |
| 2506 | abbrev_buf.name = ".debug_abbrev"; |
| 2507 | abbrev_buf.start = dwarf_abbrev; |
| 2508 | abbrev_buf.buf = dwarf_abbrev + abbrev_offset; |
| 2509 | abbrev_buf.left = dwarf_abbrev_size - abbrev_offset; |
| 2510 | abbrev_buf.is_bigendian = is_bigendian; |
| 2511 | abbrev_buf.error_callback = error_callback; |
| 2512 | abbrev_buf.data = data; |
| 2513 | abbrev_buf.reported_underflow = 0; |
| 2514 | |
| 2515 | /* Count the number of abbrevs in this list. */ |
| 2516 | |
| 2517 | count_buf = abbrev_buf; |
| 2518 | num_abbrevs = 0; |
| 2519 | while (read_uleb128 (&count_buf) != 0) |
| 2520 | { |
| 2521 | if (count_buf.reported_underflow) |
| 2522 | return 0; |
| 2523 | ++num_abbrevs; |
| 2524 | // Skip tag. |
| 2525 | read_uleb128 (&count_buf); |
| 2526 | // Skip has_children. |
| 2527 | read_byte (&count_buf); |
| 2528 | // Skip attributes. |
| 2529 | while (read_uleb128 (&count_buf) != 0) |
| 2530 | { |
| 2531 | uint64_t form; |
| 2532 | |
| 2533 | form = read_uleb128 (&count_buf); |
| 2534 | if ((enum dwarf_form) form == DW_FORM_implicit_const) |
| 2535 | read_sleb128 (&count_buf); |
| 2536 | } |
| 2537 | // Skip form of last attribute. |
| 2538 | read_uleb128 (&count_buf); |
| 2539 | } |
| 2540 | |
| 2541 | if (count_buf.reported_underflow) |
| 2542 | return 0; |
| 2543 | |
| 2544 | if (num_abbrevs == 0) |
| 2545 | return 1; |
| 2546 | |
| 2547 | abbrevs->abbrevs = ((struct abbrev *) |
| 2548 | backtrace_alloc (state, |
| 2549 | num_abbrevs * sizeof (struct abbrev), |
| 2550 | error_callback, data)); |
| 2551 | if (abbrevs->abbrevs == NULL) |
| 2552 | return 0; |
| 2553 | abbrevs->num_abbrevs = num_abbrevs; |
| 2554 | memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev)); |
| 2555 | |
| 2556 | num_abbrevs = 0; |
| 2557 | while (1) |
| 2558 | { |
| 2559 | uint64_t code; |
| 2560 | struct abbrev a; |
| 2561 | size_t num_attrs; |
| 2562 | struct attr *attrs; |
| 2563 | |
| 2564 | if (abbrev_buf.reported_underflow) |
| 2565 | goto fail; |
| 2566 | |
| 2567 | code = read_uleb128 (&abbrev_buf); |
| 2568 | if (code == 0) |
| 2569 | break; |
| 2570 | |
| 2571 | a.code = code; |
| 2572 | a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf); |
| 2573 | a.has_children = read_byte (&abbrev_buf); |
| 2574 | |
| 2575 | count_buf = abbrev_buf; |
| 2576 | num_attrs = 0; |
| 2577 | while (read_uleb128 (&count_buf) != 0) |
| 2578 | { |
| 2579 | uint64_t form; |
| 2580 | |
| 2581 | ++num_attrs; |
| 2582 | form = read_uleb128 (&count_buf); |
| 2583 | if ((enum dwarf_form) form == DW_FORM_implicit_const) |
| 2584 | read_sleb128 (&count_buf); |
| 2585 | } |
| 2586 | |
| 2587 | if (num_attrs == 0) |
| 2588 | { |
| 2589 | attrs = NULL; |
| 2590 | read_uleb128 (&abbrev_buf); |
| 2591 | read_uleb128 (&abbrev_buf); |
| 2592 | } |
| 2593 | else |
| 2594 | { |
| 2595 | attrs = ((struct attr *) |
| 2596 | backtrace_alloc (state, num_attrs * sizeof *attrs, |
| 2597 | error_callback, data)); |
| 2598 | if (attrs == NULL) |
| 2599 | goto fail; |
| 2600 | num_attrs = 0; |
| 2601 | while (1) |
| 2602 | { |
| 2603 | uint64_t name; |
| 2604 | uint64_t form; |
| 2605 | |
| 2606 | name = read_uleb128 (&abbrev_buf); |
| 2607 | form = read_uleb128 (&abbrev_buf); |
| 2608 | if (name == 0) |
| 2609 | break; |
| 2610 | attrs[num_attrs].name = (enum dwarf_attribute) name; |
| 2611 | attrs[num_attrs].form = (enum dwarf_form) form; |
| 2612 | if ((enum dwarf_form) form == DW_FORM_implicit_const) |
| 2613 | attrs[num_attrs].val = read_sleb128 (&abbrev_buf); |
| 2614 | else |
| 2615 | attrs[num_attrs].val = 0; |
| 2616 | ++num_attrs; |
| 2617 | } |
| 2618 | } |
| 2619 | |
| 2620 | a.num_attrs = num_attrs; |
| 2621 | a.attrs = attrs; |
| 2622 | |
| 2623 | abbrevs->abbrevs[num_abbrevs] = a; |
| 2624 | ++num_abbrevs; |
| 2625 | } |
| 2626 | |
| 2627 | backtrace_qsort (abbrevs->abbrevs, abbrevs->num_abbrevs, |
| 2628 | sizeof (struct abbrev), abbrev_compare); |
| 2629 | |
| 2630 | return 1; |
| 2631 | |
| 2632 | fail: |
| 2633 | free_abbrevs (state, abbrevs, error_callback, data); |
| 2634 | return 0; |
| 2635 | } |
| 2636 | |
| 2637 | /* Return the abbrev information for an abbrev code. */ |
| 2638 | |
| 2639 | static const struct abbrev * |
| 2640 | lookup_abbrev (struct abbrevs *abbrevs, uint64_t code, |
| 2641 | backtrace_error_callback error_callback, void *data) |
| 2642 | { |
| 2643 | struct abbrev key; |
| 2644 | void *p; |
| 2645 | |
| 2646 | /* With GCC, where abbrevs are simply numbered in order, we should |
| 2647 | be able to just look up the entry. */ |
| 2648 | if (code - 1 < abbrevs->num_abbrevs |
| 2649 | && abbrevs->abbrevs[code - 1].code == code) |
| 2650 | return &abbrevs->abbrevs[code - 1]; |
| 2651 | |
| 2652 | /* Otherwise we have to search. */ |
| 2653 | memset (&key, 0, sizeof key); |
| 2654 | key.code = code; |
| 2655 | p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs, |
| 2656 | sizeof (struct abbrev), abbrev_compare); |
| 2657 | if (p == NULL) |
| 2658 | { |
| 2659 | error_callback (data, "invalid abbreviation code", 0); |
| 2660 | return NULL; |
| 2661 | } |
| 2662 | return (const struct abbrev *) p; |
| 2663 | } |
| 2664 | |
| 2665 | /* This struct is used to gather address range information while |
| 2666 | reading attributes. We use this while building a mapping from |
| 2667 | address ranges to compilation units and then again while mapping |
| 2668 | from address ranges to function entries. Normally either |
| 2669 | lowpc/highpc is set or ranges is set. */ |
| 2670 | |
| 2671 | struct pcrange { |
| 2672 | uintptr_t lowpc; /* The low PC value. */ |
| 2673 | int have_lowpc; /* Whether a low PC value was found. */ |
| 2674 | int lowpc_is_addr_index; /* Whether lowpc is in .debug_addr. */ |
| 2675 | uintptr_t highpc; /* The high PC value. */ |
| 2676 | int have_highpc; /* Whether a high PC value was found. */ |
| 2677 | int highpc_is_relative; /* Whether highpc is relative to lowpc. */ |
| 2678 | int highpc_is_addr_index; /* Whether highpc is in .debug_addr. */ |
| 2679 | uint64_t ranges; /* Offset in ranges section. */ |
| 2680 | int have_ranges; /* Whether ranges is valid. */ |
| 2681 | int ranges_is_index; /* Whether ranges is DW_FORM_rnglistx. */ |
| 2682 | }; |
| 2683 | |
| 2684 | /* Update PCRANGE from an attribute value. */ |
| 2685 | |
| 2686 | static void |
| 2687 | update_pcrange (const struct attr* attr, const struct attr_val* val, |
| 2688 | struct pcrange *pcrange) |
| 2689 | { |
| 2690 | switch (attr->name) |
| 2691 | { |
| 2692 | case DW_AT_low_pc: |
| 2693 | if (val->encoding == ATTR_VAL_ADDRESS) |
| 2694 | { |
| 2695 | pcrange->lowpc = (uintptr_t) val->u.uint; |
| 2696 | pcrange->have_lowpc = 1; |
| 2697 | } |
| 2698 | else if (val->encoding == ATTR_VAL_ADDRESS_INDEX) |
| 2699 | { |
| 2700 | pcrange->lowpc = (uintptr_t) val->u.uint; |
| 2701 | pcrange->have_lowpc = 1; |
| 2702 | pcrange->lowpc_is_addr_index = 1; |
| 2703 | } |
| 2704 | break; |
| 2705 | |
| 2706 | case DW_AT_high_pc: |
| 2707 | if (val->encoding == ATTR_VAL_ADDRESS) |
| 2708 | { |
| 2709 | pcrange->highpc = (uintptr_t) val->u.uint; |
| 2710 | pcrange->have_highpc = 1; |
| 2711 | } |
| 2712 | else if (val->encoding == ATTR_VAL_UINT) |
| 2713 | { |
| 2714 | pcrange->highpc = (uintptr_t) val->u.uint; |
| 2715 | pcrange->have_highpc = 1; |
| 2716 | pcrange->highpc_is_relative = 1; |
| 2717 | } |
| 2718 | else if (val->encoding == ATTR_VAL_ADDRESS_INDEX) |
| 2719 | { |
| 2720 | pcrange->highpc = (uintptr_t) val->u.uint; |
| 2721 | pcrange->have_highpc = 1; |
| 2722 | pcrange->highpc_is_addr_index = 1; |
| 2723 | } |
| 2724 | break; |
| 2725 | |
| 2726 | case DW_AT_ranges: |
| 2727 | if (val->encoding == ATTR_VAL_UINT |
| 2728 | || val->encoding == ATTR_VAL_REF_SECTION) |
| 2729 | { |
| 2730 | pcrange->ranges = val->u.uint; |
| 2731 | pcrange->have_ranges = 1; |
| 2732 | } |
| 2733 | else if (val->encoding == ATTR_VAL_RNGLISTS_INDEX) |
| 2734 | { |
| 2735 | pcrange->ranges = val->u.uint; |
| 2736 | pcrange->have_ranges = 1; |
| 2737 | pcrange->ranges_is_index = 1; |
| 2738 | } |
| 2739 | break; |
| 2740 | |
| 2741 | default: |
| 2742 | break; |
| 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | /* Call ADD_RANGE for a low/high PC pair. Returns 1 on success, 0 on |
| 2747 | error. */ |
| 2748 | |
| 2749 | static int |
| 2750 | add_low_high_range (struct backtrace_state *state, |
| 2751 | const struct dwarf_sections *dwarf_sections, |
| 2752 | struct libbacktrace_base_address base_address, |
| 2753 | int is_bigendian, struct unit *u, |
| 2754 | const struct pcrange *pcrange, |
| 2755 | int (*add_range) (struct backtrace_state *state, |
| 2756 | void *rdata, uintptr_t lowpc, |
| 2757 | uintptr_t highpc, |
| 2758 | backtrace_error_callback error_callback, |
| 2759 | void *data, void *vec), |
| 2760 | void *rdata, |
| 2761 | backtrace_error_callback error_callback, void *data, |
| 2762 | void *vec) |
| 2763 | { |
| 2764 | uintptr_t lowpc; |
| 2765 | uintptr_t highpc; |
| 2766 | |
| 2767 | lowpc = pcrange->lowpc; |
| 2768 | if (pcrange->lowpc_is_addr_index) |
| 2769 | { |
| 2770 | if (!resolve_addr_index (dwarf_sections, u->addr_base, u->addrsize, |
| 2771 | is_bigendian, lowpc, error_callback, data, |
| 2772 | &lowpc)) |
| 2773 | return 0; |
| 2774 | } |
| 2775 | |
| 2776 | highpc = pcrange->highpc; |
| 2777 | if (pcrange->highpc_is_addr_index) |
| 2778 | { |
| 2779 | if (!resolve_addr_index (dwarf_sections, u->addr_base, u->addrsize, |
| 2780 | is_bigendian, highpc, error_callback, data, |
| 2781 | &highpc)) |
| 2782 | return 0; |
| 2783 | } |
| 2784 | if (pcrange->highpc_is_relative) |
| 2785 | highpc += lowpc; |
| 2786 | |
| 2787 | /* Add in the base address of the module when recording PC values, |
| 2788 | so that we can look up the PC directly. */ |
| 2789 | lowpc = libbacktrace_add_base (lowpc, base_address); |
| 2790 | highpc = libbacktrace_add_base (highpc, base_address); |
| 2791 | |
| 2792 | return add_range (state, rdata, lowpc, highpc, error_callback, data, vec); |
| 2793 | } |
| 2794 | |
| 2795 | /* Call ADD_RANGE for each range read from .debug_ranges, as used in |
| 2796 | DWARF versions 2 through 4. */ |
| 2797 | |
| 2798 | static int |
| 2799 | add_ranges_from_ranges ( |
| 2800 | struct backtrace_state *state, |
| 2801 | const struct dwarf_sections *dwarf_sections, |
| 2802 | struct libbacktrace_base_address base_address, int is_bigendian, |
| 2803 | struct unit *u, uintptr_t base, |
| 2804 | const struct pcrange *pcrange, |
| 2805 | int (*add_range) (struct backtrace_state *state, void *rdata, |
| 2806 | uintptr_t lowpc, uintptr_t highpc, |
| 2807 | backtrace_error_callback error_callback, void *data, |
| 2808 | void *vec), |
| 2809 | void *rdata, |
| 2810 | backtrace_error_callback error_callback, void *data, |
| 2811 | void *vec) |
| 2812 | { |
| 2813 | struct dwarf_buf ranges_buf; |
| 2814 | |
| 2815 | if (pcrange->ranges >= dwarf_sections->size[DEBUG_RANGES]) |
| 2816 | { |
| 2817 | error_callback (data, "ranges offset out of range", 0); |
| 2818 | return 0; |
| 2819 | } |
| 2820 | |
| 2821 | ranges_buf.name = ".debug_ranges"; |
| 2822 | ranges_buf.start = dwarf_sections->data[DEBUG_RANGES]; |
| 2823 | ranges_buf.buf = dwarf_sections->data[DEBUG_RANGES] + pcrange->ranges; |
| 2824 | ranges_buf.left = dwarf_sections->size[DEBUG_RANGES] - pcrange->ranges; |
| 2825 | ranges_buf.is_bigendian = is_bigendian; |
| 2826 | ranges_buf.error_callback = error_callback; |
| 2827 | ranges_buf.data = data; |
| 2828 | ranges_buf.reported_underflow = 0; |
| 2829 | |
| 2830 | while (1) |
| 2831 | { |
| 2832 | uint64_t low; |
| 2833 | uint64_t high; |
| 2834 | |
| 2835 | if (ranges_buf.reported_underflow) |
| 2836 | return 0; |
| 2837 | |
| 2838 | low = read_address (&ranges_buf, u->addrsize); |
| 2839 | high = read_address (&ranges_buf, u->addrsize); |
| 2840 | |
| 2841 | if (low == 0 && high == 0) |
| 2842 | break; |
| 2843 | |
| 2844 | if (is_highest_address (low, u->addrsize)) |
| 2845 | base = (uintptr_t) high; |
| 2846 | else |
| 2847 | { |
| 2848 | uintptr_t rl, rh; |
| 2849 | |
| 2850 | rl = libbacktrace_add_base ((uintptr_t) low + base, base_address); |
| 2851 | rh = libbacktrace_add_base ((uintptr_t) high + base, base_address); |
| 2852 | if (!add_range (state, rdata, rl, rh, error_callback, data, vec)) |
| 2853 | return 0; |
| 2854 | } |
| 2855 | } |
| 2856 | |
| 2857 | if (ranges_buf.reported_underflow) |
| 2858 | return 0; |
| 2859 | |
| 2860 | return 1; |
| 2861 | } |
| 2862 | |
| 2863 | /* Call ADD_RANGE for each range read from .debug_rnglists, as used in |
| 2864 | DWARF version 5. */ |
| 2865 | |
| 2866 | static int |
| 2867 | add_ranges_from_rnglists ( |
| 2868 | struct backtrace_state *state, |
| 2869 | const struct dwarf_sections *dwarf_sections, |
| 2870 | struct libbacktrace_base_address base_address, int is_bigendian, |
| 2871 | struct unit *u, uintptr_t base, |
| 2872 | const struct pcrange *pcrange, |
| 2873 | int (*add_range) (struct backtrace_state *state, void *rdata, |
| 2874 | uintptr_t lowpc, uintptr_t highpc, |
| 2875 | backtrace_error_callback error_callback, void *data, |
| 2876 | void *vec), |
| 2877 | void *rdata, |
| 2878 | backtrace_error_callback error_callback, void *data, |
| 2879 | void *vec) |
| 2880 | { |
| 2881 | uint64_t offset; |
| 2882 | struct dwarf_buf rnglists_buf; |
| 2883 | |
| 2884 | if (!pcrange->ranges_is_index) |
| 2885 | offset = pcrange->ranges; |
| 2886 | else |
| 2887 | offset = u->rnglists_base + pcrange->ranges * (u->is_dwarf64 ? 8 : 4); |
| 2888 | if (offset >= dwarf_sections->size[DEBUG_RNGLISTS]) |
| 2889 | { |
| 2890 | error_callback (data, "rnglists offset out of range", 0); |
| 2891 | return 0; |
| 2892 | } |
| 2893 | |
| 2894 | rnglists_buf.name = ".debug_rnglists"; |
| 2895 | rnglists_buf.start = dwarf_sections->data[DEBUG_RNGLISTS]; |
| 2896 | rnglists_buf.buf = dwarf_sections->data[DEBUG_RNGLISTS] + offset; |
| 2897 | rnglists_buf.left = dwarf_sections->size[DEBUG_RNGLISTS] - offset; |
| 2898 | rnglists_buf.is_bigendian = is_bigendian; |
| 2899 | rnglists_buf.error_callback = error_callback; |
| 2900 | rnglists_buf.data = data; |
| 2901 | rnglists_buf.reported_underflow = 0; |
| 2902 | |
| 2903 | if (pcrange->ranges_is_index) |
| 2904 | { |
| 2905 | offset = read_offset (&rnglists_buf, u->is_dwarf64); |
| 2906 | offset += u->rnglists_base; |
| 2907 | if (offset >= dwarf_sections->size[DEBUG_RNGLISTS]) |
| 2908 | { |
| 2909 | error_callback (data, "rnglists index offset out of range", 0); |
| 2910 | return 0; |
| 2911 | } |
| 2912 | rnglists_buf.buf = dwarf_sections->data[DEBUG_RNGLISTS] + offset; |
| 2913 | rnglists_buf.left = dwarf_sections->size[DEBUG_RNGLISTS] - offset; |
| 2914 | } |
| 2915 | |
| 2916 | while (1) |
| 2917 | { |
| 2918 | unsigned char rle; |
| 2919 | |
| 2920 | rle = read_byte (&rnglists_buf); |
| 2921 | if (rle == DW_RLE_end_of_list) |
| 2922 | break; |
| 2923 | switch (rle) |
| 2924 | { |
| 2925 | case DW_RLE_base_addressx: |
| 2926 | { |
| 2927 | uint64_t index; |
| 2928 | |
| 2929 | index = read_uleb128 (&rnglists_buf); |
| 2930 | if (!resolve_addr_index (dwarf_sections, u->addr_base, |
| 2931 | u->addrsize, is_bigendian, index, |
| 2932 | error_callback, data, &base)) |
| 2933 | return 0; |
| 2934 | } |
| 2935 | break; |
| 2936 | |
| 2937 | case DW_RLE_startx_endx: |
| 2938 | { |
| 2939 | uint64_t index; |
| 2940 | uintptr_t low; |
| 2941 | uintptr_t high; |
| 2942 | |
| 2943 | index = read_uleb128 (&rnglists_buf); |
| 2944 | if (!resolve_addr_index (dwarf_sections, u->addr_base, |
| 2945 | u->addrsize, is_bigendian, index, |
| 2946 | error_callback, data, &low)) |
| 2947 | return 0; |
| 2948 | index = read_uleb128 (&rnglists_buf); |
| 2949 | if (!resolve_addr_index (dwarf_sections, u->addr_base, |
| 2950 | u->addrsize, is_bigendian, index, |
| 2951 | error_callback, data, &high)) |
| 2952 | return 0; |
| 2953 | if (!add_range (state, rdata, |
| 2954 | libbacktrace_add_base (low, base_address), |
| 2955 | libbacktrace_add_base (high, base_address), |
| 2956 | error_callback, data, vec)) |
| 2957 | return 0; |
| 2958 | } |
| 2959 | break; |
| 2960 | |
| 2961 | case DW_RLE_startx_length: |
| 2962 | { |
| 2963 | uint64_t index; |
| 2964 | uintptr_t low; |
| 2965 | uintptr_t length; |
| 2966 | |
| 2967 | index = read_uleb128 (&rnglists_buf); |
| 2968 | if (!resolve_addr_index (dwarf_sections, u->addr_base, |
| 2969 | u->addrsize, is_bigendian, index, |
| 2970 | error_callback, data, &low)) |
| 2971 | return 0; |
| 2972 | length = read_uleb128 (&rnglists_buf); |
| 2973 | low = libbacktrace_add_base (low, base_address); |
| 2974 | if (!add_range (state, rdata, low, low + length, |
| 2975 | error_callback, data, vec)) |
| 2976 | return 0; |
| 2977 | } |
| 2978 | break; |
| 2979 | |
| 2980 | case DW_RLE_offset_pair: |
| 2981 | { |
| 2982 | uint64_t low; |
| 2983 | uint64_t high; |
| 2984 | |
| 2985 | low = read_uleb128 (&rnglists_buf); |
| 2986 | high = read_uleb128 (&rnglists_buf); |
| 2987 | if (!add_range (state, rdata, |
| 2988 | libbacktrace_add_base (low + base, base_address), |
| 2989 | libbacktrace_add_base (high + base, base_address), |
| 2990 | error_callback, data, vec)) |
| 2991 | return 0; |
| 2992 | } |
| 2993 | break; |
| 2994 | |
| 2995 | case DW_RLE_base_address: |
| 2996 | base = (uintptr_t) read_address (&rnglists_buf, u->addrsize); |
| 2997 | break; |
| 2998 | |
| 2999 | case DW_RLE_start_end: |
| 3000 | { |
| 3001 | uintptr_t low; |
| 3002 | uintptr_t high; |
| 3003 | |
| 3004 | low = (uintptr_t) read_address (&rnglists_buf, u->addrsize); |
| 3005 | high = (uintptr_t) read_address (&rnglists_buf, u->addrsize); |
| 3006 | if (!add_range (state, rdata, |
| 3007 | libbacktrace_add_base (low, base_address), |
| 3008 | libbacktrace_add_base (high, base_address), |
| 3009 | error_callback, data, vec)) |
| 3010 | return 0; |
| 3011 | } |
| 3012 | break; |
| 3013 | |
| 3014 | case DW_RLE_start_length: |
| 3015 | { |
| 3016 | uintptr_t low; |
| 3017 | uintptr_t length; |
| 3018 | |
| 3019 | low = (uintptr_t) read_address (&rnglists_buf, u->addrsize); |
| 3020 | length = (uintptr_t) read_uleb128 (&rnglists_buf); |
| 3021 | low = libbacktrace_add_base (low, base_address); |
| 3022 | if (!add_range (state, rdata, low, low + length, |
| 3023 | error_callback, data, vec)) |
| 3024 | return 0; |
| 3025 | } |
| 3026 | break; |
| 3027 | |
| 3028 | default: |
| 3029 | dwarf_buf_error (&rnglists_buf, "unrecognized DW_RLE value", -1); |
| 3030 | return 0; |
| 3031 | } |
| 3032 | } |
| 3033 | |
| 3034 | if (rnglists_buf.reported_underflow) |
| 3035 | return 0; |
| 3036 | |
| 3037 | return 1; |
| 3038 | } |
| 3039 | |
| 3040 | /* Call ADD_RANGE for each lowpc/highpc pair in PCRANGE. RDATA is |
| 3041 | passed to ADD_RANGE, and is either a struct unit * or a struct |
| 3042 | function *. VEC is the vector we are adding ranges to, and is |
| 3043 | either a struct unit_addrs_vector * or a struct function_vector *. |
| 3044 | Returns 1 on success, 0 on error. */ |
| 3045 | |
| 3046 | static int |
| 3047 | add_ranges (struct backtrace_state *state, |
| 3048 | const struct dwarf_sections *dwarf_sections, |
| 3049 | struct libbacktrace_base_address base_address, int is_bigendian, |
| 3050 | struct unit *u, uintptr_t base, const struct pcrange *pcrange, |
| 3051 | int (*add_range) (struct backtrace_state *state, void *rdata, |
| 3052 | uintptr_t lowpc, uintptr_t highpc, |
| 3053 | backtrace_error_callback error_callback, |
| 3054 | void *data, void *vec), |
| 3055 | void *rdata, |
| 3056 | backtrace_error_callback error_callback, void *data, |
| 3057 | void *vec) |
| 3058 | { |
| 3059 | if (pcrange->have_lowpc && pcrange->have_highpc) |
| 3060 | return add_low_high_range (state, dwarf_sections, base_address, |
| 3061 | is_bigendian, u, pcrange, add_range, rdata, |
| 3062 | error_callback, data, vec); |
| 3063 | |
| 3064 | if (!pcrange->have_ranges) |
| 3065 | { |
| 3066 | /* Did not find any address ranges to add. */ |
| 3067 | return 1; |
| 3068 | } |
| 3069 | |
| 3070 | if (u->version < 5) |
| 3071 | return add_ranges_from_ranges (state, dwarf_sections, base_address, |
| 3072 | is_bigendian, u, base, pcrange, add_range, |
| 3073 | rdata, error_callback, data, vec); |
| 3074 | else |
| 3075 | return add_ranges_from_rnglists (state, dwarf_sections, base_address, |
| 3076 | is_bigendian, u, base, pcrange, add_range, |
| 3077 | rdata, error_callback, data, vec); |
| 3078 | } |
| 3079 | |
| 3080 | /* Find the address range covered by a compilation unit, reading from |
| 3081 | UNIT_BUF and adding values to U. Returns 1 if all data could be |
| 3082 | read, 0 if there is some error. */ |
| 3083 | |
| 3084 | static int |
| 3085 | find_address_ranges (struct backtrace_state *state, |
| 3086 | struct libbacktrace_base_address base_address, |
| 3087 | struct dwarf_buf *unit_buf, |
| 3088 | const struct dwarf_sections *dwarf_sections, |
| 3089 | int is_bigendian, struct dwarf_data *altlink, |
| 3090 | backtrace_error_callback error_callback, void *data, |
| 3091 | struct unit *u, struct unit_addrs_vector *addrs, |
| 3092 | enum dwarf_tag *unit_tag) |
| 3093 | { |
| 3094 | while (unit_buf->left > 0) |
| 3095 | { |
| 3096 | uint64_t code; |
| 3097 | const struct abbrev *abbrev; |
| 3098 | struct pcrange pcrange; |
| 3099 | struct attr_val name_val; |
| 3100 | int have_name_val; |
| 3101 | struct attr_val comp_dir_val; |
| 3102 | int have_comp_dir_val; |
| 3103 | size_t i; |
| 3104 | |
| 3105 | code = read_uleb128 (unit_buf); |
| 3106 | if (code == 0) |
| 3107 | return 1; |
| 3108 | |
| 3109 | abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data); |
| 3110 | if (abbrev == NULL) |
| 3111 | return 0; |
| 3112 | |
| 3113 | if (unit_tag != NULL) |
| 3114 | *unit_tag = abbrev->tag; |
| 3115 | |
| 3116 | memset (&pcrange, 0, sizeof pcrange); |
| 3117 | memset (&name_val, 0, sizeof name_val); |
| 3118 | have_name_val = 0; |
| 3119 | memset (&comp_dir_val, 0, sizeof comp_dir_val); |
| 3120 | have_comp_dir_val = 0; |
| 3121 | for (i = 0; i < abbrev->num_attrs; ++i) |
| 3122 | { |
| 3123 | struct attr_val val; |
| 3124 | |
| 3125 | if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val, |
| 3126 | unit_buf, u->is_dwarf64, u->version, |
| 3127 | u->addrsize, dwarf_sections, altlink, &val)) |
| 3128 | return 0; |
| 3129 | |
| 3130 | switch (abbrev->attrs[i].name) |
| 3131 | { |
| 3132 | case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges: |
| 3133 | update_pcrange (&abbrev->attrs[i], &val, &pcrange); |
| 3134 | break; |
| 3135 | |
| 3136 | case DW_AT_stmt_list: |
| 3137 | if ((abbrev->tag == DW_TAG_compile_unit |
| 3138 | || abbrev->tag == DW_TAG_skeleton_unit) |
| 3139 | && (val.encoding == ATTR_VAL_UINT |
| 3140 | || val.encoding == ATTR_VAL_REF_SECTION)) |
| 3141 | u->lineoff = val.u.uint; |
| 3142 | break; |
| 3143 | |
| 3144 | case DW_AT_name: |
| 3145 | if (abbrev->tag == DW_TAG_compile_unit |
| 3146 | || abbrev->tag == DW_TAG_skeleton_unit) |
| 3147 | { |
| 3148 | name_val = val; |
| 3149 | have_name_val = 1; |
| 3150 | } |
| 3151 | break; |
| 3152 | |
| 3153 | case DW_AT_comp_dir: |
| 3154 | if (abbrev->tag == DW_TAG_compile_unit |
| 3155 | || abbrev->tag == DW_TAG_skeleton_unit) |
| 3156 | { |
| 3157 | comp_dir_val = val; |
| 3158 | have_comp_dir_val = 1; |
| 3159 | } |
| 3160 | break; |
| 3161 | |
| 3162 | case DW_AT_str_offsets_base: |
| 3163 | if ((abbrev->tag == DW_TAG_compile_unit |
| 3164 | || abbrev->tag == DW_TAG_skeleton_unit) |
| 3165 | && val.encoding == ATTR_VAL_REF_SECTION) |
| 3166 | u->str_offsets_base = val.u.uint; |
| 3167 | break; |
| 3168 | |
| 3169 | case DW_AT_addr_base: |
| 3170 | if ((abbrev->tag == DW_TAG_compile_unit |
| 3171 | || abbrev->tag == DW_TAG_skeleton_unit) |
| 3172 | && val.encoding == ATTR_VAL_REF_SECTION) |
| 3173 | u->addr_base = val.u.uint; |
| 3174 | break; |
| 3175 | |
| 3176 | case DW_AT_rnglists_base: |
| 3177 | if ((abbrev->tag == DW_TAG_compile_unit |
| 3178 | || abbrev->tag == DW_TAG_skeleton_unit) |
| 3179 | && val.encoding == ATTR_VAL_REF_SECTION) |
| 3180 | u->rnglists_base = val.u.uint; |
| 3181 | break; |
| 3182 | |
| 3183 | default: |
| 3184 | break; |
| 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | // Resolve strings after we're sure that we have seen |
| 3189 | // DW_AT_str_offsets_base. |
| 3190 | if (have_name_val) |
| 3191 | { |
| 3192 | if (!resolve_string (dwarf_sections, u->is_dwarf64, is_bigendian, |
| 3193 | u->str_offsets_base, &name_val, |
| 3194 | error_callback, data, &u->filename)) |
| 3195 | return 0; |
| 3196 | } |
| 3197 | if (have_comp_dir_val) |
| 3198 | { |
| 3199 | if (!resolve_string (dwarf_sections, u->is_dwarf64, is_bigendian, |
| 3200 | u->str_offsets_base, &comp_dir_val, |
| 3201 | error_callback, data, &u->comp_dir)) |
| 3202 | return 0; |
| 3203 | } |
| 3204 | |
| 3205 | if (abbrev->tag == DW_TAG_compile_unit |
| 3206 | || abbrev->tag == DW_TAG_subprogram |
| 3207 | || abbrev->tag == DW_TAG_skeleton_unit) |
| 3208 | { |
| 3209 | if (!add_ranges (state, dwarf_sections, base_address, |
| 3210 | is_bigendian, u, pcrange.lowpc, &pcrange, |
| 3211 | add_unit_addr, (void *) u, error_callback, data, |
| 3212 | (void *) addrs)) |
| 3213 | return 0; |
| 3214 | |
| 3215 | /* If we found the PC range in the DW_TAG_compile_unit or |
| 3216 | DW_TAG_skeleton_unit, we can stop now. */ |
| 3217 | if ((abbrev->tag == DW_TAG_compile_unit |
| 3218 | || abbrev->tag == DW_TAG_skeleton_unit) |
| 3219 | && (pcrange.have_ranges |
| 3220 | || (pcrange.have_lowpc && pcrange.have_highpc))) |
| 3221 | return 1; |
| 3222 | } |
| 3223 | |
| 3224 | if (abbrev->has_children) |
| 3225 | { |
| 3226 | if (!find_address_ranges (state, base_address, unit_buf, |
| 3227 | dwarf_sections, is_bigendian, altlink, |
| 3228 | error_callback, data, u, addrs, NULL)) |
| 3229 | return 0; |
| 3230 | } |
| 3231 | } |
| 3232 | |
| 3233 | return 1; |
| 3234 | } |
| 3235 | |
| 3236 | /* Build a mapping from address ranges to the compilation units where |
| 3237 | the line number information for that range can be found. Returns 1 |
| 3238 | on success, 0 on failure. */ |
| 3239 | |
| 3240 | static int |
| 3241 | build_address_map (struct backtrace_state *state, |
| 3242 | struct libbacktrace_base_address base_address, |
| 3243 | const struct dwarf_sections *dwarf_sections, |
| 3244 | int is_bigendian, struct dwarf_data *altlink, |
| 3245 | backtrace_error_callback error_callback, void *data, |
| 3246 | struct unit_addrs_vector *addrs, |
| 3247 | struct unit_vector *unit_vec) |
| 3248 | { |
| 3249 | struct dwarf_buf info; |
| 3250 | struct backtrace_vector units; |
| 3251 | size_t units_count; |
| 3252 | size_t i; |
| 3253 | struct unit **pu; |
| 3254 | size_t unit_offset = 0; |
| 3255 | struct unit_addrs *pa; |
| 3256 | |
| 3257 | memset (&addrs->vec, 0, sizeof addrs->vec); |
| 3258 | memset (&unit_vec->vec, 0, sizeof unit_vec->vec); |
| 3259 | addrs->count = 0; |
| 3260 | unit_vec->count = 0; |
| 3261 | |
| 3262 | /* Read through the .debug_info section. FIXME: Should we use the |
| 3263 | .debug_aranges section? gdb and addr2line don't use it, but I'm |
| 3264 | not sure why. */ |
| 3265 | |
| 3266 | info.name = ".debug_info"; |
| 3267 | info.start = dwarf_sections->data[DEBUG_INFO]; |
| 3268 | info.buf = info.start; |
| 3269 | info.left = dwarf_sections->size[DEBUG_INFO]; |
| 3270 | info.is_bigendian = is_bigendian; |
| 3271 | info.error_callback = error_callback; |
| 3272 | info.data = data; |
| 3273 | info.reported_underflow = 0; |
| 3274 | |
| 3275 | memset (&units, 0, sizeof units); |
| 3276 | units_count = 0; |
| 3277 | |
| 3278 | while (info.left > 0) |
| 3279 | { |
| 3280 | const unsigned char *unit_data_start; |
| 3281 | uint64_t len; |
| 3282 | int is_dwarf64; |
| 3283 | struct dwarf_buf unit_buf; |
| 3284 | int version; |
| 3285 | int unit_type; |
| 3286 | uint64_t abbrev_offset; |
| 3287 | int addrsize; |
| 3288 | struct unit *u; |
| 3289 | enum dwarf_tag unit_tag; |
| 3290 | |
| 3291 | if (info.reported_underflow) |
| 3292 | goto fail; |
| 3293 | |
| 3294 | unit_data_start = info.buf; |
| 3295 | |
| 3296 | len = read_initial_length (&info, &is_dwarf64); |
| 3297 | unit_buf = info; |
| 3298 | unit_buf.left = len; |
| 3299 | |
| 3300 | if (!advance (&info, len)) |
| 3301 | goto fail; |
| 3302 | |
| 3303 | version = read_uint16 (&unit_buf); |
| 3304 | if (version < 2 || version > 5) |
| 3305 | { |
| 3306 | dwarf_buf_error (&unit_buf, "unrecognized DWARF version", -1); |
| 3307 | goto fail; |
| 3308 | } |
| 3309 | |
| 3310 | if (version < 5) |
| 3311 | unit_type = 0; |
| 3312 | else |
| 3313 | { |
| 3314 | unit_type = read_byte (&unit_buf); |
| 3315 | if (unit_type == DW_UT_type || unit_type == DW_UT_split_type) |
| 3316 | { |
| 3317 | /* This unit doesn't have anything we need. */ |
| 3318 | continue; |
| 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | pu = ((struct unit **) |
| 3323 | backtrace_vector_grow (state, sizeof (struct unit *), |
| 3324 | error_callback, data, &units)); |
| 3325 | if (pu == NULL) |
| 3326 | goto fail; |
| 3327 | |
| 3328 | u = ((struct unit *) |
| 3329 | backtrace_alloc (state, sizeof *u, error_callback, data)); |
| 3330 | if (u == NULL) |
| 3331 | goto fail; |
| 3332 | |
| 3333 | *pu = u; |
| 3334 | ++units_count; |
| 3335 | |
| 3336 | if (version < 5) |
| 3337 | addrsize = 0; /* Set below. */ |
| 3338 | else |
| 3339 | addrsize = read_byte (&unit_buf); |
| 3340 | |
| 3341 | memset (&u->abbrevs, 0, sizeof u->abbrevs); |
| 3342 | abbrev_offset = read_offset (&unit_buf, is_dwarf64); |
| 3343 | if (!read_abbrevs (state, abbrev_offset, |
| 3344 | dwarf_sections->data[DEBUG_ABBREV], |
| 3345 | dwarf_sections->size[DEBUG_ABBREV], |
| 3346 | is_bigendian, error_callback, data, &u->abbrevs)) |
| 3347 | goto fail; |
| 3348 | |
| 3349 | if (version < 5) |
| 3350 | addrsize = read_byte (&unit_buf); |
| 3351 | |
| 3352 | switch (unit_type) |
| 3353 | { |
| 3354 | case 0: |
| 3355 | break; |
| 3356 | case DW_UT_compile: case DW_UT_partial: |
| 3357 | break; |
| 3358 | case DW_UT_skeleton: case DW_UT_split_compile: |
| 3359 | read_uint64 (&unit_buf); /* dwo_id */ |
| 3360 | break; |
| 3361 | default: |
| 3362 | break; |
| 3363 | } |
| 3364 | |
| 3365 | u->low_offset = unit_offset; |
| 3366 | unit_offset += len + (is_dwarf64 ? 12 : 4); |
| 3367 | u->high_offset = unit_offset; |
| 3368 | u->unit_data = unit_buf.buf; |
| 3369 | u->unit_data_len = unit_buf.left; |
| 3370 | u->unit_data_offset = unit_buf.buf - unit_data_start; |
| 3371 | u->version = version; |
| 3372 | u->is_dwarf64 = is_dwarf64; |
| 3373 | u->addrsize = addrsize; |
| 3374 | u->filename = NULL; |
| 3375 | u->comp_dir = NULL; |
| 3376 | u->abs_filename = NULL; |
| 3377 | u->lineoff = 0; |
| 3378 | u->str_offsets_base = 0; |
| 3379 | u->addr_base = 0; |
| 3380 | u->rnglists_base = 0; |
| 3381 | |
| 3382 | /* The actual line number mappings will be read as needed. */ |
| 3383 | u->lines = NULL; |
| 3384 | u->lines_count = 0; |
| 3385 | u->function_addrs = NULL; |
| 3386 | u->function_addrs_count = 0; |
| 3387 | |
| 3388 | if (!find_address_ranges (state, base_address, &unit_buf, dwarf_sections, |
| 3389 | is_bigendian, altlink, error_callback, data, |
| 3390 | u, addrs, &unit_tag)) |
| 3391 | goto fail; |
| 3392 | |
| 3393 | if (unit_buf.reported_underflow) |
| 3394 | goto fail; |
| 3395 | } |
| 3396 | if (info.reported_underflow) |
| 3397 | goto fail; |
| 3398 | |
| 3399 | /* Add a trailing addrs entry, but don't include it in addrs->count. */ |
| 3400 | pa = ((struct unit_addrs *) |
| 3401 | backtrace_vector_grow (state, sizeof (struct unit_addrs), |
| 3402 | error_callback, data, &addrs->vec)); |
| 3403 | if (pa == NULL) |
| 3404 | goto fail; |
| 3405 | pa->low = 0; |
| 3406 | --pa->low; |
| 3407 | pa->high = pa->low; |
| 3408 | pa->u = NULL; |
| 3409 | |
| 3410 | unit_vec->vec = units; |
| 3411 | unit_vec->count = units_count; |
| 3412 | return 1; |
| 3413 | |
| 3414 | fail: |
| 3415 | if (units_count > 0) |
| 3416 | { |
| 3417 | pu = (struct unit **) units.base; |
| 3418 | for (i = 0; i < units_count; i++) |
| 3419 | { |
| 3420 | free_abbrevs (state, &pu[i]->abbrevs, error_callback, data); |
| 3421 | backtrace_free (state, pu[i], sizeof **pu, error_callback, data); |
| 3422 | } |
| 3423 | backtrace_vector_free (state, &units, error_callback, data); |
| 3424 | } |
| 3425 | if (addrs->count > 0) |
| 3426 | { |
| 3427 | backtrace_vector_free (state, &addrs->vec, error_callback, data); |
| 3428 | addrs->count = 0; |
| 3429 | } |
| 3430 | return 0; |
| 3431 | } |
| 3432 | |
| 3433 | /* Add a new mapping to the vector of line mappings that we are |
| 3434 | building. Returns 1 on success, 0 on failure. */ |
| 3435 | |
| 3436 | static int |
| 3437 | add_line (struct backtrace_state *state, struct dwarf_data *ddata, |
| 3438 | uintptr_t pc, const char *filename, int lineno, |
| 3439 | backtrace_error_callback error_callback, void *data, |
| 3440 | struct line_vector *vec) |
| 3441 | { |
| 3442 | struct line *ln; |
| 3443 | |
| 3444 | /* If we are adding the same mapping, ignore it. This can happen |
| 3445 | when using discriminators. */ |
| 3446 | if (vec->count > 0) |
| 3447 | { |
| 3448 | ln = (struct line *) vec->vec.base + (vec->count - 1); |
| 3449 | if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno) |
| 3450 | return 1; |
| 3451 | } |
| 3452 | |
| 3453 | ln = ((struct line *) |
| 3454 | backtrace_vector_grow (state, sizeof (struct line), error_callback, |
| 3455 | data, &vec->vec)); |
| 3456 | if (ln == NULL) |
| 3457 | return 0; |
| 3458 | |
| 3459 | /* Add in the base address here, so that we can look up the PC |
| 3460 | directly. */ |
| 3461 | ln->pc = libbacktrace_add_base (pc, ddata->base_address); |
| 3462 | |
| 3463 | ln->filename = filename; |
| 3464 | ln->lineno = lineno; |
| 3465 | ln->idx = vec->count; |
| 3466 | |
| 3467 | ++vec->count; |
| 3468 | |
| 3469 | return 1; |
| 3470 | } |
| 3471 | |
| 3472 | /* Free the line header information. */ |
| 3473 | |
| 3474 | static void |
| 3475 | free_line_header (struct backtrace_state *state, struct line_header *hdr, |
| 3476 | backtrace_error_callback error_callback, void *data) |
| 3477 | { |
| 3478 | if (hdr->dirs_count != 0) |
| 3479 | backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *), |
| 3480 | error_callback, data); |
| 3481 | backtrace_free (state, hdr->filenames, |
| 3482 | hdr->filenames_count * sizeof (char *), |
| 3483 | error_callback, data); |
| 3484 | } |
| 3485 | |
| 3486 | /* Read the directories and file names for a line header for version |
| 3487 | 2, setting fields in HDR. Return 1 on success, 0 on failure. */ |
| 3488 | |
| 3489 | static int |
| 3490 | read_v2_paths (struct backtrace_state *state, struct unit *u, |
| 3491 | struct dwarf_buf *hdr_buf, struct line_header *hdr) |
| 3492 | { |
| 3493 | const unsigned char *p; |
| 3494 | const unsigned char *pend; |
| 3495 | size_t i; |
| 3496 | |
| 3497 | /* Count the number of directory entries. */ |
| 3498 | hdr->dirs_count = 0; |
| 3499 | p = hdr_buf->buf; |
| 3500 | pend = p + hdr_buf->left; |
| 3501 | while (p < pend && *p != '\0') |
| 3502 | { |
| 3503 | p += strnlen((const char *) p, pend - p) + 1; |
| 3504 | ++hdr->dirs_count; |
| 3505 | } |
| 3506 | |
| 3507 | /* The index of the first entry in the list of directories is 1. Index 0 is |
| 3508 | used for the current directory of the compilation. To simplify index |
| 3509 | handling, we set entry 0 to the compilation unit directory. */ |
| 3510 | ++hdr->dirs_count; |
| 3511 | hdr->dirs = ((const char **) |
| 3512 | backtrace_alloc (state, |
| 3513 | hdr->dirs_count * sizeof (const char *), |
| 3514 | hdr_buf->error_callback, |
| 3515 | hdr_buf->data)); |
| 3516 | if (hdr->dirs == NULL) |
| 3517 | return 0; |
| 3518 | |
| 3519 | hdr->dirs[0] = u->comp_dir; |
| 3520 | i = 1; |
| 3521 | while (*hdr_buf->buf != '\0') |
| 3522 | { |
| 3523 | if (hdr_buf->reported_underflow) |
| 3524 | return 0; |
| 3525 | |
| 3526 | hdr->dirs[i] = read_string (hdr_buf); |
| 3527 | if (hdr->dirs[i] == NULL) |
| 3528 | return 0; |
| 3529 | ++i; |
| 3530 | } |
| 3531 | if (!advance (hdr_buf, 1)) |
| 3532 | return 0; |
| 3533 | |
| 3534 | /* Count the number of file entries. */ |
| 3535 | hdr->filenames_count = 0; |
| 3536 | p = hdr_buf->buf; |
| 3537 | pend = p + hdr_buf->left; |
| 3538 | while (p < pend && *p != '\0') |
| 3539 | { |
| 3540 | p += strnlen ((const char *) p, pend - p) + 1; |
| 3541 | p += leb128_len (p); |
| 3542 | p += leb128_len (p); |
| 3543 | p += leb128_len (p); |
| 3544 | ++hdr->filenames_count; |
| 3545 | } |
| 3546 | |
| 3547 | /* The index of the first entry in the list of file names is 1. Index 0 is |
| 3548 | used for the DW_AT_name of the compilation unit. To simplify index |
| 3549 | handling, we set entry 0 to the compilation unit file name. */ |
| 3550 | ++hdr->filenames_count; |
| 3551 | hdr->filenames = ((const char **) |
| 3552 | backtrace_alloc (state, |
| 3553 | hdr->filenames_count * sizeof (char *), |
| 3554 | hdr_buf->error_callback, |
| 3555 | hdr_buf->data)); |
| 3556 | if (hdr->filenames == NULL) |
| 3557 | return 0; |
| 3558 | hdr->filenames[0] = u->filename; |
| 3559 | i = 1; |
| 3560 | while (*hdr_buf->buf != '\0') |
| 3561 | { |
| 3562 | const char *filename; |
| 3563 | uint64_t dir_index; |
| 3564 | |
| 3565 | if (hdr_buf->reported_underflow) |
| 3566 | return 0; |
| 3567 | |
| 3568 | filename = read_string (hdr_buf); |
| 3569 | if (filename == NULL) |
| 3570 | return 0; |
| 3571 | dir_index = read_uleb128 (hdr_buf); |
| 3572 | if (IS_ABSOLUTE_PATH (filename) |
| 3573 | || (dir_index < hdr->dirs_count && hdr->dirs[dir_index] == NULL)) |
| 3574 | hdr->filenames[i] = filename; |
| 3575 | else |
| 3576 | { |
| 3577 | const char *dir; |
| 3578 | size_t dir_len; |
| 3579 | size_t filename_len; |
| 3580 | char *s; |
| 3581 | |
| 3582 | if (dir_index < hdr->dirs_count) |
| 3583 | dir = hdr->dirs[dir_index]; |
| 3584 | else |
| 3585 | { |
| 3586 | dwarf_buf_error (hdr_buf, |
| 3587 | ("invalid directory index in " |
| 3588 | "line number program header"), |
| 3589 | 0); |
| 3590 | return 0; |
| 3591 | } |
| 3592 | dir_len = strlen (dir); |
| 3593 | filename_len = strlen (filename); |
| 3594 | s = ((char *) backtrace_alloc (state, dir_len + filename_len + 2, |
| 3595 | hdr_buf->error_callback, |
| 3596 | hdr_buf->data)); |
| 3597 | if (s == NULL) |
| 3598 | return 0; |
| 3599 | memcpy (s, dir, dir_len); |
| 3600 | /* FIXME: If we are on a DOS-based file system, and the |
| 3601 | directory or the file name use backslashes, then we |
| 3602 | should use a backslash here. */ |
| 3603 | s[dir_len] = '/'; |
| 3604 | memcpy (s + dir_len + 1, filename, filename_len + 1); |
| 3605 | hdr->filenames[i] = s; |
| 3606 | } |
| 3607 | |
| 3608 | /* Ignore the modification time and size. */ |
| 3609 | read_uleb128 (hdr_buf); |
| 3610 | read_uleb128 (hdr_buf); |
| 3611 | |
| 3612 | ++i; |
| 3613 | } |
| 3614 | |
| 3615 | return 1; |
| 3616 | } |
| 3617 | |
| 3618 | /* Read a single version 5 LNCT entry for a directory or file name in a |
| 3619 | line header. Sets *STRING to the resulting name, ignoring other |
| 3620 | data. Return 1 on success, 0 on failure. */ |
| 3621 | |
| 3622 | static int |
| 3623 | read_lnct (struct backtrace_state *state, struct dwarf_data *ddata, |
| 3624 | struct unit *u, struct dwarf_buf *hdr_buf, |
| 3625 | const struct line_header *hdr, size_t formats_count, |
| 3626 | const struct line_header_format *formats, const char **string) |
| 3627 | { |
| 3628 | size_t i; |
| 3629 | const char *dir; |
| 3630 | const char *path; |
| 3631 | |
| 3632 | dir = NULL; |
| 3633 | path = NULL; |
| 3634 | for (i = 0; i < formats_count; i++) |
| 3635 | { |
| 3636 | struct attr_val val; |
| 3637 | |
| 3638 | if (!read_attribute (formats[i].form, 0, hdr_buf, u->is_dwarf64, |
| 3639 | u->version, hdr->addrsize, &ddata->dwarf_sections, |
| 3640 | ddata->altlink, &val)) |
| 3641 | return 0; |
| 3642 | switch (formats[i].lnct) |
| 3643 | { |
| 3644 | case DW_LNCT_path: |
| 3645 | if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64, |
| 3646 | ddata->is_bigendian, u->str_offsets_base, |
| 3647 | &val, hdr_buf->error_callback, hdr_buf->data, |
| 3648 | &path)) |
| 3649 | return 0; |
| 3650 | break; |
| 3651 | case DW_LNCT_directory_index: |
| 3652 | if (val.encoding == ATTR_VAL_UINT) |
| 3653 | { |
| 3654 | if (val.u.uint >= hdr->dirs_count) |
| 3655 | { |
| 3656 | dwarf_buf_error (hdr_buf, |
| 3657 | ("invalid directory index in " |
| 3658 | "line number program header"), |
| 3659 | 0); |
| 3660 | return 0; |
| 3661 | } |
| 3662 | dir = hdr->dirs[val.u.uint]; |
| 3663 | } |
| 3664 | break; |
| 3665 | default: |
| 3666 | /* We don't care about timestamps or sizes or hashes. */ |
| 3667 | break; |
| 3668 | } |
| 3669 | } |
| 3670 | |
| 3671 | if (path == NULL) |
| 3672 | { |
| 3673 | dwarf_buf_error (hdr_buf, |
| 3674 | "missing file name in line number program header", |
| 3675 | 0); |
| 3676 | return 0; |
| 3677 | } |
| 3678 | |
| 3679 | if (dir == NULL) |
| 3680 | *string = path; |
| 3681 | else |
| 3682 | { |
| 3683 | size_t dir_len; |
| 3684 | size_t path_len; |
| 3685 | char *s; |
| 3686 | |
| 3687 | dir_len = strlen (dir); |
| 3688 | path_len = strlen (path); |
| 3689 | s = (char *) backtrace_alloc (state, dir_len + path_len + 2, |
| 3690 | hdr_buf->error_callback, hdr_buf->data); |
| 3691 | if (s == NULL) |
| 3692 | return 0; |
| 3693 | memcpy (s, dir, dir_len); |
| 3694 | /* FIXME: If we are on a DOS-based file system, and the |
| 3695 | directory or the path name use backslashes, then we should |
| 3696 | use a backslash here. */ |
| 3697 | s[dir_len] = '/'; |
| 3698 | memcpy (s + dir_len + 1, path, path_len + 1); |
| 3699 | *string = s; |
| 3700 | } |
| 3701 | |
| 3702 | return 1; |
| 3703 | } |
| 3704 | |
| 3705 | /* Read a set of DWARF 5 line header format entries, setting *PCOUNT |
| 3706 | and *PPATHS. Return 1 on success, 0 on failure. */ |
| 3707 | |
| 3708 | static int |
| 3709 | read_line_header_format_entries (struct backtrace_state *state, |
| 3710 | struct dwarf_data *ddata, |
| 3711 | struct unit *u, |
| 3712 | struct dwarf_buf *hdr_buf, |
| 3713 | struct line_header *hdr, |
| 3714 | size_t *pcount, |
| 3715 | const char ***ppaths) |
| 3716 | { |
| 3717 | size_t formats_count; |
| 3718 | struct line_header_format *formats; |
| 3719 | size_t paths_count; |
| 3720 | const char **paths; |
| 3721 | size_t i; |
| 3722 | int ret; |
| 3723 | |
| 3724 | formats_count = read_byte (hdr_buf); |
| 3725 | if (formats_count == 0) |
| 3726 | formats = NULL; |
| 3727 | else |
| 3728 | { |
| 3729 | formats = ((struct line_header_format *) |
| 3730 | backtrace_alloc (state, |
| 3731 | (formats_count |
| 3732 | * sizeof (struct line_header_format)), |
| 3733 | hdr_buf->error_callback, |
| 3734 | hdr_buf->data)); |
| 3735 | if (formats == NULL) |
| 3736 | return 0; |
| 3737 | |
| 3738 | for (i = 0; i < formats_count; i++) |
| 3739 | { |
| 3740 | formats[i].lnct = (int) read_uleb128(hdr_buf); |
| 3741 | formats[i].form = (enum dwarf_form) read_uleb128 (hdr_buf); |
| 3742 | } |
| 3743 | } |
| 3744 | |
| 3745 | paths_count = read_uleb128 (hdr_buf); |
| 3746 | if (paths_count == 0) |
| 3747 | { |
| 3748 | *pcount = 0; |
| 3749 | *ppaths = NULL; |
| 3750 | ret = 1; |
| 3751 | goto exit; |
| 3752 | } |
| 3753 | |
| 3754 | paths = ((const char **) |
| 3755 | backtrace_alloc (state, paths_count * sizeof (const char *), |
| 3756 | hdr_buf->error_callback, hdr_buf->data)); |
| 3757 | if (paths == NULL) |
| 3758 | { |
| 3759 | ret = 0; |
| 3760 | goto exit; |
| 3761 | } |
| 3762 | for (i = 0; i < paths_count; i++) |
| 3763 | { |
| 3764 | if (!read_lnct (state, ddata, u, hdr_buf, hdr, formats_count, |
| 3765 | formats, &paths[i])) |
| 3766 | { |
| 3767 | backtrace_free (state, paths, |
| 3768 | paths_count * sizeof (const char *), |
| 3769 | hdr_buf->error_callback, hdr_buf->data); |
| 3770 | ret = 0; |
| 3771 | goto exit; |
| 3772 | } |
| 3773 | } |
| 3774 | |
| 3775 | *pcount = paths_count; |
| 3776 | *ppaths = paths; |
| 3777 | |
| 3778 | ret = 1; |
| 3779 | |
| 3780 | exit: |
| 3781 | if (formats != NULL) |
| 3782 | backtrace_free (state, formats, |
| 3783 | formats_count * sizeof (struct line_header_format), |
| 3784 | hdr_buf->error_callback, hdr_buf->data); |
| 3785 | |
| 3786 | return ret; |
| 3787 | } |
| 3788 | |
| 3789 | /* Read the line header. Return 1 on success, 0 on failure. */ |
| 3790 | |
| 3791 | static int |
| 3792 | read_line_header (struct backtrace_state *state, struct dwarf_data *ddata, |
| 3793 | struct unit *u, int is_dwarf64, struct dwarf_buf *line_buf, |
| 3794 | struct line_header *hdr) |
| 3795 | { |
| 3796 | uint64_t hdrlen; |
| 3797 | struct dwarf_buf hdr_buf; |
| 3798 | |
| 3799 | hdr->version = read_uint16 (line_buf); |
| 3800 | if (hdr->version < 2 || hdr->version > 5) |
| 3801 | { |
| 3802 | dwarf_buf_error (line_buf, "unsupported line number version", -1); |
| 3803 | return 0; |
| 3804 | } |
| 3805 | |
| 3806 | if (hdr->version < 5) |
| 3807 | hdr->addrsize = u->addrsize; |
| 3808 | else |
| 3809 | { |
| 3810 | hdr->addrsize = read_byte (line_buf); |
| 3811 | /* We could support a non-zero segment_selector_size but I doubt |
| 3812 | we'll ever see it. */ |
| 3813 | if (read_byte (line_buf) != 0) |
| 3814 | { |
| 3815 | dwarf_buf_error (line_buf, |
| 3816 | "non-zero segment_selector_size not supported", |
| 3817 | -1); |
| 3818 | return 0; |
| 3819 | } |
| 3820 | } |
| 3821 | |
| 3822 | hdrlen = read_offset (line_buf, is_dwarf64); |
| 3823 | |
| 3824 | hdr_buf = *line_buf; |
| 3825 | hdr_buf.left = hdrlen; |
| 3826 | |
| 3827 | if (!advance (line_buf, hdrlen)) |
| 3828 | return 0; |
| 3829 | |
| 3830 | hdr->min_insn_len = read_byte (&hdr_buf); |
| 3831 | if (hdr->version < 4) |
| 3832 | hdr->max_ops_per_insn = 1; |
| 3833 | else |
| 3834 | hdr->max_ops_per_insn = read_byte (&hdr_buf); |
| 3835 | |
| 3836 | /* We don't care about default_is_stmt. */ |
| 3837 | read_byte (&hdr_buf); |
| 3838 | |
| 3839 | hdr->line_base = read_sbyte (&hdr_buf); |
| 3840 | hdr->line_range = read_byte (&hdr_buf); |
| 3841 | |
| 3842 | hdr->opcode_base = read_byte (&hdr_buf); |
| 3843 | hdr->opcode_lengths = hdr_buf.buf; |
| 3844 | if (!advance (&hdr_buf, hdr->opcode_base - 1)) |
| 3845 | return 0; |
| 3846 | |
| 3847 | if (hdr->version < 5) |
| 3848 | { |
| 3849 | if (!read_v2_paths (state, u, &hdr_buf, hdr)) |
| 3850 | return 0; |
| 3851 | } |
| 3852 | else |
| 3853 | { |
| 3854 | if (!read_line_header_format_entries (state, ddata, u, &hdr_buf, hdr, |
| 3855 | &hdr->dirs_count, |
| 3856 | &hdr->dirs)) |
| 3857 | return 0; |
| 3858 | if (!read_line_header_format_entries (state, ddata, u, &hdr_buf, hdr, |
| 3859 | &hdr->filenames_count, |
| 3860 | &hdr->filenames)) |
| 3861 | return 0; |
| 3862 | } |
| 3863 | |
| 3864 | if (hdr_buf.reported_underflow) |
| 3865 | return 0; |
| 3866 | |
| 3867 | return 1; |
| 3868 | } |
| 3869 | |
| 3870 | /* Read the line program, adding line mappings to VEC. Return 1 on |
| 3871 | success, 0 on failure. */ |
| 3872 | |
| 3873 | static int |
| 3874 | read_line_program (struct backtrace_state *state, struct dwarf_data *ddata, |
| 3875 | const struct line_header *hdr, struct dwarf_buf *line_buf, |
| 3876 | struct line_vector *vec) |
| 3877 | { |
| 3878 | uint64_t address; |
| 3879 | unsigned int op_index; |
| 3880 | const char *reset_filename; |
| 3881 | const char *filename; |
| 3882 | int lineno; |
| 3883 | |
| 3884 | address = 0; |
| 3885 | op_index = 0; |
| 3886 | if (hdr->filenames_count > 1) |
| 3887 | reset_filename = hdr->filenames[1]; |
| 3888 | else |
| 3889 | reset_filename = ""; |
| 3890 | filename = reset_filename; |
| 3891 | lineno = 1; |
| 3892 | while (line_buf->left > 0) |
| 3893 | { |
| 3894 | unsigned int op; |
| 3895 | |
| 3896 | op = read_byte (line_buf); |
| 3897 | if (op >= hdr->opcode_base) |
| 3898 | { |
| 3899 | unsigned int advance; |
| 3900 | |
| 3901 | /* Special opcode. */ |
| 3902 | op -= hdr->opcode_base; |
| 3903 | advance = op / hdr->line_range; |
| 3904 | address += (hdr->min_insn_len * (op_index + advance) |
| 3905 | / hdr->max_ops_per_insn); |
| 3906 | op_index = (op_index + advance) % hdr->max_ops_per_insn; |
| 3907 | lineno += hdr->line_base + (int) (op % hdr->line_range); |
| 3908 | add_line (state, ddata, address, filename, lineno, |
| 3909 | line_buf->error_callback, line_buf->data, vec); |
| 3910 | } |
| 3911 | else if (op == DW_LNS_extended_op) |
| 3912 | { |
| 3913 | uint64_t len; |
| 3914 | |
| 3915 | len = read_uleb128 (line_buf); |
| 3916 | op = read_byte (line_buf); |
| 3917 | switch (op) |
| 3918 | { |
| 3919 | case DW_LNE_end_sequence: |
| 3920 | /* FIXME: Should we mark the high PC here? It seems |
| 3921 | that we already have that information from the |
| 3922 | compilation unit. */ |
| 3923 | address = 0; |
| 3924 | op_index = 0; |
| 3925 | filename = reset_filename; |
| 3926 | lineno = 1; |
| 3927 | break; |
| 3928 | case DW_LNE_set_address: |
| 3929 | address = read_address (line_buf, hdr->addrsize); |
| 3930 | break; |
| 3931 | case DW_LNE_define_file: |
| 3932 | { |
| 3933 | const char *f; |
| 3934 | unsigned int dir_index; |
| 3935 | |
| 3936 | f = read_string (line_buf); |
| 3937 | if (f == NULL) |
| 3938 | return 0; |
| 3939 | dir_index = read_uleb128 (line_buf); |
| 3940 | /* Ignore that time and length. */ |
| 3941 | read_uleb128 (line_buf); |
| 3942 | read_uleb128 (line_buf); |
| 3943 | if (IS_ABSOLUTE_PATH (f)) |
| 3944 | filename = f; |
| 3945 | else |
| 3946 | { |
| 3947 | const char *dir; |
| 3948 | size_t dir_len; |
| 3949 | size_t f_len; |
| 3950 | char *p; |
| 3951 | |
| 3952 | if (dir_index < hdr->dirs_count) |
| 3953 | dir = hdr->dirs[dir_index]; |
| 3954 | else |
| 3955 | { |
| 3956 | dwarf_buf_error (line_buf, |
| 3957 | ("invalid directory index " |
| 3958 | "in line number program"), |
| 3959 | 0); |
| 3960 | return 0; |
| 3961 | } |
| 3962 | dir_len = strlen (dir); |
| 3963 | f_len = strlen (f); |
| 3964 | p = ((char *) |
| 3965 | backtrace_alloc (state, dir_len + f_len + 2, |
| 3966 | line_buf->error_callback, |
| 3967 | line_buf->data)); |
| 3968 | if (p == NULL) |
| 3969 | return 0; |
| 3970 | memcpy (p, dir, dir_len); |
| 3971 | /* FIXME: If we are on a DOS-based file system, |
| 3972 | and the directory or the file name use |
| 3973 | backslashes, then we should use a backslash |
| 3974 | here. */ |
| 3975 | p[dir_len] = '/'; |
| 3976 | memcpy (p + dir_len + 1, f, f_len + 1); |
| 3977 | filename = p; |
| 3978 | } |
| 3979 | } |
| 3980 | break; |
| 3981 | case DW_LNE_set_discriminator: |
| 3982 | /* We don't care about discriminators. */ |
| 3983 | read_uleb128 (line_buf); |
| 3984 | break; |
| 3985 | default: |
| 3986 | if (!advance (line_buf, len - 1)) |
| 3987 | return 0; |
| 3988 | break; |
| 3989 | } |
| 3990 | } |
| 3991 | else |
| 3992 | { |
| 3993 | switch (op) |
| 3994 | { |
| 3995 | case DW_LNS_copy: |
| 3996 | add_line (state, ddata, address, filename, lineno, |
| 3997 | line_buf->error_callback, line_buf->data, vec); |
| 3998 | break; |
| 3999 | case DW_LNS_advance_pc: |
| 4000 | { |
| 4001 | uint64_t advance; |
| 4002 | |
| 4003 | advance = read_uleb128 (line_buf); |
| 4004 | address += (hdr->min_insn_len * (op_index + advance) |
| 4005 | / hdr->max_ops_per_insn); |
| 4006 | op_index = (op_index + advance) % hdr->max_ops_per_insn; |
| 4007 | } |
| 4008 | break; |
| 4009 | case DW_LNS_advance_line: |
| 4010 | lineno += (int) read_sleb128 (line_buf); |
| 4011 | break; |
| 4012 | case DW_LNS_set_file: |
| 4013 | { |
| 4014 | uint64_t fileno; |
| 4015 | |
| 4016 | fileno = read_uleb128 (line_buf); |
| 4017 | if (fileno >= hdr->filenames_count) |
| 4018 | { |
| 4019 | dwarf_buf_error (line_buf, |
| 4020 | ("invalid file number in " |
| 4021 | "line number program"), |
| 4022 | 0); |
| 4023 | return 0; |
| 4024 | } |
| 4025 | filename = hdr->filenames[fileno]; |
| 4026 | } |
| 4027 | break; |
| 4028 | case DW_LNS_set_column: |
| 4029 | read_uleb128 (line_buf); |
| 4030 | break; |
| 4031 | case DW_LNS_negate_stmt: |
| 4032 | break; |
| 4033 | case DW_LNS_set_basic_block: |
| 4034 | break; |
| 4035 | case DW_LNS_const_add_pc: |
| 4036 | { |
| 4037 | unsigned int advance; |
| 4038 | |
| 4039 | op = 255 - hdr->opcode_base; |
| 4040 | advance = op / hdr->line_range; |
| 4041 | address += (hdr->min_insn_len * (op_index + advance) |
| 4042 | / hdr->max_ops_per_insn); |
| 4043 | op_index = (op_index + advance) % hdr->max_ops_per_insn; |
| 4044 | } |
| 4045 | break; |
| 4046 | case DW_LNS_fixed_advance_pc: |
| 4047 | address += read_uint16 (line_buf); |
| 4048 | op_index = 0; |
| 4049 | break; |
| 4050 | case DW_LNS_set_prologue_end: |
| 4051 | break; |
| 4052 | case DW_LNS_set_epilogue_begin: |
| 4053 | break; |
| 4054 | case DW_LNS_set_isa: |
| 4055 | read_uleb128 (line_buf); |
| 4056 | break; |
| 4057 | default: |
| 4058 | { |
| 4059 | unsigned int i; |
| 4060 | |
| 4061 | for (i = hdr->opcode_lengths[op - 1]; i > 0; --i) |
| 4062 | read_uleb128 (line_buf); |
| 4063 | } |
| 4064 | break; |
| 4065 | } |
| 4066 | } |
| 4067 | } |
| 4068 | |
| 4069 | return 1; |
| 4070 | } |
| 4071 | |
| 4072 | /* Read the line number information for a compilation unit. Returns 1 |
| 4073 | on success, 0 on failure. */ |
| 4074 | |
| 4075 | static int |
| 4076 | read_line_info (struct backtrace_state *state, struct dwarf_data *ddata, |
| 4077 | backtrace_error_callback error_callback, void *data, |
| 4078 | struct unit *u, struct line_header *hdr, struct line **lines, |
| 4079 | size_t *lines_count) |
| 4080 | { |
| 4081 | struct line_vector vec; |
| 4082 | struct dwarf_buf line_buf; |
| 4083 | uint64_t len; |
| 4084 | int is_dwarf64; |
| 4085 | struct line *ln; |
| 4086 | |
| 4087 | memset (&vec.vec, 0, sizeof vec.vec); |
| 4088 | vec.count = 0; |
| 4089 | |
| 4090 | memset (hdr, 0, sizeof *hdr); |
| 4091 | |
| 4092 | if (u->lineoff != (off_t) (size_t) u->lineoff |
| 4093 | || (size_t) u->lineoff >= ddata->dwarf_sections.size[DEBUG_LINE]) |
| 4094 | { |
| 4095 | error_callback (data, "unit line offset out of range", 0); |
| 4096 | goto fail; |
| 4097 | } |
| 4098 | |
| 4099 | line_buf.name = ".debug_line"; |
| 4100 | line_buf.start = ddata->dwarf_sections.data[DEBUG_LINE]; |
| 4101 | line_buf.buf = ddata->dwarf_sections.data[DEBUG_LINE] + u->lineoff; |
| 4102 | line_buf.left = ddata->dwarf_sections.size[DEBUG_LINE] - u->lineoff; |
| 4103 | line_buf.is_bigendian = ddata->is_bigendian; |
| 4104 | line_buf.error_callback = error_callback; |
| 4105 | line_buf.data = data; |
| 4106 | line_buf.reported_underflow = 0; |
| 4107 | |
| 4108 | len = read_initial_length (&line_buf, &is_dwarf64); |
| 4109 | line_buf.left = len; |
| 4110 | |
| 4111 | if (!read_line_header (state, ddata, u, is_dwarf64, &line_buf, hdr)) |
| 4112 | goto fail; |
| 4113 | |
| 4114 | if (!read_line_program (state, ddata, hdr, &line_buf, &vec)) |
| 4115 | goto fail; |
| 4116 | |
| 4117 | if (line_buf.reported_underflow) |
| 4118 | goto fail; |
| 4119 | |
| 4120 | if (vec.count == 0) |
| 4121 | { |
| 4122 | /* This is not a failure in the sense of generating an error, |
| 4123 | but it is a failure in that sense that we have no useful |
| 4124 | information. */ |
| 4125 | goto fail; |
| 4126 | } |
| 4127 | |
| 4128 | /* Allocate one extra entry at the end. */ |
| 4129 | ln = ((struct line *) |
| 4130 | backtrace_vector_grow (state, sizeof (struct line), error_callback, |
| 4131 | data, &vec.vec)); |
| 4132 | if (ln == NULL) |
| 4133 | goto fail; |
| 4134 | ln->pc = (uintptr_t) -1; |
| 4135 | ln->filename = NULL; |
| 4136 | ln->lineno = 0; |
| 4137 | ln->idx = 0; |
| 4138 | |
| 4139 | if (!backtrace_vector_release (state, &vec.vec, error_callback, data)) |
| 4140 | goto fail; |
| 4141 | |
| 4142 | ln = (struct line *) vec.vec.base; |
| 4143 | backtrace_qsort (ln, vec.count, sizeof (struct line), line_compare); |
| 4144 | |
| 4145 | *lines = ln; |
| 4146 | *lines_count = vec.count; |
| 4147 | |
| 4148 | return 1; |
| 4149 | |
| 4150 | fail: |
| 4151 | backtrace_vector_free (state, &vec.vec, error_callback, data); |
| 4152 | free_line_header (state, hdr, error_callback, data); |
| 4153 | *lines = (struct line *) (uintptr_t) -1; |
| 4154 | *lines_count = 0; |
| 4155 | return 0; |
| 4156 | } |
| 4157 | |
| 4158 | static const char *read_referenced_name (struct dwarf_data *, struct unit *, |
| 4159 | uint64_t, backtrace_error_callback, |
| 4160 | void *); |
| 4161 | |
| 4162 | /* Read the name of a function from a DIE referenced by ATTR with VAL. */ |
| 4163 | |
| 4164 | static const char * |
| 4165 | read_referenced_name_from_attr (struct dwarf_data *ddata, struct unit *u, |
| 4166 | struct attr *attr, struct attr_val *val, |
| 4167 | backtrace_error_callback error_callback, |
| 4168 | void *data) |
| 4169 | { |
| 4170 | switch (attr->name) |
| 4171 | { |
| 4172 | case DW_AT_abstract_origin: |
| 4173 | case DW_AT_specification: |
| 4174 | break; |
| 4175 | default: |
| 4176 | return NULL; |
| 4177 | } |
| 4178 | |
| 4179 | if (attr->form == DW_FORM_ref_sig8) |
| 4180 | return NULL; |
| 4181 | |
| 4182 | if (val->encoding == ATTR_VAL_REF_INFO) |
| 4183 | { |
| 4184 | struct unit *unit |
| 4185 | = find_unit (ddata->units, ddata->units_count, |
| 4186 | val->u.uint); |
| 4187 | if (unit == NULL) |
| 4188 | return NULL; |
| 4189 | |
| 4190 | uint64_t offset = val->u.uint - unit->low_offset; |
| 4191 | return read_referenced_name (ddata, unit, offset, error_callback, data); |
| 4192 | } |
| 4193 | |
| 4194 | if (val->encoding == ATTR_VAL_UINT |
| 4195 | || val->encoding == ATTR_VAL_REF_UNIT) |
| 4196 | return read_referenced_name (ddata, u, val->u.uint, error_callback, data); |
| 4197 | |
| 4198 | if (val->encoding == ATTR_VAL_REF_ALT_INFO) |
| 4199 | { |
| 4200 | struct unit *alt_unit |
| 4201 | = find_unit (ddata->altlink->units, ddata->altlink->units_count, |
| 4202 | val->u.uint); |
| 4203 | if (alt_unit == NULL) |
| 4204 | return NULL; |
| 4205 | |
| 4206 | uint64_t offset = val->u.uint - alt_unit->low_offset; |
| 4207 | return read_referenced_name (ddata->altlink, alt_unit, offset, |
| 4208 | error_callback, data); |
| 4209 | } |
| 4210 | |
| 4211 | return NULL; |
| 4212 | } |
| 4213 | |
| 4214 | /* Read the name of a function from a DIE referenced by a |
| 4215 | DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within |
| 4216 | the same compilation unit. */ |
| 4217 | |
| 4218 | static const char * |
| 4219 | read_referenced_name (struct dwarf_data *ddata, struct unit *u, |
| 4220 | uint64_t offset, backtrace_error_callback error_callback, |
| 4221 | void *data) |
| 4222 | { |
| 4223 | struct dwarf_buf unit_buf; |
| 4224 | uint64_t code; |
| 4225 | const struct abbrev *abbrev; |
| 4226 | const char *ret; |
| 4227 | size_t i; |
| 4228 | |
| 4229 | /* OFFSET is from the start of the data for this compilation unit. |
| 4230 | U->unit_data is the data, but it starts U->unit_data_offset bytes |
| 4231 | from the beginning. */ |
| 4232 | |
| 4233 | if (offset < u->unit_data_offset |
| 4234 | || offset - u->unit_data_offset >= u->unit_data_len) |
| 4235 | { |
| 4236 | error_callback (data, |
| 4237 | "abstract origin or specification out of range", |
| 4238 | 0); |
| 4239 | return NULL; |
| 4240 | } |
| 4241 | |
| 4242 | offset -= u->unit_data_offset; |
| 4243 | |
| 4244 | unit_buf.name = ".debug_info"; |
| 4245 | unit_buf.start = ddata->dwarf_sections.data[DEBUG_INFO]; |
| 4246 | unit_buf.buf = u->unit_data + offset; |
| 4247 | unit_buf.left = u->unit_data_len - offset; |
| 4248 | unit_buf.is_bigendian = ddata->is_bigendian; |
| 4249 | unit_buf.error_callback = error_callback; |
| 4250 | unit_buf.data = data; |
| 4251 | unit_buf.reported_underflow = 0; |
| 4252 | |
| 4253 | code = read_uleb128 (&unit_buf); |
| 4254 | if (code == 0) |
| 4255 | { |
| 4256 | dwarf_buf_error (&unit_buf, |
| 4257 | "invalid abstract origin or specification", |
| 4258 | 0); |
| 4259 | return NULL; |
| 4260 | } |
| 4261 | |
| 4262 | abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data); |
| 4263 | if (abbrev == NULL) |
| 4264 | return NULL; |
| 4265 | |
| 4266 | ret = NULL; |
| 4267 | for (i = 0; i < abbrev->num_attrs; ++i) |
| 4268 | { |
| 4269 | struct attr_val val; |
| 4270 | |
| 4271 | if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val, |
| 4272 | &unit_buf, u->is_dwarf64, u->version, u->addrsize, |
| 4273 | &ddata->dwarf_sections, ddata->altlink, &val)) |
| 4274 | return NULL; |
| 4275 | |
| 4276 | switch (abbrev->attrs[i].name) |
| 4277 | { |
| 4278 | case DW_AT_name: |
| 4279 | /* Third name preference: don't override. A name we found in some |
| 4280 | other way, will normally be more useful -- e.g., this name is |
| 4281 | normally not mangled. */ |
| 4282 | if (ret != NULL) |
| 4283 | break; |
| 4284 | if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64, |
| 4285 | ddata->is_bigendian, u->str_offsets_base, |
| 4286 | &val, error_callback, data, &ret)) |
| 4287 | return NULL; |
| 4288 | break; |
| 4289 | |
| 4290 | case DW_AT_linkage_name: |
| 4291 | case DW_AT_MIPS_linkage_name: |
| 4292 | /* First name preference: override all. */ |
| 4293 | { |
| 4294 | const char *s; |
| 4295 | |
| 4296 | s = NULL; |
| 4297 | if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64, |
| 4298 | ddata->is_bigendian, u->str_offsets_base, |
| 4299 | &val, error_callback, data, &s)) |
| 4300 | return NULL; |
| 4301 | if (s != NULL) |
| 4302 | return s; |
| 4303 | } |
| 4304 | break; |
| 4305 | |
| 4306 | case DW_AT_specification: |
| 4307 | /* Second name preference: override DW_AT_name, don't override |
| 4308 | DW_AT_linkage_name. */ |
| 4309 | { |
| 4310 | const char *name; |
| 4311 | |
| 4312 | name = read_referenced_name_from_attr (ddata, u, &abbrev->attrs[i], |
| 4313 | &val, error_callback, data); |
| 4314 | if (name != NULL) |
| 4315 | ret = name; |
| 4316 | } |
| 4317 | break; |
| 4318 | |
| 4319 | default: |
| 4320 | break; |
| 4321 | } |
| 4322 | } |
| 4323 | |
| 4324 | return ret; |
| 4325 | } |
| 4326 | |
| 4327 | /* Add a range to a unit that maps to a function. This is called via |
| 4328 | add_ranges. Returns 1 on success, 0 on error. */ |
| 4329 | |
| 4330 | static int |
| 4331 | add_function_range (struct backtrace_state *state, void *rdata, |
| 4332 | uintptr_t lowpc, uintptr_t highpc, |
| 4333 | backtrace_error_callback error_callback, void *data, |
| 4334 | void *pvec) |
| 4335 | { |
| 4336 | struct function *function = (struct function *) rdata; |
| 4337 | struct function_vector *vec = (struct function_vector *) pvec; |
| 4338 | struct function_addrs *p; |
| 4339 | |
| 4340 | if (vec->count > 0) |
| 4341 | { |
| 4342 | p = (struct function_addrs *) vec->vec.base + (vec->count - 1); |
| 4343 | if ((lowpc == p->high || lowpc == p->high + 1) |
| 4344 | && function == p->function) |
| 4345 | { |
| 4346 | if (highpc > p->high) |
| 4347 | p->high = highpc; |
| 4348 | return 1; |
| 4349 | } |
| 4350 | } |
| 4351 | |
| 4352 | p = ((struct function_addrs *) |
| 4353 | backtrace_vector_grow (state, sizeof (struct function_addrs), |
| 4354 | error_callback, data, &vec->vec)); |
| 4355 | if (p == NULL) |
| 4356 | return 0; |
| 4357 | |
| 4358 | p->low = lowpc; |
| 4359 | p->high = highpc; |
| 4360 | p->function = function; |
| 4361 | |
| 4362 | ++vec->count; |
| 4363 | |
| 4364 | return 1; |
| 4365 | } |
| 4366 | |
| 4367 | /* Read one entry plus all its children. Add function addresses to |
| 4368 | VEC. Returns 1 on success, 0 on error. */ |
| 4369 | |
| 4370 | static int |
| 4371 | read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata, |
| 4372 | struct unit *u, uintptr_t base, struct dwarf_buf *unit_buf, |
| 4373 | const struct line_header *lhdr, |
| 4374 | backtrace_error_callback error_callback, void *data, |
| 4375 | struct function_vector *vec_function, |
| 4376 | struct function_vector *vec_inlined) |
| 4377 | { |
| 4378 | while (unit_buf->left > 0) |
| 4379 | { |
| 4380 | uint64_t code; |
| 4381 | const struct abbrev *abbrev; |
| 4382 | int is_function; |
| 4383 | struct function *function; |
| 4384 | struct function_vector *vec; |
| 4385 | size_t i; |
| 4386 | struct pcrange pcrange; |
| 4387 | int have_linkage_name; |
| 4388 | |
| 4389 | code = read_uleb128 (unit_buf); |
| 4390 | if (code == 0) |
| 4391 | return 1; |
| 4392 | |
| 4393 | abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data); |
| 4394 | if (abbrev == NULL) |
| 4395 | return 0; |
| 4396 | |
| 4397 | is_function = (abbrev->tag == DW_TAG_subprogram |
| 4398 | || abbrev->tag == DW_TAG_entry_point |
| 4399 | || abbrev->tag == DW_TAG_inlined_subroutine); |
| 4400 | |
| 4401 | if (abbrev->tag == DW_TAG_inlined_subroutine) |
| 4402 | vec = vec_inlined; |
| 4403 | else |
| 4404 | vec = vec_function; |
| 4405 | |
| 4406 | function = NULL; |
| 4407 | if (is_function) |
| 4408 | { |
| 4409 | function = ((struct function *) |
| 4410 | backtrace_alloc (state, sizeof *function, |
| 4411 | error_callback, data)); |
| 4412 | if (function == NULL) |
| 4413 | return 0; |
| 4414 | memset (function, 0, sizeof *function); |
| 4415 | } |
| 4416 | |
| 4417 | memset (&pcrange, 0, sizeof pcrange); |
| 4418 | have_linkage_name = 0; |
| 4419 | for (i = 0; i < abbrev->num_attrs; ++i) |
| 4420 | { |
| 4421 | struct attr_val val; |
| 4422 | |
| 4423 | if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val, |
| 4424 | unit_buf, u->is_dwarf64, u->version, |
| 4425 | u->addrsize, &ddata->dwarf_sections, |
| 4426 | ddata->altlink, &val)) |
| 4427 | return 0; |
| 4428 | |
| 4429 | /* The compile unit sets the base address for any address |
| 4430 | ranges in the function entries. */ |
| 4431 | if ((abbrev->tag == DW_TAG_compile_unit |
| 4432 | || abbrev->tag == DW_TAG_skeleton_unit) |
| 4433 | && abbrev->attrs[i].name == DW_AT_low_pc) |
| 4434 | { |
| 4435 | if (val.encoding == ATTR_VAL_ADDRESS) |
| 4436 | base = (uintptr_t) val.u.uint; |
| 4437 | else if (val.encoding == ATTR_VAL_ADDRESS_INDEX) |
| 4438 | { |
| 4439 | if (!resolve_addr_index (&ddata->dwarf_sections, |
| 4440 | u->addr_base, u->addrsize, |
| 4441 | ddata->is_bigendian, val.u.uint, |
| 4442 | error_callback, data, &base)) |
| 4443 | return 0; |
| 4444 | } |
| 4445 | } |
| 4446 | |
| 4447 | if (is_function) |
| 4448 | { |
| 4449 | switch (abbrev->attrs[i].name) |
| 4450 | { |
| 4451 | case DW_AT_call_file: |
| 4452 | if (val.encoding == ATTR_VAL_UINT) |
| 4453 | { |
| 4454 | if (val.u.uint >= lhdr->filenames_count) |
| 4455 | { |
| 4456 | dwarf_buf_error (unit_buf, |
| 4457 | ("invalid file number in " |
| 4458 | "DW_AT_call_file attribute"), |
| 4459 | 0); |
| 4460 | return 0; |
| 4461 | } |
| 4462 | function->caller_filename = lhdr->filenames[val.u.uint]; |
| 4463 | } |
| 4464 | break; |
| 4465 | |
| 4466 | case DW_AT_call_line: |
| 4467 | if (val.encoding == ATTR_VAL_UINT) |
| 4468 | function->caller_lineno = val.u.uint; |
| 4469 | break; |
| 4470 | |
| 4471 | case DW_AT_abstract_origin: |
| 4472 | case DW_AT_specification: |
| 4473 | /* Second name preference: override DW_AT_name, don't override |
| 4474 | DW_AT_linkage_name. */ |
| 4475 | if (have_linkage_name) |
| 4476 | break; |
| 4477 | { |
| 4478 | const char *name; |
| 4479 | |
| 4480 | name |
| 4481 | = read_referenced_name_from_attr (ddata, u, |
| 4482 | &abbrev->attrs[i], &val, |
| 4483 | error_callback, data); |
| 4484 | if (name != NULL) |
| 4485 | function->name = name; |
| 4486 | } |
| 4487 | break; |
| 4488 | |
| 4489 | case DW_AT_name: |
| 4490 | /* Third name preference: don't override. */ |
| 4491 | if (function->name != NULL) |
| 4492 | break; |
| 4493 | if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64, |
| 4494 | ddata->is_bigendian, |
| 4495 | u->str_offsets_base, &val, |
| 4496 | error_callback, data, &function->name)) |
| 4497 | return 0; |
| 4498 | break; |
| 4499 | |
| 4500 | case DW_AT_linkage_name: |
| 4501 | case DW_AT_MIPS_linkage_name: |
| 4502 | /* First name preference: override all. */ |
| 4503 | { |
| 4504 | const char *s; |
| 4505 | |
| 4506 | s = NULL; |
| 4507 | if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64, |
| 4508 | ddata->is_bigendian, |
| 4509 | u->str_offsets_base, &val, |
| 4510 | error_callback, data, &s)) |
| 4511 | return 0; |
| 4512 | if (s != NULL) |
| 4513 | { |
| 4514 | function->name = s; |
| 4515 | have_linkage_name = 1; |
| 4516 | } |
| 4517 | } |
| 4518 | break; |
| 4519 | |
| 4520 | case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges: |
| 4521 | update_pcrange (&abbrev->attrs[i], &val, &pcrange); |
| 4522 | break; |
| 4523 | |
| 4524 | default: |
| 4525 | break; |
| 4526 | } |
| 4527 | } |
| 4528 | } |
| 4529 | |
| 4530 | /* If we couldn't find a name for the function, we have no use |
| 4531 | for it. */ |
| 4532 | if (is_function && function->name == NULL) |
| 4533 | { |
| 4534 | backtrace_free (state, function, sizeof *function, |
| 4535 | error_callback, data); |
| 4536 | is_function = 0; |
| 4537 | } |
| 4538 | |
| 4539 | if (is_function) |
| 4540 | { |
| 4541 | if (pcrange.have_ranges |
| 4542 | || (pcrange.have_lowpc && pcrange.have_highpc)) |
| 4543 | { |
| 4544 | if (!add_ranges (state, &ddata->dwarf_sections, |
| 4545 | ddata->base_address, ddata->is_bigendian, |
| 4546 | u, base, &pcrange, add_function_range, |
| 4547 | (void *) function, error_callback, data, |
| 4548 | (void *) vec)) |
| 4549 | return 0; |
| 4550 | } |
| 4551 | else |
| 4552 | { |
| 4553 | backtrace_free (state, function, sizeof *function, |
| 4554 | error_callback, data); |
| 4555 | is_function = 0; |
| 4556 | } |
| 4557 | } |
| 4558 | |
| 4559 | if (abbrev->has_children) |
| 4560 | { |
| 4561 | if (!is_function) |
| 4562 | { |
| 4563 | if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr, |
| 4564 | error_callback, data, vec_function, |
| 4565 | vec_inlined)) |
| 4566 | return 0; |
| 4567 | } |
| 4568 | else |
| 4569 | { |
| 4570 | struct function_vector fvec; |
| 4571 | |
| 4572 | /* Gather any information for inlined functions in |
| 4573 | FVEC. */ |
| 4574 | |
| 4575 | memset (&fvec, 0, sizeof fvec); |
| 4576 | |
| 4577 | if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr, |
| 4578 | error_callback, data, vec_function, |
| 4579 | &fvec)) |
| 4580 | return 0; |
| 4581 | |
| 4582 | if (fvec.count > 0) |
| 4583 | { |
| 4584 | struct function_addrs *p; |
| 4585 | struct function_addrs *faddrs; |
| 4586 | |
| 4587 | /* Allocate a trailing entry, but don't include it |
| 4588 | in fvec.count. */ |
| 4589 | p = ((struct function_addrs *) |
| 4590 | backtrace_vector_grow (state, |
| 4591 | sizeof (struct function_addrs), |
| 4592 | error_callback, data, |
| 4593 | &fvec.vec)); |
| 4594 | if (p == NULL) |
| 4595 | return 0; |
| 4596 | p->low = 0; |
| 4597 | --p->low; |
| 4598 | p->high = p->low; |
| 4599 | p->function = NULL; |
| 4600 | |
| 4601 | if (!backtrace_vector_release (state, &fvec.vec, |
| 4602 | error_callback, data)) |
| 4603 | return 0; |
| 4604 | |
| 4605 | faddrs = (struct function_addrs *) fvec.vec.base; |
| 4606 | backtrace_qsort (faddrs, fvec.count, |
| 4607 | sizeof (struct function_addrs), |
| 4608 | function_addrs_compare); |
| 4609 | |
| 4610 | function->function_addrs = faddrs; |
| 4611 | function->function_addrs_count = fvec.count; |
| 4612 | } |
| 4613 | } |
| 4614 | } |
| 4615 | } |
| 4616 | |
| 4617 | return 1; |
| 4618 | } |
| 4619 | |
| 4620 | /* Read function name information for a compilation unit. We look |
| 4621 | through the whole unit looking for function tags. */ |
| 4622 | |
| 4623 | static void |
| 4624 | read_function_info (struct backtrace_state *state, struct dwarf_data *ddata, |
| 4625 | const struct line_header *lhdr, |
| 4626 | backtrace_error_callback error_callback, void *data, |
| 4627 | struct unit *u, struct function_vector *fvec, |
| 4628 | struct function_addrs **ret_addrs, |
| 4629 | size_t *ret_addrs_count) |
| 4630 | { |
| 4631 | struct function_vector lvec; |
| 4632 | struct function_vector *pfvec; |
| 4633 | struct dwarf_buf unit_buf; |
| 4634 | struct function_addrs *p; |
| 4635 | struct function_addrs *addrs; |
| 4636 | size_t addrs_count; |
| 4637 | |
| 4638 | /* Use FVEC if it is not NULL. Otherwise use our own vector. */ |
| 4639 | if (fvec != NULL) |
| 4640 | pfvec = fvec; |
| 4641 | else |
| 4642 | { |
| 4643 | memset (&lvec, 0, sizeof lvec); |
| 4644 | pfvec = &lvec; |
| 4645 | } |
| 4646 | |
| 4647 | unit_buf.name = ".debug_info"; |
| 4648 | unit_buf.start = ddata->dwarf_sections.data[DEBUG_INFO]; |
| 4649 | unit_buf.buf = u->unit_data; |
| 4650 | unit_buf.left = u->unit_data_len; |
| 4651 | unit_buf.is_bigendian = ddata->is_bigendian; |
| 4652 | unit_buf.error_callback = error_callback; |
| 4653 | unit_buf.data = data; |
| 4654 | unit_buf.reported_underflow = 0; |
| 4655 | |
| 4656 | while (unit_buf.left > 0) |
| 4657 | { |
| 4658 | if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr, |
| 4659 | error_callback, data, pfvec, pfvec)) |
| 4660 | return; |
| 4661 | } |
| 4662 | |
| 4663 | if (pfvec->count == 0) |
| 4664 | return; |
| 4665 | |
| 4666 | /* Allocate a trailing entry, but don't include it in |
| 4667 | pfvec->count. */ |
| 4668 | p = ((struct function_addrs *) |
| 4669 | backtrace_vector_grow (state, sizeof (struct function_addrs), |
| 4670 | error_callback, data, &pfvec->vec)); |
| 4671 | if (p == NULL) |
| 4672 | return; |
| 4673 | p->low = 0; |
| 4674 | --p->low; |
| 4675 | p->high = p->low; |
| 4676 | p->function = NULL; |
| 4677 | |
| 4678 | addrs_count = pfvec->count; |
| 4679 | |
| 4680 | if (fvec == NULL) |
| 4681 | { |
| 4682 | if (!backtrace_vector_release (state, &lvec.vec, error_callback, data)) |
| 4683 | return; |
| 4684 | addrs = (struct function_addrs *) pfvec->vec.base; |
| 4685 | } |
| 4686 | else |
| 4687 | { |
| 4688 | /* Finish this list of addresses, but leave the remaining space in |
| 4689 | the vector available for the next function unit. */ |
| 4690 | addrs = ((struct function_addrs *) |
| 4691 | backtrace_vector_finish (state, &fvec->vec, |
| 4692 | error_callback, data)); |
| 4693 | if (addrs == NULL) |
| 4694 | return; |
| 4695 | fvec->count = 0; |
| 4696 | } |
| 4697 | |
| 4698 | backtrace_qsort (addrs, addrs_count, sizeof (struct function_addrs), |
| 4699 | function_addrs_compare); |
| 4700 | |
| 4701 | *ret_addrs = addrs; |
| 4702 | *ret_addrs_count = addrs_count; |
| 4703 | } |
| 4704 | |
| 4705 | /* See if PC is inlined in FUNCTION. If it is, print out the inlined |
| 4706 | information, and update FILENAME and LINENO for the caller. |
| 4707 | Returns whatever CALLBACK returns, or 0 to keep going. */ |
| 4708 | |
| 4709 | static int |
| 4710 | report_inlined_functions (uintptr_t pc, struct function *function, |
| 4711 | backtrace_full_callback callback, void *data, |
| 4712 | const char **filename, int *lineno) |
| 4713 | { |
| 4714 | struct function_addrs *p; |
| 4715 | struct function_addrs *match; |
| 4716 | struct function *inlined; |
| 4717 | int ret; |
| 4718 | |
| 4719 | if (function->function_addrs_count == 0) |
| 4720 | return 0; |
| 4721 | |
| 4722 | /* Our search isn't safe if pc == -1, as that is the sentinel |
| 4723 | value. */ |
| 4724 | if (pc + 1 == 0) |
| 4725 | return 0; |
| 4726 | |
| 4727 | p = ((struct function_addrs *) |
| 4728 | bsearch (&pc, function->function_addrs, |
| 4729 | function->function_addrs_count, |
| 4730 | sizeof (struct function_addrs), |
| 4731 | function_addrs_search)); |
| 4732 | if (p == NULL) |
| 4733 | return 0; |
| 4734 | |
| 4735 | /* Here pc >= p->low && pc < (p + 1)->low. The function_addrs are |
| 4736 | sorted by low, so if pc > p->low we are at the end of a range of |
| 4737 | function_addrs with the same low value. If pc == p->low walk |
| 4738 | forward to the end of the range with that low value. Then walk |
| 4739 | backward and use the first range that includes pc. */ |
| 4740 | while (pc == (p + 1)->low) |
| 4741 | ++p; |
| 4742 | match = NULL; |
| 4743 | while (1) |
| 4744 | { |
| 4745 | if (pc < p->high) |
| 4746 | { |
| 4747 | match = p; |
| 4748 | break; |
| 4749 | } |
| 4750 | if (p == function->function_addrs) |
| 4751 | break; |
| 4752 | if ((p - 1)->low < p->low) |
| 4753 | break; |
| 4754 | --p; |
| 4755 | } |
| 4756 | if (match == NULL) |
| 4757 | return 0; |
| 4758 | |
| 4759 | /* We found an inlined call. */ |
| 4760 | |
| 4761 | inlined = match->function; |
| 4762 | |
| 4763 | /* Report any calls inlined into this one. */ |
| 4764 | ret = report_inlined_functions (pc, inlined, callback, data, |
| 4765 | filename, lineno); |
| 4766 | if (ret != 0) |
| 4767 | return ret; |
| 4768 | |
| 4769 | /* Report this inlined call. */ |
| 4770 | ret = callback (data, pc, *filename, *lineno, inlined->name); |
| 4771 | if (ret != 0) |
| 4772 | return ret; |
| 4773 | |
| 4774 | /* Our caller will report the caller of the inlined function; tell |
| 4775 | it the appropriate filename and line number. */ |
| 4776 | *filename = inlined->caller_filename; |
| 4777 | *lineno = inlined->caller_lineno; |
| 4778 | |
| 4779 | return 0; |
| 4780 | } |
| 4781 | |
| 4782 | /* Look for a PC in the DWARF mapping for one module. On success, |
| 4783 | call CALLBACK and return whatever it returns. On error, call |
| 4784 | ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found, |
| 4785 | 0 if not. */ |
| 4786 | |
| 4787 | static int |
| 4788 | dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata, |
| 4789 | uintptr_t pc, backtrace_full_callback callback, |
| 4790 | backtrace_error_callback error_callback, void *data, |
| 4791 | int *found) |
| 4792 | { |
| 4793 | struct unit_addrs *entry; |
| 4794 | int found_entry; |
| 4795 | struct unit *u; |
| 4796 | int new_data; |
| 4797 | struct line *lines; |
| 4798 | struct line *ln; |
| 4799 | struct function_addrs *p; |
| 4800 | struct function_addrs *fmatch; |
| 4801 | struct function *function; |
| 4802 | const char *filename; |
| 4803 | int lineno; |
| 4804 | int ret; |
| 4805 | |
| 4806 | *found = 1; |
| 4807 | |
| 4808 | /* Find an address range that includes PC. Our search isn't safe if |
| 4809 | PC == -1, as we use that as a sentinel value, so skip the search |
| 4810 | in that case. */ |
| 4811 | entry = (ddata->addrs_count == 0 || pc + 1 == 0 |
| 4812 | ? NULL |
| 4813 | : bsearch (&pc, ddata->addrs, ddata->addrs_count, |
| 4814 | sizeof (struct unit_addrs), unit_addrs_search)); |
| 4815 | |
| 4816 | if (entry == NULL) |
| 4817 | { |
| 4818 | *found = 0; |
| 4819 | return 0; |
| 4820 | } |
| 4821 | |
| 4822 | /* Here pc >= entry->low && pc < (entry + 1)->low. The unit_addrs |
| 4823 | are sorted by low, so if pc > p->low we are at the end of a range |
| 4824 | of unit_addrs with the same low value. If pc == p->low walk |
| 4825 | forward to the end of the range with that low value. Then walk |
| 4826 | backward and use the first range that includes pc. */ |
| 4827 | while (pc == (entry + 1)->low) |
| 4828 | ++entry; |
| 4829 | found_entry = 0; |
| 4830 | while (1) |
| 4831 | { |
| 4832 | if (pc < entry->high) |
| 4833 | { |
| 4834 | found_entry = 1; |
| 4835 | break; |
| 4836 | } |
| 4837 | if (entry == ddata->addrs) |
| 4838 | break; |
| 4839 | if ((entry - 1)->low < entry->low) |
| 4840 | break; |
| 4841 | --entry; |
| 4842 | } |
| 4843 | if (!found_entry) |
| 4844 | { |
| 4845 | *found = 0; |
| 4846 | return 0; |
| 4847 | } |
| 4848 | |
| 4849 | /* We need the lines, lines_count, function_addrs, |
| 4850 | function_addrs_count fields of u. If they are not set, we need |
| 4851 | to set them. When running in threaded mode, we need to allow for |
| 4852 | the possibility that some other thread is setting them |
| 4853 | simultaneously. */ |
| 4854 | |
| 4855 | u = entry->u; |
| 4856 | lines = u->lines; |
| 4857 | |
| 4858 | /* Skip units with no useful line number information by walking |
| 4859 | backward. Useless line number information is marked by setting |
| 4860 | lines == -1. */ |
| 4861 | while (entry > ddata->addrs |
| 4862 | && pc >= (entry - 1)->low |
| 4863 | && pc < (entry - 1)->high) |
| 4864 | { |
| 4865 | if (state->threaded) |
| 4866 | lines = (struct line *) backtrace_atomic_load_pointer (&u->lines); |
| 4867 | |
| 4868 | if (lines != (struct line *) (uintptr_t) -1) |
| 4869 | break; |
| 4870 | |
| 4871 | --entry; |
| 4872 | |
| 4873 | u = entry->u; |
| 4874 | lines = u->lines; |
| 4875 | } |
| 4876 | |
| 4877 | if (state->threaded) |
| 4878 | lines = backtrace_atomic_load_pointer (&u->lines); |
| 4879 | |
| 4880 | new_data = 0; |
| 4881 | if (lines == NULL) |
| 4882 | { |
| 4883 | struct function_addrs *function_addrs; |
| 4884 | size_t function_addrs_count; |
| 4885 | struct line_header lhdr; |
| 4886 | size_t count; |
| 4887 | |
| 4888 | /* We have never read the line information for this unit. Read |
| 4889 | it now. */ |
| 4890 | |
| 4891 | function_addrs = NULL; |
| 4892 | function_addrs_count = 0; |
| 4893 | if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr, |
| 4894 | &lines, &count)) |
| 4895 | { |
| 4896 | struct function_vector *pfvec; |
| 4897 | |
| 4898 | /* If not threaded, reuse DDATA->FVEC for better memory |
| 4899 | consumption. */ |
| 4900 | if (state->threaded) |
| 4901 | pfvec = NULL; |
| 4902 | else |
| 4903 | pfvec = &ddata->fvec; |
| 4904 | read_function_info (state, ddata, &lhdr, error_callback, data, |
| 4905 | entry->u, pfvec, &function_addrs, |
| 4906 | &function_addrs_count); |
| 4907 | free_line_header (state, &lhdr, error_callback, data); |
| 4908 | new_data = 1; |
| 4909 | } |
| 4910 | |
| 4911 | /* Atomically store the information we just read into the unit. |
| 4912 | If another thread is simultaneously writing, it presumably |
| 4913 | read the same information, and we don't care which one we |
| 4914 | wind up with; we just leak the other one. We do have to |
| 4915 | write the lines field last, so that the acquire-loads above |
| 4916 | ensure that the other fields are set. */ |
| 4917 | |
| 4918 | if (!state->threaded) |
| 4919 | { |
| 4920 | u->lines_count = count; |
| 4921 | u->function_addrs = function_addrs; |
| 4922 | u->function_addrs_count = function_addrs_count; |
| 4923 | u->lines = lines; |
| 4924 | } |
| 4925 | else |
| 4926 | { |
| 4927 | backtrace_atomic_store_size_t (&u->lines_count, count); |
| 4928 | backtrace_atomic_store_pointer (&u->function_addrs, function_addrs); |
| 4929 | backtrace_atomic_store_size_t (&u->function_addrs_count, |
| 4930 | function_addrs_count); |
| 4931 | backtrace_atomic_store_pointer (&u->lines, lines); |
| 4932 | } |
| 4933 | } |
| 4934 | |
| 4935 | /* Now all fields of U have been initialized. */ |
| 4936 | |
| 4937 | if (lines == (struct line *) (uintptr_t) -1) |
| 4938 | { |
| 4939 | /* If reading the line number information failed in some way, |
| 4940 | try again to see if there is a better compilation unit for |
| 4941 | this PC. */ |
| 4942 | if (new_data) |
| 4943 | return dwarf_lookup_pc (state, ddata, pc, callback, error_callback, |
| 4944 | data, found); |
| 4945 | return callback (data, pc, NULL, 0, NULL); |
| 4946 | } |
| 4947 | |
| 4948 | /* Search for PC within this unit. */ |
| 4949 | |
| 4950 | ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count, |
| 4951 | sizeof (struct line), line_search); |
| 4952 | if (ln == NULL) |
| 4953 | { |
| 4954 | /* The PC is between the low_pc and high_pc attributes of the |
| 4955 | compilation unit, but no entry in the line table covers it. |
| 4956 | This implies that the start of the compilation unit has no |
| 4957 | line number information. */ |
| 4958 | |
| 4959 | if (entry->u->abs_filename == NULL) |
| 4960 | { |
| 4961 | const char *filename; |
| 4962 | |
| 4963 | filename = entry->u->filename; |
| 4964 | if (filename != NULL |
| 4965 | && !IS_ABSOLUTE_PATH (filename) |
| 4966 | && entry->u->comp_dir != NULL) |
| 4967 | { |
| 4968 | size_t filename_len; |
| 4969 | const char *dir; |
| 4970 | size_t dir_len; |
| 4971 | char *s; |
| 4972 | |
| 4973 | filename_len = strlen (filename); |
| 4974 | dir = entry->u->comp_dir; |
| 4975 | dir_len = strlen (dir); |
| 4976 | s = (char *) backtrace_alloc (state, dir_len + filename_len + 2, |
| 4977 | error_callback, data); |
| 4978 | if (s == NULL) |
| 4979 | { |
| 4980 | *found = 0; |
| 4981 | return 0; |
| 4982 | } |
| 4983 | memcpy (s, dir, dir_len); |
| 4984 | /* FIXME: Should use backslash if DOS file system. */ |
| 4985 | s[dir_len] = '/'; |
| 4986 | memcpy (s + dir_len + 1, filename, filename_len + 1); |
| 4987 | filename = s; |
| 4988 | } |
| 4989 | entry->u->abs_filename = filename; |
| 4990 | } |
| 4991 | |
| 4992 | return callback (data, pc, entry->u->abs_filename, 0, NULL); |
| 4993 | } |
| 4994 | |
| 4995 | /* Search for function name within this unit. */ |
| 4996 | |
| 4997 | if (entry->u->function_addrs_count == 0) |
| 4998 | return callback (data, pc, ln->filename, ln->lineno, NULL); |
| 4999 | |
| 5000 | p = ((struct function_addrs *) |
| 5001 | bsearch (&pc, entry->u->function_addrs, |
| 5002 | entry->u->function_addrs_count, |
| 5003 | sizeof (struct function_addrs), |
| 5004 | function_addrs_search)); |
| 5005 | if (p == NULL) |
| 5006 | return callback (data, pc, ln->filename, ln->lineno, NULL); |
| 5007 | |
| 5008 | /* Here pc >= p->low && pc < (p + 1)->low. The function_addrs are |
| 5009 | sorted by low, so if pc > p->low we are at the end of a range of |
| 5010 | function_addrs with the same low value. If pc == p->low walk |
| 5011 | forward to the end of the range with that low value. Then walk |
| 5012 | backward and use the first range that includes pc. */ |
| 5013 | while (pc == (p + 1)->low) |
| 5014 | ++p; |
| 5015 | fmatch = NULL; |
| 5016 | while (1) |
| 5017 | { |
| 5018 | if (pc < p->high) |
| 5019 | { |
| 5020 | fmatch = p; |
| 5021 | break; |
| 5022 | } |
| 5023 | if (p == entry->u->function_addrs) |
| 5024 | break; |
| 5025 | if ((p - 1)->low < p->low) |
| 5026 | break; |
| 5027 | --p; |
| 5028 | } |
| 5029 | if (fmatch == NULL) |
| 5030 | return callback (data, pc, ln->filename, ln->lineno, NULL); |
| 5031 | |
| 5032 | function = fmatch->function; |
| 5033 | |
| 5034 | filename = ln->filename; |
| 5035 | lineno = ln->lineno; |
| 5036 | |
| 5037 | ret = report_inlined_functions (pc, function, callback, data, |
| 5038 | &filename, &lineno); |
| 5039 | if (ret != 0) |
| 5040 | return ret; |
| 5041 | |
| 5042 | return callback (data, pc, filename, lineno, function->name); |
| 5043 | } |
| 5044 | |
| 5045 | |
| 5046 | /* Return the file/line information for a PC using the DWARF mapping |
| 5047 | we built earlier. */ |
| 5048 | |
| 5049 | static int |
| 5050 | dwarf_fileline (struct backtrace_state *state, uintptr_t pc, |
| 5051 | backtrace_full_callback callback, |
| 5052 | backtrace_error_callback error_callback, void *data) |
| 5053 | { |
| 5054 | struct dwarf_data *ddata; |
| 5055 | int found; |
| 5056 | int ret; |
| 5057 | |
| 5058 | if (!state->threaded) |
| 5059 | { |
| 5060 | for (ddata = (struct dwarf_data *) state->fileline_data; |
| 5061 | ddata != NULL; |
| 5062 | ddata = ddata->next) |
| 5063 | { |
| 5064 | ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback, |
| 5065 | data, &found); |
| 5066 | if (ret != 0 || found) |
| 5067 | return ret; |
| 5068 | } |
| 5069 | } |
| 5070 | else |
| 5071 | { |
| 5072 | struct dwarf_data **pp; |
| 5073 | |
| 5074 | pp = (struct dwarf_data **) (void *) &state->fileline_data; |
| 5075 | while (1) |
| 5076 | { |
| 5077 | ddata = backtrace_atomic_load_pointer (pp); |
| 5078 | if (ddata == NULL) |
| 5079 | break; |
| 5080 | |
| 5081 | ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback, |
| 5082 | data, &found); |
| 5083 | if (ret != 0 || found) |
| 5084 | return ret; |
| 5085 | |
| 5086 | pp = &ddata->next; |
| 5087 | } |
| 5088 | } |
| 5089 | |
| 5090 | /* FIXME: See if any libraries have been dlopen'ed. */ |
| 5091 | |
| 5092 | return callback (data, pc, NULL, 0, NULL); |
| 5093 | } |
| 5094 | |
| 5095 | /* Initialize our data structures from the DWARF debug info for a |
| 5096 | file. Return NULL on failure. */ |
| 5097 | |
| 5098 | static struct dwarf_data * |
| 5099 | build_dwarf_data (struct backtrace_state *state, |
| 5100 | struct libbacktrace_base_address base_address, |
| 5101 | const struct dwarf_sections *dwarf_sections, |
| 5102 | int is_bigendian, |
| 5103 | struct dwarf_data *altlink, |
| 5104 | backtrace_error_callback error_callback, |
| 5105 | void *data) |
| 5106 | { |
| 5107 | struct unit_addrs_vector addrs_vec; |
| 5108 | struct unit_vector units_vec; |
| 5109 | struct dwarf_data *fdata; |
| 5110 | |
| 5111 | if (!build_address_map (state, base_address, dwarf_sections, is_bigendian, |
| 5112 | altlink, error_callback, data, &addrs_vec, |
| 5113 | &units_vec)) |
| 5114 | return NULL; |
| 5115 | |
| 5116 | if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data)) |
| 5117 | return NULL; |
| 5118 | if (!backtrace_vector_release (state, &units_vec.vec, error_callback, data)) |
| 5119 | return NULL; |
| 5120 | |
| 5121 | backtrace_qsort ((struct unit_addrs *) addrs_vec.vec.base, addrs_vec.count, |
| 5122 | sizeof (struct unit_addrs), unit_addrs_compare); |
| 5123 | if (!resolve_unit_addrs_overlap (state, error_callback, data, &addrs_vec)) |
| 5124 | return NULL; |
| 5125 | |
| 5126 | /* No qsort for units required, already sorted. */ |
| 5127 | |
| 5128 | fdata = ((struct dwarf_data *) |
| 5129 | backtrace_alloc (state, sizeof (struct dwarf_data), |
| 5130 | error_callback, data)); |
| 5131 | if (fdata == NULL) |
| 5132 | return NULL; |
| 5133 | |
| 5134 | fdata->next = NULL; |
| 5135 | fdata->altlink = altlink; |
| 5136 | fdata->base_address = base_address; |
| 5137 | fdata->addrs = (struct unit_addrs *) addrs_vec.vec.base; |
| 5138 | fdata->addrs_count = addrs_vec.count; |
| 5139 | fdata->units = (struct unit **) units_vec.vec.base; |
| 5140 | fdata->units_count = units_vec.count; |
| 5141 | fdata->dwarf_sections = *dwarf_sections; |
| 5142 | fdata->is_bigendian = is_bigendian; |
| 5143 | memset (&fdata->fvec, 0, sizeof fdata->fvec); |
| 5144 | |
| 5145 | return fdata; |
| 5146 | } |
| 5147 | |
| 5148 | /* Build our data structures from the DWARF sections for a module. |
| 5149 | Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0 |
| 5150 | on failure. */ |
| 5151 | |
| 5152 | int |
| 5153 | backtrace_dwarf_add (struct backtrace_state *state, |
| 5154 | struct libbacktrace_base_address base_address, |
| 5155 | const struct dwarf_sections *dwarf_sections, |
| 5156 | int is_bigendian, |
| 5157 | struct dwarf_data *fileline_altlink, |
| 5158 | backtrace_error_callback error_callback, |
| 5159 | void *data, fileline *fileline_fn, |
| 5160 | struct dwarf_data **fileline_entry) |
| 5161 | { |
| 5162 | struct dwarf_data *fdata; |
| 5163 | |
| 5164 | fdata = build_dwarf_data (state, base_address, dwarf_sections, is_bigendian, |
| 5165 | fileline_altlink, error_callback, data); |
| 5166 | if (fdata == NULL) |
| 5167 | return 0; |
| 5168 | |
| 5169 | if (fileline_entry != NULL) |
| 5170 | *fileline_entry = fdata; |
| 5171 | |
| 5172 | if (!state->threaded) |
| 5173 | { |
| 5174 | struct dwarf_data **pp; |
| 5175 | |
| 5176 | for (pp = (struct dwarf_data **) (void *) &state->fileline_data; |
| 5177 | *pp != NULL; |
| 5178 | pp = &(*pp)->next) |
| 5179 | ; |
| 5180 | *pp = fdata; |
| 5181 | } |
| 5182 | else |
| 5183 | { |
| 5184 | while (1) |
| 5185 | { |
| 5186 | struct dwarf_data **pp; |
| 5187 | |
| 5188 | pp = (struct dwarf_data **) (void *) &state->fileline_data; |
| 5189 | |
| 5190 | while (1) |
| 5191 | { |
| 5192 | struct dwarf_data *p; |
| 5193 | |
| 5194 | p = backtrace_atomic_load_pointer (pp); |
| 5195 | |
| 5196 | if (p == NULL) |
| 5197 | break; |
| 5198 | |
| 5199 | pp = &p->next; |
| 5200 | } |
| 5201 | |
| 5202 | if (__sync_bool_compare_and_swap (pp, NULL, fdata)) |
| 5203 | break; |
| 5204 | } |
| 5205 | } |
| 5206 | |
| 5207 | *fileline_fn = dwarf_fileline; |
| 5208 | |
| 5209 | return 1; |
| 5210 | } |
| 5211 | // fileline.c: |
| 5212 | #include <sys/types.h> |
| 5213 | #include <sys/stat.h> |
| 5214 | #include <errno.h> |
| 5215 | #include <fcntl.h> |
| 5216 | #include <stdlib.h> |
| 5217 | #include <unistd.h> |
| 5218 | |
| 5219 | #if defined (HAVE_KERN_PROC_ARGS) || defined (HAVE_KERN_PROC) |
| 5220 | #include <sys/sysctl.h> |
| 5221 | #endif |
| 5222 | |
| 5223 | #ifdef HAVE_MACH_O_DYLD_H |
| 5224 | #include <mach-o/dyld.h> |
| 5225 | #endif |
| 5226 | |
| 5227 | #ifdef __hpux__ |
| 5228 | #include <dl.h> |
| 5229 | #endif |
| 5230 | |
| 5231 | #ifdef HAVE_WINDOWS_H |
| 5232 | #ifndef WIN32_LEAN_AND_MEAN |
| 5233 | #define WIN32_LEAN_AND_MEAN |
| 5234 | #endif |
| 5235 | |
| 5236 | #ifndef NOMINMAX |
| 5237 | #define NOMINMAX |
| 5238 | #endif |
| 5239 | |
| 5240 | #include <windows.h> |
| 5241 | #endif |
| 5242 | |
| 5243 | |
| 5244 | #ifndef HAVE_GETEXECNAME |
| 5245 | #define getexecname() NULL |
| 5246 | #endif |
| 5247 | |
| 5248 | #ifdef __hpux__ |
| 5249 | static char * |
| 5250 | hpux_get_executable_path (struct backtrace_state *state, |
| 5251 | backtrace_error_callback error_callback, void *data) |
| 5252 | { |
| 5253 | struct shl_descriptor *desc; |
| 5254 | size_t len = sizeof (struct shl_descriptor); |
| 5255 | |
| 5256 | desc = backtrace_alloc (state, len, error_callback, data); |
| 5257 | if (desc == NULL) |
| 5258 | return NULL; |
| 5259 | |
| 5260 | if (shl_get_r (0, desc) == -1) |
| 5261 | { |
| 5262 | backtrace_free (state, desc, len, error_callback, data); |
| 5263 | return NULL; |
| 5264 | } |
| 5265 | |
| 5266 | return desc->filename; |
| 5267 | } |
| 5268 | |
| 5269 | #else |
| 5270 | |
| 5271 | #define hpux_get_executable_path(state, error_callback, data) NULL |
| 5272 | |
| 5273 | #endif |
| 5274 | |
| 5275 | #if !defined (HAVE_KERN_PROC_ARGS) && !defined (HAVE_KERN_PROC) |
| 5276 | |
| 5277 | #define sysctl_exec_name1(state, error_callback, data) NULL |
| 5278 | #define sysctl_exec_name2(state, error_callback, data) NULL |
| 5279 | |
| 5280 | #else /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */ |
| 5281 | |
| 5282 | static char * |
| 5283 | sysctl_exec_name (struct backtrace_state *state, |
| 5284 | int mib0, int mib1, int mib2, int mib3, |
| 5285 | backtrace_error_callback error_callback, void *data) |
| 5286 | { |
| 5287 | int mib[4]; |
| 5288 | size_t len; |
| 5289 | char *name; |
| 5290 | size_t rlen; |
| 5291 | |
| 5292 | mib[0] = mib0; |
| 5293 | mib[1] = mib1; |
| 5294 | mib[2] = mib2; |
| 5295 | mib[3] = mib3; |
| 5296 | |
| 5297 | if (sysctl (mib, 4, NULL, &len, NULL, 0) < 0) |
| 5298 | return NULL; |
| 5299 | name = (char *) backtrace_alloc (state, len, error_callback, data); |
| 5300 | if (name == NULL) |
| 5301 | return NULL; |
| 5302 | rlen = len; |
| 5303 | if (sysctl (mib, 4, name, &rlen, NULL, 0) < 0) |
| 5304 | { |
| 5305 | backtrace_free (state, name, len, error_callback, data); |
| 5306 | return NULL; |
| 5307 | } |
| 5308 | return name; |
| 5309 | } |
| 5310 | |
| 5311 | #ifdef HAVE_KERN_PROC_ARGS |
| 5312 | |
| 5313 | static char * |
| 5314 | sysctl_exec_name1 (struct backtrace_state *state, |
| 5315 | backtrace_error_callback error_callback, void *data) |
| 5316 | { |
| 5317 | /* This variant is used on NetBSD. */ |
| 5318 | return sysctl_exec_name (state, CTL_KERN, KERN_PROC_ARGS, -1, |
| 5319 | KERN_PROC_PATHNAME, error_callback, data); |
| 5320 | } |
| 5321 | |
| 5322 | #else |
| 5323 | |
| 5324 | #define sysctl_exec_name1(state, error_callback, data) NULL |
| 5325 | |
| 5326 | #endif |
| 5327 | |
| 5328 | #ifdef HAVE_KERN_PROC |
| 5329 | |
| 5330 | static char * |
| 5331 | sysctl_exec_name2 (struct backtrace_state *state, |
| 5332 | backtrace_error_callback error_callback, void *data) |
| 5333 | { |
| 5334 | /* This variant is used on FreeBSD. */ |
| 5335 | return sysctl_exec_name (state, CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1, |
| 5336 | error_callback, data); |
| 5337 | } |
| 5338 | |
| 5339 | #else |
| 5340 | |
| 5341 | #define sysctl_exec_name2(state, error_callback, data) NULL |
| 5342 | |
| 5343 | #endif |
| 5344 | |
| 5345 | #endif /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */ |
| 5346 | |
| 5347 | #ifdef HAVE_MACH_O_DYLD_H |
| 5348 | |
| 5349 | static char * |
| 5350 | macho_get_executable_path (struct backtrace_state *state, |
| 5351 | backtrace_error_callback error_callback, void *data) |
| 5352 | { |
| 5353 | uint32_t len; |
| 5354 | char *name; |
| 5355 | |
| 5356 | len = 0; |
| 5357 | if (_NSGetExecutablePath (NULL, &len) == 0) |
| 5358 | return NULL; |
| 5359 | name = (char *) backtrace_alloc (state, len, error_callback, data); |
| 5360 | if (name == NULL) |
| 5361 | return NULL; |
| 5362 | if (_NSGetExecutablePath (name, &len) != 0) |
| 5363 | { |
| 5364 | backtrace_free (state, name, len, error_callback, data); |
| 5365 | return NULL; |
| 5366 | } |
| 5367 | return name; |
| 5368 | } |
| 5369 | |
| 5370 | #else /* !defined (HAVE_MACH_O_DYLD_H) */ |
| 5371 | |
| 5372 | #define macho_get_executable_path(state, error_callback, data) NULL |
| 5373 | |
| 5374 | #endif /* !defined (HAVE_MACH_O_DYLD_H) */ |
| 5375 | |
| 5376 | #if HAVE_DECL__PGMPTR |
| 5377 | |
| 5378 | #define windows_executable_filename() _pgmptr |
| 5379 | |
| 5380 | #else /* !HAVE_DECL__PGMPTR */ |
| 5381 | |
| 5382 | #define windows_executable_filename() NULL |
| 5383 | |
| 5384 | #endif /* !HAVE_DECL__PGMPTR */ |
| 5385 | |
| 5386 | #ifdef HAVE_WINDOWS_H |
| 5387 | |
| 5388 | #define FILENAME_BUF_SIZE (MAX_PATH) |
| 5389 | |
| 5390 | static char * |
| 5391 | windows_get_executable_path (char *buf, backtrace_error_callback error_callback, |
| 5392 | void *data) |
| 5393 | { |
| 5394 | size_t got; |
| 5395 | int error; |
| 5396 | |
| 5397 | got = GetModuleFileNameA (NULL, buf, FILENAME_BUF_SIZE - 1); |
| 5398 | error = GetLastError (); |
| 5399 | if (got == 0 |
| 5400 | || (got == FILENAME_BUF_SIZE - 1 && error == ERROR_INSUFFICIENT_BUFFER)) |
| 5401 | { |
| 5402 | error_callback (data, |
| 5403 | "could not get the filename of the current executable", |
| 5404 | error); |
| 5405 | return NULL; |
| 5406 | } |
| 5407 | return buf; |
| 5408 | } |
| 5409 | |
| 5410 | #else /* !defined (HAVE_WINDOWS_H) */ |
| 5411 | |
| 5412 | #define windows_get_executable_path(buf, error_callback, data) NULL |
| 5413 | #define FILENAME_BUF_SIZE 64 |
| 5414 | |
| 5415 | #endif /* !defined (HAVE_WINDOWS_H) */ |
| 5416 | |
| 5417 | /* Initialize the fileline information from the executable. Returns 1 |
| 5418 | on success, 0 on failure. */ |
| 5419 | |
| 5420 | static int |
| 5421 | fileline_initialize (struct backtrace_state *state, |
| 5422 | backtrace_error_callback error_callback, void *data) |
| 5423 | { |
| 5424 | int failed; |
| 5425 | fileline fileline_fn; |
| 5426 | int pass; |
| 5427 | int called_error_callback; |
| 5428 | int descriptor; |
| 5429 | const char *filename; |
| 5430 | char buf[FILENAME_BUF_SIZE]; |
| 5431 | |
| 5432 | if (!state->threaded) |
| 5433 | failed = state->fileline_initialization_failed; |
| 5434 | else |
| 5435 | failed = backtrace_atomic_load_int (&state->fileline_initialization_failed); |
| 5436 | |
| 5437 | if (failed) |
| 5438 | { |
| 5439 | error_callback (data, "failed to read executable information", -1); |
| 5440 | return 0; |
| 5441 | } |
| 5442 | |
| 5443 | if (!state->threaded) |
| 5444 | fileline_fn = state->fileline_fn; |
| 5445 | else |
| 5446 | fileline_fn = backtrace_atomic_load_pointer (&state->fileline_fn); |
| 5447 | if (fileline_fn != NULL) |
| 5448 | return 1; |
| 5449 | |
| 5450 | /* We have not initialized the information. Do it now. */ |
| 5451 | |
| 5452 | descriptor = -1; |
| 5453 | called_error_callback = 0; |
| 5454 | for (pass = 0; pass < 11; ++pass) |
| 5455 | { |
| 5456 | int does_not_exist; |
| 5457 | |
| 5458 | switch (pass) |
| 5459 | { |
| 5460 | case 0: |
| 5461 | filename = state->filename; |
| 5462 | break; |
| 5463 | case 1: |
| 5464 | filename = getexecname (); |
| 5465 | break; |
| 5466 | case 2: |
| 5467 | /* Test this before /proc/self/exe, as the latter exists but points |
| 5468 | to the wine binary (and thus doesn't work). */ |
| 5469 | filename = windows_executable_filename (); |
| 5470 | break; |
| 5471 | case 3: |
| 5472 | filename = "/proc/self/exe"; |
| 5473 | break; |
| 5474 | case 4: |
| 5475 | filename = "/proc/curproc/file"; |
| 5476 | break; |
| 5477 | case 5: |
| 5478 | snprintf (buf, sizeof (buf), "/proc/%ld/object/a.out", |
| 5479 | (long) getpid ()); |
| 5480 | filename = buf; |
| 5481 | break; |
| 5482 | case 6: |
| 5483 | filename = sysctl_exec_name1 (state, error_callback, data); |
| 5484 | break; |
| 5485 | case 7: |
| 5486 | filename = sysctl_exec_name2 (state, error_callback, data); |
| 5487 | break; |
| 5488 | case 8: |
| 5489 | filename = macho_get_executable_path (state, error_callback, data); |
| 5490 | break; |
| 5491 | case 9: |
| 5492 | filename = windows_get_executable_path (buf, error_callback, data); |
| 5493 | break; |
| 5494 | case 10: |
| 5495 | filename = hpux_get_executable_path (state, error_callback, data); |
| 5496 | break; |
| 5497 | default: |
| 5498 | abort (); |
| 5499 | } |
| 5500 | |
| 5501 | if (filename == NULL) |
| 5502 | continue; |
| 5503 | |
| 5504 | descriptor = backtrace_open (filename, error_callback, data, |
| 5505 | &does_not_exist); |
| 5506 | if (descriptor < 0 && !does_not_exist) |
| 5507 | { |
| 5508 | called_error_callback = 1; |
| 5509 | break; |
| 5510 | } |
| 5511 | if (descriptor >= 0) |
| 5512 | break; |
| 5513 | } |
| 5514 | |
| 5515 | if (descriptor < 0) |
| 5516 | { |
| 5517 | if (!called_error_callback) |
| 5518 | { |
| 5519 | if (state->filename != NULL) |
| 5520 | error_callback (data, state->filename, ENOENT); |
| 5521 | else |
| 5522 | error_callback (data, |
| 5523 | "libbacktrace could not find executable to open", |
| 5524 | 0); |
| 5525 | } |
| 5526 | failed = 1; |
| 5527 | } |
| 5528 | |
| 5529 | if (!failed) |
| 5530 | { |
| 5531 | if (!backtrace_initialize (state, filename, descriptor, error_callback, |
| 5532 | data, &fileline_fn)) |
| 5533 | failed = 1; |
| 5534 | } |
| 5535 | |
| 5536 | if (failed) |
| 5537 | { |
| 5538 | if (!state->threaded) |
| 5539 | state->fileline_initialization_failed = 1; |
| 5540 | else |
| 5541 | backtrace_atomic_store_int (&state->fileline_initialization_failed, 1); |
| 5542 | return 0; |
| 5543 | } |
| 5544 | |
| 5545 | if (!state->threaded) |
| 5546 | state->fileline_fn = fileline_fn; |
| 5547 | else |
| 5548 | { |
| 5549 | backtrace_atomic_store_pointer (&state->fileline_fn, fileline_fn); |
| 5550 | |
| 5551 | /* Note that if two threads initialize at once, one of the data |
| 5552 | sets may be leaked. */ |
| 5553 | } |
| 5554 | |
| 5555 | return 1; |
| 5556 | } |
| 5557 | |
| 5558 | /* Given a PC, find the file name, line number, and function name. */ |
| 5559 | |
| 5560 | int |
| 5561 | backtrace_pcinfo (struct backtrace_state *state, uintptr_t pc, |
| 5562 | backtrace_full_callback callback, |
| 5563 | backtrace_error_callback error_callback, void *data) |
| 5564 | { |
| 5565 | if (!fileline_initialize (state, error_callback, data)) |
| 5566 | return 0; |
| 5567 | |
| 5568 | if (state->fileline_initialization_failed) |
| 5569 | return 0; |
| 5570 | |
| 5571 | return state->fileline_fn (state, pc, callback, error_callback, data); |
| 5572 | } |
| 5573 | |
| 5574 | /* Given a PC, find the symbol for it, and its value. */ |
| 5575 | |
| 5576 | int |
| 5577 | backtrace_syminfo (struct backtrace_state *state, uintptr_t pc, |
| 5578 | backtrace_syminfo_callback callback, |
| 5579 | backtrace_error_callback error_callback, void *data) |
| 5580 | { |
| 5581 | if (!fileline_initialize (state, error_callback, data)) |
| 5582 | return 0; |
| 5583 | |
| 5584 | if (state->fileline_initialization_failed) |
| 5585 | return 0; |
| 5586 | |
| 5587 | state->syminfo_fn (state, pc, callback, error_callback, data); |
| 5588 | return 1; |
| 5589 | } |
| 5590 | |
| 5591 | /* A backtrace_syminfo_callback that can call into a |
| 5592 | backtrace_full_callback, used when we have a symbol table but no |
| 5593 | debug info. */ |
| 5594 | |
| 5595 | void |
| 5596 | backtrace_syminfo_to_full_callback (void *data, uintptr_t pc, |
| 5597 | const char *symname, |
| 5598 | uintptr_t symval ATTRIBUTE_UNUSED, |
| 5599 | uintptr_t symsize ATTRIBUTE_UNUSED) |
| 5600 | { |
| 5601 | struct backtrace_call_full *bdata = (struct backtrace_call_full *) data; |
| 5602 | |
| 5603 | bdata->ret = bdata->full_callback (bdata->full_data, pc, NULL, 0, symname); |
| 5604 | } |
| 5605 | |
| 5606 | /* An error callback that corresponds to |
| 5607 | backtrace_syminfo_to_full_callback. */ |
| 5608 | |
| 5609 | void |
| 5610 | backtrace_syminfo_to_full_error_callback (void *data, const char *msg, |
| 5611 | int errnum) |
| 5612 | { |
| 5613 | struct backtrace_call_full *bdata = (struct backtrace_call_full *) data; |
| 5614 | |
| 5615 | bdata->full_error_callback (bdata->full_data, msg, errnum); |
| 5616 | } |
| 5617 | // posix.c: |
| 5618 | #include <errno.h> |
| 5619 | #include <sys/types.h> |
| 5620 | #include <sys/stat.h> |
| 5621 | #include <fcntl.h> |
| 5622 | #include <unistd.h> |
| 5623 | |
| 5624 | |
| 5625 | #ifndef O_BINARY |
| 5626 | #define O_BINARY 0 |
| 5627 | #endif |
| 5628 | |
| 5629 | #ifndef O_CLOEXEC |
| 5630 | #define O_CLOEXEC 0 |
| 5631 | #endif |
| 5632 | |
| 5633 | #ifndef FD_CLOEXEC |
| 5634 | #define FD_CLOEXEC 1 |
| 5635 | #endif |
| 5636 | |
| 5637 | /* Open a file for reading. */ |
| 5638 | |
| 5639 | int |
| 5640 | backtrace_open (const char *filename, backtrace_error_callback error_callback, |
| 5641 | void *data, int *does_not_exist) |
| 5642 | { |
| 5643 | int descriptor; |
| 5644 | |
| 5645 | if (does_not_exist != NULL) |
| 5646 | *does_not_exist = 0; |
| 5647 | |
| 5648 | descriptor = open (filename, (int) (O_RDONLY | O_BINARY | O_CLOEXEC)); |
| 5649 | if (descriptor < 0) |
| 5650 | { |
| 5651 | /* If DOES_NOT_EXIST is not NULL, then don't call ERROR_CALLBACK |
| 5652 | if the file does not exist. We treat lacking permission to |
| 5653 | open the file as the file not existing; this case arises when |
| 5654 | running the libgo syscall package tests as root. */ |
| 5655 | if (does_not_exist != NULL && (errno == ENOENT || errno == EACCES)) |
| 5656 | *does_not_exist = 1; |
| 5657 | else |
| 5658 | error_callback (data, filename, errno); |
| 5659 | return -1; |
| 5660 | } |
| 5661 | |
| 5662 | #ifdef HAVE_FCNTL |
| 5663 | /* Set FD_CLOEXEC just in case the kernel does not support |
| 5664 | O_CLOEXEC. It doesn't matter if this fails for some reason. |
| 5665 | FIXME: At some point it should be safe to only do this if |
| 5666 | O_CLOEXEC == 0. */ |
| 5667 | fcntl (descriptor, F_SETFD, FD_CLOEXEC); |
| 5668 | #endif |
| 5669 | |
| 5670 | return descriptor; |
| 5671 | } |
| 5672 | |
| 5673 | /* Close DESCRIPTOR. */ |
| 5674 | |
| 5675 | int |
| 5676 | backtrace_close (int descriptor, backtrace_error_callback error_callback, |
| 5677 | void *data) |
| 5678 | { |
| 5679 | if (close (descriptor) < 0) |
| 5680 | { |
| 5681 | error_callback (data, "close", errno); |
| 5682 | return 0; |
| 5683 | } |
| 5684 | return 1; |
| 5685 | } |
| 5686 | // print.c: |
| 5687 | #include <stdio.h> |
| 5688 | #include <string.h> |
| 5689 | #include <sys/types.h> |
| 5690 | |
| 5691 | |
| 5692 | /* Passed to callbacks. */ |
| 5693 | |
| 5694 | struct print_data |
| 5695 | { |
| 5696 | struct backtrace_state *state; |
| 5697 | FILE *f; |
| 5698 | }; |
| 5699 | |
| 5700 | /* Print errors to stderr. */ |
| 5701 | |
| 5702 | static void |
| 5703 | error_callback (void *data, const char *msg, int errnum) |
| 5704 | { |
| 5705 | struct print_data *pdata = (struct print_data *) data; |
| 5706 | |
| 5707 | if (pdata->state->filename != NULL) |
| 5708 | fprintf (stderr, "%s: ", pdata->state->filename); |
| 5709 | fprintf (stderr, "libbacktrace: %s", msg); |
| 5710 | if (errnum > 0) |
| 5711 | fprintf (stderr, ": %s", strerror (errnum)); |
| 5712 | fputc ('\n', stderr); |
| 5713 | } |
| 5714 | |
| 5715 | /* Print one level of a backtrace if we couldn't get a file or function name. |
| 5716 | Use syminfo to try to get a symbol name. */ |
| 5717 | |
| 5718 | static void print_syminfo_callback (void *data, uintptr_t pc, |
| 5719 | const char *symname, uintptr_t symval, |
| 5720 | uintptr_t symsize ATTRIBUTE_UNUSED) |
| 5721 | { |
| 5722 | struct print_data *pdata = (struct print_data *) data; |
| 5723 | |
| 5724 | if (symname == NULL) |
| 5725 | fprintf (pdata->f, "0x%lx ???\n\t???:0\n", (unsigned long) pc); |
| 5726 | else |
| 5727 | fprintf (pdata->f, "0x%lx ???\n\t%s+0x%lx:0\n", |
| 5728 | (unsigned long) pc, |
| 5729 | symname, |
| 5730 | (unsigned long) (pc - symval)); |
| 5731 | } |
| 5732 | |
| 5733 | /* Print one level of a backtrace. */ |
| 5734 | |
| 5735 | static int |
| 5736 | print_callback (void *data, uintptr_t pc, const char *filename, int lineno, |
| 5737 | const char *function) |
| 5738 | { |
| 5739 | struct print_data *pdata = (struct print_data *) data; |
| 5740 | |
| 5741 | if (function == NULL && filename == NULL) |
| 5742 | { |
| 5743 | backtrace_syminfo (pdata->state, pc, print_syminfo_callback, |
| 5744 | error_callback, data); |
| 5745 | return 0; |
| 5746 | } |
| 5747 | |
| 5748 | fprintf (pdata->f, "0x%lx %s\n\t%s:%d\n", |
| 5749 | (unsigned long) pc, |
| 5750 | function == NULL ? "???" : function, |
| 5751 | filename == NULL ? "???" : filename, |
| 5752 | lineno); |
| 5753 | return 0; |
| 5754 | } |
| 5755 | |
| 5756 | /* Print a backtrace. */ |
| 5757 | |
| 5758 | void __attribute__((noinline)) |
| 5759 | backtrace_print (struct backtrace_state *state, int skip, FILE *f) |
| 5760 | { |
| 5761 | struct print_data data; |
| 5762 | |
| 5763 | data.state = state; |
| 5764 | data.f = f; |
| 5765 | backtrace_full (state, skip + 1, print_callback, error_callback, |
| 5766 | (void *) &data); |
| 5767 | } |
| 5768 | // sort.c: |
| 5769 | #include <stddef.h> |
| 5770 | #include <sys/types.h> |
| 5771 | |
| 5772 | |
| 5773 | /* The GNU glibc version of qsort allocates memory, which we must not |
| 5774 | do if we are invoked by a signal handler. So provide our own |
| 5775 | sort. */ |
| 5776 | |
| 5777 | static void |
| 5778 | swap (char *a, char *b, size_t size) |
| 5779 | { |
| 5780 | size_t i; |
| 5781 | |
| 5782 | for (i = 0; i < size; i++, a++, b++) |
| 5783 | { |
| 5784 | char t; |
| 5785 | |
| 5786 | t = *a; |
| 5787 | *a = *b; |
| 5788 | *b = t; |
| 5789 | } |
| 5790 | } |
| 5791 | |
| 5792 | void |
| 5793 | backtrace_qsort (void *basearg, size_t count, size_t size, |
| 5794 | int (*compar) (const void *, const void *)) |
| 5795 | { |
| 5796 | char *base = (char *) basearg; |
| 5797 | size_t i; |
| 5798 | size_t mid; |
| 5799 | |
| 5800 | tail_recurse: |
| 5801 | if (count < 2) |
| 5802 | return; |
| 5803 | |
| 5804 | /* The symbol table and DWARF tables, which is all we use this |
| 5805 | routine for, tend to be roughly sorted. Pick the middle element |
| 5806 | in the array as our pivot point, so that we are more likely to |
| 5807 | cut the array in half for each recursion step. */ |
| 5808 | swap (base, base + (count / 2) * size, size); |
| 5809 | |
| 5810 | mid = 0; |
| 5811 | for (i = 1; i < count; i++) |
| 5812 | { |
| 5813 | if ((*compar) (base, base + i * size) > 0) |
| 5814 | { |
| 5815 | ++mid; |
| 5816 | if (i != mid) |
| 5817 | swap (base + mid * size, base + i * size, size); |
| 5818 | } |
| 5819 | } |
| 5820 | |
| 5821 | if (mid > 0) |
| 5822 | swap (base, base + mid * size, size); |
| 5823 | |
| 5824 | /* Recurse with the smaller array, loop with the larger one. That |
| 5825 | ensures that our maximum stack depth is log count. */ |
| 5826 | if (2 * mid < count) |
| 5827 | { |
| 5828 | backtrace_qsort (base, mid, size, compar); |
| 5829 | base += (mid + 1) * size; |
| 5830 | count -= mid + 1; |
| 5831 | goto tail_recurse; |
| 5832 | } |
| 5833 | else |
| 5834 | { |
| 5835 | backtrace_qsort (base + (mid + 1) * size, count - (mid + 1), |
| 5836 | size, compar); |
| 5837 | count = mid; |
| 5838 | goto tail_recurse; |
| 5839 | } |
| 5840 | } |
| 5841 | // state.c: |
| 5842 | #include <string.h> |
| 5843 | #include <sys/types.h> |
| 5844 | |
| 5845 | |
| 5846 | /* Create the backtrace state. This will then be passed to all the |
| 5847 | other routines. */ |
| 5848 | |
| 5849 | struct backtrace_state * |
| 5850 | backtrace_create_state (const char *filename, int threaded, |
| 5851 | backtrace_error_callback error_callback, |
| 5852 | void *data) |
| 5853 | { |
| 5854 | struct backtrace_state init_state; |
| 5855 | struct backtrace_state *state; |
| 5856 | |
| 5857 | #ifndef HAVE_SYNC_FUNCTIONS |
| 5858 | if (threaded) |
| 5859 | { |
| 5860 | error_callback (data, "backtrace library does not support threads", 0); |
| 5861 | return NULL; |
| 5862 | } |
| 5863 | #endif |
| 5864 | |
| 5865 | memset (&init_state, 0, sizeof init_state); |
| 5866 | init_state.filename = filename; |
| 5867 | init_state.threaded = threaded; |
| 5868 | |
| 5869 | state = ((struct backtrace_state *) |
| 5870 | backtrace_alloc (&init_state, sizeof *state, error_callback, data)); |
| 5871 | if (state == NULL) |
| 5872 | return NULL; |
| 5873 | *state = init_state; |
| 5874 | |
| 5875 | return state; |
| 5876 | } |
| 5877 | // backtrace.c: |
| 5878 | #include <sys/types.h> |
| 5879 | |
| 5880 | #include <unwind.h> |
| 5881 | |
| 5882 | /* The main backtrace_full routine. */ |
| 5883 | |
| 5884 | /* Data passed through _Unwind_Backtrace. */ |
| 5885 | |
| 5886 | struct backtrace_data |
| 5887 | { |
| 5888 | /* Number of frames to skip. */ |
| 5889 | int skip; |
| 5890 | /* Library state. */ |
| 5891 | struct backtrace_state *state; |
| 5892 | /* Callback routine. */ |
| 5893 | backtrace_full_callback callback; |
| 5894 | /* Error callback routine. */ |
| 5895 | backtrace_error_callback error_callback; |
| 5896 | /* Data to pass to callback routines. */ |
| 5897 | void *data; |
| 5898 | /* Value to return from backtrace_full. */ |
| 5899 | int ret; |
| 5900 | /* Whether there is any memory available. */ |
| 5901 | int can_alloc; |
| 5902 | }; |
| 5903 | |
| 5904 | /* Unwind library callback routine. This is passed to |
| 5905 | _Unwind_Backtrace. */ |
| 5906 | |
| 5907 | static _Unwind_Reason_Code |
| 5908 | unwind (struct _Unwind_Context *context, void *vdata) |
| 5909 | { |
| 5910 | struct backtrace_data *bdata = (struct backtrace_data *) vdata; |
| 5911 | uintptr_t pc; |
| 5912 | int ip_before_insn = 0; |
| 5913 | |
| 5914 | #ifdef HAVE_GETIPINFO |
| 5915 | pc = _Unwind_GetIPInfo (context, &ip_before_insn); |
| 5916 | #else |
| 5917 | pc = _Unwind_GetIP (context); |
| 5918 | #endif |
| 5919 | |
| 5920 | if (bdata->skip > 0) |
| 5921 | { |
| 5922 | --bdata->skip; |
| 5923 | return _URC_NO_REASON; |
| 5924 | } |
| 5925 | |
| 5926 | if (!ip_before_insn) |
| 5927 | --pc; |
| 5928 | |
| 5929 | if (!bdata->can_alloc) |
| 5930 | bdata->ret = bdata->callback (bdata->data, pc, NULL, 0, NULL); |
| 5931 | else |
| 5932 | bdata->ret = backtrace_pcinfo (bdata->state, pc, bdata->callback, |
| 5933 | bdata->error_callback, bdata->data); |
| 5934 | if (bdata->ret != 0) |
| 5935 | return _URC_END_OF_STACK; |
| 5936 | |
| 5937 | return _URC_NO_REASON; |
| 5938 | } |
| 5939 | |
| 5940 | /* Get a stack backtrace. */ |
| 5941 | |
| 5942 | int __attribute__((noinline)) |
| 5943 | backtrace_full (struct backtrace_state *state, int skip, |
| 5944 | backtrace_full_callback callback, |
| 5945 | backtrace_error_callback error_callback, void *data) |
| 5946 | { |
| 5947 | struct backtrace_data bdata; |
| 5948 | void *p; |
| 5949 | |
| 5950 | bdata.skip = skip + 1; |
| 5951 | bdata.state = state; |
| 5952 | bdata.callback = callback; |
| 5953 | bdata.error_callback = error_callback; |
| 5954 | bdata.data = data; |
| 5955 | bdata.ret = 0; |
| 5956 | |
| 5957 | /* If we can't allocate any memory at all, don't try to produce |
| 5958 | file/line information. */ |
| 5959 | p = backtrace_alloc (state, 4096, NULL, NULL); |
| 5960 | if (p == NULL) |
| 5961 | bdata.can_alloc = 0; |
| 5962 | else |
| 5963 | { |
| 5964 | backtrace_free (state, p, 4096, NULL, NULL); |
| 5965 | bdata.can_alloc = 1; |
| 5966 | } |
| 5967 | |
| 5968 | _Unwind_Backtrace (unwind, &bdata); |
| 5969 | return bdata.ret; |
| 5970 | } |
| 5971 | // simple.c: |
| 5972 | #include <unwind.h> |
| 5973 | |
| 5974 | /* The simple_backtrace routine. */ |
| 5975 | |
| 5976 | /* Data passed through _Unwind_Backtrace. */ |
| 5977 | |
| 5978 | struct backtrace_simple_data |
| 5979 | { |
| 5980 | /* Number of frames to skip. */ |
| 5981 | int skip; |
| 5982 | /* Library state. */ |
| 5983 | struct backtrace_state *state; |
| 5984 | /* Callback routine. */ |
| 5985 | backtrace_simple_callback callback; |
| 5986 | /* Error callback routine. */ |
| 5987 | backtrace_error_callback error_callback; |
| 5988 | /* Data to pass to callback routine. */ |
| 5989 | void *data; |
| 5990 | /* Value to return from backtrace. */ |
| 5991 | int ret; |
| 5992 | }; |
| 5993 | |
| 5994 | /* Unwind library callback routine. This is passed to |
| 5995 | _Unwind_Backtrace. */ |
| 5996 | |
| 5997 | static _Unwind_Reason_Code |
| 5998 | simple_unwind (struct _Unwind_Context *context, void *vdata) |
| 5999 | { |
| 6000 | struct backtrace_simple_data *bdata = (struct backtrace_simple_data *) vdata; |
| 6001 | uintptr_t pc; |
| 6002 | int ip_before_insn = 0; |
| 6003 | |
| 6004 | #ifdef HAVE_GETIPINFO |
| 6005 | pc = _Unwind_GetIPInfo (context, &ip_before_insn); |
| 6006 | #else |
| 6007 | pc = _Unwind_GetIP (context); |
| 6008 | #endif |
| 6009 | |
| 6010 | if (bdata->skip > 0) |
| 6011 | { |
| 6012 | --bdata->skip; |
| 6013 | return _URC_NO_REASON; |
| 6014 | } |
| 6015 | |
| 6016 | if (!ip_before_insn) |
| 6017 | --pc; |
| 6018 | |
| 6019 | bdata->ret = bdata->callback (bdata->data, pc); |
| 6020 | |
| 6021 | if (bdata->ret != 0) |
| 6022 | return _URC_END_OF_STACK; |
| 6023 | |
| 6024 | return _URC_NO_REASON; |
| 6025 | } |
| 6026 | |
| 6027 | /* Get a simple stack backtrace. */ |
| 6028 | |
| 6029 | int __attribute__((noinline)) |
| 6030 | backtrace_simple (struct backtrace_state *state, int skip, |
| 6031 | backtrace_simple_callback callback, |
| 6032 | backtrace_error_callback error_callback, void *data) |
| 6033 | { |
| 6034 | struct backtrace_simple_data bdata; |
| 6035 | |
| 6036 | bdata.skip = skip + 1; |
| 6037 | bdata.state = state; |
| 6038 | bdata.callback = callback; |
| 6039 | bdata.error_callback = error_callback; |
| 6040 | bdata.data = data; |
| 6041 | bdata.ret = 0; |
| 6042 | _Unwind_Backtrace (simple_unwind, &bdata); |
| 6043 | return bdata.ret; |
| 6044 | } |
| 6045 | |