blob: d81d5afaaa7370f4198afb8881d91a6bfd73c020 [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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
Ben Murdochb0fe1622011-05-05 13:52:32 +010030#include "ast.h"
31#include "deoptimizer.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "frames-inl.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010033#include "full-codegen.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "mark-compact.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010035#include "safepoint-table.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036#include "scopeinfo.h"
37#include "string-stream.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000038
39namespace v8 {
40namespace internal {
41
42// Iterator that supports traversing the stack handlers of a
43// particular frame. Needs to know the top of the handler chain.
44class StackHandlerIterator BASE_EMBEDDED {
45 public:
46 StackHandlerIterator(const StackFrame* frame, StackHandler* handler)
47 : limit_(frame->fp()), handler_(handler) {
48 // Make sure the handler has already been unwound to this frame.
49 ASSERT(frame->sp() <= handler->address());
50 }
51
52 StackHandler* handler() const { return handler_; }
53
54 bool done() {
55 return handler_ == NULL || handler_->address() > limit_;
56 }
57 void Advance() {
58 ASSERT(!done());
59 handler_ = handler_->next();
60 }
61
62 private:
63 const Address limit_;
64 StackHandler* handler_;
65};
66
67
68// -------------------------------------------------------------------------
69
70
71#define INITIALIZE_SINGLETON(type, field) field##_(this),
72StackFrameIterator::StackFrameIterator()
Ben Murdoch8b112d22011-06-08 16:22:53 +010073 : isolate_(Isolate::Current()),
74 STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
Steve Block44f0eee2011-05-26 01:26:41 +010075 frame_(NULL), handler_(NULL),
Ben Murdoch8b112d22011-06-08 16:22:53 +010076 thread_(isolate_->thread_local_top()),
Steve Blocka7e24c12009-10-30 11:49:00 +000077 fp_(NULL), sp_(NULL), advance_(&StackFrameIterator::AdvanceWithHandler) {
78 Reset();
79}
Ben Murdoch8b112d22011-06-08 16:22:53 +010080StackFrameIterator::StackFrameIterator(Isolate* isolate)
81 : isolate_(isolate),
82 STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
83 frame_(NULL), handler_(NULL),
84 thread_(isolate_->thread_local_top()),
85 fp_(NULL), sp_(NULL), advance_(&StackFrameIterator::AdvanceWithHandler) {
86 Reset();
87}
88StackFrameIterator::StackFrameIterator(Isolate* isolate, ThreadLocalTop* t)
89 : isolate_(isolate),
90 STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
Steve Blocka7e24c12009-10-30 11:49:00 +000091 frame_(NULL), handler_(NULL), thread_(t),
92 fp_(NULL), sp_(NULL), advance_(&StackFrameIterator::AdvanceWithHandler) {
93 Reset();
94}
Steve Block44f0eee2011-05-26 01:26:41 +010095StackFrameIterator::StackFrameIterator(Isolate* isolate,
96 bool use_top, Address fp, Address sp)
Ben Murdoch8b112d22011-06-08 16:22:53 +010097 : isolate_(isolate),
98 STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
Steve Blocka7e24c12009-10-30 11:49:00 +000099 frame_(NULL), handler_(NULL),
Ben Murdoch8b112d22011-06-08 16:22:53 +0100100 thread_(use_top ? isolate_->thread_local_top() : NULL),
Steve Blocka7e24c12009-10-30 11:49:00 +0000101 fp_(use_top ? NULL : fp), sp_(sp),
102 advance_(use_top ? &StackFrameIterator::AdvanceWithHandler :
103 &StackFrameIterator::AdvanceWithoutHandler) {
104 if (use_top || fp != NULL) {
105 Reset();
106 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000107}
108
109#undef INITIALIZE_SINGLETON
110
111
112void StackFrameIterator::AdvanceWithHandler() {
113 ASSERT(!done());
114 // Compute the state of the calling frame before restoring
115 // callee-saved registers and unwinding handlers. This allows the
116 // frame code that computes the caller state to access the top
117 // handler and the value of any callee-saved register if needed.
118 StackFrame::State state;
119 StackFrame::Type type = frame_->GetCallerState(&state);
120
121 // Unwind handlers corresponding to the current frame.
122 StackHandlerIterator it(frame_, handler_);
123 while (!it.done()) it.Advance();
124 handler_ = it.handler();
125
126 // Advance to the calling frame.
127 frame_ = SingletonFor(type, &state);
128
129 // When we're done iterating over the stack frames, the handler
130 // chain must have been completely unwound.
131 ASSERT(!done() || handler_ == NULL);
132}
133
134
135void StackFrameIterator::AdvanceWithoutHandler() {
136 // A simpler version of Advance which doesn't care about handler.
137 ASSERT(!done());
138 StackFrame::State state;
139 StackFrame::Type type = frame_->GetCallerState(&state);
140 frame_ = SingletonFor(type, &state);
141}
142
143
144void StackFrameIterator::Reset() {
145 StackFrame::State state;
146 StackFrame::Type type;
147 if (thread_ != NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100148 type = ExitFrame::GetStateForFramePointer(
149 Isolate::c_entry_fp(thread_), &state);
150 handler_ = StackHandler::FromAddress(
151 Isolate::handler(thread_));
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 } else {
153 ASSERT(fp_ != NULL);
154 state.fp = fp_;
155 state.sp = sp_;
156 state.pc_address =
157 reinterpret_cast<Address*>(StandardFrame::ComputePCAddress(fp_));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100158 type = StackFrame::ComputeType(isolate(), &state);
Steve Blocka7e24c12009-10-30 11:49:00 +0000159 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100160 if (SingletonFor(type) == NULL) return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000161 frame_ = SingletonFor(type, &state);
162}
163
164
165StackFrame* StackFrameIterator::SingletonFor(StackFrame::Type type,
166 StackFrame::State* state) {
167 if (type == StackFrame::NONE) return NULL;
168 StackFrame* result = SingletonFor(type);
169 ASSERT(result != NULL);
170 result->state_ = *state;
171 return result;
172}
173
174
175StackFrame* StackFrameIterator::SingletonFor(StackFrame::Type type) {
176#define FRAME_TYPE_CASE(type, field) \
177 case StackFrame::type: result = &field##_; break;
178
179 StackFrame* result = NULL;
180 switch (type) {
181 case StackFrame::NONE: return NULL;
182 STACK_FRAME_TYPE_LIST(FRAME_TYPE_CASE)
183 default: break;
184 }
185 return result;
186
187#undef FRAME_TYPE_CASE
188}
189
190
191// -------------------------------------------------------------------------
192
193
194StackTraceFrameIterator::StackTraceFrameIterator() {
Leon Clarke4515c472010-02-03 11:58:03 +0000195 if (!done() && !IsValidFrame()) Advance();
Steve Blocka7e24c12009-10-30 11:49:00 +0000196}
197
198
Ben Murdoch8b112d22011-06-08 16:22:53 +0100199StackTraceFrameIterator::StackTraceFrameIterator(Isolate* isolate)
200 : JavaScriptFrameIterator(isolate) {
201 if (!done() && !IsValidFrame()) Advance();
202}
203
204
Steve Blocka7e24c12009-10-30 11:49:00 +0000205void StackTraceFrameIterator::Advance() {
206 while (true) {
207 JavaScriptFrameIterator::Advance();
208 if (done()) return;
Leon Clarke4515c472010-02-03 11:58:03 +0000209 if (IsValidFrame()) return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000210 }
211}
212
Leon Clarke4515c472010-02-03 11:58:03 +0000213bool StackTraceFrameIterator::IsValidFrame() {
214 if (!frame()->function()->IsJSFunction()) return false;
215 Object* script = JSFunction::cast(frame()->function())->shared()->script();
216 // Don't show functions from native scripts to user.
217 return (script->IsScript() &&
218 Script::TYPE_NATIVE != Script::cast(script)->type()->value());
219}
220
Steve Blocka7e24c12009-10-30 11:49:00 +0000221
222// -------------------------------------------------------------------------
223
224
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100225bool SafeStackFrameIterator::ExitFrameValidator::IsValidFP(Address fp) {
226 if (!validator_.IsValid(fp)) return false;
227 Address sp = ExitFrame::ComputeStackPointer(fp);
228 if (!validator_.IsValid(sp)) return false;
229 StackFrame::State state;
230 ExitFrame::FillState(fp, sp, &state);
231 if (!validator_.IsValid(reinterpret_cast<Address>(state.pc_address))) {
232 return false;
233 }
234 return *state.pc_address != NULL;
235}
236
237
Ben Murdoch8b112d22011-06-08 16:22:53 +0100238SafeStackFrameIterator::ActiveCountMaintainer::ActiveCountMaintainer(
239 Isolate* isolate)
240 : isolate_(isolate) {
241 isolate_->set_safe_stack_iterator_counter(
242 isolate_->safe_stack_iterator_counter() + 1);
243}
244
245
246SafeStackFrameIterator::ActiveCountMaintainer::~ActiveCountMaintainer() {
247 isolate_->set_safe_stack_iterator_counter(
248 isolate_->safe_stack_iterator_counter() - 1);
249}
250
251
Steve Blocka7e24c12009-10-30 11:49:00 +0000252SafeStackFrameIterator::SafeStackFrameIterator(
Steve Block44f0eee2011-05-26 01:26:41 +0100253 Isolate* isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 Address fp, Address sp, Address low_bound, Address high_bound) :
Ben Murdoch8b112d22011-06-08 16:22:53 +0100255 maintainer_(isolate),
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100256 stack_validator_(low_bound, high_bound),
Steve Block44f0eee2011-05-26 01:26:41 +0100257 is_valid_top_(IsValidTop(isolate, low_bound, high_bound)),
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 is_valid_fp_(IsWithinBounds(low_bound, high_bound, fp)),
259 is_working_iterator_(is_valid_top_ || is_valid_fp_),
260 iteration_done_(!is_working_iterator_),
Steve Block44f0eee2011-05-26 01:26:41 +0100261 iterator_(isolate, is_valid_top_, is_valid_fp_ ? fp : NULL, sp) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000262}
263
Ben Murdoch8b112d22011-06-08 16:22:53 +0100264bool SafeStackFrameIterator::is_active(Isolate* isolate) {
265 return isolate->safe_stack_iterator_counter() > 0;
266}
267
Steve Blocka7e24c12009-10-30 11:49:00 +0000268
Steve Block44f0eee2011-05-26 01:26:41 +0100269bool SafeStackFrameIterator::IsValidTop(Isolate* isolate,
270 Address low_bound, Address high_bound) {
271 ThreadLocalTop* top = isolate->thread_local_top();
272 Address fp = Isolate::c_entry_fp(top);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100273 ExitFrameValidator validator(low_bound, high_bound);
274 if (!validator.IsValidFP(fp)) return false;
Steve Block44f0eee2011-05-26 01:26:41 +0100275 return Isolate::handler(top) != NULL;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100276}
277
278
Steve Blocka7e24c12009-10-30 11:49:00 +0000279void SafeStackFrameIterator::Advance() {
280 ASSERT(is_working_iterator_);
281 ASSERT(!done());
282 StackFrame* last_frame = iterator_.frame();
283 Address last_sp = last_frame->sp(), last_fp = last_frame->fp();
284 // Before advancing to the next stack frame, perform pointer validity tests
285 iteration_done_ = !IsValidFrame(last_frame) ||
286 !CanIterateHandles(last_frame, iterator_.handler()) ||
287 !IsValidCaller(last_frame);
288 if (iteration_done_) return;
289
290 iterator_.Advance();
291 if (iterator_.done()) return;
292 // Check that we have actually moved to the previous frame in the stack
293 StackFrame* prev_frame = iterator_.frame();
294 iteration_done_ = prev_frame->sp() < last_sp || prev_frame->fp() < last_fp;
295}
296
297
298bool SafeStackFrameIterator::CanIterateHandles(StackFrame* frame,
299 StackHandler* handler) {
300 // If StackIterator iterates over StackHandles, verify that
301 // StackHandlerIterator can be instantiated (see StackHandlerIterator
302 // constructor.)
303 return !is_valid_top_ || (frame->sp() <= handler->address());
304}
305
306
307bool SafeStackFrameIterator::IsValidFrame(StackFrame* frame) const {
308 return IsValidStackAddress(frame->sp()) && IsValidStackAddress(frame->fp());
309}
310
311
312bool SafeStackFrameIterator::IsValidCaller(StackFrame* frame) {
313 StackFrame::State state;
314 if (frame->is_entry() || frame->is_entry_construct()) {
315 // See EntryFrame::GetCallerState. It computes the caller FP address
316 // and calls ExitFrame::GetStateForFramePointer on it. We need to be
317 // sure that caller FP address is valid.
318 Address caller_fp = Memory::Address_at(
319 frame->fp() + EntryFrameConstants::kCallerFPOffset);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100320 ExitFrameValidator validator(stack_validator_);
321 if (!validator.IsValidFP(caller_fp)) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000322 } else if (frame->is_arguments_adaptor()) {
323 // See ArgumentsAdaptorFrame::GetCallerStackPointer. It assumes that
324 // the number of arguments is stored on stack as Smi. We need to check
325 // that it really an Smi.
326 Object* number_of_args = reinterpret_cast<ArgumentsAdaptorFrame*>(frame)->
327 GetExpression(0);
328 if (!number_of_args->IsSmi()) {
329 return false;
330 }
331 }
332 frame->ComputeCallerState(&state);
333 return IsValidStackAddress(state.sp) && IsValidStackAddress(state.fp) &&
334 iterator_.SingletonFor(frame->GetCallerState(&state)) != NULL;
335}
336
337
338void SafeStackFrameIterator::Reset() {
339 if (is_working_iterator_) {
340 iterator_.Reset();
341 iteration_done_ = false;
342 }
343}
344
345
346// -------------------------------------------------------------------------
347
348
349#ifdef ENABLE_LOGGING_AND_PROFILING
350SafeStackTraceFrameIterator::SafeStackTraceFrameIterator(
Steve Block44f0eee2011-05-26 01:26:41 +0100351 Isolate* isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +0000352 Address fp, Address sp, Address low_bound, Address high_bound) :
Steve Block44f0eee2011-05-26 01:26:41 +0100353 SafeJavaScriptFrameIterator(isolate, fp, sp, low_bound, high_bound) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 if (!done() && !frame()->is_java_script()) Advance();
355}
356
357
358void SafeStackTraceFrameIterator::Advance() {
359 while (true) {
360 SafeJavaScriptFrameIterator::Advance();
361 if (done()) return;
362 if (frame()->is_java_script()) return;
363 }
364}
365#endif
366
367
Ben Murdoch8b112d22011-06-08 16:22:53 +0100368Code* StackFrame::GetSafepointData(Isolate* isolate,
369 Address pc,
Ben Murdochb8e0da22011-05-16 14:20:40 +0100370 SafepointEntry* safepoint_entry,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100371 unsigned* stack_slots) {
Steve Block44f0eee2011-05-26 01:26:41 +0100372 PcToCodeCache::PcToCodeCacheEntry* entry =
373 isolate->pc_to_code_cache()->GetCacheEntry(pc);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100374 SafepointEntry cached_safepoint_entry = entry->safepoint_entry;
375 if (!entry->safepoint_entry.is_valid()) {
376 entry->safepoint_entry = entry->code->GetSafepointEntry(pc);
377 ASSERT(entry->safepoint_entry.is_valid());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100378 } else {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100379 ASSERT(entry->safepoint_entry.Equals(entry->code->GetSafepointEntry(pc)));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100380 }
381
382 // Fill in the results and return the code.
383 Code* code = entry->code;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100384 *safepoint_entry = entry->safepoint_entry;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100385 *stack_slots = code->stack_slots();
386 return code;
387}
388
389
Steve Blocka7e24c12009-10-30 11:49:00 +0000390bool StackFrame::HasHandler() const {
391 StackHandlerIterator it(this, top_handler());
392 return !it.done();
393}
394
Ben Murdochb0fe1622011-05-05 13:52:32 +0100395
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100396void StackFrame::IteratePc(ObjectVisitor* v,
397 Address* pc_address,
398 Code* holder) {
399 Address pc = *pc_address;
400 ASSERT(holder->contains(pc));
401 unsigned pc_offset = static_cast<unsigned>(pc - holder->instruction_start());
402 Object* code = holder;
403 v->VisitPointer(&code);
404 if (code != holder) {
405 holder = reinterpret_cast<Code*>(code);
406 pc = holder->instruction_start() + pc_offset;
407 *pc_address = pc;
Steve Blocka7e24c12009-10-30 11:49:00 +0000408 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000409}
410
411
Ben Murdoch8b112d22011-06-08 16:22:53 +0100412StackFrame::Type StackFrame::ComputeType(Isolate* isolate, State* state) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100413 ASSERT(state->fp != NULL);
414 if (StandardFrame::IsArgumentsAdaptorFrame(state->fp)) {
415 return ARGUMENTS_ADAPTOR;
Steve Blocka7e24c12009-10-30 11:49:00 +0000416 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100417 // The marker and function offsets overlap. If the marker isn't a
418 // smi then the frame is a JavaScript frame -- and the marker is
419 // really the function.
420 const int offset = StandardFrameConstants::kMarkerOffset;
421 Object* marker = Memory::Object_at(state->fp + offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100422 if (!marker->IsSmi()) {
423 // If we're using a "safe" stack iterator, we treat optimized
424 // frames as normal JavaScript frames to avoid having to look
425 // into the heap to determine the state. This is safe as long
426 // as nobody tries to GC...
Ben Murdoch8b112d22011-06-08 16:22:53 +0100427 if (SafeStackFrameIterator::is_active(isolate)) return JAVA_SCRIPT;
428 Code::Kind kind = GetContainingCode(isolate, *(state->pc_address))->kind();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100429 ASSERT(kind == Code::FUNCTION || kind == Code::OPTIMIZED_FUNCTION);
430 return (kind == Code::OPTIMIZED_FUNCTION) ? OPTIMIZED : JAVA_SCRIPT;
431 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100432 return static_cast<StackFrame::Type>(Smi::cast(marker)->value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000433}
434
435
Steve Blocka7e24c12009-10-30 11:49:00 +0000436
437StackFrame::Type StackFrame::GetCallerState(State* state) const {
438 ComputeCallerState(state);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100439 return ComputeType(isolate(), state);
Steve Blocka7e24c12009-10-30 11:49:00 +0000440}
441
442
Iain Merrick75681382010-08-19 15:07:18 +0100443Code* EntryFrame::unchecked_code() const {
Steve Block44f0eee2011-05-26 01:26:41 +0100444 return HEAP->raw_unchecked_js_entry_code();
Steve Blocka7e24c12009-10-30 11:49:00 +0000445}
446
447
448void EntryFrame::ComputeCallerState(State* state) const {
449 GetCallerState(state);
450}
451
452
Steve Block6ded16b2010-05-10 14:33:55 +0100453void EntryFrame::SetCallerFp(Address caller_fp) {
454 const int offset = EntryFrameConstants::kCallerFPOffset;
455 Memory::Address_at(this->fp() + offset) = caller_fp;
456}
457
458
Steve Blocka7e24c12009-10-30 11:49:00 +0000459StackFrame::Type EntryFrame::GetCallerState(State* state) const {
460 const int offset = EntryFrameConstants::kCallerFPOffset;
461 Address fp = Memory::Address_at(this->fp() + offset);
462 return ExitFrame::GetStateForFramePointer(fp, state);
463}
464
465
Iain Merrick75681382010-08-19 15:07:18 +0100466Code* EntryConstructFrame::unchecked_code() const {
Steve Block44f0eee2011-05-26 01:26:41 +0100467 return HEAP->raw_unchecked_js_construct_entry_code();
Steve Blocka7e24c12009-10-30 11:49:00 +0000468}
469
470
Steve Blockd0582a62009-12-15 09:54:21 +0000471Object*& ExitFrame::code_slot() const {
472 const int offset = ExitFrameConstants::kCodeOffset;
473 return Memory::Object_at(fp() + offset);
474}
475
476
Iain Merrick75681382010-08-19 15:07:18 +0100477Code* ExitFrame::unchecked_code() const {
478 return reinterpret_cast<Code*>(code_slot());
Steve Blocka7e24c12009-10-30 11:49:00 +0000479}
480
481
482void ExitFrame::ComputeCallerState(State* state) const {
483 // Setup the caller state.
484 state->sp = caller_sp();
485 state->fp = Memory::Address_at(fp() + ExitFrameConstants::kCallerFPOffset);
486 state->pc_address
487 = reinterpret_cast<Address*>(fp() + ExitFrameConstants::kCallerPCOffset);
488}
489
490
Steve Block6ded16b2010-05-10 14:33:55 +0100491void ExitFrame::SetCallerFp(Address caller_fp) {
492 Memory::Address_at(fp() + ExitFrameConstants::kCallerFPOffset) = caller_fp;
493}
494
495
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100496void ExitFrame::Iterate(ObjectVisitor* v) const {
497 // The arguments are traversed as part of the expression stack of
498 // the calling frame.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100499 IteratePc(v, pc_address(), LookupCode());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100500 v->VisitPointer(&code_slot());
501}
502
503
Steve Blocka7e24c12009-10-30 11:49:00 +0000504Address ExitFrame::GetCallerStackPointer() const {
505 return fp() + ExitFrameConstants::kCallerSPDisplacement;
506}
507
508
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100509StackFrame::Type ExitFrame::GetStateForFramePointer(Address fp, State* state) {
510 if (fp == 0) return NONE;
511 Address sp = ComputeStackPointer(fp);
512 FillState(fp, sp, state);
513 ASSERT(*state->pc_address != NULL);
514 return EXIT;
515}
516
517
518void ExitFrame::FillState(Address fp, Address sp, State* state) {
519 state->sp = sp;
520 state->fp = fp;
521 state->pc_address = reinterpret_cast<Address*>(sp - 1 * kPointerSize);
522}
523
524
Steve Blocka7e24c12009-10-30 11:49:00 +0000525Address StandardFrame::GetExpressionAddress(int n) const {
526 const int offset = StandardFrameConstants::kExpressionsOffset;
527 return fp() + offset - n * kPointerSize;
528}
529
530
531int StandardFrame::ComputeExpressionsCount() const {
532 const int offset =
533 StandardFrameConstants::kExpressionsOffset + kPointerSize;
534 Address base = fp() + offset;
535 Address limit = sp();
536 ASSERT(base >= limit); // stack grows downwards
537 // Include register-allocated locals in number of expressions.
Steve Blockd0582a62009-12-15 09:54:21 +0000538 return static_cast<int>((base - limit) / kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000539}
540
541
542void StandardFrame::ComputeCallerState(State* state) const {
543 state->sp = caller_sp();
544 state->fp = caller_fp();
545 state->pc_address = reinterpret_cast<Address*>(ComputePCAddress(fp()));
546}
547
548
Steve Block6ded16b2010-05-10 14:33:55 +0100549void StandardFrame::SetCallerFp(Address caller_fp) {
550 Memory::Address_at(fp() + StandardFrameConstants::kCallerFPOffset) =
551 caller_fp;
552}
553
554
Steve Blocka7e24c12009-10-30 11:49:00 +0000555bool StandardFrame::IsExpressionInsideHandler(int n) const {
556 Address address = GetExpressionAddress(n);
557 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
558 if (it.handler()->includes(address)) return true;
559 }
560 return false;
561}
562
563
Ben Murdochb0fe1622011-05-05 13:52:32 +0100564void OptimizedFrame::Iterate(ObjectVisitor* v) const {
565#ifdef DEBUG
566 // Make sure that optimized frames do not contain any stack handlers.
567 StackHandlerIterator it(this, top_handler());
568 ASSERT(it.done());
569#endif
570
571 // Make sure that we're not doing "safe" stack frame iteration. We cannot
572 // possibly find pointers in optimized frames in that state.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100573 ASSERT(!SafeStackFrameIterator::is_active(isolate()));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100574
575 // Compute the safepoint information.
576 unsigned stack_slots = 0;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100577 SafepointEntry safepoint_entry;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100578 Code* code = StackFrame::GetSafepointData(
Ben Murdoch8b112d22011-06-08 16:22:53 +0100579 isolate(), pc(), &safepoint_entry, &stack_slots);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100580 unsigned slot_space = stack_slots * kPointerSize;
581
Ben Murdoch8b112d22011-06-08 16:22:53 +0100582 // Visit the outgoing parameters.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100583 Object** parameters_base = &Memory::Object_at(sp());
584 Object** parameters_limit = &Memory::Object_at(
585 fp() + JavaScriptFrameConstants::kFunctionOffset - slot_space);
586
Ben Murdochb8e0da22011-05-16 14:20:40 +0100587 // Visit the parameters that may be on top of the saved registers.
588 if (safepoint_entry.argument_count() > 0) {
589 v->VisitPointers(parameters_base,
590 parameters_base + safepoint_entry.argument_count());
591 parameters_base += safepoint_entry.argument_count();
592 }
593
594 // Skip saved double registers.
595 if (safepoint_entry.has_doubles()) {
596 parameters_base += DoubleRegister::kNumAllocatableRegisters *
597 kDoubleSize / kPointerSize;
598 }
599
Ben Murdochb0fe1622011-05-05 13:52:32 +0100600 // Visit the registers that contain pointers if any.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100601 if (safepoint_entry.HasRegisters()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100602 for (int i = kNumSafepointRegisters - 1; i >=0; i--) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100603 if (safepoint_entry.HasRegisterAt(i)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100604 int reg_stack_index = MacroAssembler::SafepointRegisterStackIndex(i);
605 v->VisitPointer(parameters_base + reg_stack_index);
606 }
607 }
608 // Skip the words containing the register values.
609 parameters_base += kNumSafepointRegisters;
610 }
611
612 // We're done dealing with the register bits.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100613 uint8_t* safepoint_bits = safepoint_entry.bits();
614 safepoint_bits += kNumSafepointRegisters >> kBitsPerByteLog2;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100615
616 // Visit the rest of the parameters.
617 v->VisitPointers(parameters_base, parameters_limit);
618
619 // Visit pointer spill slots and locals.
620 for (unsigned index = 0; index < stack_slots; index++) {
621 int byte_index = index >> kBitsPerByteLog2;
622 int bit_index = index & (kBitsPerByte - 1);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100623 if ((safepoint_bits[byte_index] & (1U << bit_index)) != 0) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100624 v->VisitPointer(parameters_limit + index);
625 }
626 }
627
628 // Visit the context and the function.
629 Object** fixed_base = &Memory::Object_at(
630 fp() + JavaScriptFrameConstants::kFunctionOffset);
631 Object** fixed_limit = &Memory::Object_at(fp());
632 v->VisitPointers(fixed_base, fixed_limit);
633
634 // Visit the return address in the callee and incoming arguments.
635 IteratePc(v, pc_address(), code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000636}
637
638
639bool JavaScriptFrame::IsConstructor() const {
640 Address fp = caller_fp();
641 if (has_adapted_arguments()) {
642 // Skip the arguments adaptor frame and look at the real caller.
643 fp = Memory::Address_at(fp + StandardFrameConstants::kCallerFPOffset);
644 }
645 return IsConstructFrame(fp);
646}
647
648
Iain Merrick75681382010-08-19 15:07:18 +0100649Code* JavaScriptFrame::unchecked_code() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000650 JSFunction* function = JSFunction::cast(this->function());
Iain Merrick75681382010-08-19 15:07:18 +0100651 return function->unchecked_code();
Steve Blocka7e24c12009-10-30 11:49:00 +0000652}
653
654
Ben Murdoch8b112d22011-06-08 16:22:53 +0100655int JavaScriptFrame::GetNumberOfIncomingArguments() const {
656 ASSERT(!SafeStackFrameIterator::is_active(isolate()) &&
657 isolate()->heap()->gc_state() == Heap::NOT_IN_GC);
658
659 JSFunction* function = JSFunction::cast(this->function());
660 return function->shared()->formal_parameter_count();
661}
662
663
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100664Address JavaScriptFrame::GetCallerStackPointer() const {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100665 return fp() + StandardFrameConstants::kCallerSPOffset;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100666}
667
668
Ben Murdochb0fe1622011-05-05 13:52:32 +0100669void JavaScriptFrame::GetFunctions(List<JSFunction*>* functions) {
670 ASSERT(functions->length() == 0);
671 functions->Add(JSFunction::cast(function()));
672}
673
674
675void JavaScriptFrame::Summarize(List<FrameSummary>* functions) {
676 ASSERT(functions->length() == 0);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100677 Code* code_pointer = LookupCode();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100678 int offset = static_cast<int>(pc() - code_pointer->address());
679 FrameSummary summary(receiver(),
680 JSFunction::cast(function()),
681 code_pointer,
682 offset,
683 IsConstructor());
684 functions->Add(summary);
685}
686
687
688void FrameSummary::Print() {
689 PrintF("receiver: ");
690 receiver_->ShortPrint();
691 PrintF("\nfunction: ");
692 function_->shared()->DebugName()->ShortPrint();
693 PrintF("\ncode: ");
694 code_->ShortPrint();
695 if (code_->kind() == Code::FUNCTION) PrintF(" NON-OPT");
696 if (code_->kind() == Code::OPTIMIZED_FUNCTION) PrintF(" OPT");
697 PrintF("\npc: %d\n", offset_);
698}
699
700
701void OptimizedFrame::Summarize(List<FrameSummary>* frames) {
702 ASSERT(frames->length() == 0);
703 ASSERT(is_optimized());
704
Steve Block1e0659c2011-05-24 12:43:12 +0100705 int deopt_index = Safepoint::kNoDeoptimizationIndex;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100706 DeoptimizationInputData* data = GetDeoptimizationData(&deopt_index);
707
708 // BUG(3243555): Since we don't have a lazy-deopt registered at
709 // throw-statements, we can't use the translation at the call-site of
710 // throw. An entry with no deoptimization index indicates a call-site
711 // without a lazy-deopt. As a consequence we are not allowed to inline
712 // functions containing throw.
713 if (deopt_index == Safepoint::kNoDeoptimizationIndex) {
714 JavaScriptFrame::Summarize(frames);
715 return;
716 }
717
718 TranslationIterator it(data->TranslationByteArray(),
719 data->TranslationIndex(deopt_index)->value());
720 Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next());
721 ASSERT(opcode == Translation::BEGIN);
722 int frame_count = it.Next();
723
724 // We create the summary in reverse order because the frames
725 // in the deoptimization translation are ordered bottom-to-top.
726 int i = frame_count;
727 while (i > 0) {
728 opcode = static_cast<Translation::Opcode>(it.Next());
729 if (opcode == Translation::FRAME) {
730 // We don't inline constructor calls, so only the first, outermost
731 // frame can be a constructor frame in case of inlining.
732 bool is_constructor = (i == frame_count) && IsConstructor();
733
734 i--;
735 int ast_id = it.Next();
736 int function_id = it.Next();
737 it.Next(); // Skip height.
738 JSFunction* function =
739 JSFunction::cast(data->LiteralArray()->get(function_id));
740
741 // The translation commands are ordered and the receiver is always
742 // at the first position. Since we are always at a call when we need
743 // to construct a stack trace, the receiver is always in a stack slot.
744 opcode = static_cast<Translation::Opcode>(it.Next());
Ben Murdoch257744e2011-11-30 15:57:28 +0000745 ASSERT(opcode == Translation::STACK_SLOT ||
746 opcode == Translation::LITERAL);
747 int index = it.Next();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100748
749 // Get the correct receiver in the optimized frame.
750 Object* receiver = NULL;
Ben Murdoch257744e2011-11-30 15:57:28 +0000751 if (opcode == Translation::LITERAL) {
752 receiver = data->LiteralArray()->get(index);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100753 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +0000754 // Positive index means the value is spilled to the locals
755 // area. Negative means it is stored in the incoming parameter
756 // area.
757 if (index >= 0) {
758 receiver = GetExpression(index);
759 } else {
760 // Index -1 overlaps with last parameter, -n with the first parameter,
761 // (-n - 1) with the receiver with n being the number of parameters
762 // of the outermost, optimized frame.
763 int parameter_count = ComputeParametersCount();
764 int parameter_index = index + parameter_count;
765 receiver = (parameter_index == -1)
766 ? this->receiver()
767 : this->GetParameter(parameter_index);
768 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100769 }
770
771 Code* code = function->shared()->code();
772 DeoptimizationOutputData* output_data =
773 DeoptimizationOutputData::cast(code->deoptimization_data());
774 unsigned entry = Deoptimizer::GetOutputInfo(output_data,
775 ast_id,
776 function->shared());
777 unsigned pc_offset =
778 FullCodeGenerator::PcField::decode(entry) + Code::kHeaderSize;
779 ASSERT(pc_offset > 0);
780
781 FrameSummary summary(receiver, function, code, pc_offset, is_constructor);
782 frames->Add(summary);
783 } else {
784 // Skip over operands to advance to the next opcode.
785 it.Skip(Translation::NumberOfOperandsFor(opcode));
786 }
787 }
788}
789
790
791DeoptimizationInputData* OptimizedFrame::GetDeoptimizationData(
792 int* deopt_index) {
793 ASSERT(is_optimized());
794
795 JSFunction* opt_function = JSFunction::cast(function());
796 Code* code = opt_function->code();
797
798 // The code object may have been replaced by lazy deoptimization. Fall
799 // back to a slow search in this case to find the original optimized
800 // code object.
801 if (!code->contains(pc())) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100802 code = isolate()->pc_to_code_cache()->GcSafeFindCodeForPc(pc());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100803 }
804 ASSERT(code != NULL);
805 ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION);
806
Ben Murdochb8e0da22011-05-16 14:20:40 +0100807 SafepointEntry safepoint_entry = code->GetSafepointEntry(pc());
808 *deopt_index = safepoint_entry.deoptimization_index();
Steve Block1e0659c2011-05-24 12:43:12 +0100809 ASSERT(*deopt_index != Safepoint::kNoDeoptimizationIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100810
811 return DeoptimizationInputData::cast(code->deoptimization_data());
812}
813
814
815void OptimizedFrame::GetFunctions(List<JSFunction*>* functions) {
816 ASSERT(functions->length() == 0);
817 ASSERT(is_optimized());
818
Steve Block1e0659c2011-05-24 12:43:12 +0100819 int deopt_index = Safepoint::kNoDeoptimizationIndex;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100820 DeoptimizationInputData* data = GetDeoptimizationData(&deopt_index);
821
822 TranslationIterator it(data->TranslationByteArray(),
823 data->TranslationIndex(deopt_index)->value());
824 Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next());
825 ASSERT(opcode == Translation::BEGIN);
826 int frame_count = it.Next();
827
828 // We insert the frames in reverse order because the frames
829 // in the deoptimization translation are ordered bottom-to-top.
830 while (frame_count > 0) {
831 opcode = static_cast<Translation::Opcode>(it.Next());
832 if (opcode == Translation::FRAME) {
833 frame_count--;
834 it.Next(); // Skip ast id.
835 int function_id = it.Next();
836 it.Next(); // Skip height.
837 JSFunction* function =
838 JSFunction::cast(data->LiteralArray()->get(function_id));
839 functions->Add(function);
840 } else {
841 // Skip over operands to advance to the next opcode.
842 it.Skip(Translation::NumberOfOperandsFor(opcode));
843 }
844 }
845}
846
847
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100848Address ArgumentsAdaptorFrame::GetCallerStackPointer() const {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100849 return fp() + StandardFrameConstants::kCallerSPOffset;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100850}
851
852
853Address InternalFrame::GetCallerStackPointer() const {
854 // Internal frames have no arguments. The stack pointer of the
855 // caller is at a fixed offset from the frame pointer.
856 return fp() + StandardFrameConstants::kCallerSPOffset;
857}
858
859
Iain Merrick75681382010-08-19 15:07:18 +0100860Code* ArgumentsAdaptorFrame::unchecked_code() const {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100861 return isolate()->builtins()->builtin(
Steve Block44f0eee2011-05-26 01:26:41 +0100862 Builtins::kArgumentsAdaptorTrampoline);
Steve Blocka7e24c12009-10-30 11:49:00 +0000863}
864
865
Iain Merrick75681382010-08-19 15:07:18 +0100866Code* InternalFrame::unchecked_code() const {
Steve Blocka7e24c12009-10-30 11:49:00 +0000867 const int offset = InternalFrameConstants::kCodeOffset;
868 Object* code = Memory::Object_at(fp() + offset);
869 ASSERT(code != NULL);
Iain Merrick75681382010-08-19 15:07:18 +0100870 return reinterpret_cast<Code*>(code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000871}
872
873
874void StackFrame::PrintIndex(StringStream* accumulator,
875 PrintMode mode,
876 int index) {
877 accumulator->Add((mode == OVERVIEW) ? "%5d: " : "[%d]: ", index);
878}
879
880
881void JavaScriptFrame::Print(StringStream* accumulator,
882 PrintMode mode,
883 int index) const {
884 HandleScope scope;
885 Object* receiver = this->receiver();
886 Object* function = this->function();
887
888 accumulator->PrintSecurityTokenIfChanged(function);
889 PrintIndex(accumulator, mode, index);
890 Code* code = NULL;
891 if (IsConstructor()) accumulator->Add("new ");
892 accumulator->PrintFunction(function, receiver, &code);
Steve Block6ded16b2010-05-10 14:33:55 +0100893
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100894 Handle<SerializedScopeInfo> scope_info(SerializedScopeInfo::Empty());
895
Steve Block6ded16b2010-05-10 14:33:55 +0100896 if (function->IsJSFunction()) {
897 Handle<SharedFunctionInfo> shared(JSFunction::cast(function)->shared());
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100898 scope_info = Handle<SerializedScopeInfo>(shared->scope_info());
Steve Block6ded16b2010-05-10 14:33:55 +0100899 Object* script_obj = shared->script();
900 if (script_obj->IsScript()) {
901 Handle<Script> script(Script::cast(script_obj));
902 accumulator->Add(" [");
903 accumulator->PrintName(script->name());
904
905 Address pc = this->pc();
906 if (code != NULL && code->kind() == Code::FUNCTION &&
Leon Clarkeac952652010-07-15 11:15:24 +0100907 pc >= code->instruction_start() && pc < code->instruction_end()) {
Steve Block6ded16b2010-05-10 14:33:55 +0100908 int source_pos = code->SourcePosition(pc);
909 int line = GetScriptLineNumberSafe(script, source_pos) + 1;
910 accumulator->Add(":%d", line);
911 } else {
912 int function_start_pos = shared->start_position();
913 int line = GetScriptLineNumberSafe(script, function_start_pos) + 1;
914 accumulator->Add(":~%d", line);
915 }
916
917 accumulator->Add("] ");
918 }
919 }
920
Steve Blocka7e24c12009-10-30 11:49:00 +0000921 accumulator->Add("(this=%o", receiver);
922
923 // Get scope information for nicer output, if possible. If code is
924 // NULL, or doesn't contain scope info, info will return 0 for the
925 // number of parameters, stack slots, or context slots.
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100926 ScopeInfo<PreallocatedStorage> info(*scope_info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000927
928 // Print the parameters.
929 int parameters_count = ComputeParametersCount();
930 for (int i = 0; i < parameters_count; i++) {
931 accumulator->Add(",");
932 // If we have a name for the parameter we print it. Nameless
933 // parameters are either because we have more actual parameters
934 // than formal parameters or because we have no scope information.
935 if (i < info.number_of_parameters()) {
936 accumulator->PrintName(*info.parameter_name(i));
937 accumulator->Add("=");
938 }
939 accumulator->Add("%o", GetParameter(i));
940 }
941
942 accumulator->Add(")");
943 if (mode == OVERVIEW) {
944 accumulator->Add("\n");
945 return;
946 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000947 if (is_optimized()) {
948 accumulator->Add(" {\n// optimized frame\n}\n");
949 return;
950 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000951 accumulator->Add(" {\n");
952
953 // Compute the number of locals and expression stack elements.
954 int stack_locals_count = info.number_of_stack_slots();
955 int heap_locals_count = info.number_of_context_slots();
956 int expressions_count = ComputeExpressionsCount();
957
958 // Print stack-allocated local variables.
959 if (stack_locals_count > 0) {
960 accumulator->Add(" // stack-allocated locals\n");
961 }
962 for (int i = 0; i < stack_locals_count; i++) {
963 accumulator->Add(" var ");
964 accumulator->PrintName(*info.stack_slot_name(i));
965 accumulator->Add(" = ");
966 if (i < expressions_count) {
967 accumulator->Add("%o", GetExpression(i));
968 } else {
969 accumulator->Add("// no expression found - inconsistent frame?");
970 }
971 accumulator->Add("\n");
972 }
973
974 // Try to get hold of the context of this frame.
975 Context* context = NULL;
976 if (this->context() != NULL && this->context()->IsContext()) {
977 context = Context::cast(this->context());
978 }
979
980 // Print heap-allocated local variables.
981 if (heap_locals_count > Context::MIN_CONTEXT_SLOTS) {
982 accumulator->Add(" // heap-allocated locals\n");
983 }
984 for (int i = Context::MIN_CONTEXT_SLOTS; i < heap_locals_count; i++) {
985 accumulator->Add(" var ");
986 accumulator->PrintName(*info.context_slot_name(i));
987 accumulator->Add(" = ");
988 if (context != NULL) {
989 if (i < context->length()) {
990 accumulator->Add("%o", context->get(i));
991 } else {
992 accumulator->Add(
993 "// warning: missing context slot - inconsistent frame?");
994 }
995 } else {
996 accumulator->Add("// warning: no context found - inconsistent frame?");
997 }
998 accumulator->Add("\n");
999 }
1000
1001 // Print the expression stack.
1002 int expressions_start = stack_locals_count;
1003 if (expressions_start < expressions_count) {
1004 accumulator->Add(" // expression stack (top to bottom)\n");
1005 }
1006 for (int i = expressions_count - 1; i >= expressions_start; i--) {
1007 if (IsExpressionInsideHandler(i)) continue;
1008 accumulator->Add(" [%02d] : %o\n", i, GetExpression(i));
1009 }
1010
1011 // Print details about the function.
1012 if (FLAG_max_stack_trace_source_length != 0 && code != NULL) {
1013 SharedFunctionInfo* shared = JSFunction::cast(function)->shared();
1014 accumulator->Add("--------- s o u r c e c o d e ---------\n");
1015 shared->SourceCodePrint(accumulator, FLAG_max_stack_trace_source_length);
1016 accumulator->Add("\n-----------------------------------------\n");
1017 }
1018
1019 accumulator->Add("}\n\n");
1020}
1021
1022
1023void ArgumentsAdaptorFrame::Print(StringStream* accumulator,
1024 PrintMode mode,
1025 int index) const {
1026 int actual = ComputeParametersCount();
1027 int expected = -1;
1028 Object* function = this->function();
1029 if (function->IsJSFunction()) {
1030 expected = JSFunction::cast(function)->shared()->formal_parameter_count();
1031 }
1032
1033 PrintIndex(accumulator, mode, index);
1034 accumulator->Add("arguments adaptor frame: %d->%d", actual, expected);
1035 if (mode == OVERVIEW) {
1036 accumulator->Add("\n");
1037 return;
1038 }
1039 accumulator->Add(" {\n");
1040
1041 // Print actual arguments.
1042 if (actual > 0) accumulator->Add(" // actual arguments\n");
1043 for (int i = 0; i < actual; i++) {
1044 accumulator->Add(" [%02d] : %o", i, GetParameter(i));
1045 if (expected != -1 && i >= expected) {
1046 accumulator->Add(" // not passed to callee");
1047 }
1048 accumulator->Add("\n");
1049 }
1050
1051 accumulator->Add("}\n\n");
1052}
1053
1054
1055void EntryFrame::Iterate(ObjectVisitor* v) const {
1056 StackHandlerIterator it(this, top_handler());
1057 ASSERT(!it.done());
1058 StackHandler* handler = it.handler();
1059 ASSERT(handler->is_entry());
Ben Murdoch8b112d22011-06-08 16:22:53 +01001060 handler->Iterate(v, LookupCode());
Steve Blocka7e24c12009-10-30 11:49:00 +00001061#ifdef DEBUG
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001062 // Make sure that the entry frame does not contain more than one
1063 // stack handler.
Steve Blocka7e24c12009-10-30 11:49:00 +00001064 it.Advance();
1065 ASSERT(it.done());
1066#endif
Ben Murdoch8b112d22011-06-08 16:22:53 +01001067 IteratePc(v, pc_address(), LookupCode());
Steve Blocka7e24c12009-10-30 11:49:00 +00001068}
1069
1070
1071void StandardFrame::IterateExpressions(ObjectVisitor* v) const {
1072 const int offset = StandardFrameConstants::kContextOffset;
1073 Object** base = &Memory::Object_at(sp());
1074 Object** limit = &Memory::Object_at(fp() + offset) + 1;
1075 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
1076 StackHandler* handler = it.handler();
1077 // Traverse pointers down to - but not including - the next
1078 // handler in the handler chain. Update the base to skip the
1079 // handler and allow the handler to traverse its own pointers.
1080 const Address address = handler->address();
1081 v->VisitPointers(base, reinterpret_cast<Object**>(address));
1082 base = reinterpret_cast<Object**>(address + StackHandlerConstants::kSize);
1083 // Traverse the pointers in the handler itself.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001084 handler->Iterate(v, LookupCode());
Steve Blocka7e24c12009-10-30 11:49:00 +00001085 }
1086 v->VisitPointers(base, limit);
1087}
1088
1089
1090void JavaScriptFrame::Iterate(ObjectVisitor* v) const {
1091 IterateExpressions(v);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001092 IteratePc(v, pc_address(), LookupCode());
Steve Blocka7e24c12009-10-30 11:49:00 +00001093}
1094
1095
1096void InternalFrame::Iterate(ObjectVisitor* v) const {
1097 // Internal frames only have object pointers on the expression stack
1098 // as they never have any arguments.
1099 IterateExpressions(v);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001100 IteratePc(v, pc_address(), LookupCode());
Steve Blocka7e24c12009-10-30 11:49:00 +00001101}
1102
1103
1104// -------------------------------------------------------------------------
1105
1106
1107JavaScriptFrame* StackFrameLocator::FindJavaScriptFrame(int n) {
1108 ASSERT(n >= 0);
1109 for (int i = 0; i <= n; i++) {
1110 while (!iterator_.frame()->is_java_script()) iterator_.Advance();
1111 if (i == n) return JavaScriptFrame::cast(iterator_.frame());
1112 iterator_.Advance();
1113 }
1114 UNREACHABLE();
1115 return NULL;
1116}
1117
1118
1119// -------------------------------------------------------------------------
1120
1121
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001122Code* PcToCodeCache::GcSafeCastToCode(HeapObject* object, Address pc) {
1123 Code* code = reinterpret_cast<Code*>(object);
1124 ASSERT(code != NULL && code->contains(pc));
1125 return code;
1126}
1127
1128
1129Code* PcToCodeCache::GcSafeFindCodeForPc(Address pc) {
Steve Block44f0eee2011-05-26 01:26:41 +01001130 Heap* heap = isolate_->heap();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001131 // Check if the pc points into a large object chunk.
Steve Block44f0eee2011-05-26 01:26:41 +01001132 LargeObjectChunk* chunk = heap->lo_space()->FindChunkContainingPc(pc);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001133 if (chunk != NULL) return GcSafeCastToCode(chunk->GetObject(), pc);
1134
1135 // Iterate through the 8K page until we reach the end or find an
1136 // object starting after the pc.
1137 Page* page = Page::FromAddress(pc);
Steve Block44f0eee2011-05-26 01:26:41 +01001138 HeapObjectIterator iterator(page, heap->GcSafeSizeOfOldObjectFunction());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001139 HeapObject* previous = NULL;
1140 while (true) {
1141 HeapObject* next = iterator.next();
1142 if (next == NULL || next->address() >= pc) {
1143 return GcSafeCastToCode(previous, pc);
1144 }
1145 previous = next;
1146 }
1147}
1148
Ben Murdochb0fe1622011-05-05 13:52:32 +01001149
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001150PcToCodeCache::PcToCodeCacheEntry* PcToCodeCache::GetCacheEntry(Address pc) {
Steve Block44f0eee2011-05-26 01:26:41 +01001151 isolate_->counters()->pc_to_code()->Increment();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001152 ASSERT(IsPowerOf2(kPcToCodeCacheSize));
1153 uint32_t hash = ComputeIntegerHash(
1154 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(pc)));
1155 uint32_t index = hash & (kPcToCodeCacheSize - 1);
1156 PcToCodeCacheEntry* entry = cache(index);
1157 if (entry->pc == pc) {
Steve Block44f0eee2011-05-26 01:26:41 +01001158 isolate_->counters()->pc_to_code_cached()->Increment();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001159 ASSERT(entry->code == GcSafeFindCodeForPc(pc));
1160 } else {
1161 // Because this code may be interrupted by a profiling signal that
1162 // also queries the cache, we cannot update pc before the code has
1163 // been set. Otherwise, we risk trying to use a cache entry before
1164 // the code has been computed.
1165 entry->code = GcSafeFindCodeForPc(pc);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001166 entry->safepoint_entry.Reset();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001167 entry->pc = pc;
1168 }
1169 return entry;
1170}
1171
1172
1173// -------------------------------------------------------------------------
1174
Steve Blocka7e24c12009-10-30 11:49:00 +00001175int NumRegs(RegList reglist) {
1176 int n = 0;
1177 while (reglist != 0) {
1178 n++;
1179 reglist &= reglist - 1; // clear one bit
1180 }
1181 return n;
1182}
1183
1184
Steve Block44f0eee2011-05-26 01:26:41 +01001185struct JSCallerSavedCodeData {
1186 JSCallerSavedCodeData() {
Steve Blocka7e24c12009-10-30 11:49:00 +00001187 int i = 0;
1188 for (int r = 0; r < kNumRegs; r++)
1189 if ((kJSCallerSaved & (1 << r)) != 0)
1190 reg_code[i++] = r;
1191
1192 ASSERT(i == kNumJSCallerSaved);
1193 }
Steve Block44f0eee2011-05-26 01:26:41 +01001194 int reg_code[kNumJSCallerSaved];
1195};
1196
1197
1198static const JSCallerSavedCodeData kCallerSavedCodeData;
1199
1200
1201int JSCallerSavedCode(int n) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001202 ASSERT(0 <= n && n < kNumJSCallerSaved);
Steve Block44f0eee2011-05-26 01:26:41 +01001203 return kCallerSavedCodeData.reg_code[n];
Steve Blocka7e24c12009-10-30 11:49:00 +00001204}
1205
1206
Steve Block6ded16b2010-05-10 14:33:55 +01001207#define DEFINE_WRAPPER(type, field) \
1208class field##_Wrapper : public ZoneObject { \
1209 public: /* NOLINT */ \
1210 field##_Wrapper(const field& original) : frame_(original) { \
1211 } \
1212 field frame_; \
1213};
1214STACK_FRAME_TYPE_LIST(DEFINE_WRAPPER)
1215#undef DEFINE_WRAPPER
1216
1217static StackFrame* AllocateFrameCopy(StackFrame* frame) {
1218#define FRAME_TYPE_CASE(type, field) \
1219 case StackFrame::type: { \
1220 field##_Wrapper* wrapper = \
1221 new field##_Wrapper(*(reinterpret_cast<field*>(frame))); \
1222 return &wrapper->frame_; \
1223 }
1224
1225 switch (frame->type()) {
1226 STACK_FRAME_TYPE_LIST(FRAME_TYPE_CASE)
1227 default: UNREACHABLE();
1228 }
1229#undef FRAME_TYPE_CASE
1230 return NULL;
1231}
1232
1233Vector<StackFrame*> CreateStackMap() {
1234 ZoneList<StackFrame*> list(10);
1235 for (StackFrameIterator it; !it.done(); it.Advance()) {
1236 StackFrame* frame = AllocateFrameCopy(it.frame());
1237 list.Add(frame);
1238 }
1239 return list.ToVector();
1240}
1241
1242
Steve Blocka7e24c12009-10-30 11:49:00 +00001243} } // namespace v8::internal