Debian Codegraph @

js/src/gdb/mozilla/Root.py

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