blob: e56a2c83edc7ae88018afeda2321b0378c293234 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
37namespace v8 {
38namespace internal {
39
40// Iterator that supports traversing the stack handlers of a
41// particular frame. Needs to know the top of the handler chain.
42class StackHandlerIterator BASE_EMBEDDED {
43 public:
44 StackHandlerIterator(const StackFrame* frame, StackHandler* handler)
45 : limit_(frame->fp()), handler_(handler) {
46 // Make sure the handler has already been unwound to this frame.
47 ASSERT(frame->sp() <= handler->address());
48 }
49
50 StackHandler* handler() const { return handler_; }
51
52 bool done() {
53 return handler_ == NULL || handler_->address() > limit_;
54 }
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),
70StackFrameIterator::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}
76StackFrameIterator::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}
82StackFrameIterator::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
98void 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
121void 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
130void 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
149StackFrame* 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
159StackFrame* 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
178StackTraceFrameIterator::StackTraceFrameIterator() {
Leon Clarke4515c472010-02-03 11:58:03 +0000179 if (!done() && !IsValidFrame()) Advance();
Steve Blocka7e24c12009-10-30 11:49:00 +0000180}
181
182
183void StackTraceFrameIterator::Advance() {
184 while (true) {
185 JavaScriptFrameIterator::Advance();
186 if (done()) return;
Leon Clarke4515c472010-02-03 11:58:03 +0000187 if (IsValidFrame()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000188 }
189}
190
Leon Clarke4515c472010-02-03 11:58:03 +0000191bool StackTraceFrameIterator::IsValidFrame() {
192 if (!frame()->function()->IsJSFunction()) return false;
193 Object* script = JSFunction::cast(frame()->function())->shared()->script();
194 // Don't show functions from native scripts to user.
195 return (script->IsScript() &&
196 Script::TYPE_NATIVE != Script::cast(script)->type()->value());
197}
198
Steve Blocka7e24c12009-10-30 11:49:00 +0000199
200// -------------------------------------------------------------------------
201
202
203SafeStackFrameIterator::SafeStackFrameIterator(
204 Address fp, Address sp, Address low_bound, Address high_bound) :
205 low_bound_(low_bound), high_bound_(high_bound),
206 is_valid_top_(
207 IsWithinBounds(low_bound, high_bound,
208 Top::c_entry_fp(Top::GetCurrentThread())) &&
209 Top::handler(Top::GetCurrentThread()) != NULL),
210 is_valid_fp_(IsWithinBounds(low_bound, high_bound, fp)),
211 is_working_iterator_(is_valid_top_ || is_valid_fp_),
212 iteration_done_(!is_working_iterator_),
213 iterator_(is_valid_top_, is_valid_fp_ ? fp : NULL, sp) {
214}
215
216
217void SafeStackFrameIterator::Advance() {
218 ASSERT(is_working_iterator_);
219 ASSERT(!done());
220 StackFrame* last_frame = iterator_.frame();
221 Address last_sp = last_frame->sp(), last_fp = last_frame->fp();
222 // Before advancing to the next stack frame, perform pointer validity tests
223 iteration_done_ = !IsValidFrame(last_frame) ||
224 !CanIterateHandles(last_frame, iterator_.handler()) ||
225 !IsValidCaller(last_frame);
226 if (iteration_done_) return;
227
228 iterator_.Advance();
229 if (iterator_.done()) return;
230 // Check that we have actually moved to the previous frame in the stack
231 StackFrame* prev_frame = iterator_.frame();
232 iteration_done_ = prev_frame->sp() < last_sp || prev_frame->fp() < last_fp;
233}
234
235
236bool SafeStackFrameIterator::CanIterateHandles(StackFrame* frame,
237 StackHandler* handler) {
238 // If StackIterator iterates over StackHandles, verify that
239 // StackHandlerIterator can be instantiated (see StackHandlerIterator
240 // constructor.)
241 return !is_valid_top_ || (frame->sp() <= handler->address());
242}
243
244
245bool SafeStackFrameIterator::IsValidFrame(StackFrame* frame) const {
246 return IsValidStackAddress(frame->sp()) && IsValidStackAddress(frame->fp());
247}
248
249
250bool SafeStackFrameIterator::IsValidCaller(StackFrame* frame) {
251 StackFrame::State state;
252 if (frame->is_entry() || frame->is_entry_construct()) {
253 // See EntryFrame::GetCallerState. It computes the caller FP address
254 // and calls ExitFrame::GetStateForFramePointer on it. We need to be
255 // sure that caller FP address is valid.
256 Address caller_fp = Memory::Address_at(
257 frame->fp() + EntryFrameConstants::kCallerFPOffset);
258 if (!IsValidStackAddress(caller_fp)) {
259 return false;
260 }
261 } else if (frame->is_arguments_adaptor()) {
262 // See ArgumentsAdaptorFrame::GetCallerStackPointer. It assumes that
263 // the number of arguments is stored on stack as Smi. We need to check
264 // that it really an Smi.
265 Object* number_of_args = reinterpret_cast<ArgumentsAdaptorFrame*>(frame)->
266 GetExpression(0);
267 if (!number_of_args->IsSmi()) {
268 return false;
269 }
270 }
271 frame->ComputeCallerState(&state);
272 return IsValidStackAddress(state.sp) && IsValidStackAddress(state.fp) &&
273 iterator_.SingletonFor(frame->GetCallerState(&state)) != NULL;
274}
275
276
277void SafeStackFrameIterator::Reset() {
278 if (is_working_iterator_) {
279 iterator_.Reset();
280 iteration_done_ = false;
281 }
282}
283
284
285// -------------------------------------------------------------------------
286
287
288#ifdef ENABLE_LOGGING_AND_PROFILING
289SafeStackTraceFrameIterator::SafeStackTraceFrameIterator(
290 Address fp, Address sp, Address low_bound, Address high_bound) :
291 SafeJavaScriptFrameIterator(fp, sp, low_bound, high_bound) {
292 if (!done() && !frame()->is_java_script()) Advance();
293}
294
295
296void SafeStackTraceFrameIterator::Advance() {
297 while (true) {
298 SafeJavaScriptFrameIterator::Advance();
299 if (done()) return;
300 if (frame()->is_java_script()) return;
301 }
302}
303#endif
304
305
306// -------------------------------------------------------------------------
307
308
309void StackHandler::Cook(Code* code) {
310 ASSERT(MarkCompactCollector::IsCompacting());
311 ASSERT(code->contains(pc()));
312 set_pc(AddressFrom<Address>(pc() - code->instruction_start()));
313}
314
315
316void StackHandler::Uncook(Code* code) {
Leon Clarkee46be812010-01-19 14:06:41 +0000317 ASSERT(MarkCompactCollector::HasCompacted());
Steve Blocka7e24c12009-10-30 11:49:00 +0000318 set_pc(code->instruction_start() + OffsetFrom(pc()));
319 ASSERT(code->contains(pc()));
320}
321
322
323// -------------------------------------------------------------------------
324
325
326bool StackFrame::HasHandler() const {
327 StackHandlerIterator it(this, top_handler());
328 return !it.done();
329}
330
331
332void StackFrame::CookFramesForThread(ThreadLocalTop* thread) {
333 // Only cooking frames when the collector is compacting and thus moving code
334 // around.
335 ASSERT(MarkCompactCollector::IsCompacting());
336 ASSERT(!thread->stack_is_cooked());
337 for (StackFrameIterator it(thread); !it.done(); it.Advance()) {
338 it.frame()->Cook();
339 }
340 thread->set_stack_is_cooked(true);
341}
342
343
344void StackFrame::UncookFramesForThread(ThreadLocalTop* thread) {
345 // Only uncooking frames when the collector is compacting and thus moving code
346 // around.
Leon Clarkee46be812010-01-19 14:06:41 +0000347 ASSERT(MarkCompactCollector::HasCompacted());
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 ASSERT(thread->stack_is_cooked());
349 for (StackFrameIterator it(thread); !it.done(); it.Advance()) {
350 it.frame()->Uncook();
351 }
352 thread->set_stack_is_cooked(false);
353}
354
355
356void StackFrame::Cook() {
357 Code* code = this->code();
358 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
359 it.handler()->Cook(code);
360 }
361 ASSERT(code->contains(pc()));
362 set_pc(AddressFrom<Address>(pc() - code->instruction_start()));
363}
364
365
366void StackFrame::Uncook() {
367 Code* code = this->code();
368 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
369 it.handler()->Uncook(code);
370 }
371 set_pc(code->instruction_start() + OffsetFrom(pc()));
372 ASSERT(code->contains(pc()));
373}
374
375
376StackFrame::Type StackFrame::GetCallerState(State* state) const {
377 ComputeCallerState(state);
378 return ComputeType(state);
379}
380
381
382Code* EntryFrame::code() const {
383 return Heap::js_entry_code();
384}
385
386
387void EntryFrame::ComputeCallerState(State* state) const {
388 GetCallerState(state);
389}
390
391
392StackFrame::Type EntryFrame::GetCallerState(State* state) const {
393 const int offset = EntryFrameConstants::kCallerFPOffset;
394 Address fp = Memory::Address_at(this->fp() + offset);
395 return ExitFrame::GetStateForFramePointer(fp, state);
396}
397
398
399Code* EntryConstructFrame::code() const {
400 return Heap::js_construct_entry_code();
401}
402
403
Steve Blockd0582a62009-12-15 09:54:21 +0000404Object*& ExitFrame::code_slot() const {
405 const int offset = ExitFrameConstants::kCodeOffset;
406 return Memory::Object_at(fp() + offset);
407}
408
409
Steve Blocka7e24c12009-10-30 11:49:00 +0000410Code* ExitFrame::code() const {
Steve Blockd0582a62009-12-15 09:54:21 +0000411 Object* code = code_slot();
412 if (code->IsSmi()) {
Leon Clarke4515c472010-02-03 11:58:03 +0000413 return Heap::debugger_statement_code();
Steve Blockd0582a62009-12-15 09:54:21 +0000414 } else {
415 return Code::cast(code);
416 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000417}
418
419
420void ExitFrame::ComputeCallerState(State* state) const {
421 // Setup the caller state.
422 state->sp = caller_sp();
423 state->fp = Memory::Address_at(fp() + ExitFrameConstants::kCallerFPOffset);
424 state->pc_address
425 = reinterpret_cast<Address*>(fp() + ExitFrameConstants::kCallerPCOffset);
426}
427
428
429Address ExitFrame::GetCallerStackPointer() const {
430 return fp() + ExitFrameConstants::kCallerSPDisplacement;
431}
432
433
Steve Blocka7e24c12009-10-30 11:49:00 +0000434Address StandardFrame::GetExpressionAddress(int n) const {
435 const int offset = StandardFrameConstants::kExpressionsOffset;
436 return fp() + offset - n * kPointerSize;
437}
438
439
440int StandardFrame::ComputeExpressionsCount() const {
441 const int offset =
442 StandardFrameConstants::kExpressionsOffset + kPointerSize;
443 Address base = fp() + offset;
444 Address limit = sp();
445 ASSERT(base >= limit); // stack grows downwards
446 // Include register-allocated locals in number of expressions.
Steve Blockd0582a62009-12-15 09:54:21 +0000447 return static_cast<int>((base - limit) / kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000448}
449
450
451void StandardFrame::ComputeCallerState(State* state) const {
452 state->sp = caller_sp();
453 state->fp = caller_fp();
454 state->pc_address = reinterpret_cast<Address*>(ComputePCAddress(fp()));
455}
456
457
458bool StandardFrame::IsExpressionInsideHandler(int n) const {
459 Address address = GetExpressionAddress(n);
460 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
461 if (it.handler()->includes(address)) return true;
462 }
463 return false;
464}
465
466
467Object* JavaScriptFrame::GetParameter(int index) const {
468 ASSERT(index >= 0 && index < ComputeParametersCount());
469 const int offset = JavaScriptFrameConstants::kParam0Offset;
470 return Memory::Object_at(caller_sp() + offset - (index * kPointerSize));
471}
472
473
474int JavaScriptFrame::ComputeParametersCount() const {
475 Address base = caller_sp() + JavaScriptFrameConstants::kReceiverOffset;
476 Address limit = fp() + JavaScriptFrameConstants::kSavedRegistersOffset;
Steve Blockd0582a62009-12-15 09:54:21 +0000477 return static_cast<int>((base - limit) / kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000478}
479
480
481bool JavaScriptFrame::IsConstructor() const {
482 Address fp = caller_fp();
483 if (has_adapted_arguments()) {
484 // Skip the arguments adaptor frame and look at the real caller.
485 fp = Memory::Address_at(fp + StandardFrameConstants::kCallerFPOffset);
486 }
487 return IsConstructFrame(fp);
488}
489
490
491Code* JavaScriptFrame::code() const {
492 JSFunction* function = JSFunction::cast(this->function());
493 return function->shared()->code();
494}
495
496
497Code* ArgumentsAdaptorFrame::code() const {
498 return Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline);
499}
500
501
502Code* InternalFrame::code() const {
503 const int offset = InternalFrameConstants::kCodeOffset;
504 Object* code = Memory::Object_at(fp() + offset);
505 ASSERT(code != NULL);
506 return Code::cast(code);
507}
508
509
510void StackFrame::PrintIndex(StringStream* accumulator,
511 PrintMode mode,
512 int index) {
513 accumulator->Add((mode == OVERVIEW) ? "%5d: " : "[%d]: ", index);
514}
515
516
517void JavaScriptFrame::Print(StringStream* accumulator,
518 PrintMode mode,
519 int index) const {
520 HandleScope scope;
521 Object* receiver = this->receiver();
522 Object* function = this->function();
523
524 accumulator->PrintSecurityTokenIfChanged(function);
525 PrintIndex(accumulator, mode, index);
526 Code* code = NULL;
527 if (IsConstructor()) accumulator->Add("new ");
528 accumulator->PrintFunction(function, receiver, &code);
529 accumulator->Add("(this=%o", receiver);
530
531 // Get scope information for nicer output, if possible. If code is
532 // NULL, or doesn't contain scope info, info will return 0 for the
533 // number of parameters, stack slots, or context slots.
534 ScopeInfo<PreallocatedStorage> info(code);
535
536 // Print the parameters.
537 int parameters_count = ComputeParametersCount();
538 for (int i = 0; i < parameters_count; i++) {
539 accumulator->Add(",");
540 // If we have a name for the parameter we print it. Nameless
541 // parameters are either because we have more actual parameters
542 // than formal parameters or because we have no scope information.
543 if (i < info.number_of_parameters()) {
544 accumulator->PrintName(*info.parameter_name(i));
545 accumulator->Add("=");
546 }
547 accumulator->Add("%o", GetParameter(i));
548 }
549
550 accumulator->Add(")");
551 if (mode == OVERVIEW) {
552 accumulator->Add("\n");
553 return;
554 }
555 accumulator->Add(" {\n");
556
557 // Compute the number of locals and expression stack elements.
558 int stack_locals_count = info.number_of_stack_slots();
559 int heap_locals_count = info.number_of_context_slots();
560 int expressions_count = ComputeExpressionsCount();
561
562 // Print stack-allocated local variables.
563 if (stack_locals_count > 0) {
564 accumulator->Add(" // stack-allocated locals\n");
565 }
566 for (int i = 0; i < stack_locals_count; i++) {
567 accumulator->Add(" var ");
568 accumulator->PrintName(*info.stack_slot_name(i));
569 accumulator->Add(" = ");
570 if (i < expressions_count) {
571 accumulator->Add("%o", GetExpression(i));
572 } else {
573 accumulator->Add("// no expression found - inconsistent frame?");
574 }
575 accumulator->Add("\n");
576 }
577
578 // Try to get hold of the context of this frame.
579 Context* context = NULL;
580 if (this->context() != NULL && this->context()->IsContext()) {
581 context = Context::cast(this->context());
582 }
583
584 // Print heap-allocated local variables.
585 if (heap_locals_count > Context::MIN_CONTEXT_SLOTS) {
586 accumulator->Add(" // heap-allocated locals\n");
587 }
588 for (int i = Context::MIN_CONTEXT_SLOTS; i < heap_locals_count; i++) {
589 accumulator->Add(" var ");
590 accumulator->PrintName(*info.context_slot_name(i));
591 accumulator->Add(" = ");
592 if (context != NULL) {
593 if (i < context->length()) {
594 accumulator->Add("%o", context->get(i));
595 } else {
596 accumulator->Add(
597 "// warning: missing context slot - inconsistent frame?");
598 }
599 } else {
600 accumulator->Add("// warning: no context found - inconsistent frame?");
601 }
602 accumulator->Add("\n");
603 }
604
605 // Print the expression stack.
606 int expressions_start = stack_locals_count;
607 if (expressions_start < expressions_count) {
608 accumulator->Add(" // expression stack (top to bottom)\n");
609 }
610 for (int i = expressions_count - 1; i >= expressions_start; i--) {
611 if (IsExpressionInsideHandler(i)) continue;
612 accumulator->Add(" [%02d] : %o\n", i, GetExpression(i));
613 }
614
615 // Print details about the function.
616 if (FLAG_max_stack_trace_source_length != 0 && code != NULL) {
617 SharedFunctionInfo* shared = JSFunction::cast(function)->shared();
618 accumulator->Add("--------- s o u r c e c o d e ---------\n");
619 shared->SourceCodePrint(accumulator, FLAG_max_stack_trace_source_length);
620 accumulator->Add("\n-----------------------------------------\n");
621 }
622
623 accumulator->Add("}\n\n");
624}
625
626
627void ArgumentsAdaptorFrame::Print(StringStream* accumulator,
628 PrintMode mode,
629 int index) const {
630 int actual = ComputeParametersCount();
631 int expected = -1;
632 Object* function = this->function();
633 if (function->IsJSFunction()) {
634 expected = JSFunction::cast(function)->shared()->formal_parameter_count();
635 }
636
637 PrintIndex(accumulator, mode, index);
638 accumulator->Add("arguments adaptor frame: %d->%d", actual, expected);
639 if (mode == OVERVIEW) {
640 accumulator->Add("\n");
641 return;
642 }
643 accumulator->Add(" {\n");
644
645 // Print actual arguments.
646 if (actual > 0) accumulator->Add(" // actual arguments\n");
647 for (int i = 0; i < actual; i++) {
648 accumulator->Add(" [%02d] : %o", i, GetParameter(i));
649 if (expected != -1 && i >= expected) {
650 accumulator->Add(" // not passed to callee");
651 }
652 accumulator->Add("\n");
653 }
654
655 accumulator->Add("}\n\n");
656}
657
658
659void EntryFrame::Iterate(ObjectVisitor* v) const {
660 StackHandlerIterator it(this, top_handler());
661 ASSERT(!it.done());
662 StackHandler* handler = it.handler();
663 ASSERT(handler->is_entry());
664 handler->Iterate(v);
665 // Make sure that there's the entry frame does not contain more than
666 // one stack handler.
667#ifdef DEBUG
668 it.Advance();
669 ASSERT(it.done());
670#endif
671}
672
673
674void StandardFrame::IterateExpressions(ObjectVisitor* v) const {
675 const int offset = StandardFrameConstants::kContextOffset;
676 Object** base = &Memory::Object_at(sp());
677 Object** limit = &Memory::Object_at(fp() + offset) + 1;
678 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
679 StackHandler* handler = it.handler();
680 // Traverse pointers down to - but not including - the next
681 // handler in the handler chain. Update the base to skip the
682 // handler and allow the handler to traverse its own pointers.
683 const Address address = handler->address();
684 v->VisitPointers(base, reinterpret_cast<Object**>(address));
685 base = reinterpret_cast<Object**>(address + StackHandlerConstants::kSize);
686 // Traverse the pointers in the handler itself.
687 handler->Iterate(v);
688 }
689 v->VisitPointers(base, limit);
690}
691
692
693void JavaScriptFrame::Iterate(ObjectVisitor* v) const {
694 IterateExpressions(v);
695
696 // Traverse callee-saved registers, receiver, and parameters.
697 const int kBaseOffset = JavaScriptFrameConstants::kSavedRegistersOffset;
698 const int kLimitOffset = JavaScriptFrameConstants::kReceiverOffset;
699 Object** base = &Memory::Object_at(fp() + kBaseOffset);
700 Object** limit = &Memory::Object_at(caller_sp() + kLimitOffset) + 1;
701 v->VisitPointers(base, limit);
702}
703
704
705void InternalFrame::Iterate(ObjectVisitor* v) const {
706 // Internal frames only have object pointers on the expression stack
707 // as they never have any arguments.
708 IterateExpressions(v);
709}
710
711
712// -------------------------------------------------------------------------
713
714
715JavaScriptFrame* StackFrameLocator::FindJavaScriptFrame(int n) {
716 ASSERT(n >= 0);
717 for (int i = 0; i <= n; i++) {
718 while (!iterator_.frame()->is_java_script()) iterator_.Advance();
719 if (i == n) return JavaScriptFrame::cast(iterator_.frame());
720 iterator_.Advance();
721 }
722 UNREACHABLE();
723 return NULL;
724}
725
726
727// -------------------------------------------------------------------------
728
729
730int NumRegs(RegList reglist) {
731 int n = 0;
732 while (reglist != 0) {
733 n++;
734 reglist &= reglist - 1; // clear one bit
735 }
736 return n;
737}
738
739
740int JSCallerSavedCode(int n) {
741 static int reg_code[kNumJSCallerSaved];
742 static bool initialized = false;
743 if (!initialized) {
744 initialized = true;
745 int i = 0;
746 for (int r = 0; r < kNumRegs; r++)
747 if ((kJSCallerSaved & (1 << r)) != 0)
748 reg_code[i++] = r;
749
750 ASSERT(i == kNumJSCallerSaved);
751 }
752 ASSERT(0 <= n && n < kNumJSCallerSaved);
753 return reg_code[n];
754}
755
756
757} } // namespace v8::internal