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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
|
/*
* Copyright (C) 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "StackVisitor.h"
#include "Arguments.h"
#include "CallFrameInlines.h"
#include "Executable.h"
#include "Interpreter.h"
#include "JSCInlines.h"
#include <wtf/DataLog.h>
namespace JSC {
StackVisitor::StackVisitor(CallFrame* startFrame)
{
m_frame.m_index = 0;
CallFrame* topFrame;
if (startFrame) {
m_frame.m_VMEntryFrame = startFrame->vm().topVMEntryFrame;
topFrame = startFrame->vm().topCallFrame;
} else {
m_frame.m_VMEntryFrame = 0;
topFrame = 0;
}
m_frame.m_callerIsVMEntryFrame = false;
readFrame(topFrame);
// Find the frame the caller wants to start unwinding from.
while (m_frame.callFrame() && m_frame.callFrame() != startFrame)
gotoNextFrame();
}
void StackVisitor::gotoNextFrame()
{
#if ENABLE(DFG_JIT)
if (m_frame.isInlinedFrame()) {
InlineCallFrame* inlineCallFrame = m_frame.inlineCallFrame();
CodeOrigin* callerCodeOrigin = &inlineCallFrame->caller;
readInlinedFrame(m_frame.callFrame(), callerCodeOrigin);
return;
}
#endif // ENABLE(DFG_JIT)
m_frame.m_VMEntryFrame = m_frame.m_CallerVMEntryFrame;
readFrame(m_frame.callerFrame());
}
void StackVisitor::readFrame(CallFrame* callFrame)
{
if (!callFrame) {
m_frame.setToEnd();
return;
}
#if !ENABLE(DFG_JIT)
readNonInlinedFrame(callFrame);
#else // !ENABLE(DFG_JIT)
// If the frame doesn't have a code block, then it's not a DFG frame.
// Hence, we're not at an inlined frame.
CodeBlock* codeBlock = callFrame->codeBlock();
if (!codeBlock) {
readNonInlinedFrame(callFrame);
return;
}
// If the code block does not have any code origins, then there's no
// inlining. Hence, we're not at an inlined frame.
if (!codeBlock->hasCodeOrigins()) {
readNonInlinedFrame(callFrame);
return;
}
unsigned index = callFrame->locationAsCodeOriginIndex();
ASSERT(codeBlock->canGetCodeOrigin(index));
if (!codeBlock->canGetCodeOrigin(index)) {
// See assertion above. In release builds, we try to protect ourselves
// from crashing even though stack walking will be goofed up.
m_frame.setToEnd();
return;
}
CodeOrigin codeOrigin = codeBlock->codeOrigin(index);
if (!codeOrigin.inlineCallFrame) {
readNonInlinedFrame(callFrame, &codeOrigin);
return;
}
readInlinedFrame(callFrame, &codeOrigin);
#endif // !ENABLE(DFG_JIT)
}
void StackVisitor::readNonInlinedFrame(CallFrame* callFrame, CodeOrigin* codeOrigin)
{
m_frame.m_callFrame = callFrame;
m_frame.m_argumentCountIncludingThis = callFrame->argumentCountIncludingThis();
m_frame.m_CallerVMEntryFrame = m_frame.m_VMEntryFrame;
m_frame.m_callerFrame = callFrame->callerFrame(m_frame.m_CallerVMEntryFrame);
m_frame.m_callerIsVMEntryFrame = m_frame.m_CallerVMEntryFrame != m_frame.m_VMEntryFrame;
m_frame.m_callee = callFrame->callee();
m_frame.m_scope = callFrame->scope();
m_frame.m_codeBlock = callFrame->codeBlock();
m_frame.m_bytecodeOffset = !m_frame.codeBlock() ? 0
: codeOrigin ? codeOrigin->bytecodeIndex
: callFrame->locationAsBytecodeOffset();
#if ENABLE(DFG_JIT)
m_frame.m_inlineCallFrame = 0;
#endif
}
#if ENABLE(DFG_JIT)
static int inlinedFrameOffset(CodeOrigin* codeOrigin)
{
InlineCallFrame* inlineCallFrame = codeOrigin->inlineCallFrame;
int frameOffset = inlineCallFrame ? inlineCallFrame->stackOffset : 0;
return frameOffset;
}
void StackVisitor::readInlinedFrame(CallFrame* callFrame, CodeOrigin* codeOrigin)
{
ASSERT(codeOrigin);
int frameOffset = inlinedFrameOffset(codeOrigin);
bool isInlined = !!frameOffset;
if (isInlined) {
InlineCallFrame* inlineCallFrame = codeOrigin->inlineCallFrame;
m_frame.m_callFrame = callFrame;
m_frame.m_inlineCallFrame = inlineCallFrame;
m_frame.m_argumentCountIncludingThis = inlineCallFrame->arguments.size();
m_frame.m_codeBlock = inlineCallFrame->baselineCodeBlock();
m_frame.m_bytecodeOffset = codeOrigin->bytecodeIndex;
JSFunction* callee = inlineCallFrame->calleeForCallFrame(callFrame);
m_frame.m_scope = callee->scope();
m_frame.m_callee = callee;
ASSERT(m_frame.scope());
ASSERT(m_frame.callee());
// The callerFrame just needs to be non-null to indicate that we
// haven't reached the last frame yet. Setting it to the root
// frame (i.e. the callFrame that this inlined frame is called from)
// would work just fine.
m_frame.m_callerFrame = callFrame;
return;
}
readNonInlinedFrame(callFrame, codeOrigin);
}
#endif // ENABLE(DFG_JIT)
StackVisitor::Frame::CodeType StackVisitor::Frame::codeType() const
{
if (!isJSFrame())
return CodeType::Native;
switch (codeBlock()->codeType()) {
case EvalCode:
return CodeType::Eval;
case FunctionCode:
return CodeType::Function;
case GlobalCode:
return CodeType::Global;
}
RELEASE_ASSERT_NOT_REACHED();
return CodeType::Global;
}
String StackVisitor::Frame::functionName()
{
String traceLine;
JSObject* callee = this->callee();
switch (codeType()) {
case CodeType::Eval:
traceLine = ASCIILiteral("eval code");
break;
case CodeType::Native:
if (callee)
traceLine = getCalculatedDisplayName(callFrame(), callee).impl();
break;
case CodeType::Function:
traceLine = getCalculatedDisplayName(callFrame(), callee).impl();
break;
case CodeType::Global:
traceLine = ASCIILiteral("global code");
break;
}
return traceLine.isNull() ? emptyString() : traceLine;
}
String StackVisitor::Frame::sourceURL()
{
String traceLine;
switch (codeType()) {
case CodeType::Eval:
case CodeType::Function:
case CodeType::Global: {
String sourceURL = codeBlock()->ownerExecutable()->sourceURL();
if (!sourceURL.isEmpty())
traceLine = sourceURL.impl();
break;
}
case CodeType::Native:
traceLine = ASCIILiteral("[native code]");
break;
}
return traceLine.isNull() ? emptyString() : traceLine;
}
String StackVisitor::Frame::toString()
{
StringBuilder traceBuild;
String functionName = this->functionName();
String sourceURL = this->sourceURL();
traceBuild.append(functionName);
if (!sourceURL.isEmpty()) {
if (!functionName.isEmpty())
traceBuild.append('@');
traceBuild.append(sourceURL);
if (isJSFrame()) {
unsigned line = 0;
unsigned column = 0;
computeLineAndColumn(line, column);
traceBuild.append(':');
traceBuild.appendNumber(line);
traceBuild.append(':');
traceBuild.appendNumber(column);
}
}
return traceBuild.toString().impl();
}
Arguments* StackVisitor::Frame::createArguments()
{
ASSERT(m_callFrame);
CallFrame* physicalFrame = m_callFrame;
VM& vm = physicalFrame->vm();
Arguments* arguments;
#if ENABLE(DFG_JIT)
if (isInlinedFrame()) {
ASSERT(m_inlineCallFrame);
arguments = Arguments::create(vm, physicalFrame, m_inlineCallFrame);
arguments->tearOff(physicalFrame, m_inlineCallFrame);
} else
#endif
{
arguments = Arguments::create(vm, physicalFrame);
arguments->tearOff(physicalFrame);
}
return arguments;
}
Arguments* StackVisitor::Frame::existingArguments()
{
if (codeBlock()->codeType() != FunctionCode)
return 0;
if (!codeBlock()->usesArguments())
return 0;
VirtualRegister reg;
#if ENABLE(DFG_JIT)
if (isInlinedFrame())
reg = inlineCallFrame()->argumentsRegister;
else
#endif // ENABLE(DFG_JIT)
reg = codeBlock()->argumentsRegister();
JSValue result = callFrame()->r(unmodifiedArgumentsRegister(reg).offset()).jsValue();
if (!result || !result.isCell()) // Protect against Undefined in case we throw in op_enter.
return 0;
return jsCast<Arguments*>(result);
}
void StackVisitor::Frame::computeLineAndColumn(unsigned& line, unsigned& column)
{
CodeBlock* codeBlock = this->codeBlock();
if (!codeBlock) {
line = 0;
column = 0;
return;
}
int divot = 0;
int unusedStartOffset = 0;
int unusedEndOffset = 0;
unsigned divotLine = 0;
unsigned divotColumn = 0;
retrieveExpressionInfo(divot, unusedStartOffset, unusedEndOffset, divotLine, divotColumn);
line = divotLine + codeBlock->ownerExecutable()->lineNo();
column = divotColumn + (divotLine ? 1 : codeBlock->firstLineColumnOffset());
}
void StackVisitor::Frame::retrieveExpressionInfo(int& divot, int& startOffset, int& endOffset, unsigned& line, unsigned& column)
{
CodeBlock* codeBlock = this->codeBlock();
codeBlock->unlinkedCodeBlock()->expressionRangeForBytecodeOffset(bytecodeOffset(), divot, startOffset, endOffset, line, column);
divot += codeBlock->sourceOffset();
}
void StackVisitor::Frame::setToEnd()
{
m_callFrame = 0;
#if ENABLE(DFG_JIT)
m_inlineCallFrame = 0;
#endif
}
#ifndef NDEBUG
static const char* jitTypeName(JITCode::JITType jitType)
{
switch (jitType) {
case JITCode::None: return "None";
case JITCode::HostCallThunk: return "HostCallThunk";
case JITCode::InterpreterThunk: return "InterpreterThunk";
case JITCode::BaselineJIT: return "BaselineJIT";
case JITCode::DFGJIT: return "DFGJIT";
case JITCode::FTLJIT: return "FTLJIT";
}
return "<unknown>";
}
static void printIndents(int levels)
{
while (levels--)
dataLogFString(" ");
}
static void printif(int indentLevels, const char* format, ...)
{
va_list argList;
va_start(argList, format);
if (indentLevels)
printIndents(indentLevels);
#if COMPILER(CLANG) || COMPILER(GCC)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
#pragma GCC diagnostic ignored "-Wmissing-format-attribute"
#endif
WTF::dataLogFV(format, argList);
#if COMPILER(CLANG) || COMPILER(GCC)
#pragma GCC diagnostic pop
#endif
va_end(argList);
}
void StackVisitor::Frame::print(int indentLevel)
{
int i = indentLevel;
if (!this->callFrame()) {
printif(i, "frame 0x0\n");
return;
}
CodeBlock* codeBlock = this->codeBlock();
printif(i, "frame %p {\n", this->callFrame());
CallFrame* callFrame = m_callFrame;
CallFrame* callerFrame = this->callerFrame();
void* returnPC = callFrame->hasReturnPC() ? callFrame->returnPC().value() : nullptr;
printif(i, " name '%s'\n", functionName().utf8().data());
printif(i, " sourceURL '%s'\n", sourceURL().utf8().data());
#if ENABLE(DFG_JIT)
printif(i, " isInlinedFrame %d\n", isInlinedFrame());
if (isInlinedFrame())
printif(i, " InlineCallFrame %p\n", m_inlineCallFrame);
#endif
printif(i, " callee %p\n", callee());
printif(i, " returnPC %p\n", returnPC);
printif(i, " callerFrame %p\n", callerFrame);
unsigned locationRawBits = callFrame->locationAsRawBits();
printif(i, " rawLocationBits %u 0x%x\n", locationRawBits, locationRawBits);
printif(i, " codeBlock %p\n", codeBlock);
if (codeBlock) {
JITCode::JITType jitType = codeBlock->jitType();
if (callFrame->hasLocationAsBytecodeOffset()) {
unsigned bytecodeOffset = callFrame->locationAsBytecodeOffset();
printif(i, " bytecodeOffset %u %p / %zu\n", bytecodeOffset, reinterpret_cast<void*>(bytecodeOffset), codeBlock->instructions().size());
#if ENABLE(DFG_JIT)
} else {
unsigned codeOriginIndex = callFrame->locationAsCodeOriginIndex();
printif(i, " codeOriginIdex %u %p / %zu\n", codeOriginIndex, reinterpret_cast<void*>(codeOriginIndex), codeBlock->codeOrigins().size());
#endif
}
unsigned line = 0;
unsigned column = 0;
computeLineAndColumn(line, column);
printif(i, " line %d\n", line);
printif(i, " column %d\n", column);
printif(i, " jitType %d <%s> isOptimizingJIT %d\n", jitType, jitTypeName(jitType), JITCode::isOptimizingJIT(jitType));
#if ENABLE(DFG_JIT)
printif(i, " hasCodeOrigins %d\n", codeBlock->hasCodeOrigins());
if (codeBlock->hasCodeOrigins()) {
JITCode* jitCode = codeBlock->jitCode().get();
printif(i, " jitCode %p start %p end %p\n", jitCode, jitCode->start(), jitCode->end());
}
#endif
}
printif(i, "}\n");
}
#endif // NDEBUG
} // namespace JSC
#ifndef NDEBUG
using JSC::StackVisitor;
// For debugging use
JS_EXPORT_PRIVATE void debugPrintCallFrame(JSC::CallFrame*);
JS_EXPORT_PRIVATE void debugPrintStack(JSC::CallFrame* topCallFrame);
class DebugPrintFrameFunctor {
public:
enum Action {
PrintOne,
PrintAll
};
DebugPrintFrameFunctor(Action action)
: m_action(action)
{
}
StackVisitor::Status operator()(StackVisitor& visitor)
{
visitor->print(2);
return m_action == PrintAll ? StackVisitor::Continue : StackVisitor::Done;
}
private:
Action m_action;
};
void debugPrintCallFrame(JSC::CallFrame* callFrame)
{
if (!callFrame)
return;
DebugPrintFrameFunctor functor(DebugPrintFrameFunctor::PrintOne);
callFrame->iterate(functor);
}
void debugPrintStack(JSC::CallFrame* topCallFrame)
{
if (!topCallFrame)
return;
DebugPrintFrameFunctor functor(DebugPrintFrameFunctor::PrintAll);
topCallFrame->iterate(functor);
}
#endif // !NDEBUG
|