michael@0: # Pretty-printers and utilities for SpiderMonkey rooting templates: michael@0: # Rooted, Handle, MutableHandle, etc. michael@0: michael@0: import mozilla.prettyprinters michael@0: from mozilla.prettyprinters import pretty_printer, template_pretty_printer michael@0: michael@0: # Forget any printers from previous loads of this module. michael@0: mozilla.prettyprinters.clear_module_printers(__name__) michael@0: michael@0: # Common base class for all the rooting template pretty-printers. All these michael@0: # templates have one member holding the referent (or a pointer to it), so michael@0: # there's not much to it. michael@0: class Common(object): michael@0: # The name of the template member holding the referent. michael@0: member = 'ptr' michael@0: michael@0: # If True, this is a handle type, and should be dereferenced. If False, michael@0: # the template member holds the referent directly. michael@0: handle = False michael@0: michael@0: # Initialize a pretty-printer for |value|, using |cache|. michael@0: # michael@0: # If given, |content_printer| is a pretty-printer constructor to use for michael@0: # this handle/root/etc.'s referent. Usually, we can just omit this argument michael@0: # and let GDB choose a pretty-printer for the referent given its type, but michael@0: # when the referent is a typedef of an integral type (say, |jsid| in a michael@0: # non-|DEBUG| build), the GNU toolchain (at least) loses the typedef name, michael@0: # and all we know about the referent is its fundamental integer type --- michael@0: # |JS::Rooted|, for example, appears in GDB as |JS::Rooted| --- michael@0: # and we are left with no way to choose a meaningful pretty-printer based on michael@0: # the type of the referent alone. However, because we know that the only michael@0: # integer type for which |JS::Rooted| is likely to be instantiated is michael@0: # |jsid|, we *can* register a pretty-printer constructor for the full michael@0: # instantiation |JS::Rooted|. That constructor creates a |JS::Rooted| michael@0: # pretty-printer, and explicitly specifies the constructor for the referent, michael@0: # using this initializer's |content_printer| argument. michael@0: def __init__(self, value, cache, content_printer=None): michael@0: self.value = value michael@0: self.cache = cache michael@0: self.content_printer = content_printer michael@0: def to_string(self): michael@0: ptr = self.value[self.member] michael@0: if self.handle: michael@0: ptr = ptr.dereference() michael@0: if self.content_printer: michael@0: return self.content_printer(ptr, self.cache).to_string() michael@0: else: michael@0: # As of 2012-11, GDB suppresses printing pointers in replacement michael@0: # values; see http://sourceware.org/ml/gdb/2012-11/msg00055.html michael@0: # That means that simply returning the 'ptr' member won't work. michael@0: # Instead, just invoke GDB's formatter ourselves. michael@0: return str(ptr) michael@0: michael@0: @template_pretty_printer("JS::Rooted") michael@0: class Rooted(Common): michael@0: pass michael@0: michael@0: @template_pretty_printer("JS::Handle") michael@0: class Handle(Common): michael@0: handle = True michael@0: michael@0: @template_pretty_printer("JS::MutableHandle") michael@0: class MutableHandle(Common): michael@0: handle = True michael@0: michael@0: @template_pretty_printer("js::EncapsulatedPtr") michael@0: class EncapsulatedPtr(Common): michael@0: member = 'value' michael@0: michael@0: @pretty_printer("js::EncapsulatedValue") michael@0: class EncapsulatedValue(Common): michael@0: member = 'value' michael@0: michael@0: @pretty_printer("js::BarrieredValue") michael@0: class BarrieredValue(Common): michael@0: member = 'value' michael@0: michael@0: # Return the referent of a HeapPtr, Rooted, or Handle. michael@0: def deref(root): michael@0: tag = root.type.strip_typedefs().tag michael@0: if not tag: michael@0: raise TypeError("Can't dereference type with no structure tag: %s" % (root.type,)) michael@0: elif tag.startswith('js::HeapPtr<'): michael@0: return root['value'] michael@0: elif tag.startswith('JS::Rooted<'): michael@0: return root['ptr'] michael@0: elif tag.startswith('JS::Handle<'): michael@0: return root['ptr'] michael@0: else: michael@0: raise NotImplementedError