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: 109 171 63.7 %
Date: 2019-02-01 22:03:38 Branches: 23 94 24.5 %

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
489
std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate,
30
                                               Local<Value> value) {
31
489
  TwoByteValue buffer(isolate, value);
32
489
  return StringBuffer::create(StringView(*buffer, buffer.length()));
33
}
34
35
489
class JSBindingsConnection : public AsyncWrap {
36
 public:
37
326
  class JSBindingsSessionDelegate : public InspectorSessionDelegate {
38
   public:
39
163
    JSBindingsSessionDelegate(Environment* env,
40
                              JSBindingsConnection* connection)
41
                              : env_(env),
42
163
                                connection_(connection) {
43
163
    }
44
45
489
    void SendMessageToFrontend(const v8_inspector::StringView& message)
46
        override {
47
489
      Isolate* isolate = env_->isolate();
48
489
      HandleScope handle_scope(isolate);
49
489
      Context::Scope context_scope(env_->context());
50
      MaybeLocal<String> v8string =
51
          String::NewFromTwoByte(isolate, message.characters16(),
52
489
                                 NewStringType::kNormal, message.length());
53
978
      Local<Value> argument = v8string.ToLocalChecked().As<Value>();
54
978
      connection_->OnMessage(argument);
55
489
    }
56
57
   private:
58
    Environment* env_;
59
    JSBindingsConnection* connection_;
60
  };
61
62
163
  JSBindingsConnection(Environment* env,
63
                       Local<Object> wrap,
64
                       Local<Function> callback)
65
                       : AsyncWrap(env, wrap, PROVIDER_INSPECTORJSBINDING),
66
163
                         callback_(env->isolate(), callback) {
67
163
    Agent* inspector = env->inspector_agent();
68
489
    session_ = inspector->Connect(std::unique_ptr<JSBindingsSessionDelegate>(
69
326
        new JSBindingsSessionDelegate(env, this)), false);
70
163
  }
71
72
489
  void OnMessage(Local<Value> value) {
73
978
    MakeCallback(callback_.Get(env()->isolate()), 1, &value);
74
489
  }
75
76
163
  static void New(const FunctionCallbackInfo<Value>& info) {
77
163
    Environment* env = Environment::GetCurrent(info);
78
326
    CHECK(info[0]->IsFunction());
79
326
    Local<Function> callback = info[0].As<Function>();
80
163
    new JSBindingsConnection(env, info.This(), callback);
81
163
  }
82
83
163
  void Disconnect() {
84
163
    session_.reset();
85
163
    delete this;
86
163
  }
87
88
163
  static void Disconnect(const FunctionCallbackInfo<Value>& info) {
89
    JSBindingsConnection* session;
90
326
    ASSIGN_OR_RETURN_UNWRAP(&session, info.Holder());
91
163
    session->Disconnect();
92
  }
93
94
489
  static void Dispatch(const FunctionCallbackInfo<Value>& info) {
95
489
    Environment* env = Environment::GetCurrent(info);
96
    JSBindingsConnection* session;
97
978
    ASSIGN_OR_RETURN_UNWRAP(&session, info.Holder());
98
1467
    CHECK(info[0]->IsString());
99
100
489
    if (session->session_) {
101
489
      session->session_->Dispatch(
102
978
          ToProtocolString(env->isolate(), info[0])->string());
103
    }
104
  }
105
106
  void MemoryInfo(MemoryTracker* tracker) const override {
107
    tracker->TrackField("callback", callback_);
108
    tracker->TrackFieldWithSize(
109
        "session", sizeof(*session_), "InspectorSession");
110
  }
111
112
  SET_MEMORY_INFO_NAME(JSBindingsConnection)
113
  SET_SELF_SIZE(JSBindingsConnection)
114
115
 private:
116
  std::unique_ptr<InspectorSession> session_;
117
  Persistent<Function> callback_;
118
};
119
120
137
static bool InspectorEnabled(Environment* env) {
121
137
  Agent* agent = env->inspector_agent();
122
137
  return agent->IsActive();
123
}
124
125
164
void SetConsoleExtensionInstaller(const FunctionCallbackInfo<Value>& info) {
126
164
  auto env = Environment::GetCurrent(info);
127
128
164
  CHECK_EQ(info.Length(), 1);
129
328
  CHECK(info[0]->IsFunction());
130
131
328
  env->set_inspector_console_extension_installer(info[0].As<Function>());
132
164
}
133
134
void CallAndPauseOnStart(const FunctionCallbackInfo<v8::Value>& args) {
135
  Environment* env = Environment::GetCurrent(args);
136
  CHECK_GT(args.Length(), 1);
137
  CHECK(args[0]->IsFunction());
138
  SlicedArguments call_args(args, /* start */ 2);
139
  env->inspector_agent()->PauseOnNextJavascriptStatement("Break on start");
140
  v8::MaybeLocal<v8::Value> retval =
141
      args[0].As<v8::Function>()->Call(env->context(), args[1],
142
                                       call_args.length(), call_args.out());
143
  if (!retval.IsEmpty()) {
144
    args.GetReturnValue().Set(retval.ToLocalChecked());
145
  }
146
}
147
148
137
void InspectorConsoleCall(const FunctionCallbackInfo<Value>& info) {
149
137
  Environment* env = Environment::GetCurrent(info);
150
137
  Isolate* isolate = env->isolate();
151
137
  Local<Context> context = isolate->GetCurrentContext();
152
137
  CHECK_LT(2, info.Length());
153
137
  SlicedArguments call_args(info, /* start */ 3);
154
137
  if (InspectorEnabled(env)) {
155
132
    Local<Value> inspector_method = info[0];
156
132
    CHECK(inspector_method->IsFunction());
157
132
    Local<Value> config_value = info[2];
158
132
    CHECK(config_value->IsObject());
159
132
    Local<Object> config_object = config_value.As<Object>();
160
132
    Local<String> in_call_key = FIXED_ONE_BYTE_STRING(isolate, "in_call");
161
396
    if (!config_object->Has(context, in_call_key).FromMaybe(false)) {
162
264
      CHECK(config_object->Set(context,
163
                               in_call_key,
164
                               v8::True(isolate)).FromJust());
165
528
      CHECK(!inspector_method.As<Function>()->Call(context,
166
                                                   info.Holder(),
167
                                                   call_args.length(),
168
                                                   call_args.out()).IsEmpty());
169
    }
170
264
    CHECK(config_object->Delete(context, in_call_key).FromJust());
171
  }
172
173
137
  Local<Value> node_method = info[1];
174
137
  CHECK(node_method->IsFunction());
175
  node_method.As<Function>()->Call(context,
176
                                   info.Holder(),
177
137
                                   call_args.length(),
178
685
                                   call_args.out()).FromMaybe(Local<Value>());
179
137
}
180
181
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
  return reinterpret_cast<void*>(asyncId << 1);
191
}
192
193
template <void (Agent::*asyncTaskFn)(void*)>
194
static void InvokeAsyncTaskFnWithId(const FunctionCallbackInfo<Value>& args) {
195
  Environment* env = Environment::GetCurrent(args);
196
  CHECK(args[0]->IsNumber());
197
  int64_t task_id = args[0]->IntegerValue(env->context()).FromJust();
198
  (env->inspector_agent()->*asyncTaskFn)(GetAsyncTask(task_id));
199
}
200
201
static void AsyncTaskScheduledWrapper(const FunctionCallbackInfo<Value>& args) {
202
  Environment* env = Environment::GetCurrent(args);
203
204
  CHECK(args[0]->IsString());
205
  Local<String> task_name = args[0].As<String>();
206
  String::Value task_name_value(args.GetIsolate(), task_name);
207
  StringView task_name_view(*task_name_value, task_name_value.length());
208
209
  CHECK(args[1]->IsNumber());
210
  int64_t task_id = args[1]->IntegerValue(env->context()).FromJust();
211
  void* task = GetAsyncTask(task_id);
212
213
  CHECK(args[2]->IsBoolean());
214
  bool recurring = args[2]->BooleanValue(args.GetIsolate());
215
216
  env->inspector_agent()->AsyncTaskScheduled(task_name_view, task, recurring);
217
}
218
219
164
static void RegisterAsyncHookWrapper(const FunctionCallbackInfo<Value>& args) {
220
164
  Environment* env = Environment::GetCurrent(args);
221
222
328
  CHECK(args[0]->IsFunction());
223
328
  Local<Function> enable_function = args[0].As<Function>();
224
328
  CHECK(args[1]->IsFunction());
225
328
  Local<Function> disable_function = args[1].As<Function>();
226
  env->inspector_agent()->RegisterAsyncHook(env->isolate(),
227
164
    enable_function, disable_function);
228
164
}
229
230
void IsEnabled(const FunctionCallbackInfo<Value>& args) {
231
  Environment* env = Environment::GetCurrent(args);
232
  args.GetReturnValue().Set(InspectorEnabled(env));
233
}
234
235
void Open(const FunctionCallbackInfo<Value>& args) {
236
  Environment* env = Environment::GetCurrent(args);
237
  Agent* agent = env->inspector_agent();
238
  bool wait_for_connect = false;
239
240
  if (args.Length() > 0 && args[0]->IsUint32()) {
241
    uint32_t port = args[0].As<Uint32>()->Value();
242
    agent->host_port()->set_port(static_cast<int>(port));
243
  }
244
245
  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
  if (args.Length() > 2 && args[2]->IsBoolean()) {
251
    wait_for_connect = args[2]->BooleanValue(args.GetIsolate());
252
  }
253
  agent->StartIoThread();
254
  if (wait_for_connect)
255
    agent->WaitForConnect();
256
}
257
258
void Url(const FunctionCallbackInfo<Value>& args) {
259
  Environment* env = Environment::GetCurrent(args);
260
  Agent* agent = env->inspector_agent();
261
  InspectorIo* io = agent->io();
262
263
  if (!io) return;
264
265
  std::vector<std::string> ids = io->GetTargetIds();
266
267
  if (ids.empty()) return;
268
269
  std::string url = FormatWsAddress(io->host(), io->port(), ids[0], true);
270
  args.GetReturnValue().Set(OneByteString(env->isolate(), url.c_str()));
271
}
272
273
164
void Initialize(Local<Object> target, Local<Value> unused,
274
                Local<Context> context, void* priv) {
275
164
  Environment* env = Environment::GetCurrent(context);
276
277
164
  Agent* agent = env->inspector_agent();
278
164
  env->SetMethod(target, "consoleCall", InspectorConsoleCall);
279
  env->SetMethod(
280
164
      target, "setConsoleExtensionInstaller", SetConsoleExtensionInstaller);
281
164
  if (agent->WillWaitForConnect())
282
    env->SetMethod(target, "callAndPauseOnStart", CallAndPauseOnStart);
283
164
  env->SetMethod(target, "open", Open);
284
164
  env->SetMethodNoSideEffect(target, "url", Url);
285
286
164
  env->SetMethod(target, "asyncTaskScheduled", AsyncTaskScheduledWrapper);
287
  env->SetMethod(target, "asyncTaskCanceled",
288
164
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskCanceled>);
289
  env->SetMethod(target, "asyncTaskStarted",
290
164
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskStarted>);
291
  env->SetMethod(target, "asyncTaskFinished",
292
164
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskFinished>);
293
294
164
  env->SetMethod(target, "registerAsyncHook", RegisterAsyncHookWrapper);
295
164
  env->SetMethodNoSideEffect(target, "isEnabled", IsEnabled);
296
297
164
  auto conn_str = FIXED_ONE_BYTE_STRING(env->isolate(), "Connection");
298
  Local<FunctionTemplate> tmpl =
299
164
      env->NewFunctionTemplate(JSBindingsConnection::New);
300
328
  tmpl->InstanceTemplate()->SetInternalFieldCount(1);
301
164
  tmpl->SetClassName(conn_str);
302
328
  tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
303
164
  env->SetProtoMethod(tmpl, "dispatch", JSBindingsConnection::Dispatch);
304
164
  env->SetProtoMethod(tmpl, "disconnect", JSBindingsConnection::Disconnect);
305
  target
306
      ->Set(env->context(), conn_str,
307
820
            tmpl->GetFunction(env->context()).ToLocalChecked())
308
328
      .ToChecked();
309
164
}
310
311
}  // namespace
312
}  // namespace inspector
313
}  // namespace node
314
315
164
NODE_MODULE_CONTEXT_AWARE_INTERNAL(inspector,
316
                                  node::inspector::Initialize);