blob: 1dcc81531489bd10593786fbdbe2f09c654b1bd3 [file] [log] [blame]
Brian Silverman86497922018-02-10 19:28:39 -05001/* Interfaces for libdw.
2 Copyright (C) 2002-2010, 2013, 2014, 2016, 2018 Red Hat, Inc.
3 This file is part of elfutils.
4
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
7
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
11
12 or
13
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
17
18 or both in parallel, as here.
19
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
24
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
28
29#ifndef _LIBDW_H
30#define _LIBDW_H 1
31
32#include <gelf.h>
33#include <stdbool.h>
34#include <stddef.h>
35#include <stdint.h>
36
37/* Mode for the session. */
38typedef enum
39 {
40 DWARF_C_READ, /* Read .. */
41 DWARF_C_RDWR, /* Read and write .. */
42 DWARF_C_WRITE, /* Write .. */
43 }
44Dwarf_Cmd;
45
46
47/* Callback results. */
48enum
49{
50 DWARF_CB_OK = 0,
51 DWARF_CB_ABORT
52};
53
54
55/* Error values. */
56enum
57 {
58 DW_TAG_invalid = 0
59#define DW_TAG_invalid DW_TAG_invalid
60 };
61
62
63/* Type for offset in DWARF file. */
64typedef GElf_Off Dwarf_Off;
65
66/* Type for address in DWARF file. */
67typedef GElf_Addr Dwarf_Addr;
68
69/* Integer types. Big enough to hold any numeric value. */
70typedef GElf_Xword Dwarf_Word;
71typedef GElf_Sxword Dwarf_Sword;
72/* For the times we know we do not need that much. */
73typedef GElf_Half Dwarf_Half;
74
75
76/* DWARF abbreviation record. */
77typedef struct Dwarf_Abbrev Dwarf_Abbrev;
78
79/* Returned to show the last DIE has be returned. */
80#define DWARF_END_ABBREV ((Dwarf_Abbrev *) -1l)
81
82/* Source code line information for CU. */
83typedef struct Dwarf_Lines_s Dwarf_Lines;
84
85/* One source code line information. */
86typedef struct Dwarf_Line_s Dwarf_Line;
87
88/* Source file information. */
89typedef struct Dwarf_Files_s Dwarf_Files;
90
91/* One address range record. */
92typedef struct Dwarf_Arange_s Dwarf_Arange;
93
94/* Address ranges of a file. */
95typedef struct Dwarf_Aranges_s Dwarf_Aranges;
96
97/* CU representation. */
98struct Dwarf_CU;
99typedef struct Dwarf_CU Dwarf_CU;
100
101/* Macro information. */
102typedef struct Dwarf_Macro_s Dwarf_Macro;
103
104/* Attribute representation. */
105typedef struct
106{
107 unsigned int code;
108 unsigned int form;
109 unsigned char *valp;
110 struct Dwarf_CU *cu;
111} Dwarf_Attribute;
112
113
114/* Data block representation. */
115typedef struct
116{
117 Dwarf_Word length;
118 unsigned char *data;
119} Dwarf_Block;
120
121
122/* DIE information. */
123typedef struct
124{
125 /* The offset can be computed from the address. */
126 void *addr;
127 struct Dwarf_CU *cu;
128 Dwarf_Abbrev *abbrev;
129 // XXX We'll see what other information will be needed.
130 long int padding__;
131} Dwarf_Die;
132
133/* Returned to show the last DIE has be returned. */
134#define DWARF_END_DIE ((Dwarf_Die *) -1l)
135
136
137/* Global symbol information. */
138typedef struct
139{
140 Dwarf_Off cu_offset;
141 Dwarf_Off die_offset;
142 const char *name;
143} Dwarf_Global;
144
145
146/* One operation in a DWARF location expression.
147 A location expression is an array of these. */
148typedef struct
149{
150 uint8_t atom; /* Operation */
151 Dwarf_Word number; /* Operand */
152 Dwarf_Word number2; /* Possible second operand */
153 Dwarf_Word offset; /* Offset in location expression */
154} Dwarf_Op;
155
156
157/* This describes one Common Information Entry read from a CFI section.
158 Pointers here point into the DATA->d_buf block passed to dwarf_next_cfi. */
159typedef struct
160{
161 Dwarf_Off CIE_id; /* Always DW_CIE_ID_64 in Dwarf_CIE structures. */
162
163 /* Instruction stream describing initial state used by FDEs. If
164 we did not understand the whole augmentation string and it did
165 not use 'z', then there might be more augmentation data here
166 (and in FDEs) before the actual instructions. */
167 const uint8_t *initial_instructions;
168 const uint8_t *initial_instructions_end;
169
170 Dwarf_Word code_alignment_factor;
171 Dwarf_Sword data_alignment_factor;
172 Dwarf_Word return_address_register;
173
174 const char *augmentation; /* Augmentation string. */
175
176 /* Augmentation data, might be NULL. The size is correct only if
177 we understood the augmentation string sufficiently. */
178 const uint8_t *augmentation_data;
179 size_t augmentation_data_size;
180 size_t fde_augmentation_data_size;
181} Dwarf_CIE;
182
183/* This describes one Frame Description Entry read from a CFI section.
184 Pointers here point into the DATA->d_buf block passed to dwarf_next_cfi. */
185typedef struct
186{
187 /* Section offset of CIE this FDE refers to. This will never be
188 DW_CIE_ID_64 in an FDE. If this value is DW_CIE_ID_64, this is
189 actually a Dwarf_CIE structure. */
190 Dwarf_Off CIE_pointer;
191
192 /* We can't really decode anything further without looking up the CIE
193 and checking its augmentation string. Here follows the encoded
194 initial_location and address_range, then any augmentation data,
195 then the instruction stream. This FDE describes PC locations in
196 the byte range [initial_location, initial_location+address_range).
197 When the CIE augmentation string uses 'z', the augmentation data is
198 a DW_FORM_block (self-sized). Otherwise, when we understand the
199 augmentation string completely, fde_augmentation_data_size gives
200 the number of bytes of augmentation data before the instructions. */
201 const uint8_t *start;
202 const uint8_t *end;
203} Dwarf_FDE;
204
205/* Each entry in a CFI section is either a CIE described by Dwarf_CIE or
206 an FDE described by Dward_FDE. Check CIE_id to see which you have. */
207typedef union
208{
209 Dwarf_Off CIE_id; /* Always DW_CIE_ID_64 in Dwarf_CIE structures. */
210 Dwarf_CIE cie;
211 Dwarf_FDE fde;
212} Dwarf_CFI_Entry;
213
214/* Same as DW_CIE_ID_64 from dwarf.h to keep libdw.h independent. */
215#define LIBDW_CIE_ID 0xffffffffffffffffULL
216#define dwarf_cfi_cie_p(entry) ((entry)->cie.CIE_id == LIBDW_CIE_ID)
217
218/* Opaque type representing a frame state described by CFI. */
219typedef struct Dwarf_Frame_s Dwarf_Frame;
220
221/* Opaque type representing a CFI section found in a DWARF or ELF file. */
222typedef struct Dwarf_CFI_s Dwarf_CFI;
223
224
225/* Handle for debug sessions. */
226typedef struct Dwarf Dwarf;
227
228
229/* Out-Of-Memory handler. */
230typedef void (*__noreturn_attribute__ Dwarf_OOM) (void);
231
232
233#ifdef __cplusplus
234extern "C" {
235#endif
236
237/* Create a handle for a new debug session. */
238extern Dwarf *dwarf_begin (int fildes, Dwarf_Cmd cmd);
239
240/* Create a handle for a new debug session for an ELF file. */
241extern Dwarf *dwarf_begin_elf (Elf *elf, Dwarf_Cmd cmd, Elf_Scn *scngrp);
242
243/* Retrieve ELF descriptor used for DWARF access. */
244extern Elf *dwarf_getelf (Dwarf *dwarf);
245
246/* Retieve DWARF descriptor used for a Dwarf_Die or Dwarf_Attribute.
247 A Dwarf_Die or a Dwarf_Attribute is associated with a particular
248 Dwarf_CU handle. This function returns the DWARF descriptor for
249 that Dwarf_CU. */
250extern Dwarf *dwarf_cu_getdwarf (Dwarf_CU *cu);
251
252/* Retrieves the DWARF descriptor for debugaltlink data. Returns NULL
253 if no alternate debug data has been supplied yet. libdw will try
254 to set the alt file on first use of an alt FORM if not yet explicitly
255 provided by dwarf_setalt. */
256extern Dwarf *dwarf_getalt (Dwarf *main);
257
258/* Provides the data referenced by the .gnu_debugaltlink section. The
259 caller should check that MAIN and ALT match (i.e., they have the
260 same build ID). It is the responsibility of the caller to ensure
261 that the data referenced by ALT stays valid while it is used by
262 MAIN, until dwarf_setalt is called on MAIN with a different
263 descriptor, or dwarf_end. Must be called before inspecting DIEs
264 that might have alt FORMs. Otherwise libdw will try to set the
265 alt file itself on first use. */
266extern void dwarf_setalt (Dwarf *main, Dwarf *alt);
267
268/* Release debugging handling context. */
269extern int dwarf_end (Dwarf *dwarf);
270
271
272/* Get the data block for the .debug_info section. */
273extern Elf_Data *dwarf_getscn_info (Dwarf *dwarf);
274
275/* Read the header for the DWARF CU. */
276extern int dwarf_nextcu (Dwarf *dwarf, Dwarf_Off off, Dwarf_Off *next_off,
277 size_t *header_sizep, Dwarf_Off *abbrev_offsetp,
278 uint8_t *address_sizep, uint8_t *offset_sizep)
279 __nonnull_attribute__ (3);
280
281/* Read the header of a DWARF CU or type unit. If TYPE_SIGNATUREP is not
282 null, this reads a type unit from the .debug_types section; otherwise
283 this reads a CU from the .debug_info section. */
284extern int dwarf_next_unit (Dwarf *dwarf, Dwarf_Off off, Dwarf_Off *next_off,
285 size_t *header_sizep, Dwarf_Half *versionp,
286 Dwarf_Off *abbrev_offsetp,
287 uint8_t *address_sizep, uint8_t *offset_sizep,
288 uint64_t *type_signaturep, Dwarf_Off *type_offsetp)
289 __nonnull_attribute__ (3);
290
291
292/* Decode one DWARF CFI entry (CIE or FDE) from the raw section data.
293 The E_IDENT from the originating ELF file indicates the address
294 size and byte order used in the CFI section contained in DATA;
295 EH_FRAME_P should be true for .eh_frame format and false for
296 .debug_frame format. OFFSET is the byte position in the section
297 to start at; on return *NEXT_OFFSET is filled in with the byte
298 position immediately after this entry.
299
300 On success, returns 0 and fills in *ENTRY; use dwarf_cfi_cie_p to
301 see whether ENTRY->cie or ENTRY->fde is valid.
302
303 On errors, returns -1. Some format errors will permit safely
304 skipping to the next CFI entry though the current one is unusable.
305 In that case, *NEXT_OFF will be updated before a -1 return.
306
307 If there are no more CFI entries left in the section,
308 returns 1 and sets *NEXT_OFFSET to (Dwarf_Off) -1. */
309extern int dwarf_next_cfi (const unsigned char e_ident[],
310 Elf_Data *data, bool eh_frame_p,
311 Dwarf_Off offset, Dwarf_Off *next_offset,
312 Dwarf_CFI_Entry *entry)
313 __nonnull_attribute__ (1, 2, 5, 6);
314
315/* Use the CFI in the DWARF .debug_frame section.
316 Returns NULL if there is no such section (not an error).
317 The pointer returned can be used until dwarf_end is called on DWARF,
318 and must not be passed to dwarf_cfi_end.
319 Calling this more than once returns the same pointer. */
320extern Dwarf_CFI *dwarf_getcfi (Dwarf *dwarf);
321
322/* Use the CFI in the ELF file's exception-handling data.
323 Returns NULL if there is no such data.
324 The pointer returned can be used until elf_end is called on ELF,
325 and must be passed to dwarf_cfi_end before then.
326 Calling this more than once allocates independent data structures. */
327extern Dwarf_CFI *dwarf_getcfi_elf (Elf *elf);
328
329/* Release resources allocated by dwarf_getcfi_elf. */
330extern int dwarf_cfi_end (Dwarf_CFI *cache);
331
332
333/* Return DIE at given offset in .debug_info section. */
334extern Dwarf_Die *dwarf_offdie (Dwarf *dbg, Dwarf_Off offset,
335 Dwarf_Die *result) __nonnull_attribute__ (3);
336
337/* Return DIE at given offset in .debug_types section. */
338extern Dwarf_Die *dwarf_offdie_types (Dwarf *dbg, Dwarf_Off offset,
339 Dwarf_Die *result)
340 __nonnull_attribute__ (3);
341
342/* Return offset of DIE. */
343extern Dwarf_Off dwarf_dieoffset (Dwarf_Die *die);
344
345/* Return offset of DIE in CU. */
346extern Dwarf_Off dwarf_cuoffset (Dwarf_Die *die);
347
348/* Return CU DIE containing given DIE. */
349extern Dwarf_Die *dwarf_diecu (Dwarf_Die *die, Dwarf_Die *result,
350 uint8_t *address_sizep, uint8_t *offset_sizep)
351 __nonnull_attribute__ (2);
352
353/* Return the CU DIE and the header info associated with a Dwarf_Die
354 or Dwarf_Attribute. A Dwarf_Die or a Dwarf_Attribute is associated
355 with a particular Dwarf_CU handle. This function returns the CU or
356 type unit DIE and header information for that Dwarf_CU. The
357 returned DIE is either a compile_unit, partial_unit or type_unit.
358 If it is a type_unit, then the type signature and type offset are
359 also provided, otherwise type_offset will be set to zero. See also
360 dwarf_diecu and dwarf_next_unit. */
361extern Dwarf_Die *dwarf_cu_die (Dwarf_CU *cu, Dwarf_Die *result,
362 Dwarf_Half *versionp,
363 Dwarf_Off *abbrev_offsetp,
364 uint8_t *address_sizep,
365 uint8_t *offset_sizep,
366 uint64_t *type_signaturep,
367 Dwarf_Off *type_offsetp)
368 __nonnull_attribute__ (2);
369
370/* Return CU DIE containing given address. */
371extern Dwarf_Die *dwarf_addrdie (Dwarf *dbg, Dwarf_Addr addr,
372 Dwarf_Die *result) __nonnull_attribute__ (3);
373
374/* Return child of current DIE. */
375extern int dwarf_child (Dwarf_Die *die, Dwarf_Die *result)
376 __nonnull_attribute__ (2);
377
378/* Locates the first sibling of DIE and places it in RESULT.
379 Returns 0 if a sibling was found, -1 if something went wrong.
380 Returns 1 if no sibling could be found and, if RESULT is not
381 the same as DIE, it sets RESULT->addr to the address of the
382 (non-sibling) DIE that follows this one, or NULL if this DIE
383 was the last one in the compilation unit. */
384extern int dwarf_siblingof (Dwarf_Die *die, Dwarf_Die *result)
385 __nonnull_attribute__ (2);
386
387/* For type aliases and qualifier type DIEs, which don't modify or
388 change the structural layout of the underlying type, follow the
389 DW_AT_type attribute (recursively) and return the underlying type
390 Dwarf_Die.
391
392 Returns 0 when RESULT contains a Dwarf_Die (possibly equal to the
393 given DIE) that isn't a type alias or qualifier type. Returns 1
394 when RESULT contains a type alias or qualifier Dwarf_Die that
395 couldn't be peeled further (it doesn't have a DW_TAG_type
396 attribute). Returns -1 when an error occured.
397
398 The current DWARF specification defines one type alias tag
399 (DW_TAG_typedef) and seven modifier/qualifier type tags
400 (DW_TAG_const_type, DW_TAG_volatile_type, DW_TAG_restrict_type,
401 DW_TAG_atomic_type, DW_TAG_immutable_type, DW_TAG_packed_type and
402 DW_TAG_shared_type). This function won't peel modifier type
403 tags that change the way the underlying type is accessed such
404 as the pointer or reference type tags (DW_TAG_pointer_type,
405 DW_TAG_reference_type or DW_TAG_rvalue_reference_type).
406
407 A future version of this function might peel other alias or
408 qualifier type tags if a future DWARF version or GNU extension
409 defines other type aliases or qualifier type tags that don't modify,
410 change the structural layout or the way to access the underlying type. */
411extern int dwarf_peel_type (Dwarf_Die *die, Dwarf_Die *result)
412 __nonnull_attribute__ (2);
413
414/* Check whether the DIE has children. */
415extern int dwarf_haschildren (Dwarf_Die *die) __nonnull_attribute__ (1);
416
417/* Walks the attributes of DIE, starting at the one OFFSET bytes in,
418 calling the CALLBACK function for each one. Stops if the callback
419 function ever returns a value other than DWARF_CB_OK and returns the
420 offset of the offending attribute. If the end of the attributes
421 is reached 1 is returned. If something goes wrong -1 is returned and
422 the dwarf error number is set. */
423extern ptrdiff_t dwarf_getattrs (Dwarf_Die *die,
424 int (*callback) (Dwarf_Attribute *, void *),
425 void *arg, ptrdiff_t offset)
426 __nonnull_attribute__ (2);
427
428/* Return tag of given DIE. */
429extern int dwarf_tag (Dwarf_Die *die) __nonnull_attribute__ (1);
430
431
432/* Return specific attribute of DIE. */
433extern Dwarf_Attribute *dwarf_attr (Dwarf_Die *die, unsigned int search_name,
434 Dwarf_Attribute *result)
435 __nonnull_attribute__ (3);
436
437/* Check whether given DIE has specific attribute. */
438extern int dwarf_hasattr (Dwarf_Die *die, unsigned int search_name);
439
440/* These are the same as dwarf_attr and dwarf_hasattr, respectively,
441 but they resolve an indirect attribute through DW_AT_abstract_origin. */
442extern Dwarf_Attribute *dwarf_attr_integrate (Dwarf_Die *die,
443 unsigned int search_name,
444 Dwarf_Attribute *result)
445 __nonnull_attribute__ (3);
446extern int dwarf_hasattr_integrate (Dwarf_Die *die, unsigned int search_name);
447
448
449
450
451/* Check whether given attribute has specific form. */
452extern int dwarf_hasform (Dwarf_Attribute *attr, unsigned int search_form);
453
454/* Return attribute code of given attribute. */
455extern unsigned int dwarf_whatattr (Dwarf_Attribute *attr);
456
457/* Return form code of given attribute. */
458extern unsigned int dwarf_whatform (Dwarf_Attribute *attr);
459
460
461/* Return string associated with given attribute. */
462extern const char *dwarf_formstring (Dwarf_Attribute *attrp);
463
464/* Return unsigned constant represented by attribute. */
465extern int dwarf_formudata (Dwarf_Attribute *attr, Dwarf_Word *return_uval)
466 __nonnull_attribute__ (2);
467
468/* Return signed constant represented by attribute. */
469extern int dwarf_formsdata (Dwarf_Attribute *attr, Dwarf_Sword *return_uval)
470 __nonnull_attribute__ (2);
471
472/* Return address represented by attribute. */
473extern int dwarf_formaddr (Dwarf_Attribute *attr, Dwarf_Addr *return_addr)
474 __nonnull_attribute__ (2);
475
476/* This function is deprecated. Always use dwarf_formref_die instead.
477 Return reference offset represented by attribute. */
478extern int dwarf_formref (Dwarf_Attribute *attr, Dwarf_Off *return_offset)
479 __nonnull_attribute__ (2) __deprecated_attribute__;
480
481/* Look up the DIE in a reference-form attribute. */
482extern Dwarf_Die *dwarf_formref_die (Dwarf_Attribute *attr, Dwarf_Die *die_mem)
483 __nonnull_attribute__ (2);
484
485/* Return block represented by attribute. */
486extern int dwarf_formblock (Dwarf_Attribute *attr, Dwarf_Block *return_block)
487 __nonnull_attribute__ (2);
488
489/* Return flag represented by attribute. */
490extern int dwarf_formflag (Dwarf_Attribute *attr, bool *return_bool)
491 __nonnull_attribute__ (2);
492
493
494/* Simplified attribute value access functions. */
495
496/* Return string in name attribute of DIE. */
497extern const char *dwarf_diename (Dwarf_Die *die);
498
499/* Return high PC attribute of DIE. */
500extern int dwarf_highpc (Dwarf_Die *die, Dwarf_Addr *return_addr)
501 __nonnull_attribute__ (2);
502
503/* Return low PC attribute of DIE. */
504extern int dwarf_lowpc (Dwarf_Die *die, Dwarf_Addr *return_addr)
505 __nonnull_attribute__ (2);
506
507/* Return entry_pc or low_pc attribute of DIE. */
508extern int dwarf_entrypc (Dwarf_Die *die, Dwarf_Addr *return_addr)
509 __nonnull_attribute__ (2);
510
511/* Return 1 if DIE's lowpc/highpc or ranges attributes match the PC address,
512 0 if not, or -1 for errors. */
513extern int dwarf_haspc (Dwarf_Die *die, Dwarf_Addr pc);
514
515/* Enumerate the PC address ranges covered by this DIE, covering all
516 addresses where dwarf_haspc returns true. In the first call OFFSET
517 should be zero and *BASEP need not be initialized. Returns -1 for
518 errors, zero when there are no more address ranges to report, or a
519 nonzero OFFSET value to pass to the next call. Each subsequent call
520 must preserve *BASEP from the prior call. Successful calls fill in
521 *STARTP and *ENDP with a contiguous address range. */
522extern ptrdiff_t dwarf_ranges (Dwarf_Die *die,
523 ptrdiff_t offset, Dwarf_Addr *basep,
524 Dwarf_Addr *startp, Dwarf_Addr *endp);
525
526
527/* Return byte size attribute of DIE. */
528extern int dwarf_bytesize (Dwarf_Die *die);
529
530/* Return bit size attribute of DIE. */
531extern int dwarf_bitsize (Dwarf_Die *die);
532
533/* Return bit offset attribute of DIE. */
534extern int dwarf_bitoffset (Dwarf_Die *die);
535
536/* Return array order attribute of DIE. */
537extern int dwarf_arrayorder (Dwarf_Die *die);
538
539/* Return source language attribute of DIE. */
540extern int dwarf_srclang (Dwarf_Die *die);
541
542
543/* Get abbreviation at given offset for given DIE. */
544extern Dwarf_Abbrev *dwarf_getabbrev (Dwarf_Die *die, Dwarf_Off offset,
545 size_t *lengthp);
546
547/* Get abbreviation at given offset in .debug_abbrev section. */
548extern int dwarf_offabbrev (Dwarf *dbg, Dwarf_Off offset, size_t *lengthp,
549 Dwarf_Abbrev *abbrevp)
550 __nonnull_attribute__ (4);
551
552/* Get abbreviation code. */
553extern unsigned int dwarf_getabbrevcode (Dwarf_Abbrev *abbrev);
554
555/* Get abbreviation tag. */
556extern unsigned int dwarf_getabbrevtag (Dwarf_Abbrev *abbrev);
557
558/* Return true if abbreviation is children flag set. */
559extern int dwarf_abbrevhaschildren (Dwarf_Abbrev *abbrev);
560
561/* Get number of attributes of abbreviation. */
562extern int dwarf_getattrcnt (Dwarf_Abbrev *abbrev, size_t *attrcntp)
563 __nonnull_attribute__ (2);
564
565/* Get specific attribute of abbreviation. */
566extern int dwarf_getabbrevattr (Dwarf_Abbrev *abbrev, size_t idx,
567 unsigned int *namep, unsigned int *formp,
568 Dwarf_Off *offset);
569
570
571/* Get string from-debug_str section. */
572extern const char *dwarf_getstring (Dwarf *dbg, Dwarf_Off offset,
573 size_t *lenp);
574
575
576/* Get public symbol information. */
577extern ptrdiff_t dwarf_getpubnames (Dwarf *dbg,
578 int (*callback) (Dwarf *, Dwarf_Global *,
579 void *),
580 void *arg, ptrdiff_t offset)
581 __nonnull_attribute__ (2);
582
583
584/* Get source file information for CU. */
585extern int dwarf_getsrclines (Dwarf_Die *cudie, Dwarf_Lines **lines,
586 size_t *nlines) __nonnull_attribute__ (2, 3);
587
588/* Return one of the source lines of the CU. */
589extern Dwarf_Line *dwarf_onesrcline (Dwarf_Lines *lines, size_t idx);
590
591/* Get the file source files used in the CU. */
592extern int dwarf_getsrcfiles (Dwarf_Die *cudie, Dwarf_Files **files,
593 size_t *nfiles)
594 __nonnull_attribute__ (2);
595
596
597/* Get source for address in CU. */
598extern Dwarf_Line *dwarf_getsrc_die (Dwarf_Die *cudie, Dwarf_Addr addr);
599
600/* Get source for file and line number. */
601extern int dwarf_getsrc_file (Dwarf *dbg, const char *fname, int line, int col,
602 Dwarf_Line ***srcsp, size_t *nsrcs)
603 __nonnull_attribute__ (2, 5, 6);
604
605
606/* Return line address. */
607extern int dwarf_lineaddr (Dwarf_Line *line, Dwarf_Addr *addrp);
608
609/* Return line VLIW operation index. */
610extern int dwarf_lineop_index (Dwarf_Line *line, unsigned int *op_indexp);
611
612/* Return line number. */
613extern int dwarf_lineno (Dwarf_Line *line, int *linep)
614 __nonnull_attribute__ (2);
615
616/* Return column in line. */
617extern int dwarf_linecol (Dwarf_Line *line, int *colp)
618 __nonnull_attribute__ (2);
619
620/* Return true if record is for beginning of a statement. */
621extern int dwarf_linebeginstatement (Dwarf_Line *line, bool *flagp)
622 __nonnull_attribute__ (2);
623
624/* Return true if record is for end of sequence. */
625extern int dwarf_lineendsequence (Dwarf_Line *line, bool *flagp)
626 __nonnull_attribute__ (2);
627
628/* Return true if record is for beginning of a basic block. */
629extern int dwarf_lineblock (Dwarf_Line *line, bool *flagp)
630 __nonnull_attribute__ (2);
631
632/* Return true if record is for end of prologue. */
633extern int dwarf_lineprologueend (Dwarf_Line *line, bool *flagp)
634 __nonnull_attribute__ (2);
635
636/* Return true if record is for beginning of epilogue. */
637extern int dwarf_lineepiloguebegin (Dwarf_Line *line, bool *flagp)
638 __nonnull_attribute__ (2);
639
640/* Return instruction-set architecture in this record. */
641extern int dwarf_lineisa (Dwarf_Line *line, unsigned int *isap)
642 __nonnull_attribute__ (2);
643
644/* Return code path discriminator in this record. */
645extern int dwarf_linediscriminator (Dwarf_Line *line, unsigned int *discp)
646 __nonnull_attribute__ (2);
647
648
649/* Find line information for address. */
650extern const char *dwarf_linesrc (Dwarf_Line *line,
651 Dwarf_Word *mtime, Dwarf_Word *length);
652
653/* Return file information. */
654extern const char *dwarf_filesrc (Dwarf_Files *file, size_t idx,
655 Dwarf_Word *mtime, Dwarf_Word *length);
656
657/* Return the Dwarf_Files and index associated with the given Dwarf_Line. */
658extern int dwarf_line_file (Dwarf_Line *line,
659 Dwarf_Files **files, size_t *idx)
660 __nonnull_attribute__ (2, 3);
661
662/* Return the directory list used in the file information extracted.
663 (*RESULT)[0] is the CU's DW_AT_comp_dir value, and may be null.
664 (*RESULT)[0..*NDIRS-1] are the compile-time include directory path
665 encoded by the compiler. */
666extern int dwarf_getsrcdirs (Dwarf_Files *files,
667 const char *const **result, size_t *ndirs)
668 __nonnull_attribute__ (2, 3);
669
670
671/* Return location expression, decoded as a list of operations. */
672extern int dwarf_getlocation (Dwarf_Attribute *attr, Dwarf_Op **expr,
673 size_t *exprlen) __nonnull_attribute__ (2, 3);
674
675/* Return location expressions. If the attribute uses a location list,
676 ADDRESS selects the relevant location expressions from the list.
677 There can be multiple matches, resulting in multiple expressions to
678 return. EXPRS and EXPRLENS are parallel arrays of NLOCS slots to
679 fill in. Returns the number of locations filled in, or -1 for
680 errors. If EXPRS is a null pointer, stores nothing and returns the
681 total number of locations. A return value of zero means that the
682 location list indicated no value is accessible. */
683extern int dwarf_getlocation_addr (Dwarf_Attribute *attr, Dwarf_Addr address,
684 Dwarf_Op **exprs, size_t *exprlens,
685 size_t nlocs);
686
687/* Enumerate the locations ranges and descriptions covered by the
688 given attribute. In the first call OFFSET should be zero and
689 *BASEP need not be initialized. Returns -1 for errors, zero when
690 there are no more locations to report, or a nonzero OFFSET
691 value to pass to the next call. Each subsequent call must preserve
692 *BASEP from the prior call. Successful calls fill in *STARTP and
693 *ENDP with a contiguous address range and *EXPR with a pointer to
694 an array of operations with length *EXPRLEN. If the attribute
695 describes a single location description and not a location list the
696 first call (with OFFSET zero) will return the location description
697 in *EXPR with *STARTP set to zero and *ENDP set to minus one. */
698extern ptrdiff_t dwarf_getlocations (Dwarf_Attribute *attr,
699 ptrdiff_t offset, Dwarf_Addr *basep,
700 Dwarf_Addr *startp, Dwarf_Addr *endp,
701 Dwarf_Op **expr, size_t *exprlen);
702
703/* Return the block associated with a DW_OP_implicit_value operation.
704 The OP pointer must point into an expression that dwarf_getlocation
705 or dwarf_getlocation_addr has returned given the same ATTR. */
706extern int dwarf_getlocation_implicit_value (Dwarf_Attribute *attr,
707 const Dwarf_Op *op,
708 Dwarf_Block *return_block)
709 __nonnull_attribute__ (2, 3);
710
711/* Return the attribute indicated by a DW_OP_GNU_implicit_pointer operation.
712 The OP pointer must point into an expression that dwarf_getlocation
713 or dwarf_getlocation_addr has returned given the same ATTR.
714 The result is the DW_AT_location or DW_AT_const_value attribute
715 of the OP->number DIE. */
716extern int dwarf_getlocation_implicit_pointer (Dwarf_Attribute *attr,
717 const Dwarf_Op *op,
718 Dwarf_Attribute *result)
719 __nonnull_attribute__ (2, 3);
720
721/* Return the DIE associated with an operation such as
722 DW_OP_GNU_implicit_pointer, DW_OP_GNU_parameter_ref, DW_OP_GNU_convert,
723 DW_OP_GNU_reinterpret, DW_OP_GNU_const_type, DW_OP_GNU_regval_type or
724 DW_OP_GNU_deref_type. The OP pointer must point into an expression that
725 dwarf_getlocation or dwarf_getlocation_addr has returned given the same
726 ATTR. The RESULT is a DIE that expresses a type or value needed by the
727 given OP. */
728extern int dwarf_getlocation_die (Dwarf_Attribute *attr,
729 const Dwarf_Op *op,
730 Dwarf_Die *result)
731 __nonnull_attribute__ (2, 3);
732
733/* Return the attribute expressing a value associated with an operation such
734 as DW_OP_implicit_value, DW_OP_GNU_entry_value or DW_OP_GNU_const_type.
735 The OP pointer must point into an expression that dwarf_getlocation
736 or dwarf_getlocation_addr has returned given the same ATTR.
737 The RESULT is a value expressed by an attribute such as DW_AT_location
738 or DW_AT_const_value. */
739extern int dwarf_getlocation_attr (Dwarf_Attribute *attr,
740 const Dwarf_Op *op,
741 Dwarf_Attribute *result)
742 __nonnull_attribute__ (2, 3);
743
744
745/* Compute the byte-size of a type DIE according to DWARF rules.
746 For most types, this is just DW_AT_byte_size.
747 For DW_TAG_array_type it can apply much more complex rules. */
748extern int dwarf_aggregate_size (Dwarf_Die *die, Dwarf_Word *size);
749
750/* Given a language code, as returned by dwarf_srclan, get the default
751 lower bound for a subrange type without a lower bound attribute.
752 Returns zero on success or -1 on failure when the given language
753 wasn't recognized. */
754extern int dwarf_default_lower_bound (int lang, Dwarf_Sword *result)
755 __nonnull_attribute__ (2);
756
757/* Return scope DIEs containing PC address.
758 Sets *SCOPES to a malloc'd array of Dwarf_Die structures,
759 and returns the number of elements in the array.
760 (*SCOPES)[0] is the DIE for the innermost scope containing PC,
761 (*SCOPES)[1] is the DIE for the scope containing that scope, and so on.
762 Returns -1 for errors or 0 if no scopes match PC. */
763extern int dwarf_getscopes (Dwarf_Die *cudie, Dwarf_Addr pc,
764 Dwarf_Die **scopes);
765
766/* Return scope DIEs containing the given DIE.
767 Sets *SCOPES to a malloc'd array of Dwarf_Die structures,
768 and returns the number of elements in the array.
769 (*SCOPES)[0] is a copy of DIE.
770 (*SCOPES)[1] is the DIE for the scope containing that scope, and so on.
771 Returns -1 for errors or 0 if DIE is not found in any scope entry. */
772extern int dwarf_getscopes_die (Dwarf_Die *die, Dwarf_Die **scopes);
773
774
775/* Search SCOPES[0..NSCOPES-1] for a variable called NAME.
776 Ignore the first SKIP_SHADOWS scopes that match the name.
777 If MATCH_FILE is not null, accept only declaration in that source file;
778 if MATCH_LINENO or MATCH_LINECOL are also nonzero, accept only declaration
779 at that line and column.
780
781 If successful, fill in *RESULT with the DIE of the variable found,
782 and return N where SCOPES[N] is the scope defining the variable.
783 Return -1 for errors or -2 for no matching variable found. */
784extern int dwarf_getscopevar (Dwarf_Die *scopes, int nscopes,
785 const char *name, int skip_shadows,
786 const char *match_file,
787 int match_lineno, int match_linecol,
788 Dwarf_Die *result);
789
790
791
792/* Return list address ranges. */
793extern int dwarf_getaranges (Dwarf *dbg, Dwarf_Aranges **aranges,
794 size_t *naranges)
795 __nonnull_attribute__ (2);
796
797/* Return one of the address range entries. */
798extern Dwarf_Arange *dwarf_onearange (Dwarf_Aranges *aranges, size_t idx);
799
800/* Return information in address range record. */
801extern int dwarf_getarangeinfo (Dwarf_Arange *arange, Dwarf_Addr *addrp,
802 Dwarf_Word *lengthp, Dwarf_Off *offsetp);
803
804/* Get address range which includes given address. */
805extern Dwarf_Arange *dwarf_getarange_addr (Dwarf_Aranges *aranges,
806 Dwarf_Addr addr);
807
808
809
810/* Get functions in CUDIE. The given callback will be called for all
811 defining DW_TAG_subprograms in the CU DIE tree. If the callback
812 returns DWARF_CB_ABORT the return value can be used as offset argument
813 to resume the function to find all remaining functions (this is not
814 really recommended, since it needs to rewalk the CU DIE tree first till
815 that offset is found again). If the callback returns DWARF_CB_OK
816 dwarf_getfuncs will not return but keep calling the callback for each
817 function DIE it finds. Pass zero for offset on the first call to walk
818 the full CU DIE tree. If no more functions can be found and the callback
819 returned DWARF_CB_OK then the function returns zero. */
820extern ptrdiff_t dwarf_getfuncs (Dwarf_Die *cudie,
821 int (*callback) (Dwarf_Die *, void *),
822 void *arg, ptrdiff_t offset);
823
824
825/* Return file name containing definition of the given declaration. */
826extern const char *dwarf_decl_file (Dwarf_Die *decl);
827
828/* Get line number of beginning of given declaration. */
829extern int dwarf_decl_line (Dwarf_Die *decl, int *linep)
830 __nonnull_attribute__ (2);
831
832/* Get column number of beginning of given declaration. */
833extern int dwarf_decl_column (Dwarf_Die *decl, int *colp)
834 __nonnull_attribute__ (2);
835
836
837/* Return nonzero if given function is an abstract inline definition. */
838extern int dwarf_func_inline (Dwarf_Die *func);
839
840/* Find each concrete inlined instance of the abstract inline definition. */
841extern int dwarf_func_inline_instances (Dwarf_Die *func,
842 int (*callback) (Dwarf_Die *, void *),
843 void *arg);
844
845
846/* Find the appropriate PC location or locations for function entry
847 breakpoints for the given DW_TAG_subprogram DIE. Returns -1 for errors.
848 On success, returns the number of breakpoint locations (never zero)
849 and sets *BKPTS to a malloc'd vector of addresses. */
850extern int dwarf_entry_breakpoints (Dwarf_Die *die, Dwarf_Addr **bkpts);
851
852
853/* Iterate through the macro unit referenced by CUDIE and call
854 CALLBACK for each macro information entry. To start the iteration,
855 one would pass DWARF_GETMACROS_START for TOKEN.
856
857 The iteration continues while CALLBACK returns DWARF_CB_OK. If the
858 callback returns DWARF_CB_ABORT, the iteration stops and a
859 continuation token is returned, which can be used to restart the
860 iteration at the point where it ended. Returns -1 for errors or 0
861 if there are no more macro entries.
862
863 Note that the Dwarf_Macro pointer passed to the callback is only
864 valid for the duration of the callback invocation.
865
866 For backward compatibility, a token of 0 is accepted for starting
867 the iteration as well, but in that case this interface will refuse
868 to serve opcode 0xff from .debug_macro sections. Such opcode would
869 be considered invalid and would cause dwarf_getmacros to return
870 with error. */
871#define DWARF_GETMACROS_START PTRDIFF_MIN
872extern ptrdiff_t dwarf_getmacros (Dwarf_Die *cudie,
873 int (*callback) (Dwarf_Macro *, void *),
874 void *arg, ptrdiff_t token)
875 __nonnull_attribute__ (2);
876
877/* This is similar in operation to dwarf_getmacros, but selects the
878 unit to iterate through by offset instead of by CU, and always
879 iterates .debug_macro. This can be used for handling
880 DW_MACRO_GNU_transparent_include's or similar opcodes.
881
882 TOKEN value of DWARF_GETMACROS_START can be used to start the
883 iteration.
884
885 It is not appropriate to obtain macro unit offset by hand from a CU
886 DIE and then request iteration through this interface. The reason
887 for this is that if a dwarf_macro_getsrcfiles is later called,
888 there would be no way to figure out what DW_AT_comp_dir was present
889 on the CU DIE, and file names referenced in either the macro unit
890 itself, or the .debug_line unit that it references, might be wrong.
891 Use dwarf_getmacros. */
892extern ptrdiff_t dwarf_getmacros_off (Dwarf *dbg, Dwarf_Off macoff,
893 int (*callback) (Dwarf_Macro *, void *),
894 void *arg, ptrdiff_t token)
895 __nonnull_attribute__ (3);
896
897/* Get the source files used by the macro entry. You shouldn't assume
898 that Dwarf_Files references will remain valid after MACRO becomes
899 invalid. (Which is to say it's only valid within the
900 dwarf_getmacros* callback.) Returns 0 for success or a negative
901 value in case of an error. */
902extern int dwarf_macro_getsrcfiles (Dwarf *dbg, Dwarf_Macro *macro,
903 Dwarf_Files **files, size_t *nfiles)
904 __nonnull_attribute__ (2, 3, 4);
905
906/* Return macro opcode. That's a constant that can be either from
907 DW_MACINFO_* domain or DW_MACRO_GNU_* domain. The two domains have
908 compatible values, so it's OK to use either of them for
909 comparisons. The only differences is 0xff, which could be either
910 DW_MACINFO_vendor_ext or a vendor-defined DW_MACRO_* constant. One
911 would need to look if the CU DIE which the iteration was requested
912 for has attribute DW_AT_macro_info, or either of DW_AT_GNU_macros
913 or DW_AT_macros to differentiate the two interpretations. */
914extern int dwarf_macro_opcode (Dwarf_Macro *macro, unsigned int *opcodep)
915 __nonnull_attribute__ (2);
916
917/* Get number of parameters of MACRO and store it to *PARAMCNTP. */
918extern int dwarf_macro_getparamcnt (Dwarf_Macro *macro, size_t *paramcntp);
919
920/* Get IDX-th parameter of MACRO (numbered from zero), and stores it
921 to *ATTRIBUTE. Returns 0 on success or -1 for errors.
922
923 After a successful call, you can query ATTRIBUTE by dwarf_whatform
924 to determine which of the dwarf_formX calls to make to get actual
925 value out of ATTRIBUTE. Note that calling dwarf_whatattr is not
926 meaningful for pseudo-attributes formed this way. */
927extern int dwarf_macro_param (Dwarf_Macro *macro, size_t idx,
928 Dwarf_Attribute *attribute);
929
930/* Return macro parameter with index 0. This will return -1 if the
931 parameter is not an integral value. Use dwarf_macro_param for more
932 general access. */
933extern int dwarf_macro_param1 (Dwarf_Macro *macro, Dwarf_Word *paramp)
934 __nonnull_attribute__ (2);
935
936/* Return macro parameter with index 1. This will return -1 if the
937 parameter is not an integral or string value. Use
938 dwarf_macro_param for more general access. */
939extern int dwarf_macro_param2 (Dwarf_Macro *macro, Dwarf_Word *paramp,
940 const char **strp);
941
942/* Compute what's known about a call frame when the PC is at ADDRESS.
943 Returns 0 for success or -1 for errors.
944 On success, *FRAME is a malloc'd pointer. */
945extern int dwarf_cfi_addrframe (Dwarf_CFI *cache,
946 Dwarf_Addr address, Dwarf_Frame **frame)
947 __nonnull_attribute__ (3);
948
949/* Return the DWARF register number used in FRAME to denote
950 the return address in FRAME's caller frame. The remaining
951 arguments can be non-null to fill in more information.
952
953 Fill [*START, *END) with the PC range to which FRAME's information applies.
954 Fill in *SIGNALP to indicate whether this is a signal-handling frame.
955 If true, this is the implicit call frame that calls a signal handler.
956 This frame's "caller" is actually the interrupted state, not a call;
957 its return address is an exact PC, not a PC after a call instruction. */
958extern int dwarf_frame_info (Dwarf_Frame *frame,
959 Dwarf_Addr *start, Dwarf_Addr *end, bool *signalp);
960
961/* Return a DWARF expression that yields the Canonical Frame Address at
962 this frame state. Returns -1 for errors, or zero for success, with
963 *NOPS set to the number of operations stored at *OPS. That pointer
964 can be used only as long as FRAME is alive and unchanged. *NOPS is
965 zero if the CFA cannot be determined here. Note that if nonempty,
966 *OPS is a DWARF expression, not a location description--append
967 DW_OP_stack_value to a get a location description for the CFA. */
968extern int dwarf_frame_cfa (Dwarf_Frame *frame, Dwarf_Op **ops, size_t *nops)
969 __nonnull_attribute__ (2);
970
971/* Deliver a DWARF location description that yields the location or
972 value of DWARF register number REGNO in the state described by FRAME.
973
974 Returns -1 for errors or zero for success, setting *NOPS to the
975 number of operations in the array stored at *OPS. Note the last
976 operation is DW_OP_stack_value if there is no mutable location but
977 only a computable value.
978
979 *NOPS zero with *OPS set to OPS_MEM means CFI says the caller's
980 REGNO is "undefined", i.e. it's call-clobbered and cannot be recovered.
981
982 *NOPS zero with *OPS set to a null pointer means CFI says the
983 caller's REGNO is "same_value", i.e. this frame did not change it;
984 ask the caller frame where to find it.
985
986 For common simple expressions *OPS is OPS_MEM. For arbitrary DWARF
987 expressions in the CFI, *OPS is an internal pointer that can be used as
988 long as the Dwarf_CFI used to create FRAME remains alive. */
989extern int dwarf_frame_register (Dwarf_Frame *frame, int regno,
990 Dwarf_Op ops_mem[3],
991 Dwarf_Op **ops, size_t *nops)
992 __nonnull_attribute__ (3, 4, 5);
993
994
995/* Return error code of last failing function call. This value is kept
996 separately for each thread. */
997extern int dwarf_errno (void);
998
999/* Return error string for ERROR. If ERROR is zero, return error string
1000 for most recent error or NULL is none occurred. If ERROR is -1 the
1001 behaviour is similar to the last case except that not NULL but a legal
1002 string is returned. */
1003extern const char *dwarf_errmsg (int err);
1004
1005
1006/* Register new Out-Of-Memory handler. The old handler is returned. */
1007extern Dwarf_OOM dwarf_new_oom_handler (Dwarf *dbg, Dwarf_OOM handler);
1008
1009
1010/* Inline optimizations. */
1011#ifdef __OPTIMIZE__
1012/* Return attribute code of given attribute. */
1013__libdw_extern_inline unsigned int
1014dwarf_whatattr (Dwarf_Attribute *attr)
1015{
1016 return attr == NULL ? 0 : attr->code;
1017}
1018
1019/* Return attribute code of given attribute. */
1020__libdw_extern_inline unsigned int
1021dwarf_whatform (Dwarf_Attribute *attr)
1022{
1023 return attr == NULL ? 0 : attr->form;
1024}
1025#endif /* Optimize. */
1026
1027#ifdef __cplusplus
1028}
1029#endif
1030
1031#endif /* libdw.h */