• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
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"
31 #include "mark-compact.h"
32 #include "scopeinfo.h"
33 #include "string-stream.h"
34 #include "top.h"
35 #include "zone-inl.h"
36 
37 namespace v8 {
38 namespace internal {
39 
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:
StackHandlerIterator(const StackFrame * frame,StackHandler * handler)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 
handler() const50   StackHandler* handler() const { return handler_; }
51 
done()52   bool done() {
53     return handler_ == NULL || handler_->address() > limit_;
54   }
Advance()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),
StackFrameIterator()70 StackFrameIterator::StackFrameIterator()
71     : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
72       frame_(NULL), handler_(NULL), thread_(Top::GetCurrentThread()),
73       fp_(NULL), sp_(NULL), advance_(&StackFrameIterator::AdvanceWithHandler) {
74   Reset();
75 }
StackFrameIterator(ThreadLocalTop * t)76 StackFrameIterator::StackFrameIterator(ThreadLocalTop* t)
77     : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
78       frame_(NULL), handler_(NULL), thread_(t),
79       fp_(NULL), sp_(NULL), advance_(&StackFrameIterator::AdvanceWithHandler) {
80   Reset();
81 }
StackFrameIterator(bool use_top,Address fp,Address sp)82 StackFrameIterator::StackFrameIterator(bool use_top, Address fp, Address sp)
83     : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
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   }
92   JavaScriptFrame_.DisableHeapAccess();
93 }
94 
95 #undef INITIALIZE_SINGLETON
96 
97 
AdvanceWithHandler()98 void StackFrameIterator::AdvanceWithHandler() {
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 
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 
AdvanceWithoutHandler()121 void StackFrameIterator::AdvanceWithoutHandler() {
122   // A simpler version of Advance which doesn't care about handler.
123   ASSERT(!done());
124   StackFrame::State state;
125   StackFrame::Type type = frame_->GetCallerState(&state);
126   frame_ = SingletonFor(type, &state);
127 }
128 
129 
Reset()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);
146 }
147 
148 
SingletonFor(StackFrame::Type type,StackFrame::State * state)149 StackFrame* StackFrameIterator::SingletonFor(StackFrame::Type type,
150                                              StackFrame::State* state) {
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 
SingletonFor(StackFrame::Type type)159 StackFrame* StackFrameIterator::SingletonFor(StackFrame::Type type) {
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   }
169   return result;
170 
171 #undef FRAME_TYPE_CASE
172 }
173 
174 
175 // -------------------------------------------------------------------------
176 
177 
StackTraceFrameIterator()178 StackTraceFrameIterator::StackTraceFrameIterator() {
179   if (!done() && !frame()->function()->IsJSFunction()) Advance();
180 }
181 
182 
Advance()183 void StackTraceFrameIterator::Advance() {
184   while (true) {
185     JavaScriptFrameIterator::Advance();
186     if (done()) return;
187     if (frame()->function()->IsJSFunction()) return;
188   }
189 }
190 
191 
192 // -------------------------------------------------------------------------
193 
194 
SafeStackFrameIterator(Address fp,Address sp,Address low_bound,Address high_bound)195 SafeStackFrameIterator::SafeStackFrameIterator(
196     Address fp, Address sp, Address low_bound, Address high_bound) :
197     low_bound_(low_bound), high_bound_(high_bound),
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) {
206 }
207 
208 
Advance()209 void SafeStackFrameIterator::Advance() {
210   ASSERT(is_working_iterator_);
211   ASSERT(!done());
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
215   iteration_done_ = !IsValidFrame(last_frame) ||
216       !CanIterateHandles(last_frame, iterator_.handler()) ||
217       !IsValidCaller(last_frame);
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 
CanIterateHandles(StackFrame * frame,StackHandler * handler)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 
IsValidFrame(StackFrame * frame) const237 bool SafeStackFrameIterator::IsValidFrame(StackFrame* frame) const {
238   return IsValidStackAddress(frame->sp()) && IsValidStackAddress(frame->fp());
239 }
240 
241 
IsValidCaller(StackFrame * frame)242 bool SafeStackFrameIterator::IsValidCaller(StackFrame* frame) {
243   StackFrame::State state;
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   }
263   frame->ComputeCallerState(&state);
264   return IsValidStackAddress(state.sp) && IsValidStackAddress(state.fp) &&
265       iterator_.SingletonFor(frame->GetCallerState(&state)) != NULL;
266 }
267 
268 
Reset()269 void SafeStackFrameIterator::Reset() {
270   if (is_working_iterator_) {
271     iterator_.Reset();
272     iteration_done_ = false;
273   }
274 }
275 
276 
277 // -------------------------------------------------------------------------
278 
279 
280 #ifdef ENABLE_LOGGING_AND_PROFILING
SafeStackTraceFrameIterator(Address fp,Address sp,Address low_bound,Address high_bound)281 SafeStackTraceFrameIterator::SafeStackTraceFrameIterator(
282     Address fp, Address sp, Address low_bound, Address high_bound) :
283     SafeJavaScriptFrameIterator(fp, sp, low_bound, high_bound) {
284   if (!done() && !frame()->is_java_script()) Advance();
285 }
286 
287 
Advance()288 void SafeStackTraceFrameIterator::Advance() {
289   while (true) {
290     SafeJavaScriptFrameIterator::Advance();
291     if (done()) return;
292     if (frame()->is_java_script()) return;
293   }
294 }
295 #endif
296 
297 
298 // -------------------------------------------------------------------------
299 
300 
Cook(Code * code)301 void StackHandler::Cook(Code* code) {
302   ASSERT(MarkCompactCollector::IsCompacting());
303   ASSERT(code->contains(pc()));
304   set_pc(AddressFrom<Address>(pc() - code->instruction_start()));
305 }
306 
307 
Uncook(Code * code)308 void StackHandler::Uncook(Code* code) {
309   ASSERT(MarkCompactCollector::IsCompacting());
310   set_pc(code->instruction_start() + OffsetFrom(pc()));
311   ASSERT(code->contains(pc()));
312 }
313 
314 
315 // -------------------------------------------------------------------------
316 
317 
HasHandler() const318 bool StackFrame::HasHandler() const {
319   StackHandlerIterator it(this, top_handler());
320   return !it.done();
321 }
322 
323 
CookFramesForThread(ThreadLocalTop * thread)324 void StackFrame::CookFramesForThread(ThreadLocalTop* thread) {
325   // Only cooking frames when the collector is compacting and thus moving code
326   // around.
327   ASSERT(MarkCompactCollector::IsCompacting());
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 
UncookFramesForThread(ThreadLocalTop * thread)336 void StackFrame::UncookFramesForThread(ThreadLocalTop* thread) {
337   // Only uncooking frames when the collector is compacting and thus moving code
338   // around.
339   ASSERT(MarkCompactCollector::IsCompacting());
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 
Cook()348 void StackFrame::Cook() {
349   Code* code = this->code();
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 
Uncook()358 void StackFrame::Uncook() {
359   Code* code = this->code();
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 
GetCallerState(State * state) const368 StackFrame::Type StackFrame::GetCallerState(State* state) const {
369   ComputeCallerState(state);
370   return ComputeType(state);
371 }
372 
373 
code() const374 Code* EntryFrame::code() const {
375   return Heap::js_entry_code();
376 }
377 
378 
ComputeCallerState(State * state) const379 void EntryFrame::ComputeCallerState(State* state) const {
380   GetCallerState(state);
381 }
382 
383 
GetCallerState(State * state) const384 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 
code() const391 Code* EntryConstructFrame::code() const {
392   return Heap::js_construct_entry_code();
393 }
394 
395 
code() const396 Code* ExitFrame::code() const {
397   return Heap::c_entry_code();
398 }
399 
400 
ComputeCallerState(State * state) const401 void ExitFrame::ComputeCallerState(State* state) const {
402   // Setup the caller state.
403   state->sp = caller_sp();
404   state->fp = Memory::Address_at(fp() + ExitFrameConstants::kCallerFPOffset);
405   state->pc_address
406       = reinterpret_cast<Address*>(fp() + ExitFrameConstants::kCallerPCOffset);
407 }
408 
409 
GetCallerStackPointer() const410 Address ExitFrame::GetCallerStackPointer() const {
411   return fp() + ExitFrameConstants::kCallerSPDisplacement;
412 }
413 
414 
code() const415 Code* ExitDebugFrame::code() const {
416   return Heap::c_entry_debug_break_code();
417 }
418 
419 
GetExpressionAddress(int n) const420 Address StandardFrame::GetExpressionAddress(int n) const {
421   const int offset = StandardFrameConstants::kExpressionsOffset;
422   return fp() + offset - n * kPointerSize;
423 }
424 
425 
ComputeExpressionsCount() const426 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.
433   return (base - limit) / kPointerSize;
434 }
435 
436 
ComputeCallerState(State * state) const437 void StandardFrame::ComputeCallerState(State* state) const {
438   state->sp = caller_sp();
439   state->fp = caller_fp();
440   state->pc_address = reinterpret_cast<Address*>(ComputePCAddress(fp()));
441 }
442 
443 
IsExpressionInsideHandler(int n) const444 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 
GetParameter(int index) const453 Object* JavaScriptFrame::GetParameter(int index) const {
454   ASSERT(index >= 0 && index < ComputeParametersCount());
455   const int offset = JavaScriptFrameConstants::kParam0Offset;
456   return Memory::Object_at(caller_sp() + offset - (index * kPointerSize));
457 }
458 
459 
ComputeParametersCount() const460 int JavaScriptFrame::ComputeParametersCount() const {
461   Address base  = caller_sp() + JavaScriptFrameConstants::kReceiverOffset;
462   Address limit = fp() + JavaScriptFrameConstants::kSavedRegistersOffset;
463   return (base - limit) / kPointerSize;
464 }
465 
466 
IsConstructor() const467 bool JavaScriptFrame::IsConstructor() const {
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);
474 }
475 
476 
code() const477 Code* JavaScriptFrame::code() const {
478   JSFunction* function = JSFunction::cast(this->function());
479   return function->shared()->code();
480 }
481 
482 
code() const483 Code* ArgumentsAdaptorFrame::code() const {
484   return Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline);
485 }
486 
487 
code() const488 Code* InternalFrame::code() const {
489   const int offset = InternalFrameConstants::kCodeOffset;
490   Object* code = Memory::Object_at(fp() + offset);
491   ASSERT(code != NULL);
492   return Code::cast(code);
493 }
494 
495 
PrintIndex(StringStream * accumulator,PrintMode mode,int index)496 void StackFrame::PrintIndex(StringStream* accumulator,
497                             PrintMode mode,
498                             int index) {
499   accumulator->Add((mode == OVERVIEW) ? "%5d: " : "[%d]: ", index);
500 }
501 
502 
Print(StringStream * accumulator,PrintMode mode,int index) const503 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.
592   int expressions_start = stack_locals_count;
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 
Print(StringStream * accumulator,PrintMode mode,int index) const613 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 
Iterate(ObjectVisitor * v) const645 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.
653 #ifdef DEBUG
654   it.Advance();
655   ASSERT(it.done());
656 #endif
657 }
658 
659 
IterateExpressions(ObjectVisitor * v) const660 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 
Iterate(ObjectVisitor * v) const679 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);
686   Object** limit = &Memory::Object_at(caller_sp() + kLimitOffset) + 1;
687   v->VisitPointers(base, limit);
688 }
689 
690 
Iterate(ObjectVisitor * v) const691 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 
FindJavaScriptFrame(int n)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 
NumRegs(RegList reglist)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 
JSCallerSavedCode(int n)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 
743 } }  // namespace v8::internal
744