Skip to content

Commit ab40aca

Browse files
committed
[lldb][Expression] Encode structor variant into FunctionCallLabel
This patch is an implementation of [this discussion](https://discourse.llvm.org/t/rfc-lldb-handling-abi-tagged-constructors-destructors-in-expression-evaluator/82816/7) about handling ABI-tagged structors during expression evaluation. **Motivation** LLDB encodes the mangled name of a `DW_TAG_subprogram` into `AsmLabel`s on function and method Clang AST nodes. This means that when calls to these functions get lowered into IR (when running JITted expressions), the address resolver can locate the appropriate symbol by mangled name (and it's guaranteed to find the symbol because we got the mangled name from debug-info, instead of letting Clang mangle it based on AST structure). However, we don't do this for `CXXConstructorDecl`s/`CXXDestructorDecl`s because these structor declarations in DWARF don't have a linkage name. This is because there can be multiple variants of a structor, each with a distinct mangling in the Itanium ABI. Each structor variant has its own definition `DW_TAG_subprogram`. So LLDB doesn't know which mangled name to put into the `AsmLabel`. Currently this means using an ABI-tagged constructor in LLDB expressions won't work (see [this RFC](https://discourse.llvm.org/t/rfc-lldb-handling-abi-tagged-constructors-destructors-in-expression-evaluator/82816) for concrete examples). **Proposed Solution** The `FunctionCallLabel` encoding that we put into `AsmLabel`s already supports stuffing more info about a DIE into it. So this patch extends the `FunctionCallLabel` to contain an optional discriminator (a sequence of bytes) which the `SymbolFileDWARF` plugin interprets as the constructor/destructor variant of that DIE. There's a few subtleties here: 1. At the point at which LLDB first constructs the label, it has no way of knowing (just by looking the debug-info declaration), which structor variant the expression evaluator is supposed to call. That's something that gets decided when compiling the expression. So we let the Clang mangler inject the correct structor variant into the `AsmLabel` during JITing. I adjusted the `AsmLabelAttr` mangling for this. An option would've been to create a new Clang attribute which behaved like an `AsmLabel` but with these special semantics for LLDB. My main concern there is that we'd have to adjust all the `AsmLabelAttr` checks around Clang to also now account for this new attribute. 2. The compiler is free to omit the `C1` variant of a constructor if the `C2` variant is sufficient. In that case it may alias `C1` to `C2`, leaving us with only the `C2` `DW_TAG_subprogram` in the object file. Linux is one of the platforms where this occurs. For those cases there's heuristic in `SymbolFileDWARF` where we pick `C2` if we asked for `C1` but it doesn't exist. This may not always be correct (if for some reason the compiler decided to drop `C1` for other reasons).
1 parent a650687 commit ab40aca

File tree

7 files changed

+234
-57
lines changed

7 files changed

+234
-57
lines changed

lldb/include/lldb/Expression/Expression.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,15 @@ class Expression {
103103
///
104104
/// The format being:
105105
///
106-
/// <prefix>:<module uid>:<symbol uid>:<name>
106+
/// <prefix>:<discriminator>:<module uid>:<symbol uid>:<name>
107107
///
108108
/// The label string needs to stay valid for the entire lifetime
109109
/// of this object.
110110
struct FunctionCallLabel {
111+
/// Arbitrary string which language plugins can interpret for their
112+
/// own needs.
113+
llvm::StringRef discriminator;
114+
111115
/// Unique identifier of the lldb_private::Module
112116
/// which contains the symbol identified by \c symbol_id.
113117
lldb::user_id_t module_id;
@@ -133,7 +137,7 @@ struct FunctionCallLabel {
133137
///
134138
/// The representation roundtrips through \c fromString:
135139
/// \code{.cpp}
136-
/// llvm::StringRef encoded = "$__lldb_func:0x0:0x0:_Z3foov";
140+
/// llvm::StringRef encoded = "$__lldb_func:blah:0x0:0x0:_Z3foov";
137141
/// FunctionCallLabel label = *fromString(label);
138142
///
139143
/// assert (label.toString() == encoded);

lldb/source/Expression/Expression.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,21 @@ Expression::Expression(ExecutionContextScope &exe_scope)
3434

3535
llvm::Expected<FunctionCallLabel>
3636
lldb_private::FunctionCallLabel::fromString(llvm::StringRef label) {
37-
llvm::SmallVector<llvm::StringRef, 4> components;
38-
label.split(components, ":", /*MaxSplit=*/3);
37+
llvm::SmallVector<llvm::StringRef, 5> components;
38+
label.split(components, ":", /*MaxSplit=*/4);
3939

40-
if (components.size() != 4)
40+
if (components.size() != 5)
4141
return llvm::createStringError("malformed function call label.");
4242

4343
if (components[0] != FunctionCallLabelPrefix)
4444
return llvm::createStringError(llvm::formatv(
4545
"expected function call label prefix '{0}' but found '{1}' instead.",
4646
FunctionCallLabelPrefix, components[0]));
4747

48-
llvm::StringRef module_label = components[1];
49-
llvm::StringRef die_label = components[2];
48+
llvm::StringRef discriminator = components[1];
49+
llvm::StringRef module_label = components[2];
50+
llvm::StringRef die_label = components[3];
51+
llvm::StringRef lookup_name = components[4];
5052

5153
lldb::user_id_t module_id = 0;
5254
if (!llvm::to_integer(module_label, module_id))
@@ -58,20 +60,23 @@ lldb_private::FunctionCallLabel::fromString(llvm::StringRef label) {
5860
return llvm::createStringError(
5961
llvm::formatv("failed to parse symbol ID from '{0}'.", die_label));
6062

61-
return FunctionCallLabel{/*.module_id=*/module_id,
63+
return FunctionCallLabel{/*.discriminator=*/discriminator,
64+
/*.module_id=*/module_id,
6265
/*.symbol_id=*/die_id,
63-
/*.lookup_name=*/components[3]};
66+
/*.lookup_name=*/lookup_name};
6467
}
6568

6669
std::string lldb_private::FunctionCallLabel::toString() const {
67-
return llvm::formatv("{0}:{1:x}:{2:x}:{3}", FunctionCallLabelPrefix,
68-
module_id, symbol_id, lookup_name)
70+
return llvm::formatv("{0}:{1}:{2:x}:{3:x}:{4}", FunctionCallLabelPrefix,
71+
discriminator, module_id, symbol_id, lookup_name)
6972
.str();
7073
}
7174

7275
void llvm::format_provider<FunctionCallLabel>::format(
7376
const FunctionCallLabel &label, raw_ostream &OS, StringRef Style) {
74-
OS << llvm::formatv("FunctionCallLabel{ module_id: {0:x}, symbol_id: {1:x}, "
75-
"lookup_name: {2} }",
76-
label.module_id, label.symbol_id, label.lookup_name);
77+
OS << llvm::formatv("FunctionCallLabel{ discriminator: {0}, module_id: "
78+
"{1:x}, symbol_id: {2:x}, "
79+
"lookup_name: {3} }",
80+
label.discriminator, label.module_id, label.symbol_id,
81+
label.lookup_name);
7782
}

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,52 @@ static unsigned GetCXXMethodCVQuals(const DWARFDIE &subprogram,
250250
return cv_quals;
251251
}
252252

253+
static const char *GetMangledOrStructorName(const DWARFDIE &die) {
254+
const char *name = die.GetMangledName(/*substitute_name_allowed*/ false);
255+
if (name)
256+
return name;
257+
258+
name = die.GetName();
259+
if (!name)
260+
return nullptr;
261+
262+
DWARFDIE parent = die.GetParent();
263+
if (!parent.IsStructUnionOrClass())
264+
return nullptr;
265+
266+
const char *parent_name = parent.GetName();
267+
if (!parent_name)
268+
return nullptr;
269+
270+
// Constructor.
271+
if (::strcmp(parent_name, name) == 0)
272+
return name;
273+
274+
// Destructor.
275+
if (name[0] == '~' && ::strcmp(parent_name, name + 1))
276+
return name;
277+
278+
return nullptr;
279+
}
280+
253281
static std::string MakeLLDBFuncAsmLabel(const DWARFDIE &die) {
254-
char const *name = die.GetMangledName(/*substitute_name_allowed*/ false);
282+
char const *name = GetMangledOrStructorName(die);
255283
if (!name)
256284
return {};
257285

286+
auto *cu = die.GetCU();
287+
if (!cu)
288+
return {};
289+
290+
// FIXME: When resolving function call labels, we check that
291+
// that the definition's DW_AT_specification points to the
292+
// declaration that we encoded into the label here. But if the
293+
// declaration came from a type-unit (and the definition from
294+
// .debug_info), that check won't work. So for now, don't use
295+
// function call labels for declaration DIEs from type-units.
296+
if (cu->IsTypeUnit())
297+
return {};
298+
258299
SymbolFileDWARF *dwarf = die.GetDWARF();
259300
if (!dwarf)
260301
return {};
@@ -285,7 +326,9 @@ static std::string MakeLLDBFuncAsmLabel(const DWARFDIE &die) {
285326
if (die_id == LLDB_INVALID_UID)
286327
return {};
287328

288-
return FunctionCallLabel{/*module_id=*/module_id,
329+
// Note, discriminator is added by Clang during mangling.
330+
return FunctionCallLabel{/*discriminator=*/{},
331+
/*module_id=*/module_id,
289332
/*symbol_id=*/die_id,
290333
/*.lookup_name=*/name}
291334
.toString();

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 133 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "SymbolFileDWARF.h"
10+
#include "clang/Basic/ABI.h"
1011
#include "llvm/ADT/STLExtras.h"
12+
#include "llvm/ADT/StringExtras.h"
1113
#include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
1214
#include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
1315
#include "llvm/Support/Casting.h"
@@ -78,6 +80,7 @@
7880

7981
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
8082
#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
83+
#include "llvm/Demangle/Demangle.h"
8184
#include "llvm/Support/FileSystem.h"
8285
#include "llvm/Support/FormatVariadic.h"
8386

@@ -2482,6 +2485,134 @@ bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
24822485
return false;
24832486
}
24842487

2488+
static int ClangToItaniumCtorKind(clang::CXXCtorType kind) {
2489+
switch (kind) {
2490+
case clang::CXXCtorType::Ctor_Complete:
2491+
return 1;
2492+
case clang::CXXCtorType::Ctor_Base:
2493+
return 2;
2494+
case clang::CXXCtorType::Ctor_CopyingClosure:
2495+
case clang::CXXCtorType::Ctor_DefaultClosure:
2496+
case clang::CXXCtorType::Ctor_Comdat:
2497+
llvm_unreachable("Unexpected constructor kind.");
2498+
}
2499+
}
2500+
2501+
static int ClangToItaniumDtorKind(clang::CXXDtorType kind) {
2502+
switch (kind) {
2503+
case clang::CXXDtorType::Dtor_Deleting:
2504+
return 0;
2505+
case clang::CXXDtorType::Dtor_Complete:
2506+
return 1;
2507+
case clang::CXXDtorType::Dtor_Base:
2508+
return 2;
2509+
case clang::CXXDtorType::Dtor_Comdat:
2510+
llvm_unreachable("Unexpected destructor kind.");
2511+
}
2512+
}
2513+
2514+
static std::optional<int>
2515+
GetItaniumCtorDtorVariant(llvm::StringRef discriminator) {
2516+
const bool is_ctor = discriminator.consume_front("C");
2517+
if (!is_ctor && !discriminator.consume_front("D"))
2518+
return std::nullopt;
2519+
2520+
uint64_t structor_kind;
2521+
if (!llvm::to_integer(discriminator, structor_kind))
2522+
return std::nullopt;
2523+
2524+
if (is_ctor) {
2525+
if (structor_kind > clang::CXXCtorType::Ctor_DefaultClosure)
2526+
return std::nullopt;
2527+
2528+
return ClangToItaniumCtorKind(
2529+
static_cast<clang::CXXCtorType>(structor_kind));
2530+
}
2531+
2532+
if (structor_kind > clang::CXXDtorType::Dtor_Deleting)
2533+
return std::nullopt;
2534+
2535+
return ClangToItaniumDtorKind(static_cast<clang::CXXDtorType>(structor_kind));
2536+
}
2537+
2538+
DWARFDIE SymbolFileDWARF::FindFunctionDefinition(const FunctionCallLabel &label,
2539+
const DWARFDIE &declaration) {
2540+
DWARFDIE definition;
2541+
llvm::DenseMap<int, DWARFDIE> structor_variant_to_die;
2542+
2543+
// eFunctionNameTypeFull for mangled name lookup.
2544+
// eFunctionNameTypeMethod is required for structor lookups (since we look
2545+
// those up by DW_AT_name).
2546+
Module::LookupInfo info(ConstString(label.lookup_name),
2547+
lldb::eFunctionNameTypeFull |
2548+
lldb::eFunctionNameTypeMethod,
2549+
lldb::eLanguageTypeUnknown);
2550+
2551+
m_index->GetFunctions(info, *this, {}, [&](DWARFDIE entry) {
2552+
if (entry.GetAttributeValueAsUnsigned(llvm::dwarf::DW_AT_declaration, 0))
2553+
return IterationAction::Continue;
2554+
2555+
auto spec = entry.GetAttributeValueAsReferenceDIE(DW_AT_specification);
2556+
if (!spec)
2557+
return IterationAction::Continue;
2558+
2559+
if (spec != declaration)
2560+
return IterationAction::Continue;
2561+
2562+
// We're not picking a specific structor variant DIE, so we're done here.
2563+
if (label.discriminator.empty()) {
2564+
definition = entry;
2565+
return IterationAction::Stop;
2566+
}
2567+
2568+
const char *mangled =
2569+
entry.GetMangledName(/*substitute_name_allowed=*/false);
2570+
if (!mangled)
2571+
return IterationAction::Continue;
2572+
2573+
llvm::ItaniumPartialDemangler D;
2574+
if (D.partialDemangle(mangled))
2575+
return IterationAction::Continue;
2576+
2577+
auto structor_variant = D.getCtorOrDtorVariant();
2578+
if (!structor_variant)
2579+
return IterationAction::Continue;
2580+
2581+
auto [_, inserted] = structor_variant_to_die.try_emplace(*structor_variant,
2582+
std::move(entry));
2583+
assert(inserted);
2584+
2585+
// The compiler may choose to alias the constructor variants
2586+
// (notably this happens on Linux), so we might not have a definition
2587+
// DIE for some structor variants. Hence we iterate over all variants
2588+
// and pick the most appropriate one out of those.
2589+
return IterationAction::Continue;
2590+
});
2591+
2592+
if (definition.IsValid())
2593+
return definition;
2594+
2595+
auto label_variant = GetItaniumCtorDtorVariant(label.discriminator);
2596+
if (!label_variant)
2597+
return {};
2598+
2599+
auto it = structor_variant_to_die.find(*label_variant);
2600+
2601+
// Found the exact variant.
2602+
if (it != structor_variant_to_die.end())
2603+
return it->getSecond();
2604+
2605+
// We need a C1 constructor. If debug-info only contains a DIE for C2,
2606+
// assume C1 was aliased to C2.
2607+
if (!label.lookup_name.starts_with("~") && label_variant == 1) {
2608+
if (auto it = structor_variant_to_die.find(2);
2609+
it != structor_variant_to_die.end())
2610+
return it->getSecond();
2611+
}
2612+
2613+
return {};
2614+
}
2615+
24852616
llvm::Expected<SymbolContext>
24862617
SymbolFileDWARF::ResolveFunctionCallLabel(const FunctionCallLabel &label) {
24872618
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
@@ -2494,25 +2625,8 @@ SymbolFileDWARF::ResolveFunctionCallLabel(const FunctionCallLabel &label) {
24942625
// Label was created using a declaration DIE. Need to fetch the definition
24952626
// to resolve the function call.
24962627
if (die.GetAttributeValueAsUnsigned(llvm::dwarf::DW_AT_declaration, 0)) {
2497-
Module::LookupInfo info(ConstString(label.lookup_name),
2498-
lldb::eFunctionNameTypeFull,
2499-
lldb::eLanguageTypeUnknown);
2500-
2501-
m_index->GetFunctions(info, *this, {}, [&](DWARFDIE entry) {
2502-
if (entry.GetAttributeValueAsUnsigned(llvm::dwarf::DW_AT_declaration, 0))
2503-
return IterationAction::Continue;
2504-
2505-
// We don't check whether the specification DIE for this function
2506-
// corresponds to the declaration DIE because the declaration might be in
2507-
// a type-unit but the definition in the compile-unit (and it's
2508-
// specifcation would point to the declaration in the compile-unit). We
2509-
// rely on the mangled name within the module to be enough to find us the
2510-
// unique definition.
2511-
die = entry;
2512-
return IterationAction::Stop;
2513-
});
2514-
2515-
if (die.GetAttributeValueAsUnsigned(llvm::dwarf::DW_AT_declaration, 0))
2628+
die = FindFunctionDefinition(label, die);
2629+
if (!die.IsValid())
25162630
return llvm::createStringError(
25172631
llvm::formatv("failed to find definition DIE for {0}", label));
25182632
}

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,10 @@ class SymbolFileDWARF : public SymbolFileCommon {
372372
/// Returns the DWARFIndex for this symbol, if it exists.
373373
DWARFIndex *getIndex() { return m_index.get(); }
374374

375+
private:
376+
DWARFDIE FindFunctionDefinition(const FunctionCallLabel &label,
377+
const DWARFDIE &declaration);
378+
375379
protected:
376380
SymbolFileDWARF(const SymbolFileDWARF &) = delete;
377381
const SymbolFileDWARF &operator=(const SymbolFileDWARF &) = delete;

0 commit comments

Comments
 (0)