Skip to content

[lldb] Allow mapping object file paths #101361

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

Merged
merged 1 commit into from
Jul 31, 2024
Merged
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
2 changes: 2 additions & 0 deletions lldb/include/lldb/Target/Target.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ class TargetProperties : public Properties {

PathMappingList &GetSourcePathMap() const;

PathMappingList &GetObjectPathMap() const;

bool GetAutoSourceMapRelative() const;

FileSpecList GetExecutableSearchPaths();
Expand Down
21 changes: 19 additions & 2 deletions lldb/source/Target/Target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2155,12 +2155,21 @@ bool Target::ReadPointerFromMemory(const Address &addr, Status &error,
return false;
}

ModuleSP Target::GetOrCreateModule(const ModuleSpec &module_spec, bool notify,
Status *error_ptr) {
ModuleSP Target::GetOrCreateModule(const ModuleSpec &orig_module_spec,
bool notify, Status *error_ptr) {
ModuleSP module_sp;

Status error;

// Apply any remappings specified in target.object-map:
ModuleSpec module_spec(orig_module_spec);
PathMappingList &obj_mapping = GetObjectPathMap();
if (std::optional<FileSpec> remapped_obj_file =
obj_mapping.RemapPath(orig_module_spec.GetFileSpec().GetPath(),
true /* only_if_exists */)) {
module_spec.GetFileSpec().SetPath(remapped_obj_file->GetPath());
}

// First see if we already have this module in our module list. If we do,
// then we're done, we don't need to consult the shared modules list. But
// only do this if we are passed a UUID.
Expand Down Expand Up @@ -4459,6 +4468,14 @@ PathMappingList &TargetProperties::GetSourcePathMap() const {
return option_value->GetCurrentValue();
}

PathMappingList &TargetProperties::GetObjectPathMap() const {
const uint32_t idx = ePropertyObjectMap;
OptionValuePathMappings *option_value =
m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings(idx);
assert(option_value);
return option_value->GetCurrentValue();
}

bool TargetProperties::GetAutoSourceMapRelative() const {
const uint32_t idx = ePropertyAutoSourceMapRelative;
return GetPropertyAtIndexAs<bool>(
Expand Down
3 changes: 3 additions & 0 deletions lldb/source/Target/TargetProperties.td
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ let Definition = "target" in {
def SourceMap: Property<"source-map", "PathMap">,
DefaultStringValue<"">,
Desc<"Source path remappings apply substitutions to the paths of source files, typically needed to debug from a different host than the one that built the target. The source-map property consists of an array of pairs, the first element is a path prefix, and the second is its replacement. The syntax is `prefix1 replacement1 prefix2 replacement2...`. The pairs are checked in order, the first prefix that matches is used, and that prefix is substituted with the replacement. A common pattern is to use source-map in conjunction with the clang -fdebug-prefix-map flag. In the build, use `-fdebug-prefix-map=/path/to/build_dir=.` to rewrite the host specific build directory to `.`. Then for debugging, use `settings set target.source-map . /path/to/local_dir` to convert `.` to a valid local path.">;
def ObjectMap: Property<"object-map", "PathMap">,
DefaultStringValue<"">,
Desc<"Object path remappings apply substitutions to the paths of object files, typically needed to debug from a different host than the one that built the target. The object-map property consists of an array of pairs, the first element is a path prefix, and the second is its replacement. The syntax is `prefix1 replacement1 prefix2 replacement2...`. The pairs are checked in order, the first prefix that matches is used, and that prefix is substituted with the replacement.">;
def AutoSourceMapRelative: Property<"auto-source-map-relative", "Boolean">,
DefaultTrue,
Desc<"Automatically deduce source path mappings based on source file breakpoint resolution. It only deduces source mapping if source file breakpoint request is using full path and if the debug info contains relative paths.">;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,32 @@ def test_i386_sysroot(self):

self.dbg.DeleteTarget(target)

def test_object_map(self):
"""Test that lldb can find the exe for an i386 linux core file using the object map."""

# Copy linux-i386.out to lldb_i386_object_map/a.out
tmp_object_map_root = os.path.join(self.getBuildDir(), "lldb_i386_object_map")
executable = os.path.join(tmp_object_map_root, "a.out")
lldbutil.mkdir_p(os.path.dirname(executable))
shutil.copyfile("linux-i386.out", executable)

# Replace the original module path at /home/labath/test and load the core
self.runCmd(
"settings set target.object-map /home/labath/test {}".format(
tmp_object_map_root
)
)

target = self.dbg.CreateTarget(None)
process = target.LoadCore("linux-i386.core")

# Check that we did load the mapped executable
exe_module_spec = process.GetTarget().GetModuleAtIndex(0).GetFileSpec()
self.assertTrue(exe_module_spec.fullpath.startswith(tmp_object_map_root))

self.check_all(process, self._i386_pid, self._i386_regions, "a.out")
self.dbg.DeleteTarget(target)

@skipIfLLVMTargetMissing("X86")
@skipIfWindows
def test_x86_64_sysroot(self):
Expand Down
Loading