1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012 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
;
216 /* A growable vector of line number information. This is used while
217 reading the line numbers. */
221 /* Memory. This is an array of struct line. */
222 struct backtrace_vector vec
;
223 /* Number of valid mappings. */
227 /* A function described in the debug info. */
231 /* The name of the function. */
233 /* If this is an inlined function, the filename of the call
235 const char *caller_filename
;
236 /* If this is an inlined function, the line number of the call
239 /* Map PC ranges to inlined functions. */
240 struct function_addrs
*function_addrs
;
241 size_t function_addrs_count
;
244 /* An address range for a function. This maps a PC value to a
245 specific function. */
247 struct function_addrs
249 /* Range is LOW <= PC < HIGH. */
252 /* Function for this address range. */
253 struct function
*function
;
256 /* A growable vector of function address ranges. */
258 struct function_vector
260 /* Memory. This is an array of struct function_addrs. */
261 struct backtrace_vector vec
;
262 /* Number of address ranges present. */
266 /* A DWARF compilation unit. This only holds the information we need
267 to map a PC to a file and line. */
271 /* The first entry for this compilation unit. */
272 const unsigned char *unit_data
;
273 /* The length of the data for this compilation unit. */
274 size_t unit_data_len
;
275 /* The offset of UNIT_DATA from the start of the information for
276 this compilation unit. */
277 size_t unit_data_offset
;
280 /* Whether unit is DWARF64. */
284 /* Offset into line number information. */
286 /* Compilation command working directory. */
287 const char *comp_dir
;
288 /* The abbreviations for this unit. */
289 struct abbrevs abbrevs
;
291 /* The fields above this point are read in during initialization and
292 may be accessed freely. The fields below this point are read in
293 as needed, and therefore require care, as different threads may
294 try to initialize them simultaneously. */
296 /* PC to line number mapping. This is NULL if the values have not
297 been read. This is (struct line *) -1 if there was an error
298 reading the values. */
300 /* Number of entries in lines. */
302 /* PC ranges to function. */
303 struct function_addrs
*function_addrs
;
304 size_t function_addrs_count
;
307 /* An address range for a compilation unit. This maps a PC value to a
308 specific compilation unit. Note that we invert the representation
309 in DWARF: instead of listing the units and attaching a list of
310 ranges, we list the ranges and have each one point to the unit.
311 This lets us do a binary search to find the unit. */
315 /* Range is LOW <= PC < HIGH. */
318 /* Compilation unit for this address range. */
322 /* A growable vector of compilation unit address ranges. */
324 struct unit_addrs_vector
326 /* Memory. This is an array of struct unit_addrs. */
327 struct backtrace_vector vec
;
328 /* Number of address ranges present. */
332 /* The information we need to map a PC to a file and line. */
336 /* The data for the next file we know about. */
337 struct dwarf_data
*next
;
338 /* The base address for this file. */
339 uintptr_t base_address
;
340 /* A sorted list of address ranges. */
341 struct unit_addrs
*addrs
;
342 /* Number of address ranges in list. */
344 /* The unparsed .debug_info section. */
345 const unsigned char *dwarf_info
;
346 size_t dwarf_info_size
;
347 /* The unparsed .debug_line section. */
348 const unsigned char *dwarf_line
;
349 size_t dwarf_line_size
;
350 /* The unparsed .debug_ranges section. */
351 const unsigned char *dwarf_ranges
;
352 size_t dwarf_ranges_size
;
353 /* The unparsed .debug_str section. */
354 const unsigned char *dwarf_str
;
355 size_t dwarf_str_size
;
356 /* Whether the data is big-endian or not. */
358 /* A vector used for function addresses. We keep this here so that
359 we can grow the vector as we read more functions. */
360 struct function_vector fvec
;
363 /* Report an error for a DWARF buffer. */
366 dwarf_buf_error (struct dwarf_buf
*buf
, const char *msg
)
370 snprintf (b
, sizeof b
, "%s in %s at %d",
371 msg
, buf
->name
, (int) (buf
->buf
- buf
->start
));
372 buf
->error_callback (buf
->data
, b
, 0);
375 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
379 require (struct dwarf_buf
*buf
, size_t count
)
381 if (buf
->left
>= count
)
384 if (!buf
->reported_underflow
)
386 dwarf_buf_error (buf
, "DWARF underflow");
387 buf
->reported_underflow
= 1;
393 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
397 advance (struct dwarf_buf
*buf
, size_t count
)
399 if (!require (buf
, count
))
406 /* Read one byte from BUF and advance 1 byte. */
409 read_byte (struct dwarf_buf
*buf
)
411 const unsigned char *p
= buf
->buf
;
413 if (!advance (buf
, 1))
418 /* Read a signed char from BUF and advance 1 byte. */
421 read_sbyte (struct dwarf_buf
*buf
)
423 const unsigned char *p
= buf
->buf
;
425 if (!advance (buf
, 1))
427 return (*p
^ 0x80) - 0x80;
430 /* Read a uint16 from BUF and advance 2 bytes. */
433 read_uint16 (struct dwarf_buf
*buf
)
435 const unsigned char *p
= buf
->buf
;
437 if (!advance (buf
, 2))
439 if (buf
->is_bigendian
)
440 return ((uint16_t) p
[0] << 8) | (uint16_t) p
[1];
442 return ((uint16_t) p
[1] << 8) | (uint16_t) p
[0];
445 /* Read a uint32 from BUF and advance 4 bytes. */
448 read_uint32 (struct dwarf_buf
*buf
)
450 const unsigned char *p
= buf
->buf
;
452 if (!advance (buf
, 4))
454 if (buf
->is_bigendian
)
455 return (((uint32_t) p
[0] << 24) | ((uint32_t) p
[1] << 16)
456 | ((uint32_t) p
[2] << 8) | (uint32_t) p
[3]);
458 return (((uint32_t) p
[3] << 24) | ((uint32_t) p
[2] << 16)
459 | ((uint32_t) p
[1] << 8) | (uint32_t) p
[0]);
462 /* Read a uint64 from BUF and advance 8 bytes. */
465 read_uint64 (struct dwarf_buf
*buf
)
467 const unsigned char *p
= buf
->buf
;
469 if (!advance (buf
, 8))
471 if (buf
->is_bigendian
)
472 return (((uint64_t) p
[0] << 56) | ((uint64_t) p
[1] << 48)
473 | ((uint64_t) p
[2] << 40) | ((uint64_t) p
[3] << 32)
474 | ((uint64_t) p
[4] << 24) | ((uint64_t) p
[5] << 16)
475 | ((uint64_t) p
[6] << 8) | (uint64_t) p
[7]);
477 return (((uint64_t) p
[7] << 56) | ((uint64_t) p
[6] << 48)
478 | ((uint64_t) p
[5] << 40) | ((uint64_t) p
[4] << 32)
479 | ((uint64_t) p
[3] << 24) | ((uint64_t) p
[2] << 16)
480 | ((uint64_t) p
[1] << 8) | (uint64_t) p
[0]);
483 /* Read an offset from BUF and advance the appropriate number of
487 read_offset (struct dwarf_buf
*buf
, int is_dwarf64
)
490 return read_uint64 (buf
);
492 return read_uint32 (buf
);
495 /* Read an address from BUF and advance the appropriate number of
499 read_address (struct dwarf_buf
*buf
, int addrsize
)
504 return read_byte (buf
);
506 return read_uint16 (buf
);
508 return read_uint32 (buf
);
510 return read_uint64 (buf
);
512 dwarf_buf_error (buf
, "unrecognized address size");
517 /* Return whether a value is the highest possible address, given the
521 is_highest_address (uint64_t address
, int addrsize
)
526 return address
== (unsigned char) -1;
528 return address
== (uint16_t) -1;
530 return address
== (uint32_t) -1;
532 return address
== (uint64_t) -1;
538 /* Read an unsigned LEB128 number. */
541 read_uleb128 (struct dwarf_buf
*buf
)
553 const unsigned char *p
;
556 if (!advance (buf
, 1))
560 ret
|= ((uint64_t) (b
& 0x7f)) << shift
;
563 dwarf_buf_error (buf
, "LEB128 overflows uint64_t");
568 while ((b
& 0x80) != 0);
573 /* Read a signed LEB128 number. */
576 read_sleb128 (struct dwarf_buf
*buf
)
588 const unsigned char *p
;
591 if (!advance (buf
, 1))
595 val
|= ((uint64_t) (b
& 0x7f)) << shift
;
598 dwarf_buf_error (buf
, "signed LEB128 overflows uint64_t");
603 while ((b
& 0x80) != 0);
605 if ((b
& 0x40) != 0 && shift
< 64)
606 val
|= ((uint64_t) -1) << shift
;
608 return (int64_t) val
;
611 /* Return the length of an LEB128 number. */
614 leb128_len (const unsigned char *p
)
619 while ((*p
& 0x80) != 0)
627 /* Free an abbreviations structure. */
630 free_abbrevs (struct backtrace_state
*state
, struct abbrevs
*abbrevs
,
631 backtrace_error_callback error_callback
, void *data
)
635 for (i
= 0; i
< abbrevs
->num_abbrevs
; ++i
)
636 backtrace_free (state
, abbrevs
->abbrevs
[i
].attrs
,
637 abbrevs
->abbrevs
[i
].num_attrs
* sizeof (struct attr
),
638 error_callback
, data
);
639 backtrace_free (state
, abbrevs
->abbrevs
,
640 abbrevs
->num_abbrevs
* sizeof (struct abbrev
),
641 error_callback
, data
);
642 abbrevs
->num_abbrevs
= 0;
643 abbrevs
->abbrevs
= NULL
;
646 /* Read an attribute value. Returns 1 on success, 0 on failure. If
647 the value can be represented as a uint64_t, sets *VAL and sets
648 *IS_VALID to 1. We don't try to store the value of other attribute
649 forms, because we don't care about them. */
652 read_attribute (enum dwarf_form form
, struct dwarf_buf
*buf
,
653 int is_dwarf64
, int version
, int addrsize
,
654 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
655 struct attr_val
*val
)
657 /* Avoid warnings about val.u.FIELD may be used uninitialized if
658 this function is inlined. The warnings aren't valid but can
659 occur because the different fields are set and used
661 memset (val
, 0, sizeof *val
);
666 val
->encoding
= ATTR_VAL_ADDRESS
;
667 val
->u
.uint
= read_address (buf
, addrsize
);
670 val
->encoding
= ATTR_VAL_BLOCK
;
671 return advance (buf
, read_uint16 (buf
));
673 val
->encoding
= ATTR_VAL_BLOCK
;
674 return advance (buf
, read_uint32 (buf
));
676 val
->encoding
= ATTR_VAL_UINT
;
677 val
->u
.uint
= read_uint16 (buf
);
680 val
->encoding
= ATTR_VAL_UINT
;
681 val
->u
.uint
= read_uint32 (buf
);
684 val
->encoding
= ATTR_VAL_UINT
;
685 val
->u
.uint
= read_uint64 (buf
);
688 val
->encoding
= ATTR_VAL_STRING
;
689 val
->u
.string
= (const char *) buf
->buf
;
690 return advance (buf
, strnlen ((const char *) buf
->buf
, buf
->left
) + 1);
692 val
->encoding
= ATTR_VAL_BLOCK
;
693 return advance (buf
, read_uleb128 (buf
));
695 val
->encoding
= ATTR_VAL_BLOCK
;
696 return advance (buf
, read_byte (buf
));
698 val
->encoding
= ATTR_VAL_UINT
;
699 val
->u
.uint
= read_byte (buf
);
702 val
->encoding
= ATTR_VAL_UINT
;
703 val
->u
.uint
= read_byte (buf
);
706 val
->encoding
= ATTR_VAL_SINT
;
707 val
->u
.sint
= read_sleb128 (buf
);
713 offset
= read_offset (buf
, is_dwarf64
);
714 if (offset
>= dwarf_str_size
)
716 dwarf_buf_error (buf
, "DW_FORM_strp out of range");
719 val
->encoding
= ATTR_VAL_STRING
;
720 val
->u
.string
= (const char *) dwarf_str
+ offset
;
724 val
->encoding
= ATTR_VAL_UINT
;
725 val
->u
.uint
= read_uleb128 (buf
);
727 case DW_FORM_ref_addr
:
728 val
->encoding
= ATTR_VAL_REF_INFO
;
730 val
->u
.uint
= read_address (buf
, addrsize
);
732 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
735 val
->encoding
= ATTR_VAL_REF_UNIT
;
736 val
->u
.uint
= read_byte (buf
);
739 val
->encoding
= ATTR_VAL_REF_UNIT
;
740 val
->u
.uint
= read_uint16 (buf
);
743 val
->encoding
= ATTR_VAL_REF_UNIT
;
744 val
->u
.uint
= read_uint32 (buf
);
747 val
->encoding
= ATTR_VAL_REF_UNIT
;
748 val
->u
.uint
= read_uint64 (buf
);
750 case DW_FORM_ref_udata
:
751 val
->encoding
= ATTR_VAL_REF_UNIT
;
752 val
->u
.uint
= read_uleb128 (buf
);
754 case DW_FORM_indirect
:
758 form
= read_uleb128 (buf
);
759 return read_attribute ((enum dwarf_form
) form
, buf
, is_dwarf64
,
760 version
, addrsize
, dwarf_str
, dwarf_str_size
,
763 case DW_FORM_sec_offset
:
764 val
->encoding
= ATTR_VAL_REF_SECTION
;
765 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
767 case DW_FORM_exprloc
:
768 val
->encoding
= ATTR_VAL_EXPR
;
769 return advance (buf
, read_uleb128 (buf
));
770 case DW_FORM_flag_present
:
771 val
->encoding
= ATTR_VAL_UINT
;
774 case DW_FORM_ref_sig8
:
775 val
->encoding
= ATTR_VAL_REF_TYPE
;
776 val
->u
.uint
= read_uint64 (buf
);
778 case DW_FORM_GNU_addr_index
:
779 val
->encoding
= ATTR_VAL_REF_SECTION
;
780 val
->u
.uint
= read_uleb128 (buf
);
782 case DW_FORM_GNU_str_index
:
783 val
->encoding
= ATTR_VAL_REF_SECTION
;
784 val
->u
.uint
= read_uleb128 (buf
);
786 case DW_FORM_GNU_ref_alt
:
787 val
->encoding
= ATTR_VAL_REF_SECTION
;
788 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
790 case DW_FORM_GNU_strp_alt
:
791 val
->encoding
= ATTR_VAL_REF_SECTION
;
792 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
795 dwarf_buf_error (buf
, "unrecognized DWARF form");
800 /* Compare function_addrs for qsort. When ranges are nested, make the
801 smallest one sort last. */
804 function_addrs_compare (const void *v1
, const void *v2
)
806 const struct function_addrs
*a1
= (const struct function_addrs
*) v1
;
807 const struct function_addrs
*a2
= (const struct function_addrs
*) v2
;
809 if (a1
->low
< a2
->low
)
811 if (a1
->low
> a2
->low
)
813 if (a1
->high
< a2
->high
)
815 if (a1
->high
> a2
->high
)
817 return strcmp (a1
->function
->name
, a2
->function
->name
);
820 /* Compare a PC against a function_addrs for bsearch. Note that if
821 there are multiple ranges containing PC, which one will be returned
822 is unpredictable. We compensate for that in dwarf_fileline. */
825 function_addrs_search (const void *vkey
, const void *ventry
)
827 const uintptr_t *key
= (const uintptr_t *) vkey
;
828 const struct function_addrs
*entry
= (const struct function_addrs
*) ventry
;
834 else if (pc
>= entry
->high
)
840 /* Add a new compilation unit address range to a vector. Returns 1 on
841 success, 0 on failure. */
844 add_unit_addr (struct backtrace_state
*state
, uintptr_t base_address
,
845 struct unit_addrs addrs
,
846 backtrace_error_callback error_callback
, void *data
,
847 struct unit_addrs_vector
*vec
)
849 struct unit_addrs
*p
;
851 /* Add in the base address of the module here, so that we can look
852 up the PC directly. */
853 addrs
.low
+= base_address
;
854 addrs
.high
+= base_address
;
856 /* Try to merge with the last entry. */
859 p
= (struct unit_addrs
*) vec
->vec
.base
+ (vec
->count
- 1);
860 if ((addrs
.low
== p
->high
|| addrs
.low
== p
->high
+ 1)
863 if (addrs
.high
> p
->high
)
864 p
->high
= addrs
.high
;
869 p
= ((struct unit_addrs
*)
870 backtrace_vector_grow (state
, sizeof (struct unit_addrs
),
871 error_callback
, data
, &vec
->vec
));
880 /* Free a unit address vector. */
883 free_unit_addrs_vector (struct backtrace_state
*state
,
884 struct unit_addrs_vector
*vec
,
885 backtrace_error_callback error_callback
, void *data
)
887 struct unit_addrs
*addrs
;
890 addrs
= (struct unit_addrs
*) vec
->vec
.base
;
891 for (i
= 0; i
< vec
->count
; ++i
)
892 free_abbrevs (state
, &addrs
[i
].u
->abbrevs
, error_callback
, data
);
895 /* Compare unit_addrs for qsort. When ranges are nested, make the
896 smallest one sort last. */
899 unit_addrs_compare (const void *v1
, const void *v2
)
901 const struct unit_addrs
*a1
= (const struct unit_addrs
*) v1
;
902 const struct unit_addrs
*a2
= (const struct unit_addrs
*) v2
;
904 if (a1
->low
< a2
->low
)
906 if (a1
->low
> a2
->low
)
908 if (a1
->high
< a2
->high
)
910 if (a1
->high
> a2
->high
)
912 if (a1
->u
->lineoff
< a2
->u
->lineoff
)
914 if (a1
->u
->lineoff
> a2
->u
->lineoff
)
919 /* Compare a PC against a unit_addrs for bsearch. Note that if there
920 are multiple ranges containing PC, which one will be returned is
921 unpredictable. We compensate for that in dwarf_fileline. */
924 unit_addrs_search (const void *vkey
, const void *ventry
)
926 const uintptr_t *key
= (const uintptr_t *) vkey
;
927 const struct unit_addrs
*entry
= (const struct unit_addrs
*) ventry
;
933 else if (pc
>= entry
->high
)
939 /* Sort the line vector by PC. We want a stable sort here. We know
940 that the pointers are into the same array, so it is safe to compare
944 line_compare (const void *v1
, const void *v2
)
946 const struct line
*ln1
= (const struct line
*) v1
;
947 const struct line
*ln2
= (const struct line
*) v2
;
949 if (ln1
->pc
< ln2
->pc
)
951 else if (ln1
->pc
> ln2
->pc
)
961 /* Find a PC in a line vector. We always allocate an extra entry at
962 the end of the lines vector, so that this routine can safely look
963 at the next entry. Note that when there are multiple mappings for
964 the same PC value, this will return the last one. */
967 line_search (const void *vkey
, const void *ventry
)
969 const uintptr_t *key
= (const uintptr_t *) vkey
;
970 const struct line
*entry
= (const struct line
*) ventry
;
976 else if (pc
>= (entry
+ 1)->pc
)
982 /* Sort the abbrevs by the abbrev code. This function is passed to
983 both qsort and bsearch. */
986 abbrev_compare (const void *v1
, const void *v2
)
988 const struct abbrev
*a1
= (const struct abbrev
*) v1
;
989 const struct abbrev
*a2
= (const struct abbrev
*) v2
;
991 if (a1
->code
< a2
->code
)
993 else if (a1
->code
> a2
->code
)
997 /* This really shouldn't happen. It means there are two
998 different abbrevs with the same code, and that means we don't
999 know which one lookup_abbrev should return. */
1004 /* Read the abbreviation table for a compilation unit. Returns 1 on
1005 success, 0 on failure. */
1008 read_abbrevs (struct backtrace_state
*state
, uint64_t abbrev_offset
,
1009 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1010 int is_bigendian
, backtrace_error_callback error_callback
,
1011 void *data
, struct abbrevs
*abbrevs
)
1013 struct dwarf_buf abbrev_buf
;
1014 struct dwarf_buf count_buf
;
1017 abbrevs
->num_abbrevs
= 0;
1018 abbrevs
->abbrevs
= NULL
;
1020 if (abbrev_offset
>= dwarf_abbrev_size
)
1022 error_callback (data
, "abbrev offset out of range", 0);
1026 abbrev_buf
.name
= ".debug_abbrev";
1027 abbrev_buf
.start
= dwarf_abbrev
;
1028 abbrev_buf
.buf
= dwarf_abbrev
+ abbrev_offset
;
1029 abbrev_buf
.left
= dwarf_abbrev_size
- abbrev_offset
;
1030 abbrev_buf
.is_bigendian
= is_bigendian
;
1031 abbrev_buf
.error_callback
= error_callback
;
1032 abbrev_buf
.data
= data
;
1033 abbrev_buf
.reported_underflow
= 0;
1035 /* Count the number of abbrevs in this list. */
1037 count_buf
= abbrev_buf
;
1039 while (read_uleb128 (&count_buf
) != 0)
1041 if (count_buf
.reported_underflow
)
1045 read_uleb128 (&count_buf
);
1046 // Skip has_children.
1047 read_byte (&count_buf
);
1049 while (read_uleb128 (&count_buf
) != 0)
1050 read_uleb128 (&count_buf
);
1051 // Skip form of last attribute.
1052 read_uleb128 (&count_buf
);
1055 if (count_buf
.reported_underflow
)
1058 if (num_abbrevs
== 0)
1061 abbrevs
->num_abbrevs
= num_abbrevs
;
1062 abbrevs
->abbrevs
= ((struct abbrev
*)
1063 backtrace_alloc (state
,
1064 num_abbrevs
* sizeof (struct abbrev
),
1065 error_callback
, data
));
1066 if (abbrevs
->abbrevs
== NULL
)
1068 memset (abbrevs
->abbrevs
, 0, num_abbrevs
* sizeof (struct abbrev
));
1078 if (abbrev_buf
.reported_underflow
)
1081 code
= read_uleb128 (&abbrev_buf
);
1086 a
.tag
= (enum dwarf_tag
) read_uleb128 (&abbrev_buf
);
1087 a
.has_children
= read_byte (&abbrev_buf
);
1089 count_buf
= abbrev_buf
;
1091 while (read_uleb128 (&count_buf
) != 0)
1094 read_uleb128 (&count_buf
);
1100 read_uleb128 (&abbrev_buf
);
1101 read_uleb128 (&abbrev_buf
);
1105 attrs
= ((struct attr
*)
1106 backtrace_alloc (state
, num_attrs
* sizeof *attrs
,
1107 error_callback
, data
));
1116 name
= read_uleb128 (&abbrev_buf
);
1117 form
= read_uleb128 (&abbrev_buf
);
1120 attrs
[num_attrs
].name
= (enum dwarf_attribute
) name
;
1121 attrs
[num_attrs
].form
= (enum dwarf_form
) form
;
1126 a
.num_attrs
= num_attrs
;
1129 abbrevs
->abbrevs
[num_abbrevs
] = a
;
1133 qsort (abbrevs
->abbrevs
, abbrevs
->num_abbrevs
, sizeof (struct abbrev
),
1139 free_abbrevs (state
, abbrevs
, error_callback
, data
);
1143 /* Return the abbrev information for an abbrev code. */
1145 static const struct abbrev
*
1146 lookup_abbrev (struct abbrevs
*abbrevs
, uint64_t code
,
1147 backtrace_error_callback error_callback
, void *data
)
1152 /* With GCC, where abbrevs are simply numbered in order, we should
1153 be able to just look up the entry. */
1154 if (code
- 1 < abbrevs
->num_abbrevs
1155 && abbrevs
->abbrevs
[code
- 1].code
== code
)
1156 return &abbrevs
->abbrevs
[code
- 1];
1158 /* Otherwise we have to search. */
1159 memset (&key
, 0, sizeof key
);
1161 p
= bsearch (&key
, abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1162 sizeof (struct abbrev
), abbrev_compare
);
1165 error_callback (data
, "invalid abbreviation code", 0);
1168 return (const struct abbrev
*) p
;
1171 /* Add non-contiguous address ranges for a compilation unit. Returns
1172 1 on success, 0 on failure. */
1175 add_unit_ranges (struct backtrace_state
*state
, uintptr_t base_address
,
1176 struct unit
*u
, uint64_t ranges
, uint64_t base
,
1177 int is_bigendian
, const unsigned char *dwarf_ranges
,
1178 size_t dwarf_ranges_size
,
1179 backtrace_error_callback error_callback
, void *data
,
1180 struct unit_addrs_vector
*addrs
)
1182 struct dwarf_buf ranges_buf
;
1184 if (ranges
>= dwarf_ranges_size
)
1186 error_callback (data
, "ranges offset out of range", 0);
1190 ranges_buf
.name
= ".debug_ranges";
1191 ranges_buf
.start
= dwarf_ranges
;
1192 ranges_buf
.buf
= dwarf_ranges
+ ranges
;
1193 ranges_buf
.left
= dwarf_ranges_size
- ranges
;
1194 ranges_buf
.is_bigendian
= is_bigendian
;
1195 ranges_buf
.error_callback
= error_callback
;
1196 ranges_buf
.data
= data
;
1197 ranges_buf
.reported_underflow
= 0;
1204 if (ranges_buf
.reported_underflow
)
1207 low
= read_address (&ranges_buf
, u
->addrsize
);
1208 high
= read_address (&ranges_buf
, u
->addrsize
);
1210 if (low
== 0 && high
== 0)
1213 if (is_highest_address (low
, u
->addrsize
))
1217 struct unit_addrs a
;
1220 a
.high
= high
+ base
;
1222 if (!add_unit_addr (state
, base_address
, a
, error_callback
, data
,
1228 if (ranges_buf
.reported_underflow
)
1234 /* Build a mapping from address ranges to the compilation units where
1235 the line number information for that range can be found. Returns 1
1236 on success, 0 on failure. */
1239 build_address_map (struct backtrace_state
*state
, uintptr_t base_address
,
1240 const unsigned char *dwarf_info
, size_t dwarf_info_size
,
1241 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1242 const unsigned char *dwarf_ranges
, size_t dwarf_ranges_size
,
1243 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
1244 int is_bigendian
, backtrace_error_callback error_callback
,
1245 void *data
, struct unit_addrs_vector
*addrs
)
1247 struct dwarf_buf info
;
1248 struct abbrevs abbrevs
;
1250 memset (&addrs
->vec
, 0, sizeof addrs
->vec
);
1253 /* Read through the .debug_info section. FIXME: Should we use the
1254 .debug_aranges section? gdb and addr2line don't use it, but I'm
1257 info
.name
= ".debug_info";
1258 info
.start
= dwarf_info
;
1259 info
.buf
= dwarf_info
;
1260 info
.left
= dwarf_info_size
;
1261 info
.is_bigendian
= is_bigendian
;
1262 info
.error_callback
= error_callback
;
1264 info
.reported_underflow
= 0;
1266 memset (&abbrevs
, 0, sizeof abbrevs
);
1267 while (info
.left
> 0)
1269 const unsigned char *unit_data_start
;
1272 struct dwarf_buf unit_buf
;
1274 uint64_t abbrev_offset
;
1275 const struct abbrev
*abbrev
;
1277 const unsigned char *unit_data
;
1278 size_t unit_data_len
;
1279 size_t unit_data_offset
;
1286 int highpc_is_relative
;
1291 const char *comp_dir
;
1293 if (info
.reported_underflow
)
1296 unit_data_start
= info
.buf
;
1299 len
= read_uint32 (&info
);
1300 if (len
== 0xffffffff)
1302 len
= read_uint64 (&info
);
1307 unit_buf
.left
= len
;
1309 if (!advance (&info
, len
))
1312 version
= read_uint16 (&unit_buf
);
1313 if (version
< 2 || version
> 4)
1315 dwarf_buf_error (&unit_buf
, "unrecognized DWARF version");
1319 abbrev_offset
= read_offset (&unit_buf
, is_dwarf64
);
1320 if (!read_abbrevs (state
, abbrev_offset
, dwarf_abbrev
, dwarf_abbrev_size
,
1321 is_bigendian
, error_callback
, data
, &abbrevs
))
1324 addrsize
= read_byte (&unit_buf
);
1326 unit_data
= unit_buf
.buf
;
1327 unit_data_len
= unit_buf
.left
;
1328 unit_data_offset
= unit_buf
.buf
- unit_data_start
;
1330 /* We only look at the first attribute in the compilation unit.
1331 In practice this will be a DW_TAG_compile_unit which will
1332 tell us the PC range and where to find the line number
1335 code
= read_uleb128 (&unit_buf
);
1336 abbrev
= lookup_abbrev (&abbrevs
, code
, error_callback
, data
);
1344 highpc_is_relative
= 0;
1350 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1352 struct attr_val val
;
1354 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
, is_dwarf64
,
1355 version
, addrsize
, dwarf_str
, dwarf_str_size
,
1359 switch (abbrev
->attrs
[i
].name
)
1362 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1369 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1371 highpc
= val
.u
.uint
;
1374 else if (val
.encoding
== ATTR_VAL_UINT
)
1376 highpc
= val
.u
.uint
;
1378 highpc_is_relative
= 1;
1382 if (val
.encoding
== ATTR_VAL_UINT
1383 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1385 ranges
= val
.u
.uint
;
1389 case DW_AT_stmt_list
:
1390 if (val
.encoding
== ATTR_VAL_UINT
1391 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1393 lineoff
= val
.u
.uint
;
1397 case DW_AT_comp_dir
:
1398 if (val
.encoding
== ATTR_VAL_STRING
)
1399 comp_dir
= val
.u
.string
;
1406 if (unit_buf
.reported_underflow
)
1409 if (((have_lowpc
&& have_highpc
) || have_ranges
) && have_lineoff
)
1412 struct unit_addrs a
;
1414 u
= ((struct unit
*)
1415 backtrace_alloc (state
, sizeof *u
, error_callback
, data
));
1418 u
->unit_data
= unit_data
;
1419 u
->unit_data_len
= unit_data_len
;
1420 u
->unit_data_offset
= unit_data_offset
;
1421 u
->version
= version
;
1422 u
->is_dwarf64
= is_dwarf64
;
1423 u
->addrsize
= addrsize
;
1424 u
->comp_dir
= comp_dir
;
1425 u
->lineoff
= lineoff
;
1426 u
->abbrevs
= abbrevs
;
1427 memset (&abbrevs
, 0, sizeof abbrevs
);
1429 /* The actual line number mappings will be read as
1433 u
->function_addrs
= NULL
;
1434 u
->function_addrs_count
= 0;
1438 if (!add_unit_ranges (state
, base_address
, u
, ranges
, lowpc
,
1439 is_bigendian
, dwarf_ranges
,
1440 dwarf_ranges_size
, error_callback
, data
,
1443 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1444 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1450 if (highpc_is_relative
)
1456 if (!add_unit_addr (state
, base_address
, a
, error_callback
, data
,
1459 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1460 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1467 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1468 memset (&abbrevs
, 0, sizeof abbrevs
);
1471 if (info
.reported_underflow
)
1477 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1478 free_unit_addrs_vector (state
, addrs
, error_callback
, data
);
1482 /* Add a new mapping to the vector of line mappings that we are
1483 building. Returns 1 on success, 0 on failure. */
1486 add_line (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1487 uintptr_t pc
, const char *filename
, int lineno
,
1488 backtrace_error_callback error_callback
, void *data
,
1489 struct line_vector
*vec
)
1493 /* If we are adding the same mapping, ignore it. This can happen
1494 when using discriminators. */
1497 ln
= (struct line
*) vec
->vec
.base
+ (vec
->count
- 1);
1498 if (pc
== ln
->pc
&& filename
== ln
->filename
&& lineno
== ln
->lineno
)
1502 ln
= ((struct line
*)
1503 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1508 /* Add in the base address here, so that we can look up the PC
1510 ln
->pc
= pc
+ ddata
->base_address
;
1512 ln
->filename
= filename
;
1513 ln
->lineno
= lineno
;
1520 /* Free the line header information. If FREE_FILENAMES is true we
1521 free the file names themselves, otherwise we leave them, as there
1522 may be line structures pointing to them. */
1525 free_line_header (struct backtrace_state
*state
, struct line_header
*hdr
,
1526 backtrace_error_callback error_callback
, void *data
)
1528 backtrace_free (state
, hdr
->dirs
, hdr
->dirs_count
* sizeof (const char *),
1529 error_callback
, data
);
1530 backtrace_free (state
, hdr
->filenames
,
1531 hdr
->filenames_count
* sizeof (char *),
1532 error_callback
, data
);
1535 /* Read the line header. Return 1 on success, 0 on failure. */
1538 read_line_header (struct backtrace_state
*state
, struct unit
*u
,
1539 int is_dwarf64
, struct dwarf_buf
*line_buf
,
1540 struct line_header
*hdr
)
1543 struct dwarf_buf hdr_buf
;
1544 const unsigned char *p
;
1545 const unsigned char *pend
;
1548 hdr
->version
= read_uint16 (line_buf
);
1549 if (hdr
->version
< 2 || hdr
->version
> 4)
1551 dwarf_buf_error (line_buf
, "unsupported line number version");
1555 hdrlen
= read_offset (line_buf
, is_dwarf64
);
1557 hdr_buf
= *line_buf
;
1558 hdr_buf
.left
= hdrlen
;
1560 if (!advance (line_buf
, hdrlen
))
1563 hdr
->min_insn_len
= read_byte (&hdr_buf
);
1564 if (hdr
->version
< 4)
1565 hdr
->max_ops_per_insn
= 1;
1567 hdr
->max_ops_per_insn
= read_byte (&hdr_buf
);
1569 /* We don't care about default_is_stmt. */
1570 read_byte (&hdr_buf
);
1572 hdr
->line_base
= read_sbyte (&hdr_buf
);
1573 hdr
->line_range
= read_byte (&hdr_buf
);
1575 hdr
->opcode_base
= read_byte (&hdr_buf
);
1576 hdr
->opcode_lengths
= hdr_buf
.buf
;
1577 if (!advance (&hdr_buf
, hdr
->opcode_base
- 1))
1580 /* Count the number of directory entries. */
1581 hdr
->dirs_count
= 0;
1583 pend
= p
+ hdr_buf
.left
;
1584 while (p
< pend
&& *p
!= '\0')
1586 p
+= strnlen((const char *) p
, pend
- p
) + 1;
1590 hdr
->dirs
= ((const char **)
1591 backtrace_alloc (state
,
1592 hdr
->dirs_count
* sizeof (const char *),
1593 line_buf
->error_callback
, line_buf
->data
));
1594 if (hdr
->dirs
== NULL
)
1598 while (*hdr_buf
.buf
!= '\0')
1600 if (hdr_buf
.reported_underflow
)
1603 hdr
->dirs
[i
] = (const char *) hdr_buf
.buf
;
1605 if (!advance (&hdr_buf
,
1606 strnlen ((const char *) hdr_buf
.buf
, hdr_buf
.left
) + 1))
1609 if (!advance (&hdr_buf
, 1))
1612 /* Count the number of file entries. */
1613 hdr
->filenames_count
= 0;
1615 pend
= p
+ hdr_buf
.left
;
1616 while (p
< pend
&& *p
!= '\0')
1618 p
+= strnlen ((const char *) p
, pend
- p
) + 1;
1619 p
+= leb128_len (p
);
1620 p
+= leb128_len (p
);
1621 p
+= leb128_len (p
);
1622 ++hdr
->filenames_count
;
1625 hdr
->filenames
= ((const char **)
1626 backtrace_alloc (state
,
1627 hdr
->filenames_count
* sizeof (char *),
1628 line_buf
->error_callback
,
1630 if (hdr
->filenames
== NULL
)
1633 while (*hdr_buf
.buf
!= '\0')
1635 const char *filename
;
1638 if (hdr_buf
.reported_underflow
)
1641 filename
= (const char *) hdr_buf
.buf
;
1642 if (!advance (&hdr_buf
,
1643 strnlen ((const char *) hdr_buf
.buf
, hdr_buf
.left
) + 1))
1645 dir_index
= read_uleb128 (&hdr_buf
);
1646 if (IS_ABSOLUTE_PATH (filename
))
1647 hdr
->filenames
[i
] = filename
;
1652 size_t filename_len
;
1657 else if (dir_index
- 1 < hdr
->dirs_count
)
1658 dir
= hdr
->dirs
[dir_index
- 1];
1661 dwarf_buf_error (line_buf
,
1662 ("invalid directory index in "
1663 "line number program header"));
1666 dir_len
= strlen (dir
);
1667 filename_len
= strlen (filename
);
1669 backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
1670 line_buf
->error_callback
, line_buf
->data
));
1673 memcpy (s
, dir
, dir_len
);
1674 /* FIXME: If we are on a DOS-based file system, and the
1675 directory or the file name use backslashes, then we
1676 should use a backslash here. */
1678 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
1679 hdr
->filenames
[i
] = s
;
1682 /* Ignore the modification time and size. */
1683 read_uleb128 (&hdr_buf
);
1684 read_uleb128 (&hdr_buf
);
1689 if (hdr_buf
.reported_underflow
)
1695 /* Read the line program, adding line mappings to VEC. Return 1 on
1696 success, 0 on failure. */
1699 read_line_program (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1700 struct unit
*u
, const struct line_header
*hdr
,
1701 struct dwarf_buf
*line_buf
, struct line_vector
*vec
)
1704 unsigned int op_index
;
1705 const char *reset_filename
;
1706 const char *filename
;
1711 if (hdr
->filenames_count
> 0)
1712 reset_filename
= hdr
->filenames
[0];
1714 reset_filename
= "";
1715 filename
= reset_filename
;
1717 while (line_buf
->left
> 0)
1721 op
= read_byte (line_buf
);
1722 if (op
>= hdr
->opcode_base
)
1724 unsigned int advance
;
1726 /* Special opcode. */
1727 op
-= hdr
->opcode_base
;
1728 advance
= op
/ hdr
->line_range
;
1729 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1730 / hdr
->max_ops_per_insn
);
1731 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1732 lineno
+= hdr
->line_base
+ (int) (op
% hdr
->line_range
);
1733 add_line (state
, ddata
, address
, filename
, lineno
,
1734 line_buf
->error_callback
, line_buf
->data
, vec
);
1736 else if (op
== DW_LNS_extended_op
)
1740 len
= read_uleb128 (line_buf
);
1741 op
= read_byte (line_buf
);
1744 case DW_LNE_end_sequence
:
1745 /* FIXME: Should we mark the high PC here? It seems
1746 that we already have that information from the
1747 compilation unit. */
1750 filename
= reset_filename
;
1753 case DW_LNE_set_address
:
1754 address
= read_address (line_buf
, u
->addrsize
);
1756 case DW_LNE_define_file
:
1759 unsigned int dir_index
;
1761 f
= (const char *) line_buf
->buf
;
1762 if (!advance (line_buf
, strnlen (f
, line_buf
->left
) + 1))
1764 dir_index
= read_uleb128 (line_buf
);
1765 /* Ignore that time and length. */
1766 read_uleb128 (line_buf
);
1767 read_uleb128 (line_buf
);
1768 if (IS_ABSOLUTE_PATH (f
))
1779 else if (dir_index
- 1 < hdr
->dirs_count
)
1780 dir
= hdr
->dirs
[dir_index
- 1];
1783 dwarf_buf_error (line_buf
,
1784 ("invalid directory index "
1785 "in line number program"));
1788 dir_len
= strlen (dir
);
1791 backtrace_alloc (state
, dir_len
+ f_len
+ 2,
1792 line_buf
->error_callback
,
1796 memcpy (p
, dir
, dir_len
);
1797 /* FIXME: If we are on a DOS-based file system,
1798 and the directory or the file name use
1799 backslashes, then we should use a backslash
1802 memcpy (p
+ dir_len
+ 1, f
, f_len
+ 1);
1807 case DW_LNE_set_discriminator
:
1808 /* We don't care about discriminators. */
1809 read_uleb128 (line_buf
);
1812 if (!advance (line_buf
, len
- 1))
1822 add_line (state
, ddata
, address
, filename
, lineno
,
1823 line_buf
->error_callback
, line_buf
->data
, vec
);
1825 case DW_LNS_advance_pc
:
1829 advance
= read_uleb128 (line_buf
);
1830 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1831 / hdr
->max_ops_per_insn
);
1832 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1835 case DW_LNS_advance_line
:
1836 lineno
+= (int) read_sleb128 (line_buf
);
1838 case DW_LNS_set_file
:
1842 fileno
= read_uleb128 (line_buf
);
1847 if (fileno
- 1 >= hdr
->filenames_count
)
1849 dwarf_buf_error (line_buf
,
1850 ("invalid file number in "
1851 "line number program"));
1854 filename
= hdr
->filenames
[fileno
- 1];
1858 case DW_LNS_set_column
:
1859 read_uleb128 (line_buf
);
1861 case DW_LNS_negate_stmt
:
1863 case DW_LNS_set_basic_block
:
1865 case DW_LNS_const_add_pc
:
1867 unsigned int advance
;
1869 op
= 255 - hdr
->opcode_base
;
1870 advance
= op
/ hdr
->line_range
;
1871 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1872 / hdr
->max_ops_per_insn
);
1873 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1876 case DW_LNS_fixed_advance_pc
:
1877 address
+= read_uint16 (line_buf
);
1880 case DW_LNS_set_prologue_end
:
1882 case DW_LNS_set_epilogue_begin
:
1884 case DW_LNS_set_isa
:
1885 read_uleb128 (line_buf
);
1891 for (i
= hdr
->opcode_lengths
[op
- 1]; i
> 0; --i
)
1892 read_uleb128 (line_buf
);
1902 /* Read the line number information for a compilation unit. Returns 1
1903 on success, 0 on failure. */
1906 read_line_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1907 backtrace_error_callback error_callback
, void *data
,
1908 struct unit
*u
, struct line_header
*hdr
, struct line
**lines
,
1909 size_t *lines_count
)
1911 struct line_vector vec
;
1912 struct dwarf_buf line_buf
;
1917 memset (&vec
.vec
, 0, sizeof vec
.vec
);
1920 memset (hdr
, 0, sizeof *hdr
);
1922 if (u
->lineoff
!= (off_t
) (size_t) u
->lineoff
1923 || (size_t) u
->lineoff
>= ddata
->dwarf_line_size
)
1925 error_callback (data
, "unit line offset out of range", 0);
1929 line_buf
.name
= ".debug_line";
1930 line_buf
.start
= ddata
->dwarf_line
;
1931 line_buf
.buf
= ddata
->dwarf_line
+ u
->lineoff
;
1932 line_buf
.left
= ddata
->dwarf_line_size
- u
->lineoff
;
1933 line_buf
.is_bigendian
= ddata
->is_bigendian
;
1934 line_buf
.error_callback
= error_callback
;
1935 line_buf
.data
= data
;
1936 line_buf
.reported_underflow
= 0;
1939 len
= read_uint32 (&line_buf
);
1940 if (len
== 0xffffffff)
1942 len
= read_uint64 (&line_buf
);
1945 line_buf
.left
= len
;
1947 if (!read_line_header (state
, u
, is_dwarf64
, &line_buf
, hdr
))
1950 if (!read_line_program (state
, ddata
, u
, hdr
, &line_buf
, &vec
))
1953 if (line_buf
.reported_underflow
)
1958 /* This is not a failure in the sense of a generating an error,
1959 but it is a failure in that sense that we have no useful
1964 /* Allocate one extra entry at the end. */
1965 ln
= ((struct line
*)
1966 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1970 ln
->pc
= (uintptr_t) -1;
1971 ln
->filename
= NULL
;
1974 if (!backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
))
1977 ln
= (struct line
*) vec
.vec
.base
;
1978 qsort (ln
, vec
.count
, sizeof (struct line
), line_compare
);
1981 *lines_count
= vec
.count
;
1986 vec
.vec
.alc
+= vec
.vec
.size
;
1988 backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
);
1989 free_line_header (state
, hdr
, error_callback
, data
);
1990 *lines
= (struct line
*) (uintptr_t) -1;
1995 /* Read the name of a function from a DIE referenced by a
1996 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
1997 the same compilation unit. */
2000 read_referenced_name (struct dwarf_data
*ddata
, struct unit
*u
,
2001 uint64_t offset
, backtrace_error_callback error_callback
,
2004 struct dwarf_buf unit_buf
;
2006 const struct abbrev
*abbrev
;
2010 /* OFFSET is from the start of the data for this compilation unit.
2011 U->unit_data is the data, but it starts U->unit_data_offset bytes
2012 from the beginning. */
2014 if (offset
< u
->unit_data_offset
2015 || offset
- u
->unit_data_offset
>= u
->unit_data_len
)
2017 error_callback (data
,
2018 "abstract origin or specification out of range",
2023 offset
-= u
->unit_data_offset
;
2025 unit_buf
.name
= ".debug_info";
2026 unit_buf
.start
= ddata
->dwarf_info
;
2027 unit_buf
.buf
= u
->unit_data
+ offset
;
2028 unit_buf
.left
= u
->unit_data_len
- offset
;
2029 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2030 unit_buf
.error_callback
= error_callback
;
2031 unit_buf
.data
= data
;
2032 unit_buf
.reported_underflow
= 0;
2034 code
= read_uleb128 (&unit_buf
);
2037 dwarf_buf_error (&unit_buf
, "invalid abstract origin or specification");
2041 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2046 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2048 struct attr_val val
;
2050 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
,
2051 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2052 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2056 switch (abbrev
->attrs
[i
].name
)
2059 /* We prefer the linkage name if get one. */
2060 if (val
.encoding
== ATTR_VAL_STRING
)
2064 case DW_AT_linkage_name
:
2065 case DW_AT_MIPS_linkage_name
:
2066 if (val
.encoding
== ATTR_VAL_STRING
)
2067 return val
.u
.string
;
2070 case DW_AT_specification
:
2071 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2072 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2074 /* This refers to a specification defined in some other
2075 compilation unit. We can handle this case if we
2076 must, but it's harder. */
2079 if (val
.encoding
== ATTR_VAL_UINT
2080 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2084 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2085 error_callback
, data
);
2099 /* Add a single range to U that maps to function. Returns 1 on
2100 success, 0 on error. */
2103 add_function_range (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2104 struct function
*function
, uint64_t lowpc
, uint64_t highpc
,
2105 backtrace_error_callback error_callback
,
2106 void *data
, struct function_vector
*vec
)
2108 struct function_addrs
*p
;
2110 /* Add in the base address here, so that we can look up the PC
2112 lowpc
+= ddata
->base_address
;
2113 highpc
+= ddata
->base_address
;
2117 p
= (struct function_addrs
*) vec
->vec
.base
+ vec
->count
- 1;
2118 if ((lowpc
== p
->high
|| lowpc
== p
->high
+ 1)
2119 && function
== p
->function
)
2121 if (highpc
> p
->high
)
2127 p
= ((struct function_addrs
*)
2128 backtrace_vector_grow (state
, sizeof (struct function_addrs
),
2129 error_callback
, data
, &vec
->vec
));
2135 p
->function
= function
;
2140 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2144 add_function_ranges (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2145 struct unit
*u
, struct function
*function
,
2146 uint64_t ranges
, uint64_t base
,
2147 backtrace_error_callback error_callback
, void *data
,
2148 struct function_vector
*vec
)
2150 struct dwarf_buf ranges_buf
;
2152 if (ranges
>= ddata
->dwarf_ranges_size
)
2154 error_callback (data
, "function ranges offset out of range", 0);
2158 ranges_buf
.name
= ".debug_ranges";
2159 ranges_buf
.start
= ddata
->dwarf_ranges
;
2160 ranges_buf
.buf
= ddata
->dwarf_ranges
+ ranges
;
2161 ranges_buf
.left
= ddata
->dwarf_ranges_size
- ranges
;
2162 ranges_buf
.is_bigendian
= ddata
->is_bigendian
;
2163 ranges_buf
.error_callback
= error_callback
;
2164 ranges_buf
.data
= data
;
2165 ranges_buf
.reported_underflow
= 0;
2172 if (ranges_buf
.reported_underflow
)
2175 low
= read_address (&ranges_buf
, u
->addrsize
);
2176 high
= read_address (&ranges_buf
, u
->addrsize
);
2178 if (low
== 0 && high
== 0)
2181 if (is_highest_address (low
, u
->addrsize
))
2185 if (!add_function_range (state
, ddata
, function
, low
+ base
,
2186 high
+ base
, error_callback
, data
, vec
))
2191 if (ranges_buf
.reported_underflow
)
2197 /* Read one entry plus all its children. Add function addresses to
2198 VEC. Returns 1 on success, 0 on error. */
2201 read_function_entry (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2202 struct unit
*u
, uint64_t base
, struct dwarf_buf
*unit_buf
,
2203 const struct line_header
*lhdr
,
2204 backtrace_error_callback error_callback
, void *data
,
2205 struct function_vector
*vec
)
2207 while (unit_buf
->left
> 0)
2210 const struct abbrev
*abbrev
;
2212 struct function
*function
;
2218 int highpc_is_relative
;
2222 code
= read_uleb128 (unit_buf
);
2226 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2230 is_function
= (abbrev
->tag
== DW_TAG_subprogram
2231 || abbrev
->tag
== DW_TAG_entry_point
2232 || abbrev
->tag
== DW_TAG_inlined_subroutine
);
2237 function
= ((struct function
*)
2238 backtrace_alloc (state
, sizeof *function
,
2239 error_callback
, data
));
2240 if (function
== NULL
)
2242 memset (function
, 0, sizeof *function
);
2249 highpc_is_relative
= 0;
2252 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2254 struct attr_val val
;
2256 if (!read_attribute (abbrev
->attrs
[i
].form
, unit_buf
,
2257 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2258 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2262 /* The compile unit sets the base address for any address
2263 ranges in the function entries. */
2264 if (abbrev
->tag
== DW_TAG_compile_unit
2265 && abbrev
->attrs
[i
].name
== DW_AT_low_pc
2266 && val
.encoding
== ATTR_VAL_ADDRESS
)
2271 switch (abbrev
->attrs
[i
].name
)
2273 case DW_AT_call_file
:
2274 if (val
.encoding
== ATTR_VAL_UINT
)
2276 if (val
.u
.uint
== 0)
2277 function
->caller_filename
= "";
2280 if (val
.u
.uint
- 1 >= lhdr
->filenames_count
)
2282 dwarf_buf_error (unit_buf
,
2283 ("invalid file number in "
2284 "DW_AT_call_file attribute"));
2287 function
->caller_filename
=
2288 lhdr
->filenames
[val
.u
.uint
- 1];
2293 case DW_AT_call_line
:
2294 if (val
.encoding
== ATTR_VAL_UINT
)
2295 function
->caller_lineno
= val
.u
.uint
;
2298 case DW_AT_abstract_origin
:
2299 case DW_AT_specification
:
2300 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2301 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2303 /* This refers to an abstract origin defined in
2304 some other compilation unit. We can handle
2305 this case if we must, but it's harder. */
2308 if (val
.encoding
== ATTR_VAL_UINT
2309 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2313 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2314 error_callback
, data
);
2316 function
->name
= name
;
2321 if (val
.encoding
== ATTR_VAL_STRING
)
2323 /* Don't override a name we found in some other
2324 way, as it will normally be more
2325 useful--e.g., this name is normally not
2327 if (function
->name
== NULL
)
2328 function
->name
= val
.u
.string
;
2332 case DW_AT_linkage_name
:
2333 case DW_AT_MIPS_linkage_name
:
2334 if (val
.encoding
== ATTR_VAL_STRING
)
2335 function
->name
= val
.u
.string
;
2339 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2347 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2349 highpc
= val
.u
.uint
;
2352 else if (val
.encoding
== ATTR_VAL_UINT
)
2354 highpc
= val
.u
.uint
;
2356 highpc_is_relative
= 1;
2361 if (val
.encoding
== ATTR_VAL_UINT
2362 || val
.encoding
== ATTR_VAL_REF_SECTION
)
2364 ranges
= val
.u
.uint
;
2375 /* If we couldn't find a name for the function, we have no use
2377 if (is_function
&& function
->name
== NULL
)
2379 backtrace_free (state
, function
, sizeof *function
,
2380 error_callback
, data
);
2388 if (!add_function_ranges (state
, ddata
, u
, function
, ranges
,
2389 base
, error_callback
, data
, vec
))
2392 else if (have_lowpc
&& have_highpc
)
2394 if (highpc_is_relative
)
2396 if (!add_function_range (state
, ddata
, function
, lowpc
, highpc
,
2397 error_callback
, data
, vec
))
2402 backtrace_free (state
, function
, sizeof *function
,
2403 error_callback
, data
);
2408 if (abbrev
->has_children
)
2412 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2413 error_callback
, data
, vec
))
2418 struct function_vector fvec
;
2420 /* Gather any information for inlined functions in
2423 memset (&fvec
, 0, sizeof fvec
);
2425 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2426 error_callback
, data
, &fvec
))
2431 struct function_addrs
*faddrs
;
2433 if (!backtrace_vector_release (state
, &fvec
.vec
,
2434 error_callback
, data
))
2437 faddrs
= (struct function_addrs
*) fvec
.vec
.base
;
2438 qsort (faddrs
, fvec
.count
,
2439 sizeof (struct function_addrs
),
2440 function_addrs_compare
);
2442 function
->function_addrs
= faddrs
;
2443 function
->function_addrs_count
= fvec
.count
;
2452 /* Read function name information for a compilation unit. We look
2453 through the whole unit looking for function tags. */
2456 read_function_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2457 const struct line_header
*lhdr
,
2458 backtrace_error_callback error_callback
, void *data
,
2459 struct unit
*u
, struct function_vector
*fvec
,
2460 struct function_addrs
**ret_addrs
,
2461 size_t *ret_addrs_count
)
2463 struct dwarf_buf unit_buf
;
2464 struct function_addrs
*addrs
;
2467 unit_buf
.name
= ".debug_info";
2468 unit_buf
.start
= ddata
->dwarf_info
;
2469 unit_buf
.buf
= u
->unit_data
;
2470 unit_buf
.left
= u
->unit_data_len
;
2471 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2472 unit_buf
.error_callback
= error_callback
;
2473 unit_buf
.data
= data
;
2474 unit_buf
.reported_underflow
= 0;
2476 while (unit_buf
.left
> 0)
2478 if (!read_function_entry (state
, ddata
, u
, 0, &unit_buf
, lhdr
,
2479 error_callback
, data
, fvec
))
2483 if (fvec
->count
== 0)
2486 addrs
= (struct function_addrs
*) fvec
->vec
.base
;
2487 addrs_count
= fvec
->count
;
2489 /* Finish this list of addresses, but leave the remaining space in
2490 the vector available for the next function unit. */
2491 backtrace_vector_finish (state
, &fvec
->vec
);
2494 qsort (addrs
, addrs_count
, sizeof (struct function_addrs
),
2495 function_addrs_compare
);
2498 *ret_addrs_count
= addrs_count
;
2501 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2502 information, and update FILENAME and LINENO for the caller.
2503 Returns whatever CALLBACK returns, or 0 to keep going. */
2506 report_inlined_functions (uintptr_t pc
, struct function
*function
,
2507 backtrace_full_callback callback
, void *data
,
2508 const char **filename
, int *lineno
)
2510 struct function_addrs
*function_addrs
;
2511 struct function
*inlined
;
2514 if (function
->function_addrs_count
== 0)
2517 function_addrs
= ((struct function_addrs
*)
2518 bsearch (&pc
, function
->function_addrs
,
2519 function
->function_addrs_count
,
2520 sizeof (struct function_addrs
),
2521 function_addrs_search
));
2522 if (function_addrs
== NULL
)
2525 while (((size_t) (function_addrs
- function
->function_addrs
) + 1
2526 < function
->function_addrs_count
)
2527 && pc
>= (function_addrs
+ 1)->low
2528 && pc
< (function_addrs
+ 1)->high
)
2531 /* We found an inlined call. */
2533 inlined
= function_addrs
->function
;
2535 /* Report any calls inlined into this one. */
2536 ret
= report_inlined_functions (pc
, inlined
, callback
, data
,
2541 /* Report this inlined call. */
2542 ret
= callback (data
, pc
, *filename
, *lineno
, inlined
->name
);
2546 /* Our caller will report the caller of the inlined function; tell
2547 it the appropriate filename and line number. */
2548 *filename
= inlined
->caller_filename
;
2549 *lineno
= inlined
->caller_lineno
;
2554 /* Look for a PC in the DWARF mapping for one module. On success,
2555 call CALLBACK and return whatever it returns. On error, call
2556 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
2560 dwarf_lookup_pc (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2561 uintptr_t pc
, backtrace_full_callback callback
,
2562 backtrace_error_callback error_callback
, void *data
,
2565 struct unit_addrs
*entry
;
2570 struct function_addrs
*function_addrs
;
2571 struct function
*function
;
2572 const char *filename
;
2578 /* Find an address range that includes PC. */
2579 entry
= bsearch (&pc
, ddata
->addrs
, ddata
->addrs_count
,
2580 sizeof (struct unit_addrs
), unit_addrs_search
);
2588 /* If there are multiple ranges that contain PC, use the last one,
2589 in order to produce predictable results. If we assume that all
2590 ranges are properly nested, then the last range will be the
2592 while ((size_t) (entry
- ddata
->addrs
) + 1 < ddata
->addrs_count
2593 && pc
>= (entry
+ 1)->low
2594 && pc
< (entry
+ 1)->high
)
2597 /* We need the lines, lines_count, function_addrs,
2598 function_addrs_count fields of u. If they are not set, we need
2599 to set them. When running in threaded mode, we need to allow for
2600 the possibility that some other thread is setting them
2606 /* Skip units with no useful line number information by walking
2607 backward. Useless line number information is marked by setting
2609 while (entry
> ddata
->addrs
2610 && pc
>= (entry
- 1)->low
2611 && pc
< (entry
- 1)->high
)
2613 if (state
->threaded
)
2615 /* Use __sync_bool_compare_and_swap to do a
2617 while (!__sync_bool_compare_and_swap (&u
->lines
, lines
, lines
))
2621 if (lines
!= (struct line
*) (uintptr_t) -1)
2630 /* Do a load-acquire of u->lines. */
2631 if (state
->threaded
)
2633 /* Use __sync_bool_compare_and_swap to do an atomic load. */
2634 while (!__sync_bool_compare_and_swap (&u
->lines
, lines
, lines
))
2641 size_t function_addrs_count
;
2642 struct line_header lhdr
;
2645 /* We have never read the line information for this unit. Read
2648 function_addrs
= NULL
;
2649 function_addrs_count
= 0;
2650 if (read_line_info (state
, ddata
, error_callback
, data
, entry
->u
, &lhdr
,
2653 read_function_info (state
, ddata
, &lhdr
, error_callback
, data
,
2654 entry
->u
, &ddata
->fvec
, &function_addrs
,
2655 &function_addrs_count
);
2656 free_line_header (state
, &lhdr
, error_callback
, data
);
2660 /* Atomically store the information we just read into the unit.
2661 If another thread is simultaneously writing, it presumably
2662 read the same information, and we don't care which one we
2663 wind up with; we just leak the other one. We do have to
2664 write the lines field last, so that the acquire-loads above
2665 ensure that the other fields are set. */
2667 if (!state
->threaded
)
2669 u
->lines_count
= count
;
2670 u
->function_addrs
= function_addrs
;
2671 u
->function_addrs_count
= function_addrs_count
;
2676 __sync_bool_compare_and_swap (&u
->lines_count
, 0, count
);
2677 __sync_bool_compare_and_swap (&u
->function_addrs
, NULL
,
2679 __sync_bool_compare_and_swap (&u
->function_addrs_count
, 0,
2680 function_addrs_count
);
2681 __sync_bool_compare_and_swap (&u
->lines
, NULL
, lines
);
2685 /* Now all fields of U have been initialized. */
2687 if (lines
== (struct line
*) (uintptr_t) -1)
2689 /* If reading the line number information failed in some way,
2690 try again to see if there is a better compilation unit for
2693 return dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2695 return callback (data
, pc
, NULL
, 0, NULL
);
2698 /* Search for PC within this unit. */
2700 ln
= (struct line
*) bsearch (&pc
, lines
, entry
->u
->lines_count
,
2701 sizeof (struct line
), line_search
);
2704 error_callback (data
, "inconsistent DWARF line number info", 0);
2708 /* Search for function name within this unit. */
2710 if (entry
->u
->function_addrs_count
== 0)
2711 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2713 function_addrs
= ((struct function_addrs
*)
2714 bsearch (&pc
, entry
->u
->function_addrs
,
2715 entry
->u
->function_addrs_count
,
2716 sizeof (struct function_addrs
),
2717 function_addrs_search
));
2718 if (function_addrs
== NULL
)
2719 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2721 /* If there are multiple function ranges that contain PC, use the
2722 last one, in order to produce predictable results. */
2724 while (((size_t) (function_addrs
- entry
->u
->function_addrs
+ 1)
2725 < entry
->u
->function_addrs_count
)
2726 && pc
>= (function_addrs
+ 1)->low
2727 && pc
< (function_addrs
+ 1)->high
)
2730 function
= function_addrs
->function
;
2732 filename
= ln
->filename
;
2733 lineno
= ln
->lineno
;
2735 ret
= report_inlined_functions (pc
, function
, callback
, data
,
2736 &filename
, &lineno
);
2740 return callback (data
, pc
, filename
, lineno
, function
->name
);
2744 /* Return the file/line information for a PC using the DWARF mapping
2745 we built earlier. */
2748 dwarf_fileline (struct backtrace_state
*state
, uintptr_t pc
,
2749 backtrace_full_callback callback
,
2750 backtrace_error_callback error_callback
, void *data
)
2752 struct dwarf_data
*ddata
;
2756 if (!state
->threaded
)
2758 for (ddata
= (struct dwarf_data
*) state
->fileline_data
;
2760 ddata
= ddata
->next
)
2762 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2764 if (ret
!= 0 || found
)
2770 struct dwarf_data
**pp
;
2772 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
2777 while (!__sync_bool_compare_and_swap (pp
, ddata
, ddata
))
2783 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2785 if (ret
!= 0 || found
)
2792 /* FIXME: See if any libraries have been dlopen'ed. */
2794 return callback (data
, pc
, NULL
, 0, NULL
);
2797 /* Initialize our data structures from the DWARF debug info for a
2798 file. Return NULL on failure. */
2800 static struct dwarf_data
*
2801 build_dwarf_data (struct backtrace_state
*state
,
2802 uintptr_t base_address
,
2803 const unsigned char *dwarf_info
,
2804 size_t dwarf_info_size
,
2805 const unsigned char *dwarf_line
,
2806 size_t dwarf_line_size
,
2807 const unsigned char *dwarf_abbrev
,
2808 size_t dwarf_abbrev_size
,
2809 const unsigned char *dwarf_ranges
,
2810 size_t dwarf_ranges_size
,
2811 const unsigned char *dwarf_str
,
2812 size_t dwarf_str_size
,
2814 backtrace_error_callback error_callback
,
2817 struct unit_addrs_vector addrs_vec
;
2818 struct unit_addrs
*addrs
;
2820 struct dwarf_data
*fdata
;
2822 if (!build_address_map (state
, base_address
, dwarf_info
, dwarf_info_size
,
2823 dwarf_abbrev
, dwarf_abbrev_size
, dwarf_ranges
,
2824 dwarf_ranges_size
, dwarf_str
, dwarf_str_size
,
2825 is_bigendian
, error_callback
, data
, &addrs_vec
))
2828 if (!backtrace_vector_release (state
, &addrs_vec
.vec
, error_callback
, data
))
2830 addrs
= (struct unit_addrs
*) addrs_vec
.vec
.base
;
2831 addrs_count
= addrs_vec
.count
;
2832 qsort (addrs
, addrs_count
, sizeof (struct unit_addrs
), unit_addrs_compare
);
2834 fdata
= ((struct dwarf_data
*)
2835 backtrace_alloc (state
, sizeof (struct dwarf_data
),
2836 error_callback
, data
));
2841 fdata
->base_address
= base_address
;
2842 fdata
->addrs
= addrs
;
2843 fdata
->addrs_count
= addrs_count
;
2844 fdata
->dwarf_info
= dwarf_info
;
2845 fdata
->dwarf_info_size
= dwarf_info_size
;
2846 fdata
->dwarf_line
= dwarf_line
;
2847 fdata
->dwarf_line_size
= dwarf_line_size
;
2848 fdata
->dwarf_ranges
= dwarf_ranges
;
2849 fdata
->dwarf_ranges_size
= dwarf_ranges_size
;
2850 fdata
->dwarf_str
= dwarf_str
;
2851 fdata
->dwarf_str_size
= dwarf_str_size
;
2852 fdata
->is_bigendian
= is_bigendian
;
2853 memset (&fdata
->fvec
, 0, sizeof fdata
->fvec
);
2858 /* Build our data structures from the DWARF sections for a module.
2859 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
2863 backtrace_dwarf_add (struct backtrace_state
*state
,
2864 uintptr_t base_address
,
2865 const unsigned char *dwarf_info
,
2866 size_t dwarf_info_size
,
2867 const unsigned char *dwarf_line
,
2868 size_t dwarf_line_size
,
2869 const unsigned char *dwarf_abbrev
,
2870 size_t dwarf_abbrev_size
,
2871 const unsigned char *dwarf_ranges
,
2872 size_t dwarf_ranges_size
,
2873 const unsigned char *dwarf_str
,
2874 size_t dwarf_str_size
,
2876 backtrace_error_callback error_callback
,
2877 void *data
, fileline
*fileline_fn
)
2879 struct dwarf_data
*fdata
;
2881 fdata
= build_dwarf_data (state
, base_address
, dwarf_info
, dwarf_info_size
,
2882 dwarf_line
, dwarf_line_size
, dwarf_abbrev
,
2883 dwarf_abbrev_size
, dwarf_ranges
, dwarf_ranges_size
,
2884 dwarf_str
, dwarf_str_size
, is_bigendian
,
2885 error_callback
, data
);
2889 if (!state
->threaded
)
2891 struct dwarf_data
**pp
;
2893 for (pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
2903 struct dwarf_data
**pp
;
2905 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
2909 struct dwarf_data
*p
;
2913 while (!__sync_bool_compare_and_swap (pp
, p
, p
))
2922 if (__sync_bool_compare_and_swap (pp
, NULL
, fdata
))
2927 *fileline_fn
= dwarf_fileline
;