File: StackFrame.cs

package info (click to toggle)
mono-debugger 0.60%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 9,556 kB
  • ctags: 22,787
  • sloc: ansic: 99,407; cs: 42,429; sh: 9,192; makefile: 376
file content (434 lines) | stat: -rw-r--r-- 9,870 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
using System;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;

using Mono.Debugger.Languages;
using Mono.Debugger.Backend;

namespace Mono.Debugger
{
	[Serializable]
	public sealed class Register
	{
		public readonly string Name;
		public readonly int Index;
		public readonly int Size;
		TargetAddress addr_on_stack;
		Registers registers;
		bool valid;
		long value;

		public Register (Registers registers, string name, int index, int size,
				 bool valid, long value)
		{
			this.registers = registers;
			this.Name = name;
			this.Index = index;
			this.Size = size;
			this.valid = valid;
			this.value = value;
			this.addr_on_stack = TargetAddress.Null;
		}

		public long Value {
			get {
				if (!valid)
					throw new InvalidOperationException ();

				return value;
			}
		}

		public long GetValue ()
		{
			return value;
		}

		public void SetValue (TargetAddress address, long value)
		{
			this.valid = true;
			this.addr_on_stack = address;
			this.value = value;
		}

		public void SetValue (TargetAddress address, TargetAddress value)
		{
			this.valid = true;
			this.addr_on_stack = address;
			this.value = value.Address;
		}

		public void SetValue (long value)
		{
			this.valid = true;
			this.addr_on_stack = TargetAddress.Null;
			this.value = value;
		}

		public void SetValue (TargetAddress value)
		{
			this.valid = true;
			this.addr_on_stack = TargetAddress.Null;
			this.value = value.Address;
		}

		internal void WriteRegister (TargetMemoryAccess target, long value)
		{
			this.value = value;

			if (addr_on_stack.IsNull)
				target.SetRegisters (registers);
			else if (Size == target.TargetMemoryInfo.TargetIntegerSize)
				target.WriteInteger (addr_on_stack, (int) value);
			else
				target.WriteLongInteger (addr_on_stack, value);
		}

		public void WriteRegister (Thread thread, long value)
		{
			this.value = value;

			if (addr_on_stack.IsNull)
				thread.SetRegisters (registers);
			else if (Size == thread.TargetMemoryInfo.TargetIntegerSize)
				thread.WriteInteger (addr_on_stack, (int) value);
			else
				thread.WriteLongInteger (addr_on_stack, value);
		}

		public bool Valid {
			get {
				return valid;
			}

			set {
				valid = value;
			}
		}

		public TargetAddress AddressOnStack {
			get {
				return addr_on_stack;
			}
		}

		public override string ToString ()
		{
			return String.Format ("Register ({0}:{1}:{2:x})",
					      Index, Valid, GetValue ());
		}
	}

	[Serializable]
	public sealed class Registers
	{
		Register[] regs;
		bool from_current_frame;

		internal Registers (Architecture arch)
		{
			regs = new Register [arch.CountRegisters];
			for (int i = 0; i < regs.Length; i++) {
				if (arch.RegisterSizes [i] < 0)
					continue;
				regs [i] = new Register (
					this, arch.RegisterNames [i], i,
					arch.RegisterSizes [i], false, 0);
			}
		}

		internal Registers (Architecture arch, long[] values)
		{
			regs = new Register [arch.CountRegisters];
			if (regs.Length != values.Length)
				throw new ArgumentException ();
			for (int i = 0; i < regs.Length; i++) {
				if (arch.RegisterSizes [i] < 0)
					continue;
				regs [i] = new Register (
					this, arch.RegisterNames [i], i,
					arch.RegisterSizes [i], true, values [i]);
			}
			from_current_frame = true;
		}

		internal Registers (Registers old_regs)
		{
			regs = new Register [old_regs.regs.Length];
			for (int i = 0; i < regs.Length; i++) {
				if (old_regs [i] == null)
					continue;
				regs [i] = new Register (
					this, old_regs [i].Name, i, old_regs [i].Size,
					false, old_regs [i].GetValue ());
			}
		}

		public Register this [int index] {
			get {
				return regs [index];
			}
		}

		public Register this [string name] {
			get {
				foreach (Register reg in regs) {
					if (reg == null)
						continue;
					if (reg.Name == name)
						return reg;
				}

				return null;
			}
		}

		internal long[] Values {
			get {
				long[] retval = new long [regs.Length];
				for (int i = 0; i < regs.Length; i++) {
					if (regs [i] == null)
						continue;
					retval [i] = regs [i].GetValue ();
				}

				return retval;
			}
		}

		public bool FromCurrentFrame {
			get {
				return from_current_frame;
			}
		}
	}

	[Serializable]
	public sealed class StackFrame : DebuggerMarshalByRefObject
	{
		protected readonly TargetAddress address;
		protected readonly TargetAddress stack_pointer;
		protected readonly TargetAddress frame_address;
		protected readonly Registers registers;

		int level;
		Method method;
		Thread thread;
		SourceAddress source;
		StackFrame parent_frame;
		TargetObject exc_object;
		SourceLocation location;
		TargetFunctionType function;
		Language language;
		bool has_source;
		Symbol name;

		internal StackFrame (Thread thread, TargetAddress address, TargetAddress stack_ptr,
				     TargetAddress frame_address, Registers registers)
		{
			this.thread = thread;
			this.address = address;
			this.stack_pointer = stack_ptr;
			this.frame_address = frame_address;
			this.registers = registers;
		}

		internal StackFrame (Thread thread, TargetAddress address, TargetAddress stack_ptr,
				     TargetAddress frame_address, Registers registers,
				     Language language, Symbol name)
			: this (thread, address, stack_ptr, frame_address, registers)
		{
			this.language = language;
			this.name = name;
		}

		internal StackFrame (Thread thread, TargetAddress address, TargetAddress stack_ptr,
				     TargetAddress frame_address, Registers registers,
				     Method method)
			: this (thread, address, stack_ptr, frame_address, registers)
		{
			this.method = method;
			this.language = method.Module.Language;
			if (method.IsLoaded)
				this.name = new Symbol (method.Name, method.StartAddress, 0);
			else
				this.name = new Symbol (method.Name, address, 0);
		}

		internal StackFrame (Thread thread, TargetAddress address, TargetAddress stack_ptr,
				     TargetAddress frame_address, Registers registers,
				     TargetFunctionType function, SourceLocation location)
			: this (thread, address, stack_ptr, frame_address, registers)
		{
			this.function = function;
			this.language = function.DeclaringType.Language;
			this.name = new Symbol (function.FullName, address, 0);
			this.location = location;
		}

		internal StackFrame (Thread thread, TargetAddress address, TargetAddress stack_ptr,
				     TargetAddress frame_address, Registers registers,
				     Method method, SourceAddress source)
			: this (thread, address, stack_ptr, frame_address, registers, method)
		{
			this.source = source;
			if (method.HasSource && !method.MethodSource.IsDynamic)
				location = new SourceLocation (
					method.MethodSource, source.SourceFile, source.Row);
			has_source = true;
		}

		public int Level {
			get { return level; }
		}

		internal void SetLevel (int new_level)
		{
			level = new_level;
		}

		void compute_source ()
		{
			lock (this) {
				if (has_source)
					return;
				has_source = true;
				if ((method == null) || !method.HasSource || !method.HasLineNumbers)
					return;
				source = method.LineNumberTable.Lookup (address);
				if (source == null)
					return;
				if (method.MethodSource.IsDynamic)
					return;
				location = new SourceLocation (
					method.MethodSource, source.SourceFile, source.Row);
			}
		}

		public SourceAddress SourceAddress {
			get {
				compute_source ();
				return source;
			}
		}

		public SourceLocation SourceLocation {
			get {
				compute_source ();
				return location;
			}
		}

		public TargetAddress TargetAddress {
			get { return address; }
		}

		public TargetAddress StackPointer {
			get { return stack_pointer; }
		}

		public TargetAddress FrameAddress {
			get { return frame_address; }
		}

		public Thread Thread {
			get { return thread; }
		}

		public Registers Registers {
			get { return registers; }
		}

		public Method Method {
			get { return method; }
		}

		public TargetFunctionType Function {
			get { return function; }
		}

		public Symbol Name {
			get { return name; }
		}

		public Language Language {
			get { return language; }
		}

		internal StackFrame ParentFrame {
			get { return parent_frame; }
			set { parent_frame = value; }
		}

		public TargetObject ExceptionObject {
			get { return exc_object; }
		}

		internal void SetExceptionObject (TargetAddress exc)
		{
			try {
				Language lang = thread.ThreadServant.ProcessServant.MonoLanguage;
				if (lang != null)
					exc_object = lang.CreateObject (thread, exc);
			} catch {
				exc_object = null;
			}
		}

		internal StackFrame UnwindStack (TargetMemoryAccess memory)
		{
			if (parent_frame != null)
				return parent_frame;

			StackFrame new_frame = null;
			if (method != null) {
				try {
					new_frame = method.UnwindStack (this, memory);
				} catch (TargetException) {
				}

				if (new_frame != null)
					return new_frame;
			}

			foreach (Module module in thread.Process.Modules) {
				try {
					new_frame = module.UnwindStack (this, memory);
				} catch {
					continue;
				}
				if (new_frame != null)
					return new_frame;
			}

			return thread.Architecture.UnwindStack (this, memory, null, 0);
		}

		public override string ToString ()
		{
			StringBuilder sb = new StringBuilder ();

			sb.Append (String.Format ("#{0}: ", level));

			if (method != null) {
				sb.Append (String.Format ("{0} in {1}", address, method.Name));
				if (method.IsLoaded) {
					long offset = address - method.StartAddress;
					if (offset > 0)
						sb.Append (String.Format ("+0x{0:x}", offset));
					else if (offset < 0)
						sb.Append (String.Format ("-0x{0:x}", -offset));
				}
			} else if (name != null)
				sb.Append (String.Format ("{0} in {1}", address, name));
			else
				sb.Append (String.Format ("{0}", address));

			if (SourceAddress != null)
				sb.Append (String.Format (" at {0}", source.Name));

			return sb.ToString ();
		}
	}
}