Skip to content

Commit 2a062d6

Browse files
authored
[lldb] Add SBFunction::GetBaseName() & SBSymbol::GetBaseName() (#155939)
When you are trying for instance to set a breakpoint on a function by name, but the SBFunction or SBSymbol are returning demangled names with argument lists, that match can be tedious to do. Internally, the base name of a symbol is something we handle all the time, so it's reasonable that there should be a way to get that info from the API as well. rdar://159318791
1 parent 205d461 commit 2a062d6

File tree

9 files changed

+103
-0
lines changed

9 files changed

+103
-0
lines changed

lldb/include/lldb/API/SBFunction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class LLDB_API SBFunction {
3636

3737
const char *GetMangledName() const;
3838

39+
const char *GetBaseName() const;
40+
3941
lldb::SBInstructionList GetInstructions(lldb::SBTarget target);
4042

4143
lldb::SBInstructionList GetInstructions(lldb::SBTarget target,

lldb/include/lldb/API/SBSymbol.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class LLDB_API SBSymbol {
3636

3737
const char *GetMangledName() const;
3838

39+
const char *GetBaseName() const;
40+
3941
lldb::SBInstructionList GetInstructions(lldb::SBTarget target);
4042

4143
lldb::SBInstructionList GetInstructions(lldb::SBTarget target,

lldb/include/lldb/Core/Mangled.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,17 @@ class Mangled {
287287
/// Retrieve \c DemangledNameInfo of the demangled name held by this object.
288288
const std::optional<DemangledNameInfo> &GetDemangledInfo() const;
289289

290+
/// Compute the base name (without namespace/class qualifiers) from the
291+
/// demangled name.
292+
///
293+
/// For a demangled name like "ns::MyClass<int>::templateFunc", this returns
294+
/// just "templateFunc".
295+
///
296+
/// \return
297+
/// A ConstString containing the basename, or nullptr if computation
298+
/// fails.
299+
ConstString GetBaseName() const;
300+
290301
private:
291302
/// If \c force is \c false, this function will re-use the previously
292303
/// demangled name (if any). If \c force is \c true (or the mangled name

lldb/source/API/SBFunction.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ const char *SBFunction::GetMangledName() const {
7979
return nullptr;
8080
}
8181

82+
const char *SBFunction::GetBaseName() const {
83+
LLDB_INSTRUMENT_VA(this);
84+
85+
if (!m_opaque_ptr)
86+
return nullptr;
87+
88+
return m_opaque_ptr->GetMangled().GetBaseName().AsCString();
89+
}
90+
8291
bool SBFunction::operator==(const SBFunction &rhs) const {
8392
LLDB_INSTRUMENT_VA(this, rhs);
8493

lldb/source/API/SBSymbol.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ const char *SBSymbol::GetMangledName() const {
7979
return name;
8080
}
8181

82+
const char *SBSymbol::GetBaseName() const {
83+
LLDB_INSTRUMENT_VA(this);
84+
85+
if (!m_opaque_ptr)
86+
return nullptr;
87+
88+
return m_opaque_ptr->GetMangled().GetBaseName().AsCString();
89+
}
90+
8291
bool SBSymbol::operator==(const SBSymbol &rhs) const {
8392
LLDB_INSTRUMENT_VA(this, rhs);
8493

lldb/source/Core/Mangled.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,18 @@ void Mangled::Encode(DataEncoder &file, ConstStringTable &strtab) const {
556556
break;
557557
}
558558
}
559+
560+
ConstString Mangled::GetBaseName() const {
561+
const auto &demangled_info = GetDemangledInfo();
562+
if (!demangled_info.has_value())
563+
return {};
564+
565+
ConstString demangled_name = GetDemangledName();
566+
if (!demangled_name)
567+
return {};
568+
569+
const char *name_str = demangled_name.AsCString();
570+
const auto &range = demangled_info->BasenameRange;
571+
return ConstString(
572+
llvm::StringRef(name_str + range.first, range.second - range.first));
573+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXX_SOURCES := main.cpp
2+
3+
include Makefile.rules
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Test SBFunction::GetBaseName() and SBSymbol::GetBaseName() APIs.
3+
"""
4+
5+
import lldb
6+
from lldbsuite.test.decorators import *
7+
from lldbsuite.test.lldbtest import *
8+
from lldbsuite.test import lldbutil
9+
10+
11+
class GetBaseNameTestCase(TestBase):
12+
NO_DEBUG_INFO_TESTCASE = True
13+
14+
def setUp(self):
15+
TestBase.setUp(self)
16+
self.main_source_file = lldb.SBFileSpec("main.cpp")
17+
18+
def test(self):
19+
"""Test SBFunction.GetBaseName() and SBSymbol.GetBaseName()"""
20+
self.build()
21+
_, _, thread, _ = lldbutil.run_to_source_breakpoint(
22+
self, "Set a breakpoint here", self.main_source_file
23+
)
24+
25+
frame0 = thread.GetFrameAtIndex(0)
26+
27+
# Get both function and symbol
28+
function = frame0.GetFunction()
29+
symbol = frame0.GetSymbol()
30+
31+
# Test consistency between function and symbol basename
32+
function_basename = function.GetBaseName()
33+
symbol_basename = symbol.GetBaseName()
34+
35+
self.assertEqual(function_basename, "templateFunc")
36+
self.assertEqual(symbol_basename, "templateFunc")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
3+
namespace ns {
4+
template <typename T> class MyClass {
5+
public:
6+
void templateFunc() {
7+
std::cout << "In templateFunc" << std::endl; // Set a breakpoint here
8+
}
9+
};
10+
} // namespace ns
11+
12+
int main() {
13+
ns::MyClass<int> obj;
14+
obj.templateFunc();
15+
return 0;
16+
}

0 commit comments

Comments
 (0)