Skip to content

Commit 64b5bc8

Browse files
authored
[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)`.
1 parent 76aa471 commit 64b5bc8

File tree

6 files changed

+68
-5
lines changed

6 files changed

+68
-5
lines changed

lldb/docs/use/formatting.rst

+7-4
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

+1
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

+3
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);
@@ -1808,6 +1810,7 @@ bool FormatEntity::Format(const Entry &entry, Stream &s,
18081810
case Entry::Type::FunctionFormattedArguments:
18091811
case Entry::Type::FunctionReturnRight:
18101812
case Entry::Type::FunctionReturnLeft:
1813+
case Entry::Type::FunctionSuffix:
18111814
case Entry::Type::FunctionQualifiers: {
18121815
if (!sc->function)
18131816
return false;

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

+33
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,30 @@ GetDemangledScope(const SymbolContext &sc) {
381381
return demangled_name.slice(info->ScopeRange.first, info->ScopeRange.second);
382382
}
383383

384+
/// Handles anything printed after the FunctionEncoding ItaniumDemangle
385+
/// node. Most notably the DotSUffix node.
386+
static std::optional<llvm::StringRef>
387+
GetDemangledFunctionSuffix(const SymbolContext &sc) {
388+
Mangled mangled = sc.GetPossiblyInlinedFunctionName();
389+
if (!mangled)
390+
return std::nullopt;
391+
392+
auto demangled_name = mangled.GetDemangledName().GetStringRef();
393+
if (demangled_name.empty())
394+
return std::nullopt;
395+
396+
const std::optional<DemangledNameInfo> &info = mangled.GetDemangledInfo();
397+
if (!info)
398+
return std::nullopt;
399+
400+
// Function without a basename is nonsense.
401+
if (!info->hasBasename())
402+
return std::nullopt;
403+
404+
return demangled_name.slice(info->QualifiersRange.second,
405+
llvm::StringRef::npos);
406+
}
407+
384408
bool CPlusPlusLanguage::CxxMethodName::TrySimplifiedParse() {
385409
// This method tries to parse simple method definitions which are presumably
386410
// most comman in user programs. Definitions that can be parsed by this
@@ -1966,6 +1990,15 @@ bool CPlusPlusLanguage::HandleFrameFormatVariable(
19661990

19671991
return true;
19681992
}
1993+
case FormatEntity::Entry::Type::FunctionSuffix: {
1994+
std::optional<llvm::StringRef> suffix = GetDemangledFunctionSuffix(sc);
1995+
if (!suffix)
1996+
return false;
1997+
1998+
s << *suffix;
1999+
2000+
return true;
2001+
}
19692002
default:
19702003
return false;
19712004
}

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

+1-1
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
}
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)