| 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/unwind.py --- unwinder and frame filter for SpiderMonkey |
| 6 |
|
| 7 |
import platform |
| 8 |
|
| 9 |
import gdb |
| 10 |
import gdb.types |
| 11 |
from gdb.FrameDecorator import FrameDecorator |
| 12 |
|
| 13 |
from mozilla.JSObject import get_function_name, get_function_script |
| 14 |
from mozilla.prettyprinters import TypeCache |
| 15 |
|
| 16 |
# For ease of use in Python 2, we use "long" instead of "int" |
| 17 |
# everywhere. |
| 18 |
try: |
| 19 |
long |
| 20 |
except NameError: |
| 21 |
long = int |
| 22 |
|
| 23 |
# The Python 3 |map| built-in works lazily, but in Python 2 we need |
| 24 |
# itertools.imap to get this. |
| 25 |
try: |
| 26 |
from itertools import imap |
| 27 |
except ImportError: |
| 28 |
imap = map |
| 29 |
|
| 30 |
_have_unwinder = True |
| 31 |
try: |
| 32 |
from gdb.unwinder import Unwinder |
| 33 |
except ImportError: |
| 34 |
_have_unwinder = False |
| 35 |
# We need something here; it doesn't matter what as no unwinder |
| 36 |
# will ever be instantiated. |
| 37 |
Unwinder = object |
| 38 |
|
| 39 |
|
| 40 |
def debug(something): |
| 41 |
# print("@@ " + something) |
| 42 |
pass |
| 43 |
|
| 44 |
|
| 45 |
# Maps frametype enum base names to corresponding class. |
| 46 |
SizeOfFramePrefix = { |
| 47 |
"FrameType::IonJS": "ExitFrameLayout", |
| 48 |
"FrameType::BaselineJS": "JitFrameLayout", |
| 49 |
"FrameType::BaselineStub": "BaselineStubFrameLayout", |
| 50 |
"FrameType::CppToJSJit": "JitFrameLayout", |
| 51 |
"FrameType::WasmToJSJit": "JitFrameLayout", |
| 52 |
"FrameType::Rectifier": "RectifierFrameLayout", |
| 53 |
"FrameType::IonAccessorIC": "IonAccessorICFrameLayout", |
| 54 |
"FrameType::IonICCall": "IonICCallFrameLayout", |
| 55 |
"FrameType::Exit": "ExitFrameLayout", |
| 56 |
"FrameType::Bailout": "JitFrameLayout", |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
# We cannot have semi-colon as identifier names, so use a colon instead, |
| 61 |
# and forward the name resolution to the type cache class. |
| 62 |
class UnwinderTypeCacheFrameType: |
| 63 |
def __init__(self, tc): |
| 64 |
self.tc = tc |
| 65 |
|
| 66 |
def __getattr__(self, name): |
| 67 |
return self.tc.__getattr__("FrameType::" + name) |
| 68 |
|
| 69 |
|
| 70 |
class UnwinderTypeCache(TypeCache): |
| 71 |
# All types and symbols that we need are attached to an object that we |
| 72 |
# can dispose of as needed. |
| 73 |
|
| 74 |
def __init__(self): |
| 75 |
self.d = None |
| 76 |
self.frame_enum_names = {} |
| 77 |
self.frame_class_types = {} |
| 78 |
super(UnwinderTypeCache, self).__init__(None) |
| 79 |
|
| 80 |
# We take this bizarre approach to defer trying to look up any |
| 81 |
# symbols until absolutely needed. Without this, the loading |
| 82 |
# approach taken by the gdb-tests would cause spurious exceptions. |
| 83 |
def __getattr__(self, name): |
| 84 |
if self.d is None: |
| 85 |
self.initialize() |
| 86 |
if name == "frame_type": |
| 87 |
return UnwinderTypeCacheFrameType(self) |
| 88 |
if name not in self.d: |
| 89 |
return None |
| 90 |
return self.d[name] |
| 91 |
|
| 92 |
def value(self, name): |
| 93 |
return long(gdb.lookup_symbol(name)[0].value()) |
| 94 |
|
| 95 |
def jit_value(self, name): |
| 96 |
return self.value("js::jit::" + name) |
| 97 |
|
| 98 |
def initialize(self): |
| 99 |
self.d = {} |
| 100 |
self.d["FRAMETYPE_MASK"] = (1 << self.jit_value("FRAMETYPE_BITS")) - 1 |
| 101 |
self.d["FRAMESIZE_SHIFT"] = self.jit_value("FRAMESIZE_SHIFT") |
| 102 |
self.d["FRAME_HEADER_SIZE_SHIFT"] = self.jit_value("FRAME_HEADER_SIZE_SHIFT") |
| 103 |
self.d["FRAME_HEADER_SIZE_MASK"] = self.jit_value("FRAME_HEADER_SIZE_MASK") |
| 104 |
|
| 105 |
self.compute_frame_info() |
| 106 |
commonFrameLayout = gdb.lookup_type("js::jit::CommonFrameLayout") |
| 107 |
self.d["typeCommonFrameLayout"] = commonFrameLayout |
| 108 |
self.d["typeCommonFrameLayoutPointer"] = commonFrameLayout.pointer() |
| 109 |
self.d["per_tls_context"] = gdb.lookup_global_symbol("js::TlsContext") |
| 110 |
|
| 111 |
self.d["void_starstar"] = gdb.lookup_type("void").pointer().pointer() |
| 112 |
|
| 113 |
jitframe = gdb.lookup_type("js::jit::JitFrameLayout") |
| 114 |
self.d["jitFrameLayoutPointer"] = jitframe.pointer() |
| 115 |
|
| 116 |
self.d["CalleeToken_Function"] = self.jit_value("CalleeToken_Function") |
| 117 |
self.d["CalleeToken_FunctionConstructing"] = self.jit_value( |
| 118 |
"CalleeToken_FunctionConstructing" |
| 119 |
) |
| 120 |
self.d["CalleeToken_Script"] = self.jit_value("CalleeToken_Script") |
| 121 |
self.d["JSScript"] = gdb.lookup_type("JSScript").pointer() |
| 122 |
self.d["Value"] = gdb.lookup_type("JS::Value") |
| 123 |
|
| 124 |
self.d["SOURCE_SLOT"] = self.value("js::ScriptSourceObject::SOURCE_SLOT") |
| 125 |
self.d["NativeObject"] = gdb.lookup_type("js::NativeObject").pointer() |
| 126 |
self.d["HeapSlot"] = gdb.lookup_type("js::HeapSlot").pointer() |
| 127 |
self.d["ScriptSource"] = gdb.lookup_type("js::ScriptSource").pointer() |
| 128 |
|
| 129 |
# ProcessExecutableMemory, used to identify if a pc is in the section |
| 130 |
# pre-allocated by the JIT. |
| 131 |
self.d["MaxCodeBytesPerProcess"] = self.jit_value("MaxCodeBytesPerProcess") |
| 132 |
self.d["execMemory"] = gdb.lookup_symbol("::execMemory")[0].value() |
| 133 |
|
| 134 |
# Compute maps related to jit frames. |
| 135 |
def compute_frame_info(self): |
| 136 |
t = gdb.lookup_type("enum js::jit::FrameType") |
| 137 |
for field in t.fields(): |
| 138 |
# Strip off "js::jit::", remains: "FrameType::*". |
| 139 |
name = field.name[9:] |
| 140 |
enumval = long(field.enumval) |
| 141 |
self.d[name] = enumval |
| 142 |
self.frame_enum_names[enumval] = name |
| 143 |
class_type = gdb.lookup_type("js::jit::" + SizeOfFramePrefix[name]) |
| 144 |
self.frame_class_types[enumval] = class_type.pointer() |
| 145 |
|
| 146 |
|
| 147 |
class FrameSymbol: |
| 148 |
"A symbol/value pair as expected from gdb frame decorators." |
| 149 |
|
| 150 |
def __init__(self, sym, val): |
| 151 |
self.sym = sym |
| 152 |
self.val = val |
| 153 |
|
| 154 |
def symbol(self): |
| 155 |
return self.sym |
| 156 |
|
| 157 |
def value(self): |
| 158 |
return self.val |
| 159 |
|
| 160 |
|
| 161 |
class JitFrameDecorator(FrameDecorator): |
| 162 |
"""This represents a single JIT frame for the purposes of display. |
| 163 |
That is, the frame filter creates instances of this when it sees a |
| 164 |
JIT frame in the stack.""" |
| 165 |
|
| 166 |
def __init__(self, base, info, cache): |
| 167 |
super(JitFrameDecorator, self).__init__(base) |
| 168 |
self.info = info |
| 169 |
self.cache = cache |
| 170 |
|
| 171 |
def _decode_jitframe(self, this_frame): |
| 172 |
calleetoken = long(this_frame["calleeToken_"]) |
| 173 |
tag = calleetoken & 3 |
| 174 |
calleetoken = calleetoken ^ tag |
| 175 |
function = None |
| 176 |
script = None |
| 177 |
if ( |
| 178 |
tag == self.cache.CalleeToken_Function |
| 179 |
or tag == self.cache.CalleeToken_FunctionConstructing |
| 180 |
): |
| 181 |
value = gdb.Value(calleetoken) |
| 182 |
function = get_function_name(value, self.cache) |
| 183 |
script = get_function_script(value, self.cache) |
| 184 |
elif tag == self.cache.CalleeToken_Script: |
| 185 |
script = gdb.Value(calleetoken).cast(self.cache.JSScript) |
| 186 |
return {"function": function, "script": script} |
| 187 |
|
| 188 |
def function(self): |
| 189 |
if self.info["name"] is None: |
| 190 |
return FrameDecorator.function(self) |
| 191 |
name = self.info["name"] |
| 192 |
result = "<<" + name |
| 193 |
# If we have a frame, we can extract the callee information |
| 194 |
# from it for display here. |
| 195 |
this_frame = self.info["this_frame"] |
| 196 |
if this_frame is not None: |
| 197 |
if gdb.types.has_field(this_frame.type.target(), "calleeToken_"): |
| 198 |
function = self._decode_jitframe(this_frame)["function"] |
| 199 |
if function is not None: |
| 200 |
result = result + " " + function |
| 201 |
return result + ">>" |
| 202 |
|
| 203 |
def filename(self): |
| 204 |
this_frame = self.info["this_frame"] |
| 205 |
if this_frame is not None: |
| 206 |
if gdb.types.has_field(this_frame.type.target(), "calleeToken_"): |
| 207 |
script = self._decode_jitframe(this_frame)["script"] |
| 208 |
if script is not None: |
| 209 |
obj = script["sourceObject_"]["value"] |
| 210 |
# Verify that this is a ScriptSource object. |
| 211 |
# FIXME should also deal with wrappers here. |
| 212 |
nativeobj = obj.cast(self.cache.NativeObject) |
| 213 |
# See bug 987069 and despair. At least this |
| 214 |
# approach won't give exceptions. |
| 215 |
class_name = nativeobj["group_"]["value"]["clasp_"]["name"].string( |
| 216 |
"ISO-8859-1" |
| 217 |
) |
| 218 |
if class_name != "ScriptSource": |
| 219 |
return FrameDecorator.filename(self) |
| 220 |
scriptsourceobj = (nativeobj + 1).cast(self.cache.HeapSlot)[ |
| 221 |
self.cache.SOURCE_SLOT |
| 222 |
] |
| 223 |
scriptsource = scriptsourceobj["value"]["asBits_"] << 1 |
| 224 |
scriptsource = scriptsource.cast(self.cache.ScriptSource) |
| 225 |
return scriptsource["filename_"]["mTuple"]["mFirstA"].string() |
| 226 |
return FrameDecorator.filename(self) |
| 227 |
|
| 228 |
def frame_args(self): |
| 229 |
this_frame = self.info["this_frame"] |
| 230 |
if this_frame is None: |
| 231 |
return FrameDecorator.frame_args(self) |
| 232 |
if not gdb.types.has_field(this_frame.type.target(), "numActualArgs_"): |
| 233 |
return FrameDecorator.frame_args(self) |
| 234 |
# See if this is a function call. |
| 235 |
if self._decode_jitframe(this_frame)["function"] is None: |
| 236 |
return FrameDecorator.frame_args(self) |
| 237 |
# Construct and return an iterable of all the arguments. |
| 238 |
result = [] |
| 239 |
num_args = long(this_frame["numActualArgs_"]) |
| 240 |
# Sometimes we see very large values here, so truncate it to |
| 241 |
# bypass the damage. |
| 242 |
if num_args > 10: |
| 243 |
num_args = 10 |
| 244 |
args_ptr = (this_frame + 1).cast(self.cache.Value.pointer()) |
| 245 |
for i in range(num_args + 1): |
| 246 |
# Synthesize names, since there doesn't seem to be |
| 247 |
# anything better to do. |
| 248 |
if i == 0: |
| 249 |
name = "this" |
| 250 |
else: |
| 251 |
name = "arg%d" % i |
| 252 |
result.append(FrameSymbol(name, args_ptr[i])) |
| 253 |
return result |
| 254 |
|
| 255 |
|
| 256 |
class SpiderMonkeyFrameFilter: |
| 257 |
"A frame filter for SpiderMonkey." |
| 258 |
|
| 259 |
# |state_holder| is either None, or an instance of |
| 260 |
# SpiderMonkeyUnwinder. If the latter, then this class will |
| 261 |
# reference the |unwinder_state| attribute to find the current |
| 262 |
# unwinder state. |
| 263 |
def __init__(self, cache, state_holder): |
| 264 |
self.name = "SpiderMonkey" |
| 265 |
self.enabled = True |
| 266 |
self.priority = 100 |
| 267 |
self.state_holder = state_holder |
| 268 |
self.cache = cache |
| 269 |
|
| 270 |
def maybe_wrap_frame(self, frame): |
| 271 |
if self.state_holder is None or self.state_holder.unwinder_state is None: |
| 272 |
return frame |
| 273 |
base = frame.inferior_frame() |
| 274 |
info = self.state_holder.unwinder_state.get_frame(base) |
| 275 |
if info is None: |
| 276 |
return frame |
| 277 |
return JitFrameDecorator(frame, info, self.cache) |
| 278 |
|
| 279 |
def filter(self, frame_iter): |
| 280 |
return imap(self.maybe_wrap_frame, frame_iter) |
| 281 |
|
| 282 |
|
| 283 |
class SpiderMonkeyFrameId: |
| 284 |
"A frame id class, as specified by the gdb unwinder API." |
| 285 |
|
| 286 |
def __init__(self, sp, pc): |
| 287 |
self.sp = sp |
| 288 |
self.pc = pc |
| 289 |
|
| 290 |
|
| 291 |
class UnwinderState: |
| 292 |
"""This holds all the state needed during a given unwind. Each time a |
| 293 |
new unwind is done, a new instance of this class is created. It |
| 294 |
keeps track of all the state needed to unwind JIT frames. Note that |
| 295 |
this class is not directly instantiated. |
| 296 |
|
| 297 |
This is a base class, and must be specialized for each target |
| 298 |
architecture, both because we need to use arch-specific register |
| 299 |
names, and because entry frame unwinding is arch-specific. |
| 300 |
See https://sourceware.org/bugzilla/show_bug.cgi?id=19286 for info |
| 301 |
about the register name issue. |
| 302 |
|
| 303 |
Each subclass must define SP_REGISTER, PC_REGISTER, and |
| 304 |
SENTINEL_REGISTER (see x64UnwinderState for info); and implement |
| 305 |
unwind_entry_frame_registers.""" |
| 306 |
|
| 307 |
def __init__(self, typecache): |
| 308 |
self.next_sp = None |
| 309 |
self.next_type = None |
| 310 |
self.activation = None |
| 311 |
# An unwinder instance is specific to a thread. Record the |
| 312 |
# selected thread for later verification. |
| 313 |
self.thread = gdb.selected_thread() |
| 314 |
self.frame_map = {} |
| 315 |
self.typecache = typecache |
| 316 |
|
| 317 |
# If the given gdb.Frame was created by this unwinder, return the |
| 318 |
# corresponding informational dictionary for the frame. |
| 319 |
# Otherwise, return None. This is used by the frame filter to |
| 320 |
# display extra information about the frame. |
| 321 |
def get_frame(self, frame): |
| 322 |
sp = long(frame.read_register(self.SP_REGISTER)) |
| 323 |
if sp in self.frame_map: |
| 324 |
return self.frame_map[sp] |
| 325 |
return None |
| 326 |
|
| 327 |
# Add information about a frame to the frame map. This map is |
| 328 |
# queried by |self.get_frame|. |sp| is the frame's stack pointer, |
| 329 |
# and |name| the frame's type as a string, e.g. "FrameType::Exit". |
| 330 |
def add_frame(self, sp, name=None, this_frame=None): |
| 331 |
self.frame_map[long(sp)] = {"name": name, "this_frame": this_frame} |
| 332 |
|
| 333 |
# See whether |pc| is claimed by the Jit. |
| 334 |
def is_jit_address(self, pc): |
| 335 |
execMem = self.typecache.execMemory |
| 336 |
base = long(execMem["base_"]) |
| 337 |
length = self.typecache.MaxCodeBytesPerProcess |
| 338 |
|
| 339 |
# If the base pointer is null, then no memory got allocated yet. |
| 340 |
if long(base) == 0: |
| 341 |
return False |
| 342 |
|
| 343 |
# If allocated, then we allocated MaxCodeBytesPerProcess. |
| 344 |
return base <= pc and pc < base + length |
| 345 |
|
| 346 |
# Check whether |self| is valid for the selected thread. |
| 347 |
def check(self): |
| 348 |
return gdb.selected_thread() is self.thread |
| 349 |
|
| 350 |
# Essentially js::TlsContext.get(). |
| 351 |
def get_tls_context(self): |
| 352 |
return self.typecache.per_tls_context.value()["mValue"] |
| 353 |
|
| 354 |
# |common| is a pointer to a CommonFrameLayout object. Return a |
| 355 |
# tuple (local_size, header_size, frame_type), where |size| is the |
| 356 |
# integer size of the previous frame's locals; |header_size| is |
| 357 |
# the size of this frame's header; and |frame_type| is an integer |
| 358 |
# representing the previous frame's type. |
| 359 |
def unpack_descriptor(self, common): |
| 360 |
value = long(common["descriptor_"]) |
| 361 |
local_size = value >> self.typecache.FRAMESIZE_SHIFT |
| 362 |
header_size = ( |
| 363 |
value >> self.typecache.FRAME_HEADER_SIZE_SHIFT |
| 364 |
) & self.typecache.FRAME_HEADER_SIZE_MASK |
| 365 |
header_size = header_size * self.typecache.void_starstar.sizeof |
| 366 |
frame_type = long(value & self.typecache.FRAMETYPE_MASK) |
| 367 |
if frame_type == self.typecache.frame_type.CppToJSJit: |
| 368 |
# Trampoline-x64.cpp pushes a JitFrameLayout object, but |
| 369 |
# the stack pointer is actually adjusted as if a |
| 370 |
# CommonFrameLayout object was pushed. |
| 371 |
header_size = self.typecache.typeCommonFrameLayout.sizeof |
| 372 |
return (local_size, header_size, frame_type) |
| 373 |
|
| 374 |
# Create a new frame for gdb. This makes a new unwind info object |
| 375 |
# and fills it in, then returns it. It also registers any |
| 376 |
# pertinent information with the frame filter for later display. |
| 377 |
# |
| 378 |
# |pc| is the PC from the pending frame |
| 379 |
# |sp| is the stack pointer to use |
| 380 |
# |frame| points to the CommonFrameLayout object |
| 381 |
# |frame_type| is a integer, one of the |enum FrameType| values, |
| 382 |
# describing the current frame. |
| 383 |
# |pending_frame| is the pending frame (see the gdb unwinder |
| 384 |
# documentation). |
| 385 |
def create_frame(self, pc, sp, frame, frame_type, pending_frame): |
| 386 |
# Make a frame_id that claims that |frame| is sort of like a |
| 387 |
# frame pointer for this frame. |
| 388 |
frame_id = SpiderMonkeyFrameId(frame, pc) |
| 389 |
|
| 390 |
# Read the frame layout object to find the next such object. |
| 391 |
# This lets us unwind the necessary registers for the next |
| 392 |
# frame, and also update our internal state to match. |
| 393 |
common = frame.cast(self.typecache.typeCommonFrameLayoutPointer) |
| 394 |
next_pc = common["returnAddress_"] |
| 395 |
(local_size, header_size, next_type) = self.unpack_descriptor(common) |
| 396 |
next_sp = frame + header_size + local_size |
| 397 |
|
| 398 |
# Compute the type of the next oldest frame's descriptor. |
| 399 |
this_class_type = self.typecache.frame_class_types[frame_type] |
| 400 |
this_frame = frame.cast(this_class_type) |
| 401 |
|
| 402 |
# Register this frame so the frame filter can find it. This |
| 403 |
# is registered using SP because we don't have any other good |
| 404 |
# approach -- you can't get the frame id from a gdb.Frame. |
| 405 |
# https://sourceware.org/bugzilla/show_bug.cgi?id=19800 |
| 406 |
frame_name = self.typecache.frame_enum_names[frame_type] |
| 407 |
self.add_frame(sp, name=frame_name, this_frame=this_frame) |
| 408 |
|
| 409 |
# Update internal state for the next unwind. |
| 410 |
self.next_sp = next_sp |
| 411 |
self.next_type = next_type |
| 412 |
|
| 413 |
unwind_info = pending_frame.create_unwind_info(frame_id) |
| 414 |
unwind_info.add_saved_register(self.PC_REGISTER, next_pc) |
| 415 |
unwind_info.add_saved_register(self.SP_REGISTER, next_sp) |
| 416 |
# FIXME it would be great to unwind any other registers here. |
| 417 |
return unwind_info |
| 418 |
|
| 419 |
# Unwind an "ordinary" JIT frame. This is used for JIT frames |
| 420 |
# other than enter and exit frames. Returns the newly-created |
| 421 |
# unwind info for gdb. |
| 422 |
def unwind_ordinary(self, pc, pending_frame): |
| 423 |
return self.create_frame( |
| 424 |
pc, self.next_sp, self.next_sp, self.next_type, pending_frame |
| 425 |
) |
| 426 |
|
| 427 |
# Unwind an exit frame. Returns None if this cannot be done; |
| 428 |
# otherwise returns the newly-created unwind info for gdb. |
| 429 |
def unwind_exit_frame(self, pc, pending_frame): |
| 430 |
if self.activation == 0: |
| 431 |
# Reached the end of the list. |
| 432 |
return None |
| 433 |
elif self.activation is None: |
| 434 |
cx = self.get_tls_context() |
| 435 |
self.activation = cx["jitActivation"]["value"] |
| 436 |
else: |
| 437 |
self.activation = self.activation["prevJitActivation_"] |
| 438 |
|
| 439 |
packedExitFP = self.activation["packedExitFP_"] |
| 440 |
if packedExitFP == 0: |
| 441 |
return None |
| 442 |
|
| 443 |
exit_sp = pending_frame.read_register(self.SP_REGISTER) |
| 444 |
frame_type = self.typecache.frame_type.Exit |
| 445 |
return self.create_frame(pc, exit_sp, packedExitFP, frame_type, pending_frame) |
| 446 |
|
| 447 |
# A wrapper for unwind_entry_frame_registers that handles |
| 448 |
# architecture-independent boilerplate. |
| 449 |
def unwind_entry_frame(self, pc, pending_frame): |
| 450 |
sp = self.next_sp |
| 451 |
# Notify the frame filter. |
| 452 |
self.add_frame(sp, name="FrameType::CppToJSJit") |
| 453 |
# Make an unwind_info for the per-architecture code to fill in. |
| 454 |
frame_id = SpiderMonkeyFrameId(sp, pc) |
| 455 |
unwind_info = pending_frame.create_unwind_info(frame_id) |
| 456 |
self.unwind_entry_frame_registers(sp, unwind_info) |
| 457 |
self.next_sp = None |
| 458 |
self.next_type = None |
| 459 |
return unwind_info |
| 460 |
|
| 461 |
# The main entry point that is called to try to unwind a JIT frame |
| 462 |
# of any type. Returns None if this cannot be done; otherwise |
| 463 |
# returns the newly-created unwind info for gdb. |
| 464 |
def unwind(self, pending_frame): |
| 465 |
pc = pending_frame.read_register(self.PC_REGISTER) |
| 466 |
|
| 467 |
# If the jit does not claim this address, bail. GDB defers to our |
| 468 |
# unwinder by default, but we don't really want that kind of power. |
| 469 |
if not self.is_jit_address(long(pc)): |
| 470 |
return None |
| 471 |
|
| 472 |
if self.next_sp is not None: |
| 473 |
if self.next_type == self.typecache.frame_type.CppToJSJit: |
| 474 |
return self.unwind_entry_frame(pc, pending_frame) |
| 475 |
return self.unwind_ordinary(pc, pending_frame) |
| 476 |
# Maybe we've found an exit frame. FIXME I currently don't |
| 477 |
# know how to identify these precisely, so we'll just hope for |
| 478 |
# the time being. |
| 479 |
return self.unwind_exit_frame(pc, pending_frame) |
| 480 |
|
| 481 |
|
| 482 |
class x64UnwinderState(UnwinderState): |
| 483 |
"The UnwinderState subclass for x86-64." |
| 484 |
|
| 485 |
SP_REGISTER = "rsp" |
| 486 |
PC_REGISTER = "rip" |
| 487 |
|
| 488 |
# A register unique to this architecture, that is also likely to |
| 489 |
# have been saved in any frame. The best thing to use here is |
| 490 |
# some arch-specific name for PC or SP. |
| 491 |
SENTINEL_REGISTER = "rip" |
| 492 |
|
| 493 |
# Must be in sync with Trampoline-x64.cpp:generateEnterJIT. Note |
| 494 |
# that rip isn't pushed there explicitly, but rather by the |
| 495 |
# previous function's call. |
| 496 |
PUSHED_REGS = ["r15", "r14", "r13", "r12", "rbx", "rbp", "rip"] |
| 497 |
|
| 498 |
# Fill in the unwound registers for an entry frame. |
| 499 |
def unwind_entry_frame_registers(self, sp, unwind_info): |
| 500 |
sp = sp.cast(self.typecache.void_starstar) |
| 501 |
# Skip the "result" push. |
| 502 |
sp = sp + 1 |
| 503 |
for reg in self.PUSHED_REGS: |
| 504 |
data = sp.dereference() |
| 505 |
sp = sp + 1 |
| 506 |
unwind_info.add_saved_register(reg, data) |
| 507 |
if reg == "rbp": |
| 508 |
unwind_info.add_saved_register(self.SP_REGISTER, sp) |
| 509 |
|
| 510 |
|
| 511 |
class SpiderMonkeyUnwinder(Unwinder): |
| 512 |
"""The unwinder object. This provides the "user interface" to the JIT |
| 513 |
unwinder, and also handles constructing or destroying UnwinderState |
| 514 |
objects as needed.""" |
| 515 |
|
| 516 |
# A list of all the possible unwinders. See |self.make_unwinder|. |
| 517 |
UNWINDERS = [x64UnwinderState] |
| 518 |
|
| 519 |
def __init__(self, typecache): |
| 520 |
super(SpiderMonkeyUnwinder, self).__init__("SpiderMonkey") |
| 521 |
self.typecache = typecache |
| 522 |
self.unwinder_state = None |
| 523 |
|
| 524 |
# Disabled by default until we figure out issues in gdb. |
| 525 |
self.enabled = False |
| 526 |
gdb.write( |
| 527 |
"SpiderMonkey unwinder is disabled by default, to enable it type:\n" |
| 528 |
+ "\tenable unwinder .* SpiderMonkey\n" |
| 529 |
) |
| 530 |
# Some versions of gdb did not flush the internal frame cache |
| 531 |
# when enabling or disabling an unwinder. This was fixed in |
| 532 |
# the same release of gdb that added the breakpoint_created |
| 533 |
# event. |
| 534 |
if not hasattr(gdb.events, "breakpoint_created"): |
| 535 |
gdb.write("\tflushregs\n") |
| 536 |
|
| 537 |
# We need to invalidate the unwinder state whenever the |
| 538 |
# inferior starts executing. This avoids having a stale |
| 539 |
# cache. |
| 540 |
gdb.events.cont.connect(self.invalidate_unwinder_state) |
| 541 |
assert self.test_sentinels() |
| 542 |
|
| 543 |
def test_sentinels(self): |
| 544 |
# Self-check. |
| 545 |
regs = {} |
| 546 |
for unwinder in self.UNWINDERS: |
| 547 |
if unwinder.SENTINEL_REGISTER in regs: |
| 548 |
return False |
| 549 |
regs[unwinder.SENTINEL_REGISTER] = 1 |
| 550 |
return True |
| 551 |
|
| 552 |
def make_unwinder(self, pending_frame): |
| 553 |
# gdb doesn't provide a good way to find the architecture. |
| 554 |
# See https://sourceware.org/bugzilla/show_bug.cgi?id=19399 |
| 555 |
# So, we look at each known architecture and see if the |
| 556 |
# corresponding "unique register" is known. |
| 557 |
for unwinder in self.UNWINDERS: |
| 558 |
try: |
| 559 |
pending_frame.read_register(unwinder.SENTINEL_REGISTER) |
| 560 |
except Exception: |
| 561 |
# Failed to read the register, so let's keep going. |
| 562 |
# This is more fragile than it might seem, because it |
| 563 |
# fails if the sentinel register wasn't saved in the |
| 564 |
# previous frame. |
| 565 |
continue |
| 566 |
return unwinder(self.typecache) |
| 567 |
return None |
| 568 |
|
| 569 |
def __call__(self, pending_frame): |
| 570 |
if self.unwinder_state is None or not self.unwinder_state.check(): |
| 571 |
self.unwinder_state = self.make_unwinder(pending_frame) |
| 572 |
if not self.unwinder_state: |
| 573 |
return None |
| 574 |
return self.unwinder_state.unwind(pending_frame) |
| 575 |
|
| 576 |
def invalidate_unwinder_state(self, *args, **kwargs): |
| 577 |
self.unwinder_state = None |
| 578 |
|
| 579 |
|
| 580 |
def register_unwinder(objfile): |
| 581 |
"""Register the unwinder and frame filter with |objfile|. If |objfile| |
| 582 |
is None, register them globally.""" |
| 583 |
|
| 584 |
type_cache = UnwinderTypeCache() |
| 585 |
unwinder = None |
| 586 |
# This currently only works on Linux, due to parse_proc_maps. |
| 587 |
if _have_unwinder and platform.system() == "Linux": |
| 588 |
unwinder = SpiderMonkeyUnwinder(type_cache) |
| 589 |
gdb.unwinder.register_unwinder(objfile, unwinder, replace=True) |
| 590 |
# We unconditionally register the frame filter, because at some |
| 591 |
# point we'll add interpreter frame filtering. |
| 592 |
filt = SpiderMonkeyFrameFilter(type_cache, unwinder) |
| 593 |
if objfile is None: |
| 594 |
objfile = gdb |
| 595 |
objfile.frame_filters[filt.name] = filt |