js/src/gdb/mozilla/Root.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/gdb/mozilla/Root.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,90 @@
     1.4 +# Pretty-printers and utilities for SpiderMonkey rooting templates:
     1.5 +# Rooted, Handle, MutableHandle, etc.
     1.6 +
     1.7 +import mozilla.prettyprinters
     1.8 +from mozilla.prettyprinters import pretty_printer, template_pretty_printer
     1.9 +
    1.10 +# Forget any printers from previous loads of this module.
    1.11 +mozilla.prettyprinters.clear_module_printers(__name__)
    1.12 +
    1.13 +# Common base class for all the rooting template pretty-printers. All these
    1.14 +# templates have one member holding the referent (or a pointer to it), so
    1.15 +# there's not much to it.
    1.16 +class Common(object):
    1.17 +    # The name of the template member holding the referent.
    1.18 +    member = 'ptr'
    1.19 +
    1.20 +    # If True, this is a handle type, and should be dereferenced. If False,
    1.21 +    # the template member holds the referent directly.
    1.22 +    handle = False
    1.23 +
    1.24 +    # Initialize a pretty-printer for |value|, using |cache|.
    1.25 +    #
    1.26 +    # If given, |content_printer| is a pretty-printer constructor to use for
    1.27 +    # this handle/root/etc.'s referent. Usually, we can just omit this argument
    1.28 +    # and let GDB choose a pretty-printer for the referent given its type, but
    1.29 +    # when the referent is a typedef of an integral type (say, |jsid| in a
    1.30 +    # non-|DEBUG| build), the GNU toolchain (at least) loses the typedef name,
    1.31 +    # and all we know about the referent is its fundamental integer type ---
    1.32 +    # |JS::Rooted<jsid>|, for example, appears in GDB as |JS::Rooted<long>| ---
    1.33 +    # and we are left with no way to choose a meaningful pretty-printer based on
    1.34 +    # the type of the referent alone. However, because we know that the only
    1.35 +    # integer type for which |JS::Rooted| is likely to be instantiated is
    1.36 +    # |jsid|, we *can* register a pretty-printer constructor for the full
    1.37 +    # instantiation |JS::Rooted<long>|. That constructor creates a |JS::Rooted|
    1.38 +    # pretty-printer, and explicitly specifies the constructor for the referent,
    1.39 +    # using this initializer's |content_printer| argument.
    1.40 +    def __init__(self, value, cache, content_printer=None):
    1.41 +        self.value = value
    1.42 +        self.cache = cache
    1.43 +        self.content_printer = content_printer
    1.44 +    def to_string(self):
    1.45 +        ptr = self.value[self.member]
    1.46 +        if self.handle:
    1.47 +            ptr = ptr.dereference()
    1.48 +        if self.content_printer:
    1.49 +            return self.content_printer(ptr, self.cache).to_string()
    1.50 +        else:
    1.51 +            # As of 2012-11, GDB suppresses printing pointers in replacement
    1.52 +            # values; see http://sourceware.org/ml/gdb/2012-11/msg00055.html
    1.53 +            # That means that simply returning the 'ptr' member won't work.
    1.54 +            # Instead, just invoke GDB's formatter ourselves.
    1.55 +            return str(ptr)
    1.56 +
    1.57 +@template_pretty_printer("JS::Rooted")
    1.58 +class Rooted(Common):
    1.59 +    pass
    1.60 +
    1.61 +@template_pretty_printer("JS::Handle")
    1.62 +class Handle(Common):
    1.63 +    handle = True
    1.64 +
    1.65 +@template_pretty_printer("JS::MutableHandle")
    1.66 +class MutableHandle(Common):
    1.67 +    handle = True
    1.68 +
    1.69 +@template_pretty_printer("js::EncapsulatedPtr")
    1.70 +class EncapsulatedPtr(Common):
    1.71 +    member = 'value'
    1.72 +
    1.73 +@pretty_printer("js::EncapsulatedValue")
    1.74 +class EncapsulatedValue(Common):
    1.75 +    member = 'value'
    1.76 +
    1.77 +@pretty_printer("js::BarrieredValue")
    1.78 +class BarrieredValue(Common):
    1.79 +    member = 'value'
    1.80 +
    1.81 +# Return the referent of a HeapPtr, Rooted, or Handle.
    1.82 +def deref(root):
    1.83 +    tag = root.type.strip_typedefs().tag
    1.84 +    if not tag:
    1.85 +        raise TypeError("Can't dereference type with no structure tag: %s" % (root.type,))
    1.86 +    elif tag.startswith('js::HeapPtr<'):
    1.87 +        return root['value']
    1.88 +    elif tag.startswith('JS::Rooted<'):
    1.89 +        return root['ptr']
    1.90 +    elif tag.startswith('JS::Handle<'):
    1.91 +        return root['ptr']
    1.92 +    else:
    1.93 +        raise NotImplementedError

mercurial