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