Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:00:01

0001 /* Interfaces for libdwfl.
0002    Copyright (C) 2005-2010, 2013 Red Hat, Inc.
0003    This file is part of elfutils.
0004 
0005    This file is free software; you can redistribute it and/or modify
0006    it under the terms of either
0007 
0008      * the GNU Lesser General Public License as published by the Free
0009        Software Foundation; either version 3 of the License, or (at
0010        your option) any later version
0011 
0012    or
0013 
0014      * the GNU General Public License as published by the Free
0015        Software Foundation; either version 2 of the License, or (at
0016        your option) any later version
0017 
0018    or both in parallel, as here.
0019 
0020    elfutils is distributed in the hope that it will be useful, but
0021    WITHOUT ANY WARRANTY; without even the implied warranty of
0022    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0023    General Public License for more details.
0024 
0025    You should have received copies of the GNU General Public License and
0026    the GNU Lesser General Public License along with this program.  If
0027    not, see <http://www.gnu.org/licenses/>.  */
0028 
0029 #ifndef _LIBDWFL_H
0030 #define _LIBDWFL_H  1
0031 
0032 #include "libdw.h"
0033 #include <stdio.h>
0034 
0035 /* Handle for a session using the library.  */
0036 typedef struct Dwfl Dwfl;
0037 
0038 /* Handle for a module.  */
0039 typedef struct Dwfl_Module Dwfl_Module;
0040 
0041 /* Handle describing a line record.  */
0042 typedef struct Dwfl_Line Dwfl_Line;
0043 
0044 /* This holds information common for all the frames of one backtrace for
0045    a particular thread/task/TID.  Several threads belong to one Dwfl.  */
0046 typedef struct Dwfl_Thread Dwfl_Thread;
0047 
0048 /* This holds everything we know about the state of the frame at a particular
0049    PC location described by an FDE belonging to Dwfl_Thread.  */
0050 typedef struct Dwfl_Frame Dwfl_Frame;
0051 
0052 /* Handle for debuginfod-client connection.  */
0053 #ifndef _ELFUTILS_DEBUGINFOD_CLIENT_TYPEDEF
0054 typedef struct debuginfod_client debuginfod_client;
0055 #define _ELFUTILS_DEBUGINFOD_CLIENT_TYPEDEF 1
0056 #endif
0057 
0058 /* Callbacks.  */
0059 typedef struct
0060 {
0061   int (*find_elf) (Dwfl_Module *mod, void **userdata,
0062            const char *modname, Dwarf_Addr base,
0063            char **file_name, Elf **elfp);
0064 
0065   int (*find_debuginfo) (Dwfl_Module *mod, void **userdata,
0066              const char *modname, Dwarf_Addr base,
0067              const char *file_name,
0068              const char *debuglink_file, GElf_Word debuglink_crc,
0069              char **debuginfo_file_name);
0070 
0071   /* Fill *ADDR with the loaded address of the section called SECNAME in
0072      the given module.  Use (Dwarf_Addr) -1 if this section is omitted from
0073      accessible memory.  This is called exactly once for each SHF_ALLOC
0074      section that relocations affecting DWARF data refer to, so it can
0075      easily be used to collect state about the sections referenced.  */
0076   int (*section_address) (Dwfl_Module *mod, void **userdata,
0077               const char *modname, Dwarf_Addr base,
0078               const char *secname,
0079               GElf_Word shndx, const GElf_Shdr *shdr,
0080               Dwarf_Addr *addr);
0081 
0082   char **debuginfo_path;    /* See dwfl_standard_find_debuginfo.  */
0083 } Dwfl_Callbacks;
0084 
0085 
0086 #ifdef __cplusplus
0087 extern "C" {
0088 #endif
0089 
0090 /* Start a new session with the library.  */
0091 extern Dwfl *dwfl_begin (const Dwfl_Callbacks *callbacks)
0092   __nonnull_attribute__ (1);
0093 
0094 
0095 /* End a session.  */
0096 extern void dwfl_end (Dwfl *);
0097 
0098 /* Return implementation's version string suitable for printing.  */
0099 extern const char *dwfl_version (Dwfl *);
0100 
0101 /* Return error code of last failing function call.  This value is kept
0102    separately for each thread.  */
0103 extern int dwfl_errno (void);
0104 
0105 /* Return error string for ERROR.  If ERROR is zero, return error string
0106    for most recent error or NULL if none occurred.  If ERROR is -1 the
0107    behaviour is similar to the last case except that not NULL but a legal
0108    string is returned.  */
0109 extern const char *dwfl_errmsg (int err);
0110 
0111 
0112 /* Start reporting the current set of segments and modules to the library.
0113    All existing segments are wiped.  Existing modules are marked to be
0114    deleted, and will not be found via dwfl_addrmodule et al if they are not
0115    re-reported before dwfl_report_end is called.  */
0116 extern void dwfl_report_begin (Dwfl *dwfl);
0117 
0118 /* Report that segment NDX begins at PHDR->p_vaddr + BIAS.
0119    If NDX is < 0, the value succeeding the last call's NDX
0120    is used instead (zero on the first call).  IDENT is ignored.
0121 
0122    If nonzero, the smallest PHDR->p_align value seen sets the
0123    effective page size for the address space DWFL describes.
0124    This is the granularity at which reported module boundary
0125    addresses will be considered to fall in or out of a segment.
0126 
0127    Returns -1 for errors, or NDX (or its assigned replacement) on success.
0128 
0129    Reporting segments at all is optional.  Its only benefit to the caller is to
0130    offer this quick lookup via dwfl_addrsegment, or use other segment-based
0131    calls.  */
0132 extern int dwfl_report_segment (Dwfl *dwfl, int ndx,
0133                 const GElf_Phdr *phdr, GElf_Addr bias,
0134                 const void *ident);
0135 
0136 /* Report that a module called NAME spans addresses [START, END).
0137    Returns the module handle, either existing or newly allocated,
0138    or returns a null pointer for an allocation error.  */
0139 extern Dwfl_Module *dwfl_report_module (Dwfl *dwfl, const char *name,
0140                     Dwarf_Addr start, Dwarf_Addr end);
0141 
0142 /* Report a module to address BASE with start and end addresses computed
0143    from the ELF program headers in the given file - see the table below.
0144    FD may be -1 to open FILE_NAME.  On success, FD is consumed by the
0145    library, and the `find_elf' callback will not be used for this module.
0146         ADD_P_VADDR  BASE
0147    ET_EXEC  ignored      ignored
0148    ET_DYN   false        absolute address where to place the file
0149         true         start address relative to ELF's phdr p_vaddr
0150    ET_REL   ignored      absolute address where to place the file
0151    ET_CORE  ignored      ignored
0152    ET_DYN ELF phdr p_vaddr address can be non-zero if the shared library
0153    has been prelinked by tool prelink(8).  */
0154 extern Dwfl_Module *dwfl_report_elf (Dwfl *dwfl, const char *name,
0155                      const char *file_name, int fd,
0156                      GElf_Addr base, bool add_p_vaddr);
0157 
0158 /* Similar, but report the module for offline use.  All ET_EXEC files
0159    being reported must be reported before any relocatable objects.
0160    If this is used, dwfl_report_module and dwfl_report_elf may not be
0161    used in the same reporting session.  */
0162 extern Dwfl_Module *dwfl_report_offline (Dwfl *dwfl, const char *name,
0163                      const char *file_name, int fd);
0164 
0165 /* Similar, but report ELF from memory region.  */
0166 extern Dwfl_Module *dwfl_report_offline_memory (Dwfl *dwfl, const char *name,
0167                         const char *file_name,
0168                         char *data, size_t size);
0169 
0170 /* Finish reporting the current set of modules to the library.
0171    If REMOVED is not null, it's called for each module that
0172    existed before but was not included in the current report.
0173    Returns a nonzero return value from the callback.
0174    The callback may call dwfl_report_module; doing so with the
0175    details of the module being removed prevents its removal.
0176    DWFL cannot be used until this function has returned zero.  */
0177 extern int dwfl_report_end (Dwfl *dwfl,
0178                 int (*removed) (Dwfl_Module *, void *,
0179                         const char *, Dwarf_Addr,
0180                         void *arg),
0181                 void *arg);
0182 
0183 /* Start reporting additional modules to the library.  No calls but
0184    dwfl_report_* can be made on DWFL until dwfl_report_end is called.
0185    This is like dwfl_report_begin, but all the old modules are kept on.
0186    More dwfl_report_* calls can follow to add more modules.
0187    When dwfl_report_end is called, no old modules will be removed.  */
0188 extern void dwfl_report_begin_add (Dwfl *dwfl);
0189 
0190 
0191 /* Return the name of the module, and for each non-null argument store
0192    interesting details: *USERDATA is a location for storing your own
0193    pointer, **USERDATA is initially null; *START and *END give the address
0194    range covered by the module; *DWBIAS is the address bias for debugging
0195    information, and *SYMBIAS for symbol table entries (either is -1 if not
0196    yet accessed); *MAINFILE is the name of the ELF file, and *DEBUGFILE the
0197    name of the debuginfo file (might be equal to *MAINFILE; either is null
0198    if not yet accessed).  */
0199 extern const char *dwfl_module_info (Dwfl_Module *mod, void ***userdata,
0200                      Dwarf_Addr *start, Dwarf_Addr *end,
0201                      Dwarf_Addr *dwbias, Dwarf_Addr *symbias,
0202                      const char **mainfile,
0203                      const char **debugfile);
0204 
0205 /* Iterate through the modules, starting the walk with OFFSET == 0.
0206    Calls *CALLBACK for each module as long as it returns DWARF_CB_OK.
0207    When *CALLBACK returns another value, the walk stops and the
0208    return value can be passed as OFFSET to resume it.  Returns 0 when
0209    there are no more modules, or -1 for errors.  */
0210 extern ptrdiff_t dwfl_getmodules (Dwfl *dwfl,
0211                   int (*callback) (Dwfl_Module *, void **,
0212                            const char *, Dwarf_Addr,
0213                            void *arg),
0214                   void *arg,
0215                   ptrdiff_t offset);
0216 
0217 /* Find the module containing the given address.  */
0218 extern Dwfl_Module *dwfl_addrmodule (Dwfl *dwfl, Dwarf_Addr address);
0219 
0220 /* Find the segment, if any, and module, if any, containing ADDRESS.
0221    Returns a segment index returned by dwfl_report_segment, or -1
0222    if no segment matches the address.  Regardless of the return value,
0223    *MOD is always set to the module containing ADDRESS, or to null.  */
0224 extern int dwfl_addrsegment (Dwfl *dwfl, Dwarf_Addr address, Dwfl_Module **mod);
0225 
0226 
0227 
0228 /* Report the known build ID bits associated with a module.
0229    If VADDR is nonzero, it gives the absolute address where those
0230    bits are found within the module.  This can be called at any
0231    time, but is usually used immediately after dwfl_report_module.
0232    Once the module's main ELF file is opened, the ID note found
0233    there takes precedence and cannot be changed.  */
0234 extern int dwfl_module_report_build_id (Dwfl_Module *mod,
0235                     const unsigned char *bits, size_t len,
0236                     GElf_Addr vaddr)
0237   __nonnull_attribute__ (2);
0238 
0239 /* Extract the build ID bits associated with a module.
0240    Returns -1 for errors, 0 if no ID is known, or the number of ID bytes.
0241    When an ID is found, *BITS points to it; *VADDR is the absolute address
0242    at which the ID bits are found within the module, or 0 if unknown.
0243 
0244    This returns 0 when the module's main ELF file has not yet been loaded
0245    and its build ID bits were not reported.  To ensure the ID is always
0246    returned when determinable, call dwfl_module_getelf first.  */
0247 extern int dwfl_module_build_id (Dwfl_Module *mod,
0248                  const unsigned char **bits, GElf_Addr *vaddr)
0249   __nonnull_attribute__ (2, 3);
0250 
0251 
0252 /*** Standard callbacks ***/
0253 
0254 /* These standard find_elf and find_debuginfo callbacks are
0255    controlled by a string specifying directories to look in.
0256    If `debuginfo_path' is set in the Dwfl_Callbacks structure
0257    and the char * it points to is not null, that supplies the
0258    string.  Otherwise a default path is used.
0259 
0260    If the first character of the string is + or - that enables or
0261    disables CRC32 checksum validation when it's necessary.  The
0262    remainder of the string is composed of elements separated by
0263    colons.  Each element can start with + or - to override the
0264    global checksum behavior.  This flag is never relevant when
0265    working with build IDs, but it's always parsed in the path
0266    string.  The remainder of the element indicates a directory.
0267 
0268    Searches by build ID consult only the elements naming absolute
0269    directory paths.  They look under those directories for a link
0270    named ".build-id/xx/yy" or ".build-id/xx/yy.debug", where "xxyy"
0271    is the lower-case hexadecimal representation of the ID bytes.
0272 
0273    In searches for debuginfo by name, if the remainder of the
0274    element is empty, the directory containing the main file is
0275    tried; if it's an absolute path name, the absolute directory path
0276    (and any subdirectory of that path) containing the main file is
0277    taken as a subdirectory of this path; a relative path name is taken
0278    as a subdirectory of the directory containing the main file.
0279    Hence for /usr/bin/ls, the default string ":.debug:/usr/lib/debug"
0280    says to look in /usr/bin, then /usr/bin/.debug, then the path subdirs
0281    under /usr/lib/debug, in the order /usr/lib/debug/usr/bin, then
0282    /usr/lib/debug/bin, and finally /usr/lib/debug, for the file name in
0283    the .gnu_debuglink section (or "ls.debug" if none was found).  */
0284 
0285 /* Standard find_elf callback function working solely on build ID.
0286    This can be tried first by any find_elf callback, to use the
0287    bits passed to dwfl_module_report_build_id, if any.  */
0288 extern int dwfl_build_id_find_elf (Dwfl_Module *, void **,
0289                    const char *, Dwarf_Addr,
0290                    char **, Elf **);
0291 
0292 /* Standard find_debuginfo callback function working solely on build ID.
0293    This can be tried first by any find_debuginfo callback,
0294    to use the build ID bits from the main file when present.  */
0295 extern int dwfl_build_id_find_debuginfo (Dwfl_Module *, void **,
0296                      const char *, Dwarf_Addr,
0297                      const char *, const char *,
0298                      GElf_Word, char **);
0299 
0300 /* Standard find_debuginfo callback function.
0301    If a build ID is available, this tries first to use that.
0302    If there is no build ID or no valid debuginfo found by ID,
0303    it searches the debuginfo path by name, as described above.
0304    Any file found in the path is validated by build ID if possible,
0305    or else by CRC32 checksum if enabled, and skipped if it does not match.  */
0306 extern int dwfl_standard_find_debuginfo (Dwfl_Module *, void **,
0307                      const char *, Dwarf_Addr,
0308                      const char *, const char *,
0309                      GElf_Word, char **);
0310 
0311 
0312 /* This callback must be used when using dwfl_offline_* to report modules,
0313    if ET_REL is to be supported.  */
0314 extern int dwfl_offline_section_address (Dwfl_Module *, void **,
0315                      const char *, Dwarf_Addr,
0316                      const char *, GElf_Word,
0317                      const GElf_Shdr *,
0318                      Dwarf_Addr *addr);
0319 
0320 
0321 /* Callbacks for working with kernel modules in the running Linux kernel.  */
0322 extern int dwfl_linux_kernel_find_elf (Dwfl_Module *, void **,
0323                        const char *, Dwarf_Addr,
0324                        char **, Elf **);
0325 extern int dwfl_linux_kernel_module_section_address (Dwfl_Module *, void **,
0326                              const char *, Dwarf_Addr,
0327                              const char *, GElf_Word,
0328                              const GElf_Shdr *,
0329                              Dwarf_Addr *addr);
0330 
0331 /* Call dwfl_report_elf for the running Linux kernel.
0332    Returns zero on success, -1 if dwfl_report_module failed,
0333    or an errno code if opening the kernel binary failed.  */
0334 extern int dwfl_linux_kernel_report_kernel (Dwfl *dwfl);
0335 
0336 /* Call dwfl_report_module for each kernel module in the running Linux kernel.
0337    Returns zero on success, -1 if dwfl_report_module failed,
0338    or an errno code if reading the list of modules failed.  */
0339 extern int dwfl_linux_kernel_report_modules (Dwfl *dwfl);
0340 
0341 /* Report a kernel and its modules found on disk, for offline use.
0342    If RELEASE starts with '/', it names a directory to look in;
0343    if not, it names a directory to find under /lib/modules/;
0344    if null, /lib/modules/`uname -r` is used.
0345    Returns zero on success, -1 if dwfl_report_module failed,
0346    or an errno code if finding the files on disk failed.
0347 
0348    If PREDICATE is not null, it is called with each module to be reported;
0349    its arguments are the module name, and the ELF file name or null if unknown,
0350    and its return value should be zero to skip the module, one to report it,
0351    or -1 to cause the call to fail and return errno.  */
0352 extern int dwfl_linux_kernel_report_offline (Dwfl *dwfl, const char *release,
0353                          int (*predicate) (const char *,
0354                                    const char *));
0355 
0356 /* Examine an ET_CORE file and report modules based on its contents.
0357    This can follow a dwfl_report_offline call to bootstrap the
0358    DT_DEBUG method of following the dynamic linker link_map chain, in
0359    case the core file does not contain enough of the executable's text
0360    segment to locate its PT_DYNAMIC in the dump.  In such case you need to
0361    supply non-NULL EXECUTABLE, otherwise dynamic libraries will not be loaded
0362    into the DWFL map.  This might call dwfl_report_elf on file names found in
0363    the dump if reading some link_map files is the only way to ascertain those
0364    modules' addresses.  Returns the number of modules reported, or -1 for
0365    errors.  */
0366 extern int dwfl_core_file_report (Dwfl *dwfl, Elf *elf, const char *executable);
0367 
0368 /* Call dwfl_report_module for each file mapped into the address space of PID.
0369    Returns zero on success, -1 if dwfl_report_module failed,
0370    or an errno code if opening the proc files failed.  */
0371 extern int dwfl_linux_proc_report (Dwfl *dwfl, pid_t pid);
0372 
0373 /* Similar, but reads an input stream in the format of Linux /proc/PID/maps
0374    files giving module layout, not the file for a live process.  */
0375 extern int dwfl_linux_proc_maps_report (Dwfl *dwfl, FILE *);
0376 
0377 /* Trivial find_elf callback for use with dwfl_linux_proc_report.
0378    This uses the module name as a file name directly and tries to open it
0379    if it begin with a slash, or handles the magic string "[vdso]".  */
0380 extern int dwfl_linux_proc_find_elf (Dwfl_Module *mod, void **userdata,
0381                      const char *module_name, Dwarf_Addr base,
0382                      char **file_name, Elf **);
0383 
0384 /* Standard argument parsing for using a standard callback set.  */
0385 struct argp;
0386 extern const struct argp *dwfl_standard_argp (void) __const_attribute__;
0387 
0388 
0389 /*** Relocation of addresses from Dwfl ***/
0390 
0391 /* Return the number of relocatable bases associated with the module,
0392    which is zero for ET_EXEC and one for ET_DYN.  Returns -1 for errors.  */
0393 extern int dwfl_module_relocations (Dwfl_Module *mod);
0394 
0395 /* Return the relocation base index associated with the *ADDRESS location,
0396    and adjust *ADDRESS to be an offset relative to that base.
0397    Returns -1 for errors.  */
0398 extern int dwfl_module_relocate_address (Dwfl_Module *mod,
0399                      Dwarf_Addr *address);
0400 
0401 /* Return the ELF section name for the given relocation base index;
0402    if SHNDXP is not null, set *SHNDXP to the ELF section index.
0403    For ET_DYN, returns "" and sets *SHNDXP to SHN_ABS; the relocation
0404    base is the runtime start address reported for the module.
0405    Returns null for errors.  */
0406 extern const char *dwfl_module_relocation_info (Dwfl_Module *mod,
0407                         unsigned int idx,
0408                         GElf_Word *shndxp);
0409 
0410 /* Validate that ADDRESS and ADDRESS+OFFSET lie in a known module
0411    and both within the same contiguous region for relocation purposes.
0412    Returns zero for success and -1 for errors.  */
0413 extern int dwfl_validate_address (Dwfl *dwfl,
0414                   Dwarf_Addr address, Dwarf_Sword offset);
0415 
0416 
0417 /*** ELF access functions ***/
0418 
0419 /* Fetch the module main ELF file (where the allocated sections
0420    are found) for use with libelf.  If successful, fills in *BIAS
0421    with the difference between addresses within the loaded module
0422    and those in symbol tables or Dwarf information referring to it.  */
0423 extern Elf *dwfl_module_getelf (Dwfl_Module *, GElf_Addr *bias)
0424   __nonnull_attribute__ (2);
0425 
0426 /* Return the number of symbols in the module's symbol table,
0427    or -1 for errors.  */
0428 extern int dwfl_module_getsymtab (Dwfl_Module *mod);
0429 
0430 /* Return the index of the first global symbol in the module's symbol
0431    table, or -1 for errors.  In each symbol table, all symbols with
0432    STB_LOCAL binding precede the weak and global symbols.  This
0433    function returns the symbol table index one greater than the last
0434    local symbol.  */
0435 extern int dwfl_module_getsymtab_first_global (Dwfl_Module *mod);
0436 
0437 /* Fetch one entry from the module's symbol table.  On errors, returns
0438    NULL.  If successful, fills in *SYM and returns the string for st_name.
0439    This works like gelf_getsym except that st_value is always adjusted to
0440    an absolute value based on the module's location, when the symbol is in
0441    an SHF_ALLOC section.  If SHNDXP is non-null, it's set with the section
0442    index (whether from st_shndx or extended index table); in case of a
0443    symbol in a non-allocated section, *SHNDXP is instead set to -1.
0444    Note that since symbols can come from either the main, debug or auxiliary
0445    ELF symbol file (either dynsym or symtab) the section index can only
0446    be reliably used to compare against special section constants like
0447    SHN_UNDEF or SHN_ABS.  It is recommended to use dwfl_module_getsym_info
0448    which doesn't have these deficiencies.  */
0449 extern const char *dwfl_module_getsym (Dwfl_Module *mod, int ndx,
0450                        GElf_Sym *sym, GElf_Word *shndxp)
0451   __nonnull_attribute__ (3);
0452 
0453 /* Fetch one entry from the module's symbol table and the associated
0454    address value.  On errors, returns NULL.  If successful, fills in
0455    *SYM, *ADDR and returns the string for st_name.  This works like
0456    gelf_getsym.  *ADDR is set to the st_value adjusted to an absolute
0457    value based on the module's location, when the symbol is in an
0458    SHF_ALLOC section.  For non-ET_REL files, if the arch uses function
0459    descriptors, and the st_value points to one, *ADDR will be resolved
0460    to the actual function entry address.  The SYM->ST_VALUE itself
0461    isn't adjusted in any way.  Fills in ELFP, if not NULL, with the
0462    ELF file the symbol originally came from.  Note that symbols can
0463    come from either the main, debug or auxiliary ELF symbol file
0464    (either dynsym or symtab).  If SHNDXP is non-null, it's set with
0465    the section index (whether from st_shndx or extended index table);
0466    in case of a symbol in a non-allocated section, *SHNDXP is instead
0467    set to -1.  Fills in BIAS, if not NULL, with the difference between
0468    addresses within the loaded module and those in symbol table of the
0469    ELF file.  Note that the address associated with the symbol might
0470    be in a different section than the returned symbol.  The section in
0471    the main elf file in which returned ADDR falls can be found with
0472    dwfl_module_address_section.  */
0473 extern const char *dwfl_module_getsym_info (Dwfl_Module *mod, int ndx,
0474                         GElf_Sym *sym, GElf_Addr *addr,
0475                         GElf_Word *shndxp,
0476                         Elf **elfp, Dwarf_Addr *bias)
0477   __nonnull_attribute__ (3, 4);
0478 
0479 /* Find the symbol that ADDRESS lies inside, and return its name.  */
0480 extern const char *dwfl_module_addrname (Dwfl_Module *mod, GElf_Addr address);
0481 
0482 /* Find the symbol associated with ADDRESS.  Return its name or NULL
0483    when nothing was found.  If the architecture uses function
0484    descriptors, and symbol st_value points to one, ADDRESS will be
0485    matched against either the adjusted st_value or the associated
0486    function entry value as described in dwfl_module_getsym_info.
0487    OFFSET will be filled in with the difference from the start of the
0488    symbol (or function entry), OFFSET cannot be NULL.  SYM is filled
0489    in with the symbol associated with the matched ADDRESS, SYM cannot
0490    be NULL.  The SYM->ST_VALUE itself isn't adjusted in any way.
0491    Fills in ELFP, if not NULL, with the ELF file the symbol originally
0492    came from.  Note that symbols can come from either the main, debug
0493    or auxiliary ELF symbol file (either dynsym or symtab).  If SHNDXP
0494    is non-null, it's set with the section index (whether from st_shndx
0495    or extended index table).  Fills in BIAS, if not NULL, with the
0496    difference between addresses within the loaded module and those in
0497    symbol table of the ELF file.  Note that the address matched
0498    against the symbol might be in a different section than the
0499    returned symbol.  The section in the main elf file in ADDRESS falls
0500    can be found with dwfl_module_address_section.  */
0501 extern const char *dwfl_module_addrinfo (Dwfl_Module *mod, GElf_Addr address,
0502                      GElf_Off *offset, GElf_Sym *sym,
0503                      GElf_Word *shndxp, Elf **elfp,
0504                      Dwarf_Addr *bias)
0505   __nonnull_attribute__ (3, 4);
0506 
0507 /* Find the symbol that ADDRESS lies inside, and return detailed
0508    information as for dwfl_module_getsym (above).  Note that like
0509    dwfl_module_getsym this function also adjusts SYM->ST_VALUE to an
0510    absolute value based on the module's location.  ADDRESS is only
0511    matched against this adjusted SYM->ST_VALUE.  This means that
0512    depending on architecture this might only match symbols that
0513    represent function descriptor addresses (and not function entry
0514    addresses).  For these reasons it is recommended to use
0515    dwfl_module_addrinfo instead.  */
0516 extern const char *dwfl_module_addrsym (Dwfl_Module *mod, GElf_Addr address,
0517                     GElf_Sym *sym, GElf_Word *shndxp)
0518   __nonnull_attribute__ (3);
0519 
0520 /* Find the ELF section that *ADDRESS lies inside and return it.
0521    On success, adjusts *ADDRESS to be relative to the section,
0522    and sets *BIAS to the difference between addresses used in
0523    the returned section's headers and run-time addresses.  */
0524 extern Elf_Scn *dwfl_module_address_section (Dwfl_Module *mod,
0525                          Dwarf_Addr *address,
0526                          Dwarf_Addr *bias)
0527   __nonnull_attribute__ (2, 3);
0528 
0529 
0530 /*** Dwarf access functions ***/
0531 
0532 /* Fetch the module's debug information for use with libdw.
0533    If successful, fills in *BIAS with the difference between
0534    addresses within the loaded module and those  to use with libdw.  */
0535 extern Dwarf *dwfl_module_getdwarf (Dwfl_Module *, Dwarf_Addr *bias)
0536      __nonnull_attribute__ (2);
0537 
0538 /* Get the libdw handle for each module.  */
0539 extern ptrdiff_t dwfl_getdwarf (Dwfl *,
0540                 int (*callback) (Dwfl_Module *, void **,
0541                          const char *, Dwarf_Addr,
0542                          Dwarf *, Dwarf_Addr, void *),
0543                 void *arg, ptrdiff_t offset);
0544 
0545 /* Look up the module containing ADDR and return its debugging information,
0546    loading it if necessary.  */
0547 extern Dwarf *dwfl_addrdwarf (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Addr *bias)
0548      __nonnull_attribute__ (3);
0549 
0550 
0551 /* Find the CU containing ADDR and return its DIE.  */
0552 extern Dwarf_Die *dwfl_addrdie (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Addr *bias)
0553      __nonnull_attribute__ (3);
0554 extern Dwarf_Die *dwfl_module_addrdie (Dwfl_Module *mod,
0555                        Dwarf_Addr addr, Dwarf_Addr *bias)
0556      __nonnull_attribute__ (3);
0557 
0558 /* Iterate through the CUs, start with null for LASTCU.  */
0559 extern Dwarf_Die *dwfl_nextcu (Dwfl *dwfl, Dwarf_Die *lastcu, Dwarf_Addr *bias)
0560      __nonnull_attribute__ (3);
0561 extern Dwarf_Die *dwfl_module_nextcu (Dwfl_Module *mod,
0562                       Dwarf_Die *lastcu, Dwarf_Addr *bias)
0563      __nonnull_attribute__ (3);
0564 
0565 /* Return the module containing the CU DIE.  */
0566 extern Dwfl_Module *dwfl_cumodule (Dwarf_Die *cudie);
0567 
0568 
0569 /* Cache the source line information for the CU and return the
0570    number of Dwfl_Line entries it has.  */
0571 extern int dwfl_getsrclines (Dwarf_Die *cudie, size_t *nlines);
0572 
0573 /* Access one line number entry within the CU.  */
0574 extern Dwfl_Line *dwfl_onesrcline (Dwarf_Die *cudie, size_t idx);
0575 
0576 /* Get source for address.  */
0577 extern Dwfl_Line *dwfl_module_getsrc (Dwfl_Module *mod, Dwarf_Addr addr);
0578 extern Dwfl_Line *dwfl_getsrc (Dwfl *dwfl, Dwarf_Addr addr);
0579 
0580 /* Get address for source.  */
0581 extern int dwfl_module_getsrc_file (Dwfl_Module *mod,
0582                     const char *fname, int lineno, int column,
0583                     Dwfl_Line ***srcsp, size_t *nsrcs);
0584 
0585 /* Return the module containing this line record.  */
0586 extern Dwfl_Module *dwfl_linemodule (Dwfl_Line *line);
0587 
0588 /* Return the CU containing this line record.  */
0589 extern Dwarf_Die *dwfl_linecu (Dwfl_Line *line);
0590 
0591 /* Return the source file name and fill in other information.
0592    Arguments may be null for unneeded fields.  */
0593 extern const char *dwfl_lineinfo (Dwfl_Line *line, Dwarf_Addr *addr,
0594                   int *linep, int *colp,
0595                   Dwarf_Word *mtime, Dwarf_Word *length);
0596 
0597   /* Return the equivalent Dwarf_Line and the bias to apply to its address.  */
0598 extern Dwarf_Line *dwfl_dwarf_line (Dwfl_Line *line, Dwarf_Addr *bias);
0599 
0600 /* Return the compilation directory (AT_comp_dir) from this line's CU.  */
0601 extern const char *dwfl_line_comp_dir (Dwfl_Line *line);
0602 
0603 
0604 /*** Machine backend access functions ***/
0605 
0606 /* Return location expression to find return value given a
0607    DW_TAG_subprogram, DW_TAG_subroutine_type, or similar DIE describing
0608    function itself (whose DW_AT_type attribute describes its return type).
0609    The given DIE must come from the given module.  Returns -1 for errors.
0610    Returns zero if the function has no return value (e.g. "void" in C).
0611    Otherwise, *LOCOPS gets a location expression to find the return value,
0612    and returns the number of operations in the expression.  The pointer is
0613    permanently allocated at least as long as the module is live.  */
0614 extern int dwfl_module_return_value_location (Dwfl_Module *mod,
0615                           Dwarf_Die *functypedie,
0616                           const Dwarf_Op **locops);
0617 
0618 /* Enumerate the DWARF register numbers and their names.
0619    For each register, CALLBACK gets its DWARF number, a string describing
0620    the register set (such as "integer" or "FPU"), a prefix used in
0621    assembler syntax (such as "%" or "$", may be ""), and the name for the
0622    register (contains identifier characters only, possibly all digits).
0623    The REGNAME string is valid only during the callback. */
0624 extern int dwfl_module_register_names (Dwfl_Module *mod,
0625                        int (*callback) (void *arg,
0626                             int regno,
0627                             const char *setname,
0628                             const char *prefix,
0629                             const char *regname,
0630                             int bits, int type),
0631                        void *arg);
0632 
0633 
0634 /* Find the CFI for this module.  Returns NULL if there is no CFI.
0635    On success, fills in *BIAS with the difference between addresses
0636    within the loaded module and those in the CFI referring to it.
0637    The pointer returned can be used until the module is cleaned up.
0638    Calling these more than once returns the same pointers.
0639 
0640    dwfl_module_dwarf_cfi gets the '.debug_frame' information found with the
0641    rest of the DWARF information.  dwfl_module_eh_cfi gets the '.eh_frame'
0642    information found linked into the text.  A module might have either or
0643    both.  */
0644 extern Dwarf_CFI *dwfl_module_dwarf_cfi (Dwfl_Module *mod, Dwarf_Addr *bias);
0645 extern Dwarf_CFI *dwfl_module_eh_cfi (Dwfl_Module *mod, Dwarf_Addr *bias);
0646 
0647 
0648 typedef struct
0649 {
0650   /* Called to iterate through threads.  Returns next TID (thread ID) on
0651      success, a negative number on failure and zero if there are no more
0652      threads.  dwfl_errno () should be set if negative number has been
0653      returned.  *THREAD_ARGP is NULL on first call, and may be optionally
0654      set by the implementation. The value set by the implementation will
0655      be passed in on the next call to NEXT_THREAD.  THREAD_ARGP is never
0656      NULL.  *THREAD_ARGP will be passed to set_initial_registers or
0657      thread_detach callbacks together with Dwfl_Thread *thread.  This
0658      method must not be NULL.  */
0659   pid_t (*next_thread) (Dwfl *dwfl, void *dwfl_arg, void **thread_argp)
0660     __nonnull_attribute__ (1);
0661 
0662   /* Called to get a specific thread.  Returns true if there is a
0663      thread with the given thread id number, returns false if no such
0664      thread exists and will set dwfl_errno in that case.  THREAD_ARGP
0665      is never NULL.  *THREAD_ARGP will be passed to
0666      set_initial_registers or thread_detach callbacks together with
0667      Dwfl_Thread *thread.  This method may be NULL and will then be
0668      emulated using the next_thread callback. */
0669   bool (*get_thread) (Dwfl *dwfl, pid_t tid, void *dwfl_arg,
0670               void **thread_argp)
0671     __nonnull_attribute__ (1);
0672 
0673   /* Called during unwinding to access memory (stack) state.  Returns true for
0674      successfully read *RESULT or false and sets dwfl_errno () on failure.
0675      This method may be NULL - in such case dwfl_thread_getframes will return
0676      only the initial frame.  */
0677   bool (*memory_read) (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Word *result,
0678                        void *dwfl_arg)
0679     __nonnull_attribute__ (1, 3);
0680 
0681   /* Called on initial unwind to get the initial register state of the first
0682      frame.  Should call dwfl_thread_state_registers, possibly multiple times
0683      for different ranges and possibly also dwfl_thread_state_register_pc, to
0684      fill in initial (DWARF) register values.  After this call, till at least
0685      thread_detach is called, the thread is assumed to be frozen, so that it is
0686      safe to unwind.  Returns true on success or false and sets dwfl_errno ()
0687      on failure.  In the case of a failure thread_detach will not be called.
0688      This method must not be NULL.  */
0689   bool (*set_initial_registers) (Dwfl_Thread *thread, void *thread_arg)
0690     __nonnull_attribute__ (1);
0691 
0692   /* Called by dwfl_end.  All thread_detach method calls have been already
0693      done.  This method may be NULL.  */
0694   void (*detach) (Dwfl *dwfl, void *dwfl_arg)
0695     __nonnull_attribute__ (1);
0696 
0697   /* Called when unwinding is done.  No callback will be called after
0698      this method has been called.  Iff set_initial_registers was called for
0699      a TID and it returned success thread_detach will be called before the
0700      detach method above.  This method may be NULL.  */
0701   void (*thread_detach) (Dwfl_Thread *thread, void *thread_arg)
0702     __nonnull_attribute__ (1);
0703 } Dwfl_Thread_Callbacks;
0704 
0705 /* PID is the process id associated with the DWFL state.  Architecture of DWFL
0706    modules is specified by ELF, ELF must remain valid during DWFL lifetime.
0707    Use NULL ELF to detect architecture from DWFL, the function will then detect
0708    it from arbitrary Dwfl_Module of DWFL.  DWFL_ARG is the callback backend
0709    state.  DWFL_ARG will be provided to the callbacks.  *THREAD_CALLBACKS
0710    function pointers must remain valid during lifetime of DWFL.  Function
0711    returns true on success, false otherwise.  */
0712 bool dwfl_attach_state (Dwfl *dwfl, Elf *elf, pid_t pid,
0713                         const Dwfl_Thread_Callbacks *thread_callbacks,
0714             void *dwfl_arg)
0715   __nonnull_attribute__ (1, 4);
0716 
0717 /* Calls dwfl_attach_state with Dwfl_Thread_Callbacks setup for extracting
0718    thread state from the ELF core file.  Returns the pid number extracted
0719    from the core file, or -1 for errors.  */
0720 extern int dwfl_core_file_attach (Dwfl *dwfl, Elf *elf);
0721 
0722 /* Calls dwfl_attach_state with Dwfl_Thread_Callbacks setup for extracting
0723    thread state from the proc file system.  Uses ptrace to attach and stop
0724    the thread under inspection and detaches when thread_detach is called
0725    and unwinding for the thread is done, unless ASSUME_PTRACE_STOPPED is
0726    true.  If ASSUME_PTRACE_STOPPED is true the caller should make sure that
0727    the thread is ptrace attached and stopped before unwinding by calling
0728    either dwfl_thread_getframes or dwfl_getthread_frames.  Returns zero on
0729    success, -1 if dwfl_attach_state failed, or an errno code if opening the
0730    proc files failed.  */
0731 extern int dwfl_linux_proc_attach (Dwfl *dwfl, pid_t pid,
0732                    bool assume_ptrace_stopped);
0733 
0734 /* Return PID for the process associated with DWFL.  Function returns -1 if
0735    dwfl_attach_state was not called for DWFL.  */
0736 pid_t dwfl_pid (Dwfl *dwfl)
0737   __nonnull_attribute__ (1);
0738 
0739 /* Return DWFL from which THREAD was created using dwfl_getthreads.  */
0740 Dwfl *dwfl_thread_dwfl (Dwfl_Thread *thread)
0741   __nonnull_attribute__ (1);
0742 
0743 /* Return positive TID (thread ID) for THREAD.  This function never fails.  */
0744 pid_t dwfl_thread_tid (Dwfl_Thread *thread)
0745   __nonnull_attribute__ (1);
0746 
0747 /* Return thread for frame STATE.  This function never fails.  */
0748 Dwfl_Thread *dwfl_frame_thread (Dwfl_Frame *state)
0749   __nonnull_attribute__ (1);
0750 
0751 /* Called by Dwfl_Thread_Callbacks.set_initial_registers implementation.
0752    For every known continuous block of registers <FIRSTREG..FIRSTREG+NREGS)
0753    (inclusive..exclusive) set their content to REGS (array of NREGS items).
0754    Function returns false if any of the registers has invalid number.  */
0755 bool dwfl_thread_state_registers (Dwfl_Thread *thread, int firstreg,
0756                                   unsigned nregs, const Dwarf_Word *regs)
0757   __nonnull_attribute__ (1, 4);
0758 
0759 /* Called by Dwfl_Thread_Callbacks.set_initial_registers implementation.
0760    If PC is not contained among DWARF registers passed by
0761    dwfl_thread_state_registers on the target architecture pass the PC value
0762    here.  */
0763 void dwfl_thread_state_register_pc (Dwfl_Thread *thread, Dwarf_Word pc)
0764   __nonnull_attribute__ (1);
0765 
0766 /* Iterate through the threads for a process.  Returns zero if all threads have
0767    been processed by the callback, returns -1 on error, or the value of the
0768    callback when not DWARF_CB_OK.  -1 returned on error will set dwfl_errno ().
0769    Keeps calling the callback with the next thread while the callback returns
0770    DWARF_CB_OK, till there are no more threads.  */
0771 int dwfl_getthreads (Dwfl *dwfl,
0772              int (*callback) (Dwfl_Thread *thread, void *arg),
0773              void *arg)
0774   __nonnull_attribute__ (1, 2);
0775 
0776 /* Iterate through the frames for a thread.  Returns zero if all frames
0777    have been processed by the callback, returns -1 on error, or the value of
0778    the callback when not DWARF_CB_OK.  -1 returned on error will
0779    set dwfl_errno ().  Some systems return error instead of zero on end of the
0780    backtrace, for cross-platform compatibility callers should consider error as
0781    a zero.  Keeps calling the callback with the next frame while the callback
0782    returns DWARF_CB_OK, till there are no more frames.  On start will call the
0783    set_initial_registers callback and on return will call the detach_thread
0784    callback of the Dwfl_Thread.  */
0785 int dwfl_thread_getframes (Dwfl_Thread *thread,
0786                int (*callback) (Dwfl_Frame *state, void *arg),
0787                void *arg)
0788   __nonnull_attribute__ (1, 2);
0789 
0790 /* Like dwfl_thread_getframes, but specifying the thread by its unique
0791    identifier number.  Returns zero if all frames have been processed
0792    by the callback, returns -1 on error (and when no thread with
0793    the given thread id number exists), or the value of the callback
0794    when not DWARF_CB_OK.  -1 returned on error will set dwfl_errno ().  */
0795 int dwfl_getthread_frames (Dwfl *dwfl, pid_t tid,
0796                int (*callback) (Dwfl_Frame *thread, void *arg),
0797                void *arg)
0798   __nonnull_attribute__ (1, 3);
0799 
0800 /* Return *PC (program counter) for thread-specific frame STATE.
0801    Set *ISACTIVATION according to DWARF frame "activation" definition.
0802    Typically you need to subtract 1 from *PC if *ACTIVATION is false to safely
0803    find function of the caller.  ACTIVATION may be NULL.  PC must not be NULL.
0804    Function returns false if it failed to find *PC.  */
0805 bool dwfl_frame_pc (Dwfl_Frame *state, Dwarf_Addr *pc, bool *isactivation)
0806   __nonnull_attribute__ (1, 2);
0807 
0808 /* Get the value of the DWARF register number in the given frame.
0809    Returns zero on success, -1 on error (invalid DWARF register
0810    number) or 1 if the value of the register in the frame is unknown.  */
0811 int dwfl_frame_reg (Dwfl_Frame *state, unsigned regno, Dwarf_Word *val)
0812   __nonnull_attribute__ (1);
0813 
0814 /* Return the internal debuginfod-client connection handle for the DWFL session.
0815    When the client connection has not yet been initialized, it will be done on the
0816    first call to this function. If elfutils is compiled without support for debuginfod,
0817    NULL will be returned.
0818  */
0819 extern debuginfod_client *dwfl_get_debuginfod_client (Dwfl *dwfl);
0820 
0821 #ifdef __cplusplus
0822 }
0823 #endif
0824 
0825 #endif  /* libdwfl.h */