Skip to content

Commit

Permalink
Refactor extracting CU tags from DWARF (#1495)
Browse files Browse the repository at this point in the history
This centralizes the process and gives readable names to what's
happening.
  • Loading branch information
hainest committed Aug 28, 2023
1 parent 7f1e24d commit 71f2e14
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
29 changes: 29 additions & 0 deletions dwarf/h/dwarf_cu_info.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef DWARFDYNINST_DWARF_CU_INFO_HPP
#define DWARFDYNINST_DWARF_CU_INFO_HPP

#include <dwarf.h>
#include <elfutils/libdw.h>
#include <string>

namespace Dyninst { namespace DwarfDyninst {

inline bool is_fullcu(Dwarf_Die die) { return dwarf_tag(&die) == DW_TAG_compile_unit; }
inline bool is_partialcu(Dwarf_Die die) { return dwarf_tag(&die) == DW_TAG_partial_unit; }
inline bool is_typecu(Dwarf_Die die) { return dwarf_tag(&die) == DW_TAG_type_unit; }

inline bool is_cudie(Dwarf_Die die) {
// If there is an inner CU attribute, then it's not a CU
if (die.cu)
return false;

// These are best guess. Ideally, we'd like to interrogate
// the internals of the die, but that's not currently possible
// with libdw. The internal function there is `is_cudie`.
//
// We purposefully don't include DW_TAG_skeleton_unit here as
// libdw should merge those into a single CU for us.
return is_fullcu(die) || is_partialcu(die) || is_typecu(die);
}
}}

#endif
10 changes: 6 additions & 4 deletions symtabAPI/src/Object-elf.C
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#include "common/src/vgannotations.h"
#include "unaligned_memory_access.h"
#include <dwarf_cu_info.hpp>

#include "Type.h"
#include "Variable.h"
Expand Down Expand Up @@ -2184,8 +2185,10 @@ bool Object::fix_global_symbol_modules_static_dwarf() {
Dwarf_Die cu_die, *cu_die_p;
cu_die_p = dwarf_offdie(dbg, cu_die_off, &cu_die);

Dwarf_Half moduleTag = dwarf_tag(&cu_die);
if (moduleTag != DW_TAG_compile_unit) {
// As of DWARF 5, only full and partial CUs contain debug info for symbols
bool const is_partialcu = DwarfDyninst::is_partialcu(cu_die);
bool const is_fullcu = DwarfDyninst::is_fullcu(cu_die);
if (!(is_partialcu || is_fullcu)) {
continue;
}

Expand Down Expand Up @@ -3105,8 +3108,7 @@ void Object::getModuleLanguageInfo(dyn_hash_map<string, supportedLanguages> *mod
cu_die_p = dwarf_offdie(dbg, cu_die_off, &moduleDIE);
if (cu_die_p == NULL) break;

Dwarf_Half moduleTag = dwarf_tag(&moduleDIE);
if (moduleTag != DW_TAG_compile_unit) {
if (!DwarfDyninst::is_fullcu(moduleDIE)) {
continue;
}

Expand Down
13 changes: 6 additions & 7 deletions symtabAPI/src/dwarfWalker.C
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "Type-mem.h"
#include <elfutils/libdw.h>
#include <dwarf/src/dwarf_subrange.h>
#include <dwarf_cu_info.hpp>
#include <stack>

using namespace Dyninst;
Expand Down Expand Up @@ -227,14 +228,11 @@ bool DwarfWalker::parse() {
}
bool DwarfWalker::parseModule(Dwarf_Die moduleDIE, Module *&fixUnknownMod) {

/* Make sure we've got the right one. */
Dwarf_Half moduleTag;
moduleTag = dwarf_tag(&moduleDIE);

if (moduleTag != DW_TAG_compile_unit
&& moduleTag != DW_TAG_partial_unit
&& moduleTag != DW_TAG_type_unit)
// Make sure `moduleDIE` is actually a compilation unit
if (!DwarfDyninst::is_cudie(moduleDIE)) {
dwarf_printf("(0x%lx) Attempting to parse module that isn't a compilation unit", id());
return false;
}

/* Extract the name of this module. */
std::string moduleName = die_name(moduleDIE);
Expand All @@ -244,6 +242,7 @@ bool DwarfWalker::parseModule(Dwarf_Die moduleDIE, Module *&fixUnknownMod) {
setModuleFromName(moduleName);
dwarf_printf("Mapped to Symtab module 0x%p %s\n", (void*)mod(), mod()->fileName().c_str());

auto moduleTag = dwarf_tag(&moduleDIE);
if (moduleName.empty() && moduleTag == DW_TAG_type_unit) {
uint64_t sig8 = * reinterpret_cast<uint64_t*>(&signature);
char buf[20];
Expand Down

0 comments on commit 71f2e14

Please sign in to comment.