blob: 8b069e27b188986e984d27fe17637cb88d4d8e07 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Interfaces for libdwfl.
2 Copyright (C) 2005 Red Hat, Inc.
3
4 This program is Open Source software; you can redistribute it and/or
5 modify it under the terms of the Open Software License version 1.0 as
6 published by the Open Source Initiative.
7
8 You should have received a copy of the Open Software License along
9 with this program; if not, you may obtain a copy of the Open Software
10 License version 1.0 from http://www.opensource.org/licenses/osl.php or
11 by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
12 3001 King Ranch Road, Ukiah, CA 95482. */
13
14#ifndef _LIBDWFL_H
15#define _LIBDWFL_H 1
16
Roland McGrathd89bbe42005-07-28 07:08:01 +000017#include "libdw.h"
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000018
19/* Handle for a session using the library. */
20typedef struct Dwfl Dwfl;
21
22/* Handle for a module. */
23typedef struct Dwfl_Module Dwfl_Module;
24
25/* Handle describing a line record. */
26typedef struct Dwfl_Line Dwfl_Line;
27
28/* Callbacks. */
29typedef struct
30{
31 int (*find_elf) (Dwfl_Module *mod, void **userdata,
32 const char *modname, Dwarf_Addr base,
33 char **file_name, Elf **elfp);
34
35 int (*find_debuginfo) (Dwfl_Module *mod, void **userdata,
36 const char *modname, Dwarf_Addr base,
37 const char *file_name,
38 const char *debuglink_file, GElf_Word debuglink_crc,
39 char **debuginfo_file_name);
40
Roland McGrathd17fac72005-08-23 08:20:21 +000041 /* Fill *ADDR with the loaded address of the section called SECNAME in
42 the given module. This is called exactly once for each SHF_ALLOC
43 section that relocations affecting DWARF data refer to, so it can
44 easily be used to collect state about the sections referenced. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000045 int (*section_address) (Dwfl_Module *mod, void **userdata,
46 const char *modname, Dwarf_Addr base,
Roland McGrathd17fac72005-08-23 08:20:21 +000047 const char *secname,
48 Elf32_Word shndx, const GElf_Shdr *shdr,
49 Dwarf_Addr *addr);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000050
51 char **debuginfo_path; /* See dwfl_standard_find_debuginfo. */
52} Dwfl_Callbacks;
53
54
55/* Start a new session with the library. */
Ulrich Drepper4d527362005-07-28 22:32:58 +000056extern Dwfl *dwfl_begin (const Dwfl_Callbacks *callbacks);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000057
58/* End a session. */
Ulrich Drepper4d527362005-07-28 22:32:58 +000059extern void dwfl_end (Dwfl *);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000060
61/* Return error code of last failing function call. This value is kept
62 separately for each thread. */
63extern int dwfl_errno (void);
64
65/* Return error string for ERROR. If ERROR is zero, return error string
66 for most recent error or NULL if none occurred. If ERROR is -1 the
67 behaviour is similar to the last case except that not NULL but a legal
68 string is returned. */
69extern const char *dwfl_errmsg (int err);
70
71
72/* Start reporting the current set of modules to the library. No calls but
Roland McGrathd17fac72005-08-23 08:20:21 +000073 dwfl_report_* can be made on DWFL until dwfl_report_end is called. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000074extern void dwfl_report_begin (Dwfl *dwfl);
75
76/* Report that a module called NAME spans addresses [START, END).
77 Returns the module handle, either existing or newly allocated,
78 or returns a null pointer for an allocation error. */
79extern Dwfl_Module *dwfl_report_module (Dwfl *dwfl, const char *name,
80 Dwarf_Addr start, Dwarf_Addr end);
81
82/* Report a module with start and end addresses computed from the ELF
Roland McGrathd17fac72005-08-23 08:20:21 +000083 program headers in the given file, plus BASE. For an ET_REL file,
84 does a simple absolute section layout starting at BASE.
85 FD may be -1 to open FILE_NAME. On success, FD is consumed by the
86 library, and the `find_elf' callback will not be used for this module. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000087extern Dwfl_Module *dwfl_report_elf (Dwfl *dwfl, const char *name,
88 const char *file_name, int fd,
89 GElf_Addr base);
90
Roland McGrathd17fac72005-08-23 08:20:21 +000091/* Similar, but report the module for offline use. All ET_EXEC files
92 being reported must be reported before any relocatable objects.
93 If this is used, dwfl_report_module and dwfl_report_elf may not be
94 used in the same reporting session. */
95extern Dwfl_Module *dwfl_report_offline (Dwfl *dwfl, const char *name,
96 const char *file_name, int fd);
97
98
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000099/* Finish reporting the current set of modules to the library.
100 If REMOVED is not null, it's called for each module that
101 existed before but was not included in the current report.
102 Returns a nonzero return value from the callback.
103 The callback may call dwfl_report_module; doing so with the
104 details of the module being removed prevents its removal.
105 DWFL cannot be used until this function has returned zero. */
106extern int dwfl_report_end (Dwfl *dwfl,
107 int (*removed) (Dwfl_Module *, void *,
108 const char *, Dwarf_Addr,
109 void *arg),
110 void *arg);
111
112/* Return the name of the module, and for each non-null argument store
113 interesting details: *USERDATA is a location for storing your own
114 pointer, **USERDATA is initially null; *START and *END give the address
115 range covered by the module; *DWBIAS is the address bias for debugging
116 information, and *SYMBIAS for symbol table entries (either is -1 if not
117 yet accessed); *MAINFILE is the name of the ELF file, and *DEBUGFILE the
118 name of the debuginfo file (might be equal to *MAINFILE; either is null
119 if not yet accessed). */
120extern const char *dwfl_module_info (Dwfl_Module *mod, void ***userdata,
121 Dwarf_Addr *start, Dwarf_Addr *end,
122 Dwarf_Addr *dwbias, Dwarf_Addr *symbias,
123 const char **mainfile,
124 const char **debugfile);
125
126/* Iterate through the modules, starting the walk with OFFSET == 0.
127 Calls *CALLBACK for each module as long as it returns DWARF_CB_OK.
128 When *CALLBACK returns another value, the walk stops and the
129 return value can be passed as OFFSET to resume it. Returns 0 when
130 there are no more modules, or -1 for errors. */
131extern ptrdiff_t dwfl_getmodules (Dwfl *dwfl,
132 int (*callback) (Dwfl_Module *, void **,
133 const char *, Dwarf_Addr,
134 void *arg),
135 void *arg,
136 ptrdiff_t offset);
137
138
139/*** Standard callbacks ***/
140
141/* Standard find_debuginfo callback function.
142 This is controlled by a string specifying directories to look in.
143 If `debuginfo_path' is set in the Dwfl_Callbacks structure
144 and the char * it points to is not null, that supplies the string.
145 Otherwise a default path is used.
146
147 If the first character of the string is + or - that says to check or to
148 ignore (respectively) the CRC32 checksum from the .gnu_debuglink
149 section. The default is to check it. The remainder of the string is
150 composed of elements separated by colons. Each element can start with +
151 or - to override the global checksum behavior. If the remainder of the
152 element is empty, the directory containing the main file is tried; if
153 it's an absolute path name, the absolute directory path containing the
154 main file is taken as a subdirectory of this path; a relative path name
155 is taken as a subdirectory of the directory containing the main file.
156 Hence for /bin/ls, string ":.debug:/usr/lib/debug" says to look in /bin,
157 then /bin/.debug, then /usr/lib/debug/bin, for the file name in the
158 .gnu_debuglink section (or "ls.debug" if none was found). */
159
160extern int dwfl_standard_find_debuginfo (Dwfl_Module *, void **,
161 const char *, Dwarf_Addr,
162 const char *, const char *,
163 GElf_Word, char **);
164
165
Roland McGrathd17fac72005-08-23 08:20:21 +0000166/* This callback must be used when using dwfl_offline_* to report modules,
167 if ET_REL is to be supported. */
168extern int dwfl_offline_section_address (Dwfl_Module *, void **,
169 const char *, Dwarf_Addr,
170 const char *, Elf32_Word,
171 const GElf_Shdr *,
172 Dwarf_Addr *addr);
173
174
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000175/* Callbacks for working with kernel modules in the running Linux kernel. */
176extern int dwfl_linux_kernel_find_elf (Dwfl_Module *, void **,
177 const char *, Dwarf_Addr,
178 char **, Elf **);
179extern int dwfl_linux_kernel_module_section_address (Dwfl_Module *, void **,
180 const char *, Dwarf_Addr,
Roland McGrathd17fac72005-08-23 08:20:21 +0000181 const char *, Elf32_Word,
182 const GElf_Shdr *,
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000183 Dwarf_Addr *addr);
184
185/* Call dwfl_report_elf for the running Linux kernel.
186 Returns zero on success, -1 if dwfl_report_module failed,
187 or an errno code if opening the kernel binary failed. */
188extern int dwfl_linux_kernel_report_kernel (Dwfl *dwfl);
189
190/* Call dwfl_report_module for each kernel module in the running Linux kernel.
191 Returns zero on success, -1 if dwfl_report_module failed,
192 or an errno code if reading the list of modules failed. */
193extern int dwfl_linux_kernel_report_modules (Dwfl *dwfl);
194
Roland McGrathd17fac72005-08-23 08:20:21 +0000195/* Report a kernel and its modules found on disk, for offline use.
196 If RELEASE starts with '/', it names a directory to look in;
197 if not, it names a directory to find under /lib/modules/;
198 if null, /lib/modules/`uname -r` is used.
199 Returns zero on success, -1 if dwfl_report_module failed,
200 or an errno code if finding the files on disk failed.
201
202 If PREDICATE is not null, it is called with each module to be reported;
203 its arguments are the module name, and the ELF file name or null if unknown,
204 and its return value should be zero to skip the module, one to report it,
205 or -1 to cause the call to fail and return errno. */
206extern int dwfl_linux_kernel_report_offline (Dwfl *dwfl, const char *release,
207 int (*predicate) (const char *,
208 const char *));
209
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000210
211/* Call dwfl_report_module for each file mapped into the address space of PID.
212 Returns zero on success, -1 if dwfl_report_module failed,
213 or an errno code if opening the kernel binary failed. */
Ulrich Drepper4d527362005-07-28 22:32:58 +0000214extern int dwfl_linux_proc_report (Dwfl *dwfl, pid_t pid);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000215
216/* Trivial find_elf callback for use with dwfl_linux_proc_report.
217 This uses the module name as a file name directly and tries to open it
218 if it begin with a slash, or handles the magic string "[vdso]". */
Ulrich Drepper4d527362005-07-28 22:32:58 +0000219extern int dwfl_linux_proc_find_elf (Dwfl_Module *mod, void **userdata,
220 const char *module_name, Dwarf_Addr base,
221 char **file_name, Elf **);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000222
223/* Standard argument parsing for using a standard callback set. */
224struct argp;
225extern const struct argp *dwfl_standard_argp (void) __attribute__ ((const));
226
227
Roland McGrathd17fac72005-08-23 08:20:21 +0000228/*** Relocation of addresses from Dwfl ***/
229
230/* Return the number of relocatable bases associated with the module,
231 which is zero for ET_EXEC and one for ET_DYN. Returns -1 for errors. */
232extern int dwfl_module_relocations (Dwfl_Module *mod);
233
234/* Return the relocation base index associated with the *ADDRESS location,
235 and adjust *ADDRESS to be an offset relative to that base.
236 Returns -1 for errors. */
237extern int dwfl_module_relocate_address (Dwfl_Module *mod,
238 Dwarf_Addr *address);
239
240/* Return the ELF section name for the given relocation base index;
241 if SHNDXP is not null, set *SHNDXP to the ELF section index.
242 For ET_DYN, returns "" and sets *SHNDXP to SHN_ABS; the relocation
243 base is the runtime start address reported for the module.
244 Returns null for errors. */
245extern const char *dwfl_module_relocation_info (Dwfl_Module *mod,
246 unsigned int idx,
247 Elf32_Word *shndxp);
248
249/* Validate that ADDRESS and ADDRESS+OFFSET lie in a known module
250 and both within the same contiguous region for relocation purposes.
251 Returns zero for success and -1 for errors. */
252extern int dwfl_validate_address (Dwfl *dwfl,
253 Dwarf_Addr address, Dwarf_Sword offset);
254
255
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000256/*** Dwarf access functions ***/
257
258/* Find the module containing the given address. */
259extern Dwfl_Module *dwfl_addrmodule (Dwfl *dwfl, Dwarf_Addr address);
260
261/* Fetch the module main ELF file (where the allocated sections
262 are found) for use with libelf. If successful, fills in *BIAS
263 with the difference between addresses within the loaded module
264 and those in symbol tables or Dwarf information referring to it. */
265extern Elf *dwfl_module_getelf (Dwfl_Module *, GElf_Addr *bias);
266
267/* Fetch the module's debug information for use with libdw.
268 If successful, fills in *BIAS with the difference between
269 addresses within the loaded module and those to use with libdw. */
270extern Dwarf *dwfl_module_getdwarf (Dwfl_Module *, Dwarf_Addr *bias)
271 __nonnull_attribute__ (2);
272
273/* Get the libdw handle for each module. */
274extern ptrdiff_t dwfl_getdwarf (Dwfl *,
275 int (*callback) (Dwfl_Module *, void **,
276 const char *, Dwarf_Addr,
277 Dwarf *, Dwarf_Addr, void *),
278 void *arg, ptrdiff_t offset);
279
280/* Look up the module containing ADDR and return its debugging information,
281 loading it if necessary. */
282extern Dwarf *dwfl_addrdwarf (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Addr *bias)
283 __nonnull_attribute__ (3);
284
285
286/* Find the CU containing ADDR and return its DIE. */
287extern Dwarf_Die *dwfl_addrdie (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Addr *bias)
288 __nonnull_attribute__ (3);
289extern Dwarf_Die *dwfl_module_addrdie (Dwfl_Module *mod,
290 Dwarf_Addr addr, Dwarf_Addr *bias)
291 __nonnull_attribute__ (3);
292
293/* Iterate through the CUs, start with null for LASTCU. */
294extern Dwarf_Die *dwfl_nextcu (Dwfl *dwfl, Dwarf_Die *lastcu, Dwarf_Addr *bias)
295 __nonnull_attribute__ (3);
296extern Dwarf_Die *dwfl_module_nextcu (Dwfl_Module *mod,
297 Dwarf_Die *lastcu, Dwarf_Addr *bias)
298 __nonnull_attribute__ (3);
299
300/* Return the module containing the CU DIE. */
301extern Dwfl_Module *dwfl_cumodule (Dwarf_Die *cudie);
302
303
Roland McGrathd17fac72005-08-23 08:20:21 +0000304/* Cache the source line information fo the CU and return the
305 number of Dwfl_Line entries it has. */
306extern int dwfl_getsrclines (Dwarf_Die *cudie, size_t *nlines);
307
308/* Access one line number entry within the CU. */
309extern Dwfl_Line *dwfl_onesrcline (Dwarf_Die *cudie, size_t idx);
310
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000311/* Get source for address. */
312extern Dwfl_Line *dwfl_module_getsrc (Dwfl_Module *mod, Dwarf_Addr addr);
313extern Dwfl_Line *dwfl_getsrc (Dwfl *dwfl, Dwarf_Addr addr);
314
315/* Get address for source. */
316extern int dwfl_module_getsrc_file (Dwfl_Module *mod,
317 const char *fname, int lineno, int column,
318 Dwfl_Line ***srcsp, size_t *nsrcs);
319
320/* Return the module containing this line record. */
321extern Dwfl_Module *dwfl_linemodule (Dwfl_Line *line);
322
323/* Return the source file name and fill in other information.
324 Arguments may be null for unneeded fields. */
325extern const char *dwfl_lineinfo (Dwfl_Line *line, Dwarf_Addr *addr,
326 int *linep, int *colp,
327 Dwarf_Word *mtime, Dwarf_Word *length);
328
329
330/* Find the symbol that ADDRESS lies inside, and return its name. */
Roland McGrathe47ab762005-11-17 03:16:00 +0000331extern const char *dwfl_module_addrname (Dwfl_Module *mod, GElf_Addr address);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000332
333
334
Roland McGrathe47ab762005-11-17 03:16:00 +0000335/* Return location expression to find return value given a
336 DW_TAG_subprogram, DW_TAG_subroutine_type, or similar DIE describing
337 function itself (whose DW_AT_type attribute describes its return type).
338 The given DIE must come from the given module. Returns -1 for errors.
339 Returns zero if the function has no return value (e.g. "void" in C).
340 Otherwise, *LOCOPS gets a location expression to find the return value,
341 and returns the number of operations in the expression. The pointer is
342 permanently allocated at least as long as the module is live. */
343extern int dwfl_module_return_value_location (Dwfl_Module *mod,
344 Dwarf_Die *functypedie,
345 const Dwarf_Op **locops);
346
347
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000348#endif /* libdwfl.h */