js/src/gdb/mozilla/Root.py

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 # Pretty-printers and utilities for SpiderMonkey rooting templates:
michael@0 2 # Rooted, Handle, MutableHandle, etc.
michael@0 3
michael@0 4 import mozilla.prettyprinters
michael@0 5 from mozilla.prettyprinters import pretty_printer, template_pretty_printer
michael@0 6
michael@0 7 # Forget any printers from previous loads of this module.
michael@0 8 mozilla.prettyprinters.clear_module_printers(__name__)
michael@0 9
michael@0 10 # Common base class for all the rooting template pretty-printers. All these
michael@0 11 # templates have one member holding the referent (or a pointer to it), so
michael@0 12 # there's not much to it.
michael@0 13 class Common(object):
michael@0 14 # The name of the template member holding the referent.
michael@0 15 member = 'ptr'
michael@0 16
michael@0 17 # If True, this is a handle type, and should be dereferenced. If False,
michael@0 18 # the template member holds the referent directly.
michael@0 19 handle = False
michael@0 20
michael@0 21 # Initialize a pretty-printer for |value|, using |cache|.
michael@0 22 #
michael@0 23 # If given, |content_printer| is a pretty-printer constructor to use for
michael@0 24 # this handle/root/etc.'s referent. Usually, we can just omit this argument
michael@0 25 # and let GDB choose a pretty-printer for the referent given its type, but
michael@0 26 # when the referent is a typedef of an integral type (say, |jsid| in a
michael@0 27 # non-|DEBUG| build), the GNU toolchain (at least) loses the typedef name,
michael@0 28 # and all we know about the referent is its fundamental integer type ---
michael@0 29 # |JS::Rooted<jsid>|, for example, appears in GDB as |JS::Rooted<long>| ---
michael@0 30 # and we are left with no way to choose a meaningful pretty-printer based on
michael@0 31 # the type of the referent alone. However, because we know that the only
michael@0 32 # integer type for which |JS::Rooted| is likely to be instantiated is
michael@0 33 # |jsid|, we *can* register a pretty-printer constructor for the full
michael@0 34 # instantiation |JS::Rooted<long>|. That constructor creates a |JS::Rooted|
michael@0 35 # pretty-printer, and explicitly specifies the constructor for the referent,
michael@0 36 # using this initializer's |content_printer| argument.
michael@0 37 def __init__(self, value, cache, content_printer=None):
michael@0 38 self.value = value
michael@0 39 self.cache = cache
michael@0 40 self.content_printer = content_printer
michael@0 41 def to_string(self):
michael@0 42 ptr = self.value[self.member]
michael@0 43 if self.handle:
michael@0 44 ptr = ptr.dereference()
michael@0 45 if self.content_printer:
michael@0 46 return self.content_printer(ptr, self.cache).to_string()
michael@0 47 else:
michael@0 48 # As of 2012-11, GDB suppresses printing pointers in replacement
michael@0 49 # values; see http://sourceware.org/ml/gdb/2012-11/msg00055.html
michael@0 50 # That means that simply returning the 'ptr' member won't work.
michael@0 51 # Instead, just invoke GDB's formatter ourselves.
michael@0 52 return str(ptr)
michael@0 53
michael@0 54 @template_pretty_printer("JS::Rooted")
michael@0 55 class Rooted(Common):
michael@0 56 pass
michael@0 57
michael@0 58 @template_pretty_printer("JS::Handle")
michael@0 59 class Handle(Common):
michael@0 60 handle = True
michael@0 61
michael@0 62 @template_pretty_printer("JS::MutableHandle")
michael@0 63 class MutableHandle(Common):
michael@0 64 handle = True
michael@0 65
michael@0 66 @template_pretty_printer("js::EncapsulatedPtr")
michael@0 67 class EncapsulatedPtr(Common):
michael@0 68 member = 'value'
michael@0 69
michael@0 70 @pretty_printer("js::EncapsulatedValue")
michael@0 71 class EncapsulatedValue(Common):
michael@0 72 member = 'value'
michael@0 73
michael@0 74 @pretty_printer("js::BarrieredValue")
michael@0 75 class BarrieredValue(Common):
michael@0 76 member = 'value'
michael@0 77
michael@0 78 # Return the referent of a HeapPtr, Rooted, or Handle.
michael@0 79 def deref(root):
michael@0 80 tag = root.type.strip_typedefs().tag
michael@0 81 if not tag:
michael@0 82 raise TypeError("Can't dereference type with no structure tag: %s" % (root.type,))
michael@0 83 elif tag.startswith('js::HeapPtr<'):
michael@0 84 return root['value']
michael@0 85 elif tag.startswith('JS::Rooted<'):
michael@0 86 return root['ptr']
michael@0 87 elif tag.startswith('JS::Handle<'):
michael@0 88 return root['ptr']
michael@0 89 else:
michael@0 90 raise NotImplementedError

mercurial