ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | #include "v8.h" |
| 29 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 30 | #include "ast.h" |
| 31 | #include "deoptimizer.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include "frames-inl.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 33 | #include "full-codegen.h" |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 34 | #include "mark-compact.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 35 | #include "safepoint-table.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | #include "scopeinfo.h" |
| 37 | #include "string-stream.h" |
| 38 | #include "top.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 40 | namespace v8 { |
| 41 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 43 | PcToCodeCache::PcToCodeCacheEntry |
| 44 | PcToCodeCache::cache_[PcToCodeCache::kPcToCodeCacheSize]; |
| 45 | |
| 46 | int SafeStackFrameIterator::active_count_ = 0; |
| 47 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | // Iterator that supports traversing the stack handlers of a |
| 49 | // particular frame. Needs to know the top of the handler chain. |
| 50 | class StackHandlerIterator BASE_EMBEDDED { |
| 51 | public: |
| 52 | StackHandlerIterator(const StackFrame* frame, StackHandler* handler) |
| 53 | : limit_(frame->fp()), handler_(handler) { |
| 54 | // Make sure the handler has already been unwound to this frame. |
| 55 | ASSERT(frame->sp() <= handler->address()); |
| 56 | } |
| 57 | |
| 58 | StackHandler* handler() const { return handler_; } |
| 59 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 60 | bool done() { |
| 61 | return handler_ == NULL || handler_->address() > limit_; |
| 62 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | void Advance() { |
| 64 | ASSERT(!done()); |
| 65 | handler_ = handler_->next(); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | const Address limit_; |
| 70 | StackHandler* handler_; |
| 71 | }; |
| 72 | |
| 73 | |
| 74 | // ------------------------------------------------------------------------- |
| 75 | |
| 76 | |
| 77 | #define INITIALIZE_SINGLETON(type, field) field##_(this), |
| 78 | StackFrameIterator::StackFrameIterator() |
| 79 | : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON) |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 80 | frame_(NULL), handler_(NULL), thread_(Top::GetCurrentThread()), |
| 81 | fp_(NULL), sp_(NULL), advance_(&StackFrameIterator::AdvanceWithHandler) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 82 | Reset(); |
| 83 | } |
| 84 | StackFrameIterator::StackFrameIterator(ThreadLocalTop* t) |
| 85 | : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON) |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 86 | frame_(NULL), handler_(NULL), thread_(t), |
| 87 | fp_(NULL), sp_(NULL), advance_(&StackFrameIterator::AdvanceWithHandler) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 88 | Reset(); |
| 89 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 90 | StackFrameIterator::StackFrameIterator(bool use_top, Address fp, Address sp) |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 91 | : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON) |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 92 | frame_(NULL), handler_(NULL), |
| 93 | thread_(use_top ? Top::GetCurrentThread() : NULL), |
| 94 | fp_(use_top ? NULL : fp), sp_(sp), |
| 95 | advance_(use_top ? &StackFrameIterator::AdvanceWithHandler : |
| 96 | &StackFrameIterator::AdvanceWithoutHandler) { |
| 97 | if (use_top || fp != NULL) { |
| 98 | Reset(); |
| 99 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 100 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 101 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 102 | #undef INITIALIZE_SINGLETON |
| 103 | |
| 104 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 105 | void StackFrameIterator::AdvanceWithHandler() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 106 | ASSERT(!done()); |
| 107 | // Compute the state of the calling frame before restoring |
| 108 | // callee-saved registers and unwinding handlers. This allows the |
| 109 | // frame code that computes the caller state to access the top |
| 110 | // handler and the value of any callee-saved register if needed. |
| 111 | StackFrame::State state; |
| 112 | StackFrame::Type type = frame_->GetCallerState(&state); |
| 113 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 114 | // Unwind handlers corresponding to the current frame. |
| 115 | StackHandlerIterator it(frame_, handler_); |
| 116 | while (!it.done()) it.Advance(); |
| 117 | handler_ = it.handler(); |
| 118 | |
| 119 | // Advance to the calling frame. |
| 120 | frame_ = SingletonFor(type, &state); |
| 121 | |
| 122 | // When we're done iterating over the stack frames, the handler |
| 123 | // chain must have been completely unwound. |
| 124 | ASSERT(!done() || handler_ == NULL); |
| 125 | } |
| 126 | |
| 127 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 128 | void StackFrameIterator::AdvanceWithoutHandler() { |
| 129 | // A simpler version of Advance which doesn't care about handler. |
| 130 | ASSERT(!done()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | StackFrame::State state; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 132 | StackFrame::Type type = frame_->GetCallerState(&state); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | frame_ = SingletonFor(type, &state); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
| 137 | void StackFrameIterator::Reset() { |
| 138 | StackFrame::State state; |
| 139 | StackFrame::Type type; |
| 140 | if (thread_ != NULL) { |
| 141 | type = ExitFrame::GetStateForFramePointer(Top::c_entry_fp(thread_), &state); |
| 142 | handler_ = StackHandler::FromAddress(Top::handler(thread_)); |
| 143 | } else { |
| 144 | ASSERT(fp_ != NULL); |
| 145 | state.fp = fp_; |
| 146 | state.sp = sp_; |
| 147 | state.pc_address = |
| 148 | reinterpret_cast<Address*>(StandardFrame::ComputePCAddress(fp_)); |
| 149 | type = StackFrame::ComputeType(&state); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 150 | } |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 151 | if (SingletonFor(type) == NULL) return; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 152 | frame_ = SingletonFor(type, &state); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
| 156 | StackFrame* StackFrameIterator::SingletonFor(StackFrame::Type type, |
| 157 | StackFrame::State* state) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 158 | if (type == StackFrame::NONE) return NULL; |
| 159 | StackFrame* result = SingletonFor(type); |
| 160 | ASSERT(result != NULL); |
| 161 | result->state_ = *state; |
| 162 | return result; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | StackFrame* StackFrameIterator::SingletonFor(StackFrame::Type type) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | #define FRAME_TYPE_CASE(type, field) \ |
| 168 | case StackFrame::type: result = &field##_; break; |
| 169 | |
| 170 | StackFrame* result = NULL; |
| 171 | switch (type) { |
| 172 | case StackFrame::NONE: return NULL; |
| 173 | STACK_FRAME_TYPE_LIST(FRAME_TYPE_CASE) |
| 174 | default: break; |
| 175 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 176 | return result; |
| 177 | |
| 178 | #undef FRAME_TYPE_CASE |
| 179 | } |
| 180 | |
| 181 | |
| 182 | // ------------------------------------------------------------------------- |
| 183 | |
| 184 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 185 | StackTraceFrameIterator::StackTraceFrameIterator() { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 186 | if (!done() && !IsValidFrame()) Advance(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
| 190 | void StackTraceFrameIterator::Advance() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 191 | while (true) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 192 | JavaScriptFrameIterator::Advance(); |
| 193 | if (done()) return; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 194 | if (IsValidFrame()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 198 | bool StackTraceFrameIterator::IsValidFrame() { |
| 199 | if (!frame()->function()->IsJSFunction()) return false; |
| 200 | Object* script = JSFunction::cast(frame()->function())->shared()->script(); |
| 201 | // Don't show functions from native scripts to user. |
| 202 | return (script->IsScript() && |
| 203 | Script::TYPE_NATIVE != Script::cast(script)->type()->value()); |
| 204 | } |
| 205 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 206 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 207 | // ------------------------------------------------------------------------- |
| 208 | |
| 209 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 210 | bool SafeStackFrameIterator::ExitFrameValidator::IsValidFP(Address fp) { |
| 211 | if (!validator_.IsValid(fp)) return false; |
| 212 | Address sp = ExitFrame::ComputeStackPointer(fp); |
| 213 | if (!validator_.IsValid(sp)) return false; |
| 214 | StackFrame::State state; |
| 215 | ExitFrame::FillState(fp, sp, &state); |
| 216 | if (!validator_.IsValid(reinterpret_cast<Address>(state.pc_address))) { |
| 217 | return false; |
| 218 | } |
| 219 | return *state.pc_address != NULL; |
| 220 | } |
| 221 | |
| 222 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 223 | SafeStackFrameIterator::SafeStackFrameIterator( |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 224 | Address fp, Address sp, Address low_bound, Address high_bound) : |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 225 | maintainer_(), |
| 226 | stack_validator_(low_bound, high_bound), |
| 227 | is_valid_top_(IsValidTop(low_bound, high_bound)), |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 228 | is_valid_fp_(IsWithinBounds(low_bound, high_bound, fp)), |
| 229 | is_working_iterator_(is_valid_top_ || is_valid_fp_), |
| 230 | iteration_done_(!is_working_iterator_), |
| 231 | iterator_(is_valid_top_, is_valid_fp_ ? fp : NULL, sp) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 235 | bool SafeStackFrameIterator::IsValidTop(Address low_bound, Address high_bound) { |
| 236 | Address fp = Top::c_entry_fp(Top::GetCurrentThread()); |
| 237 | ExitFrameValidator validator(low_bound, high_bound); |
| 238 | if (!validator.IsValidFP(fp)) return false; |
| 239 | return Top::handler(Top::GetCurrentThread()) != NULL; |
| 240 | } |
| 241 | |
| 242 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 243 | void SafeStackFrameIterator::Advance() { |
| 244 | ASSERT(is_working_iterator_); |
| 245 | ASSERT(!done()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 246 | StackFrame* last_frame = iterator_.frame(); |
| 247 | Address last_sp = last_frame->sp(), last_fp = last_frame->fp(); |
| 248 | // Before advancing to the next stack frame, perform pointer validity tests |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 249 | iteration_done_ = !IsValidFrame(last_frame) || |
| 250 | !CanIterateHandles(last_frame, iterator_.handler()) || |
| 251 | !IsValidCaller(last_frame); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 252 | if (iteration_done_) return; |
| 253 | |
| 254 | iterator_.Advance(); |
| 255 | if (iterator_.done()) return; |
| 256 | // Check that we have actually moved to the previous frame in the stack |
| 257 | StackFrame* prev_frame = iterator_.frame(); |
| 258 | iteration_done_ = prev_frame->sp() < last_sp || prev_frame->fp() < last_fp; |
| 259 | } |
| 260 | |
| 261 | |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 262 | bool SafeStackFrameIterator::CanIterateHandles(StackFrame* frame, |
| 263 | StackHandler* handler) { |
| 264 | // If StackIterator iterates over StackHandles, verify that |
| 265 | // StackHandlerIterator can be instantiated (see StackHandlerIterator |
| 266 | // constructor.) |
| 267 | return !is_valid_top_ || (frame->sp() <= handler->address()); |
| 268 | } |
| 269 | |
| 270 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 271 | bool SafeStackFrameIterator::IsValidFrame(StackFrame* frame) const { |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 272 | return IsValidStackAddress(frame->sp()) && IsValidStackAddress(frame->fp()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
| 276 | bool SafeStackFrameIterator::IsValidCaller(StackFrame* frame) { |
| 277 | StackFrame::State state; |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 278 | if (frame->is_entry() || frame->is_entry_construct()) { |
| 279 | // See EntryFrame::GetCallerState. It computes the caller FP address |
| 280 | // and calls ExitFrame::GetStateForFramePointer on it. We need to be |
| 281 | // sure that caller FP address is valid. |
| 282 | Address caller_fp = Memory::Address_at( |
| 283 | frame->fp() + EntryFrameConstants::kCallerFPOffset); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 284 | ExitFrameValidator validator(stack_validator_); |
| 285 | if (!validator.IsValidFP(caller_fp)) return false; |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 286 | } else if (frame->is_arguments_adaptor()) { |
| 287 | // See ArgumentsAdaptorFrame::GetCallerStackPointer. It assumes that |
| 288 | // the number of arguments is stored on stack as Smi. We need to check |
| 289 | // that it really an Smi. |
| 290 | Object* number_of_args = reinterpret_cast<ArgumentsAdaptorFrame*>(frame)-> |
| 291 | GetExpression(0); |
| 292 | if (!number_of_args->IsSmi()) { |
| 293 | return false; |
| 294 | } |
| 295 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 296 | frame->ComputeCallerState(&state); |
| 297 | return IsValidStackAddress(state.sp) && IsValidStackAddress(state.fp) && |
| 298 | iterator_.SingletonFor(frame->GetCallerState(&state)) != NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 302 | void SafeStackFrameIterator::Reset() { |
| 303 | if (is_working_iterator_) { |
| 304 | iterator_.Reset(); |
| 305 | iteration_done_ = false; |
| 306 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 310 | // ------------------------------------------------------------------------- |
| 311 | |
| 312 | |
| 313 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 314 | SafeStackTraceFrameIterator::SafeStackTraceFrameIterator( |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 315 | Address fp, Address sp, Address low_bound, Address high_bound) : |
| 316 | SafeJavaScriptFrameIterator(fp, sp, low_bound, high_bound) { |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 317 | if (!done() && !frame()->is_java_script()) Advance(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 321 | void SafeStackTraceFrameIterator::Advance() { |
| 322 | while (true) { |
| 323 | SafeJavaScriptFrameIterator::Advance(); |
| 324 | if (done()) return; |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 325 | if (frame()->is_java_script()) return; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | #endif |
| 329 | |
| 330 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 331 | Code* StackFrame::GetSafepointData(Address pc, |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 332 | SafepointEntry* safepoint_entry, |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 333 | unsigned* stack_slots) { |
| 334 | PcToCodeCache::PcToCodeCacheEntry* entry = PcToCodeCache::GetCacheEntry(pc); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 335 | SafepointEntry cached_safepoint_entry = entry->safepoint_entry; |
| 336 | if (!entry->safepoint_entry.is_valid()) { |
| 337 | entry->safepoint_entry = entry->code->GetSafepointEntry(pc); |
| 338 | ASSERT(entry->safepoint_entry.is_valid()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 339 | } else { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 340 | ASSERT(entry->safepoint_entry.Equals(entry->code->GetSafepointEntry(pc))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // Fill in the results and return the code. |
| 344 | Code* code = entry->code; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 345 | *safepoint_entry = entry->safepoint_entry; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 346 | *stack_slots = code->stack_slots(); |
| 347 | return code; |
| 348 | } |
| 349 | |
| 350 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 351 | bool StackFrame::HasHandler() const { |
| 352 | StackHandlerIterator it(this, top_handler()); |
| 353 | return !it.done(); |
| 354 | } |
| 355 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 356 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 357 | void StackFrame::IteratePc(ObjectVisitor* v, |
| 358 | Address* pc_address, |
| 359 | Code* holder) { |
| 360 | Address pc = *pc_address; |
| 361 | ASSERT(holder->contains(pc)); |
| 362 | unsigned pc_offset = static_cast<unsigned>(pc - holder->instruction_start()); |
| 363 | Object* code = holder; |
| 364 | v->VisitPointer(&code); |
| 365 | if (code != holder) { |
| 366 | holder = reinterpret_cast<Code*>(code); |
| 367 | pc = holder->instruction_start() + pc_offset; |
| 368 | *pc_address = pc; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 369 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 373 | StackFrame::Type StackFrame::ComputeType(State* state) { |
| 374 | ASSERT(state->fp != NULL); |
| 375 | if (StandardFrame::IsArgumentsAdaptorFrame(state->fp)) { |
| 376 | return ARGUMENTS_ADAPTOR; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 377 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 378 | // The marker and function offsets overlap. If the marker isn't a |
| 379 | // smi then the frame is a JavaScript frame -- and the marker is |
| 380 | // really the function. |
| 381 | const int offset = StandardFrameConstants::kMarkerOffset; |
| 382 | Object* marker = Memory::Object_at(state->fp + offset); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 383 | if (!marker->IsSmi()) { |
| 384 | // If we're using a "safe" stack iterator, we treat optimized |
| 385 | // frames as normal JavaScript frames to avoid having to look |
| 386 | // into the heap to determine the state. This is safe as long |
| 387 | // as nobody tries to GC... |
| 388 | if (SafeStackFrameIterator::is_active()) return JAVA_SCRIPT; |
| 389 | Code::Kind kind = GetContainingCode(*(state->pc_address))->kind(); |
| 390 | ASSERT(kind == Code::FUNCTION || kind == Code::OPTIMIZED_FUNCTION); |
| 391 | return (kind == Code::OPTIMIZED_FUNCTION) ? OPTIMIZED : JAVA_SCRIPT; |
| 392 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 393 | return static_cast<StackFrame::Type>(Smi::cast(marker)->value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 397 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 398 | StackFrame::Type StackFrame::GetCallerState(State* state) const { |
| 399 | ComputeCallerState(state); |
| 400 | return ComputeType(state); |
| 401 | } |
| 402 | |
| 403 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 404 | Code* EntryFrame::unchecked_code() const { |
| 405 | return Heap::raw_unchecked_js_entry_code(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 409 | void EntryFrame::ComputeCallerState(State* state) const { |
| 410 | GetCallerState(state); |
| 411 | } |
| 412 | |
| 413 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 414 | void EntryFrame::SetCallerFp(Address caller_fp) { |
| 415 | const int offset = EntryFrameConstants::kCallerFPOffset; |
| 416 | Memory::Address_at(this->fp() + offset) = caller_fp; |
| 417 | } |
| 418 | |
| 419 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 420 | StackFrame::Type EntryFrame::GetCallerState(State* state) const { |
| 421 | const int offset = EntryFrameConstants::kCallerFPOffset; |
| 422 | Address fp = Memory::Address_at(this->fp() + offset); |
| 423 | return ExitFrame::GetStateForFramePointer(fp, state); |
| 424 | } |
| 425 | |
| 426 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 427 | Code* EntryConstructFrame::unchecked_code() const { |
| 428 | return Heap::raw_unchecked_js_construct_entry_code(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 432 | Object*& ExitFrame::code_slot() const { |
| 433 | const int offset = ExitFrameConstants::kCodeOffset; |
| 434 | return Memory::Object_at(fp() + offset); |
| 435 | } |
| 436 | |
| 437 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 438 | Code* ExitFrame::unchecked_code() const { |
| 439 | return reinterpret_cast<Code*>(code_slot()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 443 | void ExitFrame::ComputeCallerState(State* state) const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 444 | // Setup the caller state. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 445 | state->sp = caller_sp(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 446 | state->fp = Memory::Address_at(fp() + ExitFrameConstants::kCallerFPOffset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 447 | state->pc_address |
| 448 | = reinterpret_cast<Address*>(fp() + ExitFrameConstants::kCallerPCOffset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 452 | void ExitFrame::SetCallerFp(Address caller_fp) { |
| 453 | Memory::Address_at(fp() + ExitFrameConstants::kCallerFPOffset) = caller_fp; |
| 454 | } |
| 455 | |
| 456 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 457 | void ExitFrame::Iterate(ObjectVisitor* v) const { |
| 458 | // The arguments are traversed as part of the expression stack of |
| 459 | // the calling frame. |
| 460 | IteratePc(v, pc_address(), code()); |
| 461 | v->VisitPointer(&code_slot()); |
| 462 | } |
| 463 | |
| 464 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 465 | Address ExitFrame::GetCallerStackPointer() const { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 466 | return fp() + ExitFrameConstants::kCallerSPDisplacement; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 470 | StackFrame::Type ExitFrame::GetStateForFramePointer(Address fp, State* state) { |
| 471 | if (fp == 0) return NONE; |
| 472 | Address sp = ComputeStackPointer(fp); |
| 473 | FillState(fp, sp, state); |
| 474 | ASSERT(*state->pc_address != NULL); |
| 475 | return EXIT; |
| 476 | } |
| 477 | |
| 478 | |
| 479 | void ExitFrame::FillState(Address fp, Address sp, State* state) { |
| 480 | state->sp = sp; |
| 481 | state->fp = fp; |
| 482 | state->pc_address = reinterpret_cast<Address*>(sp - 1 * kPointerSize); |
| 483 | } |
| 484 | |
| 485 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 486 | Address StandardFrame::GetExpressionAddress(int n) const { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 487 | const int offset = StandardFrameConstants::kExpressionsOffset; |
| 488 | return fp() + offset - n * kPointerSize; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | |
| 492 | int StandardFrame::ComputeExpressionsCount() const { |
| 493 | const int offset = |
| 494 | StandardFrameConstants::kExpressionsOffset + kPointerSize; |
| 495 | Address base = fp() + offset; |
| 496 | Address limit = sp(); |
| 497 | ASSERT(base >= limit); // stack grows downwards |
| 498 | // Include register-allocated locals in number of expressions. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 499 | return static_cast<int>((base - limit) / kPointerSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 503 | void StandardFrame::ComputeCallerState(State* state) const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 504 | state->sp = caller_sp(); |
| 505 | state->fp = caller_fp(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 506 | state->pc_address = reinterpret_cast<Address*>(ComputePCAddress(fp())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 510 | void StandardFrame::SetCallerFp(Address caller_fp) { |
| 511 | Memory::Address_at(fp() + StandardFrameConstants::kCallerFPOffset) = |
| 512 | caller_fp; |
| 513 | } |
| 514 | |
| 515 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 516 | bool StandardFrame::IsExpressionInsideHandler(int n) const { |
| 517 | Address address = GetExpressionAddress(n); |
| 518 | for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) { |
| 519 | if (it.handler()->includes(address)) return true; |
| 520 | } |
| 521 | return false; |
| 522 | } |
| 523 | |
| 524 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 525 | void OptimizedFrame::Iterate(ObjectVisitor* v) const { |
| 526 | #ifdef DEBUG |
| 527 | // Make sure that optimized frames do not contain any stack handlers. |
| 528 | StackHandlerIterator it(this, top_handler()); |
| 529 | ASSERT(it.done()); |
| 530 | #endif |
| 531 | |
| 532 | // Make sure that we're not doing "safe" stack frame iteration. We cannot |
| 533 | // possibly find pointers in optimized frames in that state. |
| 534 | ASSERT(!SafeStackFrameIterator::is_active()); |
| 535 | |
| 536 | // Compute the safepoint information. |
| 537 | unsigned stack_slots = 0; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 538 | SafepointEntry safepoint_entry; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 539 | Code* code = StackFrame::GetSafepointData( |
| 540 | pc(), &safepoint_entry, &stack_slots); |
| 541 | unsigned slot_space = stack_slots * kPointerSize; |
| 542 | |
| 543 | // Visit the outgoing parameters. This is usually dealt with by the |
| 544 | // callee, but while GC'ing we artificially lower the number of |
| 545 | // arguments to zero and let the caller deal with it. |
| 546 | Object** parameters_base = &Memory::Object_at(sp()); |
| 547 | Object** parameters_limit = &Memory::Object_at( |
| 548 | fp() + JavaScriptFrameConstants::kFunctionOffset - slot_space); |
| 549 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 550 | // Visit the parameters that may be on top of the saved registers. |
| 551 | if (safepoint_entry.argument_count() > 0) { |
| 552 | v->VisitPointers(parameters_base, |
| 553 | parameters_base + safepoint_entry.argument_count()); |
| 554 | parameters_base += safepoint_entry.argument_count(); |
| 555 | } |
| 556 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 557 | // Skip saved double registers. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 558 | if (safepoint_entry.has_doubles()) { |
| 559 | parameters_base += DoubleRegister::kNumAllocatableRegisters * |
| 560 | kDoubleSize / kPointerSize; |
| 561 | } |
| 562 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 563 | // Visit the registers that contain pointers if any. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 564 | if (safepoint_entry.HasRegisters()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 565 | for (int i = kNumSafepointRegisters - 1; i >=0; i--) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 566 | if (safepoint_entry.HasRegisterAt(i)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 567 | int reg_stack_index = MacroAssembler::SafepointRegisterStackIndex(i); |
| 568 | v->VisitPointer(parameters_base + reg_stack_index); |
| 569 | } |
| 570 | } |
| 571 | // Skip the words containing the register values. |
| 572 | parameters_base += kNumSafepointRegisters; |
| 573 | } |
| 574 | |
| 575 | // We're done dealing with the register bits. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 576 | uint8_t* safepoint_bits = safepoint_entry.bits(); |
| 577 | safepoint_bits += kNumSafepointRegisters >> kBitsPerByteLog2; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 578 | |
| 579 | // Visit the rest of the parameters. |
| 580 | v->VisitPointers(parameters_base, parameters_limit); |
| 581 | |
| 582 | // Visit pointer spill slots and locals. |
| 583 | for (unsigned index = 0; index < stack_slots; index++) { |
| 584 | int byte_index = index >> kBitsPerByteLog2; |
| 585 | int bit_index = index & (kBitsPerByte - 1); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 586 | if ((safepoint_bits[byte_index] & (1U << bit_index)) != 0) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 587 | v->VisitPointer(parameters_limit + index); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | // Visit the context and the function. |
| 592 | Object** fixed_base = &Memory::Object_at( |
| 593 | fp() + JavaScriptFrameConstants::kFunctionOffset); |
| 594 | Object** fixed_limit = &Memory::Object_at(fp()); |
| 595 | v->VisitPointers(fixed_base, fixed_limit); |
| 596 | |
| 597 | // Visit the return address in the callee and incoming arguments. |
| 598 | IteratePc(v, pc_address(), code); |
| 599 | IterateArguments(v); |
| 600 | } |
| 601 | |
| 602 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 603 | Object* JavaScriptFrame::GetParameter(int index) const { |
| 604 | ASSERT(index >= 0 && index < ComputeParametersCount()); |
| 605 | const int offset = JavaScriptFrameConstants::kParam0Offset; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 606 | return Memory::Object_at(caller_sp() + offset - (index * kPointerSize)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | |
| 610 | int JavaScriptFrame::ComputeParametersCount() const { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 611 | Address base = caller_sp() + JavaScriptFrameConstants::kReceiverOffset; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 612 | Address limit = fp() + JavaScriptFrameConstants::kSavedRegistersOffset; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 613 | return static_cast<int>((base - limit) / kPointerSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | |
| 617 | bool JavaScriptFrame::IsConstructor() const { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 618 | Address fp = caller_fp(); |
| 619 | if (has_adapted_arguments()) { |
| 620 | // Skip the arguments adaptor frame and look at the real caller. |
| 621 | fp = Memory::Address_at(fp + StandardFrameConstants::kCallerFPOffset); |
| 622 | } |
| 623 | return IsConstructFrame(fp); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 627 | Code* JavaScriptFrame::unchecked_code() const { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 628 | JSFunction* function = JSFunction::cast(this->function()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 629 | return function->unchecked_code(); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 633 | int JavaScriptFrame::GetProvidedParametersCount() const { |
| 634 | return ComputeParametersCount(); |
| 635 | } |
| 636 | |
| 637 | |
| 638 | Address JavaScriptFrame::GetCallerStackPointer() const { |
| 639 | int arguments; |
| 640 | if (Heap::gc_state() != Heap::NOT_IN_GC || |
| 641 | SafeStackFrameIterator::is_active()) { |
| 642 | // If the we are currently iterating the safe stack the |
| 643 | // arguments for frames are traversed as if they were |
| 644 | // expression stack elements of the calling frame. The reason for |
| 645 | // this rather strange decision is that we cannot access the |
| 646 | // function during mark-compact GCs when objects may have been marked. |
| 647 | // In fact accessing heap objects (like function->shared() below) |
| 648 | // at all during GC is problematic. |
| 649 | arguments = 0; |
| 650 | } else { |
| 651 | // Compute the number of arguments by getting the number of formal |
| 652 | // parameters of the function. We must remember to take the |
| 653 | // receiver into account (+1). |
| 654 | JSFunction* function = JSFunction::cast(this->function()); |
| 655 | arguments = function->shared()->formal_parameter_count() + 1; |
| 656 | } |
| 657 | const int offset = StandardFrameConstants::kCallerSPOffset; |
| 658 | return fp() + offset + (arguments * kPointerSize); |
| 659 | } |
| 660 | |
| 661 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 662 | void JavaScriptFrame::GetFunctions(List<JSFunction*>* functions) { |
| 663 | ASSERT(functions->length() == 0); |
| 664 | functions->Add(JSFunction::cast(function())); |
| 665 | } |
| 666 | |
| 667 | |
| 668 | void JavaScriptFrame::Summarize(List<FrameSummary>* functions) { |
| 669 | ASSERT(functions->length() == 0); |
| 670 | Code* code_pointer = code(); |
| 671 | int offset = static_cast<int>(pc() - code_pointer->address()); |
| 672 | FrameSummary summary(receiver(), |
| 673 | JSFunction::cast(function()), |
| 674 | code_pointer, |
| 675 | offset, |
| 676 | IsConstructor()); |
| 677 | functions->Add(summary); |
| 678 | } |
| 679 | |
| 680 | |
| 681 | void FrameSummary::Print() { |
| 682 | PrintF("receiver: "); |
| 683 | receiver_->ShortPrint(); |
| 684 | PrintF("\nfunction: "); |
| 685 | function_->shared()->DebugName()->ShortPrint(); |
| 686 | PrintF("\ncode: "); |
| 687 | code_->ShortPrint(); |
| 688 | if (code_->kind() == Code::FUNCTION) PrintF(" NON-OPT"); |
| 689 | if (code_->kind() == Code::OPTIMIZED_FUNCTION) PrintF(" OPT"); |
| 690 | PrintF("\npc: %d\n", offset_); |
| 691 | } |
| 692 | |
| 693 | |
| 694 | void OptimizedFrame::Summarize(List<FrameSummary>* frames) { |
| 695 | ASSERT(frames->length() == 0); |
| 696 | ASSERT(is_optimized()); |
| 697 | |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame^] | 698 | int deopt_index = Safepoint::kNoDeoptimizationIndex; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 699 | DeoptimizationInputData* data = GetDeoptimizationData(&deopt_index); |
| 700 | |
| 701 | // BUG(3243555): Since we don't have a lazy-deopt registered at |
| 702 | // throw-statements, we can't use the translation at the call-site of |
| 703 | // throw. An entry with no deoptimization index indicates a call-site |
| 704 | // without a lazy-deopt. As a consequence we are not allowed to inline |
| 705 | // functions containing throw. |
| 706 | if (deopt_index == Safepoint::kNoDeoptimizationIndex) { |
| 707 | JavaScriptFrame::Summarize(frames); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | TranslationIterator it(data->TranslationByteArray(), |
| 712 | data->TranslationIndex(deopt_index)->value()); |
| 713 | Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next()); |
| 714 | ASSERT(opcode == Translation::BEGIN); |
| 715 | int frame_count = it.Next(); |
| 716 | |
| 717 | // We create the summary in reverse order because the frames |
| 718 | // in the deoptimization translation are ordered bottom-to-top. |
| 719 | int i = frame_count; |
| 720 | while (i > 0) { |
| 721 | opcode = static_cast<Translation::Opcode>(it.Next()); |
| 722 | if (opcode == Translation::FRAME) { |
| 723 | // We don't inline constructor calls, so only the first, outermost |
| 724 | // frame can be a constructor frame in case of inlining. |
| 725 | bool is_constructor = (i == frame_count) && IsConstructor(); |
| 726 | |
| 727 | i--; |
| 728 | int ast_id = it.Next(); |
| 729 | int function_id = it.Next(); |
| 730 | it.Next(); // Skip height. |
| 731 | JSFunction* function = |
| 732 | JSFunction::cast(data->LiteralArray()->get(function_id)); |
| 733 | |
| 734 | // The translation commands are ordered and the receiver is always |
| 735 | // at the first position. Since we are always at a call when we need |
| 736 | // to construct a stack trace, the receiver is always in a stack slot. |
| 737 | opcode = static_cast<Translation::Opcode>(it.Next()); |
| 738 | ASSERT(opcode == Translation::STACK_SLOT); |
| 739 | int input_slot_index = it.Next(); |
| 740 | |
| 741 | // Get the correct receiver in the optimized frame. |
| 742 | Object* receiver = NULL; |
| 743 | // Positive index means the value is spilled to the locals area. Negative |
| 744 | // means it is stored in the incoming parameter area. |
| 745 | if (input_slot_index >= 0) { |
| 746 | receiver = GetExpression(input_slot_index); |
| 747 | } else { |
| 748 | // Index -1 overlaps with last parameter, -n with the first parameter, |
| 749 | // (-n - 1) with the receiver with n being the number of parameters |
| 750 | // of the outermost, optimized frame. |
| 751 | int parameter_count = ComputeParametersCount(); |
| 752 | int parameter_index = input_slot_index + parameter_count; |
| 753 | receiver = (parameter_index == -1) |
| 754 | ? this->receiver() |
| 755 | : this->GetParameter(parameter_index); |
| 756 | } |
| 757 | |
| 758 | Code* code = function->shared()->code(); |
| 759 | DeoptimizationOutputData* output_data = |
| 760 | DeoptimizationOutputData::cast(code->deoptimization_data()); |
| 761 | unsigned entry = Deoptimizer::GetOutputInfo(output_data, |
| 762 | ast_id, |
| 763 | function->shared()); |
| 764 | unsigned pc_offset = |
| 765 | FullCodeGenerator::PcField::decode(entry) + Code::kHeaderSize; |
| 766 | ASSERT(pc_offset > 0); |
| 767 | |
| 768 | FrameSummary summary(receiver, function, code, pc_offset, is_constructor); |
| 769 | frames->Add(summary); |
| 770 | } else { |
| 771 | // Skip over operands to advance to the next opcode. |
| 772 | it.Skip(Translation::NumberOfOperandsFor(opcode)); |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | |
| 778 | DeoptimizationInputData* OptimizedFrame::GetDeoptimizationData( |
| 779 | int* deopt_index) { |
| 780 | ASSERT(is_optimized()); |
| 781 | |
| 782 | JSFunction* opt_function = JSFunction::cast(function()); |
| 783 | Code* code = opt_function->code(); |
| 784 | |
| 785 | // The code object may have been replaced by lazy deoptimization. Fall |
| 786 | // back to a slow search in this case to find the original optimized |
| 787 | // code object. |
| 788 | if (!code->contains(pc())) { |
| 789 | code = PcToCodeCache::GcSafeFindCodeForPc(pc()); |
| 790 | } |
| 791 | ASSERT(code != NULL); |
| 792 | ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION); |
| 793 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 794 | SafepointEntry safepoint_entry = code->GetSafepointEntry(pc()); |
| 795 | *deopt_index = safepoint_entry.deoptimization_index(); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame^] | 796 | ASSERT(*deopt_index != Safepoint::kNoDeoptimizationIndex); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 797 | |
| 798 | return DeoptimizationInputData::cast(code->deoptimization_data()); |
| 799 | } |
| 800 | |
| 801 | |
| 802 | void OptimizedFrame::GetFunctions(List<JSFunction*>* functions) { |
| 803 | ASSERT(functions->length() == 0); |
| 804 | ASSERT(is_optimized()); |
| 805 | |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame^] | 806 | int deopt_index = Safepoint::kNoDeoptimizationIndex; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 807 | DeoptimizationInputData* data = GetDeoptimizationData(&deopt_index); |
| 808 | |
| 809 | TranslationIterator it(data->TranslationByteArray(), |
| 810 | data->TranslationIndex(deopt_index)->value()); |
| 811 | Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next()); |
| 812 | ASSERT(opcode == Translation::BEGIN); |
| 813 | int frame_count = it.Next(); |
| 814 | |
| 815 | // We insert the frames in reverse order because the frames |
| 816 | // in the deoptimization translation are ordered bottom-to-top. |
| 817 | while (frame_count > 0) { |
| 818 | opcode = static_cast<Translation::Opcode>(it.Next()); |
| 819 | if (opcode == Translation::FRAME) { |
| 820 | frame_count--; |
| 821 | it.Next(); // Skip ast id. |
| 822 | int function_id = it.Next(); |
| 823 | it.Next(); // Skip height. |
| 824 | JSFunction* function = |
| 825 | JSFunction::cast(data->LiteralArray()->get(function_id)); |
| 826 | functions->Add(function); |
| 827 | } else { |
| 828 | // Skip over operands to advance to the next opcode. |
| 829 | it.Skip(Translation::NumberOfOperandsFor(opcode)); |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 835 | Address ArgumentsAdaptorFrame::GetCallerStackPointer() const { |
| 836 | const int arguments = Smi::cast(GetExpression(0))->value(); |
| 837 | const int offset = StandardFrameConstants::kCallerSPOffset; |
| 838 | return fp() + offset + (arguments + 1) * kPointerSize; |
| 839 | } |
| 840 | |
| 841 | |
| 842 | Address InternalFrame::GetCallerStackPointer() const { |
| 843 | // Internal frames have no arguments. The stack pointer of the |
| 844 | // caller is at a fixed offset from the frame pointer. |
| 845 | return fp() + StandardFrameConstants::kCallerSPOffset; |
| 846 | } |
| 847 | |
| 848 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 849 | Code* ArgumentsAdaptorFrame::unchecked_code() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 850 | return Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline); |
| 851 | } |
| 852 | |
| 853 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 854 | Code* InternalFrame::unchecked_code() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 855 | const int offset = InternalFrameConstants::kCodeOffset; |
| 856 | Object* code = Memory::Object_at(fp() + offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 857 | ASSERT(code != NULL); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 858 | return reinterpret_cast<Code*>(code); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | |
| 862 | void StackFrame::PrintIndex(StringStream* accumulator, |
| 863 | PrintMode mode, |
| 864 | int index) { |
| 865 | accumulator->Add((mode == OVERVIEW) ? "%5d: " : "[%d]: ", index); |
| 866 | } |
| 867 | |
| 868 | |
| 869 | void JavaScriptFrame::Print(StringStream* accumulator, |
| 870 | PrintMode mode, |
| 871 | int index) const { |
| 872 | HandleScope scope; |
| 873 | Object* receiver = this->receiver(); |
| 874 | Object* function = this->function(); |
| 875 | |
| 876 | accumulator->PrintSecurityTokenIfChanged(function); |
| 877 | PrintIndex(accumulator, mode, index); |
| 878 | Code* code = NULL; |
| 879 | if (IsConstructor()) accumulator->Add("new "); |
| 880 | accumulator->PrintFunction(function, receiver, &code); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 881 | |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 882 | Handle<SerializedScopeInfo> scope_info(SerializedScopeInfo::Empty()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 883 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 884 | if (function->IsJSFunction()) { |
| 885 | Handle<SharedFunctionInfo> shared(JSFunction::cast(function)->shared()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 886 | scope_info = Handle<SerializedScopeInfo>(shared->scope_info()); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 887 | Object* script_obj = shared->script(); |
| 888 | if (script_obj->IsScript()) { |
| 889 | Handle<Script> script(Script::cast(script_obj)); |
| 890 | accumulator->Add(" ["); |
| 891 | accumulator->PrintName(script->name()); |
| 892 | |
| 893 | Address pc = this->pc(); |
| 894 | if (code != NULL && code->kind() == Code::FUNCTION && |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 895 | pc >= code->instruction_start() && pc < code->instruction_end()) { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 896 | int source_pos = code->SourcePosition(pc); |
| 897 | int line = GetScriptLineNumberSafe(script, source_pos) + 1; |
| 898 | accumulator->Add(":%d", line); |
| 899 | } else { |
| 900 | int function_start_pos = shared->start_position(); |
| 901 | int line = GetScriptLineNumberSafe(script, function_start_pos) + 1; |
| 902 | accumulator->Add(":~%d", line); |
| 903 | } |
| 904 | |
| 905 | accumulator->Add("] "); |
| 906 | } |
| 907 | } |
| 908 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 909 | accumulator->Add("(this=%o", receiver); |
| 910 | |
| 911 | // Get scope information for nicer output, if possible. If code is |
| 912 | // NULL, or doesn't contain scope info, info will return 0 for the |
| 913 | // number of parameters, stack slots, or context slots. |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 914 | ScopeInfo<PreallocatedStorage> info(*scope_info); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 915 | |
| 916 | // Print the parameters. |
| 917 | int parameters_count = ComputeParametersCount(); |
| 918 | for (int i = 0; i < parameters_count; i++) { |
| 919 | accumulator->Add(","); |
| 920 | // If we have a name for the parameter we print it. Nameless |
| 921 | // parameters are either because we have more actual parameters |
| 922 | // than formal parameters or because we have no scope information. |
| 923 | if (i < info.number_of_parameters()) { |
| 924 | accumulator->PrintName(*info.parameter_name(i)); |
| 925 | accumulator->Add("="); |
| 926 | } |
| 927 | accumulator->Add("%o", GetParameter(i)); |
| 928 | } |
| 929 | |
| 930 | accumulator->Add(")"); |
| 931 | if (mode == OVERVIEW) { |
| 932 | accumulator->Add("\n"); |
| 933 | return; |
| 934 | } |
| 935 | accumulator->Add(" {\n"); |
| 936 | |
| 937 | // Compute the number of locals and expression stack elements. |
| 938 | int stack_locals_count = info.number_of_stack_slots(); |
| 939 | int heap_locals_count = info.number_of_context_slots(); |
| 940 | int expressions_count = ComputeExpressionsCount(); |
| 941 | |
| 942 | // Print stack-allocated local variables. |
| 943 | if (stack_locals_count > 0) { |
| 944 | accumulator->Add(" // stack-allocated locals\n"); |
| 945 | } |
| 946 | for (int i = 0; i < stack_locals_count; i++) { |
| 947 | accumulator->Add(" var "); |
| 948 | accumulator->PrintName(*info.stack_slot_name(i)); |
| 949 | accumulator->Add(" = "); |
| 950 | if (i < expressions_count) { |
| 951 | accumulator->Add("%o", GetExpression(i)); |
| 952 | } else { |
| 953 | accumulator->Add("// no expression found - inconsistent frame?"); |
| 954 | } |
| 955 | accumulator->Add("\n"); |
| 956 | } |
| 957 | |
| 958 | // Try to get hold of the context of this frame. |
| 959 | Context* context = NULL; |
| 960 | if (this->context() != NULL && this->context()->IsContext()) { |
| 961 | context = Context::cast(this->context()); |
| 962 | } |
| 963 | |
| 964 | // Print heap-allocated local variables. |
| 965 | if (heap_locals_count > Context::MIN_CONTEXT_SLOTS) { |
| 966 | accumulator->Add(" // heap-allocated locals\n"); |
| 967 | } |
| 968 | for (int i = Context::MIN_CONTEXT_SLOTS; i < heap_locals_count; i++) { |
| 969 | accumulator->Add(" var "); |
| 970 | accumulator->PrintName(*info.context_slot_name(i)); |
| 971 | accumulator->Add(" = "); |
| 972 | if (context != NULL) { |
| 973 | if (i < context->length()) { |
| 974 | accumulator->Add("%o", context->get(i)); |
| 975 | } else { |
| 976 | accumulator->Add( |
| 977 | "// warning: missing context slot - inconsistent frame?"); |
| 978 | } |
| 979 | } else { |
| 980 | accumulator->Add("// warning: no context found - inconsistent frame?"); |
| 981 | } |
| 982 | accumulator->Add("\n"); |
| 983 | } |
| 984 | |
| 985 | // Print the expression stack. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 986 | int expressions_start = stack_locals_count; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 987 | if (expressions_start < expressions_count) { |
| 988 | accumulator->Add(" // expression stack (top to bottom)\n"); |
| 989 | } |
| 990 | for (int i = expressions_count - 1; i >= expressions_start; i--) { |
| 991 | if (IsExpressionInsideHandler(i)) continue; |
| 992 | accumulator->Add(" [%02d] : %o\n", i, GetExpression(i)); |
| 993 | } |
| 994 | |
| 995 | // Print details about the function. |
| 996 | if (FLAG_max_stack_trace_source_length != 0 && code != NULL) { |
| 997 | SharedFunctionInfo* shared = JSFunction::cast(function)->shared(); |
| 998 | accumulator->Add("--------- s o u r c e c o d e ---------\n"); |
| 999 | shared->SourceCodePrint(accumulator, FLAG_max_stack_trace_source_length); |
| 1000 | accumulator->Add("\n-----------------------------------------\n"); |
| 1001 | } |
| 1002 | |
| 1003 | accumulator->Add("}\n\n"); |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | void ArgumentsAdaptorFrame::Print(StringStream* accumulator, |
| 1008 | PrintMode mode, |
| 1009 | int index) const { |
| 1010 | int actual = ComputeParametersCount(); |
| 1011 | int expected = -1; |
| 1012 | Object* function = this->function(); |
| 1013 | if (function->IsJSFunction()) { |
| 1014 | expected = JSFunction::cast(function)->shared()->formal_parameter_count(); |
| 1015 | } |
| 1016 | |
| 1017 | PrintIndex(accumulator, mode, index); |
| 1018 | accumulator->Add("arguments adaptor frame: %d->%d", actual, expected); |
| 1019 | if (mode == OVERVIEW) { |
| 1020 | accumulator->Add("\n"); |
| 1021 | return; |
| 1022 | } |
| 1023 | accumulator->Add(" {\n"); |
| 1024 | |
| 1025 | // Print actual arguments. |
| 1026 | if (actual > 0) accumulator->Add(" // actual arguments\n"); |
| 1027 | for (int i = 0; i < actual; i++) { |
| 1028 | accumulator->Add(" [%02d] : %o", i, GetParameter(i)); |
| 1029 | if (expected != -1 && i >= expected) { |
| 1030 | accumulator->Add(" // not passed to callee"); |
| 1031 | } |
| 1032 | accumulator->Add("\n"); |
| 1033 | } |
| 1034 | |
| 1035 | accumulator->Add("}\n\n"); |
| 1036 | } |
| 1037 | |
| 1038 | |
| 1039 | void EntryFrame::Iterate(ObjectVisitor* v) const { |
| 1040 | StackHandlerIterator it(this, top_handler()); |
| 1041 | ASSERT(!it.done()); |
| 1042 | StackHandler* handler = it.handler(); |
| 1043 | ASSERT(handler->is_entry()); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1044 | handler->Iterate(v, code()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1045 | #ifdef DEBUG |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1046 | // Make sure that the entry frame does not contain more than one |
| 1047 | // stack handler. |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 1048 | it.Advance(); |
| 1049 | ASSERT(it.done()); |
| 1050 | #endif |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1051 | IteratePc(v, pc_address(), code()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | |
| 1055 | void StandardFrame::IterateExpressions(ObjectVisitor* v) const { |
| 1056 | const int offset = StandardFrameConstants::kContextOffset; |
| 1057 | Object** base = &Memory::Object_at(sp()); |
| 1058 | Object** limit = &Memory::Object_at(fp() + offset) + 1; |
| 1059 | for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) { |
| 1060 | StackHandler* handler = it.handler(); |
| 1061 | // Traverse pointers down to - but not including - the next |
| 1062 | // handler in the handler chain. Update the base to skip the |
| 1063 | // handler and allow the handler to traverse its own pointers. |
| 1064 | const Address address = handler->address(); |
| 1065 | v->VisitPointers(base, reinterpret_cast<Object**>(address)); |
| 1066 | base = reinterpret_cast<Object**>(address + StackHandlerConstants::kSize); |
| 1067 | // Traverse the pointers in the handler itself. |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1068 | handler->Iterate(v, code()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1069 | } |
| 1070 | v->VisitPointers(base, limit); |
| 1071 | } |
| 1072 | |
| 1073 | |
| 1074 | void JavaScriptFrame::Iterate(ObjectVisitor* v) const { |
| 1075 | IterateExpressions(v); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1076 | IteratePc(v, pc_address(), code()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1077 | IterateArguments(v); |
| 1078 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1079 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1080 | |
| 1081 | void JavaScriptFrame::IterateArguments(ObjectVisitor* v) const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1082 | // Traverse callee-saved registers, receiver, and parameters. |
| 1083 | const int kBaseOffset = JavaScriptFrameConstants::kSavedRegistersOffset; |
| 1084 | const int kLimitOffset = JavaScriptFrameConstants::kReceiverOffset; |
| 1085 | Object** base = &Memory::Object_at(fp() + kBaseOffset); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 1086 | Object** limit = &Memory::Object_at(caller_sp() + kLimitOffset) + 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1087 | v->VisitPointers(base, limit); |
| 1088 | } |
| 1089 | |
| 1090 | |
| 1091 | void InternalFrame::Iterate(ObjectVisitor* v) const { |
| 1092 | // Internal frames only have object pointers on the expression stack |
| 1093 | // as they never have any arguments. |
| 1094 | IterateExpressions(v); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1095 | IteratePc(v, pc_address(), code()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | |
| 1099 | // ------------------------------------------------------------------------- |
| 1100 | |
| 1101 | |
| 1102 | JavaScriptFrame* StackFrameLocator::FindJavaScriptFrame(int n) { |
| 1103 | ASSERT(n >= 0); |
| 1104 | for (int i = 0; i <= n; i++) { |
| 1105 | while (!iterator_.frame()->is_java_script()) iterator_.Advance(); |
| 1106 | if (i == n) return JavaScriptFrame::cast(iterator_.frame()); |
| 1107 | iterator_.Advance(); |
| 1108 | } |
| 1109 | UNREACHABLE(); |
| 1110 | return NULL; |
| 1111 | } |
| 1112 | |
| 1113 | |
| 1114 | // ------------------------------------------------------------------------- |
| 1115 | |
| 1116 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1117 | Code* PcToCodeCache::GcSafeCastToCode(HeapObject* object, Address pc) { |
| 1118 | Code* code = reinterpret_cast<Code*>(object); |
| 1119 | ASSERT(code != NULL && code->contains(pc)); |
| 1120 | return code; |
| 1121 | } |
| 1122 | |
| 1123 | |
| 1124 | Code* PcToCodeCache::GcSafeFindCodeForPc(Address pc) { |
| 1125 | // Check if the pc points into a large object chunk. |
| 1126 | LargeObjectChunk* chunk = Heap::lo_space()->FindChunkContainingPc(pc); |
| 1127 | if (chunk != NULL) return GcSafeCastToCode(chunk->GetObject(), pc); |
| 1128 | |
| 1129 | // Iterate through the 8K page until we reach the end or find an |
| 1130 | // object starting after the pc. |
| 1131 | Page* page = Page::FromAddress(pc); |
| 1132 | HeapObjectIterator iterator(page, Heap::GcSafeSizeOfOldObjectFunction()); |
| 1133 | HeapObject* previous = NULL; |
| 1134 | while (true) { |
| 1135 | HeapObject* next = iterator.next(); |
| 1136 | if (next == NULL || next->address() >= pc) { |
| 1137 | return GcSafeCastToCode(previous, pc); |
| 1138 | } |
| 1139 | previous = next; |
| 1140 | } |
| 1141 | } |
| 1142 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1143 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1144 | PcToCodeCache::PcToCodeCacheEntry* PcToCodeCache::GetCacheEntry(Address pc) { |
| 1145 | Counters::pc_to_code.Increment(); |
| 1146 | ASSERT(IsPowerOf2(kPcToCodeCacheSize)); |
| 1147 | uint32_t hash = ComputeIntegerHash( |
| 1148 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(pc))); |
| 1149 | uint32_t index = hash & (kPcToCodeCacheSize - 1); |
| 1150 | PcToCodeCacheEntry* entry = cache(index); |
| 1151 | if (entry->pc == pc) { |
| 1152 | Counters::pc_to_code_cached.Increment(); |
| 1153 | ASSERT(entry->code == GcSafeFindCodeForPc(pc)); |
| 1154 | } else { |
| 1155 | // Because this code may be interrupted by a profiling signal that |
| 1156 | // also queries the cache, we cannot update pc before the code has |
| 1157 | // been set. Otherwise, we risk trying to use a cache entry before |
| 1158 | // the code has been computed. |
| 1159 | entry->code = GcSafeFindCodeForPc(pc); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1160 | entry->safepoint_entry.Reset(); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1161 | entry->pc = pc; |
| 1162 | } |
| 1163 | return entry; |
| 1164 | } |
| 1165 | |
| 1166 | |
| 1167 | // ------------------------------------------------------------------------- |
| 1168 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1169 | int NumRegs(RegList reglist) { |
| 1170 | int n = 0; |
| 1171 | while (reglist != 0) { |
| 1172 | n++; |
| 1173 | reglist &= reglist - 1; // clear one bit |
| 1174 | } |
| 1175 | return n; |
| 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | int JSCallerSavedCode(int n) { |
| 1180 | static int reg_code[kNumJSCallerSaved]; |
| 1181 | static bool initialized = false; |
| 1182 | if (!initialized) { |
| 1183 | initialized = true; |
| 1184 | int i = 0; |
| 1185 | for (int r = 0; r < kNumRegs; r++) |
| 1186 | if ((kJSCallerSaved & (1 << r)) != 0) |
| 1187 | reg_code[i++] = r; |
| 1188 | |
| 1189 | ASSERT(i == kNumJSCallerSaved); |
| 1190 | } |
| 1191 | ASSERT(0 <= n && n < kNumJSCallerSaved); |
| 1192 | return reg_code[n]; |
| 1193 | } |
| 1194 | |
| 1195 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1196 | #define DEFINE_WRAPPER(type, field) \ |
| 1197 | class field##_Wrapper : public ZoneObject { \ |
| 1198 | public: /* NOLINT */ \ |
| 1199 | field##_Wrapper(const field& original) : frame_(original) { \ |
| 1200 | } \ |
| 1201 | field frame_; \ |
| 1202 | }; |
| 1203 | STACK_FRAME_TYPE_LIST(DEFINE_WRAPPER) |
| 1204 | #undef DEFINE_WRAPPER |
| 1205 | |
| 1206 | static StackFrame* AllocateFrameCopy(StackFrame* frame) { |
| 1207 | #define FRAME_TYPE_CASE(type, field) \ |
| 1208 | case StackFrame::type: { \ |
| 1209 | field##_Wrapper* wrapper = \ |
| 1210 | new field##_Wrapper(*(reinterpret_cast<field*>(frame))); \ |
| 1211 | return &wrapper->frame_; \ |
| 1212 | } |
| 1213 | |
| 1214 | switch (frame->type()) { |
| 1215 | STACK_FRAME_TYPE_LIST(FRAME_TYPE_CASE) |
| 1216 | default: UNREACHABLE(); |
| 1217 | } |
| 1218 | #undef FRAME_TYPE_CASE |
| 1219 | return NULL; |
| 1220 | } |
| 1221 | |
| 1222 | Vector<StackFrame*> CreateStackMap() { |
| 1223 | ZoneList<StackFrame*> list(10); |
| 1224 | for (StackFrameIterator it; !it.done(); it.Advance()) { |
| 1225 | StackFrame* frame = AllocateFrameCopy(it.frame()); |
| 1226 | list.Add(frame); |
| 1227 | } |
| 1228 | return list.ToVector(); |
| 1229 | } |
| 1230 | |
| 1231 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1232 | } } // namespace v8::internal |