blob: ac5dd4e1d6eadafe8f99b2ce8e1cb034cc3fbf19 [file] [log] [blame]
oliver@apple.com2b2e1322013-07-25 04:02:28 +00001/*
2 * Copyright (C) 2013 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#ifndef StackIterator_h
27#define StackIterator_h
28
mark.lam@apple.comfd861642013-08-29 17:41:44 +000029#include <wtf/text/WTFString.h>
oliver@apple.com2b2e1322013-07-25 04:02:28 +000030
31namespace JSC {
32
mark.lam@apple.comfd861642013-08-29 17:41:44 +000033struct CodeOrigin;
34struct InlineCallFrame;
35
oliver@apple.com2b2e1322013-07-25 04:02:28 +000036class Arguments;
mark.lam@apple.comfd861642013-08-29 17:41:44 +000037class CodeBlock;
38class ExecState;
39class JSFunction;
40class JSObject;
41class JSScope;
oliver@apple.com2b2e1322013-07-25 04:02:28 +000042
mark.lam@apple.comfd861642013-08-29 17:41:44 +000043typedef ExecState CallFrame;
44
45class StackIterator {
oliver@apple.com2b2e1322013-07-25 04:02:28 +000046public:
mark.lam@apple.comfd861642013-08-29 17:41:44 +000047 class Frame {
48 public:
49 enum CodeType {
50 Global,
51 Eval,
52 Function,
53 Native
54 };
oliver@apple.com2b2e1322013-07-25 04:02:28 +000055
mark.lam@apple.combce4c9b2013-09-04 00:26:57 +000056 size_t index() const { return m_index; }
mark.lam@apple.comfd861642013-08-29 17:41:44 +000057 size_t argumentCountIncludingThis() const { return m_argumentCountIncludingThis; }
58 CallFrame* callerFrame() const { return m_callerFrame; }
59 JSObject* callee() const { return m_callee; }
60 JSScope* scope() const { return m_scope; }
61 CodeBlock* codeBlock() const { return m_codeBlock; }
62 unsigned bytecodeOffset() const { return m_bytecodeOffset; }
63#if ENABLE(DFG_JIT)
64 InlineCallFrame* inlineCallFrame() const { return m_inlineCallFrame; }
oliver@apple.com2b2e1322013-07-25 04:02:28 +000065#endif
66
mark.lam@apple.comfd861642013-08-29 17:41:44 +000067 bool isJSFrame() const { return !!codeBlock(); }
68#if ENABLE(DFG_JIT)
69 bool isInlinedFrame() const { return !!m_inlineCallFrame; }
70#endif
oliver@apple.com2b2e1322013-07-25 04:02:28 +000071
mark.lam@apple.comfd861642013-08-29 17:41:44 +000072 JS_EXPORT_PRIVATE String functionName();
73 JS_EXPORT_PRIVATE String sourceURL();
74 JS_EXPORT_PRIVATE String toString();
75
76 CodeType codeType() const;
77 JS_EXPORT_PRIVATE void computeLineAndColumn(unsigned& line, unsigned& column);
78
79 Arguments* arguments();
80 CallFrame* callFrame() const { return m_callFrame; }
81
82#ifndef NDEBUG
83 JS_EXPORT_PRIVATE void print(int indentLevel);
84#endif
85
86 private:
87 Frame() { }
88 ~Frame() { }
89
90 void retrieveExpressionInfo(int& divot, int& startOffset, int& endOffset, unsigned& line, unsigned& column);
mark.lam@apple.comfd861642013-08-29 17:41:44 +000091 void setToEnd();
mark.lam@apple.comfd861642013-08-29 17:41:44 +000092
mark.lam@apple.combce4c9b2013-09-04 00:26:57 +000093 size_t m_index;
mark.lam@apple.comfd861642013-08-29 17:41:44 +000094 size_t m_argumentCountIncludingThis;
95 CallFrame* m_callerFrame;
96 JSObject* m_callee;
97 JSScope* m_scope;
98 CodeBlock* m_codeBlock;
99 unsigned m_bytecodeOffset;
100#if ENABLE(DFG_JIT)
101 InlineCallFrame* m_inlineCallFrame;
102#endif
103
104 CallFrame* m_callFrame;
105
106 friend class StackIterator;
107 };
108
109 typedef bool (*FrameFilter)(Frame*);
110
mark.lam@apple.combce4c9b2013-09-04 00:26:57 +0000111 enum Status {
112 Continue = 0,
113 Done = 1
114 };
115
116 // StackIterator::iterate() expects a Functor that implements the following method:
117 // Status operator()(StackIterator&);
118
119 template <typename Functor> void iterate(Functor& functor)
120 {
121 while (m_frame.callFrame()) {
122 Status status = functor(*this);
123 if (status != Continue)
124 break;
125 gotoNextFrameWithFilter();
126 }
127 }
128
mark.lam@apple.comfd861642013-08-29 17:41:44 +0000129 JS_EXPORT_PRIVATE size_t numberOfFrames();
130
131 Frame& operator*() { return m_frame; }
132 ALWAYS_INLINE Frame* operator->() { return &m_frame; }
133
mark.lam@apple.comfd861642013-08-29 17:41:44 +0000134private:
135 JS_EXPORT_PRIVATE StackIterator(CallFrame* startFrame, FrameFilter = 0);
136
mark.lam@apple.comfd861642013-08-29 17:41:44 +0000137 void gotoFrameAtIndex(size_t frameIndex);
138 void gotoNextFrame();
139 JS_EXPORT_PRIVATE void gotoNextFrameWithFilter();
140 void resetIterator();
141
142 void readFrame(CallFrame*);
143 void readNonInlinedFrame(CallFrame*, CodeOrigin* = 0);
144#if ENABLE(DFG_JIT)
145 void readInlinedFrame(CallFrame*, CodeOrigin*);
146#endif
147
148 CallFrame* m_startFrame;
mark.lam@apple.comfd861642013-08-29 17:41:44 +0000149 FrameFilter m_filter;
150 Frame m_frame;
151
152 friend class ExecState;
oliver@apple.com2b2e1322013-07-25 04:02:28 +0000153};
154
oliver@apple.com2b2e1322013-07-25 04:02:28 +0000155} // namespace JSC
156
157#endif // StackIterator_h
158