Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lldb/include/lldb/API/SBFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions lldb/include/lldb/API/SBSymbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions lldb/include/lldb/Core/Mangled.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,18 @@ 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". If the demangled name is not available or the
/// basename range is invalid, this falls back to GetDisplayDemangledName().
///
/// \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
Expand Down
9 changes: 9 additions & 0 deletions lldb/source/API/SBFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions lldb/source/API/SBSymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
18 changes: 18 additions & 0 deletions lldb/source/Core/Mangled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,21 @@ void Mangled::Encode(DataEncoder &file, ConstStringTable &strtab) const {
break;
}
}

ConstString Mangled::GetBaseName() const {
const auto &demangled_info = GetDemangledInfo();
if (!demangled_info.has_value())
return GetDisplayDemangledName();

ConstString demangled_name = GetDemangledName();
if (!demangled_name)
return GetDisplayDemangledName();

const char *name_str = demangled_name.AsCString();
const auto &range = demangled_info->BasenameRange;
if (range.first >= range.second || range.second > strlen(name_str))
return ConstString();

return ConstString(
llvm::StringRef(name_str + range.first, range.second - range.first));
}
3 changes: 3 additions & 0 deletions lldb/test/API/python_api/basename/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CXX_SOURCES := main.cpp

include Makefile.rules
53 changes: 53 additions & 0 deletions lldb/test/API/python_api/basename/TestGetBaseName.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
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):
# Call super's setUp().
TestBase.setUp(self)
# Find the line number to break on.
self.line1 = line_number(
"main.cpp", "// Find the line number for breakpoint 1 here."
)

def test(self):
"""Test SBFunction.GetBaseName() and SBSymbol.GetBaseName()"""
self.build()
exe = self.getBuildArtifact("a.out")

# Create a target by the debugger.
target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)

# Create a breakpoint inside the C++ namespaced function.
breakpoint1 = target.BreakpointCreateByLocation("main.cpp", self.line1)

# Now launch the process, and do not stop at entry point.
process = target.LaunchSimple(None, None, self.get_process_working_directory())

# Get stopped thread and frame
thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
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")

self.trace("Function basename:", function_basename)
self.trace("Symbol basename:", symbol_basename)
17 changes: 17 additions & 0 deletions lldb/test/API/python_api/basename/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>

namespace ns {
template <typename T> class MyClass {
public:
void templateFunc() {
std::cout << "In templateFunc"
<< std::endl; // Find the line number for breakpoint 1 here.
}
};
} // namespace ns

int main() {
ns::MyClass<int> obj;
obj.templateFunc();
return 0;
}
Loading