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