|
1 # Pretty-printers for JSID values. |
|
2 |
|
3 import gdb |
|
4 import mozilla.prettyprinters |
|
5 import mozilla.Root |
|
6 |
|
7 from mozilla.prettyprinters import pretty_printer |
|
8 |
|
9 # Forget any printers from previous loads of this module. |
|
10 mozilla.prettyprinters.clear_module_printers(__name__) |
|
11 |
|
12 @pretty_printer('jsid') |
|
13 class jsid(object): |
|
14 # Since people don't always build with macro debugging info, I can't |
|
15 # think of any way to avoid copying these values here, short of using |
|
16 # inferior calls for every operation (which, I hear, is broken from |
|
17 # pretty-printers in some recent GDBs). |
|
18 TYPE_STRING = 0x0 |
|
19 TYPE_INT = 0x1 |
|
20 TYPE_VOID = 0x2 |
|
21 TYPE_OBJECT = 0x4 |
|
22 TYPE_MASK = 0x7 |
|
23 |
|
24 def __init__(self, value, cache): |
|
25 self.value = value |
|
26 self.cache = cache |
|
27 self.concrete_type = self.value.type.strip_typedefs() |
|
28 |
|
29 # SpiderMonkey has two alternative definitions of jsid: a typedef for |
|
30 # ptrdiff_t, and a struct with == and != operators defined on it. |
|
31 # Extract the bits from either one. |
|
32 def as_bits(self): |
|
33 if self.concrete_type.code == gdb.TYPE_CODE_STRUCT: |
|
34 return self.value['asBits'] |
|
35 elif self.concrete_type.code == gdb.TYPE_CODE_INT: |
|
36 return self.value |
|
37 else: |
|
38 raise RuntimeError, ("definition of SpiderMonkey 'jsid' type" |
|
39 "neither struct nor integral type") |
|
40 |
|
41 def to_string(self): |
|
42 bits = self.as_bits() |
|
43 tag = bits & jsid.TYPE_MASK |
|
44 if tag == jsid.TYPE_STRING: |
|
45 body = bits.cast(self.cache.JSString_ptr_t) |
|
46 elif tag & jsid.TYPE_INT: |
|
47 body = bits >> 1 |
|
48 elif tag == jsid.TYPE_VOID: |
|
49 return "JSID_VOID" |
|
50 elif tag == jsid.TYPE_OBJECT: |
|
51 body = ((bits & ~jsid.TYPE_MASK) |
|
52 .cast(self.cache.JSObject_ptr_t)) |
|
53 else: |
|
54 body = "<unrecognized>" |
|
55 return '$jsid(%s)' % (body,) |
|
56 |
|
57 # Hard-code the referent type pretty-printer for jsid roots and handles. |
|
58 # See the comment for mozilla.Root.Common.__init__. |
|
59 @pretty_printer('JS::Rooted<long>') |
|
60 def RootedJSID(value, cache): |
|
61 return mozilla.Root.Rooted(value, cache, jsid) |
|
62 @pretty_printer('JS::Handle<long>') |
|
63 def HandleJSID(value, cache): |
|
64 return mozilla.Root.Handle(value, cache, jsid) |
|
65 @pretty_printer('JS::MutableHandle<long>') |
|
66 def MutableHandleJSID(value, cache): |
|
67 return mozilla.Root.MutableHandle(value, cache, jsid) |