4 // Lluis Sanchez Gual <lluis@novell.com>
6 // Copyright (c) 2008 Novell, Inc (http://www.novell.com)
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
29 using System
.Collections
.Generic
;
30 using Mono
.Debugger
.Languages
;
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
)
48 public virtual object ObjectValue
{
50 TargetObject ob
= Value
;
51 if (ob
is TargetFundamentalObject
) {
52 TargetFundamentalObject fob
= (TargetFundamentalObject
) ob
;
53 return fob
.GetObject (ctx
.Thread
);
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
{
70 public ObjectValue
CreateObjectValue (bool withTimeout
)
73 return Server
.Instance
.AsyncEvaluationTracker
.Run (Name
, Flags
, delegate {
74 return CreateObjectValue ();
77 return CreateObjectValue ();
80 public ObjectValue
CreateObjectValue ()
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 ()
98 if (string.IsNullOrEmpty (name
))
101 TargetObject val
= Value
;
103 return Util
.CreateObjectValue (ctx
, this, new ObjectPath (name
), Value
, Flags
);
105 return Mono
.Debugging
.Client
.ObjectValue
.CreateNullObject (name
, Type
.Name
, Flags
);
108 public string SetValue (ObjectPath path
, string value)
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
);
119 } catch (Exception ex
) {
120 Server
.Instance
.WriteDebuggerError (ex
);
121 Server
.Instance
.WriteDebuggerOutput ("Value assignment failed: {0}: {1}\n", ex
.GetType (), ex
.Message
);
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
);
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)
142 return val
.ToString ();
145 public virtual ObjectValue
[] GetChildren (ObjectPath path
, int index
, int count
)
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 ()
158 TargetStructObject val
= Value
as TargetStructObject
;
160 return Util
.GetMembers (GetChildrenContext (), val
.Type
, val
);
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
;
180 obj
= ObjectUtil
.GetRealObject (ctx
, obj
);
187 case TargetObjectKind
.Array
: {
189 TargetArrayObject arr
= obj
as TargetArrayObject
;
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
;
208 foreach (ValueReference val
in Util
.GetMembers (GetChildrenContext (), co
.Type
, co
)) {
209 if (val
.Name
== name
)