GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/inspector_js_api.cc Lines: 175 178 98.3 %
Date: 2019-02-13 22:28:58 Branches: 54 94 57.4 %

Line Branch Exec Source
1
#include "base_object-inl.h"
2
#include "inspector_agent.h"
3
#include "inspector_io.h"
4
#include "v8.h"
5
#include "v8-inspector.h"
6
7
namespace node {
8
namespace inspector {
9
namespace {
10
11
using v8::Boolean;
12
using v8::Context;
13
using v8::Function;
14
using v8::FunctionCallbackInfo;
15
using v8::FunctionTemplate;
16
using v8::HandleScope;
17
using v8::Isolate;
18
using v8::Local;
19
using v8::MaybeLocal;
20
using v8::NewStringType;
21
using v8::Object;
22
using v8::String;
23
using v8::Uint32;
24
using v8::Value;
25
26
using v8_inspector::StringBuffer;
27
using v8_inspector::StringView;
28
29
13798
std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate,
30
                                               Local<Value> value) {
31
13798
  TwoByteValue buffer(isolate, value);
32
13799
  return StringBuffer::create(StringView(*buffer, buffer.length()));
33
}
34
35
13777
class JSBindingsConnection : public AsyncWrap {
36
 public:
37
9184
  class JSBindingsSessionDelegate : public InspectorSessionDelegate {
38
   public:
39
4601
    JSBindingsSessionDelegate(Environment* env,
40
                              JSBindingsConnection* connection)
41
                              : env_(env),
42
4601
                                connection_(connection) {
43
4601
    }
44
45
16366
    void SendMessageToFrontend(const v8_inspector::StringView& message)
46
        override {
47
16366
      Isolate* isolate = env_->isolate();
48
16367
      HandleScope handle_scope(isolate);
49
16368
      Context::Scope context_scope(env_->context());
50
      MaybeLocal<String> v8string =
51
          String::NewFromTwoByte(isolate, message.characters16(),
52
16368
                                 NewStringType::kNormal, message.length());
53
32736
      Local<Value> argument = v8string.ToLocalChecked().As<Value>();
54
32509
      connection_->OnMessage(argument);
55
16141
    }
56
57
   private:
58
    Environment* env_;
59
    JSBindingsConnection* connection_;
60
  };
61
62
4601
  JSBindingsConnection(Environment* env,
63
                       Local<Object> wrap,
64
                       Local<Function> callback)
65
                       : AsyncWrap(env, wrap, PROVIDER_INSPECTORJSBINDING),
66
4601
                         callback_(env->isolate(), callback) {
67
4601
    Agent* inspector = env->inspector_agent();
68
13803
    session_ = inspector->Connect(std::unique_ptr<JSBindingsSessionDelegate>(
69
9202
        new JSBindingsSessionDelegate(env, this)), false);
70
4601
  }
71
72
16367
  void OnMessage(Local<Value> value) {
73
32734
    MakeCallback(callback_.Get(env()->isolate()), 1, &value);
74
16139
  }
75
76
4601
  static void New(const FunctionCallbackInfo<Value>& info) {
77
4601
    Environment* env = Environment::GetCurrent(info);
78
9202
    CHECK(info[0]->IsFunction());
79
9202
    Local<Function> callback = info[0].As<Function>();
80
4601
    new JSBindingsConnection(env, info.This(), callback);
81
4601
  }
82
83
4549
  void Disconnect() {
84
4549
    session_.reset();
85
4548
    delete this;
86
4549
  }
87
88
4549
  static void Disconnect(const FunctionCallbackInfo<Value>& info) {
89
    JSBindingsConnection* session;
90
9098
    ASSIGN_OR_RETURN_UNWRAP(&session, info.Holder());
91
4549
    session->Disconnect();
92
  }
93
94
13798
  static void Dispatch(const FunctionCallbackInfo<Value>& info) {
95
13798
    Environment* env = Environment::GetCurrent(info);
96
    JSBindingsConnection* session;
97
27371
    ASSIGN_OR_RETURN_UNWRAP(&session, info.Holder());
98
41394
    CHECK(info[0]->IsString());
99
100
13798
    if (session->session_) {
101
13798
      session->session_->Dispatch(
102
27598
          ToProtocolString(env->isolate(), info[0])->string());
103
    }
104
  }
105
106
35
  void MemoryInfo(MemoryTracker* tracker) const override {
107
35
    tracker->TrackField("callback", callback_);
108
    tracker->TrackFieldWithSize(
109
35
        "session", sizeof(*session_), "InspectorSession");
110
35
  }
111
112
35
  SET_MEMORY_INFO_NAME(JSBindingsConnection)
113
35
  SET_SELF_SIZE(JSBindingsConnection)
114
115
 private:
116
  std::unique_ptr<InspectorSession> session_;
117
  Persistent<Function> callback_;
118
};
119
120
138134
static bool InspectorEnabled(Environment* env) {
121
138134
  Agent* agent = env->inspector_agent();
122
138134
  return agent->IsActive();
123
}
124
125
4407
void SetConsoleExtensionInstaller(const FunctionCallbackInfo<Value>& info) {
126
4407
  auto env = Environment::GetCurrent(info);
127
128
4407
  CHECK_EQ(info.Length(), 1);
129
8814
  CHECK(info[0]->IsFunction());
130
131
8814
  env->set_inspector_console_extension_installer(info[0].As<Function>());
132
4407
}
133
134
5
void CallAndPauseOnStart(const FunctionCallbackInfo<v8::Value>& args) {
135
5
  Environment* env = Environment::GetCurrent(args);
136
5
  CHECK_GT(args.Length(), 1);
137
10
  CHECK(args[0]->IsFunction());
138
5
  SlicedArguments call_args(args, /* start */ 2);
139
5
  env->inspector_agent()->PauseOnNextJavascriptStatement("Break on start");
140
  v8::MaybeLocal<v8::Value> retval =
141
9
      args[0].As<v8::Function>()->Call(env->context(), args[1],
142
20
                                       call_args.length(), call_args.out());
143
4
  if (!retval.IsEmpty()) {
144
6
    args.GetReturnValue().Set(retval.ToLocalChecked());
145
4
  }
146
4
}
147
148
138132
void InspectorConsoleCall(const FunctionCallbackInfo<Value>& info) {
149
138132
  Environment* env = Environment::GetCurrent(info);
150
138132
  Isolate* isolate = env->isolate();
151
138132
  Local<Context> context = isolate->GetCurrentContext();
152
138132
  CHECK_LT(2, info.Length());
153
138132
  SlicedArguments call_args(info, /* start */ 3);
154
138132
  if (InspectorEnabled(env)) {
155
137998
    Local<Value> inspector_method = info[0];
156
137998
    CHECK(inspector_method->IsFunction());
157
137998
    Local<Value> config_value = info[2];
158
137998
    CHECK(config_value->IsObject());
159
137998
    Local<Object> config_object = config_value.As<Object>();
160
137998
    Local<String> in_call_key = FIXED_ONE_BYTE_STRING(isolate, "in_call");
161
413994
    if (!config_object->Has(context, in_call_key).FromMaybe(false)) {
162
275994
      CHECK(config_object->Set(context,
163
                               in_call_key,
164
                               v8::True(isolate)).FromJust());
165
551988
      CHECK(!inspector_method.As<Function>()->Call(context,
166
                                                   info.Holder(),
167
                                                   call_args.length(),
168
                                                   call_args.out()).IsEmpty());
169
    }
170
275996
    CHECK(config_object->Delete(context, in_call_key).FromJust());
171
  }
172
173
138132
  Local<Value> node_method = info[1];
174
138132
  CHECK(node_method->IsFunction());
175
  node_method.As<Function>()->Call(context,
176
                                   info.Holder(),
177
138132
                                   call_args.length(),
178
690660
                                   call_args.out()).FromMaybe(Local<Value>());
179
138132
}
180
181
7
static void* GetAsyncTask(int64_t asyncId) {
182
  // The inspector assumes that when other clients use its asyncTask* API,
183
  // they use real pointers, or at least something aligned like real pointer.
184
  // In general it means that our task_id should always be even.
185
  //
186
  // On 32bit platforms, the 64bit asyncId would get truncated when converted
187
  // to a 32bit pointer. However, the javascript part will never enable
188
  // the async_hook on 32bit platforms, therefore the truncation will never
189
  // happen in practice.
190
7
  return reinterpret_cast<void*>(asyncId << 1);
191
}
192
193
template <void (Agent::*asyncTaskFn)(void*)>
194
6
static void InvokeAsyncTaskFnWithId(const FunctionCallbackInfo<Value>& args) {
195
6
  Environment* env = Environment::GetCurrent(args);
196

12
  CHECK(args[0]->IsNumber());
197
24
  int64_t task_id = args[0]->IntegerValue(env->context()).FromJust();
198
6
  (env->inspector_agent()->*asyncTaskFn)(GetAsyncTask(task_id));
199
6
}
200
201
1
static void AsyncTaskScheduledWrapper(const FunctionCallbackInfo<Value>& args) {
202
1
  Environment* env = Environment::GetCurrent(args);
203
204
3
  CHECK(args[0]->IsString());
205
2
  Local<String> task_name = args[0].As<String>();
206
1
  String::Value task_name_value(args.GetIsolate(), task_name);
207
1
  StringView task_name_view(*task_name_value, task_name_value.length());
208
209
2
  CHECK(args[1]->IsNumber());
210
4
  int64_t task_id = args[1]->IntegerValue(env->context()).FromJust();
211
1
  void* task = GetAsyncTask(task_id);
212
213
2
  CHECK(args[2]->IsBoolean());
214
3
  bool recurring = args[2]->BooleanValue(args.GetIsolate());
215
216
1
  env->inspector_agent()->AsyncTaskScheduled(task_name_view, task, recurring);
217
1
}
218
219
4410
static void RegisterAsyncHookWrapper(const FunctionCallbackInfo<Value>& args) {
220
4410
  Environment* env = Environment::GetCurrent(args);
221
222
8820
  CHECK(args[0]->IsFunction());
223
8820
  Local<Function> enable_function = args[0].As<Function>();
224
8820
  CHECK(args[1]->IsFunction());
225
8820
  Local<Function> disable_function = args[1].As<Function>();
226
  env->inspector_agent()->RegisterAsyncHook(env->isolate(),
227
4410
    enable_function, disable_function);
228
4410
}
229
230
2
void IsEnabled(const FunctionCallbackInfo<Value>& args) {
231
2
  Environment* env = Environment::GetCurrent(args);
232
6
  args.GetReturnValue().Set(InspectorEnabled(env));
233
2
}
234
235
3
void Open(const FunctionCallbackInfo<Value>& args) {
236
3
  Environment* env = Environment::GetCurrent(args);
237
3
  Agent* agent = env->inspector_agent();
238
3
  bool wait_for_connect = false;
239
240


12
  if (args.Length() > 0 && args[0]->IsUint32()) {
241
3
    uint32_t port = args[0].As<Uint32>()->Value();
242
1
    agent->host_port()->set_port(static_cast<int>(port));
243
  }
244
245


15
  if (args.Length() > 1 && args[1]->IsString()) {
246
    Utf8Value host(env->isolate(), args[1].As<String>());
247
    agent->host_port()->set_host(*host);
248
  }
249
250


12
  if (args.Length() > 2 && args[2]->IsBoolean()) {
251
9
    wait_for_connect = args[2]->BooleanValue(args.GetIsolate());
252
  }
253
3
  agent->StartIoThread();
254
3
  if (wait_for_connect)
255
    agent->WaitForConnect();
256
3
}
257
258
5
void Url(const FunctionCallbackInfo<Value>& args) {
259
5
  Environment* env = Environment::GetCurrent(args);
260
5
  Agent* agent = env->inspector_agent();
261
5
  InspectorIo* io = agent->io();
262
263
7
  if (!io) return;
264
265
3
  std::vector<std::string> ids = io->GetTargetIds();
266
267
3
  if (ids.empty()) return;
268
269
6
  std::string url = FormatWsAddress(io->host(), io->port(), ids[0], true);
270
12
  args.GetReturnValue().Set(OneByteString(env->isolate(), url.c_str()));
271
}
272
273
4416
void Initialize(Local<Object> target, Local<Value> unused,
274
                Local<Context> context, void* priv) {
275
4416
  Environment* env = Environment::GetCurrent(context);
276
277
4416
  Agent* agent = env->inspector_agent();
278
4416
  env->SetMethod(target, "consoleCall", InspectorConsoleCall);
279
  env->SetMethod(
280
4416
      target, "setConsoleExtensionInstaller", SetConsoleExtensionInstaller);
281
4416
  if (agent->WillWaitForConnect())
282
13
    env->SetMethod(target, "callAndPauseOnStart", CallAndPauseOnStart);
283
4416
  env->SetMethod(target, "open", Open);
284
4416
  env->SetMethodNoSideEffect(target, "url", Url);
285
286
4416
  env->SetMethod(target, "asyncTaskScheduled", AsyncTaskScheduledWrapper);
287
  env->SetMethod(target, "asyncTaskCanceled",
288
4416
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskCanceled>);
289
  env->SetMethod(target, "asyncTaskStarted",
290
4416
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskStarted>);
291
  env->SetMethod(target, "asyncTaskFinished",
292
4416
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskFinished>);
293
294
4416
  env->SetMethod(target, "registerAsyncHook", RegisterAsyncHookWrapper);
295
4416
  env->SetMethodNoSideEffect(target, "isEnabled", IsEnabled);
296
297
4416
  auto conn_str = FIXED_ONE_BYTE_STRING(env->isolate(), "Connection");
298
  Local<FunctionTemplate> tmpl =
299
4416
      env->NewFunctionTemplate(JSBindingsConnection::New);
300
8832
  tmpl->InstanceTemplate()->SetInternalFieldCount(1);
301
4416
  tmpl->SetClassName(conn_str);
302
8832
  tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
303
4416
  env->SetProtoMethod(tmpl, "dispatch", JSBindingsConnection::Dispatch);
304
4416
  env->SetProtoMethod(tmpl, "disconnect", JSBindingsConnection::Disconnect);
305
  target
306
      ->Set(env->context(), conn_str,
307
22080
            tmpl->GetFunction(env->context()).ToLocalChecked())
308
8832
      .ToChecked();
309
4416
}
310
311
}  // namespace
312
}  // namespace inspector
313
}  // namespace node
314
315
4314
NODE_MODULE_CONTEXT_AWARE_INTERNAL(inspector,
316
                                  node::inspector::Initialize);