Skip to content

Commit 302e0d4

Browse files
authored
Remove all kernel32.dll references and debug breakpoints (#108647)
* remove all kernel32.dll and debugbreak references * log exception if run into oom * remove some comments * change debugBreakOnTestHang back to true * throw timeout exception when DebugBreakOnTestHang is false * throw exception if fail to write message to log file * set debugBreakOnTestHang to false by default * write message to log in timeout scenario * print out debug break when debugBreakOnTestHang is true * interrupt if debugbreak is presented, otherwise log and throw exception * add _debugBreakOnTestHang field and MissingTestException class * implement DebugBreakOrThrowException method * implement GenerateExceptionMessageAndHandler method
1 parent da78cf7 commit 302e0d4

File tree

6 files changed

+92
-96
lines changed

6 files changed

+92
-96
lines changed

src/tests/GC/Stress/Framework/RFLogging.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,13 @@ private void LogWorker()
7979
}
8080
catch (IOException e)
8181
{
82-
ReliabilityFramework.MyDebugBreak(String.Format("LogWorker IOException:{0}", e.ToString()));
8382
//Disk may be full so simply stop logging
83+
ExceptionHandler exceptionHandler = ReliabilityFramework.GenerateExceptionMessageAndHandler(ReliabilityFramework._debugBreakOnTestHang, e);
84+
string msg = exceptionHandler.HandleMessage;
85+
Action handler = exceptionHandler.Handler;
86+
87+
Console.WriteLine(msg);
88+
handler();
8489
}
8590
}
8691

@@ -120,7 +125,12 @@ private void LogWorker()
120125
}
121126
catch (IOException e)
122127
{
123-
ReliabilityFramework.MyDebugBreak(String.Format("LogWorker IOException:{0}", e.ToString()));
128+
ExceptionHandler exceptionHandler = ReliabilityFramework.GenerateExceptionMessageAndHandler(ReliabilityFramework._debugBreakOnTestHang, e);
129+
string msg = exceptionHandler.HandleMessage;
130+
Action handler = exceptionHandler.Handler;
131+
132+
Console.WriteLine(msg);
133+
handler();
124134
}
125135
}
126136
}

src/tests/GC/Stress/Framework/ReliabilityFramework.cs

Lines changed: 76 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,29 @@ public interface ISingleReliabilityTest
7979
bool Run(); // returns true on success, false on failure.
8080
}
8181

82+
public sealed class MissingTestException : Exception
83+
{
84+
public MissingTestException()
85+
{
86+
}
87+
88+
public MissingTestException(string message)
89+
: base(message)
90+
{
91+
}
92+
93+
public MissingTestException(string message, Exception inner)
94+
: base(message, inner)
95+
{
96+
}
97+
}
98+
99+
public sealed class ExceptionHandler
100+
{
101+
public string HandleMessage { get; set; }
102+
public Action Handler { get; set; }
103+
}
104+
82105
public class ReliabilityFramework
83106
{
84107
// instance members
@@ -102,6 +125,7 @@ public class ReliabilityFramework
102125
private static Random s_randNum = new Random(s_seed);
103126
private static string timeValue = null;
104127
private static bool s_fNoExit = false;
128+
internal static bool _debugBreakOnTestHang = false;
105129
// constants
106130
private const string waitingText = "Waiting for all tests to finish loading, Remaining Tests: ";
107131

@@ -226,6 +250,19 @@ public static int Main(string[] args)
226250
catch (OutOfMemoryException e)
227251
{
228252
rf.HandleOom(e, "Running tests");
253+
throw e;
254+
}
255+
catch (TimeoutException e)
256+
{
257+
throw e;
258+
}
259+
catch (PathTooLongException e)
260+
{
261+
throw e;
262+
}
263+
catch (MissingTestException e)
264+
{
265+
throw e;
229266
}
230267
catch (Exception e)
231268
{
@@ -271,25 +308,9 @@ public static int Main(string[] args)
271308

272309
public void HandleOom(Exception e, string message)
273310
{
274-
try
275-
{
276-
_logger.WriteToInstrumentationLog(_curTestSet, LoggingLevels.Tests, String.Format("Exception while running tests: {0}", e));
277-
if (_curTestSet.DebugBreakOnOutOfMemory)
278-
{
279-
OomExceptionCausedDebugBreak();
280-
}
281-
}
282-
catch (OutOfMemoryException)
283-
{
284-
// hang and let someone debug if we can't even break in...
285-
Thread.CurrentThread.Join();
286-
}
287-
}
288-
289-
[MethodImpl(MethodImplOptions.NoInlining)]
290-
private void OomExceptionCausedDebugBreak()
291-
{
292-
MyDebugBreak("Harness");
311+
_logger.WriteToInstrumentationLog(_curTestSet, LoggingLevels.Tests, String.Format("Exception while running tests: {0}", e));
312+
ExceptionHandler exceptionHandler = GenerateExceptionMessageAndHandler(_curTestSet.DebugBreakOnOutOfMemory, e);
313+
DebugBreakOrThrowException(exceptionHandler);
293314
}
294315

295316
/// <summary>
@@ -345,6 +366,7 @@ public int RunReliabilityTests(string testConfig, bool doReplay)
345366
_testsRunningCount = 0;
346367
_testsRanCount = 0;
347368
_curTestSet = testSet;
369+
_debugBreakOnTestHang = _curTestSet.DebugBreakOnTestHang;
348370
if (timeValue != null)
349371
_curTestSet.MaximumTime = ReliabilityConfig.ConvertTimeValueToTestRunTime(timeValue);
350372

@@ -506,14 +528,33 @@ public int RunReliabilityTests(string testConfig, bool doReplay)
506528
return (99);
507529
}
508530

509-
[DllImport("kernel32.dll")]
510-
private extern static void DebugBreak();
531+
public static ExceptionHandler GenerateExceptionMessageAndHandler(bool debugBreak, Exception e)
532+
{
533+
ExceptionHandler exceptionHandler = new ExceptionHandler();
511534

512-
[DllImport("kernel32.dll")]
513-
private extern static bool IsDebuggerPresent();
535+
if (debugBreak)
536+
{
537+
exceptionHandler.HandleMessage = String.Format("Interrupt for exception: {0}", e.Message);
538+
exceptionHandler.Handler = delegate() { Debugger.Break(); };
539+
}
540+
else
541+
{
542+
exceptionHandler.HandleMessage = String.Format("Throw exception: {0}", e.Message);
543+
exceptionHandler.Handler = delegate() { throw e; };
544+
}
514545

515-
[DllImport("kernel32.dll")]
516-
private extern static void OutputDebugString(string debugStr);
546+
return exceptionHandler;
547+
}
548+
549+
private void DebugBreakOrThrowException(ExceptionHandler exceptionHandler)
550+
{
551+
string msg = exceptionHandler.HandleMessage;
552+
Action handler = exceptionHandler.Handler;
553+
554+
Console.WriteLine(msg);
555+
_logger.WriteToInstrumentationLog(_curTestSet, LoggingLevels.Tests, msg);
556+
handler();
557+
}
517558

518559
/// <summary>
519560
/// Checks to see if we should block all execution due to a fatal error
@@ -533,27 +574,6 @@ private static void NoExitPoll()
533574
}
534575
}
535576
}
536-
internal static void MyDebugBreak(string extraData)
537-
{
538-
if (IsDebuggerPresent())
539-
{
540-
Console.WriteLine(string.Format("DebugBreak: {0}", extraData));
541-
DebugBreak();
542-
}
543-
{
544-
// We need to stop the process now,
545-
// but all the threads are still running
546-
try
547-
{
548-
Console.WriteLine("MyDebugBreak called, stopping process... {0}", extraData);
549-
}
550-
finally
551-
{
552-
s_fNoExit = true;
553-
Thread.CurrentThread.Join();
554-
}
555-
}
556-
}
557577

558578
/// <summary>
559579
/// Calculates the total number of tests to be run based upon the maximum
@@ -778,9 +798,11 @@ private void TestStarter()
778798
else
779799
{
780800
Thread.Sleep(250); // give the CPU a bit of a rest if we don't need to start a new test.
781-
if (_curTestSet.DebugBreakOnMissingTest && DateTime.Now.Subtract(_startTime) > minTimeToStartTest)
801+
if (DateTime.Now.Subtract(_startTime) > minTimeToStartTest)
782802
{
783-
NewTestsNotStartingDebugBreak();
803+
MissingTestException e = new MissingTestException("New tests not starting");
804+
ExceptionHandler exceptionHandler = GenerateExceptionMessageAndHandler(_curTestSet.DebugBreakOnMissingTest, e);
805+
DebugBreakOrThrowException(exceptionHandler);
784806
}
785807
}
786808
}
@@ -799,12 +821,6 @@ private void TestStarter()
799821
TestSetShutdown(totalTestsToRun);
800822
}
801823

802-
[MethodImpl(MethodImplOptions.NoInlining)]
803-
private void NewTestsNotStartingDebugBreak()
804-
{
805-
MyDebugBreak("Tests haven't been started in a long time!");
806-
}
807-
808824
/// <summary>
809825
/// Shuts down the current test set, waiting for tests to finish, etc...
810826
/// </summary>
@@ -880,22 +896,12 @@ private void TestSetShutdown(int totalTestsToRun)
880896
}
881897
}
882898

883-
if (_curTestSet.DebugBreakOnTestHang)
884-
{
885-
TestIsHungDebugBreak();
886-
}
899+
TimeoutException e = new TimeoutException("Time limit reached.");
900+
ExceptionHandler exceptionHandler = GenerateExceptionMessageAndHandler(_curTestSet.DebugBreakOnTestHang, e);
901+
DebugBreakOrThrowException(exceptionHandler);
887902
}
888903
}
889904

890-
[MethodImpl(MethodImplOptions.NoInlining)]
891-
private void TestIsHungDebugBreak()
892-
{
893-
string msg = String.Format("break");
894-
_logger.WriteToInstrumentationLog(_curTestSet, LoggingLevels.StartupShutdown, msg);
895-
896-
MyDebugBreak("TestHang");
897-
}
898-
899905
/// <summary>
900906
/// Starts the test passed. The test should already be loaded into an app domain.
901907
/// </summary>
@@ -1046,12 +1052,10 @@ private void StartTestWorker(object test)
10461052
}
10471053
_logger.WriteToInstrumentationLog(_curTestSet, LoggingLevels.Tests, String.Format("Test {0} has exited with result {1}", daTest.RefOrID, exitCode));
10481054
}
1049-
catch (PathTooLongException)
1055+
catch (PathTooLongException e)
10501056
{
1051-
if (_curTestSet.DebugBreakOnPathTooLong)
1052-
{
1053-
MyDebugBreak("Path too long");
1054-
}
1057+
ExceptionHandler exceptionHandler = GenerateExceptionMessageAndHandler(_curTestSet.DebugBreakOnPathTooLong, e);
1058+
DebugBreakOrThrowException(exceptionHandler);
10551059
}
10561060
catch (OutOfMemoryException e)
10571061
{
@@ -1196,12 +1200,6 @@ private void StartTestWorker(object test)
11961200
}
11971201
}
11981202

1199-
[MethodImpl(MethodImplOptions.NoInlining)]
1200-
private void UnexpectedThreadAbortDebugBreak()
1201-
{
1202-
MyDebugBreak("Unexpected Thread Abort");
1203-
}
1204-
12051203
/// <summary>
12061204
/// Called after a test has finished executing.
12071205
/// </summary>
@@ -1313,10 +1311,6 @@ private void TestPreLoader(ReliabilityTest test, string[] paths)
13131311
_logger.WriteToInstrumentationLog(_curTestSet, LoggingLevels.Tests, msg);
13141312
test.ConcurrentCopies = 0;
13151313
test.TestLoadFailed = true;
1316-
if (_curTestSet.DebugBreakOnBadTest)
1317-
{
1318-
BadTestDebugBreak(msg);
1319-
}
13201314

13211315
// crash on exceptions when running as a unit test.
13221316
if (IsRunningAsUnitTest)
@@ -1325,11 +1319,6 @@ private void TestPreLoader(ReliabilityTest test, string[] paths)
13251319
Interlocked.Decrement(ref LoadingCount);
13261320
}
13271321

1328-
[MethodImpl(MethodImplOptions.NoInlining)]
1329-
private void BadTestDebugBreak(string msg)
1330-
{
1331-
MyDebugBreak(msg);
1332-
}
13331322

13341323
[MethodImpl(MethodImplOptions.NoInlining)]
13351324
WeakReference UnloadAssemblyLoadContextInner(ReliabilityTest test)

src/tests/GC/Stress/Framework/ReliabilityTestSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ReliabilityTestSet
2424
private string _friendlyName;
2525
private bool _enablePerfCounters = true, _disableLogging = false, _installDetours = false;
2626
private bool _suppressConsoleOutputFromTests = false;
27-
private bool _debugBreakOnTestHang = true;
27+
private bool _debugBreakOnTestHang = false;
2828
private bool _debugBreakOnBadTest = false;
2929
private bool _debugBreakOnOutOfMemory = false;
3030
private bool _debugBreakOnPathTooLong = false;

src/tests/GC/Stress/finalization.config

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
installDetours="false"
1212
minimumTests="0"
1313
minMaxTestUseCPUCount="true"
14-
suppressConsoleOutputFromTests="true"
15-
debugBreakOnTestHang="true">
14+
suppressConsoleOutputFromTests="true">
1615

1716
<Assembly id="GCPerfSim - SOH Allocations Finalizable Objects."
1817
successCode="100"

src/tests/GC/Stress/loh.config

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
installDetours="false"
1212
minimumTests="0"
1313
minMaxTestUseCPUCount="true"
14-
suppressConsoleOutputFromTests="true"
15-
debugBreakOnTestHang="true">
14+
suppressConsoleOutputFromTests="true">
1615

1716
<!-- Add GCPerfSim with No live data on the LOH -->
1817
<Assembly id="GCPerfSim - LOH No Live Data."

src/tests/GC/Stress/poh.config

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
installDetours="false"
1212
minimumTests="0"
1313
minMaxTestUseCPUCount="true"
14-
suppressConsoleOutputFromTests="true"
15-
debugBreakOnTestHang="true">
14+
suppressConsoleOutputFromTests="true">
1615

1716
<!-- Add GCPerfSim with No live data on the POH -->
1817
<Assembly id="GCPerfSim - POH No Live Data."

0 commit comments

Comments
 (0)