Skip to content

[lldb/Target] Delay image loading after corefile process creation #70351

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
Nov 1, 2023
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/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ class Process : public std::enable_shared_from_this<Process>,
return error;
}

virtual void DidLoadCore() {}

/// The "ShadowListener" for a process is just an ordinary Listener that
/// listens for all the Process event bits. It's convenient because you can
/// specify it in the LaunchInfo or AttachInfo, so it will get events from
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,6 @@ Status ProcessMachCore::DoLoadCore() {

CreateMemoryRegions();

LoadBinariesAndSetDYLD();

CleanupMemoryRegionPermissions();

AddressableBits addressable_bits = core_objfile->GetAddressableBits();
Expand All @@ -580,6 +578,8 @@ Status ProcessMachCore::DoLoadCore() {
return error;
}

void ProcessMachCore::DidLoadCore() { LoadBinariesAndSetDYLD(); }

lldb_private::DynamicLoader *ProcessMachCore::GetDynamicLoader() {
if (m_dyld_up.get() == nullptr)
m_dyld_up.reset(DynamicLoader::FindPlugin(this, m_dyld_plugin_name));
Expand Down
2 changes: 2 additions & 0 deletions lldb/source/Plugins/Process/mach-core/ProcessMachCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class ProcessMachCore : public lldb_private::PostMortemProcess {
// Creating a new process, or attaching to an existing one
lldb_private::Status DoLoadCore() override;

void DidLoadCore() override;

lldb_private::DynamicLoader *GetDynamicLoader() override;

// PluginInterface protocol
Expand Down
29 changes: 16 additions & 13 deletions lldb/source/Target/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2639,19 +2639,6 @@ Status Process::LoadCore() {
else
StartPrivateStateThread();

DynamicLoader *dyld = GetDynamicLoader();
if (dyld)
dyld->DidAttach();

GetJITLoaders().DidAttach();

SystemRuntime *system_runtime = GetSystemRuntime();
if (system_runtime)
system_runtime->DidAttach();

if (!m_os_up)
LoadOperatingSystemPlugin(false);

// We successfully loaded a core file, now pretend we stopped so we can
// show all of the threads in the core file and explore the crashed state.
SetPrivateState(eStateStopped);
Expand All @@ -2668,7 +2655,23 @@ Status Process::LoadCore() {
StateAsCString(state));
error.SetErrorString(
"Did not get stopped event after loading the core file.");
} else {
DidLoadCore();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call DidAttach(...) instead?


DynamicLoader *dyld = GetDynamicLoader();
if (dyld)
dyld->DidAttach();

GetJITLoaders().DidAttach();

SystemRuntime *system_runtime = GetSystemRuntime();
if (system_runtime)
system_runtime->DidAttach();

if (!m_os_up)
LoadOperatingSystemPlugin(false);
}

RestoreProcessEvents();
}
return error;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CXX_SOURCES := main.cpp

override ARCH := $(shell uname -m)

include Makefile.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
Test loading python scripting resource from corefile
"""

import os, tempfile

import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
from lldbsuite.test import lldbtest


class ScriptResourceLoadingTestCase(TestBase):
NO_DEBUG_INFO_TESTCASE = True

def create_stack_skinny_corefile(self, file):
self.build()
target, process, thread, _ = lldbutil.run_to_source_breakpoint(
self, "// break", lldb.SBFileSpec("main.cpp")
)
self.assertTrue(process.IsValid(), "Process is invalid.")
# FIXME: Use SBAPI to save the process corefile.
self.runCmd("process save-core -s stack " + file)
self.assertTrue(os.path.exists(file), "No stack-only corefile found.")
self.assertTrue(self.dbg.DeleteTarget(target), "Couldn't delete target")

def move_blueprint_to_dsym(self, blueprint_name):
blueprint_origin_path = os.path.join(self.getSourceDir(), blueprint_name)
dsym_bundle = self.getBuildArtifact("a.out.dSYM")
blueprint_destination_path = os.path.join(
dsym_bundle, "Contents", "Resources", "Python"
)
if not os.path.exists(blueprint_destination_path):
os.mkdir(blueprint_destination_path)

blueprint_destination_path = os.path.join(
blueprint_destination_path, "a_out.py"
)
shutil.copy(blueprint_origin_path, blueprint_destination_path)

@skipUnlessDarwin
def test_script_resource_loading(self):
"""
Test that we're able to load the python scripting resource from
corefile dSYM bundle.

"""
self.build()

self.runCmd("settings set target.load-script-from-symbol-file true")
self.move_blueprint_to_dsym("my_scripting_resource.py")

corefile_process = None
with tempfile.NamedTemporaryFile() as file:
self.create_stack_skinny_corefile(file.name)
corefile_target = self.dbg.CreateTarget(None)
corefile_process = corefile_target.LoadCore(
self.getBuildArtifact(file.name)
)
self.assertTrue(corefile_process, PROCESS_IS_VALID)
self.expect("command script list", substrs=["test_script_resource_loading"])
self.runCmd("test_script_resource_loading")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
int foo() {
return 42; // break
}

int main() { return foo(); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sys, lldb


def test_script_resource_loading(debugger, command, exe_ctx, result, dict):
if not exe_ctx.target.process.IsValid():
result.SetError("invalid process")
process = exe_ctx.target.process
if not len(process):
result.SetError("invalid thread count")


def __lldb_init_module(debugger, dict):
debugger.HandleCommand(
"command script add -o -f a_out.test_script_resource_loading test_script_resource_loading"
)