* Makefile.am:
[monodevelop.git] / extras / MonoDevelop.Debugger.Mdb / Mono.Debugging.Server.Mdb / ValueReference.cs
bloba224fd84b9dbd8efe04dbd80da7b3eba4e271398
1 // ValueReference.cs
2 //
3 // Author:
4 // Lluis Sanchez Gual <lluis@novell.com>
5 //
6 // Copyright (c) 2008 Novell, Inc (http://www.novell.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 // THE SOFTWARE.
28 using System;
29 using System.Collections.Generic;
30 using Mono.Debugger.Languages;
31 using Mono.Debugger;
32 using Mono.Debugging.Client;
33 using Mono.Debugging.Backend;
34 using MD = Mono.Debugger;
35 using DC = Mono.Debugging.Client;
37 namespace DebuggerServer
39 public abstract class ValueReference: RemoteFrameObject, IObjectValueSource
41 EvaluationContext ctx;
43 public ValueReference (EvaluationContext ctx)
45 this.ctx = ctx;
48 public virtual object ObjectValue {
49 get {
50 TargetObject ob = Value;
51 if (ob is TargetFundamentalObject) {
52 TargetFundamentalObject fob = (TargetFundamentalObject) ob;
53 return fob.GetObject (ctx.Thread);
54 } else
55 return ob;
59 public abstract TargetObject Value { get; set; }
60 public abstract string Name { get; }
61 public abstract TargetType Type { get; }
62 public abstract ObjectValueFlags Flags { get; }
64 public EvaluationContext Context {
65 get {
66 return ctx;
70 public ObjectValue CreateObjectValue (bool withTimeout)
72 if (withTimeout) {
73 return Server.Instance.AsyncEvaluationTracker.Run (Name, Flags, delegate {
74 return CreateObjectValue ();
75 });
76 } else
77 return CreateObjectValue ();
80 public ObjectValue CreateObjectValue ()
82 Connect ();
83 try {
84 return OnCreateObjectValue ();
85 } catch (NotSupportedExpressionException ex) {
86 return DC.ObjectValue.CreateNotSupported (Name, ex.Message, Flags);
87 } catch (EvaluatorException ex) {
88 return DC.ObjectValue.CreateError (Name, ex.Message, Flags);
89 } catch (Exception ex) {
90 Server.Instance.WriteDebuggerError (ex);
91 return DC.ObjectValue.CreateUnknown (Name);
95 protected virtual ObjectValue OnCreateObjectValue ()
97 string name = Name;
98 if (string.IsNullOrEmpty (name))
99 name = "?";
101 TargetObject val = Value;
102 if (val != null)
103 return Util.CreateObjectValue (ctx, this, new ObjectPath (name), Value, Flags);
104 else
105 return Mono.Debugging.Client.ObjectValue.CreateNullObject (name, Type.Name, Flags);
108 public string SetValue (ObjectPath path, string value)
110 try {
111 Server.Instance.WaitRuntimeInvokes ();
112 EvaluationOptions ops = new EvaluationOptions ();
113 ops.ExpectedType = Type;
114 ops.CanEvaluateMethods = true;
115 ValueReference vref = Server.Instance.Evaluator.Evaluate (ctx, value, ops);
116 TargetObject newValue = vref.Value;
117 newValue = TargetObjectConvert.Cast (ctx, newValue, Type);
118 Value = newValue;
119 } catch (Exception ex) {
120 Server.Instance.WriteDebuggerError (ex);
121 Server.Instance.WriteDebuggerOutput ("Value assignment failed: {0}: {1}\n", ex.GetType (), ex.Message);
124 try {
125 return Server.Instance.Evaluator.TargetObjectToExpression (ctx, Value);
126 } catch (Exception ex) {
127 Server.Instance.WriteDebuggerError (ex);
128 Server.Instance.WriteDebuggerOutput ("Value assignment failed: {0}: {1}\n", ex.GetType (), ex.Message);
131 return value;
134 public virtual string CallToString ()
136 object val = ObjectValue;
137 if (val is TargetStructObject)
138 return ObjectUtil.CallToString (ctx, (TargetStructObject) val);
139 else if (val == null)
140 return string.Empty;
141 else
142 return val.ToString ();
145 public virtual ObjectValue[] GetChildren (ObjectPath path, int index, int count)
147 try {
148 return Util.GetObjectValueChildren (GetChildrenContext (), Value, index, count);
149 } catch (Exception ex) {
150 Console.WriteLine (ex);
151 return new ObjectValue [] { Mono.Debugging.Client.ObjectValue.CreateError ("", ex.Message, ObjectValueFlags.ReadOnly) };
155 public virtual IEnumerable<ValueReference> GetChildReferences ()
157 try {
158 TargetStructObject val = Value as TargetStructObject;
159 if (val != null)
160 return Util.GetMembers (GetChildrenContext (), val.Type, val);
161 } catch {
162 // Ignore
164 return new ValueReference [0];
167 EvaluationContext GetChildrenContext ()
169 int to = DebuggerServer.DefaultChildEvaluationTimeout;
170 return new EvaluationContext (ctx.Thread, ctx.Frame, to);
173 public virtual ValueReference GetChild (string name)
175 TargetObject obj = Value;
177 if (obj == null)
178 return null;
180 obj = ObjectUtil.GetRealObject (ctx, obj);
182 if (obj == null)
183 return null;
185 switch (obj.Kind)
187 case TargetObjectKind.Array: {
189 TargetArrayObject arr = obj as TargetArrayObject;
190 if (arr == null)
191 return null;
193 // Parse the array indices
194 string[] sinds = name.Substring (1, name.Length - 2).Split (',');
195 int[] indices = new int [sinds.Length];
196 for (int n=0; n<sinds.Length; n++)
197 indices [n] = int.Parse (sinds [n]);
199 return new ArrayValueReference (ctx, arr, indices);
202 case TargetObjectKind.Struct:
203 case TargetObjectKind.GenericInstance:
204 case TargetObjectKind.Class: {
205 TargetStructObject co = obj as TargetStructObject;
206 if (co == null)
207 return null;
208 foreach (ValueReference val in Util.GetMembers (GetChildrenContext (), co.Type, co)) {
209 if (val.Name == name)
210 return val;
212 return null;
215 default:
216 return null;