DWARF_GET_PUBTYPES(3)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1, nixpkgs unstable

πŸ’– DWARF_GET_PUBTYPES πŸ’–

okay so bestie, this one's from the Library Functions Manual and it's literally all about the libdwarf girlies who fetch info about your user-defined types. stay with me, it's actually so good πŸŽ€


✨ LIBRARY ✨

DWARF Access Library (libdwarf, -ldwarf) 🫢


🌸 SYNOPSIS 🌸

#include <libdwarf.h>

int
dwarf_get_pubtypes(Dwarf_Debug dbg, Dwarf_Type **types,
    Dwarf_Signed *ntypes, Dwarf_Error *err);

int
dwarf_pubtype_cu_offset(Dwarf_Type type, Dwarf_Off *cu_offset,
    Dwarf_Error *err);

int
dwarf_pubtype_die_offset(Dwarf_Type type, Dwarf_Off *die_offset,
    Dwarf_Error *err);

int
dwarf_pubtype_name_offsets(Dwarf_Type type, char **name,
    Dwarf_Off *die_offset, Dwarf_Off *cu_die_offset, Dwarf_Error *err);

int
dwarf_pubtypename(Dwarf_Type type, char **name, Dwarf_Error *err);

πŸ¦‹ DESCRIPTION πŸ¦‹

So these functions pull info about file-scope, user-defined types that are recorded in lookup tables inside the .debug_pubtypes DWARF section. The info comes back to you in opaque descriptors of type Dwarf_Type β€” and bestie, opaque means you can't just peek inside them yourself, you have to use the functions below to get the name and offset info out of them. iykyk πŸ’…

dwarf_get_pubtypes() grabs descriptors for all the user-defined types hanging out with the DWARF debug context you passed as dbg. The types argument should point to a spot that'll get set to a pointer to an array of Dwarf_Type descriptors, and ntypes should point to a spot that'll get set to the number of descriptors you got back. slay ✨

dwarf_pubtype_cu_offset() hands you the offset β€” relative to the .debug_info section β€” of the compilation unit that contains the debugging information entry associated with type. Point cu_offset at a location and it'll hold the returned offset for you.

dwarf_pubtype_die_offset() gets the offset β€” again, relative to .debug_info β€” of the debugging information entry associated with type, and stashes it in the location die_offset points to. πŸŽ€

dwarf_pubtype_name_offsets() is the triple threat: it gets the name and the offsets for the debugging information entry of type. Here's the breakdown:

  • name should point to a location that gets set to a pointer to a NUL-terminated string with the name of the associated debugging information entry.
  • die_offset should point to a location that gets set to the offset β€” relative to .debug_info β€” of the associated debugging information entry.
  • cu_die_offset should point to a location that gets set to the offset β€” relative to .debug_info β€” of the first debugging information entry in the compilation unit associated with type.

dwarf_pubtypename() sets the location name points to to a pointer to a NUL-terminated string holding the name of the debugging information entry associated with type. short n sweet fr fr πŸ’–

🧚 Memory Management 🧚

The memory area for the array of Dwarf_Type descriptors you get back in types from dwarf_get_pubtypes() is owned by the DWARF Access Library (libdwarf, -ldwarf). Do not try to free that pointer directly, bestie β€” that's not your bag. Portable code should instead call dwarf_types_dealloc(3) to say "yeah this memory area can be freed now". πŸ™ˆ

Same energy for the string that comes back in the name argument of dwarf_pubtype_name_offsets() and dwarf_pubtypename() β€” that memory area is owned by the DWARF Access Library (libdwarf, -ldwarf) too. Portable code should flag that it can be freed with dwarf_dealloc(3). 🫢

πŸ’€ Error Returns πŸ’€

If err isn't NULL, these functions will use it to store error info whenever something goes wrong. very considerate of them honestly.


⭐ RETURN VALUES ⭐

On success, these functions return DW_DLV_OK. In case of an error, they return DW_DLV_ERROR and set the err argument. periodt. πŸ‘‘


🌈 EXAMPLES 🌈

Wanna grab the list of file-scope user-defined types and print their names? say less:

Dwarf_Debug dbg;
Dwarf_Signed ntypes;
Dwarf_Type *types;
Dwarf_Error err;
int n, result;
char *typename;

/* Initialize dbg etc. */;
result = dwarf_get_pubtypes(dbg, &types, &ntypes, &err);
if (result != DW_DLV_OK) /* Handle the error. */
        ;

/* Iterate over the returned array of descriptors. */
for (n = 0; n < ntypes; n++) {
        result = dwarf_pubtypename(types[n], &typename, &err);
        if (result != DW_DLV_OK) /* Handle the error. */
                ;
        printf("%s\n", typename);
}

/* Deallocate the returned array. */
dwarf_types_dealloc(dbg, types, ntypes);

πŸ”₯ ERRORS πŸ”₯

These functions may fail with the following errors:

  • [DW_DLE_ARGUMENT] β€” one of the arguments cu_die_offset, cu_offset, dbg, die_offset, type, types, name, or ntypes was NULL. (yes, all of those. the whole squad.)
  • [DW_DLE_NO_ENTRY] β€” the DWARF debugging context referenced by dbg just did not contain information about user-defined types. it's giving empty. 😭

πŸ‘‘ SEE ALSO πŸ‘‘

dwarf(3), dwarf_dealloc(3), dwarf_get_cu_die_offset_given_cu_header_offset(3), dwarf_pubtypes_dealloc(3)


ngl the vibes here are immaculate β€” go read the whole dwarf family, they're iconic πŸ’œ

okay that's the whole page bestie, go forth and fetch those pubtypes βœ¨πŸ’…πŸ“

Debian 🌸 April 4, 2011 β€” mwah, that's the source, the version and the date, and this page is officially a wrap πŸŽ€πŸ¦‹πŸ’–

πŸ“¦ where this page comes from