blob: 3cdb0157e7d9acbfbbe91cc9d6f41c8ab47e4a59 [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"
Steve Blocka7e24c12009-10-30 11:49:00 +000035
36namespace v8 {
37namespace internal {
38
Kristian Monsen80d68ea2010-09-08 11:05:35 +010039PcToCodeCache::PcToCodeCacheEntry
40 PcToCodeCache::cache_[PcToCodeCache::kPcToCodeCacheSize];
41
42int SafeStackFrameIterator::active_count_ = 0;
43
Steve Blocka7e24c12009-10-30 11:49:00 +000044// Iterator that supports traversing the stack handlers of a
45// particular frame. Needs to know the top of the handler chain.
46class 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
56 bool done() {
57 return handler_ == NULL || handler_->address() > limit_;
58 }
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),
74StackFrameIterator::StackFrameIterator()
75 : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
76 frame_(NULL), handler_(NULL), thread_(Top::GetCurrentThread()),
77 fp_(NULL), sp_(NULL), advance_(&StackFrameIterator::AdvanceWithHandler) {
78 Reset();
79}
80StackFrameIterator::StackFrameIterator(ThreadLocalTop* t)
81 : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
82 frame_(NULL), handler_(NULL), thread_(t),
83 fp_(NULL), sp_(NULL), advance_(&StackFrameIterator::AdvanceWithHandler) {
84 Reset();
85}
86StackFrameIterator::StackFrameIterator(bool use_top, Address fp, Address sp)
87 : STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
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 }
Steve Blocka7e24c12009-10-30 11:49:00 +000096}
97
98#undef INITIALIZE_SINGLETON
99
100
101void StackFrameIterator::AdvanceWithHandler() {
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
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
124void StackFrameIterator::AdvanceWithoutHandler() {
125 // A simpler version of Advance which doesn't care about handler.
126 ASSERT(!done());
127 StackFrame::State state;
128 StackFrame::Type type = frame_->GetCallerState(&state);
129 frame_ = SingletonFor(type, &state);
130}
131
132
133void 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);
Steve Blocka7e24c12009-10-30 11:49:00 +0000146 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100147 if (SingletonFor(type) == NULL) return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000148 frame_ = SingletonFor(type, &state);
149}
150
151
152StackFrame* StackFrameIterator::SingletonFor(StackFrame::Type type,
153 StackFrame::State* state) {
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
162StackFrame* StackFrameIterator::SingletonFor(StackFrame::Type type) {
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 }
172 return result;
173
174#undef FRAME_TYPE_CASE
175}
176
177
178// -------------------------------------------------------------------------
179
180
181StackTraceFrameIterator::StackTraceFrameIterator() {
Leon Clarke4515c472010-02-03 11:58:03 +0000182 if (!done() && !IsValidFrame()) Advance();
Steve Blocka7e24c12009-10-30 11:49:00 +0000183}
184
185
186void StackTraceFrameIterator::Advance() {
187 while (true) {
188 JavaScriptFrameIterator::Advance();
189 if (done()) return;
Leon Clarke4515c472010-02-03 11:58:03 +0000190 if (IsValidFrame()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000191 }
192}
193
Leon Clarke4515c472010-02-03 11:58:03 +0000194bool 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000202
203// -------------------------------------------------------------------------
204
205
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100206bool 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000219SafeStackFrameIterator::SafeStackFrameIterator(
220 Address fp, Address sp, Address low_bound, Address high_bound) :
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100221 maintainer_(),
222 stack_validator_(low_bound, high_bound),
223 is_valid_top_(IsValidTop(low_bound, high_bound)),
Steve Blocka7e24c12009-10-30 11:49:00 +0000224 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) {
228}
229
230
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100231bool 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000239void SafeStackFrameIterator::Advance() {
240 ASSERT(is_working_iterator_);
241 ASSERT(!done());
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
245 iteration_done_ = !IsValidFrame(last_frame) ||
246 !CanIterateHandles(last_frame, iterator_.handler()) ||
247 !IsValidCaller(last_frame);
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
258bool 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
267bool SafeStackFrameIterator::IsValidFrame(StackFrame* frame) const {
268 return IsValidStackAddress(frame->sp()) && IsValidStackAddress(frame->fp());
269}
270
271
272bool SafeStackFrameIterator::IsValidCaller(StackFrame* frame) {
273 StackFrame::State state;
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);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100280 ExitFrameValidator validator(stack_validator_);
281 if (!validator.IsValidFP(caller_fp)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000282 } 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 }
292 frame->ComputeCallerState(&state);
293 return IsValidStackAddress(state.sp) && IsValidStackAddress(state.fp) &&
294 iterator_.SingletonFor(frame->GetCallerState(&state)) != NULL;
295}
296
297
298void SafeStackFrameIterator::Reset() {
299 if (is_working_iterator_) {
300 iterator_.Reset();
301 iteration_done_ = false;
302 }
303}
304
305
306// -------------------------------------------------------------------------
307
308
309#ifdef ENABLE_LOGGING_AND_PROFILING
310SafeStackTraceFrameIterator::SafeStackTraceFrameIterator(
311 Address fp, Address sp, Address low_bound, Address high_bound) :
312 SafeJavaScriptFrameIterator(fp, sp, low_bound, high_bound) {
313 if (!done() && !frame()->is_java_script()) Advance();
314}
315
316
317void SafeStackTraceFrameIterator::Advance() {
318 while (true) {
319 SafeJavaScriptFrameIterator::Advance();
320 if (done()) return;
321 if (frame()->is_java_script()) return;
322 }
323}
324#endif
325
326
Steve Blocka7e24c12009-10-30 11:49:00 +0000327bool StackFrame::HasHandler() const {
328 StackHandlerIterator it(this, top_handler());
329 return !it.done();
330}
331
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100332void 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;
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000345}
346
347
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100348StackFrame::Type StackFrame::ComputeType(State* state) {
349 ASSERT(state->fp != NULL);
350 if (StandardFrame::IsArgumentsAdaptorFrame(state->fp)) {
351 return ARGUMENTS_ADAPTOR;
Steve Blocka7e24c12009-10-30 11:49:00 +0000352 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100353 // 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());
Steve Blocka7e24c12009-10-30 11:49:00 +0000360}
361
362
Steve Blocka7e24c12009-10-30 11:49:00 +0000363
364StackFrame::Type StackFrame::GetCallerState(State* state) const {
365 ComputeCallerState(state);
366 return ComputeType(state);
367}
368
369
Iain Merrick75681382010-08-19 15:07:18 +0100370Code* EntryFrame::unchecked_code() const {
371 return Heap::raw_unchecked_js_entry_code();
Steve Blocka7e24c12009-10-30 11:49:00 +0000372}
373
374
375void EntryFrame::ComputeCallerState(State* state) const {
376 GetCallerState(state);
377}
378
379
Steve Block6ded16b2010-05-10 14:33:55 +0100380void EntryFrame::SetCallerFp(Address caller_fp) {
381 const int offset = EntryFrameConstants::kCallerFPOffset;
382 Memory::Address_at(this->fp() + offset) = caller_fp;
383}
384
385
Steve Blocka7e24c12009-10-30 11:49:00 +0000386StackFrame::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
Iain Merrick75681382010-08-19 15:07:18 +0100393Code* EntryConstructFrame::unchecked_code() const {
394 return Heap::raw_unchecked_js_construct_entry_code();
Steve Blocka7e24c12009-10-30 11:49:00 +0000395}
396
397
Steve Blockd0582a62009-12-15 09:54:21 +0000398Object*& ExitFrame::code_slot() const {
399 const int offset = ExitFrameConstants::kCodeOffset;
400 return Memory::Object_at(fp() + offset);
401}
402
403
Iain Merrick75681382010-08-19 15:07:18 +0100404Code* ExitFrame::unchecked_code() const {
405 return reinterpret_cast<Code*>(code_slot());
Steve Blocka7e24c12009-10-30 11:49:00 +0000406}
407
408
409void ExitFrame::ComputeCallerState(State* state) const {
410 // Setup the caller state.
411 state->sp = caller_sp();
412 state->fp = Memory::Address_at(fp() + ExitFrameConstants::kCallerFPOffset);
413 state->pc_address
414 = reinterpret_cast<Address*>(fp() + ExitFrameConstants::kCallerPCOffset);
415}
416
417
Steve Block6ded16b2010-05-10 14:33:55 +0100418void ExitFrame::SetCallerFp(Address caller_fp) {
419 Memory::Address_at(fp() + ExitFrameConstants::kCallerFPOffset) = caller_fp;
420}
421
422
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100423void 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000431Address ExitFrame::GetCallerStackPointer() const {
432 return fp() + ExitFrameConstants::kCallerSPDisplacement;
433}
434
435
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100436StackFrame::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
445void 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000452Address StandardFrame::GetExpressionAddress(int n) const {
453 const int offset = StandardFrameConstants::kExpressionsOffset;
454 return fp() + offset - n * kPointerSize;
455}
456
457
458int 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.
Steve Blockd0582a62009-12-15 09:54:21 +0000465 return static_cast<int>((base - limit) / kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000466}
467
468
469void StandardFrame::ComputeCallerState(State* state) const {
470 state->sp = caller_sp();
471 state->fp = caller_fp();
472 state->pc_address = reinterpret_cast<Address*>(ComputePCAddress(fp()));
473}
474
475
Steve Block6ded16b2010-05-10 14:33:55 +0100476void StandardFrame::SetCallerFp(Address caller_fp) {
477 Memory::Address_at(fp() + StandardFrameConstants::kCallerFPOffset) =
478 caller_fp;
479}
480
481
Steve Blocka7e24c12009-10-30 11:49:00 +0000482bool 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
491Object* JavaScriptFrame::GetParameter(int index) const {
492 ASSERT(index >= 0 && index < ComputeParametersCount());
493 const int offset = JavaScriptFrameConstants::kParam0Offset;
494 return Memory::Object_at(caller_sp() + offset - (index * kPointerSize));
495}
496
497
498int JavaScriptFrame::ComputeParametersCount() const {
499 Address base = caller_sp() + JavaScriptFrameConstants::kReceiverOffset;
500 Address limit = fp() + JavaScriptFrameConstants::kSavedRegistersOffset;
Steve Blockd0582a62009-12-15 09:54:21 +0000501 return static_cast<int>((base - limit) / kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000502}
503
504
505bool JavaScriptFrame::IsConstructor() const {
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);
512}
513
514
Iain Merrick75681382010-08-19 15:07:18 +0100515Code* JavaScriptFrame::unchecked_code() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000516 JSFunction* function = JSFunction::cast(this->function());
Iain Merrick75681382010-08-19 15:07:18 +0100517 return function->unchecked_code();
Steve Blocka7e24c12009-10-30 11:49:00 +0000518}
519
520
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100521int JavaScriptFrame::GetProvidedParametersCount() const {
522 return ComputeParametersCount();
523}
524
525
526Address 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
550Address 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
557Address 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
Iain Merrick75681382010-08-19 15:07:18 +0100564Code* ArgumentsAdaptorFrame::unchecked_code() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000565 return Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline);
566}
567
568
Iain Merrick75681382010-08-19 15:07:18 +0100569Code* InternalFrame::unchecked_code() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000570 const int offset = InternalFrameConstants::kCodeOffset;
571 Object* code = Memory::Object_at(fp() + offset);
572 ASSERT(code != NULL);
Iain Merrick75681382010-08-19 15:07:18 +0100573 return reinterpret_cast<Code*>(code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000574}
575
576
577void StackFrame::PrintIndex(StringStream* accumulator,
578 PrintMode mode,
579 int index) {
580 accumulator->Add((mode == OVERVIEW) ? "%5d: " : "[%d]: ", index);
581}
582
583
584void 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);
Steve Block6ded16b2010-05-10 14:33:55 +0100596
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100597 Handle<SerializedScopeInfo> scope_info(SerializedScopeInfo::Empty());
598
Steve Block6ded16b2010-05-10 14:33:55 +0100599 if (function->IsJSFunction()) {
600 Handle<SharedFunctionInfo> shared(JSFunction::cast(function)->shared());
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100601 scope_info = Handle<SerializedScopeInfo>(shared->scope_info());
Steve Block6ded16b2010-05-10 14:33:55 +0100602 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 &&
Leon Clarkeac952652010-07-15 11:15:24 +0100610 pc >= code->instruction_start() && pc < code->instruction_end()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100611 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000624 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.
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100629 ScopeInfo<PreallocatedStorage> info(*scope_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000630
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.
701 int expressions_start = stack_locals_count;
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
722void 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
754void 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());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100759 handler->Iterate(v, code());
Steve Blocka7e24c12009-10-30 11:49:00 +0000760#ifdef DEBUG
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100761 // Make sure that the entry frame does not contain more than one
762 // stack handler.
Steve Blocka7e24c12009-10-30 11:49:00 +0000763 it.Advance();
764 ASSERT(it.done());
765#endif
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100766 IteratePc(v, pc_address(), code());
Steve Blocka7e24c12009-10-30 11:49:00 +0000767}
768
769
770void 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.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100783 handler->Iterate(v, code());
Steve Blocka7e24c12009-10-30 11:49:00 +0000784 }
785 v->VisitPointers(base, limit);
786}
787
788
789void JavaScriptFrame::Iterate(ObjectVisitor* v) const {
790 IterateExpressions(v);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100791 IteratePc(v, pc_address(), code());
Steve Blocka7e24c12009-10-30 11:49:00 +0000792
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);
797 Object** limit = &Memory::Object_at(caller_sp() + kLimitOffset) + 1;
798 v->VisitPointers(base, limit);
799}
800
801
802void 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);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100806 IteratePc(v, pc_address(), code());
Steve Blocka7e24c12009-10-30 11:49:00 +0000807}
808
809
810// -------------------------------------------------------------------------
811
812
813JavaScriptFrame* 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
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100828Code* 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
835Code* 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
854PcToCodeCache::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
Steve Blocka7e24c12009-10-30 11:49:00 +0000878int 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
888int 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
Steve Block6ded16b2010-05-10 14:33:55 +0100905#define DEFINE_WRAPPER(type, field) \
906class field##_Wrapper : public ZoneObject { \
907 public: /* NOLINT */ \
908 field##_Wrapper(const field& original) : frame_(original) { \
909 } \
910 field frame_; \
911};
912STACK_FRAME_TYPE_LIST(DEFINE_WRAPPER)
913#undef DEFINE_WRAPPER
914
915static 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
931Vector<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
Steve Blocka7e24c12009-10-30 11:49:00 +0000941} } // namespace v8::internal