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-02-23 22:23:05 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
13451
std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate,
27
                                               Local<Value> value) {
28
13451
  TwoByteValue buffer(isolate, value);
29
13451
  return StringBuffer::create(StringView(*buffer, buffer.length()));
30
}
31
32
8064
class V8CoverageConnection : public BaseObject {
33
 public:
34
8064
  class V8CoverageSessionDelegate : public inspector::InspectorSessionDelegate {
35
   public:
36
4401
    explicit V8CoverageSessionDelegate(V8CoverageConnection* connection)
37
4401
        : connection_(connection) {}
38
39
13451
    void SendMessageToFrontend(
40
        const v8_inspector::StringView& message) override {
41
13451
      Environment* env = connection_->env();
42
13451
      Local<Function> fn = connection_->env()->on_coverage_message_function();
43
13451
      bool ending = !fn.IsEmpty();
44
      Debug(env,
45
            DebugCategory::COVERAGE,
46
            "Sending message to frontend, ending = %s\n",
47
26902
            ending ? "true" : "false");
48
13451
      if (!ending) {
49
22253
        return;
50
      }
51
4649
      Isolate* isolate = env->isolate();
52
53
4649
      HandleScope handle_scope(isolate);
54
4649
      Context::Scope context_scope(env->context());
55
      MaybeLocal<String> v8string =
56
          String::NewFromTwoByte(isolate,
57
                                 message.characters16(),
58
                                 NewStringType::kNormal,
59
4649
                                 message.length());
60
13947
      Local<Value> args[] = {v8string.ToLocalChecked().As<Value>()};
61
      USE(MakeCallback(isolate,
62
                       connection_->object(),
63
                       fn,
64
4649
                       arraysize(args),
65
                       args,
66
13947
                       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
4401
  explicit V8CoverageConnection(Environment* env)
82
4401
      : BaseObject(env, env->coverage_connection()), session_(nullptr) {
83
4401
    inspector::Agent* inspector = env->inspector_agent();
84
    std::unique_ptr<inspector::InspectorSession> session = inspector->Connect(
85
4401
        std::make_unique<V8CoverageSessionDelegate>(this), false);
86
4401
    session_ = std::move(session);
87
4401
    MakeWeak();
88
4401
  }
89
90
4401
  void Start() {
91
    Debug(this->env(),
92
          DebugCategory::COVERAGE,
93
4401
          "Sending Profiler.startPreciseCoverage\n");
94
4401
    Isolate* isolate = this->env()->isolate();
95
    Local<Value> enable = FIXED_ONE_BYTE_STRING(
96
4401
        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
4401
        "}");
104
4401
    session_->Dispatch(ToProtocolString(isolate, enable)->string());
105
4401
    session_->Dispatch(ToProtocolString(isolate, start)->string());
106
4401
  }
107
108
4649
  void End() {
109
    Debug(this->env(),
110
          DebugCategory::COVERAGE,
111
4649
          "Sending Profiler.takePreciseCoverage\n");
112
4649
    Isolate* isolate = this->env()->isolate();
113
    Local<Value> end =
114
        FIXED_ONE_BYTE_STRING(isolate,
115
                              "{"
116
                              "\"id\": 3,"
117
                              "\"method\": \"Profiler.takePreciseCoverage\""
118
4649
                              "}");
119
4649
    session_->Dispatch(ToProtocolString(isolate, end)->string());
120
4649
  }
121
122
  friend class V8CoverageSessionDelegate;
123
124
 private:
125
  std::unique_ptr<inspector::InspectorSession> session_;
126
};
127
128
4401
bool StartCoverageCollection(Environment* env) {
129
4401
  HandleScope scope(env->isolate());
130
131
4401
  Local<ObjectTemplate> t = ObjectTemplate::New(env->isolate());
132
4401
  t->SetInternalFieldCount(1);
133
  Local<Object> obj;
134
13203
  if (!t->NewInstance(env->context()).ToLocal(&obj)) {
135
    return false;
136
  }
137
138
4401
  obj->SetAlignedPointerInInternalField(0, nullptr);
139
140
8802
  CHECK(env->coverage_connection().IsEmpty());
141
4401
  env->set_coverage_connection(obj);
142
4401
  V8CoverageConnection* connection = new V8CoverageConnection(env);
143
4401
  connection->Start();
144
4401
  return true;
145
}
146
147
4649
static void EndCoverageCollection(const FunctionCallbackInfo<Value>& args) {
148
4649
  Environment* env = Environment::GetCurrent(args);
149
9298
  CHECK(args[0]->IsFunction());
150
  Debug(env, DebugCategory::COVERAGE, "Ending coverage collection\n");
151
9298
  env->set_on_coverage_message_function(args[0].As<Function>());
152
  V8CoverageConnection* connection =
153
4649
      Unwrap<V8CoverageConnection>(env->coverage_connection());
154
4649
  CHECK_NOT_NULL(connection);
155
4649
  connection->End();
156
4649
}
157
158
4353
static void Initialize(Local<Object> target,
159
                       Local<Value> unused,
160
                       Local<Context> context,
161
                       void* priv) {
162
4353
  Environment* env = Environment::GetCurrent(context);
163
4353
  env->SetMethod(target, "end", EndCoverageCollection);
164
4353
}
165
}  // namespace coverage
166
}  // namespace node
167
168
4288
NODE_MODULE_CONTEXT_AWARE_INTERNAL(coverage, node::coverage::Initialize)