summaryrefslogtreecommitdiffstats
path: root/js/src/gdb/mozilla/jsid.py
blob: b7c0e183bdad3fe59293bef2da1dc9c26a9bd652 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Pretty-printers for JSID values.

import gdb
import mozilla.prettyprinters
import mozilla.Root

from mozilla.prettyprinters import pretty_printer

# Forget any printers from previous loads of this module.
mozilla.prettyprinters.clear_module_printers(__name__)

@pretty_printer('jsid')
class jsid(object):
    # Since people don't always build with macro debugging info, I can't
    # think of any way to avoid copying these values here, short of using
    # inferior calls for every operation (which, I hear, is broken from
    # pretty-printers in some recent GDBs).
    TYPE_STRING                 = 0x0
    TYPE_INT                    = 0x1
    TYPE_VOID                   = 0x2
    TYPE_SYMBOL                 = 0x4
    TYPE_MASK                   = 0x7

    def __init__(self, value, cache):
        self.value = value
        self.cache = cache
        self.concrete_type = self.value.type.strip_typedefs()

    # SpiderMonkey has two alternative definitions of jsid: a typedef for
    # ptrdiff_t, and a struct with == and != operators defined on it.
    # Extract the bits from either one.
    def as_bits(self):
        if self.concrete_type.code == gdb.TYPE_CODE_STRUCT:
            return self.value['asBits']
        elif self.concrete_type.code == gdb.TYPE_CODE_INT:
            return self.value
        else:
            raise RuntimeError("definition of SpiderMonkey 'jsid' type"
                               "neither struct nor integral type")

    def to_string(self):
        bits = self.as_bits()
        tag = bits & jsid.TYPE_MASK
        if tag == jsid.TYPE_STRING:
            body = bits.cast(self.cache.JSString_ptr_t)
        elif tag & jsid.TYPE_INT:
            body = bits >> 1
        elif tag == jsid.TYPE_VOID:
            return "JSID_VOID"
        elif tag == jsid.TYPE_SYMBOL:
            if bits == jsid.TYPE_SYMBOL:
                return "JSID_EMPTY"
            body = ((bits & ~jsid.TYPE_MASK)
                    .cast(self.cache.JSSymbol_ptr_t))
        else:
            body = "<unrecognized>"
        return '$jsid(%s)' % (body,)

# Hard-code the referent type pretty-printer for jsid roots and handles.
# See the comment for mozilla.Root.Common.__init__.
@pretty_printer('JS::Rooted<long>')
def RootedJSID(value, cache):
    return mozilla.Root.Rooted(value, cache, jsid)
@pretty_printer('JS::Handle<long>')
def HandleJSID(value, cache):
    return mozilla.Root.Handle(value, cache, jsid)
@pretty_printer('JS::MutableHandle<long>')
def MutableHandleJSID(value, cache):
    return mozilla.Root.MutableHandle(value, cache, jsid)