Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: src/isolate.cc

Issue 674423002: Get stack trace for uncaught exceptions/promise rejections from the simple stack when available. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdlib.h> 5 #include <stdlib.h>
6 6
7 #include <fstream> // NOLINT(readability/streams) 7 #include <fstream> // NOLINT(readability/streams)
8 #include <sstream> 8 #include <sstream>
9 9
10 #include "src/v8.h" 10 #include "src/v8.h"
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 int new_capacity = JSObject::NewElementsCapacity(elements->length()); 384 int new_capacity = JSObject::NewElementsCapacity(elements->length());
385 Handle<FixedArray> new_elements = 385 Handle<FixedArray> new_elements =
386 factory()->NewFixedArrayWithHoles(new_capacity); 386 factory()->NewFixedArrayWithHoles(new_capacity);
387 for (int i = 0; i < cursor; i++) { 387 for (int i = 0; i < cursor; i++) {
388 new_elements->set(i, elements->get(i)); 388 new_elements->set(i, elements->get(i));
389 } 389 }
390 elements = new_elements; 390 elements = new_elements;
391 } 391 }
392 DCHECK(cursor + 4 <= elements->length()); 392 DCHECK(cursor + 4 <= elements->length());
393 393
394
395 Handle<Code> code = frames[i].code(); 394 Handle<Code> code = frames[i].code();
396 Handle<Smi> offset(Smi::FromInt(frames[i].offset()), this); 395 Handle<Smi> offset(Smi::FromInt(frames[i].offset()), this);
397 // The stack trace API should not expose receivers and function 396 // The stack trace API should not expose receivers and function
398 // objects on frames deeper than the top-most one with a strict 397 // objects on frames deeper than the top-most one with a strict
399 // mode function. The number of sloppy frames is stored as 398 // mode function. The number of sloppy frames is stored as
400 // first element in the result array. 399 // first element in the result array.
401 if (!encountered_strict_function) { 400 if (!encountered_strict_function) {
402 if (fun->shared()->strict_mode() == STRICT) { 401 if (fun->shared()->strict_mode() == STRICT) {
403 encountered_strict_function = true; 402 encountered_strict_function = true;
404 } else { 403 } else {
(...skipping 28 matching lines...) Expand all
433 432
434 void Isolate::CaptureAndSetSimpleStackTrace(Handle<JSObject> error_object, 433 void Isolate::CaptureAndSetSimpleStackTrace(Handle<JSObject> error_object,
435 Handle<Object> caller) { 434 Handle<Object> caller) {
436 // Capture stack trace for simple stack trace string formatting. 435 // Capture stack trace for simple stack trace string formatting.
437 Handle<Name> key = factory()->stack_trace_symbol(); 436 Handle<Name> key = factory()->stack_trace_symbol();
438 Handle<Object> stack_trace = CaptureSimpleStackTrace(error_object, caller); 437 Handle<Object> stack_trace = CaptureSimpleStackTrace(error_object, caller);
439 JSObject::SetProperty(error_object, key, stack_trace, STRICT).Assert(); 438 JSObject::SetProperty(error_object, key, stack_trace, STRICT).Assert();
440 } 439 }
441 440
442 441
442 Handle<JSArray> Isolate::GetDetailedStackTrace(Handle<JSObject> error_object) {
443 Handle<Name> key_detailed = factory()->detailed_stack_trace_symbol();
444 Handle<Object> stack_trace =
445 JSObject::GetDataProperty(error_object, key_detailed);
446 if (stack_trace->IsJSArray()) return Handle<JSArray>::cast(stack_trace);
447
448 if (!capture_stack_trace_for_uncaught_exceptions_) return Handle<JSArray>();
449
450 // Try to get details from simple stack trace.
451 Handle<JSArray> detailed_stack_trace = GetDetailedStackTraceFromSimpleStack(
452 error_object, stack_trace_for_uncaught_exceptions_frame_limit_,
Yang 2014/10/28 12:08:10 There is no need to pass those two arguments, sinc
aandrey 2014/10/28 13:12:14 Done.
453 stack_trace_for_uncaught_exceptions_options_);
454 if (!detailed_stack_trace.is_null()) {
455 // Save the detailed stack since the simple one might be withdrawn later.
456 JSObject::SetProperty(error_object, key_detailed, detailed_stack_trace,
457 STRICT).Assert();
458 }
459 return detailed_stack_trace;
460 }
461
462
463 class CaptureStackTraceHelper {
464 public:
465 CaptureStackTraceHelper(Isolate* isolate,
466 StackTrace::StackTraceOptions options)
467 : isolate_(isolate) {
468 if (options & StackTrace::kColumnOffset) {
469 column_key_ =
470 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("column"));
471 }
472 if (options & StackTrace::kLineNumber) {
473 line_key_ =
474 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("lineNumber"));
475 }
476 if (options & StackTrace::kScriptId) {
477 script_id_key_ =
478 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("scriptId"));
479 }
480 if (options & StackTrace::kScriptName) {
481 script_name_key_ =
482 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("scriptName"));
483 }
484 if (options & StackTrace::kScriptNameOrSourceURL) {
485 script_name_or_source_url_key_ = factory()->InternalizeOneByteString(
486 STATIC_CHAR_VECTOR("scriptNameOrSourceURL"));
487 }
488 if (options & StackTrace::kFunctionName) {
489 function_key_ = factory()->InternalizeOneByteString(
490 STATIC_CHAR_VECTOR("functionName"));
491 }
492 if (options & StackTrace::kIsEval) {
493 eval_key_ =
494 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("isEval"));
495 }
496 if (options & StackTrace::kIsConstructor) {
497 constructor_key_ = factory()->InternalizeOneByteString(
498 STATIC_CHAR_VECTOR("isConstructor"));
499 }
500 }
501
502 Handle<JSObject> NewStackFrameObject(Handle<JSFunction> fun,
503 Handle<Code> code, Address pc,
504 bool is_constructor) {
505 Handle<JSObject> stack_frame =
506 factory()->NewJSObject(isolate_->object_function());
507
508 Handle<Script> script(Script::cast(fun->shared()->script()));
509
510 if (!line_key_.is_null()) {
511 int script_line_offset = script->line_offset()->value();
512 int position = code->SourcePosition(pc);
513 int line_number = Script::GetLineNumber(script, position);
514 // line_number is already shifted by the script_line_offset.
515 int relative_line_number = line_number - script_line_offset;
516 if (!column_key_.is_null() && relative_line_number >= 0) {
517 Handle<FixedArray> line_ends(FixedArray::cast(script->line_ends()));
518 int start = (relative_line_number == 0) ? 0 :
519 Smi::cast(line_ends->get(relative_line_number - 1))->value() + 1;
520 int column_offset = position - start;
521 if (relative_line_number == 0) {
522 // For the case where the code is on the same line as the script
523 // tag.
524 column_offset += script->column_offset()->value();
525 }
526 JSObject::AddProperty(stack_frame, column_key_,
527 handle(Smi::FromInt(column_offset + 1), isolate_),
528 NONE);
529 }
530 JSObject::AddProperty(stack_frame, line_key_,
531 handle(Smi::FromInt(line_number + 1), isolate_),
532 NONE);
533 }
534
535 if (!script_id_key_.is_null()) {
536 JSObject::AddProperty(stack_frame, script_id_key_,
537 handle(script->id(), isolate_), NONE);
538 }
539
540 if (!script_name_key_.is_null()) {
541 JSObject::AddProperty(stack_frame, script_name_key_,
542 handle(script->name(), isolate_), NONE);
543 }
544
545 if (!script_name_or_source_url_key_.is_null()) {
546 Handle<Object> result = Script::GetNameOrSourceURL(script);
547 JSObject::AddProperty(stack_frame, script_name_or_source_url_key_, result,
548 NONE);
549 }
550
551 if (!function_key_.is_null()) {
552 Handle<Object> fun_name(fun->shared()->DebugName(), isolate_);
553 JSObject::AddProperty(stack_frame, function_key_, fun_name, NONE);
554 }
555
556 if (!eval_key_.is_null()) {
557 Handle<Object> is_eval =
558 script->compilation_type() == Script::COMPILATION_TYPE_EVAL
Yang 2014/10/28 12:08:10 you can use factory()->ToBoolean for this.
aandrey 2014/10/28 13:12:14 Done.
559 ? factory()->true_value()
560 : factory()->false_value();
561 JSObject::AddProperty(stack_frame, eval_key_, is_eval, NONE);
562 }
563
564 if (!constructor_key_.is_null()) {
565 Handle<Object> is_constructor_obj =
566 is_constructor ? factory()->true_value() : factory()->false_value();
567 JSObject::AddProperty(stack_frame, constructor_key_, is_constructor_obj,
568 NONE);
569 }
570
571 return stack_frame;
572 }
573
574 private:
575 inline Factory* factory() { return isolate_->factory(); }
576
577 Isolate* isolate_;
578 Handle<String> column_key_;
579 Handle<String> line_key_;
580 Handle<String> script_id_key_;
581 Handle<String> script_name_key_;
582 Handle<String> script_name_or_source_url_key_;
583 Handle<String> function_key_;
584 Handle<String> eval_key_;
585 Handle<String> constructor_key_;
586 };
587
588
589 Handle<JSArray> Isolate::GetDetailedStackTraceFromSimpleStack(
590 Handle<JSObject> error_object, int frame_limit,
591 StackTrace::StackTraceOptions options) {
592 Handle<Name> key = factory()->stack_trace_symbol();
593 Handle<Object> property = JSObject::GetDataProperty(error_object, key);
594 if (!property->IsJSArray()) return Handle<JSArray>();
595 Handle<JSArray> simple_stack_trace = Handle<JSArray>::cast(property);
596
597 CaptureStackTraceHelper helper(this, options);
598
599 int frames_seen = 0;
600 Handle<FixedArray> elements(FixedArray::cast(simple_stack_trace->elements()));
601 int elements_limit = Smi::cast(simple_stack_trace->length())->value();
602
603 if (frame_limit < 0) frame_limit = (elements_limit - 1) / 4;
604
605 Handle<JSArray> stack_trace = factory()->NewJSArray(frame_limit);
606 for (int i = 1; i < elements_limit && frames_seen < frame_limit; i += 4) {
607 Handle<Object> recv = handle(elements->get(i), this);
608 Handle<JSFunction> fun =
609 handle(JSFunction::cast(elements->get(i + 1)), this);
610 Handle<Code> code = handle(Code::cast(elements->get(i + 2)), this);
611 Handle<Smi> offset = handle(Smi::cast(elements->get(i + 3)), this);
612 Address pc = code->address() + offset->value();
613 bool is_constructor =
614 recv->IsJSObject() &&
615 Handle<JSObject>::cast(recv)->map()->constructor() == *fun;
616
617 Handle<JSObject> stack_frame =
618 helper.NewStackFrameObject(fun, code, pc, is_constructor);
619
620 FixedArray::cast(stack_trace->elements())->set(frames_seen, *stack_frame);
621 frames_seen++;
622 }
623
624 stack_trace->set_length(Smi::FromInt(frames_seen));
625 return stack_trace;
626 }
627
628
443 Handle<JSArray> Isolate::CaptureCurrentStackTrace( 629 Handle<JSArray> Isolate::CaptureCurrentStackTrace(
444 int frame_limit, StackTrace::StackTraceOptions options) { 630 int frame_limit, StackTrace::StackTraceOptions options) {
631 CaptureStackTraceHelper helper(this, options);
632
445 // Ensure no negative values. 633 // Ensure no negative values.
446 int limit = Max(frame_limit, 0); 634 int limit = Max(frame_limit, 0);
447 Handle<JSArray> stack_trace = factory()->NewJSArray(frame_limit); 635 Handle<JSArray> stack_trace = factory()->NewJSArray(frame_limit);
448 636
449 Handle<String> column_key =
450 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("column"));
451 Handle<String> line_key =
452 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("lineNumber"));
453 Handle<String> script_id_key =
454 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("scriptId"));
455 Handle<String> script_name_key =
456 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("scriptName"));
457 Handle<String> script_name_or_source_url_key =
458 factory()->InternalizeOneByteString(
459 STATIC_CHAR_VECTOR("scriptNameOrSourceURL"));
460 Handle<String> function_key =
461 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("functionName"));
462 Handle<String> eval_key =
463 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("isEval"));
464 Handle<String> constructor_key =
465 factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("isConstructor"));
466
467 StackTraceFrameIterator it(this); 637 StackTraceFrameIterator it(this);
468 int frames_seen = 0; 638 int frames_seen = 0;
469 while (!it.done() && (frames_seen < limit)) { 639 while (!it.done() && (frames_seen < limit)) {
470 JavaScriptFrame* frame = it.frame(); 640 JavaScriptFrame* frame = it.frame();
471 // Set initial size to the maximum inlining level + 1 for the outermost 641 // Set initial size to the maximum inlining level + 1 for the outermost
472 // function. 642 // function.
473 List<FrameSummary> frames(FLAG_max_inlining_levels + 1); 643 List<FrameSummary> frames(FLAG_max_inlining_levels + 1);
474 frame->Summarize(&frames); 644 frame->Summarize(&frames);
475 for (int i = frames.length() - 1; i >= 0 && frames_seen < limit; i--) { 645 for (int i = frames.length() - 1; i >= 0 && frames_seen < limit; i--) {
476 Handle<JSFunction> fun = frames[i].function(); 646 Handle<JSFunction> fun = frames[i].function();
477 // Filter frames from other security contexts. 647 // Filter frames from other security contexts.
478 if (!(options & StackTrace::kExposeFramesAcrossSecurityOrigins) && 648 if (!(options & StackTrace::kExposeFramesAcrossSecurityOrigins) &&
479 !this->context()->HasSameSecurityTokenAs(fun->context())) continue; 649 !this->context()->HasSameSecurityTokenAs(fun->context())) continue;
480 650
481 // Create a JSObject to hold the information for the StackFrame. 651 Handle<JSObject> stack_frame = helper.NewStackFrameObject(
482 Handle<JSObject> stack_frame = factory()->NewJSObject(object_function()); 652 fun, frames[i].code(), frames[i].pc(), frames[i].is_constructor());
483
484 Handle<Script> script(Script::cast(fun->shared()->script()));
485
486 if (options & StackTrace::kLineNumber) {
487 int script_line_offset = script->line_offset()->value();
488 int position = frames[i].code()->SourcePosition(frames[i].pc());
489 int line_number = Script::GetLineNumber(script, position);
490 // line_number is already shifted by the script_line_offset.
491 int relative_line_number = line_number - script_line_offset;
492 if (options & StackTrace::kColumnOffset && relative_line_number >= 0) {
493 Handle<FixedArray> line_ends(FixedArray::cast(script->line_ends()));
494 int start = (relative_line_number == 0) ? 0 :
495 Smi::cast(line_ends->get(relative_line_number - 1))->value() + 1;
496 int column_offset = position - start;
497 if (relative_line_number == 0) {
498 // For the case where the code is on the same line as the script
499 // tag.
500 column_offset += script->column_offset()->value();
501 }
502 JSObject::AddProperty(
503 stack_frame, column_key,
504 handle(Smi::FromInt(column_offset + 1), this), NONE);
505 }
506 JSObject::AddProperty(
507 stack_frame, line_key,
508 handle(Smi::FromInt(line_number + 1), this), NONE);
509 }
510
511 if (options & StackTrace::kScriptId) {
512 JSObject::AddProperty(
513 stack_frame, script_id_key, handle(script->id(), this), NONE);
514 }
515
516 if (options & StackTrace::kScriptName) {
517 JSObject::AddProperty(
518 stack_frame, script_name_key, handle(script->name(), this), NONE);
519 }
520
521 if (options & StackTrace::kScriptNameOrSourceURL) {
522 Handle<Object> result = Script::GetNameOrSourceURL(script);
523 JSObject::AddProperty(
524 stack_frame, script_name_or_source_url_key, result, NONE);
525 }
526
527 if (options & StackTrace::kFunctionName) {
528 Handle<Object> fun_name(fun->shared()->DebugName(), this);
529 JSObject::AddProperty(stack_frame, function_key, fun_name, NONE);
530 }
531
532 if (options & StackTrace::kIsEval) {
533 Handle<Object> is_eval =
534 script->compilation_type() == Script::COMPILATION_TYPE_EVAL ?
535 factory()->true_value() : factory()->false_value();
536 JSObject::AddProperty(stack_frame, eval_key, is_eval, NONE);
537 }
538
539 if (options & StackTrace::kIsConstructor) {
540 Handle<Object> is_constructor = (frames[i].is_constructor()) ?
541 factory()->true_value() : factory()->false_value();
542 JSObject::AddProperty(
543 stack_frame, constructor_key, is_constructor, NONE);
544 }
545 653
546 FixedArray::cast(stack_trace->elements())->set(frames_seen, *stack_frame); 654 FixedArray::cast(stack_trace->elements())->set(frames_seen, *stack_frame);
547 frames_seen++; 655 frames_seen++;
548 } 656 }
549 it.Advance(); 657 it.Advance();
550 } 658 }
551 659
552 stack_trace->set_length(Smi::FromInt(frames_seen)); 660 stack_trace->set_length(Smi::FromInt(frames_seen));
553 return stack_trace; 661 return stack_trace;
554 } 662 }
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after
999 1107
1000 static int fatal_exception_depth = 0; 1108 static int fatal_exception_depth = 0;
1001 1109
1002 1110
1003 Handle<JSMessageObject> Isolate::CreateMessage(Handle<Object> exception, 1111 Handle<JSMessageObject> Isolate::CreateMessage(Handle<Object> exception,
1004 MessageLocation* location) { 1112 MessageLocation* location) {
1005 Handle<JSArray> stack_trace_object; 1113 Handle<JSArray> stack_trace_object;
1006 if (capture_stack_trace_for_uncaught_exceptions_) { 1114 if (capture_stack_trace_for_uncaught_exceptions_) {
1007 if (IsErrorObject(exception)) { 1115 if (IsErrorObject(exception)) {
1008 // We fetch the stack trace that corresponds to this error object. 1116 // We fetch the stack trace that corresponds to this error object.
1009 Handle<Name> key = factory()->detailed_stack_trace_symbol(); 1117 // If the lookup fails, the exception is probably not a valid Error
1010 // Look up as own property. If the lookup fails, the exception is 1118 // object. In that case, we fall through and capture the stack trace
1011 // probably not a valid Error object. In that case, we fall through 1119 // at this throw site.
1012 // and capture the stack trace at this throw site. 1120 stack_trace_object =
1013 LookupIterator lookup(exception, key, 1121 GetDetailedStackTrace(Handle<JSObject>::cast(exception));
1014 LookupIterator::OWN_SKIP_INTERCEPTOR);
1015 Handle<Object> stack_trace_property;
1016 if (Object::GetProperty(&lookup).ToHandle(&stack_trace_property) &&
1017 stack_trace_property->IsJSArray()) {
1018 stack_trace_object = Handle<JSArray>::cast(stack_trace_property);
1019 }
1020 } 1122 }
1021 if (stack_trace_object.is_null()) { 1123 if (stack_trace_object.is_null()) {
1022 // Not an error object, we capture at throw site. 1124 // Not an error object, we capture at throw site.
1023 stack_trace_object = CaptureCurrentStackTrace( 1125 stack_trace_object = CaptureCurrentStackTrace(
1024 stack_trace_for_uncaught_exceptions_frame_limit_, 1126 stack_trace_for_uncaught_exceptions_frame_limit_,
1025 stack_trace_for_uncaught_exceptions_options_); 1127 stack_trace_for_uncaught_exceptions_options_);
1026 } 1128 }
1027 } 1129 }
1028 1130
1029 // If the exception argument is a custom object, turn it into a string 1131 // If the exception argument is a custom object, turn it into a string
(...skipping 1234 matching lines...) Expand 10 before | Expand all | Expand 10 after
2264 promise_reject_callback_ = callback; 2366 promise_reject_callback_ = callback;
2265 } 2367 }
2266 2368
2267 2369
2268 void Isolate::ReportPromiseReject(Handle<JSObject> promise, 2370 void Isolate::ReportPromiseReject(Handle<JSObject> promise,
2269 Handle<Object> value, 2371 Handle<Object> value,
2270 v8::PromiseRejectEvent event) { 2372 v8::PromiseRejectEvent event) {
2271 if (promise_reject_callback_ == NULL) return; 2373 if (promise_reject_callback_ == NULL) return;
2272 Handle<JSArray> stack_trace; 2374 Handle<JSArray> stack_trace;
2273 if (event == v8::kPromiseRejectWithNoHandler && value->IsJSObject()) { 2375 if (event == v8::kPromiseRejectWithNoHandler && value->IsJSObject()) {
2274 Handle<JSObject> error_obj = Handle<JSObject>::cast(value); 2376 stack_trace = GetDetailedStackTrace(Handle<JSObject>::cast(value));
2275 Handle<Name> key = factory()->detailed_stack_trace_symbol();
2276 Handle<Object> property = JSObject::GetDataProperty(error_obj, key);
2277 if (property->IsJSArray()) stack_trace = Handle<JSArray>::cast(property);
2278 } 2377 }
2279 promise_reject_callback_(v8::PromiseRejectMessage( 2378 promise_reject_callback_(v8::PromiseRejectMessage(
2280 v8::Utils::PromiseToLocal(promise), event, v8::Utils::ToLocal(value), 2379 v8::Utils::PromiseToLocal(promise), event, v8::Utils::ToLocal(value),
2281 v8::Utils::StackTraceToLocal(stack_trace))); 2380 v8::Utils::StackTraceToLocal(stack_trace)));
2282 } 2381 }
2283 2382
2284 2383
2285 void Isolate::EnqueueMicrotask(Handle<Object> microtask) { 2384 void Isolate::EnqueueMicrotask(Handle<Object> microtask) {
2286 DCHECK(microtask->IsJSFunction() || microtask->IsCallHandlerInfo()); 2385 DCHECK(microtask->IsJSFunction() || microtask->IsCallHandlerInfo());
2287 Handle<FixedArray> queue(heap()->microtask_queue(), this); 2386 Handle<FixedArray> queue(heap()->microtask_queue(), this);
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
2402 if (prev_ && prev_->Intercept(flag)) return true; 2501 if (prev_ && prev_->Intercept(flag)) return true;
2403 // Then check whether this scope intercepts. 2502 // Then check whether this scope intercepts.
2404 if ((flag & intercept_mask_)) { 2503 if ((flag & intercept_mask_)) {
2405 intercepted_flags_ |= flag; 2504 intercepted_flags_ |= flag;
2406 return true; 2505 return true;
2407 } 2506 }
2408 return false; 2507 return false;
2409 } 2508 }
2410 2509
2411 } } // namespace v8::internal 2510 } } // namespace v8::internal
OLDNEW
« src/isolate.h ('K') | « src/isolate.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698