blob: 786be22abd7e86cc7cb8a39ab740b259636b416b [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Interfaces for libdw.
2 Copyright (C) 2002, 2004, 2005 Red Hat, Inc.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5 This program is Open Source software; you can redistribute it and/or
6 modify it under the terms of the Open Software License version 1.0 as
7 published by the Open Source Initiative.
8
9 You should have received a copy of the Open Software License along
10 with this program; if not, you may obtain a copy of the Open Software
11 License version 1.0 from http://www.opensource.org/licenses/osl.php or
12 by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
13 3001 King Ranch Road, Ukiah, CA 95482. */
14
15#ifndef _LIBDW_H
16#define _LIBDW_H 1
17
18#include <gelf.h>
19#include <stdbool.h>
20#include <stddef.h>
21
22
23#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
24# define __nonnull_attribute__(...) __attribute__ ((__nonnull__ (__VA_ARGS__)))
25#else
26# define __nonnull_attribute__(args...)
27#endif
28
29
30/* Mode for the session. */
31typedef enum
32 {
33 DWARF_C_READ, /* Read .. */
34 DWARF_C_RDWR, /* Read and write .. */
35 DWARF_C_WRITE, /* Write .. */
36 }
37Dwarf_Cmd;
38
39
40/* Callback results. */
41enum
42{
43 DWARF_CB_OK = 0,
44 DWARF_CB_ABORT
45};
46
47
48/* Error values. */
49enum
50 {
51 DW_TAG_invalid = 0
52#define DW_TAG_invalid DW_TAG_invalid
53 };
54
55
56/* Type for offset in DWARF file. */
57typedef GElf_Off Dwarf_Off;
58
59/* Type for address in DWARF file. */
60typedef GElf_Addr Dwarf_Addr;
61
62/* Integer types. Big enough to hold any numeric value. */
63typedef GElf_Xword Dwarf_Word;
64typedef GElf_Sxword Dwarf_Sword;
65/* For the times we know we do not need that much. */
66typedef GElf_Half Dwarf_Half;
67
68
69/* DWARF abbreviation record. */
70typedef struct Dwarf_Abbrev Dwarf_Abbrev;
71
72/* Returned to show the last DIE has be returned. */
73#define DWARF_END_ABBREV ((Dwarf_Abbrev *) -1l)
74
75/* Source code line information for CU. */
76typedef struct Dwarf_Lines_s Dwarf_Lines;
77
78/* One source code line information. */
79typedef struct Dwarf_Line_s Dwarf_Line;
80
81/* Source file information. */
82typedef struct Dwarf_Files_s Dwarf_Files;
83
84/* One address range record. */
85typedef struct Dwarf_Arange_s Dwarf_Arange;
86
87/* Address ranges of a file. */
88typedef struct Dwarf_Aranges_s Dwarf_Aranges;
89
90/* CU representation. */
91struct Dwarf_CU;
92
93/* Function information. */
94typedef struct Dwarf_Func_s Dwarf_Func;
95
96/* Macro information. */
97typedef struct Dwarf_Macro_s Dwarf_Macro;
98
99/* Attribute representation. */
100typedef struct
101{
102 unsigned int code;
103 unsigned int form;
104 unsigned char *valp;
105 struct Dwarf_CU *cu;
106} Dwarf_Attribute;
107
108
109/* Data block representation. */
110typedef struct
111{
112 Dwarf_Word length;
113 unsigned char *data;
114} Dwarf_Block;
115
116
117/* DIE information. */
118typedef struct
119{
120 /* The offset can be computed from the address. */
121 void *addr;
122 struct Dwarf_CU *cu;
123 Dwarf_Abbrev *abbrev;
124 // XXX We'll see what other information will be needed.
125 long int padding__;
126} Dwarf_Die;
127
128/* Returned to show the last DIE has be returned. */
129#define DWARF_END_DIE ((Dwarf_Die *) -1l)
130
131
132/* Global symbol information. */
133typedef struct
134{
135 Dwarf_Off cu_offset;
136 Dwarf_Off die_offset;
137 const char *name;
138} Dwarf_Global;
139
140
141// XXX It remains to be seen whether the next two need to be exported.
142/* Location record. */
143typedef struct
144{
145 uint8_t atom; /* Operation */
146 Dwarf_Word number; /* Operand */
147 Dwarf_Word number2; /* Possible second operand */
148 Dwarf_Word offset; /* Offset in location expression */
149} Dwarf_Loc;
150
151
152/* Handle for debug sessions. */
153typedef struct Dwarf Dwarf;
154
155
156/* Out-Of-Memory handler. */
157#if __GNUC__ < 4
158typedef void (*Dwarf_OOM) (void);
159#else
160typedef void (*__attribute__ ((noreturn)) Dwarf_OOM) (void);
161#endif
162
163
164/* Create a handle for a new debug session. */
165extern Dwarf *dwarf_begin (int fildes, Dwarf_Cmd cmd);
166
167/* Create a handle for a new debug session for an ELF file. */
168extern Dwarf *dwarf_begin_elf (Elf *elf, Dwarf_Cmd cmd, Elf_Scn *scngrp);
169
170/* Retrieve ELF descriptor used for DWARF access. */
171extern Elf *dwarf_getelf (Dwarf *dwarf);
172
173/* Release debugging handling context. */
174extern int dwarf_end (Dwarf *dwarf);
175
176
177/* Get the data block for the .debug_info section. */
178extern Elf_Data *dwarf_getscn_info (Dwarf *dwarf);
179
180/* Read the header for the DWARF CU header. */
181extern int dwarf_nextcu (Dwarf *dwarf, Dwarf_Off off, Dwarf_Off *next_off,
182 size_t *header_sizep, Dwarf_Off *abbrev_offsetp,
183 uint8_t *address_sizep, uint8_t *offset_sizep)
184 __nonnull_attribute__ (3);
185
186
187/* Return DIE at given offset. */
188extern Dwarf_Die *dwarf_offdie (Dwarf *dbg, Dwarf_Off offset,
189 Dwarf_Die *result) __nonnull_attribute__ (3);
190
191/* Return offset of DIE. */
192extern Dwarf_Off dwarf_dieoffset (Dwarf_Die *die);
193
194/* Return offset of DIE in CU. */
195extern Dwarf_Off dwarf_cuoffset (Dwarf_Die *die);
196
197/* Return CU DIE containing given address. */
198extern Dwarf_Die *dwarf_addrdie (Dwarf *dbg, Dwarf_Addr addr,
199 Dwarf_Die *result) __nonnull_attribute__ (3);
200
201/* Return child of current DIE. */
202extern int dwarf_child (Dwarf_Die *die, Dwarf_Die *result)
203 __nonnull_attribute__ (2);
204
205/* Return sibling of given DIE. */
206extern int dwarf_siblingof (Dwarf_Die *die, Dwarf_Die *result)
207 __nonnull_attribute__ (2);
208
209/* Check whether the DIE has children. */
210extern int dwarf_haschildren (Dwarf_Die *die);
211
212/* Get attributes of the DIE. */
213extern ptrdiff_t dwarf_getattrs (Dwarf_Die *die,
214 int (*callback) (Dwarf_Attribute *, void *),
215 void *arg, ptrdiff_t offset);
216
217/* Return tag of given DIE. */
218extern int dwarf_tag (Dwarf_Die *die);
219
220
221/* Return specific attribute of DIE. */
222extern Dwarf_Attribute *dwarf_attr (Dwarf_Die *die, unsigned int search_name,
223 Dwarf_Attribute *result)
224 __nonnull_attribute__ (3);
225
226/* Check whether given DIE has specific attribute. */
227extern int dwarf_hasattr (Dwarf_Die *die, unsigned int search_name);
228
229/* These are the same as dwarf_attr and dwarf_hasattr, respectively,
230 but they resolve an indirect attribute through DW_AT_abstract_origin. */
231extern Dwarf_Attribute *dwarf_attr_integrate (Dwarf_Die *die,
232 unsigned int search_name,
233 Dwarf_Attribute *result)
234 __nonnull_attribute__ (3);
235extern int dwarf_hasattr_integrate (Dwarf_Die *die, unsigned int search_name);
236
237
238
239
240/* Check whether given attribute has specific form. */
241extern int dwarf_hasform (Dwarf_Attribute *attr, unsigned int search_form);
242
243/* Return attribute code of given attribute. */
244extern unsigned int dwarf_whatattr (Dwarf_Attribute *attr);
245
246/* Return form code of given attribute. */
247extern unsigned int dwarf_whatform (Dwarf_Attribute *attr);
248
249
250/* Return string associated with given attribute. */
251extern const char *dwarf_formstring (Dwarf_Attribute *attrp);
252
253/* Return unsigned constant represented by attribute. */
254extern int dwarf_formudata (Dwarf_Attribute *attr, Dwarf_Word *return_uval)
255 __nonnull_attribute__ (2);
256
257/* Return signed constant represented by attribute. */
258extern int dwarf_formsdata (Dwarf_Attribute *attr, Dwarf_Sword *return_uval)
259 __nonnull_attribute__ (2);
260
261/* Return address represented by attribute. */
262extern int dwarf_formaddr (Dwarf_Attribute *attr, Dwarf_Addr *return_addr)
263 __nonnull_attribute__ (2);
264
265/* Return reference offset represented by attribute. */
266extern int dwarf_formref (Dwarf_Attribute *attr, Dwarf_Off *return_offset)
267 __nonnull_attribute__ (2);
268
269/* Look up the DIE in a reference-form attribute. */
270extern Dwarf_Die *dwarf_formref_die (Dwarf_Attribute *attr, Dwarf_Die *die_mem)
271 __nonnull_attribute__ (2);
272
273/* Return block represented by attribute. */
274extern int dwarf_formblock (Dwarf_Attribute *attr, Dwarf_Block *return_block)
275 __nonnull_attribute__ (2);
276
277/* Return flag represented by attribute. */
278extern int dwarf_formflag (Dwarf_Attribute *attr, bool *return_bool)
279 __nonnull_attribute__ (2);
280
281
282/* Simplified attribute value access functions. */
283
284/* Return string in name attribute of DIE. */
285extern const char *dwarf_diename (Dwarf_Die *die);
286
287/* Return high PC attribute of DIE. */
288extern int dwarf_highpc (Dwarf_Die *die, Dwarf_Addr *return_addr)
289 __nonnull_attribute__ (2);
290
291/* Return low PC attribute of DIE. */
292extern int dwarf_lowpc (Dwarf_Die *die, Dwarf_Addr *return_addr)
293 __nonnull_attribute__ (2);
294
295/* Return 1 if DIE's lowpc/highpc or ranges attributes match the PC address,
296 0 if not, or -1 for errors. */
297extern int dwarf_haspc (Dwarf_Die *die, Dwarf_Addr pc);
298
299/* Return byte size attribute of DIE. */
300extern int dwarf_bytesize (Dwarf_Die *die);
301
302/* Return bit size attribute of DIE. */
303extern int dwarf_bitsize (Dwarf_Die *die);
304
305/* Return bit offset attribute of DIE. */
306extern int dwarf_bitoffset (Dwarf_Die *die);
307
308/* Return array order attribute of DIE. */
309extern int dwarf_arrayorder (Dwarf_Die *die);
310
311/* Return source language attribute of DIE. */
312extern int dwarf_srclang (Dwarf_Die *die);
313
314
315/* Get abbreviation at given offset for given DIE. */
316extern Dwarf_Abbrev *dwarf_getabbrev (Dwarf_Die *die, Dwarf_Off offset,
317 size_t *lengthp);
318
319/* Get abbreviation at given offset in .debug_abbrev section. */
320extern int dwarf_offabbrev (Dwarf *dbg, Dwarf_Off offset, size_t *lengthp,
321 Dwarf_Abbrev *abbrevp)
322 __nonnull_attribute__ (4);
323
324/* Get abbreviation code. */
325extern unsigned int dwarf_getabbrevcode (Dwarf_Abbrev *abbrev);
326
327/* Get abbreviation tag. */
328extern unsigned int dwarf_getabbrevtag (Dwarf_Abbrev *abbrev);
329
330/* Return true if abbreviation is children flag set. */
331extern int dwarf_abbrevhaschildren (Dwarf_Abbrev *abbrev);
332
333/* Get number of attributes of abbreviation. */
334extern int dwarf_getattrcnt (Dwarf_Abbrev *abbrev, size_t *attrcntp)
335 __nonnull_attribute__ (2);
336
337/* Get specific attribute of abbreviation. */
338extern int dwarf_getabbrevattr (Dwarf_Abbrev *abbrev, size_t idx,
339 unsigned int *namep, unsigned int *formp,
340 Dwarf_Off *offset);
341
342
343/* Get string from-debug_str section. */
344extern const char *dwarf_getstring (Dwarf *dbg, Dwarf_Off offset,
345 size_t *lenp);
346
347
348/* Get public symbol information. */
349extern ptrdiff_t dwarf_getpubnames (Dwarf *dbg,
350 int (*callback) (Dwarf *, Dwarf_Global *,
351 void *),
352 void *arg, ptrdiff_t offset)
353 __nonnull_attribute__ (2);
354
355
356/* Get source file information for CU. */
357extern int dwarf_getsrclines (Dwarf_Die *cudie, Dwarf_Lines **lines,
358 size_t *nlines) __nonnull_attribute__ (2, 3);
359
360/* Return one of the source lines of the CU. */
361extern Dwarf_Line *dwarf_onesrcline (Dwarf_Lines *lines, size_t idx);
362
363/* Get the file source files used in the CU. */
364extern int dwarf_getsrcfiles (Dwarf_Die *cudie, Dwarf_Files **files,
365 size_t *nfiles)
366 __nonnull_attribute__ (2);
367
368
369/* Get source for address in CU. */
370extern Dwarf_Line *dwarf_getsrc_die (Dwarf_Die *cudie, Dwarf_Addr addr);
371
372/* Get source for file and line number. */
373extern int dwarf_getsrc_file (Dwarf *dbg, const char *fname, int line, int col,
374 Dwarf_Line ***srcsp, size_t *nsrcs)
375 __nonnull_attribute__ (2, 5, 6);
376
377
378/* Return line address. */
379extern int dwarf_lineaddr (Dwarf_Line *line, Dwarf_Addr *addrp);
380
381/* Return line number. */
382extern int dwarf_lineno (Dwarf_Line *line, int *linep)
383 __nonnull_attribute__ (2);
384
385/* Return column in line. */
386extern int dwarf_linecol (Dwarf_Line *line, int *colp)
387 __nonnull_attribute__ (2);
388
389/* Return true if record is for beginning of a statement. */
390extern int dwarf_linebeginstatement (Dwarf_Line *line, bool *flagp)
391 __nonnull_attribute__ (2);
392
393/* Return true if record is for end of sequence. */
394extern int dwarf_lineendsequence (Dwarf_Line *line, bool *flagp)
395 __nonnull_attribute__ (2);
396
397/* Return true if record is for beginning of a basic block. */
398extern int dwarf_lineblock (Dwarf_Line *line, bool *flagp)
399 __nonnull_attribute__ (2);
400
401/* Return true if record is for end of prologue. */
402extern int dwarf_lineprologueend (Dwarf_Line *line, bool *flagp)
403 __nonnull_attribute__ (2);
404
405/* Return true if record is for beginning of epilogue. */
406extern int dwarf_lineepiloguebegin (Dwarf_Line *line, bool *flagp)
407 __nonnull_attribute__ (2);
408
409
410/* Find line information for address. */
411extern const char *dwarf_linesrc (Dwarf_Line *line,
412 Dwarf_Word *mtime, Dwarf_Word *length);
413
414/* Return file information. */
415extern const char *dwarf_filesrc (Dwarf_Files *file, size_t idx,
416 Dwarf_Word *mtime, Dwarf_Word *length);
417
418
419/* Return location expression list. */
420extern int dwarf_getloclist (Dwarf_Attribute *attr, Dwarf_Loc **llbuf,
421 size_t *listlen) __nonnull_attribute__ (2, 3);
422
423/* Return location expression lists. If the attribute uses a location
424 list, ADDRESS selects the relevant location expressions from the list.
425 There can be multiple matches, resulting in multiple expressions to
426 return. LLBUFS and LISTLENS are parallel arrays of NLOCS slots to fill
427 in. Returns the number of locations filled in, or -1 for errors. If
428 LLBUFS is a null pointer, stores nothing and returns the total number of
429 locations. A return value of zero means that the location list
430 indicated no value is accessible. */
431extern int dwarf_addrloclists (Dwarf_Attribute *attr, Dwarf_Addr address,
432 Dwarf_Loc **llbufs, size_t *listlens,
433 size_t nlocs);
434
435
436/* Return scope DIEs containing PC address.
437 Sets *SCOPES to a malloc'd array of Dwarf_Die structures,
438 and returns the number of elements in the array.
439 (*SCOPES)[0] is the DIE for the innermost scope containing PC,
440 (*SCOPES)[1] is the DIE for the scope containing that scope, and so on.
441 Returns -1 for errors or 0 if no scopes match PC. */
442extern int dwarf_getscopes (Dwarf_Die *cudie, Dwarf_Addr pc,
443 Dwarf_Die **scopes);
444
445/* Search SCOPES[0..NSCOPES-1] for a variable called NAME.
446 Ignore the first SKIP_SHADOWS scopes that match the name.
447 If MATCH_FILE is not null, accept only declaration in that source file;
448 if MATCH_LINENO or MATCH_LINECOL are also nonzero, accept only declaration
449 at that line and column.
450
451 If successful, fill in *RESULT with the DIE of the variable found,
452 and return N where SCOPES[N] is the scope defining the variable.
453 Return -1 for errors or -2 for no matching variable found. */
454extern int dwarf_getscopevar (Dwarf_Die *scopes, int nscopes,
455 const char *name, int skip_shadows,
456 const char *match_file,
457 int match_lineno, int match_linecol,
458 Dwarf_Die *result);
459
460
461
462/* Return list address ranges. */
463extern int dwarf_getaranges (Dwarf *dbg, Dwarf_Aranges **aranges,
464 size_t *naranges)
465 __nonnull_attribute__ (2);
466
467/* Return one of the address range entries. */
468extern Dwarf_Arange *dwarf_onearange (Dwarf_Aranges *aranges, size_t idx);
469
470/* Return information in address range record. */
471extern int dwarf_getarangeinfo (Dwarf_Arange *arange, Dwarf_Addr *addrp,
472 Dwarf_Word *lengthp, Dwarf_Off *offsetp);
473
474/* Get address range which includes given address. */
475extern Dwarf_Arange *dwarf_getarange_addr (Dwarf_Aranges *aranges,
476 Dwarf_Addr addr);
477
478
479
480/* Get functions in CUDIE. */
481extern ptrdiff_t dwarf_getfuncs (Dwarf_Die *cudie,
482 int (*callback) (Dwarf_Func *, void *),
483 void *arg, ptrdiff_t offset);
484
485/* Return name of function. */
486extern const char *dwarf_func_name (Dwarf_Func *func);
487
488/* Return start address of function. */
489extern int dwarf_func_lowpc (Dwarf_Func *func, Dwarf_Addr *return_addr)
490 __nonnull_attribute__ (2);
491
492/* Return end address of function. */
493extern int dwarf_func_highpc (Dwarf_Func *func, Dwarf_Addr *return_addr)
494 __nonnull_attribute__ (2);
495
496/* Return entry point address of function. */
497extern int dwarf_func_entrypc (Dwarf_Func *func, Dwarf_Addr *return_addr)
498 __nonnull_attribute__ (2);
499
500/* Return file name containing definition of the given function. */
501extern const char *dwarf_func_file (Dwarf_Func *func);
502
503/* Get line number of beginning of given function. */
504extern int dwarf_func_line (Dwarf_Func *func, int *linep)
505 __nonnull_attribute__ (2);
506
507/* Get column number of beginning of given function. */
508extern int dwarf_func_col (Dwarf_Func *func, int *colp)
509 __nonnull_attribute__ (2);
510
511
512/* Call callback function for each of the macro information entry for
513 the CU. */
514extern ptrdiff_t dwarf_getmacros (Dwarf_Die *cudie,
515 int (*callback) (Dwarf_Macro *, void *),
516 void *arg, ptrdiff_t offset)
517 __nonnull_attribute__ (2);
518
519/* Return macro opcode. */
520extern int dwarf_macro_opcode (Dwarf_Macro *macro, unsigned int *opcodep)
521 __nonnull_attribute__ (2);
522
523/* Return first macro parameter. */
524extern int dwarf_macro_param1 (Dwarf_Macro *macro, Dwarf_Word *paramp)
525 __nonnull_attribute__ (2);
526
527/* Return second macro parameter. */
528extern int dwarf_macro_param2 (Dwarf_Macro *macro, Dwarf_Word *paramp,
529 const char **strp);
530
531
532/* Return error code of last failing function call. This value is kept
533 separately for each thread. */
534extern int dwarf_errno (void);
535
536/* Return error string for ERROR. If ERROR is zero, return error string
537 for most recent error or NULL is none occurred. If ERROR is -1 the
538 behaviour is similar to the last case except that not NULL but a legal
539 string is returned. */
540extern const char *dwarf_errmsg (int err);
541
542
543/* Register new Out-Of-Memory handler. The old handler is returned. */
544extern Dwarf_OOM dwarf_new_oom_handler (Dwarf *dbg, Dwarf_OOM handler);
545
546
547/* Inline optimizations. */
548#ifdef __OPTIMIZE__
549/* Return attribute code of given attribute. */
550extern inline unsigned int
551dwarf_whatattr (Dwarf_Attribute *attr)
552{
553 return attr == NULL ? 0 : attr->code;
554}
555
556/* Return attribute code of given attribute. */
557extern inline unsigned int
558dwarf_whatform (Dwarf_Attribute *attr)
559{
560 return attr == NULL ? 0 : attr->form;
561}
562#endif /* Optimize. */
563
564#endif /* libdw.h */