ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_VM_STATE_INL_H_ |
| 29 | #define V8_VM_STATE_INL_H_ |
| 30 | |
| 31 | #include "vm-state.h" |
| 32 | |
| 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| 36 | // |
| 37 | // VMState class implementation. A simple stack of VM states held by the |
| 38 | // logger and partially threaded through the call stack. States are pushed by |
| 39 | // VMState construction and popped by destruction. |
| 40 | // |
| 41 | #ifdef ENABLE_VMSTATE_TRACKING |
| 42 | inline const char* StateToString(StateTag state) { |
| 43 | switch (state) { |
| 44 | case JS: |
| 45 | return "JS"; |
| 46 | case GC: |
| 47 | return "GC"; |
| 48 | case COMPILER: |
| 49 | return "COMPILER"; |
| 50 | case OTHER: |
| 51 | return "OTHER"; |
| 52 | default: |
| 53 | UNREACHABLE(); |
| 54 | return NULL; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | VMState::VMState(StateTag state) |
| 59 | : disabled_(true), |
| 60 | state_(OTHER), |
| 61 | external_callback_(NULL) { |
| 62 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 63 | if (!Logger::is_logging() && !CpuProfiler::is_profiling()) { |
| 64 | return; |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | disabled_ = false; |
| 69 | #if !defined(ENABLE_HEAP_PROTECTION) |
| 70 | // When not protecting the heap, there is no difference between |
| 71 | // EXTERNAL and OTHER. As an optimization in that case, we will not |
| 72 | // perform EXTERNAL->OTHER transitions through the API. We thus |
| 73 | // compress the two states into one. |
| 74 | if (state == EXTERNAL) state = OTHER; |
| 75 | #endif |
| 76 | state_ = state; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 77 | // Save the previous state. |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 78 | previous_ = Top::current_vm_state(); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 79 | // Install the new state. |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 80 | Top::set_current_vm_state(this); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 81 | |
| 82 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 83 | if (FLAG_log_state_changes) { |
| 84 | LOG(UncheckedStringEvent("Entering", StateToString(state_))); |
| 85 | if (previous_ != NULL) { |
| 86 | LOG(UncheckedStringEvent("From", StateToString(previous_->state_))); |
| 87 | } |
| 88 | } |
| 89 | #endif |
| 90 | |
| 91 | #ifdef ENABLE_HEAP_PROTECTION |
| 92 | if (FLAG_protect_heap) { |
| 93 | if (state_ == EXTERNAL) { |
| 94 | // We are leaving V8. |
| 95 | ASSERT((previous_ != NULL) && (previous_->state_ != EXTERNAL)); |
| 96 | Heap::Protect(); |
| 97 | } else if ((previous_ == NULL) || (previous_->state_ == EXTERNAL)) { |
| 98 | // We are entering V8. |
| 99 | Heap::Unprotect(); |
| 100 | } |
| 101 | } |
| 102 | #endif |
| 103 | } |
| 104 | |
| 105 | |
| 106 | VMState::~VMState() { |
| 107 | if (disabled_) return; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 108 | // Return to the previous state. |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 109 | Top::set_current_vm_state(previous_); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 110 | |
| 111 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 112 | if (FLAG_log_state_changes) { |
| 113 | LOG(UncheckedStringEvent("Leaving", StateToString(state_))); |
| 114 | if (previous_ != NULL) { |
| 115 | LOG(UncheckedStringEvent("To", StateToString(previous_->state_))); |
| 116 | } |
| 117 | } |
| 118 | #endif // ENABLE_LOGGING_AND_PROFILING |
| 119 | |
| 120 | #ifdef ENABLE_HEAP_PROTECTION |
| 121 | if (FLAG_protect_heap) { |
| 122 | if (state_ == EXTERNAL) { |
| 123 | // We are reentering V8. |
| 124 | ASSERT((previous_ != NULL) && (previous_->state_ != EXTERNAL)); |
| 125 | Heap::Unprotect(); |
| 126 | } else if ((previous_ == NULL) || (previous_->state_ == EXTERNAL)) { |
| 127 | // We are leaving V8. |
| 128 | Heap::Protect(); |
| 129 | } |
| 130 | } |
| 131 | #endif // ENABLE_HEAP_PROTECTION |
| 132 | } |
| 133 | #endif // ENABLE_VMSTATE_TRACKING |
| 134 | |
| 135 | } } // namespace v8::internal |
| 136 | |
| 137 | #endif // V8_VM_STATE_INL_H_ |