DWARF_GET_TYPES(3)

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

πŸ’… DWARF_GET_TYPES(3) β€” Library Functions Manual, bestie edition πŸŽ€

okay so this is a <pink>Library Functions Manual</pink> page and honestly? it's a whole vibe. we're talking about dwarf_get_types, dwarf_type_cu_offset, dwarf_type_die_offset, dwarf_type_name_offsets, and dwarf_typename β€” five iconic little functions whose entire job is pulling info about user-defined types. let's get into it πŸ’–


✨ NAME ✨

dwarf_get_types, dwarf_type_cu_offset, dwarf_type_die_offset, dwarf_type_name_offsets, dwarf_typename β€” these ones retrieve information about user-defined types. that's the whole assignment. 🫢


πŸ“š LIBRARY πŸ“š

DWARF Access Library (libdwarf, -ldwarf). yeah, link it. -ldwarf. πŸ’…


🧚 SYNOPSIS 🧚

#include <libdwarf.h>

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

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

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

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

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

like, look at them. all five. lined up. main character energy fr fr ⭐


🌸 DESCRIPTION 🌸

so these APIs retrieve information about user-defined types, and they're pulling it out of the SGI-specific .debug_typenames section. (yes, that's the SGI-specific one, we'll get to that.)

⚠️ ok bestie this is a real heads-up: standards-conformant applications should use dwarf_get_pubtypes(3), dwarf_pubtype_cu_offset(3), dwarf_pubtype_die_offset(3), dwarf_pubtype_name_offsets(3) and dwarf_pubtypename(3) instead β€” those operate on the equivalent .debug_pubtypes section defined by the DWARF3 standard. DWARF3 standard. so like, if you're being portable and correct, that's your lane. iykyk πŸ’€

Information about user-defined types is returned using opaque descriptors of type Dwarf_Type. they're opaque β€” you don't peek inside, you don't poke at them, you don't manifest your own. you use the functions described below to retrieve the name and offset info contained in these descriptors. periodt. πŸ‘‘

dwarf_get_types() retrieves descriptors for all user-defined types associated with the DWARF debug context specified by argument dbg. The argument types should point to a location that will be set to a pointer to an array of Dwarf_Type descriptors. The argument ntypes should point to a location that will be set to the number of descriptors returned. so you hand it a place to put an array, and a place to put a count. slay. πŸ’…

dwarf_type_cu_offset() returns the offset, relative to the .debug_info section, of the compilation unit that contains the debugging information entry associated with the argument type. Argument cu_offset should point to a location that will hold the returned offset. πŸŽ€

dwarf_type_die_offset() retrieves the offset, relative to the .debug_info section, of the debugging information entry associated with the argument type, and stores it into the location pointed to by the argument die_offset. ✨

dwarf_type_name_offsets() retrieves the name and offsets for the debugging information entry for argument type. and it does all three at once, highkey efficient:

  • Argument name should point to a location which will be set to a pointer to a NUL-terminated string containing the name of the associated debugging information entry.
  • Argument die_offset should point to a location which will be set to the offset, relative to the .debug_info section, of the associated debugging information entry.
  • Argument cu_die_offset should point to a location which will be set to a offset, relative to the .debug_info section, of the first debugging information entry in the compilation unit associated with argument type.

dwarf_typename() sets the location pointed to by argument name to a pointer to a NUL-terminated string holding the name of the debugging information entry associated with the argument type. just the name. no offsets. minimal. iconic. πŸ¦‹


πŸ“ Memory Management πŸ“

okay this part matters, don't skip it, i'm serious 😭

The memory area used for the array of Dwarf_Type descriptors returned in argument types by function dwarf_get_types() is owned by the DWARF Access Library (libdwarf, -ldwarf). Application code should not attempt to directly free this pointer β€” like, do not call free() on it, bestie, it's not yours. Portable code should instead use the function dwarf_types_dealloc(3) to indicate that the memory area may be freed. 🫢

The memory area used for the string returned in the name argument to functions dwarf_type_name_offsets() and dwarf_typename() is owned by the DWARF Access Library (libdwarf, -ldwarf). Portable code should indicate that the memory area can be freed using the dwarf_dealloc(3) function. use dwarf_dealloc(3). not plain free(). i'm begging. πŸ™ˆ


πŸ’€ Error Returns πŸ’€

If argument err is not NULL, these functions will use it to store error information, in case of an error. simple as that. πŸ’œ


πŸ”₯ RETURN VALUES πŸ”₯

On success, these functions returns DW_DLV_OK. (yes the original says "returns", we're keeping it, it's part of the lore 😌). In case of an error, they return DW_DLV_ERROR and set the argument err. so DW_DLV_OK = good, DW_DLV_ERROR = check your err. ⭐


🌈 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. you passed a null. it's giving "forgot to initialize". πŸ’€
  • [DW_DLE_NO_ENTRY] β€” The DWARF debugging context referenced by argument dbg did not contain information about user-defined types. nothing to find, nothing to give. 😭

πŸŽ€ SEE ALSO πŸŽ€

dwarf(3), dwarf_get_cu_die_offset_given_cu_header_offset(3), dwarf_get_pubtypes(3), dwarf_pubtype_cu_offset(3), dwarf_pubtype_die_offset(3), dwarf_pubtype_name_offsets(3), dwarf_pubtypename(3), dwarf_types_dealloc(3)

like, read them. they're all connected. it's a whole ecosystem. 🌸


and that's the page!! ✨ this one shipped from Debian, dated April 10, 2011 β€” vintage, honestly, but it still ate. go forth and read your user-defined types responsibly πŸ’–πŸ¦‹πŸ‘‘

πŸ“¦ where this page comes from