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
)
660 val
->encoding
= ATTR_VAL_ADDRESS
;
661 val
->u
.uint
= read_address (buf
, addrsize
);
664 val
->encoding
= ATTR_VAL_BLOCK
;
665 return advance (buf
, read_uint16 (buf
));
667 val
->encoding
= ATTR_VAL_BLOCK
;
668 return advance (buf
, read_uint32 (buf
));
670 val
->encoding
= ATTR_VAL_UINT
;
671 val
->u
.uint
= read_uint16 (buf
);
674 val
->encoding
= ATTR_VAL_UINT
;
675 val
->u
.uint
= read_uint32 (buf
);
678 val
->encoding
= ATTR_VAL_UINT
;
679 val
->u
.uint
= read_uint64 (buf
);
682 val
->encoding
= ATTR_VAL_STRING
;
683 val
->u
.string
= (const char *) buf
->buf
;
684 return advance (buf
, strnlen ((const char *) buf
->buf
, buf
->left
) + 1);
686 val
->encoding
= ATTR_VAL_BLOCK
;
687 return advance (buf
, read_uleb128 (buf
));
689 val
->encoding
= ATTR_VAL_BLOCK
;
690 return advance (buf
, read_byte (buf
));
692 val
->encoding
= ATTR_VAL_UINT
;
693 val
->u
.uint
= read_byte (buf
);
696 val
->encoding
= ATTR_VAL_UINT
;
697 val
->u
.uint
= read_byte (buf
);
700 val
->encoding
= ATTR_VAL_SINT
;
701 val
->u
.sint
= read_sleb128 (buf
);
707 offset
= read_offset (buf
, is_dwarf64
);
708 if (offset
>= dwarf_str_size
)
710 dwarf_buf_error (buf
, "DW_FORM_strp out of range");
713 val
->encoding
= ATTR_VAL_STRING
;
714 val
->u
.string
= (const char *) dwarf_str
+ offset
;
718 val
->encoding
= ATTR_VAL_UINT
;
719 val
->u
.uint
= read_uleb128 (buf
);
721 case DW_FORM_ref_addr
:
722 val
->encoding
= ATTR_VAL_REF_INFO
;
724 val
->u
.uint
= read_address (buf
, addrsize
);
726 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
729 val
->encoding
= ATTR_VAL_REF_UNIT
;
730 val
->u
.uint
= read_byte (buf
);
733 val
->encoding
= ATTR_VAL_REF_UNIT
;
734 val
->u
.uint
= read_uint16 (buf
);
737 val
->encoding
= ATTR_VAL_REF_UNIT
;
738 val
->u
.uint
= read_uint32 (buf
);
741 val
->encoding
= ATTR_VAL_REF_UNIT
;
742 val
->u
.uint
= read_uint64 (buf
);
744 case DW_FORM_ref_udata
:
745 val
->encoding
= ATTR_VAL_REF_UNIT
;
746 val
->u
.uint
= read_uleb128 (buf
);
748 case DW_FORM_indirect
:
752 form
= read_uleb128 (buf
);
753 return read_attribute ((enum dwarf_form
) form
, buf
, is_dwarf64
,
754 version
, addrsize
, dwarf_str
, dwarf_str_size
,
757 case DW_FORM_sec_offset
:
758 val
->encoding
= ATTR_VAL_REF_SECTION
;
759 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
761 case DW_FORM_exprloc
:
762 val
->encoding
= ATTR_VAL_EXPR
;
763 return advance (buf
, read_uleb128 (buf
));
764 case DW_FORM_flag_present
:
765 val
->encoding
= ATTR_VAL_UINT
;
768 case DW_FORM_ref_sig8
:
769 val
->encoding
= ATTR_VAL_REF_TYPE
;
770 val
->u
.uint
= read_uint64 (buf
);
772 case DW_FORM_GNU_addr_index
:
773 val
->encoding
= ATTR_VAL_REF_SECTION
;
774 val
->u
.uint
= read_uleb128 (buf
);
776 case DW_FORM_GNU_str_index
:
777 val
->encoding
= ATTR_VAL_REF_SECTION
;
778 val
->u
.uint
= read_uleb128 (buf
);
780 case DW_FORM_GNU_ref_alt
:
781 val
->encoding
= ATTR_VAL_REF_SECTION
;
782 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
784 case DW_FORM_GNU_strp_alt
:
785 val
->encoding
= ATTR_VAL_REF_SECTION
;
786 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
789 dwarf_buf_error (buf
, "unrecognized DWARF form");
794 /* Compare function_addrs for qsort. When ranges are nested, make the
795 smallest one sort last. */
798 function_addrs_compare (const void *v1
, const void *v2
)
800 const struct function_addrs
*a1
= (const struct function_addrs
*) v1
;
801 const struct function_addrs
*a2
= (const struct function_addrs
*) v2
;
803 if (a1
->low
< a2
->low
)
805 if (a1
->low
> a2
->low
)
807 if (a1
->high
< a2
->high
)
809 if (a1
->high
> a2
->high
)
811 return strcmp (a1
->function
->name
, a2
->function
->name
);
814 /* Compare a PC against a function_addrs for bsearch. Note that if
815 there are multiple ranges containing PC, which one will be returned
816 is unpredictable. We compensate for that in dwarf_fileline. */
819 function_addrs_search (const void *vkey
, const void *ventry
)
821 const uintptr_t *key
= (const uintptr_t *) vkey
;
822 const struct function_addrs
*entry
= (const struct function_addrs
*) ventry
;
828 else if (pc
>= entry
->high
)
834 /* Add a new compilation unit address range to a vector. Returns 1 on
835 success, 0 on failure. */
838 add_unit_addr (struct backtrace_state
*state
, uintptr_t base_address
,
839 struct unit_addrs addrs
,
840 backtrace_error_callback error_callback
, void *data
,
841 struct unit_addrs_vector
*vec
)
843 struct unit_addrs
*p
;
845 /* Add in the base address of the module here, so that we can look
846 up the PC directly. */
847 addrs
.low
+= base_address
;
848 addrs
.high
+= base_address
;
850 /* Try to merge with the last entry. */
853 p
= (struct unit_addrs
*) vec
->vec
.base
+ (vec
->count
- 1);
854 if ((addrs
.low
== p
->high
|| addrs
.low
== p
->high
+ 1)
857 if (addrs
.high
> p
->high
)
858 p
->high
= addrs
.high
;
863 p
= ((struct unit_addrs
*)
864 backtrace_vector_grow (state
, sizeof (struct unit_addrs
),
865 error_callback
, data
, &vec
->vec
));
874 /* Free a unit address vector. */
877 free_unit_addrs_vector (struct backtrace_state
*state
,
878 struct unit_addrs_vector
*vec
,
879 backtrace_error_callback error_callback
, void *data
)
881 struct unit_addrs
*addrs
;
884 addrs
= (struct unit_addrs
*) vec
->vec
.base
;
885 for (i
= 0; i
< vec
->count
; ++i
)
886 free_abbrevs (state
, &addrs
[i
].u
->abbrevs
, error_callback
, data
);
889 /* Compare unit_addrs for qsort. When ranges are nested, make the
890 smallest one sort last. */
893 unit_addrs_compare (const void *v1
, const void *v2
)
895 const struct unit_addrs
*a1
= (const struct unit_addrs
*) v1
;
896 const struct unit_addrs
*a2
= (const struct unit_addrs
*) v2
;
898 if (a1
->low
< a2
->low
)
900 if (a1
->low
> a2
->low
)
902 if (a1
->high
< a2
->high
)
904 if (a1
->high
> a2
->high
)
906 if (a1
->u
->lineoff
< a2
->u
->lineoff
)
908 if (a1
->u
->lineoff
> a2
->u
->lineoff
)
913 /* Compare a PC against a unit_addrs for bsearch. Note that if there
914 are multiple ranges containing PC, which one will be returned is
915 unpredictable. We compensate for that in dwarf_fileline. */
918 unit_addrs_search (const void *vkey
, const void *ventry
)
920 const uintptr_t *key
= (const uintptr_t *) vkey
;
921 const struct unit_addrs
*entry
= (const struct unit_addrs
*) ventry
;
927 else if (pc
>= entry
->high
)
933 /* Sort the line vector by PC. We want a stable sort here. We know
934 that the pointers are into the same array, so it is safe to compare
938 line_compare (const void *v1
, const void *v2
)
940 const struct line
*ln1
= (const struct line
*) v1
;
941 const struct line
*ln2
= (const struct line
*) v2
;
943 if (ln1
->pc
< ln2
->pc
)
945 else if (ln1
->pc
> ln2
->pc
)
955 /* Find a PC in a line vector. We always allocate an extra entry at
956 the end of the lines vector, so that this routine can safely look
957 at the next entry. Note that when there are multiple mappings for
958 the same PC value, this will return the last one. */
961 line_search (const void *vkey
, const void *ventry
)
963 const uintptr_t *key
= (const uintptr_t *) vkey
;
964 const struct line
*entry
= (const struct line
*) ventry
;
970 else if (pc
>= (entry
+ 1)->pc
)
976 /* Sort the abbrevs by the abbrev code. This function is passed to
977 both qsort and bsearch. */
980 abbrev_compare (const void *v1
, const void *v2
)
982 const struct abbrev
*a1
= (const struct abbrev
*) v1
;
983 const struct abbrev
*a2
= (const struct abbrev
*) v2
;
985 if (a1
->code
< a2
->code
)
987 else if (a1
->code
> a2
->code
)
991 /* This really shouldn't happen. It means there are two
992 different abbrevs with the same code, and that means we don't
993 know which one lookup_abbrev should return. */
998 /* Read the abbreviation table for a compilation unit. Returns 1 on
999 success, 0 on failure. */
1002 read_abbrevs (struct backtrace_state
*state
, uint64_t abbrev_offset
,
1003 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1004 int is_bigendian
, backtrace_error_callback error_callback
,
1005 void *data
, struct abbrevs
*abbrevs
)
1007 struct dwarf_buf abbrev_buf
;
1008 struct dwarf_buf count_buf
;
1011 abbrevs
->num_abbrevs
= 0;
1012 abbrevs
->abbrevs
= NULL
;
1014 if (abbrev_offset
>= dwarf_abbrev_size
)
1016 error_callback (data
, "abbrev offset out of range", 0);
1020 abbrev_buf
.name
= ".debug_abbrev";
1021 abbrev_buf
.start
= dwarf_abbrev
;
1022 abbrev_buf
.buf
= dwarf_abbrev
+ abbrev_offset
;
1023 abbrev_buf
.left
= dwarf_abbrev_size
- abbrev_offset
;
1024 abbrev_buf
.is_bigendian
= is_bigendian
;
1025 abbrev_buf
.error_callback
= error_callback
;
1026 abbrev_buf
.data
= data
;
1027 abbrev_buf
.reported_underflow
= 0;
1029 /* Count the number of abbrevs in this list. */
1031 count_buf
= abbrev_buf
;
1033 while (read_uleb128 (&count_buf
) != 0)
1035 if (count_buf
.reported_underflow
)
1039 read_uleb128 (&count_buf
);
1040 // Skip has_children.
1041 read_byte (&count_buf
);
1043 while (read_uleb128 (&count_buf
) != 0)
1044 read_uleb128 (&count_buf
);
1045 // Skip form of last attribute.
1046 read_uleb128 (&count_buf
);
1049 if (count_buf
.reported_underflow
)
1052 if (num_abbrevs
== 0)
1055 abbrevs
->num_abbrevs
= num_abbrevs
;
1056 abbrevs
->abbrevs
= ((struct abbrev
*)
1057 backtrace_alloc (state
,
1058 num_abbrevs
* sizeof (struct abbrev
),
1059 error_callback
, data
));
1060 if (abbrevs
->abbrevs
== NULL
)
1062 memset (abbrevs
->abbrevs
, 0, num_abbrevs
* sizeof (struct abbrev
));
1072 if (abbrev_buf
.reported_underflow
)
1075 code
= read_uleb128 (&abbrev_buf
);
1080 a
.tag
= (enum dwarf_tag
) read_uleb128 (&abbrev_buf
);
1081 a
.has_children
= read_byte (&abbrev_buf
);
1083 count_buf
= abbrev_buf
;
1085 while (read_uleb128 (&count_buf
) != 0)
1088 read_uleb128 (&count_buf
);
1094 read_uleb128 (&abbrev_buf
);
1095 read_uleb128 (&abbrev_buf
);
1099 attrs
= ((struct attr
*)
1100 backtrace_alloc (state
, num_attrs
* sizeof *attrs
,
1101 error_callback
, data
));
1110 name
= read_uleb128 (&abbrev_buf
);
1111 form
= read_uleb128 (&abbrev_buf
);
1114 attrs
[num_attrs
].name
= (enum dwarf_attribute
) name
;
1115 attrs
[num_attrs
].form
= (enum dwarf_form
) form
;
1120 a
.num_attrs
= num_attrs
;
1123 abbrevs
->abbrevs
[num_abbrevs
] = a
;
1127 qsort (abbrevs
->abbrevs
, abbrevs
->num_abbrevs
, sizeof (struct abbrev
),
1133 free_abbrevs (state
, abbrevs
, error_callback
, data
);
1137 /* Return the abbrev information for an abbrev code. */
1139 static const struct abbrev
*
1140 lookup_abbrev (struct abbrevs
*abbrevs
, uint64_t code
,
1141 backtrace_error_callback error_callback
, void *data
)
1146 /* With GCC, where abbrevs are simply numbered in order, we should
1147 be able to just look up the entry. */
1148 if (code
- 1 < abbrevs
->num_abbrevs
1149 && abbrevs
->abbrevs
[code
- 1].code
== code
)
1150 return &abbrevs
->abbrevs
[code
- 1];
1152 /* Otherwise we have to search. */
1153 memset (&key
, 0, sizeof key
);
1155 p
= bsearch (&key
, abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1156 sizeof (struct abbrev
), abbrev_compare
);
1159 error_callback (data
, "invalid abbreviation code", 0);
1162 return (const struct abbrev
*) p
;
1165 /* Add non-contiguous address ranges for a compilation unit. Returns
1166 1 on success, 0 on failure. */
1169 add_unit_ranges (struct backtrace_state
*state
, uintptr_t base_address
,
1170 struct unit
*u
, uint64_t ranges
, uint64_t base
,
1171 int is_bigendian
, const unsigned char *dwarf_ranges
,
1172 size_t dwarf_ranges_size
,
1173 backtrace_error_callback error_callback
, void *data
,
1174 struct unit_addrs_vector
*addrs
)
1176 struct dwarf_buf ranges_buf
;
1178 if (ranges
>= dwarf_ranges_size
)
1180 error_callback (data
, "ranges offset out of range", 0);
1184 ranges_buf
.name
= ".debug_ranges";
1185 ranges_buf
.start
= dwarf_ranges
;
1186 ranges_buf
.buf
= dwarf_ranges
+ ranges
;
1187 ranges_buf
.left
= dwarf_ranges_size
- ranges
;
1188 ranges_buf
.is_bigendian
= is_bigendian
;
1189 ranges_buf
.error_callback
= error_callback
;
1190 ranges_buf
.data
= data
;
1191 ranges_buf
.reported_underflow
= 0;
1198 if (ranges_buf
.reported_underflow
)
1201 low
= read_address (&ranges_buf
, u
->addrsize
);
1202 high
= read_address (&ranges_buf
, u
->addrsize
);
1204 if (low
== 0 && high
== 0)
1207 if (is_highest_address (low
, u
->addrsize
))
1211 struct unit_addrs a
;
1214 a
.high
= high
+ base
;
1216 if (!add_unit_addr (state
, base_address
, a
, error_callback
, data
,
1222 if (ranges_buf
.reported_underflow
)
1228 /* Build a mapping from address ranges to the compilation units where
1229 the line number information for that range can be found. Returns 1
1230 on success, 0 on failure. */
1233 build_address_map (struct backtrace_state
*state
, uintptr_t base_address
,
1234 const unsigned char *dwarf_info
, size_t dwarf_info_size
,
1235 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1236 const unsigned char *dwarf_ranges
, size_t dwarf_ranges_size
,
1237 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
1238 int is_bigendian
, backtrace_error_callback error_callback
,
1239 void *data
, struct unit_addrs_vector
*addrs
)
1241 struct dwarf_buf info
;
1242 struct abbrevs abbrevs
;
1244 memset (&addrs
->vec
, 0, sizeof addrs
->vec
);
1247 /* Read through the .debug_info section. FIXME: Should we use the
1248 .debug_aranges section? gdb and addr2line don't use it, but I'm
1251 info
.name
= ".debug_info";
1252 info
.start
= dwarf_info
;
1253 info
.buf
= dwarf_info
;
1254 info
.left
= dwarf_info_size
;
1255 info
.is_bigendian
= is_bigendian
;
1256 info
.error_callback
= error_callback
;
1258 info
.reported_underflow
= 0;
1260 memset (&abbrevs
, 0, sizeof abbrevs
);
1261 while (info
.left
> 0)
1263 const unsigned char *unit_data_start
;
1266 struct dwarf_buf unit_buf
;
1268 uint64_t abbrev_offset
;
1269 const struct abbrev
*abbrev
;
1271 const unsigned char *unit_data
;
1272 size_t unit_data_len
;
1273 size_t unit_data_offset
;
1280 int highpc_is_relative
;
1285 const char *comp_dir
;
1287 if (info
.reported_underflow
)
1290 unit_data_start
= info
.buf
;
1293 len
= read_uint32 (&info
);
1294 if (len
== 0xffffffff)
1296 len
= read_uint64 (&info
);
1301 unit_buf
.left
= len
;
1303 if (!advance (&info
, len
))
1306 version
= read_uint16 (&unit_buf
);
1307 if (version
< 2 || version
> 4)
1309 dwarf_buf_error (&unit_buf
, "unrecognized DWARF version");
1313 abbrev_offset
= read_offset (&unit_buf
, is_dwarf64
);
1314 if (!read_abbrevs (state
, abbrev_offset
, dwarf_abbrev
, dwarf_abbrev_size
,
1315 is_bigendian
, error_callback
, data
, &abbrevs
))
1318 addrsize
= read_byte (&unit_buf
);
1320 unit_data
= unit_buf
.buf
;
1321 unit_data_len
= unit_buf
.left
;
1322 unit_data_offset
= unit_buf
.buf
- unit_data_start
;
1324 /* We only look at the first attribute in the compilation unit.
1325 In practice this will be a DW_TAG_compile_unit which will
1326 tell us the PC range and where to find the line number
1329 code
= read_uleb128 (&unit_buf
);
1330 abbrev
= lookup_abbrev (&abbrevs
, code
, error_callback
, data
);
1338 highpc_is_relative
= 0;
1344 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1346 struct attr_val val
;
1348 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
, is_dwarf64
,
1349 version
, addrsize
, dwarf_str
, dwarf_str_size
,
1353 switch (abbrev
->attrs
[i
].name
)
1356 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1363 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1365 highpc
= val
.u
.uint
;
1368 else if (val
.encoding
== ATTR_VAL_UINT
)
1370 highpc
= val
.u
.uint
;
1372 highpc_is_relative
= 1;
1376 if (val
.encoding
== ATTR_VAL_UINT
1377 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1379 ranges
= val
.u
.uint
;
1383 case DW_AT_stmt_list
:
1384 if (val
.encoding
== ATTR_VAL_UINT
1385 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1387 lineoff
= val
.u
.uint
;
1391 case DW_AT_comp_dir
:
1392 if (val
.encoding
== ATTR_VAL_STRING
)
1393 comp_dir
= val
.u
.string
;
1400 if (unit_buf
.reported_underflow
)
1403 if (((have_lowpc
&& have_highpc
) || have_ranges
) && have_lineoff
)
1406 struct unit_addrs a
;
1408 u
= ((struct unit
*)
1409 backtrace_alloc (state
, sizeof *u
, error_callback
, data
));
1412 u
->unit_data
= unit_data
;
1413 u
->unit_data_len
= unit_data_len
;
1414 u
->unit_data_offset
= unit_data_offset
;
1415 u
->version
= version
;
1416 u
->is_dwarf64
= is_dwarf64
;
1417 u
->addrsize
= addrsize
;
1418 u
->comp_dir
= comp_dir
;
1419 u
->lineoff
= lineoff
;
1420 u
->abbrevs
= abbrevs
;
1421 memset (&abbrevs
, 0, sizeof abbrevs
);
1423 /* The actual line number mappings will be read as
1427 u
->function_addrs
= NULL
;
1428 u
->function_addrs_count
= 0;
1432 if (!add_unit_ranges (state
, base_address
, u
, ranges
, lowpc
,
1433 is_bigendian
, dwarf_ranges
,
1434 dwarf_ranges_size
, error_callback
, data
,
1437 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1438 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1444 if (highpc_is_relative
)
1450 if (!add_unit_addr (state
, base_address
, a
, error_callback
, data
,
1453 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1454 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1461 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1462 memset (&abbrevs
, 0, sizeof abbrevs
);
1465 if (info
.reported_underflow
)
1471 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1472 free_unit_addrs_vector (state
, addrs
, error_callback
, data
);
1476 /* Add a new mapping to the vector of line mappings that we are
1477 building. Returns 1 on success, 0 on failure. */
1480 add_line (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1481 uintptr_t pc
, const char *filename
, int lineno
,
1482 backtrace_error_callback error_callback
, void *data
,
1483 struct line_vector
*vec
)
1487 /* If we are adding the same mapping, ignore it. This can happen
1488 when using discriminators. */
1491 ln
= (struct line
*) vec
->vec
.base
+ (vec
->count
- 1);
1492 if (pc
== ln
->pc
&& filename
== ln
->filename
&& lineno
== ln
->lineno
)
1496 ln
= ((struct line
*)
1497 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1502 /* Add in the base address here, so that we can look up the PC
1504 ln
->pc
= pc
+ ddata
->base_address
;
1506 ln
->filename
= filename
;
1507 ln
->lineno
= lineno
;
1514 /* Free the line header information. If FREE_FILENAMES is true we
1515 free the file names themselves, otherwise we leave them, as there
1516 may be line structures pointing to them. */
1519 free_line_header (struct backtrace_state
*state
, struct line_header
*hdr
,
1520 backtrace_error_callback error_callback
, void *data
)
1522 backtrace_free (state
, hdr
->dirs
, hdr
->dirs_count
* sizeof (const char *),
1523 error_callback
, data
);
1524 backtrace_free (state
, hdr
->filenames
,
1525 hdr
->filenames_count
* sizeof (char *),
1526 error_callback
, data
);
1529 /* Read the line header. Return 1 on success, 0 on failure. */
1532 read_line_header (struct backtrace_state
*state
, struct unit
*u
,
1533 int is_dwarf64
, struct dwarf_buf
*line_buf
,
1534 struct line_header
*hdr
)
1537 struct dwarf_buf hdr_buf
;
1538 const unsigned char *p
;
1539 const unsigned char *pend
;
1542 hdr
->version
= read_uint16 (line_buf
);
1543 if (hdr
->version
< 2 || hdr
->version
> 4)
1545 dwarf_buf_error (line_buf
, "unsupported line number version");
1549 hdrlen
= read_offset (line_buf
, is_dwarf64
);
1551 hdr_buf
= *line_buf
;
1552 hdr_buf
.left
= hdrlen
;
1554 if (!advance (line_buf
, hdrlen
))
1557 hdr
->min_insn_len
= read_byte (&hdr_buf
);
1558 if (hdr
->version
< 4)
1559 hdr
->max_ops_per_insn
= 1;
1561 hdr
->max_ops_per_insn
= read_byte (&hdr_buf
);
1563 /* We don't care about default_is_stmt. */
1564 read_byte (&hdr_buf
);
1566 hdr
->line_base
= read_sbyte (&hdr_buf
);
1567 hdr
->line_range
= read_byte (&hdr_buf
);
1569 hdr
->opcode_base
= read_byte (&hdr_buf
);
1570 hdr
->opcode_lengths
= hdr_buf
.buf
;
1571 if (!advance (&hdr_buf
, hdr
->opcode_base
- 1))
1574 /* Count the number of directory entries. */
1575 hdr
->dirs_count
= 0;
1577 pend
= p
+ hdr_buf
.left
;
1578 while (p
< pend
&& *p
!= '\0')
1580 p
+= strnlen((const char *) p
, pend
- p
) + 1;
1584 hdr
->dirs
= ((const char **)
1585 backtrace_alloc (state
,
1586 hdr
->dirs_count
* sizeof (const char *),
1587 line_buf
->error_callback
, line_buf
->data
));
1588 if (hdr
->dirs
== NULL
)
1592 while (*hdr_buf
.buf
!= '\0')
1594 if (hdr_buf
.reported_underflow
)
1597 hdr
->dirs
[i
] = (const char *) hdr_buf
.buf
;
1599 if (!advance (&hdr_buf
,
1600 strnlen ((const char *) hdr_buf
.buf
, hdr_buf
.left
) + 1))
1603 if (!advance (&hdr_buf
, 1))
1606 /* Count the number of file entries. */
1607 hdr
->filenames_count
= 0;
1609 pend
= p
+ hdr_buf
.left
;
1610 while (p
< pend
&& *p
!= '\0')
1612 p
+= strnlen ((const char *) p
, pend
- p
) + 1;
1613 p
+= leb128_len (p
);
1614 p
+= leb128_len (p
);
1615 p
+= leb128_len (p
);
1616 ++hdr
->filenames_count
;
1619 hdr
->filenames
= ((const char **)
1620 backtrace_alloc (state
,
1621 hdr
->filenames_count
* sizeof (char *),
1622 line_buf
->error_callback
,
1624 if (hdr
->filenames
== NULL
)
1627 while (*hdr_buf
.buf
!= '\0')
1629 const char *filename
;
1632 if (hdr_buf
.reported_underflow
)
1635 filename
= (const char *) hdr_buf
.buf
;
1636 if (!advance (&hdr_buf
,
1637 strnlen ((const char *) hdr_buf
.buf
, hdr_buf
.left
) + 1))
1639 dir_index
= read_uleb128 (&hdr_buf
);
1640 if (IS_ABSOLUTE_PATH (filename
))
1641 hdr
->filenames
[i
] = filename
;
1646 size_t filename_len
;
1651 else if (dir_index
- 1 < hdr
->dirs_count
)
1652 dir
= hdr
->dirs
[dir_index
- 1];
1655 dwarf_buf_error (line_buf
,
1656 ("invalid directory index in "
1657 "line number program header"));
1660 dir_len
= strlen (dir
);
1661 filename_len
= strlen (filename
);
1663 backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
1664 line_buf
->error_callback
, line_buf
->data
));
1667 memcpy (s
, dir
, dir_len
);
1668 /* FIXME: If we are on a DOS-based file system, and the
1669 directory or the file name use backslashes, then we
1670 should use a backslash here. */
1672 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
1673 hdr
->filenames
[i
] = s
;
1676 /* Ignore the modification time and size. */
1677 read_uleb128 (&hdr_buf
);
1678 read_uleb128 (&hdr_buf
);
1683 if (hdr_buf
.reported_underflow
)
1689 /* Read the line program, adding line mappings to VEC. Return 1 on
1690 success, 0 on failure. */
1693 read_line_program (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1694 struct unit
*u
, const struct line_header
*hdr
,
1695 struct dwarf_buf
*line_buf
, struct line_vector
*vec
)
1698 unsigned int op_index
;
1699 const char *reset_filename
;
1700 const char *filename
;
1705 if (hdr
->filenames_count
> 0)
1706 reset_filename
= hdr
->filenames
[0];
1708 reset_filename
= "";
1709 filename
= reset_filename
;
1711 while (line_buf
->left
> 0)
1715 op
= read_byte (line_buf
);
1716 if (op
>= hdr
->opcode_base
)
1718 unsigned int advance
;
1720 /* Special opcode. */
1721 op
-= hdr
->opcode_base
;
1722 advance
= op
/ hdr
->line_range
;
1723 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1724 / hdr
->max_ops_per_insn
);
1725 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1726 lineno
+= hdr
->line_base
+ (int) (op
% hdr
->line_range
);
1727 add_line (state
, ddata
, address
, filename
, lineno
,
1728 line_buf
->error_callback
, line_buf
->data
, vec
);
1730 else if (op
== DW_LNS_extended_op
)
1734 len
= read_uleb128 (line_buf
);
1735 op
= read_byte (line_buf
);
1738 case DW_LNE_end_sequence
:
1739 /* FIXME: Should we mark the high PC here? It seems
1740 that we already have that information from the
1741 compilation unit. */
1744 filename
= reset_filename
;
1747 case DW_LNE_set_address
:
1748 address
= read_address (line_buf
, u
->addrsize
);
1750 case DW_LNE_define_file
:
1753 unsigned int dir_index
;
1755 f
= (const char *) line_buf
->buf
;
1756 if (!advance (line_buf
, strnlen (f
, line_buf
->left
) + 1))
1758 dir_index
= read_uleb128 (line_buf
);
1759 /* Ignore that time and length. */
1760 read_uleb128 (line_buf
);
1761 read_uleb128 (line_buf
);
1762 if (IS_ABSOLUTE_PATH (f
))
1773 else if (dir_index
- 1 < hdr
->dirs_count
)
1774 dir
= hdr
->dirs
[dir_index
- 1];
1777 dwarf_buf_error (line_buf
,
1778 ("invalid directory index "
1779 "in line number program"));
1782 dir_len
= strlen (dir
);
1785 backtrace_alloc (state
, dir_len
+ f_len
+ 2,
1786 line_buf
->error_callback
,
1790 memcpy (p
, dir
, dir_len
);
1791 /* FIXME: If we are on a DOS-based file system,
1792 and the directory or the file name use
1793 backslashes, then we should use a backslash
1796 memcpy (p
+ dir_len
+ 1, f
, f_len
+ 1);
1801 case DW_LNE_set_discriminator
:
1802 /* We don't care about discriminators. */
1803 read_uleb128 (line_buf
);
1806 if (!advance (line_buf
, len
- 1))
1816 add_line (state
, ddata
, address
, filename
, lineno
,
1817 line_buf
->error_callback
, line_buf
->data
, vec
);
1819 case DW_LNS_advance_pc
:
1823 advance
= read_uleb128 (line_buf
);
1824 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1825 / hdr
->max_ops_per_insn
);
1826 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1829 case DW_LNS_advance_line
:
1830 lineno
+= (int) read_sleb128 (line_buf
);
1832 case DW_LNS_set_file
:
1836 fileno
= read_uleb128 (line_buf
);
1841 if (fileno
- 1 >= hdr
->filenames_count
)
1843 dwarf_buf_error (line_buf
,
1844 ("invalid file number in "
1845 "line number program"));
1848 filename
= hdr
->filenames
[fileno
- 1];
1852 case DW_LNS_set_column
:
1853 read_uleb128 (line_buf
);
1855 case DW_LNS_negate_stmt
:
1857 case DW_LNS_set_basic_block
:
1859 case DW_LNS_const_add_pc
:
1861 unsigned int advance
;
1863 op
= 255 - hdr
->opcode_base
;
1864 advance
= op
/ hdr
->line_range
;
1865 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1866 / hdr
->max_ops_per_insn
);
1867 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1870 case DW_LNS_fixed_advance_pc
:
1871 address
+= read_uint16 (line_buf
);
1874 case DW_LNS_set_prologue_end
:
1876 case DW_LNS_set_epilogue_begin
:
1878 case DW_LNS_set_isa
:
1879 read_uleb128 (line_buf
);
1885 for (i
= hdr
->opcode_lengths
[op
- 1]; i
> 0; --i
)
1886 read_uleb128 (line_buf
);
1896 /* Read the line number information for a compilation unit. Returns 1
1897 on success, 0 on failure. */
1900 read_line_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1901 backtrace_error_callback error_callback
, void *data
,
1902 struct unit
*u
, struct line_header
*hdr
, struct line
**lines
,
1903 size_t *lines_count
)
1905 struct line_vector vec
;
1906 struct dwarf_buf line_buf
;
1911 memset (&vec
.vec
, 0, sizeof vec
.vec
);
1914 memset (hdr
, 0, sizeof *hdr
);
1916 if (u
->lineoff
!= (off_t
) (size_t) u
->lineoff
1917 || (size_t) u
->lineoff
>= ddata
->dwarf_line_size
)
1919 error_callback (data
, "unit line offset out of range", 0);
1923 line_buf
.name
= ".debug_line";
1924 line_buf
.start
= ddata
->dwarf_line
;
1925 line_buf
.buf
= ddata
->dwarf_line
+ u
->lineoff
;
1926 line_buf
.left
= ddata
->dwarf_line_size
- u
->lineoff
;
1927 line_buf
.is_bigendian
= ddata
->is_bigendian
;
1928 line_buf
.error_callback
= error_callback
;
1929 line_buf
.data
= data
;
1930 line_buf
.reported_underflow
= 0;
1933 len
= read_uint32 (&line_buf
);
1934 if (len
== 0xffffffff)
1936 len
= read_uint64 (&line_buf
);
1939 line_buf
.left
= len
;
1941 if (!read_line_header (state
, u
, is_dwarf64
, &line_buf
, hdr
))
1944 if (!read_line_program (state
, ddata
, u
, hdr
, &line_buf
, &vec
))
1947 if (line_buf
.reported_underflow
)
1952 /* This is not a failure in the sense of a generating an error,
1953 but it is a failure in that sense that we have no useful
1958 /* Allocate one extra entry at the end. */
1959 ln
= ((struct line
*)
1960 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1964 ln
->pc
= (uintptr_t) -1;
1965 ln
->filename
= NULL
;
1968 if (!backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
))
1971 ln
= (struct line
*) vec
.vec
.base
;
1972 qsort (ln
, vec
.count
, sizeof (struct line
), line_compare
);
1975 *lines_count
= vec
.count
;
1980 vec
.vec
.alc
+= vec
.vec
.size
;
1982 backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
);
1983 free_line_header (state
, hdr
, error_callback
, data
);
1984 *lines
= (struct line
*) (uintptr_t) -1;
1989 /* Read the name of a function from a DIE referenced by a
1990 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
1991 the same compilation unit. */
1994 read_referenced_name (struct dwarf_data
*ddata
, struct unit
*u
,
1995 uint64_t offset
, backtrace_error_callback error_callback
,
1998 struct dwarf_buf unit_buf
;
2000 const struct abbrev
*abbrev
;
2004 /* OFFSET is from the start of the data for this compilation unit.
2005 U->unit_data is the data, but it starts U->unit_data_offset bytes
2006 from the beginning. */
2008 if (offset
< u
->unit_data_offset
2009 || offset
- u
->unit_data_offset
>= u
->unit_data_len
)
2011 error_callback (data
,
2012 "abstract origin or specification out of range",
2017 offset
-= u
->unit_data_offset
;
2019 unit_buf
.name
= ".debug_info";
2020 unit_buf
.start
= ddata
->dwarf_info
;
2021 unit_buf
.buf
= u
->unit_data
+ offset
;
2022 unit_buf
.left
= u
->unit_data_len
- offset
;
2023 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2024 unit_buf
.error_callback
= error_callback
;
2025 unit_buf
.data
= data
;
2026 unit_buf
.reported_underflow
= 0;
2028 code
= read_uleb128 (&unit_buf
);
2031 dwarf_buf_error (&unit_buf
, "invalid abstract origin or specification");
2035 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2040 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2042 struct attr_val val
;
2044 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
,
2045 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2046 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2050 switch (abbrev
->attrs
[i
].name
)
2053 /* We prefer the linkage name if get one. */
2054 if (val
.encoding
== ATTR_VAL_STRING
)
2058 case DW_AT_linkage_name
:
2059 case DW_AT_MIPS_linkage_name
:
2060 if (val
.encoding
== ATTR_VAL_STRING
)
2061 return val
.u
.string
;
2064 case DW_AT_specification
:
2065 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2066 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2068 /* This refers to a specification defined in some other
2069 compilation unit. We can handle this case if we
2070 must, but it's harder. */
2073 if (val
.encoding
== ATTR_VAL_UINT
2074 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2078 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2079 error_callback
, data
);
2093 /* Add a single range to U that maps to function. Returns 1 on
2094 success, 0 on error. */
2097 add_function_range (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2098 struct function
*function
, uint64_t lowpc
, uint64_t highpc
,
2099 backtrace_error_callback error_callback
,
2100 void *data
, struct function_vector
*vec
)
2102 struct function_addrs
*p
;
2104 /* Add in the base address here, so that we can look up the PC
2106 lowpc
+= ddata
->base_address
;
2107 highpc
+= ddata
->base_address
;
2111 p
= (struct function_addrs
*) vec
->vec
.base
+ vec
->count
- 1;
2112 if ((lowpc
== p
->high
|| lowpc
== p
->high
+ 1)
2113 && function
== p
->function
)
2115 if (highpc
> p
->high
)
2121 p
= ((struct function_addrs
*)
2122 backtrace_vector_grow (state
, sizeof (struct function_addrs
),
2123 error_callback
, data
, &vec
->vec
));
2129 p
->function
= function
;
2134 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2138 add_function_ranges (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2139 struct unit
*u
, struct function
*function
,
2140 uint64_t ranges
, uint64_t base
,
2141 backtrace_error_callback error_callback
, void *data
,
2142 struct function_vector
*vec
)
2144 struct dwarf_buf ranges_buf
;
2146 if (ranges
>= ddata
->dwarf_ranges_size
)
2148 error_callback (data
, "function ranges offset out of range", 0);
2152 ranges_buf
.name
= ".debug_ranges";
2153 ranges_buf
.start
= ddata
->dwarf_ranges
;
2154 ranges_buf
.buf
= ddata
->dwarf_ranges
+ ranges
;
2155 ranges_buf
.left
= ddata
->dwarf_ranges_size
- ranges
;
2156 ranges_buf
.is_bigendian
= ddata
->is_bigendian
;
2157 ranges_buf
.error_callback
= error_callback
;
2158 ranges_buf
.data
= data
;
2159 ranges_buf
.reported_underflow
= 0;
2166 if (ranges_buf
.reported_underflow
)
2169 low
= read_address (&ranges_buf
, u
->addrsize
);
2170 high
= read_address (&ranges_buf
, u
->addrsize
);
2172 if (low
== 0 && high
== 0)
2175 if (is_highest_address (low
, u
->addrsize
))
2179 if (!add_function_range (state
, ddata
, function
, low
+ base
,
2180 high
+ base
, error_callback
, data
, vec
))
2185 if (ranges_buf
.reported_underflow
)
2191 /* Read one entry plus all its children. Add function addresses to
2192 VEC. Returns 1 on success, 0 on error. */
2195 read_function_entry (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2196 struct unit
*u
, uint64_t base
, struct dwarf_buf
*unit_buf
,
2197 const struct line_header
*lhdr
,
2198 backtrace_error_callback error_callback
, void *data
,
2199 struct function_vector
*vec
)
2201 while (unit_buf
->left
> 0)
2204 const struct abbrev
*abbrev
;
2206 struct function
*function
;
2212 int highpc_is_relative
;
2216 code
= read_uleb128 (unit_buf
);
2220 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2224 is_function
= (abbrev
->tag
== DW_TAG_subprogram
2225 || abbrev
->tag
== DW_TAG_entry_point
2226 || abbrev
->tag
== DW_TAG_inlined_subroutine
);
2231 function
= ((struct function
*)
2232 backtrace_alloc (state
, sizeof *function
,
2233 error_callback
, data
));
2234 if (function
== NULL
)
2236 memset (function
, 0, sizeof *function
);
2243 highpc_is_relative
= 0;
2246 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2248 struct attr_val val
;
2250 if (!read_attribute (abbrev
->attrs
[i
].form
, unit_buf
,
2251 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2252 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2256 /* The compile unit sets the base address for any address
2257 ranges in the function entries. */
2258 if (abbrev
->tag
== DW_TAG_compile_unit
2259 && abbrev
->attrs
[i
].name
== DW_AT_low_pc
2260 && val
.encoding
== ATTR_VAL_ADDRESS
)
2265 switch (abbrev
->attrs
[i
].name
)
2267 case DW_AT_call_file
:
2268 if (val
.encoding
== ATTR_VAL_UINT
)
2270 if (val
.u
.uint
== 0)
2271 function
->caller_filename
= "";
2274 if (val
.u
.uint
- 1 >= lhdr
->filenames_count
)
2276 dwarf_buf_error (unit_buf
,
2277 ("invalid file number in "
2278 "DW_AT_call_file attribute"));
2281 function
->caller_filename
=
2282 lhdr
->filenames
[val
.u
.uint
- 1];
2287 case DW_AT_call_line
:
2288 if (val
.encoding
== ATTR_VAL_UINT
)
2289 function
->caller_lineno
= val
.u
.uint
;
2292 case DW_AT_abstract_origin
:
2293 case DW_AT_specification
:
2294 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2295 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2297 /* This refers to an abstract origin defined in
2298 some other compilation unit. We can handle
2299 this case if we must, but it's harder. */
2302 if (val
.encoding
== ATTR_VAL_UINT
2303 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2307 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2308 error_callback
, data
);
2310 function
->name
= name
;
2315 if (val
.encoding
== ATTR_VAL_STRING
)
2317 /* Don't override a name we found in some other
2318 way, as it will normally be more
2319 useful--e.g., this name is normally not
2321 if (function
->name
== NULL
)
2322 function
->name
= val
.u
.string
;
2326 case DW_AT_linkage_name
:
2327 case DW_AT_MIPS_linkage_name
:
2328 if (val
.encoding
== ATTR_VAL_STRING
)
2329 function
->name
= val
.u
.string
;
2333 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2341 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2343 highpc
= val
.u
.uint
;
2346 else if (val
.encoding
== ATTR_VAL_UINT
)
2348 highpc
= val
.u
.uint
;
2350 highpc_is_relative
= 1;
2355 if (val
.encoding
== ATTR_VAL_UINT
2356 || val
.encoding
== ATTR_VAL_REF_SECTION
)
2358 ranges
= val
.u
.uint
;
2369 /* If we couldn't find a name for the function, we have no use
2371 if (is_function
&& function
->name
== NULL
)
2373 backtrace_free (state
, function
, sizeof *function
,
2374 error_callback
, data
);
2382 if (!add_function_ranges (state
, ddata
, u
, function
, ranges
,
2383 base
, error_callback
, data
, vec
))
2386 else if (have_lowpc
&& have_highpc
)
2388 if (highpc_is_relative
)
2390 if (!add_function_range (state
, ddata
, function
, lowpc
, highpc
,
2391 error_callback
, data
, vec
))
2396 backtrace_free (state
, function
, sizeof *function
,
2397 error_callback
, data
);
2402 if (abbrev
->has_children
)
2406 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2407 error_callback
, data
, vec
))
2412 struct function_vector fvec
;
2414 /* Gather any information for inlined functions in
2417 memset (&fvec
, 0, sizeof fvec
);
2419 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2420 error_callback
, data
, &fvec
))
2425 struct function_addrs
*faddrs
;
2427 if (!backtrace_vector_release (state
, &fvec
.vec
,
2428 error_callback
, data
))
2431 faddrs
= (struct function_addrs
*) fvec
.vec
.base
;
2432 qsort (faddrs
, fvec
.count
,
2433 sizeof (struct function_addrs
),
2434 function_addrs_compare
);
2436 function
->function_addrs
= faddrs
;
2437 function
->function_addrs_count
= fvec
.count
;
2446 /* Read function name information for a compilation unit. We look
2447 through the whole unit looking for function tags. */
2450 read_function_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2451 const struct line_header
*lhdr
,
2452 backtrace_error_callback error_callback
, void *data
,
2453 struct unit
*u
, struct function_vector
*fvec
,
2454 struct function_addrs
**ret_addrs
,
2455 size_t *ret_addrs_count
)
2457 struct dwarf_buf unit_buf
;
2458 struct function_addrs
*addrs
;
2461 unit_buf
.name
= ".debug_info";
2462 unit_buf
.start
= ddata
->dwarf_info
;
2463 unit_buf
.buf
= u
->unit_data
;
2464 unit_buf
.left
= u
->unit_data_len
;
2465 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2466 unit_buf
.error_callback
= error_callback
;
2467 unit_buf
.data
= data
;
2468 unit_buf
.reported_underflow
= 0;
2470 while (unit_buf
.left
> 0)
2472 if (!read_function_entry (state
, ddata
, u
, 0, &unit_buf
, lhdr
,
2473 error_callback
, data
, fvec
))
2477 if (fvec
->count
== 0)
2480 addrs
= (struct function_addrs
*) fvec
->vec
.base
;
2481 addrs_count
= fvec
->count
;
2483 /* Finish this list of addresses, but leave the remaining space in
2484 the vector available for the next function unit. */
2485 backtrace_vector_finish (state
, &fvec
->vec
);
2488 qsort (addrs
, addrs_count
, sizeof (struct function_addrs
),
2489 function_addrs_compare
);
2492 *ret_addrs_count
= addrs_count
;
2495 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2496 information, and update FILENAME and LINENO for the caller.
2497 Returns whatever CALLBACK returns, or 0 to keep going. */
2500 report_inlined_functions (uintptr_t pc
, struct function
*function
,
2501 backtrace_full_callback callback
, void *data
,
2502 const char **filename
, int *lineno
)
2504 struct function_addrs
*function_addrs
;
2505 struct function
*inlined
;
2508 if (function
->function_addrs_count
== 0)
2511 function_addrs
= ((struct function_addrs
*)
2512 bsearch (&pc
, function
->function_addrs
,
2513 function
->function_addrs_count
,
2514 sizeof (struct function_addrs
),
2515 function_addrs_search
));
2516 if (function_addrs
== NULL
)
2519 while (((size_t) (function_addrs
- function
->function_addrs
) + 1
2520 < function
->function_addrs_count
)
2521 && pc
>= (function_addrs
+ 1)->low
2522 && pc
< (function_addrs
+ 1)->high
)
2525 /* We found an inlined call. */
2527 inlined
= function_addrs
->function
;
2529 /* Report any calls inlined into this one. */
2530 ret
= report_inlined_functions (pc
, inlined
, callback
, data
,
2535 /* Report this inlined call. */
2536 ret
= callback (data
, pc
, *filename
, *lineno
, inlined
->name
);
2540 /* Our caller will report the caller of the inlined function; tell
2541 it the appropriate filename and line number. */
2542 *filename
= inlined
->caller_filename
;
2543 *lineno
= inlined
->caller_lineno
;
2548 /* Look for a PC in the DWARF mapping for one module. On success,
2549 call CALLBACK and return whatever it returns. On error, call
2550 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
2554 dwarf_lookup_pc (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2555 uintptr_t pc
, backtrace_full_callback callback
,
2556 backtrace_error_callback error_callback
, void *data
,
2559 struct unit_addrs
*entry
;
2564 struct function_addrs
*function_addrs
;
2565 struct function
*function
;
2566 const char *filename
;
2572 /* Find an address range that includes PC. */
2573 entry
= bsearch (&pc
, ddata
->addrs
, ddata
->addrs_count
,
2574 sizeof (struct unit_addrs
), unit_addrs_search
);
2582 /* If there are multiple ranges that contain PC, use the last one,
2583 in order to produce predictable results. If we assume that all
2584 ranges are properly nested, then the last range will be the
2586 while ((size_t) (entry
- ddata
->addrs
) + 1 < ddata
->addrs_count
2587 && pc
>= (entry
+ 1)->low
2588 && pc
< (entry
+ 1)->high
)
2591 /* We need the lines, lines_count, function_addrs,
2592 function_addrs_count fields of u. If they are not set, we need
2593 to set them. When running in threaded mode, we need to allow for
2594 the possibility that some other thread is setting them
2600 /* Skip units with no useful line number information by walking
2601 backward. Useless line number information is marked by setting
2603 while (entry
> ddata
->addrs
2604 && pc
>= (entry
- 1)->low
2605 && pc
< (entry
- 1)->high
)
2607 if (state
->threaded
)
2609 /* Use __sync_bool_compare_and_swap to do a
2611 while (!__sync_bool_compare_and_swap (&u
->lines
, lines
, lines
))
2615 if (lines
!= (struct line
*) (uintptr_t) -1)
2624 /* Do a load-acquire of u->lines. */
2625 if (state
->threaded
)
2627 /* Use __sync_bool_compare_and_swap to do an atomic load. */
2628 while (!__sync_bool_compare_and_swap (&u
->lines
, lines
, lines
))
2635 size_t function_addrs_count
;
2636 struct line_header lhdr
;
2639 /* We have never read the line information for this unit. Read
2642 function_addrs
= NULL
;
2643 function_addrs_count
= 0;
2644 if (read_line_info (state
, ddata
, error_callback
, data
, entry
->u
, &lhdr
,
2647 read_function_info (state
, ddata
, &lhdr
, error_callback
, data
,
2648 entry
->u
, &ddata
->fvec
, &function_addrs
,
2649 &function_addrs_count
);
2650 free_line_header (state
, &lhdr
, error_callback
, data
);
2654 /* Atomically store the information we just read into the unit.
2655 If another thread is simultaneously writing, it presumably
2656 read the same information, and we don't care which one we
2657 wind up with; we just leak the other one. We do have to
2658 write the lines field last, so that the acquire-loads above
2659 ensure that the other fields are set. */
2661 if (!state
->threaded
)
2663 u
->lines_count
= count
;
2664 u
->function_addrs
= function_addrs
;
2665 u
->function_addrs_count
= function_addrs_count
;
2670 __sync_bool_compare_and_swap (&u
->lines_count
, 0, count
);
2671 __sync_bool_compare_and_swap (&u
->function_addrs
, NULL
,
2673 __sync_bool_compare_and_swap (&u
->function_addrs_count
, 0,
2674 function_addrs_count
);
2675 __sync_bool_compare_and_swap (&u
->lines
, NULL
, lines
);
2679 /* Now all fields of U have been initialized. */
2681 if (lines
== (struct line
*) (uintptr_t) -1)
2683 /* If reading the line number information failed in some way,
2684 try again to see if there is a better compilation unit for
2687 return dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2689 return callback (data
, pc
, NULL
, 0, NULL
);
2692 /* Search for PC within this unit. */
2694 ln
= (struct line
*) bsearch (&pc
, lines
, entry
->u
->lines_count
,
2695 sizeof (struct line
), line_search
);
2698 error_callback (data
, "inconsistent DWARF line number info", 0);
2702 /* Search for function name within this unit. */
2704 if (entry
->u
->function_addrs_count
== 0)
2705 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2707 function_addrs
= ((struct function_addrs
*)
2708 bsearch (&pc
, entry
->u
->function_addrs
,
2709 entry
->u
->function_addrs_count
,
2710 sizeof (struct function_addrs
),
2711 function_addrs_search
));
2712 if (function_addrs
== NULL
)
2713 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2715 /* If there are multiple function ranges that contain PC, use the
2716 last one, in order to produce predictable results. */
2718 while (((size_t) (function_addrs
- entry
->u
->function_addrs
+ 1)
2719 < entry
->u
->function_addrs_count
)
2720 && pc
>= (function_addrs
+ 1)->low
2721 && pc
< (function_addrs
+ 1)->high
)
2724 function
= function_addrs
->function
;
2726 filename
= ln
->filename
;
2727 lineno
= ln
->lineno
;
2729 ret
= report_inlined_functions (pc
, function
, callback
, data
,
2730 &filename
, &lineno
);
2734 return callback (data
, pc
, filename
, lineno
, function
->name
);
2738 /* Return the file/line information for a PC using the DWARF mapping
2739 we built earlier. */
2742 dwarf_fileline (struct backtrace_state
*state
, uintptr_t pc
,
2743 backtrace_full_callback callback
,
2744 backtrace_error_callback error_callback
, void *data
)
2746 struct dwarf_data
*ddata
;
2750 if (!state
->threaded
)
2752 for (ddata
= (struct dwarf_data
*) state
->fileline_data
;
2754 ddata
= ddata
->next
)
2756 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2758 if (ret
!= 0 || found
)
2764 struct dwarf_data
**pp
;
2766 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
2771 while (!__sync_bool_compare_and_swap (pp
, ddata
, ddata
))
2777 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2779 if (ret
!= 0 || found
)
2786 /* FIXME: See if any libraries have been dlopen'ed. */
2788 return callback (data
, pc
, NULL
, 0, NULL
);
2791 /* Initialize our data structures from the DWARF debug info for a
2792 file. Return NULL on failure. */
2794 static struct dwarf_data
*
2795 build_dwarf_data (struct backtrace_state
*state
,
2796 uintptr_t base_address
,
2797 const unsigned char *dwarf_info
,
2798 size_t dwarf_info_size
,
2799 const unsigned char *dwarf_line
,
2800 size_t dwarf_line_size
,
2801 const unsigned char *dwarf_abbrev
,
2802 size_t dwarf_abbrev_size
,
2803 const unsigned char *dwarf_ranges
,
2804 size_t dwarf_ranges_size
,
2805 const unsigned char *dwarf_str
,
2806 size_t dwarf_str_size
,
2808 backtrace_error_callback error_callback
,
2811 struct unit_addrs_vector addrs_vec
;
2812 struct unit_addrs
*addrs
;
2814 struct dwarf_data
*fdata
;
2816 if (!build_address_map (state
, base_address
, dwarf_info
, dwarf_info_size
,
2817 dwarf_abbrev
, dwarf_abbrev_size
, dwarf_ranges
,
2818 dwarf_ranges_size
, dwarf_str
, dwarf_str_size
,
2819 is_bigendian
, error_callback
, data
, &addrs_vec
))
2822 if (!backtrace_vector_release (state
, &addrs_vec
.vec
, error_callback
, data
))
2824 addrs
= (struct unit_addrs
*) addrs_vec
.vec
.base
;
2825 addrs_count
= addrs_vec
.count
;
2826 qsort (addrs
, addrs_count
, sizeof (struct unit_addrs
), unit_addrs_compare
);
2828 fdata
= ((struct dwarf_data
*)
2829 backtrace_alloc (state
, sizeof (struct dwarf_data
),
2830 error_callback
, data
));
2835 fdata
->base_address
= base_address
;
2836 fdata
->addrs
= addrs
;
2837 fdata
->addrs_count
= addrs_count
;
2838 fdata
->dwarf_info
= dwarf_info
;
2839 fdata
->dwarf_info_size
= dwarf_info_size
;
2840 fdata
->dwarf_line
= dwarf_line
;
2841 fdata
->dwarf_line_size
= dwarf_line_size
;
2842 fdata
->dwarf_ranges
= dwarf_ranges
;
2843 fdata
->dwarf_ranges_size
= dwarf_ranges_size
;
2844 fdata
->dwarf_str
= dwarf_str
;
2845 fdata
->dwarf_str_size
= dwarf_str_size
;
2846 fdata
->is_bigendian
= is_bigendian
;
2847 memset (&fdata
->fvec
, 0, sizeof fdata
->fvec
);
2852 /* Build our data structures from the DWARF sections for a module.
2853 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
2857 backtrace_dwarf_add (struct backtrace_state
*state
,
2858 uintptr_t base_address
,
2859 const unsigned char *dwarf_info
,
2860 size_t dwarf_info_size
,
2861 const unsigned char *dwarf_line
,
2862 size_t dwarf_line_size
,
2863 const unsigned char *dwarf_abbrev
,
2864 size_t dwarf_abbrev_size
,
2865 const unsigned char *dwarf_ranges
,
2866 size_t dwarf_ranges_size
,
2867 const unsigned char *dwarf_str
,
2868 size_t dwarf_str_size
,
2870 backtrace_error_callback error_callback
,
2871 void *data
, fileline
*fileline_fn
)
2873 struct dwarf_data
*fdata
;
2875 fdata
= build_dwarf_data (state
, base_address
, dwarf_info
, dwarf_info_size
,
2876 dwarf_line
, dwarf_line_size
, dwarf_abbrev
,
2877 dwarf_abbrev_size
, dwarf_ranges
, dwarf_ranges_size
,
2878 dwarf_str
, dwarf_str_size
, is_bigendian
,
2879 error_callback
, data
);
2883 if (!state
->threaded
)
2885 struct dwarf_data
**pp
;
2887 for (pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
2897 struct dwarf_data
**pp
;
2899 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
2903 struct dwarf_data
*p
;
2907 while (!__sync_bool_compare_and_swap (pp
, p
, p
))
2916 if (__sync_bool_compare_and_swap (pp
, NULL
, fdata
))
2921 *fileline_fn
= dwarf_fileline
;