Author: Jonas Devlieghere Date: 2025-08-28T19:10:52-07:00 New Revision: 2a062d693693f92d80656cb2b334b7dc8e08121f
URL: https://github.com/llvm/llvm-project/commit/2a062d693693f92d80656cb2b334b7dc8e08121f DIFF: https://github.com/llvm/llvm-project/commit/2a062d693693f92d80656cb2b334b7dc8e08121f.diff LOG: [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 Added: lldb/test/API/python_api/basename/Makefile lldb/test/API/python_api/basename/TestGetBaseName.py lldb/test/API/python_api/basename/main.cpp Modified: lldb/include/lldb/API/SBFunction.h lldb/include/lldb/API/SBSymbol.h lldb/include/lldb/Core/Mangled.h lldb/source/API/SBFunction.cpp lldb/source/API/SBSymbol.cpp lldb/source/Core/Mangled.cpp Removed: ################################################################################ diff --git a/lldb/include/lldb/API/SBFunction.h b/lldb/include/lldb/API/SBFunction.h index 0a8aeeff1ea5a..e703ae5dd63c1 100644 --- a/lldb/include/lldb/API/SBFunction.h +++ b/lldb/include/lldb/API/SBFunction.h @@ -36,6 +36,8 @@ class LLDB_API SBFunction { const char *GetMangledName() const; + const char *GetBaseName() const; + lldb::SBInstructionList GetInstructions(lldb::SBTarget target); lldb::SBInstructionList GetInstructions(lldb::SBTarget target, diff --git a/lldb/include/lldb/API/SBSymbol.h b/lldb/include/lldb/API/SBSymbol.h index a93bc7a7ae074..580458ede212d 100644 --- a/lldb/include/lldb/API/SBSymbol.h +++ b/lldb/include/lldb/API/SBSymbol.h @@ -36,6 +36,8 @@ class LLDB_API SBSymbol { const char *GetMangledName() const; + const char *GetBaseName() const; + lldb::SBInstructionList GetInstructions(lldb::SBTarget target); lldb::SBInstructionList GetInstructions(lldb::SBTarget target, diff --git a/lldb/include/lldb/Core/Mangled.h b/lldb/include/lldb/Core/Mangled.h index eb9a58c568896..47f1c6a8d80b7 100644 --- a/lldb/include/lldb/Core/Mangled.h +++ b/lldb/include/lldb/Core/Mangled.h @@ -287,6 +287,17 @@ class Mangled { /// Retrieve \c DemangledNameInfo of the demangled name held by this object. const std::optional<DemangledNameInfo> &GetDemangledInfo() const; + /// Compute the base name (without namespace/class qualifiers) from the + /// demangled name. + /// + /// For a demangled name like "ns::MyClass<int>::templateFunc", this returns + /// just "templateFunc". + /// + /// \return + /// A ConstString containing the basename, or nullptr if computation + /// fails. + ConstString GetBaseName() const; + private: /// If \c force is \c false, this function will re-use the previously /// demangled name (if any). If \c force is \c true (or the mangled name diff --git a/lldb/source/API/SBFunction.cpp b/lldb/source/API/SBFunction.cpp index 19861f6af3645..65b02d6b309ca 100644 --- a/lldb/source/API/SBFunction.cpp +++ b/lldb/source/API/SBFunction.cpp @@ -79,6 +79,15 @@ const char *SBFunction::GetMangledName() const { return nullptr; } +const char *SBFunction::GetBaseName() const { + LLDB_INSTRUMENT_VA(this); + + if (!m_opaque_ptr) + return nullptr; + + return m_opaque_ptr->GetMangled().GetBaseName().AsCString(); +} + bool SBFunction::operator==(const SBFunction &rhs) const { LLDB_INSTRUMENT_VA(this, rhs); diff --git a/lldb/source/API/SBSymbol.cpp b/lldb/source/API/SBSymbol.cpp index 3b59119494f37..3030c83292127 100644 --- a/lldb/source/API/SBSymbol.cpp +++ b/lldb/source/API/SBSymbol.cpp @@ -79,6 +79,15 @@ const char *SBSymbol::GetMangledName() const { return name; } +const char *SBSymbol::GetBaseName() const { + LLDB_INSTRUMENT_VA(this); + + if (!m_opaque_ptr) + return nullptr; + + return m_opaque_ptr->GetMangled().GetBaseName().AsCString(); +} + bool SBSymbol::operator==(const SBSymbol &rhs) const { LLDB_INSTRUMENT_VA(this, rhs); diff --git a/lldb/source/Core/Mangled.cpp b/lldb/source/Core/Mangled.cpp index ce4db4e0daa8b..91b9c0007617d 100644 --- a/lldb/source/Core/Mangled.cpp +++ b/lldb/source/Core/Mangled.cpp @@ -556,3 +556,18 @@ void Mangled::Encode(DataEncoder &file, ConstStringTable &strtab) const { break; } } + +ConstString Mangled::GetBaseName() const { + const auto &demangled_info = GetDemangledInfo(); + if (!demangled_info.has_value()) + return {}; + + ConstString demangled_name = GetDemangledName(); + if (!demangled_name) + return {}; + + const char *name_str = demangled_name.AsCString(); + const auto &range = demangled_info->BasenameRange; + return ConstString( + llvm::StringRef(name_str + range.first, range.second - range.first)); +} diff --git a/lldb/test/API/python_api/basename/Makefile b/lldb/test/API/python_api/basename/Makefile new file mode 100644 index 0000000000000..99998b20bcb05 --- /dev/null +++ b/lldb/test/API/python_api/basename/Makefile @@ -0,0 +1,3 @@ +CXX_SOURCES := main.cpp + +include Makefile.rules diff --git a/lldb/test/API/python_api/basename/TestGetBaseName.py b/lldb/test/API/python_api/basename/TestGetBaseName.py new file mode 100644 index 0000000000000..bd91acd20404b --- /dev/null +++ b/lldb/test/API/python_api/basename/TestGetBaseName.py @@ -0,0 +1,36 @@ +""" +Test SBFunction::GetBaseName() and SBSymbol::GetBaseName() APIs. +""" + +import lldb +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil + + +class GetBaseNameTestCase(TestBase): + NO_DEBUG_INFO_TESTCASE = True + + def setUp(self): + TestBase.setUp(self) + self.main_source_file = lldb.SBFileSpec("main.cpp") + + def test(self): + """Test SBFunction.GetBaseName() and SBSymbol.GetBaseName()""" + self.build() + _, _, thread, _ = lldbutil.run_to_source_breakpoint( + self, "Set a breakpoint here", self.main_source_file + ) + + frame0 = thread.GetFrameAtIndex(0) + + # Get both function and symbol + function = frame0.GetFunction() + symbol = frame0.GetSymbol() + + # Test consistency between function and symbol basename + function_basename = function.GetBaseName() + symbol_basename = symbol.GetBaseName() + + self.assertEqual(function_basename, "templateFunc") + self.assertEqual(symbol_basename, "templateFunc") diff --git a/lldb/test/API/python_api/basename/main.cpp b/lldb/test/API/python_api/basename/main.cpp new file mode 100644 index 0000000000000..9b4140985cf45 --- /dev/null +++ b/lldb/test/API/python_api/basename/main.cpp @@ -0,0 +1,16 @@ +#include <iostream> + +namespace ns { +template <typename T> class MyClass { +public: + void templateFunc() { + std::cout << "In templateFunc" << std::endl; // Set a breakpoint here + } +}; +} // namespace ns + +int main() { + ns::MyClass<int> obj; + obj.templateFunc(); + return 0; +} _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits