oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 1 | /* |
| 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.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 29 | #include <wtf/text/WTFString.h> |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 30 | |
| 31 | namespace JSC { |
| 32 | |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 33 | struct CodeOrigin; |
| 34 | struct InlineCallFrame; |
| 35 | |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 36 | class Arguments; |
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; |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 42 | |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 43 | typedef ExecState CallFrame; |
| 44 | |
| 45 | class StackIterator { |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 46 | public: |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 47 | class Frame { |
| 48 | public: |
| 49 | enum CodeType { |
| 50 | Global, |
| 51 | Eval, |
| 52 | Function, |
| 53 | Native |
| 54 | }; |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 55 | |
mark.lam@apple.com | bce4c9b | 2013-09-04 00:26:57 +0000 | [diff] [blame^] | 56 | size_t index() const { return m_index; } |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 57 | 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.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 65 | #endif |
| 66 | |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 67 | bool isJSFrame() const { return !!codeBlock(); } |
| 68 | #if ENABLE(DFG_JIT) |
| 69 | bool isInlinedFrame() const { return !!m_inlineCallFrame; } |
| 70 | #endif |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 71 | |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 72 | 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.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 91 | void setToEnd(); |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 92 | |
mark.lam@apple.com | bce4c9b | 2013-09-04 00:26:57 +0000 | [diff] [blame^] | 93 | size_t m_index; |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 94 | 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.com | bce4c9b | 2013-09-04 00:26:57 +0000 | [diff] [blame^] | 111 | 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.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 129 | 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.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 134 | private: |
| 135 | JS_EXPORT_PRIVATE StackIterator(CallFrame* startFrame, FrameFilter = 0); |
| 136 | |
mark.lam@apple.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 137 | 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.com | fd86164 | 2013-08-29 17:41:44 +0000 | [diff] [blame] | 149 | FrameFilter m_filter; |
| 150 | Frame m_frame; |
| 151 | |
| 152 | friend class ExecState; |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
oliver@apple.com | 2b2e132 | 2013-07-25 04:02:28 +0000 | [diff] [blame] | 155 | } // namespace JSC |
| 156 | |
| 157 | #endif // StackIterator_h |
| 158 | |