js/src/gdb/mozilla/jsid.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/gdb/mozilla/jsid.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,67 @@
     1.4 +# Pretty-printers for JSID values.
     1.5 +
     1.6 +import gdb
     1.7 +import mozilla.prettyprinters
     1.8 +import mozilla.Root
     1.9 +
    1.10 +from mozilla.prettyprinters import pretty_printer
    1.11 +
    1.12 +# Forget any printers from previous loads of this module.
    1.13 +mozilla.prettyprinters.clear_module_printers(__name__)
    1.14 +
    1.15 +@pretty_printer('jsid')
    1.16 +class jsid(object):
    1.17 +    # Since people don't always build with macro debugging info, I can't
    1.18 +    # think of any way to avoid copying these values here, short of using
    1.19 +    # inferior calls for every operation (which, I hear, is broken from
    1.20 +    # pretty-printers in some recent GDBs).
    1.21 +    TYPE_STRING                 = 0x0
    1.22 +    TYPE_INT                    = 0x1
    1.23 +    TYPE_VOID                   = 0x2
    1.24 +    TYPE_OBJECT                 = 0x4
    1.25 +    TYPE_MASK                   = 0x7
    1.26 +
    1.27 +    def __init__(self, value, cache):
    1.28 +        self.value = value
    1.29 +        self.cache = cache
    1.30 +        self.concrete_type = self.value.type.strip_typedefs()
    1.31 +
    1.32 +    # SpiderMonkey has two alternative definitions of jsid: a typedef for
    1.33 +    # ptrdiff_t, and a struct with == and != operators defined on it.
    1.34 +    # Extract the bits from either one.
    1.35 +    def as_bits(self):
    1.36 +        if self.concrete_type.code == gdb.TYPE_CODE_STRUCT:
    1.37 +            return self.value['asBits']
    1.38 +        elif self.concrete_type.code == gdb.TYPE_CODE_INT:
    1.39 +            return self.value
    1.40 +        else:
    1.41 +            raise RuntimeError, ("definition of SpiderMonkey 'jsid' type"
    1.42 +                                 "neither struct nor integral type")
    1.43 +
    1.44 +    def to_string(self):
    1.45 +        bits = self.as_bits()
    1.46 +        tag = bits & jsid.TYPE_MASK
    1.47 +        if tag == jsid.TYPE_STRING:
    1.48 +            body = bits.cast(self.cache.JSString_ptr_t)
    1.49 +        elif tag & jsid.TYPE_INT:
    1.50 +            body = bits >> 1
    1.51 +        elif tag == jsid.TYPE_VOID:
    1.52 +            return "JSID_VOID"
    1.53 +        elif tag == jsid.TYPE_OBJECT:
    1.54 +            body = ((bits & ~jsid.TYPE_MASK)
    1.55 +                    .cast(self.cache.JSObject_ptr_t))
    1.56 +        else:
    1.57 +            body = "<unrecognized>"
    1.58 +        return '$jsid(%s)' % (body,)
    1.59 +
    1.60 +# Hard-code the referent type pretty-printer for jsid roots and handles.
    1.61 +# See the comment for mozilla.Root.Common.__init__.
    1.62 +@pretty_printer('JS::Rooted<long>')
    1.63 +def RootedJSID(value, cache):
    1.64 +    return mozilla.Root.Rooted(value, cache, jsid)
    1.65 +@pretty_printer('JS::Handle<long>')
    1.66 +def HandleJSID(value, cache):
    1.67 +    return mozilla.Root.Handle(value, cache, jsid)
    1.68 +@pretty_printer('JS::MutableHandle<long>')
    1.69 +def MutableHandleJSID(value, cache):
    1.70 +    return mozilla.Root.MutableHandle(value, cache, jsid)

mercurial