-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathDebuggerSession.cs
More file actions
1724 lines (1534 loc) · 48.3 KB
/
Copy pathDebuggerSession.cs
File metadata and controls
1724 lines (1534 loc) · 48.3 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
// DebuggerSession.cs
//
// Author:
// Ankit Jain <jankit@novell.com>
// Lluis Sanchez Gual <lluis@novell.com>
//
// Copyright (c) 2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System;
using System.Threading;
using System.Collections.Generic;
using Mono.Debugging.Backend;
using Mono.Debugging.Evaluation;
namespace Mono.Debugging.Client
{
public delegate void TargetEventHandler (object sender, TargetEventArgs args);
public delegate void ProcessEventHandler (int processId);
public delegate void ThreadEventHandler (int threadId);
public delegate bool ExceptionHandler (Exception ex);
public delegate string TypeResolverHandler (string identifier, SourceLocation location);
public delegate void BreakpointTraceHandler (BreakEvent be, string trace);
public delegate IExpressionEvaluator GetExpressionEvaluatorHandler (string extension);
public delegate IConnectionDialog ConnectionDialogCreatorExtended (DebuggerStartInfo dsi);
public delegate IConnectionDialog ConnectionDialogCreator ();
public abstract class DebuggerSession: IDisposable
{
readonly Dictionary<BreakEvent, BreakEventInfo> breakpoints = new Dictionary<BreakEvent, BreakEventInfo> ();
readonly Dictionary<string, string> resolvedExpressionCache = new Dictionary<string, string> ();
readonly InternalDebuggerSession frontend;
readonly object slock = new object ();
readonly EvaluationStatistics evaluationStats = new EvaluationStatistics ();
BreakpointStore breakpointStore;
DebuggerSessionOptions options;
ProcessInfo[] currentProcesses;
ThreadInfo activeThread;
bool ownedBreakpointStore;
bool adjustingBreakpoints;
bool disposed;
bool attached;
/// <summary>
/// Reports a debugger event
/// </summary>
public event EventHandler<TargetEventArgs> TargetEvent;
/// <summary>
/// Raised when the debugger resumes execution after being stopped
/// </summary>
public event EventHandler TargetStarted;
/// <summary>
/// Raised when the underlying debugging engine has been initialized and it is ready to start execution.
/// </summary>
public event EventHandler<TargetEventArgs> TargetReady;
/// <summary>
/// Raised when the debugging session is paused
/// </summary>
public event EventHandler<TargetEventArgs> TargetStopped;
/// <summary>
/// Raised when the execution is interrupted by an external event
/// </summary>
public event EventHandler<TargetEventArgs> TargetInterrupted;
/// <summary>
/// Raised when a breakpoint is hit
/// </summary>
public event EventHandler<TargetEventArgs> TargetHitBreakpoint;
/// <summary>
/// Raised when the execution is interrupted due to receiving a signal
/// </summary>
public event EventHandler<TargetEventArgs> TargetSignaled;
/// <summary>
/// Raised when the debugged process exits
/// </summary>
public event EventHandler<TargetEventArgs> TargetExited;
/// <summary>
/// Raised when an exception for which there is a catchpoint is thrown
/// </summary>
public event EventHandler<TargetEventArgs> TargetExceptionThrown;
/// <summary>
/// Raised when an exception is unhandled
/// </summary>
public event EventHandler<TargetEventArgs> TargetUnhandledException;
/// <summary>
/// Raised when a thread is started in the debugged process
/// </summary>
public event EventHandler<TargetEventArgs> TargetThreadStarted;
/// <summary>
/// Raised when a thread is stopped in the debugged process
/// </summary>
public event EventHandler<TargetEventArgs> TargetThreadStopped;
/// <summary>
/// Raised when the 'busy state' of the debugger changes.
/// The debugger may switch to busy state if it is in the middle
/// of an expression evaluation which can't be aborted.
/// </summary>
public event EventHandler<BusyStateEventArgs> BusyStateChanged;
/// <summary>
/// Raised when an assembly is loaded
/// </summary>
public event EventHandler<AssemblyEventArgs> AssemblyLoaded;
protected DebuggerSession ()
{
UseOperationThread = true;
frontend = new InternalDebuggerSession (this);
}
/// <summary>
/// Releases all resource used by the <see cref="Mono.Debugging.Client.DebuggerSession"/> object.
/// </summary>
/// <remarks>
/// Call <see cref="Dispose"/> when you are finished using the <see cref="Mono.Debugging.Client.DebuggerSession"/>.
/// The <see cref="Dispose"/> method leaves the <see cref="Mono.Debugging.Client.DebuggerSession"/> in an unusable
/// state. After calling <see cref="Dispose"/>, you must release all references to the
/// <see cref="Mono.Debugging.Client.DebuggerSession"/> so the garbage collector can reclaim the memory that the
/// <see cref="Mono.Debugging.Client.DebuggerSession"/> was occupying.
/// </remarks>
public virtual void Dispose ()
{
Dispatch (delegate {
if (!disposed) {
disposed = true;
if (!ownedBreakpointStore)
Breakpoints = null;
}
});
}
/// <summary>
/// Gets or sets an exception handler to be invoked when an exception is raised by the debugger engine.
/// </summary>
/// <remarks>
/// Notice that this handler will be used to report exceptions in the debugger, not exceptions raised
/// in the debugged process.
/// </remarks>
public ExceptionHandler ExceptionHandler {
get; set;
}
/// <summary>
/// Gets or sets the connection dialog creator callback.
/// </summary>
public ConnectionDialogCreator ConnectionDialogCreator { get; set; }
/// <summary>
/// Gets or sets the connection dialog creator callback.
/// </summary>
public ConnectionDialogCreatorExtended ConnectionDialogCreatorExtended { get; set; }
/// <summary>
/// Gets or sets the breakpoint trace handler.
/// </summary>
/// <remarks>
/// This handler is invoked when the value of a tracepoint has to be printed
/// </remarks>
public BreakpointTraceHandler BreakpointTraceHandler { get; set; }
/// <summary>
/// Gets or sets the type resolver handler.
/// </summary>
/// <remarks>
/// This handler is invoked when the expression evaluator needs to resolve a type name.
/// </remarks>
public TypeResolverHandler TypeResolverHandler { get; set; }
/// <summary>
/// Gets or sets the an expression evaluator provider
/// </summary>
/// <remarks>
/// This handler is invoked when the debugger needs to get an evaluator for a specific type of file
/// </remarks>
public GetExpressionEvaluatorHandler GetExpressionEvaluator { get; set; }
/// <summary>
/// Gets or sets the custom break event hit handler.
/// </summary>
/// <remarks>
/// This handler is invoked when a custom breakpoint is hit to determine if the debug session should
/// continue or stop.
/// </remarks>
public BreakEventHitHandler CustomBreakEventHitHandler {
get; set;
}
public EvaluationStatistics EvaluationStats {
get { return evaluationStats; }
}
/// <summary>
/// Gets or sets the breakpoint store for the debugger session.
/// </summary>
public BreakpointStore Breakpoints {
get {
lock (slock) {
if (breakpointStore == null) {
Breakpoints = new BreakpointStore ();
ownedBreakpointStore = true;
}
return breakpointStore;
}
}
set {
lock (slock) {
if (breakpointStore != null) {
lock (breakpointStore) {
foreach (BreakEvent bp in breakpointStore) {
RemoveBreakEvent (bp);
NotifyBreakEventStatusChanged (bp);
}
}
breakpointStore.BreakEventAdded -= OnBreakpointAdded;
breakpointStore.BreakEventRemoved -= OnBreakpointRemoved;
breakpointStore.BreakEventModified -= OnBreakpointModified;
breakpointStore.BreakEventEnableStatusChanged -= OnBreakpointStatusChanged;
breakpointStore.CheckingReadOnly -= BreakpointStoreCheckingReadOnly;
breakpointStore.ResetBreakpoints ();
}
breakpointStore = value;
ownedBreakpointStore = false;
if (breakpointStore != null) {
if (IsConnected) {
Dispatch (delegate {
if (IsConnected) {
lock (breakpointStore) {
foreach (BreakEvent bp in breakpointStore)
AddBreakEvent (bp);
}
}
});
}
breakpointStore.BreakEventAdded += OnBreakpointAdded;
breakpointStore.BreakEventRemoved += OnBreakpointRemoved;
breakpointStore.BreakEventModified += OnBreakpointModified;
breakpointStore.BreakEventEnableStatusChanged += OnBreakpointStatusChanged;
breakpointStore.CheckingReadOnly += BreakpointStoreCheckingReadOnly;
}
}
}
}
readonly Queue<Action> actionsQueue = new Queue<Action>();
bool threadExecuting;
void Dispatch (Action action)
{
if (UseOperationThread) {
lock (actionsQueue) {
actionsQueue.Enqueue (action);
if (!threadExecuting) {
threadExecuting = true;
ThreadPool.QueueUserWorkItem (delegate {
while (true) {
Action actionToExecute = null;
lock (actionsQueue) {
if (actionsQueue.Count > 0) {
actionToExecute = actionsQueue.Dequeue ();
} else {
threadExecuting = false;
return;
}
}
lock (slock) {
try {
actionToExecute ();
} catch (Exception ex) {
HandleException (ex);
}
}
}
});
}
}
} else {
lock (slock) {
action ();
}
}
}
/// <summary>
/// Starts a debugging session
/// </summary>
/// <param name='startInfo'>
/// Startup information
/// </param>
/// <param name='options'>
/// Session options
/// </param>
/// <exception cref='ArgumentNullException'>
/// Is thrown when an argument passed to a method is invalid because it is <see langword="null" /> .
/// </exception>
public void Run (DebuggerStartInfo startInfo, DebuggerSessionOptions options)
{
if (startInfo == null)
throw new ArgumentNullException (nameof (startInfo));
if (options == null)
throw new ArgumentNullException (nameof (options));
lock (slock) {
this.options = options;
OnRunning ();
Dispatch (delegate {
try {
OnRun (startInfo);
} catch (Exception ex) {
// should handle exception before raising Exit event because HandleException may ignore exceptions in Exited state
var exceptionHandled = HandleException (ex);
ForceExit ();
if (!exceptionHandled)
throw;
}
});
}
}
/// <summary>
/// Starts a debugging session by attaching the debugger to a running process
/// </summary>
/// <param name='proc'>
/// Process information
/// </param>
/// <param name='options'>
/// Debugging options
/// </param>
/// <exception cref='ArgumentNullException'>
/// Is thrown when an argument passed to a method is invalid because it is <see langword="null" /> .
/// </exception>
public void AttachToProcess (ProcessInfo proc, DebuggerSessionOptions options)
{
if (proc == null)
throw new ArgumentNullException (nameof (proc));
if (options == null)
throw new ArgumentNullException (nameof (options));
lock (slock) {
this.options = options;
OnRunning ();
Dispatch (delegate {
try {
OnAttachToProcess (proc);
attached = true;
} catch (Exception ex) {
// should handle exception before raising Exit event because HandleException may ignore exceptions in Exited state
var exceptionHandled = HandleException (ex);
ForceExit ();
if (!exceptionHandled)
throw;
}
});
}
}
/// <summary>
/// Detaches this debugging session from the debugged process
/// </summary>
public void Detach ()
{
lock (slock) {
Dispatch (delegate {
try {
OnDetach ();
}
catch (Exception ex) {
if (!HandleException (ex))
throw;
}
finally {
IsConnected = false;
}
});
}
}
/// <summary>
/// Gets a value indicating whether this <see cref="Mono.Debugging.Client.DebuggerSession"/> has been attached to a process using the Attach method.
/// </summary>
/// <value>
/// <c>true</c> if attached to process; otherwise, <c>false</c>.
/// </value>
public bool AttachedToProcess {
get { return attached; }
}
/// <summary>
/// Gets or sets the active thread.
/// </summary>
/// <remarks>
/// This property can only be used when the debugger is paused
/// </remarks>
public ThreadInfo ActiveThread {
get {
lock (slock) {
return activeThread;
}
}
set {
lock (slock) {
try {
activeThread = value;
OnSetActiveThread (activeThread.ProcessId, activeThread.Id);
} catch (Exception ex) {
if (!HandleException (ex))
throw;
}
}
}
}
/// <summary>
/// Executes one line of code
/// </summary>
public void NextLine ()
{
lock (slock) {
OnRunning ();
Dispatch (delegate {
try {
OnNextLine ();
} catch (Exception ex) {
ForceStop ();
if (!HandleException (ex))
throw;
}
});
}
}
/// <summary>
/// Executes one line of code, stepping into method invocations
/// </summary>
public void StepLine ()
{
lock (slock) {
OnRunning ();
Dispatch (delegate {
try {
OnStepLine ();
} catch (Exception ex) {
ForceStop ();
if (!HandleException (ex))
throw;
}
});
}
}
/// <summary>
/// Executes one low level instruction
/// </summary>
public void NextInstruction ()
{
lock (slock) {
OnRunning ();
Dispatch (delegate {
try {
OnNextInstruction ();
} catch (Exception ex) {
ForceStop ();
if (!HandleException (ex))
throw;
}
});
}
}
/// <summary>
/// Executes one low level instruction, stepping into method invocations
/// </summary>
public void StepInstruction ()
{
lock (slock) {
OnRunning ();
Dispatch (delegate {
try {
OnStepInstruction ();
} catch (Exception ex) {
ForceStop ();
if (!HandleException (ex))
throw;
}
});
}
}
/// <summary>
/// Resumes the execution of the debugger and stops when the current method is exited
/// </summary>
public void Finish ()
{
lock (slock) {
OnRunning ();
Dispatch (delegate {
try {
OnFinish ();
} catch (Exception ex) {
// should handle exception before raising Exit event because HandleException may ignore exceptions in Exited state
var exceptionHandled = HandleException (ex);
ForceExit ();
if (!exceptionHandled)
throw;
}
});
}
}
/// <summary>
/// Sets the next statement on the active thread.
/// </summary>
/// <param name="fileName">File name.</param>
/// <param name="line">Line.</param>
/// <param name="column">Column.</param>
public void SetNextStatement (string fileName, int line, int column)
{
if (fileName == null)
throw new ArgumentNullException (nameof (fileName));
if (fileName.Length == 0)
throw new ArgumentException ("Path cannot be empty.", nameof (fileName));
if (line < 1)
throw new ArgumentOutOfRangeException (nameof (line));
if (column < 1)
throw new ArgumentOutOfRangeException (nameof (column));
if (!IsConnected || IsRunning || !CanSetNextStatement)
throw new NotSupportedException ();
OnSetNextStatement (ActiveThread.Id, fileName, line, column);
}
/// <summary>
/// Sets the next statement on the active thread.
/// </summary>
/// <param name="ilOffset">The IL offset.</param>
public void SetNextStatement (int ilOffset)
{
if (ilOffset < 0)
throw new ArgumentOutOfRangeException (nameof (ilOffset));
if (!IsConnected || IsRunning || !CanSetNextStatement)
throw new NotSupportedException ();
OnSetNextStatement (ActiveThread.Id, ilOffset);
}
/// <summary>
/// Returns the status of a breakpoint for this debugger session.
/// </summary>
public BreakEventStatus GetBreakEventStatus (BreakEvent be)
{
if (IsConnected) {
lock (breakpoints) {
if (breakpoints.TryGetValue (be, out var binfo))
return binfo.Status;
}
}
return BreakEventStatus.NotBound;
}
/// <summary>
/// Returns a status message of a breakpoint for this debugger session.
/// </summary>
public string GetBreakEventStatusMessage (BreakEvent be)
{
if (IsConnected) {
lock (breakpoints) {
if (breakpoints.TryGetValue (be, out var binfo)) {
if (binfo.StatusMessage != null)
return binfo.StatusMessage;
switch (binfo.Status) {
case BreakEventStatus.BindError: return "The breakpoint could not be bound";
case BreakEventStatus.Bound: return "";
case BreakEventStatus.Disconnected: return "";
case BreakEventStatus.Invalid: return "The breakpoint location is invalid. Perhaps the source line does " +
"not contain any statements, or the source does not correspond to the current binary";
case BreakEventStatus.NotBound: return "The breakpoint could not yet be bound to a valid location";
}
}
}
}
return "The breakpoint will not currently be hit";
}
void AddBreakEvent (BreakEvent be)
{
try {
var eventInfo = OnInsertBreakEvent (be);
if (eventInfo == null)
throw new InvalidOperationException ("OnInsertBreakEvent can't return a null value. If the breakpoint can't be bound or is invalid, a BreakEventInfo with the corresponding status must be returned");
lock (breakpoints) {
breakpoints [be] = eventInfo;
}
eventInfo.AttachSession (this, be);
} catch (Exception ex) {
string msg;
if (be is FunctionBreakpoint)
msg = "Could not set breakpoint at location '" + ((FunctionBreakpoint) be).FunctionName + ":" + ((FunctionBreakpoint) be).Line + "'";
else if (be is Breakpoint)
msg = "Could not set breakpoint at location '" + ((Breakpoint) be).FileName + ":" + ((Breakpoint) be).Line + "'";
else
msg = "Could not set catchpoint for exception '" + ((Catchpoint) be).ExceptionName + "'";
msg += " (" + ex.Message + ")";
OnDebuggerOutput (false, msg + "\n");
HandleException (ex);
}
}
bool RemoveBreakEvent (BreakEvent be)
{
lock (breakpoints) {
if (breakpoints.TryGetValue (be, out var binfo)) {
try {
OnRemoveBreakEvent (binfo);
} catch (Exception ex) {
if (IsConnected)
OnDebuggerOutput (false, ex.Message);
HandleException (ex);
return false;
}
breakpoints.Remove (be);
}
return true;
}
}
void UpdateBreakEventStatus (BreakEvent be)
{
lock (breakpoints) {
if (breakpoints.TryGetValue (be, out var binfo)) {
try {
OnEnableBreakEvent (binfo, be.Enabled);
} catch (Exception ex) {
if (IsConnected)
OnDebuggerOutput (false, ex.Message);
HandleException (ex);
}
}
}
}
void UpdateBreakEvent (BreakEvent be)
{
lock (breakpoints) {
if (breakpoints.TryGetValue (be, out var binfo))
OnUpdateBreakEvent (binfo);
}
}
void OnBreakpointAdded (object s, BreakEventArgs args)
{
if (adjustingBreakpoints)
return;
if (IsConnected) {
Dispatch (delegate {
if (IsConnected)
AddBreakEvent (args.BreakEvent);
});
}
}
void OnBreakpointRemoved (object s, BreakEventArgs args)
{
if (adjustingBreakpoints)
return;
if (IsConnected) {
Dispatch (delegate {
if (IsConnected)
RemoveBreakEvent (args.BreakEvent);
});
}
}
void OnBreakpointModified (object s, BreakEventArgs args)
{
if (IsConnected) {
Dispatch (delegate {
if (IsConnected)
UpdateBreakEvent (args.BreakEvent);
});
}
}
void OnBreakpointStatusChanged (object s, BreakEventArgs args)
{
if (IsConnected) {
Dispatch (delegate {
if (IsConnected)
UpdateBreakEventStatus (args.BreakEvent);
});
}
}
void BreakpointStoreCheckingReadOnly (object sender, ReadOnlyCheckEventArgs e)
{
e.SetReadOnly (!AllowBreakEventChanges);
}
/// <summary>
/// Gets the debugger options object
/// </summary>
public DebuggerSessionOptions Options {
get { return options; }
}
/// <summary>
/// Gets or sets the evaluation options.
/// </summary>
public EvaluationOptions EvaluationOptions {
get { return options.EvaluationOptions; }
set { options.EvaluationOptions = value; }
}
/// <summary>
/// Resumes the execution of the debugger
/// </summary>
public void Continue ()
{
lock (slock) {
OnRunning ();
Dispatch (delegate {
try {
OnContinue ();
} catch (Exception ex) {
ForceStop ();
if (!HandleException (ex))
throw;
}
});
}
}
/// <summary>
/// Pauses the execution of the debugger
/// </summary>
public void Stop ()
{
Dispatch (delegate {
try {
OnStop ();
} catch (Exception ex) {
if (!HandleException (ex))
throw;
}
});
}
/// <summary>
/// Stops the execution of the debugger by killing the debugged process
/// </summary>
public void Exit ()
{
Dispatch (delegate {
try {
OnExit ();
} catch (Exception ex) {
if (!HandleException (ex))
throw;
}
});
}
/// <summary>
/// Gets a value indicating whether the debuggee is currently connected
/// </summary>
public bool IsConnected {
get; private set;
}
/// <summary>
/// Gets a value indicating whether the debuggee is currently running (not paused by the debugger)
/// </summary>
public bool IsRunning {
get; private set;
}
/// <summary>
/// Gets a value indicating whether the debuggee has exited.
/// </summary>
public bool HasExited {
get; protected set;
}
/// <summary>
/// Gets a list of all processes
/// </summary>
/// <remarks>
/// This method can only be used when the debuggee is stopped by the debugger
/// </remarks>
public ProcessInfo[] GetProcesses ()
{
lock (slock) {
if (currentProcesses == null) {
currentProcesses = OnGetProcesses ();
foreach (var process in currentProcesses)
process.Attach (this);
}
return currentProcesses;
}
}
/// <summary>
/// Gets or sets the output writer callback.
/// </summary>
/// <remarks>
/// This callback is invoked to print debuggee output
/// </remarks>
public OutputWriterDelegate OutputWriter {
get; set;
}
/// <summary>
/// Gets or sets the log writer.
/// </summary>
/// <remarks>
/// This callback is invoked to print debugger log messages
/// </remarks>
public OutputWriterDelegate LogWriter {
get; set;
}
/// <summary>
/// Gets or sets the debug writer.
/// </summary>
/// <remarks>
/// This callback is invoked to print debugge messages
/// called via System.Diagnostics.Debugger.Log
/// </remarks>
public DebugWriterDelegate DebugWriter {
get; set;
}
/// <summary>
/// Gets the disassembly of a source code file
/// </summary>
/// <returns>
/// An array of AssemblyLine, with one element for each source code line that could be disassembled
/// </returns>
/// <param name='file'>
/// The file.
/// </param>
/// <remarks>
/// This method can only be used when the debuggee is stopped by the debugger
/// </remarks>
public AssemblyLine[] DisassembleFile (string file)
{
lock (slock) {
return OnDisassembleFile (file);
}
}
public string ResolveExpression (EvaluationContext ctx, string expression, string file, int line, int column, int endLine, int endColumn)
{
return ResolveExpression (ctx, expression, new SourceLocation (null, file, line, column, endLine, endColumn, null, null));
}
public virtual string ResolveExpression (EvaluationContext ctx, string expression, SourceLocation location)
{
var key = expression + " " + location;
if (!resolvedExpressionCache.TryGetValue (key, out var resolved)) {
try {
resolved = OnResolveExpression (ctx, expression, location);
} catch (Exception ex) {
OnDebuggerOutput (true, "Error while resolving expression: " + ex.Message);
}
resolvedExpressionCache [key] = resolved;
}
return resolved ?? expression;
}
/// <summary>
/// Stops the execution of background evaluations being done by the debugger
/// </summary>
/// <remarks>
/// This method can only be used when the debuggee is stopped by the debugger
/// </remarks>
public void CancelAsyncEvaluations ()
{
if (UseOperationThread) {
ThreadPool.QueueUserWorkItem (delegate {
OnCancelAsyncEvaluations ();
});
} else
OnCancelAsyncEvaluations ();
}
/// <summary>
/// Gets a value indicating whether there are background evaluations being done by the debugger
/// which can be cancelled.
/// </summary>
/// <remarks>
/// This method can only be used when the debuggee is stopped by the debugger
/// </remarks>
public virtual bool CanCancelAsyncEvaluations {
get { return false; }
}
/// <summary>
/// Override to stop the execution of background evaluations being done by the debugger
/// </summary>
protected virtual void OnCancelAsyncEvaluations ()
{
}
readonly Dictionary<string, IExpressionEvaluator> evaluators = new Dictionary<string, IExpressionEvaluator> ();
readonly ExpressionEvaluator defaultResolver = new NRefactoryExpressionEvaluator ();
internal IExpressionEvaluator FindExpressionEvaluator (StackFrame frame)
{
if (GetExpressionEvaluator == null)
return null;
var fileName = frame.SourceLocation?.FileName;
if (string.IsNullOrEmpty (fileName))
return null;
var extension = System.IO.Path.GetExtension (fileName);
if (evaluators.TryGetValue (extension, out var result))
return result;
result = GetExpressionEvaluator (extension);
evaluators[extension] = result;
return result;
}
public ExpressionEvaluator GetEvaluator (StackFrame frame)
{
var result = FindExpressionEvaluator (frame);
if (result == null)
return defaultResolver;
return result.Evaluator;
}
protected void RaiseStopEvent ()
{
TargetEvent?.Invoke (this, new TargetEventArgs (TargetEventType.TargetStopped));
}
/// <summary>
/// Called when an expression needs to be resolved
/// </summary>
/// <param name='expression'>
/// The expression
/// </param>
/// <param name='location'>
/// The source code location
/// </param>
/// <returns>
/// The resolved expression
/// </returns>
protected virtual string OnResolveExpression (EvaluationContext ctx, string expression, SourceLocation location)
{
var resolver = defaultResolver;
if (GetExpressionEvaluator != null)
resolver = GetExpressionEvaluator(System.IO.Path.GetExtension(location.FileName))?.Evaluator ?? defaultResolver;