1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012-2019 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
38 #include <sys/types.h>
41 #include "filenames.h"
43 #include "backtrace.h"
46 #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
48 /* If strnlen is not declared, provide our own version. */
51 xstrnlen (const char *s
, size_t maxlen
)
55 for (i
= 0; i
< maxlen
; ++i
)
61 #define strnlen xstrnlen
65 /* A buffer to read DWARF info. */
69 /* Buffer name for error messages. */
71 /* Start of the buffer. */
72 const unsigned char *start
;
73 /* Next byte to read. */
74 const unsigned char *buf
;
75 /* The number of bytes remaining. */
77 /* Whether the data is big-endian. */
79 /* Error callback routine. */
80 backtrace_error_callback error_callback
;
81 /* Data for error_callback. */
83 /* Non-zero if we've reported an underflow error. */
84 int reported_underflow
;
87 /* A single attribute in a DWARF abbreviation. */
91 /* The attribute name. */
92 enum dwarf_attribute name
;
93 /* The attribute form. */
97 /* A single DWARF abbreviation. */
101 /* The abbrev code--the number used to refer to the abbrev. */
105 /* Non-zero if this abbrev has child entries. */
107 /* The number of attributes. */
109 /* The attributes. */
113 /* The DWARF abbreviations for a compilation unit. This structure
114 only exists while reading the compilation unit. Most DWARF readers
115 seem to a hash table to map abbrev ID's to abbrev entries.
116 However, we primarily care about GCC, and GCC simply issues ID's in
117 numerical order starting at 1. So we simply keep a sorted vector,
118 and try to just look up the code. */
122 /* The number of abbrevs in the vector. */
124 /* The abbrevs, sorted by the code field. */
125 struct abbrev
*abbrevs
;
128 /* The different kinds of attribute values. */
130 enum attr_val_encoding
132 /* No attribute value. */
136 /* A unsigned integer. */
138 /* A sigd integer. */
142 /* An offset to other data in the containing unit. */
144 /* An offset to other data within the .dwarf_info section. */
146 /* An offset to other data within the alt .dwarf_info section. */
147 ATTR_VAL_REF_ALT_INFO
,
148 /* An offset to data in some other section. */
149 ATTR_VAL_REF_SECTION
,
150 /* A type signature. */
152 /* A block of data (not represented). */
154 /* An expression (not represented). */
158 /* An attribute value. */
162 /* How the value is stored in the field u. */
163 enum attr_val_encoding encoding
;
166 /* ATTR_VAL_ADDRESS, ATTR_VAL_UINT, ATTR_VAL_REF*. */
170 /* ATTR_VAL_STRING. */
172 /* ATTR_VAL_BLOCK not stored. */
176 /* The line number program header. */
180 /* The version of the line number information. */
182 /* The minimum instruction length. */
183 unsigned int min_insn_len
;
184 /* The maximum number of ops per instruction. */
185 unsigned int max_ops_per_insn
;
186 /* The line base for special opcodes. */
188 /* The line range for special opcodes. */
189 unsigned int line_range
;
190 /* The opcode base--the first special opcode. */
191 unsigned int opcode_base
;
192 /* Opcode lengths, indexed by opcode - 1. */
193 const unsigned char *opcode_lengths
;
194 /* The number of directory entries. */
196 /* The directory entries. */
198 /* The number of filenames. */
199 size_t filenames_count
;
201 const char **filenames
;
204 /* Map a single PC value to a file/line. We will keep a vector of
205 these sorted by PC value. Each file/line will be correct from the
206 PC up to the PC of the next entry if there is one. We allocate one
207 extra entry at the end so that we can use bsearch. */
213 /* File name. Many entries in the array are expected to point to
214 the same file name. */
215 const char *filename
;
218 /* Index of the object in the original array read from the DWARF
219 section, before it has been sorted. The index makes it possible
220 to use Quicksort and maintain stability. */
224 /* A growable vector of line number information. This is used while
225 reading the line numbers. */
229 /* Memory. This is an array of struct line. */
230 struct backtrace_vector vec
;
231 /* Number of valid mappings. */
235 /* A function described in the debug info. */
239 /* The name of the function. */
241 /* If this is an inlined function, the filename of the call
243 const char *caller_filename
;
244 /* If this is an inlined function, the line number of the call
247 /* Map PC ranges to inlined functions. */
248 struct function_addrs
*function_addrs
;
249 size_t function_addrs_count
;
252 /* An address range for a function. This maps a PC value to a
253 specific function. */
255 struct function_addrs
257 /* Range is LOW <= PC < HIGH. */
260 /* Function for this address range. */
261 struct function
*function
;
264 /* A growable vector of function address ranges. */
266 struct function_vector
268 /* Memory. This is an array of struct function_addrs. */
269 struct backtrace_vector vec
;
270 /* Number of address ranges present. */
274 /* A DWARF compilation unit. This only holds the information we need
275 to map a PC to a file and line. */
279 /* The first entry for this compilation unit. */
280 const unsigned char *unit_data
;
281 /* The length of the data for this compilation unit. */
282 size_t unit_data_len
;
283 /* The offset of UNIT_DATA from the start of the information for
284 this compilation unit. */
285 size_t unit_data_offset
;
286 /* Offset of the start of the compilation unit from the start of the
287 .debug_info section. */
289 /* Offset of the end of the compilation unit from the start of the
290 .debug_info section. */
294 /* Whether unit is DWARF64. */
298 /* Offset into line number information. */
300 /* Primary source file. */
301 const char *filename
;
302 /* Compilation command working directory. */
303 const char *comp_dir
;
304 /* Absolute file name, only set if needed. */
305 const char *abs_filename
;
306 /* The abbreviations for this unit. */
307 struct abbrevs abbrevs
;
309 /* The fields above this point are read in during initialization and
310 may be accessed freely. The fields below this point are read in
311 as needed, and therefore require care, as different threads may
312 try to initialize them simultaneously. */
314 /* PC to line number mapping. This is NULL if the values have not
315 been read. This is (struct line *) -1 if there was an error
316 reading the values. */
318 /* Number of entries in lines. */
320 /* PC ranges to function. */
321 struct function_addrs
*function_addrs
;
322 size_t function_addrs_count
;
325 /* An address range for a compilation unit. This maps a PC value to a
326 specific compilation unit. Note that we invert the representation
327 in DWARF: instead of listing the units and attaching a list of
328 ranges, we list the ranges and have each one point to the unit.
329 This lets us do a binary search to find the unit. */
333 /* Range is LOW <= PC < HIGH. */
336 /* Compilation unit for this address range. */
340 /* A growable vector of compilation unit address ranges. */
342 struct unit_addrs_vector
344 /* Memory. This is an array of struct unit_addrs. */
345 struct backtrace_vector vec
;
346 /* Number of address ranges present. */
350 /* A growable vector of compilation unit pointer. */
354 struct backtrace_vector vec
;
358 /* The information we need to map a PC to a file and line. */
362 /* The data for the next file we know about. */
363 struct dwarf_data
*next
;
364 /* The data for .gnu_debugaltlink. */
365 struct dwarf_data
*altlink
;
366 /* The base address for this file. */
367 uintptr_t base_address
;
368 /* A sorted list of address ranges. */
369 struct unit_addrs
*addrs
;
370 /* Number of address ranges in list. */
372 /* A sorted list of units. */
374 /* Number of units in the list. */
376 /* The unparsed .debug_info section. */
377 const unsigned char *dwarf_info
;
378 size_t dwarf_info_size
;
379 /* The unparsed .debug_line section. */
380 const unsigned char *dwarf_line
;
381 size_t dwarf_line_size
;
382 /* The unparsed .debug_ranges section. */
383 const unsigned char *dwarf_ranges
;
384 size_t dwarf_ranges_size
;
385 /* The unparsed .debug_str section. */
386 const unsigned char *dwarf_str
;
387 size_t dwarf_str_size
;
388 /* Whether the data is big-endian or not. */
390 /* A vector used for function addresses. We keep this here so that
391 we can grow the vector as we read more functions. */
392 struct function_vector fvec
;
395 /* Report an error for a DWARF buffer. */
398 dwarf_buf_error (struct dwarf_buf
*buf
, const char *msg
)
402 snprintf (b
, sizeof b
, "%s in %s at %d",
403 msg
, buf
->name
, (int) (buf
->buf
- buf
->start
));
404 buf
->error_callback (buf
->data
, b
, 0);
407 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
411 require (struct dwarf_buf
*buf
, size_t count
)
413 if (buf
->left
>= count
)
416 if (!buf
->reported_underflow
)
418 dwarf_buf_error (buf
, "DWARF underflow");
419 buf
->reported_underflow
= 1;
425 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
429 advance (struct dwarf_buf
*buf
, size_t count
)
431 if (!require (buf
, count
))
438 /* Read one zero-terminated string from BUF and advance past the string. */
441 read_string (struct dwarf_buf
*buf
)
443 const char *p
= (const char *)buf
->buf
;
444 size_t len
= strnlen (p
, buf
->left
);
446 /* - If len == left, we ran out of buffer before finding the zero terminator.
447 Generate an error by advancing len + 1.
448 - If len < left, advance by len + 1 to skip past the zero terminator. */
449 size_t count
= len
+ 1;
451 if (!advance (buf
, count
))
457 /* Read one byte from BUF and advance 1 byte. */
460 read_byte (struct dwarf_buf
*buf
)
462 const unsigned char *p
= buf
->buf
;
464 if (!advance (buf
, 1))
469 /* Read a signed char from BUF and advance 1 byte. */
472 read_sbyte (struct dwarf_buf
*buf
)
474 const unsigned char *p
= buf
->buf
;
476 if (!advance (buf
, 1))
478 return (*p
^ 0x80) - 0x80;
481 /* Read a uint16 from BUF and advance 2 bytes. */
484 read_uint16 (struct dwarf_buf
*buf
)
486 const unsigned char *p
= buf
->buf
;
488 if (!advance (buf
, 2))
490 if (buf
->is_bigendian
)
491 return ((uint16_t) p
[0] << 8) | (uint16_t) p
[1];
493 return ((uint16_t) p
[1] << 8) | (uint16_t) p
[0];
496 /* Read a uint32 from BUF and advance 4 bytes. */
499 read_uint32 (struct dwarf_buf
*buf
)
501 const unsigned char *p
= buf
->buf
;
503 if (!advance (buf
, 4))
505 if (buf
->is_bigendian
)
506 return (((uint32_t) p
[0] << 24) | ((uint32_t) p
[1] << 16)
507 | ((uint32_t) p
[2] << 8) | (uint32_t) p
[3]);
509 return (((uint32_t) p
[3] << 24) | ((uint32_t) p
[2] << 16)
510 | ((uint32_t) p
[1] << 8) | (uint32_t) p
[0]);
513 /* Read a uint64 from BUF and advance 8 bytes. */
516 read_uint64 (struct dwarf_buf
*buf
)
518 const unsigned char *p
= buf
->buf
;
520 if (!advance (buf
, 8))
522 if (buf
->is_bigendian
)
523 return (((uint64_t) p
[0] << 56) | ((uint64_t) p
[1] << 48)
524 | ((uint64_t) p
[2] << 40) | ((uint64_t) p
[3] << 32)
525 | ((uint64_t) p
[4] << 24) | ((uint64_t) p
[5] << 16)
526 | ((uint64_t) p
[6] << 8) | (uint64_t) p
[7]);
528 return (((uint64_t) p
[7] << 56) | ((uint64_t) p
[6] << 48)
529 | ((uint64_t) p
[5] << 40) | ((uint64_t) p
[4] << 32)
530 | ((uint64_t) p
[3] << 24) | ((uint64_t) p
[2] << 16)
531 | ((uint64_t) p
[1] << 8) | (uint64_t) p
[0]);
534 /* Read an offset from BUF and advance the appropriate number of
538 read_offset (struct dwarf_buf
*buf
, int is_dwarf64
)
541 return read_uint64 (buf
);
543 return read_uint32 (buf
);
546 /* Read an address from BUF and advance the appropriate number of
550 read_address (struct dwarf_buf
*buf
, int addrsize
)
555 return read_byte (buf
);
557 return read_uint16 (buf
);
559 return read_uint32 (buf
);
561 return read_uint64 (buf
);
563 dwarf_buf_error (buf
, "unrecognized address size");
568 /* Return whether a value is the highest possible address, given the
572 is_highest_address (uint64_t address
, int addrsize
)
577 return address
== (unsigned char) -1;
579 return address
== (uint16_t) -1;
581 return address
== (uint32_t) -1;
583 return address
== (uint64_t) -1;
589 /* Read an unsigned LEB128 number. */
592 read_uleb128 (struct dwarf_buf
*buf
)
604 const unsigned char *p
;
607 if (!advance (buf
, 1))
611 ret
|= ((uint64_t) (b
& 0x7f)) << shift
;
614 dwarf_buf_error (buf
, "LEB128 overflows uint64_t");
619 while ((b
& 0x80) != 0);
624 /* Read a signed LEB128 number. */
627 read_sleb128 (struct dwarf_buf
*buf
)
639 const unsigned char *p
;
642 if (!advance (buf
, 1))
646 val
|= ((uint64_t) (b
& 0x7f)) << shift
;
649 dwarf_buf_error (buf
, "signed LEB128 overflows uint64_t");
654 while ((b
& 0x80) != 0);
656 if ((b
& 0x40) != 0 && shift
< 64)
657 val
|= ((uint64_t) -1) << shift
;
659 return (int64_t) val
;
662 /* Return the length of an LEB128 number. */
665 leb128_len (const unsigned char *p
)
670 while ((*p
& 0x80) != 0)
678 /* Read initial_length from BUF and advance the appropriate number of bytes. */
681 read_initial_length (struct dwarf_buf
*buf
, int *is_dwarf64
)
685 len
= read_uint32 (buf
);
686 if (len
== 0xffffffff)
688 len
= read_uint64 (buf
);
697 /* Free an abbreviations structure. */
700 free_abbrevs (struct backtrace_state
*state
, struct abbrevs
*abbrevs
,
701 backtrace_error_callback error_callback
, void *data
)
705 for (i
= 0; i
< abbrevs
->num_abbrevs
; ++i
)
706 backtrace_free (state
, abbrevs
->abbrevs
[i
].attrs
,
707 abbrevs
->abbrevs
[i
].num_attrs
* sizeof (struct attr
),
708 error_callback
, data
);
709 backtrace_free (state
, abbrevs
->abbrevs
,
710 abbrevs
->num_abbrevs
* sizeof (struct abbrev
),
711 error_callback
, data
);
712 abbrevs
->num_abbrevs
= 0;
713 abbrevs
->abbrevs
= NULL
;
716 /* Read an attribute value. Returns 1 on success, 0 on failure. If
717 the value can be represented as a uint64_t, sets *VAL and sets
718 *IS_VALID to 1. We don't try to store the value of other attribute
719 forms, because we don't care about them. */
722 read_attribute (enum dwarf_form form
, struct dwarf_buf
*buf
,
723 int is_dwarf64
, int version
, int addrsize
,
724 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
725 struct dwarf_data
*altlink
, struct attr_val
*val
)
727 /* Avoid warnings about val.u.FIELD may be used uninitialized if
728 this function is inlined. The warnings aren't valid but can
729 occur because the different fields are set and used
731 memset (val
, 0, sizeof *val
);
736 val
->encoding
= ATTR_VAL_ADDRESS
;
737 val
->u
.uint
= read_address (buf
, addrsize
);
740 val
->encoding
= ATTR_VAL_BLOCK
;
741 return advance (buf
, read_uint16 (buf
));
743 val
->encoding
= ATTR_VAL_BLOCK
;
744 return advance (buf
, read_uint32 (buf
));
746 val
->encoding
= ATTR_VAL_UINT
;
747 val
->u
.uint
= read_uint16 (buf
);
750 val
->encoding
= ATTR_VAL_UINT
;
751 val
->u
.uint
= read_uint32 (buf
);
754 val
->encoding
= ATTR_VAL_UINT
;
755 val
->u
.uint
= read_uint64 (buf
);
758 val
->encoding
= ATTR_VAL_STRING
;
759 val
->u
.string
= read_string (buf
);
760 return val
->u
.string
== NULL
? 0 : 1;
762 val
->encoding
= ATTR_VAL_BLOCK
;
763 return advance (buf
, read_uleb128 (buf
));
765 val
->encoding
= ATTR_VAL_BLOCK
;
766 return advance (buf
, read_byte (buf
));
768 val
->encoding
= ATTR_VAL_UINT
;
769 val
->u
.uint
= read_byte (buf
);
772 val
->encoding
= ATTR_VAL_UINT
;
773 val
->u
.uint
= read_byte (buf
);
776 val
->encoding
= ATTR_VAL_SINT
;
777 val
->u
.sint
= read_sleb128 (buf
);
783 offset
= read_offset (buf
, is_dwarf64
);
784 if (offset
>= dwarf_str_size
)
786 dwarf_buf_error (buf
, "DW_FORM_strp out of range");
789 val
->encoding
= ATTR_VAL_STRING
;
790 val
->u
.string
= (const char *) dwarf_str
+ offset
;
794 val
->encoding
= ATTR_VAL_UINT
;
795 val
->u
.uint
= read_uleb128 (buf
);
797 case DW_FORM_ref_addr
:
798 val
->encoding
= ATTR_VAL_REF_INFO
;
800 val
->u
.uint
= read_address (buf
, addrsize
);
802 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
805 val
->encoding
= ATTR_VAL_REF_UNIT
;
806 val
->u
.uint
= read_byte (buf
);
809 val
->encoding
= ATTR_VAL_REF_UNIT
;
810 val
->u
.uint
= read_uint16 (buf
);
813 val
->encoding
= ATTR_VAL_REF_UNIT
;
814 val
->u
.uint
= read_uint32 (buf
);
817 val
->encoding
= ATTR_VAL_REF_UNIT
;
818 val
->u
.uint
= read_uint64 (buf
);
820 case DW_FORM_ref_udata
:
821 val
->encoding
= ATTR_VAL_REF_UNIT
;
822 val
->u
.uint
= read_uleb128 (buf
);
824 case DW_FORM_indirect
:
828 form
= read_uleb128 (buf
);
829 return read_attribute ((enum dwarf_form
) form
, buf
, is_dwarf64
,
830 version
, addrsize
, dwarf_str
, dwarf_str_size
,
833 case DW_FORM_sec_offset
:
834 val
->encoding
= ATTR_VAL_REF_SECTION
;
835 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
837 case DW_FORM_exprloc
:
838 val
->encoding
= ATTR_VAL_EXPR
;
839 return advance (buf
, read_uleb128 (buf
));
840 case DW_FORM_flag_present
:
841 val
->encoding
= ATTR_VAL_UINT
;
844 case DW_FORM_ref_sig8
:
845 val
->encoding
= ATTR_VAL_REF_TYPE
;
846 val
->u
.uint
= read_uint64 (buf
);
848 case DW_FORM_GNU_addr_index
:
849 val
->encoding
= ATTR_VAL_REF_SECTION
;
850 val
->u
.uint
= read_uleb128 (buf
);
852 case DW_FORM_GNU_str_index
:
853 val
->encoding
= ATTR_VAL_REF_SECTION
;
854 val
->u
.uint
= read_uleb128 (buf
);
856 case DW_FORM_GNU_ref_alt
:
857 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
860 val
->encoding
= ATTR_VAL_NONE
;
863 val
->encoding
= ATTR_VAL_REF_ALT_INFO
;
865 case DW_FORM_GNU_strp_alt
:
868 offset
= read_offset (buf
, is_dwarf64
);
871 val
->encoding
= ATTR_VAL_NONE
;
874 if (offset
>= altlink
->dwarf_str_size
)
876 dwarf_buf_error (buf
, "DW_FORM_GNU_strp_alt out of range");
879 val
->encoding
= ATTR_VAL_STRING
;
880 val
->u
.string
= (const char *) altlink
->dwarf_str
+ offset
;
884 dwarf_buf_error (buf
, "unrecognized DWARF form");
889 /* Compare a unit offset against a unit for bsearch. */
892 units_search (const void *vkey
, const void *ventry
)
894 const size_t *key
= (const size_t *) vkey
;
895 const struct unit
*entry
= *((const struct unit
*const *) ventry
);
899 if (offset
< entry
->low_offset
)
901 else if (offset
>= entry
->high_offset
)
907 /* Find a unit in PU containing OFFSET. */
910 find_unit (struct unit
**pu
, size_t units_count
, size_t offset
)
913 u
= bsearch (&offset
, pu
, units_count
, sizeof (struct unit
*), units_search
);
914 return u
== NULL
? NULL
: *u
;
917 /* Compare function_addrs for qsort. When ranges are nested, make the
918 smallest one sort last. */
921 function_addrs_compare (const void *v1
, const void *v2
)
923 const struct function_addrs
*a1
= (const struct function_addrs
*) v1
;
924 const struct function_addrs
*a2
= (const struct function_addrs
*) v2
;
926 if (a1
->low
< a2
->low
)
928 if (a1
->low
> a2
->low
)
930 if (a1
->high
< a2
->high
)
932 if (a1
->high
> a2
->high
)
934 return strcmp (a1
->function
->name
, a2
->function
->name
);
937 /* Compare a PC against a function_addrs for bsearch. Note that if
938 there are multiple ranges containing PC, which one will be returned
939 is unpredictable. We compensate for that in dwarf_fileline. */
942 function_addrs_search (const void *vkey
, const void *ventry
)
944 const uintptr_t *key
= (const uintptr_t *) vkey
;
945 const struct function_addrs
*entry
= (const struct function_addrs
*) ventry
;
951 else if (pc
>= entry
->high
)
957 /* Add a new compilation unit address range to a vector. Returns 1 on
958 success, 0 on failure. */
961 add_unit_addr (struct backtrace_state
*state
, uintptr_t base_address
,
962 struct unit_addrs addrs
,
963 backtrace_error_callback error_callback
, void *data
,
964 struct unit_addrs_vector
*vec
)
966 struct unit_addrs
*p
;
968 /* Add in the base address of the module here, so that we can look
969 up the PC directly. */
970 addrs
.low
+= base_address
;
971 addrs
.high
+= base_address
;
973 /* Try to merge with the last entry. */
976 p
= (struct unit_addrs
*) vec
->vec
.base
+ (vec
->count
- 1);
977 if ((addrs
.low
== p
->high
|| addrs
.low
== p
->high
+ 1)
980 if (addrs
.high
> p
->high
)
981 p
->high
= addrs
.high
;
986 p
= ((struct unit_addrs
*)
987 backtrace_vector_grow (state
, sizeof (struct unit_addrs
),
988 error_callback
, data
, &vec
->vec
));
997 /* Compare unit_addrs for qsort. When ranges are nested, make the
998 smallest one sort last. */
1001 unit_addrs_compare (const void *v1
, const void *v2
)
1003 const struct unit_addrs
*a1
= (const struct unit_addrs
*) v1
;
1004 const struct unit_addrs
*a2
= (const struct unit_addrs
*) v2
;
1006 if (a1
->low
< a2
->low
)
1008 if (a1
->low
> a2
->low
)
1010 if (a1
->high
< a2
->high
)
1012 if (a1
->high
> a2
->high
)
1014 if (a1
->u
->lineoff
< a2
->u
->lineoff
)
1016 if (a1
->u
->lineoff
> a2
->u
->lineoff
)
1021 /* Compare a PC against a unit_addrs for bsearch. Note that if there
1022 are multiple ranges containing PC, which one will be returned is
1023 unpredictable. We compensate for that in dwarf_fileline. */
1026 unit_addrs_search (const void *vkey
, const void *ventry
)
1028 const uintptr_t *key
= (const uintptr_t *) vkey
;
1029 const struct unit_addrs
*entry
= (const struct unit_addrs
*) ventry
;
1033 if (pc
< entry
->low
)
1035 else if (pc
>= entry
->high
)
1041 /* Sort the line vector by PC. We want a stable sort here to maintain
1042 the order of lines for the same PC values. Since the sequence is
1043 being sorted in place, their addresses cannot be relied on to
1044 maintain stability. That is the purpose of the index member. */
1047 line_compare (const void *v1
, const void *v2
)
1049 const struct line
*ln1
= (const struct line
*) v1
;
1050 const struct line
*ln2
= (const struct line
*) v2
;
1052 if (ln1
->pc
< ln2
->pc
)
1054 else if (ln1
->pc
> ln2
->pc
)
1056 else if (ln1
->idx
< ln2
->idx
)
1058 else if (ln1
->idx
> ln2
->idx
)
1064 /* Find a PC in a line vector. We always allocate an extra entry at
1065 the end of the lines vector, so that this routine can safely look
1066 at the next entry. Note that when there are multiple mappings for
1067 the same PC value, this will return the last one. */
1070 line_search (const void *vkey
, const void *ventry
)
1072 const uintptr_t *key
= (const uintptr_t *) vkey
;
1073 const struct line
*entry
= (const struct line
*) ventry
;
1079 else if (pc
>= (entry
+ 1)->pc
)
1085 /* Sort the abbrevs by the abbrev code. This function is passed to
1086 both qsort and bsearch. */
1089 abbrev_compare (const void *v1
, const void *v2
)
1091 const struct abbrev
*a1
= (const struct abbrev
*) v1
;
1092 const struct abbrev
*a2
= (const struct abbrev
*) v2
;
1094 if (a1
->code
< a2
->code
)
1096 else if (a1
->code
> a2
->code
)
1100 /* This really shouldn't happen. It means there are two
1101 different abbrevs with the same code, and that means we don't
1102 know which one lookup_abbrev should return. */
1107 /* Read the abbreviation table for a compilation unit. Returns 1 on
1108 success, 0 on failure. */
1111 read_abbrevs (struct backtrace_state
*state
, uint64_t abbrev_offset
,
1112 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1113 int is_bigendian
, backtrace_error_callback error_callback
,
1114 void *data
, struct abbrevs
*abbrevs
)
1116 struct dwarf_buf abbrev_buf
;
1117 struct dwarf_buf count_buf
;
1120 abbrevs
->num_abbrevs
= 0;
1121 abbrevs
->abbrevs
= NULL
;
1123 if (abbrev_offset
>= dwarf_abbrev_size
)
1125 error_callback (data
, "abbrev offset out of range", 0);
1129 abbrev_buf
.name
= ".debug_abbrev";
1130 abbrev_buf
.start
= dwarf_abbrev
;
1131 abbrev_buf
.buf
= dwarf_abbrev
+ abbrev_offset
;
1132 abbrev_buf
.left
= dwarf_abbrev_size
- abbrev_offset
;
1133 abbrev_buf
.is_bigendian
= is_bigendian
;
1134 abbrev_buf
.error_callback
= error_callback
;
1135 abbrev_buf
.data
= data
;
1136 abbrev_buf
.reported_underflow
= 0;
1138 /* Count the number of abbrevs in this list. */
1140 count_buf
= abbrev_buf
;
1142 while (read_uleb128 (&count_buf
) != 0)
1144 if (count_buf
.reported_underflow
)
1148 read_uleb128 (&count_buf
);
1149 // Skip has_children.
1150 read_byte (&count_buf
);
1152 while (read_uleb128 (&count_buf
) != 0)
1153 read_uleb128 (&count_buf
);
1154 // Skip form of last attribute.
1155 read_uleb128 (&count_buf
);
1158 if (count_buf
.reported_underflow
)
1161 if (num_abbrevs
== 0)
1164 abbrevs
->abbrevs
= ((struct abbrev
*)
1165 backtrace_alloc (state
,
1166 num_abbrevs
* sizeof (struct abbrev
),
1167 error_callback
, data
));
1168 if (abbrevs
->abbrevs
== NULL
)
1170 abbrevs
->num_abbrevs
= num_abbrevs
;
1171 memset (abbrevs
->abbrevs
, 0, num_abbrevs
* sizeof (struct abbrev
));
1181 if (abbrev_buf
.reported_underflow
)
1184 code
= read_uleb128 (&abbrev_buf
);
1189 a
.tag
= (enum dwarf_tag
) read_uleb128 (&abbrev_buf
);
1190 a
.has_children
= read_byte (&abbrev_buf
);
1192 count_buf
= abbrev_buf
;
1194 while (read_uleb128 (&count_buf
) != 0)
1197 read_uleb128 (&count_buf
);
1203 read_uleb128 (&abbrev_buf
);
1204 read_uleb128 (&abbrev_buf
);
1208 attrs
= ((struct attr
*)
1209 backtrace_alloc (state
, num_attrs
* sizeof *attrs
,
1210 error_callback
, data
));
1219 name
= read_uleb128 (&abbrev_buf
);
1220 form
= read_uleb128 (&abbrev_buf
);
1223 attrs
[num_attrs
].name
= (enum dwarf_attribute
) name
;
1224 attrs
[num_attrs
].form
= (enum dwarf_form
) form
;
1229 a
.num_attrs
= num_attrs
;
1232 abbrevs
->abbrevs
[num_abbrevs
] = a
;
1236 backtrace_qsort (abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1237 sizeof (struct abbrev
), abbrev_compare
);
1242 free_abbrevs (state
, abbrevs
, error_callback
, data
);
1246 /* Return the abbrev information for an abbrev code. */
1248 static const struct abbrev
*
1249 lookup_abbrev (struct abbrevs
*abbrevs
, uint64_t code
,
1250 backtrace_error_callback error_callback
, void *data
)
1255 /* With GCC, where abbrevs are simply numbered in order, we should
1256 be able to just look up the entry. */
1257 if (code
- 1 < abbrevs
->num_abbrevs
1258 && abbrevs
->abbrevs
[code
- 1].code
== code
)
1259 return &abbrevs
->abbrevs
[code
- 1];
1261 /* Otherwise we have to search. */
1262 memset (&key
, 0, sizeof key
);
1264 p
= bsearch (&key
, abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1265 sizeof (struct abbrev
), abbrev_compare
);
1268 error_callback (data
, "invalid abbreviation code", 0);
1271 return (const struct abbrev
*) p
;
1274 /* Add non-contiguous address ranges for a compilation unit. Returns
1275 1 on success, 0 on failure. */
1278 add_unit_ranges (struct backtrace_state
*state
, uintptr_t base_address
,
1279 struct unit
*u
, uint64_t ranges
, uint64_t base
,
1280 int is_bigendian
, const unsigned char *dwarf_ranges
,
1281 size_t dwarf_ranges_size
,
1282 backtrace_error_callback error_callback
, void *data
,
1283 struct unit_addrs_vector
*addrs
)
1285 struct dwarf_buf ranges_buf
;
1287 if (ranges
>= dwarf_ranges_size
)
1289 error_callback (data
, "ranges offset out of range", 0);
1293 ranges_buf
.name
= ".debug_ranges";
1294 ranges_buf
.start
= dwarf_ranges
;
1295 ranges_buf
.buf
= dwarf_ranges
+ ranges
;
1296 ranges_buf
.left
= dwarf_ranges_size
- ranges
;
1297 ranges_buf
.is_bigendian
= is_bigendian
;
1298 ranges_buf
.error_callback
= error_callback
;
1299 ranges_buf
.data
= data
;
1300 ranges_buf
.reported_underflow
= 0;
1307 if (ranges_buf
.reported_underflow
)
1310 low
= read_address (&ranges_buf
, u
->addrsize
);
1311 high
= read_address (&ranges_buf
, u
->addrsize
);
1313 if (low
== 0 && high
== 0)
1316 if (is_highest_address (low
, u
->addrsize
))
1320 struct unit_addrs a
;
1323 a
.high
= high
+ base
;
1325 if (!add_unit_addr (state
, base_address
, a
, error_callback
, data
,
1331 if (ranges_buf
.reported_underflow
)
1337 /* Find the address range covered by a compilation unit, reading from
1338 UNIT_BUF and adding values to U. Returns 1 if all data could be
1339 read, 0 if there is some error. */
1342 find_address_ranges (struct backtrace_state
*state
, uintptr_t base_address
,
1343 struct dwarf_buf
*unit_buf
,
1344 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
1345 const unsigned char *dwarf_ranges
,
1346 size_t dwarf_ranges_size
,
1347 int is_bigendian
, struct dwarf_data
*altlink
,
1348 backtrace_error_callback error_callback
, void *data
,
1349 struct unit
*u
, struct unit_addrs_vector
*addrs
,
1350 enum dwarf_tag
*unit_tag
)
1352 while (unit_buf
->left
> 0)
1355 const struct abbrev
*abbrev
;
1360 int highpc_is_relative
;
1365 code
= read_uleb128 (unit_buf
);
1369 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
1373 if (unit_tag
!= NULL
)
1374 *unit_tag
= abbrev
->tag
;
1380 highpc_is_relative
= 0;
1383 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1385 struct attr_val val
;
1387 if (!read_attribute (abbrev
->attrs
[i
].form
, unit_buf
,
1388 u
->is_dwarf64
, u
->version
, u
->addrsize
,
1389 dwarf_str
, dwarf_str_size
, altlink
, &val
))
1392 switch (abbrev
->attrs
[i
].name
)
1395 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1403 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1405 highpc
= val
.u
.uint
;
1408 else if (val
.encoding
== ATTR_VAL_UINT
)
1410 highpc
= val
.u
.uint
;
1412 highpc_is_relative
= 1;
1417 if (val
.encoding
== ATTR_VAL_UINT
1418 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1420 ranges
= val
.u
.uint
;
1425 case DW_AT_stmt_list
:
1426 if (abbrev
->tag
== DW_TAG_compile_unit
1427 && (val
.encoding
== ATTR_VAL_UINT
1428 || val
.encoding
== ATTR_VAL_REF_SECTION
))
1429 u
->lineoff
= val
.u
.uint
;
1433 if (abbrev
->tag
== DW_TAG_compile_unit
1434 && val
.encoding
== ATTR_VAL_STRING
)
1435 u
->filename
= val
.u
.string
;
1438 case DW_AT_comp_dir
:
1439 if (abbrev
->tag
== DW_TAG_compile_unit
1440 && val
.encoding
== ATTR_VAL_STRING
)
1441 u
->comp_dir
= val
.u
.string
;
1449 if (abbrev
->tag
== DW_TAG_compile_unit
1450 || abbrev
->tag
== DW_TAG_subprogram
)
1454 if (!add_unit_ranges (state
, base_address
, u
, ranges
, lowpc
,
1455 is_bigendian
, dwarf_ranges
,
1456 dwarf_ranges_size
, error_callback
,
1460 else if (have_lowpc
&& have_highpc
)
1462 struct unit_addrs a
;
1464 if (highpc_is_relative
)
1470 if (!add_unit_addr (state
, base_address
, a
, error_callback
, data
,
1475 /* If we found the PC range in the DW_TAG_compile_unit, we
1477 if (abbrev
->tag
== DW_TAG_compile_unit
1478 && (have_ranges
|| (have_lowpc
&& have_highpc
)))
1482 if (abbrev
->has_children
)
1484 if (!find_address_ranges (state
, base_address
, unit_buf
,
1485 dwarf_str
, dwarf_str_size
,
1486 dwarf_ranges
, dwarf_ranges_size
,
1487 is_bigendian
, altlink
, error_callback
, data
,
1496 /* Build a mapping from address ranges to the compilation units where
1497 the line number information for that range can be found. Returns 1
1498 on success, 0 on failure. */
1501 build_address_map (struct backtrace_state
*state
, uintptr_t base_address
,
1502 const unsigned char *dwarf_info
, size_t dwarf_info_size
,
1503 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1504 const unsigned char *dwarf_ranges
, size_t dwarf_ranges_size
,
1505 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
1506 int is_bigendian
, struct dwarf_data
*altlink
,
1507 backtrace_error_callback error_callback
, void *data
,
1508 struct unit_addrs_vector
*addrs
,
1509 struct unit_vector
*unit_vec
)
1511 struct dwarf_buf info
;
1512 struct backtrace_vector units
;
1516 size_t unit_offset
= 0;
1518 memset (&addrs
->vec
, 0, sizeof addrs
->vec
);
1519 memset (&unit_vec
->vec
, 0, sizeof unit_vec
->vec
);
1521 unit_vec
->count
= 0;
1523 /* Read through the .debug_info section. FIXME: Should we use the
1524 .debug_aranges section? gdb and addr2line don't use it, but I'm
1527 info
.name
= ".debug_info";
1528 info
.start
= dwarf_info
;
1529 info
.buf
= dwarf_info
;
1530 info
.left
= dwarf_info_size
;
1531 info
.is_bigendian
= is_bigendian
;
1532 info
.error_callback
= error_callback
;
1534 info
.reported_underflow
= 0;
1536 memset (&units
, 0, sizeof units
);
1539 while (info
.left
> 0)
1541 const unsigned char *unit_data_start
;
1544 struct dwarf_buf unit_buf
;
1546 uint64_t abbrev_offset
;
1549 enum dwarf_tag unit_tag
;
1551 if (info
.reported_underflow
)
1554 unit_data_start
= info
.buf
;
1556 len
= read_initial_length (&info
, &is_dwarf64
);
1558 unit_buf
.left
= len
;
1560 if (!advance (&info
, len
))
1563 version
= read_uint16 (&unit_buf
);
1564 if (version
< 2 || version
> 4)
1566 dwarf_buf_error (&unit_buf
, "unrecognized DWARF version");
1570 pu
= ((struct unit
**)
1571 backtrace_vector_grow (state
, sizeof (struct unit
*),
1572 error_callback
, data
, &units
));
1576 u
= ((struct unit
*)
1577 backtrace_alloc (state
, sizeof *u
, error_callback
, data
));
1584 memset (&u
->abbrevs
, 0, sizeof u
->abbrevs
);
1585 abbrev_offset
= read_offset (&unit_buf
, is_dwarf64
);
1586 if (!read_abbrevs (state
, abbrev_offset
, dwarf_abbrev
, dwarf_abbrev_size
,
1587 is_bigendian
, error_callback
, data
, &u
->abbrevs
))
1590 addrsize
= read_byte (&unit_buf
);
1592 u
->low_offset
= unit_offset
;
1593 unit_offset
+= len
+ (is_dwarf64
? 12 : 4);
1594 u
->high_offset
= unit_offset
;
1595 u
->unit_data
= unit_buf
.buf
;
1596 u
->unit_data_len
= unit_buf
.left
;
1597 u
->unit_data_offset
= unit_buf
.buf
- unit_data_start
;
1598 u
->version
= version
;
1599 u
->is_dwarf64
= is_dwarf64
;
1600 u
->addrsize
= addrsize
;
1603 u
->abs_filename
= NULL
;
1606 /* The actual line number mappings will be read as needed. */
1609 u
->function_addrs
= NULL
;
1610 u
->function_addrs_count
= 0;
1612 if (!find_address_ranges (state
, base_address
, &unit_buf
,
1613 dwarf_str
, dwarf_str_size
,
1614 dwarf_ranges
, dwarf_ranges_size
,
1615 is_bigendian
, altlink
, error_callback
, data
,
1616 u
, addrs
, &unit_tag
))
1619 if (unit_buf
.reported_underflow
)
1622 if (info
.reported_underflow
)
1625 unit_vec
->vec
= units
;
1626 unit_vec
->count
= units_count
;
1630 if (units_count
> 0)
1632 pu
= (struct unit
**) units
.base
;
1633 for (i
= 0; i
< units_count
; i
++)
1635 free_abbrevs (state
, &pu
[i
]->abbrevs
, error_callback
, data
);
1636 backtrace_free (state
, pu
[i
], sizeof **pu
, error_callback
, data
);
1638 backtrace_vector_free (state
, &units
, error_callback
, data
);
1640 if (addrs
->count
> 0)
1642 backtrace_vector_free (state
, &addrs
->vec
, error_callback
, data
);
1648 /* Add a new mapping to the vector of line mappings that we are
1649 building. Returns 1 on success, 0 on failure. */
1652 add_line (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1653 uintptr_t pc
, const char *filename
, int lineno
,
1654 backtrace_error_callback error_callback
, void *data
,
1655 struct line_vector
*vec
)
1659 /* If we are adding the same mapping, ignore it. This can happen
1660 when using discriminators. */
1663 ln
= (struct line
*) vec
->vec
.base
+ (vec
->count
- 1);
1664 if (pc
== ln
->pc
&& filename
== ln
->filename
&& lineno
== ln
->lineno
)
1668 ln
= ((struct line
*)
1669 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1674 /* Add in the base address here, so that we can look up the PC
1676 ln
->pc
= pc
+ ddata
->base_address
;
1678 ln
->filename
= filename
;
1679 ln
->lineno
= lineno
;
1680 ln
->idx
= vec
->count
;
1687 /* Free the line header information. */
1690 free_line_header (struct backtrace_state
*state
, struct line_header
*hdr
,
1691 backtrace_error_callback error_callback
, void *data
)
1693 if (hdr
->dirs_count
!= 0)
1694 backtrace_free (state
, hdr
->dirs
, hdr
->dirs_count
* sizeof (const char *),
1695 error_callback
, data
);
1696 backtrace_free (state
, hdr
->filenames
,
1697 hdr
->filenames_count
* sizeof (char *),
1698 error_callback
, data
);
1701 /* Read the line header. Return 1 on success, 0 on failure. */
1704 read_line_header (struct backtrace_state
*state
, struct unit
*u
,
1705 int is_dwarf64
, struct dwarf_buf
*line_buf
,
1706 struct line_header
*hdr
)
1709 struct dwarf_buf hdr_buf
;
1710 const unsigned char *p
;
1711 const unsigned char *pend
;
1714 hdr
->version
= read_uint16 (line_buf
);
1715 if (hdr
->version
< 2 || hdr
->version
> 4)
1717 dwarf_buf_error (line_buf
, "unsupported line number version");
1721 hdrlen
= read_offset (line_buf
, is_dwarf64
);
1723 hdr_buf
= *line_buf
;
1724 hdr_buf
.left
= hdrlen
;
1726 if (!advance (line_buf
, hdrlen
))
1729 hdr
->min_insn_len
= read_byte (&hdr_buf
);
1730 if (hdr
->version
< 4)
1731 hdr
->max_ops_per_insn
= 1;
1733 hdr
->max_ops_per_insn
= read_byte (&hdr_buf
);
1735 /* We don't care about default_is_stmt. */
1736 read_byte (&hdr_buf
);
1738 hdr
->line_base
= read_sbyte (&hdr_buf
);
1739 hdr
->line_range
= read_byte (&hdr_buf
);
1741 hdr
->opcode_base
= read_byte (&hdr_buf
);
1742 hdr
->opcode_lengths
= hdr_buf
.buf
;
1743 if (!advance (&hdr_buf
, hdr
->opcode_base
- 1))
1746 /* Count the number of directory entries. */
1747 hdr
->dirs_count
= 0;
1749 pend
= p
+ hdr_buf
.left
;
1750 while (p
< pend
&& *p
!= '\0')
1752 p
+= strnlen((const char *) p
, pend
- p
) + 1;
1757 if (hdr
->dirs_count
!= 0)
1759 hdr
->dirs
= ((const char **)
1760 backtrace_alloc (state
,
1761 hdr
->dirs_count
* sizeof (const char *),
1762 line_buf
->error_callback
, line_buf
->data
));
1763 if (hdr
->dirs
== NULL
)
1768 while (*hdr_buf
.buf
!= '\0')
1770 if (hdr_buf
.reported_underflow
)
1773 hdr
->dirs
[i
] = read_string (&hdr_buf
);
1774 if (hdr
->dirs
[i
] == NULL
)
1778 if (!advance (&hdr_buf
, 1))
1781 /* Count the number of file entries. */
1782 hdr
->filenames_count
= 0;
1784 pend
= p
+ hdr_buf
.left
;
1785 while (p
< pend
&& *p
!= '\0')
1787 p
+= strnlen ((const char *) p
, pend
- p
) + 1;
1788 p
+= leb128_len (p
);
1789 p
+= leb128_len (p
);
1790 p
+= leb128_len (p
);
1791 ++hdr
->filenames_count
;
1794 hdr
->filenames
= ((const char **)
1795 backtrace_alloc (state
,
1796 hdr
->filenames_count
* sizeof (char *),
1797 line_buf
->error_callback
,
1799 if (hdr
->filenames
== NULL
)
1802 while (*hdr_buf
.buf
!= '\0')
1804 const char *filename
;
1807 if (hdr_buf
.reported_underflow
)
1810 filename
= read_string (&hdr_buf
);
1811 if (filename
== NULL
)
1813 dir_index
= read_uleb128 (&hdr_buf
);
1814 if (IS_ABSOLUTE_PATH (filename
)
1815 || (dir_index
== 0 && u
->comp_dir
== NULL
))
1816 hdr
->filenames
[i
] = filename
;
1821 size_t filename_len
;
1826 else if (dir_index
- 1 < hdr
->dirs_count
)
1827 dir
= hdr
->dirs
[dir_index
- 1];
1830 dwarf_buf_error (line_buf
,
1831 ("invalid directory index in "
1832 "line number program header"));
1835 dir_len
= strlen (dir
);
1836 filename_len
= strlen (filename
);
1838 backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
1839 line_buf
->error_callback
, line_buf
->data
));
1842 memcpy (s
, dir
, dir_len
);
1843 /* FIXME: If we are on a DOS-based file system, and the
1844 directory or the file name use backslashes, then we
1845 should use a backslash here. */
1847 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
1848 hdr
->filenames
[i
] = s
;
1851 /* Ignore the modification time and size. */
1852 read_uleb128 (&hdr_buf
);
1853 read_uleb128 (&hdr_buf
);
1858 if (hdr_buf
.reported_underflow
)
1864 /* Read the line program, adding line mappings to VEC. Return 1 on
1865 success, 0 on failure. */
1868 read_line_program (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1869 struct unit
*u
, const struct line_header
*hdr
,
1870 struct dwarf_buf
*line_buf
, struct line_vector
*vec
)
1873 unsigned int op_index
;
1874 const char *reset_filename
;
1875 const char *filename
;
1880 if (hdr
->filenames_count
> 0)
1881 reset_filename
= hdr
->filenames
[0];
1883 reset_filename
= "";
1884 filename
= reset_filename
;
1886 while (line_buf
->left
> 0)
1890 op
= read_byte (line_buf
);
1891 if (op
>= hdr
->opcode_base
)
1893 unsigned int advance
;
1895 /* Special opcode. */
1896 op
-= hdr
->opcode_base
;
1897 advance
= op
/ hdr
->line_range
;
1898 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1899 / hdr
->max_ops_per_insn
);
1900 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1901 lineno
+= hdr
->line_base
+ (int) (op
% hdr
->line_range
);
1902 add_line (state
, ddata
, address
, filename
, lineno
,
1903 line_buf
->error_callback
, line_buf
->data
, vec
);
1905 else if (op
== DW_LNS_extended_op
)
1909 len
= read_uleb128 (line_buf
);
1910 op
= read_byte (line_buf
);
1913 case DW_LNE_end_sequence
:
1914 /* FIXME: Should we mark the high PC here? It seems
1915 that we already have that information from the
1916 compilation unit. */
1919 filename
= reset_filename
;
1922 case DW_LNE_set_address
:
1923 address
= read_address (line_buf
, u
->addrsize
);
1925 case DW_LNE_define_file
:
1928 unsigned int dir_index
;
1930 f
= read_string (line_buf
);
1933 dir_index
= read_uleb128 (line_buf
);
1934 /* Ignore that time and length. */
1935 read_uleb128 (line_buf
);
1936 read_uleb128 (line_buf
);
1937 if (IS_ABSOLUTE_PATH (f
))
1948 else if (dir_index
- 1 < hdr
->dirs_count
)
1949 dir
= hdr
->dirs
[dir_index
- 1];
1952 dwarf_buf_error (line_buf
,
1953 ("invalid directory index "
1954 "in line number program"));
1957 dir_len
= strlen (dir
);
1960 backtrace_alloc (state
, dir_len
+ f_len
+ 2,
1961 line_buf
->error_callback
,
1965 memcpy (p
, dir
, dir_len
);
1966 /* FIXME: If we are on a DOS-based file system,
1967 and the directory or the file name use
1968 backslashes, then we should use a backslash
1971 memcpy (p
+ dir_len
+ 1, f
, f_len
+ 1);
1976 case DW_LNE_set_discriminator
:
1977 /* We don't care about discriminators. */
1978 read_uleb128 (line_buf
);
1981 if (!advance (line_buf
, len
- 1))
1991 add_line (state
, ddata
, address
, filename
, lineno
,
1992 line_buf
->error_callback
, line_buf
->data
, vec
);
1994 case DW_LNS_advance_pc
:
1998 advance
= read_uleb128 (line_buf
);
1999 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
2000 / hdr
->max_ops_per_insn
);
2001 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
2004 case DW_LNS_advance_line
:
2005 lineno
+= (int) read_sleb128 (line_buf
);
2007 case DW_LNS_set_file
:
2011 fileno
= read_uleb128 (line_buf
);
2016 if (fileno
- 1 >= hdr
->filenames_count
)
2018 dwarf_buf_error (line_buf
,
2019 ("invalid file number in "
2020 "line number program"));
2023 filename
= hdr
->filenames
[fileno
- 1];
2027 case DW_LNS_set_column
:
2028 read_uleb128 (line_buf
);
2030 case DW_LNS_negate_stmt
:
2032 case DW_LNS_set_basic_block
:
2034 case DW_LNS_const_add_pc
:
2036 unsigned int advance
;
2038 op
= 255 - hdr
->opcode_base
;
2039 advance
= op
/ hdr
->line_range
;
2040 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
2041 / hdr
->max_ops_per_insn
);
2042 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
2045 case DW_LNS_fixed_advance_pc
:
2046 address
+= read_uint16 (line_buf
);
2049 case DW_LNS_set_prologue_end
:
2051 case DW_LNS_set_epilogue_begin
:
2053 case DW_LNS_set_isa
:
2054 read_uleb128 (line_buf
);
2060 for (i
= hdr
->opcode_lengths
[op
- 1]; i
> 0; --i
)
2061 read_uleb128 (line_buf
);
2071 /* Read the line number information for a compilation unit. Returns 1
2072 on success, 0 on failure. */
2075 read_line_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2076 backtrace_error_callback error_callback
, void *data
,
2077 struct unit
*u
, struct line_header
*hdr
, struct line
**lines
,
2078 size_t *lines_count
)
2080 struct line_vector vec
;
2081 struct dwarf_buf line_buf
;
2086 memset (&vec
.vec
, 0, sizeof vec
.vec
);
2089 memset (hdr
, 0, sizeof *hdr
);
2091 if (u
->lineoff
!= (off_t
) (size_t) u
->lineoff
2092 || (size_t) u
->lineoff
>= ddata
->dwarf_line_size
)
2094 error_callback (data
, "unit line offset out of range", 0);
2098 line_buf
.name
= ".debug_line";
2099 line_buf
.start
= ddata
->dwarf_line
;
2100 line_buf
.buf
= ddata
->dwarf_line
+ u
->lineoff
;
2101 line_buf
.left
= ddata
->dwarf_line_size
- u
->lineoff
;
2102 line_buf
.is_bigendian
= ddata
->is_bigendian
;
2103 line_buf
.error_callback
= error_callback
;
2104 line_buf
.data
= data
;
2105 line_buf
.reported_underflow
= 0;
2107 len
= read_initial_length (&line_buf
, &is_dwarf64
);
2108 line_buf
.left
= len
;
2110 if (!read_line_header (state
, u
, is_dwarf64
, &line_buf
, hdr
))
2113 if (!read_line_program (state
, ddata
, u
, hdr
, &line_buf
, &vec
))
2116 if (line_buf
.reported_underflow
)
2121 /* This is not a failure in the sense of a generating an error,
2122 but it is a failure in that sense that we have no useful
2127 /* Allocate one extra entry at the end. */
2128 ln
= ((struct line
*)
2129 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
2133 ln
->pc
= (uintptr_t) -1;
2134 ln
->filename
= NULL
;
2138 if (!backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
))
2141 ln
= (struct line
*) vec
.vec
.base
;
2142 backtrace_qsort (ln
, vec
.count
, sizeof (struct line
), line_compare
);
2145 *lines_count
= vec
.count
;
2150 backtrace_vector_free (state
, &vec
.vec
, error_callback
, data
);
2151 free_line_header (state
, hdr
, error_callback
, data
);
2152 *lines
= (struct line
*) (uintptr_t) -1;
2157 static const char *read_referenced_name (struct dwarf_data
*, struct unit
*,
2158 uint64_t, backtrace_error_callback
,
2161 /* Read the name of a function from a DIE referenced by ATTR with VAL. */
2164 read_referenced_name_from_attr (struct dwarf_data
*ddata
, struct unit
*u
,
2165 struct attr
*attr
, struct attr_val
*val
,
2166 backtrace_error_callback error_callback
,
2171 case DW_AT_abstract_origin
:
2172 case DW_AT_specification
:
2178 if (attr
->form
== DW_FORM_ref_sig8
)
2181 if (val
->encoding
== ATTR_VAL_REF_INFO
)
2184 = find_unit (ddata
->units
, ddata
->units_count
,
2189 uint64_t offset
= val
->u
.uint
- unit
->low_offset
;
2190 return read_referenced_name (ddata
, unit
, offset
, error_callback
, data
);
2193 if (val
->encoding
== ATTR_VAL_UINT
2194 || val
->encoding
== ATTR_VAL_REF_UNIT
)
2195 return read_referenced_name (ddata
, u
, val
->u
.uint
, error_callback
, data
);
2197 if (val
->encoding
== ATTR_VAL_REF_ALT_INFO
)
2199 struct unit
*alt_unit
2200 = find_unit (ddata
->altlink
->units
, ddata
->altlink
->units_count
,
2202 if (alt_unit
== NULL
)
2205 uint64_t offset
= val
->u
.uint
- alt_unit
->low_offset
;
2206 return read_referenced_name (ddata
->altlink
, alt_unit
, offset
,
2207 error_callback
, data
);
2213 /* Read the name of a function from a DIE referenced by a
2214 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
2215 the same compilation unit. */
2218 read_referenced_name (struct dwarf_data
*ddata
, struct unit
*u
,
2219 uint64_t offset
, backtrace_error_callback error_callback
,
2222 struct dwarf_buf unit_buf
;
2224 const struct abbrev
*abbrev
;
2228 /* OFFSET is from the start of the data for this compilation unit.
2229 U->unit_data is the data, but it starts U->unit_data_offset bytes
2230 from the beginning. */
2232 if (offset
< u
->unit_data_offset
2233 || offset
- u
->unit_data_offset
>= u
->unit_data_len
)
2235 error_callback (data
,
2236 "abstract origin or specification out of range",
2241 offset
-= u
->unit_data_offset
;
2243 unit_buf
.name
= ".debug_info";
2244 unit_buf
.start
= ddata
->dwarf_info
;
2245 unit_buf
.buf
= u
->unit_data
+ offset
;
2246 unit_buf
.left
= u
->unit_data_len
- offset
;
2247 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2248 unit_buf
.error_callback
= error_callback
;
2249 unit_buf
.data
= data
;
2250 unit_buf
.reported_underflow
= 0;
2252 code
= read_uleb128 (&unit_buf
);
2255 dwarf_buf_error (&unit_buf
, "invalid abstract origin or specification");
2259 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2264 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2266 struct attr_val val
;
2268 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
,
2269 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2270 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2271 ddata
->altlink
, &val
))
2274 switch (abbrev
->attrs
[i
].name
)
2277 /* Third name preference: don't override. A name we found in some
2278 other way, will normally be more useful -- e.g., this name is
2279 normally not mangled. */
2282 if (val
.encoding
== ATTR_VAL_STRING
)
2286 case DW_AT_linkage_name
:
2287 case DW_AT_MIPS_linkage_name
:
2288 /* First name preference: override all. */
2289 if (val
.encoding
== ATTR_VAL_STRING
)
2290 return val
.u
.string
;
2293 case DW_AT_specification
:
2294 /* Second name preference: override DW_AT_name, don't override
2295 DW_AT_linkage_name. */
2299 name
= read_referenced_name_from_attr (ddata
, u
, &abbrev
->attrs
[i
],
2300 &val
, error_callback
, data
);
2314 /* Add a single range to U that maps to function. Returns 1 on
2315 success, 0 on error. */
2318 add_function_range (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2319 struct function
*function
, uint64_t lowpc
, uint64_t highpc
,
2320 backtrace_error_callback error_callback
,
2321 void *data
, struct function_vector
*vec
)
2323 struct function_addrs
*p
;
2325 /* Add in the base address here, so that we can look up the PC
2327 lowpc
+= ddata
->base_address
;
2328 highpc
+= ddata
->base_address
;
2332 p
= (struct function_addrs
*) vec
->vec
.base
+ vec
->count
- 1;
2333 if ((lowpc
== p
->high
|| lowpc
== p
->high
+ 1)
2334 && function
== p
->function
)
2336 if (highpc
> p
->high
)
2342 p
= ((struct function_addrs
*)
2343 backtrace_vector_grow (state
, sizeof (struct function_addrs
),
2344 error_callback
, data
, &vec
->vec
));
2350 p
->function
= function
;
2355 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2359 add_function_ranges (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2360 struct unit
*u
, struct function
*function
,
2361 uint64_t ranges
, uint64_t base
,
2362 backtrace_error_callback error_callback
, void *data
,
2363 struct function_vector
*vec
)
2365 struct dwarf_buf ranges_buf
;
2367 if (ranges
>= ddata
->dwarf_ranges_size
)
2369 error_callback (data
, "function ranges offset out of range", 0);
2373 ranges_buf
.name
= ".debug_ranges";
2374 ranges_buf
.start
= ddata
->dwarf_ranges
;
2375 ranges_buf
.buf
= ddata
->dwarf_ranges
+ ranges
;
2376 ranges_buf
.left
= ddata
->dwarf_ranges_size
- ranges
;
2377 ranges_buf
.is_bigendian
= ddata
->is_bigendian
;
2378 ranges_buf
.error_callback
= error_callback
;
2379 ranges_buf
.data
= data
;
2380 ranges_buf
.reported_underflow
= 0;
2387 if (ranges_buf
.reported_underflow
)
2390 low
= read_address (&ranges_buf
, u
->addrsize
);
2391 high
= read_address (&ranges_buf
, u
->addrsize
);
2393 if (low
== 0 && high
== 0)
2396 if (is_highest_address (low
, u
->addrsize
))
2400 if (!add_function_range (state
, ddata
, function
, low
+ base
,
2401 high
+ base
, error_callback
, data
, vec
))
2406 if (ranges_buf
.reported_underflow
)
2412 /* Read one entry plus all its children. Add function addresses to
2413 VEC. Returns 1 on success, 0 on error. */
2416 read_function_entry (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2417 struct unit
*u
, uint64_t base
, struct dwarf_buf
*unit_buf
,
2418 const struct line_header
*lhdr
,
2419 backtrace_error_callback error_callback
, void *data
,
2420 struct function_vector
*vec_function
,
2421 struct function_vector
*vec_inlined
)
2423 while (unit_buf
->left
> 0)
2426 const struct abbrev
*abbrev
;
2428 struct function
*function
;
2429 struct function_vector
*vec
;
2435 int highpc_is_relative
;
2438 int have_linkage_name
;
2440 code
= read_uleb128 (unit_buf
);
2444 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2448 is_function
= (abbrev
->tag
== DW_TAG_subprogram
2449 || abbrev
->tag
== DW_TAG_entry_point
2450 || abbrev
->tag
== DW_TAG_inlined_subroutine
);
2452 if (abbrev
->tag
== DW_TAG_inlined_subroutine
)
2460 function
= ((struct function
*)
2461 backtrace_alloc (state
, sizeof *function
,
2462 error_callback
, data
));
2463 if (function
== NULL
)
2465 memset (function
, 0, sizeof *function
);
2472 highpc_is_relative
= 0;
2475 have_linkage_name
= 0;
2476 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2478 struct attr_val val
;
2480 if (!read_attribute (abbrev
->attrs
[i
].form
, unit_buf
,
2481 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2482 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2483 ddata
->altlink
, &val
))
2486 /* The compile unit sets the base address for any address
2487 ranges in the function entries. */
2488 if (abbrev
->tag
== DW_TAG_compile_unit
2489 && abbrev
->attrs
[i
].name
== DW_AT_low_pc
2490 && val
.encoding
== ATTR_VAL_ADDRESS
)
2495 switch (abbrev
->attrs
[i
].name
)
2497 case DW_AT_call_file
:
2498 if (val
.encoding
== ATTR_VAL_UINT
)
2500 if (val
.u
.uint
== 0)
2501 function
->caller_filename
= "";
2504 if (val
.u
.uint
- 1 >= lhdr
->filenames_count
)
2506 dwarf_buf_error (unit_buf
,
2507 ("invalid file number in "
2508 "DW_AT_call_file attribute"));
2511 function
->caller_filename
=
2512 lhdr
->filenames
[val
.u
.uint
- 1];
2517 case DW_AT_call_line
:
2518 if (val
.encoding
== ATTR_VAL_UINT
)
2519 function
->caller_lineno
= val
.u
.uint
;
2522 case DW_AT_abstract_origin
:
2523 case DW_AT_specification
:
2524 /* Second name preference: override DW_AT_name, don't override
2525 DW_AT_linkage_name. */
2526 if (have_linkage_name
)
2532 = read_referenced_name_from_attr (ddata
, u
,
2533 &abbrev
->attrs
[i
], &val
,
2534 error_callback
, data
);
2536 function
->name
= name
;
2541 /* Third name preference: don't override. */
2542 if (function
->name
!= NULL
)
2544 if (val
.encoding
== ATTR_VAL_STRING
)
2545 function
->name
= val
.u
.string
;
2548 case DW_AT_linkage_name
:
2549 case DW_AT_MIPS_linkage_name
:
2550 /* First name preference: override all. */
2551 if (val
.encoding
== ATTR_VAL_STRING
)
2553 function
->name
= val
.u
.string
;
2554 have_linkage_name
= 1;
2559 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2567 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2569 highpc
= val
.u
.uint
;
2572 else if (val
.encoding
== ATTR_VAL_UINT
)
2574 highpc
= val
.u
.uint
;
2576 highpc_is_relative
= 1;
2581 if (val
.encoding
== ATTR_VAL_UINT
2582 || val
.encoding
== ATTR_VAL_REF_SECTION
)
2584 ranges
= val
.u
.uint
;
2595 /* If we couldn't find a name for the function, we have no use
2597 if (is_function
&& function
->name
== NULL
)
2599 backtrace_free (state
, function
, sizeof *function
,
2600 error_callback
, data
);
2608 if (!add_function_ranges (state
, ddata
, u
, function
, ranges
,
2609 base
, error_callback
, data
, vec
))
2612 else if (have_lowpc
&& have_highpc
)
2614 if (highpc_is_relative
)
2616 if (!add_function_range (state
, ddata
, function
, lowpc
, highpc
,
2617 error_callback
, data
, vec
))
2622 backtrace_free (state
, function
, sizeof *function
,
2623 error_callback
, data
);
2628 if (abbrev
->has_children
)
2632 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2633 error_callback
, data
, vec_function
,
2639 struct function_vector fvec
;
2641 /* Gather any information for inlined functions in
2644 memset (&fvec
, 0, sizeof fvec
);
2646 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2647 error_callback
, data
, vec_function
,
2653 struct function_addrs
*faddrs
;
2655 if (!backtrace_vector_release (state
, &fvec
.vec
,
2656 error_callback
, data
))
2659 faddrs
= (struct function_addrs
*) fvec
.vec
.base
;
2660 backtrace_qsort (faddrs
, fvec
.count
,
2661 sizeof (struct function_addrs
),
2662 function_addrs_compare
);
2664 function
->function_addrs
= faddrs
;
2665 function
->function_addrs_count
= fvec
.count
;
2674 /* Read function name information for a compilation unit. We look
2675 through the whole unit looking for function tags. */
2678 read_function_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2679 const struct line_header
*lhdr
,
2680 backtrace_error_callback error_callback
, void *data
,
2681 struct unit
*u
, struct function_vector
*fvec
,
2682 struct function_addrs
**ret_addrs
,
2683 size_t *ret_addrs_count
)
2685 struct function_vector lvec
;
2686 struct function_vector
*pfvec
;
2687 struct dwarf_buf unit_buf
;
2688 struct function_addrs
*addrs
;
2691 /* Use FVEC if it is not NULL. Otherwise use our own vector. */
2696 memset (&lvec
, 0, sizeof lvec
);
2700 unit_buf
.name
= ".debug_info";
2701 unit_buf
.start
= ddata
->dwarf_info
;
2702 unit_buf
.buf
= u
->unit_data
;
2703 unit_buf
.left
= u
->unit_data_len
;
2704 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2705 unit_buf
.error_callback
= error_callback
;
2706 unit_buf
.data
= data
;
2707 unit_buf
.reported_underflow
= 0;
2709 while (unit_buf
.left
> 0)
2711 if (!read_function_entry (state
, ddata
, u
, 0, &unit_buf
, lhdr
,
2712 error_callback
, data
, pfvec
, pfvec
))
2716 if (pfvec
->count
== 0)
2719 addrs_count
= pfvec
->count
;
2723 if (!backtrace_vector_release (state
, &lvec
.vec
, error_callback
, data
))
2725 addrs
= (struct function_addrs
*) pfvec
->vec
.base
;
2729 /* Finish this list of addresses, but leave the remaining space in
2730 the vector available for the next function unit. */
2731 addrs
= ((struct function_addrs
*)
2732 backtrace_vector_finish (state
, &fvec
->vec
,
2733 error_callback
, data
));
2739 backtrace_qsort (addrs
, addrs_count
, sizeof (struct function_addrs
),
2740 function_addrs_compare
);
2743 *ret_addrs_count
= addrs_count
;
2746 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2747 information, and update FILENAME and LINENO for the caller.
2748 Returns whatever CALLBACK returns, or 0 to keep going. */
2751 report_inlined_functions (uintptr_t pc
, struct function
*function
,
2752 backtrace_full_callback callback
, void *data
,
2753 const char **filename
, int *lineno
)
2755 struct function_addrs
*function_addrs
;
2756 struct function
*inlined
;
2759 if (function
->function_addrs_count
== 0)
2762 function_addrs
= ((struct function_addrs
*)
2763 bsearch (&pc
, function
->function_addrs
,
2764 function
->function_addrs_count
,
2765 sizeof (struct function_addrs
),
2766 function_addrs_search
));
2767 if (function_addrs
== NULL
)
2770 while (((size_t) (function_addrs
- function
->function_addrs
) + 1
2771 < function
->function_addrs_count
)
2772 && pc
>= (function_addrs
+ 1)->low
2773 && pc
< (function_addrs
+ 1)->high
)
2776 /* We found an inlined call. */
2778 inlined
= function_addrs
->function
;
2780 /* Report any calls inlined into this one. */
2781 ret
= report_inlined_functions (pc
, inlined
, callback
, data
,
2786 /* Report this inlined call. */
2787 ret
= callback (data
, pc
, *filename
, *lineno
, inlined
->name
);
2791 /* Our caller will report the caller of the inlined function; tell
2792 it the appropriate filename and line number. */
2793 *filename
= inlined
->caller_filename
;
2794 *lineno
= inlined
->caller_lineno
;
2799 /* Look for a PC in the DWARF mapping for one module. On success,
2800 call CALLBACK and return whatever it returns. On error, call
2801 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
2805 dwarf_lookup_pc (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2806 uintptr_t pc
, backtrace_full_callback callback
,
2807 backtrace_error_callback error_callback
, void *data
,
2810 struct unit_addrs
*entry
;
2815 struct function_addrs
*function_addrs
;
2816 struct function
*function
;
2817 const char *filename
;
2823 /* Find an address range that includes PC. */
2824 entry
= (ddata
->addrs_count
== 0
2826 : bsearch (&pc
, ddata
->addrs
, ddata
->addrs_count
,
2827 sizeof (struct unit_addrs
), unit_addrs_search
));
2835 /* If there are multiple ranges that contain PC, use the last one,
2836 in order to produce predictable results. If we assume that all
2837 ranges are properly nested, then the last range will be the
2839 while ((size_t) (entry
- ddata
->addrs
) + 1 < ddata
->addrs_count
2840 && pc
>= (entry
+ 1)->low
2841 && pc
< (entry
+ 1)->high
)
2844 /* We need the lines, lines_count, function_addrs,
2845 function_addrs_count fields of u. If they are not set, we need
2846 to set them. When running in threaded mode, we need to allow for
2847 the possibility that some other thread is setting them
2853 /* Skip units with no useful line number information by walking
2854 backward. Useless line number information is marked by setting
2856 while (entry
> ddata
->addrs
2857 && pc
>= (entry
- 1)->low
2858 && pc
< (entry
- 1)->high
)
2860 if (state
->threaded
)
2861 lines
= (struct line
*) backtrace_atomic_load_pointer (&u
->lines
);
2863 if (lines
!= (struct line
*) (uintptr_t) -1)
2872 if (state
->threaded
)
2873 lines
= backtrace_atomic_load_pointer (&u
->lines
);
2878 size_t function_addrs_count
;
2879 struct line_header lhdr
;
2882 /* We have never read the line information for this unit. Read
2885 function_addrs
= NULL
;
2886 function_addrs_count
= 0;
2887 if (read_line_info (state
, ddata
, error_callback
, data
, entry
->u
, &lhdr
,
2890 struct function_vector
*pfvec
;
2892 /* If not threaded, reuse DDATA->FVEC for better memory
2894 if (state
->threaded
)
2897 pfvec
= &ddata
->fvec
;
2898 read_function_info (state
, ddata
, &lhdr
, error_callback
, data
,
2899 entry
->u
, pfvec
, &function_addrs
,
2900 &function_addrs_count
);
2901 free_line_header (state
, &lhdr
, error_callback
, data
);
2905 /* Atomically store the information we just read into the unit.
2906 If another thread is simultaneously writing, it presumably
2907 read the same information, and we don't care which one we
2908 wind up with; we just leak the other one. We do have to
2909 write the lines field last, so that the acquire-loads above
2910 ensure that the other fields are set. */
2912 if (!state
->threaded
)
2914 u
->lines_count
= count
;
2915 u
->function_addrs
= function_addrs
;
2916 u
->function_addrs_count
= function_addrs_count
;
2921 backtrace_atomic_store_size_t (&u
->lines_count
, count
);
2922 backtrace_atomic_store_pointer (&u
->function_addrs
, function_addrs
);
2923 backtrace_atomic_store_size_t (&u
->function_addrs_count
,
2924 function_addrs_count
);
2925 backtrace_atomic_store_pointer (&u
->lines
, lines
);
2929 /* Now all fields of U have been initialized. */
2931 if (lines
== (struct line
*) (uintptr_t) -1)
2933 /* If reading the line number information failed in some way,
2934 try again to see if there is a better compilation unit for
2937 return dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2939 return callback (data
, pc
, NULL
, 0, NULL
);
2942 /* Search for PC within this unit. */
2944 ln
= (struct line
*) bsearch (&pc
, lines
, entry
->u
->lines_count
,
2945 sizeof (struct line
), line_search
);
2948 /* The PC is between the low_pc and high_pc attributes of the
2949 compilation unit, but no entry in the line table covers it.
2950 This implies that the start of the compilation unit has no
2951 line number information. */
2953 if (entry
->u
->abs_filename
== NULL
)
2955 const char *filename
;
2957 filename
= entry
->u
->filename
;
2958 if (filename
!= NULL
2959 && !IS_ABSOLUTE_PATH (filename
)
2960 && entry
->u
->comp_dir
!= NULL
)
2962 size_t filename_len
;
2967 filename_len
= strlen (filename
);
2968 dir
= entry
->u
->comp_dir
;
2969 dir_len
= strlen (dir
);
2970 s
= (char *) backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
2971 error_callback
, data
);
2977 memcpy (s
, dir
, dir_len
);
2978 /* FIXME: Should use backslash if DOS file system. */
2980 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
2983 entry
->u
->abs_filename
= filename
;
2986 return callback (data
, pc
, entry
->u
->abs_filename
, 0, NULL
);
2989 /* Search for function name within this unit. */
2991 if (entry
->u
->function_addrs_count
== 0)
2992 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2994 function_addrs
= ((struct function_addrs
*)
2995 bsearch (&pc
, entry
->u
->function_addrs
,
2996 entry
->u
->function_addrs_count
,
2997 sizeof (struct function_addrs
),
2998 function_addrs_search
));
2999 if (function_addrs
== NULL
)
3000 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
3002 /* If there are multiple function ranges that contain PC, use the
3003 last one, in order to produce predictable results. */
3005 while (((size_t) (function_addrs
- entry
->u
->function_addrs
+ 1)
3006 < entry
->u
->function_addrs_count
)
3007 && pc
>= (function_addrs
+ 1)->low
3008 && pc
< (function_addrs
+ 1)->high
)
3011 function
= function_addrs
->function
;
3013 filename
= ln
->filename
;
3014 lineno
= ln
->lineno
;
3016 ret
= report_inlined_functions (pc
, function
, callback
, data
,
3017 &filename
, &lineno
);
3021 return callback (data
, pc
, filename
, lineno
, function
->name
);
3025 /* Return the file/line information for a PC using the DWARF mapping
3026 we built earlier. */
3029 dwarf_fileline (struct backtrace_state
*state
, uintptr_t pc
,
3030 backtrace_full_callback callback
,
3031 backtrace_error_callback error_callback
, void *data
)
3033 struct dwarf_data
*ddata
;
3037 if (!state
->threaded
)
3039 for (ddata
= (struct dwarf_data
*) state
->fileline_data
;
3041 ddata
= ddata
->next
)
3043 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
3045 if (ret
!= 0 || found
)
3051 struct dwarf_data
**pp
;
3053 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
3056 ddata
= backtrace_atomic_load_pointer (pp
);
3060 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
3062 if (ret
!= 0 || found
)
3069 /* FIXME: See if any libraries have been dlopen'ed. */
3071 return callback (data
, pc
, NULL
, 0, NULL
);
3074 /* Initialize our data structures from the DWARF debug info for a
3075 file. Return NULL on failure. */
3077 static struct dwarf_data
*
3078 build_dwarf_data (struct backtrace_state
*state
,
3079 uintptr_t base_address
,
3080 const unsigned char *dwarf_info
,
3081 size_t dwarf_info_size
,
3082 const unsigned char *dwarf_line
,
3083 size_t dwarf_line_size
,
3084 const unsigned char *dwarf_abbrev
,
3085 size_t dwarf_abbrev_size
,
3086 const unsigned char *dwarf_ranges
,
3087 size_t dwarf_ranges_size
,
3088 const unsigned char *dwarf_str
,
3089 size_t dwarf_str_size
,
3091 struct dwarf_data
*altlink
,
3092 backtrace_error_callback error_callback
,
3095 struct unit_addrs_vector addrs_vec
;
3096 struct unit_addrs
*addrs
;
3098 struct unit_vector units_vec
;
3099 struct unit
**units
;
3101 struct dwarf_data
*fdata
;
3103 if (!build_address_map (state
, base_address
, dwarf_info
, dwarf_info_size
,
3104 dwarf_abbrev
, dwarf_abbrev_size
, dwarf_ranges
,
3105 dwarf_ranges_size
, dwarf_str
, dwarf_str_size
,
3106 is_bigendian
, altlink
, error_callback
, data
,
3107 &addrs_vec
, &units_vec
))
3110 if (!backtrace_vector_release (state
, &addrs_vec
.vec
, error_callback
, data
))
3112 if (!backtrace_vector_release (state
, &units_vec
.vec
, error_callback
, data
))
3114 addrs
= (struct unit_addrs
*) addrs_vec
.vec
.base
;
3115 units
= (struct unit
**) units_vec
.vec
.base
;
3116 addrs_count
= addrs_vec
.count
;
3117 units_count
= units_vec
.count
;
3118 backtrace_qsort (addrs
, addrs_count
, sizeof (struct unit_addrs
),
3119 unit_addrs_compare
);
3120 /* No qsort for units required, already sorted. */
3122 fdata
= ((struct dwarf_data
*)
3123 backtrace_alloc (state
, sizeof (struct dwarf_data
),
3124 error_callback
, data
));
3129 fdata
->altlink
= altlink
;
3130 fdata
->base_address
= base_address
;
3131 fdata
->addrs
= addrs
;
3132 fdata
->addrs_count
= addrs_count
;
3133 fdata
->units
= units
;
3134 fdata
->units_count
= units_count
;
3135 fdata
->dwarf_info
= dwarf_info
;
3136 fdata
->dwarf_info_size
= dwarf_info_size
;
3137 fdata
->dwarf_line
= dwarf_line
;
3138 fdata
->dwarf_line_size
= dwarf_line_size
;
3139 fdata
->dwarf_ranges
= dwarf_ranges
;
3140 fdata
->dwarf_ranges_size
= dwarf_ranges_size
;
3141 fdata
->dwarf_str
= dwarf_str
;
3142 fdata
->dwarf_str_size
= dwarf_str_size
;
3143 fdata
->is_bigendian
= is_bigendian
;
3144 memset (&fdata
->fvec
, 0, sizeof fdata
->fvec
);
3149 /* Build our data structures from the DWARF sections for a module.
3150 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
3154 backtrace_dwarf_add (struct backtrace_state
*state
,
3155 uintptr_t base_address
,
3156 const unsigned char *dwarf_info
,
3157 size_t dwarf_info_size
,
3158 const unsigned char *dwarf_line
,
3159 size_t dwarf_line_size
,
3160 const unsigned char *dwarf_abbrev
,
3161 size_t dwarf_abbrev_size
,
3162 const unsigned char *dwarf_ranges
,
3163 size_t dwarf_ranges_size
,
3164 const unsigned char *dwarf_str
,
3165 size_t dwarf_str_size
,
3167 struct dwarf_data
*fileline_altlink
,
3168 backtrace_error_callback error_callback
,
3169 void *data
, fileline
*fileline_fn
,
3170 struct dwarf_data
**fileline_entry
)
3172 struct dwarf_data
*fdata
;
3174 fdata
= build_dwarf_data (state
, base_address
, dwarf_info
, dwarf_info_size
,
3175 dwarf_line
, dwarf_line_size
, dwarf_abbrev
,
3176 dwarf_abbrev_size
, dwarf_ranges
, dwarf_ranges_size
,
3177 dwarf_str
, dwarf_str_size
, is_bigendian
,
3178 fileline_altlink
, error_callback
, data
);
3182 if (fileline_entry
!= NULL
)
3183 *fileline_entry
= fdata
;
3185 if (!state
->threaded
)
3187 struct dwarf_data
**pp
;
3189 for (pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
3199 struct dwarf_data
**pp
;
3201 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
3205 struct dwarf_data
*p
;
3207 p
= backtrace_atomic_load_pointer (pp
);
3215 if (__sync_bool_compare_and_swap (pp
, NULL
, fdata
))
3220 *fileline_fn
= dwarf_fileline
;