Skip to content

Commit 4fa4f41

Browse files
committed
[lldb][Format] Add function.suffix frame-format variable (llvm#137763)
This patch adds another frame-format variable (currently only implemented in the CPlusPlus language plugin) that represents the "suffix" of a function. The name is derived from the `DotSuffix` node of LLVM's Itanium demangler. For a function name such as `int foo() (.cold)`, the suffix would be `(.cold)`. (cherry picked from commit 64b5bc8)
1 parent fc3c828 commit 4fa4f41

File tree

6 files changed

+68
-5
lines changed

6 files changed

+68
-5
lines changed

lldb/docs/use/formatting.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ A complete list of currently supported format string variables is listed below:
106106
| ``function.return-right`` | The return type to the right of the demangled function name of the current function. This depends on the frame's language. In ``void ns::foo(int)`` there is no ``function.return-right`` so this would correspond to an empty string. However, in some cases, particularly for functions |
107107
| | returning function pointers, part of the return type is to the right of the function name. E.g., for ``void (*ns::func(float))(int)`` the ``function.return-left`` would be ``void (*`` and the ``function.return-right`` would be ``)(int)``. |
108108
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
109+
| ``function.suffix`` | Any suffix added to the demangled function name of the current function. This depends on the frame's language. E.g., for C++ the suffix for ``void ns::foo(int) (.cold)`` is '(.cold). |
110+
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
109111
| ``function.mangled-name`` | The mangled name of the current function or symbol. |
110112
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
111113
| ``function.pc-offset`` | The program counter offset within the current function or symbol |
@@ -329,26 +331,27 @@ The function names displayed in backtraces/``frame info``/``thread info`` are th
329331
- ``${function.formatted-arguments}``
330332
- ``${function.qualifiers}``
331333
- ``${function.return-right}``
334+
- ``${function.suffix}``
332335

333336
Each language plugin decides how to handle these variables. For C++, LLDB uses these variables to dictate how function names are formatted. This can be customized using the ``plugin.cplusplus.display.function-name-format`` LLDB setting.
334337

335338
E.g., the following setting would reconstruct the entire function name (and is LLDB's default):
336339

337340
::
338341

339-
(lldb) settings set plugin.cplusplus.dislpay.function-name-format "${function.return-left}${function.scope}${function.basename}${function.template-arguments}${function.formatted-arguments}${function.qualifiers}${function.return-right}"
342+
(lldb) settings set plugin.cplusplus.dislpay.function-name-format "${function.return-left}${function.scope}${function.basename}${function.template-arguments}${function.formatted-arguments}${function.qualifiers}${function.return-right}${function.suffix}"
340343

341-
If a user wanted to omit the return type and template arguments of C++ function names one could do:
344+
If a user wanted to only print the name and arguments of a C++ function one could do:
342345

343346
::
344347

345-
(lldb) settings set plugin.cplusplus.dislpay.function-name-format "${function.scope}${function.basename}${function.formatted-arguments}${function.qualifiers}"
348+
(lldb) settings set plugin.cplusplus.dislpay.function-name-format "${function.scope}${function.basename}${function.formatted-arguments}"
346349

347350

348351
Then the following would highlight just the basename in green:
349352

350353
::
351354

352-
(lldb) settings set plugin.cplusplus.dislpay.function-name-format "${function.scope}${ansi.fg.yellow}${function.basename}${ansi.normal}${function.formatted-arguments}${function.qualifiers}"
355+
(lldb) settings set plugin.cplusplus.dislpay.function-name-format "${function.scope}${ansi.fg.yellow}${function.basename}${ansi.normal}${function.formatted-arguments}"
353356

354357
The ``${function.name-with-args}`` by default asks the language plugin whether it supports a language-specific ``function-name-format`` (e.g., the ``plugin.cplusplus.display.function-name-format`` for C++), and if it does, uses it. Otherwise it will display the demangled function name.

lldb/include/lldb/Core/FormatEntity.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ struct Entry {
9595
FunctionReturnLeft,
9696
FunctionReturnRight,
9797
FunctionQualifiers,
98+
FunctionSuffix,
9899
FunctionAddrOffset,
99100
FunctionAddrOffsetConcrete,
100101
FunctionLineOffset,

lldb/source/Core/FormatEntity.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ constexpr Definition g_function_child_entries[] = {
130130
Definition("return-left", EntryType::FunctionReturnLeft),
131131
Definition("return-right", EntryType::FunctionReturnRight),
132132
Definition("qualifiers", EntryType::FunctionQualifiers),
133+
Definition("suffix", EntryType::FunctionSuffix),
133134
};
134135

135136
constexpr Definition g_line_child_entries[] = {
@@ -368,6 +369,7 @@ const char *FormatEntity::Entry::TypeToCString(Type t) {
368369
ENUM_TO_CSTR(FunctionReturnLeft);
369370
ENUM_TO_CSTR(FunctionReturnRight);
370371
ENUM_TO_CSTR(FunctionQualifiers);
372+
ENUM_TO_CSTR(FunctionSuffix);
371373
ENUM_TO_CSTR(FunctionAddrOffset);
372374
ENUM_TO_CSTR(FunctionAddrOffsetConcrete);
373375
ENUM_TO_CSTR(FunctionLineOffset);
@@ -1816,6 +1818,7 @@ bool FormatEntity::Format(const Entry &entry, Stream &s,
18161818
case Entry::Type::FunctionFormattedArguments:
18171819
case Entry::Type::FunctionReturnRight:
18181820
case Entry::Type::FunctionReturnLeft:
1821+
case Entry::Type::FunctionSuffix:
18191822
case Entry::Type::FunctionQualifiers: {
18201823
Language *language_plugin = nullptr;
18211824
if (sc->function)

lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,30 @@ GetDemangledScope(const SymbolContext &sc) {
354354
return demangled_name.slice(info->ScopeRange.first, info->ScopeRange.second);
355355
}
356356

357+
/// Handles anything printed after the FunctionEncoding ItaniumDemangle
358+
/// node. Most notably the DotSUffix node.
359+
static std::optional<llvm::StringRef>
360+
GetDemangledFunctionSuffix(const SymbolContext &sc) {
361+
Mangled mangled = sc.GetPossiblyInlinedFunctionName();
362+
if (!mangled)
363+
return std::nullopt;
364+
365+
auto demangled_name = mangled.GetDemangledName().GetStringRef();
366+
if (demangled_name.empty())
367+
return std::nullopt;
368+
369+
const std::optional<DemangledNameInfo> &info = mangled.GetDemangledInfo();
370+
if (!info)
371+
return std::nullopt;
372+
373+
// Function without a basename is nonsense.
374+
if (!info->hasBasename())
375+
return std::nullopt;
376+
377+
return demangled_name.slice(info->QualifiersRange.second,
378+
llvm::StringRef::npos);
379+
}
380+
357381
static bool PrintDemangledArgumentList(Stream &s, const SymbolContext &sc) {
358382
assert(sc.symbol);
359383

@@ -2023,6 +2047,15 @@ bool CPlusPlusLanguage::HandleFrameFormatVariable(
20232047

20242048
return true;
20252049
}
2050+
case FormatEntity::Entry::Type::FunctionSuffix: {
2051+
std::optional<llvm::StringRef> suffix = GetDemangledFunctionSuffix(sc);
2052+
if (!suffix)
2053+
return false;
2054+
2055+
s << *suffix;
2056+
2057+
return true;
2058+
}
20262059
default:
20272060
return false;
20282061
}

lldb/source/Plugins/Language/CPlusPlus/LanguageCPlusPlusProperties.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ include "../../../../include/lldb/Core/PropertiesBase.td"
33
let Definition = "language_cplusplus" in {
44
def FunctionNameFormat: Property<"function-name-format", "FormatEntity">,
55
Global,
6-
DefaultStringValue<"${function.return-left}${function.scope}${ansi.fg.yellow}${function.basename}${ansi.normal}${function.template-arguments}${function.formatted-arguments}${function.return-right}${function.qualifiers}">,
6+
DefaultStringValue<"${function.return-left}${function.scope}${ansi.fg.yellow}${function.basename}${ansi.normal}${function.template-arguments}${function.formatted-arguments}${function.return-right}${function.qualifiers}${function.suffix}">,
77
Desc<"C++ specific frame format string to use when displaying stack frame information for threads.">;
88
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# XFAIL: target-windows
2+
3+
# Test the ${function.suffix} frame-format variable.
4+
5+
# RUN: split-file %s %t
6+
# RUN: %clang_host -g -gdwarf %t/main.cpp -o %t.out
7+
# RUN: %lldb -x -b -s %t/commands.input %t.out -o exit 2>&1 \
8+
# RUN: | FileCheck %s
9+
10+
#--- main.cpp
11+
void bar() asm("_Z3barv.cold");
12+
void bar() {}
13+
14+
int main() { bar(); }
15+
16+
#--- commands.input
17+
settings set -f frame-format "custom-frame '${function.suffix}'\n"
18+
break set -n "_Z3barv.cold"
19+
20+
run
21+
bt
22+
23+
# CHECK: custom-frame ' (.cold)'

0 commit comments

Comments
 (0)