blob: 4e67463f1fb75c31e29d32d39480470944c58c06 [file] [log] [blame]
danno@chromium.org40cb8782011-05-25 07:58:50 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
kasperl@chromium.orga5551262010-12-07 12:49:48 +000030#include "ast.h"
31#include "deoptimizer.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "frames-inl.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000033#include "full-codegen.h"
kasperl@chromium.org061ef742009-02-27 12:16:20 +000034#include "mark-compact.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000035#include "safepoint-table.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036#include "scopeinfo.h"
37#include "string-stream.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
kasperl@chromium.org71affb52009-05-26 05:44:31 +000039namespace v8 {
40namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042// 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
ager@chromium.orgeadaf222009-06-16 09:43:10 +000054 bool done() {
55 return handler_ == NULL || handler_->address() > limit_;
56 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000057 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()
vegorov@chromium.org74f333b2011-04-06 11:17:46 +000073 : isolate_(Isolate::Current()),
74 STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000075 frame_(NULL), handler_(NULL),
vegorov@chromium.org74f333b2011-04-06 11:17:46 +000076 thread_(isolate_->thread_local_top()),
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000077 fp_(NULL), sp_(NULL), advance_(&StackFrameIterator::AdvanceWithHandler) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078 Reset();
79}
vegorov@chromium.org74f333b2011-04-06 11:17:46 +000080StackFrameIterator::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)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000091 frame_(NULL), handler_(NULL), thread_(t),
92 fp_(NULL), sp_(NULL), advance_(&StackFrameIterator::AdvanceWithHandler) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093 Reset();
94}
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000095StackFrameIterator::StackFrameIterator(Isolate* isolate,
96 bool use_top, Address fp, Address sp)
vegorov@chromium.org74f333b2011-04-06 11:17:46 +000097 : isolate_(isolate),
98 STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON)
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000099 frame_(NULL), handler_(NULL),
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000100 thread_(use_top ? isolate_->thread_local_top() : NULL),
ager@chromium.orgbb29dc92009-03-24 13:25:23 +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 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000107}
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000108
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000109#undef INITIALIZE_SINGLETON
110
111
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000112void StackFrameIterator::AdvanceWithHandler() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113 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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121 // 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
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000135void StackFrameIterator::AdvanceWithoutHandler() {
136 // A simpler version of Advance which doesn't care about handler.
137 ASSERT(!done());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138 StackFrame::State state;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000139 StackFrame::Type type = frame_->GetCallerState(&state);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140 frame_ = SingletonFor(type, &state);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000141}
142
143
144void StackFrameIterator::Reset() {
145 StackFrame::State state;
146 StackFrame::Type type;
147 if (thread_ != NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000148 type = ExitFrame::GetStateForFramePointer(
149 Isolate::c_entry_fp(thread_), &state);
150 handler_ = StackHandler::FromAddress(
151 Isolate::handler(thread_));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +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_));
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000158 type = StackFrame::ComputeType(isolate(), &state);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000159 }
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000160 if (SingletonFor(type) == NULL) return;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000161 frame_ = SingletonFor(type, &state);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162}
163
164
165StackFrame* StackFrameIterator::SingletonFor(StackFrame::Type type,
166 StackFrame::State* state) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000167 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) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000176#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 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 return result;
186
187#undef FRAME_TYPE_CASE
188}
189
190
191// -------------------------------------------------------------------------
192
193
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000194StackTraceFrameIterator::StackTraceFrameIterator() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000195 if (!done() && !IsValidFrame()) Advance();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000196}
197
198
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000199StackTraceFrameIterator::StackTraceFrameIterator(Isolate* isolate)
200 : JavaScriptFrameIterator(isolate) {
201 if (!done() && !IsValidFrame()) Advance();
202}
203
204
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000205void StackTraceFrameIterator::Advance() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 while (true) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000207 JavaScriptFrameIterator::Advance();
208 if (done()) return;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000209 if (IsValidFrame()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210 }
211}
212
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000222// -------------------------------------------------------------------------
223
224
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000225bool 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
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000238SafeStackFrameIterator::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
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000252SafeStackFrameIterator::SafeStackFrameIterator(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000253 Isolate* isolate,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000254 Address fp, Address sp, Address low_bound, Address high_bound) :
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000255 maintainer_(isolate),
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000256 stack_validator_(low_bound, high_bound),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000257 is_valid_top_(IsValidTop(isolate, low_bound, high_bound)),
ager@chromium.orgbb29dc92009-03-24 13:25:23 +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_),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000261 iterator_(isolate, is_valid_top_, is_valid_fp_ ? fp : NULL, sp) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000262}
263
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000264bool SafeStackFrameIterator::is_active(Isolate* isolate) {
265 return isolate->safe_stack_iterator_counter() > 0;
266}
267
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000268
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000269bool 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);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000273 ExitFrameValidator validator(low_bound, high_bound);
274 if (!validator.IsValidFP(fp)) return false;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000275 return Isolate::handler(top) != NULL;
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000276}
277
278
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000279void SafeStackFrameIterator::Advance() {
280 ASSERT(is_working_iterator_);
281 ASSERT(!done());
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000282 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
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000285 iteration_done_ = !IsValidFrame(last_frame) ||
286 !CanIterateHandles(last_frame, iterator_.handler()) ||
287 !IsValidCaller(last_frame);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000288 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
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000298bool 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
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000307bool SafeStackFrameIterator::IsValidFrame(StackFrame* frame) const {
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000308 return IsValidStackAddress(frame->sp()) && IsValidStackAddress(frame->fp());
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000309}
310
311
312bool SafeStackFrameIterator::IsValidCaller(StackFrame* frame) {
313 StackFrame::State state;
ager@chromium.org41826e72009-03-30 13:30:57 +0000314 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);
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000320 ExitFrameValidator validator(stack_validator_);
321 if (!validator.IsValidFP(caller_fp)) return false;
ager@chromium.org41826e72009-03-30 13:30:57 +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 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000332 frame->ComputeCallerState(&state);
333 return IsValidStackAddress(state.sp) && IsValidStackAddress(state.fp) &&
334 iterator_.SingletonFor(frame->GetCallerState(&state)) != NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335}
336
337
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000338void SafeStackFrameIterator::Reset() {
339 if (is_working_iterator_) {
340 iterator_.Reset();
341 iteration_done_ = false;
342 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343}
344
345
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000346// -------------------------------------------------------------------------
347
348
349#ifdef ENABLE_LOGGING_AND_PROFILING
350SafeStackTraceFrameIterator::SafeStackTraceFrameIterator(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000351 Isolate* isolate,
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000352 Address fp, Address sp, Address low_bound, Address high_bound) :
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000353 SafeJavaScriptFrameIterator(isolate, fp, sp, low_bound, high_bound) {
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000354 if (!done() && !frame()->is_java_script()) Advance();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355}
356
357
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000358void SafeStackTraceFrameIterator::Advance() {
359 while (true) {
360 SafeJavaScriptFrameIterator::Advance();
361 if (done()) return;
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000362 if (frame()->is_java_script()) return;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000363 }
364}
365#endif
366
367
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000368Code* StackFrame::GetSafepointData(Isolate* isolate,
369 Address pc,
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000370 SafepointEntry* safepoint_entry,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000371 unsigned* stack_slots) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000372 PcToCodeCache::PcToCodeCacheEntry* entry =
373 isolate->pc_to_code_cache()->GetCacheEntry(pc);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000374 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());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000378 } else {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000379 ASSERT(entry->safepoint_entry.Equals(entry->code->GetSafepointEntry(pc)));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000380 }
381
382 // Fill in the results and return the code.
383 Code* code = entry->code;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000384 *safepoint_entry = entry->safepoint_entry;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000385 *stack_slots = code->stack_slots();
386 return code;
387}
388
389
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390bool StackFrame::HasHandler() const {
391 StackHandlerIterator it(this, top_handler());
392 return !it.done();
393}
394
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000395
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000396void 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;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000408 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000409}
410
411
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000412StackFrame::Type StackFrame::ComputeType(Isolate* isolate, State* state) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000413 ASSERT(state->fp != NULL);
414 if (StandardFrame::IsArgumentsAdaptorFrame(state->fp)) {
415 return ARGUMENTS_ADAPTOR;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000417 // 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);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000422 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...
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000427 if (SafeStackFrameIterator::is_active(isolate)) return JAVA_SCRIPT;
428 Code::Kind kind = GetContainingCode(isolate, *(state->pc_address))->kind();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000429 ASSERT(kind == Code::FUNCTION || kind == Code::OPTIMIZED_FUNCTION);
430 return (kind == Code::OPTIMIZED_FUNCTION) ? OPTIMIZED : JAVA_SCRIPT;
431 }
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000432 return static_cast<StackFrame::Type>(Smi::cast(marker)->value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433}
434
435
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000437StackFrame::Type StackFrame::GetCallerState(State* state) const {
438 ComputeCallerState(state);
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000439 return ComputeType(isolate(), state);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000440}
441
442
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000443Code* EntryFrame::unchecked_code() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000444 return HEAP->raw_unchecked_js_entry_code();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445}
446
447
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000448void EntryFrame::ComputeCallerState(State* state) const {
449 GetCallerState(state);
450}
451
452
ager@chromium.org357bf652010-04-12 11:30:10 +0000453void EntryFrame::SetCallerFp(Address caller_fp) {
454 const int offset = EntryFrameConstants::kCallerFPOffset;
455 Memory::Address_at(this->fp() + offset) = caller_fp;
456}
457
458
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000466Code* EntryConstructFrame::unchecked_code() const {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000467 return HEAP->raw_unchecked_js_construct_entry_code();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468}
469
470
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000471Object*& ExitFrame::code_slot() const {
472 const int offset = ExitFrameConstants::kCodeOffset;
473 return Memory::Object_at(fp() + offset);
474}
475
476
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000477Code* ExitFrame::unchecked_code() const {
478 return reinterpret_cast<Code*>(code_slot());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479}
480
481
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000482void ExitFrame::ComputeCallerState(State* state) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483 // Setup the caller state.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000484 state->sp = caller_sp();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485 state->fp = Memory::Address_at(fp() + ExitFrameConstants::kCallerFPOffset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 state->pc_address
487 = reinterpret_cast<Address*>(fp() + ExitFrameConstants::kCallerPCOffset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488}
489
490
ager@chromium.org357bf652010-04-12 11:30:10 +0000491void ExitFrame::SetCallerFp(Address caller_fp) {
492 Memory::Address_at(fp() + ExitFrameConstants::kCallerFPOffset) = caller_fp;
493}
494
495
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000496void ExitFrame::Iterate(ObjectVisitor* v) const {
497 // The arguments are traversed as part of the expression stack of
498 // the calling frame.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000499 IteratePc(v, pc_address(), LookupCode());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000500 v->VisitPointer(&code_slot());
501}
502
503
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504Address ExitFrame::GetCallerStackPointer() const {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000505 return fp() + ExitFrameConstants::kCallerSPDisplacement;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506}
507
508
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000509StackFrame::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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525Address StandardFrame::GetExpressionAddress(int n) const {
kasper.lund7276f142008-07-30 08:49:36 +0000526 const int offset = StandardFrameConstants::kExpressionsOffset;
527 return fp() + offset - n * kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528}
529
530
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000531Object* StandardFrame::GetExpression(Address fp, int index) {
532 return Memory::Object_at(GetExpressionAddress(fp, index));
533}
534
535
536Address StandardFrame::GetExpressionAddress(Address fp, int n) {
537 const int offset = StandardFrameConstants::kExpressionsOffset;
538 return fp + offset - n * kPointerSize;
539}
540
541
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542int StandardFrame::ComputeExpressionsCount() const {
543 const int offset =
544 StandardFrameConstants::kExpressionsOffset + kPointerSize;
545 Address base = fp() + offset;
546 Address limit = sp();
547 ASSERT(base >= limit); // stack grows downwards
548 // Include register-allocated locals in number of expressions.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000549 return static_cast<int>((base - limit) / kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550}
551
552
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000553void StandardFrame::ComputeCallerState(State* state) const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554 state->sp = caller_sp();
555 state->fp = caller_fp();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556 state->pc_address = reinterpret_cast<Address*>(ComputePCAddress(fp()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557}
558
559
ager@chromium.org357bf652010-04-12 11:30:10 +0000560void StandardFrame::SetCallerFp(Address caller_fp) {
561 Memory::Address_at(fp() + StandardFrameConstants::kCallerFPOffset) =
562 caller_fp;
563}
564
565
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566bool StandardFrame::IsExpressionInsideHandler(int n) const {
567 Address address = GetExpressionAddress(n);
568 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
569 if (it.handler()->includes(address)) return true;
570 }
571 return false;
572}
573
574
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000575void OptimizedFrame::Iterate(ObjectVisitor* v) const {
576#ifdef DEBUG
577 // Make sure that optimized frames do not contain any stack handlers.
578 StackHandlerIterator it(this, top_handler());
579 ASSERT(it.done());
580#endif
581
582 // Make sure that we're not doing "safe" stack frame iteration. We cannot
583 // possibly find pointers in optimized frames in that state.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000584 ASSERT(!SafeStackFrameIterator::is_active(isolate()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000585
586 // Compute the safepoint information.
587 unsigned stack_slots = 0;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000588 SafepointEntry safepoint_entry;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000589 Code* code = StackFrame::GetSafepointData(
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000590 isolate(), pc(), &safepoint_entry, &stack_slots);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000591 unsigned slot_space = stack_slots * kPointerSize;
592
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000593 // Visit the outgoing parameters.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000594 Object** parameters_base = &Memory::Object_at(sp());
595 Object** parameters_limit = &Memory::Object_at(
596 fp() + JavaScriptFrameConstants::kFunctionOffset - slot_space);
597
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000598 // Visit the parameters that may be on top of the saved registers.
599 if (safepoint_entry.argument_count() > 0) {
600 v->VisitPointers(parameters_base,
601 parameters_base + safepoint_entry.argument_count());
602 parameters_base += safepoint_entry.argument_count();
603 }
604
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000605 // Skip saved double registers.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000606 if (safepoint_entry.has_doubles()) {
607 parameters_base += DoubleRegister::kNumAllocatableRegisters *
608 kDoubleSize / kPointerSize;
609 }
610
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000611 // Visit the registers that contain pointers if any.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000612 if (safepoint_entry.HasRegisters()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000613 for (int i = kNumSafepointRegisters - 1; i >=0; i--) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000614 if (safepoint_entry.HasRegisterAt(i)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000615 int reg_stack_index = MacroAssembler::SafepointRegisterStackIndex(i);
616 v->VisitPointer(parameters_base + reg_stack_index);
617 }
618 }
619 // Skip the words containing the register values.
620 parameters_base += kNumSafepointRegisters;
621 }
622
623 // We're done dealing with the register bits.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000624 uint8_t* safepoint_bits = safepoint_entry.bits();
625 safepoint_bits += kNumSafepointRegisters >> kBitsPerByteLog2;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000626
627 // Visit the rest of the parameters.
628 v->VisitPointers(parameters_base, parameters_limit);
629
630 // Visit pointer spill slots and locals.
631 for (unsigned index = 0; index < stack_slots; index++) {
632 int byte_index = index >> kBitsPerByteLog2;
633 int bit_index = index & (kBitsPerByte - 1);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000634 if ((safepoint_bits[byte_index] & (1U << bit_index)) != 0) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000635 v->VisitPointer(parameters_limit + index);
636 }
637 }
638
639 // Visit the context and the function.
640 Object** fixed_base = &Memory::Object_at(
641 fp() + JavaScriptFrameConstants::kFunctionOffset);
642 Object** fixed_limit = &Memory::Object_at(fp());
643 v->VisitPointers(fixed_base, fixed_limit);
644
645 // Visit the return address in the callee and incoming arguments.
646 IteratePc(v, pc_address(), code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647}
648
649
650bool JavaScriptFrame::IsConstructor() const {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000651 Address fp = caller_fp();
652 if (has_adapted_arguments()) {
653 // Skip the arguments adaptor frame and look at the real caller.
654 fp = Memory::Address_at(fp + StandardFrameConstants::kCallerFPOffset);
655 }
656 return IsConstructFrame(fp);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000657}
658
659
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000660int JavaScriptFrame::GetArgumentsLength() const {
661 // If there is an arguments adaptor frame get the arguments length from it.
662 if (has_adapted_arguments()) {
663 return Smi::cast(GetExpression(caller_fp(), 0))->value();
664 } else {
665 return GetNumberOfIncomingArguments();
666 }
667}
668
669
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000670Code* JavaScriptFrame::unchecked_code() const {
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000671 JSFunction* function = JSFunction::cast(this->function());
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000672 return function->unchecked_code();
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000673}
674
675
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000676int JavaScriptFrame::GetNumberOfIncomingArguments() const {
677 ASSERT(!SafeStackFrameIterator::is_active(isolate()) &&
678 isolate()->heap()->gc_state() == Heap::NOT_IN_GC);
679
680 JSFunction* function = JSFunction::cast(this->function());
681 return function->shared()->formal_parameter_count();
682}
683
684
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000685Address JavaScriptFrame::GetCallerStackPointer() const {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000686 return fp() + StandardFrameConstants::kCallerSPOffset;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000687}
688
689
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000690void JavaScriptFrame::GetFunctions(List<JSFunction*>* functions) {
691 ASSERT(functions->length() == 0);
692 functions->Add(JSFunction::cast(function()));
693}
694
695
696void JavaScriptFrame::Summarize(List<FrameSummary>* functions) {
697 ASSERT(functions->length() == 0);
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000698 Code* code_pointer = LookupCode();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000699 int offset = static_cast<int>(pc() - code_pointer->address());
700 FrameSummary summary(receiver(),
701 JSFunction::cast(function()),
702 code_pointer,
703 offset,
704 IsConstructor());
705 functions->Add(summary);
706}
707
708
709void FrameSummary::Print() {
710 PrintF("receiver: ");
711 receiver_->ShortPrint();
712 PrintF("\nfunction: ");
713 function_->shared()->DebugName()->ShortPrint();
714 PrintF("\ncode: ");
715 code_->ShortPrint();
716 if (code_->kind() == Code::FUNCTION) PrintF(" NON-OPT");
717 if (code_->kind() == Code::OPTIMIZED_FUNCTION) PrintF(" OPT");
718 PrintF("\npc: %d\n", offset_);
719}
720
721
722void OptimizedFrame::Summarize(List<FrameSummary>* frames) {
723 ASSERT(frames->length() == 0);
724 ASSERT(is_optimized());
725
ager@chromium.org378b34e2011-01-28 08:04:38 +0000726 int deopt_index = Safepoint::kNoDeoptimizationIndex;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000727 DeoptimizationInputData* data = GetDeoptimizationData(&deopt_index);
728
729 // BUG(3243555): Since we don't have a lazy-deopt registered at
730 // throw-statements, we can't use the translation at the call-site of
731 // throw. An entry with no deoptimization index indicates a call-site
732 // without a lazy-deopt. As a consequence we are not allowed to inline
733 // functions containing throw.
734 if (deopt_index == Safepoint::kNoDeoptimizationIndex) {
735 JavaScriptFrame::Summarize(frames);
736 return;
737 }
738
739 TranslationIterator it(data->TranslationByteArray(),
740 data->TranslationIndex(deopt_index)->value());
741 Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next());
742 ASSERT(opcode == Translation::BEGIN);
743 int frame_count = it.Next();
744
745 // We create the summary in reverse order because the frames
746 // in the deoptimization translation are ordered bottom-to-top.
747 int i = frame_count;
748 while (i > 0) {
749 opcode = static_cast<Translation::Opcode>(it.Next());
750 if (opcode == Translation::FRAME) {
751 // We don't inline constructor calls, so only the first, outermost
752 // frame can be a constructor frame in case of inlining.
753 bool is_constructor = (i == frame_count) && IsConstructor();
754
755 i--;
756 int ast_id = it.Next();
757 int function_id = it.Next();
758 it.Next(); // Skip height.
759 JSFunction* function =
760 JSFunction::cast(data->LiteralArray()->get(function_id));
761
762 // The translation commands are ordered and the receiver is always
763 // at the first position. Since we are always at a call when we need
764 // to construct a stack trace, the receiver is always in a stack slot.
765 opcode = static_cast<Translation::Opcode>(it.Next());
danno@chromium.org40cb8782011-05-25 07:58:50 +0000766 ASSERT(opcode == Translation::STACK_SLOT ||
767 opcode == Translation::LITERAL);
768 int index = it.Next();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000769
770 // Get the correct receiver in the optimized frame.
771 Object* receiver = NULL;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000772 if (opcode == Translation::LITERAL) {
773 receiver = data->LiteralArray()->get(index);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000774 } else {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000775 // Positive index means the value is spilled to the locals
776 // area. Negative means it is stored in the incoming parameter
777 // area.
778 if (index >= 0) {
779 receiver = GetExpression(index);
780 } else {
781 // Index -1 overlaps with last parameter, -n with the first parameter,
782 // (-n - 1) with the receiver with n being the number of parameters
783 // of the outermost, optimized frame.
784 int parameter_count = ComputeParametersCount();
785 int parameter_index = index + parameter_count;
786 receiver = (parameter_index == -1)
787 ? this->receiver()
788 : this->GetParameter(parameter_index);
789 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000790 }
791
792 Code* code = function->shared()->code();
793 DeoptimizationOutputData* output_data =
794 DeoptimizationOutputData::cast(code->deoptimization_data());
795 unsigned entry = Deoptimizer::GetOutputInfo(output_data,
796 ast_id,
797 function->shared());
798 unsigned pc_offset =
799 FullCodeGenerator::PcField::decode(entry) + Code::kHeaderSize;
800 ASSERT(pc_offset > 0);
801
802 FrameSummary summary(receiver, function, code, pc_offset, is_constructor);
803 frames->Add(summary);
804 } else {
805 // Skip over operands to advance to the next opcode.
806 it.Skip(Translation::NumberOfOperandsFor(opcode));
807 }
808 }
809}
810
811
812DeoptimizationInputData* OptimizedFrame::GetDeoptimizationData(
813 int* deopt_index) {
814 ASSERT(is_optimized());
815
816 JSFunction* opt_function = JSFunction::cast(function());
817 Code* code = opt_function->code();
818
819 // The code object may have been replaced by lazy deoptimization. Fall
820 // back to a slow search in this case to find the original optimized
821 // code object.
822 if (!code->contains(pc())) {
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000823 code = isolate()->pc_to_code_cache()->GcSafeFindCodeForPc(pc());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000824 }
825 ASSERT(code != NULL);
826 ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION);
827
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000828 SafepointEntry safepoint_entry = code->GetSafepointEntry(pc());
829 *deopt_index = safepoint_entry.deoptimization_index();
ager@chromium.org378b34e2011-01-28 08:04:38 +0000830 ASSERT(*deopt_index != Safepoint::kNoDeoptimizationIndex);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000831
832 return DeoptimizationInputData::cast(code->deoptimization_data());
833}
834
835
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000836int OptimizedFrame::GetInlineCount() {
837 ASSERT(is_optimized());
838
839 int deopt_index = Safepoint::kNoDeoptimizationIndex;
840 DeoptimizationInputData* data = GetDeoptimizationData(&deopt_index);
841
842 TranslationIterator it(data->TranslationByteArray(),
843 data->TranslationIndex(deopt_index)->value());
844 Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next());
845 ASSERT(opcode == Translation::BEGIN);
846 USE(opcode);
847 int frame_count = it.Next();
848 return frame_count;
849}
850
851
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000852void OptimizedFrame::GetFunctions(List<JSFunction*>* functions) {
853 ASSERT(functions->length() == 0);
854 ASSERT(is_optimized());
855
ager@chromium.org378b34e2011-01-28 08:04:38 +0000856 int deopt_index = Safepoint::kNoDeoptimizationIndex;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000857 DeoptimizationInputData* data = GetDeoptimizationData(&deopt_index);
858
859 TranslationIterator it(data->TranslationByteArray(),
860 data->TranslationIndex(deopt_index)->value());
861 Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next());
862 ASSERT(opcode == Translation::BEGIN);
863 int frame_count = it.Next();
864
865 // We insert the frames in reverse order because the frames
866 // in the deoptimization translation are ordered bottom-to-top.
867 while (frame_count > 0) {
868 opcode = static_cast<Translation::Opcode>(it.Next());
869 if (opcode == Translation::FRAME) {
870 frame_count--;
871 it.Next(); // Skip ast id.
872 int function_id = it.Next();
873 it.Next(); // Skip height.
874 JSFunction* function =
875 JSFunction::cast(data->LiteralArray()->get(function_id));
876 functions->Add(function);
877 } else {
878 // Skip over operands to advance to the next opcode.
879 it.Skip(Translation::NumberOfOperandsFor(opcode));
880 }
881 }
882}
883
884
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000885Address ArgumentsAdaptorFrame::GetCallerStackPointer() const {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000886 return fp() + StandardFrameConstants::kCallerSPOffset;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000887}
888
889
890Address InternalFrame::GetCallerStackPointer() const {
891 // Internal frames have no arguments. The stack pointer of the
892 // caller is at a fixed offset from the frame pointer.
893 return fp() + StandardFrameConstants::kCallerSPOffset;
894}
895
896
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000897Code* ArgumentsAdaptorFrame::unchecked_code() const {
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000898 return isolate()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000899 Builtins::kArgumentsAdaptorTrampoline);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900}
901
902
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000903Code* InternalFrame::unchecked_code() const {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000904 const int offset = InternalFrameConstants::kCodeOffset;
905 Object* code = Memory::Object_at(fp() + offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906 ASSERT(code != NULL);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000907 return reinterpret_cast<Code*>(code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908}
909
910
911void StackFrame::PrintIndex(StringStream* accumulator,
912 PrintMode mode,
913 int index) {
914 accumulator->Add((mode == OVERVIEW) ? "%5d: " : "[%d]: ", index);
915}
916
917
918void JavaScriptFrame::Print(StringStream* accumulator,
919 PrintMode mode,
920 int index) const {
921 HandleScope scope;
922 Object* receiver = this->receiver();
923 Object* function = this->function();
924
925 accumulator->PrintSecurityTokenIfChanged(function);
926 PrintIndex(accumulator, mode, index);
927 Code* code = NULL;
928 if (IsConstructor()) accumulator->Add("new ");
929 accumulator->PrintFunction(function, receiver, &code);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000930
ager@chromium.orgb5737492010-07-15 09:29:43 +0000931 Handle<SerializedScopeInfo> scope_info(SerializedScopeInfo::Empty());
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000932
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000933 if (function->IsJSFunction()) {
934 Handle<SharedFunctionInfo> shared(JSFunction::cast(function)->shared());
ager@chromium.orgb5737492010-07-15 09:29:43 +0000935 scope_info = Handle<SerializedScopeInfo>(shared->scope_info());
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000936 Object* script_obj = shared->script();
937 if (script_obj->IsScript()) {
938 Handle<Script> script(Script::cast(script_obj));
939 accumulator->Add(" [");
940 accumulator->PrintName(script->name());
941
942 Address pc = this->pc();
943 if (code != NULL && code->kind() == Code::FUNCTION &&
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +0000944 pc >= code->instruction_start() && pc < code->instruction_end()) {
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000945 int source_pos = code->SourcePosition(pc);
946 int line = GetScriptLineNumberSafe(script, source_pos) + 1;
947 accumulator->Add(":%d", line);
948 } else {
949 int function_start_pos = shared->start_position();
950 int line = GetScriptLineNumberSafe(script, function_start_pos) + 1;
951 accumulator->Add(":~%d", line);
952 }
953
954 accumulator->Add("] ");
955 }
956 }
957
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958 accumulator->Add("(this=%o", receiver);
959
960 // Get scope information for nicer output, if possible. If code is
961 // NULL, or doesn't contain scope info, info will return 0 for the
962 // number of parameters, stack slots, or context slots.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000963 ScopeInfo<PreallocatedStorage> info(*scope_info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964
965 // Print the parameters.
966 int parameters_count = ComputeParametersCount();
967 for (int i = 0; i < parameters_count; i++) {
968 accumulator->Add(",");
969 // If we have a name for the parameter we print it. Nameless
970 // parameters are either because we have more actual parameters
971 // than formal parameters or because we have no scope information.
972 if (i < info.number_of_parameters()) {
973 accumulator->PrintName(*info.parameter_name(i));
974 accumulator->Add("=");
975 }
976 accumulator->Add("%o", GetParameter(i));
977 }
978
979 accumulator->Add(")");
980 if (mode == OVERVIEW) {
981 accumulator->Add("\n");
982 return;
983 }
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000984 if (is_optimized()) {
985 accumulator->Add(" {\n// optimized frame\n}\n");
986 return;
987 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 accumulator->Add(" {\n");
989
990 // Compute the number of locals and expression stack elements.
991 int stack_locals_count = info.number_of_stack_slots();
992 int heap_locals_count = info.number_of_context_slots();
993 int expressions_count = ComputeExpressionsCount();
994
995 // Print stack-allocated local variables.
996 if (stack_locals_count > 0) {
997 accumulator->Add(" // stack-allocated locals\n");
998 }
999 for (int i = 0; i < stack_locals_count; i++) {
1000 accumulator->Add(" var ");
1001 accumulator->PrintName(*info.stack_slot_name(i));
1002 accumulator->Add(" = ");
1003 if (i < expressions_count) {
1004 accumulator->Add("%o", GetExpression(i));
1005 } else {
1006 accumulator->Add("// no expression found - inconsistent frame?");
1007 }
1008 accumulator->Add("\n");
1009 }
1010
1011 // Try to get hold of the context of this frame.
1012 Context* context = NULL;
1013 if (this->context() != NULL && this->context()->IsContext()) {
1014 context = Context::cast(this->context());
1015 }
1016
1017 // Print heap-allocated local variables.
1018 if (heap_locals_count > Context::MIN_CONTEXT_SLOTS) {
1019 accumulator->Add(" // heap-allocated locals\n");
1020 }
1021 for (int i = Context::MIN_CONTEXT_SLOTS; i < heap_locals_count; i++) {
1022 accumulator->Add(" var ");
1023 accumulator->PrintName(*info.context_slot_name(i));
1024 accumulator->Add(" = ");
1025 if (context != NULL) {
1026 if (i < context->length()) {
1027 accumulator->Add("%o", context->get(i));
1028 } else {
1029 accumulator->Add(
1030 "// warning: missing context slot - inconsistent frame?");
1031 }
1032 } else {
1033 accumulator->Add("// warning: no context found - inconsistent frame?");
1034 }
1035 accumulator->Add("\n");
1036 }
1037
1038 // Print the expression stack.
kasper.lund7276f142008-07-30 08:49:36 +00001039 int expressions_start = stack_locals_count;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001040 if (expressions_start < expressions_count) {
1041 accumulator->Add(" // expression stack (top to bottom)\n");
1042 }
1043 for (int i = expressions_count - 1; i >= expressions_start; i--) {
1044 if (IsExpressionInsideHandler(i)) continue;
1045 accumulator->Add(" [%02d] : %o\n", i, GetExpression(i));
1046 }
1047
1048 // Print details about the function.
1049 if (FLAG_max_stack_trace_source_length != 0 && code != NULL) {
1050 SharedFunctionInfo* shared = JSFunction::cast(function)->shared();
1051 accumulator->Add("--------- s o u r c e c o d e ---------\n");
1052 shared->SourceCodePrint(accumulator, FLAG_max_stack_trace_source_length);
1053 accumulator->Add("\n-----------------------------------------\n");
1054 }
1055
1056 accumulator->Add("}\n\n");
1057}
1058
1059
1060void ArgumentsAdaptorFrame::Print(StringStream* accumulator,
1061 PrintMode mode,
1062 int index) const {
1063 int actual = ComputeParametersCount();
1064 int expected = -1;
1065 Object* function = this->function();
1066 if (function->IsJSFunction()) {
1067 expected = JSFunction::cast(function)->shared()->formal_parameter_count();
1068 }
1069
1070 PrintIndex(accumulator, mode, index);
1071 accumulator->Add("arguments adaptor frame: %d->%d", actual, expected);
1072 if (mode == OVERVIEW) {
1073 accumulator->Add("\n");
1074 return;
1075 }
1076 accumulator->Add(" {\n");
1077
1078 // Print actual arguments.
1079 if (actual > 0) accumulator->Add(" // actual arguments\n");
1080 for (int i = 0; i < actual; i++) {
1081 accumulator->Add(" [%02d] : %o", i, GetParameter(i));
1082 if (expected != -1 && i >= expected) {
1083 accumulator->Add(" // not passed to callee");
1084 }
1085 accumulator->Add("\n");
1086 }
1087
1088 accumulator->Add("}\n\n");
1089}
1090
1091
1092void EntryFrame::Iterate(ObjectVisitor* v) const {
1093 StackHandlerIterator it(this, top_handler());
1094 ASSERT(!it.done());
1095 StackHandler* handler = it.handler();
1096 ASSERT(handler->is_entry());
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001097 handler->Iterate(v, LookupCode());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001098#ifdef DEBUG
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001099 // Make sure that the entry frame does not contain more than one
1100 // stack handler.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001101 it.Advance();
1102 ASSERT(it.done());
1103#endif
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001104 IteratePc(v, pc_address(), LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105}
1106
1107
1108void StandardFrame::IterateExpressions(ObjectVisitor* v) const {
1109 const int offset = StandardFrameConstants::kContextOffset;
1110 Object** base = &Memory::Object_at(sp());
1111 Object** limit = &Memory::Object_at(fp() + offset) + 1;
1112 for (StackHandlerIterator it(this, top_handler()); !it.done(); it.Advance()) {
1113 StackHandler* handler = it.handler();
1114 // Traverse pointers down to - but not including - the next
1115 // handler in the handler chain. Update the base to skip the
1116 // handler and allow the handler to traverse its own pointers.
1117 const Address address = handler->address();
1118 v->VisitPointers(base, reinterpret_cast<Object**>(address));
1119 base = reinterpret_cast<Object**>(address + StackHandlerConstants::kSize);
1120 // Traverse the pointers in the handler itself.
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001121 handler->Iterate(v, LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122 }
1123 v->VisitPointers(base, limit);
1124}
1125
1126
1127void JavaScriptFrame::Iterate(ObjectVisitor* v) const {
1128 IterateExpressions(v);
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001129 IteratePc(v, pc_address(), LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001130}
1131
1132
1133void InternalFrame::Iterate(ObjectVisitor* v) const {
1134 // Internal frames only have object pointers on the expression stack
1135 // as they never have any arguments.
1136 IterateExpressions(v);
vegorov@chromium.org74f333b2011-04-06 11:17:46 +00001137 IteratePc(v, pc_address(), LookupCode());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138}
1139
1140
1141// -------------------------------------------------------------------------
1142
1143
1144JavaScriptFrame* StackFrameLocator::FindJavaScriptFrame(int n) {
1145 ASSERT(n >= 0);
1146 for (int i = 0; i <= n; i++) {
1147 while (!iterator_.frame()->is_java_script()) iterator_.Advance();
1148 if (i == n) return JavaScriptFrame::cast(iterator_.frame());
1149 iterator_.Advance();
1150 }
1151 UNREACHABLE();
1152 return NULL;
1153}
1154
1155
1156// -------------------------------------------------------------------------
1157
1158
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001159Code* PcToCodeCache::GcSafeCastToCode(HeapObject* object, Address pc) {
1160 Code* code = reinterpret_cast<Code*>(object);
1161 ASSERT(code != NULL && code->contains(pc));
1162 return code;
1163}
1164
1165
1166Code* PcToCodeCache::GcSafeFindCodeForPc(Address pc) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001167 Heap* heap = isolate_->heap();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001168 // Check if the pc points into a large object chunk.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001169 LargeObjectChunk* chunk = heap->lo_space()->FindChunkContainingPc(pc);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001170 if (chunk != NULL) return GcSafeCastToCode(chunk->GetObject(), pc);
1171
1172 // Iterate through the 8K page until we reach the end or find an
1173 // object starting after the pc.
1174 Page* page = Page::FromAddress(pc);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001175 HeapObjectIterator iterator(page, heap->GcSafeSizeOfOldObjectFunction());
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001176 HeapObject* previous = NULL;
1177 while (true) {
1178 HeapObject* next = iterator.next();
1179 if (next == NULL || next->address() >= pc) {
1180 return GcSafeCastToCode(previous, pc);
1181 }
1182 previous = next;
1183 }
1184}
1185
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001186
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001187PcToCodeCache::PcToCodeCacheEntry* PcToCodeCache::GetCacheEntry(Address pc) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001188 isolate_->counters()->pc_to_code()->Increment();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001189 ASSERT(IsPowerOf2(kPcToCodeCacheSize));
1190 uint32_t hash = ComputeIntegerHash(
1191 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(pc)));
1192 uint32_t index = hash & (kPcToCodeCacheSize - 1);
1193 PcToCodeCacheEntry* entry = cache(index);
1194 if (entry->pc == pc) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001195 isolate_->counters()->pc_to_code_cached()->Increment();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001196 ASSERT(entry->code == GcSafeFindCodeForPc(pc));
1197 } else {
1198 // Because this code may be interrupted by a profiling signal that
1199 // also queries the cache, we cannot update pc before the code has
1200 // been set. Otherwise, we risk trying to use a cache entry before
1201 // the code has been computed.
1202 entry->code = GcSafeFindCodeForPc(pc);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00001203 entry->safepoint_entry.Reset();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001204 entry->pc = pc;
1205 }
1206 return entry;
1207}
1208
1209
1210// -------------------------------------------------------------------------
1211
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001212int NumRegs(RegList reglist) {
1213 int n = 0;
1214 while (reglist != 0) {
1215 n++;
1216 reglist &= reglist - 1; // clear one bit
1217 }
1218 return n;
1219}
1220
1221
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001222struct JSCallerSavedCodeData {
1223 JSCallerSavedCodeData() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224 int i = 0;
1225 for (int r = 0; r < kNumRegs; r++)
1226 if ((kJSCallerSaved & (1 << r)) != 0)
1227 reg_code[i++] = r;
1228
1229 ASSERT(i == kNumJSCallerSaved);
1230 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001231 int reg_code[kNumJSCallerSaved];
1232};
1233
1234
1235static const JSCallerSavedCodeData kCallerSavedCodeData;
1236
1237
1238int JSCallerSavedCode(int n) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 ASSERT(0 <= n && n < kNumJSCallerSaved);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001240 return kCallerSavedCodeData.reg_code[n];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001241}
1242
1243
ager@chromium.org357bf652010-04-12 11:30:10 +00001244#define DEFINE_WRAPPER(type, field) \
1245class field##_Wrapper : public ZoneObject { \
1246 public: /* NOLINT */ \
1247 field##_Wrapper(const field& original) : frame_(original) { \
1248 } \
1249 field frame_; \
1250};
1251STACK_FRAME_TYPE_LIST(DEFINE_WRAPPER)
1252#undef DEFINE_WRAPPER
1253
1254static StackFrame* AllocateFrameCopy(StackFrame* frame) {
1255#define FRAME_TYPE_CASE(type, field) \
1256 case StackFrame::type: { \
1257 field##_Wrapper* wrapper = \
1258 new field##_Wrapper(*(reinterpret_cast<field*>(frame))); \
1259 return &wrapper->frame_; \
1260 }
1261
1262 switch (frame->type()) {
1263 STACK_FRAME_TYPE_LIST(FRAME_TYPE_CASE)
1264 default: UNREACHABLE();
1265 }
1266#undef FRAME_TYPE_CASE
1267 return NULL;
1268}
1269
1270Vector<StackFrame*> CreateStackMap() {
1271 ZoneList<StackFrame*> list(10);
1272 for (StackFrameIterator it; !it.done(); it.Advance()) {
1273 StackFrame* frame = AllocateFrameCopy(it.frame());
1274 list.Add(frame);
1275 }
1276 return list.ToVector();
1277}
1278
1279
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280} } // namespace v8::internal