blob: 055074062662d8b98a241eb9e1b5e55e847f7760 [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();
Andrei Popescu31002712010-02-23 13:46:05 +0000358 ASSERT(code->IsCode());
Steve Blocka7e24c12009-10-30 11:49:00 +0000359 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
360 it.handler()->Cook(code);
361 }
362 ASSERT(code->contains(pc()));
363 set_pc(AddressFrom<Address>(pc() - code->instruction_start()));
364}
365
366
367void StackFrame::Uncook() {
368 Code* code = this->code();
Andrei Popescu31002712010-02-23 13:46:05 +0000369 ASSERT(code->IsCode());
Steve Blocka7e24c12009-10-30 11:49:00 +0000370 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
371 it.handler()->Uncook(code);
372 }
373 set_pc(code->instruction_start() + OffsetFrom(pc()));
374 ASSERT(code->contains(pc()));
375}
376
377
378StackFrame::Type StackFrame::GetCallerState(State* state) const {
379 ComputeCallerState(state);
380 return ComputeType(state);
381}
382
383
384Code* EntryFrame::code() const {
385 return Heap::js_entry_code();
386}
387
388
389void EntryFrame::ComputeCallerState(State* state) const {
390 GetCallerState(state);
391}
392
393
394StackFrame::Type EntryFrame::GetCallerState(State* state) const {
395 const int offset = EntryFrameConstants::kCallerFPOffset;
396 Address fp = Memory::Address_at(this->fp() + offset);
397 return ExitFrame::GetStateForFramePointer(fp, state);
398}
399
400
401Code* EntryConstructFrame::code() const {
402 return Heap::js_construct_entry_code();
403}
404
405
Steve Blockd0582a62009-12-15 09:54:21 +0000406Object*& ExitFrame::code_slot() const {
407 const int offset = ExitFrameConstants::kCodeOffset;
408 return Memory::Object_at(fp() + offset);
409}
410
411
Steve Blocka7e24c12009-10-30 11:49:00 +0000412Code* ExitFrame::code() const {
Steve Blockd0582a62009-12-15 09:54:21 +0000413 Object* code = code_slot();
414 if (code->IsSmi()) {
Leon Clarke4515c472010-02-03 11:58:03 +0000415 return Heap::debugger_statement_code();
Steve Blockd0582a62009-12-15 09:54:21 +0000416 } else {
417 return Code::cast(code);
418 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000419}
420
421
422void ExitFrame::ComputeCallerState(State* state) const {
423 // Setup the caller state.
424 state->sp = caller_sp();
425 state->fp = Memory::Address_at(fp() + ExitFrameConstants::kCallerFPOffset);
426 state->pc_address
427 = reinterpret_cast<Address*>(fp() + ExitFrameConstants::kCallerPCOffset);
428}
429
430
431Address ExitFrame::GetCallerStackPointer() const {
432 return fp() + ExitFrameConstants::kCallerSPDisplacement;
433}
434
435
Steve Blocka7e24c12009-10-30 11:49:00 +0000436Address StandardFrame::GetExpressionAddress(int n) const {
437 const int offset = StandardFrameConstants::kExpressionsOffset;
438 return fp() + offset - n * kPointerSize;
439}
440
441
442int StandardFrame::ComputeExpressionsCount() const {
443 const int offset =
444 StandardFrameConstants::kExpressionsOffset + kPointerSize;
445 Address base = fp() + offset;
446 Address limit = sp();
447 ASSERT(base >= limit); // stack grows downwards
448 // Include register-allocated locals in number of expressions.
Steve Blockd0582a62009-12-15 09:54:21 +0000449 return static_cast<int>((base - limit) / kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000450}
451
452
453void StandardFrame::ComputeCallerState(State* state) const {
454 state->sp = caller_sp();
455 state->fp = caller_fp();
456 state->pc_address = reinterpret_cast<Address*>(ComputePCAddress(fp()));
457}
458
459
460bool StandardFrame::IsExpressionInsideHandler(int n) const {
461 Address address = GetExpressionAddress(n);
462 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
463 if (it.handler()->includes(address)) return true;
464 }
465 return false;
466}
467
468
469Object* JavaScriptFrame::GetParameter(int index) const {
470 ASSERT(index >= 0 && index < ComputeParametersCount());
471 const int offset = JavaScriptFrameConstants::kParam0Offset;
472 return Memory::Object_at(caller_sp() + offset - (index * kPointerSize));
473}
474
475
476int JavaScriptFrame::ComputeParametersCount() const {
477 Address base = caller_sp() + JavaScriptFrameConstants::kReceiverOffset;
478 Address limit = fp() + JavaScriptFrameConstants::kSavedRegistersOffset;
Steve Blockd0582a62009-12-15 09:54:21 +0000479 return static_cast<int>((base - limit) / kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000480}
481
482
483bool JavaScriptFrame::IsConstructor() const {
484 Address fp = caller_fp();
485 if (has_adapted_arguments()) {
486 // Skip the arguments adaptor frame and look at the real caller.
487 fp = Memory::Address_at(fp + StandardFrameConstants::kCallerFPOffset);
488 }
489 return IsConstructFrame(fp);
490}
491
492
493Code* JavaScriptFrame::code() const {
494 JSFunction* function = JSFunction::cast(this->function());
495 return function->shared()->code();
496}
497
498
499Code* ArgumentsAdaptorFrame::code() const {
500 return Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline);
501}
502
503
504Code* InternalFrame::code() const {
505 const int offset = InternalFrameConstants::kCodeOffset;
506 Object* code = Memory::Object_at(fp() + offset);
507 ASSERT(code != NULL);
508 return Code::cast(code);
509}
510
511
512void StackFrame::PrintIndex(StringStream* accumulator,
513 PrintMode mode,
514 int index) {
515 accumulator->Add((mode == OVERVIEW) ? "%5d: " : "[%d]: ", index);
516}
517
518
519void JavaScriptFrame::Print(StringStream* accumulator,
520 PrintMode mode,
521 int index) const {
522 HandleScope scope;
523 Object* receiver = this->receiver();
524 Object* function = this->function();
525
526 accumulator->PrintSecurityTokenIfChanged(function);
527 PrintIndex(accumulator, mode, index);
528 Code* code = NULL;
529 if (IsConstructor()) accumulator->Add("new ");
530 accumulator->PrintFunction(function, receiver, &code);
531 accumulator->Add("(this=%o", receiver);
532
533 // Get scope information for nicer output, if possible. If code is
534 // NULL, or doesn't contain scope info, info will return 0 for the
535 // number of parameters, stack slots, or context slots.
536 ScopeInfo<PreallocatedStorage> info(code);
537
538 // Print the parameters.
539 int parameters_count = ComputeParametersCount();
540 for (int i = 0; i < parameters_count; i++) {
541 accumulator->Add(",");
542 // If we have a name for the parameter we print it. Nameless
543 // parameters are either because we have more actual parameters
544 // than formal parameters or because we have no scope information.
545 if (i < info.number_of_parameters()) {
546 accumulator->PrintName(*info.parameter_name(i));
547 accumulator->Add("=");
548 }
549 accumulator->Add("%o", GetParameter(i));
550 }
551
552 accumulator->Add(")");
553 if (mode == OVERVIEW) {
554 accumulator->Add("\n");
555 return;
556 }
557 accumulator->Add(" {\n");
558
559 // Compute the number of locals and expression stack elements.
560 int stack_locals_count = info.number_of_stack_slots();
561 int heap_locals_count = info.number_of_context_slots();
562 int expressions_count = ComputeExpressionsCount();
563
564 // Print stack-allocated local variables.
565 if (stack_locals_count > 0) {
566 accumulator->Add(" // stack-allocated locals\n");
567 }
568 for (int i = 0; i < stack_locals_count; i++) {
569 accumulator->Add(" var ");
570 accumulator->PrintName(*info.stack_slot_name(i));
571 accumulator->Add(" = ");
572 if (i < expressions_count) {
573 accumulator->Add("%o", GetExpression(i));
574 } else {
575 accumulator->Add("// no expression found - inconsistent frame?");
576 }
577 accumulator->Add("\n");
578 }
579
580 // Try to get hold of the context of this frame.
581 Context* context = NULL;
582 if (this->context() != NULL && this->context()->IsContext()) {
583 context = Context::cast(this->context());
584 }
585
586 // Print heap-allocated local variables.
587 if (heap_locals_count > Context::MIN_CONTEXT_SLOTS) {
588 accumulator->Add(" // heap-allocated locals\n");
589 }
590 for (int i = Context::MIN_CONTEXT_SLOTS; i < heap_locals_count; i++) {
591 accumulator->Add(" var ");
592 accumulator->PrintName(*info.context_slot_name(i));
593 accumulator->Add(" = ");
594 if (context != NULL) {
595 if (i < context->length()) {
596 accumulator->Add("%o", context->get(i));
597 } else {
598 accumulator->Add(
599 "// warning: missing context slot - inconsistent frame?");
600 }
601 } else {
602 accumulator->Add("// warning: no context found - inconsistent frame?");
603 }
604 accumulator->Add("\n");
605 }
606
607 // Print the expression stack.
608 int expressions_start = stack_locals_count;
609 if (expressions_start < expressions_count) {
610 accumulator->Add(" // expression stack (top to bottom)\n");
611 }
612 for (int i = expressions_count - 1; i >= expressions_start; i--) {
613 if (IsExpressionInsideHandler(i)) continue;
614 accumulator->Add(" [%02d] : %o\n", i, GetExpression(i));
615 }
616
617 // Print details about the function.
618 if (FLAG_max_stack_trace_source_length != 0 && code != NULL) {
619 SharedFunctionInfo* shared = JSFunction::cast(function)->shared();
620 accumulator->Add("--------- s o u r c e c o d e ---------\n");
621 shared->SourceCodePrint(accumulator, FLAG_max_stack_trace_source_length);
622 accumulator->Add("\n-----------------------------------------\n");
623 }
624
625 accumulator->Add("}\n\n");
626}
627
628
629void ArgumentsAdaptorFrame::Print(StringStream* accumulator,
630 PrintMode mode,
631 int index) const {
632 int actual = ComputeParametersCount();
633 int expected = -1;
634 Object* function = this->function();
635 if (function->IsJSFunction()) {
636 expected = JSFunction::cast(function)->shared()->formal_parameter_count();
637 }
638
639 PrintIndex(accumulator, mode, index);
640 accumulator->Add("arguments adaptor frame: %d->%d", actual, expected);
641 if (mode == OVERVIEW) {
642 accumulator->Add("\n");
643 return;
644 }
645 accumulator->Add(" {\n");
646
647 // Print actual arguments.
648 if (actual > 0) accumulator->Add(" // actual arguments\n");
649 for (int i = 0; i < actual; i++) {
650 accumulator->Add(" [%02d] : %o", i, GetParameter(i));
651 if (expected != -1 && i >= expected) {
652 accumulator->Add(" // not passed to callee");
653 }
654 accumulator->Add("\n");
655 }
656
657 accumulator->Add("}\n\n");
658}
659
660
661void EntryFrame::Iterate(ObjectVisitor* v) const {
662 StackHandlerIterator it(this, top_handler());
663 ASSERT(!it.done());
664 StackHandler* handler = it.handler();
665 ASSERT(handler->is_entry());
666 handler->Iterate(v);
667 // Make sure that there's the entry frame does not contain more than
668 // one stack handler.
669#ifdef DEBUG
670 it.Advance();
671 ASSERT(it.done());
672#endif
673}
674
675
676void StandardFrame::IterateExpressions(ObjectVisitor* v) const {
677 const int offset = StandardFrameConstants::kContextOffset;
678 Object** base = &Memory::Object_at(sp());
679 Object** limit = &Memory::Object_at(fp() + offset) + 1;
680 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
681 StackHandler* handler = it.handler();
682 // Traverse pointers down to - but not including - the next
683 // handler in the handler chain. Update the base to skip the
684 // handler and allow the handler to traverse its own pointers.
685 const Address address = handler->address();
686 v->VisitPointers(base, reinterpret_cast<Object**>(address));
687 base = reinterpret_cast<Object**>(address + StackHandlerConstants::kSize);
688 // Traverse the pointers in the handler itself.
689 handler->Iterate(v);
690 }
691 v->VisitPointers(base, limit);
692}
693
694
695void JavaScriptFrame::Iterate(ObjectVisitor* v) const {
696 IterateExpressions(v);
697
698 // Traverse callee-saved registers, receiver, and parameters.
699 const int kBaseOffset = JavaScriptFrameConstants::kSavedRegistersOffset;
700 const int kLimitOffset = JavaScriptFrameConstants::kReceiverOffset;
701 Object** base = &Memory::Object_at(fp() + kBaseOffset);
702 Object** limit = &Memory::Object_at(caller_sp() + kLimitOffset) + 1;
703 v->VisitPointers(base, limit);
704}
705
706
707void InternalFrame::Iterate(ObjectVisitor* v) const {
708 // Internal frames only have object pointers on the expression stack
709 // as they never have any arguments.
710 IterateExpressions(v);
711}
712
713
714// -------------------------------------------------------------------------
715
716
717JavaScriptFrame* StackFrameLocator::FindJavaScriptFrame(int n) {
718 ASSERT(n >= 0);
719 for (int i = 0; i <= n; i++) {
720 while (!iterator_.frame()->is_java_script()) iterator_.Advance();
721 if (i == n) return JavaScriptFrame::cast(iterator_.frame());
722 iterator_.Advance();
723 }
724 UNREACHABLE();
725 return NULL;
726}
727
728
729// -------------------------------------------------------------------------
730
731
732int NumRegs(RegList reglist) {
733 int n = 0;
734 while (reglist != 0) {
735 n++;
736 reglist &= reglist - 1; // clear one bit
737 }
738 return n;
739}
740
741
742int JSCallerSavedCode(int n) {
743 static int reg_code[kNumJSCallerSaved];
744 static bool initialized = false;
745 if (!initialized) {
746 initialized = true;
747 int i = 0;
748 for (int r = 0; r < kNumRegs; r++)
749 if ((kJSCallerSaved & (1 << r)) != 0)
750 reg_code[i++] = r;
751
752 ASSERT(i == kNumJSCallerSaved);
753 }
754 ASSERT(0 <= n && n < kNumJSCallerSaved);
755 return reg_code[n];
756}
757
758
759} } // namespace v8::internal