Debian Codegraph @

js/src/gdb/mozilla/ExecutableAllocator.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 """
6 All jitted code is allocated via the ExecutableAllocator class. Make GDB aware
7 of them, such that we can query for pages which are containing code which are
8 allocated by the Jits.
9 """
10
11 import gdb
12
13 import mozilla.prettyprinters
14 from mozilla.prettyprinters import pretty_printer, ptr_pretty_printer
15
16 # Forget any printers from previous loads of this module.
17 mozilla.prettyprinters.clear_module_printers(__name__)
18
19
20 class jsjitExecutableAllocatorCache:
21 """Cache information about the ExecutableAllocator type for this objfile."""
22
23 def __init__(self):
24 self.d = None
25
26 def __getattr__(self, name):
27 if self.d is None:
28 self.initialize()
29 return self.d[name]
30
31 def initialize(self):
32 self.d = {}
33 self.d["ExecutableAllocator"] = gdb.lookup_type("js::jit::ExecutableAllocator")
34 self.d["ExecutablePool"] = gdb.lookup_type("js::jit::ExecutablePool")
35 self.d["HashNumber"] = gdb.lookup_type("mozilla::HashNumber")
36
37
38 @pretty_printer("js::jit::ExecutableAllocator")
39 class jsjitExecutableAllocator:
40 def __init__(self, value, cache):
41 if not cache.mod_ExecutableAllocator:
42 cache.mod_ExecutableAllocator = jsjitExecutableAllocatorCache()
43 self.value = value
44 self.cache = cache.mod_ExecutableAllocator
45
46 def to_string(self):
47 return "ExecutableAllocator([%s])" % ", ".join([str(x) for x in self])
48
49 def __iter__(self):
50 return self.PoolIterator(self)
51
52 class PoolIterator:
53 def __init__(self, allocator):
54 self.allocator = allocator
55 self.entryType = allocator.cache.ExecutablePool.pointer()
56 self.hashNumType = allocator.cache.HashNumber
57 # Emulate the HashSet::Range
58 self.table = allocator.value["m_pools"]["mImpl"]["mTable"]
59 self.index = 0
60 kHashNumberBits = 32
61 hashShift = allocator.value["m_pools"]["mImpl"]["mHashShift"]
62 self.capacity = 1 << (kHashNumberBits - hashShift)
63 if self.table == 0:
64 self.capacity = 0
65 # auto hashes = reinterpret_cast<HashNumber*>(mTable);
66 self.hashes = self.table.cast(self.hashNumType.pointer())
67 # auto entries = reinterpret_cast<Entry*>(&hashes[capacity()]);
68 self.entries = (self.hashes + self.capacity).cast(self.entryType.pointer())
69
70 def __iter__(self):
71 return self
72
73 def next(self):
74 return self.__next__()
75
76 def __next__(self):
77 cur = self.index
78 if cur >= self.capacity:
79 raise StopIteration()
80 self.index = self.index + 1
81 if self.hashes[cur] > 1: # table[i]->isLive()
82 return self.entries[cur]
83 return self.__next__()
84
85
86 @ptr_pretty_printer("js::jit::ExecutablePool")
87 class jsjitExecutablePool(mozilla.prettyprinters.Pointer):
88 def __init__(self, value, cache):
89 if not cache.mod_ExecutableAllocator:
90 cache.mod_ExecutableAllocator = jsjitExecutableAllocatorCache()
91 self.value = value
92 self.cache = cache.mod_ExecutableAllocator
93
94 def to_string(self):
95 pages = self.value["m_allocation"]["pages"]
96 size = self.value["m_allocation"]["size"]
97 return "ExecutablePool %08x-%08x" % (pages, pages + size)