~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.Debugger.Soft/Mono.Debugging.Soft/SoftDebuggerAdaptor.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
// THE SOFTWARE.
26
26
 
27
27
using System;
 
28
using System.Linq;
28
29
using System.Diagnostics;
29
30
using Mono.Debugger.Soft;
30
31
using Mono.Debugging.Evaluation;
247
248
                        int i = candidates.IndexOf (idx);
248
249
                        return new PropertyValueReference (ctx, props[i], target, null, values);
249
250
                }
 
251
                
 
252
                bool IsCompilerGeneratedType (SoftEvaluationContext cx, TypeMirror tm)
 
253
                {
 
254
                        return tm.Name.IndexOf (">c__AnonStorey") != -1 || tm.Name.IndexOf (">c__Iterator") != -1;
 
255
                }
 
256
                
 
257
                bool InCompilerGeneratedType (EvaluationContext ctx)
 
258
                {
 
259
                        SoftEvaluationContext cx = (SoftEvaluationContext) ctx;
 
260
                        if (cx.Frame.Method.IsStatic)
 
261
                                return false;
 
262
                        TypeMirror tm = cx.Frame.Method.DeclaringType;
 
263
                        return IsCompilerGeneratedType (cx, tm);
 
264
                }
250
265
 
251
 
                public override ValueReference GetLocalVariable (EvaluationContext ctx, string name)
 
266
                IEnumerable<ValueReference> GetCompilerGeneratedLocalVariables (SoftEvaluationContext cx, ValueReference vthis)
252
267
                {
253
 
                        try {
254
 
                                SoftEvaluationContext cx = (SoftEvaluationContext) ctx;
255
 
                                LocalVariable local = cx.Frame.GetVisibleVariableByName (name);
256
 
                                if (local != null)
257
 
                                        return new VariableValueReference (ctx, name, local);
 
268
                        if (vthis == null)
 
269
                                return new ValueReference [0];
 
270
                        
 
271
                        object val = vthis.Value;
 
272
                        if (IsNull (cx, val))
 
273
                                return new ValueReference [0];
 
274
                        
 
275
                        TypeMirror tm = (TypeMirror) vthis.Type;
 
276
                        bool isIterator = tm.Name.IndexOf (">c__Iterator") != -1;
 
277
                        
 
278
                        var list = new List<ValueReference> ();
 
279
                        TypeMirror type = (TypeMirror) vthis.Type;
 
280
                        foreach (FieldInfoMirror field in type.GetFields ()) {
 
281
                                if (field.Name == "<>f__this")
 
282
                                        continue;
 
283
                                if (field.Name.StartsWith ("<>f__ref")) {
 
284
                                        list.AddRange (GetCompilerGeneratedLocalVariables (cx, new FieldValueReference (cx, field, val, type)));
 
285
                                        continue;
 
286
                                }
 
287
                                if (field.Name.StartsWith ("<")) {
 
288
                                        if (isIterator) {
 
289
                                                int i = field.Name.IndexOf ('>');
 
290
                                                if (i > 1) {
 
291
                                                        string vname = field.Name.Substring (1, i - 1);
 
292
                                                        list.Add (new FieldValueReference (cx, field, val, type, vname, ObjectValueFlags.Variable));
 
293
                                                }
 
294
                                        }
 
295
                                }
258
296
                                else
259
 
                                        return null;
 
297
                                        list.Add (new FieldValueReference (cx, field, val, type, field.Name, ObjectValueFlags.Variable));
 
298
                        }
 
299
                        return list;
 
300
                }
 
301
                
 
302
                ValueReference GetCompilerGeneratedThisReference (SoftEvaluationContext cx)
 
303
                {
 
304
                        try {
 
305
                                Value val = cx.Frame.GetThis ();
 
306
                                TypeMirror type = (TypeMirror) GetValueType (cx, val);
 
307
                                FieldInfoMirror field = type.GetField ("<>f__this");
 
308
                                if (field != null)
 
309
                                        return new FieldValueReference (cx, field, val, type, "this", ObjectValueFlags.Literal);
 
310
                        } catch (AbsentInformationException) {
 
311
                        }
 
312
                        return null;
 
313
                }
 
314
                
 
315
                protected override ValueReference OnGetLocalVariable (EvaluationContext ctx, string name)
 
316
                {
 
317
                        SoftEvaluationContext cx = (SoftEvaluationContext) ctx;
 
318
                        if (InCompilerGeneratedType (cx))
 
319
                                return FindByName (OnGetLocalVariables (cx), v => v.Name, name, ctx.CaseSensitive);
 
320
                        
 
321
                        try {
 
322
                                LocalVariable local = null;
 
323
                                if (!cx.SourceCodeAvailable) {
 
324
                                        if (name.StartsWith ("loc")) {
 
325
                                                int idx;
 
326
                                                if (int.TryParse (name.Substring (3), out idx))
 
327
                                                        local = cx.Frame.Method.GetLocals ().FirstOrDefault (loc => loc.Index == idx);
 
328
                                        }
 
329
                                } else
 
330
                                        local = ctx.CaseSensitive ? cx.Frame.GetVisibleVariableByName (name) : FindByName (cx.Frame.GetVisibleVariables(), v => v.Name, name, false);
 
331
                                
 
332
                                if (local != null) {
 
333
                                        string vname = !string.IsNullOrEmpty (local.Name) || cx.SourceCodeAvailable ? local.Name : "loc" + local.Index;
 
334
                                        return new VariableValueReference (ctx, vname, local);
 
335
                                }
 
336
                                return FindByName (OnGetLocalVariables (ctx), v => v.Name, name, ctx.CaseSensitive);;
260
337
                        } catch (AbsentInformationException) {
261
338
                                return null;
262
339
                        }
263
340
                }
264
341
 
265
 
                public override IEnumerable<ValueReference> GetLocalVariables (EvaluationContext ctx)
 
342
                protected override IEnumerable<ValueReference> OnGetLocalVariables (EvaluationContext ctx)
266
343
                {
267
344
                        SoftEvaluationContext cx = (SoftEvaluationContext) ctx;
 
345
                        if (InCompilerGeneratedType (cx)) {
 
346
                                ValueReference vthis = GetThisReference (cx);
 
347
                                return GetCompilerGeneratedLocalVariables (cx, vthis).Union (GetLocalVariables (cx));
 
348
                        }
 
349
                        else
 
350
                                return GetLocalVariables (cx);
 
351
                }
 
352
                
 
353
                IEnumerable<ValueReference> GetLocalVariables (SoftEvaluationContext cx)
 
354
                {
268
355
                        IList<LocalVariable> locals;
269
356
                        try {
270
357
                                locals = cx.Frame.GetVisibleVariables ();
272
359
                                yield break;
273
360
                        }
274
361
                        foreach (LocalVariable var in locals) {
275
 
                                if (!var.IsArg)
276
 
                                        yield return new VariableValueReference (ctx, var.Name, var);
 
362
                                if (!var.IsArg) {
 
363
                                        string name = !string.IsNullOrEmpty (var.Name) || cx.SourceCodeAvailable ? var.Name : "loc" + var.Index;
 
364
                                        if (name.Length == 0 || name.StartsWith ("<")) {
 
365
                                                if (IsCompilerGeneratedType (cx, var.Type)) {
 
366
                                                        foreach (var gv in GetCompilerGeneratedLocalVariables (cx, new VariableValueReference (cx, name, var)))
 
367
                                                                yield return gv;
 
368
                                                }
 
369
                                        }
 
370
                                        else
 
371
                                                yield return new VariableValueReference (cx, name, var);
 
372
                                }
277
373
                        }
278
374
                }
279
375
 
281
377
                {
282
378
                        TypeMirror type = (TypeMirror) t;
283
379
                        while (type != null) {
284
 
                                FieldInfoMirror field = type.GetField (name);
285
 
                                if (field != null)
 
380
                                FieldInfoMirror field = FindByName (type.GetFields(), f => f.Name, name, ctx.CaseSensitive);
 
381
                                if (field != null && (field.IsStatic || co != null))
286
382
                                        return new FieldValueReference (ctx, field, co, type);
287
 
                                PropertyInfoMirror prop = type.GetProperty (name);
288
 
                                if (prop != null)
 
383
                                PropertyInfoMirror prop = FindByName (type.GetProperties(), p => p.Name, name, ctx.CaseSensitive);
 
384
                                if (prop != null && (IsStatic (prop) || co != null))
289
385
                                        return new PropertyValueReference (ctx, prop, co, type, null);
290
386
                                type = type.BaseType;
291
387
                        }
292
388
                        return null;
293
389
                }
294
 
 
 
390
                
 
391
                bool IsStatic (PropertyInfoMirror prop)
 
392
                {
 
393
                        MethodMirror met = prop.GetGetMethod (true) ?? prop.GetSetMethod (true);
 
394
                        return met.IsStatic;
 
395
                }
 
396
                
 
397
                static T FindByName<T> (IEnumerable<T> elems, Func<T,string> getName, string name, bool caseSensitive)
 
398
                {
 
399
                        T best = default(T);
 
400
                        foreach (T t in elems) {
 
401
                                string n = getName (t);
 
402
                                if (n == name) 
 
403
                                        return t;
 
404
                                else if (!caseSensitive && n.Equals (name, StringComparison.CurrentCultureIgnoreCase))
 
405
                                        best = t;
 
406
                        }
 
407
                        return best;
 
408
                }
 
409
                
295
410
                protected override IEnumerable<ValueReference> GetMembers (EvaluationContext ctx, object t, object co, BindingFlags bindingFlags)
296
411
                {
297
412
                        Dictionary<string, PropertyInfoMirror> subProps = new Dictionary<string, PropertyInfoMirror> ();
372
487
                        types.CopyTo (childTypes);
373
488
                }
374
489
 
375
 
                public override IEnumerable<ValueReference> GetParameters (EvaluationContext ctx)
 
490
                protected override IEnumerable<ValueReference> OnGetParameters (EvaluationContext ctx)
376
491
                {
377
492
                        SoftEvaluationContext cx = (SoftEvaluationContext) ctx;
378
493
                        LocalVariable[] locals;
383
498
                        }
384
499
                                
385
500
                        foreach (LocalVariable var in locals) {
386
 
                                if (var.IsArg)
387
 
                                        yield return new VariableValueReference (ctx, var.Name, var);
 
501
                                if (var.IsArg) {
 
502
                                        string name = !string.IsNullOrEmpty (var.Name) || cx.SourceCodeAvailable ? var.Name : "arg" + var.Index;
 
503
                                        yield return new VariableValueReference (ctx, name, var);
 
504
                                }
388
505
                        }
389
506
                }
390
507
 
391
 
                public override ValueReference GetThisReference (EvaluationContext ctx)
 
508
                protected override ValueReference OnGetThisReference (EvaluationContext ctx)
 
509
                {
 
510
                        SoftEvaluationContext cx = (SoftEvaluationContext) ctx;
 
511
                        if (InCompilerGeneratedType (cx))
 
512
                                return GetCompilerGeneratedThisReference (cx);
 
513
                        else
 
514
                                return GetThisReference (cx);
 
515
                }
 
516
                
 
517
                ValueReference GetThisReference (SoftEvaluationContext cx)
392
518
                {
393
519
                        try {
394
 
                                SoftEvaluationContext cx = (SoftEvaluationContext) ctx;
395
520
                                if (cx.Frame.Method.IsStatic)
396
521
                                        return null;
397
522
                                Value val = cx.Frame.GetThis ();
398
 
                                return LiteralValueReference.CreateTargetObjectLiteral (ctx, "this", val);
 
523
                                return LiteralValueReference.CreateTargetObjectLiteral (cx, "this", val);
399
524
                        } catch (AbsentInformationException) {
400
525
                                return null;
401
526
                        }
518
643
                        else
519
644
                                return ((Type)val).FullName;
520
645
                }
521
 
 
 
646
                
522
647
                public override object GetValueType (EvaluationContext ctx, object val)
523
648
                {
524
649
                        if (val is ObjectMirror)
608
733
                protected override TypeDisplayData OnGetTypeDisplayData (EvaluationContext gctx, object type)
609
734
                {
610
735
                        SoftEvaluationContext ctx = (SoftEvaluationContext) gctx;
611
 
                        TypeDisplayData td = new TypeDisplayData ();
 
736
                        
 
737
                        bool isCompilerGenerated = false;
 
738
                        string nameString = null;
 
739
                        string typeString = null;
 
740
                        string valueString = null;
 
741
                        string proxyType = null;
 
742
                        Dictionary<string, DebuggerBrowsableState> memberData = null;
 
743
                        
612
744
                        try {
613
745
                                TypeMirror t = (TypeMirror) type;
614
746
                                foreach (CustomAttributeDataMirror attr in t.GetCustomAttributes (true)) {
615
747
                                        string attName = attr.Constructor.DeclaringType.FullName;
616
748
                                        if (attName == "System.Diagnostics.DebuggerDisplayAttribute") {
617
749
                                                DebuggerDisplayAttribute at = BuildAttribute<DebuggerDisplayAttribute> (attr);
618
 
                                                td.NameDisplayString = at.Name;
619
 
                                                td.TypeDisplayString = at.Type;
620
 
                                                td.ValueDisplayString = at.Value;
 
750
                                                nameString = at.Name;
 
751
                                                typeString = at.Type;
 
752
                                                valueString = at.Value;
621
753
                                        }
622
754
                                        else if (attName == "System.Diagnostics.DebuggerTypeProxyAttribute") {
623
755
                                                DebuggerTypeProxyAttribute at = BuildAttribute<DebuggerTypeProxyAttribute> (attr);
624
 
                                                td.ProxyType = at.ProxyTypeName;
625
 
                                                if (!string.IsNullOrEmpty (td.ProxyType))
626
 
                                                        ForceLoadType (ctx, td.ProxyType);
 
756
                                                proxyType = at.ProxyTypeName;
 
757
                                                if (!string.IsNullOrEmpty (proxyType))
 
758
                                                        ForceLoadType (ctx, proxyType);
627
759
                                        }
 
760
                                        else if (attName == "System.Runtime.CompilerServices.CompilerGeneratedAttribute")
 
761
                                                isCompilerGenerated = true;
628
762
                                }
629
763
                                foreach (FieldInfoMirror fi in t.GetFields ()) {
630
764
                                        CustomAttributeDataMirror[] attrs = fi.GetCustomAttributes (true);
635
769
                                                        att = new DebuggerBrowsableAttribute (DebuggerBrowsableState.Never);
636
770
                                        }
637
771
                                        if (att != null) {
638
 
                                                if (td.MemberData == null)
639
 
                                                        td.MemberData = new Dictionary<string, DebuggerBrowsableState> ();
640
 
                                                td.MemberData [fi.Name] = att.State;
 
772
                                                if (memberData == null)
 
773
                                                        memberData = new Dictionary<string, DebuggerBrowsableState> ();
 
774
                                                memberData [fi.Name] = att.State;
641
775
                                        }
642
776
                                }
643
777
                                foreach (PropertyInfoMirror pi in t.GetProperties ()) {
644
778
                                        DebuggerBrowsableAttribute att = GetAttribute <DebuggerBrowsableAttribute> (pi.GetCustomAttributes (true));
645
779
                                        if (att != null) {
646
 
                                                if (td.MemberData == null)
647
 
                                                        td.MemberData = new Dictionary<string, DebuggerBrowsableState> ();
648
 
                                                td.MemberData [pi.Name] = att.State;
 
780
                                                if (memberData == null)
 
781
                                                        memberData = new Dictionary<string, DebuggerBrowsableState> ();
 
782
                                                memberData [pi.Name] = att.State;
649
783
                                        }
650
784
                                }
651
785
                        } catch (Exception ex) {
652
786
                                ctx.Session.WriteDebuggerOutput (true, ex.ToString ());
653
787
                        }
654
 
                        return td;
 
788
                        return new TypeDisplayData (proxyType, valueString, typeString, nameString, isCompilerGenerated, memberData);
655
789
                }
656
 
                                            
 
790
                
657
791
                T GetAttribute<T> (CustomAttributeDataMirror[] attrs)
658
792
                {
659
793
                        foreach (CustomAttributeDataMirror attr in attrs) {
759
893
                        TypeMirror currentType = type;
760
894
                        while (currentType != null) {
761
895
                                foreach (MethodMirror met in currentType.GetMethods ()) {
762
 
                                        if (met.Name == methodName) {
 
896
                                        if (met.Name == methodName || (!ctx.CaseSensitive && met.Name.Equals (methodName, StringComparison.CurrentCultureIgnoreCase))) {
763
897
                                                ParameterInfoMirror[] pars = met.GetParameters ();
764
898
                                                if (pars.Length == argtypes.Length && (met.IsStatic && allowStatic || !met.IsStatic && allowInstance))
765
899
                                                        candidates.Add (met);
938
1072
                                var info = GetInfo ();
939
1073
                                LoggingService.LogMessage ("Aborting invocation of " + info);
940
1074
                                ((IInvokeAsyncResult) handle).Abort ();
941
 
                                WaitForCompleted (-1);
942
 
                                LoggingService.LogMessage ("Aborted invocation of " + info);
 
1075
                                // Don't wait for the abort to finish. The engine will do it.
943
1076
                        } else {
944
1077
                                throw new NotSupportedException ();
945
1078
                        }
960
1093
                                else
961
1094
                                        result = ((StructMirror)obj).EndInvokeMethod (handle);
962
1095
                        } catch (InvocationException ex) {
963
 
                                if (ex.Exception != null) {
 
1096
                                if (!Aborting && ex.Exception != null) {
964
1097
                                        string ename = ctx.Adapter.GetValueTypeName (ctx, ex.Exception);
965
1098
                                        ValueReference vref = ctx.Adapter.GetMember (ctx, null, ex.Exception, "Message");
966
1099
                                        if (vref != null) {