-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathObjectValueAdaptor.cs
1503 lines (1212 loc) · 48.8 KB
/
ObjectValueAdaptor.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
//
// ObjectValueAdaptor.cs
//
// Authors: Lluis Sanchez Gual <[email protected]>
// Jeffrey Stedfast <[email protected]>
//
// Copyright (c) 2008 Novell, Inc (http://www.novell.com)
// Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.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.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;
using Mono.Debugging.Client;
using Mono.Debugging.Backend;
namespace Mono.Debugging.Evaluation
{
public abstract class ObjectValueAdaptor: IDisposable
{
readonly Dictionary<string, TypeDisplayData> typeDisplayData = new Dictionary<string, TypeDisplayData> ();
// Time to wait while evaluating before switching to async mode
public int DefaultEvaluationWaitTime {
get { return asyncEvaluationTracker.WaitTime; }
set { asyncEvaluationTracker.WaitTime = value; }
}
/// <summary>
/// Enables or disables async evaluation
/// </summary>
public bool UseTimeout {
get { return asyncEvaluationTracker.UseTimeout; }
set { asyncEvaluationTracker.UseTimeout = value; }
}
public event EventHandler<BusyStateEventArgs> BusyStateChanged;
static readonly Dictionary<string, string> CSharpTypeNames = new Dictionary<string, string> ();
readonly AsyncEvaluationTracker asyncEvaluationTracker = new AsyncEvaluationTracker ();
readonly AsyncOperationManager asyncOperationManager = new AsyncOperationManager ();
static ObjectValueAdaptor ()
{
CSharpTypeNames["System.Void"] = "void";
CSharpTypeNames["System.Object"] = "object";
CSharpTypeNames["System.Boolean"] = "bool";
CSharpTypeNames["System.Byte"] = "byte";
CSharpTypeNames["System.SByte"] = "sbyte";
CSharpTypeNames["System.Char"] = "char";
CSharpTypeNames["System.Enum"] = "enum";
CSharpTypeNames["System.Int16"] = "short";
CSharpTypeNames["System.Int32"] = "int";
CSharpTypeNames["System.Int64"] = "long";
CSharpTypeNames["System.UInt16"] = "ushort";
CSharpTypeNames["System.UInt32"] = "uint";
CSharpTypeNames["System.UInt64"] = "ulong";
CSharpTypeNames["System.Single"] = "float";
CSharpTypeNames["System.Double"] = "double";
CSharpTypeNames["System.Decimal"] = "decimal";
CSharpTypeNames["System.String"] = "string";
}
protected ObjectValueAdaptor ()
{
DefaultEvaluationWaitTime = 100;
asyncOperationManager.BusyStateChanged += (sender, e) => OnBusyStateChanged (e);
}
public void Dispose ()
{
asyncEvaluationTracker.Dispose ();
asyncOperationManager.Dispose ();
}
public ObjectValue CreateObjectValue (EvaluationContext ctx, IObjectValueSource source, ObjectPath path, object obj, ObjectValueFlags flags)
{
try {
return CreateObjectValueImpl (ctx, source, path, obj, flags);
} catch (EvaluatorAbortedException ex) {
return ObjectValue.CreateFatalError (path.LastName, ex.Message, flags);
} catch (EvaluatorException ex) {
return ObjectValue.CreateFatalError (path.LastName, ex.Message, flags);
} catch (Exception ex) {
ctx.WriteDebuggerError (ex);
return ObjectValue.CreateFatalError (path.LastName, ex.Message, flags);
}
}
public virtual string GetDisplayTypeName (string typeName)
{
return GetDisplayTypeName (typeName.Replace ('+', '.'), 0, typeName.Length);
}
public string GetDisplayTypeName (EvaluationContext ctx, object type)
{
return GetDisplayTypeName (GetTypeName (ctx, type));
}
string GetDisplayTypeName (string typeName, int startIndex, int endIndex)
{
// Note: '[' denotes the start of an array
// '`' denotes a generic type
// ',' denotes the start of the assembly name
int tokenIndex = typeName.IndexOfAny (new [] { '[', '`', ',' }, startIndex, endIndex - startIndex);
List<string> genericArgs = null;
string array = string.Empty;
int genericEndIndex = -1;
int typeEndIndex;
retry:
if (tokenIndex == -1) // Simple type
return GetShortTypeName (typeName.Substring (startIndex, endIndex - startIndex));
if (typeName[tokenIndex] == ',') // Simple type with an assembly name
return GetShortTypeName (typeName.Substring (startIndex, tokenIndex - startIndex));
// save the index of the end of the type name
typeEndIndex = tokenIndex;
// decode generic args first, if this is a generic type
if (typeName[tokenIndex] == '`') {
genericEndIndex = typeName.IndexOf ('[', tokenIndex, endIndex - tokenIndex);
if (genericEndIndex == -1) {
// Mono's compiler seems to generate non-generic types with '`'s in the name
// e.g. __EventHandler`1_FileCopyEventArgs_DelegateFactory_2
tokenIndex = typeName.IndexOfAny (new [] { '[', ',' }, tokenIndex, endIndex - tokenIndex);
goto retry;
}
tokenIndex = genericEndIndex;
genericArgs = GetGenericArguments (typeName, ref tokenIndex, endIndex);
}
// decode array rank info
while (tokenIndex < endIndex && typeName[tokenIndex] == '[') {
int arrayEndIndex = typeName.IndexOf (']', tokenIndex, endIndex - tokenIndex);
if (arrayEndIndex == -1)
break;
arrayEndIndex++;
array += typeName.Substring (tokenIndex, arrayEndIndex - tokenIndex);
tokenIndex = arrayEndIndex;
}
string name = typeName.Substring (startIndex, typeEndIndex - startIndex);
if (genericArgs == null)
return GetShortTypeName (name) + array;
// Use the prettier name for nullable types
if (name == "System.Nullable" && genericArgs.Count == 1)
return genericArgs[0] + "?" + array;
// Insert the generic arguments next to each type.
// for example: Foo`1+Bar`1[System.Int32,System.String]
// is converted to: Foo<int>.Bar<string>
var builder = new StringBuilder (name);
int i = typeEndIndex + 1;
int genericIndex = 0;
int argCount, next;
while (i < genericEndIndex) {
// decode the argument count
argCount = 0;
while (i < genericEndIndex && char.IsDigit (typeName[i])) {
argCount = (argCount * 10) + (typeName[i] - '0');
i++;
}
// insert the argument types
builder.Append ('<');
while (argCount > 0 && genericIndex < genericArgs.Count) {
builder.Append (genericArgs[genericIndex++]);
if (--argCount > 0)
builder.Append (',');
}
builder.Append ('>');
// Find the end of the next generic type component
if ((next = typeName.IndexOf ('`', i, genericEndIndex - i)) == -1)
next = genericEndIndex;
// Append the next generic type component
builder.Append (typeName.Substring (i, next - i));
i = next + 1;
}
return builder + array;
}
List<string> GetGenericArguments (string typeName, ref int i, int endIndex)
{
// Get a list of the generic arguments.
// When returning, i points to the next char after the closing ']'
var genericArgs = new List<string> ();
i++;
while (i < endIndex && typeName [i] != ']') {
int pend = FindTypeEnd (typeName, i, endIndex);
bool escaped = typeName [i] == '[';
genericArgs.Add (GetDisplayTypeName (typeName, escaped ? i + 1 : i, escaped ? pend - 1 : pend));
i = pend;
if (i < endIndex && typeName[i] == ',')
i++;
}
i++;
return genericArgs;
}
static int FindTypeEnd (string typeName, int startIndex, int endIndex)
{
int i = startIndex;
int brackets = 0;
while (i < endIndex) {
char c = typeName[i];
if (c == '[') {
brackets++;
} else if (c == ']') {
if (brackets <= 0)
return i;
brackets--;
} else if (c == ',' && brackets == 0) {
return i;
}
i++;
}
return i;
}
public virtual string GetShortTypeName (string typeName)
{
int star = typeName.IndexOf ('*');
string name, ptr, csharp;
if (star != -1) {
name = typeName.Substring (0, star);
ptr = typeName.Substring (star);
} else {
ptr = string.Empty;
name = typeName;
}
if (CSharpTypeNames.TryGetValue (name, out csharp))
return csharp + ptr;
return typeName;
}
public virtual void OnBusyStateChanged (BusyStateEventArgs e)
{
EventHandler<BusyStateEventArgs> evnt = BusyStateChanged;
if (evnt != null)
evnt (this, e);
}
public abstract ICollectionAdaptor CreateArrayAdaptor (EvaluationContext ctx, object arr);
public abstract IStringAdaptor CreateStringAdaptor (EvaluationContext ctx, object str);
public abstract bool IsNull (EvaluationContext ctx, object val);
public abstract bool IsPrimitive (EvaluationContext ctx, object val);
public abstract bool IsPointer (EvaluationContext ctx, object val);
public abstract bool IsString (EvaluationContext ctx, object val);
public abstract bool IsArray (EvaluationContext ctx, object val);
public abstract bool IsEnum (EvaluationContext ctx, object val);
public abstract bool IsValueType (object type);
public abstract bool IsClass (EvaluationContext ctx, object type);
public abstract object TryCast (EvaluationContext ctx, object val, object type);
public abstract object GetValueType (EvaluationContext ctx, object val);
public abstract string GetTypeName (EvaluationContext ctx, object type);
public abstract object[] GetTypeArgs (EvaluationContext ctx, object type);
public abstract object GetBaseType (EvaluationContext ctx, object type);
public virtual bool IsGenericType (EvaluationContext ctx, object type)
{
return type != null && GetTypeName (ctx, type).IndexOf ('`') != -1;
}
public virtual bool IsNullableType (EvaluationContext ctx, object type)
{
return type != null && GetTypeName (ctx, type).StartsWith ("System.Nullable`1", StringComparison.Ordinal);
}
public virtual bool NullableHasValue (EvaluationContext ctx, object type, object obj)
{
ValueReference hasValue = GetMember (ctx, type, obj, "HasValue");
return (bool) hasValue.ObjectValue;
}
public virtual ValueReference NullableGetValue (EvaluationContext ctx, object type, object obj)
{
return GetMember (ctx, type, obj, "Value");
}
public virtual bool IsFlagsEnumType (EvaluationContext ctx, object type)
{
return true;
}
public virtual bool IsSafeToInvokeMethod (EvaluationContext ctx, object method, object obj)
{
return true;
}
public virtual IEnumerable<EnumMember> GetEnumMembers (EvaluationContext ctx, object type)
{
object longType = GetType (ctx, "System.Int64");
var tref = new TypeValueReference (ctx, type);
foreach (var cr in tref.GetChildReferences (ctx.Options)) {
var c = TryCast (ctx, cr.Value, longType);
if (c == null)
continue;
long val = (long) TargetObjectToObject (ctx, c);
var em = new EnumMember { Name = cr.Name, Value = val };
yield return em;
}
}
public object GetBaseType (EvaluationContext ctx, object type, bool includeObjectClass)
{
object bt = GetBaseType (ctx, type);
string tn = bt != null ? GetTypeName (ctx, bt) : null;
if (!includeObjectClass && bt != null && (tn == "System.Object" || tn == "System.ValueType"))
return null;
if (tn == "System.Enum")
return GetMembers (ctx, type, null, BindingFlags.GetField | BindingFlags.Instance | BindingFlags.Public).FirstOrDefault ()?.Type;
return bt;
}
public virtual bool IsClassInstance (EvaluationContext ctx, object val)
{
return IsClass (ctx, GetValueType (ctx, val));
}
public virtual bool IsExternalType (EvaluationContext ctx, object type)
{
return false;
}
public object GetType (EvaluationContext ctx, string name)
{
return GetType (ctx, name, null);
}
public abstract object GetType (EvaluationContext ctx, string name, object[] typeArgs);
public virtual string GetValueTypeName (EvaluationContext ctx, object val)
{
return GetTypeName (ctx, GetValueType (ctx, val));
}
public virtual object CreateTypeObject (EvaluationContext ctx, object type)
{
return default (object);
}
public virtual bool IsTypeLoaded (EvaluationContext ctx, string typeName)
{
var type = GetType (ctx, typeName);
return type != null && IsTypeLoaded (ctx, type);
}
public virtual bool IsTypeLoaded (EvaluationContext ctx, object type)
{
return true;
}
public virtual object ForceLoadType (EvaluationContext ctx, string typeName)
{
var type = GetType (ctx, typeName);
if (type == null || IsTypeLoaded (ctx, type))
return type;
return ForceLoadType (ctx, type) ? type : null;
}
public virtual bool ForceLoadType (EvaluationContext ctx, object type)
{
return true;
}
public abstract object CreateValue (EvaluationContext ctx, object value);
public abstract object CreateValue (EvaluationContext ctx, object type, params object[] args);
public abstract object CreateNullValue (EvaluationContext ctx, object type);
public virtual object GetBaseValue (EvaluationContext ctx, object val)
{
return val;
}
public virtual string[] GetImportedNamespaces (EvaluationContext ctx)
{
return new string[0];
}
public virtual void GetNamespaceContents (EvaluationContext ctx, string namspace, out string[] childNamespaces, out string[] childTypes)
{
childTypes = childNamespaces = new string[0];
}
protected virtual ObjectValue CreateObjectValueImpl (EvaluationContext ctx, IObjectValueSource source, ObjectPath path, object obj, ObjectValueFlags flags)
{
object type = obj != null ? GetValueType (ctx, obj) : null;
string typeName = type != null ? GetTypeName (ctx, type) : "";
if (obj == null || IsNull (ctx, obj))
return ObjectValue.CreateNullObject (source, path, GetDisplayTypeName (typeName), flags);
if (IsPrimitive (ctx, obj) || IsEnum (ctx,obj))
return ObjectValue.CreatePrimitive (source, path, GetDisplayTypeName (typeName), ctx.Evaluator.TargetObjectToExpression (ctx, obj), flags);
if (IsArray (ctx, obj))
return ObjectValue.CreateObject (source, path, GetDisplayTypeName (typeName), ctx.Evaluator.TargetObjectToExpression (ctx, obj), flags, null);
EvaluationResult tvalue = null;
TypeDisplayData tdata = null;
string tname;
if (IsNullableType (ctx, type)) {
if (NullableHasValue (ctx, type, obj)) {
ValueReference value = NullableGetValue (ctx, type, obj);
tdata = GetTypeDisplayData (ctx, value.Type);
obj = value.Value;
} else {
tdata = GetTypeDisplayData (ctx, type);
tvalue = new EvaluationResult ("null");
}
tname = GetDisplayTypeName (typeName);
} else {
tdata = GetTypeDisplayData (ctx, type);
if (!string.IsNullOrEmpty (tdata.TypeDisplayString) && ctx.Options.AllowDisplayStringEvaluation) {
try {
tname = EvaluateDisplayString (ctx, obj, tdata.TypeDisplayString);
} catch (MissingMemberException) {
// missing property or otherwise malformed DebuggerDisplay string
tname = GetDisplayTypeName (typeName);
}
} else {
tname = GetDisplayTypeName (typeName);
}
}
if (tvalue == null) {
if (!string.IsNullOrEmpty (tdata.ValueDisplayString) && ctx.Options.AllowDisplayStringEvaluation) {
try {
tvalue = new EvaluationResult (EvaluateDisplayString (ctx, obj, tdata.ValueDisplayString));
} catch (MissingMemberException) {
// missing property or otherwise malformed DebuggerDisplay string
tvalue = ctx.Evaluator.TargetObjectToExpression (ctx, obj);
}
} else {
tvalue = ctx.Evaluator.TargetObjectToExpression (ctx, obj);
}
}
ObjectValue oval = ObjectValue.CreateObject (source, path, tname, tvalue, flags, null);
if (!string.IsNullOrEmpty (tdata.NameDisplayString) && ctx.Options.AllowDisplayStringEvaluation) {
try {
oval.Name = EvaluateDisplayString (ctx, obj, tdata.NameDisplayString);
} catch (MissingMemberException) {
// missing property or otherwise malformed DebuggerDisplay string
}
}
return oval;
}
public ObjectValue[] GetObjectValueChildren (EvaluationContext ctx, IObjectSource objectSource, object obj, int firstItemIndex, int count)
{
return GetObjectValueChildren (ctx, objectSource, GetValueType (ctx, obj), obj, firstItemIndex, count, true);
}
public virtual ObjectValue[] GetObjectValueChildren (EvaluationContext ctx, IObjectSource objectSource, object type, object obj, int firstItemIndex, int count, bool dereferenceProxy)
{
if (obj is EvaluationResult)
return new ObjectValue[0];
if (IsArray (ctx, obj)) {
var agroup = new ArrayElementGroup (ctx, CreateArrayAdaptor (ctx, obj));
return agroup.GetChildren (ctx.Options);
}
if (IsPrimitive (ctx, obj))
return new ObjectValue[0];
if (IsNullableType (ctx, type)) {
if (NullableHasValue (ctx, type, obj)) {
ValueReference value = NullableGetValue (ctx, type, obj);
return GetObjectValueChildren (ctx, objectSource, value.Type, value.Value, firstItemIndex, count, dereferenceProxy);
}
return new ObjectValue[0];
}
bool showRawView = false;
// If there is a proxy, it has to show the members of the proxy
object proxy = obj;
if (dereferenceProxy) {
proxy = GetProxyObject (ctx, obj);
if (proxy != obj) {
type = GetValueType (ctx, proxy);
showRawView = true;
}
}
TypeDisplayData tdata = GetTypeDisplayData (ctx, type);
bool groupPrivateMembers = ctx.Options.GroupPrivateMembers || IsExternalType (ctx, type);
var values = new List<ObjectValue> ();
BindingFlags flattenFlag = ctx.Options.FlattenHierarchy ? (BindingFlags)0 : BindingFlags.DeclaredOnly;
BindingFlags nonPublicFlag = !(groupPrivateMembers || showRawView) ? BindingFlags.NonPublic : (BindingFlags) 0;
BindingFlags staticFlag = ctx.Options.GroupStaticMembers ? (BindingFlags)0 : BindingFlags.Static;
BindingFlags access = BindingFlags.Public | BindingFlags.Instance | flattenFlag | nonPublicFlag | staticFlag;
// Load all members to a list before creating the object values,
// to avoid problems with objects being invalidated due to evaluations in the target,
var list = new List<ValueReference> ();
list.AddRange (GetMembersSorted (ctx, objectSource, type, proxy, access));
// Some implementations of DebuggerProxies(showRawView==true) only have private members
if (showRawView && list.Count == 0) {
list.AddRange (GetMembersSorted (ctx, objectSource, type, proxy, access | BindingFlags.NonPublic));
}
var names = new ObjectValueNameTracker (ctx);
object tdataType = type;
foreach (ValueReference val in list) {
try {
object decType = val.DeclaringType;
if (decType != null && decType != tdataType) {
tdataType = decType;
tdata = GetTypeDisplayData (ctx, decType);
}
DebuggerBrowsableState state = tdata.GetMemberBrowsableState (val.Name);
if (state == DebuggerBrowsableState.Never)
continue;
if (state == DebuggerBrowsableState.RootHidden && dereferenceProxy) {
object ob = val.Value;
if (ob != null) {
values.Clear ();
values.AddRange (GetObjectValueChildren (ctx, val, ob, -1, -1));
showRawView = true;
break;
}
} else {
ObjectValue oval = val.CreateObjectValue (true);
names.Disambiguate (val, oval);
values.Add (oval);
}
} catch (Exception ex) {
ctx.WriteDebuggerError (ex);
values.Add (ObjectValue.CreateError (null, new ObjectPath (val.Name), GetDisplayTypeName (GetTypeName (ctx, val.Type)), ex.Message, val.Flags));
}
}
if (showRawView) {
values.Add (RawViewSource.CreateRawView (ctx, objectSource, obj));
} else {
if (IsArray (ctx, proxy)) {
var col = CreateArrayAdaptor (ctx, proxy);
var agroup = new ArrayElementGroup (ctx, col);
var val = ObjectValue.CreateObject (null, new ObjectPath ("Raw View"), "", "", ObjectValueFlags.ReadOnly, values.ToArray ());
values = new List<ObjectValue> ();
values.Add (val);
values.AddRange (agroup.GetChildren (ctx.Options));
} else {
if (ctx.Options.GroupStaticMembers && HasMembers (ctx, type, proxy, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | flattenFlag)) {
access = BindingFlags.Static | BindingFlags.Public | flattenFlag | nonPublicFlag;
values.Add (FilteredMembersSource.CreateStaticsNode (ctx, objectSource, type, proxy, access));
}
if (groupPrivateMembers && HasMembers (ctx, type, proxy, BindingFlags.Instance | BindingFlags.NonPublic | flattenFlag | staticFlag))
values.Add (FilteredMembersSource.CreateNonPublicsNode (ctx, objectSource, type, proxy, BindingFlags.Instance | BindingFlags.NonPublic | flattenFlag | staticFlag));
if (!ctx.Options.FlattenHierarchy) {
object baseType = GetBaseType (ctx, type, false);
if (baseType != null)
values.Insert (0, BaseTypeViewSource.CreateBaseTypeView (ctx, objectSource, baseType, proxy));
}
if (ctx.SupportIEnumerable) {
var iEnumerableType = GetImplementedInterfaces (ctx, type).FirstOrDefault ((interfaceType) => {
string interfaceName = GetTypeName (ctx, interfaceType);
if (interfaceName == "System.Collections.IEnumerable")
return true;
if (interfaceName == "System.Collections.Generic.IEnumerable`1")
return true;
return false;
});
if (iEnumerableType != null)
values.Add (ObjectValue.CreatePrimitive (new EnumerableSource (proxy, iEnumerableType, ctx), new ObjectPath ("IEnumerator"), "", new EvaluationResult (""), ObjectValueFlags.ReadOnly | ObjectValueFlags.Object | ObjectValueFlags.Group | ObjectValueFlags.IEnumerable));
}
}
}
return values.ToArray ();
}
public ObjectValue[] GetExpressionValuesAsync (EvaluationContext ctx, string[] expressions)
{
var values = new ObjectValue[expressions.Length];
for (int n = 0; n < values.Length; n++) {
string exp = expressions[n];
// This is a workaround to a bug in mono 2.0. That mono version fails to compile
// an anonymous method here
var edata = new ExpData (ctx, exp, this);
values[n] = asyncEvaluationTracker.Run (exp, ObjectValueFlags.Literal, edata.Run);
}
return values;
}
class ExpData
{
readonly ObjectValueAdaptor adaptor;
readonly EvaluationContext ctx;
readonly string exp;
public ExpData (EvaluationContext ctx, string exp, ObjectValueAdaptor adaptor)
{
this.ctx = ctx;
this.exp = exp;
this.adaptor = adaptor;
}
public ObjectValue Run ()
{
return adaptor.GetExpressionValue (ctx, exp);
}
}
public virtual ValueReference GetIndexerReference (EvaluationContext ctx, object target, object[] indices)
{
return null;
}
public virtual ValueReference GetIndexerReference (EvaluationContext ctx, object target, object type, object[] indices)
{
return GetIndexerReference (ctx, target, indices);
}
public ValueReference GetLocalVariable (EvaluationContext ctx, string name)
{
return OnGetLocalVariable (ctx, name);
}
protected virtual ValueReference OnGetLocalVariable (EvaluationContext ctx, string name)
{
ValueReference best = null;
foreach (ValueReference var in GetLocalVariables (ctx)) {
if (var.Name == name)
return var;
if (!ctx.Evaluator.CaseSensitive && var.Name.Equals (name, StringComparison.CurrentCultureIgnoreCase))
best = var;
}
return best;
}
public virtual ValueReference GetParameter (EvaluationContext ctx, string name)
{
return OnGetParameter (ctx, name);
}
protected virtual ValueReference OnGetParameter (EvaluationContext ctx, string name)
{
ValueReference best = null;
foreach (ValueReference var in GetParameters (ctx)) {
if (var.Name == name)
return var;
if (!ctx.Evaluator.CaseSensitive && var.Name.Equals (name, StringComparison.CurrentCultureIgnoreCase))
best = var;
}
return best;
}
public IEnumerable<ValueReference> GetLocalVariables (EvaluationContext ctx)
{
return OnGetLocalVariables (ctx);
}
public ValueReference GetThisReference (EvaluationContext ctx)
{
return OnGetThisReference (ctx);
}
public IEnumerable<ValueReference> GetParameters (EvaluationContext ctx)
{
return OnGetParameters (ctx);
}
protected virtual IEnumerable<ValueReference> OnGetLocalVariables (EvaluationContext ctx)
{
yield break;
}
protected virtual IEnumerable<ValueReference> OnGetParameters (EvaluationContext ctx)
{
yield break;
}
protected virtual ValueReference OnGetThisReference (EvaluationContext ctx)
{
return null;
}
public virtual ValueReference GetCurrentException (EvaluationContext ctx)
{
return null;
}
public virtual object GetEnclosingType (EvaluationContext ctx)
{
return null;
}
protected virtual CompletionData GetMemberCompletionData (EvaluationContext ctx, ValueReference vr)
{
var data = new CompletionData ();
foreach (var cv in vr.GetChildReferences (ctx.Options))
data.Items.Add (new CompletionItem (cv.Name, cv.Flags));
data.ExpressionLength = 0;
return data;
}
public virtual CompletionData GetExpressionCompletionData (EvaluationContext ctx, string expr)
{
if (string.IsNullOrEmpty (expr))
return null;
int dot = expr.LastIndexOf ('.');
if (dot != -1) {
try {
var vr = ctx.Evaluator.Evaluate (ctx, expr.Substring (0, dot), null);
if (vr != null) {
var completionData = GetMemberCompletionData (ctx, vr);
completionData.ExpressionLength = expr.Length - dot - 1;
return completionData;
}
// FIXME: handle types and namespaces...
} catch (Exception ex) {
ctx.WriteDebuggerError (ex);
}
return null;
}
bool lastWastLetter = false;
int i = expr.Length - 1;
while (i >= 0) {
char c = expr[i--];
if (!char.IsLetterOrDigit (c) && c != '_')
break;
lastWastLetter = !char.IsDigit (c);
}
if (lastWastLetter) {
string partialWord = expr.Substring (i + 1);
var data = new CompletionData ();
data.ExpressionLength = partialWord.Length;
// Local variables
foreach (var vc in GetLocalVariables (ctx)) {
if (vc.Name.StartsWith (partialWord, StringComparison.InvariantCulture))
data.Items.Add (new CompletionItem (vc.Name, vc.Flags));
}
// Parameters
foreach (var vc in GetParameters (ctx)) {
if (vc.Name.StartsWith (partialWord, StringComparison.InvariantCulture))
data.Items.Add (new CompletionItem (vc.Name, vc.Flags));
}
// Members
ValueReference thisobj = GetThisReference (ctx);
if (thisobj != null)
data.Items.Add (new CompletionItem ("this", ObjectValueFlags.Field | ObjectValueFlags.ReadOnly));
object type = GetEnclosingType (ctx);
foreach (var vc in GetMembers (ctx, null, type, thisobj != null ? thisobj.Value : null)) {
if (vc.Name.StartsWith (partialWord, StringComparison.InvariantCulture))
data.Items.Add (new CompletionItem (vc.Name, vc.Flags));
}
if (data.Items.Count > 0)
return data;
}
return null;
}
public IEnumerable<ValueReference> GetMembers (EvaluationContext ctx, IObjectSource objectSource, object t, object co)
{
foreach (ValueReference val in GetMembers (ctx, t, co, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)) {
val.ParentSource = objectSource;
yield return val;
}
}
public ValueReference GetMember (EvaluationContext ctx, IObjectSource objectSource, object co, string name)
{
return GetMember (ctx, objectSource, GetValueType (ctx, co), co, name);
}
public ValueReference GetMember (EvaluationContext ctx, IObjectSource objectSource, object t, object co, string name)
{
ValueReference m = GetMember (ctx, t, co, name);
if (m != null)
m.ParentSource = objectSource;
return m;
}
protected virtual ValueReference GetMember (EvaluationContext ctx, object t, object co, string name)
{
ValueReference best = null;
foreach (ValueReference var in GetMembers (ctx, t, co, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)) {
if (var.Name == name)
return var;
if (!ctx.Evaluator.CaseSensitive && var.Name.Equals (name, StringComparison.CurrentCultureIgnoreCase))
best = var;
}
return best;
}
internal IEnumerable<ValueReference> GetMembersSorted (EvaluationContext ctx, IObjectSource objectSource, object t, object co)
{
return GetMembersSorted (ctx, objectSource, t, co, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
}
internal IEnumerable<ValueReference> GetMembersSorted (EvaluationContext ctx, IObjectSource objectSource, object t, object co, BindingFlags bindingFlags)
{
var list = new List<ValueReference> ();
foreach (var vr in GetMembers (ctx, t, co, bindingFlags)) {
vr.ParentSource = objectSource;
list.Add (vr);
}
list.Sort ((v1, v2) => string.Compare (v1.Name, v2.Name, StringComparison.Ordinal));
return list;
}
public bool HasMembers (EvaluationContext ctx, object t, object co, BindingFlags bindingFlags)
{
return GetMembers (ctx, t, co, bindingFlags).Any ();
}
public bool HasMember (EvaluationContext ctx, object type, string memberName)
{
return HasMember (ctx, type, memberName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
}
public abstract bool HasMember (EvaluationContext ctx, object type, string memberName, BindingFlags bindingFlags);
/// <summary>
/// Returns all members of a type. The following binding flags have to be honored:
/// BindingFlags.Static, BindingFlags.Instance, BindingFlags.Public, BindingFlags.NonPublic, BindingFlags.DeclareOnly
/// </summary>
protected abstract IEnumerable<ValueReference> GetMembers (EvaluationContext ctx, object t, object co, BindingFlags bindingFlags);
public virtual IEnumerable<object> GetNestedTypes (EvaluationContext ctx, object type)
{
yield break;
}
public virtual IEnumerable<object> GetImplementedInterfaces (EvaluationContext ctx, object type)
{
yield break;
}
public virtual object GetParentType (EvaluationContext ctx, object type)
{
var tt = type as Type;
if (tt != null)
return tt.DeclaringType;
var name = GetTypeName (ctx, type);
int plus = name.LastIndexOf ('+');
return plus != -1 ? GetType (ctx, name.Substring (0, plus)) : null;
}
public virtual object CreateArray (EvaluationContext ctx, object type, object[] values)
{
var arrType = GetType (ctx, "System.Collections.ArrayList");
var arrayList = CreateValue (ctx, arrType, new object[0]);
object[] objTypes = { GetType (ctx, "System.Object") };
foreach (object value in values)
RuntimeInvoke (ctx, arrType, arrayList, "Add", objTypes, new [] { value });
var typof = CreateTypeObject (ctx, type);
objTypes = new [] { GetType (ctx, "System.Type") };
return RuntimeInvoke (ctx, arrType, arrayList, "ToArray", objTypes, new [] { typof });
}
public virtual object ToRawValue (EvaluationContext ctx, IObjectSource source, object obj)
{
if (IsEnum (ctx, obj)) {
var longType = GetType (ctx, "System.Int64");
var c = Cast (ctx, obj, longType);
return TargetObjectToObject (ctx, c);
}
if (ctx.Options.ChunkRawStrings && IsString (ctx, obj)) {
var adaptor = CreateStringAdaptor (ctx, obj);
return new RawValueString (new RemoteRawValueString (adaptor, obj));
}
if (IsPrimitive (ctx, obj))
return TargetObjectToObject (ctx, obj);
if (IsArray (ctx, obj)) {
var adaptor = CreateArrayAdaptor (ctx, obj);
return new RawValueArray (new RemoteRawValueArray (ctx, source, adaptor, obj));
}
return new RawValue (new RemoteRawValue (ctx, source, obj));
}
public virtual object FromRawValue (EvaluationContext ctx, object obj)
{
var rawValue = obj as RawValue;
if (rawValue != null) {
var val = rawValue.Source as RemoteRawValue;
if (val == null)
throw new InvalidOperationException ("Unknown RawValue source: " + rawValue.Source);
return val.TargetObject;
}
var rawArray = obj as RawValueArray;