jingham created this revision.
jingham added reviewers: jasonmolenda, clayborg, JDevlieghere.
jingham requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

process attach -c

stopped working at some point.  Since there was no test for this feature, I'm 
not sure when it broke, but I think it was when we started being more rigorous 
about tracking the interpreter execution context.

The continue in the CommandObjectProcessAttach was handled by calling 
`HandleCommand("process attach", ...)`.  That seems a little odd at first 
blush, but makes sense because it means the result of the continue gets 
reported just as an ordinary user "continue" would.

The problem is that though we've made a new process (and maybe even a new 
target) in the basic attach command, we haven't told the command interpreter 
about them yet, so CheckRequirements will fail before we get a chance to run 
the command.

In this case, since we know exactly which process we want to continue, we 
should just be explicit and pass an execution context for that process to 
HandleCommand.

Also added a test for this.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D110787

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


Index: lldb/test/API/commands/process/attach/TestProcessAttach.py
===================================================================
--- lldb/test/API/commands/process/attach/TestProcessAttach.py
+++ lldb/test/API/commands/process/attach/TestProcessAttach.py
@@ -43,6 +43,23 @@
         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_different_dir_by_id(self):
Index: lldb/source/Commands/CommandObjectProcess.cpp
===================================================================
--- lldb/source/Commands/CommandObjectProcess.cpp
+++ lldb/source/Commands/CommandObjectProcess.cpp
@@ -418,9 +418,10 @@
     }
 
     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);
@@ -472,8 +473,13 @@
 
     // 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();
   }


Index: lldb/test/API/commands/process/attach/TestProcessAttach.py
===================================================================
--- lldb/test/API/commands/process/attach/TestProcessAttach.py
+++ lldb/test/API/commands/process/attach/TestProcessAttach.py
@@ -43,6 +43,23 @@
         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_different_dir_by_id(self):
Index: lldb/source/Commands/CommandObjectProcess.cpp
===================================================================
--- lldb/source/Commands/CommandObjectProcess.cpp
+++ lldb/source/Commands/CommandObjectProcess.cpp
@@ -418,9 +418,10 @@
     }
 
     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);
@@ -472,8 +473,13 @@
 
     // 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();
   }
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
  • [Lldb-commits] [PATCH] ... Jim Ingham via Phabricator via lldb-commits

Reply via email to