Explorar el Código

LibDebug: Add static attach() function

This function is similar to exec_and_attach(), but instead of spawning
a new process and debugging it, it attaches to an existing process.
Itamar hace 2 años
padre
commit
26250fe14a

+ 20 - 0
Userland/Libraries/LibDebug/DebugSession.cpp

@@ -129,6 +129,26 @@ OwnPtr<DebugSession> DebugSession::exec_and_attach(DeprecatedString const& comma
     return debug_session;
     return debug_session;
 }
 }
 
 
+OwnPtr<DebugSession> DebugSession::attach(pid_t pid, DeprecatedString source_root)
+{
+    if (ptrace(PT_ATTACH, pid, 0, 0) < 0) {
+        perror("PT_ATTACH");
+        return {};
+    }
+
+    int status = 0;
+    if (waitpid(pid, &status, WSTOPPED | WEXITED) != pid || !WIFSTOPPED(status)) {
+        perror("waitpid");
+        return {};
+    }
+
+    auto debug_session = adopt_own(*new DebugSession(pid, source_root));
+    // At this point, libraries should have been loaded
+    debug_session->update_loaded_libs();
+
+    return debug_session;
+}
+
 bool DebugSession::poke(FlatPtr address, FlatPtr data)
 bool DebugSession::poke(FlatPtr address, FlatPtr data)
 {
 {
     if (ptrace(PT_POKE, m_debuggee_pid, bit_cast<void*>(address), bit_cast<void*>(data)) < 0) {
     if (ptrace(PT_POKE, m_debuggee_pid, bit_cast<void*>(address), bit_cast<void*>(data)) < 0) {

+ 1 - 0
Userland/Libraries/LibDebug/DebugSession.h

@@ -28,6 +28,7 @@ namespace Debug {
 class DebugSession : public ProcessInspector {
 class DebugSession : public ProcessInspector {
 public:
 public:
     static OwnPtr<DebugSession> exec_and_attach(DeprecatedString const& command, DeprecatedString source_root = {}, Function<ErrorOr<void>()> setup_child = {});
     static OwnPtr<DebugSession> exec_and_attach(DeprecatedString const& command, DeprecatedString source_root = {}, Function<ErrorOr<void>()> setup_child = {});
+    static OwnPtr<DebugSession> attach(pid_t pid, DeprecatedString source_root = {});
 
 
     virtual ~DebugSession() override;
     virtual ~DebugSession() override;