Skip to content

Commit c1d55d2

Browse files
committed
[lldb] Let Mangled decide whether a name is mangled or not
We have a handful of places in LLDB where we try to outsmart the logic in Mangled to determine whether a string is mangled or not. There's at least one place (*) where we are getting this wrong and causes a subtle bug. The `cstring_is_mangled` is cheap enough that we should always rely on it to determine whether a string is mangled or not. (*) `ObjectFileMachO` assumes that a symbol that starts with a double underscore (such as `__pthread_kill`) is mangled. That's mostly harmless, until you use `function.name-without-args` in the frame format. The formatter calls `Symbol::GetNameNoArguments()` which is a wrapper around `Mangled::GetName(ePreferDemangledWithoutArguments)`. The latter will first try using the appropriate language plugin to get the demangled name without arguments, and if that fails, falls back to returning the demangled name. Because we forced Mangled to treat the symbol as a mangled name (even though it's not) there's no demangled name. The result is that frames don't show any symbol at all. Differential revision: https://reviews.llvm.org/D148846
1 parent 482c1df commit c1d55d2

File tree

9 files changed

+49
-49
lines changed

9 files changed

+49
-49
lines changed

lldb/include/lldb/Core/Mangled.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,6 @@ class Mangled {
185185
/// The number of bytes that this object occupies in memory.
186186
size_t MemorySize() const;
187187

188-
/// Set the string value in this object.
189-
///
190-
/// If \a is_mangled is \b true, then the mangled named is set to \a name,
191-
/// else the demangled name is set to \a name.
192-
///
193-
/// \param[in] name
194-
/// The already const version of the name for this object.
195-
///
196-
/// \param[in] is_mangled
197-
/// If \b true then \a name is a mangled name, if \b false then
198-
/// \a name is demangled.
199-
void SetValue(ConstString name, bool is_mangled);
200-
201188
/// Set the string value in this object.
202189
///
203190
/// This version auto detects if the string is mangled by inspecting the

lldb/source/Core/Mangled.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,6 @@ int Mangled::Compare(const Mangled &a, const Mangled &b) {
9090
b.GetName(ePreferMangled));
9191
}
9292

93-
// Set the string value in this objects. If "mangled" is true, then the mangled
94-
// named is set with the new value in "s", else the demangled name is set.
95-
void Mangled::SetValue(ConstString s, bool mangled) {
96-
if (s) {
97-
if (mangled) {
98-
m_demangled.Clear();
99-
m_mangled = s;
100-
} else {
101-
m_demangled = s;
102-
m_mangled.Clear();
103-
}
104-
} else {
105-
m_demangled.Clear();
106-
m_mangled.Clear();
107-
}
108-
}
109-
11093
void Mangled::SetValue(ConstString name) {
11194
if (name) {
11295
if (cstring_is_mangled(name.GetStringRef())) {

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,7 +3030,7 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
30303030
// the first contains a directory and the
30313031
// second contains a full path.
30323032
sym[sym_idx - 1].GetMangled().SetValue(
3033-
ConstString(symbol_name), false);
3033+
ConstString(symbol_name));
30343034
m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
30353035
add_nlist = false;
30363036
} else {
@@ -3076,7 +3076,7 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
30763076
full_so_path += '/';
30773077
full_so_path += symbol_name;
30783078
sym[sym_idx - 1].GetMangled().SetValue(
3079-
ConstString(full_so_path.c_str()), false);
3079+
ConstString(full_so_path.c_str()));
30803080
add_nlist = false;
30813081
m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
30823082
}
@@ -3468,17 +3468,13 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
34683468
sym[sym_idx].GetMangled().SetDemangledName(
34693469
ConstString(symbol_name));
34703470
} else {
3471-
bool symbol_name_is_mangled = false;
3472-
34733471
if (symbol_name && symbol_name[0] == '_') {
3474-
symbol_name_is_mangled = symbol_name[1] == '_';
34753472
symbol_name++; // Skip the leading underscore
34763473
}
34773474

34783475
if (symbol_name) {
34793476
ConstString const_symbol_name(symbol_name);
3480-
sym[sym_idx].GetMangled().SetValue(
3481-
const_symbol_name, symbol_name_is_mangled);
3477+
sym[sym_idx].GetMangled().SetValue(const_symbol_name);
34823478
if (is_gsym && is_debug) {
34833479
const char *gsym_name =
34843480
sym[sym_idx]
@@ -3938,8 +3934,8 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
39383934
if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) {
39393935
// We have two consecutive N_SO entries where the first
39403936
// contains a directory and the second contains a full path.
3941-
sym[sym_idx - 1].GetMangled().SetValue(ConstString(symbol_name),
3942-
false);
3937+
sym[sym_idx - 1].GetMangled().SetValue(
3938+
ConstString(symbol_name));
39433939
m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
39443940
add_nlist = false;
39453941
} else {
@@ -3977,7 +3973,7 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
39773973
full_so_path += '/';
39783974
full_so_path += symbol_name;
39793975
sym[sym_idx - 1].GetMangled().SetValue(
3980-
ConstString(full_so_path.c_str()), false);
3976+
ConstString(full_so_path.c_str()));
39813977
add_nlist = false;
39823978
m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
39833979
}
@@ -4330,17 +4326,14 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
43304326
ConstString(symbol_name_non_abi_mangled));
43314327
sym[sym_idx].GetMangled().SetDemangledName(ConstString(symbol_name));
43324328
} else {
4333-
bool symbol_name_is_mangled = false;
43344329

43354330
if (symbol_name && symbol_name[0] == '_') {
4336-
symbol_name_is_mangled = symbol_name[1] == '_';
43374331
symbol_name++; // Skip the leading underscore
43384332
}
43394333

43404334
if (symbol_name) {
43414335
ConstString const_symbol_name(symbol_name);
4342-
sym[sym_idx].GetMangled().SetValue(const_symbol_name,
4343-
symbol_name_is_mangled);
4336+
sym[sym_idx].GetMangled().SetValue(const_symbol_name);
43444337
}
43454338
}
43464339

lldb/source/Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ FunctionSP SymbolFileBreakpad::GetOrCreateFunction(CompileUnit &comp_unit) {
246246

247247
if (auto record = FuncRecord::parse(*It)) {
248248
Mangled func_name;
249-
func_name.SetValue(ConstString(record->Name), false);
249+
func_name.SetValue(ConstString(record->Name));
250250
addr_t address = record->Address + base;
251251
SectionSP section_sp = list->FindSectionContainingFileAddress(address);
252252
if (section_sp) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,7 +2417,7 @@ DWARFASTParserClang::ParseFunctionFromDWARF(CompileUnit &comp_unit,
24172417
&frame_base)) {
24182418
Mangled func_name;
24192419
if (mangled)
2420-
func_name.SetValue(ConstString(mangled), true);
2420+
func_name.SetValue(ConstString(mangled));
24212421
else if ((die.GetParent().Tag() == DW_TAG_compile_unit ||
24222422
die.GetParent().Tag() == DW_TAG_partial_unit) &&
24232423
Language::LanguageIsCPlusPlus(
@@ -2428,9 +2428,9 @@ DWARFASTParserClang::ParseFunctionFromDWARF(CompileUnit &comp_unit,
24282428
// If the mangled name is not present in the DWARF, generate the
24292429
// demangled name using the decl context. We skip if the function is
24302430
// "main" as its name is never mangled.
2431-
func_name.SetValue(ConstructDemangledNameFromDWARF(die), false);
2431+
func_name.SetValue(ConstructDemangledNameFromDWARF(die));
24322432
} else
2433-
func_name.SetValue(ConstString(name), false);
2433+
func_name.SetValue(ConstString(name));
24342434

24352435
FunctionSP func_sp;
24362436
std::unique_ptr<Declaration> decl_up;

lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1980,7 +1980,7 @@ SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
19801980
} else if (!func_undecorated_name.empty()) {
19811981
mangled.SetDemangledName(ConstString(func_undecorated_name));
19821982
} else if (!func_name.empty())
1983-
mangled.SetValue(ConstString(func_name), false);
1983+
mangled.SetValue(ConstString(func_name));
19841984

19851985
return mangled;
19861986
}

lldb/test/API/macosx/format/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
C_SOURCES := main.c
2+
3+
include Makefile.rules
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
from lldbsuite.test.lldbtest import *
4+
5+
6+
class TestFunctionNameWithoutArgs(TestBase):
7+
@skipUnlessDarwin
8+
@no_debug_info_test
9+
def test_function_name_without_args(self):
10+
self.build()
11+
target = self.createTestTarget()
12+
target.LaunchSimple(None, None, self.get_process_working_directory())
13+
14+
self.runCmd("run", RUN_SUCCEEDED)
15+
self.expect(
16+
"bt",
17+
substrs=[
18+
"stop reason = hit program assert",
19+
"libsystem_kernel.dylib`__pthread_kill",
20+
],
21+
)
22+
self.runCmd(
23+
'settings set frame-format "frame #${frame.index}: ${function.name-without-args}\n"'
24+
)
25+
self.expect(
26+
"bt",
27+
substrs=["stop reason = hit program assert", "frame #0: __pthread_kill"],
28+
)

lldb/test/API/macosx/format/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <assert.h>
2+
3+
int main(int argc, char **argv) {
4+
assert(argc != 1);
5+
return 0;
6+
}

0 commit comments

Comments
 (0)