2009-04-06 Zoltan Varga <vargaz@gmail.com>
[mono.git] / data / gdb / mono-gdb.py
1 #
2 # Author: Zoltan Varga (vargaz@gmail.com)
3 # License: MIT/X11
4 #
5
6 #
7 # This is a mono support mode for a python-enabled gdb:
8 # http://sourceware.org/gdb/wiki/PythonGdb
9 # Usage:
10 # - copy/symlink this file, plus mono-gdbinit to the directory where the mono 
11 #   executable lives.
12 # - run mono under gdb, or attach to a mono process using gdb
13 # - Type 'xdb' in gdb to load/reload the debugging info emitted by the runtime.
14 # - The debug info is emitted to a file called xdb.s in the current working directory.
15 #   When attaching to a mono process, make sure you are in the same directory.
16 #
17
18 import os
19
20 class StringPrinter:
21     "Print a C# string"
22
23     def __init__(self, val):
24         self.val = val
25
26     def to_string(self):
27         if int(self.val.cast (gdb.Type ("guint64"))) == 0:
28             return "null"
29
30         obj = self.val.cast (gdb.Type ("MonoString").pointer ()).dereference ()
31         len = obj ['length']
32         chars = obj ['chars']
33         i = 0
34         res = ['"']
35         while i < len:
36             val = (chars.cast(gdb.Type ("gint64")) + (i * 2)).cast(gdb.Type ("gunichar2").pointer ()).dereference ()
37             if val >= 256:
38                 c = "\u%X" % val
39             else:
40                 c = chr (val)
41             res.append (c)
42             i = i + 1
43         res.append ('"')
44         return ''.join (res)
45
46 def stringify_class_name(ns, name):
47     if ns == "System":
48         if name == "Byte":
49             return "byte"
50         if name == "String":
51             return "string"
52     return "%s.%s" % (ns, name)
53
54 class ArrayPrinter:
55     "Print a C# array"
56
57     def __init__(self, val, class_ns, class_name):
58         self.val = val
59         self.class_ns = class_ns
60         self.class_name = class_name
61
62     def to_string(self):
63         obj = self.val.cast (gdb.Type ("MonoArray").pointer ()).dereference ()
64         length = obj ['max_length']
65         return "%s [%d]" % (stringify_class_name (self.class_ns, self.class_name [0:len(self.class_name) - 2]), int(length))
66         
67 class ObjectPrinter:
68     "Print a C# object"
69
70     def __init__(self, val):
71         if str(val.type ())[-1] == "&":
72             self.val = val.address ().cast (gdb.Type ("MonoObject").pointer ())
73         else:
74             self.val = val.cast (gdb.Type ("MonoObject").pointer ())
75
76     class _iterator:
77         def __init__(self,obj):
78             self.obj = obj
79             self.iter = self.obj.type ().fields ().__iter__ ()
80             pass
81
82         def __iter__(self):
83             return self
84
85         def next(self):
86             field = self.iter.next ()
87             try:
88                 if str(self.obj [field.name].type ()) == "object":
89                     # Avoid recursion
90                     return (field.name, self.obj [field.name].cast (gdb.Type ("void").pointer ()))
91                 else:
92                     return (field.name, self.obj [field.name])
93             except:
94                 # Superclass
95                 return (field.name, self.obj.cast (gdb.Type ("%s" % (field.name))))
96
97     def children(self):
98         # FIXME: It would be easier if gdb.Value would support iteration itself
99         # It would also be better if we could return None
100         if int(self.val.cast (gdb.Type ("guint64"))) == 0:
101             return {}.__iter__ ()
102         try:
103             obj = self.val.dereference ()
104             class_ns = obj ['vtable'].dereference ()['klass'].dereference ()['name_space'].string ()
105             class_name = obj ['vtable'].dereference ()['klass'].dereference ()['name'].string ()
106             if class_name [-2:len(class_name)] == "[]":
107                 return {}.__iter__ ()
108             gdb_type = gdb.Type ("struct %s_%s" % (class_ns.replace (".", "_"), class_name))
109             return self._iterator(obj.cast (gdb_type))
110         except:
111             print sys.exc_info ()[0]
112             print sys.exc_info ()[1]
113             return {}.__iter__ ()
114
115     def to_string(self):
116         if int(self.val.cast (gdb.Type ("guint64"))) == 0:
117             return "null"
118         try:
119             obj = self.val.dereference ()
120             class_ns = obj ['vtable'].dereference ()['klass'].dereference ()['name_space'].string ()
121             class_name = obj ['vtable'].dereference ()['klass'].dereference ()['name'].string ()
122             if class_ns == "System" and class_name == "String":
123                 return StringPrinter (self.val).to_string ()
124             if class_name [-2:len(class_name)] == "[]":
125                 return ArrayPrinter (self.val,class_ns,class_name).to_string ()
126             if class_ns != "":
127                 try:
128                     gdb_type = gdb.Type ("struct %s.%s" % (class_ns, class_name))
129                 except:
130                     # Maybe there is no debug info for that type
131                     return "%s.%s" % (class_ns, class_name)
132                 #return obj.cast (gdb_type)
133                 return "%s.%s" % (class_ns, class_name)
134             return class_name
135         except:
136             print sys.exc_info ()[0]
137             print sys.exc_info ()[1]
138             # FIXME: This can happen because we don't have liveness information
139             return self.val.cast (gdb.Type ("guint64"))
140
141 def lookup_pretty_printer(val):
142     t = str (val.type ())
143     if t == "object":
144         return ObjectPrinter (val)
145     if t[0:5] == "class" and t[-1] == "&":
146         return ObjectPrinter (val)    
147     if t == "string":
148         return StringPrinter (val)
149     return None
150
151 def register_csharp_printers(obj):
152     "Register C# pretty-printers with objfile Obj."
153
154     if obj == None:
155         obj = gdb
156
157     obj.pretty_printers.append (lookup_pretty_printer)
158
159 register_csharp_printers (gdb.current_objfile())
160
161 class MonoSupport(object):
162
163     def __init__(self):
164         self.s_size = 0
165
166     def run_hook(self):
167         if os.access ("xdb.s", os.F_OK):
168             os.remove ("xdb.s")
169         gdb.execute ("set environment MONO_XDEBUG 1")
170         
171     def stop_hook(self):
172         # Called when the program is stopped
173         # Need to recompile+reload the xdb.s file if needed
174         # FIXME: Need to detect half-written files created when the child is
175         # interrupted while executing the xdb.s writing code
176         # FIXME: Handle appdomain unload
177         if os.access ("xdb.s", os.F_OK):
178             new_size = os.stat ("xdb.s").st_size
179             if new_size > self.s_size:
180                 sofile = "xdb.so"
181                 gdb.execute ("shell as -o xdb.o xdb.s && ld -shared -o %s xdb.o" % sofile)
182                 # FIXME: This prints messages which couldn't be turned off
183                 gdb.execute ("add-symbol-file %s 0" % sofile)
184                 self.s_size = new_size
185
186 class RunHook (gdb.Command):
187     def __init__ (self):
188         super (RunHook, self).__init__ ("hook-run", gdb.COMMAND_NONE,
189                                         gdb.COMPLETE_COMMAND, pre_hook_of="run")
190
191     def invoke(self, arg, from_tty):
192         mono_support.run_hook ()
193
194 print "Mono support loaded."
195
196 mono_support = MonoSupport ()
197
198 # This depends on the changes in gdb-python.diff to work
199 #RunHook ()
200
201 # Register our hooks
202 # This currently cannot be done from python code
203
204 exec_file = gdb.current_objfile ().filename
205 # FIXME: Is there a way to detect symbolic links ?
206 if os.stat (exec_file).st_size != os.lstat (exec_file).st_size:
207     exec_file = os.readlink (exec_file)
208 exec_dir = os.path.dirname (exec_file)
209 gdb.execute ("source %s/%s-gdbinit" % (exec_dir, os.path.basename (exec_file)))