1/*
2 * Copyright (C) 2016-2019 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "StackFrame.h"
28
29#include "CodeBlock.h"
30#include "DebuggerPrimitives.h"
31#include "JSCInlines.h"
32#include <wtf/text/StringBuilder.h>
33
34namespace JSC {
35
36StackFrame::StackFrame(VM& vm, JSCell* owner, JSCell* callee)
37 : m_callee(vm, owner, callee)
38{
39}
40
41StackFrame::StackFrame(VM& vm, JSCell* owner, JSCell* callee, CodeBlock* codeBlock, BytecodeIndex bytecodeIndex)
42 : m_callee(vm, owner, callee)
43 , m_codeBlock(vm, owner, codeBlock)
44 , m_bytecodeIndex(bytecodeIndex)
45{
46}
47
48StackFrame::StackFrame(Wasm::IndexOrName indexOrName)
49 : m_wasmFunctionIndexOrName(indexOrName)
50 , m_isWasmFrame(true)
51{
52}
53
54intptr_t StackFrame::sourceID() const
55{
56 if (!m_codeBlock)
57 return noSourceID;
58 return m_codeBlock->ownerExecutable()->sourceID();
59}
60
61String StackFrame::sourceURL() const
62{
63 if (m_isWasmFrame)
64 return "[wasm code]"_s;
65
66 if (!m_codeBlock) {
67 return "[native code]"_s;
68 }
69
70 String sourceURL = m_codeBlock->ownerExecutable()->sourceURL();
71 if (!sourceURL.isNull())
72 return sourceURL;
73 return emptyString();
74}
75
76String StackFrame::functionName(VM& vm) const
77{
78 if (m_isWasmFrame)
79 return makeString(m_wasmFunctionIndexOrName);
80
81 if (m_codeBlock) {
82 switch (m_codeBlock->codeType()) {
83 case EvalCode:
84 return "eval code"_s;
85 case ModuleCode:
86 return "module code"_s;
87 case FunctionCode:
88 break;
89 case GlobalCode:
90 return "global code"_s;
91 default:
92 ASSERT_NOT_REACHED();
93 }
94 }
95 String name;
96 if (m_callee) {
97 if (m_callee->isObject())
98 name = getCalculatedDisplayName(vm, jsCast<JSObject*>(m_callee.get())).impl();
99 }
100 return name.isNull() ? emptyString() : name;
101}
102
103void StackFrame::computeLineAndColumn(unsigned& line, unsigned& column) const
104{
105 if (!m_codeBlock) {
106 line = 0;
107 column = 0;
108 return;
109 }
110
111 int divot = 0;
112 int unusedStartOffset = 0;
113 int unusedEndOffset = 0;
114 m_codeBlock->expressionRangeForBytecodeIndex(m_bytecodeIndex, divot, unusedStartOffset, unusedEndOffset, line, column);
115
116 ScriptExecutable* executable = m_codeBlock->ownerExecutable();
117 if (Optional<int> overrideLineNumber = executable->overrideLineNumber(m_codeBlock->vm()))
118 line = overrideLineNumber.value();
119}
120
121String StackFrame::toString(VM& vm) const
122{
123 StringBuilder traceBuild;
124 String functionName = this->functionName(vm);
125 String sourceURL = this->sourceURL();
126 traceBuild.append(functionName);
127 if (!sourceURL.isEmpty()) {
128 if (!functionName.isEmpty())
129 traceBuild.append('@');
130 traceBuild.append(sourceURL);
131 if (hasLineAndColumnInfo()) {
132 unsigned line;
133 unsigned column;
134 computeLineAndColumn(line, column);
135
136 traceBuild.append(':');
137 traceBuild.appendNumber(line);
138 traceBuild.append(':');
139 traceBuild.appendNumber(column);
140 }
141 }
142 return traceBuild.toString().impl();
143}
144
145void StackFrame::visitChildren(SlotVisitor& visitor)
146{
147 if (m_callee)
148 visitor.append(m_callee);
149 if (m_codeBlock)
150 visitor.append(m_codeBlock);
151}
152
153} // namespace JSC
154
155