oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 2 | * Copyright (C) 2013, 2015 Apple Inc. All rights reserved. |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 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 | |
mark.lam@apple.com | fa2a142 | 2013-09-05 00:40:15 +0000 | [diff] [blame] | 26 | #ifndef StackVisitor_h |
| 27 | #define StackVisitor_h |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 28 | |
msaboff@apple.com | 4d563e4 | 2014-08-16 01:45:40 +0000 | [diff] [blame] | 29 | #include "VMEntryRecord.h" |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 30 | #include <wtf/text/WTFString.h> |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 31 | |
| 32 | namespace JSC { |
| 33 | |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 34 | struct CodeOrigin; |
| 35 | struct InlineCallFrame; |
| 36 | |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 37 | class CodeBlock; |
| 38 | class ExecState; |
| 39 | class JSFunction; |
| 40 | class JSObject; |
| 41 | class JSScope; |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 42 | class ClonedArguments; |
fpizlo@apple.com | 7130944 | 2013-09-21 17:40:35 +0000 | [diff] [blame] | 43 | class Register; |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 44 | |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 45 | typedef ExecState CallFrame; |
| 46 | |
mark.lam@apple.com | fa2a142 | 2013-09-05 00:40:15 +0000 | [diff] [blame] | 47 | class StackVisitor { |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 48 | public: |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 49 | class Frame { |
| 50 | public: |
| 51 | enum CodeType { |
| 52 | Global, |
| 53 | Eval, |
| 54 | Function, |
utatane.tea@gmail.com | a8309d9 | 2015-09-01 02:05:30 +0000 | [diff] [blame] | 55 | Module, |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 56 | Native |
| 57 | }; |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 58 | |
mark.lam@apple.com | bce4c9b | 2013-09-04 00:26:57 +0000 | [diff] [blame] | 59 | size_t index() const { return m_index; } |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 60 | size_t argumentCountIncludingThis() const { return m_argumentCountIncludingThis; } |
msaboff@apple.com | 4d563e4 | 2014-08-16 01:45:40 +0000 | [diff] [blame] | 61 | bool callerIsVMEntryFrame() const { return m_callerIsVMEntryFrame; } |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 62 | CallFrame* callerFrame() const { return m_callerFrame; } |
| 63 | JSObject* callee() const { return m_callee; } |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 64 | 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.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 70 | bool isJSFrame() const { return !!codeBlock(); } |
| 71 | #if ENABLE(DFG_JIT) |
| 72 | bool isInlinedFrame() const { return !!m_inlineCallFrame; } |
| 73 | #endif |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 74 | |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 75 | 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.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 82 | ClonedArguments* createArguments(); |
msaboff@apple.com | 0576b24 | 2014-08-22 19:54:30 +0000 | [diff] [blame] | 83 | VMEntryFrame* vmEntryFrame() const { return m_VMEntryFrame; } |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 84 | CallFrame* callFrame() const { return m_callFrame; } |
fpizlo@apple.com | 7130944 | 2013-09-21 17:40:35 +0000 | [diff] [blame] | 85 | |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 86 | JS_EXPORT_PRIVATE void print(int indentLevel); |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 87 | |
| 88 | private: |
| 89 | Frame() { } |
| 90 | ~Frame() { } |
| 91 | |
| 92 | void retrieveExpressionInfo(int& divot, int& startOffset, int& endOffset, unsigned& line, unsigned& column); |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 93 | void setToEnd(); |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 94 | |
mark.lam@apple.com | bce4c9b | 2013-09-04 00:26:57 +0000 | [diff] [blame] | 95 | size_t m_index; |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 96 | size_t m_argumentCountIncludingThis; |
msaboff@apple.com | 4d563e4 | 2014-08-16 01:45:40 +0000 | [diff] [blame] | 97 | VMEntryFrame* m_VMEntryFrame; |
msaboff@apple.com | 0576b24 | 2014-08-22 19:54:30 +0000 | [diff] [blame] | 98 | VMEntryFrame* m_CallerVMEntryFrame; |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 99 | CallFrame* m_callerFrame; |
| 100 | JSObject* m_callee; |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 101 | CodeBlock* m_codeBlock; |
| 102 | unsigned m_bytecodeOffset; |
msaboff@apple.com | 4d563e4 | 2014-08-16 01:45:40 +0000 | [diff] [blame] | 103 | bool m_callerIsVMEntryFrame; |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 104 | #if ENABLE(DFG_JIT) |
| 105 | InlineCallFrame* m_inlineCallFrame; |
| 106 | #endif |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 107 | CallFrame* m_callFrame; |
| 108 | |
mark.lam@apple.com | fa2a142 | 2013-09-05 00:40:15 +0000 | [diff] [blame] | 109 | friend class StackVisitor; |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
mark.lam@apple.com | bce4c9b | 2013-09-04 00:26:57 +0000 | [diff] [blame] | 112 | enum Status { |
| 113 | Continue = 0, |
| 114 | Done = 1 |
| 115 | }; |
| 116 | |
mark.lam@apple.com | fa2a142 | 2013-09-05 00:40:15 +0000 | [diff] [blame] | 117 | // StackVisitor::visit() expects a Functor that implements the following method: |
| 118 | // Status operator()(StackVisitor&); |
mark.lam@apple.com | bce4c9b | 2013-09-04 00:26:57 +0000 | [diff] [blame] | 119 | |
mark.lam@apple.com | fa2a142 | 2013-09-05 00:40:15 +0000 | [diff] [blame] | 120 | template <typename Functor> |
| 121 | static void visit(CallFrame* startFrame, Functor& functor) |
mark.lam@apple.com | bce4c9b | 2013-09-04 00:26:57 +0000 | [diff] [blame] | 122 | { |
mark.lam@apple.com | fa2a142 | 2013-09-05 00:40:15 +0000 | [diff] [blame] | 123 | StackVisitor visitor(startFrame); |
| 124 | while (visitor->callFrame()) { |
| 125 | Status status = functor(visitor); |
mark.lam@apple.com | bce4c9b | 2013-09-04 00:26:57 +0000 | [diff] [blame] | 126 | if (status != Continue) |
| 127 | break; |
mark.lam@apple.com | fa2a142 | 2013-09-05 00:40:15 +0000 | [diff] [blame] | 128 | visitor.gotoNextFrame(); |
mark.lam@apple.com | bce4c9b | 2013-09-04 00:26:57 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 132 | Frame& operator*() { return m_frame; } |
| 133 | ALWAYS_INLINE Frame* operator->() { return &m_frame; } |
sbarati@apple.com | 36c1340 | 2015-09-18 23:37:42 +0000 | [diff] [blame] | 134 | void unwindToMachineCodeBlockFrame(); |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 135 | |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 136 | private: |
mark.lam@apple.com | fa2a142 | 2013-09-05 00:40:15 +0000 | [diff] [blame] | 137 | JS_EXPORT_PRIVATE StackVisitor(CallFrame* startFrame); |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 138 | |
mark.lam@apple.com | 99c89d4 | 2013-09-04 22:33:57 +0000 | [diff] [blame] | 139 | JS_EXPORT_PRIVATE void gotoNextFrame(); |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 140 | |
| 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.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 147 | Frame m_frame; |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
msaboff@apple.com | bfc39d9 | 2014-11-06 19:47:17 +0000 | [diff] [blame] | 150 | class CallerFunctor { |
| 151 | public: |
| 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 | |
| 171 | private: |
| 172 | bool m_hasSkippedFirstFrame; |
| 173 | CallFrame* m_callerFrame; |
| 174 | }; |
| 175 | |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 176 | } // namespace JSC |
| 177 | |
mark.lam@apple.com | fa2a142 | 2013-09-05 00:40:15 +0000 | [diff] [blame] | 178 | #endif // StackVisitor_h |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 179 | |