1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
38 #include <sys/types.h>
41 #include "filenames.h"
43 #include "backtrace.h"
46 #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
48 /* If strnlen is not declared, provide our own version. */
51 xstrnlen (const char *s
, size_t maxlen
)
55 for (i
= 0; i
< maxlen
; ++i
)
61 #define strnlen xstrnlen
65 /* A buffer to read DWARF info. */
69 /* Buffer name for error messages. */
71 /* Start of the buffer. */
72 const unsigned char *start
;
73 /* Next byte to read. */
74 const unsigned char *buf
;
75 /* The number of bytes remaining. */
77 /* Whether the data is big-endian. */
79 /* Error callback routine. */
80 backtrace_error_callback error_callback
;
81 /* Data for error_callback. */
83 /* Non-zero if we've reported an underflow error. */
84 int reported_underflow
;
87 /* A single attribute in a DWARF abbreviation. */
91 /* The attribute name. */
92 enum dwarf_attribute name
;
93 /* The attribute form. */
95 /* The attribute value, for DW_FORM_implicit_const. */
99 /* A single DWARF abbreviation. */
103 /* The abbrev code--the number used to refer to the abbrev. */
107 /* Non-zero if this abbrev has child entries. */
109 /* The number of attributes. */
111 /* The attributes. */
115 /* The DWARF abbreviations for a compilation unit. This structure
116 only exists while reading the compilation unit. Most DWARF readers
117 seem to a hash table to map abbrev ID's to abbrev entries.
118 However, we primarily care about GCC, and GCC simply issues ID's in
119 numerical order starting at 1. So we simply keep a sorted vector,
120 and try to just look up the code. */
124 /* The number of abbrevs in the vector. */
126 /* The abbrevs, sorted by the code field. */
127 struct abbrev
*abbrevs
;
130 /* The different kinds of attribute values. */
132 enum attr_val_encoding
134 /* No attribute value. */
138 /* An index into the .debug_addr section, whose value is relative to
139 * the DW_AT_addr_base attribute of the compilation unit. */
140 ATTR_VAL_ADDRESS_INDEX
,
141 /* A unsigned integer. */
143 /* A sigd integer. */
147 /* An index into the .debug_str_offsets section. */
148 ATTR_VAL_STRING_INDEX
,
149 /* An offset to other data in the containing unit. */
151 /* An offset to other data within the .debug_info section. */
153 /* An offset to other data within the alt .debug_info section. */
154 ATTR_VAL_REF_ALT_INFO
,
155 /* An offset to data in some other section. */
156 ATTR_VAL_REF_SECTION
,
157 /* A type signature. */
159 /* An index into the .debug_rnglists section. */
160 ATTR_VAL_RNGLISTS_INDEX
,
161 /* A block of data (not represented). */
163 /* An expression (not represented). */
167 /* An attribute value. */
171 /* How the value is stored in the field u. */
172 enum attr_val_encoding encoding
;
175 /* ATTR_VAL_ADDRESS*, ATTR_VAL_UINT, ATTR_VAL_REF*. */
179 /* ATTR_VAL_STRING. */
181 /* ATTR_VAL_BLOCK not stored. */
185 /* The line number program header. */
189 /* The version of the line number information. */
193 /* The minimum instruction length. */
194 unsigned int min_insn_len
;
195 /* The maximum number of ops per instruction. */
196 unsigned int max_ops_per_insn
;
197 /* The line base for special opcodes. */
199 /* The line range for special opcodes. */
200 unsigned int line_range
;
201 /* The opcode base--the first special opcode. */
202 unsigned int opcode_base
;
203 /* Opcode lengths, indexed by opcode - 1. */
204 const unsigned char *opcode_lengths
;
205 /* The number of directory entries. */
207 /* The directory entries. */
209 /* The number of filenames. */
210 size_t filenames_count
;
212 const char **filenames
;
215 /* A format description from a line header. */
217 struct line_header_format
219 int lnct
; /* LNCT code. */
220 enum dwarf_form form
; /* Form of entry data. */
223 /* Map a single PC value to a file/line. We will keep a vector of
224 these sorted by PC value. Each file/line will be correct from the
225 PC up to the PC of the next entry if there is one. We allocate one
226 extra entry at the end so that we can use bsearch. */
232 /* File name. Many entries in the array are expected to point to
233 the same file name. */
234 const char *filename
;
237 /* Index of the object in the original array read from the DWARF
238 section, before it has been sorted. The index makes it possible
239 to use Quicksort and maintain stability. */
243 /* A growable vector of line number information. This is used while
244 reading the line numbers. */
248 /* Memory. This is an array of struct line. */
249 struct backtrace_vector vec
;
250 /* Number of valid mappings. */
254 /* A function described in the debug info. */
258 /* The name of the function. */
260 /* If this is an inlined function, the filename of the call
262 const char *caller_filename
;
263 /* If this is an inlined function, the line number of the call
266 /* Map PC ranges to inlined functions. */
267 struct function_addrs
*function_addrs
;
268 size_t function_addrs_count
;
271 /* An address range for a function. This maps a PC value to a
272 specific function. */
274 struct function_addrs
276 /* Range is LOW <= PC < HIGH. */
279 /* Function for this address range. */
280 struct function
*function
;
283 /* A growable vector of function address ranges. */
285 struct function_vector
287 /* Memory. This is an array of struct function_addrs. */
288 struct backtrace_vector vec
;
289 /* Number of address ranges present. */
293 /* A DWARF compilation unit. This only holds the information we need
294 to map a PC to a file and line. */
298 /* The first entry for this compilation unit. */
299 const unsigned char *unit_data
;
300 /* The length of the data for this compilation unit. */
301 size_t unit_data_len
;
302 /* The offset of UNIT_DATA from the start of the information for
303 this compilation unit. */
304 size_t unit_data_offset
;
305 /* Offset of the start of the compilation unit from the start of the
306 .debug_info section. */
308 /* Offset of the end of the compilation unit from the start of the
309 .debug_info section. */
313 /* Whether unit is DWARF64. */
317 /* Offset into line number information. */
319 /* Offset of compilation unit in .debug_str_offsets. */
320 uint64_t str_offsets_base
;
321 /* Offset of compilation unit in .debug_addr. */
323 /* Offset of compilation unit in .debug_rnglists. */
324 uint64_t rnglists_base
;
325 /* Primary source file. */
326 const char *filename
;
327 /* Compilation command working directory. */
328 const char *comp_dir
;
329 /* Absolute file name, only set if needed. */
330 const char *abs_filename
;
331 /* The abbreviations for this unit. */
332 struct abbrevs abbrevs
;
334 /* The fields above this point are read in during initialization and
335 may be accessed freely. The fields below this point are read in
336 as needed, and therefore require care, as different threads may
337 try to initialize them simultaneously. */
339 /* PC to line number mapping. This is NULL if the values have not
340 been read. This is (struct line *) -1 if there was an error
341 reading the values. */
343 /* Number of entries in lines. */
345 /* PC ranges to function. */
346 struct function_addrs
*function_addrs
;
347 size_t function_addrs_count
;
350 /* An address range for a compilation unit. This maps a PC value to a
351 specific compilation unit. Note that we invert the representation
352 in DWARF: instead of listing the units and attaching a list of
353 ranges, we list the ranges and have each one point to the unit.
354 This lets us do a binary search to find the unit. */
358 /* Range is LOW <= PC < HIGH. */
361 /* Compilation unit for this address range. */
365 /* A growable vector of compilation unit address ranges. */
367 struct unit_addrs_vector
369 /* Memory. This is an array of struct unit_addrs. */
370 struct backtrace_vector vec
;
371 /* Number of address ranges present. */
375 /* A growable vector of compilation unit pointer. */
379 struct backtrace_vector vec
;
383 /* The information we need to map a PC to a file and line. */
387 /* The data for the next file we know about. */
388 struct dwarf_data
*next
;
389 /* The data for .gnu_debugaltlink. */
390 struct dwarf_data
*altlink
;
391 /* The base address for this file. */
392 uintptr_t base_address
;
393 /* A sorted list of address ranges. */
394 struct unit_addrs
*addrs
;
395 /* Number of address ranges in list. */
397 /* A sorted list of units. */
399 /* Number of units in the list. */
401 /* The unparsed DWARF debug data. */
402 struct dwarf_sections dwarf_sections
;
403 /* Whether the data is big-endian or not. */
405 /* A vector used for function addresses. We keep this here so that
406 we can grow the vector as we read more functions. */
407 struct function_vector fvec
;
410 /* Report an error for a DWARF buffer. */
413 dwarf_buf_error (struct dwarf_buf
*buf
, const char *msg
)
417 snprintf (b
, sizeof b
, "%s in %s at %d",
418 msg
, buf
->name
, (int) (buf
->buf
- buf
->start
));
419 buf
->error_callback (buf
->data
, b
, 0);
422 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
426 require (struct dwarf_buf
*buf
, size_t count
)
428 if (buf
->left
>= count
)
431 if (!buf
->reported_underflow
)
433 dwarf_buf_error (buf
, "DWARF underflow");
434 buf
->reported_underflow
= 1;
440 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
444 advance (struct dwarf_buf
*buf
, size_t count
)
446 if (!require (buf
, count
))
453 /* Read one zero-terminated string from BUF and advance past the string. */
456 read_string (struct dwarf_buf
*buf
)
458 const char *p
= (const char *)buf
->buf
;
459 size_t len
= strnlen (p
, buf
->left
);
461 /* - If len == left, we ran out of buffer before finding the zero terminator.
462 Generate an error by advancing len + 1.
463 - If len < left, advance by len + 1 to skip past the zero terminator. */
464 size_t count
= len
+ 1;
466 if (!advance (buf
, count
))
472 /* Read one byte from BUF and advance 1 byte. */
475 read_byte (struct dwarf_buf
*buf
)
477 const unsigned char *p
= buf
->buf
;
479 if (!advance (buf
, 1))
484 /* Read a signed char from BUF and advance 1 byte. */
487 read_sbyte (struct dwarf_buf
*buf
)
489 const unsigned char *p
= buf
->buf
;
491 if (!advance (buf
, 1))
493 return (*p
^ 0x80) - 0x80;
496 /* Read a uint16 from BUF and advance 2 bytes. */
499 read_uint16 (struct dwarf_buf
*buf
)
501 const unsigned char *p
= buf
->buf
;
503 if (!advance (buf
, 2))
505 if (buf
->is_bigendian
)
506 return ((uint16_t) p
[0] << 8) | (uint16_t) p
[1];
508 return ((uint16_t) p
[1] << 8) | (uint16_t) p
[0];
511 /* Read a 24 bit value from BUF and advance 3 bytes. */
514 read_uint24 (struct dwarf_buf
*buf
)
516 const unsigned char *p
= buf
->buf
;
518 if (!advance (buf
, 3))
520 if (buf
->is_bigendian
)
521 return (((uint32_t) p
[0] << 16) | ((uint32_t) p
[1] << 8)
524 return (((uint32_t) p
[2] << 16) | ((uint32_t) p
[1] << 8)
528 /* Read a uint32 from BUF and advance 4 bytes. */
531 read_uint32 (struct dwarf_buf
*buf
)
533 const unsigned char *p
= buf
->buf
;
535 if (!advance (buf
, 4))
537 if (buf
->is_bigendian
)
538 return (((uint32_t) p
[0] << 24) | ((uint32_t) p
[1] << 16)
539 | ((uint32_t) p
[2] << 8) | (uint32_t) p
[3]);
541 return (((uint32_t) p
[3] << 24) | ((uint32_t) p
[2] << 16)
542 | ((uint32_t) p
[1] << 8) | (uint32_t) p
[0]);
545 /* Read a uint64 from BUF and advance 8 bytes. */
548 read_uint64 (struct dwarf_buf
*buf
)
550 const unsigned char *p
= buf
->buf
;
552 if (!advance (buf
, 8))
554 if (buf
->is_bigendian
)
555 return (((uint64_t) p
[0] << 56) | ((uint64_t) p
[1] << 48)
556 | ((uint64_t) p
[2] << 40) | ((uint64_t) p
[3] << 32)
557 | ((uint64_t) p
[4] << 24) | ((uint64_t) p
[5] << 16)
558 | ((uint64_t) p
[6] << 8) | (uint64_t) p
[7]);
560 return (((uint64_t) p
[7] << 56) | ((uint64_t) p
[6] << 48)
561 | ((uint64_t) p
[5] << 40) | ((uint64_t) p
[4] << 32)
562 | ((uint64_t) p
[3] << 24) | ((uint64_t) p
[2] << 16)
563 | ((uint64_t) p
[1] << 8) | (uint64_t) p
[0]);
566 /* Read an offset from BUF and advance the appropriate number of
570 read_offset (struct dwarf_buf
*buf
, int is_dwarf64
)
573 return read_uint64 (buf
);
575 return read_uint32 (buf
);
578 /* Read an address from BUF and advance the appropriate number of
582 read_address (struct dwarf_buf
*buf
, int addrsize
)
587 return read_byte (buf
);
589 return read_uint16 (buf
);
591 return read_uint32 (buf
);
593 return read_uint64 (buf
);
595 dwarf_buf_error (buf
, "unrecognized address size");
600 /* Return whether a value is the highest possible address, given the
604 is_highest_address (uint64_t address
, int addrsize
)
609 return address
== (unsigned char) -1;
611 return address
== (uint16_t) -1;
613 return address
== (uint32_t) -1;
615 return address
== (uint64_t) -1;
621 /* Read an unsigned LEB128 number. */
624 read_uleb128 (struct dwarf_buf
*buf
)
636 const unsigned char *p
;
639 if (!advance (buf
, 1))
643 ret
|= ((uint64_t) (b
& 0x7f)) << shift
;
646 dwarf_buf_error (buf
, "LEB128 overflows uint64_t");
651 while ((b
& 0x80) != 0);
656 /* Read a signed LEB128 number. */
659 read_sleb128 (struct dwarf_buf
*buf
)
671 const unsigned char *p
;
674 if (!advance (buf
, 1))
678 val
|= ((uint64_t) (b
& 0x7f)) << shift
;
681 dwarf_buf_error (buf
, "signed LEB128 overflows uint64_t");
686 while ((b
& 0x80) != 0);
688 if ((b
& 0x40) != 0 && shift
< 64)
689 val
|= ((uint64_t) -1) << shift
;
691 return (int64_t) val
;
694 /* Return the length of an LEB128 number. */
697 leb128_len (const unsigned char *p
)
702 while ((*p
& 0x80) != 0)
710 /* Read initial_length from BUF and advance the appropriate number of bytes. */
713 read_initial_length (struct dwarf_buf
*buf
, int *is_dwarf64
)
717 len
= read_uint32 (buf
);
718 if (len
== 0xffffffff)
720 len
= read_uint64 (buf
);
729 /* Free an abbreviations structure. */
732 free_abbrevs (struct backtrace_state
*state
, struct abbrevs
*abbrevs
,
733 backtrace_error_callback error_callback
, void *data
)
737 for (i
= 0; i
< abbrevs
->num_abbrevs
; ++i
)
738 backtrace_free (state
, abbrevs
->abbrevs
[i
].attrs
,
739 abbrevs
->abbrevs
[i
].num_attrs
* sizeof (struct attr
),
740 error_callback
, data
);
741 backtrace_free (state
, abbrevs
->abbrevs
,
742 abbrevs
->num_abbrevs
* sizeof (struct abbrev
),
743 error_callback
, data
);
744 abbrevs
->num_abbrevs
= 0;
745 abbrevs
->abbrevs
= NULL
;
748 /* Read an attribute value. Returns 1 on success, 0 on failure. If
749 the value can be represented as a uint64_t, sets *VAL and sets
750 *IS_VALID to 1. We don't try to store the value of other attribute
751 forms, because we don't care about them. */
754 read_attribute (enum dwarf_form form
, uint64_t implicit_val
,
755 struct dwarf_buf
*buf
, int is_dwarf64
, int version
,
756 int addrsize
, const struct dwarf_sections
*dwarf_sections
,
757 struct dwarf_data
*altlink
, struct attr_val
*val
)
759 /* Avoid warnings about val.u.FIELD may be used uninitialized if
760 this function is inlined. The warnings aren't valid but can
761 occur because the different fields are set and used
763 memset (val
, 0, sizeof *val
);
768 val
->encoding
= ATTR_VAL_ADDRESS
;
769 val
->u
.uint
= read_address (buf
, addrsize
);
772 val
->encoding
= ATTR_VAL_BLOCK
;
773 return advance (buf
, read_uint16 (buf
));
775 val
->encoding
= ATTR_VAL_BLOCK
;
776 return advance (buf
, read_uint32 (buf
));
778 val
->encoding
= ATTR_VAL_UINT
;
779 val
->u
.uint
= read_uint16 (buf
);
782 val
->encoding
= ATTR_VAL_UINT
;
783 val
->u
.uint
= read_uint32 (buf
);
786 val
->encoding
= ATTR_VAL_UINT
;
787 val
->u
.uint
= read_uint64 (buf
);
790 val
->encoding
= ATTR_VAL_BLOCK
;
791 return advance (buf
, 16);
793 val
->encoding
= ATTR_VAL_STRING
;
794 val
->u
.string
= read_string (buf
);
795 return val
->u
.string
== NULL
? 0 : 1;
797 val
->encoding
= ATTR_VAL_BLOCK
;
798 return advance (buf
, read_uleb128 (buf
));
800 val
->encoding
= ATTR_VAL_BLOCK
;
801 return advance (buf
, read_byte (buf
));
803 val
->encoding
= ATTR_VAL_UINT
;
804 val
->u
.uint
= read_byte (buf
);
807 val
->encoding
= ATTR_VAL_UINT
;
808 val
->u
.uint
= read_byte (buf
);
811 val
->encoding
= ATTR_VAL_SINT
;
812 val
->u
.sint
= read_sleb128 (buf
);
818 offset
= read_offset (buf
, is_dwarf64
);
819 if (offset
>= dwarf_sections
->size
[DEBUG_STR
])
821 dwarf_buf_error (buf
, "DW_FORM_strp out of range");
824 val
->encoding
= ATTR_VAL_STRING
;
826 (const char *) dwarf_sections
->data
[DEBUG_STR
] + offset
;
829 case DW_FORM_line_strp
:
833 offset
= read_offset (buf
, is_dwarf64
);
834 if (offset
>= dwarf_sections
->size
[DEBUG_LINE_STR
])
836 dwarf_buf_error (buf
, "DW_FORM_line_strp out of range");
839 val
->encoding
= ATTR_VAL_STRING
;
841 (const char *) dwarf_sections
->data
[DEBUG_LINE_STR
] + offset
;
845 val
->encoding
= ATTR_VAL_UINT
;
846 val
->u
.uint
= read_uleb128 (buf
);
848 case DW_FORM_ref_addr
:
849 val
->encoding
= ATTR_VAL_REF_INFO
;
851 val
->u
.uint
= read_address (buf
, addrsize
);
853 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
856 val
->encoding
= ATTR_VAL_REF_UNIT
;
857 val
->u
.uint
= read_byte (buf
);
860 val
->encoding
= ATTR_VAL_REF_UNIT
;
861 val
->u
.uint
= read_uint16 (buf
);
864 val
->encoding
= ATTR_VAL_REF_UNIT
;
865 val
->u
.uint
= read_uint32 (buf
);
868 val
->encoding
= ATTR_VAL_REF_UNIT
;
869 val
->u
.uint
= read_uint64 (buf
);
871 case DW_FORM_ref_udata
:
872 val
->encoding
= ATTR_VAL_REF_UNIT
;
873 val
->u
.uint
= read_uleb128 (buf
);
875 case DW_FORM_indirect
:
879 form
= read_uleb128 (buf
);
880 if (form
== DW_FORM_implicit_const
)
882 dwarf_buf_error (buf
,
883 "DW_FORM_indirect to DW_FORM_implicit_const");
886 return read_attribute ((enum dwarf_form
) form
, 0, buf
, is_dwarf64
,
887 version
, addrsize
, dwarf_sections
, altlink
,
890 case DW_FORM_sec_offset
:
891 val
->encoding
= ATTR_VAL_REF_SECTION
;
892 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
894 case DW_FORM_exprloc
:
895 val
->encoding
= ATTR_VAL_EXPR
;
896 return advance (buf
, read_uleb128 (buf
));
897 case DW_FORM_flag_present
:
898 val
->encoding
= ATTR_VAL_UINT
;
901 case DW_FORM_ref_sig8
:
902 val
->encoding
= ATTR_VAL_REF_TYPE
;
903 val
->u
.uint
= read_uint64 (buf
);
905 case DW_FORM_strx
: case DW_FORM_strx1
: case DW_FORM_strx2
:
906 case DW_FORM_strx3
: case DW_FORM_strx4
:
913 offset
= read_uleb128 (buf
);
916 offset
= read_byte (buf
);
919 offset
= read_uint16 (buf
);
922 offset
= read_uint24 (buf
);
925 offset
= read_uint32 (buf
);
928 /* This case can't happen. */
931 val
->encoding
= ATTR_VAL_STRING_INDEX
;
932 val
->u
.uint
= offset
;
935 case DW_FORM_addrx
: case DW_FORM_addrx1
: case DW_FORM_addrx2
:
936 case DW_FORM_addrx3
: case DW_FORM_addrx4
:
943 offset
= read_uleb128 (buf
);
946 offset
= read_byte (buf
);
949 offset
= read_uint16 (buf
);
952 offset
= read_uint24 (buf
);
955 offset
= read_uint32 (buf
);
958 /* This case can't happen. */
961 val
->encoding
= ATTR_VAL_ADDRESS_INDEX
;
962 val
->u
.uint
= offset
;
965 case DW_FORM_ref_sup4
:
966 val
->encoding
= ATTR_VAL_REF_SECTION
;
967 val
->u
.uint
= read_uint32 (buf
);
969 case DW_FORM_ref_sup8
:
970 val
->encoding
= ATTR_VAL_REF_SECTION
;
971 val
->u
.uint
= read_uint64 (buf
);
973 case DW_FORM_implicit_const
:
974 val
->encoding
= ATTR_VAL_UINT
;
975 val
->u
.uint
= implicit_val
;
977 case DW_FORM_loclistx
:
978 /* We don't distinguish this from DW_FORM_sec_offset. It
979 * shouldn't matter since we don't care about loclists. */
980 val
->encoding
= ATTR_VAL_REF_SECTION
;
981 val
->u
.uint
= read_uleb128 (buf
);
983 case DW_FORM_rnglistx
:
984 val
->encoding
= ATTR_VAL_RNGLISTS_INDEX
;
985 val
->u
.uint
= read_uleb128 (buf
);
987 case DW_FORM_GNU_addr_index
:
988 val
->encoding
= ATTR_VAL_REF_SECTION
;
989 val
->u
.uint
= read_uleb128 (buf
);
991 case DW_FORM_GNU_str_index
:
992 val
->encoding
= ATTR_VAL_REF_SECTION
;
993 val
->u
.uint
= read_uleb128 (buf
);
995 case DW_FORM_GNU_ref_alt
:
996 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
999 val
->encoding
= ATTR_VAL_NONE
;
1002 val
->encoding
= ATTR_VAL_REF_ALT_INFO
;
1004 case DW_FORM_strp_sup
: case DW_FORM_GNU_strp_alt
:
1008 offset
= read_offset (buf
, is_dwarf64
);
1009 if (altlink
== NULL
)
1011 val
->encoding
= ATTR_VAL_NONE
;
1014 if (offset
>= altlink
->dwarf_sections
.size
[DEBUG_STR
])
1016 dwarf_buf_error (buf
, "DW_FORM_strp_sup out of range");
1019 val
->encoding
= ATTR_VAL_STRING
;
1021 (const char *) altlink
->dwarf_sections
.data
[DEBUG_STR
] + offset
;
1025 dwarf_buf_error (buf
, "unrecognized DWARF form");
1030 /* If we can determine the value of a string attribute, set *STRING to
1031 point to the string. Return 1 on success, 0 on error. If we don't
1032 know the value, we consider that a success, and we don't change
1033 *STRING. An error is only reported for some sort of out of range
1037 resolve_string (const struct dwarf_sections
*dwarf_sections
, int is_dwarf64
,
1038 int is_bigendian
, uint64_t str_offsets_base
,
1039 const struct attr_val
*val
,
1040 backtrace_error_callback error_callback
, void *data
,
1041 const char **string
)
1043 switch (val
->encoding
)
1045 case ATTR_VAL_STRING
:
1046 *string
= val
->u
.string
;
1049 case ATTR_VAL_STRING_INDEX
:
1052 struct dwarf_buf offset_buf
;
1054 offset
= val
->u
.uint
* (is_dwarf64
? 8 : 4) + str_offsets_base
;
1055 if (offset
+ (is_dwarf64
? 8 : 4)
1056 >= dwarf_sections
->size
[DEBUG_STR_OFFSETS
])
1058 error_callback (data
, "DW_FORM_strx value out of range", 0);
1062 offset_buf
.name
= ".debug_str_offsets";
1063 offset_buf
.start
= dwarf_sections
->data
[DEBUG_STR_OFFSETS
];
1064 offset_buf
.buf
= dwarf_sections
->data
[DEBUG_STR_OFFSETS
] + offset
;
1065 offset_buf
.left
= dwarf_sections
->size
[DEBUG_STR_OFFSETS
] - offset
;
1066 offset_buf
.is_bigendian
= is_bigendian
;
1067 offset_buf
.error_callback
= error_callback
;
1068 offset_buf
.data
= data
;
1069 offset_buf
.reported_underflow
= 0;
1071 offset
= read_offset (&offset_buf
, is_dwarf64
);
1072 if (offset
>= dwarf_sections
->size
[DEBUG_STR
])
1074 dwarf_buf_error (&offset_buf
, "DW_FORM_strx offset out of range");
1077 *string
= (const char *) dwarf_sections
->data
[DEBUG_STR
] + offset
;
1086 /* Set *ADDRESS to the real address for a ATTR_VAL_ADDRESS_INDEX.
1087 Return 1 on success, 0 on error. */
1090 resolve_addr_index (const struct dwarf_sections
*dwarf_sections
,
1091 uint64_t addr_base
, int addrsize
, int is_bigendian
,
1092 uint64_t addr_index
,
1093 backtrace_error_callback error_callback
, void *data
,
1097 struct dwarf_buf addr_buf
;
1099 offset
= addr_index
* addrsize
+ addr_base
;
1100 if (offset
+ addrsize
>= dwarf_sections
->size
[DEBUG_ADDR
])
1102 error_callback (data
, "DW_FORM_addrx value out of range", 0);
1106 addr_buf
.name
= ".debug_addr";
1107 addr_buf
.start
= dwarf_sections
->data
[DEBUG_ADDR
];
1108 addr_buf
.buf
= dwarf_sections
->data
[DEBUG_ADDR
] + offset
;
1109 addr_buf
.left
= dwarf_sections
->size
[DEBUG_ADDR
] - offset
;
1110 addr_buf
.is_bigendian
= is_bigendian
;
1111 addr_buf
.error_callback
= error_callback
;
1112 addr_buf
.data
= data
;
1113 addr_buf
.reported_underflow
= 0;
1115 *address
= read_address (&addr_buf
, addrsize
);
1119 /* Compare a unit offset against a unit for bsearch. */
1122 units_search (const void *vkey
, const void *ventry
)
1124 const size_t *key
= (const size_t *) vkey
;
1125 const struct unit
*entry
= *((const struct unit
*const *) ventry
);
1129 if (offset
< entry
->low_offset
)
1131 else if (offset
>= entry
->high_offset
)
1137 /* Find a unit in PU containing OFFSET. */
1139 static struct unit
*
1140 find_unit (struct unit
**pu
, size_t units_count
, size_t offset
)
1143 u
= bsearch (&offset
, pu
, units_count
, sizeof (struct unit
*), units_search
);
1144 return u
== NULL
? NULL
: *u
;
1147 /* Compare function_addrs for qsort. When ranges are nested, make the
1148 smallest one sort last. */
1151 function_addrs_compare (const void *v1
, const void *v2
)
1153 const struct function_addrs
*a1
= (const struct function_addrs
*) v1
;
1154 const struct function_addrs
*a2
= (const struct function_addrs
*) v2
;
1156 if (a1
->low
< a2
->low
)
1158 if (a1
->low
> a2
->low
)
1160 if (a1
->high
< a2
->high
)
1162 if (a1
->high
> a2
->high
)
1164 return strcmp (a1
->function
->name
, a2
->function
->name
);
1167 /* Compare a PC against a function_addrs for bsearch. Note that if
1168 there are multiple ranges containing PC, which one will be returned
1169 is unpredictable. We compensate for that in dwarf_fileline. */
1172 function_addrs_search (const void *vkey
, const void *ventry
)
1174 const uintptr_t *key
= (const uintptr_t *) vkey
;
1175 const struct function_addrs
*entry
= (const struct function_addrs
*) ventry
;
1179 if (pc
< entry
->low
)
1181 else if (pc
>= entry
->high
)
1187 /* Add a new compilation unit address range to a vector. This is
1188 called via add_ranges. Returns 1 on success, 0 on failure. */
1191 add_unit_addr (struct backtrace_state
*state
, void *rdata
,
1192 uint64_t lowpc
, uint64_t highpc
,
1193 backtrace_error_callback error_callback
, void *data
,
1196 struct unit
*u
= (struct unit
*) rdata
;
1197 struct unit_addrs_vector
*vec
= (struct unit_addrs_vector
*) pvec
;
1198 struct unit_addrs
*p
;
1200 /* Try to merge with the last entry. */
1203 p
= (struct unit_addrs
*) vec
->vec
.base
+ (vec
->count
- 1);
1204 if ((lowpc
== p
->high
|| lowpc
== p
->high
+ 1)
1207 if (highpc
> p
->high
)
1213 p
= ((struct unit_addrs
*)
1214 backtrace_vector_grow (state
, sizeof (struct unit_addrs
),
1215 error_callback
, data
, &vec
->vec
));
1228 /* Compare unit_addrs for qsort. When ranges are nested, make the
1229 smallest one sort last. */
1232 unit_addrs_compare (const void *v1
, const void *v2
)
1234 const struct unit_addrs
*a1
= (const struct unit_addrs
*) v1
;
1235 const struct unit_addrs
*a2
= (const struct unit_addrs
*) v2
;
1237 if (a1
->low
< a2
->low
)
1239 if (a1
->low
> a2
->low
)
1241 if (a1
->high
< a2
->high
)
1243 if (a1
->high
> a2
->high
)
1245 if (a1
->u
->lineoff
< a2
->u
->lineoff
)
1247 if (a1
->u
->lineoff
> a2
->u
->lineoff
)
1252 /* Compare a PC against a unit_addrs for bsearch. Note that if there
1253 are multiple ranges containing PC, which one will be returned is
1254 unpredictable. We compensate for that in dwarf_fileline. */
1257 unit_addrs_search (const void *vkey
, const void *ventry
)
1259 const uintptr_t *key
= (const uintptr_t *) vkey
;
1260 const struct unit_addrs
*entry
= (const struct unit_addrs
*) ventry
;
1264 if (pc
< entry
->low
)
1266 else if (pc
>= entry
->high
)
1272 /* Sort the line vector by PC. We want a stable sort here to maintain
1273 the order of lines for the same PC values. Since the sequence is
1274 being sorted in place, their addresses cannot be relied on to
1275 maintain stability. That is the purpose of the index member. */
1278 line_compare (const void *v1
, const void *v2
)
1280 const struct line
*ln1
= (const struct line
*) v1
;
1281 const struct line
*ln2
= (const struct line
*) v2
;
1283 if (ln1
->pc
< ln2
->pc
)
1285 else if (ln1
->pc
> ln2
->pc
)
1287 else if (ln1
->idx
< ln2
->idx
)
1289 else if (ln1
->idx
> ln2
->idx
)
1295 /* Find a PC in a line vector. We always allocate an extra entry at
1296 the end of the lines vector, so that this routine can safely look
1297 at the next entry. Note that when there are multiple mappings for
1298 the same PC value, this will return the last one. */
1301 line_search (const void *vkey
, const void *ventry
)
1303 const uintptr_t *key
= (const uintptr_t *) vkey
;
1304 const struct line
*entry
= (const struct line
*) ventry
;
1310 else if (pc
>= (entry
+ 1)->pc
)
1316 /* Sort the abbrevs by the abbrev code. This function is passed to
1317 both qsort and bsearch. */
1320 abbrev_compare (const void *v1
, const void *v2
)
1322 const struct abbrev
*a1
= (const struct abbrev
*) v1
;
1323 const struct abbrev
*a2
= (const struct abbrev
*) v2
;
1325 if (a1
->code
< a2
->code
)
1327 else if (a1
->code
> a2
->code
)
1331 /* This really shouldn't happen. It means there are two
1332 different abbrevs with the same code, and that means we don't
1333 know which one lookup_abbrev should return. */
1338 /* Read the abbreviation table for a compilation unit. Returns 1 on
1339 success, 0 on failure. */
1342 read_abbrevs (struct backtrace_state
*state
, uint64_t abbrev_offset
,
1343 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1344 int is_bigendian
, backtrace_error_callback error_callback
,
1345 void *data
, struct abbrevs
*abbrevs
)
1347 struct dwarf_buf abbrev_buf
;
1348 struct dwarf_buf count_buf
;
1351 abbrevs
->num_abbrevs
= 0;
1352 abbrevs
->abbrevs
= NULL
;
1354 if (abbrev_offset
>= dwarf_abbrev_size
)
1356 error_callback (data
, "abbrev offset out of range", 0);
1360 abbrev_buf
.name
= ".debug_abbrev";
1361 abbrev_buf
.start
= dwarf_abbrev
;
1362 abbrev_buf
.buf
= dwarf_abbrev
+ abbrev_offset
;
1363 abbrev_buf
.left
= dwarf_abbrev_size
- abbrev_offset
;
1364 abbrev_buf
.is_bigendian
= is_bigendian
;
1365 abbrev_buf
.error_callback
= error_callback
;
1366 abbrev_buf
.data
= data
;
1367 abbrev_buf
.reported_underflow
= 0;
1369 /* Count the number of abbrevs in this list. */
1371 count_buf
= abbrev_buf
;
1373 while (read_uleb128 (&count_buf
) != 0)
1375 if (count_buf
.reported_underflow
)
1379 read_uleb128 (&count_buf
);
1380 // Skip has_children.
1381 read_byte (&count_buf
);
1383 while (read_uleb128 (&count_buf
) != 0)
1387 form
= read_uleb128 (&count_buf
);
1388 if ((enum dwarf_form
) form
== DW_FORM_implicit_const
)
1389 read_sleb128 (&count_buf
);
1391 // Skip form of last attribute.
1392 read_uleb128 (&count_buf
);
1395 if (count_buf
.reported_underflow
)
1398 if (num_abbrevs
== 0)
1401 abbrevs
->abbrevs
= ((struct abbrev
*)
1402 backtrace_alloc (state
,
1403 num_abbrevs
* sizeof (struct abbrev
),
1404 error_callback
, data
));
1405 if (abbrevs
->abbrevs
== NULL
)
1407 abbrevs
->num_abbrevs
= num_abbrevs
;
1408 memset (abbrevs
->abbrevs
, 0, num_abbrevs
* sizeof (struct abbrev
));
1418 if (abbrev_buf
.reported_underflow
)
1421 code
= read_uleb128 (&abbrev_buf
);
1426 a
.tag
= (enum dwarf_tag
) read_uleb128 (&abbrev_buf
);
1427 a
.has_children
= read_byte (&abbrev_buf
);
1429 count_buf
= abbrev_buf
;
1431 while (read_uleb128 (&count_buf
) != 0)
1436 form
= read_uleb128 (&count_buf
);
1437 if ((enum dwarf_form
) form
== DW_FORM_implicit_const
)
1438 read_sleb128 (&count_buf
);
1444 read_uleb128 (&abbrev_buf
);
1445 read_uleb128 (&abbrev_buf
);
1449 attrs
= ((struct attr
*)
1450 backtrace_alloc (state
, num_attrs
* sizeof *attrs
,
1451 error_callback
, data
));
1460 name
= read_uleb128 (&abbrev_buf
);
1461 form
= read_uleb128 (&abbrev_buf
);
1464 attrs
[num_attrs
].name
= (enum dwarf_attribute
) name
;
1465 attrs
[num_attrs
].form
= (enum dwarf_form
) form
;
1466 if ((enum dwarf_form
) form
== DW_FORM_implicit_const
)
1467 attrs
[num_attrs
].val
= read_sleb128 (&abbrev_buf
);
1469 attrs
[num_attrs
].val
= 0;
1474 a
.num_attrs
= num_attrs
;
1477 abbrevs
->abbrevs
[num_abbrevs
] = a
;
1481 backtrace_qsort (abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1482 sizeof (struct abbrev
), abbrev_compare
);
1487 free_abbrevs (state
, abbrevs
, error_callback
, data
);
1491 /* Return the abbrev information for an abbrev code. */
1493 static const struct abbrev
*
1494 lookup_abbrev (struct abbrevs
*abbrevs
, uint64_t code
,
1495 backtrace_error_callback error_callback
, void *data
)
1500 /* With GCC, where abbrevs are simply numbered in order, we should
1501 be able to just look up the entry. */
1502 if (code
- 1 < abbrevs
->num_abbrevs
1503 && abbrevs
->abbrevs
[code
- 1].code
== code
)
1504 return &abbrevs
->abbrevs
[code
- 1];
1506 /* Otherwise we have to search. */
1507 memset (&key
, 0, sizeof key
);
1509 p
= bsearch (&key
, abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1510 sizeof (struct abbrev
), abbrev_compare
);
1513 error_callback (data
, "invalid abbreviation code", 0);
1516 return (const struct abbrev
*) p
;
1519 /* This struct is used to gather address range information while
1520 reading attributes. We use this while building a mapping from
1521 address ranges to compilation units and then again while mapping
1522 from address ranges to function entries. Normally either
1523 lowpc/highpc is set or ranges is set. */
1526 uint64_t lowpc
; /* The low PC value. */
1527 int have_lowpc
; /* Whether a low PC value was found. */
1528 int lowpc_is_addr_index
; /* Whether lowpc is in .debug_addr. */
1529 uint64_t highpc
; /* The high PC value. */
1530 int have_highpc
; /* Whether a high PC value was found. */
1531 int highpc_is_relative
; /* Whether highpc is relative to lowpc. */
1532 int highpc_is_addr_index
; /* Whether highpc is in .debug_addr. */
1533 uint64_t ranges
; /* Offset in ranges section. */
1534 int have_ranges
; /* Whether ranges is valid. */
1535 int ranges_is_index
; /* Whether ranges is DW_FORM_rnglistx. */
1538 /* Update PCRANGE from an attribute value. */
1541 update_pcrange (const struct attr
* attr
, const struct attr_val
* val
,
1542 struct pcrange
*pcrange
)
1547 if (val
->encoding
== ATTR_VAL_ADDRESS
)
1549 pcrange
->lowpc
= val
->u
.uint
;
1550 pcrange
->have_lowpc
= 1;
1552 else if (val
->encoding
== ATTR_VAL_ADDRESS_INDEX
)
1554 pcrange
->lowpc
= val
->u
.uint
;
1555 pcrange
->have_lowpc
= 1;
1556 pcrange
->lowpc_is_addr_index
= 1;
1561 if (val
->encoding
== ATTR_VAL_ADDRESS
)
1563 pcrange
->highpc
= val
->u
.uint
;
1564 pcrange
->have_highpc
= 1;
1566 else if (val
->encoding
== ATTR_VAL_UINT
)
1568 pcrange
->highpc
= val
->u
.uint
;
1569 pcrange
->have_highpc
= 1;
1570 pcrange
->highpc_is_relative
= 1;
1572 else if (val
->encoding
== ATTR_VAL_ADDRESS_INDEX
)
1574 pcrange
->highpc
= val
->u
.uint
;
1575 pcrange
->have_highpc
= 1;
1576 pcrange
->highpc_is_addr_index
= 1;
1581 if (val
->encoding
== ATTR_VAL_UINT
1582 || val
->encoding
== ATTR_VAL_REF_SECTION
)
1584 pcrange
->ranges
= val
->u
.uint
;
1585 pcrange
->have_ranges
= 1;
1587 else if (val
->encoding
== ATTR_VAL_RNGLISTS_INDEX
)
1589 pcrange
->ranges
= val
->u
.uint
;
1590 pcrange
->have_ranges
= 1;
1591 pcrange
->ranges_is_index
= 1;
1600 /* Call ADD_RANGE for a low/high PC pair. Returns 1 on success, 0 on
1604 add_low_high_range (struct backtrace_state
*state
,
1605 const struct dwarf_sections
*dwarf_sections
,
1606 uintptr_t base_address
, int is_bigendian
,
1607 struct unit
*u
, const struct pcrange
*pcrange
,
1608 int (*add_range
) (struct backtrace_state
*state
,
1609 void *rdata
, uint64_t lowpc
,
1611 backtrace_error_callback error_callback
,
1612 void *data
, void *vec
),
1614 backtrace_error_callback error_callback
, void *data
,
1620 lowpc
= pcrange
->lowpc
;
1621 if (pcrange
->lowpc_is_addr_index
)
1623 if (!resolve_addr_index (dwarf_sections
, u
->addr_base
, u
->addrsize
,
1624 is_bigendian
, lowpc
, error_callback
, data
,
1629 highpc
= pcrange
->highpc
;
1630 if (pcrange
->highpc_is_addr_index
)
1632 if (!resolve_addr_index (dwarf_sections
, u
->addr_base
, u
->addrsize
,
1633 is_bigendian
, highpc
, error_callback
, data
,
1637 if (pcrange
->highpc_is_relative
)
1640 /* Add in the base address of the module when recording PC values,
1641 so that we can look up the PC directly. */
1642 lowpc
+= base_address
;
1643 highpc
+= base_address
;
1645 return add_range (state
, rdata
, lowpc
, highpc
, error_callback
, data
, vec
);
1648 /* Call ADD_RANGE for each range read from .debug_ranges, as used in
1649 DWARF versions 2 through 4. */
1652 add_ranges_from_ranges (
1653 struct backtrace_state
*state
,
1654 const struct dwarf_sections
*dwarf_sections
,
1655 uintptr_t base_address
, int is_bigendian
,
1656 struct unit
*u
, uint64_t base
,
1657 const struct pcrange
*pcrange
,
1658 int (*add_range
) (struct backtrace_state
*state
, void *rdata
,
1659 uint64_t lowpc
, uint64_t highpc
,
1660 backtrace_error_callback error_callback
, void *data
,
1663 backtrace_error_callback error_callback
, void *data
,
1666 struct dwarf_buf ranges_buf
;
1668 if (pcrange
->ranges
>= dwarf_sections
->size
[DEBUG_RANGES
])
1670 error_callback (data
, "ranges offset out of range", 0);
1674 ranges_buf
.name
= ".debug_ranges";
1675 ranges_buf
.start
= dwarf_sections
->data
[DEBUG_RANGES
];
1676 ranges_buf
.buf
= dwarf_sections
->data
[DEBUG_RANGES
] + pcrange
->ranges
;
1677 ranges_buf
.left
= dwarf_sections
->size
[DEBUG_RANGES
] - pcrange
->ranges
;
1678 ranges_buf
.is_bigendian
= is_bigendian
;
1679 ranges_buf
.error_callback
= error_callback
;
1680 ranges_buf
.data
= data
;
1681 ranges_buf
.reported_underflow
= 0;
1688 if (ranges_buf
.reported_underflow
)
1691 low
= read_address (&ranges_buf
, u
->addrsize
);
1692 high
= read_address (&ranges_buf
, u
->addrsize
);
1694 if (low
== 0 && high
== 0)
1697 if (is_highest_address (low
, u
->addrsize
))
1701 if (!add_range (state
, rdata
,
1702 low
+ base
+ base_address
,
1703 high
+ base
+ base_address
,
1704 error_callback
, data
, vec
))
1709 if (ranges_buf
.reported_underflow
)
1715 /* Call ADD_RANGE for each range read from .debug_rnglists, as used in
1719 add_ranges_from_rnglists (
1720 struct backtrace_state
*state
,
1721 const struct dwarf_sections
*dwarf_sections
,
1722 uintptr_t base_address
, int is_bigendian
,
1723 struct unit
*u
, uint64_t base
,
1724 const struct pcrange
*pcrange
,
1725 int (*add_range
) (struct backtrace_state
*state
, void *rdata
,
1726 uint64_t lowpc
, uint64_t highpc
,
1727 backtrace_error_callback error_callback
, void *data
,
1730 backtrace_error_callback error_callback
, void *data
,
1734 struct dwarf_buf rnglists_buf
;
1736 if (!pcrange
->ranges_is_index
)
1737 offset
= pcrange
->ranges
;
1739 offset
= u
->rnglists_base
+ pcrange
->ranges
* (u
->is_dwarf64
? 8 : 4);
1740 if (offset
>= dwarf_sections
->size
[DEBUG_RNGLISTS
])
1742 error_callback (data
, "rnglists offset out of range", 0);
1746 rnglists_buf
.name
= ".debug_rnglists";
1747 rnglists_buf
.start
= dwarf_sections
->data
[DEBUG_RNGLISTS
];
1748 rnglists_buf
.buf
= dwarf_sections
->data
[DEBUG_RNGLISTS
] + offset
;
1749 rnglists_buf
.left
= dwarf_sections
->size
[DEBUG_RNGLISTS
] - offset
;
1750 rnglists_buf
.is_bigendian
= is_bigendian
;
1751 rnglists_buf
.error_callback
= error_callback
;
1752 rnglists_buf
.data
= data
;
1753 rnglists_buf
.reported_underflow
= 0;
1755 if (pcrange
->ranges_is_index
)
1757 offset
= read_offset (&rnglists_buf
, u
->is_dwarf64
);
1758 offset
+= u
->rnglists_base
;
1759 if (offset
>= dwarf_sections
->size
[DEBUG_RNGLISTS
])
1761 error_callback (data
, "rnglists index offset out of range", 0);
1764 rnglists_buf
.buf
= dwarf_sections
->data
[DEBUG_RNGLISTS
] + offset
;
1765 rnglists_buf
.left
= dwarf_sections
->size
[DEBUG_RNGLISTS
] - offset
;
1772 rle
= read_byte (&rnglists_buf
);
1773 if (rle
== DW_RLE_end_of_list
)
1777 case DW_RLE_base_addressx
:
1781 index
= read_uleb128 (&rnglists_buf
);
1782 if (!resolve_addr_index (dwarf_sections
, u
->addr_base
,
1783 u
->addrsize
, is_bigendian
, index
,
1784 error_callback
, data
, &base
))
1789 case DW_RLE_startx_endx
:
1795 index
= read_uleb128 (&rnglists_buf
);
1796 if (!resolve_addr_index (dwarf_sections
, u
->addr_base
,
1797 u
->addrsize
, is_bigendian
, index
,
1798 error_callback
, data
, &low
))
1800 index
= read_uleb128 (&rnglists_buf
);
1801 if (!resolve_addr_index (dwarf_sections
, u
->addr_base
,
1802 u
->addrsize
, is_bigendian
, index
,
1803 error_callback
, data
, &high
))
1805 if (!add_range (state
, rdata
, low
+ base_address
,
1806 high
+ base_address
, error_callback
, data
,
1812 case DW_RLE_startx_length
:
1818 index
= read_uleb128 (&rnglists_buf
);
1819 if (!resolve_addr_index (dwarf_sections
, u
->addr_base
,
1820 u
->addrsize
, is_bigendian
, index
,
1821 error_callback
, data
, &low
))
1823 length
= read_uleb128 (&rnglists_buf
);
1824 low
+= base_address
;
1825 if (!add_range (state
, rdata
, low
, low
+ length
,
1826 error_callback
, data
, vec
))
1831 case DW_RLE_offset_pair
:
1836 low
= read_uleb128 (&rnglists_buf
);
1837 high
= read_uleb128 (&rnglists_buf
);
1838 if (!add_range (state
, rdata
, low
+ base
+ base_address
,
1839 high
+ base
+ base_address
,
1840 error_callback
, data
, vec
))
1845 case DW_RLE_base_address
:
1846 base
= read_address (&rnglists_buf
, u
->addrsize
);
1849 case DW_RLE_start_end
:
1854 low
= read_address (&rnglists_buf
, u
->addrsize
);
1855 high
= read_address (&rnglists_buf
, u
->addrsize
);
1856 if (!add_range (state
, rdata
, low
+ base_address
,
1857 high
+ base_address
, error_callback
, data
,
1863 case DW_RLE_start_length
:
1868 low
= read_address (&rnglists_buf
, u
->addrsize
);
1869 length
= read_uleb128 (&rnglists_buf
);
1870 low
+= base_address
;
1871 if (!add_range (state
, rdata
, low
, low
+ length
,
1872 error_callback
, data
, vec
))
1878 dwarf_buf_error (&rnglists_buf
, "unrecognized DW_RLE value");
1883 if (rnglists_buf
.reported_underflow
)
1889 /* Call ADD_RANGE for each lowpc/highpc pair in PCRANGE. RDATA is
1890 passed to ADD_RANGE, and is either a struct unit * or a struct
1891 function *. VEC is the vector we are adding ranges to, and is
1892 either a struct unit_addrs_vector * or a struct function_vector *.
1893 Returns 1 on success, 0 on error. */
1896 add_ranges (struct backtrace_state
*state
,
1897 const struct dwarf_sections
*dwarf_sections
,
1898 uintptr_t base_address
, int is_bigendian
,
1899 struct unit
*u
, uint64_t base
, const struct pcrange
*pcrange
,
1900 int (*add_range
) (struct backtrace_state
*state
, void *rdata
,
1901 uint64_t lowpc
, uint64_t highpc
,
1902 backtrace_error_callback error_callback
,
1903 void *data
, void *vec
),
1905 backtrace_error_callback error_callback
, void *data
,
1908 if (pcrange
->have_lowpc
&& pcrange
->have_highpc
)
1909 return add_low_high_range (state
, dwarf_sections
, base_address
,
1910 is_bigendian
, u
, pcrange
, add_range
, rdata
,
1911 error_callback
, data
, vec
);
1913 if (!pcrange
->have_ranges
)
1915 /* Did not find any address ranges to add. */
1920 return add_ranges_from_ranges (state
, dwarf_sections
, base_address
,
1921 is_bigendian
, u
, base
, pcrange
, add_range
,
1922 rdata
, error_callback
, data
, vec
);
1924 return add_ranges_from_rnglists (state
, dwarf_sections
, base_address
,
1925 is_bigendian
, u
, base
, pcrange
, add_range
,
1926 rdata
, error_callback
, data
, vec
);
1929 /* Find the address range covered by a compilation unit, reading from
1930 UNIT_BUF and adding values to U. Returns 1 if all data could be
1931 read, 0 if there is some error. */
1934 find_address_ranges (struct backtrace_state
*state
, uintptr_t base_address
,
1935 struct dwarf_buf
*unit_buf
,
1936 const struct dwarf_sections
*dwarf_sections
,
1937 int is_bigendian
, struct dwarf_data
*altlink
,
1938 backtrace_error_callback error_callback
, void *data
,
1939 struct unit
*u
, struct unit_addrs_vector
*addrs
,
1940 enum dwarf_tag
*unit_tag
)
1942 while (unit_buf
->left
> 0)
1945 const struct abbrev
*abbrev
;
1946 struct pcrange pcrange
;
1947 struct attr_val name_val
;
1949 struct attr_val comp_dir_val
;
1950 int have_comp_dir_val
;
1953 code
= read_uleb128 (unit_buf
);
1957 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
1961 if (unit_tag
!= NULL
)
1962 *unit_tag
= abbrev
->tag
;
1964 memset (&pcrange
, 0, sizeof pcrange
);
1965 memset (&name_val
, 0, sizeof name_val
);
1967 memset (&comp_dir_val
, 0, sizeof comp_dir_val
);
1968 have_comp_dir_val
= 0;
1969 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1971 struct attr_val val
;
1973 if (!read_attribute (abbrev
->attrs
[i
].form
, abbrev
->attrs
[i
].val
,
1974 unit_buf
, u
->is_dwarf64
, u
->version
,
1975 u
->addrsize
, dwarf_sections
, altlink
, &val
))
1978 switch (abbrev
->attrs
[i
].name
)
1980 case DW_AT_low_pc
: case DW_AT_high_pc
: case DW_AT_ranges
:
1981 update_pcrange (&abbrev
->attrs
[i
], &val
, &pcrange
);
1984 case DW_AT_stmt_list
:
1985 if (abbrev
->tag
== DW_TAG_compile_unit
1986 && (val
.encoding
== ATTR_VAL_UINT
1987 || val
.encoding
== ATTR_VAL_REF_SECTION
))
1988 u
->lineoff
= val
.u
.uint
;
1992 if (abbrev
->tag
== DW_TAG_compile_unit
)
1999 case DW_AT_comp_dir
:
2000 if (abbrev
->tag
== DW_TAG_compile_unit
)
2003 have_comp_dir_val
= 1;
2007 case DW_AT_str_offsets_base
:
2008 if (abbrev
->tag
== DW_TAG_compile_unit
2009 && val
.encoding
== ATTR_VAL_REF_SECTION
)
2010 u
->str_offsets_base
= val
.u
.uint
;
2013 case DW_AT_addr_base
:
2014 if (abbrev
->tag
== DW_TAG_compile_unit
2015 && val
.encoding
== ATTR_VAL_REF_SECTION
)
2016 u
->addr_base
= val
.u
.uint
;
2019 case DW_AT_rnglists_base
:
2020 if (abbrev
->tag
== DW_TAG_compile_unit
2021 && val
.encoding
== ATTR_VAL_REF_SECTION
)
2022 u
->rnglists_base
= val
.u
.uint
;
2030 // Resolve strings after we're sure that we have seen
2031 // DW_AT_str_offsets_base.
2034 if (!resolve_string (dwarf_sections
, u
->is_dwarf64
, is_bigendian
,
2035 u
->str_offsets_base
, &name_val
,
2036 error_callback
, data
, &u
->filename
))
2039 if (have_comp_dir_val
)
2041 if (!resolve_string (dwarf_sections
, u
->is_dwarf64
, is_bigendian
,
2042 u
->str_offsets_base
, &comp_dir_val
,
2043 error_callback
, data
, &u
->comp_dir
))
2047 if (abbrev
->tag
== DW_TAG_compile_unit
2048 || abbrev
->tag
== DW_TAG_subprogram
)
2050 if (!add_ranges (state
, dwarf_sections
, base_address
,
2051 is_bigendian
, u
, pcrange
.lowpc
, &pcrange
,
2052 add_unit_addr
, (void *) u
, error_callback
, data
,
2056 /* If we found the PC range in the DW_TAG_compile_unit, we
2058 if (abbrev
->tag
== DW_TAG_compile_unit
2059 && (pcrange
.have_ranges
2060 || (pcrange
.have_lowpc
&& pcrange
.have_highpc
)))
2064 if (abbrev
->has_children
)
2066 if (!find_address_ranges (state
, base_address
, unit_buf
,
2067 dwarf_sections
, is_bigendian
, altlink
,
2068 error_callback
, data
, u
, addrs
, NULL
))
2076 /* Build a mapping from address ranges to the compilation units where
2077 the line number information for that range can be found. Returns 1
2078 on success, 0 on failure. */
2081 build_address_map (struct backtrace_state
*state
, uintptr_t base_address
,
2082 const struct dwarf_sections
*dwarf_sections
,
2083 int is_bigendian
, struct dwarf_data
*altlink
,
2084 backtrace_error_callback error_callback
, void *data
,
2085 struct unit_addrs_vector
*addrs
,
2086 struct unit_vector
*unit_vec
)
2088 struct dwarf_buf info
;
2089 struct backtrace_vector units
;
2093 size_t unit_offset
= 0;
2095 memset (&addrs
->vec
, 0, sizeof addrs
->vec
);
2096 memset (&unit_vec
->vec
, 0, sizeof unit_vec
->vec
);
2098 unit_vec
->count
= 0;
2100 /* Read through the .debug_info section. FIXME: Should we use the
2101 .debug_aranges section? gdb and addr2line don't use it, but I'm
2104 info
.name
= ".debug_info";
2105 info
.start
= dwarf_sections
->data
[DEBUG_INFO
];
2106 info
.buf
= info
.start
;
2107 info
.left
= dwarf_sections
->size
[DEBUG_INFO
];
2108 info
.is_bigendian
= is_bigendian
;
2109 info
.error_callback
= error_callback
;
2111 info
.reported_underflow
= 0;
2113 memset (&units
, 0, sizeof units
);
2116 while (info
.left
> 0)
2118 const unsigned char *unit_data_start
;
2121 struct dwarf_buf unit_buf
;
2124 uint64_t abbrev_offset
;
2127 enum dwarf_tag unit_tag
;
2129 if (info
.reported_underflow
)
2132 unit_data_start
= info
.buf
;
2134 len
= read_initial_length (&info
, &is_dwarf64
);
2136 unit_buf
.left
= len
;
2138 if (!advance (&info
, len
))
2141 version
= read_uint16 (&unit_buf
);
2142 if (version
< 2 || version
> 5)
2144 dwarf_buf_error (&unit_buf
, "unrecognized DWARF version");
2152 unit_type
= read_byte (&unit_buf
);
2153 if (unit_type
== DW_UT_type
|| unit_type
== DW_UT_split_type
)
2155 /* This unit doesn't have anything we need. */
2160 pu
= ((struct unit
**)
2161 backtrace_vector_grow (state
, sizeof (struct unit
*),
2162 error_callback
, data
, &units
));
2166 u
= ((struct unit
*)
2167 backtrace_alloc (state
, sizeof *u
, error_callback
, data
));
2175 addrsize
= 0; /* Set below. */
2177 addrsize
= read_byte (&unit_buf
);
2179 memset (&u
->abbrevs
, 0, sizeof u
->abbrevs
);
2180 abbrev_offset
= read_offset (&unit_buf
, is_dwarf64
);
2181 if (!read_abbrevs (state
, abbrev_offset
,
2182 dwarf_sections
->data
[DEBUG_ABBREV
],
2183 dwarf_sections
->size
[DEBUG_ABBREV
],
2184 is_bigendian
, error_callback
, data
, &u
->abbrevs
))
2188 addrsize
= read_byte (&unit_buf
);
2194 case DW_UT_compile
: case DW_UT_partial
:
2196 case DW_UT_skeleton
: case DW_UT_split_compile
:
2197 read_uint64 (&unit_buf
); /* dwo_id */
2203 u
->low_offset
= unit_offset
;
2204 unit_offset
+= len
+ (is_dwarf64
? 12 : 4);
2205 u
->high_offset
= unit_offset
;
2206 u
->unit_data
= unit_buf
.buf
;
2207 u
->unit_data_len
= unit_buf
.left
;
2208 u
->unit_data_offset
= unit_buf
.buf
- unit_data_start
;
2209 u
->version
= version
;
2210 u
->is_dwarf64
= is_dwarf64
;
2211 u
->addrsize
= addrsize
;
2214 u
->abs_filename
= NULL
;
2217 /* The actual line number mappings will be read as needed. */
2220 u
->function_addrs
= NULL
;
2221 u
->function_addrs_count
= 0;
2223 if (!find_address_ranges (state
, base_address
, &unit_buf
, dwarf_sections
,
2224 is_bigendian
, altlink
, error_callback
, data
,
2225 u
, addrs
, &unit_tag
))
2228 if (unit_buf
.reported_underflow
)
2231 if (info
.reported_underflow
)
2234 unit_vec
->vec
= units
;
2235 unit_vec
->count
= units_count
;
2239 if (units_count
> 0)
2241 pu
= (struct unit
**) units
.base
;
2242 for (i
= 0; i
< units_count
; i
++)
2244 free_abbrevs (state
, &pu
[i
]->abbrevs
, error_callback
, data
);
2245 backtrace_free (state
, pu
[i
], sizeof **pu
, error_callback
, data
);
2247 backtrace_vector_free (state
, &units
, error_callback
, data
);
2249 if (addrs
->count
> 0)
2251 backtrace_vector_free (state
, &addrs
->vec
, error_callback
, data
);
2257 /* Add a new mapping to the vector of line mappings that we are
2258 building. Returns 1 on success, 0 on failure. */
2261 add_line (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2262 uintptr_t pc
, const char *filename
, int lineno
,
2263 backtrace_error_callback error_callback
, void *data
,
2264 struct line_vector
*vec
)
2268 /* If we are adding the same mapping, ignore it. This can happen
2269 when using discriminators. */
2272 ln
= (struct line
*) vec
->vec
.base
+ (vec
->count
- 1);
2273 if (pc
== ln
->pc
&& filename
== ln
->filename
&& lineno
== ln
->lineno
)
2277 ln
= ((struct line
*)
2278 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
2283 /* Add in the base address here, so that we can look up the PC
2285 ln
->pc
= pc
+ ddata
->base_address
;
2287 ln
->filename
= filename
;
2288 ln
->lineno
= lineno
;
2289 ln
->idx
= vec
->count
;
2296 /* Free the line header information. */
2299 free_line_header (struct backtrace_state
*state
, struct line_header
*hdr
,
2300 backtrace_error_callback error_callback
, void *data
)
2302 if (hdr
->dirs_count
!= 0)
2303 backtrace_free (state
, hdr
->dirs
, hdr
->dirs_count
* sizeof (const char *),
2304 error_callback
, data
);
2305 backtrace_free (state
, hdr
->filenames
,
2306 hdr
->filenames_count
* sizeof (char *),
2307 error_callback
, data
);
2310 /* Read the directories and file names for a line header for version
2311 2, setting fields in HDR. Return 1 on success, 0 on failure. */
2314 read_v2_paths (struct backtrace_state
*state
, struct unit
*u
,
2315 struct dwarf_buf
*hdr_buf
, struct line_header
*hdr
)
2317 const unsigned char *p
;
2318 const unsigned char *pend
;
2321 /* Count the number of directory entries. */
2322 hdr
->dirs_count
= 0;
2324 pend
= p
+ hdr_buf
->left
;
2325 while (p
< pend
&& *p
!= '\0')
2327 p
+= strnlen((const char *) p
, pend
- p
) + 1;
2332 if (hdr
->dirs_count
!= 0)
2334 hdr
->dirs
= ((const char **)
2335 backtrace_alloc (state
,
2336 hdr
->dirs_count
* sizeof (const char *),
2337 hdr_buf
->error_callback
,
2339 if (hdr
->dirs
== NULL
)
2344 while (*hdr_buf
->buf
!= '\0')
2346 if (hdr_buf
->reported_underflow
)
2349 hdr
->dirs
[i
] = read_string (hdr_buf
);
2350 if (hdr
->dirs
[i
] == NULL
)
2354 if (!advance (hdr_buf
, 1))
2357 /* Count the number of file entries. */
2358 hdr
->filenames_count
= 0;
2360 pend
= p
+ hdr_buf
->left
;
2361 while (p
< pend
&& *p
!= '\0')
2363 p
+= strnlen ((const char *) p
, pend
- p
) + 1;
2364 p
+= leb128_len (p
);
2365 p
+= leb128_len (p
);
2366 p
+= leb128_len (p
);
2367 ++hdr
->filenames_count
;
2370 hdr
->filenames
= ((const char **)
2371 backtrace_alloc (state
,
2372 hdr
->filenames_count
* sizeof (char *),
2373 hdr_buf
->error_callback
,
2375 if (hdr
->filenames
== NULL
)
2378 while (*hdr_buf
->buf
!= '\0')
2380 const char *filename
;
2383 if (hdr_buf
->reported_underflow
)
2386 filename
= read_string (hdr_buf
);
2387 if (filename
== NULL
)
2389 dir_index
= read_uleb128 (hdr_buf
);
2390 if (IS_ABSOLUTE_PATH (filename
)
2391 || (dir_index
== 0 && u
->comp_dir
== NULL
))
2392 hdr
->filenames
[i
] = filename
;
2397 size_t filename_len
;
2402 else if (dir_index
- 1 < hdr
->dirs_count
)
2403 dir
= hdr
->dirs
[dir_index
- 1];
2406 dwarf_buf_error (hdr_buf
,
2407 ("invalid directory index in "
2408 "line number program header"));
2411 dir_len
= strlen (dir
);
2412 filename_len
= strlen (filename
);
2413 s
= ((char *) backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
2414 hdr_buf
->error_callback
,
2418 memcpy (s
, dir
, dir_len
);
2419 /* FIXME: If we are on a DOS-based file system, and the
2420 directory or the file name use backslashes, then we
2421 should use a backslash here. */
2423 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
2424 hdr
->filenames
[i
] = s
;
2427 /* Ignore the modification time and size. */
2428 read_uleb128 (hdr_buf
);
2429 read_uleb128 (hdr_buf
);
2437 /* Read a single version 5 LNCT entry for a directory or file name in a
2438 line header. Sets *STRING to the resulting name, ignoring other
2439 data. Return 1 on success, 0 on failure. */
2442 read_lnct (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2443 struct unit
*u
, struct dwarf_buf
*hdr_buf
,
2444 const struct line_header
*hdr
, size_t formats_count
,
2445 const struct line_header_format
*formats
, const char **string
)
2453 for (i
= 0; i
< formats_count
; i
++)
2455 struct attr_val val
;
2457 if (!read_attribute (formats
[i
].form
, 0, hdr_buf
, u
->is_dwarf64
,
2458 u
->version
, hdr
->addrsize
, &ddata
->dwarf_sections
,
2459 ddata
->altlink
, &val
))
2461 switch (formats
[i
].lnct
)
2464 if (!resolve_string (&ddata
->dwarf_sections
, u
->is_dwarf64
,
2465 ddata
->is_bigendian
, u
->str_offsets_base
,
2466 &val
, hdr_buf
->error_callback
, hdr_buf
->data
,
2470 case DW_LNCT_directory_index
:
2471 if (val
.encoding
== ATTR_VAL_UINT
)
2473 if (val
.u
.uint
>= hdr
->dirs_count
)
2475 dwarf_buf_error (hdr_buf
,
2476 ("invalid directory index in "
2477 "line number program header"));
2480 dir
= hdr
->dirs
[val
.u
.uint
];
2484 /* We don't care about timestamps or sizes or hashes. */
2491 dwarf_buf_error (hdr_buf
,
2492 "missing file name in line number program header");
2504 dir_len
= strlen (dir
);
2505 path_len
= strlen (path
);
2506 s
= (char *) backtrace_alloc (state
, dir_len
+ path_len
+ 2,
2507 hdr_buf
->error_callback
, hdr_buf
->data
);
2510 memcpy (s
, dir
, dir_len
);
2511 /* FIXME: If we are on a DOS-based file system, and the
2512 directory or the path name use backslashes, then we should
2513 use a backslash here. */
2515 memcpy (s
+ dir_len
+ 1, path
, path_len
+ 1);
2522 /* Read a set of DWARF 5 line header format entries, setting *PCOUNT
2523 and *PPATHS. Return 1 on success, 0 on failure. */
2526 read_line_header_format_entries (struct backtrace_state
*state
,
2527 struct dwarf_data
*ddata
,
2529 struct dwarf_buf
*hdr_buf
,
2530 struct line_header
*hdr
,
2532 const char ***ppaths
)
2534 size_t formats_count
;
2535 struct line_header_format
*formats
;
2541 formats_count
= read_byte (hdr_buf
);
2542 if (formats_count
== 0)
2546 formats
= ((struct line_header_format
*)
2547 backtrace_alloc (state
,
2549 * sizeof (struct line_header_format
)),
2550 hdr_buf
->error_callback
,
2552 if (formats
== NULL
)
2555 for (i
= 0; i
< formats_count
; i
++)
2557 formats
[i
].lnct
= (int) read_uleb128(hdr_buf
);
2558 formats
[i
].form
= (enum dwarf_form
) read_uleb128 (hdr_buf
);
2562 paths_count
= read_uleb128 (hdr_buf
);
2563 if (paths_count
== 0)
2571 paths
= ((const char **)
2572 backtrace_alloc (state
, paths_count
* sizeof (const char *),
2573 hdr_buf
->error_callback
, hdr_buf
->data
));
2579 for (i
= 0; i
< paths_count
; i
++)
2581 if (!read_lnct (state
, ddata
, u
, hdr_buf
, hdr
, formats_count
,
2582 formats
, &paths
[i
]))
2584 backtrace_free (state
, paths
,
2585 paths_count
* sizeof (const char *),
2586 hdr_buf
->error_callback
, hdr_buf
->data
);
2592 *pcount
= paths_count
;
2598 if (formats
!= NULL
)
2599 backtrace_free (state
, formats
,
2600 formats_count
* sizeof (struct line_header_format
),
2601 hdr_buf
->error_callback
, hdr_buf
->data
);
2606 /* Read the line header. Return 1 on success, 0 on failure. */
2609 read_line_header (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2610 struct unit
*u
, int is_dwarf64
, struct dwarf_buf
*line_buf
,
2611 struct line_header
*hdr
)
2614 struct dwarf_buf hdr_buf
;
2616 hdr
->version
= read_uint16 (line_buf
);
2617 if (hdr
->version
< 2 || hdr
->version
> 5)
2619 dwarf_buf_error (line_buf
, "unsupported line number version");
2623 if (hdr
->version
< 5)
2624 hdr
->addrsize
= u
->addrsize
;
2627 hdr
->addrsize
= read_byte (line_buf
);
2628 /* We could support a non-zero segment_selector_size but I doubt
2629 we'll ever see it. */
2630 if (read_byte (line_buf
) != 0)
2632 dwarf_buf_error (line_buf
,
2633 "non-zero segment_selector_size not supported");
2638 hdrlen
= read_offset (line_buf
, is_dwarf64
);
2640 hdr_buf
= *line_buf
;
2641 hdr_buf
.left
= hdrlen
;
2643 if (!advance (line_buf
, hdrlen
))
2646 hdr
->min_insn_len
= read_byte (&hdr_buf
);
2647 if (hdr
->version
< 4)
2648 hdr
->max_ops_per_insn
= 1;
2650 hdr
->max_ops_per_insn
= read_byte (&hdr_buf
);
2652 /* We don't care about default_is_stmt. */
2653 read_byte (&hdr_buf
);
2655 hdr
->line_base
= read_sbyte (&hdr_buf
);
2656 hdr
->line_range
= read_byte (&hdr_buf
);
2658 hdr
->opcode_base
= read_byte (&hdr_buf
);
2659 hdr
->opcode_lengths
= hdr_buf
.buf
;
2660 if (!advance (&hdr_buf
, hdr
->opcode_base
- 1))
2663 if (hdr
->version
< 5)
2665 if (!read_v2_paths (state
, u
, &hdr_buf
, hdr
))
2670 if (!read_line_header_format_entries (state
, ddata
, u
, &hdr_buf
, hdr
,
2674 if (!read_line_header_format_entries (state
, ddata
, u
, &hdr_buf
, hdr
,
2675 &hdr
->filenames_count
,
2680 if (hdr_buf
.reported_underflow
)
2686 /* Read the line program, adding line mappings to VEC. Return 1 on
2687 success, 0 on failure. */
2690 read_line_program (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2691 struct unit
*u
, const struct line_header
*hdr
,
2692 struct dwarf_buf
*line_buf
, struct line_vector
*vec
)
2695 unsigned int op_index
;
2696 const char *reset_filename
;
2697 const char *filename
;
2702 if (hdr
->filenames_count
> 0)
2703 reset_filename
= hdr
->filenames
[0];
2705 reset_filename
= "";
2706 filename
= reset_filename
;
2708 while (line_buf
->left
> 0)
2712 op
= read_byte (line_buf
);
2713 if (op
>= hdr
->opcode_base
)
2715 unsigned int advance
;
2717 /* Special opcode. */
2718 op
-= hdr
->opcode_base
;
2719 advance
= op
/ hdr
->line_range
;
2720 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
2721 / hdr
->max_ops_per_insn
);
2722 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
2723 lineno
+= hdr
->line_base
+ (int) (op
% hdr
->line_range
);
2724 add_line (state
, ddata
, address
, filename
, lineno
,
2725 line_buf
->error_callback
, line_buf
->data
, vec
);
2727 else if (op
== DW_LNS_extended_op
)
2731 len
= read_uleb128 (line_buf
);
2732 op
= read_byte (line_buf
);
2735 case DW_LNE_end_sequence
:
2736 /* FIXME: Should we mark the high PC here? It seems
2737 that we already have that information from the
2738 compilation unit. */
2741 filename
= reset_filename
;
2744 case DW_LNE_set_address
:
2745 address
= read_address (line_buf
, hdr
->addrsize
);
2747 case DW_LNE_define_file
:
2750 unsigned int dir_index
;
2752 f
= read_string (line_buf
);
2755 dir_index
= read_uleb128 (line_buf
);
2756 /* Ignore that time and length. */
2757 read_uleb128 (line_buf
);
2758 read_uleb128 (line_buf
);
2759 if (IS_ABSOLUTE_PATH (f
))
2768 if (dir_index
== 0 && hdr
->version
< 5)
2770 else if (dir_index
- 1 < hdr
->dirs_count
)
2771 dir
= hdr
->dirs
[dir_index
- 1];
2774 dwarf_buf_error (line_buf
,
2775 ("invalid directory index "
2776 "in line number program"));
2779 dir_len
= strlen (dir
);
2782 backtrace_alloc (state
, dir_len
+ f_len
+ 2,
2783 line_buf
->error_callback
,
2787 memcpy (p
, dir
, dir_len
);
2788 /* FIXME: If we are on a DOS-based file system,
2789 and the directory or the file name use
2790 backslashes, then we should use a backslash
2793 memcpy (p
+ dir_len
+ 1, f
, f_len
+ 1);
2798 case DW_LNE_set_discriminator
:
2799 /* We don't care about discriminators. */
2800 read_uleb128 (line_buf
);
2803 if (!advance (line_buf
, len
- 1))
2813 add_line (state
, ddata
, address
, filename
, lineno
,
2814 line_buf
->error_callback
, line_buf
->data
, vec
);
2816 case DW_LNS_advance_pc
:
2820 advance
= read_uleb128 (line_buf
);
2821 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
2822 / hdr
->max_ops_per_insn
);
2823 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
2826 case DW_LNS_advance_line
:
2827 lineno
+= (int) read_sleb128 (line_buf
);
2829 case DW_LNS_set_file
:
2833 fileno
= read_uleb128 (line_buf
);
2838 if (fileno
- 1 >= hdr
->filenames_count
)
2840 dwarf_buf_error (line_buf
,
2841 ("invalid file number in "
2842 "line number program"));
2845 filename
= hdr
->filenames
[fileno
- 1];
2849 case DW_LNS_set_column
:
2850 read_uleb128 (line_buf
);
2852 case DW_LNS_negate_stmt
:
2854 case DW_LNS_set_basic_block
:
2856 case DW_LNS_const_add_pc
:
2858 unsigned int advance
;
2860 op
= 255 - hdr
->opcode_base
;
2861 advance
= op
/ hdr
->line_range
;
2862 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
2863 / hdr
->max_ops_per_insn
);
2864 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
2867 case DW_LNS_fixed_advance_pc
:
2868 address
+= read_uint16 (line_buf
);
2871 case DW_LNS_set_prologue_end
:
2873 case DW_LNS_set_epilogue_begin
:
2875 case DW_LNS_set_isa
:
2876 read_uleb128 (line_buf
);
2882 for (i
= hdr
->opcode_lengths
[op
- 1]; i
> 0; --i
)
2883 read_uleb128 (line_buf
);
2893 /* Read the line number information for a compilation unit. Returns 1
2894 on success, 0 on failure. */
2897 read_line_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2898 backtrace_error_callback error_callback
, void *data
,
2899 struct unit
*u
, struct line_header
*hdr
, struct line
**lines
,
2900 size_t *lines_count
)
2902 struct line_vector vec
;
2903 struct dwarf_buf line_buf
;
2908 memset (&vec
.vec
, 0, sizeof vec
.vec
);
2911 memset (hdr
, 0, sizeof *hdr
);
2913 if (u
->lineoff
!= (off_t
) (size_t) u
->lineoff
2914 || (size_t) u
->lineoff
>= ddata
->dwarf_sections
.size
[DEBUG_LINE
])
2916 error_callback (data
, "unit line offset out of range", 0);
2920 line_buf
.name
= ".debug_line";
2921 line_buf
.start
= ddata
->dwarf_sections
.data
[DEBUG_LINE
];
2922 line_buf
.buf
= ddata
->dwarf_sections
.data
[DEBUG_LINE
] + u
->lineoff
;
2923 line_buf
.left
= ddata
->dwarf_sections
.size
[DEBUG_LINE
] - u
->lineoff
;
2924 line_buf
.is_bigendian
= ddata
->is_bigendian
;
2925 line_buf
.error_callback
= error_callback
;
2926 line_buf
.data
= data
;
2927 line_buf
.reported_underflow
= 0;
2929 len
= read_initial_length (&line_buf
, &is_dwarf64
);
2930 line_buf
.left
= len
;
2932 if (!read_line_header (state
, ddata
, u
, is_dwarf64
, &line_buf
, hdr
))
2935 if (!read_line_program (state
, ddata
, u
, hdr
, &line_buf
, &vec
))
2938 if (line_buf
.reported_underflow
)
2943 /* This is not a failure in the sense of a generating an error,
2944 but it is a failure in that sense that we have no useful
2949 /* Allocate one extra entry at the end. */
2950 ln
= ((struct line
*)
2951 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
2955 ln
->pc
= (uintptr_t) -1;
2956 ln
->filename
= NULL
;
2960 if (!backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
))
2963 ln
= (struct line
*) vec
.vec
.base
;
2964 backtrace_qsort (ln
, vec
.count
, sizeof (struct line
), line_compare
);
2967 *lines_count
= vec
.count
;
2972 backtrace_vector_free (state
, &vec
.vec
, error_callback
, data
);
2973 free_line_header (state
, hdr
, error_callback
, data
);
2974 *lines
= (struct line
*) (uintptr_t) -1;
2979 static const char *read_referenced_name (struct dwarf_data
*, struct unit
*,
2980 uint64_t, backtrace_error_callback
,
2983 /* Read the name of a function from a DIE referenced by ATTR with VAL. */
2986 read_referenced_name_from_attr (struct dwarf_data
*ddata
, struct unit
*u
,
2987 struct attr
*attr
, struct attr_val
*val
,
2988 backtrace_error_callback error_callback
,
2993 case DW_AT_abstract_origin
:
2994 case DW_AT_specification
:
3000 if (attr
->form
== DW_FORM_ref_sig8
)
3003 if (val
->encoding
== ATTR_VAL_REF_INFO
)
3006 = find_unit (ddata
->units
, ddata
->units_count
,
3011 uint64_t offset
= val
->u
.uint
- unit
->low_offset
;
3012 return read_referenced_name (ddata
, unit
, offset
, error_callback
, data
);
3015 if (val
->encoding
== ATTR_VAL_UINT
3016 || val
->encoding
== ATTR_VAL_REF_UNIT
)
3017 return read_referenced_name (ddata
, u
, val
->u
.uint
, error_callback
, data
);
3019 if (val
->encoding
== ATTR_VAL_REF_ALT_INFO
)
3021 struct unit
*alt_unit
3022 = find_unit (ddata
->altlink
->units
, ddata
->altlink
->units_count
,
3024 if (alt_unit
== NULL
)
3027 uint64_t offset
= val
->u
.uint
- alt_unit
->low_offset
;
3028 return read_referenced_name (ddata
->altlink
, alt_unit
, offset
,
3029 error_callback
, data
);
3035 /* Read the name of a function from a DIE referenced by a
3036 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
3037 the same compilation unit. */
3040 read_referenced_name (struct dwarf_data
*ddata
, struct unit
*u
,
3041 uint64_t offset
, backtrace_error_callback error_callback
,
3044 struct dwarf_buf unit_buf
;
3046 const struct abbrev
*abbrev
;
3050 /* OFFSET is from the start of the data for this compilation unit.
3051 U->unit_data is the data, but it starts U->unit_data_offset bytes
3052 from the beginning. */
3054 if (offset
< u
->unit_data_offset
3055 || offset
- u
->unit_data_offset
>= u
->unit_data_len
)
3057 error_callback (data
,
3058 "abstract origin or specification out of range",
3063 offset
-= u
->unit_data_offset
;
3065 unit_buf
.name
= ".debug_info";
3066 unit_buf
.start
= ddata
->dwarf_sections
.data
[DEBUG_INFO
];
3067 unit_buf
.buf
= u
->unit_data
+ offset
;
3068 unit_buf
.left
= u
->unit_data_len
- offset
;
3069 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
3070 unit_buf
.error_callback
= error_callback
;
3071 unit_buf
.data
= data
;
3072 unit_buf
.reported_underflow
= 0;
3074 code
= read_uleb128 (&unit_buf
);
3077 dwarf_buf_error (&unit_buf
, "invalid abstract origin or specification");
3081 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
3086 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3088 struct attr_val val
;
3090 if (!read_attribute (abbrev
->attrs
[i
].form
, abbrev
->attrs
[i
].val
,
3091 &unit_buf
, u
->is_dwarf64
, u
->version
, u
->addrsize
,
3092 &ddata
->dwarf_sections
, ddata
->altlink
, &val
))
3095 switch (abbrev
->attrs
[i
].name
)
3098 /* Third name preference: don't override. A name we found in some
3099 other way, will normally be more useful -- e.g., this name is
3100 normally not mangled. */
3103 if (!resolve_string (&ddata
->dwarf_sections
, u
->is_dwarf64
,
3104 ddata
->is_bigendian
, u
->str_offsets_base
,
3105 &val
, error_callback
, data
, &ret
))
3109 case DW_AT_linkage_name
:
3110 case DW_AT_MIPS_linkage_name
:
3111 /* First name preference: override all. */
3116 if (!resolve_string (&ddata
->dwarf_sections
, u
->is_dwarf64
,
3117 ddata
->is_bigendian
, u
->str_offsets_base
,
3118 &val
, error_callback
, data
, &s
))
3125 case DW_AT_specification
:
3126 /* Second name preference: override DW_AT_name, don't override
3127 DW_AT_linkage_name. */
3131 name
= read_referenced_name_from_attr (ddata
, u
, &abbrev
->attrs
[i
],
3132 &val
, error_callback
, data
);
3146 /* Add a range to a unit that maps to a function. This is called via
3147 add_ranges. Returns 1 on success, 0 on error. */
3150 add_function_range (struct backtrace_state
*state
, void *rdata
,
3151 uint64_t lowpc
, uint64_t highpc
,
3152 backtrace_error_callback error_callback
, void *data
,
3155 struct function
*function
= (struct function
*) rdata
;
3156 struct function_vector
*vec
= (struct function_vector
*) pvec
;
3157 struct function_addrs
*p
;
3161 p
= (struct function_addrs
*) vec
->vec
.base
+ (vec
->count
- 1);
3162 if ((lowpc
== p
->high
|| lowpc
== p
->high
+ 1)
3163 && function
== p
->function
)
3165 if (highpc
> p
->high
)
3171 p
= ((struct function_addrs
*)
3172 backtrace_vector_grow (state
, sizeof (struct function_addrs
),
3173 error_callback
, data
, &vec
->vec
));
3179 p
->function
= function
;
3186 /* Read one entry plus all its children. Add function addresses to
3187 VEC. Returns 1 on success, 0 on error. */
3190 read_function_entry (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
3191 struct unit
*u
, uint64_t base
, struct dwarf_buf
*unit_buf
,
3192 const struct line_header
*lhdr
,
3193 backtrace_error_callback error_callback
, void *data
,
3194 struct function_vector
*vec_function
,
3195 struct function_vector
*vec_inlined
)
3197 while (unit_buf
->left
> 0)
3200 const struct abbrev
*abbrev
;
3202 struct function
*function
;
3203 struct function_vector
*vec
;
3205 struct pcrange pcrange
;
3206 int have_linkage_name
;
3208 code
= read_uleb128 (unit_buf
);
3212 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
3216 is_function
= (abbrev
->tag
== DW_TAG_subprogram
3217 || abbrev
->tag
== DW_TAG_entry_point
3218 || abbrev
->tag
== DW_TAG_inlined_subroutine
);
3220 if (abbrev
->tag
== DW_TAG_inlined_subroutine
)
3228 function
= ((struct function
*)
3229 backtrace_alloc (state
, sizeof *function
,
3230 error_callback
, data
));
3231 if (function
== NULL
)
3233 memset (function
, 0, sizeof *function
);
3236 memset (&pcrange
, 0, sizeof pcrange
);
3237 have_linkage_name
= 0;
3238 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
3240 struct attr_val val
;
3242 if (!read_attribute (abbrev
->attrs
[i
].form
, abbrev
->attrs
[i
].val
,
3243 unit_buf
, u
->is_dwarf64
, u
->version
,
3244 u
->addrsize
, &ddata
->dwarf_sections
,
3245 ddata
->altlink
, &val
))
3248 /* The compile unit sets the base address for any address
3249 ranges in the function entries. */
3250 if (abbrev
->tag
== DW_TAG_compile_unit
3251 && abbrev
->attrs
[i
].name
== DW_AT_low_pc
)
3253 if (val
.encoding
== ATTR_VAL_ADDRESS
)
3255 else if (val
.encoding
== ATTR_VAL_ADDRESS_INDEX
)
3257 if (!resolve_addr_index (&ddata
->dwarf_sections
,
3258 u
->addr_base
, u
->addrsize
,
3259 ddata
->is_bigendian
, val
.u
.uint
,
3260 error_callback
, data
, &base
))
3267 switch (abbrev
->attrs
[i
].name
)
3269 case DW_AT_call_file
:
3270 if (val
.encoding
== ATTR_VAL_UINT
)
3272 if (val
.u
.uint
== 0)
3273 function
->caller_filename
= "";
3276 if (val
.u
.uint
- 1 >= lhdr
->filenames_count
)
3278 dwarf_buf_error (unit_buf
,
3279 ("invalid file number in "
3280 "DW_AT_call_file attribute"));
3283 function
->caller_filename
=
3284 lhdr
->filenames
[val
.u
.uint
- 1];
3289 case DW_AT_call_line
:
3290 if (val
.encoding
== ATTR_VAL_UINT
)
3291 function
->caller_lineno
= val
.u
.uint
;
3294 case DW_AT_abstract_origin
:
3295 case DW_AT_specification
:
3296 /* Second name preference: override DW_AT_name, don't override
3297 DW_AT_linkage_name. */
3298 if (have_linkage_name
)
3304 = read_referenced_name_from_attr (ddata
, u
,
3305 &abbrev
->attrs
[i
], &val
,
3306 error_callback
, data
);
3308 function
->name
= name
;
3313 /* Third name preference: don't override. */
3314 if (function
->name
!= NULL
)
3316 if (!resolve_string (&ddata
->dwarf_sections
, u
->is_dwarf64
,
3317 ddata
->is_bigendian
,
3318 u
->str_offsets_base
, &val
,
3319 error_callback
, data
, &function
->name
))
3323 case DW_AT_linkage_name
:
3324 case DW_AT_MIPS_linkage_name
:
3325 /* First name preference: override all. */
3330 if (!resolve_string (&ddata
->dwarf_sections
, u
->is_dwarf64
,
3331 ddata
->is_bigendian
,
3332 u
->str_offsets_base
, &val
,
3333 error_callback
, data
, &s
))
3338 have_linkage_name
= 1;
3343 case DW_AT_low_pc
: case DW_AT_high_pc
: case DW_AT_ranges
:
3344 update_pcrange (&abbrev
->attrs
[i
], &val
, &pcrange
);
3353 /* If we couldn't find a name for the function, we have no use
3355 if (is_function
&& function
->name
== NULL
)
3357 backtrace_free (state
, function
, sizeof *function
,
3358 error_callback
, data
);
3364 if (pcrange
.have_ranges
3365 || (pcrange
.have_lowpc
&& pcrange
.have_highpc
))
3367 if (!add_ranges (state
, &ddata
->dwarf_sections
,
3368 ddata
->base_address
, ddata
->is_bigendian
,
3369 u
, base
, &pcrange
, add_function_range
,
3370 (void *) function
, error_callback
, data
,
3376 backtrace_free (state
, function
, sizeof *function
,
3377 error_callback
, data
);
3382 if (abbrev
->has_children
)
3386 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
3387 error_callback
, data
, vec_function
,
3393 struct function_vector fvec
;
3395 /* Gather any information for inlined functions in
3398 memset (&fvec
, 0, sizeof fvec
);
3400 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
3401 error_callback
, data
, vec_function
,
3407 struct function_addrs
*faddrs
;
3409 if (!backtrace_vector_release (state
, &fvec
.vec
,
3410 error_callback
, data
))
3413 faddrs
= (struct function_addrs
*) fvec
.vec
.base
;
3414 backtrace_qsort (faddrs
, fvec
.count
,
3415 sizeof (struct function_addrs
),
3416 function_addrs_compare
);
3418 function
->function_addrs
= faddrs
;
3419 function
->function_addrs_count
= fvec
.count
;
3428 /* Read function name information for a compilation unit. We look
3429 through the whole unit looking for function tags. */
3432 read_function_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
3433 const struct line_header
*lhdr
,
3434 backtrace_error_callback error_callback
, void *data
,
3435 struct unit
*u
, struct function_vector
*fvec
,
3436 struct function_addrs
**ret_addrs
,
3437 size_t *ret_addrs_count
)
3439 struct function_vector lvec
;
3440 struct function_vector
*pfvec
;
3441 struct dwarf_buf unit_buf
;
3442 struct function_addrs
*addrs
;
3445 /* Use FVEC if it is not NULL. Otherwise use our own vector. */
3450 memset (&lvec
, 0, sizeof lvec
);
3454 unit_buf
.name
= ".debug_info";
3455 unit_buf
.start
= ddata
->dwarf_sections
.data
[DEBUG_INFO
];
3456 unit_buf
.buf
= u
->unit_data
;
3457 unit_buf
.left
= u
->unit_data_len
;
3458 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
3459 unit_buf
.error_callback
= error_callback
;
3460 unit_buf
.data
= data
;
3461 unit_buf
.reported_underflow
= 0;
3463 while (unit_buf
.left
> 0)
3465 if (!read_function_entry (state
, ddata
, u
, 0, &unit_buf
, lhdr
,
3466 error_callback
, data
, pfvec
, pfvec
))
3470 if (pfvec
->count
== 0)
3473 addrs_count
= pfvec
->count
;
3477 if (!backtrace_vector_release (state
, &lvec
.vec
, error_callback
, data
))
3479 addrs
= (struct function_addrs
*) pfvec
->vec
.base
;
3483 /* Finish this list of addresses, but leave the remaining space in
3484 the vector available for the next function unit. */
3485 addrs
= ((struct function_addrs
*)
3486 backtrace_vector_finish (state
, &fvec
->vec
,
3487 error_callback
, data
));
3493 backtrace_qsort (addrs
, addrs_count
, sizeof (struct function_addrs
),
3494 function_addrs_compare
);
3497 *ret_addrs_count
= addrs_count
;
3500 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
3501 information, and update FILENAME and LINENO for the caller.
3502 Returns whatever CALLBACK returns, or 0 to keep going. */
3505 report_inlined_functions (uintptr_t pc
, struct function
*function
,
3506 backtrace_full_callback callback
, void *data
,
3507 const char **filename
, int *lineno
)
3509 struct function_addrs
*function_addrs
;
3510 struct function
*inlined
;
3513 if (function
->function_addrs_count
== 0)
3516 function_addrs
= ((struct function_addrs
*)
3517 bsearch (&pc
, function
->function_addrs
,
3518 function
->function_addrs_count
,
3519 sizeof (struct function_addrs
),
3520 function_addrs_search
));
3521 if (function_addrs
== NULL
)
3524 while (((size_t) (function_addrs
- function
->function_addrs
) + 1
3525 < function
->function_addrs_count
)
3526 && pc
>= (function_addrs
+ 1)->low
3527 && pc
< (function_addrs
+ 1)->high
)
3530 /* We found an inlined call. */
3532 inlined
= function_addrs
->function
;
3534 /* Report any calls inlined into this one. */
3535 ret
= report_inlined_functions (pc
, inlined
, callback
, data
,
3540 /* Report this inlined call. */
3541 ret
= callback (data
, pc
, *filename
, *lineno
, inlined
->name
);
3545 /* Our caller will report the caller of the inlined function; tell
3546 it the appropriate filename and line number. */
3547 *filename
= inlined
->caller_filename
;
3548 *lineno
= inlined
->caller_lineno
;
3553 /* Look for a PC in the DWARF mapping for one module. On success,
3554 call CALLBACK and return whatever it returns. On error, call
3555 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
3559 dwarf_lookup_pc (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
3560 uintptr_t pc
, backtrace_full_callback callback
,
3561 backtrace_error_callback error_callback
, void *data
,
3564 struct unit_addrs
*entry
;
3569 struct function_addrs
*function_addrs
;
3570 struct function
*function
;
3571 const char *filename
;
3577 /* Find an address range that includes PC. */
3578 entry
= (ddata
->addrs_count
== 0
3580 : bsearch (&pc
, ddata
->addrs
, ddata
->addrs_count
,
3581 sizeof (struct unit_addrs
), unit_addrs_search
));
3589 /* If there are multiple ranges that contain PC, use the last one,
3590 in order to produce predictable results. If we assume that all
3591 ranges are properly nested, then the last range will be the
3593 while ((size_t) (entry
- ddata
->addrs
) + 1 < ddata
->addrs_count
3594 && pc
>= (entry
+ 1)->low
3595 && pc
< (entry
+ 1)->high
)
3598 /* We need the lines, lines_count, function_addrs,
3599 function_addrs_count fields of u. If they are not set, we need
3600 to set them. When running in threaded mode, we need to allow for
3601 the possibility that some other thread is setting them
3607 /* Skip units with no useful line number information by walking
3608 backward. Useless line number information is marked by setting
3610 while (entry
> ddata
->addrs
3611 && pc
>= (entry
- 1)->low
3612 && pc
< (entry
- 1)->high
)
3614 if (state
->threaded
)
3615 lines
= (struct line
*) backtrace_atomic_load_pointer (&u
->lines
);
3617 if (lines
!= (struct line
*) (uintptr_t) -1)
3626 if (state
->threaded
)
3627 lines
= backtrace_atomic_load_pointer (&u
->lines
);
3632 size_t function_addrs_count
;
3633 struct line_header lhdr
;
3636 /* We have never read the line information for this unit. Read
3639 function_addrs
= NULL
;
3640 function_addrs_count
= 0;
3641 if (read_line_info (state
, ddata
, error_callback
, data
, entry
->u
, &lhdr
,
3644 struct function_vector
*pfvec
;
3646 /* If not threaded, reuse DDATA->FVEC for better memory
3648 if (state
->threaded
)
3651 pfvec
= &ddata
->fvec
;
3652 read_function_info (state
, ddata
, &lhdr
, error_callback
, data
,
3653 entry
->u
, pfvec
, &function_addrs
,
3654 &function_addrs_count
);
3655 free_line_header (state
, &lhdr
, error_callback
, data
);
3659 /* Atomically store the information we just read into the unit.
3660 If another thread is simultaneously writing, it presumably
3661 read the same information, and we don't care which one we
3662 wind up with; we just leak the other one. We do have to
3663 write the lines field last, so that the acquire-loads above
3664 ensure that the other fields are set. */
3666 if (!state
->threaded
)
3668 u
->lines_count
= count
;
3669 u
->function_addrs
= function_addrs
;
3670 u
->function_addrs_count
= function_addrs_count
;
3675 backtrace_atomic_store_size_t (&u
->lines_count
, count
);
3676 backtrace_atomic_store_pointer (&u
->function_addrs
, function_addrs
);
3677 backtrace_atomic_store_size_t (&u
->function_addrs_count
,
3678 function_addrs_count
);
3679 backtrace_atomic_store_pointer (&u
->lines
, lines
);
3683 /* Now all fields of U have been initialized. */
3685 if (lines
== (struct line
*) (uintptr_t) -1)
3687 /* If reading the line number information failed in some way,
3688 try again to see if there is a better compilation unit for
3691 return dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
3693 return callback (data
, pc
, NULL
, 0, NULL
);
3696 /* Search for PC within this unit. */
3698 ln
= (struct line
*) bsearch (&pc
, lines
, entry
->u
->lines_count
,
3699 sizeof (struct line
), line_search
);
3702 /* The PC is between the low_pc and high_pc attributes of the
3703 compilation unit, but no entry in the line table covers it.
3704 This implies that the start of the compilation unit has no
3705 line number information. */
3707 if (entry
->u
->abs_filename
== NULL
)
3709 const char *filename
;
3711 filename
= entry
->u
->filename
;
3712 if (filename
!= NULL
3713 && !IS_ABSOLUTE_PATH (filename
)
3714 && entry
->u
->comp_dir
!= NULL
)
3716 size_t filename_len
;
3721 filename_len
= strlen (filename
);
3722 dir
= entry
->u
->comp_dir
;
3723 dir_len
= strlen (dir
);
3724 s
= (char *) backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
3725 error_callback
, data
);
3731 memcpy (s
, dir
, dir_len
);
3732 /* FIXME: Should use backslash if DOS file system. */
3734 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
3737 entry
->u
->abs_filename
= filename
;
3740 return callback (data
, pc
, entry
->u
->abs_filename
, 0, NULL
);
3743 /* Search for function name within this unit. */
3745 if (entry
->u
->function_addrs_count
== 0)
3746 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
3748 function_addrs
= ((struct function_addrs
*)
3749 bsearch (&pc
, entry
->u
->function_addrs
,
3750 entry
->u
->function_addrs_count
,
3751 sizeof (struct function_addrs
),
3752 function_addrs_search
));
3753 if (function_addrs
== NULL
)
3754 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
3756 /* If there are multiple function ranges that contain PC, use the
3757 last one, in order to produce predictable results. */
3759 while (((size_t) (function_addrs
- entry
->u
->function_addrs
+ 1)
3760 < entry
->u
->function_addrs_count
)
3761 && pc
>= (function_addrs
+ 1)->low
3762 && pc
< (function_addrs
+ 1)->high
)
3765 function
= function_addrs
->function
;
3767 filename
= ln
->filename
;
3768 lineno
= ln
->lineno
;
3770 ret
= report_inlined_functions (pc
, function
, callback
, data
,
3771 &filename
, &lineno
);
3775 return callback (data
, pc
, filename
, lineno
, function
->name
);
3779 /* Return the file/line information for a PC using the DWARF mapping
3780 we built earlier. */
3783 dwarf_fileline (struct backtrace_state
*state
, uintptr_t pc
,
3784 backtrace_full_callback callback
,
3785 backtrace_error_callback error_callback
, void *data
)
3787 struct dwarf_data
*ddata
;
3791 if (!state
->threaded
)
3793 for (ddata
= (struct dwarf_data
*) state
->fileline_data
;
3795 ddata
= ddata
->next
)
3797 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
3799 if (ret
!= 0 || found
)
3805 struct dwarf_data
**pp
;
3807 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
3810 ddata
= backtrace_atomic_load_pointer (pp
);
3814 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
3816 if (ret
!= 0 || found
)
3823 /* FIXME: See if any libraries have been dlopen'ed. */
3825 return callback (data
, pc
, NULL
, 0, NULL
);
3828 /* Initialize our data structures from the DWARF debug info for a
3829 file. Return NULL on failure. */
3831 static struct dwarf_data
*
3832 build_dwarf_data (struct backtrace_state
*state
,
3833 uintptr_t base_address
,
3834 const struct dwarf_sections
*dwarf_sections
,
3836 struct dwarf_data
*altlink
,
3837 backtrace_error_callback error_callback
,
3840 struct unit_addrs_vector addrs_vec
;
3841 struct unit_addrs
*addrs
;
3843 struct unit_vector units_vec
;
3844 struct unit
**units
;
3846 struct dwarf_data
*fdata
;
3848 if (!build_address_map (state
, base_address
, dwarf_sections
, is_bigendian
,
3849 altlink
, error_callback
, data
, &addrs_vec
,
3853 if (!backtrace_vector_release (state
, &addrs_vec
.vec
, error_callback
, data
))
3855 if (!backtrace_vector_release (state
, &units_vec
.vec
, error_callback
, data
))
3857 addrs
= (struct unit_addrs
*) addrs_vec
.vec
.base
;
3858 units
= (struct unit
**) units_vec
.vec
.base
;
3859 addrs_count
= addrs_vec
.count
;
3860 units_count
= units_vec
.count
;
3861 backtrace_qsort (addrs
, addrs_count
, sizeof (struct unit_addrs
),
3862 unit_addrs_compare
);
3863 /* No qsort for units required, already sorted. */
3865 fdata
= ((struct dwarf_data
*)
3866 backtrace_alloc (state
, sizeof (struct dwarf_data
),
3867 error_callback
, data
));
3872 fdata
->altlink
= altlink
;
3873 fdata
->base_address
= base_address
;
3874 fdata
->addrs
= addrs
;
3875 fdata
->addrs_count
= addrs_count
;
3876 fdata
->units
= units
;
3877 fdata
->units_count
= units_count
;
3878 fdata
->dwarf_sections
= *dwarf_sections
;
3879 fdata
->is_bigendian
= is_bigendian
;
3880 memset (&fdata
->fvec
, 0, sizeof fdata
->fvec
);
3885 /* Build our data structures from the DWARF sections for a module.
3886 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
3890 backtrace_dwarf_add (struct backtrace_state
*state
,
3891 uintptr_t base_address
,
3892 const struct dwarf_sections
*dwarf_sections
,
3894 struct dwarf_data
*fileline_altlink
,
3895 backtrace_error_callback error_callback
,
3896 void *data
, fileline
*fileline_fn
,
3897 struct dwarf_data
**fileline_entry
)
3899 struct dwarf_data
*fdata
;
3901 fdata
= build_dwarf_data (state
, base_address
, dwarf_sections
, is_bigendian
,
3902 fileline_altlink
, error_callback
, data
);
3906 if (fileline_entry
!= NULL
)
3907 *fileline_entry
= fdata
;
3909 if (!state
->threaded
)
3911 struct dwarf_data
**pp
;
3913 for (pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
3923 struct dwarf_data
**pp
;
3925 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
3929 struct dwarf_data
*p
;
3931 p
= backtrace_atomic_load_pointer (pp
);
3939 if (__sync_bool_compare_and_swap (pp
, NULL
, fdata
))
3944 *fileline_fn
= dwarf_fileline
;