jit-debug 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. * How to debug your C# application with the JIT engine
  2. To debug a C# application you need to run the JIT in your debugger.
  3. Before you can do anything useful in a debugger, you need a symbol
  4. file which tells your debugger about functions, types, line numbers
  5. and such. Unfortunately, this symbol file needs to be recreated each
  6. time the JIT compiles a new method since it doesn't know anything
  7. about this method (especially not its memory address) before actually
  8. compiling it.
  9. You have two ways of creating a symbol file:
  10. ** Letting the JIT dynamically create the symbol file
  11. This'll give you a symbol file which is suitable for debugging IL byte
  12. code - you won't see your C# source code.
  13. However, this method has the advantage that it works with every assembly,
  14. no matter whether it has been compiled with Mono's C# compiler (MCS) or
  15. with any other compiler. It's currently the only way to debug
  16. <tt>corlib.dll</tt> or any other library which cannot be compiled with
  17. our compiler yet.
  18. All that you need is a dump of the IL bytecode for each assembly (including
  19. all assemblies this assembly is referencing). This is done by using the
  20. <tt>monodis</tt> utility:
  21. <pre>
  22. monodis /home/export/martin/MONO-LINUX/lib/corlib.dll > corlib.il<br>
  23. monodis /home/export/martin/MONO-LINUX/lib/System.dll > System.il<br>
  24. monodis /home/export/martin/MONO-LINUX/bin/mcs.exe > mcs.il
  25. </pre>
  26. This is normally done automatically, but you can also disable
  27. it and create them by hand. See the <tt>mono</tt> manual page
  28. for details.
  29. Make sure that all the .il files have the same name as their corresponding
  30. assembly and that they're all created in the current directory.
  31. The JIT supports two different debugging file formats:
  32. <ul>
  33. * STABS: This is a very simple debugging format, but it may be the only one
  34. which is supported on your system. It is limited to source files of no more
  35. than 65.535 lines and it's type support is also very limited. You should only
  36. use this if your debugger doesn't support DWARF 2.
  37. To generate STABS output, use the <tt>--debug=stabs</tt> command line argument.
  38. * DWARF 2: The DWARF 2 debugging format is a very powerful debugging format
  39. which can handle source files of arbitrary size and has a highly sophisticated
  40. type support. It's the recommended format unless you need to use STABS because
  41. your debugger doesn't support DWARF 2.
  42. To generate DWARF 2 output, use the <tt>--debug=dwarf</tt> command line argument.
  43. </ul>
  44. You need to regenerate the symbol file each time the JIT compiled a new
  45. method and each time you restart the JIT. You cannot reuse your symbol file
  46. if you start the JIT a second file, not even if you're running the same
  47. application with the same input data a second time.
  48. Regenerating the symbol file is done by calling the JIT's
  49. <tt>mono_debug_make_symbols ()</tt> function from within your debugger and
  50. then reloading the symbol files. This function creates a <tt>filename-dwarf.s</tt>
  51. (or <tt>filename-stabs.s</tt>) assembler input file in the current directory and
  52. an object file in <tt>/tmp/filename.o</tt> - you need to tell your debugger to
  53. add this object file as symbol file.
  54. If you're using the GNU debugger, this is done like this:
  55. <pre>
  56. call mono_debug_make_symbols ()
  57. add-symbol-file /tmp/corlib.o
  58. add-symbol-file /tmp/mcs.o
  59. add-symbol-file /tmp/Mono.CSharp.Debugger.o
  60. </pre>
  61. You can also write a GDB macro like this:
  62. <pre>
  63. define reload
  64. call mono_debug_make_symbols ()
  65. add-symbol-file /tmp/corlib.o
  66. add-symbol-file /tmp/mcs.o
  67. add-symbol-file /tmp/Mono.CSharp.Debugger.o
  68. end
  69. </pre>
  70. Then you can just say <tt>reload</tt> to have GDB recreate the symbol file.
  71. There's also an <a href="jit-debug-sample.html">example debugging session</a> using
  72. the GNU debugger.
  73. ** Using a symbol file which have been created by the Mono C# compiler
  74. If you compiled your application with Mono's C# compiler (MCS), you can tell it to
  75. create a symbol file which is then processed and rewritten by the JIT engine.
  76. To do this, you must give MCS the <tt>-g</tt> option:
  77. <pre>
  78. $ mcs -g Foo.cs
  79. </pre>
  80. This creates a <tt>Foo-debug.s</tt> assembler input file.
  81. To use this in the JIT, you must first copy it to the target machine (the machine
  82. where you want to run the JIT to debug your application) and run it through the
  83. assembler to produce an object file <tt>Foo-debug.o</tt>. This object file must be
  84. in the current directory.
  85. Then start the JIT in your debugger and give it the <tt>--debug=dwarf-plus</tt> command
  86. line argument.
  87. Each time you call <tt>mono_debug_make_symbols ()</tt> from withing your debugger,
  88. the JIT will read this <tt>Foo-debug.o</tt>, fix some machine dependent things like
  89. memory addresses etc. in it and write it back to disk.
  90. If you're using the GNU debugger, you'll want to use a macro like this:
  91. <pre>
  92. define relocate
  93. call mono_debug_make_symbols ()
  94. add-symbol-file /tmp/corlib.o
  95. add-symbol-file mcs-debug.o
  96. add-symbol-file Mono.CSharp.Debugger-debug.o
  97. end
  98. </pre>
  99. If there is no <tt>assembly-debug.o</tt> file, but an <tt>assembly.il</tt> one, the
  100. JIT will fall back to normal DWARF 2 (in the example above, <tt>corlib.dll</tt> was
  101. compiled with Microsoft's compiler and the JIT is thus using DWARF to debug it).
  102. This debugging method only works if you compiled your assembly with MCS, but it'll
  103. allow you to actually debug your C# source code :-)
  104. Here's an <a href="jit-debug-sample2.html">example debugging session</a> using
  105. the GNU debugger.
  106. ** Breakpoints and single stepping
  107. The JIT has a <tt>--break</tt> command line argument to insert a breakpoint at the
  108. beginning of this method. It takes a <tt>Namespace.Class:Method</tt> argument which
  109. is the method. This argument can be given multiple times.
  110. However, once your application is stopped in GDB you may want to insert a breakpoint
  111. the next time the JIT compiles a method. There's a global variable
  112. <tt>mono_debug_insert_breakpoint</tt> which you can modify in your debugger.
  113. If this variable is set to a non-zero value, the JIT's <tt>arch_compile_method</tt>
  114. will insert a breakpoint the next time it is called, ie. at the top of the next
  115. method it compiles. If this value has a positive value, it acts as a counter and is
  116. decremented after inserting the breakpoint - setting it to a negative value will let
  117. the JIT insert the breakpoint each time it compiles a new method.
  118. There's also global variable <tt>mono_debug_last_breakpoint_address</tt> which always
  119. contains the address of the last inserted breakpoint. You may manually override this
  120. address with a <tt>nop</tt> instruction to delete the breakpoint.
  121. For instance, I have a GDB macro called <tt>enter</tt> which I use to enter a method
  122. rather than stepping over it:
  123. <pre>
  124. define enter
  125. set mono_debug_insert_breakpoint = 1
  126. continue
  127. set *mono_debug_last_breakpoint_address = 0x90
  128. relocate
  129. frame
  130. </pre>
  131. Btw. speaking of single stepping - you should use your debuggers <tt>next</tt> command,
  132. not its <tt>step</tt> command for single stepping unless you compiled the JIT without
  133. debugging support. The reason for this is that the JIT creates machine code which contains
  134. calls to JIT methods such as <tt>mono_object_new_wrapper</tt> at places where you don't
  135. expect them - so unless the JIT is compiled at least without line numbers, your debugger
  136. will enter such methods if you use <tt>step</tt> rather than <tt>next</tt>.