Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (c) 2010 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "bindings/v8/ScriptCallStackFactory.h"
     33 
     34 #include "bindings/v8/ScriptValue.h"
     35 #include "bindings/v8/V8Binding.h"
     36 #include "core/inspector/InspectorInstrumentation.h"
     37 #include "core/inspector/ScriptArguments.h"
     38 #include "core/inspector/ScriptCallFrame.h"
     39 #include "core/inspector/ScriptCallStack.h"
     40 #include "platform/JSONValues.h"
     41 #include "wtf/text/StringBuilder.h"
     42 
     43 #include <v8-debug.h>
     44 
     45 namespace WebCore {
     46 
     47 class ExecutionContext;
     48 
     49 static ScriptCallFrame toScriptCallFrame(v8::Handle<v8::StackFrame> frame)
     50 {
     51     StringBuilder stringBuilder;
     52     stringBuilder.appendNumber(frame->GetScriptId());
     53     String scriptId = stringBuilder.toString();
     54     String sourceName;
     55     v8::Local<v8::String> sourceNameValue(frame->GetScriptNameOrSourceURL());
     56     if (!sourceNameValue.IsEmpty())
     57         sourceName = toCoreString(sourceNameValue);
     58 
     59     String functionName;
     60     v8::Local<v8::String> functionNameValue(frame->GetFunctionName());
     61     if (!functionNameValue.IsEmpty())
     62         functionName = toCoreString(functionNameValue);
     63 
     64     int sourceLineNumber = frame->GetLineNumber();
     65     int sourceColumn = frame->GetColumn();
     66     return ScriptCallFrame(functionName, scriptId, sourceName, sourceLineNumber, sourceColumn);
     67 }
     68 
     69 static void toScriptCallFramesVector(v8::Handle<v8::StackTrace> stackTrace, Vector<ScriptCallFrame>& scriptCallFrames, size_t maxStackSize, bool emptyStackIsAllowed, v8::Isolate* isolate)
     70 {
     71     ASSERT(isolate->InContext());
     72     int frameCount = stackTrace->GetFrameCount();
     73     if (frameCount > static_cast<int>(maxStackSize))
     74         frameCount = maxStackSize;
     75     for (int i = 0; i < frameCount; i++) {
     76         v8::Local<v8::StackFrame> stackFrame = stackTrace->GetFrame(i);
     77         scriptCallFrames.append(toScriptCallFrame(stackFrame));
     78     }
     79     if (!frameCount && !emptyStackIsAllowed) {
     80         // Successfully grabbed stack trace, but there are no frames. It may happen in case
     81         // when a bound function is called from native code for example.
     82         // Fallback to setting lineNumber to 0, and source and function name to "undefined".
     83         scriptCallFrames.append(ScriptCallFrame("undefined", "", "undefined", 0));
     84     }
     85 }
     86 
     87 static PassRefPtrWillBeRawPtr<ScriptCallStack> createScriptCallStack(v8::Handle<v8::StackTrace> stackTrace, size_t maxStackSize, bool emptyStackIsAllowed, v8::Isolate* isolate)
     88 {
     89     ASSERT(isolate->InContext());
     90     v8::HandleScope scope(isolate);
     91     Vector<ScriptCallFrame> scriptCallFrames;
     92     toScriptCallFramesVector(stackTrace, scriptCallFrames, maxStackSize, emptyStackIsAllowed, isolate);
     93     return ScriptCallStack::create(scriptCallFrames);
     94 }
     95 
     96 PassRefPtrWillBeRawPtr<ScriptCallStack> createScriptCallStack(v8::Handle<v8::StackTrace> stackTrace, size_t maxStackSize, v8::Isolate* isolate)
     97 {
     98     return createScriptCallStack(stackTrace, maxStackSize, true, isolate);
     99 }
    100 
    101 PassRefPtrWillBeRawPtr<ScriptCallStack> createScriptCallStack(size_t maxStackSize, bool emptyStackIsAllowed)
    102 {
    103     v8::Isolate* isolate = v8::Isolate::GetCurrent();
    104     if (!isolate->InContext())
    105         return nullptr;
    106     v8::HandleScope handleScope(isolate);
    107     v8::Handle<v8::StackTrace> stackTrace(v8::StackTrace::CurrentStackTrace(isolate, maxStackSize, stackTraceOptions));
    108     return createScriptCallStack(stackTrace, maxStackSize, emptyStackIsAllowed, isolate);
    109 }
    110 
    111 PassRefPtrWillBeRawPtr<ScriptCallStack> createScriptCallStackForConsole(ScriptState* scriptState, size_t maxStackSize)
    112 {
    113     size_t stackSize = 1;
    114     if (InspectorInstrumentation::hasFrontends()) {
    115         if (InspectorInstrumentation::consoleAgentEnabled(scriptState->executionContext()))
    116             stackSize = maxStackSize;
    117     }
    118     return createScriptCallStack(stackSize);
    119 }
    120 
    121 PassRefPtrWillBeRawPtr<ScriptArguments> createScriptArguments(ScriptState* scriptState, const v8::FunctionCallbackInfo<v8::Value>& v8arguments, unsigned skipArgumentCount)
    122 {
    123     Vector<ScriptValue> arguments;
    124     for (int i = skipArgumentCount; i < v8arguments.Length(); ++i)
    125         arguments.append(ScriptValue(scriptState, v8arguments[i]));
    126 
    127     return ScriptArguments::create(scriptState, arguments);
    128 }
    129 
    130 } // namespace WebCore
    131