Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / Mono.Debugger.Soft / Mono.Debugger.Soft / StackFrame.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4
5 namespace Mono.Debugger.Soft
6 {
7         public class StackFrame : Mirror
8         {
9                 ThreadMirror thread;
10                 MethodMirror method;
11                 int il_offset;
12                 Location location;
13                 StackFrameFlags flags;
14
15                 /*
16                  * FIXME: Decide on the way to request/handle debugging information:
17                  * - request the info in bulk for all frames/on demand for individual frames
18                  * - request the info from the runtime/request only the il offset, and compute
19                  *   everything else based on this info using the method debug info.
20                  */
21
22                 internal StackFrame (VirtualMachine vm, long id, ThreadMirror thread, MethodMirror method, int il_offset, StackFrameFlags flags) : base (vm, id) {
23                         this.thread = thread;
24                         this.method = method;
25                         this.il_offset = il_offset;
26                         this.flags = flags;
27                 }
28
29                 public ThreadMirror Thread {
30                         get {
31                                 return thread;
32                         }
33                 }
34
35                 public MethodMirror Method {
36                         get {
37                                 return method;
38                         }
39                 }
40
41                 public Location Location {
42                         get {
43                                 if (location == null) {
44                                         int line_number;
45                                         string src_file = null;
46                                         byte[] hash = null;
47                                         int column_number = 0;
48
49                                         if (il_offset == -1)
50                                                 line_number = -1;
51                                         else
52                                                 line_number = method.il_offset_to_line_number (il_offset, out src_file, out hash, out column_number);
53
54                                         location = new Location (vm, Method, 0, il_offset, src_file != null ? src_file : method.SourceFile, line_number, column_number, hash);
55                                 }
56                                 return location;
57                         }
58                 }
59
60                 public string FileName {
61                         get {
62                                 return Location.SourceFile;
63                         }
64             }
65
66                 public int ILOffset {
67                         get {
68                                 return Location.ILOffset;
69                         }
70                 }
71
72                 public int LineNumber {
73                         get {
74                                 return Location.LineNumber;
75                         }
76             }
77
78                 public bool IsDebuggerInvoke {
79                         get {
80                                 return (flags & StackFrameFlags.DEBUGGER_INVOKE) != 0;
81                         }
82                 }
83
84                 /*
85                  * Whenever this frame transitions to native code. The method associated
86                  * with the frame is either an InternalCall or a pinvoke method.
87                  */
88                 public bool IsNativeTransition {
89                         get {
90                                 return (flags & StackFrameFlags.NATIVE_TRANSITION) != 0;
91                         }
92                 }
93
94                 public Value GetValue (ParameterInfoMirror param) {
95                         if (param == null)
96                                 throw new ArgumentNullException ("param");
97                         if (param.Method != Method)
98                                 throw new ArgumentException ("Parameter doesn't belong to this frame's method.");
99                         if (param.IsRetval)
100                                 throw new ArgumentException ("Parameter represents the method return value.");
101
102                         // FIXME: Liveness
103                         // FIXME: Allow returning the frame return value if possible
104                         return vm.DecodeValue (vm.conn.StackFrame_GetValues (thread.Id, Id, new int [] { (- param.Position) - 1 })[0]);
105                 }
106
107                 public Value GetValue (LocalVariable var) {
108                         if (var == null)
109                                 throw new ArgumentNullException ("var");
110                         if (var.Method != Method)
111                                 throw new ArgumentException ("Local variable doesn't belong to this frame's method.");
112
113                         // FIXME: Liveness
114                         // FIXME: Check for return value
115                         // FIXME: Allow returning the frame return value if possible
116                         return vm.DecodeValue (vm.conn.StackFrame_GetValues (thread.Id, Id, new int [] { var.GetValueIndex } )[0]);
117                 }
118
119                 public Value[] GetValues (LocalVariable[] vars) {
120                         if (vars == null)
121                                 throw new ArgumentNullException ("vars");
122                         for (int i = 0; i < vars.Length; ++i) {
123                                 if (vars [i] == null)
124                                         throw new ArgumentNullException ("vars");
125                                 if (vars [i].Method != Method)
126                                         throw new ArgumentException ("Local variable doesn't belong to this frame's method.");
127                         }
128                         int[] pos = new int [vars.Length];
129                         for (int i = 0; i < vars.Length; ++i)
130                                 pos [i] = vars [i].GetValueIndex;
131                         return vm.DecodeValues (vm.conn.StackFrame_GetValues (thread.Id, Id, pos));
132                 }
133
134                 public Value GetArgument (int pos) {
135                         return GetValue (Method.GetParameters () [pos]);
136                 }
137
138                 public Value GetThis () {
139                         return vm.DecodeValue (vm.conn.StackFrame_GetThis (thread.Id, Id));
140                 }
141
142                 public void SetValue (LocalVariable var, Value value) {
143                         if (var == null)
144                                 throw new ArgumentNullException ("var");
145                         if (var.Method != Method)
146                                 throw new ArgumentException ("Local variable doesn't belong to this frame's method.");
147                         if (value == null)
148                                 throw new ArgumentNullException ("value");
149                         CheckMirror (value);
150                         // FIXME: Liveness
151                         // FIXME: Check for return value
152                         try {
153                                 vm.conn.StackFrame_SetValues (thread.Id, Id, new int [] { var.GetValueIndex }, new ValueImpl [] { vm.EncodeValue (value) });
154                         } catch (CommandException ex) {
155                                 if (ex.ErrorCode == ErrorCode.INVALID_ARGUMENT)
156                                         throw new ArgumentException ("Value does not match the type of the local variable.");
157                                 else
158                                         throw;
159                         }
160                 }
161
162                 public void SetValue (ParameterInfoMirror param, Value value) {
163                         if (param == null)
164                                 throw new ArgumentNullException ("param");
165                         if (param.Method != Method)
166                                 throw new ArgumentException ("Parameter doesn't belong to this frame's method.");
167                         if (param.IsRetval)
168                                 throw new ArgumentException ("Parameter represents the method return value.");
169                         if (value == null)
170                                 throw new ArgumentNullException ("value");
171                         CheckMirror (value);
172
173                         // FIXME: Liveness
174                         // FIXME: Allow setting the frame return value if possible
175                         try {
176                                 vm.conn.StackFrame_SetValues (thread.Id, Id, new int [] { (- param.Position) - 1 }, new ValueImpl [] { vm.EncodeValue (value) });
177                         } catch (CommandException ex) {
178                                 if (ex.ErrorCode == ErrorCode.INVALID_ARGUMENT)
179                                         throw new ArgumentException ("Value does not match the type of the variable.");
180                                 else
181                                         throw;
182                         }
183                 }
184
185                 public IList<LocalVariable> GetVisibleVariables () {
186                         if (Location.ILOffset == -1)
187                                 throw new AbsentInformationException ();
188
189                         return Method.GetLocals ().Where (l => l.LiveRangeStart <= location.ILOffset && l.LiveRangeEnd >= location.ILOffset).ToList ();
190                 }
191
192                 public LocalVariable GetVisibleVariableByName (string name) {
193                         if (name == null)
194                                 throw new ArgumentNullException ("name");
195
196                         if (Location.ILOffset == -1)
197                                 throw new AbsentInformationException ();
198
199                         return Method.GetLocals ().Where (l => l.LiveRangeStart <= location.ILOffset && l.LiveRangeEnd >= location.ILOffset && l.Name == name).FirstOrDefault ();
200                 }
201     }
202 }