1 /* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012-2013 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 /* Primary source file. */
287 const char *filename
;
288 /* Compilation command working directory. */
289 const char *comp_dir
;
290 /* Absolute file name, only set if needed. */
291 const char *abs_filename
;
292 /* The abbreviations for this unit. */
293 struct abbrevs abbrevs
;
295 /* The fields above this point are read in during initialization and
296 may be accessed freely. The fields below this point are read in
297 as needed, and therefore require care, as different threads may
298 try to initialize them simultaneously. */
300 /* PC to line number mapping. This is NULL if the values have not
301 been read. This is (struct line *) -1 if there was an error
302 reading the values. */
304 /* Number of entries in lines. */
306 /* PC ranges to function. */
307 struct function_addrs
*function_addrs
;
308 size_t function_addrs_count
;
311 /* An address range for a compilation unit. This maps a PC value to a
312 specific compilation unit. Note that we invert the representation
313 in DWARF: instead of listing the units and attaching a list of
314 ranges, we list the ranges and have each one point to the unit.
315 This lets us do a binary search to find the unit. */
319 /* Range is LOW <= PC < HIGH. */
322 /* Compilation unit for this address range. */
326 /* A growable vector of compilation unit address ranges. */
328 struct unit_addrs_vector
330 /* Memory. This is an array of struct unit_addrs. */
331 struct backtrace_vector vec
;
332 /* Number of address ranges present. */
336 /* The information we need to map a PC to a file and line. */
340 /* The data for the next file we know about. */
341 struct dwarf_data
*next
;
342 /* The base address for this file. */
343 uintptr_t base_address
;
344 /* A sorted list of address ranges. */
345 struct unit_addrs
*addrs
;
346 /* Number of address ranges in list. */
348 /* The unparsed .debug_info section. */
349 const unsigned char *dwarf_info
;
350 size_t dwarf_info_size
;
351 /* The unparsed .debug_line section. */
352 const unsigned char *dwarf_line
;
353 size_t dwarf_line_size
;
354 /* The unparsed .debug_ranges section. */
355 const unsigned char *dwarf_ranges
;
356 size_t dwarf_ranges_size
;
357 /* The unparsed .debug_str section. */
358 const unsigned char *dwarf_str
;
359 size_t dwarf_str_size
;
360 /* Whether the data is big-endian or not. */
362 /* A vector used for function addresses. We keep this here so that
363 we can grow the vector as we read more functions. */
364 struct function_vector fvec
;
367 /* Report an error for a DWARF buffer. */
370 dwarf_buf_error (struct dwarf_buf
*buf
, const char *msg
)
374 snprintf (b
, sizeof b
, "%s in %s at %d",
375 msg
, buf
->name
, (int) (buf
->buf
- buf
->start
));
376 buf
->error_callback (buf
->data
, b
, 0);
379 /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
383 require (struct dwarf_buf
*buf
, size_t count
)
385 if (buf
->left
>= count
)
388 if (!buf
->reported_underflow
)
390 dwarf_buf_error (buf
, "DWARF underflow");
391 buf
->reported_underflow
= 1;
397 /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
401 advance (struct dwarf_buf
*buf
, size_t count
)
403 if (!require (buf
, count
))
410 /* Read one byte from BUF and advance 1 byte. */
413 read_byte (struct dwarf_buf
*buf
)
415 const unsigned char *p
= buf
->buf
;
417 if (!advance (buf
, 1))
422 /* Read a signed char from BUF and advance 1 byte. */
425 read_sbyte (struct dwarf_buf
*buf
)
427 const unsigned char *p
= buf
->buf
;
429 if (!advance (buf
, 1))
431 return (*p
^ 0x80) - 0x80;
434 /* Read a uint16 from BUF and advance 2 bytes. */
437 read_uint16 (struct dwarf_buf
*buf
)
439 const unsigned char *p
= buf
->buf
;
441 if (!advance (buf
, 2))
443 if (buf
->is_bigendian
)
444 return ((uint16_t) p
[0] << 8) | (uint16_t) p
[1];
446 return ((uint16_t) p
[1] << 8) | (uint16_t) p
[0];
449 /* Read a uint32 from BUF and advance 4 bytes. */
452 read_uint32 (struct dwarf_buf
*buf
)
454 const unsigned char *p
= buf
->buf
;
456 if (!advance (buf
, 4))
458 if (buf
->is_bigendian
)
459 return (((uint32_t) p
[0] << 24) | ((uint32_t) p
[1] << 16)
460 | ((uint32_t) p
[2] << 8) | (uint32_t) p
[3]);
462 return (((uint32_t) p
[3] << 24) | ((uint32_t) p
[2] << 16)
463 | ((uint32_t) p
[1] << 8) | (uint32_t) p
[0]);
466 /* Read a uint64 from BUF and advance 8 bytes. */
469 read_uint64 (struct dwarf_buf
*buf
)
471 const unsigned char *p
= buf
->buf
;
473 if (!advance (buf
, 8))
475 if (buf
->is_bigendian
)
476 return (((uint64_t) p
[0] << 56) | ((uint64_t) p
[1] << 48)
477 | ((uint64_t) p
[2] << 40) | ((uint64_t) p
[3] << 32)
478 | ((uint64_t) p
[4] << 24) | ((uint64_t) p
[5] << 16)
479 | ((uint64_t) p
[6] << 8) | (uint64_t) p
[7]);
481 return (((uint64_t) p
[7] << 56) | ((uint64_t) p
[6] << 48)
482 | ((uint64_t) p
[5] << 40) | ((uint64_t) p
[4] << 32)
483 | ((uint64_t) p
[3] << 24) | ((uint64_t) p
[2] << 16)
484 | ((uint64_t) p
[1] << 8) | (uint64_t) p
[0]);
487 /* Read an offset from BUF and advance the appropriate number of
491 read_offset (struct dwarf_buf
*buf
, int is_dwarf64
)
494 return read_uint64 (buf
);
496 return read_uint32 (buf
);
499 /* Read an address from BUF and advance the appropriate number of
503 read_address (struct dwarf_buf
*buf
, int addrsize
)
508 return read_byte (buf
);
510 return read_uint16 (buf
);
512 return read_uint32 (buf
);
514 return read_uint64 (buf
);
516 dwarf_buf_error (buf
, "unrecognized address size");
521 /* Return whether a value is the highest possible address, given the
525 is_highest_address (uint64_t address
, int addrsize
)
530 return address
== (unsigned char) -1;
532 return address
== (uint16_t) -1;
534 return address
== (uint32_t) -1;
536 return address
== (uint64_t) -1;
542 /* Read an unsigned LEB128 number. */
545 read_uleb128 (struct dwarf_buf
*buf
)
557 const unsigned char *p
;
560 if (!advance (buf
, 1))
564 ret
|= ((uint64_t) (b
& 0x7f)) << shift
;
567 dwarf_buf_error (buf
, "LEB128 overflows uint64_t");
572 while ((b
& 0x80) != 0);
577 /* Read a signed LEB128 number. */
580 read_sleb128 (struct dwarf_buf
*buf
)
592 const unsigned char *p
;
595 if (!advance (buf
, 1))
599 val
|= ((uint64_t) (b
& 0x7f)) << shift
;
602 dwarf_buf_error (buf
, "signed LEB128 overflows uint64_t");
607 while ((b
& 0x80) != 0);
609 if ((b
& 0x40) != 0 && shift
< 64)
610 val
|= ((uint64_t) -1) << shift
;
612 return (int64_t) val
;
615 /* Return the length of an LEB128 number. */
618 leb128_len (const unsigned char *p
)
623 while ((*p
& 0x80) != 0)
631 /* Free an abbreviations structure. */
634 free_abbrevs (struct backtrace_state
*state
, struct abbrevs
*abbrevs
,
635 backtrace_error_callback error_callback
, void *data
)
639 for (i
= 0; i
< abbrevs
->num_abbrevs
; ++i
)
640 backtrace_free (state
, abbrevs
->abbrevs
[i
].attrs
,
641 abbrevs
->abbrevs
[i
].num_attrs
* sizeof (struct attr
),
642 error_callback
, data
);
643 backtrace_free (state
, abbrevs
->abbrevs
,
644 abbrevs
->num_abbrevs
* sizeof (struct abbrev
),
645 error_callback
, data
);
646 abbrevs
->num_abbrevs
= 0;
647 abbrevs
->abbrevs
= NULL
;
650 /* Read an attribute value. Returns 1 on success, 0 on failure. If
651 the value can be represented as a uint64_t, sets *VAL and sets
652 *IS_VALID to 1. We don't try to store the value of other attribute
653 forms, because we don't care about them. */
656 read_attribute (enum dwarf_form form
, struct dwarf_buf
*buf
,
657 int is_dwarf64
, int version
, int addrsize
,
658 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
659 struct attr_val
*val
)
661 /* Avoid warnings about val.u.FIELD may be used uninitialized if
662 this function is inlined. The warnings aren't valid but can
663 occur because the different fields are set and used
665 memset (val
, 0, sizeof *val
);
670 val
->encoding
= ATTR_VAL_ADDRESS
;
671 val
->u
.uint
= read_address (buf
, addrsize
);
674 val
->encoding
= ATTR_VAL_BLOCK
;
675 return advance (buf
, read_uint16 (buf
));
677 val
->encoding
= ATTR_VAL_BLOCK
;
678 return advance (buf
, read_uint32 (buf
));
680 val
->encoding
= ATTR_VAL_UINT
;
681 val
->u
.uint
= read_uint16 (buf
);
684 val
->encoding
= ATTR_VAL_UINT
;
685 val
->u
.uint
= read_uint32 (buf
);
688 val
->encoding
= ATTR_VAL_UINT
;
689 val
->u
.uint
= read_uint64 (buf
);
692 val
->encoding
= ATTR_VAL_STRING
;
693 val
->u
.string
= (const char *) buf
->buf
;
694 return advance (buf
, strnlen ((const char *) buf
->buf
, buf
->left
) + 1);
696 val
->encoding
= ATTR_VAL_BLOCK
;
697 return advance (buf
, read_uleb128 (buf
));
699 val
->encoding
= ATTR_VAL_BLOCK
;
700 return advance (buf
, read_byte (buf
));
702 val
->encoding
= ATTR_VAL_UINT
;
703 val
->u
.uint
= read_byte (buf
);
706 val
->encoding
= ATTR_VAL_UINT
;
707 val
->u
.uint
= read_byte (buf
);
710 val
->encoding
= ATTR_VAL_SINT
;
711 val
->u
.sint
= read_sleb128 (buf
);
717 offset
= read_offset (buf
, is_dwarf64
);
718 if (offset
>= dwarf_str_size
)
720 dwarf_buf_error (buf
, "DW_FORM_strp out of range");
723 val
->encoding
= ATTR_VAL_STRING
;
724 val
->u
.string
= (const char *) dwarf_str
+ offset
;
728 val
->encoding
= ATTR_VAL_UINT
;
729 val
->u
.uint
= read_uleb128 (buf
);
731 case DW_FORM_ref_addr
:
732 val
->encoding
= ATTR_VAL_REF_INFO
;
734 val
->u
.uint
= read_address (buf
, addrsize
);
736 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
739 val
->encoding
= ATTR_VAL_REF_UNIT
;
740 val
->u
.uint
= read_byte (buf
);
743 val
->encoding
= ATTR_VAL_REF_UNIT
;
744 val
->u
.uint
= read_uint16 (buf
);
747 val
->encoding
= ATTR_VAL_REF_UNIT
;
748 val
->u
.uint
= read_uint32 (buf
);
751 val
->encoding
= ATTR_VAL_REF_UNIT
;
752 val
->u
.uint
= read_uint64 (buf
);
754 case DW_FORM_ref_udata
:
755 val
->encoding
= ATTR_VAL_REF_UNIT
;
756 val
->u
.uint
= read_uleb128 (buf
);
758 case DW_FORM_indirect
:
762 form
= read_uleb128 (buf
);
763 return read_attribute ((enum dwarf_form
) form
, buf
, is_dwarf64
,
764 version
, addrsize
, dwarf_str
, dwarf_str_size
,
767 case DW_FORM_sec_offset
:
768 val
->encoding
= ATTR_VAL_REF_SECTION
;
769 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
771 case DW_FORM_exprloc
:
772 val
->encoding
= ATTR_VAL_EXPR
;
773 return advance (buf
, read_uleb128 (buf
));
774 case DW_FORM_flag_present
:
775 val
->encoding
= ATTR_VAL_UINT
;
778 case DW_FORM_ref_sig8
:
779 val
->encoding
= ATTR_VAL_REF_TYPE
;
780 val
->u
.uint
= read_uint64 (buf
);
782 case DW_FORM_GNU_addr_index
:
783 val
->encoding
= ATTR_VAL_REF_SECTION
;
784 val
->u
.uint
= read_uleb128 (buf
);
786 case DW_FORM_GNU_str_index
:
787 val
->encoding
= ATTR_VAL_REF_SECTION
;
788 val
->u
.uint
= read_uleb128 (buf
);
790 case DW_FORM_GNU_ref_alt
:
791 val
->encoding
= ATTR_VAL_REF_SECTION
;
792 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
794 case DW_FORM_GNU_strp_alt
:
795 val
->encoding
= ATTR_VAL_REF_SECTION
;
796 val
->u
.uint
= read_offset (buf
, is_dwarf64
);
799 dwarf_buf_error (buf
, "unrecognized DWARF form");
804 /* Compare function_addrs for qsort. When ranges are nested, make the
805 smallest one sort last. */
808 function_addrs_compare (const void *v1
, const void *v2
)
810 const struct function_addrs
*a1
= (const struct function_addrs
*) v1
;
811 const struct function_addrs
*a2
= (const struct function_addrs
*) v2
;
813 if (a1
->low
< a2
->low
)
815 if (a1
->low
> a2
->low
)
817 if (a1
->high
< a2
->high
)
819 if (a1
->high
> a2
->high
)
821 return strcmp (a1
->function
->name
, a2
->function
->name
);
824 /* Compare a PC against a function_addrs for bsearch. Note that if
825 there are multiple ranges containing PC, which one will be returned
826 is unpredictable. We compensate for that in dwarf_fileline. */
829 function_addrs_search (const void *vkey
, const void *ventry
)
831 const uintptr_t *key
= (const uintptr_t *) vkey
;
832 const struct function_addrs
*entry
= (const struct function_addrs
*) ventry
;
838 else if (pc
>= entry
->high
)
844 /* Add a new compilation unit address range to a vector. Returns 1 on
845 success, 0 on failure. */
848 add_unit_addr (struct backtrace_state
*state
, uintptr_t base_address
,
849 struct unit_addrs addrs
,
850 backtrace_error_callback error_callback
, void *data
,
851 struct unit_addrs_vector
*vec
)
853 struct unit_addrs
*p
;
855 /* Add in the base address of the module here, so that we can look
856 up the PC directly. */
857 addrs
.low
+= base_address
;
858 addrs
.high
+= base_address
;
860 /* Try to merge with the last entry. */
863 p
= (struct unit_addrs
*) vec
->vec
.base
+ (vec
->count
- 1);
864 if ((addrs
.low
== p
->high
|| addrs
.low
== p
->high
+ 1)
867 if (addrs
.high
> p
->high
)
868 p
->high
= addrs
.high
;
873 p
= ((struct unit_addrs
*)
874 backtrace_vector_grow (state
, sizeof (struct unit_addrs
),
875 error_callback
, data
, &vec
->vec
));
884 /* Free a unit address vector. */
887 free_unit_addrs_vector (struct backtrace_state
*state
,
888 struct unit_addrs_vector
*vec
,
889 backtrace_error_callback error_callback
, void *data
)
891 struct unit_addrs
*addrs
;
894 addrs
= (struct unit_addrs
*) vec
->vec
.base
;
895 for (i
= 0; i
< vec
->count
; ++i
)
896 free_abbrevs (state
, &addrs
[i
].u
->abbrevs
, error_callback
, data
);
899 /* Compare unit_addrs for qsort. When ranges are nested, make the
900 smallest one sort last. */
903 unit_addrs_compare (const void *v1
, const void *v2
)
905 const struct unit_addrs
*a1
= (const struct unit_addrs
*) v1
;
906 const struct unit_addrs
*a2
= (const struct unit_addrs
*) v2
;
908 if (a1
->low
< a2
->low
)
910 if (a1
->low
> a2
->low
)
912 if (a1
->high
< a2
->high
)
914 if (a1
->high
> a2
->high
)
916 if (a1
->u
->lineoff
< a2
->u
->lineoff
)
918 if (a1
->u
->lineoff
> a2
->u
->lineoff
)
923 /* Compare a PC against a unit_addrs for bsearch. Note that if there
924 are multiple ranges containing PC, which one will be returned is
925 unpredictable. We compensate for that in dwarf_fileline. */
928 unit_addrs_search (const void *vkey
, const void *ventry
)
930 const uintptr_t *key
= (const uintptr_t *) vkey
;
931 const struct unit_addrs
*entry
= (const struct unit_addrs
*) ventry
;
937 else if (pc
>= entry
->high
)
943 /* Sort the line vector by PC. We want a stable sort here. We know
944 that the pointers are into the same array, so it is safe to compare
948 line_compare (const void *v1
, const void *v2
)
950 const struct line
*ln1
= (const struct line
*) v1
;
951 const struct line
*ln2
= (const struct line
*) v2
;
953 if (ln1
->pc
< ln2
->pc
)
955 else if (ln1
->pc
> ln2
->pc
)
965 /* Find a PC in a line vector. We always allocate an extra entry at
966 the end of the lines vector, so that this routine can safely look
967 at the next entry. Note that when there are multiple mappings for
968 the same PC value, this will return the last one. */
971 line_search (const void *vkey
, const void *ventry
)
973 const uintptr_t *key
= (const uintptr_t *) vkey
;
974 const struct line
*entry
= (const struct line
*) ventry
;
980 else if (pc
>= (entry
+ 1)->pc
)
986 /* Sort the abbrevs by the abbrev code. This function is passed to
987 both qsort and bsearch. */
990 abbrev_compare (const void *v1
, const void *v2
)
992 const struct abbrev
*a1
= (const struct abbrev
*) v1
;
993 const struct abbrev
*a2
= (const struct abbrev
*) v2
;
995 if (a1
->code
< a2
->code
)
997 else if (a1
->code
> a2
->code
)
1001 /* This really shouldn't happen. It means there are two
1002 different abbrevs with the same code, and that means we don't
1003 know which one lookup_abbrev should return. */
1008 /* Read the abbreviation table for a compilation unit. Returns 1 on
1009 success, 0 on failure. */
1012 read_abbrevs (struct backtrace_state
*state
, uint64_t abbrev_offset
,
1013 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1014 int is_bigendian
, backtrace_error_callback error_callback
,
1015 void *data
, struct abbrevs
*abbrevs
)
1017 struct dwarf_buf abbrev_buf
;
1018 struct dwarf_buf count_buf
;
1021 abbrevs
->num_abbrevs
= 0;
1022 abbrevs
->abbrevs
= NULL
;
1024 if (abbrev_offset
>= dwarf_abbrev_size
)
1026 error_callback (data
, "abbrev offset out of range", 0);
1030 abbrev_buf
.name
= ".debug_abbrev";
1031 abbrev_buf
.start
= dwarf_abbrev
;
1032 abbrev_buf
.buf
= dwarf_abbrev
+ abbrev_offset
;
1033 abbrev_buf
.left
= dwarf_abbrev_size
- abbrev_offset
;
1034 abbrev_buf
.is_bigendian
= is_bigendian
;
1035 abbrev_buf
.error_callback
= error_callback
;
1036 abbrev_buf
.data
= data
;
1037 abbrev_buf
.reported_underflow
= 0;
1039 /* Count the number of abbrevs in this list. */
1041 count_buf
= abbrev_buf
;
1043 while (read_uleb128 (&count_buf
) != 0)
1045 if (count_buf
.reported_underflow
)
1049 read_uleb128 (&count_buf
);
1050 // Skip has_children.
1051 read_byte (&count_buf
);
1053 while (read_uleb128 (&count_buf
) != 0)
1054 read_uleb128 (&count_buf
);
1055 // Skip form of last attribute.
1056 read_uleb128 (&count_buf
);
1059 if (count_buf
.reported_underflow
)
1062 if (num_abbrevs
== 0)
1065 abbrevs
->num_abbrevs
= num_abbrevs
;
1066 abbrevs
->abbrevs
= ((struct abbrev
*)
1067 backtrace_alloc (state
,
1068 num_abbrevs
* sizeof (struct abbrev
),
1069 error_callback
, data
));
1070 if (abbrevs
->abbrevs
== NULL
)
1072 memset (abbrevs
->abbrevs
, 0, num_abbrevs
* sizeof (struct abbrev
));
1082 if (abbrev_buf
.reported_underflow
)
1085 code
= read_uleb128 (&abbrev_buf
);
1090 a
.tag
= (enum dwarf_tag
) read_uleb128 (&abbrev_buf
);
1091 a
.has_children
= read_byte (&abbrev_buf
);
1093 count_buf
= abbrev_buf
;
1095 while (read_uleb128 (&count_buf
) != 0)
1098 read_uleb128 (&count_buf
);
1104 read_uleb128 (&abbrev_buf
);
1105 read_uleb128 (&abbrev_buf
);
1109 attrs
= ((struct attr
*)
1110 backtrace_alloc (state
, num_attrs
* sizeof *attrs
,
1111 error_callback
, data
));
1120 name
= read_uleb128 (&abbrev_buf
);
1121 form
= read_uleb128 (&abbrev_buf
);
1124 attrs
[num_attrs
].name
= (enum dwarf_attribute
) name
;
1125 attrs
[num_attrs
].form
= (enum dwarf_form
) form
;
1130 a
.num_attrs
= num_attrs
;
1133 abbrevs
->abbrevs
[num_abbrevs
] = a
;
1137 qsort (abbrevs
->abbrevs
, abbrevs
->num_abbrevs
, sizeof (struct abbrev
),
1143 free_abbrevs (state
, abbrevs
, error_callback
, data
);
1147 /* Return the abbrev information for an abbrev code. */
1149 static const struct abbrev
*
1150 lookup_abbrev (struct abbrevs
*abbrevs
, uint64_t code
,
1151 backtrace_error_callback error_callback
, void *data
)
1156 /* With GCC, where abbrevs are simply numbered in order, we should
1157 be able to just look up the entry. */
1158 if (code
- 1 < abbrevs
->num_abbrevs
1159 && abbrevs
->abbrevs
[code
- 1].code
== code
)
1160 return &abbrevs
->abbrevs
[code
- 1];
1162 /* Otherwise we have to search. */
1163 memset (&key
, 0, sizeof key
);
1165 p
= bsearch (&key
, abbrevs
->abbrevs
, abbrevs
->num_abbrevs
,
1166 sizeof (struct abbrev
), abbrev_compare
);
1169 error_callback (data
, "invalid abbreviation code", 0);
1172 return (const struct abbrev
*) p
;
1175 /* Add non-contiguous address ranges for a compilation unit. Returns
1176 1 on success, 0 on failure. */
1179 add_unit_ranges (struct backtrace_state
*state
, uintptr_t base_address
,
1180 struct unit
*u
, uint64_t ranges
, uint64_t base
,
1181 int is_bigendian
, const unsigned char *dwarf_ranges
,
1182 size_t dwarf_ranges_size
,
1183 backtrace_error_callback error_callback
, void *data
,
1184 struct unit_addrs_vector
*addrs
)
1186 struct dwarf_buf ranges_buf
;
1188 if (ranges
>= dwarf_ranges_size
)
1190 error_callback (data
, "ranges offset out of range", 0);
1194 ranges_buf
.name
= ".debug_ranges";
1195 ranges_buf
.start
= dwarf_ranges
;
1196 ranges_buf
.buf
= dwarf_ranges
+ ranges
;
1197 ranges_buf
.left
= dwarf_ranges_size
- ranges
;
1198 ranges_buf
.is_bigendian
= is_bigendian
;
1199 ranges_buf
.error_callback
= error_callback
;
1200 ranges_buf
.data
= data
;
1201 ranges_buf
.reported_underflow
= 0;
1208 if (ranges_buf
.reported_underflow
)
1211 low
= read_address (&ranges_buf
, u
->addrsize
);
1212 high
= read_address (&ranges_buf
, u
->addrsize
);
1214 if (low
== 0 && high
== 0)
1217 if (is_highest_address (low
, u
->addrsize
))
1221 struct unit_addrs a
;
1224 a
.high
= high
+ base
;
1226 if (!add_unit_addr (state
, base_address
, a
, error_callback
, data
,
1232 if (ranges_buf
.reported_underflow
)
1238 /* Build a mapping from address ranges to the compilation units where
1239 the line number information for that range can be found. Returns 1
1240 on success, 0 on failure. */
1243 build_address_map (struct backtrace_state
*state
, uintptr_t base_address
,
1244 const unsigned char *dwarf_info
, size_t dwarf_info_size
,
1245 const unsigned char *dwarf_abbrev
, size_t dwarf_abbrev_size
,
1246 const unsigned char *dwarf_ranges
, size_t dwarf_ranges_size
,
1247 const unsigned char *dwarf_str
, size_t dwarf_str_size
,
1248 int is_bigendian
, backtrace_error_callback error_callback
,
1249 void *data
, struct unit_addrs_vector
*addrs
)
1251 struct dwarf_buf info
;
1252 struct abbrevs abbrevs
;
1254 memset (&addrs
->vec
, 0, sizeof addrs
->vec
);
1257 /* Read through the .debug_info section. FIXME: Should we use the
1258 .debug_aranges section? gdb and addr2line don't use it, but I'm
1261 info
.name
= ".debug_info";
1262 info
.start
= dwarf_info
;
1263 info
.buf
= dwarf_info
;
1264 info
.left
= dwarf_info_size
;
1265 info
.is_bigendian
= is_bigendian
;
1266 info
.error_callback
= error_callback
;
1268 info
.reported_underflow
= 0;
1270 memset (&abbrevs
, 0, sizeof abbrevs
);
1271 while (info
.left
> 0)
1273 const unsigned char *unit_data_start
;
1276 struct dwarf_buf unit_buf
;
1278 uint64_t abbrev_offset
;
1279 const struct abbrev
*abbrev
;
1281 const unsigned char *unit_data
;
1282 size_t unit_data_len
;
1283 size_t unit_data_offset
;
1290 int highpc_is_relative
;
1295 const char *filename
;
1296 const char *comp_dir
;
1298 if (info
.reported_underflow
)
1301 unit_data_start
= info
.buf
;
1304 len
= read_uint32 (&info
);
1305 if (len
== 0xffffffff)
1307 len
= read_uint64 (&info
);
1312 unit_buf
.left
= len
;
1314 if (!advance (&info
, len
))
1317 version
= read_uint16 (&unit_buf
);
1318 if (version
< 2 || version
> 4)
1320 dwarf_buf_error (&unit_buf
, "unrecognized DWARF version");
1324 abbrev_offset
= read_offset (&unit_buf
, is_dwarf64
);
1325 if (!read_abbrevs (state
, abbrev_offset
, dwarf_abbrev
, dwarf_abbrev_size
,
1326 is_bigendian
, error_callback
, data
, &abbrevs
))
1329 addrsize
= read_byte (&unit_buf
);
1331 unit_data
= unit_buf
.buf
;
1332 unit_data_len
= unit_buf
.left
;
1333 unit_data_offset
= unit_buf
.buf
- unit_data_start
;
1335 /* We only look at the first attribute in the compilation unit.
1336 In practice this will be a DW_TAG_compile_unit which will
1337 tell us the PC range and where to find the line number
1340 code
= read_uleb128 (&unit_buf
);
1341 abbrev
= lookup_abbrev (&abbrevs
, code
, error_callback
, data
);
1349 highpc_is_relative
= 0;
1356 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
1358 struct attr_val val
;
1360 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
, is_dwarf64
,
1361 version
, addrsize
, dwarf_str
, dwarf_str_size
,
1365 switch (abbrev
->attrs
[i
].name
)
1368 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1375 if (val
.encoding
== ATTR_VAL_ADDRESS
)
1377 highpc
= val
.u
.uint
;
1380 else if (val
.encoding
== ATTR_VAL_UINT
)
1382 highpc
= val
.u
.uint
;
1384 highpc_is_relative
= 1;
1388 if (val
.encoding
== ATTR_VAL_UINT
1389 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1391 ranges
= val
.u
.uint
;
1395 case DW_AT_stmt_list
:
1396 if (val
.encoding
== ATTR_VAL_UINT
1397 || val
.encoding
== ATTR_VAL_REF_SECTION
)
1399 lineoff
= val
.u
.uint
;
1404 if (val
.encoding
== ATTR_VAL_STRING
)
1405 filename
= val
.u
.string
;
1407 case DW_AT_comp_dir
:
1408 if (val
.encoding
== ATTR_VAL_STRING
)
1409 comp_dir
= val
.u
.string
;
1416 if (unit_buf
.reported_underflow
)
1419 if (((have_lowpc
&& have_highpc
) || have_ranges
) && have_lineoff
)
1422 struct unit_addrs a
;
1424 u
= ((struct unit
*)
1425 backtrace_alloc (state
, sizeof *u
, error_callback
, data
));
1428 u
->unit_data
= unit_data
;
1429 u
->unit_data_len
= unit_data_len
;
1430 u
->unit_data_offset
= unit_data_offset
;
1431 u
->version
= version
;
1432 u
->is_dwarf64
= is_dwarf64
;
1433 u
->addrsize
= addrsize
;
1434 u
->filename
= filename
;
1435 u
->comp_dir
= comp_dir
;
1436 u
->abs_filename
= NULL
;
1437 u
->lineoff
= lineoff
;
1438 u
->abbrevs
= abbrevs
;
1439 memset (&abbrevs
, 0, sizeof abbrevs
);
1441 /* The actual line number mappings will be read as
1445 u
->function_addrs
= NULL
;
1446 u
->function_addrs_count
= 0;
1450 if (!add_unit_ranges (state
, base_address
, u
, ranges
, lowpc
,
1451 is_bigendian
, dwarf_ranges
,
1452 dwarf_ranges_size
, error_callback
, data
,
1455 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1456 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1462 if (highpc_is_relative
)
1468 if (!add_unit_addr (state
, base_address
, a
, error_callback
, data
,
1471 free_abbrevs (state
, &u
->abbrevs
, error_callback
, data
);
1472 backtrace_free (state
, u
, sizeof *u
, error_callback
, data
);
1479 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1480 memset (&abbrevs
, 0, sizeof abbrevs
);
1483 if (info
.reported_underflow
)
1489 free_abbrevs (state
, &abbrevs
, error_callback
, data
);
1490 free_unit_addrs_vector (state
, addrs
, error_callback
, data
);
1494 /* Add a new mapping to the vector of line mappings that we are
1495 building. Returns 1 on success, 0 on failure. */
1498 add_line (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1499 uintptr_t pc
, const char *filename
, int lineno
,
1500 backtrace_error_callback error_callback
, void *data
,
1501 struct line_vector
*vec
)
1505 /* If we are adding the same mapping, ignore it. This can happen
1506 when using discriminators. */
1509 ln
= (struct line
*) vec
->vec
.base
+ (vec
->count
- 1);
1510 if (pc
== ln
->pc
&& filename
== ln
->filename
&& lineno
== ln
->lineno
)
1514 ln
= ((struct line
*)
1515 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1520 /* Add in the base address here, so that we can look up the PC
1522 ln
->pc
= pc
+ ddata
->base_address
;
1524 ln
->filename
= filename
;
1525 ln
->lineno
= lineno
;
1532 /* Free the line header information. If FREE_FILENAMES is true we
1533 free the file names themselves, otherwise we leave them, as there
1534 may be line structures pointing to them. */
1537 free_line_header (struct backtrace_state
*state
, struct line_header
*hdr
,
1538 backtrace_error_callback error_callback
, void *data
)
1540 backtrace_free (state
, hdr
->dirs
, hdr
->dirs_count
* sizeof (const char *),
1541 error_callback
, data
);
1542 backtrace_free (state
, hdr
->filenames
,
1543 hdr
->filenames_count
* sizeof (char *),
1544 error_callback
, data
);
1547 /* Read the line header. Return 1 on success, 0 on failure. */
1550 read_line_header (struct backtrace_state
*state
, struct unit
*u
,
1551 int is_dwarf64
, struct dwarf_buf
*line_buf
,
1552 struct line_header
*hdr
)
1555 struct dwarf_buf hdr_buf
;
1556 const unsigned char *p
;
1557 const unsigned char *pend
;
1560 hdr
->version
= read_uint16 (line_buf
);
1561 if (hdr
->version
< 2 || hdr
->version
> 4)
1563 dwarf_buf_error (line_buf
, "unsupported line number version");
1567 hdrlen
= read_offset (line_buf
, is_dwarf64
);
1569 hdr_buf
= *line_buf
;
1570 hdr_buf
.left
= hdrlen
;
1572 if (!advance (line_buf
, hdrlen
))
1575 hdr
->min_insn_len
= read_byte (&hdr_buf
);
1576 if (hdr
->version
< 4)
1577 hdr
->max_ops_per_insn
= 1;
1579 hdr
->max_ops_per_insn
= read_byte (&hdr_buf
);
1581 /* We don't care about default_is_stmt. */
1582 read_byte (&hdr_buf
);
1584 hdr
->line_base
= read_sbyte (&hdr_buf
);
1585 hdr
->line_range
= read_byte (&hdr_buf
);
1587 hdr
->opcode_base
= read_byte (&hdr_buf
);
1588 hdr
->opcode_lengths
= hdr_buf
.buf
;
1589 if (!advance (&hdr_buf
, hdr
->opcode_base
- 1))
1592 /* Count the number of directory entries. */
1593 hdr
->dirs_count
= 0;
1595 pend
= p
+ hdr_buf
.left
;
1596 while (p
< pend
&& *p
!= '\0')
1598 p
+= strnlen((const char *) p
, pend
- p
) + 1;
1602 hdr
->dirs
= ((const char **)
1603 backtrace_alloc (state
,
1604 hdr
->dirs_count
* sizeof (const char *),
1605 line_buf
->error_callback
, line_buf
->data
));
1606 if (hdr
->dirs
== NULL
)
1610 while (*hdr_buf
.buf
!= '\0')
1612 if (hdr_buf
.reported_underflow
)
1615 hdr
->dirs
[i
] = (const char *) hdr_buf
.buf
;
1617 if (!advance (&hdr_buf
,
1618 strnlen ((const char *) hdr_buf
.buf
, hdr_buf
.left
) + 1))
1621 if (!advance (&hdr_buf
, 1))
1624 /* Count the number of file entries. */
1625 hdr
->filenames_count
= 0;
1627 pend
= p
+ hdr_buf
.left
;
1628 while (p
< pend
&& *p
!= '\0')
1630 p
+= strnlen ((const char *) p
, pend
- p
) + 1;
1631 p
+= leb128_len (p
);
1632 p
+= leb128_len (p
);
1633 p
+= leb128_len (p
);
1634 ++hdr
->filenames_count
;
1637 hdr
->filenames
= ((const char **)
1638 backtrace_alloc (state
,
1639 hdr
->filenames_count
* sizeof (char *),
1640 line_buf
->error_callback
,
1642 if (hdr
->filenames
== NULL
)
1645 while (*hdr_buf
.buf
!= '\0')
1647 const char *filename
;
1650 if (hdr_buf
.reported_underflow
)
1653 filename
= (const char *) hdr_buf
.buf
;
1654 if (!advance (&hdr_buf
,
1655 strnlen ((const char *) hdr_buf
.buf
, hdr_buf
.left
) + 1))
1657 dir_index
= read_uleb128 (&hdr_buf
);
1658 if (IS_ABSOLUTE_PATH (filename
))
1659 hdr
->filenames
[i
] = filename
;
1664 size_t filename_len
;
1669 else if (dir_index
- 1 < hdr
->dirs_count
)
1670 dir
= hdr
->dirs
[dir_index
- 1];
1673 dwarf_buf_error (line_buf
,
1674 ("invalid directory index in "
1675 "line number program header"));
1678 dir_len
= strlen (dir
);
1679 filename_len
= strlen (filename
);
1681 backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
1682 line_buf
->error_callback
, line_buf
->data
));
1685 memcpy (s
, dir
, dir_len
);
1686 /* FIXME: If we are on a DOS-based file system, and the
1687 directory or the file name use backslashes, then we
1688 should use a backslash here. */
1690 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
1691 hdr
->filenames
[i
] = s
;
1694 /* Ignore the modification time and size. */
1695 read_uleb128 (&hdr_buf
);
1696 read_uleb128 (&hdr_buf
);
1701 if (hdr_buf
.reported_underflow
)
1707 /* Read the line program, adding line mappings to VEC. Return 1 on
1708 success, 0 on failure. */
1711 read_line_program (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1712 struct unit
*u
, const struct line_header
*hdr
,
1713 struct dwarf_buf
*line_buf
, struct line_vector
*vec
)
1716 unsigned int op_index
;
1717 const char *reset_filename
;
1718 const char *filename
;
1723 if (hdr
->filenames_count
> 0)
1724 reset_filename
= hdr
->filenames
[0];
1726 reset_filename
= "";
1727 filename
= reset_filename
;
1729 while (line_buf
->left
> 0)
1733 op
= read_byte (line_buf
);
1734 if (op
>= hdr
->opcode_base
)
1736 unsigned int advance
;
1738 /* Special opcode. */
1739 op
-= hdr
->opcode_base
;
1740 advance
= op
/ hdr
->line_range
;
1741 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1742 / hdr
->max_ops_per_insn
);
1743 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1744 lineno
+= hdr
->line_base
+ (int) (op
% hdr
->line_range
);
1745 add_line (state
, ddata
, address
, filename
, lineno
,
1746 line_buf
->error_callback
, line_buf
->data
, vec
);
1748 else if (op
== DW_LNS_extended_op
)
1752 len
= read_uleb128 (line_buf
);
1753 op
= read_byte (line_buf
);
1756 case DW_LNE_end_sequence
:
1757 /* FIXME: Should we mark the high PC here? It seems
1758 that we already have that information from the
1759 compilation unit. */
1762 filename
= reset_filename
;
1765 case DW_LNE_set_address
:
1766 address
= read_address (line_buf
, u
->addrsize
);
1768 case DW_LNE_define_file
:
1771 unsigned int dir_index
;
1773 f
= (const char *) line_buf
->buf
;
1774 if (!advance (line_buf
, strnlen (f
, line_buf
->left
) + 1))
1776 dir_index
= read_uleb128 (line_buf
);
1777 /* Ignore that time and length. */
1778 read_uleb128 (line_buf
);
1779 read_uleb128 (line_buf
);
1780 if (IS_ABSOLUTE_PATH (f
))
1791 else if (dir_index
- 1 < hdr
->dirs_count
)
1792 dir
= hdr
->dirs
[dir_index
- 1];
1795 dwarf_buf_error (line_buf
,
1796 ("invalid directory index "
1797 "in line number program"));
1800 dir_len
= strlen (dir
);
1803 backtrace_alloc (state
, dir_len
+ f_len
+ 2,
1804 line_buf
->error_callback
,
1808 memcpy (p
, dir
, dir_len
);
1809 /* FIXME: If we are on a DOS-based file system,
1810 and the directory or the file name use
1811 backslashes, then we should use a backslash
1814 memcpy (p
+ dir_len
+ 1, f
, f_len
+ 1);
1819 case DW_LNE_set_discriminator
:
1820 /* We don't care about discriminators. */
1821 read_uleb128 (line_buf
);
1824 if (!advance (line_buf
, len
- 1))
1834 add_line (state
, ddata
, address
, filename
, lineno
,
1835 line_buf
->error_callback
, line_buf
->data
, vec
);
1837 case DW_LNS_advance_pc
:
1841 advance
= read_uleb128 (line_buf
);
1842 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1843 / hdr
->max_ops_per_insn
);
1844 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1847 case DW_LNS_advance_line
:
1848 lineno
+= (int) read_sleb128 (line_buf
);
1850 case DW_LNS_set_file
:
1854 fileno
= read_uleb128 (line_buf
);
1859 if (fileno
- 1 >= hdr
->filenames_count
)
1861 dwarf_buf_error (line_buf
,
1862 ("invalid file number in "
1863 "line number program"));
1866 filename
= hdr
->filenames
[fileno
- 1];
1870 case DW_LNS_set_column
:
1871 read_uleb128 (line_buf
);
1873 case DW_LNS_negate_stmt
:
1875 case DW_LNS_set_basic_block
:
1877 case DW_LNS_const_add_pc
:
1879 unsigned int advance
;
1881 op
= 255 - hdr
->opcode_base
;
1882 advance
= op
/ hdr
->line_range
;
1883 address
+= (hdr
->min_insn_len
* (op_index
+ advance
)
1884 / hdr
->max_ops_per_insn
);
1885 op_index
= (op_index
+ advance
) % hdr
->max_ops_per_insn
;
1888 case DW_LNS_fixed_advance_pc
:
1889 address
+= read_uint16 (line_buf
);
1892 case DW_LNS_set_prologue_end
:
1894 case DW_LNS_set_epilogue_begin
:
1896 case DW_LNS_set_isa
:
1897 read_uleb128 (line_buf
);
1903 for (i
= hdr
->opcode_lengths
[op
- 1]; i
> 0; --i
)
1904 read_uleb128 (line_buf
);
1914 /* Read the line number information for a compilation unit. Returns 1
1915 on success, 0 on failure. */
1918 read_line_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
1919 backtrace_error_callback error_callback
, void *data
,
1920 struct unit
*u
, struct line_header
*hdr
, struct line
**lines
,
1921 size_t *lines_count
)
1923 struct line_vector vec
;
1924 struct dwarf_buf line_buf
;
1929 memset (&vec
.vec
, 0, sizeof vec
.vec
);
1932 memset (hdr
, 0, sizeof *hdr
);
1934 if (u
->lineoff
!= (off_t
) (size_t) u
->lineoff
1935 || (size_t) u
->lineoff
>= ddata
->dwarf_line_size
)
1937 error_callback (data
, "unit line offset out of range", 0);
1941 line_buf
.name
= ".debug_line";
1942 line_buf
.start
= ddata
->dwarf_line
;
1943 line_buf
.buf
= ddata
->dwarf_line
+ u
->lineoff
;
1944 line_buf
.left
= ddata
->dwarf_line_size
- u
->lineoff
;
1945 line_buf
.is_bigendian
= ddata
->is_bigendian
;
1946 line_buf
.error_callback
= error_callback
;
1947 line_buf
.data
= data
;
1948 line_buf
.reported_underflow
= 0;
1951 len
= read_uint32 (&line_buf
);
1952 if (len
== 0xffffffff)
1954 len
= read_uint64 (&line_buf
);
1957 line_buf
.left
= len
;
1959 if (!read_line_header (state
, u
, is_dwarf64
, &line_buf
, hdr
))
1962 if (!read_line_program (state
, ddata
, u
, hdr
, &line_buf
, &vec
))
1965 if (line_buf
.reported_underflow
)
1970 /* This is not a failure in the sense of a generating an error,
1971 but it is a failure in that sense that we have no useful
1976 /* Allocate one extra entry at the end. */
1977 ln
= ((struct line
*)
1978 backtrace_vector_grow (state
, sizeof (struct line
), error_callback
,
1982 ln
->pc
= (uintptr_t) -1;
1983 ln
->filename
= NULL
;
1986 if (!backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
))
1989 ln
= (struct line
*) vec
.vec
.base
;
1990 qsort (ln
, vec
.count
, sizeof (struct line
), line_compare
);
1993 *lines_count
= vec
.count
;
1998 vec
.vec
.alc
+= vec
.vec
.size
;
2000 backtrace_vector_release (state
, &vec
.vec
, error_callback
, data
);
2001 free_line_header (state
, hdr
, error_callback
, data
);
2002 *lines
= (struct line
*) (uintptr_t) -1;
2007 /* Read the name of a function from a DIE referenced by a
2008 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
2009 the same compilation unit. */
2012 read_referenced_name (struct dwarf_data
*ddata
, struct unit
*u
,
2013 uint64_t offset
, backtrace_error_callback error_callback
,
2016 struct dwarf_buf unit_buf
;
2018 const struct abbrev
*abbrev
;
2022 /* OFFSET is from the start of the data for this compilation unit.
2023 U->unit_data is the data, but it starts U->unit_data_offset bytes
2024 from the beginning. */
2026 if (offset
< u
->unit_data_offset
2027 || offset
- u
->unit_data_offset
>= u
->unit_data_len
)
2029 error_callback (data
,
2030 "abstract origin or specification out of range",
2035 offset
-= u
->unit_data_offset
;
2037 unit_buf
.name
= ".debug_info";
2038 unit_buf
.start
= ddata
->dwarf_info
;
2039 unit_buf
.buf
= u
->unit_data
+ offset
;
2040 unit_buf
.left
= u
->unit_data_len
- offset
;
2041 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2042 unit_buf
.error_callback
= error_callback
;
2043 unit_buf
.data
= data
;
2044 unit_buf
.reported_underflow
= 0;
2046 code
= read_uleb128 (&unit_buf
);
2049 dwarf_buf_error (&unit_buf
, "invalid abstract origin or specification");
2053 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2058 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2060 struct attr_val val
;
2062 if (!read_attribute (abbrev
->attrs
[i
].form
, &unit_buf
,
2063 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2064 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2068 switch (abbrev
->attrs
[i
].name
)
2071 /* We prefer the linkage name if get one. */
2072 if (val
.encoding
== ATTR_VAL_STRING
)
2076 case DW_AT_linkage_name
:
2077 case DW_AT_MIPS_linkage_name
:
2078 if (val
.encoding
== ATTR_VAL_STRING
)
2079 return val
.u
.string
;
2082 case DW_AT_specification
:
2083 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2084 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2086 /* This refers to a specification defined in some other
2087 compilation unit. We can handle this case if we
2088 must, but it's harder. */
2091 if (val
.encoding
== ATTR_VAL_UINT
2092 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2096 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2097 error_callback
, data
);
2111 /* Add a single range to U that maps to function. Returns 1 on
2112 success, 0 on error. */
2115 add_function_range (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2116 struct function
*function
, uint64_t lowpc
, uint64_t highpc
,
2117 backtrace_error_callback error_callback
,
2118 void *data
, struct function_vector
*vec
)
2120 struct function_addrs
*p
;
2122 /* Add in the base address here, so that we can look up the PC
2124 lowpc
+= ddata
->base_address
;
2125 highpc
+= ddata
->base_address
;
2129 p
= (struct function_addrs
*) vec
->vec
.base
+ vec
->count
- 1;
2130 if ((lowpc
== p
->high
|| lowpc
== p
->high
+ 1)
2131 && function
== p
->function
)
2133 if (highpc
> p
->high
)
2139 p
= ((struct function_addrs
*)
2140 backtrace_vector_grow (state
, sizeof (struct function_addrs
),
2141 error_callback
, data
, &vec
->vec
));
2147 p
->function
= function
;
2152 /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0
2156 add_function_ranges (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2157 struct unit
*u
, struct function
*function
,
2158 uint64_t ranges
, uint64_t base
,
2159 backtrace_error_callback error_callback
, void *data
,
2160 struct function_vector
*vec
)
2162 struct dwarf_buf ranges_buf
;
2164 if (ranges
>= ddata
->dwarf_ranges_size
)
2166 error_callback (data
, "function ranges offset out of range", 0);
2170 ranges_buf
.name
= ".debug_ranges";
2171 ranges_buf
.start
= ddata
->dwarf_ranges
;
2172 ranges_buf
.buf
= ddata
->dwarf_ranges
+ ranges
;
2173 ranges_buf
.left
= ddata
->dwarf_ranges_size
- ranges
;
2174 ranges_buf
.is_bigendian
= ddata
->is_bigendian
;
2175 ranges_buf
.error_callback
= error_callback
;
2176 ranges_buf
.data
= data
;
2177 ranges_buf
.reported_underflow
= 0;
2184 if (ranges_buf
.reported_underflow
)
2187 low
= read_address (&ranges_buf
, u
->addrsize
);
2188 high
= read_address (&ranges_buf
, u
->addrsize
);
2190 if (low
== 0 && high
== 0)
2193 if (is_highest_address (low
, u
->addrsize
))
2197 if (!add_function_range (state
, ddata
, function
, low
+ base
,
2198 high
+ base
, error_callback
, data
, vec
))
2203 if (ranges_buf
.reported_underflow
)
2209 /* Read one entry plus all its children. Add function addresses to
2210 VEC. Returns 1 on success, 0 on error. */
2213 read_function_entry (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2214 struct unit
*u
, uint64_t base
, struct dwarf_buf
*unit_buf
,
2215 const struct line_header
*lhdr
,
2216 backtrace_error_callback error_callback
, void *data
,
2217 struct function_vector
*vec
)
2219 while (unit_buf
->left
> 0)
2222 const struct abbrev
*abbrev
;
2224 struct function
*function
;
2230 int highpc_is_relative
;
2234 code
= read_uleb128 (unit_buf
);
2238 abbrev
= lookup_abbrev (&u
->abbrevs
, code
, error_callback
, data
);
2242 is_function
= (abbrev
->tag
== DW_TAG_subprogram
2243 || abbrev
->tag
== DW_TAG_entry_point
2244 || abbrev
->tag
== DW_TAG_inlined_subroutine
);
2249 function
= ((struct function
*)
2250 backtrace_alloc (state
, sizeof *function
,
2251 error_callback
, data
));
2252 if (function
== NULL
)
2254 memset (function
, 0, sizeof *function
);
2261 highpc_is_relative
= 0;
2264 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
2266 struct attr_val val
;
2268 if (!read_attribute (abbrev
->attrs
[i
].form
, unit_buf
,
2269 u
->is_dwarf64
, u
->version
, u
->addrsize
,
2270 ddata
->dwarf_str
, ddata
->dwarf_str_size
,
2274 /* The compile unit sets the base address for any address
2275 ranges in the function entries. */
2276 if (abbrev
->tag
== DW_TAG_compile_unit
2277 && abbrev
->attrs
[i
].name
== DW_AT_low_pc
2278 && val
.encoding
== ATTR_VAL_ADDRESS
)
2283 switch (abbrev
->attrs
[i
].name
)
2285 case DW_AT_call_file
:
2286 if (val
.encoding
== ATTR_VAL_UINT
)
2288 if (val
.u
.uint
== 0)
2289 function
->caller_filename
= "";
2292 if (val
.u
.uint
- 1 >= lhdr
->filenames_count
)
2294 dwarf_buf_error (unit_buf
,
2295 ("invalid file number in "
2296 "DW_AT_call_file attribute"));
2299 function
->caller_filename
=
2300 lhdr
->filenames
[val
.u
.uint
- 1];
2305 case DW_AT_call_line
:
2306 if (val
.encoding
== ATTR_VAL_UINT
)
2307 function
->caller_lineno
= val
.u
.uint
;
2310 case DW_AT_abstract_origin
:
2311 case DW_AT_specification
:
2312 if (abbrev
->attrs
[i
].form
== DW_FORM_ref_addr
2313 || abbrev
->attrs
[i
].form
== DW_FORM_ref_sig8
)
2315 /* This refers to an abstract origin defined in
2316 some other compilation unit. We can handle
2317 this case if we must, but it's harder. */
2320 if (val
.encoding
== ATTR_VAL_UINT
2321 || val
.encoding
== ATTR_VAL_REF_UNIT
)
2325 name
= read_referenced_name (ddata
, u
, val
.u
.uint
,
2326 error_callback
, data
);
2328 function
->name
= name
;
2333 if (val
.encoding
== ATTR_VAL_STRING
)
2335 /* Don't override a name we found in some other
2336 way, as it will normally be more
2337 useful--e.g., this name is normally not
2339 if (function
->name
== NULL
)
2340 function
->name
= val
.u
.string
;
2344 case DW_AT_linkage_name
:
2345 case DW_AT_MIPS_linkage_name
:
2346 if (val
.encoding
== ATTR_VAL_STRING
)
2347 function
->name
= val
.u
.string
;
2351 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2359 if (val
.encoding
== ATTR_VAL_ADDRESS
)
2361 highpc
= val
.u
.uint
;
2364 else if (val
.encoding
== ATTR_VAL_UINT
)
2366 highpc
= val
.u
.uint
;
2368 highpc_is_relative
= 1;
2373 if (val
.encoding
== ATTR_VAL_UINT
2374 || val
.encoding
== ATTR_VAL_REF_SECTION
)
2376 ranges
= val
.u
.uint
;
2387 /* If we couldn't find a name for the function, we have no use
2389 if (is_function
&& function
->name
== NULL
)
2391 backtrace_free (state
, function
, sizeof *function
,
2392 error_callback
, data
);
2400 if (!add_function_ranges (state
, ddata
, u
, function
, ranges
,
2401 base
, error_callback
, data
, vec
))
2404 else if (have_lowpc
&& have_highpc
)
2406 if (highpc_is_relative
)
2408 if (!add_function_range (state
, ddata
, function
, lowpc
, highpc
,
2409 error_callback
, data
, vec
))
2414 backtrace_free (state
, function
, sizeof *function
,
2415 error_callback
, data
);
2420 if (abbrev
->has_children
)
2424 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2425 error_callback
, data
, vec
))
2430 struct function_vector fvec
;
2432 /* Gather any information for inlined functions in
2435 memset (&fvec
, 0, sizeof fvec
);
2437 if (!read_function_entry (state
, ddata
, u
, base
, unit_buf
, lhdr
,
2438 error_callback
, data
, &fvec
))
2443 struct function_addrs
*faddrs
;
2445 if (!backtrace_vector_release (state
, &fvec
.vec
,
2446 error_callback
, data
))
2449 faddrs
= (struct function_addrs
*) fvec
.vec
.base
;
2450 qsort (faddrs
, fvec
.count
,
2451 sizeof (struct function_addrs
),
2452 function_addrs_compare
);
2454 function
->function_addrs
= faddrs
;
2455 function
->function_addrs_count
= fvec
.count
;
2464 /* Read function name information for a compilation unit. We look
2465 through the whole unit looking for function tags. */
2468 read_function_info (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2469 const struct line_header
*lhdr
,
2470 backtrace_error_callback error_callback
, void *data
,
2471 struct unit
*u
, struct function_vector
*fvec
,
2472 struct function_addrs
**ret_addrs
,
2473 size_t *ret_addrs_count
)
2475 struct dwarf_buf unit_buf
;
2476 struct function_addrs
*addrs
;
2479 unit_buf
.name
= ".debug_info";
2480 unit_buf
.start
= ddata
->dwarf_info
;
2481 unit_buf
.buf
= u
->unit_data
;
2482 unit_buf
.left
= u
->unit_data_len
;
2483 unit_buf
.is_bigendian
= ddata
->is_bigendian
;
2484 unit_buf
.error_callback
= error_callback
;
2485 unit_buf
.data
= data
;
2486 unit_buf
.reported_underflow
= 0;
2488 while (unit_buf
.left
> 0)
2490 if (!read_function_entry (state
, ddata
, u
, 0, &unit_buf
, lhdr
,
2491 error_callback
, data
, fvec
))
2495 if (fvec
->count
== 0)
2498 addrs
= (struct function_addrs
*) fvec
->vec
.base
;
2499 addrs_count
= fvec
->count
;
2501 /* Finish this list of addresses, but leave the remaining space in
2502 the vector available for the next function unit. */
2503 backtrace_vector_finish (state
, &fvec
->vec
);
2506 qsort (addrs
, addrs_count
, sizeof (struct function_addrs
),
2507 function_addrs_compare
);
2510 *ret_addrs_count
= addrs_count
;
2513 /* See if PC is inlined in FUNCTION. If it is, print out the inlined
2514 information, and update FILENAME and LINENO for the caller.
2515 Returns whatever CALLBACK returns, or 0 to keep going. */
2518 report_inlined_functions (uintptr_t pc
, struct function
*function
,
2519 backtrace_full_callback callback
, void *data
,
2520 const char **filename
, int *lineno
)
2522 struct function_addrs
*function_addrs
;
2523 struct function
*inlined
;
2526 if (function
->function_addrs_count
== 0)
2529 function_addrs
= ((struct function_addrs
*)
2530 bsearch (&pc
, function
->function_addrs
,
2531 function
->function_addrs_count
,
2532 sizeof (struct function_addrs
),
2533 function_addrs_search
));
2534 if (function_addrs
== NULL
)
2537 while (((size_t) (function_addrs
- function
->function_addrs
) + 1
2538 < function
->function_addrs_count
)
2539 && pc
>= (function_addrs
+ 1)->low
2540 && pc
< (function_addrs
+ 1)->high
)
2543 /* We found an inlined call. */
2545 inlined
= function_addrs
->function
;
2547 /* Report any calls inlined into this one. */
2548 ret
= report_inlined_functions (pc
, inlined
, callback
, data
,
2553 /* Report this inlined call. */
2554 ret
= callback (data
, pc
, *filename
, *lineno
, inlined
->name
);
2558 /* Our caller will report the caller of the inlined function; tell
2559 it the appropriate filename and line number. */
2560 *filename
= inlined
->caller_filename
;
2561 *lineno
= inlined
->caller_lineno
;
2566 /* Look for a PC in the DWARF mapping for one module. On success,
2567 call CALLBACK and return whatever it returns. On error, call
2568 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
2572 dwarf_lookup_pc (struct backtrace_state
*state
, struct dwarf_data
*ddata
,
2573 uintptr_t pc
, backtrace_full_callback callback
,
2574 backtrace_error_callback error_callback
, void *data
,
2577 struct unit_addrs
*entry
;
2582 struct function_addrs
*function_addrs
;
2583 struct function
*function
;
2584 const char *filename
;
2590 /* Find an address range that includes PC. */
2591 entry
= bsearch (&pc
, ddata
->addrs
, ddata
->addrs_count
,
2592 sizeof (struct unit_addrs
), unit_addrs_search
);
2600 /* If there are multiple ranges that contain PC, use the last one,
2601 in order to produce predictable results. If we assume that all
2602 ranges are properly nested, then the last range will be the
2604 while ((size_t) (entry
- ddata
->addrs
) + 1 < ddata
->addrs_count
2605 && pc
>= (entry
+ 1)->low
2606 && pc
< (entry
+ 1)->high
)
2609 /* We need the lines, lines_count, function_addrs,
2610 function_addrs_count fields of u. If they are not set, we need
2611 to set them. When running in threaded mode, we need to allow for
2612 the possibility that some other thread is setting them
2618 /* Skip units with no useful line number information by walking
2619 backward. Useless line number information is marked by setting
2621 while (entry
> ddata
->addrs
2622 && pc
>= (entry
- 1)->low
2623 && pc
< (entry
- 1)->high
)
2625 if (state
->threaded
)
2627 /* Use __sync_bool_compare_and_swap to do a
2629 while (!__sync_bool_compare_and_swap (&u
->lines
, lines
, lines
))
2633 if (lines
!= (struct line
*) (uintptr_t) -1)
2642 /* Do a load-acquire of u->lines. */
2643 if (state
->threaded
)
2645 /* Use __sync_bool_compare_and_swap to do an atomic load. */
2646 while (!__sync_bool_compare_and_swap (&u
->lines
, lines
, lines
))
2653 size_t function_addrs_count
;
2654 struct line_header lhdr
;
2657 /* We have never read the line information for this unit. Read
2660 function_addrs
= NULL
;
2661 function_addrs_count
= 0;
2662 if (read_line_info (state
, ddata
, error_callback
, data
, entry
->u
, &lhdr
,
2665 read_function_info (state
, ddata
, &lhdr
, error_callback
, data
,
2666 entry
->u
, &ddata
->fvec
, &function_addrs
,
2667 &function_addrs_count
);
2668 free_line_header (state
, &lhdr
, error_callback
, data
);
2672 /* Atomically store the information we just read into the unit.
2673 If another thread is simultaneously writing, it presumably
2674 read the same information, and we don't care which one we
2675 wind up with; we just leak the other one. We do have to
2676 write the lines field last, so that the acquire-loads above
2677 ensure that the other fields are set. */
2679 if (!state
->threaded
)
2681 u
->lines_count
= count
;
2682 u
->function_addrs
= function_addrs
;
2683 u
->function_addrs_count
= function_addrs_count
;
2688 __sync_bool_compare_and_swap (&u
->lines_count
, 0, count
);
2689 __sync_bool_compare_and_swap (&u
->function_addrs
, NULL
,
2691 __sync_bool_compare_and_swap (&u
->function_addrs_count
, 0,
2692 function_addrs_count
);
2693 __sync_bool_compare_and_swap (&u
->lines
, NULL
, lines
);
2697 /* Now all fields of U have been initialized. */
2699 if (lines
== (struct line
*) (uintptr_t) -1)
2701 /* If reading the line number information failed in some way,
2702 try again to see if there is a better compilation unit for
2705 return dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2707 return callback (data
, pc
, NULL
, 0, NULL
);
2710 /* Search for PC within this unit. */
2712 ln
= (struct line
*) bsearch (&pc
, lines
, entry
->u
->lines_count
,
2713 sizeof (struct line
), line_search
);
2716 /* The PC is between the low_pc and high_pc attributes of the
2717 compilation unit, but no entry in the line table covers it.
2718 This implies that the start of the compilation unit has no
2719 line number information. */
2721 if (entry
->u
->abs_filename
== NULL
)
2723 const char *filename
;
2725 filename
= entry
->u
->filename
;
2726 if (filename
!= NULL
2727 && !IS_ABSOLUTE_PATH (filename
)
2728 && entry
->u
->comp_dir
!= NULL
)
2730 size_t filename_len
;
2735 filename_len
= strlen (filename
);
2736 dir
= entry
->u
->comp_dir
;
2737 dir_len
= strlen (dir
);
2738 s
= (char *) backtrace_alloc (state
, dir_len
+ filename_len
+ 2,
2739 error_callback
, data
);
2745 memcpy (s
, dir
, dir_len
);
2746 /* FIXME: Should use backslash if DOS file system. */
2748 memcpy (s
+ dir_len
+ 1, filename
, filename_len
+ 1);
2751 entry
->u
->abs_filename
= filename
;
2754 return callback (data
, pc
, entry
->u
->abs_filename
, 0, NULL
);
2757 /* Search for function name within this unit. */
2759 if (entry
->u
->function_addrs_count
== 0)
2760 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2762 function_addrs
= ((struct function_addrs
*)
2763 bsearch (&pc
, entry
->u
->function_addrs
,
2764 entry
->u
->function_addrs_count
,
2765 sizeof (struct function_addrs
),
2766 function_addrs_search
));
2767 if (function_addrs
== NULL
)
2768 return callback (data
, pc
, ln
->filename
, ln
->lineno
, NULL
);
2770 /* If there are multiple function ranges that contain PC, use the
2771 last one, in order to produce predictable results. */
2773 while (((size_t) (function_addrs
- entry
->u
->function_addrs
+ 1)
2774 < entry
->u
->function_addrs_count
)
2775 && pc
>= (function_addrs
+ 1)->low
2776 && pc
< (function_addrs
+ 1)->high
)
2779 function
= function_addrs
->function
;
2781 filename
= ln
->filename
;
2782 lineno
= ln
->lineno
;
2784 ret
= report_inlined_functions (pc
, function
, callback
, data
,
2785 &filename
, &lineno
);
2789 return callback (data
, pc
, filename
, lineno
, function
->name
);
2793 /* Return the file/line information for a PC using the DWARF mapping
2794 we built earlier. */
2797 dwarf_fileline (struct backtrace_state
*state
, uintptr_t pc
,
2798 backtrace_full_callback callback
,
2799 backtrace_error_callback error_callback
, void *data
)
2801 struct dwarf_data
*ddata
;
2805 if (!state
->threaded
)
2807 for (ddata
= (struct dwarf_data
*) state
->fileline_data
;
2809 ddata
= ddata
->next
)
2811 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2813 if (ret
!= 0 || found
)
2819 struct dwarf_data
**pp
;
2821 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
2826 while (!__sync_bool_compare_and_swap (pp
, ddata
, ddata
))
2832 ret
= dwarf_lookup_pc (state
, ddata
, pc
, callback
, error_callback
,
2834 if (ret
!= 0 || found
)
2841 /* FIXME: See if any libraries have been dlopen'ed. */
2843 return callback (data
, pc
, NULL
, 0, NULL
);
2846 /* Initialize our data structures from the DWARF debug info for a
2847 file. Return NULL on failure. */
2849 static struct dwarf_data
*
2850 build_dwarf_data (struct backtrace_state
*state
,
2851 uintptr_t base_address
,
2852 const unsigned char *dwarf_info
,
2853 size_t dwarf_info_size
,
2854 const unsigned char *dwarf_line
,
2855 size_t dwarf_line_size
,
2856 const unsigned char *dwarf_abbrev
,
2857 size_t dwarf_abbrev_size
,
2858 const unsigned char *dwarf_ranges
,
2859 size_t dwarf_ranges_size
,
2860 const unsigned char *dwarf_str
,
2861 size_t dwarf_str_size
,
2863 backtrace_error_callback error_callback
,
2866 struct unit_addrs_vector addrs_vec
;
2867 struct unit_addrs
*addrs
;
2869 struct dwarf_data
*fdata
;
2871 if (!build_address_map (state
, base_address
, dwarf_info
, dwarf_info_size
,
2872 dwarf_abbrev
, dwarf_abbrev_size
, dwarf_ranges
,
2873 dwarf_ranges_size
, dwarf_str
, dwarf_str_size
,
2874 is_bigendian
, error_callback
, data
, &addrs_vec
))
2877 if (!backtrace_vector_release (state
, &addrs_vec
.vec
, error_callback
, data
))
2879 addrs
= (struct unit_addrs
*) addrs_vec
.vec
.base
;
2880 addrs_count
= addrs_vec
.count
;
2881 qsort (addrs
, addrs_count
, sizeof (struct unit_addrs
), unit_addrs_compare
);
2883 fdata
= ((struct dwarf_data
*)
2884 backtrace_alloc (state
, sizeof (struct dwarf_data
),
2885 error_callback
, data
));
2890 fdata
->base_address
= base_address
;
2891 fdata
->addrs
= addrs
;
2892 fdata
->addrs_count
= addrs_count
;
2893 fdata
->dwarf_info
= dwarf_info
;
2894 fdata
->dwarf_info_size
= dwarf_info_size
;
2895 fdata
->dwarf_line
= dwarf_line
;
2896 fdata
->dwarf_line_size
= dwarf_line_size
;
2897 fdata
->dwarf_ranges
= dwarf_ranges
;
2898 fdata
->dwarf_ranges_size
= dwarf_ranges_size
;
2899 fdata
->dwarf_str
= dwarf_str
;
2900 fdata
->dwarf_str_size
= dwarf_str_size
;
2901 fdata
->is_bigendian
= is_bigendian
;
2902 memset (&fdata
->fvec
, 0, sizeof fdata
->fvec
);
2907 /* Build our data structures from the DWARF sections for a module.
2908 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
2912 backtrace_dwarf_add (struct backtrace_state
*state
,
2913 uintptr_t base_address
,
2914 const unsigned char *dwarf_info
,
2915 size_t dwarf_info_size
,
2916 const unsigned char *dwarf_line
,
2917 size_t dwarf_line_size
,
2918 const unsigned char *dwarf_abbrev
,
2919 size_t dwarf_abbrev_size
,
2920 const unsigned char *dwarf_ranges
,
2921 size_t dwarf_ranges_size
,
2922 const unsigned char *dwarf_str
,
2923 size_t dwarf_str_size
,
2925 backtrace_error_callback error_callback
,
2926 void *data
, fileline
*fileline_fn
)
2928 struct dwarf_data
*fdata
;
2930 fdata
= build_dwarf_data (state
, base_address
, dwarf_info
, dwarf_info_size
,
2931 dwarf_line
, dwarf_line_size
, dwarf_abbrev
,
2932 dwarf_abbrev_size
, dwarf_ranges
, dwarf_ranges_size
,
2933 dwarf_str
, dwarf_str_size
, is_bigendian
,
2934 error_callback
, data
);
2938 if (!state
->threaded
)
2940 struct dwarf_data
**pp
;
2942 for (pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
2952 struct dwarf_data
**pp
;
2954 pp
= (struct dwarf_data
**) (void *) &state
->fileline_data
;
2958 struct dwarf_data
*p
;
2962 while (!__sync_bool_compare_and_swap (pp
, p
, p
))
2971 if (__sync_bool_compare_and_swap (pp
, NULL
, fdata
))
2976 *fileline_fn
= dwarf_fileline
;