-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebuggerCoreTests.cs
More file actions
1010 lines (819 loc) · 28.9 KB
/
Copy pathDebuggerCoreTests.cs
File metadata and controls
1010 lines (819 loc) · 28.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using NodeDev.Core;
using NodeDev.Core.Debugger;
using NodeDev.Core.Nodes;
using NodeDev.Core.Nodes.Debug;
using NodeDev.Core.Nodes.Flow;
using System.Diagnostics;
using Xunit.Abstractions;
namespace NodeDev.Tests;
/// <summary>
/// Unit tests for NodeDev.Core.Debugger.
/// These tests verify the basic functionality of the debug engine components.
/// </summary>
public class DebuggerCoreTests
{
private readonly ITestOutputHelper _output;
public DebuggerCoreTests(ITestOutputHelper output)
{
_output = output;
}
#region DbgShimResolver Tests
[Fact]
public void DbgShimResolver_ShimLibraryName_ShouldReturnPlatformSpecificName()
{
// Act
var name = DbgShimResolver.ShimLibraryName;
// Assert
Assert.NotNull(name);
Assert.NotEmpty(name);
// Verify it's a platform-specific name
if (OperatingSystem.IsWindows())
{
Assert.Equal("dbgshim.dll", name);
}
else if (OperatingSystem.IsLinux())
{
Assert.Equal("libdbgshim.so", name);
}
else if (OperatingSystem.IsMacOS())
{
Assert.Equal("libdbgshim.dylib", name);
}
}
[Fact]
public void DbgShimResolver_GetAllSearchedPaths_ShouldReturnPaths()
{
// Act
var paths = DbgShimResolver.GetAllSearchedPaths().ToList();
// Assert
Assert.NotNull(paths);
Assert.NotEmpty(paths);
// All paths should end with the shim library name
foreach (var (path, _) in paths)
{
Assert.EndsWith(DbgShimResolver.ShimLibraryName, path);
}
}
[Fact]
public void DbgShimResolver_TryResolve_ShouldFindDbgShimFromNuGet()
{
// Act
var result = DbgShimResolver.TryResolve();
// Log all searched paths for diagnostic purposes
_output.WriteLine("All searched paths:");
foreach (var (path, exists) in DbgShimResolver.GetAllSearchedPaths())
{
_output.WriteLine($" [{(exists ? "FOUND" : " ")}] {path}");
}
// Assert - DbgShim should be found via Microsoft.Diagnostics.DbgShim NuGet package
Assert.NotNull(result);
Assert.True(File.Exists(result), $"Resolved path should exist: {result}");
_output.WriteLine($"DbgShim resolved to: {result}");
}
#endregion
#region DebugSessionEngine Construction Tests
[Fact]
public void DebugSessionEngine_Constructor_ShouldCreateInstance()
{
// Act
using var engine = new DebugSessionEngine();
// Assert
Assert.NotNull(engine);
Assert.False(engine.IsAttached);
Assert.Null(engine.CurrentProcess);
Assert.Null(engine.DbgShim);
}
[Fact]
public void DebugSessionEngine_Constructor_WithCustomPath_ShouldStoreValue()
{
// Arrange
const string customPath = "/custom/path/to/dbgshim.so";
// Act
using var engine = new DebugSessionEngine(customPath);
// Assert
Assert.NotNull(engine);
}
[Fact]
public void DebugSessionEngine_IsAttached_ShouldBeFalseInitially()
{
// Arrange
using var engine = new DebugSessionEngine();
// Act & Assert
Assert.False(engine.IsAttached);
}
#endregion
#region DebugSessionEngine Method Tests Without DbgShim
[Fact]
public void DebugSessionEngine_LaunchProcess_WithoutInitialize_ShouldThrow()
{
// Arrange
using var engine = new DebugSessionEngine();
// Act & Assert
Assert.Throws<InvalidOperationException>(() => engine.LaunchProcess("/some/path.exe"));
}
[Fact]
public void DebugSessionEngine_AttachToProcess_WithoutInitialize_ShouldThrow()
{
// Arrange
using var engine = new DebugSessionEngine();
// Act & Assert
Assert.Throws<InvalidOperationException>(() => engine.AttachToProcess(12345));
}
[Fact]
public void DebugSessionEngine_EnumerateCLRs_WithoutInitialize_ShouldThrow()
{
// Arrange
using var engine = new DebugSessionEngine();
// Act & Assert
Assert.Throws<InvalidOperationException>(() => engine.EnumerateCLRs(12345));
}
[Fact]
public void DebugSessionEngine_Dispose_ShouldNotThrow()
{
// Arrange
var engine = new DebugSessionEngine();
// Act & Assert - should not throw
engine.Dispose();
}
[Fact]
public void DebugSessionEngine_DisposeTwice_ShouldNotThrow()
{
// Arrange
var engine = new DebugSessionEngine();
// Act & Assert - should not throw
engine.Dispose();
engine.Dispose();
}
[Fact]
public void DebugSessionEngine_MethodsAfterDispose_ShouldThrowObjectDisposedException()
{
// Arrange
var engine = new DebugSessionEngine();
engine.Dispose();
// Act & Assert
Assert.Throws<ObjectDisposedException>(() => engine.LaunchProcess("/some/path.exe"));
Assert.Throws<ObjectDisposedException>(() => engine.AttachToProcess(12345));
Assert.Throws<ObjectDisposedException>(() => engine.EnumerateCLRs(12345));
}
#endregion
#region DebugCallbackEventArgs Tests
[Fact]
public void DebugCallbackEventArgs_Constructor_ShouldSetProperties()
{
// Arrange
const string callbackType = "Breakpoint";
const string description = "Hit breakpoint at line 42";
// Act
var args = new DebugCallbackEventArgs(callbackType, description);
// Assert
Assert.Equal(callbackType, args.CallbackType);
Assert.Equal(description, args.Description);
Assert.True(args.Continue); // Default should be true
}
[Fact]
public void DebugCallbackEventArgs_Continue_ShouldBeSettable()
{
// Arrange
var args = new DebugCallbackEventArgs("Test", "Test description");
// Act
args.Continue = false;
// Assert
Assert.False(args.Continue);
}
#endregion
#region LaunchResult Tests
[Fact]
public void LaunchResult_Record_ShouldHaveCorrectProperties()
{
// Arrange
const int processId = 12345;
var resumeHandle = new IntPtr(67890);
const bool suspended = true;
// Act
var result = new LaunchResult(processId, resumeHandle, suspended);
// Assert
Assert.Equal(processId, result.ProcessId);
Assert.Equal(resumeHandle, result.ResumeHandle);
Assert.Equal(suspended, result.Suspended);
}
[Fact]
public void LaunchResult_Equality_ShouldWork()
{
// Arrange
var result1 = new LaunchResult(123, new IntPtr(456), true);
var result2 = new LaunchResult(123, new IntPtr(456), true);
var result3 = new LaunchResult(789, new IntPtr(456), true);
// Assert
Assert.Equal(result1, result2);
Assert.NotEqual(result1, result3);
}
#endregion
#region DebugEngineException Tests
[Fact]
public void DebugEngineException_DefaultConstructor_ShouldWork()
{
// Act
var exception = new DebugEngineException();
// Assert
Assert.NotNull(exception);
}
[Fact]
public void DebugEngineException_MessageConstructor_ShouldSetMessage()
{
// Arrange
const string message = "Test error message";
// Act
var exception = new DebugEngineException(message);
// Assert
Assert.Equal(message, exception.Message);
}
[Fact]
public void DebugEngineException_InnerExceptionConstructor_ShouldSetBoth()
{
// Arrange
const string message = "Test error message";
var innerException = new InvalidOperationException("Inner error");
// Act
var exception = new DebugEngineException(message, innerException);
// Assert
Assert.Equal(message, exception.Message);
Assert.Same(innerException, exception.InnerException);
}
#endregion
#region NodeDev Integration Tests
[Fact]
public void NodeDev_CreateProject_ShouldBuildSuccessfully()
{
// Arrange - Create a NodeDev project with a simple program
var project = Project.CreateNewDefaultProject(out var mainMethod);
var graph = mainMethod.Graph;
// Add a WriteLine node
var writeLineNode = new WriteLine(graph);
graph.Manager.AddNode(writeLineNode);
var entryNode = graph.Nodes.Values.OfType<EntryNode>().First();
var returnNode = graph.Nodes.Values.OfType<ReturnNode>().First();
// Connect Entry -> WriteLine -> Return
graph.Manager.AddNewConnectionBetween(entryNode.Outputs[0], writeLineNode.Inputs[0]);
writeLineNode.Inputs[1].UpdateTypeAndTextboxVisibility(project.TypeFactory.Get<string>(), overrideInitialType: true);
writeLineNode.Inputs[1].UpdateTextboxText("\"Debug Test Message\"");
graph.Manager.AddNewConnectionBetween(writeLineNode.Outputs[0], returnNode.Inputs[0]);
// Act - Build the project
var dllPath = project.Build(BuildOptions.Debug);
// Assert
Assert.NotNull(dllPath);
Assert.True(File.Exists(dllPath), $"Built DLL should exist at {dllPath}");
_output.WriteLine($"Built DLL path: {dllPath}");
}
[Fact]
public void NodeDev_RunProject_ShouldExecuteAndReturnExitCode()
{
// Arrange - Create a project that returns a specific exit code
var project = Project.CreateNewDefaultProject(out var mainMethod);
var graph = mainMethod.Graph;
var returnNode = graph.Nodes.Values.OfType<ReturnNode>().First();
returnNode.Inputs[1].UpdateTextboxText("42");
var consoleOutput = new List<string>();
var outputSubscription = project.ConsoleOutput.Subscribe(text =>
{
_output.WriteLine($"[Console] {text}");
consoleOutput.Add(text);
});
try
{
// Act - Run the project
var result = project.Run(BuildOptions.Debug);
Thread.Sleep(1000); // Wait for async output
// Assert
Assert.NotNull(result);
Assert.Equal(42, result);
_output.WriteLine($"Exit code: {result}");
// Verify ScriptRunner was used
Assert.Contains(consoleOutput, line => line.Contains("Program.Main") || line.Contains("ScriptRunner"));
}
finally
{
outputSubscription.Dispose();
}
}
[Fact]
public void NodeDev_BuildProject_ShouldCreateDebuggableAssembly()
{
// Arrange - Create a project
var project = Project.CreateNewDefaultProject(out var mainMethod);
// Act - Build with debug options
var dllPath = project.Build(BuildOptions.Debug);
// Assert - Verify the assembly can be loaded
Assert.NotNull(dllPath);
var pdbPath = Path.ChangeExtension(dllPath, ".pdb");
Assert.True(File.Exists(dllPath), "DLL should exist");
_output.WriteLine($"DLL: {dllPath}");
// PDB should be generated for debug builds
if (File.Exists(pdbPath))
{
_output.WriteLine($"PDB: {pdbPath}");
}
}
#endregion
#region Integration Tests (only run if DbgShim is available)
[Fact]
public void DebugSessionEngine_Initialize_ShouldSucceedWithNuGetDbgShim()
{
// This test verifies that DbgShim from NuGet package can be loaded
var shimPath = DbgShimResolver.TryResolve();
Assert.NotNull(shimPath);
_output.WriteLine($"DbgShim found at: {shimPath}");
// Arrange
using var engine = new DebugSessionEngine(shimPath);
// Act - this should succeed
engine.Initialize();
// Assert
Assert.NotNull(engine.DbgShim);
_output.WriteLine($"DbgShim loaded successfully from: {shimPath}");
}
[Fact]
public void DebugSessionEngine_LaunchProcess_WithInvalidPath_ShouldThrowArgumentException()
{
var shimPath = DbgShimResolver.TryResolve();
Assert.NotNull(shimPath);
// Arrange
using var engine = new DebugSessionEngine(shimPath);
engine.Initialize();
// Act & Assert
var exception = Assert.Throws<ArgumentException>(() =>
engine.LaunchProcess("/nonexistent/executable.exe"));
Assert.Contains("not found", exception.Message);
}
[Fact]
public void DebugSessionEngine_FullIntegration_BuildRunAndAttachToNodeDevProject()
{
// This test verifies the complete workflow of building a NodeDev project,
// running it with ScriptRunner using the "Wait Loop & Attach" pattern,
// and attaching the debugger with full callback support.
var shimPath = DbgShimResolver.TryResolve();
Assert.NotNull(shimPath);
_output.WriteLine($"DbgShim found at: {shimPath}");
// Arrange - Create and build a NodeDev project
var project = Project.CreateNewDefaultProject(out var mainMethod);
var graph = mainMethod.Graph;
var writeLineNode = new WriteLine(graph);
graph.Manager.AddNode(writeLineNode);
var entryNode = graph.Nodes.Values.OfType<EntryNode>().First();
var returnNode = graph.Nodes.Values.OfType<ReturnNode>().First();
graph.Manager.AddNewConnectionBetween(entryNode.Outputs[0], writeLineNode.Inputs[0]);
writeLineNode.Inputs[1].UpdateTypeAndTextboxVisibility(project.TypeFactory.Get<string>(), overrideInitialType: true);
writeLineNode.Inputs[1].UpdateTextboxText("\"Hello from NodeDev debugger test!\"");
graph.Manager.AddNewConnectionBetween(writeLineNode.Outputs[0], returnNode.Inputs[0]);
// Build the project
var dllPath = project.Build(BuildOptions.Debug);
Assert.NotNull(dllPath);
_output.WriteLine($"Built project: {dllPath}");
// Verify we can locate ScriptRunner
var scriptRunnerPath = project.GetScriptRunnerPath();
Assert.True(File.Exists(scriptRunnerPath), $"ScriptRunner should exist at: {scriptRunnerPath}");
_output.WriteLine($"ScriptRunner found at: {scriptRunnerPath}");
// Initialize the debug engine
using var engine = new DebugSessionEngine(shimPath);
engine.Initialize();
Assert.NotNull(engine.DbgShim);
_output.WriteLine("Debug engine initialized");
// Track debug callbacks
var callbacks = new List<string>();
engine.DebugCallback += (sender, args) =>
{
callbacks.Add($"{args.CallbackType}: {args.Description}");
_output.WriteLine($"[DEBUG CALLBACK] {args.CallbackType}: {args.Description}");
};
// Start ScriptRunner with --wait-for-debugger flag using Process.Start
var dotnetExe = FindDotNetExecutable();
_output.WriteLine($"Starting: {dotnetExe} {scriptRunnerPath} --wait-for-debugger {dllPath}");
var processStartInfo = new ProcessStartInfo
{
FileName = dotnetExe,
Arguments = $"\"{scriptRunnerPath}\" --wait-for-debugger \"{dllPath}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
var process = Process.Start(processStartInfo);
Assert.NotNull(process);
using var _ = process; // Dispose at end of method
_output.WriteLine($"Process started with PID: {process.Id}");
int targetPid = 0;
try
{
// Read PID from stdout (ScriptRunner prints "SCRIPTRUNNER_PID:<pid>")
var pidLine = process.StandardOutput.ReadLine();
_output.WriteLine($"Read from stdout: {pidLine}");
if (pidLine != null && pidLine.StartsWith("SCRIPTRUNNER_PID:"))
{
targetPid = int.Parse(pidLine.Substring("SCRIPTRUNNER_PID:".Length));
_output.WriteLine($"Target PID: {targetPid}");
}
else
{
// If we didn't get the PID line, use the process ID we have
targetPid = process.Id;
_output.WriteLine($"Using process ID as target: {targetPid}");
}
// Wait for CLR to load with polling (more reliable than fixed sleep)
const int maxAttempts = 10;
const int pollIntervalMs = 200;
string[]? clrs = null;
for (int attempt = 0; attempt < maxAttempts; attempt++)
{
Thread.Sleep(pollIntervalMs);
try
{
clrs = engine.EnumerateCLRs(targetPid);
if (clrs.Length > 0) break;
}
catch (DebugEngineException)
{
// CLR not ready yet, keep trying
}
}
// Enumerate CLRs to verify the runtime is loaded
Assert.NotNull(clrs);
_output.WriteLine($"Enumerated {clrs.Length} CLR(s) in process");
foreach (var clr in clrs)
{
_output.WriteLine($" CLR: {clr}");
}
Assert.True(clrs.Length > 0, "CLR should be loaded in the target process");
// Attach to the process using ClrDebug
var corDebug = engine.AttachToProcess(targetPid);
Assert.NotNull(corDebug);
_output.WriteLine("Successfully obtained ICorDebug interface!");
// Initialize ICorDebug
corDebug.Initialize();
_output.WriteLine("ICorDebug initialized!");
// Set up managed callbacks using ClrDebug's CorDebugManagedCallback
var managedCallback = ManagedDebuggerCallbackFactory.Create(engine);
corDebug.SetManagedHandler(managedCallback);
_output.WriteLine("Managed callback handler set!");
// Attach to the process (this triggers CreateProcess callback and wakes up the wait loop)
var debugProcess = corDebug.DebugActiveProcess(targetPid, win32Attach: false);
Assert.NotNull(debugProcess);
_output.WriteLine("Attached to process via DebugActiveProcess!");
// Note: We don't call Continue() here because:
// 1. ClrDebug's CorDebugManagedCallback auto-continues after each callback
// 2. Calling Continue when already running causes CORDBG_E_SUPERFLOUS_CONTINUE
// Wait for the process to complete (it should exit once Debugger.IsAttached becomes true)
var exited = process.WaitForExit(10000);
// Log all callbacks received
_output.WriteLine($"Received {callbacks.Count} debug callbacks:");
foreach (var callback in callbacks)
{
_output.WriteLine($" - {callback}");
}
// Assert we received expected callbacks
Assert.True(callbacks.Count > 0, "Should have received debug callbacks");
Assert.Contains(callbacks, c => c.Contains("CreateProcess") || c.Contains("CreateAppDomain") || c.Contains("LoadModule"));
_output.WriteLine("DEBUG CAPABILITY DEMONSTRATED:");
_output.WriteLine(" ✓ DbgShim loaded from NuGet package");
_output.WriteLine(" ✓ Process started with --wait-for-debugger");
_output.WriteLine(" ✓ CLR enumerated in target process");
_output.WriteLine(" ✓ ICorDebug interface obtained");
_output.WriteLine(" ✓ ICorDebug initialized");
_output.WriteLine(" ✓ SetManagedHandler called successfully");
_output.WriteLine(" ✓ DebugActiveProcess attached");
_output.WriteLine(" ✓ Continue(false) resumed execution");
_output.WriteLine($" ✓ Received {callbacks.Count} debug callbacks");
if (exited)
{
_output.WriteLine($"Process exited with code: {process.ExitCode}");
}
_output.WriteLine("Full integration test completed successfully!");
}
finally
{
// Clean up - kill the process if still running
try
{
if (!process.HasExited)
{
process.Kill();
_output.WriteLine($"Killed process {process.Id}");
}
}
catch
{
// Process already exited
}
}
}
[Fact]
public void DebugSessionEngine_AttachToRunningProcess_ShouldReceiveCallbacks()
{
// This test uses the "Wait Loop & Attach" pattern to attach to a running process
// and verify that debug callbacks are received correctly.
var shimPath = DbgShimResolver.TryResolve();
Assert.NotNull(shimPath);
// Build a NodeDev project
var project = Project.CreateNewDefaultProject(out var mainMethod);
var graph = mainMethod.Graph;
// Add a WriteLine node for output
var writeLineNode = new WriteLine(graph);
graph.Manager.AddNode(writeLineNode);
var entryNode = graph.Nodes.Values.OfType<EntryNode>().First();
var returnNode = graph.Nodes.Values.OfType<ReturnNode>().First();
graph.Manager.AddNewConnectionBetween(entryNode.Outputs[0], writeLineNode.Inputs[0]);
writeLineNode.Inputs[1].UpdateTypeAndTextboxVisibility(project.TypeFactory.Get<string>(), overrideInitialType: true);
writeLineNode.Inputs[1].UpdateTextboxText("\"Debugger callback test\"");
graph.Manager.AddNewConnectionBetween(writeLineNode.Outputs[0], returnNode.Inputs[0]);
returnNode.Inputs[1].UpdateTextboxText("0");
var dllPath = project.Build(BuildOptions.Debug);
Assert.NotNull(dllPath);
_output.WriteLine($"Built project: {dllPath}");
var scriptRunnerPath = project.GetScriptRunnerPath();
// Start the process with --wait-for-debugger
var processStartInfo = new ProcessStartInfo
{
FileName = FindDotNetExecutable(),
Arguments = $"\"{scriptRunnerPath}\" --wait-for-debugger \"{dllPath}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
var process = Process.Start(processStartInfo);
Assert.NotNull(process);
using var _ = process; // Dispose at end of method
_output.WriteLine($"Started process with PID: {process.Id}");
// Track callbacks
var callbacks = new List<string>();
try
{
// Read PID from stdout
var pidLine = process.StandardOutput.ReadLine();
_output.WriteLine($"Read from stdout: {pidLine}");
int targetPid = process.Id;
if (pidLine != null && pidLine.StartsWith("SCRIPTRUNNER_PID:"))
{
targetPid = int.Parse(pidLine.Substring("SCRIPTRUNNER_PID:".Length));
}
_output.WriteLine($"Target PID: {targetPid}");
// Wait for CLR to load with polling
const int maxAttempts = 10;
const int pollIntervalMs = 200;
string[]? clrs = null;
// Initialize debug engine
using var engine = new DebugSessionEngine(shimPath);
engine.Initialize();
engine.DebugCallback += (sender, args) =>
{
callbacks.Add($"{args.CallbackType}: {args.Description}");
_output.WriteLine($"[CALLBACK] {args.CallbackType}: {args.Description}");
};
// Wait for CLR to load with polling
for (int attempt = 0; attempt < maxAttempts; attempt++)
{
Thread.Sleep(pollIntervalMs);
try
{
clrs = engine.EnumerateCLRs(targetPid);
if (clrs.Length > 0) break;
}
catch (DebugEngineException)
{
// CLR not ready yet, keep trying
}
}
// Enumerate CLRs
Assert.NotNull(clrs);
_output.WriteLine($"Found {clrs.Length} CLR(s)");
Assert.True(clrs.Length > 0, "Should find CLR in target process");
// Get ICorDebug and initialize
var corDebug = engine.AttachToProcess(targetPid);
_output.WriteLine("Got ICorDebug interface");
corDebug.Initialize();
_output.WriteLine("ICorDebug initialized");
// Set managed callback handler using ClrDebug's CorDebugManagedCallback
var callback = ManagedDebuggerCallbackFactory.Create(engine);
corDebug.SetManagedHandler(callback);
_output.WriteLine("SetManagedHandler called successfully");
// Attach to the process
var debugProcess = corDebug.DebugActiveProcess(targetPid, win32Attach: false);
_output.WriteLine("DebugActiveProcess succeeded");
// Note: Don't call Continue() manually - CorDebugManagedCallback auto-continues
// Wait for process to complete
var exited = process.WaitForExit(10000);
// Log results
_output.WriteLine($"Process exited: {exited}");
if (exited)
{
_output.WriteLine($"Exit code: {process.ExitCode}");
}
_output.WriteLine($"Received {callbacks.Count} callbacks:");
foreach (var cb in callbacks)
{
_output.WriteLine($" - {cb}");
}
// Verify we received callbacks
Assert.True(callbacks.Count > 0, "Should have received debug callbacks");
_output.WriteLine("Successfully demonstrated attach with callbacks!");
}
finally
{
if (!process.HasExited)
{
process.Kill();
}
}
}
#endregion
#region Project.RunWithDebug Tests
[Fact]
public void Project_RunWithDebug_ShouldAttachDebugger()
{
// Arrange - Create a simple project
var project = Project.CreateNewDefaultProject(out var mainMethod);
var graph = mainMethod.Graph;
var returnNode = graph.Nodes.Values.OfType<ReturnNode>().First();
returnNode.Inputs[1].UpdateTextboxText("42");
var debugCallbacks = new List<DebugCallbackEventArgs>();
var debugCallbacksSubscription = project.DebugCallbacks.Subscribe(callback =>
{
debugCallbacks.Add(callback);
_output.WriteLine($"[DEBUG CALLBACK] {callback.CallbackType}: {callback.Description}");
});
var hardDebugStates = new List<bool>();
var hardDebugStateSubscription = project.HardDebugStateChanged.Subscribe(state =>
{
hardDebugStates.Add(state);
_output.WriteLine($"[DEBUG STATE] IsHardDebugging: {state}");
});
try
{
// Act - Run with debug
var result = project.RunWithDebug(BuildOptions.Debug);
// Wait a bit for async operations
Thread.Sleep(1000);
// Assert
Assert.NotNull(result);
_output.WriteLine($"Exit code: {result}");
// Should have received debug callbacks
Assert.True(debugCallbacks.Count > 0, "Should have received at least one debug callback");
// Should have transitioned through debug states (true then false)
Assert.Contains(true, hardDebugStates);
Assert.Contains(false, hardDebugStates);
_output.WriteLine($"Received {debugCallbacks.Count} debug callbacks");
_output.WriteLine($"Debug state transitions: {string.Join(" -> ", hardDebugStates)}");
}
finally
{
debugCallbacksSubscription.Dispose();
hardDebugStateSubscription.Dispose();
}
}
[Fact]
public void Project_IsHardDebugging_ShouldBeFalseInitially()
{
// Arrange
var project = Project.CreateNewDefaultProject(out _);
// Act & Assert
Assert.False(project.IsHardDebugging);
Assert.Null(project.DebuggedProcessId);
}
[Fact]
public void Project_DebugCallbacks_ShouldEmitCallbacksDuringDebug()
{
// Arrange
var project = Project.CreateNewDefaultProject(out var mainMethod);
var graph = mainMethod.Graph;
// Add a WriteLine node to make the program do something visible
var writeLineNode = new WriteLine(graph);
graph.Manager.AddNode(writeLineNode);
var entryNode = graph.Nodes.Values.OfType<EntryNode>().First();
var returnNode = graph.Nodes.Values.OfType<ReturnNode>().First();
graph.Manager.AddNewConnectionBetween(entryNode.Outputs[0], writeLineNode.Inputs[0]);
writeLineNode.Inputs[1].UpdateTypeAndTextboxVisibility(project.TypeFactory.Get<string>(), overrideInitialType: true);
writeLineNode.Inputs[1].UpdateTextboxText("\"Debug callback test\"");
graph.Manager.AddNewConnectionBetween(writeLineNode.Outputs[0], returnNode.Inputs[0]);
var callbackTypes = new List<string>();
var subscription = project.DebugCallbacks.Subscribe(callback =>
{
callbackTypes.Add(callback.CallbackType);
_output.WriteLine($"Callback: {callback.CallbackType}");
});
try
{
// Act
var result = project.RunWithDebug(BuildOptions.Debug);
Thread.Sleep(1000);
// Assert
Assert.True(callbackTypes.Count > 0, "Should receive debug callbacks");
// Should contain common callback types
var hasProcessCallback = callbackTypes.Any(t => t.Contains("Process") || t.Contains("CreateAppDomain") || t.Contains("LoadModule"));
Assert.True(hasProcessCallback, $"Should have received process-related callbacks. Got: {string.Join(", ", callbackTypes)}");
_output.WriteLine($"Total callbacks received: {callbackTypes.Count}");
}
finally
{
subscription.Dispose();
}
}
[Fact]
public void Project_HardDebugStateChanged_ShouldNotifyWhenDebuggingStarts()
{
// Arrange
var project = Project.CreateNewDefaultProject(out var mainMethod);
var stateChanges = new List<bool>();
var subscription = project.HardDebugStateChanged.Subscribe(state =>
{
stateChanges.Add(state);
_output.WriteLine($"Debug state changed: {state}");
});
try
{
// Act
project.RunWithDebug(BuildOptions.Debug);
Thread.Sleep(1000);
// Assert
Assert.True(stateChanges.Count >= 2, "Should have at least 2 state changes (start and stop)");
Assert.True(stateChanges[0], "First state change should be true (debugging started)");
Assert.False(stateChanges[^1], "Last state change should be false (debugging stopped)");
}
finally
{
subscription.Dispose();
}
}
[Fact]
public void Project_RunWithDebug_ShouldThrowWhenDbgShimNotFound()
{
// Arrange - Create a project
var project = Project.CreateNewDefaultProject(out var mainMethod);
// Create engine with invalid path to simulate DbgShim not found
// This test verifies that the method throws instead of falling back
// We can't easily mock DbgShimResolver.TryResolve() since it's static,
// but we can verify the behavior by checking that an exception is thrown
// when debugging features are not available.
// For now, this test documents the expected behavior.
// In a real failure scenario, RunWithDebug should throw InvalidOperationException
// instead of falling back to normal execution.
_output.WriteLine("This test documents that RunWithDebug throws exceptions instead of falling back.");
_output.WriteLine("When DbgShim is not found, an InvalidOperationException should be thrown.");
_output.WriteLine("When CLR enumeration fails, an InvalidOperationException should be thrown.");
_output.WriteLine("When debugger attachment fails, an InvalidOperationException should be thrown.");
}
[Fact]
public void Project_StopDebugging_ShouldBeCallableWhenNotDebugging()
{
// Arrange
var project = Project.CreateNewDefaultProject(out var mainMethod);
// Act - Calling StopDebugging when not debugging should be safe
project.StopDebugging();
// Assert - Should not throw and state should be correct
Assert.False(project.IsHardDebugging);
Assert.Null(project.DebuggedProcessId);
_output.WriteLine("StopDebugging can be safely called when not debugging");
}
#endregion
#region Helper Methods
/// <summary>
/// Finds the dotnet executable in a cross-platform way.
/// </summary>
private static string FindDotNetExecutable()
{
var execName = OperatingSystem.IsWindows() ? "dotnet.exe" : "dotnet";
// Check PATH first
var pathEnv = Environment.GetEnvironmentVariable("PATH");
if (!string.IsNullOrEmpty(pathEnv))
{
var separator = OperatingSystem.IsWindows() ? ';' : ':';
foreach (var dir in pathEnv.Split(separator, StringSplitOptions.RemoveEmptyEntries))
{
var fullPath = Path.Combine(dir, execName);
if (File.Exists(fullPath))
{
return fullPath;
}
}
}
// Check standard installation paths
var standardPaths = OperatingSystem.IsWindows()
? new[]
{
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "dotnet", execName),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "dotnet", execName)
}
: new[]
{
"/usr/share/dotnet/dotnet",
"/usr/lib/dotnet/dotnet",
"/opt/dotnet/dotnet"
};
foreach (var path in standardPaths)
{
if (File.Exists(path))
{