Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:55:01

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