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