| 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 |
# mozilla/prettyprinters.py --- infrastructure for SpiderMonkey's auto-loaded pretty-printers. |
| 6 |
|
| 7 |
import re |
| 8 |
|
| 9 |
import gdb |
| 10 |
|
| 11 |
# Decorators for declaring pretty-printers. |
| 12 |
# |
| 13 |
# In each case, the decoratee should be a SpiderMonkey-style pretty-printer |
| 14 |
# factory, taking both a gdb.Value instance and a TypeCache instance as |
| 15 |
# arguments; see TypeCache, below. |
| 16 |
|
| 17 |
|
| 18 |
def check_for_reused_pretty_printer(fn): |
| 19 |
# Check that |fn| hasn't been registered as a pretty-printer under some |
| 20 |
# other name already. (The 'enabled' flags used by GDB's |
| 21 |
# 'enable/disable/info pretty-printer' commands are simply stored as |
| 22 |
# properties of the function objects themselves, so a single function |
| 23 |
# object can't carry the 'enabled' flags for two different printers.) |
| 24 |
if hasattr(fn, "enabled"): |
| 25 |
raise RuntimeError("pretty-printer function %r registered more than once" % fn) |
| 26 |
|
| 27 |
|
| 28 |
# a dictionary mapping gdb.Type tags to pretty-printer functions. |
| 29 |
printers_by_tag = {} |
| 30 |
|
| 31 |
# A decorator: add the decoratee as a pretty-printer lookup function for types |
| 32 |
# named |type_name|. |
| 33 |
|
| 34 |
|
| 35 |
def pretty_printer(type_name): |
| 36 |
def add(fn): |
| 37 |
check_for_reused_pretty_printer(fn) |
| 38 |
add_to_subprinter_list(fn, type_name) |
| 39 |
printers_by_tag[type_name] = fn |
| 40 |
return fn |
| 41 |
|
| 42 |
return add |
| 43 |
|
| 44 |
|
| 45 |
# a dictionary mapping gdb.Type tags to pretty-printer functions for pointers to |
| 46 |
# that type. |
| 47 |
ptr_printers_by_tag = {} |
| 48 |
|
| 49 |
# A decorator: add the decoratee as a pretty-printer lookup function for |
| 50 |
# pointers to types named |type_name|. |
| 51 |
|
| 52 |
|
| 53 |
def ptr_pretty_printer(type_name): |
| 54 |
def add(fn): |
| 55 |
check_for_reused_pretty_printer(fn) |
| 56 |
add_to_subprinter_list(fn, "ptr-to-" + type_name) |
| 57 |
ptr_printers_by_tag[type_name] = fn |
| 58 |
return fn |
| 59 |
|
| 60 |
return add |
| 61 |
|
| 62 |
|
| 63 |
# a dictionary mapping gdb.Type tags to pretty-printer functions for |
| 64 |
# references to that type. |
| 65 |
ref_printers_by_tag = {} |
| 66 |
|
| 67 |
# A decorator: add the decoratee as a pretty-printer lookup function for |
| 68 |
# references to instances of types named |type_name|. |
| 69 |
|
| 70 |
|
| 71 |
def ref_pretty_printer(type_name): |
| 72 |
def add(fn): |
| 73 |
check_for_reused_pretty_printer(fn) |
| 74 |
add_to_subprinter_list(fn, "ref-to-" + type_name) |
| 75 |
ref_printers_by_tag[type_name] = fn |
| 76 |
return fn |
| 77 |
|
| 78 |
return add |
| 79 |
|
| 80 |
|
| 81 |
# a dictionary mapping the template name portion of gdb.Type tags to |
| 82 |
# pretty-printer functions for instantiations of that template. |
| 83 |
template_printers_by_tag = {} |
| 84 |
|
| 85 |
# A decorator: add the decoratee as a pretty-printer lookup function for |
| 86 |
# instantiations of templates named |template_name|. |
| 87 |
|
| 88 |
|
| 89 |
def template_pretty_printer(template_name): |
| 90 |
def add(fn): |
| 91 |
check_for_reused_pretty_printer(fn) |
| 92 |
add_to_subprinter_list(fn, "instantiations-of-" + template_name) |
| 93 |
template_printers_by_tag[template_name] = fn |
| 94 |
return fn |
| 95 |
|
| 96 |
return add |
| 97 |
|
| 98 |
|
| 99 |
# A list of (REGEXP, PRINTER) pairs, such that if REGEXP (a RegexObject) |
| 100 |
# matches the result of converting a gdb.Value's type to a string, then |
| 101 |
# PRINTER is a pretty-printer lookup function that will probably like that |
| 102 |
# value. |
| 103 |
printers_by_regexp = [] |
| 104 |
|
| 105 |
# A decorator: add the decoratee as a pretty-printer factory for types |
| 106 |
# that, when converted to a string, match |pattern|. Use |name| as the |
| 107 |
# pretty-printer's name, when listing, enabling and disabling. |
| 108 |
|
| 109 |
|
| 110 |
def pretty_printer_for_regexp(pattern, name): |
| 111 |
compiled = re.compile(pattern) |
| 112 |
|
| 113 |
def add(fn): |
| 114 |
check_for_reused_pretty_printer(fn) |
| 115 |
add_to_subprinter_list(fn, name) |
| 116 |
printers_by_regexp.append((compiled, fn)) |
| 117 |
return fn |
| 118 |
|
| 119 |
return add |
| 120 |
|
| 121 |
|
| 122 |
# Forget all pretty-printer lookup functions defined in the module name |
| 123 |
# |module_name|, if any exist. Use this at the top of each pretty-printer |
| 124 |
# module like this: |
| 125 |
# |
| 126 |
# clear_module_printers(__name__) |
| 127 |
|
| 128 |
|
| 129 |
def clear_module_printers(module_name): |
| 130 |
global printers_by_tag, ptr_printers_by_tag, ref_printers_by_tag |
| 131 |
global template_printers_by_tag, printers_by_regexp |
| 132 |
|
| 133 |
# Remove all pretty-printers defined in the module named |module_name| |
| 134 |
# from d. |
| 135 |
def clear_dictionary(d): |
| 136 |
# Walk the dictionary, building a list of keys whose entries we |
| 137 |
# should remove. (It's not safe to delete entries from a dictionary |
| 138 |
# while we're iterating over it.) |
| 139 |
to_delete = [] |
| 140 |
for k, v in d.items(): |
| 141 |
if v.__module__ == module_name: |
| 142 |
to_delete.append(k) |
| 143 |
remove_from_subprinter_list(v) |
| 144 |
for k in to_delete: |
| 145 |
del d[k] |
| 146 |
|
| 147 |
clear_dictionary(printers_by_tag) |
| 148 |
clear_dictionary(ptr_printers_by_tag) |
| 149 |
clear_dictionary(ref_printers_by_tag) |
| 150 |
clear_dictionary(template_printers_by_tag) |
| 151 |
|
| 152 |
# Iterate over printers_by_regexp, deleting entries from the given module. |
| 153 |
new_list = [] |
| 154 |
for p in printers_by_regexp: |
| 155 |
if p.__module__ == module_name: |
| 156 |
remove_from_subprinter_list(p) |
| 157 |
else: |
| 158 |
new_list.append(p) |
| 159 |
printers_by_regexp = new_list |
| 160 |
|
| 161 |
|
| 162 |
# Our subprinters array. The 'subprinters' attributes of all lookup |
| 163 |
# functions returned by lookup_for_objfile point to this array instance, |
| 164 |
# which we mutate as subprinters are added and removed. |
| 165 |
subprinters = [] |
| 166 |
|
| 167 |
# Set up the 'name' and 'enabled' attributes on |subprinter|, and add it to our |
| 168 |
# list of all SpiderMonkey subprinters. |
| 169 |
|
| 170 |
|
| 171 |
def add_to_subprinter_list(subprinter, name): |
| 172 |
subprinter.name = name |
| 173 |
subprinter.enabled = True |
| 174 |
subprinters.append(subprinter) |
| 175 |
|
| 176 |
|
| 177 |
# Remove |subprinter| from our list of all SpiderMonkey subprinters. |
| 178 |
|
| 179 |
|
| 180 |
def remove_from_subprinter_list(subprinter): |
| 181 |
subprinters.remove(subprinter) |
| 182 |
|
| 183 |
|
| 184 |
# An exception class meaning, "This objfile has no SpiderMonkey in it." |
| 185 |
|
| 186 |
|
| 187 |
class NotSpiderMonkeyObjfileError(TypeError): |
| 188 |
pass |
| 189 |
|
| 190 |
|
| 191 |
# TypeCache: a cache for frequently used information about an objfile. |
| 192 |
# |
| 193 |
# When a new SpiderMonkey objfile is loaded, we construct an instance of |
| 194 |
# this class for it. Then, whenever we construct a pretty-printer for some |
| 195 |
# gdb.Value, we also pass, as a second argument, the TypeCache for the |
| 196 |
# objfile to which that value's type belongs. |
| 197 |
# |
| 198 |
# if objfile doesn't seem to have SpiderMonkey code in it, the constructor |
| 199 |
# raises NotSpiderMonkeyObjfileError. |
| 200 |
# |
| 201 |
# Pretty-printer modules may add attributes to this to hold their own |
| 202 |
# cached values. Such attributes should be named mod_NAME, where the module |
| 203 |
# is named mozilla.NAME; for example, mozilla.JSString should store its |
| 204 |
# metadata in the TypeCache's mod_JSString attribute. |
| 205 |
|
| 206 |
|
| 207 |
class TypeCache: |
| 208 |
def __init__(self, objfile): |
| 209 |
self.objfile = objfile |
| 210 |
|
| 211 |
# Unfortunately, the Python interface doesn't allow us to specify |
| 212 |
# the objfile in whose scope lookups should occur. But simply |
| 213 |
# knowing that we need to lookup the types afresh is probably |
| 214 |
# enough. |
| 215 |
self.void_t = gdb.lookup_type("void") |
| 216 |
self.void_ptr_t = self.void_t.pointer() |
| 217 |
self.uintptr_t = gdb.lookup_type("uintptr_t") |
| 218 |
try: |
| 219 |
self.JSString_ptr_t = gdb.lookup_type("JSString").pointer() |
| 220 |
self.JSSymbol_ptr_t = gdb.lookup_type("JS::Symbol").pointer() |
| 221 |
self.JSObject_ptr_t = gdb.lookup_type("JSObject").pointer() |
| 222 |
except gdb.error: |
| 223 |
raise NotSpiderMonkeyObjfileError |
| 224 |
|
| 225 |
self.mod_GCCellPtr = None |
| 226 |
self.mod_Interpreter = None |
| 227 |
self.mod_JSObject = None |
| 228 |
self.mod_JSOp = None |
| 229 |
self.mod_JSString = None |
| 230 |
self.mod_JS_Value = None |
| 231 |
self.mod_ExecutableAllocator = None |
| 232 |
self.mod_IonGraph = None |
| 233 |
|
| 234 |
|
| 235 |
# Yield a series of all the types that |t| implements, by following typedefs |
| 236 |
# and iterating over base classes. Specifically: |
| 237 |
# - |t| itself is the first value yielded. |
| 238 |
# - If we yield a typedef, we later yield its definition. |
| 239 |
# - If we yield a type with base classes, we later yield those base classes. |
| 240 |
# - If we yield a type with some base classes that are typedefs, |
| 241 |
# we yield all the type's base classes before following the typedefs. |
| 242 |
# |
| 243 |
# This is a hokey attempt to order the implemented types by meaningfulness when |
| 244 |
# pretty-printed. Perhaps it is entirely misguided, and we should actually |
| 245 |
# collect all applicable pretty-printers, and then use some ordering on the |
| 246 |
# pretty-printers themselves. |
| 247 |
# |
| 248 |
# We may yield a type more than once (say, if it appears more than once in the |
| 249 |
# class hierarchy). |
| 250 |
|
| 251 |
|
| 252 |
def implemented_types(t): |
| 253 |
# Yield all types that follow |t|. |
| 254 |
def followers(t): |
| 255 |
if t.code == gdb.TYPE_CODE_TYPEDEF: |
| 256 |
yield t.target() |
| 257 |
for t2 in followers(t.target()): |
| 258 |
yield t2 |
| 259 |
elif is_struct_or_union(t): |
| 260 |
base_classes = [] |
| 261 |
for f in t.fields(): |
| 262 |
if f.is_base_class: |
| 263 |
yield f.type |
| 264 |
base_classes.append(f.type) |
| 265 |
for b in base_classes: |
| 266 |
for t2 in followers(b): |
| 267 |
yield t2 |
| 268 |
|
| 269 |
yield t |
| 270 |
for t2 in followers(t): |
| 271 |
yield t2 |
| 272 |
|
| 273 |
|
| 274 |
template_regexp = re.compile(r"([\w_:]+)<") |
| 275 |
|
| 276 |
|
| 277 |
def is_struct_or_union(t): |
| 278 |
return t.code in (gdb.TYPE_CODE_STRUCT, gdb.TYPE_CODE_UNION) |
| 279 |
|
| 280 |
|
| 281 |
def is_struct_or_union_or_enum(t): |
| 282 |
return t.code in (gdb.TYPE_CODE_STRUCT, gdb.TYPE_CODE_UNION, gdb.TYPE_CODE_ENUM) |
| 283 |
|
| 284 |
|
| 285 |
# Construct and return a pretty-printer lookup function for objfile, or |
| 286 |
# return None if the objfile doesn't contain SpiderMonkey code |
| 287 |
# (specifically, definitions for SpiderMonkey types). |
| 288 |
|
| 289 |
|
| 290 |
def lookup_for_objfile(objfile): |
| 291 |
# Create a type cache for this objfile. |
| 292 |
try: |
| 293 |
cache = TypeCache(objfile) |
| 294 |
except NotSpiderMonkeyObjfileError: |
| 295 |
if gdb.parameter("verbose"): |
| 296 |
gdb.write( |
| 297 |
"objfile '%s' has no SpiderMonkey code; not registering pretty-printers\n" |
| 298 |
% (objfile.filename,) |
| 299 |
) |
| 300 |
return None |
| 301 |
|
| 302 |
# Return a pretty-printer for |value|, if we have one. This is the lookup |
| 303 |
# function object we place in each gdb.Objfile's pretty-printers list, so it |
| 304 |
# carries |name|, |enabled|, and |subprinters| attributes. |
| 305 |
def lookup(value): |
| 306 |
# If |table| has a pretty-printer for |tag|, apply it to |value|. |
| 307 |
def check_table(table, tag): |
| 308 |
if tag in table: |
| 309 |
f = table[tag] |
| 310 |
if f.enabled: |
| 311 |
return f(value, cache) |
| 312 |
return None |
| 313 |
|
| 314 |
def check_table_by_type_name(table, t): |
| 315 |
if t.code == gdb.TYPE_CODE_TYPEDEF: |
| 316 |
return check_table(table, str(t)) |
| 317 |
elif is_struct_or_union_or_enum(t) and t.tag: |
| 318 |
return check_table(table, t.tag) |
| 319 |
else: |
| 320 |
return None |
| 321 |
|
| 322 |
for t in implemented_types(value.type): |
| 323 |
if t.code == gdb.TYPE_CODE_PTR: |
| 324 |
for t2 in implemented_types(t.target()): |
| 325 |
p = check_table_by_type_name(ptr_printers_by_tag, t2) |
| 326 |
if p: |
| 327 |
return p |
| 328 |
elif t.code == gdb.TYPE_CODE_REF: |
| 329 |
for t2 in implemented_types(t.target()): |
| 330 |
p = check_table_by_type_name(ref_printers_by_tag, t2) |
| 331 |
if p: |
| 332 |
return p |
| 333 |
else: |
| 334 |
p = check_table_by_type_name(printers_by_tag, t) |
| 335 |
if p: |
| 336 |
return p |
| 337 |
if is_struct_or_union(t) and t.tag: |
| 338 |
m = template_regexp.match(t.tag) |
| 339 |
if m: |
| 340 |
p = check_table(template_printers_by_tag, m.group(1)) |
| 341 |
if p: |
| 342 |
return p |
| 343 |
|
| 344 |
# Failing that, look for a printer in printers_by_regexp. We have |
| 345 |
# to scan the whole list, so regexp printers should be used |
| 346 |
# sparingly. |
| 347 |
s = str(value.type) |
| 348 |
for r, f in printers_by_regexp: |
| 349 |
if f.enabled: |
| 350 |
m = r.match(s) |
| 351 |
if m: |
| 352 |
p = f(value, cache) |
| 353 |
if p: |
| 354 |
return p |
| 355 |
|
| 356 |
# No luck. |
| 357 |
return None |
| 358 |
|
| 359 |
# Give |lookup| the attributes expected of a pretty-printer with |
| 360 |
# subprinters, for enabling and disabling. |
| 361 |
lookup.name = "SpiderMonkey" |
| 362 |
lookup.enabled = True |
| 363 |
lookup.subprinters = subprinters |
| 364 |
|
| 365 |
return lookup |
| 366 |
|
| 367 |
|
| 368 |
# A base class for pretty-printers for pointer values that handles null |
| 369 |
# pointers, by declining to construct a pretty-printer for them at all. |
| 370 |
# Derived classes may simply assume that self.value is non-null. |
| 371 |
# |
| 372 |
# To help share code, this class can also be used with reference types. |
| 373 |
# |
| 374 |
# This class provides the following methods, which subclasses are free to |
| 375 |
# override: |
| 376 |
# |
| 377 |
# __init__(self, value, cache): Save value and cache as properties by those names |
| 378 |
# on the instance. |
| 379 |
# |
| 380 |
# to_string(self): format the type's name and address, as GDB would, and then |
| 381 |
# call a 'summary' method (which the subclass must define) to produce a |
| 382 |
# description of the referent. |
| 383 |
# |
| 384 |
# Note that pretty-printers returning a 'string' display hint must not use |
| 385 |
# this default 'to_string' method, as GDB will take everything it returns, |
| 386 |
# including the type name and address, as string contents. |
| 387 |
|
| 388 |
|
| 389 |
class Pointer: |
| 390 |
def __new__(cls, value, cache): |
| 391 |
# Don't try to provide pretty-printers for NULL pointers. |
| 392 |
if value.type.strip_typedefs().code == gdb.TYPE_CODE_PTR and value == 0: |
| 393 |
return None |
| 394 |
return super(Pointer, cls).__new__(cls) |
| 395 |
|
| 396 |
def __init__(self, value, cache): |
| 397 |
self.value = value |
| 398 |
self.cache = cache |
| 399 |
|
| 400 |
def to_string(self): |
| 401 |
# See comment above. |
| 402 |
assert not hasattr(self, "display_hint") or self.display_hint() != "string" |
| 403 |
concrete_type = self.value.type.strip_typedefs() |
| 404 |
if concrete_type.code == gdb.TYPE_CODE_PTR: |
| 405 |
address = self.value.cast(self.cache.void_ptr_t) |
| 406 |
elif concrete_type.code == gdb.TYPE_CODE_REF: |
| 407 |
address = "@" + str(self.value.address.cast(self.cache.void_ptr_t)) |
| 408 |
else: |
| 409 |
assert not "mozilla.prettyprinters.Pointer applied to bad value type" |
| 410 |
try: |
| 411 |
summary = self.summary() |
| 412 |
except gdb.MemoryError as r: |
| 413 |
summary = str(r) |
| 414 |
v = "(%s) %s %s" % (self.value.type, address, summary) |
| 415 |
return v |
| 416 |
|
| 417 |
def summary(self): |
| 418 |
raise NotImplementedError |
| 419 |
|
| 420 |
|
| 421 |
field_enum_value = None |
| 422 |
|
| 423 |
# Given |t|, a gdb.Type instance representing an enum type, return the |
| 424 |
# numeric value of the enum value named |name|. |
| 425 |
# |
| 426 |
# Pre-2012-4-18 versions of GDB store the value of an enum member on the |
| 427 |
# gdb.Field's 'bitpos' attribute; later versions store it on the 'enumval' |
| 428 |
# attribute. This function retrieves the value from either. |
| 429 |
|
| 430 |
|
| 431 |
def enum_value(t, name): |
| 432 |
global field_enum_value |
| 433 |
f = t[name] |
| 434 |
# Monkey-patching is a-okay in polyfills! Just because. |
| 435 |
if not field_enum_value: |
| 436 |
if hasattr(f, "enumval"): |
| 437 |
|
| 438 |
def field_enum_value(f): |
| 439 |
return f.enumval |
| 440 |
|
| 441 |
else: |
| 442 |
|
| 443 |
def field_enum_value(f): |
| 444 |
return f.bitpos |
| 445 |
|
| 446 |
return field_enum_value(f) |