jit-debug-sample 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. * A debugging session using a dynamically generated symbol file.
  2. Let's assume we have the following C# application which we want to debug:
  3. <pre>
  4. using System;
  5. public class Foo
  6. {
  7. public struct MyStruct {
  8. int a;
  9. long b;
  10. double c;
  11. }
  12. public static void Main ()
  13. {
  14. Int32 value = 5;
  15. long test = 512;
  16. MyStruct my_struct;
  17. my_struct.a = 5;
  18. my_struct.b = test;
  19. my_struct.c = 23323.5235;
  20. }
  21. }
  22. </pre>
  23. First of all, we need to compile it and create the .il files:
  24. <pre>
  25. $ mcs ./Foo.cs
  26. $ monodis /home/export/martin/MONO-LINUX/lib/corlib.dll > corlib.il
  27. $ monodis Foo.exe > Foo.il
  28. </pre>
  29. Now we can start the JIT in the debugger:
  30. <pre>
  31. $ gdb ~/monocvs/mono/mono/jit/mono
  32. (gdb) r --dwarf --debug Foo:Main ./Foo.exe
  33. Starting program: /home/martin/monocvs/mono/mono/jit/mono --dwarf --debug Foo:Main ./Foo.exe
  34. 0x081e8911 in ?? ()
  35. (gdb) call mono_debug_make_symbols ()
  36. (gdb) add-symbol-file /tmp/Foo.o
  37. Reading symbols from /tmp/Foo.o...done.
  38. Current language: auto; currently c++
  39. (gdb) frame
  40. #0 Foo.Main () at Foo.il:26
  41. 26 // method line 2
  42. (gdb) n
  43. Foo.Main () at Foo.il:38
  44. 38 IL_0000: ldc.i4.5
  45. (gdb) list
  46. 33 .maxstack 2
  47. 34 .locals (
  48. 35 int32 V_0,
  49. 36 int64 V_1,
  50. 37 valuetype MyStruct V_2)
  51. 38 IL_0000: ldc.i4.5
  52. 39 IL_0001: stloc.0
  53. 40 IL_0002: ldc.i4 512
  54. 41 IL_0007: conv.i8
  55. 42 IL_0008: stloc.1
  56. 43 IL_0009: ldloca.s 2
  57. 44 IL_000b: ldc.i4.5
  58. 45 IL_000c: stfld int32 .MyStruct::a
  59. 46 IL_0011: ldloca.s 2
  60. 47 IL_0013: ldloc.1
  61. 48 IL_0014: stfld int64 .MyStruct::b
  62. 49 IL_0019: ldloca.s 2
  63. 50 IL_001b: ldc.r8 23323.5
  64. 51 IL_0024: stfld float64 .MyStruct::c
  65. 52 IL_0029: ret
  66. (gdb) until 52
  67. Foo.Main () at Foo.il:53
  68. 53 }
  69. (gdb) info locals
  70. V_0 = 5
  71. V_1 = 512
  72. V_2 = {a = 5, b = 512, c = 23323.523499999999}
  73. </pre>
  74. As you see in this example, you need to know IL code to use this debugging method - but
  75. it may be the only way to debug a library.