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: 176 194 90.7 %
Date: 2020-06-24 22:13:30 Branches: 39 82 47.6 %

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

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

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

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

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

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

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

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