Merge pull request #555 from jack-pappas/sigaltstack-patch
[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 int ColumnNumber {
79                         get {
80                                 return Location.ColumnNumber;
81                         }
82                 }
83
84                 public bool IsDebuggerInvoke {
85                         get {
86                                 return (flags & StackFrameFlags.DEBUGGER_INVOKE) != 0;
87                         }
88                 }
89
90                 /*
91                  * Whenever this frame transitions to native code. The method associated
92                  * with the frame is either an InternalCall or a pinvoke method.
93                  */
94                 public bool IsNativeTransition {
95                         get {
96                                 return (flags & StackFrameFlags.NATIVE_TRANSITION) != 0;
97                         }
98                 }
99
100                 public Value GetValue (ParameterInfoMirror param) {
101                         if (param == null)
102                                 throw new ArgumentNullException ("param");
103                         if (param.Method != Method)
104                                 throw new ArgumentException ("Parameter doesn't belong to this frame's method.");
105                         if (param.IsRetval)
106                                 throw new ArgumentException ("Parameter represents the method return value.");
107
108                         // FIXME: Liveness
109                         // FIXME: Allow returning the frame return value if possible
110                         return vm.DecodeValue (vm.conn.StackFrame_GetValues (thread.Id, Id, new int [] { (- param.Position) - 1 })[0]);
111                 }
112
113                 public Value GetValue (LocalVariable var) {
114                         if (var == null)
115                                 throw new ArgumentNullException ("var");
116                         if (var.Method != Method)
117                                 throw new ArgumentException ("Local variable doesn't belong to this frame's method.");
118
119                         // FIXME: Liveness
120                         // FIXME: Check for return value
121                         // FIXME: Allow returning the frame return value if possible
122                         return vm.DecodeValue (vm.conn.StackFrame_GetValues (thread.Id, Id, new int [] { var.GetValueIndex } )[0]);
123                 }
124
125                 public Value[] GetValues (LocalVariable[] vars) {
126                         if (vars == null)
127                                 throw new ArgumentNullException ("vars");
128                         for (int i = 0; i < vars.Length; ++i) {
129                                 if (vars [i] == null)
130                                         throw new ArgumentNullException ("vars");
131                                 if (vars [i].Method != Method)
132                                         throw new ArgumentException ("Local variable doesn't belong to this frame's method.");
133                         }
134                         int[] pos = new int [vars.Length];
135                         for (int i = 0; i < vars.Length; ++i)
136                                 pos [i] = vars [i].GetValueIndex;
137                         return vm.DecodeValues (vm.conn.StackFrame_GetValues (thread.Id, Id, pos));
138                 }
139
140                 public Value GetArgument (int pos) {
141                         return GetValue (Method.GetParameters () [pos]);
142                 }
143
144                 public Value GetThis () {
145                         return vm.DecodeValue (vm.conn.StackFrame_GetThis (thread.Id, Id));
146                 }
147
148                 public void SetValue (LocalVariable var, Value value) {
149                         if (var == null)
150                                 throw new ArgumentNullException ("var");
151                         if (var.Method != Method)
152                                 throw new ArgumentException ("Local variable doesn't belong to this frame's method.");
153                         if (value == null)
154                                 throw new ArgumentNullException ("value");
155                         CheckMirror (value);
156                         // FIXME: Liveness
157                         // FIXME: Check for return value
158                         try {
159                                 vm.conn.StackFrame_SetValues (thread.Id, Id, new int [] { var.GetValueIndex }, new ValueImpl [] { vm.EncodeValue (value) });
160                         } catch (CommandException ex) {
161                                 if (ex.ErrorCode == ErrorCode.INVALID_ARGUMENT)
162                                         throw new ArgumentException ("Value does not match the type of the local variable.");
163                                 else
164                                         throw;
165                         }
166                 }
167
168                 public void SetValue (ParameterInfoMirror param, Value value) {
169                         if (param == null)
170                                 throw new ArgumentNullException ("param");
171                         if (param.Method != Method)
172                                 throw new ArgumentException ("Parameter doesn't belong to this frame's method.");
173                         if (param.IsRetval)
174                                 throw new ArgumentException ("Parameter represents the method return value.");
175                         if (value == null)
176                                 throw new ArgumentNullException ("value");
177                         CheckMirror (value);
178
179                         // FIXME: Liveness
180                         // FIXME: Allow setting the frame return value if possible
181                         try {
182                                 vm.conn.StackFrame_SetValues (thread.Id, Id, new int [] { (- param.Position) - 1 }, new ValueImpl [] { vm.EncodeValue (value) });
183                         } catch (CommandException ex) {
184                                 if (ex.ErrorCode == ErrorCode.INVALID_ARGUMENT)
185                                         throw new ArgumentException ("Value does not match the type of the variable.");
186                                 else
187                                         throw;
188                         }
189                 }
190
191                 public IList<LocalVariable> GetVisibleVariables () {
192                         if (Location.ILOffset == -1)
193                                 throw new AbsentInformationException ();
194
195                         return Method.GetLocals ().Where (l => l.LiveRangeStart <= location.ILOffset && l.LiveRangeEnd >= location.ILOffset).ToList ();
196                 }
197
198                 public LocalVariable GetVisibleVariableByName (string name) {
199                         if (name == null)
200                                 throw new ArgumentNullException ("name");
201
202                         if (Location.ILOffset == -1)
203                                 throw new AbsentInformationException ();
204
205                         return Method.GetLocals ().Where (l => l.LiveRangeStart <= location.ILOffset && l.LiveRangeEnd >= location.ILOffset && l.Name == name).FirstOrDefault ();
206                 }
207     }
208 }