[Lldb-commits] [lldb] 2303391 - Make "process attach -c" work correctly, and add a test for it.

Jim Ingham via lldb-commits lldb-commits at lists.llvm.org
Wed Sep 29 19:40:34 PDT 2021


Author: Jim Ingham
Date: 2021-09-29T19:38:09-07:00
New Revision: 2303391d1f543f4e57f9ed0fc68bad2d4cf890dc

URL: https://github.com/llvm/llvm-project/commit/2303391d1f543f4e57f9ed0fc68bad2d4cf890dc
DIFF: https://github.com/llvm/llvm-project/commit/2303391d1f543f4e57f9ed0fc68bad2d4cf890dc.diff

LOG: Make "process attach -c" work correctly, and add a test for it.

The issue here was that we were not updating the interpreter's
execution context when calling HandleCommand to continue the process.
Since we had just created the process, it wasn't in the interpreter's
execution context so HandleCommand failed at CheckRequirements.  The
patch fixes that by passing the process execution context directly
to HandleCommand.

Differential Revision: https://reviews.llvm.org/D110787

Added: 
    

Modified: 
    lldb/source/Commands/CommandObjectProcess.cpp
    lldb/test/API/commands/process/attach/TestProcessAttach.py

Removed: 
    


################################################################################
diff  --git a/lldb/source/Commands/CommandObjectProcess.cpp b/lldb/source/Commands/CommandObjectProcess.cpp
index b3e2f6a1a02b7..f3d20b390c6d1 100644
--- a/lldb/source/Commands/CommandObjectProcess.cpp
+++ b/lldb/source/Commands/CommandObjectProcess.cpp
@@ -398,9 +398,10 @@ class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach {
     }
 
     StreamString stream;
+    ProcessSP process_sp;
     const auto error = target->Attach(m_options.attach_info, &stream);
     if (error.Success()) {
-      ProcessSP process_sp(target->GetProcessSP());
+      process_sp = target->GetProcessSP();
       if (process_sp) {
         result.AppendMessage(stream.GetString());
         result.SetStatus(eReturnStatusSuccessFinishNoResult);
@@ -452,8 +453,13 @@ class CommandObjectProcessAttach : public CommandObjectProcessLaunchOrAttach {
 
     // This supports the use-case scenario of immediately continuing the
     // process once attached.
-    if (m_options.attach_info.GetContinueOnceAttached())
-      m_interpreter.HandleCommand("process continue", eLazyBoolNo, result);
+    if (m_options.attach_info.GetContinueOnceAttached()) {
+      // We have made a process but haven't told the interpreter about it yet,
+      // so CheckRequirements will fail for "process continue".  Set the override
+      // here:
+      ExecutionContext exe_ctx(process_sp);
+      m_interpreter.HandleCommand("process continue", eLazyBoolNo, exe_ctx, result);
+    }
 
     return result.Succeeded();
   }

diff  --git a/lldb/test/API/commands/process/attach/TestProcessAttach.py b/lldb/test/API/commands/process/attach/TestProcessAttach.py
index b33aeebccdd1e..4738ee5851e7a 100644
--- a/lldb/test/API/commands/process/attach/TestProcessAttach.py
+++ b/lldb/test/API/commands/process/attach/TestProcessAttach.py
@@ -43,6 +43,23 @@ def test_attach_to_process_by_id(self):
         process = target.GetProcess()
         self.assertTrue(process, PROCESS_IS_VALID)
 
+    @skipIfiOSSimulator
+    def test_attach_to_process_by_id_autocontinue(self):
+        """Test attach by process id"""
+        self.build()
+        exe = self.getBuildArtifact(exe_name)
+
+        # Spawn a new process
+        popen = self.spawnSubprocess(exe)
+
+        self.runCmd("process attach -c -p " + str(popen.pid))
+
+        target = self.dbg.GetSelectedTarget()
+
+        process = target.GetProcess()
+        self.assertTrue(process, PROCESS_IS_VALID)
+        self.assertTrue(process.GetState(), lldb.eStateRunning)
+
     @skipIfReproducer # FIXME: Unexpected packet during (active) replay
     @skipIfWindows # This is flakey on Windows AND when it fails, it hangs: llvm.org/pr48806
     def test_attach_to_process_from_
diff erent_dir_by_id(self):


        


More information about the lldb-commits mailing list