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 | |
| 30 | #include "frames-inl.h" |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 31 | #include "mark-compact.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 32 | #include "scopeinfo.h" |
| 33 | #include "string-stream.h" |
| 34 | #include "top.h" |
| 35 | #include "zone-inl.h" |
| 36 | |
| 37 | namespace v8 { namespace internal { |
| 38 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | // Iterator that supports traversing the stack handlers of a |
| 40 | // particular frame. Needs to know the top of the handler chain. |
| 41 | class StackHandlerIterator BASE_EMBEDDED { |
| 42 | public: |
| 43 | StackHandlerIterator(const StackFrame* frame, StackHandler* handler) |
| 44 | : limit_(frame->fp()), handler_(handler) { |
| 45 | // Make sure the handler has already been unwound to this frame. |
| 46 | ASSERT(frame->sp() <= handler->address()); |
| 47 | } |
| 48 | |
| 49 | StackHandler* handler() const { return handler_; } |
| 50 | |
| 51 | bool done() { return handler_->address() > limit_; } |
| 52 | void Advance() { |
| 53 | ASSERT(!done()); |
| 54 | handler_ = handler_->next(); |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | const Address limit_; |
| 59 | StackHandler* handler_; |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | // ------------------------------------------------------------------------- |
| 64 | |
| 65 | |
| 66 | #define INITIALIZE_SINGLETON(type, field) field##_(this), |
| 67 | StackFrameIterator::StackFrameIterator() |
| 68 | : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON) |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 69 | frame_(NULL), handler_(NULL), thread_(Top::GetCurrentThread()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 70 | Reset(); |
| 71 | } |
| 72 | StackFrameIterator::StackFrameIterator(ThreadLocalTop* t) |
| 73 | : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON) |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 74 | frame_(NULL), handler_(NULL), thread_(t) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 75 | Reset(); |
| 76 | } |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 77 | StackFrameIterator::StackFrameIterator(bool reset) |
| 78 | : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON) |
| 79 | frame_(NULL), handler_(NULL), thread_(Top::GetCurrentThread()) { |
| 80 | if (reset) Reset(); |
| 81 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 82 | #undef INITIALIZE_SINGLETON |
| 83 | |
| 84 | |
| 85 | void StackFrameIterator::Advance() { |
| 86 | ASSERT(!done()); |
| 87 | // Compute the state of the calling frame before restoring |
| 88 | // callee-saved registers and unwinding handlers. This allows the |
| 89 | // frame code that computes the caller state to access the top |
| 90 | // handler and the value of any callee-saved register if needed. |
| 91 | StackFrame::State state; |
| 92 | StackFrame::Type type = frame_->GetCallerState(&state); |
| 93 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 94 | // Unwind handlers corresponding to the current frame. |
| 95 | StackHandlerIterator it(frame_, handler_); |
| 96 | while (!it.done()) it.Advance(); |
| 97 | handler_ = it.handler(); |
| 98 | |
| 99 | // Advance to the calling frame. |
| 100 | frame_ = SingletonFor(type, &state); |
| 101 | |
| 102 | // When we're done iterating over the stack frames, the handler |
| 103 | // chain must have been completely unwound. |
| 104 | ASSERT(!done() || handler_ == NULL); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void StackFrameIterator::Reset() { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 109 | Address fp = Top::c_entry_fp(thread_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 110 | StackFrame::State state; |
| 111 | StackFrame::Type type = ExitFrame::GetStateForFramePointer(fp, &state); |
| 112 | frame_ = SingletonFor(type, &state); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 113 | handler_ = StackHandler::FromAddress(Top::handler(thread_)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
| 117 | StackFrame* StackFrameIterator::SingletonFor(StackFrame::Type type, |
| 118 | StackFrame::State* state) { |
| 119 | #define FRAME_TYPE_CASE(type, field) \ |
| 120 | case StackFrame::type: result = &field##_; break; |
| 121 | |
| 122 | StackFrame* result = NULL; |
| 123 | switch (type) { |
| 124 | case StackFrame::NONE: return NULL; |
| 125 | STACK_FRAME_TYPE_LIST(FRAME_TYPE_CASE) |
| 126 | default: break; |
| 127 | } |
| 128 | ASSERT(result != NULL); |
| 129 | result->state_ = *state; |
| 130 | return result; |
| 131 | |
| 132 | #undef FRAME_TYPE_CASE |
| 133 | } |
| 134 | |
| 135 | |
| 136 | // ------------------------------------------------------------------------- |
| 137 | |
| 138 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 139 | StackTraceFrameIterator::StackTraceFrameIterator() { |
| 140 | if (!done() && !frame()->function()->IsJSFunction()) Advance(); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | void StackTraceFrameIterator::Advance() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | while (true) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 146 | JavaScriptFrameIterator::Advance(); |
| 147 | if (done()) return; |
| 148 | if (frame()->function()->IsJSFunction()) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 153 | // ------------------------------------------------------------------------- |
| 154 | |
| 155 | |
| 156 | SafeStackFrameIterator::SafeStackFrameIterator( |
| 157 | Address low_bound, Address high_bound) : |
| 158 | low_bound_(low_bound), high_bound_(high_bound), |
| 159 | is_working_iterator_(IsInBounds(low_bound, high_bound, |
| 160 | Top::c_entry_fp(Top::GetCurrentThread()))), |
| 161 | iteration_done_(!is_working_iterator_), iterator_(is_working_iterator_) { |
| 162 | } |
| 163 | |
| 164 | |
| 165 | void SafeStackFrameIterator::Advance() { |
| 166 | ASSERT(is_working_iterator_); |
| 167 | ASSERT(!done()); |
| 168 | StackFrame* frame = iterator_.frame(); |
| 169 | iteration_done_ = |
| 170 | !IsGoodStackAddress(frame->sp()) || !IsGoodStackAddress(frame->fp()); |
| 171 | if (!iteration_done_) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 172 | iterator_.Advance(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 173 | if (!iterator_.done()) { |
| 174 | // Check that we have actually moved to the previous frame in the stack |
| 175 | StackFrame* prev_frame = iterator_.frame(); |
| 176 | iteration_done_ = |
| 177 | prev_frame->sp() < frame->sp() || prev_frame->fp() < frame->fp(); |
| 178 | } |
| 179 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 183 | void SafeStackFrameIterator::Reset() { |
| 184 | if (is_working_iterator_) { |
| 185 | iterator_.Reset(); |
| 186 | iteration_done_ = false; |
| 187 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 191 | // ------------------------------------------------------------------------- |
| 192 | |
| 193 | |
| 194 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 195 | SafeStackTraceFrameIterator::SafeStackTraceFrameIterator( |
| 196 | Address low_bound, Address high_bound) : |
| 197 | SafeJavaScriptFrameIterator(low_bound, high_bound) { |
| 198 | if (!done() && !frame()->function()->IsJSFunction()) Advance(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame^] | 202 | void SafeStackTraceFrameIterator::Advance() { |
| 203 | while (true) { |
| 204 | SafeJavaScriptFrameIterator::Advance(); |
| 205 | if (done()) return; |
| 206 | if (frame()->function()->IsJSFunction()) return; |
| 207 | } |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 212 | // ------------------------------------------------------------------------- |
| 213 | |
| 214 | |
| 215 | void StackHandler::Cook(Code* code) { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 216 | ASSERT(MarkCompactCollector::IsCompacting()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 217 | ASSERT(code->contains(pc())); |
| 218 | set_pc(AddressFrom<Address>(pc() - code->instruction_start())); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | void StackHandler::Uncook(Code* code) { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 223 | ASSERT(MarkCompactCollector::IsCompacting()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 224 | set_pc(code->instruction_start() + OffsetFrom(pc())); |
| 225 | ASSERT(code->contains(pc())); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | // ------------------------------------------------------------------------- |
| 230 | |
| 231 | |
| 232 | bool StackFrame::HasHandler() const { |
| 233 | StackHandlerIterator it(this, top_handler()); |
| 234 | return !it.done(); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | void StackFrame::CookFramesForThread(ThreadLocalTop* thread) { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 239 | // Only cooking frames when the collector is compacting and thus moving code |
| 240 | // around. |
| 241 | ASSERT(MarkCompactCollector::IsCompacting()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 242 | ASSERT(!thread->stack_is_cooked()); |
| 243 | for (StackFrameIterator it(thread); !it.done(); it.Advance()) { |
| 244 | it.frame()->Cook(); |
| 245 | } |
| 246 | thread->set_stack_is_cooked(true); |
| 247 | } |
| 248 | |
| 249 | |
| 250 | void StackFrame::UncookFramesForThread(ThreadLocalTop* thread) { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 251 | // Only uncooking frames when the collector is compacting and thus moving code |
| 252 | // around. |
| 253 | ASSERT(MarkCompactCollector::IsCompacting()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 254 | ASSERT(thread->stack_is_cooked()); |
| 255 | for (StackFrameIterator it(thread); !it.done(); it.Advance()) { |
| 256 | it.frame()->Uncook(); |
| 257 | } |
| 258 | thread->set_stack_is_cooked(false); |
| 259 | } |
| 260 | |
| 261 | |
| 262 | void StackFrame::Cook() { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 263 | Code* code = this->code(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 264 | for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) { |
| 265 | it.handler()->Cook(code); |
| 266 | } |
| 267 | ASSERT(code->contains(pc())); |
| 268 | set_pc(AddressFrom<Address>(pc() - code->instruction_start())); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | void StackFrame::Uncook() { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 273 | Code* code = this->code(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 274 | for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) { |
| 275 | it.handler()->Uncook(code); |
| 276 | } |
| 277 | set_pc(code->instruction_start() + OffsetFrom(pc())); |
| 278 | ASSERT(code->contains(pc())); |
| 279 | } |
| 280 | |
| 281 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 282 | Code* EntryFrame::code() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 283 | return Heap::js_entry_code(); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | StackFrame::Type EntryFrame::GetCallerState(State* state) const { |
| 288 | const int offset = EntryFrameConstants::kCallerFPOffset; |
| 289 | Address fp = Memory::Address_at(this->fp() + offset); |
| 290 | return ExitFrame::GetStateForFramePointer(fp, state); |
| 291 | } |
| 292 | |
| 293 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 294 | Code* EntryConstructFrame::code() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 295 | return Heap::js_construct_entry_code(); |
| 296 | } |
| 297 | |
| 298 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 299 | Code* ExitFrame::code() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 300 | return Heap::c_entry_code(); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | StackFrame::Type ExitFrame::GetCallerState(State* state) const { |
| 305 | // Setup the caller state. |
| 306 | state->sp = pp(); |
| 307 | state->fp = Memory::Address_at(fp() + ExitFrameConstants::kCallerFPOffset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 308 | state->pc_address |
| 309 | = reinterpret_cast<Address*>(fp() + ExitFrameConstants::kCallerPCOffset); |
| 310 | return ComputeType(state); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | Address ExitFrame::GetCallerStackPointer() const { |
| 315 | return fp() + ExitFrameConstants::kPPDisplacement; |
| 316 | } |
| 317 | |
| 318 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 319 | Code* ExitDebugFrame::code() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 320 | return Heap::c_entry_debug_break_code(); |
| 321 | } |
| 322 | |
| 323 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 324 | Address StandardFrame::GetExpressionAddress(int n) const { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 325 | const int offset = StandardFrameConstants::kExpressionsOffset; |
| 326 | return fp() + offset - n * kPointerSize; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | |
| 330 | int StandardFrame::ComputeExpressionsCount() const { |
| 331 | const int offset = |
| 332 | StandardFrameConstants::kExpressionsOffset + kPointerSize; |
| 333 | Address base = fp() + offset; |
| 334 | Address limit = sp(); |
| 335 | ASSERT(base >= limit); // stack grows downwards |
| 336 | // Include register-allocated locals in number of expressions. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 337 | return (base - limit) / kPointerSize; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | |
| 341 | StackFrame::Type StandardFrame::GetCallerState(State* state) const { |
| 342 | state->sp = caller_sp(); |
| 343 | state->fp = caller_fp(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 344 | state->pc_address = reinterpret_cast<Address*>(ComputePCAddress(fp())); |
| 345 | return ComputeType(state); |
| 346 | } |
| 347 | |
| 348 | |
| 349 | bool StandardFrame::IsExpressionInsideHandler(int n) const { |
| 350 | Address address = GetExpressionAddress(n); |
| 351 | for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) { |
| 352 | if (it.handler()->includes(address)) return true; |
| 353 | } |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | |
| 358 | Object* JavaScriptFrame::GetParameter(int index) const { |
| 359 | ASSERT(index >= 0 && index < ComputeParametersCount()); |
| 360 | const int offset = JavaScriptFrameConstants::kParam0Offset; |
| 361 | return Memory::Object_at(pp() + offset - (index * kPointerSize)); |
| 362 | } |
| 363 | |
| 364 | |
| 365 | int JavaScriptFrame::ComputeParametersCount() const { |
| 366 | Address base = pp() + JavaScriptFrameConstants::kReceiverOffset; |
| 367 | Address limit = fp() + JavaScriptFrameConstants::kSavedRegistersOffset; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 368 | return (base - limit) / kPointerSize; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | |
| 372 | bool JavaScriptFrame::IsConstructor() const { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 373 | Address fp = caller_fp(); |
| 374 | if (has_adapted_arguments()) { |
| 375 | // Skip the arguments adaptor frame and look at the real caller. |
| 376 | fp = Memory::Address_at(fp + StandardFrameConstants::kCallerFPOffset); |
| 377 | } |
| 378 | return IsConstructFrame(fp); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 382 | Code* JavaScriptFrame::code() const { |
| 383 | JSFunction* function = JSFunction::cast(this->function()); |
| 384 | return function->shared()->code(); |
| 385 | } |
| 386 | |
| 387 | |
| 388 | Code* ArgumentsAdaptorFrame::code() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 389 | return Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline); |
| 390 | } |
| 391 | |
| 392 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 393 | Code* InternalFrame::code() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 394 | const int offset = InternalFrameConstants::kCodeOffset; |
| 395 | Object* code = Memory::Object_at(fp() + offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 396 | ASSERT(code != NULL); |
| 397 | return Code::cast(code); |
| 398 | } |
| 399 | |
| 400 | |
| 401 | void StackFrame::PrintIndex(StringStream* accumulator, |
| 402 | PrintMode mode, |
| 403 | int index) { |
| 404 | accumulator->Add((mode == OVERVIEW) ? "%5d: " : "[%d]: ", index); |
| 405 | } |
| 406 | |
| 407 | |
| 408 | void JavaScriptFrame::Print(StringStream* accumulator, |
| 409 | PrintMode mode, |
| 410 | int index) const { |
| 411 | HandleScope scope; |
| 412 | Object* receiver = this->receiver(); |
| 413 | Object* function = this->function(); |
| 414 | |
| 415 | accumulator->PrintSecurityTokenIfChanged(function); |
| 416 | PrintIndex(accumulator, mode, index); |
| 417 | Code* code = NULL; |
| 418 | if (IsConstructor()) accumulator->Add("new "); |
| 419 | accumulator->PrintFunction(function, receiver, &code); |
| 420 | accumulator->Add("(this=%o", receiver); |
| 421 | |
| 422 | // Get scope information for nicer output, if possible. If code is |
| 423 | // NULL, or doesn't contain scope info, info will return 0 for the |
| 424 | // number of parameters, stack slots, or context slots. |
| 425 | ScopeInfo<PreallocatedStorage> info(code); |
| 426 | |
| 427 | // Print the parameters. |
| 428 | int parameters_count = ComputeParametersCount(); |
| 429 | for (int i = 0; i < parameters_count; i++) { |
| 430 | accumulator->Add(","); |
| 431 | // If we have a name for the parameter we print it. Nameless |
| 432 | // parameters are either because we have more actual parameters |
| 433 | // than formal parameters or because we have no scope information. |
| 434 | if (i < info.number_of_parameters()) { |
| 435 | accumulator->PrintName(*info.parameter_name(i)); |
| 436 | accumulator->Add("="); |
| 437 | } |
| 438 | accumulator->Add("%o", GetParameter(i)); |
| 439 | } |
| 440 | |
| 441 | accumulator->Add(")"); |
| 442 | if (mode == OVERVIEW) { |
| 443 | accumulator->Add("\n"); |
| 444 | return; |
| 445 | } |
| 446 | accumulator->Add(" {\n"); |
| 447 | |
| 448 | // Compute the number of locals and expression stack elements. |
| 449 | int stack_locals_count = info.number_of_stack_slots(); |
| 450 | int heap_locals_count = info.number_of_context_slots(); |
| 451 | int expressions_count = ComputeExpressionsCount(); |
| 452 | |
| 453 | // Print stack-allocated local variables. |
| 454 | if (stack_locals_count > 0) { |
| 455 | accumulator->Add(" // stack-allocated locals\n"); |
| 456 | } |
| 457 | for (int i = 0; i < stack_locals_count; i++) { |
| 458 | accumulator->Add(" var "); |
| 459 | accumulator->PrintName(*info.stack_slot_name(i)); |
| 460 | accumulator->Add(" = "); |
| 461 | if (i < expressions_count) { |
| 462 | accumulator->Add("%o", GetExpression(i)); |
| 463 | } else { |
| 464 | accumulator->Add("// no expression found - inconsistent frame?"); |
| 465 | } |
| 466 | accumulator->Add("\n"); |
| 467 | } |
| 468 | |
| 469 | // Try to get hold of the context of this frame. |
| 470 | Context* context = NULL; |
| 471 | if (this->context() != NULL && this->context()->IsContext()) { |
| 472 | context = Context::cast(this->context()); |
| 473 | } |
| 474 | |
| 475 | // Print heap-allocated local variables. |
| 476 | if (heap_locals_count > Context::MIN_CONTEXT_SLOTS) { |
| 477 | accumulator->Add(" // heap-allocated locals\n"); |
| 478 | } |
| 479 | for (int i = Context::MIN_CONTEXT_SLOTS; i < heap_locals_count; i++) { |
| 480 | accumulator->Add(" var "); |
| 481 | accumulator->PrintName(*info.context_slot_name(i)); |
| 482 | accumulator->Add(" = "); |
| 483 | if (context != NULL) { |
| 484 | if (i < context->length()) { |
| 485 | accumulator->Add("%o", context->get(i)); |
| 486 | } else { |
| 487 | accumulator->Add( |
| 488 | "// warning: missing context slot - inconsistent frame?"); |
| 489 | } |
| 490 | } else { |
| 491 | accumulator->Add("// warning: no context found - inconsistent frame?"); |
| 492 | } |
| 493 | accumulator->Add("\n"); |
| 494 | } |
| 495 | |
| 496 | // Print the expression stack. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 497 | int expressions_start = stack_locals_count; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 498 | if (expressions_start < expressions_count) { |
| 499 | accumulator->Add(" // expression stack (top to bottom)\n"); |
| 500 | } |
| 501 | for (int i = expressions_count - 1; i >= expressions_start; i--) { |
| 502 | if (IsExpressionInsideHandler(i)) continue; |
| 503 | accumulator->Add(" [%02d] : %o\n", i, GetExpression(i)); |
| 504 | } |
| 505 | |
| 506 | // Print details about the function. |
| 507 | if (FLAG_max_stack_trace_source_length != 0 && code != NULL) { |
| 508 | SharedFunctionInfo* shared = JSFunction::cast(function)->shared(); |
| 509 | accumulator->Add("--------- s o u r c e c o d e ---------\n"); |
| 510 | shared->SourceCodePrint(accumulator, FLAG_max_stack_trace_source_length); |
| 511 | accumulator->Add("\n-----------------------------------------\n"); |
| 512 | } |
| 513 | |
| 514 | accumulator->Add("}\n\n"); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | void ArgumentsAdaptorFrame::Print(StringStream* accumulator, |
| 519 | PrintMode mode, |
| 520 | int index) const { |
| 521 | int actual = ComputeParametersCount(); |
| 522 | int expected = -1; |
| 523 | Object* function = this->function(); |
| 524 | if (function->IsJSFunction()) { |
| 525 | expected = JSFunction::cast(function)->shared()->formal_parameter_count(); |
| 526 | } |
| 527 | |
| 528 | PrintIndex(accumulator, mode, index); |
| 529 | accumulator->Add("arguments adaptor frame: %d->%d", actual, expected); |
| 530 | if (mode == OVERVIEW) { |
| 531 | accumulator->Add("\n"); |
| 532 | return; |
| 533 | } |
| 534 | accumulator->Add(" {\n"); |
| 535 | |
| 536 | // Print actual arguments. |
| 537 | if (actual > 0) accumulator->Add(" // actual arguments\n"); |
| 538 | for (int i = 0; i < actual; i++) { |
| 539 | accumulator->Add(" [%02d] : %o", i, GetParameter(i)); |
| 540 | if (expected != -1 && i >= expected) { |
| 541 | accumulator->Add(" // not passed to callee"); |
| 542 | } |
| 543 | accumulator->Add("\n"); |
| 544 | } |
| 545 | |
| 546 | accumulator->Add("}\n\n"); |
| 547 | } |
| 548 | |
| 549 | |
| 550 | void EntryFrame::Iterate(ObjectVisitor* v) const { |
| 551 | StackHandlerIterator it(this, top_handler()); |
| 552 | ASSERT(!it.done()); |
| 553 | StackHandler* handler = it.handler(); |
| 554 | ASSERT(handler->is_entry()); |
| 555 | handler->Iterate(v); |
| 556 | // Make sure that there's the entry frame does not contain more than |
| 557 | // one stack handler. |
| 558 | if (kDebug) { |
| 559 | it.Advance(); |
| 560 | ASSERT(it.done()); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | |
| 565 | void StandardFrame::IterateExpressions(ObjectVisitor* v) const { |
| 566 | const int offset = StandardFrameConstants::kContextOffset; |
| 567 | Object** base = &Memory::Object_at(sp()); |
| 568 | Object** limit = &Memory::Object_at(fp() + offset) + 1; |
| 569 | for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) { |
| 570 | StackHandler* handler = it.handler(); |
| 571 | // Traverse pointers down to - but not including - the next |
| 572 | // handler in the handler chain. Update the base to skip the |
| 573 | // handler and allow the handler to traverse its own pointers. |
| 574 | const Address address = handler->address(); |
| 575 | v->VisitPointers(base, reinterpret_cast<Object**>(address)); |
| 576 | base = reinterpret_cast<Object**>(address + StackHandlerConstants::kSize); |
| 577 | // Traverse the pointers in the handler itself. |
| 578 | handler->Iterate(v); |
| 579 | } |
| 580 | v->VisitPointers(base, limit); |
| 581 | } |
| 582 | |
| 583 | |
| 584 | void JavaScriptFrame::Iterate(ObjectVisitor* v) const { |
| 585 | IterateExpressions(v); |
| 586 | |
| 587 | // Traverse callee-saved registers, receiver, and parameters. |
| 588 | const int kBaseOffset = JavaScriptFrameConstants::kSavedRegistersOffset; |
| 589 | const int kLimitOffset = JavaScriptFrameConstants::kReceiverOffset; |
| 590 | Object** base = &Memory::Object_at(fp() + kBaseOffset); |
| 591 | Object** limit = &Memory::Object_at(pp() + kLimitOffset) + 1; |
| 592 | v->VisitPointers(base, limit); |
| 593 | } |
| 594 | |
| 595 | |
| 596 | void InternalFrame::Iterate(ObjectVisitor* v) const { |
| 597 | // Internal frames only have object pointers on the expression stack |
| 598 | // as they never have any arguments. |
| 599 | IterateExpressions(v); |
| 600 | } |
| 601 | |
| 602 | |
| 603 | // ------------------------------------------------------------------------- |
| 604 | |
| 605 | |
| 606 | JavaScriptFrame* StackFrameLocator::FindJavaScriptFrame(int n) { |
| 607 | ASSERT(n >= 0); |
| 608 | for (int i = 0; i <= n; i++) { |
| 609 | while (!iterator_.frame()->is_java_script()) iterator_.Advance(); |
| 610 | if (i == n) return JavaScriptFrame::cast(iterator_.frame()); |
| 611 | iterator_.Advance(); |
| 612 | } |
| 613 | UNREACHABLE(); |
| 614 | return NULL; |
| 615 | } |
| 616 | |
| 617 | |
| 618 | // ------------------------------------------------------------------------- |
| 619 | |
| 620 | |
| 621 | int NumRegs(RegList reglist) { |
| 622 | int n = 0; |
| 623 | while (reglist != 0) { |
| 624 | n++; |
| 625 | reglist &= reglist - 1; // clear one bit |
| 626 | } |
| 627 | return n; |
| 628 | } |
| 629 | |
| 630 | |
| 631 | int JSCallerSavedCode(int n) { |
| 632 | static int reg_code[kNumJSCallerSaved]; |
| 633 | static bool initialized = false; |
| 634 | if (!initialized) { |
| 635 | initialized = true; |
| 636 | int i = 0; |
| 637 | for (int r = 0; r < kNumRegs; r++) |
| 638 | if ((kJSCallerSaved & (1 << r)) != 0) |
| 639 | reg_code[i++] = r; |
| 640 | |
| 641 | ASSERT(i == kNumJSCallerSaved); |
| 642 | } |
| 643 | ASSERT(0 <= n && n < kNumJSCallerSaved); |
| 644 | return reg_code[n]; |
| 645 | } |
| 646 | |
| 647 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 648 | } } // namespace v8::internal |