Skip to content

[lldb] fix cropped demangled names in Swift backtraces #11068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: swift/release/6.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lldb/source/Core/Mangled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,21 @@ GetSwiftDemangledStr(ConstString m_mangled, const SymbolContext *sc,
}
auto [demangled, info] = SwiftLanguageRuntime::TrackedDemangleSymbolAsString(
mangled_name, demangle_mode, sc);
// TODO: The logic below should be in the NodePrinter.
info.PrefixRange.second =
std::min(info.BasenameRange.first, info.ArgumentsRange.first);
info.SuffixRange.first =
std::max(info.BasenameRange.second, info.ArgumentsRange.second);
info.SuffixRange.second = demangled.length();
if (info.hasBasename() && info.hasArguments()) {
if (info.hasTemplateArguments() && info.TemplateArgumentsRange.first > 0) {
info.NameQualifiersRange.second = std::min(
info.ArgumentsRange.first, info.TemplateArgumentsRange.first);
} else {
info.NameQualifiersRange.second = info.ArgumentsRange.first;
}
info.NameQualifiersRange.first = info.BasenameRange.second;
}

// Don't cache the demangled name if the function isn't available yet.
// Only cache eFullName demangled functions to keep the cache consistent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ include "../../../../include/lldb/Core/PropertiesBase.td"
let Definition = "language_swift" in {
def FunctionNameFormat: Property<"function-name-format", "FormatEntity">,
Global,
DefaultStringValue<"${function.prefix}${ansi.fg.yellow}${function.basename}${ansi.normal}${function.formatted-arguments}${function.suffix}">,
DefaultStringValue<"${function.prefix}${ansi.fg.yellow}${function.basename}${ansi.normal}${function.name-qualifiers}${function.template-arguments}${function.formatted-arguments}${function.suffix}">,
Desc<"Swift specific frame format string to use when displaying stack frame information for threads.">;
}
129 changes: 84 additions & 45 deletions lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1722,13 +1722,7 @@ bool SwiftLanguage::GetFunctionDisplayName(
// No need to customize this.
return false;
case Language::FunctionNameRepresentation::eNameWithNoArgs: {
if (!sc.function)
return false;
if (sc.function->GetLanguage() != eLanguageTypeSwift)
return false;
std::string display_name = SwiftLanguageRuntime::DemangleSymbolAsString(
sc.function->GetMangled().GetMangledName().GetStringRef(),
SwiftLanguageRuntime::eSimplified, &sc, exe_ctx);
std::string display_name = GetDemangledFunctionName(sc, exe_ctx);
if (display_name.empty())
return false;
s << display_name;
Expand Down Expand Up @@ -1763,69 +1757,82 @@ bool SwiftLanguage::GetFunctionDisplayName(
variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument,
args);

s << GetFunctionTemplateArguments(sc, exe_ctx);
s << GetFunctionDisplayArgs(sc, args, exe_ctx);
return true;
}
}
return false;
}

std::string SwiftLanguage::GetFunctionName(const SymbolContext &sc,
const ExecutionContext *exe_ctx) {
std::string
SwiftLanguage::GetDemangledFunctionName(const SymbolContext &sc,
const ExecutionContext *exe_ctx) {
if (!sc.function)
return {};
if (sc.function->GetLanguage() != eLanguageTypeSwift)
return {};
std::string name = SwiftLanguageRuntime::DemangleSymbolAsString(
return SwiftLanguageRuntime::DemangleSymbolAsString(
sc.GetPossiblyInlinedFunctionName().GetMangledName(),
SwiftLanguageRuntime::eSimplified, &sc, exe_ctx);
if (name.empty())
}

std::string SwiftLanguage::GetFunctionName(const SymbolContext &sc,
const ExecutionContext *exe_ctx) {
std::string demangled_name = GetDemangledFunctionName(sc, exe_ctx);
if (demangled_name.empty())
return {};
size_t open_paren = name.find('(');
size_t generic = name.find('<');
size_t open_paren = demangled_name.find('(');
size_t generic = demangled_name.find('<');
size_t name_end = std::min(open_paren, generic);
if (name_end == std::string::npos)
return name;
return name.substr(0, name_end);
return demangled_name;
return demangled_name.substr(0, name_end);
}

std::string
SwiftLanguage::GetFunctionTemplateArguments(const SymbolContext &sc,
const ExecutionContext *exe_ctx) {
std::string demangled_name = GetDemangledFunctionName(sc, exe_ctx);
if (demangled_name.empty())
return {};
size_t open_paren = demangled_name.find('(');
size_t generic_start = demangled_name.find('<');
if (generic_start == std::string::npos || generic_start > open_paren)
return {};

int generic_depth = 1;
size_t generic_end = generic_start + 1;

while (generic_end < demangled_name.size() && generic_depth > 0) {
if (demangled_name[generic_end] == '<') {
generic_depth++;
} else if (demangled_name[generic_end] == '>') {
generic_depth--;
}
generic_end++;
}

if (generic_depth != 0)
return {};

return demangled_name.substr(generic_start, generic_end - generic_start);
}

std::string SwiftLanguage::GetFunctionDisplayArgs(
const SymbolContext &sc, VariableList &args,
const lldb_private::ExecutionContext *exe_ctx) {
ExecutionContextScope *exe_scope =
exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
std::string name = SwiftLanguageRuntime::DemangleSymbolAsString(
sc.function->GetMangled().GetMangledName().GetStringRef(),
SwiftLanguageRuntime::eSimplified, &sc, exe_ctx);
std::string name = GetDemangledFunctionName(sc, exe_ctx);
lldb_private::StreamString s;
const char *cstr = name.data();
const char *open_paren = strchr(cstr, '(');
const char *close_paren = nullptr;
const char *generic = strchr(cstr, '<');
// If before the arguments list begins there is a template sign
// then scan to the end of the generic args before you try to find
// the arguments list.
const char *generic_start = generic;
if (generic && open_paren && generic < open_paren) {
int generic_depth = 1;
++generic;
for (; *generic && generic_depth > 0; generic++) {
if (*generic == '<')
generic_depth++;
if (*generic == '>')
generic_depth--;
}
if (*generic)
open_paren = strchr(generic, '(');
else
open_paren = nullptr;
}
if (open_paren) {
close_paren = strchr(open_paren, ')');
}

if (generic_start && generic_start < open_paren)
s.Write(generic_start, open_paren - generic_start);
s.PutChar('(');

const size_t num_args = args.GetSize();
Expand Down Expand Up @@ -1949,6 +1956,23 @@ GetDemangledBasename(const SymbolContext &sc) {
info.BasenameRange.second);
}

static llvm::Expected<llvm::StringRef>
GetDemangledNameQualifiers(const SymbolContext &sc) {
auto info_or_err = GetAndValidateInfo(sc);
if (!info_or_err)
return info_or_err.takeError();

auto [demangled_name, info] = *info_or_err;

if (!info.hasPrefix())
return llvm::createStringError(
"DemangledInfo for '%s does not have a name qualifiers range.",
demangled_name.data());

return demangled_name.slice(info.NameQualifiersRange.first,
info.NameQualifiersRange.second);
}

static llvm::Expected<llvm::StringRef>
GetDemangledFunctionPrefix(const SymbolContext &sc) {
auto info_or_err = GetAndValidateInfo(sc);
Expand All @@ -1959,7 +1983,7 @@ GetDemangledFunctionPrefix(const SymbolContext &sc) {

if (!info.hasPrefix())
return llvm::createStringError(
"DemangledInfo for '%s does not have suffix range.",
"DemangledInfo for '%s does not have a prefix range.",
demangled_name.data());

return demangled_name.slice(info.PrefixRange.first, info.PrefixRange.second);
Expand All @@ -1975,7 +1999,7 @@ GetDemangledFunctionSuffix(const SymbolContext &sc) {

if (!info.hasSuffix())
return llvm::createStringError(
"DemangledInfo for '%s does not have suffix range.",
"DemangledInfo for '%s does not have a suffix range.",
demangled_name.data());

return demangled_name.slice(info.SuffixRange.first, info.SuffixRange.second);
Expand Down Expand Up @@ -2030,6 +2054,24 @@ bool SwiftLanguage::HandleFrameFormatVariable(const SymbolContext &sc,

return true;
}
case FormatEntity::Entry::Type::FunctionNameQualifiers: {
auto qualifiers_or_err = GetDemangledNameQualifiers(sc);
if (!qualifiers_or_err) {
LLDB_LOG_ERROR(GetLog(LLDBLog::Language), qualifiers_or_err.takeError(),
"Failed to handle ${{function.name-qualifiers}} "
"frame-format variable: {0}");
return false;
}

s << *qualifiers_or_err;

return true;
}
case FormatEntity::Entry::Type::FunctionTemplateArguments: {
s << GetFunctionTemplateArguments(sc, exe_ctx);

return true;
}
case FormatEntity::Entry::Type::FunctionFormattedArguments: {
// This ensures we print the arguments even when no debug-info is available.
//
Expand All @@ -2038,9 +2080,7 @@ bool SwiftLanguage::HandleFrameFormatVariable(const SymbolContext &sc,
// once we have a "fallback operator" in the frame-format language.
if (!sc.function && sc.symbol)
return PrintDemangledArgumentList(s, sc);
std::string display_name = SwiftLanguageRuntime::DemangleSymbolAsString(
sc.function->GetMangled().GetMangledName().GetStringRef(),
SwiftLanguageRuntime::eSimplified, &sc, exe_ctx);
std::string display_name = GetDemangledFunctionName(sc, exe_ctx);
if (display_name.empty())
return false;

Expand Down Expand Up @@ -2080,7 +2120,6 @@ bool SwiftLanguage::HandleFrameFormatVariable(const SymbolContext &sc,
}

case FormatEntity::Entry::Type::FunctionScope:
case FormatEntity::Entry::Type::FunctionTemplateArguments:
case FormatEntity::Entry::Type::FunctionReturnRight:
case FormatEntity::Entry::Type::FunctionReturnLeft:
case FormatEntity::Entry::Type::FunctionQualifiers:
Expand Down
6 changes: 6 additions & 0 deletions lldb/source/Plugins/Language/Swift/SwiftLanguage.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class SwiftLanguage : public Language {
FunctionNameRepresentation representation,
Stream &s) override;

std::string GetDemangledFunctionName(const SymbolContext &sc,
const ExecutionContext *exe_ctx);

/// Returns the name of function up to the first generic or opening
/// parenthesis.
///
Expand All @@ -83,6 +86,9 @@ class SwiftLanguage : public Language {
std::string GetFunctionName(const SymbolContext &sc,
const ExecutionContext *exe_ctx);

std::string GetFunctionTemplateArguments(const SymbolContext &sc,
const ExecutionContext *exe_ctx);

/// Returns the arguments of a function call with its generics if any.
///
/// Calling GetFunctionDisplayArgs on the following function call will return
Expand Down
26 changes: 26 additions & 0 deletions lldb/source/Plugins/Language/Swift/SwiftMangled.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class TrackingNodePrinter : public NodePrinter {
private:
lldb_private::DemangledNameInfo info;
std::optional<unsigned> parametersDepth;
std::optional<unsigned> genericsSignatureDepth;

void startName() {
if (!info.hasBasename())
Expand All @@ -43,6 +44,25 @@ class TrackingNodePrinter : public NodePrinter {
info.BasenameRange.second = getStreamLength();
}

void startGenericSignature(unsigned depth) {
if (genericsSignatureDepth || !info.hasBasename() ||
info.TemplateArgumentsRange.first <
info.TemplateArgumentsRange.second) {
return;
}
info.TemplateArgumentsRange.first = getStreamLength();
genericsSignatureDepth = depth;
}

void endGenericSignature(unsigned depth) {
if (!genericsSignatureDepth || *genericsSignatureDepth != depth ||
info.TemplateArgumentsRange.first <
info.TemplateArgumentsRange.second) {
return;
}
info.TemplateArgumentsRange.second = getStreamLength();
}

void startParameters(unsigned depth) {
if (parametersDepth || !info.hasBasename() ||
info.ArgumentsRange.first < info.ArgumentsRange.second) {
Expand Down Expand Up @@ -85,6 +105,12 @@ class TrackingNodePrinter : public NodePrinter {
endName();
}

void printGenericSignature(NodePointer Node, unsigned depth) override {
startGenericSignature(depth);
NodePrinter::printGenericSignature(Node, depth);
endGenericSignature(depth);
}

void printFunctionParameters(NodePointer LabelList, NodePointer ParameterType,
unsigned depth, bool showTypes) override {
startParameters(depth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lldbsuite.test.lldbutil as lldbutil


@skipIf(bugnumber="rdar://156178892")
class TestSwiftAsyncHiddenFrames(lldbtest.TestBase):

NO_DEBUG_INFO_TESTCASE = True
Expand Down
Loading