1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012-2014 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
38 #include <sys/types.h>
41 #include "filenames.h"
43 #include "backtrace.h"
48 #pragma GCC diagnostic push
49 #pragma GCC diagnostic ignored "-Wswitch"
50 #pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
54 #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
56 /* If strnlen is not declared, provide our own version. */
59 xstrnlen (const char *s, size_t maxlen)
63 for (i = 0; i < maxlen; ++i)
69 #define strnlen xstrnlen
73 /* A buffer to read DWARF info. */
77 /* Buffer name for error messages. */
79 /* Start of the buffer. */
80 const unsigned char *start;
81 /* Next byte to read. */
82 const unsigned char *buf;
83 /* The number of bytes remaining. */
85 /* Whether the data is big-endian. */
87 /* Error callback routine. */
88 backtrace_error_callback error_callback;
89 /* Data for error_callback. */
91 /* Non-zero if we've reported an underflow error. */
92 int reported_underflow;
95 /* A single attribute in a DWARF abbreviation. */
99 /* The attribute name. */
100 enum dwarf_attribute name;
101 /* The attribute form. */
102 enum dwarf_form form;
105 /* A single DWARF abbreviation. */
109 /* The abbrev code--the number used to refer to the abbrev. */
113 /* Non-zero if this abbrev has child entries. */
115 /* The number of attributes. */
117 /* The attributes. */
121 /* The DWARF abbreviations for a compilation unit. This structure
122 only exists while reading the compilation unit. Most DWARF readers
123 seem to a hash table to map abbrev ID's to abbrev entries.
124 However, we primarily care about GCC, and GCC simply issues ID's in
125 numerical order starting at 1. So we simply keep a sorted vector,
126 and try to just look up the code. */
130 /* The number of abbrevs in the vector. */
132 /* The abbrevs, sorted by the code field. */
133 struct abbrev *abbrevs;
136 /* The different kinds of attribute values. */
138 enum attr_val_encoding
142 /* A unsigned integer. */
144 /* A sigd integer. */
148 /* An offset to other data in the containing unit. */
150 /* An offset to other data within the .dwarf_info section. */
152 /* An offset to data in some other section. */
153 ATTR_VAL_REF_SECTION,
154 /* A type signature. */
156 /* A block of data (not represented). */
158 /* An expression (not represented). */
162 /* An attribute value. */
166 /* How the value is stored in the field u. */
167 enum attr_val_encoding encoding;
170 /* ATTR_VAL_ADDRESS, ATTR_VAL_UINT, ATTR_VAL_REF*. */
174 /* ATTR_VAL_STRING. */
176 /* ATTR_VAL_BLOCK not stored. */
180 /* The line number program header. */
184 /* The version of the line number information. */
186 /* The minimum instruction length. */
187 unsigned int min_insn_len;
188 /* The maximum number of ops per instruction. */
189 unsigned int max_ops_per_insn;
190 /* The line base for special opcodes. */
192 /* The line range for special opcodes. */
193 unsigned int line_range;
194 /* The opcode base--the first special opcode. */
195 unsigned int opcode_base;
196 /* Opcode lengths, indexed by opcode - 1. */
197 const unsigned char *opcode_lengths;
198 /* The number of directory entries. */
200 /* The directory entries. */
202 /* The number of filenames. */
203 size_t filenames_count;
205 const char **filenames;
208 /* Map a single PC value to a file/line. We will keep a vector of
209 these sorted by PC value. Each file/line will be correct from the
210 PC up to the PC of the next entry if there is one. We allocate one
211 extra entry at the end so that we can use bsearch. */
217 /* File name. Many entries in the array are expected to point to
218 the same file name. */
219 const char *filename;
224 /* A growable vector of line number information. This is used while
225 reading the line numbers. */
229 /* Memory. This is an array of struct line. */
230 struct backtrace_vector vec;
231 /* Number of valid mappings. */
235 /* A function described in the debug info. */
239 /* The name of the function. */
241 /* If this is an inlined function, the filename of the call
243 const char *caller_filename;
244 /* If this is an inlined function, the line number of the call
247 /* Map PC ranges to inlined functions. */
248 struct function_addrs *function_addrs;
249 size_t function_addrs_count;
252 /* An address range for a function. This maps a PC value to a
253 specific function. */
255 struct function_addrs
257 /* Range is LOW <= PC < HIGH. */
260 /* Function for this address range. */
261 struct function *function;
264 /* A growable vector of function address ranges. */
266 struct function_vector
268 /* Memory. This is an array of struct function_addrs. */
269 struct backtrace_vector vec;
270 /* Number of address ranges present. */
274 /* A DWARF compilation unit. This only holds the information we need
275 to map a PC to a file and line. */
279 /* The first entry for this compilation unit. */
280 const unsigned char *unit_data;
281 /* The length of the data for this compilation unit. */
282 size_t unit_data_len;
283 /* The offset of UNIT_DATA from the start of the information for
284 this compilation unit. */
285 size_t unit_data_offset;
288 /* Whether unit is DWARF64. */
292 /* Offset into line number information. */
294 /* Primary source file. */
295 const char *filename;
296 /* Compilation command working directory. */
297 const char *comp_dir;
298 /* Absolute file name, only set if needed. */
299 const char *abs_filename;
300 /* The abbreviations for this unit. */
301 struct abbrevs abbrevs;
303 /* The fields above this point are read in during initialization and
304 may be accessed freely. The fields below this point are read in
305 as needed, and therefore require care, as different threads may
306 try to initialize them simultaneously. */
308 /* PC to line number mapping. This is NULL if the values have not
309 been read. This is (struct line *) -1 if there was an error
310 reading the values. */
312 /* Number of entries in lines. */
314 /* PC ranges to function. */
315 struct function_addrs *function_addrs;
316 size_t function_addrs_count;
319 /* An address range for a compilation unit. This maps a PC value to a
320 specific compilation unit. Note that we invert the representation
321 in DWARF: instead of listing the units and attaching a list of
322 ranges, we list the ranges and have each one point to the unit.
323 This lets us do a binary search to find the unit. */
327 /* Range is LOW <= PC < HIGH. */
330 /* Compilation unit for this address range. */
334 /* A growable vector of compilation unit address ranges. */
336 struct unit_addrs_vector
338 /* Memory. This is an array of struct unit_addrs. */
339 struct backtrace_vector vec;
340 /* Number of address ranges present. */
344 /* The information we need to map a PC to a file and line. */
348 /* The data for the next file we know about. */
349 struct dwarf_data *next;
350 /* The base address for this file. */
351 uintptr_t base_address;
352 /* A sorted list of address ranges. */
353 struct unit_addrs *addrs;
354 /* Number of address ranges in list. */
356 /* The unparsed .debug_info section. */
357 const unsigned char *dwarf_info;
358 size_t dwarf_info_size;
359 /* The unparsed .debug_line section. */
360 const unsigned char *dwarf_line;
361 size_t dwarf_line_size;
362 /* The unparsed .debug_ranges section. */
363 const unsigned char *dwarf_ranges;
364 size_t dwarf_ranges_size;
365 /* The unparsed .debug_str section. */
366 const unsigned char *dwarf_str;
367 size_t dwarf_str_size;
368 /* Whether the data is big-endian or not. */
370 /* A vector used for function addresses. We keep this here so that
371 we can grow the vector as we read more functions. */
372 struct function_vector fvec;
375 /* Report an error for a DWARF buffer. */
378 dwarf_buf_error (struct dwarf_buf *buf, const char *msg)
382 snprintf (b, sizeof b, "%s in %s at %d",
383 msg, buf->name, (int) (buf->buf - buf->start));
384 buf->error_callback (buf->data, b, 0);
387 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
391 require (struct dwarf_buf *buf, size_t count)
393 if (buf->left >= count)
396 if (!buf->reported_underflow)
398 dwarf_buf_error (buf, "DWARF underflow");
399 buf->reported_underflow = 1;
405 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
409 advance (struct dwarf_buf *buf, size_t count)
411 if (!require (buf, count))
418 /* Read one byte from BUF and advance 1 byte. */
421 read_byte (struct dwarf_buf *buf)
423 const unsigned char *p = buf->buf;
425 if (!advance (buf, 1))
430 /* Read a signed char from BUF and advance 1 byte. */
433 read_sbyte (struct dwarf_buf *buf)
435 const unsigned char *p = buf->buf;
437 if (!advance (buf, 1))
439 return (*p ^ 0x80) - 0x80;
442 /* Read a uint16 from BUF and advance 2 bytes. */
445 read_uint16 (struct dwarf_buf *buf)
447 const unsigned char *p = buf->buf;
449 if (!advance (buf, 2))
451 if (buf->is_bigendian)
452 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
454 return ((uint16_t) p[1] << 8) | (uint16_t) p[0];
457 /* Read a uint32 from BUF and advance 4 bytes. */
460 read_uint32 (struct dwarf_buf *buf)
462 const unsigned char *p = buf->buf;
464 if (!advance (buf, 4))
466 if (buf->is_bigendian)
467 return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16)
468 | ((uint32_t) p[2] << 8) | (uint32_t) p[3]);
470 return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16)
471 | ((uint32_t) p[1] << 8) | (uint32_t) p[0]);
474 /* Read a uint64 from BUF and advance 8 bytes. */
477 read_uint64 (struct dwarf_buf *buf)
479 const unsigned char *p = buf->buf;
481 if (!advance (buf, 8))
483 if (buf->is_bigendian)
484 return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48)
485 | ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32)
486 | ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16)
487 | ((uint64_t) p[6] << 8) | (uint64_t) p[7]);
489 return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48)
490 | ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32)
491 | ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16)
492 | ((uint64_t) p[1] << 8) | (uint64_t) p[0]);
495 /* Read an offset from BUF and advance the appropriate number of
499 read_offset (struct dwarf_buf *buf, int is_dwarf64)
502 return read_uint64 (buf);
504 return read_uint32 (buf);
507 /* Read an address from BUF and advance the appropriate number of
511 read_address (struct dwarf_buf *buf, int addrsize)
516 return read_byte (buf);
518 return read_uint16 (buf);
520 return read_uint32 (buf);
522 return read_uint64 (buf);
524 dwarf_buf_error (buf, "unrecognized address size");
529 /* Return whether a value is the highest possible address, given the
533 is_highest_address (uint64_t address, int addrsize)
538 return address == (unsigned char) -1;
540 return address == (uint16_t) -1;
542 return address == (uint32_t) -1;
544 return address == (uint64_t) -1;
550 /* Read an unsigned LEB128 number. */
553 read_uleb128 (struct dwarf_buf *buf)
565 const unsigned char *p;
568 if (!advance (buf, 1))
572 ret |= ((uint64_t) (b & 0x7f)) << shift;
575 dwarf_buf_error (buf, "LEB128 overflows uint64_t");
580 while ((b & 0x80) != 0);
585 /* Read a signed LEB128 number. */
588 read_sleb128 (struct dwarf_buf *buf)
600 const unsigned char *p;
603 if (!advance (buf, 1))
607 val |= ((uint64_t) (b & 0x7f)) << shift;
610 dwarf_buf_error (buf, "signed LEB128 overflows uint64_t");
615 while ((b & 0x80) != 0);
617 if ((b & 0x40) != 0 && shift < 64)
618 val |= ((uint64_t) -1) << shift;
620 return (int64_t) val;
623 /* Return the length of an LEB128 number. */
626 leb128_len (const unsigned char *p)
631 while ((*p & 0x80) != 0)
639 /* Free an abbreviations structure. */
642 free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs,
643 backtrace_error_callback error_callback, void *data)
647 for (i = 0; i < abbrevs->num_abbrevs; ++i)
648 backtrace_free (state, abbrevs->abbrevs[i].attrs,
649 abbrevs->abbrevs[i].num_attrs * sizeof (struct attr),
650 error_callback, data);
651 backtrace_free (state, abbrevs->abbrevs,
652 abbrevs->num_abbrevs * sizeof (struct abbrev),
653 error_callback, data);
654 abbrevs->num_abbrevs = 0;
655 abbrevs->abbrevs = NULL;
658 /* Read an attribute value. Returns 1 on success, 0 on failure. If
659 the value can be represented as a uint64_t, sets *VAL and sets
660 *IS_VALID to 1. We don't try to store the value of other attribute
661 forms, because we don't care about them. */
664 read_attribute (enum dwarf_form form, struct dwarf_buf *buf,
665 int is_dwarf64, int version, int addrsize,
666 const unsigned char *dwarf_str, size_t dwarf_str_size,
667 struct attr_val *val)
669 /* Avoid warnings about val.u.FIELD may be used uninitialized if
670 this function is inlined. The warnings aren't valid but can
671 occur because the different fields are set and used
673 memset (val, 0, sizeof *val);
678 val->encoding = ATTR_VAL_ADDRESS;
679 val->u.uint = read_address (buf, addrsize);
682 val->encoding = ATTR_VAL_BLOCK;
683 return advance (buf, read_uint16 (buf));
685 val->encoding = ATTR_VAL_BLOCK;
686 return advance (buf, read_uint32 (buf));
688 val->encoding = ATTR_VAL_UINT;
689 val->u.uint = read_uint16 (buf);
692 val->encoding = ATTR_VAL_UINT;
693 val->u.uint = read_uint32 (buf);
696 val->encoding = ATTR_VAL_UINT;
697 val->u.uint = read_uint64 (buf);
700 val->encoding = ATTR_VAL_STRING;
701 val->u.string = (const char *) buf->buf;
702 return advance (buf, strnlen ((const char *) buf->buf, buf->left) + 1);
704 val->encoding = ATTR_VAL_BLOCK;
705 return advance (buf, read_uleb128 (buf));
707 val->encoding = ATTR_VAL_BLOCK;
708 return advance (buf, read_byte (buf));
710 val->encoding = ATTR_VAL_UINT;
711 val->u.uint = read_byte (buf);
714 val->encoding = ATTR_VAL_UINT;
715 val->u.uint = read_byte (buf);
718 val->encoding = ATTR_VAL_SINT;
719 val->u.sint = read_sleb128 (buf);
725 offset = read_offset (buf, is_dwarf64);
726 if (offset >= dwarf_str_size)
728 dwarf_buf_error (buf, "DW_FORM_strp out of range");
731 val->encoding = ATTR_VAL_STRING;
732 val->u.string = (const char *) dwarf_str + offset;
736 val->encoding = ATTR_VAL_UINT;
737 val->u.uint = read_uleb128 (buf);
739 case DW_FORM_ref_addr:
740 val->encoding = ATTR_VAL_REF_INFO;
742 val->u.uint = read_address (buf, addrsize);
744 val->u.uint = read_offset (buf, is_dwarf64);
747 val->encoding = ATTR_VAL_REF_UNIT;
748 val->u.uint = read_byte (buf);
751 val->encoding = ATTR_VAL_REF_UNIT;
752 val->u.uint = read_uint16 (buf);
755 val->encoding = ATTR_VAL_REF_UNIT;
756 val->u.uint = read_uint32 (buf);
759 val->encoding = ATTR_VAL_REF_UNIT;
760 val->u.uint = read_uint64 (buf);
762 case DW_FORM_ref_udata:
763 val->encoding = ATTR_VAL_REF_UNIT;
764 val->u.uint = read_uleb128 (buf);
766 case DW_FORM_indirect:
770 form = read_uleb128 (buf);
771 return read_attribute ((enum dwarf_form) form, buf, is_dwarf64,
772 version, addrsize, dwarf_str, dwarf_str_size,
775 case DW_FORM_sec_offset:
776 val->encoding = ATTR_VAL_REF_SECTION;
777 val->u.uint = read_offset (buf, is_dwarf64);
779 case DW_FORM_exprloc:
780 val->encoding = ATTR_VAL_EXPR;
781 return advance (buf, read_uleb128 (buf));
782 case DW_FORM_flag_present:
783 val->encoding = ATTR_VAL_UINT;
786 case DW_FORM_ref_sig8:
787 val->encoding = ATTR_VAL_REF_TYPE;
788 val->u.uint = read_uint64 (buf);
790 case DW_FORM_GNU_addr_index:
791 val->encoding = ATTR_VAL_REF_SECTION;
792 val->u.uint = read_uleb128 (buf);
794 case DW_FORM_GNU_str_index:
795 val->encoding = ATTR_VAL_REF_SECTION;
796 val->u.uint = read_uleb128 (buf);
798 case DW_FORM_GNU_ref_alt:
799 val->encoding = ATTR_VAL_REF_SECTION;
800 val->u.uint = read_offset (buf, is_dwarf64);
802 case DW_FORM_GNU_strp_alt:
803 val->encoding = ATTR_VAL_REF_SECTION;
804 val->u.uint = read_offset (buf, is_dwarf64);
807 dwarf_buf_error (buf, "unrecognized DWARF form");
812 /* Compare function_addrs for qsort. When ranges are nested, make the
813 smallest one sort last. */
816 function_addrs_compare (const void *v1, const void *v2)
818 const struct function_addrs *a1 = (const struct function_addrs *) v1;
819 const struct function_addrs *a2 = (const struct function_addrs *) v2;
821 if (a1->low < a2->low)
823 if (a1->low > a2->low)
825 if (a1->high < a2->high)
827 if (a1->high > a2->high)
829 return strcmp (a1->function->name, a2->function->name);
832 /* Compare a PC against a function_addrs for bsearch. Note that if
833 there are multiple ranges containing PC, which one will be returned
834 is unpredictable. We compensate for that in dwarf_fileline. */
837 function_addrs_search (const void *vkey, const void *ventry)
839 const uintptr_t *key = (const uintptr_t *) vkey;
840 const struct function_addrs *entry = (const struct function_addrs *) ventry;
846 else if (pc >= entry->high)
852 /* Add a new compilation unit address range to a vector. Returns 1 on
853 success, 0 on failure. */
856 add_unit_addr (struct backtrace_state *state, uintptr_t base_address,
857 struct unit_addrs addrs,
858 backtrace_error_callback error_callback, void *data,
859 struct unit_addrs_vector *vec)
861 struct unit_addrs *p;
863 /* Add in the base address of the module here, so that we can look
864 up the PC directly. */
865 addrs.low += base_address;
866 addrs.high += base_address;
868 /* Try to merge with the last entry. */
871 p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
872 if ((addrs.low == p->high || addrs.low == p->high + 1)
875 if (addrs.high > p->high)
876 p->high = addrs.high;
881 p = ((struct unit_addrs *)
882 backtrace_vector_grow (state, sizeof (struct unit_addrs),
883 error_callback, data, &vec->vec));
892 /* Free a unit address vector. */
895 free_unit_addrs_vector (struct backtrace_state *state,
896 struct unit_addrs_vector *vec,
897 backtrace_error_callback error_callback, void *data)
899 struct unit_addrs *addrs;
902 addrs = (struct unit_addrs *) vec->vec.base;
903 for (i = 0; i < vec->count; ++i)
904 free_abbrevs (state, &addrs[i].u->abbrevs, error_callback, data);
907 /* Compare unit_addrs for qsort. When ranges are nested, make the
908 smallest one sort last. */
911 unit_addrs_compare (const void *v1, const void *v2)
913 const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
914 const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
916 if (a1->low < a2->low)
918 if (a1->low > a2->low)
920 if (a1->high < a2->high)
922 if (a1->high > a2->high)
924 if (a1->u->lineoff < a2->u->lineoff)
926 if (a1->u->lineoff > a2->u->lineoff)
931 /* Compare a PC against a unit_addrs for bsearch. Note that if there
932 are multiple ranges containing PC, which one will be returned is
933 unpredictable. We compensate for that in dwarf_fileline. */
936 unit_addrs_search (const void *vkey, const void *ventry)
938 const uintptr_t *key = (const uintptr_t *) vkey;
939 const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
945 else if (pc >= entry->high)
951 /* Sort the line vector by PC. We want a stable sort here. We know
952 that the pointers are into the same array, so it is safe to compare
956 line_compare (const void *v1, const void *v2)
958 const struct line *ln1 = (const struct line *) v1;
959 const struct line *ln2 = (const struct line *) v2;
961 if (ln1->pc < ln2->pc)
963 else if (ln1->pc > ln2->pc)
973 /* Find a PC in a line vector. We always allocate an extra entry at
974 the end of the lines vector, so that this routine can safely look
975 at the next entry. Note that when there are multiple mappings for
976 the same PC value, this will return the last one. */
979 line_search (const void *vkey, const void *ventry)
981 const uintptr_t *key = (const uintptr_t *) vkey;
982 const struct line *entry = (const struct line *) ventry;
988 else if (pc >= (entry + 1)->pc)
994 /* Sort the abbrevs by the abbrev code. This function is passed to
995 both qsort and bsearch. */
998 abbrev_compare (const void *v1, const void *v2)
1000 const struct abbrev *a1 = (const struct abbrev *) v1;
1001 const struct abbrev *a2 = (const struct abbrev *) v2;
1003 if (a1->code < a2->code)
1005 else if (a1->code > a2->code)
1009 /* This really shouldn't happen. It means there are two
1010 different abbrevs with the same code, and that means we don't
1011 know which one lookup_abbrev should return. */
1016 /* Read the abbreviation table for a compilation unit. Returns 1 on
1017 success, 0 on failure. */
1020 read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset,
1021 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1022 int is_bigendian, backtrace_error_callback error_callback,
1023 void *data, struct abbrevs *abbrevs)
1025 struct dwarf_buf abbrev_buf;
1026 struct dwarf_buf count_buf;
1029 abbrevs->num_abbrevs = 0;
1030 abbrevs->abbrevs = NULL;
1032 if (abbrev_offset >= dwarf_abbrev_size)
1034 error_callback (data, "abbrev offset out of range", 0);
1038 abbrev_buf.name = ".debug_abbrev";
1039 abbrev_buf.start = dwarf_abbrev;
1040 abbrev_buf.buf = dwarf_abbrev + abbrev_offset;
1041 abbrev_buf.left = dwarf_abbrev_size - abbrev_offset;
1042 abbrev_buf.is_bigendian = is_bigendian;
1043 abbrev_buf.error_callback = error_callback;
1044 abbrev_buf.data = data;
1045 abbrev_buf.reported_underflow = 0;
1047 /* Count the number of abbrevs in this list. */
1049 count_buf = abbrev_buf;
1051 while (read_uleb128 (&count_buf) != 0)
1053 if (count_buf.reported_underflow)
1057 read_uleb128 (&count_buf);
1058 // Skip has_children.
1059 read_byte (&count_buf);
1061 while (read_uleb128 (&count_buf) != 0)
1062 read_uleb128 (&count_buf);
1063 // Skip form of last attribute.
1064 read_uleb128 (&count_buf);
1067 if (count_buf.reported_underflow)
1070 if (num_abbrevs == 0)
1073 abbrevs->num_abbrevs = num_abbrevs;
1074 abbrevs->abbrevs = ((struct abbrev *)
1075 backtrace_alloc (state,
1076 num_abbrevs * sizeof (struct abbrev),
1077 error_callback, data));
1078 if (abbrevs->abbrevs == NULL)
1080 memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
1090 if (abbrev_buf.reported_underflow)
1093 code = read_uleb128 (&abbrev_buf);
1098 a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
1099 a.has_children = read_byte (&abbrev_buf);
1101 count_buf = abbrev_buf;
1103 while (read_uleb128 (&count_buf) != 0)
1106 read_uleb128 (&count_buf);
1112 read_uleb128 (&abbrev_buf);
1113 read_uleb128 (&abbrev_buf);
1117 attrs = ((struct attr *)
1118 backtrace_alloc (state, num_attrs * sizeof *attrs,
1119 error_callback, data));
1128 name = read_uleb128 (&abbrev_buf);
1129 form = read_uleb128 (&abbrev_buf);
1132 attrs[num_attrs].name = (enum dwarf_attribute) name;
1133 attrs[num_attrs].form = (enum dwarf_form) form;
1138 a.num_attrs = num_attrs;
1141 abbrevs->abbrevs[num_abbrevs] = a;
1145 qsort (abbrevs->abbrevs, abbrevs->num_abbrevs, sizeof (struct abbrev),
1151 free_abbrevs (state, abbrevs, error_callback, data);
1155 /* Return the abbrev information for an abbrev code. */
1157 static const struct abbrev *
1158 lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
1159 backtrace_error_callback error_callback, void *data)
1164 /* With GCC, where abbrevs are simply numbered in order, we should
1165 be able to just look up the entry. */
1166 if (code - 1 < abbrevs->num_abbrevs
1167 && abbrevs->abbrevs[code - 1].code == code)
1168 return &abbrevs->abbrevs[code - 1];
1170 /* Otherwise we have to search. */
1171 memset (&key, 0, sizeof key);
1173 p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs,
1174 sizeof (struct abbrev), abbrev_compare);
1177 error_callback (data, "invalid abbreviation code", 0);
1180 return (const struct abbrev *) p;
1183 /* Add non-contiguous address ranges for a compilation unit. Returns
1184 1 on success, 0 on failure. */
1187 add_unit_ranges (struct backtrace_state *state, uintptr_t base_address,
1188 struct unit *u, uint64_t ranges, uint64_t base,
1189 int is_bigendian, const unsigned char *dwarf_ranges,
1190 size_t dwarf_ranges_size,
1191 backtrace_error_callback error_callback, void *data,
1192 struct unit_addrs_vector *addrs)
1194 struct dwarf_buf ranges_buf;
1196 if (ranges >= dwarf_ranges_size)
1198 error_callback (data, "ranges offset out of range", 0);
1202 ranges_buf.name = ".debug_ranges";
1203 ranges_buf.start = dwarf_ranges;
1204 ranges_buf.buf = dwarf_ranges + ranges;
1205 ranges_buf.left = dwarf_ranges_size - ranges;
1206 ranges_buf.is_bigendian = is_bigendian;
1207 ranges_buf.error_callback = error_callback;
1208 ranges_buf.data = data;
1209 ranges_buf.reported_underflow = 0;
1216 if (ranges_buf.reported_underflow)
1219 low = read_address (&ranges_buf, u->addrsize);
1220 high = read_address (&ranges_buf, u->addrsize);
1222 if (low == 0 && high == 0)
1225 if (is_highest_address (low, u->addrsize))
1229 struct unit_addrs a;
1232 a.high = high + base;
1234 if (!add_unit_addr (state, base_address, a, error_callback, data,
1240 if (ranges_buf.reported_underflow)
1246 /* Find the address range covered by a compilation unit, reading from
1247 UNIT_BUF and adding values to U. Returns 1 if all data could be
1248 read, 0 if there is some error. */
1251 find_address_ranges (struct backtrace_state *state, uintptr_t base_address,
1252 struct dwarf_buf *unit_buf,
1253 const unsigned char *dwarf_str, size_t dwarf_str_size,
1254 const unsigned char *dwarf_ranges,
1255 size_t dwarf_ranges_size,
1256 int is_bigendian, backtrace_error_callback error_callback,
1257 void *data, struct unit *u,
1258 struct unit_addrs_vector *addrs)
1260 while (unit_buf->left > 0)
1263 const struct abbrev *abbrev;
1268 int highpc_is_relative;
1273 code = read_uleb128 (unit_buf);
1277 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
1285 highpc_is_relative = 0;
1288 for (i = 0; i < abbrev->num_attrs; ++i)
1290 struct attr_val val;
1292 if (!read_attribute (abbrev->attrs[i].form, unit_buf,
1293 u->is_dwarf64, u->version, u->addrsize,
1294 dwarf_str, dwarf_str_size, &val))
1297 switch (abbrev->attrs[i].name)
1300 if (val.encoding == ATTR_VAL_ADDRESS)
1308 if (val.encoding == ATTR_VAL_ADDRESS)
1310 highpc = val.u.uint;
1313 else if (val.encoding == ATTR_VAL_UINT)
1315 highpc = val.u.uint;
1317 highpc_is_relative = 1;
1322 if (val.encoding == ATTR_VAL_UINT
1323 || val.encoding == ATTR_VAL_REF_SECTION)
1325 ranges = val.u.uint;
1330 case DW_AT_stmt_list:
1331 if (abbrev->tag == DW_TAG_compile_unit
1332 && (val.encoding == ATTR_VAL_UINT
1333 || val.encoding == ATTR_VAL_REF_SECTION))
1334 u->lineoff = val.u.uint;
1338 if (abbrev->tag == DW_TAG_compile_unit
1339 && val.encoding == ATTR_VAL_STRING)
1340 u->filename = val.u.string;
1343 case DW_AT_comp_dir:
1344 if (abbrev->tag == DW_TAG_compile_unit
1345 && val.encoding == ATTR_VAL_STRING)
1346 u->comp_dir = val.u.string;
1354 if (abbrev->tag == DW_TAG_compile_unit
1355 || abbrev->tag == DW_TAG_subprogram)
1359 if (!add_unit_ranges (state, base_address, u, ranges, lowpc,
1360 is_bigendian, dwarf_ranges,
1361 dwarf_ranges_size, error_callback,
1365 else if (have_lowpc && have_highpc)
1367 struct unit_addrs a;
1369 if (highpc_is_relative)
1375 if (!add_unit_addr (state, base_address, a, error_callback, data,
1380 /* If we found the PC range in the DW_TAG_compile_unit, we
1382 if (abbrev->tag == DW_TAG_compile_unit
1383 && (have_ranges || (have_lowpc && have_highpc)))
1387 if (abbrev->has_children)
1389 if (!find_address_ranges (state, base_address, unit_buf,
1390 dwarf_str, dwarf_str_size,
1391 dwarf_ranges, dwarf_ranges_size,
1392 is_bigendian, error_callback, data,
1401 /* Build a mapping from address ranges to the compilation units where
1402 the line number information for that range can be found. Returns 1
1403 on success, 0 on failure. */
1406 build_address_map (struct backtrace_state *state, uintptr_t base_address,
1407 const unsigned char *dwarf_info, size_t dwarf_info_size,
1408 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1409 const unsigned char *dwarf_ranges, size_t dwarf_ranges_size,
1410 const unsigned char *dwarf_str, size_t dwarf_str_size,
1411 int is_bigendian, backtrace_error_callback error_callback,
1412 void *data, struct unit_addrs_vector *addrs)
1414 struct dwarf_buf info;
1415 struct abbrevs abbrevs;
1417 memset (&addrs->vec, 0, sizeof addrs->vec);
1420 /* Read through the .debug_info section. FIXME: Should we use the
1421 .debug_aranges section? gdb and addr2line don't use it, but I'm
1424 info.name = ".debug_info";
1425 info.start = dwarf_info;
1426 info.buf = dwarf_info;
1427 info.left = dwarf_info_size;
1428 info.is_bigendian = is_bigendian;
1429 info.error_callback = error_callback;
1431 info.reported_underflow = 0;
1433 memset (&abbrevs, 0, sizeof abbrevs);
1434 while (info.left > 0)
1436 const unsigned char *unit_data_start;
1439 struct dwarf_buf unit_buf;
1441 uint64_t abbrev_offset;
1445 if (info.reported_underflow)
1448 unit_data_start = info.buf;
1451 len = read_uint32 (&info);
1452 if (len == 0xffffffff)
1454 len = read_uint64 (&info);
1459 unit_buf.left = len;
1461 if (!advance (&info, len))
1464 version = read_uint16 (&unit_buf);
1465 if (version < 2 || version > 4)
1467 dwarf_buf_error (&unit_buf, "unrecognized DWARF version");
1471 abbrev_offset = read_offset (&unit_buf, is_dwarf64);
1472 if (!read_abbrevs (state, abbrev_offset, dwarf_abbrev, dwarf_abbrev_size,
1473 is_bigendian, error_callback, data, &abbrevs))
1476 addrsize = read_byte (&unit_buf);
1478 u = ((struct unit *)
1479 backtrace_alloc (state, sizeof *u, error_callback, data));
1482 u->unit_data = unit_buf.buf;
1483 u->unit_data_len = unit_buf.left;
1484 u->unit_data_offset = unit_buf.buf - unit_data_start;
1485 u->version = version;
1486 u->is_dwarf64 = is_dwarf64;
1487 u->addrsize = addrsize;
1490 u->abs_filename = NULL;
1492 u->abbrevs = abbrevs;
1493 memset (&abbrevs, 0, sizeof abbrevs);
1495 /* The actual line number mappings will be read as needed. */
1498 u->function_addrs = NULL;
1499 u->function_addrs_count = 0;
1501 if (!find_address_ranges (state, base_address, &unit_buf,
1502 dwarf_str, dwarf_str_size,
1503 dwarf_ranges, dwarf_ranges_size,
1504 is_bigendian, error_callback, data,
1507 free_abbrevs (state, &u->abbrevs, error_callback, data);
1508 backtrace_free (state, u, sizeof *u, error_callback, data);
1512 if (unit_buf.reported_underflow)
1514 free_abbrevs (state, &u->abbrevs, error_callback, data);
1515 backtrace_free (state, u, sizeof *u, error_callback, data);
1519 if (info.reported_underflow)
1525 free_abbrevs (state, &abbrevs, error_callback, data);
1526 free_unit_addrs_vector (state, addrs, error_callback, data);
1530 /* Add a new mapping to the vector of line mappings that we are
1531 building. Returns 1 on success, 0 on failure. */
1534 add_line (struct backtrace_state *state, struct dwarf_data *ddata,
1535 uintptr_t pc, const char *filename, int lineno,
1536 backtrace_error_callback error_callback, void *data,
1537 struct line_vector *vec)
1541 /* If we are adding the same mapping, ignore it. This can happen
1542 when using discriminators. */
1545 ln = (struct line *) vec->vec.base + (vec->count - 1);
1546 if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
1550 ln = ((struct line *)
1551 backtrace_vector_grow (state, sizeof (struct line), error_callback,
1556 /* Add in the base address here, so that we can look up the PC
1558 ln->pc = pc + ddata->base_address;
1560 ln->filename = filename;
1561 ln->lineno = lineno;
1568 /* Free the line header information. If FREE_FILENAMES is true we
1569 free the file names themselves, otherwise we leave them, as there
1570 may be line structures pointing to them. */
1573 free_line_header (struct backtrace_state *state, struct line_header *hdr,
1574 backtrace_error_callback error_callback, void *data)
1576 backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
1577 error_callback, data);
1578 backtrace_free (state, hdr->filenames,
1579 hdr->filenames_count * sizeof (char *),
1580 error_callback, data);
1583 /* Read the line header. Return 1 on success, 0 on failure. */
1586 read_line_header (struct backtrace_state *state, struct unit *u,
1587 int is_dwarf64, struct dwarf_buf *line_buf,
1588 struct line_header *hdr)
1591 struct dwarf_buf hdr_buf;
1592 const unsigned char *p;
1593 const unsigned char *pend;
1596 hdr->version = read_uint16 (line_buf);
1597 if (hdr->version < 2 || hdr->version > 4)
1599 dwarf_buf_error (line_buf, "unsupported line number version");
1603 hdrlen = read_offset (line_buf, is_dwarf64);
1605 hdr_buf = *line_buf;
1606 hdr_buf.left = hdrlen;
1608 if (!advance (line_buf, hdrlen))
1611 hdr->min_insn_len = read_byte (&hdr_buf);
1612 if (hdr->version < 4)
1613 hdr->max_ops_per_insn = 1;
1615 hdr->max_ops_per_insn = read_byte (&hdr_buf);
1617 /* We don't care about default_is_stmt. */
1618 read_byte (&hdr_buf);
1620 hdr->line_base = read_sbyte (&hdr_buf);
1621 hdr->line_range = read_byte (&hdr_buf);
1623 hdr->opcode_base = read_byte (&hdr_buf);
1624 hdr->opcode_lengths = hdr_buf.buf;
1625 if (!advance (&hdr_buf, hdr->opcode_base - 1))
1628 /* Count the number of directory entries. */
1629 hdr->dirs_count = 0;
1631 pend = p + hdr_buf.left;
1632 while (p < pend && *p != '\0')
1634 p += strnlen((const char *) p, pend - p) + 1;
1638 hdr->dirs = ((const char **)
1639 backtrace_alloc (state,
1640 hdr->dirs_count * sizeof (const char *),
1641 line_buf->error_callback, line_buf->data));
1642 if (hdr->dirs == NULL)
1646 while (*hdr_buf.buf != '\0')
1648 if (hdr_buf.reported_underflow)
1651 hdr->dirs[i] = (const char *) hdr_buf.buf;
1653 if (!advance (&hdr_buf,
1654 strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1))
1657 if (!advance (&hdr_buf, 1))
1660 /* Count the number of file entries. */
1661 hdr->filenames_count = 0;
1663 pend = p + hdr_buf.left;
1664 while (p < pend && *p != '\0')
1666 p += strnlen ((const char *) p, pend - p) + 1;
1667 p += leb128_len (p);
1668 p += leb128_len (p);
1669 p += leb128_len (p);
1670 ++hdr->filenames_count;
1673 hdr->filenames = ((const char **)
1674 backtrace_alloc (state,
1675 hdr->filenames_count * sizeof (char *),
1676 line_buf->error_callback,
1678 if (hdr->filenames == NULL)
1681 while (*hdr_buf.buf != '\0')
1683 const char *filename;
1686 if (hdr_buf.reported_underflow)
1689 filename = (const char *) hdr_buf.buf;
1690 if (!advance (&hdr_buf,
1691 strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1))
1693 dir_index = read_uleb128 (&hdr_buf);
1694 if (IS_ABSOLUTE_PATH (filename)
1695 || (dir_index == 0 && u->comp_dir == NULL))
1696 hdr->filenames[i] = filename;
1701 size_t filename_len;
1706 else if (dir_index - 1 < hdr->dirs_count)
1707 dir = hdr->dirs[dir_index - 1];
1710 dwarf_buf_error (line_buf,
1711 ("invalid directory index in "
1712 "line number program header"));
1715 dir_len = strlen (dir);
1716 filename_len = strlen (filename);
1718 backtrace_alloc (state, dir_len + filename_len + 2,
1719 line_buf->error_callback, line_buf->data));
1722 memcpy (s, dir, dir_len);
1723 /* FIXME: If we are on a DOS-based file system, and the
1724 directory or the file name use backslashes, then we
1725 should use a backslash here. */
1727 memcpy (s + dir_len + 1, filename, filename_len + 1);
1728 hdr->filenames[i] = s;
1731 /* Ignore the modification time and size. */
1732 read_uleb128 (&hdr_buf);
1733 read_uleb128 (&hdr_buf);
1738 if (hdr_buf.reported_underflow)
1744 /* Read the line program, adding line mappings to VEC. Return 1 on
1745 success, 0 on failure. */
1748 read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
1749 struct unit *u, const struct line_header *hdr,
1750 struct dwarf_buf *line_buf, struct line_vector *vec)
1753 unsigned int op_index;
1754 const char *reset_filename;
1755 const char *filename;
1760 if (hdr->filenames_count > 0)
1761 reset_filename = hdr->filenames[0];
1763 reset_filename = "";
1764 filename = reset_filename;
1766 while (line_buf->left > 0)
1770 op = read_byte (line_buf);
1771 if (op >= hdr->opcode_base)
1773 unsigned int advance;
1775 /* Special opcode. */
1776 op -= hdr->opcode_base;
1777 advance = op / hdr->line_range;
1778 address += (hdr->min_insn_len * (op_index + advance)
1779 / hdr->max_ops_per_insn);
1780 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1781 lineno += hdr->line_base + (int) (op % hdr->line_range);
1782 add_line (state, ddata, address, filename, lineno,
1783 line_buf->error_callback, line_buf->data, vec);
1785 else if (op == DW_LNS_extended_op)
1789 len = read_uleb128 (line_buf);
1790 op = read_byte (line_buf);
1793 case DW_LNE_end_sequence:
1794 /* FIXME: Should we mark the high PC here? It seems
1795 that we already have that information from the
1796 compilation unit. */
1799 filename = reset_filename;
1802 case DW_LNE_set_address:
1803 address = read_address (line_buf, u->addrsize);
1805 case DW_LNE_define_file:
1808 unsigned int dir_index;
1810 f = (const char *) line_buf->buf;
1811 if (!advance (line_buf, strnlen (f, line_buf->left) + 1))
1813 dir_index = read_uleb128 (line_buf);
1814 /* Ignore that time and length. */
1815 read_uleb128 (line_buf);
1816 read_uleb128 (line_buf);
1817 if (IS_ABSOLUTE_PATH (f))
1828 else if (dir_index - 1 < hdr->dirs_count)
1829 dir = hdr->dirs[dir_index - 1];
1832 dwarf_buf_error (line_buf,
1833 ("invalid directory index "
1834 "in line number program"));
1837 dir_len = strlen (dir);
1840 backtrace_alloc (state, dir_len + f_len + 2,
1841 line_buf->error_callback,
1845 memcpy (p, dir, dir_len);
1846 /* FIXME: If we are on a DOS-based file system,
1847 and the directory or the file name use
1848 backslashes, then we should use a backslash
1851 memcpy (p + dir_len + 1, f, f_len + 1);
1856 case DW_LNE_set_discriminator:
1857 /* We don't care about discriminators. */
1858 read_uleb128 (line_buf);
1861 if (!advance (line_buf, len - 1))
1871 add_line (state, ddata, address, filename, lineno,
1872 line_buf->error_callback, line_buf->data, vec);
1874 case DW_LNS_advance_pc:
1878 advance = read_uleb128 (line_buf);
1879 address += (hdr->min_insn_len * (op_index + advance)
1880 / hdr->max_ops_per_insn);
1881 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1884 case DW_LNS_advance_line:
1885 lineno += (int) read_sleb128 (line_buf);
1887 case DW_LNS_set_file:
1891 fileno = read_uleb128 (line_buf);
1896 if (fileno - 1 >= hdr->filenames_count)
1898 dwarf_buf_error (line_buf,
1899 ("invalid file number in "
1900 "line number program"));
1903 filename = hdr->filenames[fileno - 1];
1907 case DW_LNS_set_column:
1908 read_uleb128 (line_buf);
1910 case DW_LNS_negate_stmt:
1912 case DW_LNS_set_basic_block:
1914 case DW_LNS_const_add_pc:
1916 unsigned int advance;
1918 op = 255 - hdr->opcode_base;
1919 advance = op / hdr->line_range;
1920 address += (hdr->min_insn_len * (op_index + advance)
1921 / hdr->max_ops_per_insn);
1922 op_index = (op_index + advance) % hdr->max_ops_per_insn;
1925 case DW_LNS_fixed_advance_pc:
1926 address += read_uint16 (line_buf);
1929 case DW_LNS_set_prologue_end:
1931 case DW_LNS_set_epilogue_begin:
1933 case DW_LNS_set_isa:
1934 read_uleb128 (line_buf);
1940 for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
1941 read_uleb128 (line_buf);
1951 /* Read the line number information for a compilation unit. Returns 1
1952 on success, 0 on failure. */
1955 read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
1956 backtrace_error_callback error_callback, void *data,
1957 struct unit *u, struct line_header *hdr, struct line **lines,
1958 size_t *lines_count)
1960 struct line_vector vec;
1961 struct dwarf_buf line_buf;
1966 memset (&vec.vec, 0, sizeof vec.vec);
1969 memset (hdr, 0, sizeof *hdr);
1971 if (u->lineoff != (off_t) (size_t) u->lineoff
1972 || (size_t) u->lineoff >= ddata->dwarf_line_size)
1974 error_callback (data, "unit line offset out of range", 0);
1978 line_buf.name = ".debug_line";
1979 line_buf.start = ddata->dwarf_line;
1980 line_buf.buf = ddata->dwarf_line + u->lineoff;
1981 line_buf.left = ddata->dwarf_line_size - u->lineoff;
1982 line_buf.is_bigendian = ddata->is_bigendian;
1983 line_buf.error_callback = error_callback;
1984 line_buf.data = data;
1985 line_buf.reported_underflow = 0;
1988 len = read_uint32 (&line_buf);
1989 if (len == 0xffffffff)
1991 len = read_uint64 (&line_buf);
1994 line_buf.left = len;
1996 if (!read_line_header (state, u, is_dwarf64, &line_buf, hdr))
1999 if (!read_line_program (state, ddata, u, hdr, &line_buf, &vec))
2002 if (line_buf.reported_underflow)
2007 /* This is not a failure in the sense of a generating an error,
2008 but it is a failure in that sense that we have no useful
2013 /* Allocate one extra entry at the end. */
2014 ln = ((struct line *)
2015 backtrace_vector_grow (state, sizeof (struct line), error_callback,
2019 ln->pc = (uintptr_t) -1;
2020 ln->filename = NULL;
2023 if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
2026 ln = (struct line *) vec.vec.base;
2027 qsort (ln, vec.count, sizeof (struct line), line_compare);
2030 *lines_count = vec.count;
2035 vec.vec.alc += vec.vec.size;
2037 backtrace_vector_release (state, &vec.vec, error_callback, data);
2038 free_line_header (state, hdr, error_callback, data);
2039 *lines = (struct line *) (uintptr_t) -1;
2044 /* Read the name of a function from a DIE referenced by a
2045 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
2046 the same compilation unit. */
2049 read_referenced_name (struct dwarf_data *ddata, struct unit *u,
2050 uint64_t offset, backtrace_error_callback error_callback,
2053 struct dwarf_buf unit_buf;
2055 const struct abbrev *abbrev;
2059 /* OFFSET is from the start of the data for this compilation unit.
2060 U->unit_data is the data, but it starts U->unit_data_offset bytes
2061 from the beginning. */
2063 if (offset < u->unit_data_offset
2064 || offset - u->unit_data_offset >= u->unit_data_len)
2066 error_callback (data,
2067 "abstract origin or specification out of range",
2072 offset -= u->unit_data_offset;
2074 unit_buf.name = ".debug_info";
2075 unit_buf.start = ddata->dwarf_info;
2076 unit_buf.buf = u->unit_data + offset;
2077 unit_buf.left = u->unit_data_len - offset;
2078 unit_buf.is_bigendian = ddata->is_bigendian;
2079 unit_buf.error_callback = error_callback;
2080 unit_buf.data = data;
2081 unit_buf.reported_underflow = 0;
2083 code = read_uleb128 (&unit_buf);
2086 dwarf_buf_error (&unit_buf, "invalid abstract origin or specification");
2090 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2095 for (i = 0; i < abbrev->num_attrs; ++i)
2097 struct attr_val val;
2099 if (!read_attribute (abbrev->attrs[i].form, &unit_buf,
2100 u->is_dwarf64, u->version, u->addrsize,
2101 ddata->dwarf_str, ddata->dwarf_str_size,
2105 switch (abbrev->attrs[i].name)
2108 /* We prefer the linkage name if get one. */
2109 if (val.encoding == ATTR_VAL_STRING)
2113 case DW_AT_linkage_name:
2114 case DW_AT_MIPS_linkage_name:
2115 if (val.encoding == ATTR_VAL_STRING)
2116 return val.u.string;
2119 case DW_AT_specification:
2120 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2121 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2123 /* This refers to a specification defined in some other
2124 compilation unit. We can handle this case if we
2125 must, but it's harder. */
2128 if (val.encoding == ATTR_VAL_UINT
2129 || val.encoding == ATTR_VAL_REF_UNIT)
2133 name = read_referenced_name (ddata, u, val.u.uint,
2134 error_callback, data);
2148 /* Add a single range to U that maps to function. Returns 1 on
2149 success, 0 on error. */
2152 add_function_range (struct backtrace_state *state, struct dwarf_data *ddata,
2153 struct function *function, uint64_t lowpc, uint64_t highpc,
2154 backtrace_error_callback error_callback,
2155 void *data, struct function_vector *vec)
2157 struct function_addrs *p;
2159 /* Add in the base address here, so that we can look up the PC
2161 lowpc += ddata->base_address;
2162 highpc += ddata->base_address;
2166 p = (struct function_addrs *) vec->vec.base + vec->count - 1;
2167 if ((lowpc == p->high || lowpc == p->high + 1)
2168 && function == p->function)
2170 if (highpc > p->high)
2176 p = ((struct function_addrs *)
2177 backtrace_vector_grow (state, sizeof (struct function_addrs),
2178 error_callback, data, &vec->vec));
2184 p->function = function;
2189 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2193 add_function_ranges (struct backtrace_state *state, struct dwarf_data *ddata,
2194 struct unit *u, struct function *function,
2195 uint64_t ranges, uint64_t base,
2196 backtrace_error_callback error_callback, void *data,
2197 struct function_vector *vec)
2199 struct dwarf_buf ranges_buf;
2201 if (ranges >= ddata->dwarf_ranges_size)
2203 error_callback (data, "function ranges offset out of range", 0);
2207 ranges_buf.name = ".debug_ranges";
2208 ranges_buf.start = ddata->dwarf_ranges;
2209 ranges_buf.buf = ddata->dwarf_ranges + ranges;
2210 ranges_buf.left = ddata->dwarf_ranges_size - ranges;
2211 ranges_buf.is_bigendian = ddata->is_bigendian;
2212 ranges_buf.error_callback = error_callback;
2213 ranges_buf.data = data;
2214 ranges_buf.reported_underflow = 0;
2221 if (ranges_buf.reported_underflow)
2224 low = read_address (&ranges_buf, u->addrsize);
2225 high = read_address (&ranges_buf, u->addrsize);
2227 if (low == 0 && high == 0)
2230 if (is_highest_address (low, u->addrsize))
2234 if (!add_function_range (state, ddata, function, low + base,
2235 high + base, error_callback, data, vec))
2240 if (ranges_buf.reported_underflow)
2246 /* Read one entry plus all its children. Add function addresses to
2247 VEC. Returns 1 on success, 0 on error. */
2250 read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
2251 struct unit *u, uint64_t base, struct dwarf_buf *unit_buf,
2252 const struct line_header *lhdr,
2253 backtrace_error_callback error_callback, void *data,
2254 struct function_vector *vec)
2256 while (unit_buf->left > 0)
2259 const struct abbrev *abbrev;
2261 struct function *function;
2267 int highpc_is_relative;
2271 code = read_uleb128 (unit_buf);
2275 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2279 is_function = (abbrev->tag == DW_TAG_subprogram
2280 || abbrev->tag == DW_TAG_entry_point
2281 || abbrev->tag == DW_TAG_inlined_subroutine);
2286 function = ((struct function *)
2287 backtrace_alloc (state, sizeof *function,
2288 error_callback, data));
2289 if (function == NULL)
2291 memset (function, 0, sizeof *function);
2298 highpc_is_relative = 0;
2301 for (i = 0; i < abbrev->num_attrs; ++i)
2303 struct attr_val val;
2305 if (!read_attribute (abbrev->attrs[i].form, unit_buf,
2306 u->is_dwarf64, u->version, u->addrsize,
2307 ddata->dwarf_str, ddata->dwarf_str_size,
2311 /* The compile unit sets the base address for any address
2312 ranges in the function entries. */
2313 if (abbrev->tag == DW_TAG_compile_unit
2314 && abbrev->attrs[i].name == DW_AT_low_pc
2315 && val.encoding == ATTR_VAL_ADDRESS)
2320 switch (abbrev->attrs[i].name)
2322 case DW_AT_call_file:
2323 if (val.encoding == ATTR_VAL_UINT)
2325 if (val.u.uint == 0)
2326 function->caller_filename = "";
2329 if (val.u.uint - 1 >= lhdr->filenames_count)
2331 dwarf_buf_error (unit_buf,
2332 ("invalid file number in "
2333 "DW_AT_call_file attribute"));
2336 function->caller_filename =
2337 lhdr->filenames[val.u.uint - 1];
2342 case DW_AT_call_line:
2343 if (val.encoding == ATTR_VAL_UINT)
2344 function->caller_lineno = val.u.uint;
2347 case DW_AT_abstract_origin:
2348 case DW_AT_specification:
2349 if (abbrev->attrs[i].form == DW_FORM_ref_addr
2350 || abbrev->attrs[i].form == DW_FORM_ref_sig8)
2352 /* This refers to an abstract origin defined in
2353 some other compilation unit. We can handle
2354 this case if we must, but it's harder. */
2357 if (val.encoding == ATTR_VAL_UINT
2358 || val.encoding == ATTR_VAL_REF_UNIT)
2362 name = read_referenced_name (ddata, u, val.u.uint,
2363 error_callback, data);
2365 function->name = name;
2370 if (val.encoding == ATTR_VAL_STRING)
2372 /* Don't override a name we found in some other
2373 way, as it will normally be more
2374 useful--e.g., this name is normally not
2376 if (function->name == NULL)
2377 function->name = val.u.string;
2381 case DW_AT_linkage_name:
2382 case DW_AT_MIPS_linkage_name:
2383 if (val.encoding == ATTR_VAL_STRING)
2384 function->name = val.u.string;
2388 if (val.encoding == ATTR_VAL_ADDRESS)
2396 if (val.encoding == ATTR_VAL_ADDRESS)
2398 highpc = val.u.uint;
2401 else if (val.encoding == ATTR_VAL_UINT)
2403 highpc = val.u.uint;
2405 highpc_is_relative = 1;
2410 if (val.encoding == ATTR_VAL_UINT
2411 || val.encoding == ATTR_VAL_REF_SECTION)
2413 ranges = val.u.uint;
2424 /* If we couldn't find a name for the function, we have no use
2426 if (is_function && function->name == NULL)
2428 backtrace_free (state, function, sizeof *function,
2429 error_callback, data);
2437 if (!add_function_ranges (state, ddata, u, function, ranges,
2438 base, error_callback, data, vec))
2441 else if (have_lowpc && have_highpc)
2443 if (highpc_is_relative)
2445 if (!add_function_range (state, ddata, function, lowpc, highpc,
2446 error_callback, data, vec))
2451 backtrace_free (state, function, sizeof *function,
2452 error_callback, data);
2457 if (abbrev->has_children)
2461 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2462 error_callback, data, vec))
2467 struct function_vector fvec;
2469 /* Gather any information for inlined functions in
2472 memset (&fvec, 0, sizeof fvec);
2474 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
2475 error_callback, data, &fvec))
2480 struct function_addrs *faddrs;
2482 if (!backtrace_vector_release (state, &fvec.vec,
2483 error_callback, data))
2486 faddrs = (struct function_addrs *) fvec.vec.base;
2487 qsort (faddrs, fvec.count,
2488 sizeof (struct function_addrs),
2489 function_addrs_compare);
2491 function->function_addrs = faddrs;
2492 function->function_addrs_count = fvec.count;
2501 /* Read function name information for a compilation unit. We look
2502 through the whole unit looking for function tags. */
2505 read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
2506 const struct line_header *lhdr,
2507 backtrace_error_callback error_callback, void *data,
2508 struct unit *u, struct function_vector *fvec,
2509 struct function_addrs **ret_addrs,
2510 size_t *ret_addrs_count)
2512 struct function_vector lvec;
2513 struct function_vector *pfvec;
2514 struct dwarf_buf unit_buf;
2515 struct function_addrs *addrs;
2518 /* Use FVEC if it is not NULL. Otherwise use our own vector. */
2523 memset (&lvec, 0, sizeof lvec);
2527 unit_buf.name = ".debug_info";
2528 unit_buf.start = ddata->dwarf_info;
2529 unit_buf.buf = u->unit_data;
2530 unit_buf.left = u->unit_data_len;
2531 unit_buf.is_bigendian = ddata->is_bigendian;
2532 unit_buf.error_callback = error_callback;
2533 unit_buf.data = data;
2534 unit_buf.reported_underflow = 0;
2536 while (unit_buf.left > 0)
2538 if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
2539 error_callback, data, pfvec))
2543 if (pfvec->count == 0)
2546 addrs_count = pfvec->count;
2550 if (!backtrace_vector_release (state, &lvec.vec, error_callback, data))
2552 addrs = (struct function_addrs *) pfvec->vec.base;
2556 /* Finish this list of addresses, but leave the remaining space in
2557 the vector available for the next function unit. */
2558 addrs = ((struct function_addrs *)
2559 backtrace_vector_finish (state, &fvec->vec,
2560 error_callback, data));
2566 qsort (addrs, addrs_count, sizeof (struct function_addrs),
2567 function_addrs_compare);
2570 *ret_addrs_count = addrs_count;
2573 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2574 information, and update FILENAME and LINENO for the caller.
2575 Returns whatever CALLBACK returns, or 0 to keep going. */
2578 report_inlined_functions (uintptr_t pc, struct function *function,
2579 backtrace_full_callback callback, void *data,
2580 const char **filename, int *lineno)
2582 struct function_addrs *function_addrs;
2583 struct function *inlined;
2586 if (function->function_addrs_count == 0)
2589 function_addrs = ((struct function_addrs *)
2590 bsearch (&pc, function->function_addrs,
2591 function->function_addrs_count,
2592 sizeof (struct function_addrs),
2593 function_addrs_search));
2594 if (function_addrs == NULL)
2597 while (((size_t) (function_addrs - function->function_addrs) + 1
2598 < function->function_addrs_count)
2599 && pc >= (function_addrs + 1)->low
2600 && pc < (function_addrs + 1)->high)
2603 /* We found an inlined call. */
2605 inlined = function_addrs->function;
2607 /* Report any calls inlined into this one. */
2608 ret = report_inlined_functions (pc, inlined, callback, data,
2613 /* Report this inlined call. */
2614 ret = callback (data, pc, *filename, *lineno, inlined->name);
2618 /* Our caller will report the caller of the inlined function; tell
2619 it the appropriate filename and line number. */
2620 *filename = inlined->caller_filename;
2621 *lineno = inlined->caller_lineno;
2626 /* Look for a PC in the DWARF mapping for one module. On success,
2627 call CALLBACK and return whatever it returns. On error, call
2628 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
2632 dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata,
2633 uintptr_t pc, backtrace_full_callback callback,
2634 backtrace_error_callback error_callback, void *data,
2637 struct unit_addrs *entry;
2642 struct function_addrs *function_addrs;
2643 struct function *function;
2644 const char *filename;
2650 /* Find an address range that includes PC. */
2651 entry = bsearch (&pc, ddata->addrs, ddata->addrs_count,
2652 sizeof (struct unit_addrs), unit_addrs_search);
2660 //$ TODO mikesart: this isn't the case with clang. We need to search all ranges...
2662 /* If there are multiple ranges that contain PC, use the last one,
2663 in order to produce predictable results. If we assume that all
2664 ranges are properly nested, then the last range will be the
2666 while ((size_t) (entry - ddata->addrs) + 1 < ddata->addrs_count
2667 && pc >= (entry + 1)->low
2668 && pc < (entry + 1)->high)
2671 /* We need the lines, lines_count, function_addrs,
2672 function_addrs_count fields of u. If they are not set, we need
2673 to set them. When running in threaded mode, we need to allow for
2674 the possibility that some other thread is setting them
2680 /* Skip units with no useful line number information by walking
2681 backward. Useless line number information is marked by setting
2683 while (entry > ddata->addrs
2684 && pc >= (entry - 1)->low
2685 && pc < (entry - 1)->high)
2687 if (state->threaded)
2688 lines = (struct line *) backtrace_atomic_load_pointer (&u->lines);
2690 if (lines != (struct line *) (uintptr_t) -1)
2699 if (state->threaded)
2700 lines = backtrace_atomic_load_pointer (&u->lines);
2705 size_t function_addrs_count;
2706 struct line_header lhdr;
2709 /* We have never read the line information for this unit. Read
2712 function_addrs = NULL;
2713 function_addrs_count = 0;
2714 if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
2717 struct function_vector *pfvec;
2719 /* If not threaded, reuse DDATA->FVEC for better memory
2721 if (state->threaded)
2724 pfvec = &ddata->fvec;
2725 read_function_info (state, ddata, &lhdr, error_callback, data,
2726 entry->u, pfvec, &function_addrs,
2727 &function_addrs_count);
2728 free_line_header (state, &lhdr, error_callback, data);
2732 /* Atomically store the information we just read into the unit.
2733 If another thread is simultaneously writing, it presumably
2734 read the same information, and we don't care which one we
2735 wind up with; we just leak the other one. We do have to
2736 write the lines field last, so that the acquire-loads above
2737 ensure that the other fields are set. */
2739 if (!state->threaded)
2741 u->lines_count = count;
2742 u->function_addrs = function_addrs;
2743 u->function_addrs_count = function_addrs_count;
2748 backtrace_atomic_store_size_t (&u->lines_count, count);
2749 backtrace_atomic_store_pointer (&u->function_addrs, function_addrs);
2750 backtrace_atomic_store_size_t (&u->function_addrs_count,
2751 function_addrs_count);
2752 backtrace_atomic_store_pointer (&u->lines, lines);
2756 /* Now all fields of U have been initialized. */
2758 if (lines == (struct line *) (uintptr_t) -1)
2760 /* If reading the line number information failed in some way,
2761 try again to see if there is a better compilation unit for
2764 return dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2766 return callback (data, pc, NULL, 0, NULL);
2769 /* Search for PC within this unit. */
2771 ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
2772 sizeof (struct line), line_search);
2775 /* The PC is between the low_pc and high_pc attributes of the
2776 compilation unit, but no entry in the line table covers it.
2777 This implies that the start of the compilation unit has no
2778 line number information. */
2780 if (entry->u->abs_filename == NULL)
2782 const char *filename;
2784 filename = entry->u->filename;
2785 if (filename != NULL
2786 && !IS_ABSOLUTE_PATH (filename)
2787 && entry->u->comp_dir != NULL)
2789 size_t filename_len;
2794 filename_len = strlen (filename);
2795 dir = entry->u->comp_dir;
2796 dir_len = strlen (dir);
2797 s = (char *) backtrace_alloc (state, dir_len + filename_len + 2,
2798 error_callback, data);
2804 memcpy (s, dir, dir_len);
2805 /* FIXME: Should use backslash if DOS file system. */
2807 memcpy (s + dir_len + 1, filename, filename_len + 1);
2810 entry->u->abs_filename = filename;
2813 return callback (data, pc, entry->u->abs_filename, 0, NULL);
2816 /* Search for function name within this unit. */
2818 if (entry->u->function_addrs_count == 0)
2819 return callback (data, pc, ln->filename, ln->lineno, NULL);
2821 function_addrs = ((struct function_addrs *)
2822 bsearch (&pc, entry->u->function_addrs,
2823 entry->u->function_addrs_count,
2824 sizeof (struct function_addrs),
2825 function_addrs_search));
2826 if (function_addrs == NULL)
2827 return callback (data, pc, ln->filename, ln->lineno, NULL);
2829 /* If there are multiple function ranges that contain PC, use the
2830 last one, in order to produce predictable results. */
2832 while (((size_t) (function_addrs - entry->u->function_addrs + 1)
2833 < entry->u->function_addrs_count)
2834 && pc >= (function_addrs + 1)->low
2835 && pc < (function_addrs + 1)->high)
2838 function = function_addrs->function;
2840 filename = ln->filename;
2841 lineno = ln->lineno;
2843 ret = report_inlined_functions (pc, function, callback, data,
2844 &filename, &lineno);
2848 return callback (data, pc, filename, lineno, function->name);
2852 /* Return the file/line information for a PC using the DWARF mapping
2853 we built earlier. */
2856 dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
2857 backtrace_full_callback callback,
2858 backtrace_error_callback error_callback, void *data)
2860 struct dwarf_data *ddata;
2864 if (!state->threaded)
2866 for (ddata = (struct dwarf_data *) state->fileline_data;
2868 ddata = ddata->next)
2870 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2872 if (ret != 0 || found)
2878 struct dwarf_data **pp;
2880 pp = (struct dwarf_data **) (void *) &state->fileline_data;
2883 ddata = backtrace_atomic_load_pointer (pp);
2887 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
2889 if (ret != 0 || found)
2896 /* FIXME: See if any libraries have been dlopen'ed. */
2898 return callback (data, pc, NULL, 0, NULL);
2901 /* Initialize our data structures from the DWARF debug info for a
2902 file. Return NULL on failure. */
2904 static struct dwarf_data *
2905 build_dwarf_data (struct backtrace_state *state,
2906 uintptr_t base_address,
2907 const unsigned char *dwarf_info,
2908 size_t dwarf_info_size,
2909 const unsigned char *dwarf_line,
2910 size_t dwarf_line_size,
2911 const unsigned char *dwarf_abbrev,
2912 size_t dwarf_abbrev_size,
2913 const unsigned char *dwarf_ranges,
2914 size_t dwarf_ranges_size,
2915 const unsigned char *dwarf_str,
2916 size_t dwarf_str_size,
2918 backtrace_error_callback error_callback,
2921 struct unit_addrs_vector addrs_vec;
2922 struct unit_addrs *addrs;
2924 struct dwarf_data *fdata;
2926 if (!build_address_map (state, base_address, dwarf_info, dwarf_info_size,
2927 dwarf_abbrev, dwarf_abbrev_size, dwarf_ranges,
2928 dwarf_ranges_size, dwarf_str, dwarf_str_size,
2929 is_bigendian, error_callback, data, &addrs_vec))
2932 if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
2934 addrs = (struct unit_addrs *) addrs_vec.vec.base;
2935 addrs_count = addrs_vec.count;
2936 qsort (addrs, addrs_count, sizeof (struct unit_addrs), unit_addrs_compare);
2938 fdata = ((struct dwarf_data *)
2939 backtrace_alloc (state, sizeof (struct dwarf_data),
2940 error_callback, data));
2945 fdata->base_address = base_address;
2946 fdata->addrs = addrs;
2947 fdata->addrs_count = addrs_count;
2948 fdata->dwarf_info = dwarf_info;
2949 fdata->dwarf_info_size = dwarf_info_size;
2950 fdata->dwarf_line = dwarf_line;
2951 fdata->dwarf_line_size = dwarf_line_size;
2952 fdata->dwarf_ranges = dwarf_ranges;
2953 fdata->dwarf_ranges_size = dwarf_ranges_size;
2954 fdata->dwarf_str = dwarf_str;
2955 fdata->dwarf_str_size = dwarf_str_size;
2956 fdata->is_bigendian = is_bigendian;
2957 memset (&fdata->fvec, 0, sizeof fdata->fvec);
2962 /* Build our data structures from the DWARF sections for a module.
2963 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
2967 backtrace_dwarf_add (struct backtrace_state *state,
2968 uintptr_t base_address,
2969 const unsigned char *dwarf_info,
2970 size_t dwarf_info_size,
2971 const unsigned char *dwarf_line,
2972 size_t dwarf_line_size,
2973 const unsigned char *dwarf_abbrev,
2974 size_t dwarf_abbrev_size,
2975 const unsigned char *dwarf_ranges,
2976 size_t dwarf_ranges_size,
2977 const unsigned char *dwarf_str,
2978 size_t dwarf_str_size,
2980 backtrace_error_callback error_callback,
2981 void *data, fileline *fileline_fn)
2983 struct dwarf_data *fdata;
2985 fdata = build_dwarf_data (state, base_address, dwarf_info, dwarf_info_size,
2986 dwarf_line, dwarf_line_size, dwarf_abbrev,
2987 dwarf_abbrev_size, dwarf_ranges, dwarf_ranges_size,
2988 dwarf_str, dwarf_str_size, is_bigendian,
2989 error_callback, data);
2993 if (!state->threaded)
2995 struct dwarf_data **pp;
2997 for (pp = (struct dwarf_data **) (void *) &state->fileline_data;
3007 struct dwarf_data **pp;
3009 pp = (struct dwarf_data **) (void *) &state->fileline_data;
3013 struct dwarf_data *p;
3015 p = backtrace_atomic_load_pointer (pp);
3023 if (__sync_bool_compare_and_swap (pp, NULL, fdata))
3028 *fileline_fn = dwarf_fileline;