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 backtrace_vector_free (state
, &vec
.vec
, error_callback
, data
);
2061 free_line_header (state
, hdr
, error_callback
, data
);
2062 *lines
= (struct line
*) (uintptr_t) -1;
2067 /* Read the name of a function from a DIE referenced by a
2068 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
2069 the same compilation unit. */
2072 read_referenced_name (struct dwarf_data
*ddata
, struct unit
*u
,
2073 uint64_t offset
, backtrace_error_callback error_callback
,
2076 struct dwarf_buf unit_buf
;
2078 const struct abbrev
*abbrev
;
2082 /* OFFSET is from the start of the data for this compilation unit.
2083 U->unit_data is the data, but it starts U->unit_data_offset bytes
2084 from the beginning. */
2086 if (offset
< u
->unit_data_offset
2087 || offset
- u
->unit_data_offset
>= u
->unit_data_len
)
2089 error_callback (data
,
2090 "abstract origin or specification out of range",
2095 offset
-= u
->unit_data_offset
;
2097 unit_buf
.name
= ".debug_info";
2098 unit_buf
.start
= ddata
->dwarf_info
;
2099 unit_buf
.buf
= u
->unit_data
+ offset
;
2100 unit_buf
.left
= u
->unit_data_len
- offset
;
2101 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2102 unit_buf
.error_callback
= error_callback
;
2103 unit_buf
.data
= data
;
2104 unit_buf
.reported_underflow
= 0;
2106 code
= read_uleb128 (&unit_buf
);
2109 dwarf_buf_error (&unit_buf
, "invalid abstract origin or specification");
2113 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2118 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2120 struct attr_val val
;
2122 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
,
2123 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2124 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2128 switch (abbrev
->attrs
[i
].name
)
2131 /* We prefer the linkage name if get one. */
2132 if (val
.encoding
== ATTR_VAL_STRING
)
2136 case DW_AT_linkage_name
:
2137 case DW_AT_MIPS_linkage_name
:
2138 if (val
.encoding
== ATTR_VAL_STRING
)
2139 return val
.u
.string
;
2142 case DW_AT_specification
:
2143 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2144 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2146 /* This refers to a specification defined in some other
2147 compilation unit. We can handle this case if we
2148 must, but it's harder. */
2151 if (val
.encoding
== ATTR_VAL_UINT
2152 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2156 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2157 error_callback
, data
);
2171 /* Add a single range to U that maps to function. Returns 1 on
2172 success, 0 on error. */
2175 add_function_range (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2176 struct function
*function
, uint64_t lowpc
, uint64_t highpc
,
2177 backtrace_error_callback error_callback
,
2178 void *data
, struct function_vector
*vec
)
2180 struct function_addrs
*p
;
2182 /* Add in the base address here, so that we can look up the PC
2184 lowpc
+= ddata
->base_address
;
2185 highpc
+= ddata
->base_address
;
2189 p
= (struct function_addrs
*) vec
->vec
.base
+ vec
->count
- 1;
2190 if ((lowpc
== p
->high
|| lowpc
== p
->high
+ 1)
2191 && function
== p
->function
)
2193 if (highpc
> p
->high
)
2199 p
= ((struct function_addrs
*)
2200 backtrace_vector_grow (state
, sizeof (struct function_addrs
),
2201 error_callback
, data
, &vec
->vec
));
2207 p
->function
= function
;
2212 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2216 add_function_ranges (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2217 struct unit
*u
, struct function
*function
,
2218 uint64_t ranges
, uint64_t base
,
2219 backtrace_error_callback error_callback
, void *data
,
2220 struct function_vector
*vec
)
2222 struct dwarf_buf ranges_buf
;
2224 if (ranges
>= ddata
->dwarf_ranges_size
)
2226 error_callback (data
, "function ranges offset out of range", 0);
2230 ranges_buf
.name
= ".debug_ranges";
2231 ranges_buf
.start
= ddata
->dwarf_ranges
;
2232 ranges_buf
.buf
= ddata
->dwarf_ranges
+ ranges
;
2233 ranges_buf
.left
= ddata
->dwarf_ranges_size
- ranges
;
2234 ranges_buf
.is_bigendian
= ddata
->is_bigendian
;
2235 ranges_buf
.error_callback
= error_callback
;
2236 ranges_buf
.data
= data
;
2237 ranges_buf
.reported_underflow
= 0;
2244 if (ranges_buf
.reported_underflow
)
2247 low
= read_address (&ranges_buf
, u
->addrsize
);
2248 high
= read_address (&ranges_buf
, u
->addrsize
);
2250 if (low
== 0 && high
== 0)
2253 if (is_highest_address (low
, u
->addrsize
))
2257 if (!add_function_range (state
, ddata
, function
, low
+ base
,
2258 high
+ base
, error_callback
, data
, vec
))
2263 if (ranges_buf
.reported_underflow
)
2269 /* Read one entry plus all its children. Add function addresses to
2270 VEC. Returns 1 on success, 0 on error. */
2273 read_function_entry (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2274 struct unit
*u
, uint64_t base
, struct dwarf_buf
*unit_buf
,
2275 const struct line_header
*lhdr
,
2276 backtrace_error_callback error_callback
, void *data
,
2277 struct function_vector
*vec_function
,
2278 struct function_vector
*vec_inlined
)
2280 while (unit_buf
->left
> 0)
2283 const struct abbrev
*abbrev
;
2285 struct function
*function
;
2286 struct function_vector
*vec
;
2292 int highpc_is_relative
;
2296 code
= read_uleb128 (unit_buf
);
2300 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2304 is_function
= (abbrev
->tag
== DW_TAG_subprogram
2305 || abbrev
->tag
== DW_TAG_entry_point
2306 || abbrev
->tag
== DW_TAG_inlined_subroutine
);
2308 if (abbrev
->tag
== DW_TAG_inlined_subroutine
)
2316 function
= ((struct function
*)
2317 backtrace_alloc (state
, sizeof *function
,
2318 error_callback
, data
));
2319 if (function
== NULL
)
2321 memset (function
, 0, sizeof *function
);
2328 highpc_is_relative
= 0;
2331 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2333 struct attr_val val
;
2335 if (!read_attribute (abbrev
->attrs
[i
].form
, unit_buf
,
2336 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2337 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2341 /* The compile unit sets the base address for any address
2342 ranges in the function entries. */
2343 if (abbrev
->tag
== DW_TAG_compile_unit
2344 && abbrev
->attrs
[i
].name
== DW_AT_low_pc
2345 && val
.encoding
== ATTR_VAL_ADDRESS
)
2350 switch (abbrev
->attrs
[i
].name
)
2352 case DW_AT_call_file
:
2353 if (val
.encoding
== ATTR_VAL_UINT
)
2355 if (val
.u
.uint
== 0)
2356 function
->caller_filename
= "";
2359 if (val
.u
.uint
- 1 >= lhdr
->filenames_count
)
2361 dwarf_buf_error (unit_buf
,
2362 ("invalid file number in "
2363 "DW_AT_call_file attribute"));
2366 function
->caller_filename
=
2367 lhdr
->filenames
[val
.u
.uint
- 1];
2372 case DW_AT_call_line
:
2373 if (val
.encoding
== ATTR_VAL_UINT
)
2374 function
->caller_lineno
= val
.u
.uint
;
2377 case DW_AT_abstract_origin
:
2378 case DW_AT_specification
:
2379 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2380 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2382 /* This refers to an abstract origin defined in
2383 some other compilation unit. We can handle
2384 this case if we must, but it's harder. */
2387 if (val
.encoding
== ATTR_VAL_UINT
2388 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2392 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2393 error_callback
, data
);
2395 function
->name
= name
;
2400 if (val
.encoding
== ATTR_VAL_STRING
)
2402 /* Don't override a name we found in some other
2403 way, as it will normally be more
2404 useful--e.g., this name is normally not
2406 if (function
->name
== NULL
)
2407 function
->name
= val
.u
.string
;
2411 case DW_AT_linkage_name
:
2412 case DW_AT_MIPS_linkage_name
:
2413 if (val
.encoding
== ATTR_VAL_STRING
)
2414 function
->name
= val
.u
.string
;
2418 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2426 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2428 highpc
= val
.u
.uint
;
2431 else if (val
.encoding
== ATTR_VAL_UINT
)
2433 highpc
= val
.u
.uint
;
2435 highpc_is_relative
= 1;
2440 if (val
.encoding
== ATTR_VAL_UINT
2441 || val
.encoding
== ATTR_VAL_REF_SECTION
)
2443 ranges
= val
.u
.uint
;
2454 /* If we couldn't find a name for the function, we have no use
2456 if (is_function
&& function
->name
== NULL
)
2458 backtrace_free (state
, function
, sizeof *function
,
2459 error_callback
, data
);
2467 if (!add_function_ranges (state
, ddata
, u
, function
, ranges
,
2468 base
, error_callback
, data
, vec
))
2471 else if (have_lowpc
&& have_highpc
)
2473 if (highpc_is_relative
)
2475 if (!add_function_range (state
, ddata
, function
, lowpc
, highpc
,
2476 error_callback
, data
, vec
))
2481 backtrace_free (state
, function
, sizeof *function
,
2482 error_callback
, data
);
2487 if (abbrev
->has_children
)
2491 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2492 error_callback
, data
, vec_function
,
2498 struct function_vector fvec
;
2500 /* Gather any information for inlined functions in
2503 memset (&fvec
, 0, sizeof fvec
);
2505 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2506 error_callback
, data
, vec_function
,
2512 struct function_addrs
*faddrs
;
2514 if (!backtrace_vector_release (state
, &fvec
.vec
,
2515 error_callback
, data
))
2518 faddrs
= (struct function_addrs
*) fvec
.vec
.base
;
2519 backtrace_qsort (faddrs
, fvec
.count
,
2520 sizeof (struct function_addrs
),
2521 function_addrs_compare
);
2523 function
->function_addrs
= faddrs
;
2524 function
->function_addrs_count
= fvec
.count
;
2533 /* Read function name information for a compilation unit. We look
2534 through the whole unit looking for function tags. */
2537 read_function_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2538 const struct line_header
*lhdr
,
2539 backtrace_error_callback error_callback
, void *data
,
2540 struct unit
*u
, struct function_vector
*fvec
,
2541 struct function_addrs
**ret_addrs
,
2542 size_t *ret_addrs_count
)
2544 struct function_vector lvec
;
2545 struct function_vector
*pfvec
;
2546 struct dwarf_buf unit_buf
;
2547 struct function_addrs
*addrs
;
2550 /* Use FVEC if it is not NULL. Otherwise use our own vector. */
2555 memset (&lvec
, 0, sizeof lvec
);
2559 unit_buf
.name
= ".debug_info";
2560 unit_buf
.start
= ddata
->dwarf_info
;
2561 unit_buf
.buf
= u
->unit_data
;
2562 unit_buf
.left
= u
->unit_data_len
;
2563 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2564 unit_buf
.error_callback
= error_callback
;
2565 unit_buf
.data
= data
;
2566 unit_buf
.reported_underflow
= 0;
2568 while (unit_buf
.left
> 0)
2570 if (!read_function_entry (state
, ddata
, u
, 0, &unit_buf
, lhdr
,
2571 error_callback
, data
, pfvec
, pfvec
))
2575 if (pfvec
->count
== 0)
2578 addrs_count
= pfvec
->count
;
2582 if (!backtrace_vector_release (state
, &lvec
.vec
, error_callback
, data
))
2584 addrs
= (struct function_addrs
*) pfvec
->vec
.base
;
2588 /* Finish this list of addresses, but leave the remaining space in
2589 the vector available for the next function unit. */
2590 addrs
= ((struct function_addrs
*)
2591 backtrace_vector_finish (state
, &fvec
->vec
,
2592 error_callback
, data
));
2598 backtrace_qsort (addrs
, addrs_count
, sizeof (struct function_addrs
),
2599 function_addrs_compare
);
2602 *ret_addrs_count
= addrs_count
;
2605 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2606 information, and update FILENAME and LINENO for the caller.
2607 Returns whatever CALLBACK returns, or 0 to keep going. */
2610 report_inlined_functions (uintptr_t pc
, struct function
*function
,
2611 backtrace_full_callback callback
, void *data
,
2612 const char **filename
, int *lineno
)
2614 struct function_addrs
*function_addrs
;
2615 struct function
*inlined
;
2618 if (function
->function_addrs_count
== 0)
2621 function_addrs
= ((struct function_addrs
*)
2622 bsearch (&pc
, function
->function_addrs
,
2623 function
->function_addrs_count
,
2624 sizeof (struct function_addrs
),
2625 function_addrs_search
));
2626 if (function_addrs
== NULL
)
2629 while (((size_t) (function_addrs
- function
->function_addrs
) + 1
2630 < function
->function_addrs_count
)
2631 && pc
>= (function_addrs
+ 1)->low
2632 && pc
< (function_addrs
+ 1)->high
)
2635 /* We found an inlined call. */
2637 inlined
= function_addrs
->function
;
2639 /* Report any calls inlined into this one. */
2640 ret
= report_inlined_functions (pc
, inlined
, callback
, data
,
2645 /* Report this inlined call. */
2646 ret
= callback (data
, pc
, *filename
, *lineno
, inlined
->name
);
2650 /* Our caller will report the caller of the inlined function; tell
2651 it the appropriate filename and line number. */
2652 *filename
= inlined
->caller_filename
;
2653 *lineno
= inlined
->caller_lineno
;
2658 /* Look for a PC in the DWARF mapping for one module. On success,
2659 call CALLBACK and return whatever it returns. On error, call
2660 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
2664 dwarf_lookup_pc (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2665 uintptr_t pc
, backtrace_full_callback callback
,
2666 backtrace_error_callback error_callback
, void *data
,
2669 struct unit_addrs
*entry
;
2674 struct function_addrs
*function_addrs
;
2675 struct function
*function
;
2676 const char *filename
;
2682 /* Find an address range that includes PC. */
2683 entry
= bsearch (&pc
, ddata
->addrs
, ddata
->addrs_count
,
2684 sizeof (struct unit_addrs
), unit_addrs_search
);
2692 /* If there are multiple ranges that contain PC, use the last one,
2693 in order to produce predictable results. If we assume that all
2694 ranges are properly nested, then the last range will be the
2696 while ((size_t) (entry
- ddata
->addrs
) + 1 < ddata
->addrs_count
2697 && pc
>= (entry
+ 1)->low
2698 && pc
< (entry
+ 1)->high
)
2701 /* We need the lines, lines_count, function_addrs,
2702 function_addrs_count fields of u. If they are not set, we need
2703 to set them. When running in threaded mode, we need to allow for
2704 the possibility that some other thread is setting them
2710 /* Skip units with no useful line number information by walking
2711 backward. Useless line number information is marked by setting
2713 while (entry
> ddata
->addrs
2714 && pc
>= (entry
- 1)->low
2715 && pc
< (entry
- 1)->high
)
2717 if (state
->threaded
)
2718 lines
= (struct line
*) backtrace_atomic_load_pointer (&u
->lines
);
2720 if (lines
!= (struct line
*) (uintptr_t) -1)
2729 if (state
->threaded
)
2730 lines
= backtrace_atomic_load_pointer (&u
->lines
);
2735 size_t function_addrs_count
;
2736 struct line_header lhdr
;
2739 /* We have never read the line information for this unit. Read
2742 function_addrs
= NULL
;
2743 function_addrs_count
= 0;
2744 if (read_line_info (state
, ddata
, error_callback
, data
, entry
->u
, &lhdr
,
2747 struct function_vector
*pfvec
;
2749 /* If not threaded, reuse DDATA->FVEC for better memory
2751 if (state
->threaded
)
2754 pfvec
= &ddata
->fvec
;
2755 read_function_info (state
, ddata
, &lhdr
, error_callback
, data
,
2756 entry
->u
, pfvec
, &function_addrs
,
2757 &function_addrs_count
);
2758 free_line_header (state
, &lhdr
, error_callback
, data
);
2762 /* Atomically store the information we just read into the unit.
2763 If another thread is simultaneously writing, it presumably
2764 read the same information, and we don't care which one we
2765 wind up with; we just leak the other one. We do have to
2766 write the lines field last, so that the acquire-loads above
2767 ensure that the other fields are set. */
2769 if (!state
->threaded
)
2771 u
->lines_count
= count
;
2772 u
->function_addrs
= function_addrs
;
2773 u
->function_addrs_count
= function_addrs_count
;
2778 backtrace_atomic_store_size_t (&u
->lines_count
, count
);
2779 backtrace_atomic_store_pointer (&u
->function_addrs
, function_addrs
);
2780 backtrace_atomic_store_size_t (&u
->function_addrs_count
,
2781 function_addrs_count
);
2782 backtrace_atomic_store_pointer (&u
->lines
, lines
);
2786 /* Now all fields of U have been initialized. */
2788 if (lines
== (struct line
*) (uintptr_t) -1)
2790 /* If reading the line number information failed in some way,
2791 try again to see if there is a better compilation unit for
2794 return dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2796 return callback (data
, pc
, NULL
, 0, NULL
);
2799 /* Search for PC within this unit. */
2801 ln
= (struct line
*) bsearch (&pc
, lines
, entry
->u
->lines_count
,
2802 sizeof (struct line
), line_search
);
2805 /* The PC is between the low_pc and high_pc attributes of the
2806 compilation unit, but no entry in the line table covers it.
2807 This implies that the start of the compilation unit has no
2808 line number information. */
2810 if (entry
->u
->abs_filename
== NULL
)
2812 const char *filename
;
2814 filename
= entry
->u
->filename
;
2815 if (filename
!= NULL
2816 && !IS_ABSOLUTE_PATH (filename
)
2817 && entry
->u
->comp_dir
!= NULL
)
2819 size_t filename_len
;
2824 filename_len
= strlen (filename
);
2825 dir
= entry
->u
->comp_dir
;
2826 dir_len
= strlen (dir
);
2827 s
= (char *) backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
2828 error_callback
, data
);
2834 memcpy (s
, dir
, dir_len
);
2835 /* FIXME: Should use backslash if DOS file system. */
2837 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
2840 entry
->u
->abs_filename
= filename
;
2843 return callback (data
, pc
, entry
->u
->abs_filename
, 0, NULL
);
2846 /* Search for function name within this unit. */
2848 if (entry
->u
->function_addrs_count
== 0)
2849 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2851 function_addrs
= ((struct function_addrs
*)
2852 bsearch (&pc
, entry
->u
->function_addrs
,
2853 entry
->u
->function_addrs_count
,
2854 sizeof (struct function_addrs
),
2855 function_addrs_search
));
2856 if (function_addrs
== NULL
)
2857 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2859 /* If there are multiple function ranges that contain PC, use the
2860 last one, in order to produce predictable results. */
2862 while (((size_t) (function_addrs
- entry
->u
->function_addrs
+ 1)
2863 < entry
->u
->function_addrs_count
)
2864 && pc
>= (function_addrs
+ 1)->low
2865 && pc
< (function_addrs
+ 1)->high
)
2868 function
= function_addrs
->function
;
2870 filename
= ln
->filename
;
2871 lineno
= ln
->lineno
;
2873 ret
= report_inlined_functions (pc
, function
, callback
, data
,
2874 &filename
, &lineno
);
2878 return callback (data
, pc
, filename
, lineno
, function
->name
);
2882 /* Return the file/line information for a PC using the DWARF mapping
2883 we built earlier. */
2886 dwarf_fileline (struct backtrace_state
*state
, uintptr_t pc
,
2887 backtrace_full_callback callback
,
2888 backtrace_error_callback error_callback
, void *data
)
2890 struct dwarf_data
*ddata
;
2894 if (!state
->threaded
)
2896 for (ddata
= (struct dwarf_data
*) state
->fileline_data
;
2898 ddata
= ddata
->next
)
2900 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2902 if (ret
!= 0 || found
)
2908 struct dwarf_data
**pp
;
2910 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
2913 ddata
= backtrace_atomic_load_pointer (pp
);
2917 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2919 if (ret
!= 0 || found
)
2926 /* FIXME: See if any libraries have been dlopen'ed. */
2928 return callback (data
, pc
, NULL
, 0, NULL
);
2931 /* Initialize our data structures from the DWARF debug info for a
2932 file. Return NULL on failure. */
2934 static struct dwarf_data
*
2935 build_dwarf_data (struct backtrace_state
*state
,
2936 uintptr_t base_address
,
2937 const unsigned char *dwarf_info
,
2938 size_t dwarf_info_size
,
2939 const unsigned char *dwarf_line
,
2940 size_t dwarf_line_size
,
2941 const unsigned char *dwarf_abbrev
,
2942 size_t dwarf_abbrev_size
,
2943 const unsigned char *dwarf_ranges
,
2944 size_t dwarf_ranges_size
,
2945 const unsigned char *dwarf_str
,
2946 size_t dwarf_str_size
,
2948 backtrace_error_callback error_callback
,
2951 struct unit_addrs_vector addrs_vec
;
2952 struct unit_addrs
*addrs
;
2954 struct dwarf_data
*fdata
;
2956 if (!build_address_map (state
, base_address
, dwarf_info
, dwarf_info_size
,
2957 dwarf_abbrev
, dwarf_abbrev_size
, dwarf_ranges
,
2958 dwarf_ranges_size
, dwarf_str
, dwarf_str_size
,
2959 is_bigendian
, error_callback
, data
, &addrs_vec
))
2962 if (!backtrace_vector_release (state
, &addrs_vec
.vec
, error_callback
, data
))
2964 addrs
= (struct unit_addrs
*) addrs_vec
.vec
.base
;
2965 addrs_count
= addrs_vec
.count
;
2966 backtrace_qsort (addrs
, addrs_count
, sizeof (struct unit_addrs
),
2967 unit_addrs_compare
);
2969 fdata
= ((struct dwarf_data
*)
2970 backtrace_alloc (state
, sizeof (struct dwarf_data
),
2971 error_callback
, data
));
2976 fdata
->base_address
= base_address
;
2977 fdata
->addrs
= addrs
;
2978 fdata
->addrs_count
= addrs_count
;
2979 fdata
->dwarf_info
= dwarf_info
;
2980 fdata
->dwarf_info_size
= dwarf_info_size
;
2981 fdata
->dwarf_line
= dwarf_line
;
2982 fdata
->dwarf_line_size
= dwarf_line_size
;
2983 fdata
->dwarf_ranges
= dwarf_ranges
;
2984 fdata
->dwarf_ranges_size
= dwarf_ranges_size
;
2985 fdata
->dwarf_str
= dwarf_str
;
2986 fdata
->dwarf_str_size
= dwarf_str_size
;
2987 fdata
->is_bigendian
= is_bigendian
;
2988 memset (&fdata
->fvec
, 0, sizeof fdata
->fvec
);
2993 /* Build our data structures from the DWARF sections for a module.
2994 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
2998 backtrace_dwarf_add (struct backtrace_state
*state
,
2999 uintptr_t base_address
,
3000 const unsigned char *dwarf_info
,
3001 size_t dwarf_info_size
,
3002 const unsigned char *dwarf_line
,
3003 size_t dwarf_line_size
,
3004 const unsigned char *dwarf_abbrev
,
3005 size_t dwarf_abbrev_size
,
3006 const unsigned char *dwarf_ranges
,
3007 size_t dwarf_ranges_size
,
3008 const unsigned char *dwarf_str
,
3009 size_t dwarf_str_size
,
3011 backtrace_error_callback error_callback
,
3012 void *data
, fileline
*fileline_fn
)
3014 struct dwarf_data
*fdata
;
3016 fdata
= build_dwarf_data (state
, base_address
, dwarf_info
, dwarf_info_size
,
3017 dwarf_line
, dwarf_line_size
, dwarf_abbrev
,
3018 dwarf_abbrev_size
, dwarf_ranges
, dwarf_ranges_size
,
3019 dwarf_str
, dwarf_str_size
, is_bigendian
,
3020 error_callback
, data
);
3024 if (!state
->threaded
)
3026 struct dwarf_data
**pp
;
3028 for (pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
3038 struct dwarf_data
**pp
;
3040 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
3044 struct dwarf_data
*p
;
3046 p
= backtrace_atomic_load_pointer (pp
);
3054 if (__sync_bool_compare_and_swap (pp
, NULL
, fdata
))
3059 *fileline_fn
= dwarf_fileline
;