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-09-06 22:14:11 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
1025
std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate,
35
                                               Local<Value> value) {
36
2050
  TwoByteValue buffer(isolate, value);
37
2050
  return StringBuffer::create(StringView(*buffer, buffer.length()));
38
}
39
40
struct LocalConnection {
41
767
  static std::unique_ptr<InspectorSession> Connect(
42
      Agent* inspector, std::unique_ptr<InspectorSessionDelegate> delegate) {
43
767
    return inspector->Connect(std::move(delegate), false);
44
  }
45
46
453
  static Local<String> GetClassName(Environment* env) {
47
453
    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
453
  static Local<String> GetClassName(Environment* env) {
58
453
    return FIXED_ONE_BYTE_STRING(env->isolate(), "MainThreadConnection");
59
  }
60
};
61
62
template <typename ConnectionType>
63
2307
class JSBindingsConnection : public AsyncWrap {
64
 public:
65
1538
  class JSBindingsSessionDelegate : public InspectorSessionDelegate {
66
   public:
67
769
    JSBindingsSessionDelegate(Environment* env,
68
                              JSBindingsConnection* connection)
69
                              : env_(env),
70
769
                                connection_(connection) {
71
769
    }
72
73
5547
    void SendMessageToFrontend(const v8_inspector::StringView& message)
74
        override {
75
5547
      Isolate* isolate = env_->isolate();
76
11094
      HandleScope handle_scope(isolate);
77
5547
      Context::Scope context_scope(env_->context());
78
      MaybeLocal<String> v8string =
79
5547
          String::NewFromTwoByte(isolate, message.characters16(),
80
11094
                                 NewStringType::kNormal, message.length());
81
11094
      Local<Value> argument = v8string.ToLocalChecked().As<Value>();
82
5547
      connection_->OnMessage(argument);
83
5547
    }
84
85
   private:
86
    Environment* env_;
87
    BaseObjectPtr<JSBindingsConnection> connection_;
88
  };
89
90
769
  JSBindingsConnection(Environment* env,
91
                       Local<Object> wrap,
92
                       Local<Function> callback)
93
                       : AsyncWrap(env, wrap, PROVIDER_INSPECTORJSBINDING),
94
769
                         callback_(env->isolate(), callback) {
95
769
    Agent* inspector = env->inspector_agent();
96
769
    session_ = ConnectionType::Connect(
97
        inspector, std::make_unique<JSBindingsSessionDelegate>(env, this));
98
769
  }
99
100
5547
  void OnMessage(Local<Value> value) {
101
11094
    MakeCallback(callback_.Get(env()->isolate()), 1, &value);
102
5547
  }
103
104
906
  static void Bind(Environment* env, Local<Object> target) {
105
906
    Local<String> class_name = ConnectionType::GetClassName(env);
106
    Local<FunctionTemplate> tmpl =
107
906
        env->NewFunctionTemplate(JSBindingsConnection::New);
108
1812
    tmpl->InstanceTemplate()->SetInternalFieldCount(
109
        JSBindingsConnection::kInternalFieldCount);
110
906
    tmpl->SetClassName(class_name);
111
1812
    tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
112
906
    env->SetProtoMethod(tmpl, "dispatch", JSBindingsConnection::Dispatch);
113
906
    env->SetProtoMethod(tmpl, "disconnect", JSBindingsConnection::Disconnect);
114
2718
    target->Set(env->context(),
115
                class_name,
116
2718
                tmpl->GetFunction(env->context()).ToLocalChecked())
117
        .ToChecked();
118
906
  }
119
120
769
  static void New(const FunctionCallbackInfo<Value>& info) {
121
769
    Environment* env = Environment::GetCurrent(info);
122

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

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

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

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

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

1025
    if (session->session_) {
145
2050
      session->session_->Dispatch(
146
2050
          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
40537
static bool InspectorEnabled(Environment* env) {
165
40537
  Agent* agent = env->inspector_agent();
166
40537
  return agent->IsActive();
167
}
168
169
453
void SetConsoleExtensionInstaller(const FunctionCallbackInfo<Value>& info) {
170
453
  auto env = Environment::GetCurrent(info);
171
172
453
  CHECK_EQ(info.Length(), 1);
173
906
  CHECK(info[0]->IsFunction());
174
175
906
  env->set_inspector_console_extension_installer(info[0].As<Function>());
176
453
}
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
40532
void InspectorConsoleCall(const FunctionCallbackInfo<Value>& info) {
193
40532
  Environment* env = Environment::GetCurrent(info);
194
40532
  Isolate* isolate = env->isolate();
195
40532
  Local<Context> context = isolate->GetCurrentContext();
196
40532
  CHECK_GE(info.Length(), 2);
197
81055
  SlicedArguments call_args(info, /* start */ 2);
198
40532
  if (InspectorEnabled(env)) {
199
40499
    Local<Value> inspector_method = info[0];
200
40499
    CHECK(inspector_method->IsFunction());
201
40499
    if (!env->is_in_inspector_console_call()) {
202
40498
      env->set_is_in_inspector_console_call(true);
203
      MaybeLocal<Value> ret =
204
80996
          inspector_method.As<Function>()->Call(context,
205
                                                info.Holder(),
206
40498
                                                call_args.length(),
207
161992
                                                call_args.out());
208
40498
      env->set_is_in_inspector_console_call(false);
209
40498
      if (ret.IsEmpty())
210
9
        return;
211
    }
212
  }
213
214
40523
  Local<Value> node_method = info[1];
215
40523
  CHECK(node_method->IsFunction());
216
121569
  node_method.As<Function>()->Call(context,
217
                                   info.Holder(),
218
40523
                                   call_args.length(),
219
162092
                                   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
4828
static void RegisterAsyncHookWrapper(const FunctionCallbackInfo<Value>& args) {
261
4828
  Environment* env = Environment::GetCurrent(args);
262
263
9656
  CHECK(args[0]->IsFunction());
264
9656
  Local<Function> enable_function = args[0].As<Function>();
265
9656
  CHECK(args[1]->IsFunction());
266
9656
  Local<Function> disable_function = args[1].As<Function>();
267
4828
  env->inspector_agent()->RegisterAsyncHook(env->isolate(),
268
4828
    enable_function, disable_function);
269
4828
}
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
453
void Initialize(Local<Object> target, Local<Value> unused,
313
                Local<Context> context, void* priv) {
314
453
  Environment* env = Environment::GetCurrent(context);
315
316
  v8::Local<v8::Function> consoleCallFunc =
317
906
      env->NewFunctionTemplate(InspectorConsoleCall, v8::Local<v8::Signature>(),
318
                               v8::ConstructorBehavior::kThrow,
319
453
                               v8::SideEffectType::kHasSideEffect)
320
906
          ->GetFunction(context)
321
453
          .ToLocalChecked();
322
453
  auto name_string = FIXED_ONE_BYTE_STRING(env->isolate(), "consoleCall");
323
906
  target->Set(context, name_string, consoleCallFunc).Check();
324
453
  consoleCallFunc->SetName(name_string);
325
326
  env->SetMethod(
327
453
      target, "setConsoleExtensionInstaller", SetConsoleExtensionInstaller);
328
453
  env->SetMethod(target, "callAndPauseOnStart", CallAndPauseOnStart);
329
453
  env->SetMethod(target, "open", Open);
330
453
  env->SetMethodNoSideEffect(target, "url", Url);
331
453
  env->SetMethod(target, "waitForDebugger", WaitForDebugger);
332
333
453
  env->SetMethod(target, "asyncTaskScheduled", AsyncTaskScheduledWrapper);
334
  env->SetMethod(target, "asyncTaskCanceled",
335
453
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskCanceled>);
336
  env->SetMethod(target, "asyncTaskStarted",
337
453
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskStarted>);
338
  env->SetMethod(target, "asyncTaskFinished",
339
453
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskFinished>);
340
341
453
  env->SetMethod(target, "registerAsyncHook", RegisterAsyncHookWrapper);
342
453
  env->SetMethodNoSideEffect(target, "isEnabled", IsEnabled);
343
344
453
  JSBindingsConnection<LocalConnection>::Bind(env, target);
345
453
  JSBindingsConnection<MainThreadConnection>::Bind(env, target);
346
453
}
347
348
}  // namespace
349
350
4403
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
351
4403
  registry->Register(InspectorConsoleCall);
352
4403
  registry->Register(SetConsoleExtensionInstaller);
353
4403
  registry->Register(CallAndPauseOnStart);
354
4403
  registry->Register(Open);
355
4403
  registry->Register(Url);
356
4403
  registry->Register(WaitForDebugger);
357
358
4403
  registry->Register(AsyncTaskScheduledWrapper);
359
4403
  registry->Register(InvokeAsyncTaskFnWithId<&Agent::AsyncTaskCanceled>);
360
4403
  registry->Register(InvokeAsyncTaskFnWithId<&Agent::AsyncTaskStarted>);
361
4403
  registry->Register(InvokeAsyncTaskFnWithId<&Agent::AsyncTaskFinished>);
362
363
4403
  registry->Register(RegisterAsyncHookWrapper);
364
4403
  registry->Register(IsEnabled);
365
366
4403
  registry->Register(JSBindingsConnection<LocalConnection>::New);
367
4403
  registry->Register(JSBindingsConnection<LocalConnection>::Dispatch);
368
4403
  registry->Register(JSBindingsConnection<LocalConnection>::Disconnect);
369
4403
  registry->Register(JSBindingsConnection<MainThreadConnection>::New);
370
4403
  registry->Register(JSBindingsConnection<MainThreadConnection>::Dispatch);
371
4403
  registry->Register(JSBindingsConnection<MainThreadConnection>::Disconnect);
372
4403
}
373
374
}  // namespace inspector
375
}  // namespace node
376
377
4472
NODE_MODULE_CONTEXT_AWARE_INTERNAL(inspector,
378
                                  node::inspector::Initialize)
379

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