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 /* A buffer to read DWARF info. */
51 /* Buffer name for error messages. */
53 /* Start of the buffer. */
54 const unsigned char *start
;
55 /* Next byte to read. */
56 const unsigned char *buf
;
57 /* The number of bytes remaining. */
59 /* Whether the data is big-endian. */
61 /* Error callback routine. */
62 backtrace_error_callback error_callback
;
63 /* Data for error_callback. */
65 /* Non-zero if we've reported an underflow error. */
66 int reported_underflow
;
69 /* A single attribute in a DWARF abbreviation. */
73 /* The attribute name. */
74 enum dwarf_attribute name
;
75 /* The attribute form. */
79 /* A single DWARF abbreviation. */
83 /* The abbrev code--the number used to refer to the abbrev. */
87 /* Non-zero if this abbrev has child entries. */
89 /* The number of attributes. */
95 /* The DWARF abbreviations for a compilation unit. This structure
96 only exists while reading the compilation unit. Most DWARF readers
97 seem to a hash table to map abbrev ID's to abbrev entries.
98 However, we primarily care about GCC, and GCC simply issues ID's in
99 numerical order starting at 1. So we simply keep a sorted vector,
100 and try to just look up the code. */
104 /* The number of abbrevs in the vector. */
106 /* The abbrevs, sorted by the code field. */
107 struct abbrev
*abbrevs
;
110 /* The different kinds of attribute values. */
112 enum attr_val_encoding
116 /* A unsigned integer. */
118 /* A sigd integer. */
122 /* An offset to other data in the containing unit. */
124 /* An offset to other data within the .dwarf_info section. */
126 /* An offset to data in some other section. */
127 ATTR_VAL_REF_SECTION
,
128 /* A type signature. */
130 /* A block of data (not represented). */
132 /* An expression (not represented). */
136 /* An attribute value. */
140 /* How the value is stored in the field u. */
141 enum attr_val_encoding encoding
;
144 /* ATTR_VAL_ADDRESS, ATTR_VAL_UINT, ATTR_VAL_REF*. */
148 /* ATTR_VAL_STRING. */
150 /* ATTR_VAL_BLOCK not stored. */
154 /* The line number program header. */
158 /* The version of the line number information. */
160 /* The minimum instruction length. */
161 unsigned int min_insn_len
;
162 /* The maximum number of ops per instruction. */
163 unsigned int max_ops_per_insn
;
164 /* The line base for special opcodes. */
166 /* The line range for special opcodes. */
167 unsigned int line_range
;
168 /* The opcode base--the first special opcode. */
169 unsigned int opcode_base
;
170 /* Opcode lengths, indexed by opcode - 1. */
171 const unsigned char *opcode_lengths
;
172 /* The number of directory entries. */
174 /* The directory entries. */
176 /* The number of filenames. */
177 size_t filenames_count
;
179 const char **filenames
;
182 /* Map a single PC value to a file/line. We will keep a vector of
183 these sorted by PC value. Each file/line will be correct from the
184 PC up to the PC of the next entry if there is one. We allocate one
185 extra entry at the end so that we can use bsearch. */
191 /* File name. Many entries in the array are expected to point to
192 the same file name. */
193 const char *filename
;
198 /* A growable vector of line number information. This is used while
199 reading the line numbers. */
203 /* Memory. This is an array of struct line. */
204 struct backtrace_vector vec
;
205 /* Number of valid mappings. */
209 /* A function described in the debug info. */
213 /* The name of the function. */
215 /* If this is an inlined function, the filename of the call
217 const char *caller_filename
;
218 /* If this is an inlined function, the line number of the call
221 /* Map PC ranges to inlined functions. */
222 struct function_addrs
*function_addrs
;
223 size_t function_addrs_count
;
226 /* An address range for a function. This maps a PC value to a
227 specific function. */
229 struct function_addrs
231 /* Range is LOW <= PC < HIGH. */
234 /* Function for this address range. */
235 struct function
*function
;
238 /* A growable vector of function address ranges. */
240 struct function_vector
242 /* Memory. This is an array of struct function_addrs. */
243 struct backtrace_vector vec
;
244 /* Number of address ranges present. */
248 /* A DWARF compilation unit. This only holds the information we need
249 to map a PC to a file and line. */
253 /* The first entry for this compilation unit. */
254 const unsigned char *unit_data
;
255 /* The length of the data for this compilation unit. */
256 size_t unit_data_len
;
257 /* The offset of UNIT_DATA from the start of the information for
258 this compilation unit. */
259 size_t unit_data_offset
;
262 /* Whether unit is DWARF64. */
266 /* Offset into line number information. */
268 /* Compilation command working directory. */
269 const char *comp_dir
;
270 /* The abbreviations for this unit. */
271 struct abbrevs abbrevs
;
273 /* The fields above this point are read in during initialization and
274 may be accessed freely. The fields below this point are read in
275 as needed, and therefore require care, as different threads may
276 try to initialize them simultaneously. */
278 /* PC to line number mapping. This is NULL if the values have not
279 been read. This is (struct line *) -1 if there was an error
280 reading the values. */
282 /* Number of entries in lines. */
284 /* PC ranges to function. */
285 struct function_addrs
*function_addrs
;
286 size_t function_addrs_count
;
289 /* An address range for a compilation unit. This maps a PC value to a
290 specific compilation unit. Note that we invert the representation
291 in DWARF: instead of listing the units and attaching a list of
292 ranges, we list the ranges and have each one point to the unit.
293 This lets us do a binary search to find the unit. */
297 /* Range is LOW <= PC < HIGH. */
300 /* Compilation unit for this address range. */
304 /* A growable vector of compilation unit address ranges. */
306 struct unit_addrs_vector
308 /* Memory. This is an array of struct unit_addrs. */
309 struct backtrace_vector vec
;
310 /* Number of address ranges present. */
314 /* The information we need to map a PC to a file and line. */
318 /* A sorted list of address ranges. */
319 struct unit_addrs
*addrs
;
320 /* Number of address ranges in list. */
322 /* The unparsed .debug_info section. */
323 const unsigned char *dwarf_info
;
324 size_t dwarf_info_size
;
325 /* The unparsed .debug_line section. */
326 const unsigned char *dwarf_line
;
327 size_t dwarf_line_size
;
328 /* The unparsed .debug_ranges section. */
329 const unsigned char *dwarf_ranges
;
330 size_t dwarf_ranges_size
;
331 /* The unparsed .debug_str section. */
332 const unsigned char *dwarf_str
;
333 size_t dwarf_str_size
;
334 /* Whether the data is big-endian or not. */
336 /* A vector used for function addresses. We keep this here so that
337 we can grow the vector as we read more functions. */
338 struct function_vector fvec
;
341 /* Report an error for a DWARF buffer. */
344 dwarf_buf_error (struct dwarf_buf
*buf
, const char *msg
)
348 snprintf (b
, sizeof b
, "%s in %s at %d",
349 msg
, buf
->name
, (int) (buf
->buf
- buf
->start
));
350 buf
->error_callback (buf
->data
, b
, 0);
353 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
357 require (struct dwarf_buf
*buf
, size_t count
)
359 if (buf
->left
>= count
)
362 if (!buf
->reported_underflow
)
364 dwarf_buf_error (buf
, "DWARF underflow");
365 buf
->reported_underflow
= 1;
371 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
375 advance (struct dwarf_buf
*buf
, size_t count
)
377 if (!require (buf
, count
))
384 /* Read one byte from BUF and advance 1 byte. */
387 read_byte (struct dwarf_buf
*buf
)
389 const unsigned char *p
= buf
->buf
;
391 if (!advance (buf
, 1))
396 /* Read a signed char from BUF and advance 1 byte. */
399 read_sbyte (struct dwarf_buf
*buf
)
401 const unsigned char *p
= buf
->buf
;
403 if (!advance (buf
, 1))
405 return (*p
^ 0x80) - 0x80;
408 /* Read a uint16 from BUF and advance 2 bytes. */
411 read_uint16 (struct dwarf_buf
*buf
)
413 const unsigned char *p
= buf
->buf
;
415 if (!advance (buf
, 2))
417 if (buf
->is_bigendian
)
418 return ((uint16_t) p
[0] << 8) | (uint16_t) p
[1];
420 return ((uint16_t) p
[1] << 8) | (uint16_t) p
[0];
423 /* Read a uint32 from BUF and advance 4 bytes. */
426 read_uint32 (struct dwarf_buf
*buf
)
428 const unsigned char *p
= buf
->buf
;
430 if (!advance (buf
, 4))
432 if (buf
->is_bigendian
)
433 return (((uint32_t) p
[0] << 24) | ((uint32_t) p
[1] << 16)
434 | ((uint32_t) p
[2] << 8) | (uint32_t) p
[3]);
436 return (((uint32_t) p
[3] << 24) | ((uint32_t) p
[2] << 16)
437 | ((uint32_t) p
[1] << 8) | (uint32_t) p
[0]);
440 /* Read a uint64 from BUF and advance 8 bytes. */
443 read_uint64 (struct dwarf_buf
*buf
)
445 const unsigned char *p
= buf
->buf
;
447 if (!advance (buf
, 8))
449 if (buf
->is_bigendian
)
450 return (((uint64_t) p
[0] << 56) | ((uint64_t) p
[1] << 48)
451 | ((uint64_t) p
[2] << 40) | ((uint64_t) p
[3] << 32)
452 | ((uint64_t) p
[4] << 24) | ((uint64_t) p
[5] << 16)
453 | ((uint64_t) p
[6] << 8) | (uint64_t) p
[7]);
455 return (((uint64_t) p
[7] << 56) | ((uint64_t) p
[6] << 48)
456 | ((uint64_t) p
[5] << 40) | ((uint64_t) p
[4] << 32)
457 | ((uint64_t) p
[3] << 24) | ((uint64_t) p
[2] << 16)
458 | ((uint64_t) p
[1] << 8) | (uint64_t) p
[0]);
461 /* Read an offset from BUF and advance the appropriate number of
465 read_offset (struct dwarf_buf
*buf
, int is_dwarf64
)
468 return read_uint64 (buf
);
470 return read_uint32 (buf
);
473 /* Read an address from BUF and advance the appropriate number of
477 read_address (struct dwarf_buf
*buf
, int addrsize
)
482 return read_byte (buf
);
484 return read_uint16 (buf
);
486 return read_uint32 (buf
);
488 return read_uint64 (buf
);
490 dwarf_buf_error (buf
, "unrecognized address size");
495 /* Return whether a value is the highest possible address, given the
499 is_highest_address (uint64_t address
, int addrsize
)
504 return address
== (unsigned char) -1;
506 return address
== (uint16_t) -1;
508 return address
== (uint32_t) -1;
510 return address
== (uint64_t) -1;
516 /* Read an unsigned LEB128 number. */
519 read_uleb128 (struct dwarf_buf
*buf
)
529 const unsigned char *p
;
532 if (!advance (buf
, 1))
535 ret
|= ((uint64_t) (b
& 0x7f)) << shift
;
538 while ((b
& 0x80) != 0);
541 dwarf_buf_error (buf
, "LEB128 overflows uint64_5");
546 /* Read a signed LEB128 number. */
549 read_sleb128 (struct dwarf_buf
*buf
)
559 const unsigned char *p
;
562 if (!advance (buf
, 1))
565 val
|= ((uint64_t) (b
& 0x7f)) << shift
;
568 while ((b
& 0x80) != 0);
571 dwarf_buf_error (buf
, "signed LEB128 overflows uint64_t");
574 val
|= ((uint64_t) -1) << shift
;
576 return (int64_t) val
;
579 /* Return the length of an LEB128 number. */
582 leb128_len (const unsigned char *p
)
587 while ((*p
& 0x80) != 0)
595 /* Free an abbreviations structure. */
598 free_abbrevs (struct backtrace_state
*state
, struct abbrevs
*abbrevs
,
599 backtrace_error_callback error_callback
, void *data
)
603 for (i
= 0; i
< abbrevs
->num_abbrevs
; ++i
)
604 backtrace_free (state
, abbrevs
->abbrevs
[i
].attrs
,
605 abbrevs
->abbrevs
[i
].num_attrs
* sizeof (struct attr
),
606 error_callback
, data
);
607 backtrace_free (state
, abbrevs
->abbrevs
,
608 abbrevs
->num_abbrevs
* sizeof (struct abbrev
),
609 error_callback
, data
);
610 abbrevs
->num_abbrevs
= 0;
611 abbrevs
->abbrevs
= NULL
;
614 /* Read an attribute value. Returns 1 on success, 0 on failure. If
615 the value can be represented as a uint64_t, sets *VAL and sets
616 *IS_VALID to 1. We don't try to store the value of other attribute
617 forms, because we don't care about them. */
620 read_attribute (enum dwarf_form form
, struct dwarf_buf
*buf
,
621 int is_dwarf64
, int version
, int addrsize
,
622 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
623 struct attr_val
*val
)
628 val
->encoding
= ATTR_VAL_ADDRESS
;
629 val
->u
.uint
= read_address (buf
, addrsize
);
632 val
->encoding
= ATTR_VAL_BLOCK
;
633 return advance (buf
, read_uint16 (buf
));
635 val
->encoding
= ATTR_VAL_BLOCK
;
636 return advance (buf
, read_uint32 (buf
));
638 val
->encoding
= ATTR_VAL_UINT
;
639 val
->u
.uint
= read_uint16 (buf
);
642 val
->encoding
= ATTR_VAL_UINT
;
643 val
->u
.uint
= read_uint32 (buf
);
646 val
->encoding
= ATTR_VAL_UINT
;
647 val
->u
.uint
= read_uint64 (buf
);
650 val
->encoding
= ATTR_VAL_STRING
;
651 val
->u
.string
= (const char *) buf
->buf
;
652 return advance (buf
, strnlen ((const char *) buf
->buf
, buf
->left
) + 1);
654 val
->encoding
= ATTR_VAL_BLOCK
;
655 return advance (buf
, read_uleb128 (buf
));
657 val
->encoding
= ATTR_VAL_BLOCK
;
658 return advance (buf
, read_byte (buf
));
660 val
->encoding
= ATTR_VAL_UINT
;
661 val
->u
.uint
= read_byte (buf
);
664 val
->encoding
= ATTR_VAL_UINT
;
665 val
->u
.uint
= read_byte (buf
);
668 val
->encoding
= ATTR_VAL_SINT
;
669 val
->u
.sint
= read_sleb128 (buf
);
675 offset
= read_offset (buf
, is_dwarf64
);
676 if (offset
>= dwarf_str_size
)
678 dwarf_buf_error (buf
, "DW_FORM_strp out of range");
681 val
->encoding
= ATTR_VAL_STRING
;
682 val
->u
.string
= (const char *) dwarf_str
+ offset
;
686 val
->encoding
= ATTR_VAL_UINT
;
687 val
->u
.uint
= read_uleb128 (buf
);
689 case DW_FORM_ref_addr
:
690 val
->encoding
= ATTR_VAL_REF_INFO
;
692 val
->u
.uint
= read_address (buf
, addrsize
);
694 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
697 val
->encoding
= ATTR_VAL_REF_UNIT
;
698 val
->u
.uint
= read_byte (buf
);
701 val
->encoding
= ATTR_VAL_REF_UNIT
;
702 val
->u
.uint
= read_uint16 (buf
);
705 val
->encoding
= ATTR_VAL_REF_UNIT
;
706 val
->u
.uint
= read_uint32 (buf
);
709 val
->encoding
= ATTR_VAL_REF_UNIT
;
710 val
->u
.uint
= read_uint64 (buf
);
712 case DW_FORM_ref_udata
:
713 val
->encoding
= ATTR_VAL_REF_UNIT
;
714 val
->u
.uint
= read_uleb128 (buf
);
716 case DW_FORM_indirect
:
720 form
= read_uleb128 (buf
);
721 return read_attribute ((enum dwarf_form
) form
, buf
, is_dwarf64
,
722 version
, addrsize
, dwarf_str
, dwarf_str_size
,
725 case DW_FORM_sec_offset
:
726 val
->encoding
= ATTR_VAL_REF_SECTION
;
727 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
729 case DW_FORM_exprloc
:
730 val
->encoding
= ATTR_VAL_EXPR
;
731 return advance (buf
, read_uleb128 (buf
));
732 case DW_FORM_flag_present
:
733 val
->encoding
= ATTR_VAL_UINT
;
736 case DW_FORM_ref_sig8
:
737 val
->encoding
= ATTR_VAL_REF_TYPE
;
738 val
->u
.uint
= read_uint64 (buf
);
740 case DW_FORM_GNU_addr_index
:
741 val
->encoding
= ATTR_VAL_REF_SECTION
;
742 val
->u
.uint
= read_uleb128 (buf
);
744 case DW_FORM_GNU_str_index
:
745 val
->encoding
= ATTR_VAL_REF_SECTION
;
746 val
->u
.uint
= read_uleb128 (buf
);
748 case DW_FORM_GNU_ref_alt
:
749 val
->encoding
= ATTR_VAL_REF_SECTION
;
750 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
752 case DW_FORM_GNU_strp_alt
:
753 val
->encoding
= ATTR_VAL_REF_SECTION
;
754 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
757 dwarf_buf_error (buf
, "unrecognized DWARF form");
762 /* Compare function_addrs for qsort. When ranges are nested, make the
763 smallest one sort last. */
766 function_addrs_compare (const void *v1
, const void *v2
)
768 const struct function_addrs
*a1
= (const struct function_addrs
*) v1
;
769 const struct function_addrs
*a2
= (const struct function_addrs
*) v2
;
771 if (a1
->low
< a2
->low
)
773 if (a1
->low
> a2
->low
)
775 if (a1
->high
< a2
->high
)
777 if (a1
->high
> a2
->high
)
779 return strcmp (a1
->function
->name
, a2
->function
->name
);
782 /* Compare a PC against a function_addrs for bsearch. Note that if
783 there are multiple ranges containing PC, which one will be returned
784 is unpredictable. We compensate for that in dwarf_fileline. */
787 function_addrs_search (const void *vkey
, const void *ventry
)
789 const uintptr_t *key
= (const uintptr_t *) vkey
;
790 const struct function_addrs
*entry
= (const struct function_addrs
*) ventry
;
796 else if (pc
>= entry
->high
)
802 /* Add a new compilation unit address range to a vector. Returns 1 on
803 success, 0 on failure. */
806 add_unit_addr (struct backtrace_state
*state
, struct unit_addrs addrs
,
807 backtrace_error_callback error_callback
, void *data
,
808 struct unit_addrs_vector
*vec
)
810 struct unit_addrs
*p
;
812 /* Try to merge with the last entry. */
815 p
= (struct unit_addrs
*) vec
->vec
.base
+ (vec
->count
- 1);
816 if ((addrs
.low
== p
->high
|| addrs
.low
== p
->high
+ 1)
819 if (addrs
.high
> p
->high
)
820 p
->high
= addrs
.high
;
825 p
= ((struct unit_addrs
*)
826 backtrace_vector_grow (state
, sizeof (struct unit_addrs
),
827 error_callback
, data
, &vec
->vec
));
836 /* Free a unit address vector. */
839 free_unit_addrs_vector (struct backtrace_state
*state
,
840 struct unit_addrs_vector
*vec
,
841 backtrace_error_callback error_callback
, void *data
)
843 struct unit_addrs
*addrs
;
846 addrs
= (struct unit_addrs
*) vec
->vec
.base
;
847 for (i
= 0; i
< vec
->count
; ++i
)
848 free_abbrevs (state
, &addrs
[i
].u
->abbrevs
, error_callback
, data
);
851 /* Compare unit_addrs for qsort. When ranges are nested, make the
852 smallest one sort last. */
855 unit_addrs_compare (const void *v1
, const void *v2
)
857 const struct unit_addrs
*a1
= (const struct unit_addrs
*) v1
;
858 const struct unit_addrs
*a2
= (const struct unit_addrs
*) v2
;
860 if (a1
->low
< a2
->low
)
862 if (a1
->low
> a2
->low
)
864 if (a1
->high
< a2
->high
)
866 if (a1
->high
> a2
->high
)
868 if (a1
->u
->lineoff
< a2
->u
->lineoff
)
870 if (a1
->u
->lineoff
> a2
->u
->lineoff
)
875 /* Compare a PC against a unit_addrs for bsearch. Note that if there
876 are multiple ranges containing PC, which one will be returned is
877 unpredictable. We compensate for that in dwarf_fileline. */
880 unit_addrs_search (const void *vkey
, const void *ventry
)
882 const uintptr_t *key
= (const uintptr_t *) vkey
;
883 const struct unit_addrs
*entry
= (const struct unit_addrs
*) ventry
;
889 else if (pc
>= entry
->high
)
895 /* Sort the line vector by PC. We want a stable sort here. We know
896 that the pointers are into the same array, so it is safe to compare
900 line_compare (const void *v1
, const void *v2
)
902 const struct line
*ln1
= (const struct line
*) v1
;
903 const struct line
*ln2
= (const struct line
*) v2
;
905 if (ln1
->pc
< ln2
->pc
)
907 else if (ln1
->pc
> ln2
->pc
)
917 /* Find a PC in a line vector. We always allocate an extra entry at
918 the end of the lines vector, so that this routine can safely look
919 at the next entry. Note that when there are multiple mappings for
920 the same PC value, this will return the last one. */
923 line_search (const void *vkey
, const void *ventry
)
925 const uintptr_t *key
= (const uintptr_t *) vkey
;
926 const struct line
*entry
= (const struct line
*) ventry
;
932 else if (pc
>= (entry
+ 1)->pc
)
938 /* Sort the abbrevs by the abbrev code. This function is passed to
939 both qsort and bsearch. */
942 abbrev_compare (const void *v1
, const void *v2
)
944 const struct abbrev
*a1
= (const struct abbrev
*) v1
;
945 const struct abbrev
*a2
= (const struct abbrev
*) v2
;
947 if (a1
->code
< a2
->code
)
949 else if (a1
->code
> a2
->code
)
953 /* This really shouldn't happen. It means there are two
954 different abbrevs with the same code, and that means we don't
955 know which one lookup_abbrev should return. */
960 /* Read the abbreviation table for a compilation unit. Returns 1 on
961 success, 0 on failure. */
964 read_abbrevs (struct backtrace_state
*state
, uint64_t abbrev_offset
,
965 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
966 int is_bigendian
, backtrace_error_callback error_callback
,
967 void *data
, struct abbrevs
*abbrevs
)
969 struct dwarf_buf abbrev_buf
;
970 struct dwarf_buf count_buf
;
973 abbrevs
->num_abbrevs
= 0;
974 abbrevs
->abbrevs
= NULL
;
976 if (abbrev_offset
>= dwarf_abbrev_size
)
978 error_callback (data
, "abbrev offset out of range", 0);
982 abbrev_buf
.name
= ".debug_abbrev";
983 abbrev_buf
.start
= dwarf_abbrev
;
984 abbrev_buf
.buf
= dwarf_abbrev
+ abbrev_offset
;
985 abbrev_buf
.left
= dwarf_abbrev_size
- abbrev_offset
;
986 abbrev_buf
.is_bigendian
= is_bigendian
;
987 abbrev_buf
.error_callback
= error_callback
;
988 abbrev_buf
.data
= data
;
989 abbrev_buf
.reported_underflow
= 0;
991 /* Count the number of abbrevs in this list. */
993 count_buf
= abbrev_buf
;
995 while (read_uleb128 (&count_buf
) != 0)
997 if (count_buf
.reported_underflow
)
1001 read_uleb128 (&count_buf
);
1002 // Skip has_children.
1003 read_byte (&count_buf
);
1005 while (read_uleb128 (&count_buf
) != 0)
1006 read_uleb128 (&count_buf
);
1007 // Skip form of last attribute.
1008 read_uleb128 (&count_buf
);
1011 if (count_buf
.reported_underflow
)
1014 if (num_abbrevs
== 0)
1017 abbrevs
->num_abbrevs
= num_abbrevs
;
1018 abbrevs
->abbrevs
= ((struct abbrev
*)
1019 backtrace_alloc (state
,
1020 num_abbrevs
* sizeof (struct abbrev
),
1021 error_callback
, data
));
1022 if (abbrevs
->abbrevs
== NULL
)
1024 memset (abbrevs
->abbrevs
, 0, num_abbrevs
* sizeof (struct abbrev
));
1034 if (abbrev_buf
.reported_underflow
)
1037 code
= read_uleb128 (&abbrev_buf
);
1042 a
.tag
= (enum dwarf_tag
) read_uleb128 (&abbrev_buf
);
1043 a
.has_children
= read_byte (&abbrev_buf
);
1045 count_buf
= abbrev_buf
;
1047 while (read_uleb128 (&count_buf
) != 0)
1050 read_uleb128 (&count_buf
);
1056 read_uleb128 (&abbrev_buf
);
1057 read_uleb128 (&abbrev_buf
);
1061 attrs
= ((struct attr
*)
1062 backtrace_alloc (state
, num_attrs
* sizeof *attrs
,
1063 error_callback
, data
));
1072 name
= read_uleb128 (&abbrev_buf
);
1073 form
= read_uleb128 (&abbrev_buf
);
1076 attrs
[num_attrs
].name
= (enum dwarf_attribute
) name
;
1077 attrs
[num_attrs
].form
= (enum dwarf_form
) form
;
1082 a
.num_attrs
= num_attrs
;
1085 abbrevs
->abbrevs
[num_abbrevs
] = a
;
1089 qsort (abbrevs
->abbrevs
, abbrevs
->num_abbrevs
, sizeof (struct abbrev
),
1095 free_abbrevs (state
, abbrevs
, error_callback
, data
);
1099 /* Return the abbrev information for an abbrev code. */
1101 static const struct abbrev
*
1102 lookup_abbrev (struct abbrevs
*abbrevs
, uint64_t code
,
1103 backtrace_error_callback error_callback
, void *data
)
1108 /* With GCC, where abbrevs are simply numbered in order, we should
1109 be able to just look up the entry. */
1110 if (code
- 1 < abbrevs
->num_abbrevs
1111 && abbrevs
->abbrevs
[code
- 1].code
== code
)
1112 return &abbrevs
->abbrevs
[code
- 1];
1114 /* Otherwise we have to search. */
1115 memset (&key
, 0, sizeof key
);
1117 p
= bsearch (&key
, abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1118 sizeof (struct abbrev
), abbrev_compare
);
1121 error_callback (data
, "invalid abbreviation code", 0);
1124 return (const struct abbrev
*) p
;
1127 /* Add non-contiguous address ranges for a compilation unit. Returns
1128 1 on success, 0 on failure. */
1131 add_unit_ranges (struct backtrace_state
*state
, struct unit
*u
,
1132 uint64_t ranges
, uint64_t base
, int is_bigendian
,
1133 const unsigned char *dwarf_ranges
, size_t dwarf_ranges_size
,
1134 backtrace_error_callback error_callback
, void *data
,
1135 struct unit_addrs_vector
*addrs
)
1137 struct dwarf_buf ranges_buf
;
1139 if (ranges
>= dwarf_ranges_size
)
1141 error_callback (data
, "ranges offset out of range", 0);
1145 ranges_buf
.name
= ".debug_ranges";
1146 ranges_buf
.start
= dwarf_ranges
;
1147 ranges_buf
.buf
= dwarf_ranges
+ ranges
;
1148 ranges_buf
.left
= dwarf_ranges_size
- ranges
;
1149 ranges_buf
.is_bigendian
= is_bigendian
;
1150 ranges_buf
.error_callback
= error_callback
;
1151 ranges_buf
.data
= data
;
1152 ranges_buf
.reported_underflow
= 0;
1159 if (ranges_buf
.reported_underflow
)
1162 low
= read_address (&ranges_buf
, u
->addrsize
);
1163 high
= read_address (&ranges_buf
, u
->addrsize
);
1165 if (low
== 0 && high
== 0)
1168 if (is_highest_address (low
, u
->addrsize
))
1172 struct unit_addrs a
;
1175 a
.high
= high
+ base
;
1177 if (!add_unit_addr (state
, a
, error_callback
, data
, addrs
))
1182 if (ranges_buf
.reported_underflow
)
1188 /* Build a mapping from address ranges to the compilation units where
1189 the line number information for that range can be found. Returns 1
1190 on success, 0 on failure. */
1193 build_address_map (struct backtrace_state
*state
,
1194 const unsigned char *dwarf_info
, size_t dwarf_info_size
,
1195 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1196 const unsigned char *dwarf_ranges
, size_t dwarf_ranges_size
,
1197 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
1198 int is_bigendian
, backtrace_error_callback error_callback
,
1199 void *data
, struct unit_addrs_vector
*addrs
)
1201 struct dwarf_buf info
;
1202 struct abbrevs abbrevs
;
1204 memset (&addrs
->vec
, 0, sizeof addrs
->vec
);
1207 /* Read through the .debug_info section. FIXME: Should we use the
1208 .debug_aranges section? gdb and addr2line don't use it, but I'm
1211 info
.name
= ".debug_info";
1212 info
.start
= dwarf_info
;
1213 info
.buf
= dwarf_info
;
1214 info
.left
= dwarf_info_size
;
1215 info
.is_bigendian
= is_bigendian
;
1216 info
.error_callback
= error_callback
;
1218 info
.reported_underflow
= 0;
1220 memset (&abbrevs
, 0, sizeof abbrevs
);
1221 while (info
.left
> 0)
1223 const unsigned char *unit_data_start
;
1226 struct dwarf_buf unit_buf
;
1228 uint64_t abbrev_offset
;
1229 const struct abbrev
*abbrev
;
1231 const unsigned char *unit_data
;
1232 size_t unit_data_len
;
1233 size_t unit_data_offset
;
1240 int highpc_is_relative
;
1245 const char *comp_dir
;
1247 if (info
.reported_underflow
)
1250 unit_data_start
= info
.buf
;
1253 len
= read_uint32 (&info
);
1254 if (len
== 0xffffffff)
1256 len
= read_uint64 (&info
);
1261 unit_buf
.start
= info
.buf
;
1262 unit_buf
.left
= len
;
1264 if (!advance (&info
, len
))
1267 version
= read_uint16 (&unit_buf
);
1268 if (version
< 2 || version
> 4)
1270 dwarf_buf_error (&unit_buf
, "unrecognized DWARF version");
1274 abbrev_offset
= read_offset (&unit_buf
, is_dwarf64
);
1275 if (!read_abbrevs (state
, abbrev_offset
, dwarf_abbrev
, dwarf_abbrev_size
,
1276 is_bigendian
, error_callback
, data
, &abbrevs
))
1279 addrsize
= read_byte (&unit_buf
);
1281 unit_data
= unit_buf
.buf
;
1282 unit_data_len
= unit_buf
.left
;
1283 unit_data_offset
= unit_buf
.buf
- unit_data_start
;
1285 /* We only look at the first attribute in the compilation unit.
1286 In practice this will be a DW_TAG_compile_unit which will
1287 tell us the PC range and where to find the line number
1290 code
= read_uleb128 (&unit_buf
);
1291 abbrev
= lookup_abbrev (&abbrevs
, code
, error_callback
, data
);
1299 highpc_is_relative
= 0;
1305 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1307 struct attr_val val
;
1309 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
, is_dwarf64
,
1310 version
, addrsize
, dwarf_str
, dwarf_str_size
,
1314 switch (abbrev
->attrs
[i
].name
)
1317 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1324 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1326 highpc
= val
.u
.uint
;
1329 else if (val
.encoding
== ATTR_VAL_UINT
)
1331 highpc
= val
.u
.uint
;
1333 highpc_is_relative
= 1;
1337 if (val
.encoding
== ATTR_VAL_UINT
1338 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1340 ranges
= val
.u
.uint
;
1344 case DW_AT_stmt_list
:
1345 if (val
.encoding
== ATTR_VAL_UINT
1346 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1348 lineoff
= val
.u
.uint
;
1352 case DW_AT_comp_dir
:
1353 if (val
.encoding
== ATTR_VAL_STRING
)
1354 comp_dir
= val
.u
.string
;
1361 if (unit_buf
.reported_underflow
)
1364 if (((have_lowpc
&& have_highpc
) || have_ranges
) && have_lineoff
)
1367 struct unit_addrs a
;
1369 u
= ((struct unit
*)
1370 backtrace_alloc (state
, sizeof *u
, error_callback
, data
));
1373 u
->unit_data
= unit_data
;
1374 u
->unit_data_len
= unit_data_len
;
1375 u
->unit_data_offset
= unit_data_offset
;
1376 u
->version
= version
;
1377 u
->is_dwarf64
= is_dwarf64
;
1378 u
->addrsize
= addrsize
;
1379 u
->comp_dir
= comp_dir
;
1380 u
->lineoff
= lineoff
;
1381 u
->abbrevs
= abbrevs
;
1382 memset (&abbrevs
, 0, sizeof abbrevs
);
1384 /* The actual line number mappings will be read as
1388 u
->function_addrs
= NULL
;
1389 u
->function_addrs_count
= 0;
1393 if (!add_unit_ranges (state
, u
, ranges
, lowpc
, is_bigendian
,
1394 dwarf_ranges
, dwarf_ranges_size
,
1395 error_callback
, data
, addrs
))
1397 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1398 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1404 if (highpc_is_relative
)
1410 if (!add_unit_addr (state
, a
, error_callback
, data
, addrs
))
1412 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1413 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1420 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1421 memset (&abbrevs
, 0, sizeof abbrevs
);
1424 if (info
.reported_underflow
)
1430 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1431 free_unit_addrs_vector (state
, addrs
, error_callback
, data
);
1435 /* Add a new mapping to the vector of line mappings that we are
1436 building. Returns 1 on success, 0 on failure. */
1439 add_line (struct backtrace_state
*state
, uintptr_t pc
, const char *filename
,
1440 int lineno
, backtrace_error_callback error_callback
, void *data
,
1441 struct line_vector
*vec
)
1445 /* If we are adding the same mapping, ignore it. This can happen
1446 when using discriminators. */
1449 ln
= (struct line
*) vec
->vec
.base
+ (vec
->count
- 1);
1450 if (pc
== ln
->pc
&& filename
== ln
->filename
&& lineno
== ln
->lineno
)
1454 ln
= ((struct line
*)
1455 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1461 ln
->filename
= filename
;
1462 ln
->lineno
= lineno
;
1469 /* Free the line header information. If FREE_FILENAMES is true we
1470 free the file names themselves, otherwise we leave them, as there
1471 may be line structures pointing to them. */
1474 free_line_header (struct backtrace_state
*state
, struct line_header
*hdr
,
1475 backtrace_error_callback error_callback
, void *data
)
1477 backtrace_free (state
, hdr
->dirs
, hdr
->dirs_count
* sizeof (const char *),
1478 error_callback
, data
);
1479 backtrace_free (state
, hdr
->filenames
,
1480 hdr
->filenames_count
* sizeof (char *),
1481 error_callback
, data
);
1484 /* Read the line header. Return 1 on success, 0 on failure. */
1487 read_line_header (struct backtrace_state
*state
, struct unit
*u
,
1488 int is_dwarf64
, struct dwarf_buf
*line_buf
,
1489 struct line_header
*hdr
)
1492 struct dwarf_buf hdr_buf
;
1493 const unsigned char *p
;
1494 const unsigned char *pend
;
1497 hdr
->version
= read_uint16 (line_buf
);
1498 if (hdr
->version
< 2 || hdr
->version
> 4)
1500 dwarf_buf_error (line_buf
, "unsupported line number version");
1504 hdrlen
= read_offset (line_buf
, is_dwarf64
);
1506 hdr_buf
= *line_buf
;
1507 hdr_buf
.left
= hdrlen
;
1509 if (!advance (line_buf
, hdrlen
))
1512 hdr
->min_insn_len
= read_byte (&hdr_buf
);
1513 if (hdr
->version
< 4)
1514 hdr
->max_ops_per_insn
= 1;
1516 hdr
->max_ops_per_insn
= read_byte (&hdr_buf
);
1518 /* We don't care about default_is_stmt. */
1519 read_byte (&hdr_buf
);
1521 hdr
->line_base
= read_sbyte (&hdr_buf
);
1522 hdr
->line_range
= read_byte (&hdr_buf
);
1524 hdr
->opcode_base
= read_byte (&hdr_buf
);
1525 hdr
->opcode_lengths
= hdr_buf
.buf
;
1526 if (!advance (&hdr_buf
, hdr
->opcode_base
- 1))
1529 /* Count the number of directory entries. */
1530 hdr
->dirs_count
= 0;
1532 pend
= p
+ hdr_buf
.left
;
1533 while (p
< pend
&& *p
!= '\0')
1535 p
+= strnlen((const char *) p
, pend
- p
) + 1;
1539 hdr
->dirs
= ((const char **)
1540 backtrace_alloc (state
,
1541 hdr
->dirs_count
* sizeof (const char *),
1542 line_buf
->error_callback
, line_buf
->data
));
1543 if (hdr
->dirs
== NULL
)
1547 while (*hdr_buf
.buf
!= '\0')
1549 if (hdr_buf
.reported_underflow
)
1552 hdr
->dirs
[i
] = (const char *) hdr_buf
.buf
;
1554 if (!advance (&hdr_buf
,
1555 strnlen ((const char *) hdr_buf
.buf
, hdr_buf
.left
) + 1))
1558 if (!advance (&hdr_buf
, 1))
1561 /* Count the number of file entries. */
1562 hdr
->filenames_count
= 0;
1564 pend
= p
+ hdr_buf
.left
;
1565 while (p
< pend
&& *p
!= '\0')
1567 p
+= strnlen ((const char *) p
, pend
- p
) + 1;
1568 p
+= leb128_len (p
);
1569 p
+= leb128_len (p
);
1570 p
+= leb128_len (p
);
1571 ++hdr
->filenames_count
;
1574 hdr
->filenames
= ((const char **)
1575 backtrace_alloc (state
,
1576 hdr
->filenames_count
* sizeof (char *),
1577 line_buf
->error_callback
,
1579 if (hdr
->filenames
== NULL
)
1582 while (*hdr_buf
.buf
!= '\0')
1584 const char *filename
;
1587 if (hdr_buf
.reported_underflow
)
1590 filename
= (const char *) hdr_buf
.buf
;
1591 if (!advance (&hdr_buf
,
1592 strnlen ((const char *) hdr_buf
.buf
, hdr_buf
.left
) + 1))
1594 dir_index
= read_uleb128 (&hdr_buf
);
1595 if (IS_ABSOLUTE_PATH (filename
))
1596 hdr
->filenames
[i
] = filename
;
1601 size_t filename_len
;
1606 else if (dir_index
- 1 < hdr
->dirs_count
)
1607 dir
= hdr
->dirs
[dir_index
- 1];
1610 dwarf_buf_error (line_buf
,
1611 ("invalid directory index in "
1612 "line number program header"));
1615 dir_len
= strlen (dir
);
1616 filename_len
= strlen (filename
);
1618 backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
1619 line_buf
->error_callback
, line_buf
->data
));
1622 memcpy (s
, dir
, dir_len
);
1623 /* FIXME: If we are on a DOS-based file system, and the
1624 directory or the file name use backslashes, then we
1625 should use a backslash here. */
1627 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
1628 hdr
->filenames
[i
] = s
;
1631 /* Ignore the modification time and size. */
1632 read_uleb128 (&hdr_buf
);
1633 read_uleb128 (&hdr_buf
);
1638 if (hdr_buf
.reported_underflow
)
1644 /* Read the line program, adding line mappings to VEC. Return 1 on
1645 success, 0 on failure. */
1648 read_line_program (struct backtrace_state
*state
, struct unit
*u
,
1649 const struct line_header
*hdr
, struct dwarf_buf
*line_buf
,
1650 struct line_vector
*vec
)
1653 unsigned int op_index
;
1654 const char *reset_filename
;
1655 const char *filename
;
1660 if (hdr
->filenames_count
> 0)
1661 reset_filename
= hdr
->filenames
[0];
1663 reset_filename
= "";
1664 filename
= reset_filename
;
1666 while (line_buf
->left
> 0)
1670 op
= read_byte (line_buf
);
1671 if (op
>= hdr
->opcode_base
)
1673 unsigned int advance
;
1675 /* Special opcode. */
1676 op
-= hdr
->opcode_base
;
1677 advance
= op
/ hdr
->line_range
;
1678 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1679 / hdr
->max_ops_per_insn
);
1680 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1681 lineno
+= hdr
->line_base
+ (int) (op
% hdr
->line_range
);
1682 add_line (state
, address
, filename
, lineno
, line_buf
->error_callback
,
1683 line_buf
->data
, vec
);
1685 else if (op
== DW_LNS_extended_op
)
1689 len
= read_uleb128 (line_buf
);
1690 op
= read_byte (line_buf
);
1693 case DW_LNE_end_sequence
:
1694 /* FIXME: Should we mark the high PC here? It seems
1695 that we already have that information from the
1696 compilation unit. */
1699 filename
= reset_filename
;
1702 case DW_LNE_set_address
:
1703 address
= read_address (line_buf
, u
->addrsize
);
1705 case DW_LNE_define_file
:
1708 unsigned int dir_index
;
1710 f
= (const char *) line_buf
->buf
;
1711 if (!advance (line_buf
, strnlen (f
, line_buf
->left
) + 1))
1713 dir_index
= read_uleb128 (line_buf
);
1714 /* Ignore that time and length. */
1715 read_uleb128 (line_buf
);
1716 read_uleb128 (line_buf
);
1717 if (IS_ABSOLUTE_PATH (f
))
1728 else if (dir_index
- 1 < hdr
->dirs_count
)
1729 dir
= hdr
->dirs
[dir_index
- 1];
1732 dwarf_buf_error (line_buf
,
1733 ("invalid directory index "
1734 "in line number program"));
1737 dir_len
= strlen (dir
);
1740 backtrace_alloc (state
, dir_len
+ f_len
+ 2,
1741 line_buf
->error_callback
,
1745 memcpy (p
, dir
, dir_len
);
1746 /* FIXME: If we are on a DOS-based file system,
1747 and the directory or the file name use
1748 backslashes, then we should use a backslash
1751 memcpy (p
+ dir_len
+ 1, f
, f_len
+ 1);
1756 case DW_LNE_set_discriminator
:
1757 /* We don't care about discriminators. */
1758 read_uleb128 (line_buf
);
1761 if (!advance (line_buf
, len
- 1))
1771 add_line (state
, address
, filename
, lineno
,
1772 line_buf
->error_callback
, line_buf
->data
, vec
);
1774 case DW_LNS_advance_pc
:
1778 advance
= read_uleb128 (line_buf
);
1779 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1780 / hdr
->max_ops_per_insn
);
1781 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1784 case DW_LNS_advance_line
:
1785 lineno
+= (int) read_sleb128 (line_buf
);
1787 case DW_LNS_set_file
:
1791 fileno
= read_uleb128 (line_buf
);
1796 if (fileno
- 1 >= hdr
->filenames_count
)
1798 dwarf_buf_error (line_buf
,
1799 ("invalid file number in "
1800 "line number program"));
1803 filename
= hdr
->filenames
[fileno
- 1];
1807 case DW_LNS_set_column
:
1808 read_uleb128 (line_buf
);
1810 case DW_LNS_negate_stmt
:
1812 case DW_LNS_set_basic_block
:
1814 case DW_LNS_const_add_pc
:
1816 unsigned int advance
;
1818 op
= 255 - hdr
->opcode_base
;
1819 advance
= op
/ hdr
->line_range
;
1820 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1821 / hdr
->max_ops_per_insn
);
1822 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1825 case DW_LNS_fixed_advance_pc
:
1826 address
+= read_uint16 (line_buf
);
1829 case DW_LNS_set_prologue_end
:
1831 case DW_LNS_set_epilogue_begin
:
1833 case DW_LNS_set_isa
:
1834 read_uleb128 (line_buf
);
1840 for (i
= hdr
->opcode_lengths
[op
- 1]; i
> 0; --i
)
1841 read_uleb128 (line_buf
);
1851 /* Read the line number information for a compilation unit. Returns 1
1852 on success, 0 on failure. */
1855 read_line_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1856 backtrace_error_callback error_callback
, void *data
,
1857 struct unit
*u
, struct line_header
*hdr
, struct line
**lines
,
1858 size_t *lines_count
)
1860 struct line_vector vec
;
1861 struct dwarf_buf line_buf
;
1866 memset (&vec
.vec
, 0, sizeof vec
.vec
);
1869 memset (hdr
, 0, sizeof *hdr
);
1871 if (u
->lineoff
!= (off_t
) (size_t) u
->lineoff
1872 || (size_t) u
->lineoff
>= ddata
->dwarf_line_size
)
1874 error_callback (data
, "unit line offset out of range", 0);
1878 line_buf
.name
= ".debug_line";
1879 line_buf
.start
= ddata
->dwarf_line
;
1880 line_buf
.buf
= ddata
->dwarf_line
+ u
->lineoff
;
1881 line_buf
.left
= ddata
->dwarf_line_size
- u
->lineoff
;
1882 line_buf
.is_bigendian
= ddata
->is_bigendian
;
1883 line_buf
.error_callback
= error_callback
;
1884 line_buf
.data
= data
;
1885 line_buf
.reported_underflow
= 0;
1888 len
= read_uint32 (&line_buf
);
1889 if (len
== 0xffffffff)
1891 len
= read_uint64 (&line_buf
);
1894 line_buf
.left
= len
;
1896 if (!read_line_header (state
, u
, is_dwarf64
, &line_buf
, hdr
))
1899 if (!read_line_program (state
, u
, hdr
, &line_buf
, &vec
))
1902 if (line_buf
.reported_underflow
)
1907 /* This is not a failure in the sense of a generating an error,
1908 but it is a failure in that sense that we have no useful
1913 /* Allocate one extra entry at the end. */
1914 ln
= ((struct line
*)
1915 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1919 ln
->pc
= (uintptr_t) -1;
1920 ln
->filename
= NULL
;
1923 if (!backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
))
1926 ln
= (struct line
*) vec
.vec
.base
;
1927 qsort (ln
, vec
.count
, sizeof (struct line
), line_compare
);
1930 *lines_count
= vec
.count
;
1935 vec
.vec
.alc
+= vec
.vec
.size
;
1937 backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
);
1938 free_line_header (state
, hdr
, error_callback
, data
);
1939 *lines
= (struct line
*) (uintptr_t) -1;
1944 /* Read the name of a function from a DIE referenced by a
1945 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
1946 the same compilation unit. */
1949 read_referenced_name (struct dwarf_data
*ddata
, struct unit
*u
,
1950 uint64_t offset
, backtrace_error_callback error_callback
,
1953 struct dwarf_buf unit_buf
;
1955 const struct abbrev
*abbrev
;
1959 /* OFFSET is from the start of the data for this compilation unit.
1960 U->unit_data is the data, but it starts U->unit_data_offset bytes
1961 from the beginning. */
1963 if (offset
< u
->unit_data_offset
1964 || offset
- u
->unit_data_offset
>= u
->unit_data_len
)
1966 error_callback (data
,
1967 "abstract origin or specification out of range",
1972 offset
-= u
->unit_data_offset
;
1974 unit_buf
.name
= ".debug_info";
1975 unit_buf
.start
= ddata
->dwarf_info
;
1976 unit_buf
.buf
= u
->unit_data
+ offset
;
1977 unit_buf
.left
= u
->unit_data_len
- offset
;
1978 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
1979 unit_buf
.error_callback
= error_callback
;
1980 unit_buf
.data
= data
;
1981 unit_buf
.reported_underflow
= 0;
1983 code
= read_uleb128 (&unit_buf
);
1986 dwarf_buf_error (&unit_buf
, "invalid abstract origin or specification");
1990 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
1995 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1997 struct attr_val val
;
1999 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
,
2000 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2001 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2005 switch (abbrev
->attrs
[i
].name
)
2008 /* We prefer the linkage name if get one. */
2009 if (val
.encoding
== ATTR_VAL_STRING
)
2013 case DW_AT_linkage_name
:
2014 case DW_AT_MIPS_linkage_name
:
2015 if (val
.encoding
== ATTR_VAL_STRING
)
2016 return val
.u
.string
;
2019 case DW_AT_specification
:
2020 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2021 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2023 /* This refers to a specification defined in some other
2024 compilation unit. We can handle this case if we
2025 must, but it's harder. */
2028 if (val
.encoding
== ATTR_VAL_UINT
2029 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2033 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2034 error_callback
, data
);
2048 /* Add a single range to U that maps to function. Returns 1 on
2049 success, 0 on error. */
2052 add_function_range (struct backtrace_state
*state
, struct function
*function
,
2053 uint64_t lowpc
, uint64_t highpc
,
2054 backtrace_error_callback error_callback
,
2055 void *data
, struct function_vector
*vec
)
2057 struct function_addrs
*p
;
2061 p
= (struct function_addrs
*) vec
->vec
.base
+ vec
->count
- 1;
2062 if ((lowpc
== p
->high
|| lowpc
== p
->high
+ 1)
2063 && function
== p
->function
)
2065 if (highpc
> p
->high
)
2071 p
= ((struct function_addrs
*)
2072 backtrace_vector_grow (state
, sizeof (struct function_addrs
),
2073 error_callback
, data
, &vec
->vec
));
2079 p
->function
= function
;
2084 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2088 add_function_ranges (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2089 struct unit
*u
, struct function
*function
,
2090 uint64_t ranges
, uint64_t base
,
2091 backtrace_error_callback error_callback
, void *data
,
2092 struct function_vector
*vec
)
2094 struct dwarf_buf ranges_buf
;
2096 if (ranges
>= ddata
->dwarf_ranges_size
)
2098 error_callback (data
, "function ranges offset out of range", 0);
2102 ranges_buf
.name
= ".debug_ranges";
2103 ranges_buf
.start
= ddata
->dwarf_ranges
;
2104 ranges_buf
.buf
= ddata
->dwarf_ranges
+ ranges
;
2105 ranges_buf
.left
= ddata
->dwarf_ranges_size
- ranges
;
2106 ranges_buf
.is_bigendian
= ddata
->is_bigendian
;
2107 ranges_buf
.error_callback
= error_callback
;
2108 ranges_buf
.data
= data
;
2109 ranges_buf
.reported_underflow
= 0;
2116 if (ranges_buf
.reported_underflow
)
2119 low
= read_address (&ranges_buf
, u
->addrsize
);
2120 high
= read_address (&ranges_buf
, u
->addrsize
);
2122 if (low
== 0 && high
== 0)
2125 if (is_highest_address (low
, u
->addrsize
))
2129 if (!add_function_range (state
, function
, low
+ base
, high
+ base
,
2130 error_callback
, data
, vec
))
2135 if (ranges_buf
.reported_underflow
)
2141 /* Read one entry plus all its children. Add function addresses to
2142 VEC. Returns 1 on success, 0 on error. */
2145 read_function_entry (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2146 struct unit
*u
, uint64_t base
, struct dwarf_buf
*unit_buf
,
2147 const struct line_header
*lhdr
,
2148 backtrace_error_callback error_callback
, void *data
,
2149 struct function_vector
*vec
)
2151 while (unit_buf
->left
> 0)
2154 const struct abbrev
*abbrev
;
2156 struct function
*function
;
2162 int highpc_is_relative
;
2166 code
= read_uleb128 (unit_buf
);
2170 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2174 is_function
= (abbrev
->tag
== DW_TAG_subprogram
2175 || abbrev
->tag
== DW_TAG_entry_point
2176 || abbrev
->tag
== DW_TAG_inlined_subroutine
);
2181 function
= ((struct function
*)
2182 backtrace_alloc (state
, sizeof *function
,
2183 error_callback
, data
));
2184 if (function
== NULL
)
2186 memset (function
, 0, sizeof *function
);
2193 highpc_is_relative
= 0;
2196 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2198 struct attr_val val
;
2200 if (!read_attribute (abbrev
->attrs
[i
].form
, unit_buf
,
2201 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2202 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2206 /* The compile unit sets the base address for any address
2207 ranges in the function entries. */
2208 if (abbrev
->tag
== DW_TAG_compile_unit
2209 && abbrev
->attrs
[i
].name
== DW_AT_low_pc
2210 && val
.encoding
== ATTR_VAL_ADDRESS
)
2215 switch (abbrev
->attrs
[i
].name
)
2217 case DW_AT_call_file
:
2218 if (val
.encoding
== ATTR_VAL_UINT
)
2220 if (val
.u
.uint
== 0)
2221 function
->caller_filename
= "";
2224 if (val
.u
.uint
- 1 >= lhdr
->filenames_count
)
2226 dwarf_buf_error (unit_buf
,
2227 ("invalid file number in "
2228 "DW_AT_call_file attribute"));
2231 function
->caller_filename
=
2232 lhdr
->filenames
[val
.u
.uint
- 1];
2237 case DW_AT_call_line
:
2238 if (val
.encoding
== ATTR_VAL_UINT
)
2239 function
->caller_lineno
= val
.u
.uint
;
2242 case DW_AT_abstract_origin
:
2243 case DW_AT_specification
:
2244 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2245 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2247 /* This refers to an abstract origin defined in
2248 some other compilation unit. We can handle
2249 this case if we must, but it's harder. */
2252 if (val
.encoding
== ATTR_VAL_UINT
2253 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2257 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2258 error_callback
, data
);
2260 function
->name
= name
;
2265 if (val
.encoding
== ATTR_VAL_STRING
)
2267 /* Don't override a name we found in some other
2268 way, as it will normally be more
2269 useful--e.g., this name is normally not
2271 if (function
->name
== NULL
)
2272 function
->name
= val
.u
.string
;
2276 case DW_AT_linkage_name
:
2277 case DW_AT_MIPS_linkage_name
:
2278 if (val
.encoding
== ATTR_VAL_STRING
)
2279 function
->name
= val
.u
.string
;
2283 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2291 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2293 highpc
= val
.u
.uint
;
2296 else if (val
.encoding
== ATTR_VAL_UINT
)
2298 highpc
= val
.u
.uint
;
2300 highpc_is_relative
= 1;
2305 if (val
.encoding
== ATTR_VAL_UINT
2306 || val
.encoding
== ATTR_VAL_REF_SECTION
)
2308 ranges
= val
.u
.uint
;
2319 /* If we couldn't find a name for the function, we have no use
2321 if (is_function
&& function
->name
== NULL
)
2323 backtrace_free (state
, function
, sizeof *function
,
2324 error_callback
, data
);
2332 if (!add_function_ranges (state
, ddata
, u
, function
, ranges
,
2333 base
, error_callback
, data
, vec
))
2336 else if (have_lowpc
&& have_highpc
)
2338 if (highpc_is_relative
)
2340 if (!add_function_range (state
, function
, lowpc
, highpc
,
2341 error_callback
, data
, vec
))
2346 backtrace_free (state
, function
, sizeof *function
,
2347 error_callback
, data
);
2352 if (abbrev
->has_children
)
2356 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2357 error_callback
, data
, vec
))
2362 struct function_vector fvec
;
2364 /* Gather any information for inlined functions in
2367 memset (&fvec
, 0, sizeof fvec
);
2369 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2370 error_callback
, data
, &fvec
))
2375 struct function_addrs
*faddrs
;
2377 if (!backtrace_vector_release (state
, &fvec
.vec
,
2378 error_callback
, data
))
2381 faddrs
= (struct function_addrs
*) fvec
.vec
.base
;
2382 qsort (faddrs
, fvec
.count
,
2383 sizeof (struct function_addrs
),
2384 function_addrs_compare
);
2386 function
->function_addrs
= faddrs
;
2387 function
->function_addrs_count
= fvec
.count
;
2396 /* Read function name information for a compilation unit. We look
2397 through the whole unit looking for function tags. */
2400 read_function_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2401 const struct line_header
*lhdr
,
2402 backtrace_error_callback error_callback
, void *data
,
2403 struct unit
*u
, struct function_vector
*fvec
,
2404 struct function_addrs
**ret_addrs
,
2405 size_t *ret_addrs_count
)
2407 struct dwarf_buf unit_buf
;
2408 struct function_addrs
*addrs
;
2411 unit_buf
.name
= ".debug_info";
2412 unit_buf
.start
= ddata
->dwarf_info
;
2413 unit_buf
.buf
= u
->unit_data
;
2414 unit_buf
.left
= u
->unit_data_len
;
2415 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2416 unit_buf
.error_callback
= error_callback
;
2417 unit_buf
.data
= data
;
2418 unit_buf
.reported_underflow
= 0;
2420 while (unit_buf
.left
> 0)
2422 if (!read_function_entry (state
, ddata
, u
, 0, &unit_buf
, lhdr
,
2423 error_callback
, data
, fvec
))
2427 if (fvec
->count
== 0)
2430 addrs
= (struct function_addrs
*) fvec
->vec
.base
;
2431 addrs_count
= fvec
->count
;
2433 /* Finish this list of addresses, but leave the remaining space in
2434 the vector available for the next function unit. */
2435 backtrace_vector_finish (state
, &fvec
->vec
);
2438 qsort (addrs
, addrs_count
, sizeof (struct function_addrs
),
2439 function_addrs_compare
);
2442 *ret_addrs_count
= addrs_count
;
2445 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2446 information, and update FILENAME and LINENO for the caller.
2447 Returns whatever CALLBACK returns, or 0 to keep going. */
2450 report_inlined_functions (uintptr_t pc
, struct function
*function
,
2451 backtrace_full_callback callback
, void *data
,
2452 const char **filename
, int *lineno
)
2454 struct function_addrs
*function_addrs
;
2455 struct function
*inlined
;
2458 if (function
->function_addrs_count
== 0)
2461 function_addrs
= ((struct function_addrs
*)
2462 bsearch (&pc
, function
->function_addrs
,
2463 function
->function_addrs_count
,
2464 sizeof (struct function_addrs
),
2465 function_addrs_search
));
2466 if (function_addrs
== NULL
)
2469 while (((size_t) (function_addrs
- function
->function_addrs
) + 1
2470 < function
->function_addrs_count
)
2471 && pc
>= (function_addrs
+ 1)->low
2472 && pc
< (function_addrs
+ 1)->high
)
2475 /* We found an inlined call. */
2477 inlined
= function_addrs
->function
;
2479 /* Report any calls inlined into this one. */
2480 ret
= report_inlined_functions (pc
, inlined
, callback
, data
,
2485 /* Report this inlined call. */
2486 ret
= callback (data
, pc
, *filename
, *lineno
, inlined
->name
);
2490 /* Our caller will report the caller of the inlined function; tell
2491 it the appropriate filename and line number. */
2492 *filename
= inlined
->caller_filename
;
2493 *lineno
= inlined
->caller_lineno
;
2498 /* Return the file/line information for a PC using the DWARF mapping
2499 we built earlier. */
2502 dwarf_fileline (struct backtrace_state
*state
, uintptr_t pc
,
2503 backtrace_full_callback callback
,
2504 backtrace_error_callback error_callback
, void *data
)
2506 struct dwarf_data
*ddata
;
2507 struct unit_addrs
*entry
;
2512 struct function_addrs
*function_addrs
;
2513 struct function
*function
;
2514 const char *filename
;
2518 ddata
= (struct dwarf_data
*) state
->fileline_data
;
2520 /* Find an address range that includes PC. */
2521 entry
= bsearch (&pc
, ddata
->addrs
, ddata
->addrs_count
,
2522 sizeof (struct unit_addrs
), unit_addrs_search
);
2525 return callback (data
, pc
, NULL
, 0, NULL
);
2527 /* If there are multiple ranges that contain PC, use the last one,
2528 in order to produce predictable results. If we assume that all
2529 ranges are properly nested, then the last range will be the
2531 while ((size_t) (entry
- ddata
->addrs
) + 1 < ddata
->addrs_count
2532 && pc
>= (entry
+ 1)->low
2533 && pc
< (entry
+ 1)->high
)
2536 /* We need the lines, lines_count, function_addrs,
2537 function_addrs_count fields of u. If they are not set, we need
2538 to set them. When running in threaded mode, we need to allow for
2539 the possibility that some other thread is setting them
2545 /* Skip units with no useful line number information by walking
2546 backward. Useless line number information is marked by setting
2548 while (entry
> ddata
->addrs
2549 && pc
>= (entry
- 1)->low
2550 && pc
< (entry
- 1)->high
)
2552 if (state
->threaded
)
2554 /* Use __sync_bool_compare_and_swap to do a
2556 while (!__sync_bool_compare_and_swap (&u
->lines
, lines
, lines
))
2560 if (lines
!= (struct line
*) (uintptr_t) -1)
2569 /* Do a load-acquire of u->lines. */
2570 if (state
->threaded
)
2572 /* Use __sync_bool_compare_and_swap to do an atomic load. */
2573 while (!__sync_bool_compare_and_swap (&u
->lines
, lines
, lines
))
2580 size_t function_addrs_count
;
2581 struct line_header lhdr
;
2584 /* We have never read the line information for this unit. Read
2587 function_addrs
= NULL
;
2588 function_addrs_count
= 0;
2589 if (read_line_info (state
, ddata
, error_callback
, data
, entry
->u
, &lhdr
,
2592 read_function_info (state
, ddata
, &lhdr
, error_callback
, data
,
2593 entry
->u
, &ddata
->fvec
, &function_addrs
,
2594 &function_addrs_count
);
2595 free_line_header (state
, &lhdr
, error_callback
, data
);
2599 /* Atomically store the information we just read into the unit.
2600 If another thread is simultaneously writing, it presumably
2601 read the same information, and we don't care which one we
2602 wind up with; we just leak the other one. We do have to
2603 write the lines field last, so that the acquire-loads above
2604 ensure that the other fields are set. */
2606 if (!state
->threaded
)
2608 u
->lines_count
= count
;
2609 u
->function_addrs
= function_addrs
;
2610 u
->function_addrs_count
= function_addrs_count
;
2615 __sync_bool_compare_and_swap (&u
->lines_count
, 0, count
);
2616 __sync_bool_compare_and_swap (&u
->function_addrs
, NULL
,
2618 __sync_bool_compare_and_swap (&u
->function_addrs_count
, 0,
2619 function_addrs_count
);
2620 __sync_bool_compare_and_swap (&u
->lines
, NULL
, lines
);
2624 /* Now all fields of U have been initialized. */
2626 if (lines
== (struct line
*) (uintptr_t) -1)
2628 /* If reading the line number information failed in some way,
2629 try again to see if there is a better compilation unit for
2632 dwarf_fileline (state
, pc
, callback
, error_callback
, data
);
2633 return callback (data
, pc
, NULL
, 0, NULL
);
2636 /* Search for PC within this unit. */
2638 ln
= (struct line
*) bsearch (&pc
, lines
, entry
->u
->lines_count
,
2639 sizeof (struct line
), line_search
);
2642 error_callback (data
, "inconsistent DWARF line number info", 0);
2646 /* Search for function name within this unit. */
2648 if (entry
->u
->function_addrs_count
== 0)
2649 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2651 function_addrs
= ((struct function_addrs
*)
2652 bsearch (&pc
, entry
->u
->function_addrs
,
2653 entry
->u
->function_addrs_count
,
2654 sizeof (struct function_addrs
),
2655 function_addrs_search
));
2656 if (function_addrs
== NULL
)
2657 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2659 /* If there are multiple function ranges that contain PC, use the
2660 last one, in order to produce predictable results. */
2662 while (((size_t) (function_addrs
- entry
->u
->function_addrs
+ 1)
2663 < entry
->u
->function_addrs_count
)
2664 && pc
>= (function_addrs
+ 1)->low
2665 && pc
< (function_addrs
+ 1)->high
)
2668 function
= function_addrs
->function
;
2670 filename
= ln
->filename
;
2671 lineno
= ln
->lineno
;
2673 ret
= report_inlined_functions (pc
, function
, callback
, data
,
2674 &filename
, &lineno
);
2678 return callback (data
, pc
, filename
, lineno
, function
->name
);
2681 /* Build our data structures from the .debug_info and .debug_line
2682 sections. Set *FILELINE_FN and *FILELINE_DATA. Return 1 on
2683 success, 0 on failure. */
2686 backtrace_dwarf_initialize (struct backtrace_state
*state
,
2687 const unsigned char *dwarf_info
,
2688 size_t dwarf_info_size
,
2689 const unsigned char *dwarf_line
,
2690 size_t dwarf_line_size
,
2691 const unsigned char *dwarf_abbrev
,
2692 size_t dwarf_abbrev_size
,
2693 const unsigned char *dwarf_ranges
,
2694 size_t dwarf_ranges_size
,
2695 const unsigned char *dwarf_str
,
2696 size_t dwarf_str_size
,
2698 backtrace_error_callback error_callback
,
2699 void *data
, fileline
*fileline_fn
)
2701 struct unit_addrs_vector addrs_vec
;
2702 struct unit_addrs
*addrs
;
2704 struct dwarf_data
*fdata
;
2706 if (!build_address_map (state
, dwarf_info
, dwarf_info_size
, dwarf_abbrev
,
2707 dwarf_abbrev_size
, dwarf_ranges
, dwarf_ranges_size
,
2708 dwarf_str
, dwarf_str_size
, is_bigendian
,
2709 error_callback
, data
, &addrs_vec
))
2712 if (!backtrace_vector_release (state
, &addrs_vec
.vec
, error_callback
, data
))
2714 addrs
= (struct unit_addrs
*) addrs_vec
.vec
.base
;
2715 addrs_count
= addrs_vec
.count
;
2716 qsort (addrs
, addrs_count
, sizeof (struct unit_addrs
), unit_addrs_compare
);
2718 fdata
= ((struct dwarf_data
*)
2719 backtrace_alloc (state
, sizeof (struct dwarf_data
),
2720 error_callback
, data
));
2724 fdata
->addrs
= addrs
;
2725 fdata
->addrs_count
= addrs_count
;
2726 fdata
->dwarf_info
= dwarf_info
;
2727 fdata
->dwarf_info_size
= dwarf_info_size
;
2728 fdata
->dwarf_line
= dwarf_line
;
2729 fdata
->dwarf_line_size
= dwarf_line_size
;
2730 fdata
->dwarf_ranges
= dwarf_ranges
;
2731 fdata
->dwarf_ranges_size
= dwarf_ranges_size
;
2732 fdata
->dwarf_str
= dwarf_str
;
2733 fdata
->dwarf_str_size
= dwarf_str_size
;
2734 fdata
->is_bigendian
= is_bigendian
;
2735 memset (&fdata
->fvec
, 0, sizeof fdata
->fvec
);
2737 state
->fileline_data
= fdata
;
2739 *fileline_fn
= dwarf_fileline
;