From 25451541059ab737847f16ec6b289a6fc3162624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Srbeck=C3=BD?= Date: Mon, 23 Jun 2008 20:24:01 +0000 Subject: [PATCH] Changed semantics of Process.Terminate: The process is not immediately marked as exited; It is marked as exited when the callback is received. Added tests for process termination. Tracking of COM objects is no longer outputted to the console. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@3132 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Src/Control/NDebugger-Processes.cs | 2 +- .../Project/Src/Control/NDebugger.cs | 2 - .../Src/Control/Process-StateControl.cs | 6 +- .../Project/Src/Control/Process.cs | 1 + .../Project/Src/Internal/ManagedCallback.cs | 1 - .../Src/Internal/ManagedCallbackSwitch.cs | 12 ++-- .../Project/Src/Wrappers/ResourceManager.cs | 2 +- .../Project/Debugger.Tests.csproj | 2 + .../Project/Src/DebuggerTestsBase.cs | 8 ++- .../Src/TestPrograms/MainThreadExit.cs | 7 ++- .../TestPrograms/TerminatePausedProcess.cs | 57 +++++++++++++++++++ .../TestPrograms/TerminateRunningProcess.cs | 54 ++++++++++++++++++ 12 files changed, 139 insertions(+), 15 deletions(-) create mode 100644 src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/TerminatePausedProcess.cs create mode 100644 src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/TerminateRunningProcess.cs diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/NDebugger-Processes.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/NDebugger-Processes.cs index 9690749200..2a4f8729ad 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/NDebugger-Processes.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/NDebugger-Processes.cs @@ -33,7 +33,7 @@ namespace Debugger return process; } } - throw new DebuggerException("Process is not in collection"); + return null; } internal void AddProcess(Process process) diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/NDebugger.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/NDebugger.cs index 512b780667..c9e7f52598 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/NDebugger.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/NDebugger.cs @@ -119,9 +119,7 @@ namespace Debugger TraceMessage("ICorDebug terminated"); - Wrappers.ResourceManager.TraceMessagesEnabled = true; Wrappers.ResourceManager.ReleaseAllTrackedCOMObjects(); - Wrappers.ResourceManager.TraceMessagesEnabled = false; TraceMessage("Tracked COM objects released"); } diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process-StateControl.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process-StateControl.cs index 691269cf09..a41afa9f08 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process-StateControl.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process-StateControl.cs @@ -220,6 +220,9 @@ namespace Debugger corProcess.Continue(0); } + /// Terminates the execution of the process + /// The process does not terminate immediately + /// after the call public void Terminate() { // Resume stoped tread @@ -235,7 +238,8 @@ namespace Debugger corProcess.Stop(uint.MaxValue); corProcess.Terminate(0); - this.NotifyHasExpired(); + // Just give the command and continue without marking the process as + // exited. We will get ExitProcess callback soon } void SelectSomeThread() diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process.cs index 022fc0689a..d2ba6b2a7c 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process.cs @@ -45,6 +45,7 @@ namespace Debugger if (DebuggeeState != null) { ExpireDebuggeeState(); } + debugger.RemoveProcess(this); } } diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Internal/ManagedCallback.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Internal/ManagedCallback.cs index 509181a745..fd5ba7197d 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Internal/ManagedCallback.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Internal/ManagedCallback.cs @@ -431,7 +431,6 @@ namespace Debugger process.TraceMessage("Callback: ExitProcess"); process.NotifyHasExpired(); - process.Debugger.RemoveProcess(process); } #endregion diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Internal/ManagedCallbackSwitch.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Internal/ManagedCallbackSwitch.cs index c9dc81be6f..f808f0dcf6 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Internal/ManagedCallbackSwitch.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Internal/ManagedCallbackSwitch.cs @@ -55,15 +55,17 @@ namespace Debugger public ManagedCallback GetProcessCallbackInterface(ICorDebugProcess pProcess) { Process process = debugger.GetProcess(pProcess); - if (process.HasExpired) { + if (process == null || process.HasExpired) { + debugger.TraceMessage("Ignoring callback of exited process (process not found)"); return null; } + // Check that the process is not exited try { int isRunning = process.CorProcess.IsRunning; } catch (COMException e) { // 0x80131301: Process was terminated if ((uint)e.ErrorCode == 0x80131301) { - process.TraceMessage("Ingoring callback of exited process"); + process.TraceMessage("Ignoring callback of exited process (check failed)"); return null; } } @@ -72,8 +74,10 @@ namespace Debugger public void ExitProcess(ICorDebugProcess pProcess) { - ManagedCallback managedCallback = debugger.GetProcess(pProcess).CallbackInterface; - managedCallback.ExitProcess(pProcess); + ManagedCallback managedCallback = GetProcessCallbackInterface(pProcess); + if (managedCallback != null) { + managedCallback.ExitProcess(pProcess); + } } #region Program folow control diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Wrappers/ResourceManager.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Wrappers/ResourceManager.cs index 811a53b5fc..3cb36faf7a 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Wrappers/ResourceManager.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Wrappers/ResourceManager.cs @@ -30,7 +30,7 @@ namespace Debugger.Wrappers public static class ResourceManager { static MTA2STA mta2sta = new MTA2STA(); - static bool trace = true; + static bool trace = false; static Dictionary trackedCOMObjects = new Dictionary(); public static bool TraceMessagesEnabled { diff --git a/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Debugger.Tests.csproj b/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Debugger.Tests.csproj index e68ed363dc..1c2f8612f5 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Debugger.Tests.csproj +++ b/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Debugger.Tests.csproj @@ -69,6 +69,8 @@ + + diff --git a/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/DebuggerTestsBase.cs b/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/DebuggerTestsBase.cs index 871dcfe2f8..356879f76d 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/DebuggerTestsBase.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/DebuggerTestsBase.cs @@ -78,7 +78,7 @@ namespace Debugger.Tests CheckXmlOutput(); } - void CheckXmlOutput() + protected void CheckXmlOutput() { string startMark = "#if EXPECTED_OUTPUT\r\n"; string endMark = "#endif // EXPECTED_OUTPUT"; @@ -307,12 +307,16 @@ namespace Debugger.Tests string path = Path.GetTempPath(); path = Path.Combine(path, "SharpDevelop"); path = Path.Combine(path, "DebuggerTests"); - path = Path.Combine(path, md5); + path = Path.Combine(path, testName + "." + md5); Directory.CreateDirectory(path); string codeFilename = Path.Combine(path, testName); string exeFilename = Path.Combine(path, testName.Replace(".cs", ".exe")); + if (File.Exists(exeFilename)) { + return exeFilename; + } + StreamWriter file = new StreamWriter(codeFilename); file.Write(code); file.Close(); diff --git a/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/MainThreadExit.cs b/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/MainThreadExit.cs index 6f491f364f..2497e88644 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/MainThreadExit.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/MainThreadExit.cs @@ -19,6 +19,7 @@ namespace Debugger.Tests.TestPrograms System.Threading.Thread t = new System.Threading.Thread(WaitForALongTime); t.Name = "Worker thread"; t.Start(); + System.Threading.Thread.Sleep(0); System.Diagnostics.Debugger.Break(); } @@ -82,11 +83,11 @@ namespace Debugger.Tests { False 0 False - False + True True False - System.Threading.ThreadHelper.ThreadStart - null + System.Threading.WaitHandle.WaitOne + Debugger.Tests.TestPrograms.MainThreadExit.WaitForALongTime Worker thread System.Threading.ThreadHelper.ThreadStart Normal diff --git a/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/TerminatePausedProcess.cs b/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/TerminatePausedProcess.cs new file mode 100644 index 0000000000..2641080ead --- /dev/null +++ b/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/TerminatePausedProcess.cs @@ -0,0 +1,57 @@ +// +// +// +// +// $Revision$ +// + +using System; + +namespace Debugger.Tests.TestPrograms +{ + public class TerminatePausedProcess + { + public static void Main() + { + System.Diagnostics.Debugger.Break(); + } + } +} + +#if TEST_CODE +namespace Debugger.Tests { + public partial class DebuggerTests + { + [NUnit.Framework.Test] + public void TerminatePausedProcess() + { + StartTest("TerminatePausedProcess.cs"); + process.Terminate(); + process.WaitForExit(); + + StartTest("TerminatePausedProcess.cs"); + process.Terminate(); + process.WaitForExit(); + CheckXmlOutput(); + } + } +} +#endif + +#if EXPECTED_OUTPUT + + + + + mscorlib.dll + TerminatePausedProcess.exe + Break + + + mscorlib.dll + TerminatePausedProcess.exe + Break + + + +#endif // EXPECTED_OUTPUT \ No newline at end of file diff --git a/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/TerminateRunningProcess.cs b/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/TerminateRunningProcess.cs new file mode 100644 index 0000000000..ad95c93ee8 --- /dev/null +++ b/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/TerminateRunningProcess.cs @@ -0,0 +1,54 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Threading; + +namespace Debugger.Tests.TestPrograms +{ + public class TerminateRunningProcess + { + static ManualResetEvent doSomething = new ManualResetEvent(false); + + public static void Main() + { + doSomething.WaitOne(); + } + } +} + +#if TEST_CODE +namespace Debugger.Tests { + public partial class DebuggerTests + { + [NUnit.Framework.Test] + public void TerminateRunningProcess() + { + StartTest("TerminateRunningProcess.cs", false); + process.Terminate(); + process.WaitForExit(); + + StartTest("TerminateRunningProcess.cs", false); + process.Terminate(); + process.WaitForExit(); + CheckXmlOutput(); + } + } +} +#endif + +#if EXPECTED_OUTPUT + + + + + + + + + +#endif // EXPECTED_OUTPUT \ No newline at end of file