This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
MonoLanguageBackend.cs
955 lines (782 loc) · 29.8 KB
/
MonoLanguageBackend.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
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections;
using System.Threading;
using C = Mono.CompilerServices.SymbolWriter;
using R = System.Reflection;
using Mono.Debugger;
using Mono.Debugger.Backends;
using Mono.Debugger.Architecture;
namespace Mono.Debugger.Languages.Mono
{
internal delegate void BreakpointHandler (Inferior inferior, TargetAddress address,
object user_data);
// <summary>
// This class is the managed representation of the MONO_DEBUGGER__debugger_info struct.
// as defined in debugger/wrapper/mono-debugger-jit-wrapper.h
// </summary>
internal class MonoDebuggerInfo
{
public readonly TargetAddress MonoTrampolineCode;
public readonly TargetAddress SymbolTable;
public readonly int SymbolTableSize;
public readonly TargetAddress CompileMethod;
public readonly TargetAddress GetVirtualMethod;
public readonly TargetAddress GetBoxedObjectMethod;
public readonly TargetAddress InsertBreakpoint;
public readonly TargetAddress RemoveBreakpoint;
public readonly TargetAddress RuntimeInvoke;
public readonly TargetAddress CreateString;
public readonly TargetAddress ClassGetStaticFieldData;
public readonly TargetAddress LookupType;
public readonly TargetAddress LookupAssembly;
public readonly TargetAddress Heap;
public readonly int HeapSize;
internal MonoDebuggerInfo (ITargetMemoryReader reader)
{
/* skip past magic, version, and total_size */
reader.Offset = 16;
SymbolTableSize = reader.ReadInteger ();
HeapSize = reader.ReadInteger ();
MonoTrampolineCode = reader.ReadGlobalAddress ();
SymbolTable = reader.ReadGlobalAddress ();
CompileMethod = reader.ReadGlobalAddress ();
GetVirtualMethod = reader.ReadGlobalAddress ();
GetBoxedObjectMethod = reader.ReadGlobalAddress ();
InsertBreakpoint = reader.ReadGlobalAddress ();
RemoveBreakpoint = reader.ReadGlobalAddress ();
RuntimeInvoke = reader.ReadGlobalAddress ();
CreateString = reader.ReadGlobalAddress ();
ClassGetStaticFieldData = reader.ReadGlobalAddress ();
LookupType = reader.ReadGlobalAddress ();
LookupAssembly = reader.ReadGlobalAddress ();
Heap = reader.ReadAddress ();
Report.Debug (DebugFlags.JitSymtab, this);
}
public override string ToString ()
{
return String.Format (
"MonoDebuggerInfo ({0:x}:{1:x}:{2:x}:{3:x}:{4:x}:{5:x}:{6:x})",
MonoTrampolineCode, SymbolTable, SymbolTableSize,
CompileMethod, InsertBreakpoint, RemoveBreakpoint,
RuntimeInvoke);
}
}
// <summary>
// This class is the managed representation of the
// MonoDefaults struct (at least the types we're interested
// in) as defined in mono/metadata/class-internals.h.
// </summary>
internal class MonoBuiltinTypeInfo
{
public readonly MonoSymbolFile Corlib;
public readonly MonoObjectType ObjectType;
public readonly MonoFundamentalType ByteType;
public readonly MonoOpaqueType VoidType;
public readonly MonoFundamentalType BooleanType;
public readonly MonoFundamentalType SByteType;
public readonly MonoFundamentalType Int16Type;
public readonly MonoFundamentalType UInt16Type;
public readonly MonoFundamentalType Int32Type;
public readonly MonoFundamentalType UInt32Type;
public readonly MonoFundamentalType IntType;
public readonly MonoFundamentalType UIntType;
public readonly MonoFundamentalType Int64Type;
public readonly MonoFundamentalType UInt64Type;
public readonly MonoFundamentalType SingleType;
public readonly MonoFundamentalType DoubleType;
public readonly MonoFundamentalType CharType;
public readonly MonoStringType StringType;
public readonly MonoClassType ExceptionType;
public readonly int KlassFieldOffset;
public readonly int KlassMethodsOffset;
public readonly int KlassMethodCountOffset;
public readonly int FieldInfoSize;
public MonoBuiltinTypeInfo (MonoSymbolFile corlib, ITargetMemoryAccess memory,
TargetAddress address)
{
this.Corlib = corlib;
int size = memory.ReadInteger (address);
TargetBinaryReader reader = memory.ReadMemory (address, size).GetReader ();
reader.ReadInt32 ();
int defaults_size = reader.ReadInt32 ();
TargetAddress defaults_address = new TargetAddress (
memory.GlobalAddressDomain, reader.ReadAddress ());
KlassFieldOffset = reader.ReadInt32 ();
KlassMethodsOffset = reader.ReadInt32 ();
KlassMethodCountOffset = reader.ReadInt32 ();
FieldInfoSize = reader.ReadInt32 ();
TargetReader mono_defaults = new TargetReader (
memory.ReadMemory (defaults_address, defaults_size).Contents, memory);
mono_defaults.ReadAddress ();
TargetAddress klass = mono_defaults.ReadGlobalAddress ();
int object_size = 2 * corlib.TargetInfo.TargetAddressSize;
Cecil.ITypeDefinition object_type = corlib.Module.Types ["System.Object"];
ObjectType = new MonoObjectType (corlib, object_type, object_size, klass);
corlib.AddCoreType (ObjectType);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition byte_type = corlib.Module.Types ["System.Byte"];
ByteType = new MonoFundamentalType (corlib, byte_type, FundamentalKind.Byte, 1, klass);
corlib.AddCoreType (ByteType);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition void_type = corlib.Module.Types ["System.Void"];
VoidType = new MonoOpaqueType (corlib, void_type);
corlib.AddCoreType (VoidType);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition bool_type = corlib.Module.Types ["System.Boolean"];
BooleanType = new MonoFundamentalType (corlib, bool_type, FundamentalKind.Byte, 1, klass);
corlib.AddCoreType (BooleanType);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition sbyte_type = corlib.Module.Types ["System.SByte"];
SByteType = new MonoFundamentalType (corlib, sbyte_type, FundamentalKind.SByte, 1, klass);
corlib.AddCoreType (SByteType);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition short_type = corlib.Module.Types ["System.Int16"];
Int16Type = new MonoFundamentalType (corlib, short_type, FundamentalKind.Int16, 2, klass);
corlib.AddCoreType (Int16Type);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition ushort_type = corlib.Module.Types ["System.UInt16"];
UInt16Type = new MonoFundamentalType (corlib, ushort_type, FundamentalKind.UInt16, 2, klass);
corlib.AddCoreType (UInt16Type);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition int_type = corlib.Module.Types ["System.Int32"];
Int32Type = new MonoFundamentalType (corlib, int_type, FundamentalKind.Int32, 4, klass);
Int32Type.GetTypeInfo ();
corlib.AddCoreType (Int32Type);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition uint_type = corlib.Module.Types ["System.UInt32"];
UInt32Type = new MonoFundamentalType (corlib, uint_type, FundamentalKind.UInt32, 4, klass);
corlib.AddCoreType (UInt32Type);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition intptr_type = corlib.Module.Types ["System.IntPtr"];
IntType = new MonoFundamentalType (corlib, intptr_type, FundamentalKind.IntPtr, 4, klass);
corlib.AddCoreType (IntType);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition uintptr_type = corlib.Module.Types ["System.UIntPtr"];
UIntType = new MonoFundamentalType (corlib, uintptr_type, FundamentalKind.Object, 4, klass);
corlib.AddCoreType (UIntType);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition long_type = corlib.Module.Types ["System.Int64"];
Int64Type = new MonoFundamentalType (corlib, long_type, FundamentalKind.Int64, 8, klass);
corlib.AddCoreType (Int64Type);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition ulong_type = corlib.Module.Types ["System.UInt64"];
UInt64Type = new MonoFundamentalType (corlib, ulong_type, FundamentalKind.UInt64, 8, klass);
corlib.AddCoreType (UInt64Type);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition float_type = corlib.Module.Types ["System.Single"];
SingleType = new MonoFundamentalType (corlib, float_type, FundamentalKind.Single, 4, klass);
corlib.AddCoreType (SingleType);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition double_type = corlib.Module.Types ["System.Double"];
DoubleType = new MonoFundamentalType (corlib, double_type, FundamentalKind.Double, 8, klass);
corlib.AddCoreType (DoubleType);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition char_type = corlib.Module.Types ["System.Char"];
CharType = new MonoFundamentalType (corlib, char_type, FundamentalKind.Char, 2, klass);
corlib.AddCoreType (CharType);
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition string_type = corlib.Module.Types ["System.String"];
StringType = new MonoStringType (
corlib, string_type, object_size, object_size + 4, klass);
corlib.AddCoreType (StringType);
// Skip a whole bunch of clases we don't care about
mono_defaults.Offset += 10 * corlib.TargetInfo.TargetAddressSize;
// and get to the Exception class
klass = mono_defaults.ReadGlobalAddress ();
Cecil.ITypeDefinition exception_type = corlib.Module.Types ["System.Exception"];
ExceptionType = new MonoClassType (corlib, exception_type);
corlib.AddCoreType (ExceptionType);
}
}
internal static class MonoDebuggerSupport
{
static GetTypeFunc get_type;
static GetLocalTypeFromSignatureFunc local_type_from_sig;
static GetGuidFunc get_guid;
static CheckRuntimeVersionFunc check_runtime_version;
delegate Type GetTypeFunc (R.Assembly assembly, int token);
delegate R.MethodBase GetMethodFunc (R.Assembly assembly, int token);
delegate Type GetLocalTypeFromSignatureFunc (R.Assembly assembly, byte[] sig);
delegate Guid GetGuidFunc (R.Module module);
delegate string CheckRuntimeVersionFunc (string filename);
delegate Type ResolveTypeFunc (R.Module module, int token);
delegate int GetTypeTokenFunc (Type type);
static Delegate create_delegate (Type type, Type delegate_type, string name)
{
R.MethodInfo mi = type.GetMethod (name, R.BindingFlags.Static |
R.BindingFlags.NonPublic);
if (mi == null)
throw new InternalError ("Can't find " + name);
return Delegate.CreateDelegate (delegate_type, mi);
}
static MonoDebuggerSupport ()
{
get_type = (GetTypeFunc) create_delegate (
typeof (R.Assembly), typeof (GetTypeFunc),
"MonoDebugger_GetType");
local_type_from_sig = (GetLocalTypeFromSignatureFunc) create_delegate (
typeof (R.Assembly), typeof (GetLocalTypeFromSignatureFunc),
"MonoDebugger_GetLocalTypeFromSignature");
get_guid = (GetGuidFunc) create_delegate (
typeof (R.Module), typeof (GetGuidFunc), "Mono_GetGuid");
check_runtime_version = (CheckRuntimeVersionFunc) create_delegate (
typeof (R.Assembly), typeof (CheckRuntimeVersionFunc),
"MonoDebugger_CheckRuntimeVersion");
}
public static Type GetType (R.Assembly assembly, int token)
{
return get_type (assembly, token);
}
public static int GetMethodToken (Cecil.IMethodDefinition method)
{
return (int) (method.MetadataToken.TokenType + method.MetadataToken.RID);
}
public static Cecil.IMethodDefinition GetMethod (Cecil.IModuleDefinition module, int token)
{
return (Cecil.IMethodDefinition) module.LookupByToken (
Cecil.Metadata.TokenType.Method, token & 0xffffff);
}
public static Type GetLocalTypeFromSignature (R.Assembly assembly, byte[] sig)
{
return local_type_from_sig (assembly, sig);
}
public static string CheckRuntimeVersion (string filename)
{
return check_runtime_version (filename);
}
public static Guid GetGuid (R.Module module)
{
return get_guid (module);
}
public static Cecil.ITypeReference MakeArrayType (Cecil.ITypeReference type, int rank)
{
/// XXXX - TODO
throw new NotImplementedException ();
}
public static Cecil.ITypeReference ResolveType (Cecil.IModuleDefinition module, int token)
{
return (Cecil.ITypeReference) module.LookupByToken (
Cecil.Metadata.TokenType.TypeDef, token);
}
public static int GetTypeToken (Cecil.ITypeReference type)
{
return (int) (type.MetadataToken.TokenType + type.MetadataToken.RID);
}
}
internal class MonoLanguageBackend : MarshalByRefObject, ILanguage, ILanguageBackend
{
// These constants must match up with those in mono/mono/metadata/mono-debug.h
public const int MinDynamicVersion = 52;
public const int MaxDynamicVersion = 52;
public const long DynamicMagic = 0x7aff65af4253d427;
ArrayList symbol_files;
int last_num_symbol_files;
Hashtable assembly_hash;
Hashtable class_hash;
MonoSymbolFile corlib;
MonoBuiltinTypeInfo builtin_types;
int last_num_data_tables;
int last_data_table_offset;
DebuggerBackend backend;
MonoDebuggerInfo info;
TargetAddress[] trampolines;
bool initialized;
DebuggerMutex mutex;
Heap heap;
public MonoLanguageBackend (DebuggerBackend backend)
{
this.backend = backend;
mutex = new DebuggerMutex ("mono_mutex");
}
// needed for both ILanguage and ILanguageBackend interfaces
public string Name {
get { return "Mono"; }
}
internal MonoDebuggerInfo MonoDebuggerInfo {
get { return info; }
}
internal MonoBuiltinTypeInfo BuiltinTypes {
get { return builtin_types; }
}
public Heap DataHeap {
get { return heap; }
}
public SourceFileFactory SourceFileFactory {
get { return backend.SourceFileFactory; }
}
internal bool TryFindImage (Process process, string filename)
{
R.Assembly ass = R.Assembly.LoadFrom (filename);
if (ass == null)
return false;
MonoSymbolFile file = (MonoSymbolFile) assembly_hash [ass];
if (file != null)
return true;
mutex.Lock ();
process.CallMethod (info.LookupAssembly, 0, ass.Location);
mutex.Unlock ();
// always return true?
return true;
}
public MonoType LookupMonoType (Cecil.ITypeReference type)
{
MonoSymbolFile file = (MonoSymbolFile) assembly_hash [type.Module.Assembly];
if (file == null) {
Console.WriteLine ("Type `{0}' from unknown assembly `{1}'",
type, type.Module.Assembly);
return null;
}
return file.LookupMonoType (type);
}
public void AddClass (TargetAddress klass_address, MonoType type)
{
class_hash.Add (klass_address, type);
}
public MonoType GetClass (TargetAddress klass_address)
{
return (MonoType) class_hash [klass_address];
}
void read_mono_debugger_info (ITargetMemoryAccess memory, Bfd bfd)
{
TargetAddress symbol_info = bfd ["MONO_DEBUGGER__debugger_info"];
if (symbol_info.IsNull)
throw new SymbolTableException (
"Can't get address of `MONO_DEBUGGER__debugger_info'.");
TargetBinaryReader header = memory.ReadMemory (symbol_info, 16).GetReader ();
long magic = header.ReadInt64 ();
if (magic != DynamicMagic)
throw new SymbolTableException (
"`MONO_DEBUGGER__debugger_info' has unknown magic {0:x}.", magic);
int version = header.ReadInt32 ();
if (version < MinDynamicVersion)
throw new SymbolTableException (
"`MONO_DEBUGGER__debugger_info' has version {0}, " +
"but expected at least {1}.", version, MinDynamicVersion);
if (version > MaxDynamicVersion)
throw new SymbolTableException (
"`MONO_DEBUGGER__debugger_info' has version {0}, " +
"but expected at most {1}.", version, MaxDynamicVersion);
int size = header.ReadInt32 ();
TargetReader table = new TargetReader (
memory.ReadMemory (symbol_info, size), memory);
info = new MonoDebuggerInfo (table);
init_trampolines (memory);
heap = new Heap ((ITargetInfo) memory, info.Heap, info.HeapSize);
symbol_files = new ArrayList ();
assembly_hash = new Hashtable ();
class_hash = new Hashtable ();
}
void init_trampolines (ITargetMemoryAccess memory)
{
trampolines = new TargetAddress [4];
TargetAddress address = info.MonoTrampolineCode;
trampolines [0] = memory.ReadGlobalAddress (address);
address += memory.TargetAddressSize;
trampolines [1] = memory.ReadGlobalAddress (address);
address += memory.TargetAddressSize;
trampolines [2] = memory.ReadGlobalAddress (address);
address += 2 * memory.TargetAddressSize;
trampolines [3] = memory.ReadGlobalAddress (address);
}
#region symbol table management
void do_update_symbol_table (ITargetMemoryAccess memory)
{
Report.Debug (DebugFlags.JitSymtab, "Starting to update symbol table");
backend.ModuleManager.Lock ();
try {
do_update (memory);
} catch (ThreadAbortException) {
return;
} catch (Exception e) {
Console.WriteLine ("Can't update symbol table: {0}", e);
return;
} finally {
backend.ModuleManager.UnLock ();
}
Report.Debug (DebugFlags.JitSymtab, "Done updating symbol table");
}
// This method reads the MonoDebuggerSymbolTable structure
// (struct definition is in mono-debug-debugger.h)
void do_update (ITargetMemoryAccess memory)
{
TargetAddress symtab_address = memory.ReadAddress (info.SymbolTable);
TargetReader header = new TargetReader (
memory.ReadMemory (symtab_address, info.SymbolTableSize), memory);
long magic = header.ReadLongInteger ();
if (magic != DynamicMagic)
throw new SymbolTableException (
"Debugger symbol table has unknown magic {0:x}.", magic);
int version = header.ReadInteger ();
if (version < MinDynamicVersion)
throw new SymbolTableException (
"Debugger symbol table has version {0}, but " +
"expected at least {1}.", version,
MinDynamicVersion);
if (version > MaxDynamicVersion)
throw new SymbolTableException (
"Debugger symbol table has version {0}, but " +
"expected at most {1}.", version,
MaxDynamicVersion);
int total_size = header.ReadInteger ();
if (total_size != info.SymbolTableSize)
throw new SymbolTableException (
"Debugger symbol table has size {0}, but " +
"expected {1}.", total_size, info.SymbolTableSize);
TargetAddress corlib_address = header.ReadGlobalAddress ();
TargetAddress metadata_info = header.ReadGlobalAddress ();
TargetAddress symfiles_address = header.ReadAddress ();
int num_symbol_files = header.ReadInteger ();
symfiles_address += last_num_symbol_files * memory.TargetAddressSize;
for (int i = last_num_symbol_files; i < num_symbol_files; i++) {
TargetAddress address = memory.ReadGlobalAddress (symfiles_address);
symfiles_address += memory.TargetAddressSize;
try {
MonoSymbolFile symfile = new MonoSymbolFile (
this, backend, memory, memory, address);
symbol_files.Add (symfile);
assembly_hash.Add (symfile.Assembly, symfile);
if (address != corlib_address)
continue;
corlib = symfile;
builtin_types = new MonoBuiltinTypeInfo (corlib, memory, metadata_info);
} catch (C.MonoSymbolFileException ex) {
Console.WriteLine (ex.Message);
} catch (Exception ex) {
Console.WriteLine (ex.Message);
}
}
last_num_symbol_files = num_symbol_files;
read_data_table (memory, header);
}
// This method reads a portion of the data table (defn in mono-debug.h)
void read_data_table (ITargetMemoryAccess memory, ITargetMemoryReader header)
{
int num_data_tables = header.ReadInteger ();
TargetAddress data_tables = header.ReadAddress ();
Report.Debug (DebugFlags.JitSymtab, "DATA TABLES: {0} {1} {2}",
last_num_data_tables, num_data_tables, data_tables);
if (num_data_tables != last_num_data_tables) {
int old_offset = last_data_table_offset;
data_tables += last_num_data_tables * memory.TargetAddressSize;
for (int i = last_num_data_tables; i < num_data_tables; i++) {
TargetAddress old_table = memory.ReadAddress (data_tables);
data_tables += memory.TargetAddressSize;
int old_size = memory.ReadInteger (old_table);
read_data_items (memory, old_table, old_offset, old_size);
old_offset = 0;
}
last_num_data_tables = num_data_tables;
last_data_table_offset = 0;
}
TargetAddress data_table_address = header.ReadAddress ();
int data_table_size = header.ReadInteger ();
int offset = header.ReadInteger ();
int size = offset - last_data_table_offset;
Report.Debug (DebugFlags.JitSymtab,
"DATA TABLE: {0} {1} {2} - {3} {4}",
data_table_address, data_table_size, offset,
last_data_table_offset, size);
if (size != 0)
read_data_items (memory, data_table_address, last_data_table_offset, offset);
last_data_table_offset = offset;
}
private enum DataItemType {
Unknown = 0,
Method,
Class,
Wrapper
}
void read_data_items (ITargetMemoryAccess memory, TargetAddress address, int start, int end)
{
TargetReader reader = new TargetReader (
memory.ReadMemory (address + start, end - start), memory);
Report.Debug (DebugFlags.JitSymtab,
"READ DATA ITEMS: {0} {1} {2} - {3} {4}", address, start, end,
reader.BinaryReader.Position, reader.Size);
if (start == 0)
reader.BinaryReader.Position = memory.TargetAddressSize;
while (reader.BinaryReader.Position + 4 < reader.Size) {
int item_size = reader.BinaryReader.ReadInt32 ();
if (item_size == 0)
break;
DataItemType item_type = (DataItemType) reader.BinaryReader.ReadInt32 ();
long pos = reader.BinaryReader.Position;
switch (item_type) {
case DataItemType.Method:
read_range_entry (reader);
break;
case DataItemType.Class:
read_class_entry (reader);
break;
case DataItemType.Wrapper:
read_wrapper_entry (memory, reader);
break;
}
reader.BinaryReader.Position = pos + item_size;
}
}
void read_range_entry (ITargetMemoryReader reader)
{
int size = reader.BinaryReader.PeekInt32 ();
byte[] contents = reader.BinaryReader.PeekBuffer (size);
reader.BinaryReader.ReadInt32 ();
MonoSymbolFile file = (MonoSymbolFile) symbol_files [reader.BinaryReader.ReadInt32 ()];
file.AddRangeEntry (reader, contents);
}
void read_class_entry (ITargetMemoryReader reader)
{
int size = reader.BinaryReader.PeekInt32 ();
byte[] contents = reader.BinaryReader.PeekBuffer (size);
reader.BinaryReader.ReadInt32 ();
int file_idx = reader.BinaryReader.ReadInt32 ();
if (file_idx >= symbol_files.Count)
return;
MonoSymbolFile file = (MonoSymbolFile) symbol_files [file_idx];
if (file == null)
return;
file.AddClassEntry (reader, contents);
}
void read_wrapper_entry (ITargetMemoryAccess memory, ITargetMemoryReader reader)
{
int size = reader.BinaryReader.PeekInt32 ();
byte[] contents = reader.BinaryReader.PeekBuffer (size);
reader.BinaryReader.ReadInt32 ();
corlib.AddWrapperEntry (memory, reader, contents);
#if FIXME
TargetAddress wrapper = reader.ReadGlobalAddress ();
TargetAddress code = reader.ReadGlobalAddress ();
int size = reader.ReadInteger ();
TargetAddress naddr = memory.ReadAddress (
wrapper + 8 + 2 * reader.TargetAddressSize);
string name = "<" + memory.ReadString (naddr) + ">";
corlib.AddWrapperEntry (wrapper, name, code, size);
#endif
}
#endregion
#region jit breakpoint handling
private struct MyBreakpointHandle
{
public readonly int Index;
public readonly BreakpointHandler Handler;
public readonly object UserData;
public MyBreakpointHandle (int index, BreakpointHandler handler, object user_data)
{
this.Index = index;
this.Handler = handler;
this.UserData = user_data;
}
}
Hashtable breakpoints = new Hashtable ();
internal int InsertBreakpoint (Process process, string method_name,
BreakpointHandler handler, object user_data)
{
TargetAddress retval = process.CallMethod (info.InsertBreakpoint, 0, method_name);
int index = (int) retval.Address;
if (index <= 0)
return -1;
breakpoints.Add (index, new MyBreakpointHandle (index, handler, user_data));
return index;
}
#endregion
#region ILanguage implementation
public string SourceLanguage (StackFrame frame)
{
return "";
}
public ITargetType LookupType (StackFrame frame, string name)
{
switch (name) {
case "short": name = "System.Int16"; break;
case "ushort": name = "System.UInt16"; break;
case "int": name = "System.Int32"; break;
case "uint": name = "System.UInt32"; break;
case "long": name = "System.Int64"; break;
case "ulong": name = "System.UInt64"; break;
case "float": name = "System.Single"; break;
case "double": name = "System.Double"; break;
case "char": name = "System.Char"; break;
case "byte": name = "System.Byte"; break;
case "sbyte": name = "System.SByte"; break;
case "object": name = "System.Object"; break;
case "string": name = "System.String"; break;
case "bool": name = "System.Boolean"; break;
case "void": name = "System.Void"; break;
case "decimal": name = "System.Decimal"; break;
}
if (name.IndexOf ('[') >= 0)
return null;
// XXX this fixes #74391. we basically need
// to ensure the class has been initialized by
// the runtime (and therefore had its
// debugging info made available to the
// debugger).
mutex.Lock ();
frame.Process.CallMethod (info.LookupType, 0, name);
mutex.Unlock ();
foreach (MonoSymbolFile symfile in symbol_files) {
Cecil.ITypeDefinition type = symfile.Assembly.MainModule.Types [name];
if (type == null)
continue;
return symfile.LookupMonoType (type);
}
return null;
}
public bool CanCreateInstance (Type type)
{
return false;
}
public ITargetObject CreateInstance (StackFrame frame, object obj)
{
#if FIXME
MonoFundamentalType type = LookupMonoType (obj.GetType ()) as MonoFundamentalType;
if (type == null)
return null;
return type.CreateInstance (frame, obj);
#else
throw new NotImplementedException ();
#endif
}
public ITargetFundamentalObject CreateInstance (ITargetAccess target, int value)
{
return builtin_types.Int32Type.CreateInstance (target, value);
}
public ITargetPointerObject CreatePointer (StackFrame frame, TargetAddress address)
{
return backend.BfdContainer.NativeLanguage.CreatePointer (frame, address);
}
public ITargetObject CreateObject (ITargetAccess target, TargetAddress address)
{
TargetLocation location = new AbsoluteTargetLocation (null, target, address);
MonoObjectObject obj = (MonoObjectObject)builtin_types.ObjectType.GetTypeInfo().GetObject (location);
if (obj == null)
return null;
if (obj.HasDereferencedObject)
return obj.DereferencedObject;
else
return obj;
}
ITargetFundamentalType ILanguage.IntegerType {
get { return builtin_types.Int32Type; }
}
ITargetFundamentalType ILanguage.LongIntegerType {
get { return builtin_types.Int64Type; }
}
ITargetFundamentalType ILanguage.StringType {
get { return builtin_types.StringType; }
}
ITargetType ILanguage.PointerType {
get { return builtin_types.IntType; }
}
ITargetType ILanguage.ExceptionType {
get { return builtin_types.ExceptionType; }
}
#endregion
#region ILanguageBackend implementation
public TargetAddress RuntimeInvokeFunc {
get { return info.RuntimeInvoke; }
}
public TargetAddress GetTrampolineAddress (ITargetMemoryAccess memory,
TargetAddress address,
out bool is_start)
{
is_start = false;
if (trampolines == null)
return TargetAddress.Null;
foreach (TargetAddress trampoline in trampolines) {
TargetAddress result = memory.Architecture.GetTrampoline (
memory, address, trampoline);
if (!result.IsNull)
return result;
}
return TargetAddress.Null;
}
public TargetAddress CompileMethodFunc {
get { return info.CompileMethod; }
}
public TargetAddress GetVirtualMethodFunc {
get { return info.GetVirtualMethod; }
}
public TargetAddress GetBoxedObjectFunc {
get { return info.GetBoxedObjectMethod; }
}
public SourceMethod GetTrampoline (ITargetMemoryAccess memory,
TargetAddress address)
{
bool is_start;
TargetAddress trampoline = GetTrampolineAddress (memory, address, out is_start);
if (trampoline.IsNull)
return null;
int token = memory.ReadInteger (trampoline + 4);
TargetAddress klass = memory.ReadGlobalAddress (trampoline + 8);
TargetAddress image = memory.ReadGlobalAddress (klass);
foreach (MonoSymbolFile file in symbol_files) {
if (file.MonoImage != image)
continue;
return file.GetMethodByToken (token);
}
return null;
}
public void Notification (Inferior inferior, NotificationType type,
TargetAddress data, long arg)
{
switch (type) {
case NotificationType.InitializeManagedCode:
read_mono_debugger_info (inferior, inferior.Bfd);
do_update_symbol_table (inferior);
break;
case NotificationType.AddModule:
Report.Debug (DebugFlags.JitSymtab, "Module added");
do_update_symbol_table (inferior);
break;
case NotificationType.ReloadSymtabs:
do_update_symbol_table (inferior);
break;
case NotificationType.JitBreakpoint:
if (!breakpoints.Contains ((int) arg))
break;
do_update_symbol_table (inferior);
MyBreakpointHandle handle = (MyBreakpointHandle) breakpoints [(int) arg];
handle.Handler (inferior, data, handle.UserData);
breakpoints.Remove (arg);
break;
case NotificationType.MethodCompiled:
do_update_symbol_table (inferior);
break;
default:
Console.WriteLine ("Received unknown notification {0:x}",
(int) type);
break;
}
}
#endregion
private bool disposed = false;
private void Dispose (bool disposing)
{
lock (this) {
if (disposed)
return;
disposed = true;
}
if (disposing) {
if (symbol_files != null) {
foreach (MonoSymbolFile symfile in symbol_files)
symfile.Dispose();
symbol_files = null;
}
}
}
public void Dispose ()
{
Dispose (true);
// Take yourself off the Finalization queue
GC.SuppressFinalize (this);
}
~MonoLanguageBackend ()
{
Dispose (false);
}
}
}