Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mono-gdb.py « gdb « data - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fc101f122f9ec0d56f2b588fdf00fc4bd3a2a959 (plain)
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
#
# Author: Zoltan Varga (vargaz@gmail.com)
# License: MIT/X11
#

#
# This is a mono support mode for gdb 7.0 and later
# Usage:
# - copy/symlink this file to the directory where the mono executable lives.
# - run mono under gdb, or attach to a mono process started with --debug=gdb using gdb.
#

import os

class StringPrinter:
    "Print a C# string"

    def __init__(self, val):
        self.val = val

    def to_string(self):
        if int(self.val.cast (gdb.lookup_type ("guint64"))) == 0:
            return "null"

        obj = self.val.cast (gdb.lookup_type ("MonoString").pointer ()).dereference ()
        len = obj ['length']
        chars = obj ['chars']
        i = 0
        res = ['"']
        while i < len:
            val = (chars.cast(gdb.lookup_type ("gint64")) + (i * 2)).cast(gdb.lookup_type ("gunichar2").pointer ()).dereference ()
            if val >= 256:
                c = "\u%X" % val
            else:
                c = chr (val)
            res.append (c)
            i = i + 1
        res.append ('"')
        return ''.join (res)

def stringify_class_name(ns, name):
    if ns == "System":
        if name == "Byte":
            return "byte"
        if name == "String":
            return "string"
    if ns == "":
        return name
    else:
        return "%s.%s" % (ns, name)

class ArrayPrinter:
    "Print a C# array"

    def __init__(self, val, class_ns, class_name):
        self.val = val
        self.class_ns = class_ns
        self.class_name = class_name

    def to_string(self):
        obj = self.val.cast (gdb.lookup_type ("MonoArray").pointer ()).dereference ()
        length = obj ['max_length']
        return "%s [%d]" % (stringify_class_name (self.class_ns, self.class_name [0:len(self.class_name) - 2]), int(length))
        
class ObjectPrinter:
    "Print a C# object"

    def __init__(self, val):
        if str(val.type)[-1] == "&":
            self.val = val.address.cast (gdb.lookup_type ("MonoObject").pointer ())
        else:
            self.val = val.cast (gdb.lookup_type ("MonoObject").pointer ())

    class _iterator:
        def __init__(self,obj):
            self.obj = obj
            self.iter = self.obj.type.fields ().__iter__ ()
            pass

        def __iter__(self):
            return self

        def next(self):
            field = self.iter.next ()
            try:
                if str(self.obj [field.name].type) == "object":
                    # Avoid recursion
                    return (field.name, self.obj [field.name].cast (gdb.lookup_type ("void").pointer ()))
                else:
                    return (field.name, self.obj [field.name])
            except:
                # Superclass
                return (field.name, self.obj.cast (gdb.lookup_type ("%s" % (field.name))))

    def children(self):
        # FIXME: It would be easier if gdb.Value would support iteration itself
        # It would also be better if we could return None
        if int(self.val.cast (gdb.lookup_type ("guint64"))) == 0:
            return {}.__iter__ ()
        try:
            obj = self.val.dereference ()
            class_ns = obj ['vtable'].dereference ()['klass'].dereference ()['name_space'].string ()
            class_name = obj ['vtable'].dereference ()['klass'].dereference ()['name'].string ()
            if class_name [-2:len(class_name)] == "[]":
                return {}.__iter__ ()
            gdb_type = gdb.lookup_type ("struct %s_%s" % (class_ns.replace (".", "_"), class_name))
            return self._iterator(obj.cast (gdb_type))
        except:
            print sys.exc_info ()[0]
            print sys.exc_info ()[1]
            return {}.__iter__ ()

    def to_string(self):
        if int(self.val.cast (gdb.lookup_type ("guint64"))) == 0:
            return "null"
        try:
            obj = self.val.dereference ()
            class_ns = obj ['vtable'].dereference ()['klass'].dereference ()['name_space'].string ()
            class_name = obj ['vtable'].dereference ()['klass'].dereference ()['name'].string ()
            if class_ns == "System" and class_name == "String":
                return StringPrinter (self.val).to_string ()
            if class_name [-2:len(class_name)] == "[]":
                return ArrayPrinter (self.val,class_ns,class_name).to_string ()
            if class_ns != "":
                try:
                    gdb_type = gdb.lookup_type ("struct %s.%s" % (class_ns, class_name))
                except:
                    # Maybe there is no debug info for that type
                    return "%s.%s" % (class_ns, class_name)
                #return obj.cast (gdb_type)
                return "%s.%s" % (class_ns, class_name)
            return class_name
        except:
            print sys.exc_info ()[0]
            print sys.exc_info ()[1]
            # FIXME: This can happen because we don't have liveness information
            return self.val.cast (gdb.lookup_type ("guint64"))
        
class MonoMethodPrinter:
    "Print a MonoMethod structure"

    def __init__(self, val):
        self.val = val

    def to_string(self):
        if int(self.val.cast (gdb.lookup_type ("guint64"))) == 0:
            return "0x0"
        val = self.val.dereference ()
        klass = val ["klass"].dereference ()
        class_name = stringify_class_name (klass ["name_space"].string (), klass ["name"].string ())
        return "\"%s:%s ()\"" % (class_name, val ["name"].string ())
        # This returns more info but requires calling into the inferior
        #return "\"%s\"" % (gdb.parse_and_eval ("mono_method_full_name (%s, 1)" % (str (int (self.val.cast (gdb.lookup_type ("guint64")))))).string ())

class MonoClassPrinter:
    "Print a MonoClass structure"

    def __init__(self, val):
        self.val = val

    def to_string_inner(self, add_quotes):
        if int(self.val.cast (gdb.lookup_type ("guint64"))) == 0:
            return "0x0"
        klass = self.val.dereference ()
        class_name = stringify_class_name (klass ["name_space"].string (), klass ["name"].string ())
        if add_quotes:
            return "\"%s\"" % (class_name)
        else:
            return class_name
        # This returns more info but requires calling into the inferior
        #return "\"%s\"" % (gdb.parse_and_eval ("mono_type_full_name (&((MonoClass*)%s)->byval_arg)" % (str (int ((self.val).cast (gdb.lookup_type ("guint64")))))))

    def to_string(self):
        try:
            return self.to_string_inner (True)
        except:
            #print sys.exc_info ()[0]
            #print sys.exc_info ()[1]
            return str(self.val.cast (gdb.lookup_type ("gpointer")))

class MonoGenericInstPrinter:
    "Print a MonoGenericInst structure"

    def __init__(self, val):
        self.val = val

    def to_string(self):
        inst = self.val.dereference ()
        inst_len = inst ["type_argc"]
        inst_args = inst ["type_argv"]
        inst_str = ""
        for i in range(0, inst_len):
            print inst_args
            type_printer = MonoTypePrinter (inst_args [i])
            if i > 0:
                inst_str = inst_str + ", "
            inst_str = inst_str + type_printer.to_string ()
        return inst_str

class MonoGenericClassPrinter:
    "Print a MonoGenericClass structure"

    def __init__(self, val):
        self.val = val

    def to_string_inner(self):
        gclass = self.val.dereference ()
        container_str = str(gclass ["container_class"])
        class_inst = gclass ["context"]["class_inst"]
        class_inst_str = ""
        if int(class_inst.cast (gdb.lookup_type ("guint64"))) != 0:
            class_inst_str  = str(class_inst)
        method_inst = gclass ["context"]["method_inst"]
        method_inst_str = ""
        if int(method_inst.cast (gdb.lookup_type ("guint64"))) != 0:
            method_inst_str  = str(method_inst)
        return "%s, [%s], [%s]>" % (container_str, class_inst_str, method_inst_str)

    def to_string(self):
        try:
            return self.to_string_inner ()
        except:
            #print sys.exc_info ()[0]
            #print sys.exc_info ()[1]
            return str(self.val.cast (gdb.lookup_type ("gpointer")))

class MonoTypePrinter:
    "Print a MonoType structure"

    def __init__(self, val):
        self.val = val

    def to_string_inner(self, csharp):
        try:
            t = self.val.dereference ()

            kind = str (t ["type"]).replace ("MONO_TYPE_", "").lower ()
            info = ""

            if kind == "class":
                p = MonoClassPrinter(t ["data"]["klass"])
                info = p.to_string ()
            elif kind == "genericinst":
                info = str(t ["data"]["generic_class"])

            if info != "":
                return "{%s, %s}" % (kind, info)
            else:
                return "{%s}" % (kind)
        except:
            #print sys.exc_info ()[0]
            #print sys.exc_info ()[1]
            return str(self.val.cast (gdb.lookup_type ("gpointer")))

    def to_string(self):
        return self.to_string_inner (False)

class MonoMethodRgctxPrinter:
    "Print a MonoMethodRgctx structure"

    def __init__(self, val):
        self.val = val

    def to_string(self):
        rgctx = self.val.dereference ()
        klass = rgctx ["class_vtable"].dereference () ["klass"]
        klass_printer = MonoClassPrinter (klass)
        inst = rgctx ["method_inst"].dereference ()
        inst_len = inst ["type_argc"]
        inst_args = inst ["type_argv"]
        inst_str = ""
        for i in range(0, inst_len):
            print inst_args
            type_printer = MonoTypePrinter (inst_args [i])
            if i > 0:
                inst_str = inst_str + ", "
            inst_str = inst_str + type_printer.to_string ()
        return "MRGCTX[%s, [%s]]" % (klass_printer.to_string(), inst_str)

def lookup_pretty_printer(val):
    t = str (val.type)
    if t == "object":
        return ObjectPrinter (val)
    if t[0:5] == "class" and t[-1] == "&":
        return ObjectPrinter (val)    
    if t == "string":
        return StringPrinter (val)
    if t == "MonoMethod *":
        return MonoMethodPrinter (val)
    if t == "MonoClass *":
        return MonoClassPrinter (val)
    if t == "MonoType *":
        return MonoTypePrinter (val)
    if t == "MonoGenericInst *":
        return MonoGenericInstPrinter (val)
    if t == "MonoGenericClass *":
        return MonoGenericClassPrinter (val)
    if t == "MonoMethodRuntimeGenericContext *":
        return MonoMethodRgctxPrinter (val)
    return None

def register_csharp_printers(obj):
    "Register C# pretty-printers with objfile Obj."

    if obj == None:
        obj = gdb

    obj.pretty_printers.append (lookup_pretty_printer)

# This command will flush the debugging info collected by the runtime
class XdbCommand (gdb.Command):
    def __init__ (self):
        super (XdbCommand, self).__init__ ("xdb", gdb.COMMAND_NONE,
                                           gdb.COMPLETE_COMMAND)

    def invoke(self, arg, from_tty):
        gdb.execute ("call mono_xdebug_flush ()")

register_csharp_printers (gdb.current_objfile())

XdbCommand ()

gdb.execute ("set environment MONO_XDEBUG gdb")

print "Mono support loaded."