-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathdebugger.cs
5731 lines (4904 loc) · 203 KB
/
debugger.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Management.Automation.Host;
using System.Management.Automation.Internal.Host;
using System.Management.Automation.Language;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Internal;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Diagnostics.CodeAnalysis;
namespace System.Management.Automation
{
#region Event Args
/// <summary>
/// Possible actions for the debugger after hitting a breakpoint/step
/// </summary>
public enum DebuggerResumeAction
{
/// <summary>
/// Continue running until the next breakpoint, or the end of the script
/// </summary>
Continue = 0,
/// <summary>
/// Step to next statement, going into functions, scripts, etc
/// </summary>
StepInto = 1,
/// <summary>
/// Step to next statement, going over functions, scripts, etc
/// </summary>
StepOut = 2,
/// <summary>
/// Step to next statement after the current function, script, etc
/// </summary>
StepOver = 3,
/// <summary>
/// Stop executing the script
/// </summary>
Stop = 4,
};
/// <summary>
/// Arguments for the DebuggerStop event.
/// </summary>
public class DebuggerStopEventArgs : EventArgs
{
/// <summary>
/// Initializes the DebuggerStopEventArgs
/// </summary>
internal DebuggerStopEventArgs(InvocationInfo invocationInfo, List<Breakpoint> breakpoints)
{
this.InvocationInfo = invocationInfo;
this.Breakpoints = new ReadOnlyCollection<Breakpoint>(breakpoints);
this.ResumeAction = DebuggerResumeAction.Continue;
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="invocationInfo"></param>
/// <param name="breakpoints"></param>
/// <param name="resumeAction"></param>
public DebuggerStopEventArgs(
InvocationInfo invocationInfo,
Collection<Breakpoint> breakpoints,
DebuggerResumeAction resumeAction)
{
this.InvocationInfo = invocationInfo;
this.Breakpoints = new ReadOnlyCollection<Breakpoint>(breakpoints);
this.ResumeAction = resumeAction;
}
/// <summary>
/// Invocation info of the code being executed
/// </summary>
public InvocationInfo InvocationInfo { get; internal set; }
/// <summary>
/// The breakpoint(s) hit
/// </summary>
/// <remarks>
/// Note there may be more than one breakpoint on the same object (line, variable, command). A single event is
/// raised for all these breakpoints.
/// </remarks>
public ReadOnlyCollection<Breakpoint> Breakpoints { get; private set; }
/// <summary>
/// This property must be set in the event handler to indicate the debugger what it should do next
/// </summary>
/// <remarks>
/// The default action is DebuggerAction.Continue.
/// DebuggerAction.StepToLine is only valid when debugging an script.
/// </remarks>
public DebuggerResumeAction ResumeAction { get; set; }
/// <summary>
/// This property is used internally for remote debug stops only. It is used to signal the remote debugger proxy
/// that it should *not* send a resume action to the remote debugger. This is used by runspace debug processing to
/// leave pending runspace debug sessions suspended until a debugger is attached.
/// </summary>
internal bool SuspendRemote { get; set; }
};
/// <summary>
/// Kinds of breakpoint updates
/// </summary>
public enum BreakpointUpdateType
{
/// <summary>
/// A breakpoint was set
/// </summary>
Set = 0,
/// <summary>
/// A breakpoint was removed
/// </summary>
Removed = 1,
/// <summary>
/// A breakpoint was enabled
/// </summary>
Enabled = 2,
/// <summary>
/// A breakpoint was disabled
/// </summary>
Disabled = 3
};
/// <summary>
/// Arguments for the BreakpointUpdated event.
/// </summary>
public class BreakpointUpdatedEventArgs : EventArgs
{
/// <summary>
/// Initializes the BreakpointUpdatedEventArgs
/// </summary>
internal BreakpointUpdatedEventArgs(Breakpoint breakpoint, BreakpointUpdateType updateType, int breakpointCount)
{
this.Breakpoint = breakpoint;
this.UpdateType = updateType;
this.BreakpointCount = breakpointCount;
}
/// <summary>
/// Gets the breakpoint that was updated
/// </summary>
public Breakpoint Breakpoint { get; private set; }
/// <summary>
/// Gets the type of update
/// </summary>
public BreakpointUpdateType UpdateType { get; private set; }
/// <summary>
/// Gets the current breakpoint count
/// </summary>
public int BreakpointCount { get; private set; }
};
#region PSJobStartEventArgs
/// <summary>
/// Arguments for the script job start callback event.
/// </summary>
public sealed class PSJobStartEventArgs : EventArgs
{
/// <summary>
/// Job to be started
/// </summary>
public Job Job
{
get;
private set;
}
/// <summary>
/// Job debugger
/// </summary>
public Debugger Debugger
{
get;
private set;
}
/// <summary>
/// Job is run asynchronously
/// </summary>
public bool IsAsync
{
get;
private set;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="job">Started job</param>
/// <param name="debugger">Debugger</param>
/// <param name="isAsync">Job started asynchronously</param>
public PSJobStartEventArgs(Job job, Debugger debugger, bool isAsync)
{
this.Job = job;
this.Debugger = debugger;
this.IsAsync = isAsync;
}
}
#endregion
#region Runspace Debug Processing
/// <summary>
/// StartRunspaceDebugProcessing event arguments
/// </summary>
public sealed class StartRunspaceDebugProcessingEventArgs : EventArgs
{
/// <summary> The runspace to process </summary>
public Runspace Runspace
{
get;
private set;
}
/// <summary>
/// When set to true this will cause PowerShell to process this runspace debug session through its
/// script debugger. To use the default processing return from this event call after setting
/// this property to true.
/// </summary>
public bool UseDefaultProcessing
{
get;
set;
}
/// <summary>
/// Constructor
/// </summary>
public StartRunspaceDebugProcessingEventArgs(Runspace runspace)
{
if (runspace == null) { throw new PSArgumentNullException("runspace"); }
Runspace = runspace;
}
}
/// <summary>
/// ProcessRunspaceDebugEnd event arguments
/// </summary>
public sealed class ProcessRunspaceDebugEndEventArgs : EventArgs
{
/// <summary>
/// The runspace where internal debug processing has ended
/// </summary>
public Runspace Runspace
{
get;
private set;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="runspace"></param>
public ProcessRunspaceDebugEndEventArgs(Runspace runspace)
{
if (runspace == null) { throw new PSArgumentNullException("runspace"); }
Runspace = runspace;
}
}
#endregion
#endregion
#region Enums
/// <summary>
/// Defines debugging mode.
/// </summary>
[Flags]
public enum DebugModes
{
/// <summary>
/// PowerShell script debugging is disabled.
/// </summary>
None = 0x0,
/// <summary>
/// Default setting for original PowerShell script debugging.
/// Compatible with PowerShell Versions 2 and 3.
/// </summary>
Default = 0x1,
/// <summary>
/// PowerShell script debugging including workflow script.
/// </summary>
LocalScript = 0x2,
/// <summary>
/// PowerShell remote script and workflow debugging.
/// </summary>
RemoteScript = 0x4
};
/// <summary>
/// Defines unhandled breakpoint processing behavior
/// </summary>
internal enum UnhandledBreakpointProcessingMode
{
/// <summary>
/// Ignore unhandled breakpoint events.
/// </summary>
Ignore = 1,
/// <summary>
/// Wait on unhandled breakpoint events until a handler is available.
/// </summary>
Wait
}
#endregion
#region Debugger base class
/// <summary>
/// Base class for all PowerShell debuggers.
/// </summary>
public abstract class Debugger
{
#region Events
/// <summary>
/// Event raised when the debugger hits a breakpoint or a step
/// </summary>
public event EventHandler<DebuggerStopEventArgs> DebuggerStop;
/// <summary>
/// Event raised when a breakpoint is updated
/// </summary>
public event EventHandler<BreakpointUpdatedEventArgs> BreakpointUpdated;
/// <summary>
/// Event raised when nested debugging is cancelled.
/// </summary>
internal event EventHandler<EventArgs> NestedDebuggingCancelledEvent;
#region Runspace Debug Processing Events
/// <summary>
/// Event raised when a runspace debugger needs breakpoint processing.
/// </summary>
public event EventHandler<StartRunspaceDebugProcessingEventArgs> StartRunspaceDebugProcessing;
/// <summary>
/// Event raised when a runspace debugger is finished being processed.
/// </summary>
public event EventHandler<ProcessRunspaceDebugEndEventArgs> RunspaceDebugProcessingCompleted;
/// <summary>
/// Event raised to indicate that the debugging session is over and runspace debuggers queued for
/// processing should be released.
/// </summary>
public event EventHandler<EventArgs> CancelRunspaceDebugProcessing;
#endregion
#endregion
#region Properties
/// <summary>
/// True when the debugger is stopped.
/// </summary>
protected bool DebuggerStopped
{
get;
private set;
}
/// <summary>
/// IsPushed
/// </summary>
internal virtual bool IsPushed
{
get { return false; }
}
/// <summary>
/// IsRemote
/// </summary>
internal virtual bool IsRemote
{
get { return false; }
}
/// <summary>
/// Returns true if the debugger is preserving a DebuggerStopEvent
/// event. Use ReleaseSavedDebugStop() to allow event to process.
/// </summary>
internal virtual bool IsPendingDebugStopEvent
{
get { throw new PSNotImplementedException(); }
}
/// <summary>
/// Returns true if debugger has been set to stepInto mode.
/// </summary>
internal virtual bool IsDebuggerSteppingEnabled
{
get { throw new PSNotImplementedException(); }
}
/// <summary>
/// Returns true if there is a handler for debugger stops.
/// </summary>
internal bool IsDebugHandlerSubscribed
{
get { return (DebuggerStop != null); }
}
/// <summary>
/// UnhandledBreakpointMode
/// </summary>
internal virtual UnhandledBreakpointProcessingMode UnhandledBreakpointMode
{
get { throw new PSNotImplementedException(); }
set { throw new PSNotImplementedException(); }
}
/// <summary>
/// DebuggerMode
/// </summary>
public DebugModes DebugMode { get; protected set; } = DebugModes.Default;
/// <summary>
/// Returns true if debugger has breakpoints set and
/// is currently active.
/// </summary>
public virtual bool IsActive
{
get { return false; }
}
/// <summary>
/// InstanceId
/// </summary>
public virtual Guid InstanceId
{
get { return s_instanceId; }
}
/// <summary>
/// True when debugger is stopped at a breakpoint.
/// </summary>
public virtual bool InBreakpoint
{
get { return DebuggerStopped; }
}
#endregion
#region Protected Methods
/// <summary>
/// RaiseDebuggerStopEvent
/// </summary>
/// <param name="args">DebuggerStopEventArgs</param>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected void RaiseDebuggerStopEvent(DebuggerStopEventArgs args)
{
try
{
DebuggerStopped = true;
DebuggerStop.SafeInvoke<DebuggerStopEventArgs>(this, args);
}
finally
{
DebuggerStopped = false;
}
}
/// <summary>
/// IsDebuggerStopEventSubscribed
/// </summary>
/// <returns>True if event subscription exists</returns>
protected bool IsDebuggerStopEventSubscribed()
{
return (DebuggerStop != null);
}
/// <summary>
/// RaiseBreakpointUpdatedEvent
/// </summary>
/// <param name="args">BreakpointUpdatedEventArgs</param>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected void RaiseBreakpointUpdatedEvent(BreakpointUpdatedEventArgs args)
{
BreakpointUpdated.SafeInvoke<BreakpointUpdatedEventArgs>(this, args);
}
/// <summary>
/// IsDebuggerBreakpointUpdatedEventSubscribed
/// </summary>
/// <returns>True if event subscription exists</returns>
protected bool IsDebuggerBreakpointUpdatedEventSubscribed()
{
return (BreakpointUpdated != null);
}
#region Runspace Debug Processing
/// <summary/>
protected void RaiseStartRunspaceDebugProcessingEvent(StartRunspaceDebugProcessingEventArgs args)
{
if (args == null) { throw new PSArgumentNullException("args"); }
StartRunspaceDebugProcessing.SafeInvoke<StartRunspaceDebugProcessingEventArgs>(this, args);
}
/// <summary/>
protected void RaiseRunspaceProcessingCompletedEvent(ProcessRunspaceDebugEndEventArgs args)
{
if (args == null) { throw new PSArgumentNullException("args"); }
RunspaceDebugProcessingCompleted.SafeInvoke<ProcessRunspaceDebugEndEventArgs>(this, args);
}
/// <summary/>
protected bool IsStartRunspaceDebugProcessingEventSubscribed()
{
return (StartRunspaceDebugProcessing != null);
}
/// <summary/>
protected void RaiseCancelRunspaceDebugProcessingEvent()
{
CancelRunspaceDebugProcessing.SafeInvoke<EventArgs>(this, null);
}
#endregion
#endregion
#region Public Methods
/// <summary>
/// Evaluates provided command either as a debugger specific command
/// or a PowerShell command.
/// </summary>
/// <param name="command">PowerShell command</param>
/// <param name="output">Output</param>
/// <returns>DebuggerCommandResults</returns>
public abstract DebuggerCommandResults ProcessCommand(PSCommand command, PSDataCollection<PSObject> output);
/// <summary>
/// Sets the debugger resume action.
/// </summary>
/// <param name="resumeAction">DebuggerResumeAction</param>
public abstract void SetDebuggerAction(DebuggerResumeAction resumeAction);
/// <summary>
/// Stops a running command.
/// </summary>
public abstract void StopProcessCommand();
/// <summary>
/// Returns current debugger stop event arguments if debugger is in
/// debug stop state. Otherwise returns null.
/// </summary>
/// <returns>DebuggerStopEventArgs</returns>
public abstract DebuggerStopEventArgs GetDebuggerStopArgs();
/// <summary>
/// Sets the parent debugger, breakpoints and other debugging context information.
/// </summary>
/// <param name="parent">Parent debugger</param>
/// <param name="breakPoints">List of breakpoints</param>
/// <param name="startAction">Debugger mode</param>
/// <param name="host">host</param>
/// <param name="path">Current path</param>
public virtual void SetParent(
Debugger parent,
IEnumerable<Breakpoint> breakPoints,
DebuggerResumeAction? startAction,
PSHost host,
PathInfo path)
{
throw new PSNotImplementedException();
}
/// <summary>
/// Sets the debugger mode.
/// </summary>
public virtual void SetDebugMode(DebugModes mode)
{
this.DebugMode = mode;
}
/// <summary>
/// Returns IEnumerable of CallStackFrame objects.
/// </summary>
/// <returns></returns>
public virtual IEnumerable<CallStackFrame> GetCallStack()
{
return new Collection<CallStackFrame>();
}
/// <summary>
/// Adds the provided set of breakpoints to the debugger.
/// </summary>
/// <param name="breakpoints">Breakpoints.</param>
public virtual void SetBreakpoints(IEnumerable<Breakpoint> breakpoints)
{
throw new PSNotImplementedException();
}
/// <summary>
/// Resets the command processor source information so that it is
/// updated with latest information on the next debug stop.
/// </summary>
public virtual void ResetCommandProcessorSource()
{
throw new PSNotImplementedException();
}
/// <summary>
/// Sets debugger stepping mode.
/// </summary>
/// <param name="enabled">True if stepping is to be enabled</param>
public virtual void SetDebuggerStepMode(bool enabled)
{
throw new PSNotImplementedException();
}
#endregion
#region Internal Methods
/// <summary>
/// Passes the debugger command to the internal script debugger command processor. This
/// is used internally to handle debugger commands such as list, help, etc.
/// </summary>
/// <param name="command">Command string</param>
/// <param name="output">Output collection</param>
/// <returns>DebuggerCommand containing information on whether and how the command was processed.</returns>
internal virtual DebuggerCommand InternalProcessCommand(string command, IList<PSObject> output)
{
throw new PSNotImplementedException();
}
/// <summary>
/// Creates a source list based on root script debugger source information if available, with
/// the current source line highlighted. This is used internally for nested runspace debugging
/// where the runspace command is run in context of a parent script.
/// </summary>
/// <param name="lineNum">Current source line</param>
/// <param name="output">Output collection</param>
/// <returns>True if source listed successfully</returns>
internal virtual bool InternalProcessListCommand(int lineNum, IList<PSObject> output)
{
throw new PSNotImplementedException();
}
/// <summary>
/// Sets up debugger to debug provided job or its child jobs.
/// </summary>
/// <param name="job">
/// Job object that is either a debuggable job or a container
/// of debuggable child jobs.
/// </param>
internal virtual void DebugJob(Job job)
{
throw new PSNotImplementedException();
}
/// <summary>
/// Removes job from debugger job list and pops the its
/// debugger from the active debugger stack.
/// </summary>
/// <param name="job">Job</param>
internal virtual void StopDebugJob(Job job)
{
throw new PSNotImplementedException();
}
/// <summary>
/// GetActiveDebuggerCallStack.
/// </summary>
/// <returns>Array of stack frame objects of active debugger</returns>
internal virtual CallStackFrame[] GetActiveDebuggerCallStack()
{
throw new PSNotImplementedException();
}
/// <summary>
/// Method to add the provided runspace information to the debugger
/// for monitoring of debugger events. This is used to implement nested
/// debugging of runspaces.
/// </summary>
/// <param name="args">PSEntityCreatedRunspaceEventArgs</param>
internal virtual void StartMonitoringRunspace(PSMonitorRunspaceInfo args)
{
throw new PSNotImplementedException();
}
/// <summary>
/// Method to end the monitoring of a runspace for debugging events.
/// </summary>
/// <param name="args">PSEntityCreatedRunspaceEventArgs</param>
internal virtual void EndMonitoringRunspace(PSMonitorRunspaceInfo args)
{
throw new PSNotImplementedException();
}
/// <summary>
/// If a debug stop event is currently pending then this method will release
/// the event to continue processing.
/// </summary>
internal virtual void ReleaseSavedDebugStop()
{
throw new PSNotImplementedException();
}
/// <summary>
/// Sets up debugger to debug provided Runspace in a nested debug session.
/// </summary>
/// <param name="runspace">Runspace to debug</param>
internal virtual void DebugRunspace(Runspace runspace)
{
throw new PSNotImplementedException();
}
/// <summary>
/// Removes the provided Runspace from the nested "active" debugger state.
/// </summary>
/// <param name="runspace">Runspace</param>
internal virtual void StopDebugRunspace(Runspace runspace)
{
throw new PSNotImplementedException();
}
/// <summary>
/// Raises the NestedDebuggingCancelledEvent event.
/// </summary>
internal void RaiseNestedDebuggingCancelEvent()
{
// Raise event on worker thread.
Threading.ThreadPool.QueueUserWorkItem(
(state) =>
{
try
{
NestedDebuggingCancelledEvent.SafeInvoke<EventArgs>(this, null);
}
catch (Exception)
{
}
});
}
#endregion
#region Runspace Debug Processing Methods
/// <summary>
/// Adds the provided Runspace object to the runspace debugger processing queue.
/// The queue will then raise the StartRunspaceDebugProcessing events for each runspace to allow
/// a host script debugger implementation to provide an active debugging session.
/// </summary>
/// <param name="runspace">Runspace to debug</param>
internal virtual void QueueRunspaceForDebug(Runspace runspace)
{
throw new PSNotImplementedException();
}
/// <summary>
/// Causes the CancelRunspaceDebugProcessing event to be raised which notifies subscribers that current debugging
/// sessions should be cancelled.
/// </summary>
public virtual void CancelDebuggerProcessing()
{
throw new PSNotImplementedException();
}
#endregion
#region Members
internal const string CannotProcessCommandNotStopped = "Debugger:CannotProcessCommandNotStopped";
internal const string CannotEnableDebuggerSteppingInvalidMode = "Debugger:CannotEnableDebuggerSteppingInvalidMode";
private static readonly Guid s_instanceId = new Guid();
#endregion
}
#endregion
#region ScriptDebugger class
/// <summary>
/// Holds the debugging information for a Monad Shell session
/// </summary>
internal sealed class ScriptDebugger : Debugger, IDisposable
{
#region constructors
internal ScriptDebugger(ExecutionContext context)
{
_context = context;
_inBreakpoint = false;
_idToBreakpoint = new Dictionary<int, Breakpoint>();
_pendingBreakpoints = new List<LineBreakpoint>();
_boundBreakpoints = new Dictionary<string, Tuple<WeakReference, List<LineBreakpoint>>>(StringComparer.OrdinalIgnoreCase);
_commandBreakpoints = new List<CommandBreakpoint>();
_variableBreakpoints = new Dictionary<string, List<VariableBreakpoint>>(StringComparer.OrdinalIgnoreCase);
_steppingMode = SteppingMode.None;
_callStack = new CallStackList { _callStackList = new List<CallStackInfo>() };
_runningJobs = new Dictionary<Guid, PSJobStartEventArgs>();
_activeDebuggers = new ConcurrentStack<Debugger>();
_debuggerStopEventArgs = new ConcurrentStack<DebuggerStopEventArgs>();
_syncObject = new object();
_syncActiveDebuggerStopObject = new object();
_runningRunspaces = new Dictionary<Guid, PSMonitorRunspaceInfo>();
}
/// <summary>
/// Static constructor
/// </summary>
static ScriptDebugger()
{
s_processDebugPromptMatch = StringUtil.Format(@"""[{0}:", DebuggerStrings.NestedRunspaceDebuggerPromptProcessName);
}
#endregion constructors
#region properties
/// <summary>
/// True when debugger is stopped at a breakpoint.
/// </summary>
public override bool InBreakpoint
{
get
{
if (_inBreakpoint)
{
return _inBreakpoint;
}
Debugger activeDebugger;
if (_activeDebuggers.TryPeek(out activeDebugger))
{
return activeDebugger.InBreakpoint;
}
return false;
}
}
internal override bool IsPushed
{
get { return (_activeDebuggers.Count > 0); }
}
/// <summary>
/// Returns true if the debugger is preserving a DebuggerStopEvent
/// event. Use ReleaseSavedDebugStop() to allow event to process.
/// </summary>
internal override bool IsPendingDebugStopEvent
{
get
{
return ((_preserveDebugStopEvent != null) && !_preserveDebugStopEvent.IsSet);
}
}
/// <summary>
/// Returns true if debugger has been set to stepInto mode.
/// </summary>
internal override bool IsDebuggerSteppingEnabled
{
get
{
return ((_context._debuggingMode == (int)InternalDebugMode.Enabled) &&
(_currentDebuggerAction == DebuggerResumeAction.StepInto) &&
(_steppingMode != SteppingMode.None));
}
}
private bool? _isLocalSession;
private bool IsLocalSession
{
get
{
if (_isLocalSession == null)
{
// Remote debug sessions always have a ServerRemoteHost. Otherwise it is a local session.
_isLocalSession = !(((_context.InternalHost.ExternalHost != null) &&
(_context.InternalHost.ExternalHost is System.Management.Automation.Remoting.ServerRemoteHost)));
}
return _isLocalSession.Value;
}
}
#endregion properties
#region internal methods
#region Reset Debugger
/// <summary>
/// Resets debugger to initial state.
/// </summary>
internal void ResetDebugger()
{
SetDebugMode(DebugModes.None);
SetInternalDebugMode(InternalDebugMode.Disabled);
_steppingMode = SteppingMode.None;
_inBreakpoint = false;
_idToBreakpoint.Clear();
_pendingBreakpoints.Clear();
_boundBreakpoints.Clear();
_commandBreakpoints.Clear();
_variableBreakpoints.Clear();
s_emptyBreakpointList.Clear();
_callStack.Clear();
_overOrOutFrame = null;
_commandProcessor = new DebuggerCommandProcessor();
_currentInvocationInfo = null;
_inBreakpoint = false;
_psDebuggerCommand = null;
_savedIgnoreScriptDebug = false;
_isLocalSession = null;
_nestedDebuggerStop = false;
_writeWFErrorOnce = false;
_debuggerStopEventArgs.Clear();
_lastActiveDebuggerAction = DebuggerResumeAction.Continue;
_currentDebuggerAction = DebuggerResumeAction.Continue;
_previousDebuggerAction = DebuggerResumeAction.Continue;
_nestedRunningFrame = null;
_nestedDebuggerStop = false;
_processingOutputCount = 0;
_preserveUnhandledDebugStopEvent = false;
ClearRunningJobList();
ClearRunningRunspaceList();
_activeDebuggers.Clear();
ReleaseSavedDebugStop();
SetDebugMode(DebugModes.Default);
}
#endregion
#region Call stack management
// Called from generated code on entering the script function, called once for each dynamicparam, begin, or end
// block, and once for each object written to the pipeline. Also called when entering a trap.
internal void EnterScriptFunction(FunctionContext functionContext)
{
Diagnostics.Assert(functionContext._executionContext == _context, "Wrong debugger is being used.");
var invocationInfo = (InvocationInfo)functionContext._localsTuple.GetAutomaticVariable(AutomaticVariable.MyInvocation);
var newCallStackInfo = new CallStackInfo
{
InvocationInfo = invocationInfo,
File = functionContext._file,
DebuggerStepThrough = functionContext._debuggerStepThrough,
FunctionContext = functionContext,
IsFrameHidden = functionContext._debuggerHidden,
};
_callStack.Add(newCallStackInfo);
if (_context._debuggingMode > 0)
{
var scriptCommandInfo = invocationInfo.MyCommand as ExternalScriptInfo;
if (scriptCommandInfo != null)
{
RegisterScriptFile(scriptCommandInfo);
}
bool checkLineBp = CheckCommand(invocationInfo);
SetupBreakpoints(functionContext);