GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/inspector_coverage.cc Lines: 71 72 98.6 %
Date: 2019-03-02 22:23:06 Branches: 10 16 62.5 %

Line Branch Exec Source
1
#include "base_object-inl.h"
2
#include "debug_utils.h"
3
#include "inspector_agent.h"
4
#include "node_internals.h"
5
#include "v8-inspector.h"
6
7
namespace node {
8
namespace coverage {
9
10
using v8::Context;
11
using v8::Function;
12
using v8::FunctionCallbackInfo;
13
using v8::HandleScope;
14
using v8::Isolate;
15
using v8::Local;
16
using v8::MaybeLocal;
17
using v8::NewStringType;
18
using v8::Object;
19
using v8::ObjectTemplate;
20
using v8::String;
21
using v8::Value;
22
23
using v8_inspector::StringBuffer;
24
using v8_inspector::StringView;
25
26
13485
std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate,
27
                                               Local<Value> value) {
28
13485
  TwoByteValue buffer(isolate, value);
29
13485
  return StringBuffer::create(StringView(*buffer, buffer.length()));
30
}
31
32
8088
class V8CoverageConnection : public BaseObject {
33
 public:
34
8088
  class V8CoverageSessionDelegate : public inspector::InspectorSessionDelegate {
35
   public:
36
4412
    explicit V8CoverageSessionDelegate(V8CoverageConnection* connection)
37
4412
        : connection_(connection) {}
38
39
13485
    void SendMessageToFrontend(
40
        const v8_inspector::StringView& message) override {
41
13485
      Environment* env = connection_->env();
42
13485
      Local<Function> fn = connection_->env()->on_coverage_message_function();
43
13485
      bool ending = !fn.IsEmpty();
44
      Debug(env,
45
            DebugCategory::COVERAGE,
46
            "Sending message to frontend, ending = %s\n",
47
26970
            ending ? "true" : "false");
48
13485
      if (!ending) {
49
22309
        return;
50
      }
51
4661
      Isolate* isolate = env->isolate();
52
53
4661
      HandleScope handle_scope(isolate);
54
4661
      Context::Scope context_scope(env->context());
55
      MaybeLocal<String> v8string =
56
          String::NewFromTwoByte(isolate,
57
                                 message.characters16(),
58
                                 NewStringType::kNormal,
59
4661
                                 message.length());
60
13983
      Local<Value> args[] = {v8string.ToLocalChecked().As<Value>()};
61
      USE(MakeCallback(isolate,
62
                       connection_->object(),
63
                       fn,
64
4661
                       arraysize(args),
65
                       args,
66
13983
                       async_context{0, 0}));
67
    }
68
69
   private:
70
    V8CoverageConnection* connection_;
71
  };
72
73
32
  SET_MEMORY_INFO_NAME(V8CoverageConnection)
74
32
  SET_SELF_SIZE(V8CoverageConnection)
75
76
32
  void MemoryInfo(MemoryTracker* tracker) const override {
77
    tracker->TrackFieldWithSize(
78
32
        "session", sizeof(*session_), "InspectorSession");
79
32
  }
80
81
4412
  explicit V8CoverageConnection(Environment* env)
82
4412
      : BaseObject(env, env->coverage_connection()), session_(nullptr) {
83
4412
    inspector::Agent* inspector = env->inspector_agent();
84
    std::unique_ptr<inspector::InspectorSession> session = inspector->Connect(
85
4412
        std::make_unique<V8CoverageSessionDelegate>(this), false);
86
4412
    session_ = std::move(session);
87
4412
    MakeWeak();
88
4412
  }
89
90
4412
  void Start() {
91
    Debug(this->env(),
92
          DebugCategory::COVERAGE,
93
4412
          "Sending Profiler.startPreciseCoverage\n");
94
4412
    Isolate* isolate = this->env()->isolate();
95
    Local<Value> enable = FIXED_ONE_BYTE_STRING(
96
4412
        isolate, "{\"id\": 1, \"method\": \"Profiler.enable\"}");
97
    Local<Value> start = FIXED_ONE_BYTE_STRING(
98
        isolate,
99
        "{"
100
        "\"id\": 2,"
101
        "\"method\": \"Profiler.startPreciseCoverage\","
102
        "\"params\": {\"callCount\": true, \"detailed\": true}"
103
4412
        "}");
104
4412
    session_->Dispatch(ToProtocolString(isolate, enable)->string());
105
4412
    session_->Dispatch(ToProtocolString(isolate, start)->string());
106
4412
  }
107
108
4661
  void End() {
109
    Debug(this->env(),
110
          DebugCategory::COVERAGE,
111
4661
          "Sending Profiler.takePreciseCoverage\n");
112
4661
    Isolate* isolate = this->env()->isolate();
113
    Local<Value> end =
114
        FIXED_ONE_BYTE_STRING(isolate,
115
                              "{"
116
                              "\"id\": 3,"
117
                              "\"method\": \"Profiler.takePreciseCoverage\""
118
4661
                              "}");
119
4661
    session_->Dispatch(ToProtocolString(isolate, end)->string());
120
4661
  }
121
122
  friend class V8CoverageSessionDelegate;
123
124
 private:
125
  std::unique_ptr<inspector::InspectorSession> session_;
126
};
127
128
4412
bool StartCoverageCollection(Environment* env) {
129
4412
  HandleScope scope(env->isolate());
130
131
4412
  Local<ObjectTemplate> t = ObjectTemplate::New(env->isolate());
132
4412
  t->SetInternalFieldCount(1);
133
  Local<Object> obj;
134
13236
  if (!t->NewInstance(env->context()).ToLocal(&obj)) {
135
    return false;
136
  }
137
138
4412
  obj->SetAlignedPointerInInternalField(0, nullptr);
139
140
8824
  CHECK(env->coverage_connection().IsEmpty());
141
4412
  env->set_coverage_connection(obj);
142
4412
  V8CoverageConnection* connection = new V8CoverageConnection(env);
143
4412
  connection->Start();
144
4412
  return true;
145
}
146
147
4661
static void EndCoverageCollection(const FunctionCallbackInfo<Value>& args) {
148
4661
  Environment* env = Environment::GetCurrent(args);
149
9322
  CHECK(args[0]->IsFunction());
150
  Debug(env, DebugCategory::COVERAGE, "Ending coverage collection\n");
151
9322
  env->set_on_coverage_message_function(args[0].As<Function>());
152
  V8CoverageConnection* connection =
153
4661
      Unwrap<V8CoverageConnection>(env->coverage_connection());
154
4661
  CHECK_NOT_NULL(connection);
155
4661
  connection->End();
156
4661
}
157
158
4364
static void Initialize(Local<Object> target,
159
                       Local<Value> unused,
160
                       Local<Context> context,
161
                       void* priv) {
162
4364
  Environment* env = Environment::GetCurrent(context);
163
4364
  env->SetMethod(target, "end", EndCoverageCollection);
164
4364
}
165
}  // namespace coverage
166
}  // namespace node
167
168
4292
NODE_MODULE_CONTEXT_AWARE_INTERNAL(coverage, node::coverage::Initialize)