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. */
39 #include <sys/types.h>
42 #include "filenames.h"
44 #include "backtrace.h"
47 #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
48 /* The function is defined in libiberty if needed. */
49 extern size_t strnlen (const char *, size_t);
52 /* A buffer to read DWARF info. */
56 /* Buffer name for error messages. */
58 /* Start of the buffer. */
59 const unsigned char *start
;
60 /* Next byte to read. */
61 const unsigned char *buf
;
62 /* The number of bytes remaining. */
64 /* Whether the data is big-endian. */
66 /* Error callback routine. */
67 backtrace_error_callback error_callback
;
68 /* Data for error_callback. */
70 /* Non-zero if we've reported an underflow error. */
71 int reported_underflow
;
74 /* A single attribute in a DWARF abbreviation. */
78 /* The attribute name. */
79 enum dwarf_attribute name
;
80 /* The attribute form. */
84 /* A single DWARF abbreviation. */
88 /* The abbrev code--the number used to refer to the abbrev. */
92 /* Non-zero if this abbrev has child entries. */
94 /* The number of attributes. */
100 /* The DWARF abbreviations for a compilation unit. This structure
101 only exists while reading the compilation unit. Most DWARF readers
102 seem to a hash table to map abbrev ID's to abbrev entries.
103 However, we primarily care about GCC, and GCC simply issues ID's in
104 numerical order starting at 1. So we simply keep a sorted vector,
105 and try to just look up the code. */
109 /* The number of abbrevs in the vector. */
111 /* The abbrevs, sorted by the code field. */
112 struct abbrev
*abbrevs
;
115 /* The different kinds of attribute values. */
117 enum attr_val_encoding
121 /* A unsigned integer. */
123 /* A sigd integer. */
127 /* An offset to other data in the containing unit. */
129 /* An offset to other data within the .dwarf_info section. */
131 /* An offset to data in some other section. */
132 ATTR_VAL_REF_SECTION
,
133 /* A type signature. */
135 /* A block of data (not represented). */
137 /* An expression (not represented). */
141 /* An attribute value. */
145 /* How the value is stored in the field u. */
146 enum attr_val_encoding encoding
;
149 /* ATTR_VAL_ADDRESS, ATTR_VAL_UINT, ATTR_VAL_REF*. */
153 /* ATTR_VAL_STRING. */
155 /* ATTR_VAL_BLOCK not stored. */
159 /* The line number program header. */
163 /* The version of the line number information. */
165 /* The minimum instruction length. */
166 unsigned int min_insn_len
;
167 /* The maximum number of ops per instruction. */
168 unsigned int max_ops_per_insn
;
169 /* The line base for special opcodes. */
171 /* The line range for special opcodes. */
172 unsigned int line_range
;
173 /* The opcode base--the first special opcode. */
174 unsigned int opcode_base
;
175 /* Opcode lengths, indexed by opcode - 1. */
176 const unsigned char *opcode_lengths
;
177 /* The number of directory entries. */
179 /* The directory entries. */
181 /* The number of filenames. */
182 size_t filenames_count
;
184 const char **filenames
;
187 /* Map a single PC value to a file/line. We will keep a vector of
188 these sorted by PC value. Each file/line will be correct from the
189 PC up to the PC of the next entry if there is one. We allocate one
190 extra entry at the end so that we can use bsearch. */
196 /* File name. Many entries in the array are expected to point to
197 the same file name. */
198 const char *filename
;
203 /* A growable vector of line number information. This is used while
204 reading the line numbers. */
208 /* Memory. This is an array of struct line. */
209 struct backtrace_vector vec
;
210 /* Number of valid mappings. */
214 /* A function described in the debug info. */
218 /* The name of the function. */
220 /* If this is an inlined function, the filename of the call
222 const char *caller_filename
;
223 /* If this is an inlined function, the line number of the call
226 /* Map PC ranges to inlined functions. */
227 struct function_addrs
*function_addrs
;
228 size_t function_addrs_count
;
231 /* An address range for a function. This maps a PC value to a
232 specific function. */
234 struct function_addrs
236 /* Range is LOW <= PC < HIGH. */
239 /* Function for this address range. */
240 struct function
*function
;
243 /* A growable vector of function address ranges. */
245 struct function_vector
247 /* Memory. This is an array of struct function_addrs. */
248 struct backtrace_vector vec
;
249 /* Number of address ranges present. */
253 /* A DWARF compilation unit. This only holds the information we need
254 to map a PC to a file and line. */
258 /* The first entry for this compilation unit. */
259 const unsigned char *unit_data
;
260 /* The length of the data for this compilation unit. */
261 size_t unit_data_len
;
262 /* The offset of UNIT_DATA from the start of the information for
263 this compilation unit. */
264 size_t unit_data_offset
;
267 /* Whether unit is DWARF64. */
271 /* Offset into line number information. */
273 /* Compilation command working directory. */
274 const char *comp_dir
;
275 /* The abbreviations for this unit. */
276 struct abbrevs abbrevs
;
278 /* The fields above this point are read in during initialization and
279 may be accessed freely. The fields below this point are read in
280 as needed, and therefore require care, as different threads may
281 try to initialize them simultaneously. */
283 /* PC to line number mapping. This is NULL if the values have not
284 been read. This is (struct line *) -1 if there was an error
285 reading the values. */
287 /* Number of entries in lines. */
289 /* PC ranges to function. */
290 struct function_addrs
*function_addrs
;
291 size_t function_addrs_count
;
294 /* An address range for a compilation unit. This maps a PC value to a
295 specific compilation unit. Note that we invert the representation
296 in DWARF: instead of listing the units and attaching a list of
297 ranges, we list the ranges and have each one point to the unit.
298 This lets us do a binary search to find the unit. */
302 /* Range is LOW <= PC < HIGH. */
305 /* Compilation unit for this address range. */
309 /* A growable vector of compilation unit address ranges. */
311 struct unit_addrs_vector
313 /* Memory. This is an array of struct unit_addrs. */
314 struct backtrace_vector vec
;
315 /* Number of address ranges present. */
319 /* The information we need to map a PC to a file and line. */
323 /* A sorted list of address ranges. */
324 struct unit_addrs
*addrs
;
325 /* Number of address ranges in list. */
327 /* The unparsed .debug_info section. */
328 const unsigned char *dwarf_info
;
329 size_t dwarf_info_size
;
330 /* The unparsed .debug_line section. */
331 const unsigned char *dwarf_line
;
332 size_t dwarf_line_size
;
333 /* The unparsed .debug_ranges section. */
334 const unsigned char *dwarf_ranges
;
335 size_t dwarf_ranges_size
;
336 /* The unparsed .debug_str section. */
337 const unsigned char *dwarf_str
;
338 size_t dwarf_str_size
;
339 /* Whether the data is big-endian or not. */
341 /* A vector used for function addresses. We keep this here so that
342 we can grow the vector as we read more functions. */
343 struct function_vector fvec
;
346 /* Report an error for a DWARF buffer. */
349 dwarf_buf_error (struct dwarf_buf
*buf
, const char *msg
)
353 snprintf (b
, sizeof b
, "%s in %s at %d",
354 msg
, buf
->name
, (int) (buf
->buf
- buf
->start
));
355 buf
->error_callback (buf
->data
, b
, 0);
358 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
362 require (struct dwarf_buf
*buf
, size_t count
)
364 if (buf
->left
>= count
)
367 if (!buf
->reported_underflow
)
369 dwarf_buf_error (buf
, "DWARF underflow");
370 buf
->reported_underflow
= 1;
376 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
380 advance (struct dwarf_buf
*buf
, size_t count
)
382 if (!require (buf
, count
))
389 /* Read one byte from BUF and advance 1 byte. */
392 read_byte (struct dwarf_buf
*buf
)
394 const unsigned char *p
= buf
->buf
;
396 if (!advance (buf
, 1))
401 /* Read a signed char from BUF and advance 1 byte. */
404 read_sbyte (struct dwarf_buf
*buf
)
406 const unsigned char *p
= buf
->buf
;
408 if (!advance (buf
, 1))
410 return (*p
^ 0x80) - 0x80;
413 /* Read a uint16 from BUF and advance 2 bytes. */
416 read_uint16 (struct dwarf_buf
*buf
)
418 const unsigned char *p
= buf
->buf
;
420 if (!advance (buf
, 2))
422 if (buf
->is_bigendian
)
423 return ((uint16_t) p
[0] << 8) | (uint16_t) p
[1];
425 return ((uint16_t) p
[1] << 8) | (uint16_t) p
[0];
428 /* Read a uint32 from BUF and advance 4 bytes. */
431 read_uint32 (struct dwarf_buf
*buf
)
433 const unsigned char *p
= buf
->buf
;
435 if (!advance (buf
, 4))
437 if (buf
->is_bigendian
)
438 return (((uint32_t) p
[0] << 24) | ((uint32_t) p
[1] << 16)
439 | ((uint32_t) p
[2] << 8) | (uint32_t) p
[3]);
441 return (((uint32_t) p
[3] << 24) | ((uint32_t) p
[2] << 16)
442 | ((uint32_t) p
[1] << 8) | (uint32_t) p
[0]);
445 /* Read a uint64 from BUF and advance 8 bytes. */
448 read_uint64 (struct dwarf_buf
*buf
)
450 const unsigned char *p
= buf
->buf
;
452 if (!advance (buf
, 8))
454 if (buf
->is_bigendian
)
455 return (((uint64_t) p
[0] << 56) | ((uint64_t) p
[1] << 48)
456 | ((uint64_t) p
[2] << 40) | ((uint64_t) p
[3] << 32)
457 | ((uint64_t) p
[4] << 24) | ((uint64_t) p
[5] << 16)
458 | ((uint64_t) p
[6] << 8) | (uint64_t) p
[7]);
460 return (((uint64_t) p
[7] << 56) | ((uint64_t) p
[6] << 48)
461 | ((uint64_t) p
[5] << 40) | ((uint64_t) p
[4] << 32)
462 | ((uint64_t) p
[3] << 24) | ((uint64_t) p
[2] << 16)
463 | ((uint64_t) p
[1] << 8) | (uint64_t) p
[0]);
466 /* Read an offset from BUF and advance the appropriate number of
470 read_offset (struct dwarf_buf
*buf
, int is_dwarf64
)
473 return read_uint64 (buf
);
475 return read_uint32 (buf
);
478 /* Read an address from BUF and advance the appropriate number of
482 read_address (struct dwarf_buf
*buf
, int addrsize
)
487 return read_byte (buf
);
489 return read_uint16 (buf
);
491 return read_uint32 (buf
);
493 return read_uint64 (buf
);
495 dwarf_buf_error (buf
, "unrecognized address size");
500 /* Return whether a value is the highest possible address, given the
504 is_highest_address (uint64_t address
, int addrsize
)
509 return address
== (unsigned char) -1;
511 return address
== (uint16_t) -1;
513 return address
== (uint32_t) -1;
515 return address
== (uint64_t) -1;
521 /* Read an unsigned LEB128 number. */
524 read_uleb128 (struct dwarf_buf
*buf
)
534 const unsigned char *p
;
537 if (!advance (buf
, 1))
540 ret
|= ((uint64_t) (b
& 0x7f)) << shift
;
543 while ((b
& 0x80) != 0);
546 dwarf_buf_error (buf
, "LEB128 overflows uint64_5");
551 /* Read a signed LEB128 number. */
554 read_sleb128 (struct dwarf_buf
*buf
)
564 const unsigned char *p
;
567 if (!advance (buf
, 1))
570 val
|= ((uint64_t) (b
& 0x7f)) << shift
;
573 while ((b
& 0x80) != 0);
576 dwarf_buf_error (buf
, "signed LEB128 overflows uint64_t");
579 val
|= ((uint64_t) -1) << shift
;
581 return (int64_t) val
;
584 /* Return the length of an LEB128 number. */
587 leb128_len (const unsigned char *p
)
592 while ((*p
& 0x80) != 0)
600 /* Free an abbreviations structure. */
603 free_abbrevs (struct backtrace_state
*state
, struct abbrevs
*abbrevs
,
604 backtrace_error_callback error_callback
, void *data
)
608 for (i
= 0; i
< abbrevs
->num_abbrevs
; ++i
)
609 backtrace_free (state
, abbrevs
->abbrevs
[i
].attrs
,
610 abbrevs
->abbrevs
[i
].num_attrs
* sizeof (struct attr
),
611 error_callback
, data
);
612 backtrace_free (state
, abbrevs
->abbrevs
,
613 abbrevs
->num_abbrevs
* sizeof (struct abbrev
),
614 error_callback
, data
);
615 abbrevs
->num_abbrevs
= 0;
616 abbrevs
->abbrevs
= NULL
;
619 /* Read an attribute value. Returns 1 on success, 0 on failure. If
620 the value can be represented as a uint64_t, sets *VAL and sets
621 *IS_VALID to 1. We don't try to store the value of other attribute
622 forms, because we don't care about them. */
625 read_attribute (enum dwarf_form form
, struct dwarf_buf
*buf
,
626 int is_dwarf64
, int version
, int addrsize
,
627 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
628 struct attr_val
*val
)
633 val
->encoding
= ATTR_VAL_ADDRESS
;
634 val
->u
.uint
= read_address (buf
, addrsize
);
637 val
->encoding
= ATTR_VAL_BLOCK
;
638 return advance (buf
, read_uint16 (buf
));
640 val
->encoding
= ATTR_VAL_BLOCK
;
641 return advance (buf
, read_uint32 (buf
));
643 val
->encoding
= ATTR_VAL_UINT
;
644 val
->u
.uint
= read_uint16 (buf
);
647 val
->encoding
= ATTR_VAL_UINT
;
648 val
->u
.uint
= read_uint32 (buf
);
651 val
->encoding
= ATTR_VAL_UINT
;
652 val
->u
.uint
= read_uint64 (buf
);
655 val
->encoding
= ATTR_VAL_STRING
;
656 val
->u
.string
= (const char *) buf
->buf
;
657 return advance (buf
, strnlen ((const char *) buf
->buf
, buf
->left
) + 1);
659 val
->encoding
= ATTR_VAL_BLOCK
;
660 return advance (buf
, read_uleb128 (buf
));
662 val
->encoding
= ATTR_VAL_BLOCK
;
663 return advance (buf
, read_byte (buf
));
665 val
->encoding
= ATTR_VAL_UINT
;
666 val
->u
.uint
= read_byte (buf
);
669 val
->encoding
= ATTR_VAL_UINT
;
670 val
->u
.uint
= read_byte (buf
);
673 val
->encoding
= ATTR_VAL_SINT
;
674 val
->u
.sint
= read_sleb128 (buf
);
680 offset
= read_offset (buf
, is_dwarf64
);
681 if (offset
>= dwarf_str_size
)
683 dwarf_buf_error (buf
, "DW_FORM_strp out of range");
686 val
->encoding
= ATTR_VAL_STRING
;
687 val
->u
.string
= (const char *) dwarf_str
+ offset
;
691 val
->encoding
= ATTR_VAL_UINT
;
692 val
->u
.uint
= read_uleb128 (buf
);
694 case DW_FORM_ref_addr
:
695 val
->encoding
= ATTR_VAL_REF_INFO
;
697 val
->u
.uint
= read_address (buf
, addrsize
);
699 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
702 val
->encoding
= ATTR_VAL_REF_UNIT
;
703 val
->u
.uint
= read_byte (buf
);
706 val
->encoding
= ATTR_VAL_REF_UNIT
;
707 val
->u
.uint
= read_uint16 (buf
);
710 val
->encoding
= ATTR_VAL_REF_UNIT
;
711 val
->u
.uint
= read_uint32 (buf
);
714 val
->encoding
= ATTR_VAL_REF_UNIT
;
715 val
->u
.uint
= read_uint64 (buf
);
717 case DW_FORM_ref_udata
:
718 val
->encoding
= ATTR_VAL_REF_UNIT
;
719 val
->u
.uint
= read_uleb128 (buf
);
721 case DW_FORM_indirect
:
725 form
= read_uleb128 (buf
);
726 return read_attribute ((enum dwarf_form
) form
, buf
, is_dwarf64
,
727 version
, addrsize
, dwarf_str
, dwarf_str_size
,
730 case DW_FORM_sec_offset
:
731 val
->encoding
= ATTR_VAL_REF_SECTION
;
732 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
734 case DW_FORM_exprloc
:
735 val
->encoding
= ATTR_VAL_EXPR
;
736 return advance (buf
, read_uleb128 (buf
));
737 case DW_FORM_flag_present
:
738 val
->encoding
= ATTR_VAL_UINT
;
741 case DW_FORM_ref_sig8
:
742 val
->encoding
= ATTR_VAL_REF_TYPE
;
743 val
->u
.uint
= read_uint64 (buf
);
745 case DW_FORM_GNU_addr_index
:
746 val
->encoding
= ATTR_VAL_REF_SECTION
;
747 val
->u
.uint
= read_uleb128 (buf
);
749 case DW_FORM_GNU_str_index
:
750 val
->encoding
= ATTR_VAL_REF_SECTION
;
751 val
->u
.uint
= read_uleb128 (buf
);
753 case DW_FORM_GNU_ref_alt
:
754 val
->encoding
= ATTR_VAL_REF_SECTION
;
755 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
757 case DW_FORM_GNU_strp_alt
:
758 val
->encoding
= ATTR_VAL_REF_SECTION
;
759 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
762 dwarf_buf_error (buf
, "unrecognized DWARF form");
767 /* Compare function_addrs for qsort. When ranges are nested, make the
768 smallest one sort last. */
771 function_addrs_compare (const void *v1
, const void *v2
)
773 const struct function_addrs
*a1
= (const struct function_addrs
*) v1
;
774 const struct function_addrs
*a2
= (const struct function_addrs
*) v2
;
776 if (a1
->low
< a2
->low
)
778 if (a1
->low
> a2
->low
)
780 if (a1
->high
< a2
->high
)
782 if (a1
->high
> a2
->high
)
784 return strcmp (a1
->function
->name
, a2
->function
->name
);
787 /* Compare a PC against a function_addrs for bsearch. Note that if
788 there are multiple ranges containing PC, which one will be returned
789 is unpredictable. We compensate for that in dwarf_fileline. */
792 function_addrs_search (const void *vkey
, const void *ventry
)
794 const uintptr_t *key
= (const uintptr_t *) vkey
;
795 const struct function_addrs
*entry
= (const struct function_addrs
*) ventry
;
801 else if (pc
>= entry
->high
)
807 /* Add a new compilation unit address range to a vector. Returns 1 on
808 success, 0 on failure. */
811 add_unit_addr (struct backtrace_state
*state
, struct unit_addrs addrs
,
812 backtrace_error_callback error_callback
, void *data
,
813 struct unit_addrs_vector
*vec
)
815 struct unit_addrs
*p
;
817 /* Try to merge with the last entry. */
820 p
= (struct unit_addrs
*) vec
->vec
.base
+ (vec
->count
- 1);
821 if ((addrs
.low
== p
->high
|| addrs
.low
== p
->high
+ 1)
824 if (addrs
.high
> p
->high
)
825 p
->high
= addrs
.high
;
830 p
= ((struct unit_addrs
*)
831 backtrace_vector_grow (state
, sizeof (struct unit_addrs
),
832 error_callback
, data
, &vec
->vec
));
841 /* Free a unit address vector. */
844 free_unit_addrs_vector (struct backtrace_state
*state
,
845 struct unit_addrs_vector
*vec
,
846 backtrace_error_callback error_callback
, void *data
)
848 struct unit_addrs
*addrs
;
851 addrs
= (struct unit_addrs
*) vec
->vec
.base
;
852 for (i
= 0; i
< vec
->count
; ++i
)
853 free_abbrevs (state
, &addrs
[i
].u
->abbrevs
, error_callback
, data
);
856 /* Compare unit_addrs for qsort. When ranges are nested, make the
857 smallest one sort last. */
860 unit_addrs_compare (const void *v1
, const void *v2
)
862 const struct unit_addrs
*a1
= (const struct unit_addrs
*) v1
;
863 const struct unit_addrs
*a2
= (const struct unit_addrs
*) v2
;
865 if (a1
->low
< a2
->low
)
867 if (a1
->low
> a2
->low
)
869 if (a1
->high
< a2
->high
)
871 if (a1
->high
> a2
->high
)
873 if (a1
->u
->lineoff
< a2
->u
->lineoff
)
875 if (a1
->u
->lineoff
> a2
->u
->lineoff
)
880 /* Compare a PC against a unit_addrs for bsearch. Note that if there
881 are multiple ranges containing PC, which one will be returned is
882 unpredictable. We compensate for that in dwarf_fileline. */
885 unit_addrs_search (const void *vkey
, const void *ventry
)
887 const uintptr_t *key
= (const uintptr_t *) vkey
;
888 const struct unit_addrs
*entry
= (const struct unit_addrs
*) ventry
;
894 else if (pc
>= entry
->high
)
900 /* Sort the line vector by PC. We want a stable sort here. We know
901 that the pointers are into the same array, so it is safe to compare
905 line_compare (const void *v1
, const void *v2
)
907 const struct line
*ln1
= (const struct line
*) v1
;
908 const struct line
*ln2
= (const struct line
*) v2
;
910 if (ln1
->pc
< ln2
->pc
)
912 else if (ln1
->pc
> ln2
->pc
)
922 /* Find a PC in a line vector. We always allocate an extra entry at
923 the end of the lines vector, so that this routine can safely look
924 at the next entry. Note that when there are multiple mappings for
925 the same PC value, this will return the last one. */
928 line_search (const void *vkey
, const void *ventry
)
930 const uintptr_t *key
= (const uintptr_t *) vkey
;
931 const struct line
*entry
= (const struct line
*) ventry
;
937 else if (pc
>= (entry
+ 1)->pc
)
943 /* Sort the abbrevs by the abbrev code. This function is passed to
944 both qsort and bsearch. */
947 abbrev_compare (const void *v1
, const void *v2
)
949 const struct abbrev
*a1
= (const struct abbrev
*) v1
;
950 const struct abbrev
*a2
= (const struct abbrev
*) v2
;
952 if (a1
->code
< a2
->code
)
954 else if (a1
->code
> a2
->code
)
958 /* This really shouldn't happen. It means there are two
959 different abbrevs with the same code, and that means we don't
960 know which one lookup_abbrev should return. */
965 /* Read the abbreviation table for a compilation unit. Returns 1 on
966 success, 0 on failure. */
969 read_abbrevs (struct backtrace_state
*state
, uint64_t abbrev_offset
,
970 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
971 int is_bigendian
, backtrace_error_callback error_callback
,
972 void *data
, struct abbrevs
*abbrevs
)
974 struct dwarf_buf abbrev_buf
;
975 struct dwarf_buf count_buf
;
978 abbrevs
->num_abbrevs
= 0;
979 abbrevs
->abbrevs
= NULL
;
981 if (abbrev_offset
>= dwarf_abbrev_size
)
983 error_callback (data
, "abbrev offset out of range", 0);
987 abbrev_buf
.name
= ".debug_abbrev";
988 abbrev_buf
.start
= dwarf_abbrev
;
989 abbrev_buf
.buf
= dwarf_abbrev
+ abbrev_offset
;
990 abbrev_buf
.left
= dwarf_abbrev_size
- abbrev_offset
;
991 abbrev_buf
.is_bigendian
= is_bigendian
;
992 abbrev_buf
.error_callback
= error_callback
;
993 abbrev_buf
.data
= data
;
994 abbrev_buf
.reported_underflow
= 0;
996 /* Count the number of abbrevs in this list. */
998 count_buf
= abbrev_buf
;
1000 while (read_uleb128 (&count_buf
) != 0)
1002 if (count_buf
.reported_underflow
)
1006 read_uleb128 (&count_buf
);
1007 // Skip has_children.
1008 read_byte (&count_buf
);
1010 while (read_uleb128 (&count_buf
) != 0)
1011 read_uleb128 (&count_buf
);
1012 // Skip form of last attribute.
1013 read_uleb128 (&count_buf
);
1016 if (count_buf
.reported_underflow
)
1019 if (num_abbrevs
== 0)
1022 abbrevs
->num_abbrevs
= num_abbrevs
;
1023 abbrevs
->abbrevs
= ((struct abbrev
*)
1024 backtrace_alloc (state
,
1025 num_abbrevs
* sizeof (struct abbrev
),
1026 error_callback
, data
));
1027 if (abbrevs
->abbrevs
== NULL
)
1029 memset (abbrevs
->abbrevs
, 0, num_abbrevs
* sizeof (struct abbrev
));
1039 if (abbrev_buf
.reported_underflow
)
1042 code
= read_uleb128 (&abbrev_buf
);
1047 a
.tag
= (enum dwarf_tag
) read_uleb128 (&abbrev_buf
);
1048 a
.has_children
= read_byte (&abbrev_buf
);
1050 count_buf
= abbrev_buf
;
1052 while (read_uleb128 (&count_buf
) != 0)
1055 read_uleb128 (&count_buf
);
1061 read_uleb128 (&abbrev_buf
);
1062 read_uleb128 (&abbrev_buf
);
1066 attrs
= ((struct attr
*)
1067 backtrace_alloc (state
, num_attrs
* sizeof *attrs
,
1068 error_callback
, data
));
1077 name
= read_uleb128 (&abbrev_buf
);
1078 form
= read_uleb128 (&abbrev_buf
);
1081 attrs
[num_attrs
].name
= (enum dwarf_attribute
) name
;
1082 attrs
[num_attrs
].form
= (enum dwarf_form
) form
;
1087 a
.num_attrs
= num_attrs
;
1090 abbrevs
->abbrevs
[num_abbrevs
] = a
;
1094 qsort (abbrevs
->abbrevs
, abbrevs
->num_abbrevs
, sizeof (struct abbrev
),
1100 free_abbrevs (state
, abbrevs
, error_callback
, data
);
1104 /* Return the abbrev information for an abbrev code. */
1106 static const struct abbrev
*
1107 lookup_abbrev (struct abbrevs
*abbrevs
, uint64_t code
,
1108 backtrace_error_callback error_callback
, void *data
)
1113 /* With GCC, where abbrevs are simply numbered in order, we should
1114 be able to just look up the entry. */
1115 if (code
- 1 < abbrevs
->num_abbrevs
1116 && abbrevs
->abbrevs
[code
- 1].code
== code
)
1117 return &abbrevs
->abbrevs
[code
- 1];
1119 /* Otherwise we have to search. */
1120 memset (&key
, 0, sizeof key
);
1122 p
= bsearch (&key
, abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1123 sizeof (struct abbrev
), abbrev_compare
);
1126 error_callback (data
, "invalid abbreviation code", 0);
1129 return (const struct abbrev
*) p
;
1132 /* Add non-contiguous address ranges for a compilation unit. Returns
1133 1 on success, 0 on failure. */
1136 add_unit_ranges (struct backtrace_state
*state
, struct unit
*u
,
1137 uint64_t ranges
, uint64_t base
, int is_bigendian
,
1138 const unsigned char *dwarf_ranges
, size_t dwarf_ranges_size
,
1139 backtrace_error_callback error_callback
, void *data
,
1140 struct unit_addrs_vector
*addrs
)
1142 struct dwarf_buf ranges_buf
;
1144 if (ranges
>= dwarf_ranges_size
)
1146 error_callback (data
, "ranges offset out of range", 0);
1150 ranges_buf
.name
= ".debug_ranges";
1151 ranges_buf
.start
= dwarf_ranges
;
1152 ranges_buf
.buf
= dwarf_ranges
+ ranges
;
1153 ranges_buf
.left
= dwarf_ranges_size
- ranges
;
1154 ranges_buf
.is_bigendian
= is_bigendian
;
1155 ranges_buf
.error_callback
= error_callback
;
1156 ranges_buf
.data
= data
;
1157 ranges_buf
.reported_underflow
= 0;
1164 if (ranges_buf
.reported_underflow
)
1167 low
= read_address (&ranges_buf
, u
->addrsize
);
1168 high
= read_address (&ranges_buf
, u
->addrsize
);
1170 if (low
== 0 && high
== 0)
1173 if (is_highest_address (low
, u
->addrsize
))
1177 struct unit_addrs a
;
1180 a
.high
= high
+ base
;
1182 if (!add_unit_addr (state
, a
, error_callback
, data
, addrs
))
1187 if (ranges_buf
.reported_underflow
)
1193 /* Build a mapping from address ranges to the compilation units where
1194 the line number information for that range can be found. Returns 1
1195 on success, 0 on failure. */
1198 build_address_map (struct backtrace_state
*state
,
1199 const unsigned char *dwarf_info
, size_t dwarf_info_size
,
1200 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1201 const unsigned char *dwarf_ranges
, size_t dwarf_ranges_size
,
1202 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
1203 int is_bigendian
, backtrace_error_callback error_callback
,
1204 void *data
, struct unit_addrs_vector
*addrs
)
1206 struct dwarf_buf info
;
1207 struct abbrevs abbrevs
;
1209 memset (&addrs
->vec
, 0, sizeof addrs
->vec
);
1212 /* Read through the .debug_info section. FIXME: Should we use the
1213 .debug_aranges section? gdb and addr2line don't use it, but I'm
1216 info
.name
= ".debug_info";
1217 info
.start
= dwarf_info
;
1218 info
.buf
= dwarf_info
;
1219 info
.left
= dwarf_info_size
;
1220 info
.is_bigendian
= is_bigendian
;
1221 info
.error_callback
= error_callback
;
1223 info
.reported_underflow
= 0;
1225 memset (&abbrevs
, 0, sizeof abbrevs
);
1226 while (info
.left
> 0)
1228 const unsigned char *unit_data_start
;
1231 struct dwarf_buf unit_buf
;
1233 uint64_t abbrev_offset
;
1234 const struct abbrev
*abbrev
;
1236 const unsigned char *unit_data
;
1237 size_t unit_data_len
;
1238 size_t unit_data_offset
;
1245 int highpc_is_relative
;
1250 const char *comp_dir
;
1252 if (info
.reported_underflow
)
1255 unit_data_start
= info
.buf
;
1258 len
= read_uint32 (&info
);
1259 if (len
== 0xffffffff)
1261 len
= read_uint64 (&info
);
1266 unit_buf
.start
= info
.buf
;
1267 unit_buf
.left
= len
;
1269 if (!advance (&info
, len
))
1272 version
= read_uint16 (&unit_buf
);
1273 if (version
< 2 || version
> 4)
1275 dwarf_buf_error (&unit_buf
, "unrecognized DWARF version");
1279 abbrev_offset
= read_offset (&unit_buf
, is_dwarf64
);
1280 if (!read_abbrevs (state
, abbrev_offset
, dwarf_abbrev
, dwarf_abbrev_size
,
1281 is_bigendian
, error_callback
, data
, &abbrevs
))
1284 addrsize
= read_byte (&unit_buf
);
1286 unit_data
= unit_buf
.buf
;
1287 unit_data_len
= unit_buf
.left
;
1288 unit_data_offset
= unit_buf
.buf
- unit_data_start
;
1290 /* We only look at the first attribute in the compilation unit.
1291 In practice this will be a DW_TAG_compile_unit which will
1292 tell us the PC range and where to find the line number
1295 code
= read_uleb128 (&unit_buf
);
1296 abbrev
= lookup_abbrev (&abbrevs
, code
, error_callback
, data
);
1304 highpc_is_relative
= 0;
1310 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1312 struct attr_val val
;
1314 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
, is_dwarf64
,
1315 version
, addrsize
, dwarf_str
, dwarf_str_size
,
1319 switch (abbrev
->attrs
[i
].name
)
1322 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1329 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1331 highpc
= val
.u
.uint
;
1334 else if (val
.encoding
== ATTR_VAL_UINT
)
1336 highpc
= val
.u
.uint
;
1338 highpc_is_relative
= 1;
1342 if (val
.encoding
== ATTR_VAL_UINT
1343 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1345 ranges
= val
.u
.uint
;
1349 case DW_AT_stmt_list
:
1350 if (val
.encoding
== ATTR_VAL_UINT
1351 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1353 lineoff
= val
.u
.uint
;
1357 case DW_AT_comp_dir
:
1358 if (val
.encoding
== ATTR_VAL_STRING
)
1359 comp_dir
= val
.u
.string
;
1366 if (unit_buf
.reported_underflow
)
1369 if (((have_lowpc
&& have_highpc
) || have_ranges
) && have_lineoff
)
1372 struct unit_addrs a
;
1374 u
= ((struct unit
*)
1375 backtrace_alloc (state
, sizeof *u
, error_callback
, data
));
1378 u
->unit_data
= unit_data
;
1379 u
->unit_data_len
= unit_data_len
;
1380 u
->unit_data_offset
= unit_data_offset
;
1381 u
->version
= version
;
1382 u
->is_dwarf64
= is_dwarf64
;
1383 u
->addrsize
= addrsize
;
1384 u
->comp_dir
= comp_dir
;
1385 u
->lineoff
= lineoff
;
1386 u
->abbrevs
= abbrevs
;
1387 memset (&abbrevs
, 0, sizeof abbrevs
);
1389 /* The actual line number mappings will be read as
1393 u
->function_addrs
= NULL
;
1394 u
->function_addrs_count
= 0;
1398 if (!add_unit_ranges (state
, u
, ranges
, lowpc
, is_bigendian
,
1399 dwarf_ranges
, dwarf_ranges_size
,
1400 error_callback
, data
, addrs
))
1402 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1403 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1409 if (highpc_is_relative
)
1415 if (!add_unit_addr (state
, a
, error_callback
, data
, addrs
))
1417 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1418 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1425 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1426 memset (&abbrevs
, 0, sizeof abbrevs
);
1429 if (info
.reported_underflow
)
1435 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1436 free_unit_addrs_vector (state
, addrs
, error_callback
, data
);
1440 /* Add a new mapping to the vector of line mappings that we are
1441 building. Returns 1 on success, 0 on failure. */
1444 add_line (struct backtrace_state
*state
, uintptr_t pc
, const char *filename
,
1445 int lineno
, backtrace_error_callback error_callback
, void *data
,
1446 struct line_vector
*vec
)
1450 /* If we are adding the same mapping, ignore it. This can happen
1451 when using discriminators. */
1454 ln
= (struct line
*) vec
->vec
.base
+ (vec
->count
- 1);
1455 if (pc
== ln
->pc
&& filename
== ln
->filename
&& lineno
== ln
->lineno
)
1459 ln
= ((struct line
*)
1460 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1466 ln
->filename
= filename
;
1467 ln
->lineno
= lineno
;
1474 /* Free the line header information. If FREE_FILENAMES is true we
1475 free the file names themselves, otherwise we leave them, as there
1476 may be line structures pointing to them. */
1479 free_line_header (struct backtrace_state
*state
, struct line_header
*hdr
,
1480 backtrace_error_callback error_callback
, void *data
)
1482 backtrace_free (state
, hdr
->dirs
, hdr
->dirs_count
* sizeof (const char *),
1483 error_callback
, data
);
1484 backtrace_free (state
, hdr
->filenames
,
1485 hdr
->filenames_count
* sizeof (char *),
1486 error_callback
, data
);
1489 /* Read the line header. Return 1 on success, 0 on failure. */
1492 read_line_header (struct backtrace_state
*state
, struct unit
*u
,
1493 int is_dwarf64
, struct dwarf_buf
*line_buf
,
1494 struct line_header
*hdr
)
1497 struct dwarf_buf hdr_buf
;
1498 const unsigned char *p
;
1499 const unsigned char *pend
;
1502 hdr
->version
= read_uint16 (line_buf
);
1503 if (hdr
->version
< 2 || hdr
->version
> 4)
1505 dwarf_buf_error (line_buf
, "unsupported line number version");
1509 hdrlen
= read_offset (line_buf
, is_dwarf64
);
1511 hdr_buf
= *line_buf
;
1512 hdr_buf
.left
= hdrlen
;
1514 if (!advance (line_buf
, hdrlen
))
1517 hdr
->min_insn_len
= read_byte (&hdr_buf
);
1518 if (hdr
->version
< 4)
1519 hdr
->max_ops_per_insn
= 1;
1521 hdr
->max_ops_per_insn
= read_byte (&hdr_buf
);
1523 /* We don't care about default_is_stmt. */
1524 read_byte (&hdr_buf
);
1526 hdr
->line_base
= read_sbyte (&hdr_buf
);
1527 hdr
->line_range
= read_byte (&hdr_buf
);
1529 hdr
->opcode_base
= read_byte (&hdr_buf
);
1530 hdr
->opcode_lengths
= hdr_buf
.buf
;
1531 if (!advance (&hdr_buf
, hdr
->opcode_base
- 1))
1534 /* Count the number of directory entries. */
1535 hdr
->dirs_count
= 0;
1537 pend
= p
+ hdr_buf
.left
;
1538 while (p
< pend
&& *p
!= '\0')
1540 p
+= strnlen((const char *) p
, pend
- p
) + 1;
1544 hdr
->dirs
= ((const char **)
1545 backtrace_alloc (state
,
1546 hdr
->dirs_count
* sizeof (const char *),
1547 line_buf
->error_callback
, line_buf
->data
));
1548 if (hdr
->dirs
== NULL
)
1552 while (*hdr_buf
.buf
!= '\0')
1554 if (hdr_buf
.reported_underflow
)
1557 hdr
->dirs
[i
] = (const char *) hdr_buf
.buf
;
1559 if (!advance (&hdr_buf
,
1560 strnlen ((const char *) hdr_buf
.buf
, hdr_buf
.left
) + 1))
1563 if (!advance (&hdr_buf
, 1))
1566 /* Count the number of file entries. */
1567 hdr
->filenames_count
= 0;
1569 pend
= p
+ hdr_buf
.left
;
1570 while (p
< pend
&& *p
!= '\0')
1572 p
+= strnlen ((const char *) p
, pend
- p
) + 1;
1573 p
+= leb128_len (p
);
1574 p
+= leb128_len (p
);
1575 p
+= leb128_len (p
);
1576 ++hdr
->filenames_count
;
1579 hdr
->filenames
= ((const char **)
1580 backtrace_alloc (state
,
1581 hdr
->filenames_count
* sizeof (char *),
1582 line_buf
->error_callback
,
1584 if (hdr
->filenames
== NULL
)
1587 while (*hdr_buf
.buf
!= '\0')
1589 const char *filename
;
1592 if (hdr_buf
.reported_underflow
)
1595 filename
= (const char *) hdr_buf
.buf
;
1596 if (!advance (&hdr_buf
,
1597 strnlen ((const char *) hdr_buf
.buf
, hdr_buf
.left
) + 1))
1599 dir_index
= read_uleb128 (&hdr_buf
);
1600 if (IS_ABSOLUTE_PATH (filename
))
1601 hdr
->filenames
[i
] = filename
;
1606 size_t filename_len
;
1611 else if (dir_index
- 1 < hdr
->dirs_count
)
1612 dir
= hdr
->dirs
[dir_index
- 1];
1615 dwarf_buf_error (line_buf
,
1616 ("invalid directory index in "
1617 "line number program header"));
1620 dir_len
= strlen (dir
);
1621 filename_len
= strlen (filename
);
1623 backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
1624 line_buf
->error_callback
, line_buf
->data
));
1627 memcpy (s
, dir
, dir_len
);
1628 /* FIXME: If we are on a DOS-based file system, and the
1629 directory or the file name use backslashes, then we
1630 should use a backslash here. */
1632 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
1633 hdr
->filenames
[i
] = s
;
1636 /* Ignore the modification time and size. */
1637 read_uleb128 (&hdr_buf
);
1638 read_uleb128 (&hdr_buf
);
1643 if (hdr_buf
.reported_underflow
)
1649 /* Read the line program, adding line mappings to VEC. Return 1 on
1650 success, 0 on failure. */
1653 read_line_program (struct backtrace_state
*state
, struct unit
*u
,
1654 const struct line_header
*hdr
, struct dwarf_buf
*line_buf
,
1655 struct line_vector
*vec
)
1658 unsigned int op_index
;
1659 const char *reset_filename
;
1660 const char *filename
;
1665 if (hdr
->filenames_count
> 0)
1666 reset_filename
= hdr
->filenames
[0];
1668 reset_filename
= "";
1669 filename
= reset_filename
;
1671 while (line_buf
->left
> 0)
1675 op
= read_byte (line_buf
);
1676 if (op
>= hdr
->opcode_base
)
1678 unsigned int advance
;
1680 /* Special opcode. */
1681 op
-= hdr
->opcode_base
;
1682 advance
= op
/ hdr
->line_range
;
1683 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1684 / hdr
->max_ops_per_insn
);
1685 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1686 lineno
+= hdr
->line_base
+ (int) (op
% hdr
->line_range
);
1687 add_line (state
, address
, filename
, lineno
, line_buf
->error_callback
,
1688 line_buf
->data
, vec
);
1690 else if (op
== DW_LNS_extended_op
)
1694 len
= read_uleb128 (line_buf
);
1695 op
= read_byte (line_buf
);
1698 case DW_LNE_end_sequence
:
1699 /* FIXME: Should we mark the high PC here? It seems
1700 that we already have that information from the
1701 compilation unit. */
1704 filename
= reset_filename
;
1707 case DW_LNE_set_address
:
1708 address
= read_address (line_buf
, u
->addrsize
);
1710 case DW_LNE_define_file
:
1713 unsigned int dir_index
;
1715 f
= (const char *) line_buf
->buf
;
1716 if (!advance (line_buf
, strnlen (f
, line_buf
->left
) + 1))
1718 dir_index
= read_uleb128 (line_buf
);
1719 /* Ignore that time and length. */
1720 read_uleb128 (line_buf
);
1721 read_uleb128 (line_buf
);
1722 if (IS_ABSOLUTE_PATH (f
))
1733 else if (dir_index
- 1 < hdr
->dirs_count
)
1734 dir
= hdr
->dirs
[dir_index
- 1];
1737 dwarf_buf_error (line_buf
,
1738 ("invalid directory index "
1739 "in line number program"));
1742 dir_len
= strlen (dir
);
1745 backtrace_alloc (state
, dir_len
+ f_len
+ 2,
1746 line_buf
->error_callback
,
1750 memcpy (p
, dir
, dir_len
);
1751 /* FIXME: If we are on a DOS-based file system,
1752 and the directory or the file name use
1753 backslashes, then we should use a backslash
1756 memcpy (p
+ dir_len
+ 1, f
, f_len
+ 1);
1761 case DW_LNE_set_discriminator
:
1762 /* We don't care about discriminators. */
1763 read_uleb128 (line_buf
);
1766 if (!advance (line_buf
, len
- 1))
1776 add_line (state
, address
, filename
, lineno
,
1777 line_buf
->error_callback
, line_buf
->data
, vec
);
1779 case DW_LNS_advance_pc
:
1783 advance
= read_uleb128 (line_buf
);
1784 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1785 / hdr
->max_ops_per_insn
);
1786 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1789 case DW_LNS_advance_line
:
1790 lineno
+= (int) read_sleb128 (line_buf
);
1792 case DW_LNS_set_file
:
1796 fileno
= read_uleb128 (line_buf
);
1801 if (fileno
- 1 >= hdr
->filenames_count
)
1803 dwarf_buf_error (line_buf
,
1804 ("invalid file number in "
1805 "line number program"));
1808 filename
= hdr
->filenames
[fileno
- 1];
1812 case DW_LNS_set_column
:
1813 read_uleb128 (line_buf
);
1815 case DW_LNS_negate_stmt
:
1817 case DW_LNS_set_basic_block
:
1819 case DW_LNS_const_add_pc
:
1821 unsigned int advance
;
1823 op
= 255 - hdr
->opcode_base
;
1824 advance
= op
/ hdr
->line_range
;
1825 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1826 / hdr
->max_ops_per_insn
);
1827 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1830 case DW_LNS_fixed_advance_pc
:
1831 address
+= read_uint16 (line_buf
);
1834 case DW_LNS_set_prologue_end
:
1836 case DW_LNS_set_epilogue_begin
:
1838 case DW_LNS_set_isa
:
1839 read_uleb128 (line_buf
);
1845 for (i
= hdr
->opcode_lengths
[op
- 1]; i
> 0; --i
)
1846 read_uleb128 (line_buf
);
1856 /* Read the line number information for a compilation unit. Returns 1
1857 on success, 0 on failure. */
1860 read_line_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1861 backtrace_error_callback error_callback
, void *data
,
1862 struct unit
*u
, struct line_header
*hdr
, struct line
**lines
,
1863 size_t *lines_count
)
1865 struct line_vector vec
;
1866 struct dwarf_buf line_buf
;
1871 memset (&vec
.vec
, 0, sizeof vec
.vec
);
1874 memset (hdr
, 0, sizeof *hdr
);
1876 if (u
->lineoff
!= (off_t
) (size_t) u
->lineoff
1877 || (size_t) u
->lineoff
>= ddata
->dwarf_line_size
)
1879 error_callback (data
, "unit line offset out of range", 0);
1883 line_buf
.name
= ".debug_line";
1884 line_buf
.start
= ddata
->dwarf_line
;
1885 line_buf
.buf
= ddata
->dwarf_line
+ u
->lineoff
;
1886 line_buf
.left
= ddata
->dwarf_line_size
- u
->lineoff
;
1887 line_buf
.is_bigendian
= ddata
->is_bigendian
;
1888 line_buf
.error_callback
= error_callback
;
1889 line_buf
.data
= data
;
1890 line_buf
.reported_underflow
= 0;
1893 len
= read_uint32 (&line_buf
);
1894 if (len
== 0xffffffff)
1896 len
= read_uint64 (&line_buf
);
1899 line_buf
.left
= len
;
1901 if (!read_line_header (state
, u
, is_dwarf64
, &line_buf
, hdr
))
1904 if (!read_line_program (state
, u
, hdr
, &line_buf
, &vec
))
1907 if (line_buf
.reported_underflow
)
1912 /* This is not a failure in the sense of a generating an error,
1913 but it is a failure in that sense that we have no useful
1918 /* Allocate one extra entry at the end. */
1919 ln
= ((struct line
*)
1920 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1924 ln
->pc
= (uintptr_t) -1;
1925 ln
->filename
= NULL
;
1928 if (!backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
))
1931 ln
= (struct line
*) vec
.vec
.base
;
1932 qsort (ln
, vec
.count
, sizeof (struct line
), line_compare
);
1935 *lines_count
= vec
.count
;
1940 vec
.vec
.alc
+= vec
.vec
.size
;
1942 backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
);
1943 free_line_header (state
, hdr
, error_callback
, data
);
1944 *lines
= (struct line
*) (uintptr_t) -1;
1949 /* Read the name of a function from a DIE referenced by a
1950 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
1951 the same compilation unit. */
1954 read_referenced_name (struct dwarf_data
*ddata
, struct unit
*u
,
1955 uint64_t offset
, backtrace_error_callback error_callback
,
1958 struct dwarf_buf unit_buf
;
1960 const struct abbrev
*abbrev
;
1964 /* OFFSET is from the start of the data for this compilation unit.
1965 U->unit_data is the data, but it starts U->unit_data_offset bytes
1966 from the beginning. */
1968 if (offset
< u
->unit_data_offset
1969 || offset
- u
->unit_data_offset
>= u
->unit_data_len
)
1971 error_callback (data
,
1972 "abstract origin or specification out of range",
1977 offset
-= u
->unit_data_offset
;
1979 unit_buf
.name
= ".debug_info";
1980 unit_buf
.start
= ddata
->dwarf_info
;
1981 unit_buf
.buf
= u
->unit_data
+ offset
;
1982 unit_buf
.left
= u
->unit_data_len
- offset
;
1983 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
1984 unit_buf
.error_callback
= error_callback
;
1985 unit_buf
.data
= data
;
1986 unit_buf
.reported_underflow
= 0;
1988 code
= read_uleb128 (&unit_buf
);
1991 dwarf_buf_error (&unit_buf
, "invalid abstract origin or specification");
1995 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2000 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2002 struct attr_val val
;
2004 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
,
2005 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2006 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2010 switch (abbrev
->attrs
[i
].name
)
2013 /* We prefer the linkage name if get one. */
2014 if (val
.encoding
== ATTR_VAL_STRING
)
2018 case DW_AT_linkage_name
:
2019 case DW_AT_MIPS_linkage_name
:
2020 if (val
.encoding
== ATTR_VAL_STRING
)
2021 return val
.u
.string
;
2024 case DW_AT_specification
:
2025 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2026 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2028 /* This refers to a specification defined in some other
2029 compilation unit. We can handle this case if we
2030 must, but it's harder. */
2033 if (val
.encoding
== ATTR_VAL_UINT
2034 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2038 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2039 error_callback
, data
);
2053 /* Add a single range to U that maps to function. Returns 1 on
2054 success, 0 on error. */
2057 add_function_range (struct backtrace_state
*state
, struct function
*function
,
2058 uint64_t lowpc
, uint64_t highpc
,
2059 backtrace_error_callback error_callback
,
2060 void *data
, struct function_vector
*vec
)
2062 struct function_addrs
*p
;
2066 p
= (struct function_addrs
*) vec
->vec
.base
+ vec
->count
- 1;
2067 if ((lowpc
== p
->high
|| lowpc
== p
->high
+ 1)
2068 && function
== p
->function
)
2070 if (highpc
> p
->high
)
2076 p
= ((struct function_addrs
*)
2077 backtrace_vector_grow (state
, sizeof (struct function_addrs
),
2078 error_callback
, data
, &vec
->vec
));
2084 p
->function
= function
;
2089 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2093 add_function_ranges (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2094 struct unit
*u
, struct function
*function
,
2095 uint64_t ranges
, uint64_t base
,
2096 backtrace_error_callback error_callback
, void *data
,
2097 struct function_vector
*vec
)
2099 struct dwarf_buf ranges_buf
;
2101 if (ranges
>= ddata
->dwarf_ranges_size
)
2103 error_callback (data
, "function ranges offset out of range", 0);
2107 ranges_buf
.name
= ".debug_ranges";
2108 ranges_buf
.start
= ddata
->dwarf_ranges
;
2109 ranges_buf
.buf
= ddata
->dwarf_ranges
+ ranges
;
2110 ranges_buf
.left
= ddata
->dwarf_ranges_size
- ranges
;
2111 ranges_buf
.is_bigendian
= ddata
->is_bigendian
;
2112 ranges_buf
.error_callback
= error_callback
;
2113 ranges_buf
.data
= data
;
2114 ranges_buf
.reported_underflow
= 0;
2121 if (ranges_buf
.reported_underflow
)
2124 low
= read_address (&ranges_buf
, u
->addrsize
);
2125 high
= read_address (&ranges_buf
, u
->addrsize
);
2127 if (low
== 0 && high
== 0)
2130 if (is_highest_address (low
, u
->addrsize
))
2134 if (!add_function_range (state
, function
, low
+ base
, high
+ base
,
2135 error_callback
, data
, vec
))
2140 if (ranges_buf
.reported_underflow
)
2146 /* Read one entry plus all its children. Add function addresses to
2147 VEC. Returns 1 on success, 0 on error. */
2150 read_function_entry (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2151 struct unit
*u
, uint64_t base
, struct dwarf_buf
*unit_buf
,
2152 const struct line_header
*lhdr
,
2153 backtrace_error_callback error_callback
, void *data
,
2154 struct function_vector
*vec
)
2156 while (unit_buf
->left
> 0)
2159 const struct abbrev
*abbrev
;
2161 struct function
*function
;
2167 int highpc_is_relative
;
2171 code
= read_uleb128 (unit_buf
);
2175 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2179 is_function
= (abbrev
->tag
== DW_TAG_subprogram
2180 || abbrev
->tag
== DW_TAG_entry_point
2181 || abbrev
->tag
== DW_TAG_inlined_subroutine
);
2186 function
= ((struct function
*)
2187 backtrace_alloc (state
, sizeof *function
,
2188 error_callback
, data
));
2189 if (function
== NULL
)
2191 memset (function
, 0, sizeof *function
);
2198 highpc_is_relative
= 0;
2201 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2203 struct attr_val val
;
2205 if (!read_attribute (abbrev
->attrs
[i
].form
, unit_buf
,
2206 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2207 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2211 /* The compile unit sets the base address for any address
2212 ranges in the function entries. */
2213 if (abbrev
->tag
== DW_TAG_compile_unit
2214 && abbrev
->attrs
[i
].name
== DW_AT_low_pc
2215 && val
.encoding
== ATTR_VAL_ADDRESS
)
2220 switch (abbrev
->attrs
[i
].name
)
2222 case DW_AT_call_file
:
2223 if (val
.encoding
== ATTR_VAL_UINT
)
2225 if (val
.u
.uint
== 0)
2226 function
->caller_filename
= "";
2229 if (val
.u
.uint
- 1 >= lhdr
->filenames_count
)
2231 dwarf_buf_error (unit_buf
,
2232 ("invalid file number in "
2233 "DW_AT_call_file attribute"));
2236 function
->caller_filename
=
2237 lhdr
->filenames
[val
.u
.uint
- 1];
2242 case DW_AT_call_line
:
2243 if (val
.encoding
== ATTR_VAL_UINT
)
2244 function
->caller_lineno
= val
.u
.uint
;
2247 case DW_AT_abstract_origin
:
2248 case DW_AT_specification
:
2249 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2250 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2252 /* This refers to an abstract origin defined in
2253 some other compilation unit. We can handle
2254 this case if we must, but it's harder. */
2257 if (val
.encoding
== ATTR_VAL_UINT
2258 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2262 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2263 error_callback
, data
);
2265 function
->name
= name
;
2270 if (val
.encoding
== ATTR_VAL_STRING
)
2272 /* Don't override a name we found in some other
2273 way, as it will normally be more
2274 useful--e.g., this name is normally not
2276 if (function
->name
== NULL
)
2277 function
->name
= val
.u
.string
;
2281 case DW_AT_linkage_name
:
2282 case DW_AT_MIPS_linkage_name
:
2283 if (val
.encoding
== ATTR_VAL_STRING
)
2284 function
->name
= val
.u
.string
;
2288 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2296 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2298 highpc
= val
.u
.uint
;
2301 else if (val
.encoding
== ATTR_VAL_UINT
)
2303 highpc
= val
.u
.uint
;
2305 highpc_is_relative
= 1;
2310 if (val
.encoding
== ATTR_VAL_UINT
2311 || val
.encoding
== ATTR_VAL_REF_SECTION
)
2313 ranges
= val
.u
.uint
;
2324 /* If we couldn't find a name for the function, we have no use
2326 if (is_function
&& function
->name
== NULL
)
2328 backtrace_free (state
, function
, sizeof *function
,
2329 error_callback
, data
);
2337 if (!add_function_ranges (state
, ddata
, u
, function
, ranges
,
2338 base
, error_callback
, data
, vec
))
2341 else if (have_lowpc
&& have_highpc
)
2343 if (highpc_is_relative
)
2345 if (!add_function_range (state
, function
, lowpc
, highpc
,
2346 error_callback
, data
, vec
))
2351 backtrace_free (state
, function
, sizeof *function
,
2352 error_callback
, data
);
2357 if (abbrev
->has_children
)
2361 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2362 error_callback
, data
, vec
))
2367 struct function_vector fvec
;
2369 /* Gather any information for inlined functions in
2372 memset (&fvec
, 0, sizeof fvec
);
2374 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2375 error_callback
, data
, &fvec
))
2380 struct function_addrs
*faddrs
;
2382 if (!backtrace_vector_release (state
, &fvec
.vec
,
2383 error_callback
, data
))
2386 faddrs
= (struct function_addrs
*) fvec
.vec
.base
;
2387 qsort (faddrs
, fvec
.count
,
2388 sizeof (struct function_addrs
),
2389 function_addrs_compare
);
2391 function
->function_addrs
= faddrs
;
2392 function
->function_addrs_count
= fvec
.count
;
2401 /* Read function name information for a compilation unit. We look
2402 through the whole unit looking for function tags. */
2405 read_function_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2406 const struct line_header
*lhdr
,
2407 backtrace_error_callback error_callback
, void *data
,
2408 struct unit
*u
, struct function_vector
*fvec
,
2409 struct function_addrs
**ret_addrs
,
2410 size_t *ret_addrs_count
)
2412 struct dwarf_buf unit_buf
;
2413 struct function_addrs
*addrs
;
2416 unit_buf
.name
= ".debug_info";
2417 unit_buf
.start
= ddata
->dwarf_info
;
2418 unit_buf
.buf
= u
->unit_data
;
2419 unit_buf
.left
= u
->unit_data_len
;
2420 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2421 unit_buf
.error_callback
= error_callback
;
2422 unit_buf
.data
= data
;
2423 unit_buf
.reported_underflow
= 0;
2425 while (unit_buf
.left
> 0)
2427 if (!read_function_entry (state
, ddata
, u
, 0, &unit_buf
, lhdr
,
2428 error_callback
, data
, fvec
))
2432 if (fvec
->count
== 0)
2435 addrs
= (struct function_addrs
*) fvec
->vec
.base
;
2436 addrs_count
= fvec
->count
;
2438 /* Finish this list of addresses, but leave the remaining space in
2439 the vector available for the next function unit. */
2440 backtrace_vector_finish (state
, &fvec
->vec
);
2443 qsort (addrs
, addrs_count
, sizeof (struct function_addrs
),
2444 function_addrs_compare
);
2447 *ret_addrs_count
= addrs_count
;
2450 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2451 information, and update FILENAME and LINENO for the caller.
2452 Returns whatever CALLBACK returns, or 0 to keep going. */
2455 report_inlined_functions (uintptr_t pc
, struct function
*function
,
2456 backtrace_full_callback callback
, void *data
,
2457 const char **filename
, int *lineno
)
2459 struct function_addrs
*function_addrs
;
2460 struct function
*inlined
;
2463 if (function
->function_addrs_count
== 0)
2466 function_addrs
= ((struct function_addrs
*)
2467 bsearch (&pc
, function
->function_addrs
,
2468 function
->function_addrs_count
,
2469 sizeof (struct function_addrs
),
2470 function_addrs_search
));
2471 if (function_addrs
== NULL
)
2474 while (((size_t) (function_addrs
- function
->function_addrs
) + 1
2475 < function
->function_addrs_count
)
2476 && pc
>= (function_addrs
+ 1)->low
2477 && pc
< (function_addrs
+ 1)->high
)
2480 /* We found an inlined call. */
2482 inlined
= function_addrs
->function
;
2484 /* Report any calls inlined into this one. */
2485 ret
= report_inlined_functions (pc
, inlined
, callback
, data
,
2490 /* Report this inlined call. */
2491 ret
= callback (data
, pc
, *filename
, *lineno
, inlined
->name
);
2495 /* Our caller will report the caller of the inlined function; tell
2496 it the appropriate filename and line number. */
2497 *filename
= inlined
->caller_filename
;
2498 *lineno
= inlined
->caller_lineno
;
2503 /* Return the file/line information for a PC using the DWARF mapping
2504 we built earlier. */
2507 dwarf_fileline (struct backtrace_state
*state
, uintptr_t pc
,
2508 backtrace_full_callback callback
,
2509 backtrace_error_callback error_callback
, void *data
)
2511 struct dwarf_data
*ddata
;
2512 struct unit_addrs
*entry
;
2517 struct function_addrs
*function_addrs
;
2518 struct function
*function
;
2519 const char *filename
;
2523 ddata
= (struct dwarf_data
*) state
->fileline_data
;
2525 /* Find an address range that includes PC. */
2526 entry
= bsearch (&pc
, ddata
->addrs
, ddata
->addrs_count
,
2527 sizeof (struct unit_addrs
), unit_addrs_search
);
2530 return callback (data
, pc
, NULL
, 0, NULL
);
2532 /* If there are multiple ranges that contain PC, use the last one,
2533 in order to produce predictable results. If we assume that all
2534 ranges are properly nested, then the last range will be the
2536 while ((size_t) (entry
- ddata
->addrs
) + 1 < ddata
->addrs_count
2537 && pc
>= (entry
+ 1)->low
2538 && pc
< (entry
+ 1)->high
)
2541 /* We need the lines, lines_count, function_addrs,
2542 function_addrs_count fields of u. If they are not set, we need
2543 to set them. When running in threaded mode, we need to allow for
2544 the possibility that some other thread is setting them
2550 /* Skip units with no useful line number information by walking
2551 backward. Useless line number information is marked by setting
2553 while (entry
> ddata
->addrs
2554 && pc
>= (entry
- 1)->low
2555 && pc
< (entry
- 1)->high
)
2557 if (state
->threaded
)
2559 /* Use __sync_bool_compare_and_swap to do a
2561 while (!__sync_bool_compare_and_swap (&u
->lines
, lines
, lines
))
2565 if (lines
!= (struct line
*) (uintptr_t) -1)
2574 /* Do a load-acquire of u->lines. */
2575 if (state
->threaded
)
2577 /* Use __sync_bool_compare_and_swap to do an atomic load. */
2578 while (!__sync_bool_compare_and_swap (&u
->lines
, lines
, lines
))
2585 size_t function_addrs_count
;
2586 struct line_header lhdr
;
2589 /* We have never read the line information for this unit. Read
2592 function_addrs
= NULL
;
2593 function_addrs_count
= 0;
2594 if (read_line_info (state
, ddata
, error_callback
, data
, entry
->u
, &lhdr
,
2597 read_function_info (state
, ddata
, &lhdr
, error_callback
, data
,
2598 entry
->u
, &ddata
->fvec
, &function_addrs
,
2599 &function_addrs_count
);
2600 free_line_header (state
, &lhdr
, error_callback
, data
);
2604 /* Atomically store the information we just read into the unit.
2605 If another thread is simultaneously writing, it presumably
2606 read the same information, and we don't care which one we
2607 wind up with; we just leak the other one. We do have to
2608 write the lines field last, so that the acquire-loads above
2609 ensure that the other fields are set. */
2611 if (!state
->threaded
)
2613 u
->lines_count
= count
;
2614 u
->function_addrs
= function_addrs
;
2615 u
->function_addrs_count
= function_addrs_count
;
2620 __sync_bool_compare_and_swap (&u
->lines_count
, 0, count
);
2621 __sync_bool_compare_and_swap (&u
->function_addrs
, NULL
,
2623 __sync_bool_compare_and_swap (&u
->function_addrs_count
, 0,
2624 function_addrs_count
);
2625 __sync_bool_compare_and_swap (&u
->lines
, NULL
, lines
);
2629 /* Now all fields of U have been initialized. */
2631 if (lines
== (struct line
*) (uintptr_t) -1)
2633 /* If reading the line number information failed in some way,
2634 try again to see if there is a better compilation unit for
2637 dwarf_fileline (state
, pc
, callback
, error_callback
, data
);
2638 return callback (data
, pc
, NULL
, 0, NULL
);
2641 /* Search for PC within this unit. */
2643 ln
= (struct line
*) bsearch (&pc
, lines
, entry
->u
->lines_count
,
2644 sizeof (struct line
), line_search
);
2647 error_callback (data
, "inconsistent DWARF line number info", 0);
2651 /* Search for function name within this unit. */
2653 if (entry
->u
->function_addrs_count
== 0)
2654 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2656 function_addrs
= ((struct function_addrs
*)
2657 bsearch (&pc
, entry
->u
->function_addrs
,
2658 entry
->u
->function_addrs_count
,
2659 sizeof (struct function_addrs
),
2660 function_addrs_search
));
2661 if (function_addrs
== NULL
)
2662 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2664 /* If there are multiple function ranges that contain PC, use the
2665 last one, in order to produce predictable results. */
2667 while (((size_t) (function_addrs
- entry
->u
->function_addrs
+ 1)
2668 < entry
->u
->function_addrs_count
)
2669 && pc
>= (function_addrs
+ 1)->low
2670 && pc
< (function_addrs
+ 1)->high
)
2673 function
= function_addrs
->function
;
2675 filename
= ln
->filename
;
2676 lineno
= ln
->lineno
;
2678 ret
= report_inlined_functions (pc
, function
, callback
, data
,
2679 &filename
, &lineno
);
2683 return callback (data
, pc
, filename
, lineno
, function
->name
);
2686 /* Build our data structures from the .debug_info and .debug_line
2687 sections. Set *FILELINE_FN and *FILELINE_DATA. Return 1 on
2688 success, 0 on failure. */
2691 backtrace_dwarf_initialize (struct backtrace_state
*state
,
2692 const unsigned char *dwarf_info
,
2693 size_t dwarf_info_size
,
2694 const unsigned char *dwarf_line
,
2695 size_t dwarf_line_size
,
2696 const unsigned char *dwarf_abbrev
,
2697 size_t dwarf_abbrev_size
,
2698 const unsigned char *dwarf_ranges
,
2699 size_t dwarf_ranges_size
,
2700 const unsigned char *dwarf_str
,
2701 size_t dwarf_str_size
,
2703 backtrace_error_callback error_callback
,
2704 void *data
, fileline
*fileline_fn
)
2706 struct unit_addrs_vector addrs_vec
;
2707 struct unit_addrs
*addrs
;
2709 struct dwarf_data
*fdata
;
2711 if (!build_address_map (state
, dwarf_info
, dwarf_info_size
, dwarf_abbrev
,
2712 dwarf_abbrev_size
, dwarf_ranges
, dwarf_ranges_size
,
2713 dwarf_str
, dwarf_str_size
, is_bigendian
,
2714 error_callback
, data
, &addrs_vec
))
2717 if (!backtrace_vector_release (state
, &addrs_vec
.vec
, error_callback
, data
))
2719 addrs
= (struct unit_addrs
*) addrs_vec
.vec
.base
;
2720 addrs_count
= addrs_vec
.count
;
2721 qsort (addrs
, addrs_count
, sizeof (struct unit_addrs
), unit_addrs_compare
);
2723 fdata
= ((struct dwarf_data
*)
2724 backtrace_alloc (state
, sizeof (struct dwarf_data
),
2725 error_callback
, data
));
2729 fdata
->addrs
= addrs
;
2730 fdata
->addrs_count
= addrs_count
;
2731 fdata
->dwarf_info
= dwarf_info
;
2732 fdata
->dwarf_info_size
= dwarf_info_size
;
2733 fdata
->dwarf_line
= dwarf_line
;
2734 fdata
->dwarf_line_size
= dwarf_line_size
;
2735 fdata
->dwarf_ranges
= dwarf_ranges
;
2736 fdata
->dwarf_ranges_size
= dwarf_ranges_size
;
2737 fdata
->dwarf_str
= dwarf_str
;
2738 fdata
->dwarf_str_size
= dwarf_str_size
;
2739 fdata
->is_bigendian
= is_bigendian
;
2740 memset (&fdata
->fvec
, 0, sizeof fdata
->fvec
);
2742 state
->fileline_data
= fdata
;
2744 *fileline_fn
= dwarf_fileline
;