Skip to content

[lldb][ResolveSourceFileCallback] Call resolve source file callback #120833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion lldb/include/lldb/Symbol/LineEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ struct LineEntry {
///
/// \param[in] target_sp
/// Shared pointer to the target this LineEntry belongs to.
void ApplyFileMappings(lldb::TargetSP target_sp);
///
/// \param[in] module_sp
/// Shared pointer to the module this LineEntry belongs to.
void ApplyFileMappings(lldb::TargetSP target_sp, lldb::ModuleSP module_sp);

/// Helper to access the file.
const FileSpec &GetFile() const { return file_sp->GetSpecOnly(); }
Expand Down
19 changes: 19 additions & 0 deletions lldb/include/lldb/Target/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ class Platform : public PluginInterface {
virtual bool GetModuleSpec(const FileSpec &module_file_spec,
const ArchSpec &arch, ModuleSpec &module_spec);

void
CallResolveSourceFileCallbackIfSet(const lldb::ModuleSP &module_sp,
const FileSpec &original_source_file_spec,
FileSpec &resolved_source_file_spec,
bool *did_create_ptr);

virtual Status ConnectRemote(Args &args);

virtual Status DisconnectRemote();
Expand Down Expand Up @@ -967,6 +973,11 @@ class Platform : public PluginInterface {
FileSpec &symbol_file_spec)>
LocateModuleCallback;

typedef std::function<Status(const lldb::ModuleSP &module_sp,
const FileSpec &original_file_spec,
FileSpec &resolved_file_spec)>
ResolveSourceFileCallback;

/// Set locate module callback. This allows users to implement their own
/// module cache system. For example, to leverage artifacts of build system,
/// to bypass pulling files from remote platform, or to search symbol files
Expand All @@ -975,6 +986,13 @@ class Platform : public PluginInterface {

LocateModuleCallback GetLocateModuleCallback() const;

/// Set resolve source file callback. This allows users to implement their own
/// source file cache system. For example, to search source files from source
/// servers.
void SetResolveSourceFileCallback(ResolveSourceFileCallback callback);

ResolveSourceFileCallback GetResolveSourceFileCallback() const;

protected:
/// Create a list of ArchSpecs with the given OS and a architectures. The
/// vendor field is left as an "unspecified unknown".
Expand Down Expand Up @@ -1022,6 +1040,7 @@ class Platform : public PluginInterface {
bool m_calculated_trap_handlers;
const std::unique_ptr<ModuleCache> m_module_cache;
LocateModuleCallback m_locate_module_callback;
ResolveSourceFileCallback m_resolve_source_file_callback;

/// Ask the Platform subclass to fill in the list of trap handler names
///
Expand Down
37 changes: 30 additions & 7 deletions lldb/source/Symbol/LineEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
//===----------------------------------------------------------------------===//

#include "lldb/Symbol/LineEntry.h"
#include "lldb/Core/Module.h"
#include "lldb/Symbol/CompileUnit.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Target/Platform.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Target.h"

Expand Down Expand Up @@ -241,13 +244,33 @@ AddressRange LineEntry::GetSameLineContiguousAddressRange(
return complete_line_range;
}

void LineEntry::ApplyFileMappings(lldb::TargetSP target_sp) {
if (target_sp) {
// Apply any file remappings to our file.
if (auto new_file_spec = target_sp->GetSourcePathMap().FindFile(
original_file_sp->GetSpecOnly())) {
file_sp = std::make_shared<SupportFile>(*new_file_spec,
original_file_sp->GetChecksum());
void LineEntry::ApplyFileMappings(lldb::TargetSP target_sp,
lldb::ModuleSP module_sp) {
if (!target_sp) {
return;
}

// Apply any file remappings to our file.
if (auto new_file_spec = target_sp->GetSourcePathMap().FindFile(
original_file_sp->GetSpecOnly())) {
file_sp = std::make_shared<SupportFile>(*new_file_spec,
original_file_sp->GetChecksum());
}

lldb::PlatformSP platform_sp = target_sp->GetPlatform();
if (module_sp && platform_sp) {
FileSpec original_file_spec = original_file_sp->GetSpecOnly();
FileSpec resolved_source_file_spec;
bool didResolveSourceFile = false;

// Call resolve source file callback if set. This allows users to implement
// their own source file cache system. For example, to search source files
// from source servers.
platform_sp->CallResolveSourceFileCallbackIfSet(
module_sp, original_file_spec, resolved_source_file_spec,
&didResolveSourceFile);
if (didResolveSourceFile) {
file_sp = std::make_shared<SupportFile>(resolved_source_file_spec);
}
}
}
55 changes: 55 additions & 0 deletions lldb/source/Target/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,51 @@ void Platform::CallLocateModuleCallbackIfSet(const ModuleSpec &module_spec,
}
}

void Platform::CallResolveSourceFileCallbackIfSet(
const lldb::ModuleSP &module_sp, const FileSpec &original_source_file_spec,
FileSpec &resolved_source_file_spec, bool *did_create_ptr) {
if (!m_resolve_source_file_callback) {
// Resolve source file callback is not set.
return;
}

Status error = m_resolve_source_file_callback(
module_sp, original_source_file_spec, resolved_source_file_spec);

// Resolve source file callback is set and called. Check the error.
Log *log = GetLog(LLDBLog::Platform);
if (error.Fail()) {
LLDB_LOGF(log, "%s: Resolve source file callback failed: %s",
LLVM_PRETTY_FUNCTION, error.AsCString());
return;
}

if (!resolved_source_file_spec) {
LLDB_LOGF(log,
"%s: Resolve source file callback did not set "
"resolved_source_file_spec",
LLVM_PRETTY_FUNCTION);
return;
}

// If the callback returned a source file, it should exist.
if (resolved_source_file_spec &&
!FileSystem::Instance().Exists(resolved_source_file_spec)) {
LLDB_LOGF(log,
"%s: Resolve source file callback set a non-existent file to "
"source_file_spec: %s",
LLVM_PRETTY_FUNCTION,
resolved_source_file_spec.GetPath().c_str());
// Clear source_file_spec for the error.
resolved_source_file_spec.Clear();
return;
}

if (did_create_ptr) {
*did_create_ptr = true;
}
}

bool Platform::GetCachedSharedModule(const ModuleSpec &module_spec,
lldb::ModuleSP &module_sp,
bool *did_create_ptr) {
Expand Down Expand Up @@ -2104,6 +2149,16 @@ Platform::LocateModuleCallback Platform::GetLocateModuleCallback() const {
return m_locate_module_callback;
}

void Platform::SetResolveSourceFileCallback(
ResolveSourceFileCallback callback) {
m_resolve_source_file_callback = callback;
}

Platform::ResolveSourceFileCallback
Platform::GetResolveSourceFileCallback() const {
return m_resolve_source_file_callback;
}

PlatformSP PlatformList::GetOrCreate(llvm::StringRef name) {
std::lock_guard<std::recursive_mutex> guard(m_mutex);
for (const PlatformSP &platform_sp : m_platforms) {
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Target/StackFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ StackFrame::GetSymbolContext(SymbolContextItem resolve_scope) {
if ((resolved & eSymbolContextLineEntry) &&
!m_sc.line_entry.IsValid()) {
m_sc.line_entry = sc.line_entry;
m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
m_sc.line_entry.ApplyFileMappings(m_sc.target_sp, m_sc.module_sp);
}
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Target/StackFrameList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ bool StackFrameList::FetchFramesUpTo(uint32_t end_idx,

while (unwind_sc.GetParentOfInlinedScope(
curr_frame_address, next_frame_sc, next_frame_address)) {
next_frame_sc.line_entry.ApplyFileMappings(target_sp);
next_frame_sc.line_entry.ApplyFileMappings(target_sp,
unwind_sc.module_sp);
behaves_like_zeroth_frame = false;
StackFrameSP frame_sp(new StackFrame(
m_thread.shared_from_this(), m_frames.size(), idx,
Expand Down
7 changes: 5 additions & 2 deletions lldb/source/Target/ThreadPlanStepRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,11 @@ bool ThreadPlanStepRange::SetNextBranchBreakpoint() {
new SupportFile(call_site_file_spec));
top_most_line_entry.range = range;
top_most_line_entry.file_sp.reset();
top_most_line_entry.ApplyFileMappings(
GetThread().CalculateTarget());

lldb::ModuleSP module_sp =
run_to_address.CalculateSymbolContextModule();
top_most_line_entry.ApplyFileMappings(GetThread().CalculateTarget(),
module_sp);
if (!top_most_line_entry.file_sp)
top_most_line_entry.file_sp =
top_most_line_entry.original_file_sp;
Expand Down
2 changes: 2 additions & 0 deletions lldb/unittests/Symbol/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ add_lldb_unittest(SymbolTests
lldbUtilityHelpers
lldbPluginObjectFileELF
lldbPluginObjectFileMachO
lldbPluginPlatformLinux
lldbPluginSymbolFileDWARF
lldbPluginSymbolFileSymtab
lldbPluginTypeSystemClang
LLVMTestingSupport
)

set(test_inputs
inlined-functions.cpp
inlined-functions.yaml
)
add_unittest_inputs(SymbolTests "${test_inputs}")
22 changes: 22 additions & 0 deletions lldb/unittests/Symbol/Inputs/inlined-functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
inline __attribute__((always_inline)) int sum2(int a, int b) {
int result = a + b;
return result;
}

int sum3(int a, int b, int c) {
int result = a + b + c;
return result;
}

inline __attribute__((always_inline)) int sum4(int a, int b, int c, int d) {
int result = sum2(a, b) + sum2(c, d);
result += 0;
return result;
}

int main(int argc, char **argv) {
sum3(3, 4, 5) + sum2(1, 2);
int sum = sum4(1, 2, 3, 4);
sum2(5, 6);
return 0;
}
Loading
Loading