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

Side by Side Diff: src/inspector/v8-runtime-agent-impl.cc

Issue 2700743002: [inspector] extend protocol for code coverage. (Closed)
Patch Set: small fix Created 3 years, 10 months 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 12 matching lines...) Expand all
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "src/inspector/v8-runtime-agent-impl.h" 31 #include "src/inspector/v8-runtime-agent-impl.h"
32 32
33 #include "src/debug/debug-interface.h"
33 #include "src/inspector/injected-script.h" 34 #include "src/inspector/injected-script.h"
34 #include "src/inspector/inspected-context.h" 35 #include "src/inspector/inspected-context.h"
35 #include "src/inspector/protocol/Protocol.h" 36 #include "src/inspector/protocol/Protocol.h"
36 #include "src/inspector/remote-object-id.h" 37 #include "src/inspector/remote-object-id.h"
37 #include "src/inspector/string-util.h" 38 #include "src/inspector/string-util.h"
38 #include "src/inspector/v8-console-message.h" 39 #include "src/inspector/v8-console-message.h"
39 #include "src/inspector/v8-debugger-agent-impl.h" 40 #include "src/inspector/v8-debugger-agent-impl.h"
40 #include "src/inspector/v8-debugger.h" 41 #include "src/inspector/v8-debugger.h"
41 #include "src/inspector/v8-inspector-impl.h" 42 #include "src/inspector/v8-inspector-impl.h"
42 #include "src/inspector/v8-inspector-session-impl.h" 43 #include "src/inspector/v8-inspector-session-impl.h"
43 #include "src/inspector/v8-stack-trace-impl.h" 44 #include "src/inspector/v8-stack-trace-impl.h"
44 #include "src/tracing/trace-event.h" 45 #include "src/tracing/trace-event.h"
45 46
46 #include "include/v8-inspector.h" 47 #include "include/v8-inspector.h"
47 48
48 namespace v8_inspector { 49 namespace v8_inspector {
49 50
50 namespace V8RuntimeAgentImplState { 51 namespace V8RuntimeAgentImplState {
51 static const char customObjectFormatterEnabled[] = 52 static const char customObjectFormatterEnabled[] =
52 "customObjectFormatterEnabled"; 53 "customObjectFormatterEnabled";
53 static const char runtimeEnabled[] = "runtimeEnabled"; 54 static const char runtimeEnabled[] = "runtimeEnabled";
55 static const char preciseCoverageStarted[] = "preciseCoverageStarted";
54 }; 56 };
55 57
56 using protocol::Runtime::RemoteObject; 58 using protocol::Runtime::RemoteObject;
57 59
58 namespace { 60 namespace {
59 61
60 template <typename Callback> 62 template <typename Callback>
61 class ProtocolPromiseHandler { 63 class ProtocolPromiseHandler {
62 public: 64 public:
63 static void add(V8InspectorImpl* inspector, v8::Local<v8::Context> context, 65 static void add(V8InspectorImpl* inspector, v8::Local<v8::Context> context,
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 } 640 }
639 ProtocolPromiseHandler<RunScriptCallback>::add( 641 ProtocolPromiseHandler<RunScriptCallback>::add(
640 m_inspector, scope.context(), maybeResultValue.ToLocalChecked(), 642 m_inspector, scope.context(), maybeResultValue.ToLocalChecked(),
641 "Result of the script execution is not a promise", 643 "Result of the script execution is not a promise",
642 m_session->contextGroupId(), 644 m_session->contextGroupId(),
643 scope.injectedScript()->context()->contextId(), objectGroup.fromMaybe(""), 645 scope.injectedScript()->context()->contextId(), objectGroup.fromMaybe(""),
644 returnByValue.fromMaybe(false), generatePreview.fromMaybe(false), 646 returnByValue.fromMaybe(false), generatePreview.fromMaybe(false),
645 std::move(callback)); 647 std::move(callback));
646 } 648 }
647 649
650 Response V8RuntimeAgentImpl::startPreciseCoverage() {
651 m_state->setBoolean(V8RuntimeAgentImplState::preciseCoverageStarted, true);
652 v8::debug::Coverage::TogglePrecise(m_inspector->isolate(), true);
653 return Response::OK();
654 }
655
656 Response V8RuntimeAgentImpl::stopPreciseCoverage() {
657 m_state->setBoolean(V8RuntimeAgentImplState::preciseCoverageStarted, false);
658 v8::debug::Coverage::TogglePrecise(m_inspector->isolate(), false);
659 return Response::OK();
660 }
661
662 namespace {
663 Response takeCoverage(
664 v8::Isolate* isolate,
665 std::unique_ptr<protocol::Array<protocol::Runtime::ScriptCoverage>>*
666 out_result) {
667 std::unique_ptr<protocol::Array<protocol::Runtime::ScriptCoverage>> result =
668 protocol::Array<protocol::Runtime::ScriptCoverage>::create();
669 v8::HandleScope handle_scope(isolate);
670 v8::debug::Coverage coverage = v8::debug::Coverage::Collect(isolate);
671 for (size_t i = 0; i < coverage.ScriptCount(); i++) {
672 v8::debug::Coverage::ScriptData script_data = coverage.GetScriptData(i);
673 v8::Local<v8::debug::Script> script = script_data.GetScript();
674 std::unique_ptr<protocol::Array<protocol::Runtime::FunctionCoverage>>
675 functions =
676 protocol::Array<protocol::Runtime::FunctionCoverage>::create();
677 for (size_t j = 0; j < script_data.FunctionCount(); j++) {
678 v8::debug::Coverage::FunctionData function_data =
679 script_data.GetFunctionData(j);
680 std::unique_ptr<protocol::Array<protocol::Runtime::CoverageRange>>
681 ranges = protocol::Array<protocol::Runtime::CoverageRange>::create();
682 // At this point we only have per-function coverage data, so there is
683 // only one range per function.
684 ranges->addItem(
685 protocol::Runtime::CoverageRange::create()
686 .setStartLineNumber(function_data.Start().GetLineNumber())
687 .setStartColumnNumber(function_data.Start().GetColumnNumber())
688 .setEndLineNumber(function_data.End().GetLineNumber())
689 .setEndColumnNumber(function_data.End().GetColumnNumber())
690 .setCount(function_data.Count())
691 .build());
692 functions->addItem(
693 protocol::Runtime::FunctionCoverage::create()
694 .setFunctionName(toProtocolString(
695 function_data.Name().FromMaybe(v8::Local<v8::String>())))
696 .setRanges(std::move(ranges))
697 .build());
698 }
699 String16 url;
700 v8::Local<v8::String> name;
701 if (script->Name().ToLocal(&name) || script->SourceURL().ToLocal(&name)) {
702 url = toProtocolString(name);
703 }
704 result->addItem(protocol::Runtime::ScriptCoverage::create()
705 .setScriptId(String16::fromInteger(script->Id()))
706 .setUrl(url)
707 .setFunctions(std::move(functions))
708 .build());
709 }
710 *out_result = std::move(result);
711 return Response::OK();
712 }
713 } // anonymous namespace
714
715 Response V8RuntimeAgentImpl::takePreciseCoverage(
716 std::unique_ptr<protocol::Array<protocol::Runtime::ScriptCoverage>>*
717 out_result) {
718 if (!m_state->booleanProperty(V8RuntimeAgentImplState::preciseCoverageStarted,
719 false)) {
720 return Response::Error("Precise coverage has not been started.");
721 }
722 return takeCoverage(m_inspector->isolate(), out_result);
723 }
724
725 Response V8RuntimeAgentImpl::takeBestEffortCoverage(
726 std::unique_ptr<protocol::Array<protocol::Runtime::ScriptCoverage>>*
727 out_result) {
728 return takeCoverage(m_inspector->isolate(), out_result);
729 }
730
648 void V8RuntimeAgentImpl::restore() { 731 void V8RuntimeAgentImpl::restore() {
649 if (!m_state->booleanProperty(V8RuntimeAgentImplState::runtimeEnabled, false)) 732 if (!m_state->booleanProperty(V8RuntimeAgentImplState::runtimeEnabled, false))
650 return; 733 return;
651 m_frontend.executionContextsCleared(); 734 m_frontend.executionContextsCleared();
652 enable(); 735 enable();
653 if (m_state->booleanProperty( 736 if (m_state->booleanProperty(
654 V8RuntimeAgentImplState::customObjectFormatterEnabled, false)) 737 V8RuntimeAgentImplState::customObjectFormatterEnabled, false))
655 m_session->setCustomObjectFormatterEnabled(true); 738 m_session->setCustomObjectFormatterEnabled(true);
739 if (m_state->booleanProperty(V8RuntimeAgentImplState::preciseCoverageStarted,
740 false))
741 startPreciseCoverage();
656 } 742 }
657 743
658 Response V8RuntimeAgentImpl::enable() { 744 Response V8RuntimeAgentImpl::enable() {
659 if (m_enabled) return Response::OK(); 745 if (m_enabled) return Response::OK();
660 m_inspector->client()->beginEnsureAllContextsInGroup( 746 m_inspector->client()->beginEnsureAllContextsInGroup(
661 m_session->contextGroupId()); 747 m_session->contextGroupId());
662 m_enabled = true; 748 m_enabled = true;
663 m_state->setBoolean(V8RuntimeAgentImplState::runtimeEnabled, true); 749 m_state->setBoolean(V8RuntimeAgentImplState::runtimeEnabled, true);
664 m_inspector->enableStackCapturingIfNeeded(); 750 m_inspector->enableStackCapturingIfNeeded();
665 m_session->reportAllContexts(this); 751 m_session->reportAllContexts(this);
666 V8ConsoleMessageStorage* storage = 752 V8ConsoleMessageStorage* storage =
667 m_inspector->ensureConsoleMessageStorage(m_session->contextGroupId()); 753 m_inspector->ensureConsoleMessageStorage(m_session->contextGroupId());
668 for (const auto& message : storage->messages()) { 754 for (const auto& message : storage->messages()) {
669 if (!reportMessage(message.get(), false)) break; 755 if (!reportMessage(message.get(), false)) break;
670 } 756 }
671 return Response::OK(); 757 return Response::OK();
672 } 758 }
673 759
674 Response V8RuntimeAgentImpl::disable() { 760 Response V8RuntimeAgentImpl::disable() {
675 if (!m_enabled) return Response::OK(); 761 if (!m_enabled) return Response::OK();
676 m_enabled = false; 762 m_enabled = false;
677 m_state->setBoolean(V8RuntimeAgentImplState::runtimeEnabled, false); 763 m_state->setBoolean(V8RuntimeAgentImplState::runtimeEnabled, false);
678 m_inspector->disableStackCapturingIfNeeded(); 764 m_inspector->disableStackCapturingIfNeeded();
679 m_session->discardInjectedScripts(); 765 m_session->discardInjectedScripts();
680 reset(); 766 reset();
681 m_inspector->client()->endEnsureAllContextsInGroup( 767 m_inspector->client()->endEnsureAllContextsInGroup(
682 m_session->contextGroupId()); 768 m_session->contextGroupId());
769 stopPreciseCoverage();
683 return Response::OK(); 770 return Response::OK();
684 } 771 }
685 772
686 void V8RuntimeAgentImpl::reset() { 773 void V8RuntimeAgentImpl::reset() {
687 m_compiledScripts.clear(); 774 m_compiledScripts.clear();
688 if (m_enabled) { 775 if (m_enabled) {
689 if (const V8InspectorImpl::ContextByIdMap* contexts = 776 if (const V8InspectorImpl::ContextByIdMap* contexts =
690 m_inspector->contextGroup(m_session->contextGroupId())) { 777 m_inspector->contextGroup(m_session->contextGroupId())) {
691 for (auto& idContext : *contexts) idContext.second->setReported(false); 778 for (auto& idContext : *contexts) idContext.second->setReported(false);
692 } 779 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
730 } 817 }
731 818
732 bool V8RuntimeAgentImpl::reportMessage(V8ConsoleMessage* message, 819 bool V8RuntimeAgentImpl::reportMessage(V8ConsoleMessage* message,
733 bool generatePreview) { 820 bool generatePreview) {
734 message->reportToFrontend(&m_frontend, m_session, generatePreview); 821 message->reportToFrontend(&m_frontend, m_session, generatePreview);
735 m_frontend.flush(); 822 m_frontend.flush();
736 return m_inspector->hasConsoleMessageStorage(m_session->contextGroupId()); 823 return m_inspector->hasConsoleMessageStorage(m_session->contextGroupId());
737 } 824 }
738 825
739 } // namespace v8_inspector 826 } // namespace v8_inspector
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698