blob: bbf37fe9c79cf3a6a299c3d13988d9d67e8a7174 [file] [log] [blame]
oliver@apple.com2b2e1322013-07-25 04:02:28 +00001/*
fpizlo@apple.comda834ae2015-03-26 04:28:43 +00002 * Copyright (C) 2013, 2015 Apple Inc. All rights reserved.
oliver@apple.com2b2e1322013-07-25 04:02:28 +00003 *
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
mark.lam@apple.comfa2a1422013-09-05 00:40:15 +000026#ifndef StackVisitor_h
27#define StackVisitor_h
oliver@apple.com2b2e1322013-07-25 04:02:28 +000028
msaboff@apple.com4d563e42014-08-16 01:45:40 +000029#include "VMEntryRecord.h"
mark.lam@apple.comfd861642013-08-29 17:41:44 +000030#include <wtf/text/WTFString.h>
oliver@apple.com2b2e1322013-07-25 04:02:28 +000031
32namespace JSC {
33
mark.lam@apple.comfd861642013-08-29 17:41:44 +000034struct CodeOrigin;
35struct InlineCallFrame;
36
mark.lam@apple.comfd861642013-08-29 17:41:44 +000037class CodeBlock;
38class ExecState;
39class JSFunction;
40class JSObject;
41class JSScope;
fpizlo@apple.comda834ae2015-03-26 04:28:43 +000042class ClonedArguments;
fpizlo@apple.com71309442013-09-21 17:40:35 +000043class Register;
oliver@apple.com2b2e1322013-07-25 04:02:28 +000044
mark.lam@apple.comfd861642013-08-29 17:41:44 +000045typedef ExecState CallFrame;
46
mark.lam@apple.comfa2a1422013-09-05 00:40:15 +000047class StackVisitor {
oliver@apple.com2b2e1322013-07-25 04:02:28 +000048public:
mark.lam@apple.comfd861642013-08-29 17:41:44 +000049 class Frame {
50 public:
51 enum CodeType {
52 Global,
53 Eval,
54 Function,
utatane.tea@gmail.coma8309d92015-09-01 02:05:30 +000055 Module,
mark.lam@apple.comfd861642013-08-29 17:41:44 +000056 Native
57 };
oliver@apple.com2b2e1322013-07-25 04:02:28 +000058
mark.lam@apple.combce4c9b2013-09-04 00:26:57 +000059 size_t index() const { return m_index; }
mark.lam@apple.comfd861642013-08-29 17:41:44 +000060 size_t argumentCountIncludingThis() const { return m_argumentCountIncludingThis; }
msaboff@apple.com4d563e42014-08-16 01:45:40 +000061 bool callerIsVMEntryFrame() const { return m_callerIsVMEntryFrame; }
mark.lam@apple.comfd861642013-08-29 17:41:44 +000062 CallFrame* callerFrame() const { return m_callerFrame; }
63 JSObject* callee() const { return m_callee; }
mark.lam@apple.comfd861642013-08-29 17:41:44 +000064 CodeBlock* codeBlock() const { return m_codeBlock; }
65 unsigned bytecodeOffset() const { return m_bytecodeOffset; }
66#if ENABLE(DFG_JIT)
67 InlineCallFrame* inlineCallFrame() const { return m_inlineCallFrame; }
oliver@apple.com2b2e1322013-07-25 04:02:28 +000068#endif
69
mark.lam@apple.comfd861642013-08-29 17:41:44 +000070 bool isJSFrame() const { return !!codeBlock(); }
71#if ENABLE(DFG_JIT)
72 bool isInlinedFrame() const { return !!m_inlineCallFrame; }
73#endif
oliver@apple.com2b2e1322013-07-25 04:02:28 +000074
mark.lam@apple.comfd861642013-08-29 17:41:44 +000075 JS_EXPORT_PRIVATE String functionName();
76 JS_EXPORT_PRIVATE String sourceURL();
77 JS_EXPORT_PRIVATE String toString();
78
79 CodeType codeType() const;
80 JS_EXPORT_PRIVATE void computeLineAndColumn(unsigned& line, unsigned& column);
81
fpizlo@apple.comda834ae2015-03-26 04:28:43 +000082 ClonedArguments* createArguments();
msaboff@apple.com0576b242014-08-22 19:54:30 +000083 VMEntryFrame* vmEntryFrame() const { return m_VMEntryFrame; }
mark.lam@apple.comfd861642013-08-29 17:41:44 +000084 CallFrame* callFrame() const { return m_callFrame; }
fpizlo@apple.com71309442013-09-21 17:40:35 +000085
mark.lam@apple.comfd861642013-08-29 17:41:44 +000086 JS_EXPORT_PRIVATE void print(int indentLevel);
mark.lam@apple.comfd861642013-08-29 17:41:44 +000087
88 private:
89 Frame() { }
90 ~Frame() { }
91
92 void retrieveExpressionInfo(int& divot, int& startOffset, int& endOffset, unsigned& line, unsigned& column);
mark.lam@apple.comfd861642013-08-29 17:41:44 +000093 void setToEnd();
mark.lam@apple.comfd861642013-08-29 17:41:44 +000094
mark.lam@apple.combce4c9b2013-09-04 00:26:57 +000095 size_t m_index;
mark.lam@apple.comfd861642013-08-29 17:41:44 +000096 size_t m_argumentCountIncludingThis;
msaboff@apple.com4d563e42014-08-16 01:45:40 +000097 VMEntryFrame* m_VMEntryFrame;
msaboff@apple.com0576b242014-08-22 19:54:30 +000098 VMEntryFrame* m_CallerVMEntryFrame;
mark.lam@apple.comfd861642013-08-29 17:41:44 +000099 CallFrame* m_callerFrame;
100 JSObject* m_callee;
mark.lam@apple.comfd861642013-08-29 17:41:44 +0000101 CodeBlock* m_codeBlock;
102 unsigned m_bytecodeOffset;
msaboff@apple.com4d563e42014-08-16 01:45:40 +0000103 bool m_callerIsVMEntryFrame;
mark.lam@apple.comfd861642013-08-29 17:41:44 +0000104#if ENABLE(DFG_JIT)
105 InlineCallFrame* m_inlineCallFrame;
106#endif
mark.lam@apple.comfd861642013-08-29 17:41:44 +0000107 CallFrame* m_callFrame;
108
mark.lam@apple.comfa2a1422013-09-05 00:40:15 +0000109 friend class StackVisitor;
mark.lam@apple.comfd861642013-08-29 17:41:44 +0000110 };
111
mark.lam@apple.combce4c9b2013-09-04 00:26:57 +0000112 enum Status {
113 Continue = 0,
114 Done = 1
115 };
116
mark.lam@apple.comfa2a1422013-09-05 00:40:15 +0000117 // StackVisitor::visit() expects a Functor that implements the following method:
118 // Status operator()(StackVisitor&);
mark.lam@apple.combce4c9b2013-09-04 00:26:57 +0000119
mark.lam@apple.comfa2a1422013-09-05 00:40:15 +0000120 template <typename Functor>
121 static void visit(CallFrame* startFrame, Functor& functor)
mark.lam@apple.combce4c9b2013-09-04 00:26:57 +0000122 {
mark.lam@apple.comfa2a1422013-09-05 00:40:15 +0000123 StackVisitor visitor(startFrame);
124 while (visitor->callFrame()) {
125 Status status = functor(visitor);
mark.lam@apple.combce4c9b2013-09-04 00:26:57 +0000126 if (status != Continue)
127 break;
mark.lam@apple.comfa2a1422013-09-05 00:40:15 +0000128 visitor.gotoNextFrame();
mark.lam@apple.combce4c9b2013-09-04 00:26:57 +0000129 }
130 }
131
mark.lam@apple.comfd861642013-08-29 17:41:44 +0000132 Frame& operator*() { return m_frame; }
133 ALWAYS_INLINE Frame* operator->() { return &m_frame; }
sbarati@apple.com36c13402015-09-18 23:37:42 +0000134 void unwindToMachineCodeBlockFrame();
mark.lam@apple.comfd861642013-08-29 17:41:44 +0000135
mark.lam@apple.comfd861642013-08-29 17:41:44 +0000136private:
mark.lam@apple.comfa2a1422013-09-05 00:40:15 +0000137 JS_EXPORT_PRIVATE StackVisitor(CallFrame* startFrame);
mark.lam@apple.comfd861642013-08-29 17:41:44 +0000138
mark.lam@apple.com99c89d42013-09-04 22:33:57 +0000139 JS_EXPORT_PRIVATE void gotoNextFrame();
mark.lam@apple.comfd861642013-08-29 17:41:44 +0000140
141 void readFrame(CallFrame*);
142 void readNonInlinedFrame(CallFrame*, CodeOrigin* = 0);
143#if ENABLE(DFG_JIT)
144 void readInlinedFrame(CallFrame*, CodeOrigin*);
145#endif
146
mark.lam@apple.comfd861642013-08-29 17:41:44 +0000147 Frame m_frame;
oliver@apple.com2b2e1322013-07-25 04:02:28 +0000148};
149
msaboff@apple.combfc39d92014-11-06 19:47:17 +0000150class CallerFunctor {
151public:
152 CallerFunctor()
153 : m_hasSkippedFirstFrame(false)
154 , m_callerFrame(0)
155 {
156 }
157
158 CallFrame* callerFrame() const { return m_callerFrame; }
159
160 StackVisitor::Status operator()(StackVisitor& visitor)
161 {
162 if (!m_hasSkippedFirstFrame) {
163 m_hasSkippedFirstFrame = true;
164 return StackVisitor::Continue;
165 }
166
167 m_callerFrame = visitor->callFrame();
168 return StackVisitor::Done;
169 }
170
171private:
172 bool m_hasSkippedFirstFrame;
173 CallFrame* m_callerFrame;
174};
175
oliver@apple.com2b2e1322013-07-25 04:02:28 +0000176} // namespace JSC
177
mark.lam@apple.comfa2a1422013-09-05 00:40:15 +0000178#endif // StackVisitor_h
oliver@apple.com2b2e1322013-07-25 04:02:28 +0000179