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