1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012-2018 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
134 /* A unsigned integer. */
136 /* A sigd integer. */
140 /* An offset to other data in the containing unit. */
142 /* An offset to other data within the .dwarf_info section. */
144 /* An offset to data in some other section. */
145 ATTR_VAL_REF_SECTION
,
146 /* A type signature. */
148 /* A block of data (not represented). */
150 /* An expression (not represented). */
154 /* An attribute value. */
158 /* How the value is stored in the field u. */
159 enum attr_val_encoding encoding
;
162 /* ATTR_VAL_ADDRESS, ATTR_VAL_UINT, ATTR_VAL_REF*. */
166 /* ATTR_VAL_STRING. */
168 /* ATTR_VAL_BLOCK not stored. */
172 /* The line number program header. */
176 /* The version of the line number information. */
178 /* The minimum instruction length. */
179 unsigned int min_insn_len
;
180 /* The maximum number of ops per instruction. */
181 unsigned int max_ops_per_insn
;
182 /* The line base for special opcodes. */
184 /* The line range for special opcodes. */
185 unsigned int line_range
;
186 /* The opcode base--the first special opcode. */
187 unsigned int opcode_base
;
188 /* Opcode lengths, indexed by opcode - 1. */
189 const unsigned char *opcode_lengths
;
190 /* The number of directory entries. */
192 /* The directory entries. */
194 /* The number of filenames. */
195 size_t filenames_count
;
197 const char **filenames
;
200 /* Map a single PC value to a file/line. We will keep a vector of
201 these sorted by PC value. Each file/line will be correct from the
202 PC up to the PC of the next entry if there is one. We allocate one
203 extra entry at the end so that we can use bsearch. */
209 /* File name. Many entries in the array are expected to point to
210 the same file name. */
211 const char *filename
;
214 /* Index of the object in the original array read from the DWARF
215 section, before it has been sorted. The index makes it possible
216 to use Quicksort and maintain stability. */
220 /* A growable vector of line number information. This is used while
221 reading the line numbers. */
225 /* Memory. This is an array of struct line. */
226 struct backtrace_vector vec
;
227 /* Number of valid mappings. */
231 /* A function described in the debug info. */
235 /* The name of the function. */
237 /* If this is an inlined function, the filename of the call
239 const char *caller_filename
;
240 /* If this is an inlined function, the line number of the call
243 /* Map PC ranges to inlined functions. */
244 struct function_addrs
*function_addrs
;
245 size_t function_addrs_count
;
248 /* An address range for a function. This maps a PC value to a
249 specific function. */
251 struct function_addrs
253 /* Range is LOW <= PC < HIGH. */
256 /* Function for this address range. */
257 struct function
*function
;
260 /* A growable vector of function address ranges. */
262 struct function_vector
264 /* Memory. This is an array of struct function_addrs. */
265 struct backtrace_vector vec
;
266 /* Number of address ranges present. */
270 /* A DWARF compilation unit. This only holds the information we need
271 to map a PC to a file and line. */
275 /* The first entry for this compilation unit. */
276 const unsigned char *unit_data
;
277 /* The length of the data for this compilation unit. */
278 size_t unit_data_len
;
279 /* The offset of UNIT_DATA from the start of the information for
280 this compilation unit. */
281 size_t unit_data_offset
;
284 /* Whether unit is DWARF64. */
288 /* Offset into line number information. */
290 /* Primary source file. */
291 const char *filename
;
292 /* Compilation command working directory. */
293 const char *comp_dir
;
294 /* Absolute file name, only set if needed. */
295 const char *abs_filename
;
296 /* The abbreviations for this unit. */
297 struct abbrevs abbrevs
;
299 /* The fields above this point are read in during initialization and
300 may be accessed freely. The fields below this point are read in
301 as needed, and therefore require care, as different threads may
302 try to initialize them simultaneously. */
304 /* PC to line number mapping. This is NULL if the values have not
305 been read. This is (struct line *) -1 if there was an error
306 reading the values. */
308 /* Number of entries in lines. */
310 /* PC ranges to function. */
311 struct function_addrs
*function_addrs
;
312 size_t function_addrs_count
;
315 /* An address range for a compilation unit. This maps a PC value to a
316 specific compilation unit. Note that we invert the representation
317 in DWARF: instead of listing the units and attaching a list of
318 ranges, we list the ranges and have each one point to the unit.
319 This lets us do a binary search to find the unit. */
323 /* Range is LOW <= PC < HIGH. */
326 /* Compilation unit for this address range. */
330 /* A growable vector of compilation unit address ranges. */
332 struct unit_addrs_vector
334 /* Memory. This is an array of struct unit_addrs. */
335 struct backtrace_vector vec
;
336 /* Number of address ranges present. */
340 /* The information we need to map a PC to a file and line. */
344 /* The data for the next file we know about. */
345 struct dwarf_data
*next
;
346 /* The base address for this file. */
347 uintptr_t base_address
;
348 /* A sorted list of address ranges. */
349 struct unit_addrs
*addrs
;
350 /* Number of address ranges in list. */
352 /* The unparsed .debug_info section. */
353 const unsigned char *dwarf_info
;
354 size_t dwarf_info_size
;
355 /* The unparsed .debug_line section. */
356 const unsigned char *dwarf_line
;
357 size_t dwarf_line_size
;
358 /* The unparsed .debug_ranges section. */
359 const unsigned char *dwarf_ranges
;
360 size_t dwarf_ranges_size
;
361 /* The unparsed .debug_str section. */
362 const unsigned char *dwarf_str
;
363 size_t dwarf_str_size
;
364 /* Whether the data is big-endian or not. */
366 /* A vector used for function addresses. We keep this here so that
367 we can grow the vector as we read more functions. */
368 struct function_vector fvec
;
371 /* Report an error for a DWARF buffer. */
374 dwarf_buf_error (struct dwarf_buf
*buf
, const char *msg
)
378 snprintf (b
, sizeof b
, "%s in %s at %d",
379 msg
, buf
->name
, (int) (buf
->buf
- buf
->start
));
380 buf
->error_callback (buf
->data
, b
, 0);
383 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
387 require (struct dwarf_buf
*buf
, size_t count
)
389 if (buf
->left
>= count
)
392 if (!buf
->reported_underflow
)
394 dwarf_buf_error (buf
, "DWARF underflow");
395 buf
->reported_underflow
= 1;
401 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
405 advance (struct dwarf_buf
*buf
, size_t count
)
407 if (!require (buf
, count
))
414 /* Read one zero-terminated string from BUF and advance past the string. */
417 read_string (struct dwarf_buf
*buf
)
419 const char *p
= (const char *)buf
->buf
;
420 size_t len
= strnlen (p
, buf
->left
);
422 /* - If len == left, we ran out of buffer before finding the zero terminator.
423 Generate an error by advancing len + 1.
424 - If len < left, advance by len + 1 to skip past the zero terminator. */
425 size_t count
= len
+ 1;
427 if (!advance (buf
, count
))
433 /* Read one byte from BUF and advance 1 byte. */
436 read_byte (struct dwarf_buf
*buf
)
438 const unsigned char *p
= buf
->buf
;
440 if (!advance (buf
, 1))
445 /* Read a signed char from BUF and advance 1 byte. */
448 read_sbyte (struct dwarf_buf
*buf
)
450 const unsigned char *p
= buf
->buf
;
452 if (!advance (buf
, 1))
454 return (*p
^ 0x80) - 0x80;
457 /* Read a uint16 from BUF and advance 2 bytes. */
460 read_uint16 (struct dwarf_buf
*buf
)
462 const unsigned char *p
= buf
->buf
;
464 if (!advance (buf
, 2))
466 if (buf
->is_bigendian
)
467 return ((uint16_t) p
[0] << 8) | (uint16_t) p
[1];
469 return ((uint16_t) p
[1] << 8) | (uint16_t) p
[0];
472 /* Read a uint32 from BUF and advance 4 bytes. */
475 read_uint32 (struct dwarf_buf
*buf
)
477 const unsigned char *p
= buf
->buf
;
479 if (!advance (buf
, 4))
481 if (buf
->is_bigendian
)
482 return (((uint32_t) p
[0] << 24) | ((uint32_t) p
[1] << 16)
483 | ((uint32_t) p
[2] << 8) | (uint32_t) p
[3]);
485 return (((uint32_t) p
[3] << 24) | ((uint32_t) p
[2] << 16)
486 | ((uint32_t) p
[1] << 8) | (uint32_t) p
[0]);
489 /* Read a uint64 from BUF and advance 8 bytes. */
492 read_uint64 (struct dwarf_buf
*buf
)
494 const unsigned char *p
= buf
->buf
;
496 if (!advance (buf
, 8))
498 if (buf
->is_bigendian
)
499 return (((uint64_t) p
[0] << 56) | ((uint64_t) p
[1] << 48)
500 | ((uint64_t) p
[2] << 40) | ((uint64_t) p
[3] << 32)
501 | ((uint64_t) p
[4] << 24) | ((uint64_t) p
[5] << 16)
502 | ((uint64_t) p
[6] << 8) | (uint64_t) p
[7]);
504 return (((uint64_t) p
[7] << 56) | ((uint64_t) p
[6] << 48)
505 | ((uint64_t) p
[5] << 40) | ((uint64_t) p
[4] << 32)
506 | ((uint64_t) p
[3] << 24) | ((uint64_t) p
[2] << 16)
507 | ((uint64_t) p
[1] << 8) | (uint64_t) p
[0]);
510 /* Read an offset from BUF and advance the appropriate number of
514 read_offset (struct dwarf_buf
*buf
, int is_dwarf64
)
517 return read_uint64 (buf
);
519 return read_uint32 (buf
);
522 /* Read an address from BUF and advance the appropriate number of
526 read_address (struct dwarf_buf
*buf
, int addrsize
)
531 return read_byte (buf
);
533 return read_uint16 (buf
);
535 return read_uint32 (buf
);
537 return read_uint64 (buf
);
539 dwarf_buf_error (buf
, "unrecognized address size");
544 /* Return whether a value is the highest possible address, given the
548 is_highest_address (uint64_t address
, int addrsize
)
553 return address
== (unsigned char) -1;
555 return address
== (uint16_t) -1;
557 return address
== (uint32_t) -1;
559 return address
== (uint64_t) -1;
565 /* Read an unsigned LEB128 number. */
568 read_uleb128 (struct dwarf_buf
*buf
)
580 const unsigned char *p
;
583 if (!advance (buf
, 1))
587 ret
|= ((uint64_t) (b
& 0x7f)) << shift
;
590 dwarf_buf_error (buf
, "LEB128 overflows uint64_t");
595 while ((b
& 0x80) != 0);
600 /* Read a signed LEB128 number. */
603 read_sleb128 (struct dwarf_buf
*buf
)
615 const unsigned char *p
;
618 if (!advance (buf
, 1))
622 val
|= ((uint64_t) (b
& 0x7f)) << shift
;
625 dwarf_buf_error (buf
, "signed LEB128 overflows uint64_t");
630 while ((b
& 0x80) != 0);
632 if ((b
& 0x40) != 0 && shift
< 64)
633 val
|= ((uint64_t) -1) << shift
;
635 return (int64_t) val
;
638 /* Return the length of an LEB128 number. */
641 leb128_len (const unsigned char *p
)
646 while ((*p
& 0x80) != 0)
654 /* Read initial_length from BUF and advance the appropriate number of bytes. */
657 read_initial_length (struct dwarf_buf
*buf
, int *is_dwarf64
)
661 len
= read_uint32 (buf
);
662 if (len
== 0xffffffff)
664 len
= read_uint64 (buf
);
673 /* Free an abbreviations structure. */
676 free_abbrevs (struct backtrace_state
*state
, struct abbrevs
*abbrevs
,
677 backtrace_error_callback error_callback
, void *data
)
681 for (i
= 0; i
< abbrevs
->num_abbrevs
; ++i
)
682 backtrace_free (state
, abbrevs
->abbrevs
[i
].attrs
,
683 abbrevs
->abbrevs
[i
].num_attrs
* sizeof (struct attr
),
684 error_callback
, data
);
685 backtrace_free (state
, abbrevs
->abbrevs
,
686 abbrevs
->num_abbrevs
* sizeof (struct abbrev
),
687 error_callback
, data
);
688 abbrevs
->num_abbrevs
= 0;
689 abbrevs
->abbrevs
= NULL
;
692 /* Read an attribute value. Returns 1 on success, 0 on failure. If
693 the value can be represented as a uint64_t, sets *VAL and sets
694 *IS_VALID to 1. We don't try to store the value of other attribute
695 forms, because we don't care about them. */
698 read_attribute (enum dwarf_form form
, struct dwarf_buf
*buf
,
699 int is_dwarf64
, int version
, int addrsize
,
700 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
701 struct attr_val
*val
)
703 /* Avoid warnings about val.u.FIELD may be used uninitialized if
704 this function is inlined. The warnings aren't valid but can
705 occur because the different fields are set and used
707 memset (val
, 0, sizeof *val
);
712 val
->encoding
= ATTR_VAL_ADDRESS
;
713 val
->u
.uint
= read_address (buf
, addrsize
);
716 val
->encoding
= ATTR_VAL_BLOCK
;
717 return advance (buf
, read_uint16 (buf
));
719 val
->encoding
= ATTR_VAL_BLOCK
;
720 return advance (buf
, read_uint32 (buf
));
722 val
->encoding
= ATTR_VAL_UINT
;
723 val
->u
.uint
= read_uint16 (buf
);
726 val
->encoding
= ATTR_VAL_UINT
;
727 val
->u
.uint
= read_uint32 (buf
);
730 val
->encoding
= ATTR_VAL_UINT
;
731 val
->u
.uint
= read_uint64 (buf
);
734 val
->encoding
= ATTR_VAL_STRING
;
735 val
->u
.string
= read_string (buf
);
736 return val
->u
.string
== NULL
? 0 : 1;
738 val
->encoding
= ATTR_VAL_BLOCK
;
739 return advance (buf
, read_uleb128 (buf
));
741 val
->encoding
= ATTR_VAL_BLOCK
;
742 return advance (buf
, read_byte (buf
));
744 val
->encoding
= ATTR_VAL_UINT
;
745 val
->u
.uint
= read_byte (buf
);
748 val
->encoding
= ATTR_VAL_UINT
;
749 val
->u
.uint
= read_byte (buf
);
752 val
->encoding
= ATTR_VAL_SINT
;
753 val
->u
.sint
= read_sleb128 (buf
);
759 offset
= read_offset (buf
, is_dwarf64
);
760 if (offset
>= dwarf_str_size
)
762 dwarf_buf_error (buf
, "DW_FORM_strp out of range");
765 val
->encoding
= ATTR_VAL_STRING
;
766 val
->u
.string
= (const char *) dwarf_str
+ offset
;
770 val
->encoding
= ATTR_VAL_UINT
;
771 val
->u
.uint
= read_uleb128 (buf
);
773 case DW_FORM_ref_addr
:
774 val
->encoding
= ATTR_VAL_REF_INFO
;
776 val
->u
.uint
= read_address (buf
, addrsize
);
778 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
781 val
->encoding
= ATTR_VAL_REF_UNIT
;
782 val
->u
.uint
= read_byte (buf
);
785 val
->encoding
= ATTR_VAL_REF_UNIT
;
786 val
->u
.uint
= read_uint16 (buf
);
789 val
->encoding
= ATTR_VAL_REF_UNIT
;
790 val
->u
.uint
= read_uint32 (buf
);
793 val
->encoding
= ATTR_VAL_REF_UNIT
;
794 val
->u
.uint
= read_uint64 (buf
);
796 case DW_FORM_ref_udata
:
797 val
->encoding
= ATTR_VAL_REF_UNIT
;
798 val
->u
.uint
= read_uleb128 (buf
);
800 case DW_FORM_indirect
:
804 form
= read_uleb128 (buf
);
805 return read_attribute ((enum dwarf_form
) form
, buf
, is_dwarf64
,
806 version
, addrsize
, dwarf_str
, dwarf_str_size
,
809 case DW_FORM_sec_offset
:
810 val
->encoding
= ATTR_VAL_REF_SECTION
;
811 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
813 case DW_FORM_exprloc
:
814 val
->encoding
= ATTR_VAL_EXPR
;
815 return advance (buf
, read_uleb128 (buf
));
816 case DW_FORM_flag_present
:
817 val
->encoding
= ATTR_VAL_UINT
;
820 case DW_FORM_ref_sig8
:
821 val
->encoding
= ATTR_VAL_REF_TYPE
;
822 val
->u
.uint
= read_uint64 (buf
);
824 case DW_FORM_GNU_addr_index
:
825 val
->encoding
= ATTR_VAL_REF_SECTION
;
826 val
->u
.uint
= read_uleb128 (buf
);
828 case DW_FORM_GNU_str_index
:
829 val
->encoding
= ATTR_VAL_REF_SECTION
;
830 val
->u
.uint
= read_uleb128 (buf
);
832 case DW_FORM_GNU_ref_alt
:
833 val
->encoding
= ATTR_VAL_REF_SECTION
;
834 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
836 case DW_FORM_GNU_strp_alt
:
837 val
->encoding
= ATTR_VAL_REF_SECTION
;
838 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
841 dwarf_buf_error (buf
, "unrecognized DWARF form");
846 /* Compare function_addrs for qsort. When ranges are nested, make the
847 smallest one sort last. */
850 function_addrs_compare (const void *v1
, const void *v2
)
852 const struct function_addrs
*a1
= (const struct function_addrs
*) v1
;
853 const struct function_addrs
*a2
= (const struct function_addrs
*) v2
;
855 if (a1
->low
< a2
->low
)
857 if (a1
->low
> a2
->low
)
859 if (a1
->high
< a2
->high
)
861 if (a1
->high
> a2
->high
)
863 return strcmp (a1
->function
->name
, a2
->function
->name
);
866 /* Compare a PC against a function_addrs for bsearch. Note that if
867 there are multiple ranges containing PC, which one will be returned
868 is unpredictable. We compensate for that in dwarf_fileline. */
871 function_addrs_search (const void *vkey
, const void *ventry
)
873 const uintptr_t *key
= (const uintptr_t *) vkey
;
874 const struct function_addrs
*entry
= (const struct function_addrs
*) ventry
;
880 else if (pc
>= entry
->high
)
886 /* Add a new compilation unit address range to a vector. Returns 1 on
887 success, 0 on failure. */
890 add_unit_addr (struct backtrace_state
*state
, uintptr_t base_address
,
891 struct unit_addrs addrs
,
892 backtrace_error_callback error_callback
, void *data
,
893 struct unit_addrs_vector
*vec
)
895 struct unit_addrs
*p
;
897 /* Add in the base address of the module here, so that we can look
898 up the PC directly. */
899 addrs
.low
+= base_address
;
900 addrs
.high
+= base_address
;
902 /* Try to merge with the last entry. */
905 p
= (struct unit_addrs
*) vec
->vec
.base
+ (vec
->count
- 1);
906 if ((addrs
.low
== p
->high
|| addrs
.low
== p
->high
+ 1)
909 if (addrs
.high
> p
->high
)
910 p
->high
= addrs
.high
;
915 p
= ((struct unit_addrs
*)
916 backtrace_vector_grow (state
, sizeof (struct unit_addrs
),
917 error_callback
, data
, &vec
->vec
));
926 /* Free a unit address vector. */
929 free_unit_addrs_vector (struct backtrace_state
*state
,
930 struct unit_addrs_vector
*vec
,
931 backtrace_error_callback error_callback
, void *data
)
933 struct unit_addrs
*addrs
;
936 addrs
= (struct unit_addrs
*) vec
->vec
.base
;
937 for (i
= 0; i
< vec
->count
; ++i
)
938 free_abbrevs (state
, &addrs
[i
].u
->abbrevs
, error_callback
, data
);
941 /* Compare unit_addrs for qsort. When ranges are nested, make the
942 smallest one sort last. */
945 unit_addrs_compare (const void *v1
, const void *v2
)
947 const struct unit_addrs
*a1
= (const struct unit_addrs
*) v1
;
948 const struct unit_addrs
*a2
= (const struct unit_addrs
*) v2
;
950 if (a1
->low
< a2
->low
)
952 if (a1
->low
> a2
->low
)
954 if (a1
->high
< a2
->high
)
956 if (a1
->high
> a2
->high
)
958 if (a1
->u
->lineoff
< a2
->u
->lineoff
)
960 if (a1
->u
->lineoff
> a2
->u
->lineoff
)
965 /* Compare a PC against a unit_addrs for bsearch. Note that if there
966 are multiple ranges containing PC, which one will be returned is
967 unpredictable. We compensate for that in dwarf_fileline. */
970 unit_addrs_search (const void *vkey
, const void *ventry
)
972 const uintptr_t *key
= (const uintptr_t *) vkey
;
973 const struct unit_addrs
*entry
= (const struct unit_addrs
*) ventry
;
979 else if (pc
>= entry
->high
)
985 /* Sort the line vector by PC. We want a stable sort here to maintain
986 the order of lines for the same PC values. Since the sequence is
987 being sorted in place, their addresses cannot be relied on to
988 maintain stability. That is the purpose of the index member. */
991 line_compare (const void *v1
, const void *v2
)
993 const struct line
*ln1
= (const struct line
*) v1
;
994 const struct line
*ln2
= (const struct line
*) v2
;
996 if (ln1
->pc
< ln2
->pc
)
998 else if (ln1
->pc
> ln2
->pc
)
1000 else if (ln1
->idx
< ln2
->idx
)
1002 else if (ln1
->idx
> ln2
->idx
)
1008 /* Find a PC in a line vector. We always allocate an extra entry at
1009 the end of the lines vector, so that this routine can safely look
1010 at the next entry. Note that when there are multiple mappings for
1011 the same PC value, this will return the last one. */
1014 line_search (const void *vkey
, const void *ventry
)
1016 const uintptr_t *key
= (const uintptr_t *) vkey
;
1017 const struct line
*entry
= (const struct line
*) ventry
;
1023 else if (pc
>= (entry
+ 1)->pc
)
1029 /* Sort the abbrevs by the abbrev code. This function is passed to
1030 both qsort and bsearch. */
1033 abbrev_compare (const void *v1
, const void *v2
)
1035 const struct abbrev
*a1
= (const struct abbrev
*) v1
;
1036 const struct abbrev
*a2
= (const struct abbrev
*) v2
;
1038 if (a1
->code
< a2
->code
)
1040 else if (a1
->code
> a2
->code
)
1044 /* This really shouldn't happen. It means there are two
1045 different abbrevs with the same code, and that means we don't
1046 know which one lookup_abbrev should return. */
1051 /* Read the abbreviation table for a compilation unit. Returns 1 on
1052 success, 0 on failure. */
1055 read_abbrevs (struct backtrace_state
*state
, uint64_t abbrev_offset
,
1056 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1057 int is_bigendian
, backtrace_error_callback error_callback
,
1058 void *data
, struct abbrevs
*abbrevs
)
1060 struct dwarf_buf abbrev_buf
;
1061 struct dwarf_buf count_buf
;
1064 abbrevs
->num_abbrevs
= 0;
1065 abbrevs
->abbrevs
= NULL
;
1067 if (abbrev_offset
>= dwarf_abbrev_size
)
1069 error_callback (data
, "abbrev offset out of range", 0);
1073 abbrev_buf
.name
= ".debug_abbrev";
1074 abbrev_buf
.start
= dwarf_abbrev
;
1075 abbrev_buf
.buf
= dwarf_abbrev
+ abbrev_offset
;
1076 abbrev_buf
.left
= dwarf_abbrev_size
- abbrev_offset
;
1077 abbrev_buf
.is_bigendian
= is_bigendian
;
1078 abbrev_buf
.error_callback
= error_callback
;
1079 abbrev_buf
.data
= data
;
1080 abbrev_buf
.reported_underflow
= 0;
1082 /* Count the number of abbrevs in this list. */
1084 count_buf
= abbrev_buf
;
1086 while (read_uleb128 (&count_buf
) != 0)
1088 if (count_buf
.reported_underflow
)
1092 read_uleb128 (&count_buf
);
1093 // Skip has_children.
1094 read_byte (&count_buf
);
1096 while (read_uleb128 (&count_buf
) != 0)
1097 read_uleb128 (&count_buf
);
1098 // Skip form of last attribute.
1099 read_uleb128 (&count_buf
);
1102 if (count_buf
.reported_underflow
)
1105 if (num_abbrevs
== 0)
1108 abbrevs
->abbrevs
= ((struct abbrev
*)
1109 backtrace_alloc (state
,
1110 num_abbrevs
* sizeof (struct abbrev
),
1111 error_callback
, data
));
1112 if (abbrevs
->abbrevs
== NULL
)
1114 abbrevs
->num_abbrevs
= num_abbrevs
;
1115 memset (abbrevs
->abbrevs
, 0, num_abbrevs
* sizeof (struct abbrev
));
1125 if (abbrev_buf
.reported_underflow
)
1128 code
= read_uleb128 (&abbrev_buf
);
1133 a
.tag
= (enum dwarf_tag
) read_uleb128 (&abbrev_buf
);
1134 a
.has_children
= read_byte (&abbrev_buf
);
1136 count_buf
= abbrev_buf
;
1138 while (read_uleb128 (&count_buf
) != 0)
1141 read_uleb128 (&count_buf
);
1147 read_uleb128 (&abbrev_buf
);
1148 read_uleb128 (&abbrev_buf
);
1152 attrs
= ((struct attr
*)
1153 backtrace_alloc (state
, num_attrs
* sizeof *attrs
,
1154 error_callback
, data
));
1163 name
= read_uleb128 (&abbrev_buf
);
1164 form
= read_uleb128 (&abbrev_buf
);
1167 attrs
[num_attrs
].name
= (enum dwarf_attribute
) name
;
1168 attrs
[num_attrs
].form
= (enum dwarf_form
) form
;
1173 a
.num_attrs
= num_attrs
;
1176 abbrevs
->abbrevs
[num_abbrevs
] = a
;
1180 backtrace_qsort (abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1181 sizeof (struct abbrev
), abbrev_compare
);
1186 free_abbrevs (state
, abbrevs
, error_callback
, data
);
1190 /* Return the abbrev information for an abbrev code. */
1192 static const struct abbrev
*
1193 lookup_abbrev (struct abbrevs
*abbrevs
, uint64_t code
,
1194 backtrace_error_callback error_callback
, void *data
)
1199 /* With GCC, where abbrevs are simply numbered in order, we should
1200 be able to just look up the entry. */
1201 if (code
- 1 < abbrevs
->num_abbrevs
1202 && abbrevs
->abbrevs
[code
- 1].code
== code
)
1203 return &abbrevs
->abbrevs
[code
- 1];
1205 /* Otherwise we have to search. */
1206 memset (&key
, 0, sizeof key
);
1208 p
= bsearch (&key
, abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1209 sizeof (struct abbrev
), abbrev_compare
);
1212 error_callback (data
, "invalid abbreviation code", 0);
1215 return (const struct abbrev
*) p
;
1218 /* Add non-contiguous address ranges for a compilation unit. Returns
1219 1 on success, 0 on failure. */
1222 add_unit_ranges (struct backtrace_state
*state
, uintptr_t base_address
,
1223 struct unit
*u
, uint64_t ranges
, uint64_t base
,
1224 int is_bigendian
, const unsigned char *dwarf_ranges
,
1225 size_t dwarf_ranges_size
,
1226 backtrace_error_callback error_callback
, void *data
,
1227 struct unit_addrs_vector
*addrs
)
1229 struct dwarf_buf ranges_buf
;
1231 if (ranges
>= dwarf_ranges_size
)
1233 error_callback (data
, "ranges offset out of range", 0);
1237 ranges_buf
.name
= ".debug_ranges";
1238 ranges_buf
.start
= dwarf_ranges
;
1239 ranges_buf
.buf
= dwarf_ranges
+ ranges
;
1240 ranges_buf
.left
= dwarf_ranges_size
- ranges
;
1241 ranges_buf
.is_bigendian
= is_bigendian
;
1242 ranges_buf
.error_callback
= error_callback
;
1243 ranges_buf
.data
= data
;
1244 ranges_buf
.reported_underflow
= 0;
1251 if (ranges_buf
.reported_underflow
)
1254 low
= read_address (&ranges_buf
, u
->addrsize
);
1255 high
= read_address (&ranges_buf
, u
->addrsize
);
1257 if (low
== 0 && high
== 0)
1260 if (is_highest_address (low
, u
->addrsize
))
1264 struct unit_addrs a
;
1267 a
.high
= high
+ base
;
1269 if (!add_unit_addr (state
, base_address
, a
, error_callback
, data
,
1275 if (ranges_buf
.reported_underflow
)
1281 /* Find the address range covered by a compilation unit, reading from
1282 UNIT_BUF and adding values to U. Returns 1 if all data could be
1283 read, 0 if there is some error. */
1286 find_address_ranges (struct backtrace_state
*state
, uintptr_t base_address
,
1287 struct dwarf_buf
*unit_buf
,
1288 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
1289 const unsigned char *dwarf_ranges
,
1290 size_t dwarf_ranges_size
,
1291 int is_bigendian
, backtrace_error_callback error_callback
,
1292 void *data
, struct unit
*u
,
1293 struct unit_addrs_vector
*addrs
)
1295 while (unit_buf
->left
> 0)
1298 const struct abbrev
*abbrev
;
1303 int highpc_is_relative
;
1308 code
= read_uleb128 (unit_buf
);
1312 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
1320 highpc_is_relative
= 0;
1323 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1325 struct attr_val val
;
1327 if (!read_attribute (abbrev
->attrs
[i
].form
, unit_buf
,
1328 u
->is_dwarf64
, u
->version
, u
->addrsize
,
1329 dwarf_str
, dwarf_str_size
, &val
))
1332 switch (abbrev
->attrs
[i
].name
)
1335 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1343 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1345 highpc
= val
.u
.uint
;
1348 else if (val
.encoding
== ATTR_VAL_UINT
)
1350 highpc
= val
.u
.uint
;
1352 highpc_is_relative
= 1;
1357 if (val
.encoding
== ATTR_VAL_UINT
1358 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1360 ranges
= val
.u
.uint
;
1365 case DW_AT_stmt_list
:
1366 if (abbrev
->tag
== DW_TAG_compile_unit
1367 && (val
.encoding
== ATTR_VAL_UINT
1368 || val
.encoding
== ATTR_VAL_REF_SECTION
))
1369 u
->lineoff
= val
.u
.uint
;
1373 if (abbrev
->tag
== DW_TAG_compile_unit
1374 && val
.encoding
== ATTR_VAL_STRING
)
1375 u
->filename
= val
.u
.string
;
1378 case DW_AT_comp_dir
:
1379 if (abbrev
->tag
== DW_TAG_compile_unit
1380 && val
.encoding
== ATTR_VAL_STRING
)
1381 u
->comp_dir
= val
.u
.string
;
1389 if (abbrev
->tag
== DW_TAG_compile_unit
1390 || abbrev
->tag
== DW_TAG_subprogram
)
1394 if (!add_unit_ranges (state
, base_address
, u
, ranges
, lowpc
,
1395 is_bigendian
, dwarf_ranges
,
1396 dwarf_ranges_size
, error_callback
,
1400 else if (have_lowpc
&& have_highpc
)
1402 struct unit_addrs a
;
1404 if (highpc_is_relative
)
1410 if (!add_unit_addr (state
, base_address
, a
, error_callback
, data
,
1415 /* If we found the PC range in the DW_TAG_compile_unit, we
1417 if (abbrev
->tag
== DW_TAG_compile_unit
1418 && (have_ranges
|| (have_lowpc
&& have_highpc
)))
1422 if (abbrev
->has_children
)
1424 if (!find_address_ranges (state
, base_address
, unit_buf
,
1425 dwarf_str
, dwarf_str_size
,
1426 dwarf_ranges
, dwarf_ranges_size
,
1427 is_bigendian
, error_callback
, data
,
1436 /* Build a mapping from address ranges to the compilation units where
1437 the line number information for that range can be found. Returns 1
1438 on success, 0 on failure. */
1441 build_address_map (struct backtrace_state
*state
, uintptr_t base_address
,
1442 const unsigned char *dwarf_info
, size_t dwarf_info_size
,
1443 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1444 const unsigned char *dwarf_ranges
, size_t dwarf_ranges_size
,
1445 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
1446 int is_bigendian
, backtrace_error_callback error_callback
,
1447 void *data
, struct unit_addrs_vector
*addrs
)
1449 struct dwarf_buf info
;
1450 struct abbrevs abbrevs
;
1452 memset (&addrs
->vec
, 0, sizeof addrs
->vec
);
1455 /* Read through the .debug_info section. FIXME: Should we use the
1456 .debug_aranges section? gdb and addr2line don't use it, but I'm
1459 info
.name
= ".debug_info";
1460 info
.start
= dwarf_info
;
1461 info
.buf
= dwarf_info
;
1462 info
.left
= dwarf_info_size
;
1463 info
.is_bigendian
= is_bigendian
;
1464 info
.error_callback
= error_callback
;
1466 info
.reported_underflow
= 0;
1468 memset (&abbrevs
, 0, sizeof abbrevs
);
1469 while (info
.left
> 0)
1471 const unsigned char *unit_data_start
;
1474 struct dwarf_buf unit_buf
;
1476 uint64_t abbrev_offset
;
1480 if (info
.reported_underflow
)
1483 unit_data_start
= info
.buf
;
1485 len
= read_initial_length (&info
, &is_dwarf64
);
1487 unit_buf
.left
= len
;
1489 if (!advance (&info
, len
))
1492 version
= read_uint16 (&unit_buf
);
1493 if (version
< 2 || version
> 4)
1495 dwarf_buf_error (&unit_buf
, "unrecognized DWARF version");
1499 abbrev_offset
= read_offset (&unit_buf
, is_dwarf64
);
1500 if (!read_abbrevs (state
, abbrev_offset
, dwarf_abbrev
, dwarf_abbrev_size
,
1501 is_bigendian
, error_callback
, data
, &abbrevs
))
1504 addrsize
= read_byte (&unit_buf
);
1506 u
= ((struct unit
*)
1507 backtrace_alloc (state
, sizeof *u
, error_callback
, data
));
1510 u
->unit_data
= unit_buf
.buf
;
1511 u
->unit_data_len
= unit_buf
.left
;
1512 u
->unit_data_offset
= unit_buf
.buf
- unit_data_start
;
1513 u
->version
= version
;
1514 u
->is_dwarf64
= is_dwarf64
;
1515 u
->addrsize
= addrsize
;
1518 u
->abs_filename
= NULL
;
1520 u
->abbrevs
= abbrevs
;
1521 memset (&abbrevs
, 0, sizeof abbrevs
);
1523 /* The actual line number mappings will be read as needed. */
1526 u
->function_addrs
= NULL
;
1527 u
->function_addrs_count
= 0;
1529 if (!find_address_ranges (state
, base_address
, &unit_buf
,
1530 dwarf_str
, dwarf_str_size
,
1531 dwarf_ranges
, dwarf_ranges_size
,
1532 is_bigendian
, error_callback
, data
,
1535 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1536 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1540 if (unit_buf
.reported_underflow
)
1542 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1543 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1547 if (info
.reported_underflow
)
1553 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1554 free_unit_addrs_vector (state
, addrs
, error_callback
, data
);
1558 /* Add a new mapping to the vector of line mappings that we are
1559 building. Returns 1 on success, 0 on failure. */
1562 add_line (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1563 uintptr_t pc
, const char *filename
, int lineno
,
1564 backtrace_error_callback error_callback
, void *data
,
1565 struct line_vector
*vec
)
1569 /* If we are adding the same mapping, ignore it. This can happen
1570 when using discriminators. */
1573 ln
= (struct line
*) vec
->vec
.base
+ (vec
->count
- 1);
1574 if (pc
== ln
->pc
&& filename
== ln
->filename
&& lineno
== ln
->lineno
)
1578 ln
= ((struct line
*)
1579 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1584 /* Add in the base address here, so that we can look up the PC
1586 ln
->pc
= pc
+ ddata
->base_address
;
1588 ln
->filename
= filename
;
1589 ln
->lineno
= lineno
;
1590 ln
->idx
= vec
->count
;
1597 /* Free the line header information. */
1600 free_line_header (struct backtrace_state
*state
, struct line_header
*hdr
,
1601 backtrace_error_callback error_callback
, void *data
)
1603 if (hdr
->dirs_count
!= 0)
1604 backtrace_free (state
, hdr
->dirs
, hdr
->dirs_count
* sizeof (const char *),
1605 error_callback
, data
);
1606 backtrace_free (state
, hdr
->filenames
,
1607 hdr
->filenames_count
* sizeof (char *),
1608 error_callback
, data
);
1611 /* Read the line header. Return 1 on success, 0 on failure. */
1614 read_line_header (struct backtrace_state
*state
, struct unit
*u
,
1615 int is_dwarf64
, struct dwarf_buf
*line_buf
,
1616 struct line_header
*hdr
)
1619 struct dwarf_buf hdr_buf
;
1620 const unsigned char *p
;
1621 const unsigned char *pend
;
1624 hdr
->version
= read_uint16 (line_buf
);
1625 if (hdr
->version
< 2 || hdr
->version
> 4)
1627 dwarf_buf_error (line_buf
, "unsupported line number version");
1631 hdrlen
= read_offset (line_buf
, is_dwarf64
);
1633 hdr_buf
= *line_buf
;
1634 hdr_buf
.left
= hdrlen
;
1636 if (!advance (line_buf
, hdrlen
))
1639 hdr
->min_insn_len
= read_byte (&hdr_buf
);
1640 if (hdr
->version
< 4)
1641 hdr
->max_ops_per_insn
= 1;
1643 hdr
->max_ops_per_insn
= read_byte (&hdr_buf
);
1645 /* We don't care about default_is_stmt. */
1646 read_byte (&hdr_buf
);
1648 hdr
->line_base
= read_sbyte (&hdr_buf
);
1649 hdr
->line_range
= read_byte (&hdr_buf
);
1651 hdr
->opcode_base
= read_byte (&hdr_buf
);
1652 hdr
->opcode_lengths
= hdr_buf
.buf
;
1653 if (!advance (&hdr_buf
, hdr
->opcode_base
- 1))
1656 /* Count the number of directory entries. */
1657 hdr
->dirs_count
= 0;
1659 pend
= p
+ hdr_buf
.left
;
1660 while (p
< pend
&& *p
!= '\0')
1662 p
+= strnlen((const char *) p
, pend
- p
) + 1;
1667 if (hdr
->dirs_count
!= 0)
1669 hdr
->dirs
= ((const char **)
1670 backtrace_alloc (state
,
1671 hdr
->dirs_count
* sizeof (const char *),
1672 line_buf
->error_callback
, line_buf
->data
));
1673 if (hdr
->dirs
== NULL
)
1678 while (*hdr_buf
.buf
!= '\0')
1680 if (hdr_buf
.reported_underflow
)
1683 hdr
->dirs
[i
] = read_string (&hdr_buf
);
1684 if (hdr
->dirs
[i
] == NULL
)
1688 if (!advance (&hdr_buf
, 1))
1691 /* Count the number of file entries. */
1692 hdr
->filenames_count
= 0;
1694 pend
= p
+ hdr_buf
.left
;
1695 while (p
< pend
&& *p
!= '\0')
1697 p
+= strnlen ((const char *) p
, pend
- p
) + 1;
1698 p
+= leb128_len (p
);
1699 p
+= leb128_len (p
);
1700 p
+= leb128_len (p
);
1701 ++hdr
->filenames_count
;
1704 hdr
->filenames
= ((const char **)
1705 backtrace_alloc (state
,
1706 hdr
->filenames_count
* sizeof (char *),
1707 line_buf
->error_callback
,
1709 if (hdr
->filenames
== NULL
)
1712 while (*hdr_buf
.buf
!= '\0')
1714 const char *filename
;
1717 if (hdr_buf
.reported_underflow
)
1720 filename
= read_string (&hdr_buf
);
1721 if (filename
== NULL
)
1723 dir_index
= read_uleb128 (&hdr_buf
);
1724 if (IS_ABSOLUTE_PATH (filename
)
1725 || (dir_index
== 0 && u
->comp_dir
== NULL
))
1726 hdr
->filenames
[i
] = filename
;
1731 size_t filename_len
;
1736 else if (dir_index
- 1 < hdr
->dirs_count
)
1737 dir
= hdr
->dirs
[dir_index
- 1];
1740 dwarf_buf_error (line_buf
,
1741 ("invalid directory index in "
1742 "line number program header"));
1745 dir_len
= strlen (dir
);
1746 filename_len
= strlen (filename
);
1748 backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
1749 line_buf
->error_callback
, line_buf
->data
));
1752 memcpy (s
, dir
, dir_len
);
1753 /* FIXME: If we are on a DOS-based file system, and the
1754 directory or the file name use backslashes, then we
1755 should use a backslash here. */
1757 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
1758 hdr
->filenames
[i
] = s
;
1761 /* Ignore the modification time and size. */
1762 read_uleb128 (&hdr_buf
);
1763 read_uleb128 (&hdr_buf
);
1768 if (hdr_buf
.reported_underflow
)
1774 /* Read the line program, adding line mappings to VEC. Return 1 on
1775 success, 0 on failure. */
1778 read_line_program (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1779 struct unit
*u
, const struct line_header
*hdr
,
1780 struct dwarf_buf
*line_buf
, struct line_vector
*vec
)
1783 unsigned int op_index
;
1784 const char *reset_filename
;
1785 const char *filename
;
1790 if (hdr
->filenames_count
> 0)
1791 reset_filename
= hdr
->filenames
[0];
1793 reset_filename
= "";
1794 filename
= reset_filename
;
1796 while (line_buf
->left
> 0)
1800 op
= read_byte (line_buf
);
1801 if (op
>= hdr
->opcode_base
)
1803 unsigned int advance
;
1805 /* Special opcode. */
1806 op
-= hdr
->opcode_base
;
1807 advance
= op
/ hdr
->line_range
;
1808 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1809 / hdr
->max_ops_per_insn
);
1810 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1811 lineno
+= hdr
->line_base
+ (int) (op
% hdr
->line_range
);
1812 add_line (state
, ddata
, address
, filename
, lineno
,
1813 line_buf
->error_callback
, line_buf
->data
, vec
);
1815 else if (op
== DW_LNS_extended_op
)
1819 len
= read_uleb128 (line_buf
);
1820 op
= read_byte (line_buf
);
1823 case DW_LNE_end_sequence
:
1824 /* FIXME: Should we mark the high PC here? It seems
1825 that we already have that information from the
1826 compilation unit. */
1829 filename
= reset_filename
;
1832 case DW_LNE_set_address
:
1833 address
= read_address (line_buf
, u
->addrsize
);
1835 case DW_LNE_define_file
:
1838 unsigned int dir_index
;
1840 f
= read_string (line_buf
);
1843 dir_index
= read_uleb128 (line_buf
);
1844 /* Ignore that time and length. */
1845 read_uleb128 (line_buf
);
1846 read_uleb128 (line_buf
);
1847 if (IS_ABSOLUTE_PATH (f
))
1858 else if (dir_index
- 1 < hdr
->dirs_count
)
1859 dir
= hdr
->dirs
[dir_index
- 1];
1862 dwarf_buf_error (line_buf
,
1863 ("invalid directory index "
1864 "in line number program"));
1867 dir_len
= strlen (dir
);
1870 backtrace_alloc (state
, dir_len
+ f_len
+ 2,
1871 line_buf
->error_callback
,
1875 memcpy (p
, dir
, dir_len
);
1876 /* FIXME: If we are on a DOS-based file system,
1877 and the directory or the file name use
1878 backslashes, then we should use a backslash
1881 memcpy (p
+ dir_len
+ 1, f
, f_len
+ 1);
1886 case DW_LNE_set_discriminator
:
1887 /* We don't care about discriminators. */
1888 read_uleb128 (line_buf
);
1891 if (!advance (line_buf
, len
- 1))
1901 add_line (state
, ddata
, address
, filename
, lineno
,
1902 line_buf
->error_callback
, line_buf
->data
, vec
);
1904 case DW_LNS_advance_pc
:
1908 advance
= read_uleb128 (line_buf
);
1909 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1910 / hdr
->max_ops_per_insn
);
1911 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1914 case DW_LNS_advance_line
:
1915 lineno
+= (int) read_sleb128 (line_buf
);
1917 case DW_LNS_set_file
:
1921 fileno
= read_uleb128 (line_buf
);
1926 if (fileno
- 1 >= hdr
->filenames_count
)
1928 dwarf_buf_error (line_buf
,
1929 ("invalid file number in "
1930 "line number program"));
1933 filename
= hdr
->filenames
[fileno
- 1];
1937 case DW_LNS_set_column
:
1938 read_uleb128 (line_buf
);
1940 case DW_LNS_negate_stmt
:
1942 case DW_LNS_set_basic_block
:
1944 case DW_LNS_const_add_pc
:
1946 unsigned int advance
;
1948 op
= 255 - hdr
->opcode_base
;
1949 advance
= op
/ hdr
->line_range
;
1950 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1951 / hdr
->max_ops_per_insn
);
1952 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1955 case DW_LNS_fixed_advance_pc
:
1956 address
+= read_uint16 (line_buf
);
1959 case DW_LNS_set_prologue_end
:
1961 case DW_LNS_set_epilogue_begin
:
1963 case DW_LNS_set_isa
:
1964 read_uleb128 (line_buf
);
1970 for (i
= hdr
->opcode_lengths
[op
- 1]; i
> 0; --i
)
1971 read_uleb128 (line_buf
);
1981 /* Read the line number information for a compilation unit. Returns 1
1982 on success, 0 on failure. */
1985 read_line_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1986 backtrace_error_callback error_callback
, void *data
,
1987 struct unit
*u
, struct line_header
*hdr
, struct line
**lines
,
1988 size_t *lines_count
)
1990 struct line_vector vec
;
1991 struct dwarf_buf line_buf
;
1996 memset (&vec
.vec
, 0, sizeof vec
.vec
);
1999 memset (hdr
, 0, sizeof *hdr
);
2001 if (u
->lineoff
!= (off_t
) (size_t) u
->lineoff
2002 || (size_t) u
->lineoff
>= ddata
->dwarf_line_size
)
2004 error_callback (data
, "unit line offset out of range", 0);
2008 line_buf
.name
= ".debug_line";
2009 line_buf
.start
= ddata
->dwarf_line
;
2010 line_buf
.buf
= ddata
->dwarf_line
+ u
->lineoff
;
2011 line_buf
.left
= ddata
->dwarf_line_size
- u
->lineoff
;
2012 line_buf
.is_bigendian
= ddata
->is_bigendian
;
2013 line_buf
.error_callback
= error_callback
;
2014 line_buf
.data
= data
;
2015 line_buf
.reported_underflow
= 0;
2017 len
= read_initial_length (&line_buf
, &is_dwarf64
);
2018 line_buf
.left
= len
;
2020 if (!read_line_header (state
, u
, is_dwarf64
, &line_buf
, hdr
))
2023 if (!read_line_program (state
, ddata
, u
, hdr
, &line_buf
, &vec
))
2026 if (line_buf
.reported_underflow
)
2031 /* This is not a failure in the sense of a generating an error,
2032 but it is a failure in that sense that we have no useful
2037 /* Allocate one extra entry at the end. */
2038 ln
= ((struct line
*)
2039 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
2043 ln
->pc
= (uintptr_t) -1;
2044 ln
->filename
= NULL
;
2048 if (!backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
))
2051 ln
= (struct line
*) vec
.vec
.base
;
2052 backtrace_qsort (ln
, vec
.count
, sizeof (struct line
), line_compare
);
2055 *lines_count
= vec
.count
;
2060 vec
.vec
.alc
+= vec
.vec
.size
;
2062 backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
);
2063 free_line_header (state
, hdr
, error_callback
, data
);
2064 *lines
= (struct line
*) (uintptr_t) -1;
2069 /* Read the name of a function from a DIE referenced by a
2070 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
2071 the same compilation unit. */
2074 read_referenced_name (struct dwarf_data
*ddata
, struct unit
*u
,
2075 uint64_t offset
, backtrace_error_callback error_callback
,
2078 struct dwarf_buf unit_buf
;
2080 const struct abbrev
*abbrev
;
2084 /* OFFSET is from the start of the data for this compilation unit.
2085 U->unit_data is the data, but it starts U->unit_data_offset bytes
2086 from the beginning. */
2088 if (offset
< u
->unit_data_offset
2089 || offset
- u
->unit_data_offset
>= u
->unit_data_len
)
2091 error_callback (data
,
2092 "abstract origin or specification out of range",
2097 offset
-= u
->unit_data_offset
;
2099 unit_buf
.name
= ".debug_info";
2100 unit_buf
.start
= ddata
->dwarf_info
;
2101 unit_buf
.buf
= u
->unit_data
+ offset
;
2102 unit_buf
.left
= u
->unit_data_len
- offset
;
2103 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2104 unit_buf
.error_callback
= error_callback
;
2105 unit_buf
.data
= data
;
2106 unit_buf
.reported_underflow
= 0;
2108 code
= read_uleb128 (&unit_buf
);
2111 dwarf_buf_error (&unit_buf
, "invalid abstract origin or specification");
2115 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2120 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2122 struct attr_val val
;
2124 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
,
2125 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2126 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2130 switch (abbrev
->attrs
[i
].name
)
2133 /* We prefer the linkage name if get one. */
2134 if (val
.encoding
== ATTR_VAL_STRING
)
2138 case DW_AT_linkage_name
:
2139 case DW_AT_MIPS_linkage_name
:
2140 if (val
.encoding
== ATTR_VAL_STRING
)
2141 return val
.u
.string
;
2144 case DW_AT_specification
:
2145 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2146 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2148 /* This refers to a specification defined in some other
2149 compilation unit. We can handle this case if we
2150 must, but it's harder. */
2153 if (val
.encoding
== ATTR_VAL_UINT
2154 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2158 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2159 error_callback
, data
);
2173 /* Add a single range to U that maps to function. Returns 1 on
2174 success, 0 on error. */
2177 add_function_range (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2178 struct function
*function
, uint64_t lowpc
, uint64_t highpc
,
2179 backtrace_error_callback error_callback
,
2180 void *data
, struct function_vector
*vec
)
2182 struct function_addrs
*p
;
2184 /* Add in the base address here, so that we can look up the PC
2186 lowpc
+= ddata
->base_address
;
2187 highpc
+= ddata
->base_address
;
2191 p
= (struct function_addrs
*) vec
->vec
.base
+ vec
->count
- 1;
2192 if ((lowpc
== p
->high
|| lowpc
== p
->high
+ 1)
2193 && function
== p
->function
)
2195 if (highpc
> p
->high
)
2201 p
= ((struct function_addrs
*)
2202 backtrace_vector_grow (state
, sizeof (struct function_addrs
),
2203 error_callback
, data
, &vec
->vec
));
2209 p
->function
= function
;
2214 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2218 add_function_ranges (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2219 struct unit
*u
, struct function
*function
,
2220 uint64_t ranges
, uint64_t base
,
2221 backtrace_error_callback error_callback
, void *data
,
2222 struct function_vector
*vec
)
2224 struct dwarf_buf ranges_buf
;
2226 if (ranges
>= ddata
->dwarf_ranges_size
)
2228 error_callback (data
, "function ranges offset out of range", 0);
2232 ranges_buf
.name
= ".debug_ranges";
2233 ranges_buf
.start
= ddata
->dwarf_ranges
;
2234 ranges_buf
.buf
= ddata
->dwarf_ranges
+ ranges
;
2235 ranges_buf
.left
= ddata
->dwarf_ranges_size
- ranges
;
2236 ranges_buf
.is_bigendian
= ddata
->is_bigendian
;
2237 ranges_buf
.error_callback
= error_callback
;
2238 ranges_buf
.data
= data
;
2239 ranges_buf
.reported_underflow
= 0;
2246 if (ranges_buf
.reported_underflow
)
2249 low
= read_address (&ranges_buf
, u
->addrsize
);
2250 high
= read_address (&ranges_buf
, u
->addrsize
);
2252 if (low
== 0 && high
== 0)
2255 if (is_highest_address (low
, u
->addrsize
))
2259 if (!add_function_range (state
, ddata
, function
, low
+ base
,
2260 high
+ base
, error_callback
, data
, vec
))
2265 if (ranges_buf
.reported_underflow
)
2271 /* Read one entry plus all its children. Add function addresses to
2272 VEC. Returns 1 on success, 0 on error. */
2275 read_function_entry (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2276 struct unit
*u
, uint64_t base
, struct dwarf_buf
*unit_buf
,
2277 const struct line_header
*lhdr
,
2278 backtrace_error_callback error_callback
, void *data
,
2279 struct function_vector
*vec_function
,
2280 struct function_vector
*vec_inlined
)
2282 while (unit_buf
->left
> 0)
2285 const struct abbrev
*abbrev
;
2287 struct function
*function
;
2288 struct function_vector
*vec
;
2294 int highpc_is_relative
;
2298 code
= read_uleb128 (unit_buf
);
2302 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2306 is_function
= (abbrev
->tag
== DW_TAG_subprogram
2307 || abbrev
->tag
== DW_TAG_entry_point
2308 || abbrev
->tag
== DW_TAG_inlined_subroutine
);
2310 if (abbrev
->tag
== DW_TAG_inlined_subroutine
)
2318 function
= ((struct function
*)
2319 backtrace_alloc (state
, sizeof *function
,
2320 error_callback
, data
));
2321 if (function
== NULL
)
2323 memset (function
, 0, sizeof *function
);
2330 highpc_is_relative
= 0;
2333 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2335 struct attr_val val
;
2337 if (!read_attribute (abbrev
->attrs
[i
].form
, unit_buf
,
2338 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2339 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2343 /* The compile unit sets the base address for any address
2344 ranges in the function entries. */
2345 if (abbrev
->tag
== DW_TAG_compile_unit
2346 && abbrev
->attrs
[i
].name
== DW_AT_low_pc
2347 && val
.encoding
== ATTR_VAL_ADDRESS
)
2352 switch (abbrev
->attrs
[i
].name
)
2354 case DW_AT_call_file
:
2355 if (val
.encoding
== ATTR_VAL_UINT
)
2357 if (val
.u
.uint
== 0)
2358 function
->caller_filename
= "";
2361 if (val
.u
.uint
- 1 >= lhdr
->filenames_count
)
2363 dwarf_buf_error (unit_buf
,
2364 ("invalid file number in "
2365 "DW_AT_call_file attribute"));
2368 function
->caller_filename
=
2369 lhdr
->filenames
[val
.u
.uint
- 1];
2374 case DW_AT_call_line
:
2375 if (val
.encoding
== ATTR_VAL_UINT
)
2376 function
->caller_lineno
= val
.u
.uint
;
2379 case DW_AT_abstract_origin
:
2380 case DW_AT_specification
:
2381 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2382 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2384 /* This refers to an abstract origin defined in
2385 some other compilation unit. We can handle
2386 this case if we must, but it's harder. */
2389 if (val
.encoding
== ATTR_VAL_UINT
2390 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2394 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2395 error_callback
, data
);
2397 function
->name
= name
;
2402 if (val
.encoding
== ATTR_VAL_STRING
)
2404 /* Don't override a name we found in some other
2405 way, as it will normally be more
2406 useful--e.g., this name is normally not
2408 if (function
->name
== NULL
)
2409 function
->name
= val
.u
.string
;
2413 case DW_AT_linkage_name
:
2414 case DW_AT_MIPS_linkage_name
:
2415 if (val
.encoding
== ATTR_VAL_STRING
)
2416 function
->name
= val
.u
.string
;
2420 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2428 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2430 highpc
= val
.u
.uint
;
2433 else if (val
.encoding
== ATTR_VAL_UINT
)
2435 highpc
= val
.u
.uint
;
2437 highpc_is_relative
= 1;
2442 if (val
.encoding
== ATTR_VAL_UINT
2443 || val
.encoding
== ATTR_VAL_REF_SECTION
)
2445 ranges
= val
.u
.uint
;
2456 /* If we couldn't find a name for the function, we have no use
2458 if (is_function
&& function
->name
== NULL
)
2460 backtrace_free (state
, function
, sizeof *function
,
2461 error_callback
, data
);
2469 if (!add_function_ranges (state
, ddata
, u
, function
, ranges
,
2470 base
, error_callback
, data
, vec
))
2473 else if (have_lowpc
&& have_highpc
)
2475 if (highpc_is_relative
)
2477 if (!add_function_range (state
, ddata
, function
, lowpc
, highpc
,
2478 error_callback
, data
, vec
))
2483 backtrace_free (state
, function
, sizeof *function
,
2484 error_callback
, data
);
2489 if (abbrev
->has_children
)
2493 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2494 error_callback
, data
, vec_function
,
2500 struct function_vector fvec
;
2502 /* Gather any information for inlined functions in
2505 memset (&fvec
, 0, sizeof fvec
);
2507 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2508 error_callback
, data
, vec_function
,
2514 struct function_addrs
*faddrs
;
2516 if (!backtrace_vector_release (state
, &fvec
.vec
,
2517 error_callback
, data
))
2520 faddrs
= (struct function_addrs
*) fvec
.vec
.base
;
2521 backtrace_qsort (faddrs
, fvec
.count
,
2522 sizeof (struct function_addrs
),
2523 function_addrs_compare
);
2525 function
->function_addrs
= faddrs
;
2526 function
->function_addrs_count
= fvec
.count
;
2535 /* Read function name information for a compilation unit. We look
2536 through the whole unit looking for function tags. */
2539 read_function_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2540 const struct line_header
*lhdr
,
2541 backtrace_error_callback error_callback
, void *data
,
2542 struct unit
*u
, struct function_vector
*fvec
,
2543 struct function_addrs
**ret_addrs
,
2544 size_t *ret_addrs_count
)
2546 struct function_vector lvec
;
2547 struct function_vector
*pfvec
;
2548 struct dwarf_buf unit_buf
;
2549 struct function_addrs
*addrs
;
2552 /* Use FVEC if it is not NULL. Otherwise use our own vector. */
2557 memset (&lvec
, 0, sizeof lvec
);
2561 unit_buf
.name
= ".debug_info";
2562 unit_buf
.start
= ddata
->dwarf_info
;
2563 unit_buf
.buf
= u
->unit_data
;
2564 unit_buf
.left
= u
->unit_data_len
;
2565 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2566 unit_buf
.error_callback
= error_callback
;
2567 unit_buf
.data
= data
;
2568 unit_buf
.reported_underflow
= 0;
2570 while (unit_buf
.left
> 0)
2572 if (!read_function_entry (state
, ddata
, u
, 0, &unit_buf
, lhdr
,
2573 error_callback
, data
, pfvec
, pfvec
))
2577 if (pfvec
->count
== 0)
2580 addrs_count
= pfvec
->count
;
2584 if (!backtrace_vector_release (state
, &lvec
.vec
, error_callback
, data
))
2586 addrs
= (struct function_addrs
*) pfvec
->vec
.base
;
2590 /* Finish this list of addresses, but leave the remaining space in
2591 the vector available for the next function unit. */
2592 addrs
= ((struct function_addrs
*)
2593 backtrace_vector_finish (state
, &fvec
->vec
,
2594 error_callback
, data
));
2600 backtrace_qsort (addrs
, addrs_count
, sizeof (struct function_addrs
),
2601 function_addrs_compare
);
2604 *ret_addrs_count
= addrs_count
;
2607 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2608 information, and update FILENAME and LINENO for the caller.
2609 Returns whatever CALLBACK returns, or 0 to keep going. */
2612 report_inlined_functions (uintptr_t pc
, struct function
*function
,
2613 backtrace_full_callback callback
, void *data
,
2614 const char **filename
, int *lineno
)
2616 struct function_addrs
*function_addrs
;
2617 struct function
*inlined
;
2620 if (function
->function_addrs_count
== 0)
2623 function_addrs
= ((struct function_addrs
*)
2624 bsearch (&pc
, function
->function_addrs
,
2625 function
->function_addrs_count
,
2626 sizeof (struct function_addrs
),
2627 function_addrs_search
));
2628 if (function_addrs
== NULL
)
2631 while (((size_t) (function_addrs
- function
->function_addrs
) + 1
2632 < function
->function_addrs_count
)
2633 && pc
>= (function_addrs
+ 1)->low
2634 && pc
< (function_addrs
+ 1)->high
)
2637 /* We found an inlined call. */
2639 inlined
= function_addrs
->function
;
2641 /* Report any calls inlined into this one. */
2642 ret
= report_inlined_functions (pc
, inlined
, callback
, data
,
2647 /* Report this inlined call. */
2648 ret
= callback (data
, pc
, *filename
, *lineno
, inlined
->name
);
2652 /* Our caller will report the caller of the inlined function; tell
2653 it the appropriate filename and line number. */
2654 *filename
= inlined
->caller_filename
;
2655 *lineno
= inlined
->caller_lineno
;
2660 /* Look for a PC in the DWARF mapping for one module. On success,
2661 call CALLBACK and return whatever it returns. On error, call
2662 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
2666 dwarf_lookup_pc (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2667 uintptr_t pc
, backtrace_full_callback callback
,
2668 backtrace_error_callback error_callback
, void *data
,
2671 struct unit_addrs
*entry
;
2676 struct function_addrs
*function_addrs
;
2677 struct function
*function
;
2678 const char *filename
;
2684 /* Find an address range that includes PC. */
2685 entry
= bsearch (&pc
, ddata
->addrs
, ddata
->addrs_count
,
2686 sizeof (struct unit_addrs
), unit_addrs_search
);
2694 /* If there are multiple ranges that contain PC, use the last one,
2695 in order to produce predictable results. If we assume that all
2696 ranges are properly nested, then the last range will be the
2698 while ((size_t) (entry
- ddata
->addrs
) + 1 < ddata
->addrs_count
2699 && pc
>= (entry
+ 1)->low
2700 && pc
< (entry
+ 1)->high
)
2703 /* We need the lines, lines_count, function_addrs,
2704 function_addrs_count fields of u. If they are not set, we need
2705 to set them. When running in threaded mode, we need to allow for
2706 the possibility that some other thread is setting them
2712 /* Skip units with no useful line number information by walking
2713 backward. Useless line number information is marked by setting
2715 while (entry
> ddata
->addrs
2716 && pc
>= (entry
- 1)->low
2717 && pc
< (entry
- 1)->high
)
2719 if (state
->threaded
)
2720 lines
= (struct line
*) backtrace_atomic_load_pointer (&u
->lines
);
2722 if (lines
!= (struct line
*) (uintptr_t) -1)
2731 if (state
->threaded
)
2732 lines
= backtrace_atomic_load_pointer (&u
->lines
);
2737 size_t function_addrs_count
;
2738 struct line_header lhdr
;
2741 /* We have never read the line information for this unit. Read
2744 function_addrs
= NULL
;
2745 function_addrs_count
= 0;
2746 if (read_line_info (state
, ddata
, error_callback
, data
, entry
->u
, &lhdr
,
2749 struct function_vector
*pfvec
;
2751 /* If not threaded, reuse DDATA->FVEC for better memory
2753 if (state
->threaded
)
2756 pfvec
= &ddata
->fvec
;
2757 read_function_info (state
, ddata
, &lhdr
, error_callback
, data
,
2758 entry
->u
, pfvec
, &function_addrs
,
2759 &function_addrs_count
);
2760 free_line_header (state
, &lhdr
, error_callback
, data
);
2764 /* Atomically store the information we just read into the unit.
2765 If another thread is simultaneously writing, it presumably
2766 read the same information, and we don't care which one we
2767 wind up with; we just leak the other one. We do have to
2768 write the lines field last, so that the acquire-loads above
2769 ensure that the other fields are set. */
2771 if (!state
->threaded
)
2773 u
->lines_count
= count
;
2774 u
->function_addrs
= function_addrs
;
2775 u
->function_addrs_count
= function_addrs_count
;
2780 backtrace_atomic_store_size_t (&u
->lines_count
, count
);
2781 backtrace_atomic_store_pointer (&u
->function_addrs
, function_addrs
);
2782 backtrace_atomic_store_size_t (&u
->function_addrs_count
,
2783 function_addrs_count
);
2784 backtrace_atomic_store_pointer (&u
->lines
, lines
);
2788 /* Now all fields of U have been initialized. */
2790 if (lines
== (struct line
*) (uintptr_t) -1)
2792 /* If reading the line number information failed in some way,
2793 try again to see if there is a better compilation unit for
2796 return dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2798 return callback (data
, pc
, NULL
, 0, NULL
);
2801 /* Search for PC within this unit. */
2803 ln
= (struct line
*) bsearch (&pc
, lines
, entry
->u
->lines_count
,
2804 sizeof (struct line
), line_search
);
2807 /* The PC is between the low_pc and high_pc attributes of the
2808 compilation unit, but no entry in the line table covers it.
2809 This implies that the start of the compilation unit has no
2810 line number information. */
2812 if (entry
->u
->abs_filename
== NULL
)
2814 const char *filename
;
2816 filename
= entry
->u
->filename
;
2817 if (filename
!= NULL
2818 && !IS_ABSOLUTE_PATH (filename
)
2819 && entry
->u
->comp_dir
!= NULL
)
2821 size_t filename_len
;
2826 filename_len
= strlen (filename
);
2827 dir
= entry
->u
->comp_dir
;
2828 dir_len
= strlen (dir
);
2829 s
= (char *) backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
2830 error_callback
, data
);
2836 memcpy (s
, dir
, dir_len
);
2837 /* FIXME: Should use backslash if DOS file system. */
2839 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
2842 entry
->u
->abs_filename
= filename
;
2845 return callback (data
, pc
, entry
->u
->abs_filename
, 0, NULL
);
2848 /* Search for function name within this unit. */
2850 if (entry
->u
->function_addrs_count
== 0)
2851 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2853 function_addrs
= ((struct function_addrs
*)
2854 bsearch (&pc
, entry
->u
->function_addrs
,
2855 entry
->u
->function_addrs_count
,
2856 sizeof (struct function_addrs
),
2857 function_addrs_search
));
2858 if (function_addrs
== NULL
)
2859 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2861 /* If there are multiple function ranges that contain PC, use the
2862 last one, in order to produce predictable results. */
2864 while (((size_t) (function_addrs
- entry
->u
->function_addrs
+ 1)
2865 < entry
->u
->function_addrs_count
)
2866 && pc
>= (function_addrs
+ 1)->low
2867 && pc
< (function_addrs
+ 1)->high
)
2870 function
= function_addrs
->function
;
2872 filename
= ln
->filename
;
2873 lineno
= ln
->lineno
;
2875 ret
= report_inlined_functions (pc
, function
, callback
, data
,
2876 &filename
, &lineno
);
2880 return callback (data
, pc
, filename
, lineno
, function
->name
);
2884 /* Return the file/line information for a PC using the DWARF mapping
2885 we built earlier. */
2888 dwarf_fileline (struct backtrace_state
*state
, uintptr_t pc
,
2889 backtrace_full_callback callback
,
2890 backtrace_error_callback error_callback
, void *data
)
2892 struct dwarf_data
*ddata
;
2896 if (!state
->threaded
)
2898 for (ddata
= (struct dwarf_data
*) state
->fileline_data
;
2900 ddata
= ddata
->next
)
2902 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2904 if (ret
!= 0 || found
)
2910 struct dwarf_data
**pp
;
2912 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
2915 ddata
= backtrace_atomic_load_pointer (pp
);
2919 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2921 if (ret
!= 0 || found
)
2928 /* FIXME: See if any libraries have been dlopen'ed. */
2930 return callback (data
, pc
, NULL
, 0, NULL
);
2933 /* Initialize our data structures from the DWARF debug info for a
2934 file. Return NULL on failure. */
2936 static struct dwarf_data
*
2937 build_dwarf_data (struct backtrace_state
*state
,
2938 uintptr_t base_address
,
2939 const unsigned char *dwarf_info
,
2940 size_t dwarf_info_size
,
2941 const unsigned char *dwarf_line
,
2942 size_t dwarf_line_size
,
2943 const unsigned char *dwarf_abbrev
,
2944 size_t dwarf_abbrev_size
,
2945 const unsigned char *dwarf_ranges
,
2946 size_t dwarf_ranges_size
,
2947 const unsigned char *dwarf_str
,
2948 size_t dwarf_str_size
,
2950 backtrace_error_callback error_callback
,
2953 struct unit_addrs_vector addrs_vec
;
2954 struct unit_addrs
*addrs
;
2956 struct dwarf_data
*fdata
;
2958 if (!build_address_map (state
, base_address
, dwarf_info
, dwarf_info_size
,
2959 dwarf_abbrev
, dwarf_abbrev_size
, dwarf_ranges
,
2960 dwarf_ranges_size
, dwarf_str
, dwarf_str_size
,
2961 is_bigendian
, error_callback
, data
, &addrs_vec
))
2964 if (!backtrace_vector_release (state
, &addrs_vec
.vec
, error_callback
, data
))
2966 addrs
= (struct unit_addrs
*) addrs_vec
.vec
.base
;
2967 addrs_count
= addrs_vec
.count
;
2968 backtrace_qsort (addrs
, addrs_count
, sizeof (struct unit_addrs
),
2969 unit_addrs_compare
);
2971 fdata
= ((struct dwarf_data
*)
2972 backtrace_alloc (state
, sizeof (struct dwarf_data
),
2973 error_callback
, data
));
2978 fdata
->base_address
= base_address
;
2979 fdata
->addrs
= addrs
;
2980 fdata
->addrs_count
= addrs_count
;
2981 fdata
->dwarf_info
= dwarf_info
;
2982 fdata
->dwarf_info_size
= dwarf_info_size
;
2983 fdata
->dwarf_line
= dwarf_line
;
2984 fdata
->dwarf_line_size
= dwarf_line_size
;
2985 fdata
->dwarf_ranges
= dwarf_ranges
;
2986 fdata
->dwarf_ranges_size
= dwarf_ranges_size
;
2987 fdata
->dwarf_str
= dwarf_str
;
2988 fdata
->dwarf_str_size
= dwarf_str_size
;
2989 fdata
->is_bigendian
= is_bigendian
;
2990 memset (&fdata
->fvec
, 0, sizeof fdata
->fvec
);
2995 /* Build our data structures from the DWARF sections for a module.
2996 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
3000 backtrace_dwarf_add (struct backtrace_state
*state
,
3001 uintptr_t base_address
,
3002 const unsigned char *dwarf_info
,
3003 size_t dwarf_info_size
,
3004 const unsigned char *dwarf_line
,
3005 size_t dwarf_line_size
,
3006 const unsigned char *dwarf_abbrev
,
3007 size_t dwarf_abbrev_size
,
3008 const unsigned char *dwarf_ranges
,
3009 size_t dwarf_ranges_size
,
3010 const unsigned char *dwarf_str
,
3011 size_t dwarf_str_size
,
3013 backtrace_error_callback error_callback
,
3014 void *data
, fileline
*fileline_fn
)
3016 struct dwarf_data
*fdata
;
3018 fdata
= build_dwarf_data (state
, base_address
, dwarf_info
, dwarf_info_size
,
3019 dwarf_line
, dwarf_line_size
, dwarf_abbrev
,
3020 dwarf_abbrev_size
, dwarf_ranges
, dwarf_ranges_size
,
3021 dwarf_str
, dwarf_str_size
, is_bigendian
,
3022 error_callback
, data
);
3026 if (!state
->threaded
)
3028 struct dwarf_data
**pp
;
3030 for (pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
3040 struct dwarf_data
**pp
;
3042 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
3046 struct dwarf_data
*p
;
3048 p
= backtrace_atomic_load_pointer (pp
);
3056 if (__sync_bool_compare_and_swap (pp
, NULL
, fdata
))
3061 *fileline_fn
= dwarf_fileline
;