This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
/
Debugger.cs
402 lines (317 loc) · 8.82 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
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.Backends;
using Mono.Debugger.Languages;
using Mono.Debugger.Languages.Mono;
using Mono.Debugger.Remoting;
namespace Mono.Debugger
{
public delegate void DebuggerEventHandler (Debugger debugger, Process process);
public abstract class Debugger : MarshalByRefObject, IDisposable
{
BfdContainer bfd_container;
ArrayList languages;
DebuggerManager manager;
SourceFileFactory source_factory;
MonoLanguageBackend mono_language;
SymbolTableManager symtab_manager;
ModuleManager module_manager;
ThreadManager thread_manager;
ProcessStart start;
protected Debugger (DebuggerManager manager)
{
this.manager = manager;
DebuggerContext.CreateServerContext (this);
module_manager = new ModuleManager ();
source_factory = new SourceFileFactory ();
languages = new ArrayList ();
bfd_container = new BfdContainer (this);
symtab_manager = new SymbolTableManager ();
symtab_manager.ModulesChangedEvent +=
new SymbolTableManager.ModuleHandler (modules_reloaded);
module_manager.ModulesChanged += new ModulesChangedHandler (modules_changed);
module_manager.BreakpointsChanged += new BreakpointsChangedHandler (breakpoints_changed);
thread_manager = new ThreadManager (this);
}
public ModuleManager ModuleManager {
get {
return module_manager;
}
}
public SymbolTableManager SymbolTableManager {
get {
return symtab_manager;
}
}
internal ThreadManager ThreadManager {
get {
return thread_manager;
}
}
internal ProcessStart ProcessStart {
get {
return start;
}
}
public SourceFileFactory SourceFileFactory {
get {
return source_factory;
}
}
internal BfdContainer BfdContainer {
get {
return bfd_container;
}
}
public Language NativeLanguage {
get {
return bfd_container.NativeLanguage;
}
}
public event DebuggerEventHandler InitializedEvent;
public event DebuggerEventHandler MainThreadCreatedEvent;
public event DebuggerEventHandler ThreadCreatedEvent;
public event DebuggerEventHandler ThreadExitedEvent;
public event TargetExitedHandler TargetExitedEvent;
public event TargetEventHandler TargetEvent;
public event SymbolTableChangedHandler SymbolTableChanged;
public event ModulesChangedHandler ModulesChangedEvent;
public event BreakpointsChangedHandler BreakpointsChangedEvent;
public void Run (DebuggerOptions options)
{
check_disposed ();
if (thread_manager.HasTarget)
throw new TargetException (TargetError.AlreadyHaveTarget);
start = new ProcessStart (options);
module_manager.Lock ();
thread_manager.StartApplication (start);
}
public void Attach (DebuggerOptions options, int pid)
{
check_disposed ();
if (thread_manager.HasTarget)
throw new TargetException (TargetError.AlreadyHaveTarget);
start = new ProcessStart (options, pid);
module_manager.Lock ();
thread_manager.StartApplication (start);
}
public Process OpenCoreFile (DebuggerOptions options, string core_file,
out Process[] threads)
{
check_disposed ();
if (thread_manager.HasTarget)
throw new TargetException (TargetError.AlreadyHaveTarget);
start = new ProcessStart (options, core_file);
module_manager.Lock ();
return thread_manager.OpenCoreFile (start, out threads);
}
public Process WaitForApplication ()
{
return thread_manager.WaitForApplication ();
}
internal void OnInitializedEvent (Process main_process)
{
manager.MainThreadGroup.AddThread (main_process.ID);
if (InitializedEvent != null)
InitializedEvent (this, main_process);
}
internal void OnMainThreadCreatedEvent (Process new_process)
{
if (MainThreadCreatedEvent != null)
MainThreadCreatedEvent (this, new_process);
}
internal void OnThreadCreatedEvent (Process new_process)
{
if (ThreadCreatedEvent != null)
ThreadCreatedEvent (this, new_process);
}
internal void OnThreadExitedEvent (Process process)
{
if (ThreadExitedEvent != null)
ThreadExitedEvent (this, process);
}
internal void OnTargetExitedEvent ()
{
if (TargetExitedEvent != null)
TargetExitedEvent ();
}
internal void SendTargetEvent (SingleSteppingEngine sse, TargetEventArgs args)
{
try {
if (TargetEvent != null)
TargetEvent (sse.TargetAccess, args);
} catch (Exception ex) {
Error ("{0} caught exception while sending {1}:\n{2}",
sse, args, ex);
}
}
void modules_changed ()
{
check_disposed ();
symtab_manager.SetModules (module_manager.Modules);
}
Module[] current_modules = null;
void breakpoints_changed ()
{
if (BreakpointsChangedEvent != null)
BreakpointsChangedEvent ();
}
void modules_reloaded (object sender, Module[] modules)
{
current_modules = modules;
if (ModulesChangedEvent != null)
ModulesChangedEvent ();
}
internal void ReachedMain ()
{
module_manager.UnLock ();
symtab_manager.Wait ();
}
// XXX This desperately needs to be renamed.
internal ILanguageBackend CreateDebuggerHandler (MonoDebuggerInfo info)
{
mono_language = new MonoLanguageBackend (this, info);
languages.Add (mono_language);
return mono_language;
}
internal void AddLanguage (ILanguageBackend language)
{
languages.Add (language);
}
internal ArrayList Languages {
get { return languages; }
}
internal DebuggerManager DebuggerManager {
get { return manager; }
}
public SourceLocation FindLocation (string file, int line)
{
foreach (Module module in Modules) {
SourceLocation location = module.FindLocation (file, line);
if (location != null)
return location;
}
return null;
}
public SourceLocation FindMethod (string name)
{
foreach (Module module in Modules) {
SourceMethod method = module.FindMethod (name);
if (method != null)
return new SourceLocation (method);
}
return null;
}
public void Error (string message, params object[] args)
{
Console.WriteLine ("ERROR: " + String.Format (message, args));
}
internal void UpdateSymbolTable (ITargetMemoryAccess target)
{
if (mono_language != null)
mono_language.Update (target);
}
public Module[] Modules {
get {
if (current_modules != null)
return current_modules;
return new Module [0];
}
}
public void LoadLibrary (Process process, string filename)
{
if (mono_language == null)
throw new SymbolTableException (
"Cannot load .NET assembly {0} while " +
"debugging an unmanaged application",
filename);
if (!mono_language.TryFindImage (process, filename))
bfd_container.AddFile (process, filename, TargetAddress.Null, true, false);
}
internal MonoLanguageBackend MonoLanguage {
get {
if (mono_language == null)
throw new InvalidOperationException ();
return mono_language;
}
}
internal bool IsManagedApplication {
get { return mono_language != null; }
}
public EventHandle InsertBreakpoint (TargetAccess target, int domain,
Breakpoint bpt, SourceLocation location)
{
return new BreakpointHandle (target, domain, bpt, location);
}
public EventHandle InsertBreakpoint (TargetAccess target, Breakpoint bpt,
TargetFunctionType func)
{
return new BreakpointHandle (target, bpt, func);
}
public EventHandle InsertExceptionCatchPoint (TargetAccess target, ThreadGroup group,
TargetType exception)
{
return new CatchpointHandle (target, group, exception);
}
//
// IDisposable
//
protected abstract void DebuggerExited ();
private bool disposed = false;
private void check_disposed ()
{
if (disposed)
throw new ObjectDisposedException ("Debugger");
}
protected virtual void Dispose (bool disposing)
{
// Check to see if Dispose has already been called.
lock (this) {
if (disposed)
return;
disposed = true;
}
// If this is a call to Dispose, dispose all managed resources.
if (disposing) {
if (symtab_manager != null) {
symtab_manager.Dispose ();
symtab_manager = null;
}
if (thread_manager != null) {
thread_manager.Dispose ();
thread_manager = null;
}
if (bfd_container != null) {
bfd_container.Dispose ();
bfd_container = null;
}
if (languages != null) {
foreach (ILanguageBackend lang in languages)
lang.Dispose();
languages = null;
}
ObjectCache.Shutdown ();
DebuggerExited ();
}
}
public void Dispose ()
{
Dispose (true);
// Take yourself off the Finalization queue
GC.SuppressFinalize (this);
}
~Debugger ()
{
Dispose (false);
}
}
}