DWARF_GET_PUBTYPES(3)
π 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:
nameshould 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_offsetshould point to a location that gets set to the offset β relative to.debug_infoβ of the associated debugging information entry.cu_die_offsetshould 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 withtype.
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 argumentscu_die_offset,cu_offset,dbg,die_offset,type,types,name, orntypeswasNULL. (yes, all of those. the whole squad.)[DW_DLE_NO_ENTRY]β the DWARF debugging context referenced bydbgjust 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
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: libdwarf 15.0.0
- Copyright (c) 2011 Kai Wang
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms