1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012 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"
46 #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
48 /* If strnlen is not declared, provide our own version. */
51 xstrnlen (const char *s
, size_t maxlen
)
55 for (i
= 0; i
< maxlen
; ++i
)
61 #define strnlen xstrnlen
65 /* A buffer to read DWARF info. */
69 /* Buffer name for error messages. */
71 /* Start of the buffer. */
72 const unsigned char *start
;
73 /* Next byte to read. */
74 const unsigned char *buf
;
75 /* The number of bytes remaining. */
77 /* Whether the data is big-endian. */
79 /* Error callback routine. */
80 backtrace_error_callback error_callback
;
81 /* Data for error_callback. */
83 /* Non-zero if we've reported an underflow error. */
84 int reported_underflow
;
87 /* A single attribute in a DWARF abbreviation. */
91 /* The attribute name. */
92 enum dwarf_attribute name
;
93 /* The attribute form. */
97 /* A single DWARF abbreviation. */
101 /* The abbrev code--the number used to refer to the abbrev. */
105 /* Non-zero if this abbrev has child entries. */
107 /* The number of attributes. */
109 /* The attributes. */
113 /* The DWARF abbreviations for a compilation unit. This structure
114 only exists while reading the compilation unit. Most DWARF readers
115 seem to a hash table to map abbrev ID's to abbrev entries.
116 However, we primarily care about GCC, and GCC simply issues ID's in
117 numerical order starting at 1. So we simply keep a sorted vector,
118 and try to just look up the code. */
122 /* The number of abbrevs in the vector. */
124 /* The abbrevs, sorted by the code field. */
125 struct abbrev
*abbrevs
;
128 /* The different kinds of attribute values. */
130 enum attr_val_encoding
134 /* A unsigned integer. */
136 /* A sigd integer. */
140 /* An offset to other data in the containing unit. */
142 /* An offset to other data within the .dwarf_info section. */
144 /* An offset to data in some other section. */
145 ATTR_VAL_REF_SECTION
,
146 /* A type signature. */
148 /* A block of data (not represented). */
150 /* An expression (not represented). */
154 /* An attribute value. */
158 /* How the value is stored in the field u. */
159 enum attr_val_encoding encoding
;
162 /* ATTR_VAL_ADDRESS, ATTR_VAL_UINT, ATTR_VAL_REF*. */
166 /* ATTR_VAL_STRING. */
168 /* ATTR_VAL_BLOCK not stored. */
172 /* The line number program header. */
176 /* The version of the line number information. */
178 /* The minimum instruction length. */
179 unsigned int min_insn_len
;
180 /* The maximum number of ops per instruction. */
181 unsigned int max_ops_per_insn
;
182 /* The line base for special opcodes. */
184 /* The line range for special opcodes. */
185 unsigned int line_range
;
186 /* The opcode base--the first special opcode. */
187 unsigned int opcode_base
;
188 /* Opcode lengths, indexed by opcode - 1. */
189 const unsigned char *opcode_lengths
;
190 /* The number of directory entries. */
192 /* The directory entries. */
194 /* The number of filenames. */
195 size_t filenames_count
;
197 const char **filenames
;
200 /* Map a single PC value to a file/line. We will keep a vector of
201 these sorted by PC value. Each file/line will be correct from the
202 PC up to the PC of the next entry if there is one. We allocate one
203 extra entry at the end so that we can use bsearch. */
209 /* File name. Many entries in the array are expected to point to
210 the same file name. */
211 const char *filename
;
216 /* A growable vector of line number information. This is used while
217 reading the line numbers. */
221 /* Memory. This is an array of struct line. */
222 struct backtrace_vector vec
;
223 /* Number of valid mappings. */
227 /* A function described in the debug info. */
231 /* The name of the function. */
233 /* If this is an inlined function, the filename of the call
235 const char *caller_filename
;
236 /* If this is an inlined function, the line number of the call
239 /* Map PC ranges to inlined functions. */
240 struct function_addrs
*function_addrs
;
241 size_t function_addrs_count
;
244 /* An address range for a function. This maps a PC value to a
245 specific function. */
247 struct function_addrs
249 /* Range is LOW <= PC < HIGH. */
252 /* Function for this address range. */
253 struct function
*function
;
256 /* A growable vector of function address ranges. */
258 struct function_vector
260 /* Memory. This is an array of struct function_addrs. */
261 struct backtrace_vector vec
;
262 /* Number of address ranges present. */
266 /* A DWARF compilation unit. This only holds the information we need
267 to map a PC to a file and line. */
271 /* The first entry for this compilation unit. */
272 const unsigned char *unit_data
;
273 /* The length of the data for this compilation unit. */
274 size_t unit_data_len
;
275 /* The offset of UNIT_DATA from the start of the information for
276 this compilation unit. */
277 size_t unit_data_offset
;
280 /* Whether unit is DWARF64. */
284 /* Offset into line number information. */
286 /* Compilation command working directory. */
287 const char *comp_dir
;
288 /* The abbreviations for this unit. */
289 struct abbrevs abbrevs
;
291 /* The fields above this point are read in during initialization and
292 may be accessed freely. The fields below this point are read in
293 as needed, and therefore require care, as different threads may
294 try to initialize them simultaneously. */
296 /* PC to line number mapping. This is NULL if the values have not
297 been read. This is (struct line *) -1 if there was an error
298 reading the values. */
300 /* Number of entries in lines. */
302 /* PC ranges to function. */
303 struct function_addrs
*function_addrs
;
304 size_t function_addrs_count
;
307 /* An address range for a compilation unit. This maps a PC value to a
308 specific compilation unit. Note that we invert the representation
309 in DWARF: instead of listing the units and attaching a list of
310 ranges, we list the ranges and have each one point to the unit.
311 This lets us do a binary search to find the unit. */
315 /* Range is LOW <= PC < HIGH. */
318 /* Compilation unit for this address range. */
322 /* A growable vector of compilation unit address ranges. */
324 struct unit_addrs_vector
326 /* Memory. This is an array of struct unit_addrs. */
327 struct backtrace_vector vec
;
328 /* Number of address ranges present. */
332 /* The information we need to map a PC to a file and line. */
336 /* A sorted list of address ranges. */
337 struct unit_addrs
*addrs
;
338 /* Number of address ranges in list. */
340 /* The unparsed .debug_info section. */
341 const unsigned char *dwarf_info
;
342 size_t dwarf_info_size
;
343 /* The unparsed .debug_line section. */
344 const unsigned char *dwarf_line
;
345 size_t dwarf_line_size
;
346 /* The unparsed .debug_ranges section. */
347 const unsigned char *dwarf_ranges
;
348 size_t dwarf_ranges_size
;
349 /* The unparsed .debug_str section. */
350 const unsigned char *dwarf_str
;
351 size_t dwarf_str_size
;
352 /* Whether the data is big-endian or not. */
354 /* A vector used for function addresses. We keep this here so that
355 we can grow the vector as we read more functions. */
356 struct function_vector fvec
;
359 /* Report an error for a DWARF buffer. */
362 dwarf_buf_error (struct dwarf_buf
*buf
, const char *msg
)
366 snprintf (b
, sizeof b
, "%s in %s at %d",
367 msg
, buf
->name
, (int) (buf
->buf
- buf
->start
));
368 buf
->error_callback (buf
->data
, b
, 0);
371 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
375 require (struct dwarf_buf
*buf
, size_t count
)
377 if (buf
->left
>= count
)
380 if (!buf
->reported_underflow
)
382 dwarf_buf_error (buf
, "DWARF underflow");
383 buf
->reported_underflow
= 1;
389 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
393 advance (struct dwarf_buf
*buf
, size_t count
)
395 if (!require (buf
, count
))
402 /* Read one byte from BUF and advance 1 byte. */
405 read_byte (struct dwarf_buf
*buf
)
407 const unsigned char *p
= buf
->buf
;
409 if (!advance (buf
, 1))
414 /* Read a signed char from BUF and advance 1 byte. */
417 read_sbyte (struct dwarf_buf
*buf
)
419 const unsigned char *p
= buf
->buf
;
421 if (!advance (buf
, 1))
423 return (*p
^ 0x80) - 0x80;
426 /* Read a uint16 from BUF and advance 2 bytes. */
429 read_uint16 (struct dwarf_buf
*buf
)
431 const unsigned char *p
= buf
->buf
;
433 if (!advance (buf
, 2))
435 if (buf
->is_bigendian
)
436 return ((uint16_t) p
[0] << 8) | (uint16_t) p
[1];
438 return ((uint16_t) p
[1] << 8) | (uint16_t) p
[0];
441 /* Read a uint32 from BUF and advance 4 bytes. */
444 read_uint32 (struct dwarf_buf
*buf
)
446 const unsigned char *p
= buf
->buf
;
448 if (!advance (buf
, 4))
450 if (buf
->is_bigendian
)
451 return (((uint32_t) p
[0] << 24) | ((uint32_t) p
[1] << 16)
452 | ((uint32_t) p
[2] << 8) | (uint32_t) p
[3]);
454 return (((uint32_t) p
[3] << 24) | ((uint32_t) p
[2] << 16)
455 | ((uint32_t) p
[1] << 8) | (uint32_t) p
[0]);
458 /* Read a uint64 from BUF and advance 8 bytes. */
461 read_uint64 (struct dwarf_buf
*buf
)
463 const unsigned char *p
= buf
->buf
;
465 if (!advance (buf
, 8))
467 if (buf
->is_bigendian
)
468 return (((uint64_t) p
[0] << 56) | ((uint64_t) p
[1] << 48)
469 | ((uint64_t) p
[2] << 40) | ((uint64_t) p
[3] << 32)
470 | ((uint64_t) p
[4] << 24) | ((uint64_t) p
[5] << 16)
471 | ((uint64_t) p
[6] << 8) | (uint64_t) p
[7]);
473 return (((uint64_t) p
[7] << 56) | ((uint64_t) p
[6] << 48)
474 | ((uint64_t) p
[5] << 40) | ((uint64_t) p
[4] << 32)
475 | ((uint64_t) p
[3] << 24) | ((uint64_t) p
[2] << 16)
476 | ((uint64_t) p
[1] << 8) | (uint64_t) p
[0]);
479 /* Read an offset from BUF and advance the appropriate number of
483 read_offset (struct dwarf_buf
*buf
, int is_dwarf64
)
486 return read_uint64 (buf
);
488 return read_uint32 (buf
);
491 /* Read an address from BUF and advance the appropriate number of
495 read_address (struct dwarf_buf
*buf
, int addrsize
)
500 return read_byte (buf
);
502 return read_uint16 (buf
);
504 return read_uint32 (buf
);
506 return read_uint64 (buf
);
508 dwarf_buf_error (buf
, "unrecognized address size");
513 /* Return whether a value is the highest possible address, given the
517 is_highest_address (uint64_t address
, int addrsize
)
522 return address
== (unsigned char) -1;
524 return address
== (uint16_t) -1;
526 return address
== (uint32_t) -1;
528 return address
== (uint64_t) -1;
534 /* Read an unsigned LEB128 number. */
537 read_uleb128 (struct dwarf_buf
*buf
)
549 const unsigned char *p
;
552 if (!advance (buf
, 1))
556 ret
|= ((uint64_t) (b
& 0x7f)) << shift
;
559 dwarf_buf_error (buf
, "LEB128 overflows uint64_t");
564 while ((b
& 0x80) != 0);
569 /* Read a signed LEB128 number. */
572 read_sleb128 (struct dwarf_buf
*buf
)
584 const unsigned char *p
;
587 if (!advance (buf
, 1))
591 val
|= ((uint64_t) (b
& 0x7f)) << shift
;
594 dwarf_buf_error (buf
, "signed LEB128 overflows uint64_t");
599 while ((b
& 0x80) != 0);
601 if ((b
& 0x40) != 0 && shift
< 64)
602 val
|= ((uint64_t) -1) << shift
;
604 return (int64_t) val
;
607 /* Return the length of an LEB128 number. */
610 leb128_len (const unsigned char *p
)
615 while ((*p
& 0x80) != 0)
623 /* Free an abbreviations structure. */
626 free_abbrevs (struct backtrace_state
*state
, struct abbrevs
*abbrevs
,
627 backtrace_error_callback error_callback
, void *data
)
631 for (i
= 0; i
< abbrevs
->num_abbrevs
; ++i
)
632 backtrace_free (state
, abbrevs
->abbrevs
[i
].attrs
,
633 abbrevs
->abbrevs
[i
].num_attrs
* sizeof (struct attr
),
634 error_callback
, data
);
635 backtrace_free (state
, abbrevs
->abbrevs
,
636 abbrevs
->num_abbrevs
* sizeof (struct abbrev
),
637 error_callback
, data
);
638 abbrevs
->num_abbrevs
= 0;
639 abbrevs
->abbrevs
= NULL
;
642 /* Read an attribute value. Returns 1 on success, 0 on failure. If
643 the value can be represented as a uint64_t, sets *VAL and sets
644 *IS_VALID to 1. We don't try to store the value of other attribute
645 forms, because we don't care about them. */
648 read_attribute (enum dwarf_form form
, struct dwarf_buf
*buf
,
649 int is_dwarf64
, int version
, int addrsize
,
650 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
651 struct attr_val
*val
)
656 val
->encoding
= ATTR_VAL_ADDRESS
;
657 val
->u
.uint
= read_address (buf
, addrsize
);
660 val
->encoding
= ATTR_VAL_BLOCK
;
661 return advance (buf
, read_uint16 (buf
));
663 val
->encoding
= ATTR_VAL_BLOCK
;
664 return advance (buf
, read_uint32 (buf
));
666 val
->encoding
= ATTR_VAL_UINT
;
667 val
->u
.uint
= read_uint16 (buf
);
670 val
->encoding
= ATTR_VAL_UINT
;
671 val
->u
.uint
= read_uint32 (buf
);
674 val
->encoding
= ATTR_VAL_UINT
;
675 val
->u
.uint
= read_uint64 (buf
);
678 val
->encoding
= ATTR_VAL_STRING
;
679 val
->u
.string
= (const char *) buf
->buf
;
680 return advance (buf
, strnlen ((const char *) buf
->buf
, buf
->left
) + 1);
682 val
->encoding
= ATTR_VAL_BLOCK
;
683 return advance (buf
, read_uleb128 (buf
));
685 val
->encoding
= ATTR_VAL_BLOCK
;
686 return advance (buf
, read_byte (buf
));
688 val
->encoding
= ATTR_VAL_UINT
;
689 val
->u
.uint
= read_byte (buf
);
692 val
->encoding
= ATTR_VAL_UINT
;
693 val
->u
.uint
= read_byte (buf
);
696 val
->encoding
= ATTR_VAL_SINT
;
697 val
->u
.sint
= read_sleb128 (buf
);
703 offset
= read_offset (buf
, is_dwarf64
);
704 if (offset
>= dwarf_str_size
)
706 dwarf_buf_error (buf
, "DW_FORM_strp out of range");
709 val
->encoding
= ATTR_VAL_STRING
;
710 val
->u
.string
= (const char *) dwarf_str
+ offset
;
714 val
->encoding
= ATTR_VAL_UINT
;
715 val
->u
.uint
= read_uleb128 (buf
);
717 case DW_FORM_ref_addr
:
718 val
->encoding
= ATTR_VAL_REF_INFO
;
720 val
->u
.uint
= read_address (buf
, addrsize
);
722 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
725 val
->encoding
= ATTR_VAL_REF_UNIT
;
726 val
->u
.uint
= read_byte (buf
);
729 val
->encoding
= ATTR_VAL_REF_UNIT
;
730 val
->u
.uint
= read_uint16 (buf
);
733 val
->encoding
= ATTR_VAL_REF_UNIT
;
734 val
->u
.uint
= read_uint32 (buf
);
737 val
->encoding
= ATTR_VAL_REF_UNIT
;
738 val
->u
.uint
= read_uint64 (buf
);
740 case DW_FORM_ref_udata
:
741 val
->encoding
= ATTR_VAL_REF_UNIT
;
742 val
->u
.uint
= read_uleb128 (buf
);
744 case DW_FORM_indirect
:
748 form
= read_uleb128 (buf
);
749 return read_attribute ((enum dwarf_form
) form
, buf
, is_dwarf64
,
750 version
, addrsize
, dwarf_str
, dwarf_str_size
,
753 case DW_FORM_sec_offset
:
754 val
->encoding
= ATTR_VAL_REF_SECTION
;
755 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
757 case DW_FORM_exprloc
:
758 val
->encoding
= ATTR_VAL_EXPR
;
759 return advance (buf
, read_uleb128 (buf
));
760 case DW_FORM_flag_present
:
761 val
->encoding
= ATTR_VAL_UINT
;
764 case DW_FORM_ref_sig8
:
765 val
->encoding
= ATTR_VAL_REF_TYPE
;
766 val
->u
.uint
= read_uint64 (buf
);
768 case DW_FORM_GNU_addr_index
:
769 val
->encoding
= ATTR_VAL_REF_SECTION
;
770 val
->u
.uint
= read_uleb128 (buf
);
772 case DW_FORM_GNU_str_index
:
773 val
->encoding
= ATTR_VAL_REF_SECTION
;
774 val
->u
.uint
= read_uleb128 (buf
);
776 case DW_FORM_GNU_ref_alt
:
777 val
->encoding
= ATTR_VAL_REF_SECTION
;
778 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
780 case DW_FORM_GNU_strp_alt
:
781 val
->encoding
= ATTR_VAL_REF_SECTION
;
782 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
785 dwarf_buf_error (buf
, "unrecognized DWARF form");
790 /* Compare function_addrs for qsort. When ranges are nested, make the
791 smallest one sort last. */
794 function_addrs_compare (const void *v1
, const void *v2
)
796 const struct function_addrs
*a1
= (const struct function_addrs
*) v1
;
797 const struct function_addrs
*a2
= (const struct function_addrs
*) v2
;
799 if (a1
->low
< a2
->low
)
801 if (a1
->low
> a2
->low
)
803 if (a1
->high
< a2
->high
)
805 if (a1
->high
> a2
->high
)
807 return strcmp (a1
->function
->name
, a2
->function
->name
);
810 /* Compare a PC against a function_addrs for bsearch. Note that if
811 there are multiple ranges containing PC, which one will be returned
812 is unpredictable. We compensate for that in dwarf_fileline. */
815 function_addrs_search (const void *vkey
, const void *ventry
)
817 const uintptr_t *key
= (const uintptr_t *) vkey
;
818 const struct function_addrs
*entry
= (const struct function_addrs
*) ventry
;
824 else if (pc
>= entry
->high
)
830 /* Add a new compilation unit address range to a vector. Returns 1 on
831 success, 0 on failure. */
834 add_unit_addr (struct backtrace_state
*state
, struct unit_addrs addrs
,
835 backtrace_error_callback error_callback
, void *data
,
836 struct unit_addrs_vector
*vec
)
838 struct unit_addrs
*p
;
840 /* Try to merge with the last entry. */
843 p
= (struct unit_addrs
*) vec
->vec
.base
+ (vec
->count
- 1);
844 if ((addrs
.low
== p
->high
|| addrs
.low
== p
->high
+ 1)
847 if (addrs
.high
> p
->high
)
848 p
->high
= addrs
.high
;
853 p
= ((struct unit_addrs
*)
854 backtrace_vector_grow (state
, sizeof (struct unit_addrs
),
855 error_callback
, data
, &vec
->vec
));
864 /* Free a unit address vector. */
867 free_unit_addrs_vector (struct backtrace_state
*state
,
868 struct unit_addrs_vector
*vec
,
869 backtrace_error_callback error_callback
, void *data
)
871 struct unit_addrs
*addrs
;
874 addrs
= (struct unit_addrs
*) vec
->vec
.base
;
875 for (i
= 0; i
< vec
->count
; ++i
)
876 free_abbrevs (state
, &addrs
[i
].u
->abbrevs
, error_callback
, data
);
879 /* Compare unit_addrs for qsort. When ranges are nested, make the
880 smallest one sort last. */
883 unit_addrs_compare (const void *v1
, const void *v2
)
885 const struct unit_addrs
*a1
= (const struct unit_addrs
*) v1
;
886 const struct unit_addrs
*a2
= (const struct unit_addrs
*) v2
;
888 if (a1
->low
< a2
->low
)
890 if (a1
->low
> a2
->low
)
892 if (a1
->high
< a2
->high
)
894 if (a1
->high
> a2
->high
)
896 if (a1
->u
->lineoff
< a2
->u
->lineoff
)
898 if (a1
->u
->lineoff
> a2
->u
->lineoff
)
903 /* Compare a PC against a unit_addrs for bsearch. Note that if there
904 are multiple ranges containing PC, which one will be returned is
905 unpredictable. We compensate for that in dwarf_fileline. */
908 unit_addrs_search (const void *vkey
, const void *ventry
)
910 const uintptr_t *key
= (const uintptr_t *) vkey
;
911 const struct unit_addrs
*entry
= (const struct unit_addrs
*) ventry
;
917 else if (pc
>= entry
->high
)
923 /* Sort the line vector by PC. We want a stable sort here. We know
924 that the pointers are into the same array, so it is safe to compare
928 line_compare (const void *v1
, const void *v2
)
930 const struct line
*ln1
= (const struct line
*) v1
;
931 const struct line
*ln2
= (const struct line
*) v2
;
933 if (ln1
->pc
< ln2
->pc
)
935 else if (ln1
->pc
> ln2
->pc
)
945 /* Find a PC in a line vector. We always allocate an extra entry at
946 the end of the lines vector, so that this routine can safely look
947 at the next entry. Note that when there are multiple mappings for
948 the same PC value, this will return the last one. */
951 line_search (const void *vkey
, const void *ventry
)
953 const uintptr_t *key
= (const uintptr_t *) vkey
;
954 const struct line
*entry
= (const struct line
*) ventry
;
960 else if (pc
>= (entry
+ 1)->pc
)
966 /* Sort the abbrevs by the abbrev code. This function is passed to
967 both qsort and bsearch. */
970 abbrev_compare (const void *v1
, const void *v2
)
972 const struct abbrev
*a1
= (const struct abbrev
*) v1
;
973 const struct abbrev
*a2
= (const struct abbrev
*) v2
;
975 if (a1
->code
< a2
->code
)
977 else if (a1
->code
> a2
->code
)
981 /* This really shouldn't happen. It means there are two
982 different abbrevs with the same code, and that means we don't
983 know which one lookup_abbrev should return. */
988 /* Read the abbreviation table for a compilation unit. Returns 1 on
989 success, 0 on failure. */
992 read_abbrevs (struct backtrace_state
*state
, uint64_t abbrev_offset
,
993 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
994 int is_bigendian
, backtrace_error_callback error_callback
,
995 void *data
, struct abbrevs
*abbrevs
)
997 struct dwarf_buf abbrev_buf
;
998 struct dwarf_buf count_buf
;
1001 abbrevs
->num_abbrevs
= 0;
1002 abbrevs
->abbrevs
= NULL
;
1004 if (abbrev_offset
>= dwarf_abbrev_size
)
1006 error_callback (data
, "abbrev offset out of range", 0);
1010 abbrev_buf
.name
= ".debug_abbrev";
1011 abbrev_buf
.start
= dwarf_abbrev
;
1012 abbrev_buf
.buf
= dwarf_abbrev
+ abbrev_offset
;
1013 abbrev_buf
.left
= dwarf_abbrev_size
- abbrev_offset
;
1014 abbrev_buf
.is_bigendian
= is_bigendian
;
1015 abbrev_buf
.error_callback
= error_callback
;
1016 abbrev_buf
.data
= data
;
1017 abbrev_buf
.reported_underflow
= 0;
1019 /* Count the number of abbrevs in this list. */
1021 count_buf
= abbrev_buf
;
1023 while (read_uleb128 (&count_buf
) != 0)
1025 if (count_buf
.reported_underflow
)
1029 read_uleb128 (&count_buf
);
1030 // Skip has_children.
1031 read_byte (&count_buf
);
1033 while (read_uleb128 (&count_buf
) != 0)
1034 read_uleb128 (&count_buf
);
1035 // Skip form of last attribute.
1036 read_uleb128 (&count_buf
);
1039 if (count_buf
.reported_underflow
)
1042 if (num_abbrevs
== 0)
1045 abbrevs
->num_abbrevs
= num_abbrevs
;
1046 abbrevs
->abbrevs
= ((struct abbrev
*)
1047 backtrace_alloc (state
,
1048 num_abbrevs
* sizeof (struct abbrev
),
1049 error_callback
, data
));
1050 if (abbrevs
->abbrevs
== NULL
)
1052 memset (abbrevs
->abbrevs
, 0, num_abbrevs
* sizeof (struct abbrev
));
1062 if (abbrev_buf
.reported_underflow
)
1065 code
= read_uleb128 (&abbrev_buf
);
1070 a
.tag
= (enum dwarf_tag
) read_uleb128 (&abbrev_buf
);
1071 a
.has_children
= read_byte (&abbrev_buf
);
1073 count_buf
= abbrev_buf
;
1075 while (read_uleb128 (&count_buf
) != 0)
1078 read_uleb128 (&count_buf
);
1084 read_uleb128 (&abbrev_buf
);
1085 read_uleb128 (&abbrev_buf
);
1089 attrs
= ((struct attr
*)
1090 backtrace_alloc (state
, num_attrs
* sizeof *attrs
,
1091 error_callback
, data
));
1100 name
= read_uleb128 (&abbrev_buf
);
1101 form
= read_uleb128 (&abbrev_buf
);
1104 attrs
[num_attrs
].name
= (enum dwarf_attribute
) name
;
1105 attrs
[num_attrs
].form
= (enum dwarf_form
) form
;
1110 a
.num_attrs
= num_attrs
;
1113 abbrevs
->abbrevs
[num_abbrevs
] = a
;
1117 qsort (abbrevs
->abbrevs
, abbrevs
->num_abbrevs
, sizeof (struct abbrev
),
1123 free_abbrevs (state
, abbrevs
, error_callback
, data
);
1127 /* Return the abbrev information for an abbrev code. */
1129 static const struct abbrev
*
1130 lookup_abbrev (struct abbrevs
*abbrevs
, uint64_t code
,
1131 backtrace_error_callback error_callback
, void *data
)
1136 /* With GCC, where abbrevs are simply numbered in order, we should
1137 be able to just look up the entry. */
1138 if (code
- 1 < abbrevs
->num_abbrevs
1139 && abbrevs
->abbrevs
[code
- 1].code
== code
)
1140 return &abbrevs
->abbrevs
[code
- 1];
1142 /* Otherwise we have to search. */
1143 memset (&key
, 0, sizeof key
);
1145 p
= bsearch (&key
, abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1146 sizeof (struct abbrev
), abbrev_compare
);
1149 error_callback (data
, "invalid abbreviation code", 0);
1152 return (const struct abbrev
*) p
;
1155 /* Add non-contiguous address ranges for a compilation unit. Returns
1156 1 on success, 0 on failure. */
1159 add_unit_ranges (struct backtrace_state
*state
, struct unit
*u
,
1160 uint64_t ranges
, uint64_t base
, int is_bigendian
,
1161 const unsigned char *dwarf_ranges
, size_t dwarf_ranges_size
,
1162 backtrace_error_callback error_callback
, void *data
,
1163 struct unit_addrs_vector
*addrs
)
1165 struct dwarf_buf ranges_buf
;
1167 if (ranges
>= dwarf_ranges_size
)
1169 error_callback (data
, "ranges offset out of range", 0);
1173 ranges_buf
.name
= ".debug_ranges";
1174 ranges_buf
.start
= dwarf_ranges
;
1175 ranges_buf
.buf
= dwarf_ranges
+ ranges
;
1176 ranges_buf
.left
= dwarf_ranges_size
- ranges
;
1177 ranges_buf
.is_bigendian
= is_bigendian
;
1178 ranges_buf
.error_callback
= error_callback
;
1179 ranges_buf
.data
= data
;
1180 ranges_buf
.reported_underflow
= 0;
1187 if (ranges_buf
.reported_underflow
)
1190 low
= read_address (&ranges_buf
, u
->addrsize
);
1191 high
= read_address (&ranges_buf
, u
->addrsize
);
1193 if (low
== 0 && high
== 0)
1196 if (is_highest_address (low
, u
->addrsize
))
1200 struct unit_addrs a
;
1203 a
.high
= high
+ base
;
1205 if (!add_unit_addr (state
, a
, error_callback
, data
, addrs
))
1210 if (ranges_buf
.reported_underflow
)
1216 /* Build a mapping from address ranges to the compilation units where
1217 the line number information for that range can be found. Returns 1
1218 on success, 0 on failure. */
1221 build_address_map (struct backtrace_state
*state
,
1222 const unsigned char *dwarf_info
, size_t dwarf_info_size
,
1223 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1224 const unsigned char *dwarf_ranges
, size_t dwarf_ranges_size
,
1225 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
1226 int is_bigendian
, backtrace_error_callback error_callback
,
1227 void *data
, struct unit_addrs_vector
*addrs
)
1229 struct dwarf_buf info
;
1230 struct abbrevs abbrevs
;
1232 memset (&addrs
->vec
, 0, sizeof addrs
->vec
);
1235 /* Read through the .debug_info section. FIXME: Should we use the
1236 .debug_aranges section? gdb and addr2line don't use it, but I'm
1239 info
.name
= ".debug_info";
1240 info
.start
= dwarf_info
;
1241 info
.buf
= dwarf_info
;
1242 info
.left
= dwarf_info_size
;
1243 info
.is_bigendian
= is_bigendian
;
1244 info
.error_callback
= error_callback
;
1246 info
.reported_underflow
= 0;
1248 memset (&abbrevs
, 0, sizeof abbrevs
);
1249 while (info
.left
> 0)
1251 const unsigned char *unit_data_start
;
1254 struct dwarf_buf unit_buf
;
1256 uint64_t abbrev_offset
;
1257 const struct abbrev
*abbrev
;
1259 const unsigned char *unit_data
;
1260 size_t unit_data_len
;
1261 size_t unit_data_offset
;
1268 int highpc_is_relative
;
1273 const char *comp_dir
;
1275 if (info
.reported_underflow
)
1278 unit_data_start
= info
.buf
;
1281 len
= read_uint32 (&info
);
1282 if (len
== 0xffffffff)
1284 len
= read_uint64 (&info
);
1289 unit_buf
.left
= len
;
1291 if (!advance (&info
, len
))
1294 version
= read_uint16 (&unit_buf
);
1295 if (version
< 2 || version
> 4)
1297 dwarf_buf_error (&unit_buf
, "unrecognized DWARF version");
1301 abbrev_offset
= read_offset (&unit_buf
, is_dwarf64
);
1302 if (!read_abbrevs (state
, abbrev_offset
, dwarf_abbrev
, dwarf_abbrev_size
,
1303 is_bigendian
, error_callback
, data
, &abbrevs
))
1306 addrsize
= read_byte (&unit_buf
);
1308 unit_data
= unit_buf
.buf
;
1309 unit_data_len
= unit_buf
.left
;
1310 unit_data_offset
= unit_buf
.buf
- unit_data_start
;
1312 /* We only look at the first attribute in the compilation unit.
1313 In practice this will be a DW_TAG_compile_unit which will
1314 tell us the PC range and where to find the line number
1317 code
= read_uleb128 (&unit_buf
);
1318 abbrev
= lookup_abbrev (&abbrevs
, code
, error_callback
, data
);
1326 highpc_is_relative
= 0;
1332 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1334 struct attr_val val
;
1336 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
, is_dwarf64
,
1337 version
, addrsize
, dwarf_str
, dwarf_str_size
,
1341 switch (abbrev
->attrs
[i
].name
)
1344 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1351 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1353 highpc
= val
.u
.uint
;
1356 else if (val
.encoding
== ATTR_VAL_UINT
)
1358 highpc
= val
.u
.uint
;
1360 highpc_is_relative
= 1;
1364 if (val
.encoding
== ATTR_VAL_UINT
1365 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1367 ranges
= val
.u
.uint
;
1371 case DW_AT_stmt_list
:
1372 if (val
.encoding
== ATTR_VAL_UINT
1373 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1375 lineoff
= val
.u
.uint
;
1379 case DW_AT_comp_dir
:
1380 if (val
.encoding
== ATTR_VAL_STRING
)
1381 comp_dir
= val
.u
.string
;
1388 if (unit_buf
.reported_underflow
)
1391 if (((have_lowpc
&& have_highpc
) || have_ranges
) && have_lineoff
)
1394 struct unit_addrs a
;
1396 u
= ((struct unit
*)
1397 backtrace_alloc (state
, sizeof *u
, error_callback
, data
));
1400 u
->unit_data
= unit_data
;
1401 u
->unit_data_len
= unit_data_len
;
1402 u
->unit_data_offset
= unit_data_offset
;
1403 u
->version
= version
;
1404 u
->is_dwarf64
= is_dwarf64
;
1405 u
->addrsize
= addrsize
;
1406 u
->comp_dir
= comp_dir
;
1407 u
->lineoff
= lineoff
;
1408 u
->abbrevs
= abbrevs
;
1409 memset (&abbrevs
, 0, sizeof abbrevs
);
1411 /* The actual line number mappings will be read as
1415 u
->function_addrs
= NULL
;
1416 u
->function_addrs_count
= 0;
1420 if (!add_unit_ranges (state
, u
, ranges
, lowpc
, is_bigendian
,
1421 dwarf_ranges
, dwarf_ranges_size
,
1422 error_callback
, data
, addrs
))
1424 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1425 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1431 if (highpc_is_relative
)
1437 if (!add_unit_addr (state
, a
, error_callback
, data
, addrs
))
1439 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1440 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1447 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1448 memset (&abbrevs
, 0, sizeof abbrevs
);
1451 if (info
.reported_underflow
)
1457 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1458 free_unit_addrs_vector (state
, addrs
, error_callback
, data
);
1462 /* Add a new mapping to the vector of line mappings that we are
1463 building. Returns 1 on success, 0 on failure. */
1466 add_line (struct backtrace_state
*state
, uintptr_t pc
, const char *filename
,
1467 int lineno
, backtrace_error_callback error_callback
, void *data
,
1468 struct line_vector
*vec
)
1472 /* If we are adding the same mapping, ignore it. This can happen
1473 when using discriminators. */
1476 ln
= (struct line
*) vec
->vec
.base
+ (vec
->count
- 1);
1477 if (pc
== ln
->pc
&& filename
== ln
->filename
&& lineno
== ln
->lineno
)
1481 ln
= ((struct line
*)
1482 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1488 ln
->filename
= filename
;
1489 ln
->lineno
= lineno
;
1496 /* Free the line header information. If FREE_FILENAMES is true we
1497 free the file names themselves, otherwise we leave them, as there
1498 may be line structures pointing to them. */
1501 free_line_header (struct backtrace_state
*state
, struct line_header
*hdr
,
1502 backtrace_error_callback error_callback
, void *data
)
1504 backtrace_free (state
, hdr
->dirs
, hdr
->dirs_count
* sizeof (const char *),
1505 error_callback
, data
);
1506 backtrace_free (state
, hdr
->filenames
,
1507 hdr
->filenames_count
* sizeof (char *),
1508 error_callback
, data
);
1511 /* Read the line header. Return 1 on success, 0 on failure. */
1514 read_line_header (struct backtrace_state
*state
, struct unit
*u
,
1515 int is_dwarf64
, struct dwarf_buf
*line_buf
,
1516 struct line_header
*hdr
)
1519 struct dwarf_buf hdr_buf
;
1520 const unsigned char *p
;
1521 const unsigned char *pend
;
1524 hdr
->version
= read_uint16 (line_buf
);
1525 if (hdr
->version
< 2 || hdr
->version
> 4)
1527 dwarf_buf_error (line_buf
, "unsupported line number version");
1531 hdrlen
= read_offset (line_buf
, is_dwarf64
);
1533 hdr_buf
= *line_buf
;
1534 hdr_buf
.left
= hdrlen
;
1536 if (!advance (line_buf
, hdrlen
))
1539 hdr
->min_insn_len
= read_byte (&hdr_buf
);
1540 if (hdr
->version
< 4)
1541 hdr
->max_ops_per_insn
= 1;
1543 hdr
->max_ops_per_insn
= read_byte (&hdr_buf
);
1545 /* We don't care about default_is_stmt. */
1546 read_byte (&hdr_buf
);
1548 hdr
->line_base
= read_sbyte (&hdr_buf
);
1549 hdr
->line_range
= read_byte (&hdr_buf
);
1551 hdr
->opcode_base
= read_byte (&hdr_buf
);
1552 hdr
->opcode_lengths
= hdr_buf
.buf
;
1553 if (!advance (&hdr_buf
, hdr
->opcode_base
- 1))
1556 /* Count the number of directory entries. */
1557 hdr
->dirs_count
= 0;
1559 pend
= p
+ hdr_buf
.left
;
1560 while (p
< pend
&& *p
!= '\0')
1562 p
+= strnlen((const char *) p
, pend
- p
) + 1;
1566 hdr
->dirs
= ((const char **)
1567 backtrace_alloc (state
,
1568 hdr
->dirs_count
* sizeof (const char *),
1569 line_buf
->error_callback
, line_buf
->data
));
1570 if (hdr
->dirs
== NULL
)
1574 while (*hdr_buf
.buf
!= '\0')
1576 if (hdr_buf
.reported_underflow
)
1579 hdr
->dirs
[i
] = (const char *) hdr_buf
.buf
;
1581 if (!advance (&hdr_buf
,
1582 strnlen ((const char *) hdr_buf
.buf
, hdr_buf
.left
) + 1))
1585 if (!advance (&hdr_buf
, 1))
1588 /* Count the number of file entries. */
1589 hdr
->filenames_count
= 0;
1591 pend
= p
+ hdr_buf
.left
;
1592 while (p
< pend
&& *p
!= '\0')
1594 p
+= strnlen ((const char *) p
, pend
- p
) + 1;
1595 p
+= leb128_len (p
);
1596 p
+= leb128_len (p
);
1597 p
+= leb128_len (p
);
1598 ++hdr
->filenames_count
;
1601 hdr
->filenames
= ((const char **)
1602 backtrace_alloc (state
,
1603 hdr
->filenames_count
* sizeof (char *),
1604 line_buf
->error_callback
,
1606 if (hdr
->filenames
== NULL
)
1609 while (*hdr_buf
.buf
!= '\0')
1611 const char *filename
;
1614 if (hdr_buf
.reported_underflow
)
1617 filename
= (const char *) hdr_buf
.buf
;
1618 if (!advance (&hdr_buf
,
1619 strnlen ((const char *) hdr_buf
.buf
, hdr_buf
.left
) + 1))
1621 dir_index
= read_uleb128 (&hdr_buf
);
1622 if (IS_ABSOLUTE_PATH (filename
))
1623 hdr
->filenames
[i
] = filename
;
1628 size_t filename_len
;
1633 else if (dir_index
- 1 < hdr
->dirs_count
)
1634 dir
= hdr
->dirs
[dir_index
- 1];
1637 dwarf_buf_error (line_buf
,
1638 ("invalid directory index in "
1639 "line number program header"));
1642 dir_len
= strlen (dir
);
1643 filename_len
= strlen (filename
);
1645 backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
1646 line_buf
->error_callback
, line_buf
->data
));
1649 memcpy (s
, dir
, dir_len
);
1650 /* FIXME: If we are on a DOS-based file system, and the
1651 directory or the file name use backslashes, then we
1652 should use a backslash here. */
1654 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
1655 hdr
->filenames
[i
] = s
;
1658 /* Ignore the modification time and size. */
1659 read_uleb128 (&hdr_buf
);
1660 read_uleb128 (&hdr_buf
);
1665 if (hdr_buf
.reported_underflow
)
1671 /* Read the line program, adding line mappings to VEC. Return 1 on
1672 success, 0 on failure. */
1675 read_line_program (struct backtrace_state
*state
, struct unit
*u
,
1676 const struct line_header
*hdr
, struct dwarf_buf
*line_buf
,
1677 struct line_vector
*vec
)
1680 unsigned int op_index
;
1681 const char *reset_filename
;
1682 const char *filename
;
1687 if (hdr
->filenames_count
> 0)
1688 reset_filename
= hdr
->filenames
[0];
1690 reset_filename
= "";
1691 filename
= reset_filename
;
1693 while (line_buf
->left
> 0)
1697 op
= read_byte (line_buf
);
1698 if (op
>= hdr
->opcode_base
)
1700 unsigned int advance
;
1702 /* Special opcode. */
1703 op
-= hdr
->opcode_base
;
1704 advance
= op
/ hdr
->line_range
;
1705 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1706 / hdr
->max_ops_per_insn
);
1707 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1708 lineno
+= hdr
->line_base
+ (int) (op
% hdr
->line_range
);
1709 add_line (state
, address
, filename
, lineno
, line_buf
->error_callback
,
1710 line_buf
->data
, vec
);
1712 else if (op
== DW_LNS_extended_op
)
1716 len
= read_uleb128 (line_buf
);
1717 op
= read_byte (line_buf
);
1720 case DW_LNE_end_sequence
:
1721 /* FIXME: Should we mark the high PC here? It seems
1722 that we already have that information from the
1723 compilation unit. */
1726 filename
= reset_filename
;
1729 case DW_LNE_set_address
:
1730 address
= read_address (line_buf
, u
->addrsize
);
1732 case DW_LNE_define_file
:
1735 unsigned int dir_index
;
1737 f
= (const char *) line_buf
->buf
;
1738 if (!advance (line_buf
, strnlen (f
, line_buf
->left
) + 1))
1740 dir_index
= read_uleb128 (line_buf
);
1741 /* Ignore that time and length. */
1742 read_uleb128 (line_buf
);
1743 read_uleb128 (line_buf
);
1744 if (IS_ABSOLUTE_PATH (f
))
1755 else if (dir_index
- 1 < hdr
->dirs_count
)
1756 dir
= hdr
->dirs
[dir_index
- 1];
1759 dwarf_buf_error (line_buf
,
1760 ("invalid directory index "
1761 "in line number program"));
1764 dir_len
= strlen (dir
);
1767 backtrace_alloc (state
, dir_len
+ f_len
+ 2,
1768 line_buf
->error_callback
,
1772 memcpy (p
, dir
, dir_len
);
1773 /* FIXME: If we are on a DOS-based file system,
1774 and the directory or the file name use
1775 backslashes, then we should use a backslash
1778 memcpy (p
+ dir_len
+ 1, f
, f_len
+ 1);
1783 case DW_LNE_set_discriminator
:
1784 /* We don't care about discriminators. */
1785 read_uleb128 (line_buf
);
1788 if (!advance (line_buf
, len
- 1))
1798 add_line (state
, address
, filename
, lineno
,
1799 line_buf
->error_callback
, line_buf
->data
, vec
);
1801 case DW_LNS_advance_pc
:
1805 advance
= read_uleb128 (line_buf
);
1806 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1807 / hdr
->max_ops_per_insn
);
1808 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1811 case DW_LNS_advance_line
:
1812 lineno
+= (int) read_sleb128 (line_buf
);
1814 case DW_LNS_set_file
:
1818 fileno
= read_uleb128 (line_buf
);
1823 if (fileno
- 1 >= hdr
->filenames_count
)
1825 dwarf_buf_error (line_buf
,
1826 ("invalid file number in "
1827 "line number program"));
1830 filename
= hdr
->filenames
[fileno
- 1];
1834 case DW_LNS_set_column
:
1835 read_uleb128 (line_buf
);
1837 case DW_LNS_negate_stmt
:
1839 case DW_LNS_set_basic_block
:
1841 case DW_LNS_const_add_pc
:
1843 unsigned int advance
;
1845 op
= 255 - hdr
->opcode_base
;
1846 advance
= op
/ hdr
->line_range
;
1847 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1848 / hdr
->max_ops_per_insn
);
1849 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1852 case DW_LNS_fixed_advance_pc
:
1853 address
+= read_uint16 (line_buf
);
1856 case DW_LNS_set_prologue_end
:
1858 case DW_LNS_set_epilogue_begin
:
1860 case DW_LNS_set_isa
:
1861 read_uleb128 (line_buf
);
1867 for (i
= hdr
->opcode_lengths
[op
- 1]; i
> 0; --i
)
1868 read_uleb128 (line_buf
);
1878 /* Read the line number information for a compilation unit. Returns 1
1879 on success, 0 on failure. */
1882 read_line_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1883 backtrace_error_callback error_callback
, void *data
,
1884 struct unit
*u
, struct line_header
*hdr
, struct line
**lines
,
1885 size_t *lines_count
)
1887 struct line_vector vec
;
1888 struct dwarf_buf line_buf
;
1893 memset (&vec
.vec
, 0, sizeof vec
.vec
);
1896 memset (hdr
, 0, sizeof *hdr
);
1898 if (u
->lineoff
!= (off_t
) (size_t) u
->lineoff
1899 || (size_t) u
->lineoff
>= ddata
->dwarf_line_size
)
1901 error_callback (data
, "unit line offset out of range", 0);
1905 line_buf
.name
= ".debug_line";
1906 line_buf
.start
= ddata
->dwarf_line
;
1907 line_buf
.buf
= ddata
->dwarf_line
+ u
->lineoff
;
1908 line_buf
.left
= ddata
->dwarf_line_size
- u
->lineoff
;
1909 line_buf
.is_bigendian
= ddata
->is_bigendian
;
1910 line_buf
.error_callback
= error_callback
;
1911 line_buf
.data
= data
;
1912 line_buf
.reported_underflow
= 0;
1915 len
= read_uint32 (&line_buf
);
1916 if (len
== 0xffffffff)
1918 len
= read_uint64 (&line_buf
);
1921 line_buf
.left
= len
;
1923 if (!read_line_header (state
, u
, is_dwarf64
, &line_buf
, hdr
))
1926 if (!read_line_program (state
, u
, hdr
, &line_buf
, &vec
))
1929 if (line_buf
.reported_underflow
)
1934 /* This is not a failure in the sense of a generating an error,
1935 but it is a failure in that sense that we have no useful
1940 /* Allocate one extra entry at the end. */
1941 ln
= ((struct line
*)
1942 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1946 ln
->pc
= (uintptr_t) -1;
1947 ln
->filename
= NULL
;
1950 if (!backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
))
1953 ln
= (struct line
*) vec
.vec
.base
;
1954 qsort (ln
, vec
.count
, sizeof (struct line
), line_compare
);
1957 *lines_count
= vec
.count
;
1962 vec
.vec
.alc
+= vec
.vec
.size
;
1964 backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
);
1965 free_line_header (state
, hdr
, error_callback
, data
);
1966 *lines
= (struct line
*) (uintptr_t) -1;
1971 /* Read the name of a function from a DIE referenced by a
1972 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
1973 the same compilation unit. */
1976 read_referenced_name (struct dwarf_data
*ddata
, struct unit
*u
,
1977 uint64_t offset
, backtrace_error_callback error_callback
,
1980 struct dwarf_buf unit_buf
;
1982 const struct abbrev
*abbrev
;
1986 /* OFFSET is from the start of the data for this compilation unit.
1987 U->unit_data is the data, but it starts U->unit_data_offset bytes
1988 from the beginning. */
1990 if (offset
< u
->unit_data_offset
1991 || offset
- u
->unit_data_offset
>= u
->unit_data_len
)
1993 error_callback (data
,
1994 "abstract origin or specification out of range",
1999 offset
-= u
->unit_data_offset
;
2001 unit_buf
.name
= ".debug_info";
2002 unit_buf
.start
= ddata
->dwarf_info
;
2003 unit_buf
.buf
= u
->unit_data
+ offset
;
2004 unit_buf
.left
= u
->unit_data_len
- offset
;
2005 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2006 unit_buf
.error_callback
= error_callback
;
2007 unit_buf
.data
= data
;
2008 unit_buf
.reported_underflow
= 0;
2010 code
= read_uleb128 (&unit_buf
);
2013 dwarf_buf_error (&unit_buf
, "invalid abstract origin or specification");
2017 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2022 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2024 struct attr_val val
;
2026 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
,
2027 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2028 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2032 switch (abbrev
->attrs
[i
].name
)
2035 /* We prefer the linkage name if get one. */
2036 if (val
.encoding
== ATTR_VAL_STRING
)
2040 case DW_AT_linkage_name
:
2041 case DW_AT_MIPS_linkage_name
:
2042 if (val
.encoding
== ATTR_VAL_STRING
)
2043 return val
.u
.string
;
2046 case DW_AT_specification
:
2047 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2048 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2050 /* This refers to a specification defined in some other
2051 compilation unit. We can handle this case if we
2052 must, but it's harder. */
2055 if (val
.encoding
== ATTR_VAL_UINT
2056 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2060 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2061 error_callback
, data
);
2075 /* Add a single range to U that maps to function. Returns 1 on
2076 success, 0 on error. */
2079 add_function_range (struct backtrace_state
*state
, struct function
*function
,
2080 uint64_t lowpc
, uint64_t highpc
,
2081 backtrace_error_callback error_callback
,
2082 void *data
, struct function_vector
*vec
)
2084 struct function_addrs
*p
;
2088 p
= (struct function_addrs
*) vec
->vec
.base
+ vec
->count
- 1;
2089 if ((lowpc
== p
->high
|| lowpc
== p
->high
+ 1)
2090 && function
== p
->function
)
2092 if (highpc
> p
->high
)
2098 p
= ((struct function_addrs
*)
2099 backtrace_vector_grow (state
, sizeof (struct function_addrs
),
2100 error_callback
, data
, &vec
->vec
));
2106 p
->function
= function
;
2111 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2115 add_function_ranges (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2116 struct unit
*u
, struct function
*function
,
2117 uint64_t ranges
, uint64_t base
,
2118 backtrace_error_callback error_callback
, void *data
,
2119 struct function_vector
*vec
)
2121 struct dwarf_buf ranges_buf
;
2123 if (ranges
>= ddata
->dwarf_ranges_size
)
2125 error_callback (data
, "function ranges offset out of range", 0);
2129 ranges_buf
.name
= ".debug_ranges";
2130 ranges_buf
.start
= ddata
->dwarf_ranges
;
2131 ranges_buf
.buf
= ddata
->dwarf_ranges
+ ranges
;
2132 ranges_buf
.left
= ddata
->dwarf_ranges_size
- ranges
;
2133 ranges_buf
.is_bigendian
= ddata
->is_bigendian
;
2134 ranges_buf
.error_callback
= error_callback
;
2135 ranges_buf
.data
= data
;
2136 ranges_buf
.reported_underflow
= 0;
2143 if (ranges_buf
.reported_underflow
)
2146 low
= read_address (&ranges_buf
, u
->addrsize
);
2147 high
= read_address (&ranges_buf
, u
->addrsize
);
2149 if (low
== 0 && high
== 0)
2152 if (is_highest_address (low
, u
->addrsize
))
2156 if (!add_function_range (state
, function
, low
+ base
, high
+ base
,
2157 error_callback
, data
, vec
))
2162 if (ranges_buf
.reported_underflow
)
2168 /* Read one entry plus all its children. Add function addresses to
2169 VEC. Returns 1 on success, 0 on error. */
2172 read_function_entry (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2173 struct unit
*u
, uint64_t base
, struct dwarf_buf
*unit_buf
,
2174 const struct line_header
*lhdr
,
2175 backtrace_error_callback error_callback
, void *data
,
2176 struct function_vector
*vec
)
2178 while (unit_buf
->left
> 0)
2181 const struct abbrev
*abbrev
;
2183 struct function
*function
;
2189 int highpc_is_relative
;
2193 code
= read_uleb128 (unit_buf
);
2197 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2201 is_function
= (abbrev
->tag
== DW_TAG_subprogram
2202 || abbrev
->tag
== DW_TAG_entry_point
2203 || abbrev
->tag
== DW_TAG_inlined_subroutine
);
2208 function
= ((struct function
*)
2209 backtrace_alloc (state
, sizeof *function
,
2210 error_callback
, data
));
2211 if (function
== NULL
)
2213 memset (function
, 0, sizeof *function
);
2220 highpc_is_relative
= 0;
2223 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2225 struct attr_val val
;
2227 if (!read_attribute (abbrev
->attrs
[i
].form
, unit_buf
,
2228 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2229 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2233 /* The compile unit sets the base address for any address
2234 ranges in the function entries. */
2235 if (abbrev
->tag
== DW_TAG_compile_unit
2236 && abbrev
->attrs
[i
].name
== DW_AT_low_pc
2237 && val
.encoding
== ATTR_VAL_ADDRESS
)
2242 switch (abbrev
->attrs
[i
].name
)
2244 case DW_AT_call_file
:
2245 if (val
.encoding
== ATTR_VAL_UINT
)
2247 if (val
.u
.uint
== 0)
2248 function
->caller_filename
= "";
2251 if (val
.u
.uint
- 1 >= lhdr
->filenames_count
)
2253 dwarf_buf_error (unit_buf
,
2254 ("invalid file number in "
2255 "DW_AT_call_file attribute"));
2258 function
->caller_filename
=
2259 lhdr
->filenames
[val
.u
.uint
- 1];
2264 case DW_AT_call_line
:
2265 if (val
.encoding
== ATTR_VAL_UINT
)
2266 function
->caller_lineno
= val
.u
.uint
;
2269 case DW_AT_abstract_origin
:
2270 case DW_AT_specification
:
2271 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2272 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2274 /* This refers to an abstract origin defined in
2275 some other compilation unit. We can handle
2276 this case if we must, but it's harder. */
2279 if (val
.encoding
== ATTR_VAL_UINT
2280 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2284 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2285 error_callback
, data
);
2287 function
->name
= name
;
2292 if (val
.encoding
== ATTR_VAL_STRING
)
2294 /* Don't override a name we found in some other
2295 way, as it will normally be more
2296 useful--e.g., this name is normally not
2298 if (function
->name
== NULL
)
2299 function
->name
= val
.u
.string
;
2303 case DW_AT_linkage_name
:
2304 case DW_AT_MIPS_linkage_name
:
2305 if (val
.encoding
== ATTR_VAL_STRING
)
2306 function
->name
= val
.u
.string
;
2310 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2318 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2320 highpc
= val
.u
.uint
;
2323 else if (val
.encoding
== ATTR_VAL_UINT
)
2325 highpc
= val
.u
.uint
;
2327 highpc_is_relative
= 1;
2332 if (val
.encoding
== ATTR_VAL_UINT
2333 || val
.encoding
== ATTR_VAL_REF_SECTION
)
2335 ranges
= val
.u
.uint
;
2346 /* If we couldn't find a name for the function, we have no use
2348 if (is_function
&& function
->name
== NULL
)
2350 backtrace_free (state
, function
, sizeof *function
,
2351 error_callback
, data
);
2359 if (!add_function_ranges (state
, ddata
, u
, function
, ranges
,
2360 base
, error_callback
, data
, vec
))
2363 else if (have_lowpc
&& have_highpc
)
2365 if (highpc_is_relative
)
2367 if (!add_function_range (state
, function
, lowpc
, highpc
,
2368 error_callback
, data
, vec
))
2373 backtrace_free (state
, function
, sizeof *function
,
2374 error_callback
, data
);
2379 if (abbrev
->has_children
)
2383 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2384 error_callback
, data
, vec
))
2389 struct function_vector fvec
;
2391 /* Gather any information for inlined functions in
2394 memset (&fvec
, 0, sizeof fvec
);
2396 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2397 error_callback
, data
, &fvec
))
2402 struct function_addrs
*faddrs
;
2404 if (!backtrace_vector_release (state
, &fvec
.vec
,
2405 error_callback
, data
))
2408 faddrs
= (struct function_addrs
*) fvec
.vec
.base
;
2409 qsort (faddrs
, fvec
.count
,
2410 sizeof (struct function_addrs
),
2411 function_addrs_compare
);
2413 function
->function_addrs
= faddrs
;
2414 function
->function_addrs_count
= fvec
.count
;
2423 /* Read function name information for a compilation unit. We look
2424 through the whole unit looking for function tags. */
2427 read_function_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2428 const struct line_header
*lhdr
,
2429 backtrace_error_callback error_callback
, void *data
,
2430 struct unit
*u
, struct function_vector
*fvec
,
2431 struct function_addrs
**ret_addrs
,
2432 size_t *ret_addrs_count
)
2434 struct dwarf_buf unit_buf
;
2435 struct function_addrs
*addrs
;
2438 unit_buf
.name
= ".debug_info";
2439 unit_buf
.start
= ddata
->dwarf_info
;
2440 unit_buf
.buf
= u
->unit_data
;
2441 unit_buf
.left
= u
->unit_data_len
;
2442 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2443 unit_buf
.error_callback
= error_callback
;
2444 unit_buf
.data
= data
;
2445 unit_buf
.reported_underflow
= 0;
2447 while (unit_buf
.left
> 0)
2449 if (!read_function_entry (state
, ddata
, u
, 0, &unit_buf
, lhdr
,
2450 error_callback
, data
, fvec
))
2454 if (fvec
->count
== 0)
2457 addrs
= (struct function_addrs
*) fvec
->vec
.base
;
2458 addrs_count
= fvec
->count
;
2460 /* Finish this list of addresses, but leave the remaining space in
2461 the vector available for the next function unit. */
2462 backtrace_vector_finish (state
, &fvec
->vec
);
2465 qsort (addrs
, addrs_count
, sizeof (struct function_addrs
),
2466 function_addrs_compare
);
2469 *ret_addrs_count
= addrs_count
;
2472 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2473 information, and update FILENAME and LINENO for the caller.
2474 Returns whatever CALLBACK returns, or 0 to keep going. */
2477 report_inlined_functions (uintptr_t pc
, struct function
*function
,
2478 backtrace_full_callback callback
, void *data
,
2479 const char **filename
, int *lineno
)
2481 struct function_addrs
*function_addrs
;
2482 struct function
*inlined
;
2485 if (function
->function_addrs_count
== 0)
2488 function_addrs
= ((struct function_addrs
*)
2489 bsearch (&pc
, function
->function_addrs
,
2490 function
->function_addrs_count
,
2491 sizeof (struct function_addrs
),
2492 function_addrs_search
));
2493 if (function_addrs
== NULL
)
2496 while (((size_t) (function_addrs
- function
->function_addrs
) + 1
2497 < function
->function_addrs_count
)
2498 && pc
>= (function_addrs
+ 1)->low
2499 && pc
< (function_addrs
+ 1)->high
)
2502 /* We found an inlined call. */
2504 inlined
= function_addrs
->function
;
2506 /* Report any calls inlined into this one. */
2507 ret
= report_inlined_functions (pc
, inlined
, callback
, data
,
2512 /* Report this inlined call. */
2513 ret
= callback (data
, pc
, *filename
, *lineno
, inlined
->name
);
2517 /* Our caller will report the caller of the inlined function; tell
2518 it the appropriate filename and line number. */
2519 *filename
= inlined
->caller_filename
;
2520 *lineno
= inlined
->caller_lineno
;
2525 /* Return the file/line information for a PC using the DWARF mapping
2526 we built earlier. */
2529 dwarf_fileline (struct backtrace_state
*state
, uintptr_t pc
,
2530 backtrace_full_callback callback
,
2531 backtrace_error_callback error_callback
, void *data
)
2533 struct dwarf_data
*ddata
;
2534 struct unit_addrs
*entry
;
2539 struct function_addrs
*function_addrs
;
2540 struct function
*function
;
2541 const char *filename
;
2545 ddata
= (struct dwarf_data
*) state
->fileline_data
;
2547 /* Find an address range that includes PC. */
2548 entry
= bsearch (&pc
, ddata
->addrs
, ddata
->addrs_count
,
2549 sizeof (struct unit_addrs
), unit_addrs_search
);
2552 return callback (data
, pc
, NULL
, 0, NULL
);
2554 /* If there are multiple ranges that contain PC, use the last one,
2555 in order to produce predictable results. If we assume that all
2556 ranges are properly nested, then the last range will be the
2558 while ((size_t) (entry
- ddata
->addrs
) + 1 < ddata
->addrs_count
2559 && pc
>= (entry
+ 1)->low
2560 && pc
< (entry
+ 1)->high
)
2563 /* We need the lines, lines_count, function_addrs,
2564 function_addrs_count fields of u. If they are not set, we need
2565 to set them. When running in threaded mode, we need to allow for
2566 the possibility that some other thread is setting them
2572 /* Skip units with no useful line number information by walking
2573 backward. Useless line number information is marked by setting
2575 while (entry
> ddata
->addrs
2576 && pc
>= (entry
- 1)->low
2577 && pc
< (entry
- 1)->high
)
2579 if (state
->threaded
)
2581 /* Use __sync_bool_compare_and_swap to do a
2583 while (!__sync_bool_compare_and_swap (&u
->lines
, lines
, lines
))
2587 if (lines
!= (struct line
*) (uintptr_t) -1)
2596 /* Do a load-acquire of u->lines. */
2597 if (state
->threaded
)
2599 /* Use __sync_bool_compare_and_swap to do an atomic load. */
2600 while (!__sync_bool_compare_and_swap (&u
->lines
, lines
, lines
))
2607 size_t function_addrs_count
;
2608 struct line_header lhdr
;
2611 /* We have never read the line information for this unit. Read
2614 function_addrs
= NULL
;
2615 function_addrs_count
= 0;
2616 if (read_line_info (state
, ddata
, error_callback
, data
, entry
->u
, &lhdr
,
2619 read_function_info (state
, ddata
, &lhdr
, error_callback
, data
,
2620 entry
->u
, &ddata
->fvec
, &function_addrs
,
2621 &function_addrs_count
);
2622 free_line_header (state
, &lhdr
, error_callback
, data
);
2626 /* Atomically store the information we just read into the unit.
2627 If another thread is simultaneously writing, it presumably
2628 read the same information, and we don't care which one we
2629 wind up with; we just leak the other one. We do have to
2630 write the lines field last, so that the acquire-loads above
2631 ensure that the other fields are set. */
2633 if (!state
->threaded
)
2635 u
->lines_count
= count
;
2636 u
->function_addrs
= function_addrs
;
2637 u
->function_addrs_count
= function_addrs_count
;
2642 __sync_bool_compare_and_swap (&u
->lines_count
, 0, count
);
2643 __sync_bool_compare_and_swap (&u
->function_addrs
, NULL
,
2645 __sync_bool_compare_and_swap (&u
->function_addrs_count
, 0,
2646 function_addrs_count
);
2647 __sync_bool_compare_and_swap (&u
->lines
, NULL
, lines
);
2651 /* Now all fields of U have been initialized. */
2653 if (lines
== (struct line
*) (uintptr_t) -1)
2655 /* If reading the line number information failed in some way,
2656 try again to see if there is a better compilation unit for
2659 dwarf_fileline (state
, pc
, callback
, error_callback
, data
);
2660 return callback (data
, pc
, NULL
, 0, NULL
);
2663 /* Search for PC within this unit. */
2665 ln
= (struct line
*) bsearch (&pc
, lines
, entry
->u
->lines_count
,
2666 sizeof (struct line
), line_search
);
2669 error_callback (data
, "inconsistent DWARF line number info", 0);
2673 /* Search for function name within this unit. */
2675 if (entry
->u
->function_addrs_count
== 0)
2676 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2678 function_addrs
= ((struct function_addrs
*)
2679 bsearch (&pc
, entry
->u
->function_addrs
,
2680 entry
->u
->function_addrs_count
,
2681 sizeof (struct function_addrs
),
2682 function_addrs_search
));
2683 if (function_addrs
== NULL
)
2684 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2686 /* If there are multiple function ranges that contain PC, use the
2687 last one, in order to produce predictable results. */
2689 while (((size_t) (function_addrs
- entry
->u
->function_addrs
+ 1)
2690 < entry
->u
->function_addrs_count
)
2691 && pc
>= (function_addrs
+ 1)->low
2692 && pc
< (function_addrs
+ 1)->high
)
2695 function
= function_addrs
->function
;
2697 filename
= ln
->filename
;
2698 lineno
= ln
->lineno
;
2700 ret
= report_inlined_functions (pc
, function
, callback
, data
,
2701 &filename
, &lineno
);
2705 return callback (data
, pc
, filename
, lineno
, function
->name
);
2708 /* Build our data structures from the .debug_info and .debug_line
2709 sections. Set *FILELINE_FN and *FILELINE_DATA. Return 1 on
2710 success, 0 on failure. */
2713 backtrace_dwarf_initialize (struct backtrace_state
*state
,
2714 const unsigned char *dwarf_info
,
2715 size_t dwarf_info_size
,
2716 const unsigned char *dwarf_line
,
2717 size_t dwarf_line_size
,
2718 const unsigned char *dwarf_abbrev
,
2719 size_t dwarf_abbrev_size
,
2720 const unsigned char *dwarf_ranges
,
2721 size_t dwarf_ranges_size
,
2722 const unsigned char *dwarf_str
,
2723 size_t dwarf_str_size
,
2725 backtrace_error_callback error_callback
,
2726 void *data
, fileline
*fileline_fn
)
2728 struct unit_addrs_vector addrs_vec
;
2729 struct unit_addrs
*addrs
;
2731 struct dwarf_data
*fdata
;
2733 if (!build_address_map (state
, dwarf_info
, dwarf_info_size
, dwarf_abbrev
,
2734 dwarf_abbrev_size
, dwarf_ranges
, dwarf_ranges_size
,
2735 dwarf_str
, dwarf_str_size
, is_bigendian
,
2736 error_callback
, data
, &addrs_vec
))
2739 if (!backtrace_vector_release (state
, &addrs_vec
.vec
, error_callback
, data
))
2741 addrs
= (struct unit_addrs
*) addrs_vec
.vec
.base
;
2742 addrs_count
= addrs_vec
.count
;
2743 qsort (addrs
, addrs_count
, sizeof (struct unit_addrs
), unit_addrs_compare
);
2745 fdata
= ((struct dwarf_data
*)
2746 backtrace_alloc (state
, sizeof (struct dwarf_data
),
2747 error_callback
, data
));
2751 fdata
->addrs
= addrs
;
2752 fdata
->addrs_count
= addrs_count
;
2753 fdata
->dwarf_info
= dwarf_info
;
2754 fdata
->dwarf_info_size
= dwarf_info_size
;
2755 fdata
->dwarf_line
= dwarf_line
;
2756 fdata
->dwarf_line_size
= dwarf_line_size
;
2757 fdata
->dwarf_ranges
= dwarf_ranges
;
2758 fdata
->dwarf_ranges_size
= dwarf_ranges_size
;
2759 fdata
->dwarf_str
= dwarf_str
;
2760 fdata
->dwarf_str_size
= dwarf_str_size
;
2761 fdata
->is_bigendian
= is_bigendian
;
2762 memset (&fdata
->fvec
, 0, sizeof fdata
->fvec
);
2764 state
->fileline_data
= fdata
;
2766 *fileline_fn
= dwarf_fileline
;