1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012-2019 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
38 #include <sys/types.h>
41 #include "filenames.h"
43 #include "backtrace.h"
46 #if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
48 /* If strnlen is not declared, provide our own version. */
51 xstrnlen (const char *s
, size_t maxlen
)
55 for (i
= 0; i
< maxlen
; ++i
)
61 #define strnlen xstrnlen
65 /* A buffer to read DWARF info. */
69 /* Buffer name for error messages. */
71 /* Start of the buffer. */
72 const unsigned char *start
;
73 /* Next byte to read. */
74 const unsigned char *buf
;
75 /* The number of bytes remaining. */
77 /* Whether the data is big-endian. */
79 /* Error callback routine. */
80 backtrace_error_callback error_callback
;
81 /* Data for error_callback. */
83 /* Non-zero if we've reported an underflow error. */
84 int reported_underflow
;
87 /* A single attribute in a DWARF abbreviation. */
91 /* The attribute name. */
92 enum dwarf_attribute name
;
93 /* The attribute form. */
97 /* A single DWARF abbreviation. */
101 /* The abbrev code--the number used to refer to the abbrev. */
105 /* Non-zero if this abbrev has child entries. */
107 /* The number of attributes. */
109 /* The attributes. */
113 /* The DWARF abbreviations for a compilation unit. This structure
114 only exists while reading the compilation unit. Most DWARF readers
115 seem to a hash table to map abbrev ID's to abbrev entries.
116 However, we primarily care about GCC, and GCC simply issues ID's in
117 numerical order starting at 1. So we simply keep a sorted vector,
118 and try to just look up the code. */
122 /* The number of abbrevs in the vector. */
124 /* The abbrevs, sorted by the code field. */
125 struct abbrev
*abbrevs
;
128 /* The different kinds of attribute values. */
130 enum attr_val_encoding
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 /* Compare unit_addrs for qsort. When ranges are nested, make the
927 smallest one sort last. */
930 unit_addrs_compare (const void *v1
, const void *v2
)
932 const struct unit_addrs
*a1
= (const struct unit_addrs
*) v1
;
933 const struct unit_addrs
*a2
= (const struct unit_addrs
*) v2
;
935 if (a1
->low
< a2
->low
)
937 if (a1
->low
> a2
->low
)
939 if (a1
->high
< a2
->high
)
941 if (a1
->high
> a2
->high
)
943 if (a1
->u
->lineoff
< a2
->u
->lineoff
)
945 if (a1
->u
->lineoff
> a2
->u
->lineoff
)
950 /* Compare a PC against a unit_addrs for bsearch. Note that if there
951 are multiple ranges containing PC, which one will be returned is
952 unpredictable. We compensate for that in dwarf_fileline. */
955 unit_addrs_search (const void *vkey
, const void *ventry
)
957 const uintptr_t *key
= (const uintptr_t *) vkey
;
958 const struct unit_addrs
*entry
= (const struct unit_addrs
*) ventry
;
964 else if (pc
>= entry
->high
)
970 /* Sort the line vector by PC. We want a stable sort here to maintain
971 the order of lines for the same PC values. Since the sequence is
972 being sorted in place, their addresses cannot be relied on to
973 maintain stability. That is the purpose of the index member. */
976 line_compare (const void *v1
, const void *v2
)
978 const struct line
*ln1
= (const struct line
*) v1
;
979 const struct line
*ln2
= (const struct line
*) v2
;
981 if (ln1
->pc
< ln2
->pc
)
983 else if (ln1
->pc
> ln2
->pc
)
985 else if (ln1
->idx
< ln2
->idx
)
987 else if (ln1
->idx
> ln2
->idx
)
993 /* Find a PC in a line vector. We always allocate an extra entry at
994 the end of the lines vector, so that this routine can safely look
995 at the next entry. Note that when there are multiple mappings for
996 the same PC value, this will return the last one. */
999 line_search (const void *vkey
, const void *ventry
)
1001 const uintptr_t *key
= (const uintptr_t *) vkey
;
1002 const struct line
*entry
= (const struct line
*) ventry
;
1008 else if (pc
>= (entry
+ 1)->pc
)
1014 /* Sort the abbrevs by the abbrev code. This function is passed to
1015 both qsort and bsearch. */
1018 abbrev_compare (const void *v1
, const void *v2
)
1020 const struct abbrev
*a1
= (const struct abbrev
*) v1
;
1021 const struct abbrev
*a2
= (const struct abbrev
*) v2
;
1023 if (a1
->code
< a2
->code
)
1025 else if (a1
->code
> a2
->code
)
1029 /* This really shouldn't happen. It means there are two
1030 different abbrevs with the same code, and that means we don't
1031 know which one lookup_abbrev should return. */
1036 /* Read the abbreviation table for a compilation unit. Returns 1 on
1037 success, 0 on failure. */
1040 read_abbrevs (struct backtrace_state
*state
, uint64_t abbrev_offset
,
1041 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1042 int is_bigendian
, backtrace_error_callback error_callback
,
1043 void *data
, struct abbrevs
*abbrevs
)
1045 struct dwarf_buf abbrev_buf
;
1046 struct dwarf_buf count_buf
;
1049 abbrevs
->num_abbrevs
= 0;
1050 abbrevs
->abbrevs
= NULL
;
1052 if (abbrev_offset
>= dwarf_abbrev_size
)
1054 error_callback (data
, "abbrev offset out of range", 0);
1058 abbrev_buf
.name
= ".debug_abbrev";
1059 abbrev_buf
.start
= dwarf_abbrev
;
1060 abbrev_buf
.buf
= dwarf_abbrev
+ abbrev_offset
;
1061 abbrev_buf
.left
= dwarf_abbrev_size
- abbrev_offset
;
1062 abbrev_buf
.is_bigendian
= is_bigendian
;
1063 abbrev_buf
.error_callback
= error_callback
;
1064 abbrev_buf
.data
= data
;
1065 abbrev_buf
.reported_underflow
= 0;
1067 /* Count the number of abbrevs in this list. */
1069 count_buf
= abbrev_buf
;
1071 while (read_uleb128 (&count_buf
) != 0)
1073 if (count_buf
.reported_underflow
)
1077 read_uleb128 (&count_buf
);
1078 // Skip has_children.
1079 read_byte (&count_buf
);
1081 while (read_uleb128 (&count_buf
) != 0)
1082 read_uleb128 (&count_buf
);
1083 // Skip form of last attribute.
1084 read_uleb128 (&count_buf
);
1087 if (count_buf
.reported_underflow
)
1090 if (num_abbrevs
== 0)
1093 abbrevs
->abbrevs
= ((struct abbrev
*)
1094 backtrace_alloc (state
,
1095 num_abbrevs
* sizeof (struct abbrev
),
1096 error_callback
, data
));
1097 if (abbrevs
->abbrevs
== NULL
)
1099 abbrevs
->num_abbrevs
= num_abbrevs
;
1100 memset (abbrevs
->abbrevs
, 0, num_abbrevs
* sizeof (struct abbrev
));
1110 if (abbrev_buf
.reported_underflow
)
1113 code
= read_uleb128 (&abbrev_buf
);
1118 a
.tag
= (enum dwarf_tag
) read_uleb128 (&abbrev_buf
);
1119 a
.has_children
= read_byte (&abbrev_buf
);
1121 count_buf
= abbrev_buf
;
1123 while (read_uleb128 (&count_buf
) != 0)
1126 read_uleb128 (&count_buf
);
1132 read_uleb128 (&abbrev_buf
);
1133 read_uleb128 (&abbrev_buf
);
1137 attrs
= ((struct attr
*)
1138 backtrace_alloc (state
, num_attrs
* sizeof *attrs
,
1139 error_callback
, data
));
1148 name
= read_uleb128 (&abbrev_buf
);
1149 form
= read_uleb128 (&abbrev_buf
);
1152 attrs
[num_attrs
].name
= (enum dwarf_attribute
) name
;
1153 attrs
[num_attrs
].form
= (enum dwarf_form
) form
;
1158 a
.num_attrs
= num_attrs
;
1161 abbrevs
->abbrevs
[num_abbrevs
] = a
;
1165 backtrace_qsort (abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1166 sizeof (struct abbrev
), abbrev_compare
);
1171 free_abbrevs (state
, abbrevs
, error_callback
, data
);
1175 /* Return the abbrev information for an abbrev code. */
1177 static const struct abbrev
*
1178 lookup_abbrev (struct abbrevs
*abbrevs
, uint64_t code
,
1179 backtrace_error_callback error_callback
, void *data
)
1184 /* With GCC, where abbrevs are simply numbered in order, we should
1185 be able to just look up the entry. */
1186 if (code
- 1 < abbrevs
->num_abbrevs
1187 && abbrevs
->abbrevs
[code
- 1].code
== code
)
1188 return &abbrevs
->abbrevs
[code
- 1];
1190 /* Otherwise we have to search. */
1191 memset (&key
, 0, sizeof key
);
1193 p
= bsearch (&key
, abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1194 sizeof (struct abbrev
), abbrev_compare
);
1197 error_callback (data
, "invalid abbreviation code", 0);
1200 return (const struct abbrev
*) p
;
1203 /* Add non-contiguous address ranges for a compilation unit. Returns
1204 1 on success, 0 on failure. */
1207 add_unit_ranges (struct backtrace_state
*state
, uintptr_t base_address
,
1208 struct unit
*u
, uint64_t ranges
, uint64_t base
,
1209 int is_bigendian
, const unsigned char *dwarf_ranges
,
1210 size_t dwarf_ranges_size
,
1211 backtrace_error_callback error_callback
, void *data
,
1212 struct unit_addrs_vector
*addrs
)
1214 struct dwarf_buf ranges_buf
;
1216 if (ranges
>= dwarf_ranges_size
)
1218 error_callback (data
, "ranges offset out of range", 0);
1222 ranges_buf
.name
= ".debug_ranges";
1223 ranges_buf
.start
= dwarf_ranges
;
1224 ranges_buf
.buf
= dwarf_ranges
+ ranges
;
1225 ranges_buf
.left
= dwarf_ranges_size
- ranges
;
1226 ranges_buf
.is_bigendian
= is_bigendian
;
1227 ranges_buf
.error_callback
= error_callback
;
1228 ranges_buf
.data
= data
;
1229 ranges_buf
.reported_underflow
= 0;
1236 if (ranges_buf
.reported_underflow
)
1239 low
= read_address (&ranges_buf
, u
->addrsize
);
1240 high
= read_address (&ranges_buf
, u
->addrsize
);
1242 if (low
== 0 && high
== 0)
1245 if (is_highest_address (low
, u
->addrsize
))
1249 struct unit_addrs a
;
1252 a
.high
= high
+ base
;
1254 if (!add_unit_addr (state
, base_address
, a
, error_callback
, data
,
1260 if (ranges_buf
.reported_underflow
)
1266 /* Find the address range covered by a compilation unit, reading from
1267 UNIT_BUF and adding values to U. Returns 1 if all data could be
1268 read, 0 if there is some error. */
1271 find_address_ranges (struct backtrace_state
*state
, uintptr_t base_address
,
1272 struct dwarf_buf
*unit_buf
,
1273 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
1274 const unsigned char *dwarf_ranges
,
1275 size_t dwarf_ranges_size
,
1276 int is_bigendian
, backtrace_error_callback error_callback
,
1277 void *data
, struct unit
*u
,
1278 struct unit_addrs_vector
*addrs
)
1280 while (unit_buf
->left
> 0)
1283 const struct abbrev
*abbrev
;
1288 int highpc_is_relative
;
1293 code
= read_uleb128 (unit_buf
);
1297 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
1305 highpc_is_relative
= 0;
1308 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1310 struct attr_val val
;
1312 if (!read_attribute (abbrev
->attrs
[i
].form
, unit_buf
,
1313 u
->is_dwarf64
, u
->version
, u
->addrsize
,
1314 dwarf_str
, dwarf_str_size
, &val
))
1317 switch (abbrev
->attrs
[i
].name
)
1320 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1328 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1330 highpc
= val
.u
.uint
;
1333 else if (val
.encoding
== ATTR_VAL_UINT
)
1335 highpc
= val
.u
.uint
;
1337 highpc_is_relative
= 1;
1342 if (val
.encoding
== ATTR_VAL_UINT
1343 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1345 ranges
= val
.u
.uint
;
1350 case DW_AT_stmt_list
:
1351 if (abbrev
->tag
== DW_TAG_compile_unit
1352 && (val
.encoding
== ATTR_VAL_UINT
1353 || val
.encoding
== ATTR_VAL_REF_SECTION
))
1354 u
->lineoff
= val
.u
.uint
;
1358 if (abbrev
->tag
== DW_TAG_compile_unit
1359 && val
.encoding
== ATTR_VAL_STRING
)
1360 u
->filename
= val
.u
.string
;
1363 case DW_AT_comp_dir
:
1364 if (abbrev
->tag
== DW_TAG_compile_unit
1365 && val
.encoding
== ATTR_VAL_STRING
)
1366 u
->comp_dir
= val
.u
.string
;
1374 if (abbrev
->tag
== DW_TAG_compile_unit
1375 || abbrev
->tag
== DW_TAG_subprogram
)
1379 if (!add_unit_ranges (state
, base_address
, u
, ranges
, lowpc
,
1380 is_bigendian
, dwarf_ranges
,
1381 dwarf_ranges_size
, error_callback
,
1385 else if (have_lowpc
&& have_highpc
)
1387 struct unit_addrs a
;
1389 if (highpc_is_relative
)
1395 if (!add_unit_addr (state
, base_address
, a
, error_callback
, data
,
1400 /* If we found the PC range in the DW_TAG_compile_unit, we
1402 if (abbrev
->tag
== DW_TAG_compile_unit
1403 && (have_ranges
|| (have_lowpc
&& have_highpc
)))
1407 if (abbrev
->has_children
)
1409 if (!find_address_ranges (state
, base_address
, unit_buf
,
1410 dwarf_str
, dwarf_str_size
,
1411 dwarf_ranges
, dwarf_ranges_size
,
1412 is_bigendian
, error_callback
, data
,
1421 /* Build a mapping from address ranges to the compilation units where
1422 the line number information for that range can be found. Returns 1
1423 on success, 0 on failure. */
1426 build_address_map (struct backtrace_state
*state
, uintptr_t base_address
,
1427 const unsigned char *dwarf_info
, size_t dwarf_info_size
,
1428 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1429 const unsigned char *dwarf_ranges
, size_t dwarf_ranges_size
,
1430 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
1431 int is_bigendian
, backtrace_error_callback error_callback
,
1432 void *data
, struct unit_addrs_vector
*addrs
)
1434 struct dwarf_buf info
;
1435 struct backtrace_vector units
;
1439 size_t prev_addrs_count
;
1441 memset (&addrs
->vec
, 0, sizeof addrs
->vec
);
1443 prev_addrs_count
= 0;
1445 /* Read through the .debug_info section. FIXME: Should we use the
1446 .debug_aranges section? gdb and addr2line don't use it, but I'm
1449 info
.name
= ".debug_info";
1450 info
.start
= dwarf_info
;
1451 info
.buf
= dwarf_info
;
1452 info
.left
= dwarf_info_size
;
1453 info
.is_bigendian
= is_bigendian
;
1454 info
.error_callback
= error_callback
;
1456 info
.reported_underflow
= 0;
1458 memset (&units
, 0, sizeof units
);
1461 while (info
.left
> 0)
1463 const unsigned char *unit_data_start
;
1466 struct dwarf_buf unit_buf
;
1468 uint64_t abbrev_offset
;
1472 if (info
.reported_underflow
)
1475 unit_data_start
= info
.buf
;
1477 len
= read_initial_length (&info
, &is_dwarf64
);
1479 unit_buf
.left
= len
;
1481 if (!advance (&info
, len
))
1484 version
= read_uint16 (&unit_buf
);
1485 if (version
< 2 || version
> 4)
1487 dwarf_buf_error (&unit_buf
, "unrecognized DWARF version");
1491 pu
= ((struct unit
**)
1492 backtrace_vector_grow (state
, sizeof (struct unit
*),
1493 error_callback
, data
, &units
));
1497 u
= ((struct unit
*)
1498 backtrace_alloc (state
, sizeof *u
, error_callback
, data
));
1505 memset (&u
->abbrevs
, 0, sizeof u
->abbrevs
);
1506 abbrev_offset
= read_offset (&unit_buf
, is_dwarf64
);
1507 if (!read_abbrevs (state
, abbrev_offset
, dwarf_abbrev
, dwarf_abbrev_size
,
1508 is_bigendian
, error_callback
, data
, &u
->abbrevs
))
1511 addrsize
= read_byte (&unit_buf
);
1513 u
->unit_data
= unit_buf
.buf
;
1514 u
->unit_data_len
= unit_buf
.left
;
1515 u
->unit_data_offset
= unit_buf
.buf
- unit_data_start
;
1516 u
->version
= version
;
1517 u
->is_dwarf64
= is_dwarf64
;
1518 u
->addrsize
= addrsize
;
1521 u
->abs_filename
= NULL
;
1524 /* The actual line number mappings will be read as needed. */
1527 u
->function_addrs
= NULL
;
1528 u
->function_addrs_count
= 0;
1530 if (!find_address_ranges (state
, base_address
, &unit_buf
,
1531 dwarf_str
, dwarf_str_size
,
1532 dwarf_ranges
, dwarf_ranges_size
,
1533 is_bigendian
, error_callback
, data
,
1537 if (unit_buf
.reported_underflow
)
1540 if (addrs
->count
> prev_addrs_count
)
1541 prev_addrs_count
= addrs
->count
;
1544 /* Unit was not used; remove it from the vector. */
1546 units
.size
-= sizeof (u
);
1547 units
.alc
+= sizeof (u
);
1548 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1549 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1552 if (info
.reported_underflow
)
1555 // We only kept the list of units to free them on failure. On
1556 // success the units are retained, pointed to by the entries in
1558 backtrace_vector_free (state
, &units
, error_callback
, data
);
1563 if (units_count
> 0)
1565 pu
= (struct unit
**) units
.base
;
1566 for (i
= 0; i
< units_count
; i
++)
1568 free_abbrevs (state
, &pu
[i
]->abbrevs
, error_callback
, data
);
1569 backtrace_free (state
, pu
[i
], sizeof **pu
, error_callback
, data
);
1571 backtrace_vector_free (state
, &units
, error_callback
, data
);
1573 if (addrs
->count
> 0)
1575 backtrace_vector_free (state
, &addrs
->vec
, error_callback
, data
);
1581 /* Add a new mapping to the vector of line mappings that we are
1582 building. Returns 1 on success, 0 on failure. */
1585 add_line (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1586 uintptr_t pc
, const char *filename
, int lineno
,
1587 backtrace_error_callback error_callback
, void *data
,
1588 struct line_vector
*vec
)
1592 /* If we are adding the same mapping, ignore it. This can happen
1593 when using discriminators. */
1596 ln
= (struct line
*) vec
->vec
.base
+ (vec
->count
- 1);
1597 if (pc
== ln
->pc
&& filename
== ln
->filename
&& lineno
== ln
->lineno
)
1601 ln
= ((struct line
*)
1602 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1607 /* Add in the base address here, so that we can look up the PC
1609 ln
->pc
= pc
+ ddata
->base_address
;
1611 ln
->filename
= filename
;
1612 ln
->lineno
= lineno
;
1613 ln
->idx
= vec
->count
;
1620 /* Free the line header information. */
1623 free_line_header (struct backtrace_state
*state
, struct line_header
*hdr
,
1624 backtrace_error_callback error_callback
, void *data
)
1626 if (hdr
->dirs_count
!= 0)
1627 backtrace_free (state
, hdr
->dirs
, hdr
->dirs_count
* sizeof (const char *),
1628 error_callback
, data
);
1629 backtrace_free (state
, hdr
->filenames
,
1630 hdr
->filenames_count
* sizeof (char *),
1631 error_callback
, data
);
1634 /* Read the line header. Return 1 on success, 0 on failure. */
1637 read_line_header (struct backtrace_state
*state
, struct unit
*u
,
1638 int is_dwarf64
, struct dwarf_buf
*line_buf
,
1639 struct line_header
*hdr
)
1642 struct dwarf_buf hdr_buf
;
1643 const unsigned char *p
;
1644 const unsigned char *pend
;
1647 hdr
->version
= read_uint16 (line_buf
);
1648 if (hdr
->version
< 2 || hdr
->version
> 4)
1650 dwarf_buf_error (line_buf
, "unsupported line number version");
1654 hdrlen
= read_offset (line_buf
, is_dwarf64
);
1656 hdr_buf
= *line_buf
;
1657 hdr_buf
.left
= hdrlen
;
1659 if (!advance (line_buf
, hdrlen
))
1662 hdr
->min_insn_len
= read_byte (&hdr_buf
);
1663 if (hdr
->version
< 4)
1664 hdr
->max_ops_per_insn
= 1;
1666 hdr
->max_ops_per_insn
= read_byte (&hdr_buf
);
1668 /* We don't care about default_is_stmt. */
1669 read_byte (&hdr_buf
);
1671 hdr
->line_base
= read_sbyte (&hdr_buf
);
1672 hdr
->line_range
= read_byte (&hdr_buf
);
1674 hdr
->opcode_base
= read_byte (&hdr_buf
);
1675 hdr
->opcode_lengths
= hdr_buf
.buf
;
1676 if (!advance (&hdr_buf
, hdr
->opcode_base
- 1))
1679 /* Count the number of directory entries. */
1680 hdr
->dirs_count
= 0;
1682 pend
= p
+ hdr_buf
.left
;
1683 while (p
< pend
&& *p
!= '\0')
1685 p
+= strnlen((const char *) p
, pend
- p
) + 1;
1690 if (hdr
->dirs_count
!= 0)
1692 hdr
->dirs
= ((const char **)
1693 backtrace_alloc (state
,
1694 hdr
->dirs_count
* sizeof (const char *),
1695 line_buf
->error_callback
, line_buf
->data
));
1696 if (hdr
->dirs
== NULL
)
1701 while (*hdr_buf
.buf
!= '\0')
1703 if (hdr_buf
.reported_underflow
)
1706 hdr
->dirs
[i
] = read_string (&hdr_buf
);
1707 if (hdr
->dirs
[i
] == NULL
)
1711 if (!advance (&hdr_buf
, 1))
1714 /* Count the number of file entries. */
1715 hdr
->filenames_count
= 0;
1717 pend
= p
+ hdr_buf
.left
;
1718 while (p
< pend
&& *p
!= '\0')
1720 p
+= strnlen ((const char *) p
, pend
- p
) + 1;
1721 p
+= leb128_len (p
);
1722 p
+= leb128_len (p
);
1723 p
+= leb128_len (p
);
1724 ++hdr
->filenames_count
;
1727 hdr
->filenames
= ((const char **)
1728 backtrace_alloc (state
,
1729 hdr
->filenames_count
* sizeof (char *),
1730 line_buf
->error_callback
,
1732 if (hdr
->filenames
== NULL
)
1735 while (*hdr_buf
.buf
!= '\0')
1737 const char *filename
;
1740 if (hdr_buf
.reported_underflow
)
1743 filename
= read_string (&hdr_buf
);
1744 if (filename
== NULL
)
1746 dir_index
= read_uleb128 (&hdr_buf
);
1747 if (IS_ABSOLUTE_PATH (filename
)
1748 || (dir_index
== 0 && u
->comp_dir
== NULL
))
1749 hdr
->filenames
[i
] = filename
;
1754 size_t filename_len
;
1759 else if (dir_index
- 1 < hdr
->dirs_count
)
1760 dir
= hdr
->dirs
[dir_index
- 1];
1763 dwarf_buf_error (line_buf
,
1764 ("invalid directory index in "
1765 "line number program header"));
1768 dir_len
= strlen (dir
);
1769 filename_len
= strlen (filename
);
1771 backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
1772 line_buf
->error_callback
, line_buf
->data
));
1775 memcpy (s
, dir
, dir_len
);
1776 /* FIXME: If we are on a DOS-based file system, and the
1777 directory or the file name use backslashes, then we
1778 should use a backslash here. */
1780 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
1781 hdr
->filenames
[i
] = s
;
1784 /* Ignore the modification time and size. */
1785 read_uleb128 (&hdr_buf
);
1786 read_uleb128 (&hdr_buf
);
1791 if (hdr_buf
.reported_underflow
)
1797 /* Read the line program, adding line mappings to VEC. Return 1 on
1798 success, 0 on failure. */
1801 read_line_program (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1802 struct unit
*u
, const struct line_header
*hdr
,
1803 struct dwarf_buf
*line_buf
, struct line_vector
*vec
)
1806 unsigned int op_index
;
1807 const char *reset_filename
;
1808 const char *filename
;
1813 if (hdr
->filenames_count
> 0)
1814 reset_filename
= hdr
->filenames
[0];
1816 reset_filename
= "";
1817 filename
= reset_filename
;
1819 while (line_buf
->left
> 0)
1823 op
= read_byte (line_buf
);
1824 if (op
>= hdr
->opcode_base
)
1826 unsigned int advance
;
1828 /* Special opcode. */
1829 op
-= hdr
->opcode_base
;
1830 advance
= op
/ hdr
->line_range
;
1831 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1832 / hdr
->max_ops_per_insn
);
1833 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1834 lineno
+= hdr
->line_base
+ (int) (op
% hdr
->line_range
);
1835 add_line (state
, ddata
, address
, filename
, lineno
,
1836 line_buf
->error_callback
, line_buf
->data
, vec
);
1838 else if (op
== DW_LNS_extended_op
)
1842 len
= read_uleb128 (line_buf
);
1843 op
= read_byte (line_buf
);
1846 case DW_LNE_end_sequence
:
1847 /* FIXME: Should we mark the high PC here? It seems
1848 that we already have that information from the
1849 compilation unit. */
1852 filename
= reset_filename
;
1855 case DW_LNE_set_address
:
1856 address
= read_address (line_buf
, u
->addrsize
);
1858 case DW_LNE_define_file
:
1861 unsigned int dir_index
;
1863 f
= read_string (line_buf
);
1866 dir_index
= read_uleb128 (line_buf
);
1867 /* Ignore that time and length. */
1868 read_uleb128 (line_buf
);
1869 read_uleb128 (line_buf
);
1870 if (IS_ABSOLUTE_PATH (f
))
1881 else if (dir_index
- 1 < hdr
->dirs_count
)
1882 dir
= hdr
->dirs
[dir_index
- 1];
1885 dwarf_buf_error (line_buf
,
1886 ("invalid directory index "
1887 "in line number program"));
1890 dir_len
= strlen (dir
);
1893 backtrace_alloc (state
, dir_len
+ f_len
+ 2,
1894 line_buf
->error_callback
,
1898 memcpy (p
, dir
, dir_len
);
1899 /* FIXME: If we are on a DOS-based file system,
1900 and the directory or the file name use
1901 backslashes, then we should use a backslash
1904 memcpy (p
+ dir_len
+ 1, f
, f_len
+ 1);
1909 case DW_LNE_set_discriminator
:
1910 /* We don't care about discriminators. */
1911 read_uleb128 (line_buf
);
1914 if (!advance (line_buf
, len
- 1))
1924 add_line (state
, ddata
, address
, filename
, lineno
,
1925 line_buf
->error_callback
, line_buf
->data
, vec
);
1927 case DW_LNS_advance_pc
:
1931 advance
= read_uleb128 (line_buf
);
1932 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1933 / hdr
->max_ops_per_insn
);
1934 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1937 case DW_LNS_advance_line
:
1938 lineno
+= (int) read_sleb128 (line_buf
);
1940 case DW_LNS_set_file
:
1944 fileno
= read_uleb128 (line_buf
);
1949 if (fileno
- 1 >= hdr
->filenames_count
)
1951 dwarf_buf_error (line_buf
,
1952 ("invalid file number in "
1953 "line number program"));
1956 filename
= hdr
->filenames
[fileno
- 1];
1960 case DW_LNS_set_column
:
1961 read_uleb128 (line_buf
);
1963 case DW_LNS_negate_stmt
:
1965 case DW_LNS_set_basic_block
:
1967 case DW_LNS_const_add_pc
:
1969 unsigned int advance
;
1971 op
= 255 - hdr
->opcode_base
;
1972 advance
= op
/ hdr
->line_range
;
1973 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1974 / hdr
->max_ops_per_insn
);
1975 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1978 case DW_LNS_fixed_advance_pc
:
1979 address
+= read_uint16 (line_buf
);
1982 case DW_LNS_set_prologue_end
:
1984 case DW_LNS_set_epilogue_begin
:
1986 case DW_LNS_set_isa
:
1987 read_uleb128 (line_buf
);
1993 for (i
= hdr
->opcode_lengths
[op
- 1]; i
> 0; --i
)
1994 read_uleb128 (line_buf
);
2004 /* Read the line number information for a compilation unit. Returns 1
2005 on success, 0 on failure. */
2008 read_line_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2009 backtrace_error_callback error_callback
, void *data
,
2010 struct unit
*u
, struct line_header
*hdr
, struct line
**lines
,
2011 size_t *lines_count
)
2013 struct line_vector vec
;
2014 struct dwarf_buf line_buf
;
2019 memset (&vec
.vec
, 0, sizeof vec
.vec
);
2022 memset (hdr
, 0, sizeof *hdr
);
2024 if (u
->lineoff
!= (off_t
) (size_t) u
->lineoff
2025 || (size_t) u
->lineoff
>= ddata
->dwarf_line_size
)
2027 error_callback (data
, "unit line offset out of range", 0);
2031 line_buf
.name
= ".debug_line";
2032 line_buf
.start
= ddata
->dwarf_line
;
2033 line_buf
.buf
= ddata
->dwarf_line
+ u
->lineoff
;
2034 line_buf
.left
= ddata
->dwarf_line_size
- u
->lineoff
;
2035 line_buf
.is_bigendian
= ddata
->is_bigendian
;
2036 line_buf
.error_callback
= error_callback
;
2037 line_buf
.data
= data
;
2038 line_buf
.reported_underflow
= 0;
2040 len
= read_initial_length (&line_buf
, &is_dwarf64
);
2041 line_buf
.left
= len
;
2043 if (!read_line_header (state
, u
, is_dwarf64
, &line_buf
, hdr
))
2046 if (!read_line_program (state
, ddata
, u
, hdr
, &line_buf
, &vec
))
2049 if (line_buf
.reported_underflow
)
2054 /* This is not a failure in the sense of a generating an error,
2055 but it is a failure in that sense that we have no useful
2060 /* Allocate one extra entry at the end. */
2061 ln
= ((struct line
*)
2062 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
2066 ln
->pc
= (uintptr_t) -1;
2067 ln
->filename
= NULL
;
2071 if (!backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
))
2074 ln
= (struct line
*) vec
.vec
.base
;
2075 backtrace_qsort (ln
, vec
.count
, sizeof (struct line
), line_compare
);
2078 *lines_count
= vec
.count
;
2083 backtrace_vector_free (state
, &vec
.vec
, error_callback
, data
);
2084 free_line_header (state
, hdr
, error_callback
, data
);
2085 *lines
= (struct line
*) (uintptr_t) -1;
2090 /* Read the name of a function from a DIE referenced by a
2091 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
2092 the same compilation unit. */
2095 read_referenced_name (struct dwarf_data
*ddata
, struct unit
*u
,
2096 uint64_t offset
, backtrace_error_callback error_callback
,
2099 struct dwarf_buf unit_buf
;
2101 const struct abbrev
*abbrev
;
2105 /* OFFSET is from the start of the data for this compilation unit.
2106 U->unit_data is the data, but it starts U->unit_data_offset bytes
2107 from the beginning. */
2109 if (offset
< u
->unit_data_offset
2110 || offset
- u
->unit_data_offset
>= u
->unit_data_len
)
2112 error_callback (data
,
2113 "abstract origin or specification out of range",
2118 offset
-= u
->unit_data_offset
;
2120 unit_buf
.name
= ".debug_info";
2121 unit_buf
.start
= ddata
->dwarf_info
;
2122 unit_buf
.buf
= u
->unit_data
+ offset
;
2123 unit_buf
.left
= u
->unit_data_len
- offset
;
2124 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2125 unit_buf
.error_callback
= error_callback
;
2126 unit_buf
.data
= data
;
2127 unit_buf
.reported_underflow
= 0;
2129 code
= read_uleb128 (&unit_buf
);
2132 dwarf_buf_error (&unit_buf
, "invalid abstract origin or specification");
2136 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2141 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2143 struct attr_val val
;
2145 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
,
2146 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2147 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2151 switch (abbrev
->attrs
[i
].name
)
2154 /* We prefer the linkage name if get one. */
2155 if (val
.encoding
== ATTR_VAL_STRING
)
2159 case DW_AT_linkage_name
:
2160 case DW_AT_MIPS_linkage_name
:
2161 if (val
.encoding
== ATTR_VAL_STRING
)
2162 return val
.u
.string
;
2165 case DW_AT_specification
:
2166 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2167 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2169 /* This refers to a specification defined in some other
2170 compilation unit. We can handle this case if we
2171 must, but it's harder. */
2174 if (val
.encoding
== ATTR_VAL_UINT
2175 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2179 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2180 error_callback
, data
);
2194 /* Add a single range to U that maps to function. Returns 1 on
2195 success, 0 on error. */
2198 add_function_range (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2199 struct function
*function
, uint64_t lowpc
, uint64_t highpc
,
2200 backtrace_error_callback error_callback
,
2201 void *data
, struct function_vector
*vec
)
2203 struct function_addrs
*p
;
2205 /* Add in the base address here, so that we can look up the PC
2207 lowpc
+= ddata
->base_address
;
2208 highpc
+= ddata
->base_address
;
2212 p
= (struct function_addrs
*) vec
->vec
.base
+ vec
->count
- 1;
2213 if ((lowpc
== p
->high
|| lowpc
== p
->high
+ 1)
2214 && function
== p
->function
)
2216 if (highpc
> p
->high
)
2222 p
= ((struct function_addrs
*)
2223 backtrace_vector_grow (state
, sizeof (struct function_addrs
),
2224 error_callback
, data
, &vec
->vec
));
2230 p
->function
= function
;
2235 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2239 add_function_ranges (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2240 struct unit
*u
, struct function
*function
,
2241 uint64_t ranges
, uint64_t base
,
2242 backtrace_error_callback error_callback
, void *data
,
2243 struct function_vector
*vec
)
2245 struct dwarf_buf ranges_buf
;
2247 if (ranges
>= ddata
->dwarf_ranges_size
)
2249 error_callback (data
, "function ranges offset out of range", 0);
2253 ranges_buf
.name
= ".debug_ranges";
2254 ranges_buf
.start
= ddata
->dwarf_ranges
;
2255 ranges_buf
.buf
= ddata
->dwarf_ranges
+ ranges
;
2256 ranges_buf
.left
= ddata
->dwarf_ranges_size
- ranges
;
2257 ranges_buf
.is_bigendian
= ddata
->is_bigendian
;
2258 ranges_buf
.error_callback
= error_callback
;
2259 ranges_buf
.data
= data
;
2260 ranges_buf
.reported_underflow
= 0;
2267 if (ranges_buf
.reported_underflow
)
2270 low
= read_address (&ranges_buf
, u
->addrsize
);
2271 high
= read_address (&ranges_buf
, u
->addrsize
);
2273 if (low
== 0 && high
== 0)
2276 if (is_highest_address (low
, u
->addrsize
))
2280 if (!add_function_range (state
, ddata
, function
, low
+ base
,
2281 high
+ base
, error_callback
, data
, vec
))
2286 if (ranges_buf
.reported_underflow
)
2292 /* Read one entry plus all its children. Add function addresses to
2293 VEC. Returns 1 on success, 0 on error. */
2296 read_function_entry (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2297 struct unit
*u
, uint64_t base
, struct dwarf_buf
*unit_buf
,
2298 const struct line_header
*lhdr
,
2299 backtrace_error_callback error_callback
, void *data
,
2300 struct function_vector
*vec_function
,
2301 struct function_vector
*vec_inlined
)
2303 while (unit_buf
->left
> 0)
2306 const struct abbrev
*abbrev
;
2308 struct function
*function
;
2309 struct function_vector
*vec
;
2315 int highpc_is_relative
;
2319 code
= read_uleb128 (unit_buf
);
2323 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2327 is_function
= (abbrev
->tag
== DW_TAG_subprogram
2328 || abbrev
->tag
== DW_TAG_entry_point
2329 || abbrev
->tag
== DW_TAG_inlined_subroutine
);
2331 if (abbrev
->tag
== DW_TAG_inlined_subroutine
)
2339 function
= ((struct function
*)
2340 backtrace_alloc (state
, sizeof *function
,
2341 error_callback
, data
));
2342 if (function
== NULL
)
2344 memset (function
, 0, sizeof *function
);
2351 highpc_is_relative
= 0;
2354 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2356 struct attr_val val
;
2358 if (!read_attribute (abbrev
->attrs
[i
].form
, unit_buf
,
2359 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2360 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2364 /* The compile unit sets the base address for any address
2365 ranges in the function entries. */
2366 if (abbrev
->tag
== DW_TAG_compile_unit
2367 && abbrev
->attrs
[i
].name
== DW_AT_low_pc
2368 && val
.encoding
== ATTR_VAL_ADDRESS
)
2373 switch (abbrev
->attrs
[i
].name
)
2375 case DW_AT_call_file
:
2376 if (val
.encoding
== ATTR_VAL_UINT
)
2378 if (val
.u
.uint
== 0)
2379 function
->caller_filename
= "";
2382 if (val
.u
.uint
- 1 >= lhdr
->filenames_count
)
2384 dwarf_buf_error (unit_buf
,
2385 ("invalid file number in "
2386 "DW_AT_call_file attribute"));
2389 function
->caller_filename
=
2390 lhdr
->filenames
[val
.u
.uint
- 1];
2395 case DW_AT_call_line
:
2396 if (val
.encoding
== ATTR_VAL_UINT
)
2397 function
->caller_lineno
= val
.u
.uint
;
2400 case DW_AT_abstract_origin
:
2401 case DW_AT_specification
:
2402 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2403 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2405 /* This refers to an abstract origin defined in
2406 some other compilation unit. We can handle
2407 this case if we must, but it's harder. */
2410 if (val
.encoding
== ATTR_VAL_UINT
2411 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2415 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2416 error_callback
, data
);
2418 function
->name
= name
;
2423 if (val
.encoding
== ATTR_VAL_STRING
)
2425 /* Don't override a name we found in some other
2426 way, as it will normally be more
2427 useful--e.g., this name is normally not
2429 if (function
->name
== NULL
)
2430 function
->name
= val
.u
.string
;
2434 case DW_AT_linkage_name
:
2435 case DW_AT_MIPS_linkage_name
:
2436 if (val
.encoding
== ATTR_VAL_STRING
)
2437 function
->name
= val
.u
.string
;
2441 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2449 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2451 highpc
= val
.u
.uint
;
2454 else if (val
.encoding
== ATTR_VAL_UINT
)
2456 highpc
= val
.u
.uint
;
2458 highpc_is_relative
= 1;
2463 if (val
.encoding
== ATTR_VAL_UINT
2464 || val
.encoding
== ATTR_VAL_REF_SECTION
)
2466 ranges
= val
.u
.uint
;
2477 /* If we couldn't find a name for the function, we have no use
2479 if (is_function
&& function
->name
== NULL
)
2481 backtrace_free (state
, function
, sizeof *function
,
2482 error_callback
, data
);
2490 if (!add_function_ranges (state
, ddata
, u
, function
, ranges
,
2491 base
, error_callback
, data
, vec
))
2494 else if (have_lowpc
&& have_highpc
)
2496 if (highpc_is_relative
)
2498 if (!add_function_range (state
, ddata
, function
, lowpc
, highpc
,
2499 error_callback
, data
, vec
))
2504 backtrace_free (state
, function
, sizeof *function
,
2505 error_callback
, data
);
2510 if (abbrev
->has_children
)
2514 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2515 error_callback
, data
, vec_function
,
2521 struct function_vector fvec
;
2523 /* Gather any information for inlined functions in
2526 memset (&fvec
, 0, sizeof fvec
);
2528 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2529 error_callback
, data
, vec_function
,
2535 struct function_addrs
*faddrs
;
2537 if (!backtrace_vector_release (state
, &fvec
.vec
,
2538 error_callback
, data
))
2541 faddrs
= (struct function_addrs
*) fvec
.vec
.base
;
2542 backtrace_qsort (faddrs
, fvec
.count
,
2543 sizeof (struct function_addrs
),
2544 function_addrs_compare
);
2546 function
->function_addrs
= faddrs
;
2547 function
->function_addrs_count
= fvec
.count
;
2556 /* Read function name information for a compilation unit. We look
2557 through the whole unit looking for function tags. */
2560 read_function_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2561 const struct line_header
*lhdr
,
2562 backtrace_error_callback error_callback
, void *data
,
2563 struct unit
*u
, struct function_vector
*fvec
,
2564 struct function_addrs
**ret_addrs
,
2565 size_t *ret_addrs_count
)
2567 struct function_vector lvec
;
2568 struct function_vector
*pfvec
;
2569 struct dwarf_buf unit_buf
;
2570 struct function_addrs
*addrs
;
2573 /* Use FVEC if it is not NULL. Otherwise use our own vector. */
2578 memset (&lvec
, 0, sizeof lvec
);
2582 unit_buf
.name
= ".debug_info";
2583 unit_buf
.start
= ddata
->dwarf_info
;
2584 unit_buf
.buf
= u
->unit_data
;
2585 unit_buf
.left
= u
->unit_data_len
;
2586 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2587 unit_buf
.error_callback
= error_callback
;
2588 unit_buf
.data
= data
;
2589 unit_buf
.reported_underflow
= 0;
2591 while (unit_buf
.left
> 0)
2593 if (!read_function_entry (state
, ddata
, u
, 0, &unit_buf
, lhdr
,
2594 error_callback
, data
, pfvec
, pfvec
))
2598 if (pfvec
->count
== 0)
2601 addrs_count
= pfvec
->count
;
2605 if (!backtrace_vector_release (state
, &lvec
.vec
, error_callback
, data
))
2607 addrs
= (struct function_addrs
*) pfvec
->vec
.base
;
2611 /* Finish this list of addresses, but leave the remaining space in
2612 the vector available for the next function unit. */
2613 addrs
= ((struct function_addrs
*)
2614 backtrace_vector_finish (state
, &fvec
->vec
,
2615 error_callback
, data
));
2621 backtrace_qsort (addrs
, addrs_count
, sizeof (struct function_addrs
),
2622 function_addrs_compare
);
2625 *ret_addrs_count
= addrs_count
;
2628 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2629 information, and update FILENAME and LINENO for the caller.
2630 Returns whatever CALLBACK returns, or 0 to keep going. */
2633 report_inlined_functions (uintptr_t pc
, struct function
*function
,
2634 backtrace_full_callback callback
, void *data
,
2635 const char **filename
, int *lineno
)
2637 struct function_addrs
*function_addrs
;
2638 struct function
*inlined
;
2641 if (function
->function_addrs_count
== 0)
2644 function_addrs
= ((struct function_addrs
*)
2645 bsearch (&pc
, function
->function_addrs
,
2646 function
->function_addrs_count
,
2647 sizeof (struct function_addrs
),
2648 function_addrs_search
));
2649 if (function_addrs
== NULL
)
2652 while (((size_t) (function_addrs
- function
->function_addrs
) + 1
2653 < function
->function_addrs_count
)
2654 && pc
>= (function_addrs
+ 1)->low
2655 && pc
< (function_addrs
+ 1)->high
)
2658 /* We found an inlined call. */
2660 inlined
= function_addrs
->function
;
2662 /* Report any calls inlined into this one. */
2663 ret
= report_inlined_functions (pc
, inlined
, callback
, data
,
2668 /* Report this inlined call. */
2669 ret
= callback (data
, pc
, *filename
, *lineno
, inlined
->name
);
2673 /* Our caller will report the caller of the inlined function; tell
2674 it the appropriate filename and line number. */
2675 *filename
= inlined
->caller_filename
;
2676 *lineno
= inlined
->caller_lineno
;
2681 /* Look for a PC in the DWARF mapping for one module. On success,
2682 call CALLBACK and return whatever it returns. On error, call
2683 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
2687 dwarf_lookup_pc (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2688 uintptr_t pc
, backtrace_full_callback callback
,
2689 backtrace_error_callback error_callback
, void *data
,
2692 struct unit_addrs
*entry
;
2697 struct function_addrs
*function_addrs
;
2698 struct function
*function
;
2699 const char *filename
;
2705 /* Find an address range that includes PC. */
2706 entry
= bsearch (&pc
, ddata
->addrs
, ddata
->addrs_count
,
2707 sizeof (struct unit_addrs
), unit_addrs_search
);
2715 /* If there are multiple ranges that contain PC, use the last one,
2716 in order to produce predictable results. If we assume that all
2717 ranges are properly nested, then the last range will be the
2719 while ((size_t) (entry
- ddata
->addrs
) + 1 < ddata
->addrs_count
2720 && pc
>= (entry
+ 1)->low
2721 && pc
< (entry
+ 1)->high
)
2724 /* We need the lines, lines_count, function_addrs,
2725 function_addrs_count fields of u. If they are not set, we need
2726 to set them. When running in threaded mode, we need to allow for
2727 the possibility that some other thread is setting them
2733 /* Skip units with no useful line number information by walking
2734 backward. Useless line number information is marked by setting
2736 while (entry
> ddata
->addrs
2737 && pc
>= (entry
- 1)->low
2738 && pc
< (entry
- 1)->high
)
2740 if (state
->threaded
)
2741 lines
= (struct line
*) backtrace_atomic_load_pointer (&u
->lines
);
2743 if (lines
!= (struct line
*) (uintptr_t) -1)
2752 if (state
->threaded
)
2753 lines
= backtrace_atomic_load_pointer (&u
->lines
);
2758 size_t function_addrs_count
;
2759 struct line_header lhdr
;
2762 /* We have never read the line information for this unit. Read
2765 function_addrs
= NULL
;
2766 function_addrs_count
= 0;
2767 if (read_line_info (state
, ddata
, error_callback
, data
, entry
->u
, &lhdr
,
2770 struct function_vector
*pfvec
;
2772 /* If not threaded, reuse DDATA->FVEC for better memory
2774 if (state
->threaded
)
2777 pfvec
= &ddata
->fvec
;
2778 read_function_info (state
, ddata
, &lhdr
, error_callback
, data
,
2779 entry
->u
, pfvec
, &function_addrs
,
2780 &function_addrs_count
);
2781 free_line_header (state
, &lhdr
, error_callback
, data
);
2785 /* Atomically store the information we just read into the unit.
2786 If another thread is simultaneously writing, it presumably
2787 read the same information, and we don't care which one we
2788 wind up with; we just leak the other one. We do have to
2789 write the lines field last, so that the acquire-loads above
2790 ensure that the other fields are set. */
2792 if (!state
->threaded
)
2794 u
->lines_count
= count
;
2795 u
->function_addrs
= function_addrs
;
2796 u
->function_addrs_count
= function_addrs_count
;
2801 backtrace_atomic_store_size_t (&u
->lines_count
, count
);
2802 backtrace_atomic_store_pointer (&u
->function_addrs
, function_addrs
);
2803 backtrace_atomic_store_size_t (&u
->function_addrs_count
,
2804 function_addrs_count
);
2805 backtrace_atomic_store_pointer (&u
->lines
, lines
);
2809 /* Now all fields of U have been initialized. */
2811 if (lines
== (struct line
*) (uintptr_t) -1)
2813 /* If reading the line number information failed in some way,
2814 try again to see if there is a better compilation unit for
2817 return dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2819 return callback (data
, pc
, NULL
, 0, NULL
);
2822 /* Search for PC within this unit. */
2824 ln
= (struct line
*) bsearch (&pc
, lines
, entry
->u
->lines_count
,
2825 sizeof (struct line
), line_search
);
2828 /* The PC is between the low_pc and high_pc attributes of the
2829 compilation unit, but no entry in the line table covers it.
2830 This implies that the start of the compilation unit has no
2831 line number information. */
2833 if (entry
->u
->abs_filename
== NULL
)
2835 const char *filename
;
2837 filename
= entry
->u
->filename
;
2838 if (filename
!= NULL
2839 && !IS_ABSOLUTE_PATH (filename
)
2840 && entry
->u
->comp_dir
!= NULL
)
2842 size_t filename_len
;
2847 filename_len
= strlen (filename
);
2848 dir
= entry
->u
->comp_dir
;
2849 dir_len
= strlen (dir
);
2850 s
= (char *) backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
2851 error_callback
, data
);
2857 memcpy (s
, dir
, dir_len
);
2858 /* FIXME: Should use backslash if DOS file system. */
2860 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
2863 entry
->u
->abs_filename
= filename
;
2866 return callback (data
, pc
, entry
->u
->abs_filename
, 0, NULL
);
2869 /* Search for function name within this unit. */
2871 if (entry
->u
->function_addrs_count
== 0)
2872 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2874 function_addrs
= ((struct function_addrs
*)
2875 bsearch (&pc
, entry
->u
->function_addrs
,
2876 entry
->u
->function_addrs_count
,
2877 sizeof (struct function_addrs
),
2878 function_addrs_search
));
2879 if (function_addrs
== NULL
)
2880 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2882 /* If there are multiple function ranges that contain PC, use the
2883 last one, in order to produce predictable results. */
2885 while (((size_t) (function_addrs
- entry
->u
->function_addrs
+ 1)
2886 < entry
->u
->function_addrs_count
)
2887 && pc
>= (function_addrs
+ 1)->low
2888 && pc
< (function_addrs
+ 1)->high
)
2891 function
= function_addrs
->function
;
2893 filename
= ln
->filename
;
2894 lineno
= ln
->lineno
;
2896 ret
= report_inlined_functions (pc
, function
, callback
, data
,
2897 &filename
, &lineno
);
2901 return callback (data
, pc
, filename
, lineno
, function
->name
);
2905 /* Return the file/line information for a PC using the DWARF mapping
2906 we built earlier. */
2909 dwarf_fileline (struct backtrace_state
*state
, uintptr_t pc
,
2910 backtrace_full_callback callback
,
2911 backtrace_error_callback error_callback
, void *data
)
2913 struct dwarf_data
*ddata
;
2917 if (!state
->threaded
)
2919 for (ddata
= (struct dwarf_data
*) state
->fileline_data
;
2921 ddata
= ddata
->next
)
2923 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2925 if (ret
!= 0 || found
)
2931 struct dwarf_data
**pp
;
2933 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
2936 ddata
= backtrace_atomic_load_pointer (pp
);
2940 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2942 if (ret
!= 0 || found
)
2949 /* FIXME: See if any libraries have been dlopen'ed. */
2951 return callback (data
, pc
, NULL
, 0, NULL
);
2954 /* Initialize our data structures from the DWARF debug info for a
2955 file. Return NULL on failure. */
2957 static struct dwarf_data
*
2958 build_dwarf_data (struct backtrace_state
*state
,
2959 uintptr_t base_address
,
2960 const unsigned char *dwarf_info
,
2961 size_t dwarf_info_size
,
2962 const unsigned char *dwarf_line
,
2963 size_t dwarf_line_size
,
2964 const unsigned char *dwarf_abbrev
,
2965 size_t dwarf_abbrev_size
,
2966 const unsigned char *dwarf_ranges
,
2967 size_t dwarf_ranges_size
,
2968 const unsigned char *dwarf_str
,
2969 size_t dwarf_str_size
,
2971 backtrace_error_callback error_callback
,
2974 struct unit_addrs_vector addrs_vec
;
2975 struct unit_addrs
*addrs
;
2977 struct dwarf_data
*fdata
;
2979 if (!build_address_map (state
, base_address
, dwarf_info
, dwarf_info_size
,
2980 dwarf_abbrev
, dwarf_abbrev_size
, dwarf_ranges
,
2981 dwarf_ranges_size
, dwarf_str
, dwarf_str_size
,
2982 is_bigendian
, error_callback
, data
, &addrs_vec
))
2985 if (!backtrace_vector_release (state
, &addrs_vec
.vec
, error_callback
, data
))
2987 addrs
= (struct unit_addrs
*) addrs_vec
.vec
.base
;
2988 addrs_count
= addrs_vec
.count
;
2989 backtrace_qsort (addrs
, addrs_count
, sizeof (struct unit_addrs
),
2990 unit_addrs_compare
);
2992 fdata
= ((struct dwarf_data
*)
2993 backtrace_alloc (state
, sizeof (struct dwarf_data
),
2994 error_callback
, data
));
2999 fdata
->base_address
= base_address
;
3000 fdata
->addrs
= addrs
;
3001 fdata
->addrs_count
= addrs_count
;
3002 fdata
->dwarf_info
= dwarf_info
;
3003 fdata
->dwarf_info_size
= dwarf_info_size
;
3004 fdata
->dwarf_line
= dwarf_line
;
3005 fdata
->dwarf_line_size
= dwarf_line_size
;
3006 fdata
->dwarf_ranges
= dwarf_ranges
;
3007 fdata
->dwarf_ranges_size
= dwarf_ranges_size
;
3008 fdata
->dwarf_str
= dwarf_str
;
3009 fdata
->dwarf_str_size
= dwarf_str_size
;
3010 fdata
->is_bigendian
= is_bigendian
;
3011 memset (&fdata
->fvec
, 0, sizeof fdata
->fvec
);
3016 /* Build our data structures from the DWARF sections for a module.
3017 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
3021 backtrace_dwarf_add (struct backtrace_state
*state
,
3022 uintptr_t base_address
,
3023 const unsigned char *dwarf_info
,
3024 size_t dwarf_info_size
,
3025 const unsigned char *dwarf_line
,
3026 size_t dwarf_line_size
,
3027 const unsigned char *dwarf_abbrev
,
3028 size_t dwarf_abbrev_size
,
3029 const unsigned char *dwarf_ranges
,
3030 size_t dwarf_ranges_size
,
3031 const unsigned char *dwarf_str
,
3032 size_t dwarf_str_size
,
3034 backtrace_error_callback error_callback
,
3035 void *data
, fileline
*fileline_fn
)
3037 struct dwarf_data
*fdata
;
3039 fdata
= build_dwarf_data (state
, base_address
, dwarf_info
, dwarf_info_size
,
3040 dwarf_line
, dwarf_line_size
, dwarf_abbrev
,
3041 dwarf_abbrev_size
, dwarf_ranges
, dwarf_ranges_size
,
3042 dwarf_str
, dwarf_str_size
, is_bigendian
,
3043 error_callback
, data
);
3047 if (!state
->threaded
)
3049 struct dwarf_data
**pp
;
3051 for (pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
3061 struct dwarf_data
**pp
;
3063 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
3067 struct dwarf_data
*p
;
3069 p
= backtrace_atomic_load_pointer (pp
);
3077 if (__sync_bool_compare_and_swap (pp
, NULL
, fdata
))
3082 *fileline_fn
= dwarf_fileline
;