1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
38 #include <sys/types.h>
41 #include "filenames.h"
43 #include "backtrace.h"
46 #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
47 /* The function is defined in libiberty if needed. */
48 extern size_t strnlen (const char *, size_t);
51 /* A buffer to read DWARF info. */
55 /* Buffer name for error messages. */
57 /* Start of the buffer. */
58 const unsigned char *start
;
59 /* Next byte to read. */
60 const unsigned char *buf
;
61 /* The number of bytes remaining. */
63 /* Whether the data is big-endian. */
65 /* Error callback routine. */
66 backtrace_error_callback error_callback
;
67 /* Data for error_callback. */
69 /* Non-zero if we've reported an underflow error. */
70 int reported_underflow
;
73 /* A single attribute in a DWARF abbreviation. */
77 /* The attribute name. */
78 enum dwarf_attribute name
;
79 /* The attribute form. */
83 /* A single DWARF abbreviation. */
87 /* The abbrev code--the number used to refer to the abbrev. */
91 /* Non-zero if this abbrev has child entries. */
93 /* The number of attributes. */
99 /* The DWARF abbreviations for a compilation unit. This structure
100 only exists while reading the compilation unit. Most DWARF readers
101 seem to a hash table to map abbrev ID's to abbrev entries.
102 However, we primarily care about GCC, and GCC simply issues ID's in
103 numerical order starting at 1. So we simply keep a sorted vector,
104 and try to just look up the code. */
108 /* The number of abbrevs in the vector. */
110 /* The abbrevs, sorted by the code field. */
111 struct abbrev
*abbrevs
;
114 /* The different kinds of attribute values. */
116 enum attr_val_encoding
120 /* A unsigned integer. */
122 /* A sigd integer. */
126 /* An offset to other data in the containing unit. */
128 /* An offset to other data within the .dwarf_info section. */
130 /* An offset to data in some other section. */
131 ATTR_VAL_REF_SECTION
,
132 /* A type signature. */
134 /* A block of data (not represented). */
136 /* An expression (not represented). */
140 /* An attribute value. */
144 /* How the value is stored in the field u. */
145 enum attr_val_encoding encoding
;
148 /* ATTR_VAL_ADDRESS, ATTR_VAL_UINT, ATTR_VAL_REF*. */
152 /* ATTR_VAL_STRING. */
154 /* ATTR_VAL_BLOCK not stored. */
158 /* The line number program header. */
162 /* The version of the line number information. */
164 /* The minimum instruction length. */
165 unsigned int min_insn_len
;
166 /* The maximum number of ops per instruction. */
167 unsigned int max_ops_per_insn
;
168 /* The line base for special opcodes. */
170 /* The line range for special opcodes. */
171 unsigned int line_range
;
172 /* The opcode base--the first special opcode. */
173 unsigned int opcode_base
;
174 /* Opcode lengths, indexed by opcode - 1. */
175 const unsigned char *opcode_lengths
;
176 /* The number of directory entries. */
178 /* The directory entries. */
180 /* The number of filenames. */
181 size_t filenames_count
;
183 const char **filenames
;
186 /* Map a single PC value to a file/line. We will keep a vector of
187 these sorted by PC value. Each file/line will be correct from the
188 PC up to the PC of the next entry if there is one. We allocate one
189 extra entry at the end so that we can use bsearch. */
195 /* File name. Many entries in the array are expected to point to
196 the same file name. */
197 const char *filename
;
202 /* A growable vector of line number information. This is used while
203 reading the line numbers. */
207 /* Memory. This is an array of struct line. */
208 struct backtrace_vector vec
;
209 /* Number of valid mappings. */
213 /* A function described in the debug info. */
217 /* The name of the function. */
219 /* If this is an inlined function, the filename of the call
221 const char *caller_filename
;
222 /* If this is an inlined function, the line number of the call
225 /* Map PC ranges to inlined functions. */
226 struct function_addrs
*function_addrs
;
227 size_t function_addrs_count
;
230 /* An address range for a function. This maps a PC value to a
231 specific function. */
233 struct function_addrs
235 /* Range is LOW <= PC < HIGH. */
238 /* Function for this address range. */
239 struct function
*function
;
242 /* A growable vector of function address ranges. */
244 struct function_vector
246 /* Memory. This is an array of struct function_addrs. */
247 struct backtrace_vector vec
;
248 /* Number of address ranges present. */
252 /* A DWARF compilation unit. This only holds the information we need
253 to map a PC to a file and line. */
257 /* The first entry for this compilation unit. */
258 const unsigned char *unit_data
;
259 /* The length of the data for this compilation unit. */
260 size_t unit_data_len
;
261 /* The offset of UNIT_DATA from the start of the information for
262 this compilation unit. */
263 size_t unit_data_offset
;
266 /* Whether unit is DWARF64. */
270 /* Offset into line number information. */
272 /* Compilation command working directory. */
273 const char *comp_dir
;
274 /* The abbreviations for this unit. */
275 struct abbrevs abbrevs
;
277 /* The fields above this point are read in during initialization and
278 may be accessed freely. The fields below this point are read in
279 as needed, and therefore require care, as different threads may
280 try to initialize them simultaneously. */
282 /* PC to line number mapping. This is NULL if the values have not
283 been read. This is (struct line *) -1 if there was an error
284 reading the values. */
286 /* Number of entries in lines. */
288 /* PC ranges to function. */
289 struct function_addrs
*function_addrs
;
290 size_t function_addrs_count
;
293 /* An address range for a compilation unit. This maps a PC value to a
294 specific compilation unit. Note that we invert the representation
295 in DWARF: instead of listing the units and attaching a list of
296 ranges, we list the ranges and have each one point to the unit.
297 This lets us do a binary search to find the unit. */
301 /* Range is LOW <= PC < HIGH. */
304 /* Compilation unit for this address range. */
308 /* A growable vector of compilation unit address ranges. */
310 struct unit_addrs_vector
312 /* Memory. This is an array of struct unit_addrs. */
313 struct backtrace_vector vec
;
314 /* Number of address ranges present. */
318 /* The information we need to map a PC to a file and line. */
322 /* A sorted list of address ranges. */
323 struct unit_addrs
*addrs
;
324 /* Number of address ranges in list. */
326 /* The unparsed .debug_info section. */
327 const unsigned char *dwarf_info
;
328 size_t dwarf_info_size
;
329 /* The unparsed .debug_line section. */
330 const unsigned char *dwarf_line
;
331 size_t dwarf_line_size
;
332 /* The unparsed .debug_ranges section. */
333 const unsigned char *dwarf_ranges
;
334 size_t dwarf_ranges_size
;
335 /* The unparsed .debug_str section. */
336 const unsigned char *dwarf_str
;
337 size_t dwarf_str_size
;
338 /* Whether the data is big-endian or not. */
340 /* A vector used for function addresses. We keep this here so that
341 we can grow the vector as we read more functions. */
342 struct function_vector fvec
;
345 /* Report an error for a DWARF buffer. */
348 dwarf_buf_error (struct dwarf_buf
*buf
, const char *msg
)
352 snprintf (b
, sizeof b
, "%s in %s at %d",
353 msg
, buf
->name
, (int) (buf
->buf
- buf
->start
));
354 buf
->error_callback (buf
->data
, b
, 0);
357 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
361 require (struct dwarf_buf
*buf
, size_t count
)
363 if (buf
->left
>= count
)
366 if (!buf
->reported_underflow
)
368 dwarf_buf_error (buf
, "DWARF underflow");
369 buf
->reported_underflow
= 1;
375 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
379 advance (struct dwarf_buf
*buf
, size_t count
)
381 if (!require (buf
, count
))
388 /* Read one byte from BUF and advance 1 byte. */
391 read_byte (struct dwarf_buf
*buf
)
393 const unsigned char *p
= buf
->buf
;
395 if (!advance (buf
, 1))
400 /* Read a signed char from BUF and advance 1 byte. */
403 read_sbyte (struct dwarf_buf
*buf
)
405 const unsigned char *p
= buf
->buf
;
407 if (!advance (buf
, 1))
409 return (*p
^ 0x80) - 0x80;
412 /* Read a uint16 from BUF and advance 2 bytes. */
415 read_uint16 (struct dwarf_buf
*buf
)
417 const unsigned char *p
= buf
->buf
;
419 if (!advance (buf
, 2))
421 if (buf
->is_bigendian
)
422 return ((uint16_t) p
[0] << 8) | (uint16_t) p
[1];
424 return ((uint16_t) p
[1] << 8) | (uint16_t) p
[0];
427 /* Read a uint32 from BUF and advance 4 bytes. */
430 read_uint32 (struct dwarf_buf
*buf
)
432 const unsigned char *p
= buf
->buf
;
434 if (!advance (buf
, 4))
436 if (buf
->is_bigendian
)
437 return (((uint32_t) p
[0] << 24) | ((uint32_t) p
[1] << 16)
438 | ((uint32_t) p
[2] << 8) | (uint32_t) p
[3]);
440 return (((uint32_t) p
[3] << 24) | ((uint32_t) p
[2] << 16)
441 | ((uint32_t) p
[1] << 8) | (uint32_t) p
[0]);
444 /* Read a uint64 from BUF and advance 8 bytes. */
447 read_uint64 (struct dwarf_buf
*buf
)
449 const unsigned char *p
= buf
->buf
;
451 if (!advance (buf
, 8))
453 if (buf
->is_bigendian
)
454 return (((uint64_t) p
[0] << 56) | ((uint64_t) p
[1] << 48)
455 | ((uint64_t) p
[2] << 40) | ((uint64_t) p
[3] << 32)
456 | ((uint64_t) p
[4] << 24) | ((uint64_t) p
[5] << 16)
457 | ((uint64_t) p
[6] << 8) | (uint64_t) p
[7]);
459 return (((uint64_t) p
[7] << 56) | ((uint64_t) p
[6] << 48)
460 | ((uint64_t) p
[5] << 40) | ((uint64_t) p
[4] << 32)
461 | ((uint64_t) p
[3] << 24) | ((uint64_t) p
[2] << 16)
462 | ((uint64_t) p
[1] << 8) | (uint64_t) p
[0]);
465 /* Read an offset from BUF and advance the appropriate number of
469 read_offset (struct dwarf_buf
*buf
, int is_dwarf64
)
472 return read_uint64 (buf
);
474 return read_uint32 (buf
);
477 /* Read an address from BUF and advance the appropriate number of
481 read_address (struct dwarf_buf
*buf
, int addrsize
)
486 return read_byte (buf
);
488 return read_uint16 (buf
);
490 return read_uint32 (buf
);
492 return read_uint64 (buf
);
494 dwarf_buf_error (buf
, "unrecognized address size");
499 /* Return whether a value is the highest possible address, given the
503 is_highest_address (uint64_t address
, int addrsize
)
508 return address
== (unsigned char) -1;
510 return address
== (uint16_t) -1;
512 return address
== (uint32_t) -1;
514 return address
== (uint64_t) -1;
520 /* Read an unsigned LEB128 number. */
523 read_uleb128 (struct dwarf_buf
*buf
)
533 const unsigned char *p
;
536 if (!advance (buf
, 1))
539 ret
|= ((uint64_t) (b
& 0x7f)) << shift
;
542 while ((b
& 0x80) != 0);
545 dwarf_buf_error (buf
, "LEB128 overflows uint64_5");
550 /* Read a signed LEB128 number. */
553 read_sleb128 (struct dwarf_buf
*buf
)
563 const unsigned char *p
;
566 if (!advance (buf
, 1))
569 val
|= ((uint64_t) (b
& 0x7f)) << shift
;
572 while ((b
& 0x80) != 0);
575 dwarf_buf_error (buf
, "signed LEB128 overflows uint64_t");
578 val
|= ((uint64_t) -1) << shift
;
580 return (int64_t) val
;
583 /* Return the length of an LEB128 number. */
586 leb128_len (const unsigned char *p
)
591 while ((*p
& 0x80) != 0)
599 /* Free an abbreviations structure. */
602 free_abbrevs (struct backtrace_state
*state
, struct abbrevs
*abbrevs
,
603 backtrace_error_callback error_callback
, void *data
)
607 for (i
= 0; i
< abbrevs
->num_abbrevs
; ++i
)
608 backtrace_free (state
, abbrevs
->abbrevs
[i
].attrs
,
609 abbrevs
->abbrevs
[i
].num_attrs
* sizeof (struct attr
),
610 error_callback
, data
);
611 backtrace_free (state
, abbrevs
->abbrevs
,
612 abbrevs
->num_abbrevs
* sizeof (struct abbrev
),
613 error_callback
, data
);
614 abbrevs
->num_abbrevs
= 0;
615 abbrevs
->abbrevs
= NULL
;
618 /* Read an attribute value. Returns 1 on success, 0 on failure. If
619 the value can be represented as a uint64_t, sets *VAL and sets
620 *IS_VALID to 1. We don't try to store the value of other attribute
621 forms, because we don't care about them. */
624 read_attribute (enum dwarf_form form
, struct dwarf_buf
*buf
,
625 int is_dwarf64
, int version
, int addrsize
,
626 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
627 struct attr_val
*val
)
632 val
->encoding
= ATTR_VAL_ADDRESS
;
633 val
->u
.uint
= read_address (buf
, addrsize
);
636 val
->encoding
= ATTR_VAL_BLOCK
;
637 return advance (buf
, read_uint16 (buf
));
639 val
->encoding
= ATTR_VAL_BLOCK
;
640 return advance (buf
, read_uint32 (buf
));
642 val
->encoding
= ATTR_VAL_UINT
;
643 val
->u
.uint
= read_uint16 (buf
);
646 val
->encoding
= ATTR_VAL_UINT
;
647 val
->u
.uint
= read_uint32 (buf
);
650 val
->encoding
= ATTR_VAL_UINT
;
651 val
->u
.uint
= read_uint64 (buf
);
654 val
->encoding
= ATTR_VAL_STRING
;
655 val
->u
.string
= (const char *) buf
->buf
;
656 return advance (buf
, strnlen ((const char *) buf
->buf
, buf
->left
) + 1);
658 val
->encoding
= ATTR_VAL_BLOCK
;
659 return advance (buf
, read_uleb128 (buf
));
661 val
->encoding
= ATTR_VAL_BLOCK
;
662 return advance (buf
, read_byte (buf
));
664 val
->encoding
= ATTR_VAL_UINT
;
665 val
->u
.uint
= read_byte (buf
);
668 val
->encoding
= ATTR_VAL_UINT
;
669 val
->u
.uint
= read_byte (buf
);
672 val
->encoding
= ATTR_VAL_SINT
;
673 val
->u
.sint
= read_sleb128 (buf
);
679 offset
= read_offset (buf
, is_dwarf64
);
680 if (offset
>= dwarf_str_size
)
682 dwarf_buf_error (buf
, "DW_FORM_strp out of range");
685 val
->encoding
= ATTR_VAL_STRING
;
686 val
->u
.string
= (const char *) dwarf_str
+ offset
;
690 val
->encoding
= ATTR_VAL_UINT
;
691 val
->u
.uint
= read_uleb128 (buf
);
693 case DW_FORM_ref_addr
:
694 val
->encoding
= ATTR_VAL_REF_INFO
;
696 val
->u
.uint
= read_address (buf
, addrsize
);
698 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
701 val
->encoding
= ATTR_VAL_REF_UNIT
;
702 val
->u
.uint
= read_byte (buf
);
705 val
->encoding
= ATTR_VAL_REF_UNIT
;
706 val
->u
.uint
= read_uint16 (buf
);
709 val
->encoding
= ATTR_VAL_REF_UNIT
;
710 val
->u
.uint
= read_uint32 (buf
);
713 val
->encoding
= ATTR_VAL_REF_UNIT
;
714 val
->u
.uint
= read_uint64 (buf
);
716 case DW_FORM_ref_udata
:
717 val
->encoding
= ATTR_VAL_REF_UNIT
;
718 val
->u
.uint
= read_uleb128 (buf
);
720 case DW_FORM_indirect
:
724 form
= read_uleb128 (buf
);
725 return read_attribute ((enum dwarf_form
) form
, buf
, is_dwarf64
,
726 version
, addrsize
, dwarf_str
, dwarf_str_size
,
729 case DW_FORM_sec_offset
:
730 val
->encoding
= ATTR_VAL_REF_SECTION
;
731 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
733 case DW_FORM_exprloc
:
734 val
->encoding
= ATTR_VAL_EXPR
;
735 return advance (buf
, read_uleb128 (buf
));
736 case DW_FORM_flag_present
:
737 val
->encoding
= ATTR_VAL_UINT
;
740 case DW_FORM_ref_sig8
:
741 val
->encoding
= ATTR_VAL_REF_TYPE
;
742 val
->u
.uint
= read_uint64 (buf
);
744 case DW_FORM_GNU_addr_index
:
745 val
->encoding
= ATTR_VAL_REF_SECTION
;
746 val
->u
.uint
= read_uleb128 (buf
);
748 case DW_FORM_GNU_str_index
:
749 val
->encoding
= ATTR_VAL_REF_SECTION
;
750 val
->u
.uint
= read_uleb128 (buf
);
752 case DW_FORM_GNU_ref_alt
:
753 val
->encoding
= ATTR_VAL_REF_SECTION
;
754 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
756 case DW_FORM_GNU_strp_alt
:
757 val
->encoding
= ATTR_VAL_REF_SECTION
;
758 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
761 dwarf_buf_error (buf
, "unrecognized DWARF form");
766 /* Compare function_addrs for qsort. When ranges are nested, make the
767 smallest one sort last. */
770 function_addrs_compare (const void *v1
, const void *v2
)
772 const struct function_addrs
*a1
= (const struct function_addrs
*) v1
;
773 const struct function_addrs
*a2
= (const struct function_addrs
*) v2
;
775 if (a1
->low
< a2
->low
)
777 if (a1
->low
> a2
->low
)
779 if (a1
->high
< a2
->high
)
781 if (a1
->high
> a2
->high
)
783 return strcmp (a1
->function
->name
, a2
->function
->name
);
786 /* Compare a PC against a function_addrs for bsearch. Note that if
787 there are multiple ranges containing PC, which one will be returned
788 is unpredictable. We compensate for that in dwarf_fileline. */
791 function_addrs_search (const void *vkey
, const void *ventry
)
793 const uintptr_t *key
= (const uintptr_t *) vkey
;
794 const struct function_addrs
*entry
= (const struct function_addrs
*) ventry
;
800 else if (pc
>= entry
->high
)
806 /* Add a new compilation unit address range to a vector. Returns 1 on
807 success, 0 on failure. */
810 add_unit_addr (struct backtrace_state
*state
, struct unit_addrs addrs
,
811 backtrace_error_callback error_callback
, void *data
,
812 struct unit_addrs_vector
*vec
)
814 struct unit_addrs
*p
;
816 /* Try to merge with the last entry. */
819 p
= (struct unit_addrs
*) vec
->vec
.base
+ (vec
->count
- 1);
820 if ((addrs
.low
== p
->high
|| addrs
.low
== p
->high
+ 1)
823 if (addrs
.high
> p
->high
)
824 p
->high
= addrs
.high
;
829 p
= ((struct unit_addrs
*)
830 backtrace_vector_grow (state
, sizeof (struct unit_addrs
),
831 error_callback
, data
, &vec
->vec
));
840 /* Free a unit address vector. */
843 free_unit_addrs_vector (struct backtrace_state
*state
,
844 struct unit_addrs_vector
*vec
,
845 backtrace_error_callback error_callback
, void *data
)
847 struct unit_addrs
*addrs
;
850 addrs
= (struct unit_addrs
*) vec
->vec
.base
;
851 for (i
= 0; i
< vec
->count
; ++i
)
852 free_abbrevs (state
, &addrs
[i
].u
->abbrevs
, error_callback
, data
);
855 /* Compare unit_addrs for qsort. When ranges are nested, make the
856 smallest one sort last. */
859 unit_addrs_compare (const void *v1
, const void *v2
)
861 const struct unit_addrs
*a1
= (const struct unit_addrs
*) v1
;
862 const struct unit_addrs
*a2
= (const struct unit_addrs
*) v2
;
864 if (a1
->low
< a2
->low
)
866 if (a1
->low
> a2
->low
)
868 if (a1
->high
< a2
->high
)
870 if (a1
->high
> a2
->high
)
872 if (a1
->u
->lineoff
< a2
->u
->lineoff
)
874 if (a1
->u
->lineoff
> a2
->u
->lineoff
)
879 /* Compare a PC against a unit_addrs for bsearch. Note that if there
880 are multiple ranges containing PC, which one will be returned is
881 unpredictable. We compensate for that in dwarf_fileline. */
884 unit_addrs_search (const void *vkey
, const void *ventry
)
886 const uintptr_t *key
= (const uintptr_t *) vkey
;
887 const struct unit_addrs
*entry
= (const struct unit_addrs
*) ventry
;
893 else if (pc
>= entry
->high
)
899 /* Sort the line vector by PC. We want a stable sort here. We know
900 that the pointers are into the same array, so it is safe to compare
904 line_compare (const void *v1
, const void *v2
)
906 const struct line
*ln1
= (const struct line
*) v1
;
907 const struct line
*ln2
= (const struct line
*) v2
;
909 if (ln1
->pc
< ln2
->pc
)
911 else if (ln1
->pc
> ln2
->pc
)
921 /* Find a PC in a line vector. We always allocate an extra entry at
922 the end of the lines vector, so that this routine can safely look
923 at the next entry. Note that when there are multiple mappings for
924 the same PC value, this will return the last one. */
927 line_search (const void *vkey
, const void *ventry
)
929 const uintptr_t *key
= (const uintptr_t *) vkey
;
930 const struct line
*entry
= (const struct line
*) ventry
;
936 else if (pc
>= (entry
+ 1)->pc
)
942 /* Sort the abbrevs by the abbrev code. This function is passed to
943 both qsort and bsearch. */
946 abbrev_compare (const void *v1
, const void *v2
)
948 const struct abbrev
*a1
= (const struct abbrev
*) v1
;
949 const struct abbrev
*a2
= (const struct abbrev
*) v2
;
951 if (a1
->code
< a2
->code
)
953 else if (a1
->code
> a2
->code
)
957 /* This really shouldn't happen. It means there are two
958 different abbrevs with the same code, and that means we don't
959 know which one lookup_abbrev should return. */
964 /* Read the abbreviation table for a compilation unit. Returns 1 on
965 success, 0 on failure. */
968 read_abbrevs (struct backtrace_state
*state
, uint64_t abbrev_offset
,
969 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
970 int is_bigendian
, backtrace_error_callback error_callback
,
971 void *data
, struct abbrevs
*abbrevs
)
973 struct dwarf_buf abbrev_buf
;
974 struct dwarf_buf count_buf
;
977 abbrevs
->num_abbrevs
= 0;
978 abbrevs
->abbrevs
= NULL
;
980 if (abbrev_offset
>= dwarf_abbrev_size
)
982 error_callback (data
, "abbrev offset out of range", 0);
986 abbrev_buf
.name
= ".debug_abbrev";
987 abbrev_buf
.start
= dwarf_abbrev
;
988 abbrev_buf
.buf
= dwarf_abbrev
+ abbrev_offset
;
989 abbrev_buf
.left
= dwarf_abbrev_size
- abbrev_offset
;
990 abbrev_buf
.is_bigendian
= is_bigendian
;
991 abbrev_buf
.error_callback
= error_callback
;
992 abbrev_buf
.data
= data
;
993 abbrev_buf
.reported_underflow
= 0;
995 /* Count the number of abbrevs in this list. */
997 count_buf
= abbrev_buf
;
999 while (read_uleb128 (&count_buf
) != 0)
1001 if (count_buf
.reported_underflow
)
1005 read_uleb128 (&count_buf
);
1006 // Skip has_children.
1007 read_byte (&count_buf
);
1009 while (read_uleb128 (&count_buf
) != 0)
1010 read_uleb128 (&count_buf
);
1011 // Skip form of last attribute.
1012 read_uleb128 (&count_buf
);
1015 if (count_buf
.reported_underflow
)
1018 if (num_abbrevs
== 0)
1021 abbrevs
->num_abbrevs
= num_abbrevs
;
1022 abbrevs
->abbrevs
= ((struct abbrev
*)
1023 backtrace_alloc (state
,
1024 num_abbrevs
* sizeof (struct abbrev
),
1025 error_callback
, data
));
1026 if (abbrevs
->abbrevs
== NULL
)
1028 memset (abbrevs
->abbrevs
, 0, num_abbrevs
* sizeof (struct abbrev
));
1038 if (abbrev_buf
.reported_underflow
)
1041 code
= read_uleb128 (&abbrev_buf
);
1046 a
.tag
= (enum dwarf_tag
) read_uleb128 (&abbrev_buf
);
1047 a
.has_children
= read_byte (&abbrev_buf
);
1049 count_buf
= abbrev_buf
;
1051 while (read_uleb128 (&count_buf
) != 0)
1054 read_uleb128 (&count_buf
);
1060 read_uleb128 (&abbrev_buf
);
1061 read_uleb128 (&abbrev_buf
);
1065 attrs
= ((struct attr
*)
1066 backtrace_alloc (state
, num_attrs
* sizeof *attrs
,
1067 error_callback
, data
));
1076 name
= read_uleb128 (&abbrev_buf
);
1077 form
= read_uleb128 (&abbrev_buf
);
1080 attrs
[num_attrs
].name
= (enum dwarf_attribute
) name
;
1081 attrs
[num_attrs
].form
= (enum dwarf_form
) form
;
1086 a
.num_attrs
= num_attrs
;
1089 abbrevs
->abbrevs
[num_abbrevs
] = a
;
1093 qsort (abbrevs
->abbrevs
, abbrevs
->num_abbrevs
, sizeof (struct abbrev
),
1099 free_abbrevs (state
, abbrevs
, error_callback
, data
);
1103 /* Return the abbrev information for an abbrev code. */
1105 static const struct abbrev
*
1106 lookup_abbrev (struct abbrevs
*abbrevs
, uint64_t code
,
1107 backtrace_error_callback error_callback
, void *data
)
1112 /* With GCC, where abbrevs are simply numbered in order, we should
1113 be able to just look up the entry. */
1114 if (code
- 1 < abbrevs
->num_abbrevs
1115 && abbrevs
->abbrevs
[code
- 1].code
== code
)
1116 return &abbrevs
->abbrevs
[code
- 1];
1118 /* Otherwise we have to search. */
1119 memset (&key
, 0, sizeof key
);
1121 p
= bsearch (&key
, abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1122 sizeof (struct abbrev
), abbrev_compare
);
1125 error_callback (data
, "invalid abbreviation code", 0);
1128 return (const struct abbrev
*) p
;
1131 /* Add non-contiguous address ranges for a compilation unit. Returns
1132 1 on success, 0 on failure. */
1135 add_unit_ranges (struct backtrace_state
*state
, struct unit
*u
,
1136 uint64_t ranges
, uint64_t base
, int is_bigendian
,
1137 const unsigned char *dwarf_ranges
, size_t dwarf_ranges_size
,
1138 backtrace_error_callback error_callback
, void *data
,
1139 struct unit_addrs_vector
*addrs
)
1141 struct dwarf_buf ranges_buf
;
1143 if (ranges
>= dwarf_ranges_size
)
1145 error_callback (data
, "ranges offset out of range", 0);
1149 ranges_buf
.name
= ".debug_ranges";
1150 ranges_buf
.start
= dwarf_ranges
;
1151 ranges_buf
.buf
= dwarf_ranges
+ ranges
;
1152 ranges_buf
.left
= dwarf_ranges_size
- ranges
;
1153 ranges_buf
.is_bigendian
= is_bigendian
;
1154 ranges_buf
.error_callback
= error_callback
;
1155 ranges_buf
.data
= data
;
1156 ranges_buf
.reported_underflow
= 0;
1163 if (ranges_buf
.reported_underflow
)
1166 low
= read_address (&ranges_buf
, u
->addrsize
);
1167 high
= read_address (&ranges_buf
, u
->addrsize
);
1169 if (low
== 0 && high
== 0)
1172 if (is_highest_address (low
, u
->addrsize
))
1176 struct unit_addrs a
;
1179 a
.high
= high
+ base
;
1181 if (!add_unit_addr (state
, a
, error_callback
, data
, addrs
))
1186 if (ranges_buf
.reported_underflow
)
1192 /* Build a mapping from address ranges to the compilation units where
1193 the line number information for that range can be found. Returns 1
1194 on success, 0 on failure. */
1197 build_address_map (struct backtrace_state
*state
,
1198 const unsigned char *dwarf_info
, size_t dwarf_info_size
,
1199 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1200 const unsigned char *dwarf_ranges
, size_t dwarf_ranges_size
,
1201 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
1202 int is_bigendian
, backtrace_error_callback error_callback
,
1203 void *data
, struct unit_addrs_vector
*addrs
)
1205 struct dwarf_buf info
;
1206 struct abbrevs abbrevs
;
1208 memset (&addrs
->vec
, 0, sizeof addrs
->vec
);
1211 /* Read through the .debug_info section. FIXME: Should we use the
1212 .debug_aranges section? gdb and addr2line don't use it, but I'm
1215 info
.name
= ".debug_info";
1216 info
.start
= dwarf_info
;
1217 info
.buf
= dwarf_info
;
1218 info
.left
= dwarf_info_size
;
1219 info
.is_bigendian
= is_bigendian
;
1220 info
.error_callback
= error_callback
;
1222 info
.reported_underflow
= 0;
1224 memset (&abbrevs
, 0, sizeof abbrevs
);
1225 while (info
.left
> 0)
1227 const unsigned char *unit_data_start
;
1230 struct dwarf_buf unit_buf
;
1232 uint64_t abbrev_offset
;
1233 const struct abbrev
*abbrev
;
1235 const unsigned char *unit_data
;
1236 size_t unit_data_len
;
1237 size_t unit_data_offset
;
1244 int highpc_is_relative
;
1249 const char *comp_dir
;
1251 if (info
.reported_underflow
)
1254 unit_data_start
= info
.buf
;
1257 len
= read_uint32 (&info
);
1258 if (len
== 0xffffffff)
1260 len
= read_uint64 (&info
);
1265 unit_buf
.start
= info
.buf
;
1266 unit_buf
.left
= len
;
1268 if (!advance (&info
, len
))
1271 version
= read_uint16 (&unit_buf
);
1272 if (version
< 2 || version
> 4)
1274 dwarf_buf_error (&unit_buf
, "unrecognized DWARF version");
1278 abbrev_offset
= read_offset (&unit_buf
, is_dwarf64
);
1279 if (!read_abbrevs (state
, abbrev_offset
, dwarf_abbrev
, dwarf_abbrev_size
,
1280 is_bigendian
, error_callback
, data
, &abbrevs
))
1283 addrsize
= read_byte (&unit_buf
);
1285 unit_data
= unit_buf
.buf
;
1286 unit_data_len
= unit_buf
.left
;
1287 unit_data_offset
= unit_buf
.buf
- unit_data_start
;
1289 /* We only look at the first attribute in the compilation unit.
1290 In practice this will be a DW_TAG_compile_unit which will
1291 tell us the PC range and where to find the line number
1294 code
= read_uleb128 (&unit_buf
);
1295 abbrev
= lookup_abbrev (&abbrevs
, code
, error_callback
, data
);
1303 highpc_is_relative
= 0;
1309 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1311 struct attr_val val
;
1313 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
, is_dwarf64
,
1314 version
, addrsize
, dwarf_str
, dwarf_str_size
,
1318 switch (abbrev
->attrs
[i
].name
)
1321 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1328 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1330 highpc
= val
.u
.uint
;
1333 else if (val
.encoding
== ATTR_VAL_UINT
)
1335 highpc
= val
.u
.uint
;
1337 highpc_is_relative
= 1;
1341 if (val
.encoding
== ATTR_VAL_UINT
1342 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1344 ranges
= val
.u
.uint
;
1348 case DW_AT_stmt_list
:
1349 if (val
.encoding
== ATTR_VAL_UINT
1350 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1352 lineoff
= val
.u
.uint
;
1356 case DW_AT_comp_dir
:
1357 if (val
.encoding
== ATTR_VAL_STRING
)
1358 comp_dir
= val
.u
.string
;
1365 if (unit_buf
.reported_underflow
)
1368 if (((have_lowpc
&& have_highpc
) || have_ranges
) && have_lineoff
)
1371 struct unit_addrs a
;
1373 u
= ((struct unit
*)
1374 backtrace_alloc (state
, sizeof *u
, error_callback
, data
));
1377 u
->unit_data
= unit_data
;
1378 u
->unit_data_len
= unit_data_len
;
1379 u
->unit_data_offset
= unit_data_offset
;
1380 u
->version
= version
;
1381 u
->is_dwarf64
= is_dwarf64
;
1382 u
->addrsize
= addrsize
;
1383 u
->comp_dir
= comp_dir
;
1384 u
->lineoff
= lineoff
;
1385 u
->abbrevs
= abbrevs
;
1386 memset (&abbrevs
, 0, sizeof abbrevs
);
1388 /* The actual line number mappings will be read as
1392 u
->function_addrs
= NULL
;
1393 u
->function_addrs_count
= 0;
1397 if (!add_unit_ranges (state
, u
, ranges
, lowpc
, is_bigendian
,
1398 dwarf_ranges
, dwarf_ranges_size
,
1399 error_callback
, data
, addrs
))
1401 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1402 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1408 if (highpc_is_relative
)
1414 if (!add_unit_addr (state
, a
, error_callback
, data
, addrs
))
1416 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1417 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1424 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1425 memset (&abbrevs
, 0, sizeof abbrevs
);
1428 if (info
.reported_underflow
)
1434 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1435 free_unit_addrs_vector (state
, addrs
, error_callback
, data
);
1439 /* Add a new mapping to the vector of line mappings that we are
1440 building. Returns 1 on success, 0 on failure. */
1443 add_line (struct backtrace_state
*state
, uintptr_t pc
, const char *filename
,
1444 int lineno
, backtrace_error_callback error_callback
, void *data
,
1445 struct line_vector
*vec
)
1449 /* If we are adding the same mapping, ignore it. This can happen
1450 when using discriminators. */
1453 ln
= (struct line
*) vec
->vec
.base
+ (vec
->count
- 1);
1454 if (pc
== ln
->pc
&& filename
== ln
->filename
&& lineno
== ln
->lineno
)
1458 ln
= ((struct line
*)
1459 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1465 ln
->filename
= filename
;
1466 ln
->lineno
= lineno
;
1473 /* Free the line header information. If FREE_FILENAMES is true we
1474 free the file names themselves, otherwise we leave them, as there
1475 may be line structures pointing to them. */
1478 free_line_header (struct backtrace_state
*state
, struct line_header
*hdr
,
1479 backtrace_error_callback error_callback
, void *data
)
1481 backtrace_free (state
, hdr
->dirs
, hdr
->dirs_count
* sizeof (const char *),
1482 error_callback
, data
);
1483 backtrace_free (state
, hdr
->filenames
,
1484 hdr
->filenames_count
* sizeof (char *),
1485 error_callback
, data
);
1488 /* Read the line header. Return 1 on success, 0 on failure. */
1491 read_line_header (struct backtrace_state
*state
, struct unit
*u
,
1492 int is_dwarf64
, struct dwarf_buf
*line_buf
,
1493 struct line_header
*hdr
)
1496 struct dwarf_buf hdr_buf
;
1497 const unsigned char *p
;
1498 const unsigned char *pend
;
1501 hdr
->version
= read_uint16 (line_buf
);
1502 if (hdr
->version
< 2 || hdr
->version
> 4)
1504 dwarf_buf_error (line_buf
, "unsupported line number version");
1508 hdrlen
= read_offset (line_buf
, is_dwarf64
);
1510 hdr_buf
= *line_buf
;
1511 hdr_buf
.left
= hdrlen
;
1513 if (!advance (line_buf
, hdrlen
))
1516 hdr
->min_insn_len
= read_byte (&hdr_buf
);
1517 if (hdr
->version
< 4)
1518 hdr
->max_ops_per_insn
= 1;
1520 hdr
->max_ops_per_insn
= read_byte (&hdr_buf
);
1522 /* We don't care about default_is_stmt. */
1523 read_byte (&hdr_buf
);
1525 hdr
->line_base
= read_sbyte (&hdr_buf
);
1526 hdr
->line_range
= read_byte (&hdr_buf
);
1528 hdr
->opcode_base
= read_byte (&hdr_buf
);
1529 hdr
->opcode_lengths
= hdr_buf
.buf
;
1530 if (!advance (&hdr_buf
, hdr
->opcode_base
- 1))
1533 /* Count the number of directory entries. */
1534 hdr
->dirs_count
= 0;
1536 pend
= p
+ hdr_buf
.left
;
1537 while (p
< pend
&& *p
!= '\0')
1539 p
+= strnlen((const char *) p
, pend
- p
) + 1;
1543 hdr
->dirs
= ((const char **)
1544 backtrace_alloc (state
,
1545 hdr
->dirs_count
* sizeof (const char *),
1546 line_buf
->error_callback
, line_buf
->data
));
1547 if (hdr
->dirs
== NULL
)
1551 while (*hdr_buf
.buf
!= '\0')
1553 if (hdr_buf
.reported_underflow
)
1556 hdr
->dirs
[i
] = (const char *) hdr_buf
.buf
;
1558 if (!advance (&hdr_buf
,
1559 strnlen ((const char *) hdr_buf
.buf
, hdr_buf
.left
) + 1))
1562 if (!advance (&hdr_buf
, 1))
1565 /* Count the number of file entries. */
1566 hdr
->filenames_count
= 0;
1568 pend
= p
+ hdr_buf
.left
;
1569 while (p
< pend
&& *p
!= '\0')
1571 p
+= strnlen ((const char *) p
, pend
- p
) + 1;
1572 p
+= leb128_len (p
);
1573 p
+= leb128_len (p
);
1574 p
+= leb128_len (p
);
1575 ++hdr
->filenames_count
;
1578 hdr
->filenames
= ((const char **)
1579 backtrace_alloc (state
,
1580 hdr
->filenames_count
* sizeof (char *),
1581 line_buf
->error_callback
,
1583 if (hdr
->filenames
== NULL
)
1586 while (*hdr_buf
.buf
!= '\0')
1588 const char *filename
;
1591 if (hdr_buf
.reported_underflow
)
1594 filename
= (const char *) hdr_buf
.buf
;
1595 if (!advance (&hdr_buf
,
1596 strnlen ((const char *) hdr_buf
.buf
, hdr_buf
.left
) + 1))
1598 dir_index
= read_uleb128 (&hdr_buf
);
1599 if (IS_ABSOLUTE_PATH (filename
))
1600 hdr
->filenames
[i
] = filename
;
1605 size_t filename_len
;
1610 else if (dir_index
- 1 < hdr
->dirs_count
)
1611 dir
= hdr
->dirs
[dir_index
- 1];
1614 dwarf_buf_error (line_buf
,
1615 ("invalid directory index in "
1616 "line number program header"));
1619 dir_len
= strlen (dir
);
1620 filename_len
= strlen (filename
);
1622 backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
1623 line_buf
->error_callback
, line_buf
->data
));
1626 memcpy (s
, dir
, dir_len
);
1627 /* FIXME: If we are on a DOS-based file system, and the
1628 directory or the file name use backslashes, then we
1629 should use a backslash here. */
1631 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
1632 hdr
->filenames
[i
] = s
;
1635 /* Ignore the modification time and size. */
1636 read_uleb128 (&hdr_buf
);
1637 read_uleb128 (&hdr_buf
);
1642 if (hdr_buf
.reported_underflow
)
1648 /* Read the line program, adding line mappings to VEC. Return 1 on
1649 success, 0 on failure. */
1652 read_line_program (struct backtrace_state
*state
, struct unit
*u
,
1653 const struct line_header
*hdr
, struct dwarf_buf
*line_buf
,
1654 struct line_vector
*vec
)
1657 unsigned int op_index
;
1658 const char *reset_filename
;
1659 const char *filename
;
1664 if (hdr
->filenames_count
> 0)
1665 reset_filename
= hdr
->filenames
[0];
1667 reset_filename
= "";
1668 filename
= reset_filename
;
1670 while (line_buf
->left
> 0)
1674 op
= read_byte (line_buf
);
1675 if (op
>= hdr
->opcode_base
)
1677 unsigned int advance
;
1679 /* Special opcode. */
1680 op
-= hdr
->opcode_base
;
1681 advance
= op
/ hdr
->line_range
;
1682 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1683 / hdr
->max_ops_per_insn
);
1684 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1685 lineno
+= hdr
->line_base
+ (int) (op
% hdr
->line_range
);
1686 add_line (state
, address
, filename
, lineno
, line_buf
->error_callback
,
1687 line_buf
->data
, vec
);
1689 else if (op
== DW_LNS_extended_op
)
1693 len
= read_uleb128 (line_buf
);
1694 op
= read_byte (line_buf
);
1697 case DW_LNE_end_sequence
:
1698 /* FIXME: Should we mark the high PC here? It seems
1699 that we already have that information from the
1700 compilation unit. */
1703 filename
= reset_filename
;
1706 case DW_LNE_set_address
:
1707 address
= read_address (line_buf
, u
->addrsize
);
1709 case DW_LNE_define_file
:
1712 unsigned int dir_index
;
1714 f
= (const char *) line_buf
->buf
;
1715 if (!advance (line_buf
, strnlen (f
, line_buf
->left
) + 1))
1717 dir_index
= read_uleb128 (line_buf
);
1718 /* Ignore that time and length. */
1719 read_uleb128 (line_buf
);
1720 read_uleb128 (line_buf
);
1721 if (IS_ABSOLUTE_PATH (f
))
1732 else if (dir_index
- 1 < hdr
->dirs_count
)
1733 dir
= hdr
->dirs
[dir_index
- 1];
1736 dwarf_buf_error (line_buf
,
1737 ("invalid directory index "
1738 "in line number program"));
1741 dir_len
= strlen (dir
);
1744 backtrace_alloc (state
, dir_len
+ f_len
+ 2,
1745 line_buf
->error_callback
,
1749 memcpy (p
, dir
, dir_len
);
1750 /* FIXME: If we are on a DOS-based file system,
1751 and the directory or the file name use
1752 backslashes, then we should use a backslash
1755 memcpy (p
+ dir_len
+ 1, f
, f_len
+ 1);
1760 case DW_LNE_set_discriminator
:
1761 /* We don't care about discriminators. */
1762 read_uleb128 (line_buf
);
1765 if (!advance (line_buf
, len
- 1))
1775 add_line (state
, address
, filename
, lineno
,
1776 line_buf
->error_callback
, line_buf
->data
, vec
);
1778 case DW_LNS_advance_pc
:
1782 advance
= read_uleb128 (line_buf
);
1783 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1784 / hdr
->max_ops_per_insn
);
1785 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1788 case DW_LNS_advance_line
:
1789 lineno
+= (int) read_sleb128 (line_buf
);
1791 case DW_LNS_set_file
:
1795 fileno
= read_uleb128 (line_buf
);
1800 if (fileno
- 1 >= hdr
->filenames_count
)
1802 dwarf_buf_error (line_buf
,
1803 ("invalid file number in "
1804 "line number program"));
1807 filename
= hdr
->filenames
[fileno
- 1];
1811 case DW_LNS_set_column
:
1812 read_uleb128 (line_buf
);
1814 case DW_LNS_negate_stmt
:
1816 case DW_LNS_set_basic_block
:
1818 case DW_LNS_const_add_pc
:
1820 unsigned int advance
;
1822 op
= 255 - hdr
->opcode_base
;
1823 advance
= op
/ hdr
->line_range
;
1824 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1825 / hdr
->max_ops_per_insn
);
1826 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1829 case DW_LNS_fixed_advance_pc
:
1830 address
+= read_uint16 (line_buf
);
1833 case DW_LNS_set_prologue_end
:
1835 case DW_LNS_set_epilogue_begin
:
1837 case DW_LNS_set_isa
:
1838 read_uleb128 (line_buf
);
1844 for (i
= hdr
->opcode_lengths
[op
- 1]; i
> 0; --i
)
1845 read_uleb128 (line_buf
);
1855 /* Read the line number information for a compilation unit. Returns 1
1856 on success, 0 on failure. */
1859 read_line_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1860 backtrace_error_callback error_callback
, void *data
,
1861 struct unit
*u
, struct line_header
*hdr
, struct line
**lines
,
1862 size_t *lines_count
)
1864 struct line_vector vec
;
1865 struct dwarf_buf line_buf
;
1870 memset (&vec
.vec
, 0, sizeof vec
.vec
);
1873 memset (hdr
, 0, sizeof *hdr
);
1875 if (u
->lineoff
!= (off_t
) (size_t) u
->lineoff
1876 || (size_t) u
->lineoff
>= ddata
->dwarf_line_size
)
1878 error_callback (data
, "unit line offset out of range", 0);
1882 line_buf
.name
= ".debug_line";
1883 line_buf
.start
= ddata
->dwarf_line
;
1884 line_buf
.buf
= ddata
->dwarf_line
+ u
->lineoff
;
1885 line_buf
.left
= ddata
->dwarf_line_size
- u
->lineoff
;
1886 line_buf
.is_bigendian
= ddata
->is_bigendian
;
1887 line_buf
.error_callback
= error_callback
;
1888 line_buf
.data
= data
;
1889 line_buf
.reported_underflow
= 0;
1892 len
= read_uint32 (&line_buf
);
1893 if (len
== 0xffffffff)
1895 len
= read_uint64 (&line_buf
);
1898 line_buf
.left
= len
;
1900 if (!read_line_header (state
, u
, is_dwarf64
, &line_buf
, hdr
))
1903 if (!read_line_program (state
, u
, hdr
, &line_buf
, &vec
))
1906 if (line_buf
.reported_underflow
)
1911 /* This is not a failure in the sense of a generating an error,
1912 but it is a failure in that sense that we have no useful
1917 /* Allocate one extra entry at the end. */
1918 ln
= ((struct line
*)
1919 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1923 ln
->pc
= (uintptr_t) -1;
1924 ln
->filename
= NULL
;
1927 if (!backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
))
1930 ln
= (struct line
*) vec
.vec
.base
;
1931 qsort (ln
, vec
.count
, sizeof (struct line
), line_compare
);
1934 *lines_count
= vec
.count
;
1939 vec
.vec
.alc
+= vec
.vec
.size
;
1941 backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
);
1942 free_line_header (state
, hdr
, error_callback
, data
);
1943 *lines
= (struct line
*) (uintptr_t) -1;
1948 /* Read the name of a function from a DIE referenced by a
1949 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
1950 the same compilation unit. */
1953 read_referenced_name (struct dwarf_data
*ddata
, struct unit
*u
,
1954 uint64_t offset
, backtrace_error_callback error_callback
,
1957 struct dwarf_buf unit_buf
;
1959 const struct abbrev
*abbrev
;
1963 /* OFFSET is from the start of the data for this compilation unit.
1964 U->unit_data is the data, but it starts U->unit_data_offset bytes
1965 from the beginning. */
1967 if (offset
< u
->unit_data_offset
1968 || offset
- u
->unit_data_offset
>= u
->unit_data_len
)
1970 error_callback (data
,
1971 "abstract origin or specification out of range",
1976 offset
-= u
->unit_data_offset
;
1978 unit_buf
.name
= ".debug_info";
1979 unit_buf
.start
= ddata
->dwarf_info
;
1980 unit_buf
.buf
= u
->unit_data
+ offset
;
1981 unit_buf
.left
= u
->unit_data_len
- offset
;
1982 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
1983 unit_buf
.error_callback
= error_callback
;
1984 unit_buf
.data
= data
;
1985 unit_buf
.reported_underflow
= 0;
1987 code
= read_uleb128 (&unit_buf
);
1990 dwarf_buf_error (&unit_buf
, "invalid abstract origin or specification");
1994 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
1999 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2001 struct attr_val val
;
2003 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
,
2004 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2005 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2009 switch (abbrev
->attrs
[i
].name
)
2012 /* We prefer the linkage name if get one. */
2013 if (val
.encoding
== ATTR_VAL_STRING
)
2017 case DW_AT_linkage_name
:
2018 case DW_AT_MIPS_linkage_name
:
2019 if (val
.encoding
== ATTR_VAL_STRING
)
2020 return val
.u
.string
;
2023 case DW_AT_specification
:
2024 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2025 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2027 /* This refers to a specification defined in some other
2028 compilation unit. We can handle this case if we
2029 must, but it's harder. */
2032 if (val
.encoding
== ATTR_VAL_UINT
2033 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2037 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2038 error_callback
, data
);
2052 /* Add a single range to U that maps to function. Returns 1 on
2053 success, 0 on error. */
2056 add_function_range (struct backtrace_state
*state
, struct function
*function
,
2057 uint64_t lowpc
, uint64_t highpc
,
2058 backtrace_error_callback error_callback
,
2059 void *data
, struct function_vector
*vec
)
2061 struct function_addrs
*p
;
2065 p
= (struct function_addrs
*) vec
->vec
.base
+ vec
->count
- 1;
2066 if ((lowpc
== p
->high
|| lowpc
== p
->high
+ 1)
2067 && function
== p
->function
)
2069 if (highpc
> p
->high
)
2075 p
= ((struct function_addrs
*)
2076 backtrace_vector_grow (state
, sizeof (struct function_addrs
),
2077 error_callback
, data
, &vec
->vec
));
2083 p
->function
= function
;
2088 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2092 add_function_ranges (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2093 struct unit
*u
, struct function
*function
,
2094 uint64_t ranges
, uint64_t base
,
2095 backtrace_error_callback error_callback
, void *data
,
2096 struct function_vector
*vec
)
2098 struct dwarf_buf ranges_buf
;
2100 if (ranges
>= ddata
->dwarf_ranges_size
)
2102 error_callback (data
, "function ranges offset out of range", 0);
2106 ranges_buf
.name
= ".debug_ranges";
2107 ranges_buf
.start
= ddata
->dwarf_ranges
;
2108 ranges_buf
.buf
= ddata
->dwarf_ranges
+ ranges
;
2109 ranges_buf
.left
= ddata
->dwarf_ranges_size
- ranges
;
2110 ranges_buf
.is_bigendian
= ddata
->is_bigendian
;
2111 ranges_buf
.error_callback
= error_callback
;
2112 ranges_buf
.data
= data
;
2113 ranges_buf
.reported_underflow
= 0;
2120 if (ranges_buf
.reported_underflow
)
2123 low
= read_address (&ranges_buf
, u
->addrsize
);
2124 high
= read_address (&ranges_buf
, u
->addrsize
);
2126 if (low
== 0 && high
== 0)
2129 if (is_highest_address (low
, u
->addrsize
))
2133 if (!add_function_range (state
, function
, low
+ base
, high
+ base
,
2134 error_callback
, data
, vec
))
2139 if (ranges_buf
.reported_underflow
)
2145 /* Read one entry plus all its children. Add function addresses to
2146 VEC. Returns 1 on success, 0 on error. */
2149 read_function_entry (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2150 struct unit
*u
, uint64_t base
, struct dwarf_buf
*unit_buf
,
2151 const struct line_header
*lhdr
,
2152 backtrace_error_callback error_callback
, void *data
,
2153 struct function_vector
*vec
)
2155 while (unit_buf
->left
> 0)
2158 const struct abbrev
*abbrev
;
2160 struct function
*function
;
2166 int highpc_is_relative
;
2170 code
= read_uleb128 (unit_buf
);
2174 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2178 is_function
= (abbrev
->tag
== DW_TAG_subprogram
2179 || abbrev
->tag
== DW_TAG_entry_point
2180 || abbrev
->tag
== DW_TAG_inlined_subroutine
);
2185 function
= ((struct function
*)
2186 backtrace_alloc (state
, sizeof *function
,
2187 error_callback
, data
));
2188 if (function
== NULL
)
2190 memset (function
, 0, sizeof *function
);
2197 highpc_is_relative
= 0;
2200 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2202 struct attr_val val
;
2204 if (!read_attribute (abbrev
->attrs
[i
].form
, unit_buf
,
2205 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2206 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2210 /* The compile unit sets the base address for any address
2211 ranges in the function entries. */
2212 if (abbrev
->tag
== DW_TAG_compile_unit
2213 && abbrev
->attrs
[i
].name
== DW_AT_low_pc
2214 && val
.encoding
== ATTR_VAL_ADDRESS
)
2219 switch (abbrev
->attrs
[i
].name
)
2221 case DW_AT_call_file
:
2222 if (val
.encoding
== ATTR_VAL_UINT
)
2224 if (val
.u
.uint
== 0)
2225 function
->caller_filename
= "";
2228 if (val
.u
.uint
- 1 >= lhdr
->filenames_count
)
2230 dwarf_buf_error (unit_buf
,
2231 ("invalid file number in "
2232 "DW_AT_call_file attribute"));
2235 function
->caller_filename
=
2236 lhdr
->filenames
[val
.u
.uint
- 1];
2241 case DW_AT_call_line
:
2242 if (val
.encoding
== ATTR_VAL_UINT
)
2243 function
->caller_lineno
= val
.u
.uint
;
2246 case DW_AT_abstract_origin
:
2247 case DW_AT_specification
:
2248 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2249 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2251 /* This refers to an abstract origin defined in
2252 some other compilation unit. We can handle
2253 this case if we must, but it's harder. */
2256 if (val
.encoding
== ATTR_VAL_UINT
2257 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2261 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2262 error_callback
, data
);
2264 function
->name
= name
;
2269 if (val
.encoding
== ATTR_VAL_STRING
)
2271 /* Don't override a name we found in some other
2272 way, as it will normally be more
2273 useful--e.g., this name is normally not
2275 if (function
->name
== NULL
)
2276 function
->name
= val
.u
.string
;
2280 case DW_AT_linkage_name
:
2281 case DW_AT_MIPS_linkage_name
:
2282 if (val
.encoding
== ATTR_VAL_STRING
)
2283 function
->name
= val
.u
.string
;
2287 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2295 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2297 highpc
= val
.u
.uint
;
2300 else if (val
.encoding
== ATTR_VAL_UINT
)
2302 highpc
= val
.u
.uint
;
2304 highpc_is_relative
= 1;
2309 if (val
.encoding
== ATTR_VAL_UINT
2310 || val
.encoding
== ATTR_VAL_REF_SECTION
)
2312 ranges
= val
.u
.uint
;
2323 /* If we couldn't find a name for the function, we have no use
2325 if (is_function
&& function
->name
== NULL
)
2327 backtrace_free (state
, function
, sizeof *function
,
2328 error_callback
, data
);
2336 if (!add_function_ranges (state
, ddata
, u
, function
, ranges
,
2337 base
, error_callback
, data
, vec
))
2340 else if (have_lowpc
&& have_highpc
)
2342 if (highpc_is_relative
)
2344 if (!add_function_range (state
, function
, lowpc
, highpc
,
2345 error_callback
, data
, vec
))
2350 backtrace_free (state
, function
, sizeof *function
,
2351 error_callback
, data
);
2356 if (abbrev
->has_children
)
2360 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2361 error_callback
, data
, vec
))
2366 struct function_vector fvec
;
2368 /* Gather any information for inlined functions in
2371 memset (&fvec
, 0, sizeof fvec
);
2373 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2374 error_callback
, data
, &fvec
))
2379 struct function_addrs
*faddrs
;
2381 if (!backtrace_vector_release (state
, &fvec
.vec
,
2382 error_callback
, data
))
2385 faddrs
= (struct function_addrs
*) fvec
.vec
.base
;
2386 qsort (faddrs
, fvec
.count
,
2387 sizeof (struct function_addrs
),
2388 function_addrs_compare
);
2390 function
->function_addrs
= faddrs
;
2391 function
->function_addrs_count
= fvec
.count
;
2400 /* Read function name information for a compilation unit. We look
2401 through the whole unit looking for function tags. */
2404 read_function_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2405 const struct line_header
*lhdr
,
2406 backtrace_error_callback error_callback
, void *data
,
2407 struct unit
*u
, struct function_vector
*fvec
,
2408 struct function_addrs
**ret_addrs
,
2409 size_t *ret_addrs_count
)
2411 struct dwarf_buf unit_buf
;
2412 struct function_addrs
*addrs
;
2415 unit_buf
.name
= ".debug_info";
2416 unit_buf
.start
= ddata
->dwarf_info
;
2417 unit_buf
.buf
= u
->unit_data
;
2418 unit_buf
.left
= u
->unit_data_len
;
2419 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2420 unit_buf
.error_callback
= error_callback
;
2421 unit_buf
.data
= data
;
2422 unit_buf
.reported_underflow
= 0;
2424 while (unit_buf
.left
> 0)
2426 if (!read_function_entry (state
, ddata
, u
, 0, &unit_buf
, lhdr
,
2427 error_callback
, data
, fvec
))
2431 if (fvec
->count
== 0)
2434 addrs
= (struct function_addrs
*) fvec
->vec
.base
;
2435 addrs_count
= fvec
->count
;
2437 /* Finish this list of addresses, but leave the remaining space in
2438 the vector available for the next function unit. */
2439 backtrace_vector_finish (state
, &fvec
->vec
);
2442 qsort (addrs
, addrs_count
, sizeof (struct function_addrs
),
2443 function_addrs_compare
);
2446 *ret_addrs_count
= addrs_count
;
2449 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2450 information, and update FILENAME and LINENO for the caller.
2451 Returns whatever CALLBACK returns, or 0 to keep going. */
2454 report_inlined_functions (uintptr_t pc
, struct function
*function
,
2455 backtrace_full_callback callback
, void *data
,
2456 const char **filename
, int *lineno
)
2458 struct function_addrs
*function_addrs
;
2459 struct function
*inlined
;
2462 if (function
->function_addrs_count
== 0)
2465 function_addrs
= ((struct function_addrs
*)
2466 bsearch (&pc
, function
->function_addrs
,
2467 function
->function_addrs_count
,
2468 sizeof (struct function_addrs
),
2469 function_addrs_search
));
2470 if (function_addrs
== NULL
)
2473 while (((size_t) (function_addrs
- function
->function_addrs
) + 1
2474 < function
->function_addrs_count
)
2475 && pc
>= (function_addrs
+ 1)->low
2476 && pc
< (function_addrs
+ 1)->high
)
2479 /* We found an inlined call. */
2481 inlined
= function_addrs
->function
;
2483 /* Report any calls inlined into this one. */
2484 ret
= report_inlined_functions (pc
, inlined
, callback
, data
,
2489 /* Report this inlined call. */
2490 ret
= callback (data
, pc
, *filename
, *lineno
, inlined
->name
);
2494 /* Our caller will report the caller of the inlined function; tell
2495 it the appropriate filename and line number. */
2496 *filename
= inlined
->caller_filename
;
2497 *lineno
= inlined
->caller_lineno
;
2502 /* Return the file/line information for a PC using the DWARF mapping
2503 we built earlier. */
2506 dwarf_fileline (struct backtrace_state
*state
, uintptr_t pc
,
2507 backtrace_full_callback callback
,
2508 backtrace_error_callback error_callback
, void *data
)
2510 struct dwarf_data
*ddata
;
2511 struct unit_addrs
*entry
;
2516 struct function_addrs
*function_addrs
;
2517 struct function
*function
;
2518 const char *filename
;
2522 ddata
= (struct dwarf_data
*) state
->fileline_data
;
2524 /* Find an address range that includes PC. */
2525 entry
= bsearch (&pc
, ddata
->addrs
, ddata
->addrs_count
,
2526 sizeof (struct unit_addrs
), unit_addrs_search
);
2529 return callback (data
, pc
, NULL
, 0, NULL
);
2531 /* If there are multiple ranges that contain PC, use the last one,
2532 in order to produce predictable results. If we assume that all
2533 ranges are properly nested, then the last range will be the
2535 while ((size_t) (entry
- ddata
->addrs
) + 1 < ddata
->addrs_count
2536 && pc
>= (entry
+ 1)->low
2537 && pc
< (entry
+ 1)->high
)
2540 /* We need the lines, lines_count, function_addrs,
2541 function_addrs_count fields of u. If they are not set, we need
2542 to set them. When running in threaded mode, we need to allow for
2543 the possibility that some other thread is setting them
2549 /* Skip units with no useful line number information by walking
2550 backward. Useless line number information is marked by setting
2552 while (entry
> ddata
->addrs
2553 && pc
>= (entry
- 1)->low
2554 && pc
< (entry
- 1)->high
)
2556 if (state
->threaded
)
2558 /* Use __sync_bool_compare_and_swap to do a
2560 while (!__sync_bool_compare_and_swap (&u
->lines
, lines
, lines
))
2564 if (lines
!= (struct line
*) (uintptr_t) -1)
2573 /* Do a load-acquire of u->lines. */
2574 if (state
->threaded
)
2576 /* Use __sync_bool_compare_and_swap to do an atomic load. */
2577 while (!__sync_bool_compare_and_swap (&u
->lines
, lines
, lines
))
2584 size_t function_addrs_count
;
2585 struct line_header lhdr
;
2588 /* We have never read the line information for this unit. Read
2591 function_addrs
= NULL
;
2592 function_addrs_count
= 0;
2593 if (read_line_info (state
, ddata
, error_callback
, data
, entry
->u
, &lhdr
,
2596 read_function_info (state
, ddata
, &lhdr
, error_callback
, data
,
2597 entry
->u
, &ddata
->fvec
, &function_addrs
,
2598 &function_addrs_count
);
2599 free_line_header (state
, &lhdr
, error_callback
, data
);
2603 /* Atomically store the information we just read into the unit.
2604 If another thread is simultaneously writing, it presumably
2605 read the same information, and we don't care which one we
2606 wind up with; we just leak the other one. We do have to
2607 write the lines field last, so that the acquire-loads above
2608 ensure that the other fields are set. */
2610 if (!state
->threaded
)
2612 u
->lines_count
= count
;
2613 u
->function_addrs
= function_addrs
;
2614 u
->function_addrs_count
= function_addrs_count
;
2619 __sync_bool_compare_and_swap (&u
->lines_count
, 0, count
);
2620 __sync_bool_compare_and_swap (&u
->function_addrs
, NULL
,
2622 __sync_bool_compare_and_swap (&u
->function_addrs_count
, 0,
2623 function_addrs_count
);
2624 __sync_bool_compare_and_swap (&u
->lines
, NULL
, lines
);
2628 /* Now all fields of U have been initialized. */
2630 if (lines
== (struct line
*) (uintptr_t) -1)
2632 /* If reading the line number information failed in some way,
2633 try again to see if there is a better compilation unit for
2636 dwarf_fileline (state
, pc
, callback
, error_callback
, data
);
2637 return callback (data
, pc
, NULL
, 0, NULL
);
2640 /* Search for PC within this unit. */
2642 ln
= (struct line
*) bsearch (&pc
, lines
, entry
->u
->lines_count
,
2643 sizeof (struct line
), line_search
);
2646 error_callback (data
, "inconsistent DWARF line number info", 0);
2650 /* Search for function name within this unit. */
2652 if (entry
->u
->function_addrs_count
== 0)
2653 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2655 function_addrs
= ((struct function_addrs
*)
2656 bsearch (&pc
, entry
->u
->function_addrs
,
2657 entry
->u
->function_addrs_count
,
2658 sizeof (struct function_addrs
),
2659 function_addrs_search
));
2660 if (function_addrs
== NULL
)
2661 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2663 /* If there are multiple function ranges that contain PC, use the
2664 last one, in order to produce predictable results. */
2666 while (((size_t) (function_addrs
- entry
->u
->function_addrs
+ 1)
2667 < entry
->u
->function_addrs_count
)
2668 && pc
>= (function_addrs
+ 1)->low
2669 && pc
< (function_addrs
+ 1)->high
)
2672 function
= function_addrs
->function
;
2674 filename
= ln
->filename
;
2675 lineno
= ln
->lineno
;
2677 ret
= report_inlined_functions (pc
, function
, callback
, data
,
2678 &filename
, &lineno
);
2682 return callback (data
, pc
, filename
, lineno
, function
->name
);
2685 /* Build our data structures from the .debug_info and .debug_line
2686 sections. Set *FILELINE_FN and *FILELINE_DATA. Return 1 on
2687 success, 0 on failure. */
2690 backtrace_dwarf_initialize (struct backtrace_state
*state
,
2691 const unsigned char *dwarf_info
,
2692 size_t dwarf_info_size
,
2693 const unsigned char *dwarf_line
,
2694 size_t dwarf_line_size
,
2695 const unsigned char *dwarf_abbrev
,
2696 size_t dwarf_abbrev_size
,
2697 const unsigned char *dwarf_ranges
,
2698 size_t dwarf_ranges_size
,
2699 const unsigned char *dwarf_str
,
2700 size_t dwarf_str_size
,
2702 backtrace_error_callback error_callback
,
2703 void *data
, fileline
*fileline_fn
)
2705 struct unit_addrs_vector addrs_vec
;
2706 struct unit_addrs
*addrs
;
2708 struct dwarf_data
*fdata
;
2710 if (!build_address_map (state
, dwarf_info
, dwarf_info_size
, dwarf_abbrev
,
2711 dwarf_abbrev_size
, dwarf_ranges
, dwarf_ranges_size
,
2712 dwarf_str
, dwarf_str_size
, is_bigendian
,
2713 error_callback
, data
, &addrs_vec
))
2716 if (!backtrace_vector_release (state
, &addrs_vec
.vec
, error_callback
, data
))
2718 addrs
= (struct unit_addrs
*) addrs_vec
.vec
.base
;
2719 addrs_count
= addrs_vec
.count
;
2720 qsort (addrs
, addrs_count
, sizeof (struct unit_addrs
), unit_addrs_compare
);
2722 fdata
= ((struct dwarf_data
*)
2723 backtrace_alloc (state
, sizeof (struct dwarf_data
),
2724 error_callback
, data
));
2728 fdata
->addrs
= addrs
;
2729 fdata
->addrs_count
= addrs_count
;
2730 fdata
->dwarf_info
= dwarf_info
;
2731 fdata
->dwarf_info_size
= dwarf_info_size
;
2732 fdata
->dwarf_line
= dwarf_line
;
2733 fdata
->dwarf_line_size
= dwarf_line_size
;
2734 fdata
->dwarf_ranges
= dwarf_ranges
;
2735 fdata
->dwarf_ranges_size
= dwarf_ranges_size
;
2736 fdata
->dwarf_str
= dwarf_str
;
2737 fdata
->dwarf_str_size
= dwarf_str_size
;
2738 fdata
->is_bigendian
= is_bigendian
;
2739 memset (&fdata
->fvec
, 0, sizeof fdata
->fvec
);
2741 state
->fileline_data
= fdata
;
2743 *fileline_fn
= dwarf_fileline
;