File: CoreFile.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 (739 lines) | stat: -rw-r--r-- 18,354 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
using System;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;
using Mono.Debugger.Languages;
using Mono.Debugger.Languages.Mono;

namespace Mono.Debugger.Backend
{
	internal class CoreFile : ProcessServant
	{
		TargetMemoryInfo info;
		Bfd bfd, core_bfd;
		string core_file;
		string application;
		ArrayList threads;

		MonoDebuggerInfo debugger_info;

		public static CoreFile OpenCoreFile (ThreadManager manager, ProcessStart start)
		{
			return new CoreFile (manager, start);
		}

		protected CoreFile (ThreadManager manager, ProcessStart start)
			: base (manager, start)
		{
			info = Inferior.GetTargetMemoryInfo (manager.AddressDomain);

			bfd = BfdContainer.AddFile (
				info, start.TargetApplication, TargetAddress.Null,
				start.LoadNativeSymbolTable, true);

			BfdContainer.SetupInferior (info, bfd);

			core_file = start.CoreFile;
			application = bfd.FileName;

			core_bfd = bfd.OpenCoreFile (core_file);

#if FIXME
			string crash_program = core_bfd.CrashProgram;
			string[] crash_program_args = crash_program.Split (' ');

			if (crash_program_args [0] != application)
				throw new TargetException (
					TargetError.CannotStartTarget,
					"Core file (generated from {0}) doesn't match executable {1}.",
					crash_program, application);

			bool ok;
			try {
				DateTime core_date = Directory.GetLastWriteTime (core_file);
				DateTime app_date = Directory.GetLastWriteTime (application);

				ok = app_date < core_date;
			} catch {
				ok = false;
			}

			if (!ok)
				throw new TargetException (
					TargetError.CannotStartTarget,
					"Executable {0} is more recent than core file {1}.",
					application, core_file);
#endif

			read_note_section ();
			main_thread = (CoreFileThread) threads [0];

			TargetMemoryAccess target_access = ((CoreFileThread) threads [0]).TargetAccess;
			bfd.UpdateSharedLibraryInfo (null, target_access);

			TargetAddress mdb_debug_info = bfd.GetSectionAddress (".mdb_debug_info");
			if (!mdb_debug_info.IsNull) {
				mdb_debug_info = main_thread.ReadAddress (mdb_debug_info);
				debugger_info = MonoDebuggerInfo.Create (target_access, mdb_debug_info);
				read_thread_table ();
				CreateMonoLanguage (debugger_info);
				mono_language.InitializeCoreFile (target_access);
				mono_language.Update (target_access);
			}
		}

		void read_thread_table ()
		{
			TargetAddress ptr = main_thread.ReadAddress (debugger_info.ThreadTable);
			Console.WriteLine ("READ THREAD TABLE: {0}", ptr);
			while (!ptr.IsNull) {
				int size = 56 + main_thread.TargetMemoryInfo.TargetAddressSize;
				TargetReader reader = new TargetReader (main_thread.ReadMemory (ptr, size));

				long tid = reader.ReadLongInteger ();
				TargetAddress lmf_addr = reader.ReadAddress ();
				TargetAddress end_stack = reader.ReadAddress ();

				ptr = reader.ReadAddress ();

				TargetAddress stack_start = reader.ReadAddress ();
				TargetAddress signal_stack_start = reader.ReadAddress ();
				int stack_size = reader.ReadInteger ();
				int signal_stack_size = reader.ReadInteger ();

				Console.WriteLine ("READ THREAD TABLE #1: {0:x} {1} {2} {3} {4} {5} {6}",
						   tid, lmf_addr, end_stack,
						   stack_start, stack_start + stack_size,
						   signal_stack_start,
						   signal_stack_start + signal_stack_size);

				bool found = false;
				foreach (CoreFileThread thread in threads) {
					TargetAddress sp = thread.CurrentFrame.StackPointer;

					if ((sp >= stack_start) && (sp < stack_start + stack_size)) {
						Console.WriteLine ("SET LMF: {0} {1:x} {2}",
								   thread, tid, lmf_addr);
						thread.SetLMFAddress (tid, lmf_addr);
						found = true;
						break;
					} else if (!signal_stack_start.IsNull &&
						   (sp >= signal_stack_start) &&
						   (sp < signal_stack_start + signal_stack_size)) {
						Console.WriteLine ("SET LMF: {0} {1:x} {2}",
								   thread, tid, lmf_addr);
						thread.SetLMFAddress (tid, lmf_addr);
						found = true;
						break;
					}
				}

				if (!found)
					Console.WriteLine ("FUCK!");
			}
		}

		protected TargetReader GetReader (TargetAddress address)
		{
			TargetReader reader = core_bfd.GetReader (address, true);
			if (reader != null)
				return reader;

			Bfd bfd = BfdContainer.LookupLibrary (address);
			if (bfd != null)
				return bfd.GetReader (address, false);

			return null;
		}

		void read_note_section ()
		{
			threads = new ArrayList ();
			foreach (Bfd.Section section in core_bfd.Sections) {
				if (!section.name.StartsWith (".reg/"))
					continue;

				int pid = Int32.Parse (section.name.Substring (5));
				CoreFileThread thread = new CoreFileThread (this, pid);
				OnThreadCreatedEvent (thread);
				threads.Add (thread);
			}

#if FIXME
			TargetReader reader = core_bfd.GetSectionReader ("note0");
			while (reader.Offset < reader.Size) {
				long offset = reader.Offset;
				int namesz = reader.ReadInteger ();
				int descsz = reader.ReadInteger ();
				int type = reader.ReadInteger ();

				string name = null;
				if (namesz != 0) {
					char[] namebuf = new char [namesz];
					for (int i = 0; i < namesz; i++)
						namebuf [i] = (char) reader.ReadByte ();

					name = new String (namebuf);
				}

				byte[] desc = null;
				if (descsz != 0)
					desc = reader.BinaryReader.ReadBuffer (descsz);

				// Console.WriteLine ("NOTE: {0} {1:x} {2}", offset, type, name);
				// Console.WriteLine (TargetBinaryReader.HexDump (desc));

				reader.Offset += 4 - (reader.Offset % 4);
			}
#endif
		}

		public Bfd Bfd {
			get { return bfd; }
		}

		public Bfd CoreBfd {
			get { return core_bfd; }
		}

		public TargetMemoryInfo TargetMemoryInfo {
			get { return info; }
		}

		protected class CoreFileThread : ThreadServant
		{
			public readonly CoreFile CoreFile;
			public readonly Thread Thread;
			public readonly Registers Registers;
			public readonly CoreFileTargetAccess TargetAccess;
			Backtrace current_backtrace;
			StackFrame current_frame;
			Method current_method;
			long tid;
			int pid;

			TargetAddress lmf_address = TargetAddress.Null;

			public CoreFileThread (CoreFile core, int pid)
				: base (core.ThreadManager, core)
			{
				this.pid = pid;
				this.CoreFile = core;
				this.Thread = new Thread (this, ID);

				this.Registers = read_registers ();

				this.TargetAccess = new CoreFileTargetAccess (this);
			}

			Registers read_registers ()
			{
				string sname = String.Format (".reg/{0}", PID);
				TargetReader reader = CoreFile.CoreBfd.GetSectionReader (sname);

				Architecture arch = CoreFile.Architecture;
				long[] values = new long [arch.CountRegisters];
				for (int i = 0; i < values.Length; i++) {
					int size = arch.RegisterSizes [i];
					if (size == 4)
						values [i] = reader.BinaryReader.ReadInt32 ();
					else if (size == 8)
						values [i] = reader.BinaryReader.ReadInt64 ();
					else
						throw new InternalError ();
				}

				return new Registers (arch, values);
			}

			internal void SetLMFAddress (long tid, TargetAddress lmf)
			{
				this.tid = tid;
				this.lmf_address = lmf;
			}

			internal Disassembler Disassembler {
				get { return CoreFile.Architecture.Disassembler; }
			}

			internal override ThreadManager ThreadManager {
				get { return CoreFile.ThreadManager; }
			}

			internal override ProcessServant ProcessServant {
				get { return CoreFile; }
			}

			public override TargetEventArgs LastTargetEvent {
				get { throw new InvalidOperationException (); }
			}

			public override TargetMemoryInfo TargetMemoryInfo {
				get { return CoreFile.TargetMemoryInfo; }
			}

			internal override object DoTargetAccess (TargetAccessHandler func)
			{
				return func (TargetAccess);
			}

			public override int PID {
				get { return pid; }
			}

			public override long TID {
				get { return tid; }
			}

			public override TargetAddress LMFAddress {
				get { return lmf_address; }
			}

			public override bool IsAlive {
				get { return true; }
			}

			public override bool CanRun {
				get { return false; }
			}

			public override bool CanStep {
				get { return false; }
			}

			public override bool IsStopped {
				get { return true; }
			}

			public override Backtrace GetBacktrace (Backtrace.Mode mode, int max_frames)
			{
				current_backtrace = new Backtrace (CurrentFrame);

				current_backtrace.GetBacktrace (
					this, TargetAccess, mode, TargetAddress.Null, max_frames);

				return current_backtrace;
			}

			public override TargetState State {
				get { return TargetState.CoreFile; }
			}

			public override StackFrame CurrentFrame {
				get {
					if (current_frame == null)
						current_frame = CoreFile.Architecture.CreateFrame (
							Thread, TargetAccess, Registers, true);

					return current_frame;
				}
			}

			public override Method CurrentMethod {
				get {
					if (current_method == null)
						current_method = Lookup (CurrentFrameAddress);

					return current_method;
				}
			}

			public override TargetAddress CurrentFrameAddress {
				get { return CurrentFrame.TargetAddress; }
			}

			public override Backtrace CurrentBacktrace {
				get { return current_backtrace; }
			}

			public override Registers GetRegisters ()
			{
				return Registers;
			}

			public override int GetInstructionSize (TargetAddress address)
			{
				return Disassembler.GetInstructionSize (TargetAccess, address);
			}

			public override AssemblerLine DisassembleInstruction (Method method,
									      TargetAddress address)
			{
				return Disassembler.DisassembleInstruction (TargetAccess, method, address);
			}

			public override AssemblerMethod DisassembleMethod (Method method)
			{
				return Disassembler.DisassembleMethod (TargetAccess, method);
			}

			public override TargetMemoryArea[] GetMemoryMaps ()
			{
				throw new NotImplementedException ();
			}

			public override Method Lookup (TargetAddress address)
			{
				return CoreFile.SymbolTableManager.Lookup (address);
			}

			public override Symbol SimpleLookup (TargetAddress address, bool exact_match)
			{
				return CoreFile.SymbolTableManager.SimpleLookup (address, exact_match);
			}

			internal override object Invoke (TargetAccessDelegate func, object data)
			{
				throw new InvalidOperationException ();
			}

			internal override void AcquireThreadLock ()
			{
				throw new InvalidOperationException ();
			}

			internal override void ReleaseThreadLock ()
			{
				throw new InvalidOperationException ();
			}

			internal override void ReleaseThreadLockDone ()
			{
				throw new InvalidOperationException ();
			}

			//
			// TargetMemoryAccess
			//

			internal override Architecture Architecture {
				get { return CoreFile.Architecture; }
			}

			public override byte ReadByte (TargetAddress address)
			{
				return CoreFile.GetReader (address).ReadByte ();
			}

			public override int ReadInteger (TargetAddress address)
			{
				return CoreFile.GetReader (address).ReadInteger ();
			}

			public override long ReadLongInteger (TargetAddress address)
			{
				return CoreFile.GetReader (address).ReadLongInteger ();
			}

			public override TargetAddress ReadAddress (TargetAddress address)
			{
				return CoreFile.GetReader (address).ReadAddress ();
			}

			public override string ReadString (TargetAddress address)
			{
				return CoreFile.GetReader (address).BinaryReader.ReadString ();
			}

			public override TargetBlob ReadMemory (TargetAddress address, int size)
			{
				return new TargetBlob (ReadBuffer (address, size), TargetMemoryInfo);
			}

			public override byte[] ReadBuffer (TargetAddress address, int size)
			{
				return CoreFile.GetReader (address).BinaryReader.ReadBuffer (size);
			}

			internal override Registers GetCallbackFrame (TargetAddress stack_pointer,
								      bool exact_match)
			{
				return null;
			}

			public override bool CanWrite {
				get { return false; }
			}

			public override void WriteBuffer (TargetAddress address, byte[] buffer)
			{
				throw new InvalidOperationException ();
			}

			public override void WriteByte (TargetAddress address, byte value)
			{
				throw new InvalidOperationException ();
			}

			public override void WriteInteger (TargetAddress address, int value)
			{
				throw new InvalidOperationException ();
			}

			public override void WriteLongInteger (TargetAddress address, long value)
			{
				throw new InvalidOperationException ();
			}

			public override void WriteAddress (TargetAddress address, TargetAddress value)
			{
				throw new InvalidOperationException ();
			}

			public override void SetRegisters (Registers registers)
			{
				throw new InvalidOperationException ();
			}

			internal override void InsertBreakpoint (BreakpointHandle handle,
								 TargetAddress address, int domain)
			{
				throw new InvalidOperationException ();
			}

			internal override void RemoveBreakpoint (BreakpointHandle handle)
			{
				throw new InvalidOperationException ();
			}

			public override void StepInstruction (CommandResult result)
			{
				throw new InvalidOperationException ();
			}

			public override void StepNativeInstruction (CommandResult result)
			{
				throw new InvalidOperationException ();
			}

			public override void NextInstruction (CommandResult result)
			{
				throw new InvalidOperationException ();
			}

			public override void StepLine (CommandResult result)
			{
				throw new InvalidOperationException ();
			}

			public override void NextLine (CommandResult result)
			{
				throw new InvalidOperationException ();
			}

			public override void Finish (bool native, CommandResult result)
			{
				throw new InvalidOperationException ();
			}

			public override void Continue (TargetAddress until, CommandResult result)
			{
				throw new InvalidOperationException ();
			}

			public override void Background (TargetAddress until, CommandResult result)
			{
				throw new InvalidOperationException ();
			}

			public override void Kill ()
			{ }

			public override void Detach ()
			{
				throw new InvalidOperationException ();
			}

			internal override void DetachThread ()
			{
				throw new InvalidOperationException ();
			}

			public override void Stop ()
			{
				throw new InvalidOperationException ();
			}

			public override int AddEventHandler (Event handle)
			{
				throw new InvalidOperationException ();
			}

			public override void RemoveEventHandler (int index)
			{
				throw new InvalidOperationException ();
			}

			public override string PrintObject (Style style, TargetObject obj,
							    DisplayFormat format)
			{
				return style.FormatObject (Thread, obj, format);
			}

			public override string PrintType (Style style, TargetType type)
			{
				return style.FormatType (Thread, type);
			}

			public override void RuntimeInvoke (TargetFunctionType function,
							    TargetClassObject object_argument,
							    TargetObject[] param_objects,
							    bool is_virtual, bool debug,
							    RuntimeInvokeResult result)
			{
				throw new InvalidOperationException ();
			}

			public override CommandResult CallMethod (TargetAddress method,
								  long arg1, long arg2)
			{
				throw new InvalidOperationException ();
			}

			public override CommandResult CallMethod (TargetAddress method,
								  long arg1, long arg2, long arg3,
								  string string_arg)
			{
				throw new InvalidOperationException ();
			}

			public override CommandResult Return (bool run_finally)
			{
				throw new InvalidOperationException ();
			}

			public override CommandResult AbortInvocation ()
			{
				throw new InvalidOperationException ();
			}

			protected class CoreFileTargetAccess : TargetMemoryAccess
			{
				public readonly CoreFileThread Thread;

				public CoreFileTargetAccess (CoreFileThread thread)
				{
					this.Thread = thread;
				}

				public override TargetMemoryInfo TargetMemoryInfo {
					get { return Thread.TargetMemoryInfo; }
				}

				public override AddressDomain AddressDomain {
					get { return Thread.AddressDomain; }
				}

				public override int TargetIntegerSize {
					get {
						return Thread.TargetIntegerSize;
					}
				}

				public override int TargetLongIntegerSize {
					get {
						return Thread.TargetLongIntegerSize;
					}
				}

				public override int TargetAddressSize {
					get {
						return Thread.TargetAddressSize;
					}
				}

				public override bool IsBigEndian {
					get {
				return Thread.IsBigEndian;
					}
				}

				public override byte ReadByte (TargetAddress address)
				{
					return Thread.ReadByte (address);
				}

				public override int ReadInteger (TargetAddress address)
				{
					return Thread.ReadInteger (address);
				}

				public override long ReadLongInteger (TargetAddress address)
				{
					return Thread.ReadLongInteger (address);
				}

				public override TargetAddress ReadAddress (TargetAddress address)
				{
					return Thread.ReadAddress (address);
				}

				public override string ReadString (TargetAddress address)
				{
					return Thread.ReadString (address);
				}

				public override TargetBlob ReadMemory (TargetAddress address, int size)
				{
					return Thread.ReadMemory (address, size);
				}

				public override byte[] ReadBuffer (TargetAddress address, int size)
				{
					return Thread.ReadBuffer (address, size);
				}

				public override Registers GetRegisters ()
				{
					return Thread.GetRegisters ();
				}

				public override bool CanWrite {
					get { return false; }
				}

				public override void WriteBuffer (TargetAddress address, byte[] buffer)
				{
					throw new InvalidOperationException ();
				}

				public override void WriteByte (TargetAddress address, byte value)
				{
					throw new InvalidOperationException ();
				}

				public override void WriteInteger (TargetAddress address, int value)
				{
					throw new InvalidOperationException ();
				}

				public override void WriteLongInteger (TargetAddress address, long value)
				{
					throw new InvalidOperationException ();
				}

				public override void WriteAddress (TargetAddress address,
								   TargetAddress value)
				{
					throw new InvalidOperationException ();
				}

				public override void SetRegisters (Registers registers)
				{
					throw new InvalidOperationException ();
				}
			}
		}

		//
		// IDisposable
		//

		protected override void DoDispose ()
		{
			if (core_bfd != null)
				core_bfd.Dispose ();
			base.DoDispose ();
		}
	}
}