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: 197 215 91.6 %
Date: 2020-08-22 22:13:06 Branches: 41 86 47.7 %

Line Branch Exec Source
1
#include "base_object-inl.h"
2
#include "inspector_agent.h"
3
#include "inspector_io.h"
4
#include "memory_tracker-inl.h"
5
#include "node_external_reference.h"
6
#include "util-inl.h"
7
#include "v8-inspector.h"
8
#include "v8.h"
9
10
#include <memory>
11
12
namespace node {
13
namespace inspector {
14
namespace {
15
16
using v8::Context;
17
using v8::Function;
18
using v8::FunctionCallbackInfo;
19
using v8::FunctionTemplate;
20
using v8::Global;
21
using v8::HandleScope;
22
using v8::Isolate;
23
using v8::Local;
24
using v8::MaybeLocal;
25
using v8::NewStringType;
26
using v8::Object;
27
using v8::String;
28
using v8::Uint32;
29
using v8::Value;
30
31
using v8_inspector::StringBuffer;
32
using v8_inspector::StringView;
33
34
495
std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate,
35
                                               Local<Value> value) {
36
990
  TwoByteValue buffer(isolate, value);
37
990
  return StringBuffer::create(StringView(*buffer, buffer.length()));
38
}
39
40
struct LocalConnection {
41
279
  static std::unique_ptr<InspectorSession> Connect(
42
      Agent* inspector, std::unique_ptr<InspectorSessionDelegate> delegate) {
43
279
    return inspector->Connect(std::move(delegate), false);
44
  }
45
46
454
  static Local<String> GetClassName(Environment* env) {
47
454
    return FIXED_ONE_BYTE_STRING(env->isolate(), "Connection");
48
  }
49
};
50
51
struct MainThreadConnection {
52
2
  static std::unique_ptr<InspectorSession> Connect(
53
      Agent* inspector, std::unique_ptr<InspectorSessionDelegate> delegate) {
54
2
    return inspector->ConnectToMainThread(std::move(delegate), true);
55
  }
56
57
454
  static Local<String> GetClassName(Environment* env) {
58
454
    return FIXED_ONE_BYTE_STRING(env->isolate(), "MainThreadConnection");
59
  }
60
};
61
62
template <typename ConnectionType>
63
843
class JSBindingsConnection : public AsyncWrap {
64
 public:
65
562
  class JSBindingsSessionDelegate : public InspectorSessionDelegate {
66
   public:
67
281
    JSBindingsSessionDelegate(Environment* env,
68
                              JSBindingsConnection* connection)
69
                              : env_(env),
70
281
                                connection_(connection) {
71
281
    }
72
73
5017
    void SendMessageToFrontend(const v8_inspector::StringView& message)
74
        override {
75
5017
      Isolate* isolate = env_->isolate();
76
10034
      HandleScope handle_scope(isolate);
77
5017
      Context::Scope context_scope(env_->context());
78
      MaybeLocal<String> v8string =
79
5017
          String::NewFromTwoByte(isolate, message.characters16(),
80
10034
                                 NewStringType::kNormal, message.length());
81
10034
      Local<Value> argument = v8string.ToLocalChecked().As<Value>();
82
5017
      connection_->OnMessage(argument);
83
5017
    }
84
85
   private:
86
    Environment* env_;
87
    BaseObjectPtr<JSBindingsConnection> connection_;
88
  };
89
90
281
  JSBindingsConnection(Environment* env,
91
                       Local<Object> wrap,
92
                       Local<Function> callback)
93
                       : AsyncWrap(env, wrap, PROVIDER_INSPECTORJSBINDING),
94
281
                         callback_(env->isolate(), callback) {
95
281
    Agent* inspector = env->inspector_agent();
96
281
    session_ = ConnectionType::Connect(
97
        inspector, std::make_unique<JSBindingsSessionDelegate>(env, this));
98
281
  }
99
100
5017
  void OnMessage(Local<Value> value) {
101
10034
    MakeCallback(callback_.Get(env()->isolate()), 1, &value);
102
5017
  }
103
104
908
  static void Bind(Environment* env, Local<Object> target) {
105
908
    Local<String> class_name = ConnectionType::GetClassName(env);
106
    Local<FunctionTemplate> tmpl =
107
908
        env->NewFunctionTemplate(JSBindingsConnection::New);
108
1816
    tmpl->InstanceTemplate()->SetInternalFieldCount(
109
        JSBindingsConnection::kInternalFieldCount);
110
908
    tmpl->SetClassName(class_name);
111
1816
    tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
112
908
    env->SetProtoMethod(tmpl, "dispatch", JSBindingsConnection::Dispatch);
113
908
    env->SetProtoMethod(tmpl, "disconnect", JSBindingsConnection::Disconnect);
114
2724
    target->Set(env->context(),
115
                class_name,
116
2724
                tmpl->GetFunction(env->context()).ToLocalChecked())
117
        .ToChecked();
118
908
  }
119
120
281
  static void New(const FunctionCallbackInfo<Value>& info) {
121
281
    Environment* env = Environment::GetCurrent(info);
122

562
    CHECK(info[0]->IsFunction());
123
562
    Local<Function> callback = info[0].As<Function>();
124
281
    new JSBindingsConnection(env, info.This(), callback);
125
281
  }
126
127
267
  void Disconnect() {
128
267
    session_.reset();
129

267
    delete this;
130
267
  }
131
132
267
  static void Disconnect(const FunctionCallbackInfo<Value>& info) {
133
    JSBindingsConnection* session;
134

267
    ASSIGN_OR_RETURN_UNWRAP(&session, info.Holder());
135
267
    session->Disconnect();
136
  }
137
138
495
  static void Dispatch(const FunctionCallbackInfo<Value>& info) {
139
495
    Environment* env = Environment::GetCurrent(info);
140
    JSBindingsConnection* session;
141

495
    ASSIGN_OR_RETURN_UNWRAP(&session, info.Holder());
142

1485
    CHECK(info[0]->IsString());
143
144

495
    if (session->session_) {
145
990
      session->session_->Dispatch(
146
990
          ToProtocolString(env->isolate(), info[0])->string());
147
    }
148
  }
149
150
2
  void MemoryInfo(MemoryTracker* tracker) const override {
151
2
    tracker->TrackField("callback", callback_);
152
2
    tracker->TrackFieldWithSize(
153
        "session", sizeof(*session_), "InspectorSession");
154
2
  }
155
156
2
  SET_MEMORY_INFO_NAME(JSBindingsConnection)
157
2
  SET_SELF_SIZE(JSBindingsConnection)
158
159
 private:
160
  std::unique_ptr<InspectorSession> session_;
161
  Global<Function> callback_;
162
};
163
164
40518
static bool InspectorEnabled(Environment* env) {
165
40518
  Agent* agent = env->inspector_agent();
166
40518
  return agent->IsActive();
167
}
168
169
454
void SetConsoleExtensionInstaller(const FunctionCallbackInfo<Value>& info) {
170
454
  auto env = Environment::GetCurrent(info);
171
172
454
  CHECK_EQ(info.Length(), 1);
173
908
  CHECK(info[0]->IsFunction());
174
175
908
  env->set_inspector_console_extension_installer(info[0].As<Function>());
176
454
}
177
178
8
void CallAndPauseOnStart(const FunctionCallbackInfo<v8::Value>& args) {
179
8
  Environment* env = Environment::GetCurrent(args);
180
8
  CHECK_GT(args.Length(), 1);
181
16
  CHECK(args[0]->IsFunction());
182
14
  SlicedArguments call_args(args, /* start */ 2);
183
8
  env->inspector_agent()->PauseOnNextJavascriptStatement("Break on start");
184
  v8::MaybeLocal<v8::Value> retval =
185
22
      args[0].As<v8::Function>()->Call(env->context(), args[1],
186
24
                                       call_args.length(), call_args.out());
187
6
  if (!retval.IsEmpty()) {
188
10
    args.GetReturnValue().Set(retval.ToLocalChecked());
189
  }
190
6
}
191
192
40513
void InspectorConsoleCall(const FunctionCallbackInfo<Value>& info) {
193
40513
  Environment* env = Environment::GetCurrent(info);
194
40513
  Isolate* isolate = env->isolate();
195
40513
  Local<Context> context = isolate->GetCurrentContext();
196
40513
  CHECK_GE(info.Length(), 2);
197
81017
  SlicedArguments call_args(info, /* start */ 2);
198
40513
  if (InspectorEnabled(env)) {
199
40480
    Local<Value> inspector_method = info[0];
200
40480
    CHECK(inspector_method->IsFunction());
201
40480
    if (!env->is_in_inspector_console_call()) {
202
40479
      env->set_is_in_inspector_console_call(true);
203
      MaybeLocal<Value> ret =
204
80958
          inspector_method.As<Function>()->Call(context,
205
                                                info.Holder(),
206
40479
                                                call_args.length(),
207
161916
                                                call_args.out());
208
40479
      env->set_is_in_inspector_console_call(false);
209
40479
      if (ret.IsEmpty())
210
9
        return;
211
    }
212
  }
213
214
40504
  Local<Value> node_method = info[1];
215
40504
  CHECK(node_method->IsFunction());
216
121512
  node_method.As<Function>()->Call(context,
217
                                   info.Holder(),
218
40504
                                   call_args.length(),
219
162016
                                   call_args.out()).FromMaybe(Local<Value>());
220
}
221
222
7
static void* GetAsyncTask(int64_t asyncId) {
223
  // The inspector assumes that when other clients use its asyncTask* API,
224
  // they use real pointers, or at least something aligned like real pointer.
225
  // In general it means that our task_id should always be even.
226
  //
227
  // On 32bit platforms, the 64bit asyncId would get truncated when converted
228
  // to a 32bit pointer. However, the javascript part will never enable
229
  // the async_hook on 32bit platforms, therefore the truncation will never
230
  // happen in practice.
231
7
  return reinterpret_cast<void*>(asyncId << 1);
232
}
233
234
template <void (Agent::*asyncTaskFn)(void*)>
235
6
static void InvokeAsyncTaskFnWithId(const FunctionCallbackInfo<Value>& args) {
236
6
  Environment* env = Environment::GetCurrent(args);
237

12
  CHECK(args[0]->IsNumber());
238
24
  int64_t task_id = args[0]->IntegerValue(env->context()).FromJust();
239
6
  (env->inspector_agent()->*asyncTaskFn)(GetAsyncTask(task_id));
240
6
}
241
242
1
static void AsyncTaskScheduledWrapper(const FunctionCallbackInfo<Value>& args) {
243
1
  Environment* env = Environment::GetCurrent(args);
244
245
3
  CHECK(args[0]->IsString());
246
2
  Local<String> task_name = args[0].As<String>();
247
2
  String::Value task_name_value(args.GetIsolate(), task_name);
248
1
  StringView task_name_view(*task_name_value, task_name_value.length());
249
250
2
  CHECK(args[1]->IsNumber());
251
4
  int64_t task_id = args[1]->IntegerValue(env->context()).FromJust();
252
1
  void* task = GetAsyncTask(task_id);
253
254
2
  CHECK(args[2]->IsBoolean());
255
3
  bool recurring = args[2]->BooleanValue(args.GetIsolate());
256
257
1
  env->inspector_agent()->AsyncTaskScheduled(task_name_view, task, recurring);
258
1
}
259
260
4819
static void RegisterAsyncHookWrapper(const FunctionCallbackInfo<Value>& args) {
261
4819
  Environment* env = Environment::GetCurrent(args);
262
263
9638
  CHECK(args[0]->IsFunction());
264
9638
  Local<Function> enable_function = args[0].As<Function>();
265
9638
  CHECK(args[1]->IsFunction());
266
9638
  Local<Function> disable_function = args[1].As<Function>();
267
4819
  env->inspector_agent()->RegisterAsyncHook(env->isolate(),
268
4819
    enable_function, disable_function);
269
4819
}
270
271
5
void IsEnabled(const FunctionCallbackInfo<Value>& args) {
272
5
  Environment* env = Environment::GetCurrent(args);
273
15
  args.GetReturnValue().Set(InspectorEnabled(env));
274
5
}
275
276
void Open(const FunctionCallbackInfo<Value>& args) {
277
  Environment* env = Environment::GetCurrent(args);
278
  Agent* agent = env->inspector_agent();
279
280
  if (args.Length() > 0 && args[0]->IsUint32()) {
281
    uint32_t port = args[0].As<Uint32>()->Value();
282
    ExclusiveAccess<HostPort>::Scoped host_port(agent->host_port());
283
    host_port->set_port(static_cast<int>(port));
284
  }
285
286
  if (args.Length() > 1 && args[1]->IsString()) {
287
    Utf8Value host(env->isolate(), args[1].As<String>());
288
    ExclusiveAccess<HostPort>::Scoped host_port(agent->host_port());
289
    host_port->set_host(*host);
290
  }
291
292
  agent->StartIoThread();
293
}
294
295
void WaitForDebugger(const FunctionCallbackInfo<Value>& args) {
296
  Environment* env = Environment::GetCurrent(args);
297
  Agent* agent = env->inspector_agent();
298
  if (agent->IsActive())
299
    agent->WaitForConnect();
300
  args.GetReturnValue().Set(agent->IsActive());
301
}
302
303
6
void Url(const FunctionCallbackInfo<Value>& args) {
304
6
  Environment* env = Environment::GetCurrent(args);
305
11
  std::string url = env->inspector_agent()->GetWsUrl();
306
6
  if (url.empty()) {
307
1
    return;
308
  }
309
15
  args.GetReturnValue().Set(OneByteString(env->isolate(), url.c_str()));
310
}
311
312
454
void Initialize(Local<Object> target, Local<Value> unused,
313
                Local<Context> context, void* priv) {
314
454
  Environment* env = Environment::GetCurrent(context);
315
316
  v8::Local<v8::Function> consoleCallFunc =
317
908
      env->NewFunctionTemplate(InspectorConsoleCall, v8::Local<v8::Signature>(),
318
                               v8::ConstructorBehavior::kThrow,
319
454
                               v8::SideEffectType::kHasSideEffect)
320
908
          ->GetFunction(context)
321
454
          .ToLocalChecked();
322
454
  auto name_string = FIXED_ONE_BYTE_STRING(env->isolate(), "consoleCall");
323
908
  target->Set(context, name_string, consoleCallFunc).Check();
324
454
  consoleCallFunc->SetName(name_string);
325
326
  env->SetMethod(
327
454
      target, "setConsoleExtensionInstaller", SetConsoleExtensionInstaller);
328
454
  env->SetMethod(target, "callAndPauseOnStart", CallAndPauseOnStart);
329
454
  env->SetMethod(target, "open", Open);
330
454
  env->SetMethodNoSideEffect(target, "url", Url);
331
454
  env->SetMethod(target, "waitForDebugger", WaitForDebugger);
332
333
454
  env->SetMethod(target, "asyncTaskScheduled", AsyncTaskScheduledWrapper);
334
  env->SetMethod(target, "asyncTaskCanceled",
335
454
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskCanceled>);
336
  env->SetMethod(target, "asyncTaskStarted",
337
454
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskStarted>);
338
  env->SetMethod(target, "asyncTaskFinished",
339
454
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskFinished>);
340
341
454
  env->SetMethod(target, "registerAsyncHook", RegisterAsyncHookWrapper);
342
454
  env->SetMethodNoSideEffect(target, "isEnabled", IsEnabled);
343
344
454
  JSBindingsConnection<LocalConnection>::Bind(env, target);
345
454
  JSBindingsConnection<MainThreadConnection>::Bind(env, target);
346
454
}
347
348
}  // namespace
349
350
4395
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
351
4395
  registry->Register(InspectorConsoleCall);
352
4395
  registry->Register(SetConsoleExtensionInstaller);
353
4395
  registry->Register(CallAndPauseOnStart);
354
4395
  registry->Register(Open);
355
4395
  registry->Register(Url);
356
4395
  registry->Register(WaitForDebugger);
357
358
4395
  registry->Register(AsyncTaskScheduledWrapper);
359
4395
  registry->Register(InvokeAsyncTaskFnWithId<&Agent::AsyncTaskCanceled>);
360
4395
  registry->Register(InvokeAsyncTaskFnWithId<&Agent::AsyncTaskStarted>);
361
4395
  registry->Register(InvokeAsyncTaskFnWithId<&Agent::AsyncTaskFinished>);
362
363
4395
  registry->Register(RegisterAsyncHookWrapper);
364
4395
  registry->Register(IsEnabled);
365
366
4395
  registry->Register(JSBindingsConnection<LocalConnection>::New);
367
4395
  registry->Register(JSBindingsConnection<LocalConnection>::Dispatch);
368
4395
  registry->Register(JSBindingsConnection<LocalConnection>::Disconnect);
369
4395
  registry->Register(JSBindingsConnection<MainThreadConnection>::New);
370
4395
  registry->Register(JSBindingsConnection<MainThreadConnection>::Dispatch);
371
4395
  registry->Register(JSBindingsConnection<MainThreadConnection>::Disconnect);
372
4395
}
373
374
}  // namespace inspector
375
}  // namespace node
376
377
4464
NODE_MODULE_CONTEXT_AWARE_INTERNAL(inspector,
378
                                  node::inspector::Initialize)
379

17790
NODE_MODULE_EXTERNAL_REFERENCE(inspector,
380
                               node::inspector::RegisterExternalReferences)