This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
/
Debugger.cs
479 lines (389 loc) · 11.1 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
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Configuration;
using System.Globalization;
using System.Reflection;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using Mono.Debugger.Backend;
using Mono.Debugger.Languages;
using Mono.Debugger.Languages.Mono;
namespace Mono.Debugger
{
public delegate void DebuggerEventHandler (Debugger debugger);
public delegate void ThreadEventHandler (Debugger debugger, Thread thread);
public delegate void ProcessEventHandler (Debugger debugger, Process process);
public sealed class Debugger : DebuggerMarshalByRefObject
{
ManualResetEvent kill_event;
DebuggerConfiguration config;
ThreadManager thread_manager;
Hashtable process_hash;
Process main_process;
MyOperationHost operation_host;
bool alive;
public Debugger (DebuggerConfiguration config)
{
this.config = config;
this.alive = true;
ObjectCache.Initialize ();
kill_event = new ManualResetEvent (false);
thread_manager = new ThreadManager (this);
process_hash = Hashtable.Synchronized (new Hashtable ());
stopped_event = new ManualResetEvent (false);
operation_host = new MyOperationHost (this);
}
public DebuggerConfiguration Configuration {
get { return config; }
}
internal IOperationHost OperationHost {
get { return operation_host; }
}
internal ThreadManager ThreadManager {
get { return thread_manager; }
}
public event ThreadEventHandler ThreadCreatedEvent;
public event ThreadEventHandler ThreadExitedEvent;
public event ThreadEventHandler ManagedThreadCreatedEvent;
public event ProcessEventHandler MainProcessCreatedEvent;
public event ProcessEventHandler ProcessReachedMainEvent;
public event ProcessEventHandler ProcessCreatedEvent;
public event ProcessEventHandler ProcessExitedEvent;
public event ProcessEventHandler ProcessExecdEvent;
public event ModuleEventHandler ModuleLoadedEvent;
public event ModuleEventHandler ModuleUnLoadedEvent;
public event DebuggerEventHandler TargetExitedEvent;
public event TargetEventHandler TargetEvent;
public event SymbolTableChangedHandler SymbolTableChanged;
public event ThreadEventHandler EnterNestedBreakStateEvent;
public event ThreadEventHandler LeaveNestedBreakStateEvent;
internal Thread CreateThread (ThreadServant servant, int id)
{
return new Thread (servant, id);
}
internal void OnMainProcessCreatedEvent (Process process)
{
process_hash.Add (process, process);
if (MainProcessCreatedEvent != null)
MainProcessCreatedEvent (this, process);
}
internal void OnProcessCreatedEvent (Process process)
{
process_hash.Add (process, process);
if (ProcessCreatedEvent != null)
ProcessCreatedEvent (this, process);
}
internal void OnProcessExitedEvent (Process process)
{
process_hash.Remove (process);
if (ProcessExitedEvent != null)
ProcessExitedEvent (this, process);
if (process_hash.Count == 0)
OnTargetExitedEvent ();
}
internal void OnProcessReachedMainEvent (Process process)
{
if (ProcessReachedMainEvent != null)
ProcessReachedMainEvent (this, process);
}
internal void OnTargetExitedEvent ()
{
ThreadPool.QueueUserWorkItem (delegate {
Dispose ();
if (TargetExitedEvent != null)
TargetExitedEvent (this);
kill_event.Set ();
});
}
internal void OnProcessExecdEvent (Process process)
{
if (ProcessExecdEvent != null)
ProcessExecdEvent (this, process);
}
internal void OnThreadCreatedEvent (Thread new_process)
{
if (ThreadCreatedEvent != null)
ThreadCreatedEvent (this, new_process);
}
internal void OnManagedThreadCreatedEvent (Thread new_thread)
{
if (ManagedThreadCreatedEvent != null)
ManagedThreadCreatedEvent (this, new_thread);
}
internal void OnThreadExitedEvent (Thread thread)
{
if (ThreadExitedEvent != null)
ThreadExitedEvent (this, thread);
}
internal void OnTargetEvent (Thread thread, TargetEventArgs args)
{
try {
if (TargetEvent != null)
TargetEvent (thread, args);
} catch (Exception ex) {
Error ("{0} caught exception while sending {1}:\n{2}",
thread, args, ex);
}
}
internal void OnModuleLoadedEvent (Module module)
{
if (ModuleLoadedEvent != null)
ModuleLoadedEvent (module);
}
internal void OnModuleUnLoadedEvent (Module module)
{
if (ModuleUnLoadedEvent != null)
ModuleUnLoadedEvent (module);
}
internal void OnEnterNestedBreakState (Thread thread)
{
if (EnterNestedBreakStateEvent != null)
EnterNestedBreakStateEvent (this, thread);
}
internal void OnLeaveNestedBreakState (Thread thread)
{
if (LeaveNestedBreakStateEvent != null)
LeaveNestedBreakStateEvent (this, thread);
}
public void Error (string message, params object[] args)
{
Console.WriteLine ("ERROR: " + String.Format (message, args));
}
public void Kill ()
{
if (!alive)
return;
main_process = null;
Process[] procs;
lock (process_hash.SyncRoot) {
procs = new Process [process_hash.Count];
process_hash.Values.CopyTo (procs, 0);
}
foreach (Process proc in procs) {
proc.Kill ();
}
kill_event.WaitOne ();
}
public void Detach ()
{
if (main_process == null)
throw new TargetException (TargetError.NoTarget);
else if (!main_process.CanDetach)
throw new TargetException (TargetError.CannotDetach);
Process[] procs;
lock (process_hash.SyncRoot) {
procs = new Process [process_hash.Count];
process_hash.Values.CopyTo (procs, 0);
}
foreach (Process proc in procs) {
proc.Detach ();
}
}
public Process Run (DebuggerSession session)
{
CommandResult dummy;
return Run (session, out dummy);
}
public Process Run (DebuggerSession session, out CommandResult result)
{
check_alive ();
if (main_process != null)
throw new TargetException (TargetError.AlreadyHaveTarget);
ProcessStart start = new ProcessStart (session);
main_process = thread_manager.StartApplication (start, out result);
return main_process;
}
public Process Attach (DebuggerSession session, int pid)
{
CommandResult dummy;
return Attach (session, pid, out dummy);
}
public Process Attach (DebuggerSession session, int pid, out CommandResult result)
{
check_alive ();
if (main_process != null)
throw new TargetException (TargetError.AlreadyHaveTarget);
ProcessStart start = new ProcessStart (session, pid);
main_process = thread_manager.StartApplication (start, out result);
return main_process;
}
#if DISABLED
public Process OpenCoreFile (DebuggerSession session, string core_file,
out Thread[] threads)
{
check_alive ();
if (main_process != null)
throw new TargetException (TargetError.AlreadyHaveTarget);
ProcessStart start = new ProcessStart (session, core_file);
main_process = thread_manager.OpenCoreFile (start, out threads);
process_hash.Add (main_process, main_process);
return main_process;
}
#endif
public bool HasTarget {
get { return alive; }
}
public Process[] Processes {
get {
check_alive ();
lock (process_hash.SyncRoot) {
int count = process_hash.Count;
Process[] procs = new Process [count];
Process[] servants = new Process [count];
process_hash.Values.CopyTo (servants, 0);
for (int i = 0; i < count; i++)
procs [i] = servants [i];
return procs;
}
}
}
public bool CanDetach {
get {
return alive && (main_process != null) && main_process.CanDetach;
}
}
#region Global Threading Model
ManualResetEvent stopped_event;
OperationCommandResult current_operation;
internal WaitHandle WaitHandle {
get { return stopped_event; }
}
internal CommandResult StartOperation (ThreadingModel model, SingleSteppingEngine caller)
{
if (!ThreadManager.InBackgroundThread)
throw new InternalError ();
if ((model & ThreadingModel.ThreadingMode) == ThreadingModel.Default) {
if (Inferior.HasThreadEvents)
model |= ThreadingModel.Single;
else
model |= ThreadingModel.Process;
}
if ((model & ThreadingModel.ThreadingMode) != ThreadingModel.Global)
return caller.Process.StartOperation (model, caller);
if (current_operation != null)
throw new TargetException (TargetError.NotStopped);
lock (this) {
stopped_event.Reset ();
current_operation = new GlobalCommandResult (this, model);
}
foreach (Process process in process_hash.Values) {
process.StartGlobalOperation (model, caller, current_operation);
}
return current_operation;
}
void OperationCompleted (SingleSteppingEngine caller, TargetEventArgs result, ThreadingModel model)
{
if (!ThreadManager.InBackgroundThread)
throw new InternalError ();
foreach (Process process in process_hash.Values) {
process.OperationCompleted (caller, result, model);
}
lock (this) {
current_operation = null;
stopped_event.Set ();
}
}
void StopAll ()
{
foreach (Process process in process_hash.Values) {
process.Stop ();
}
}
class MyOperationHost : IOperationHost
{
public Debugger Debugger;
public MyOperationHost (Debugger debugger)
{
this.Debugger = debugger;
}
void IOperationHost.OperationCompleted (SingleSteppingEngine caller, TargetEventArgs result, ThreadingModel model)
{
Debugger.OperationCompleted (caller, result, model);
}
WaitHandle IOperationHost.WaitHandle {
get { return Debugger.stopped_event; }
}
void IOperationHost.SendResult (SingleSteppingEngine sse, TargetEventArgs args)
{
Debugger.OnTargetEvent (sse.Thread, args);
}
void IOperationHost.Abort ()
{
Debugger.StopAll ();
}
}
class GlobalCommandResult : OperationCommandResult
{
public Debugger Debugger {
get; private set;
}
internal override IOperationHost Host {
get { return Debugger.OperationHost; }
}
internal GlobalCommandResult (Debugger debugger, ThreadingModel model)
: base (model)
{
this.Debugger = debugger;
}
internal override void OnExecd (SingleSteppingEngine new_thread)
{ }
}
#endregion
[Obsolete]
GUIManager gui_manager;
[Obsolete]
internal GUIManager GUIManager {
get {
if (gui_manager == null)
gui_manager = new GUIManager (this);
return gui_manager;
}
}
//
// IDisposable
//
void check_alive ()
{
if (!alive)
throw new TargetException (TargetError.NoTarget);
}
private bool disposed = false;
private void check_disposed ()
{
if (disposed)
throw new ObjectDisposedException ("DebuggerServant");
}
void Dispose (bool disposing)
{
// Check to see if Dispose has already been called.
lock (this) {
if (disposed)
return;
disposed = true;
alive = false;
}
// If this is a call to Dispose, dispose all managed resources.
if (disposing) {
if (thread_manager != null) {
thread_manager.Dispose ();
thread_manager = null;
}
ObjectCache.Shutdown ();
}
}
public void Dispose ()
{
Dispose (true);
// Take yourself off the Finalization queue
GC.SuppressFinalize (this);
}
~Debugger ()
{
Dispose (false);
}
}
}