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: 177 179 98.9 %
Date: 2019-09-07 22:28:56 Branches: 48 78 61.5 %

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::Boolean;
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
433
std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate,
35
                                               Local<Value> value) {
36
433
  TwoByteValue buffer(isolate, value);
37
433
  return StringBuffer::create(StringView(*buffer, buffer.length()));
38
}
39
40
630
class JSBindingsConnection : public AsyncWrap {
41
 public:
42
420
  class JSBindingsSessionDelegate : public InspectorSessionDelegate {
43
   public:
44
210
    JSBindingsSessionDelegate(Environment* env,
45
                              JSBindingsConnection* connection)
46
                              : env_(env),
47
210
                                connection_(connection) {
48
210
    }
49
50
3887
    void SendMessageToFrontend(const v8_inspector::StringView& message)
51
        override {
52
3887
      Isolate* isolate = env_->isolate();
53
3887
      HandleScope handle_scope(isolate);
54
3887
      Context::Scope context_scope(env_->context());
55
      MaybeLocal<String> v8string =
56
          String::NewFromTwoByte(isolate, message.characters16(),
57
3887
                                 NewStringType::kNormal, message.length());
58
7774
      Local<Value> argument = v8string.ToLocalChecked().As<Value>();
59
7774
      connection_->OnMessage(argument);
60
3887
    }
61
62
   private:
63
    Environment* env_;
64
    JSBindingsConnection* connection_;
65
  };
66
67
210
  JSBindingsConnection(Environment* env,
68
                       Local<Object> wrap,
69
                       Local<Function> callback)
70
                       : AsyncWrap(env, wrap, PROVIDER_INSPECTORJSBINDING),
71
210
                         callback_(env->isolate(), callback) {
72
210
    Agent* inspector = env->inspector_agent();
73
420
    session_ = inspector->Connect(std::make_unique<JSBindingsSessionDelegate>(
74
210
        env, this), false);
75
210
  }
76
77
3887
  void OnMessage(Local<Value> value) {
78
7774
    MakeCallback(callback_.Get(env()->isolate()), 1, &value);
79
3887
  }
80
81
210
  static void New(const FunctionCallbackInfo<Value>& info) {
82
210
    Environment* env = Environment::GetCurrent(info);
83
420
    CHECK(info[0]->IsFunction());
84
420
    Local<Function> callback = info[0].As<Function>();
85
210
    new JSBindingsConnection(env, info.This(), callback);
86
210
  }
87
88
200
  void Disconnect() {
89
200
    session_.reset();
90
200
    delete this;
91
200
  }
92
93
200
  static void Disconnect(const FunctionCallbackInfo<Value>& info) {
94
    JSBindingsConnection* session;
95
400
    ASSIGN_OR_RETURN_UNWRAP(&session, info.Holder());
96
200
    session->Disconnect();
97
  }
98
99
433
  static void Dispatch(const FunctionCallbackInfo<Value>& info) {
100
433
    Environment* env = Environment::GetCurrent(info);
101
    JSBindingsConnection* session;
102
866
    ASSIGN_OR_RETURN_UNWRAP(&session, info.Holder());
103
1299
    CHECK(info[0]->IsString());
104
105
433
    if (session->session_) {
106
433
      session->session_->Dispatch(
107
866
          ToProtocolString(env->isolate(), info[0])->string());
108
    }
109
  }
110
111
2
  void MemoryInfo(MemoryTracker* tracker) const override {
112
2
    tracker->TrackField("callback", callback_);
113
    tracker->TrackFieldWithSize(
114
2
        "session", sizeof(*session_), "InspectorSession");
115
2
  }
116
117
2
  SET_MEMORY_INFO_NAME(JSBindingsConnection)
118
2
  SET_SELF_SIZE(JSBindingsConnection)
119
120
 private:
121
  std::unique_ptr<InspectorSession> session_;
122
  Global<Function> callback_;
123
};
124
125
231953
static bool InspectorEnabled(Environment* env) {
126
231953
  Agent* agent = env->inspector_agent();
127
231953
  return agent->IsActive();
128
}
129
130
5091
void SetConsoleExtensionInstaller(const FunctionCallbackInfo<Value>& info) {
131
5091
  auto env = Environment::GetCurrent(info);
132
133
5091
  CHECK_EQ(info.Length(), 1);
134
10182
  CHECK(info[0]->IsFunction());
135
136
10182
  env->set_inspector_console_extension_installer(info[0].As<Function>());
137
5091
}
138
139
6
void CallAndPauseOnStart(const FunctionCallbackInfo<v8::Value>& args) {
140
6
  Environment* env = Environment::GetCurrent(args);
141
6
  CHECK_GT(args.Length(), 1);
142
12
  CHECK(args[0]->IsFunction());
143
6
  SlicedArguments call_args(args, /* start */ 2);
144
6
  env->inspector_agent()->PauseOnNextJavascriptStatement("Break on start");
145
  v8::MaybeLocal<v8::Value> retval =
146
11
      args[0].As<v8::Function>()->Call(env->context(), args[1],
147
24
                                       call_args.length(), call_args.out());
148
5
  if (!retval.IsEmpty()) {
149
8
    args.GetReturnValue().Set(retval.ToLocalChecked());
150
5
  }
151
5
}
152
153
231951
void InspectorConsoleCall(const FunctionCallbackInfo<Value>& info) {
154
231951
  Environment* env = Environment::GetCurrent(info);
155
231951
  Isolate* isolate = env->isolate();
156
231951
  Local<Context> context = isolate->GetCurrentContext();
157
231951
  CHECK_GE(info.Length(), 2);
158
231951
  SlicedArguments call_args(info, /* start */ 2);
159
231951
  if (InspectorEnabled(env)) {
160
231884
    Local<Value> inspector_method = info[0];
161
231884
    CHECK(inspector_method->IsFunction());
162
231884
    if (!env->is_in_inspector_console_call()) {
163
231883
      env->set_is_in_inspector_console_call(true);
164
      MaybeLocal<Value> ret =
165
          inspector_method.As<Function>()->Call(context,
166
                                                info.Holder(),
167
231883
                                                call_args.length(),
168
927532
                                                call_args.out());
169
231883
      env->set_is_in_inspector_console_call(false);
170
231883
      if (ret.IsEmpty())
171
231960
        return;
172
    }
173
  }
174
175
231942
  Local<Value> node_method = info[1];
176
231942
  CHECK(node_method->IsFunction());
177
  node_method.As<Function>()->Call(context,
178
                                   info.Holder(),
179
231942
                                   call_args.length(),
180
1159710
                                   call_args.out()).FromMaybe(Local<Value>());
181
}
182
183
7
static void* GetAsyncTask(int64_t asyncId) {
184
  // The inspector assumes that when other clients use its asyncTask* API,
185
  // they use real pointers, or at least something aligned like real pointer.
186
  // In general it means that our task_id should always be even.
187
  //
188
  // On 32bit platforms, the 64bit asyncId would get truncated when converted
189
  // to a 32bit pointer. However, the javascript part will never enable
190
  // the async_hook on 32bit platforms, therefore the truncation will never
191
  // happen in practice.
192
7
  return reinterpret_cast<void*>(asyncId << 1);
193
}
194
195
template <void (Agent::*asyncTaskFn)(void*)>
196
6
static void InvokeAsyncTaskFnWithId(const FunctionCallbackInfo<Value>& args) {
197
6
  Environment* env = Environment::GetCurrent(args);
198

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


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


20
  if (args.Length() > 1 && args[1]->IsString()) {
247
    Utf8Value host(env->isolate(), args[1].As<String>());
248
    agent->host_port()->set_host(*host);
249
  }
250
251
4
  agent->StartIoThread();
252
4
}
253
254
2
void WaitForDebugger(const FunctionCallbackInfo<Value>& args) {
255
2
  Environment* env = Environment::GetCurrent(args);
256
2
  Agent* agent = env->inspector_agent();
257
2
  if (agent->IsActive())
258
2
    agent->WaitForConnect();
259
6
  args.GetReturnValue().Set(agent->IsActive());
260
2
}
261
262
9
void Url(const FunctionCallbackInfo<Value>& args) {
263
9
  Environment* env = Environment::GetCurrent(args);
264
9
  std::string url = env->inspector_agent()->GetWsUrl();
265
9
  if (url.length() == 0) {
266
11
    return;
267
  }
268
21
  args.GetReturnValue().Set(OneByteString(env->isolate(), url.c_str()));
269
}
270
271
5091
void Initialize(Local<Object> target, Local<Value> unused,
272
                Local<Context> context, void* priv) {
273
5091
  Environment* env = Environment::GetCurrent(context);
274
275
  v8::Local<v8::Function> consoleCallFunc =
276
      env->NewFunctionTemplate(InspectorConsoleCall, v8::Local<v8::Signature>(),
277
                               v8::ConstructorBehavior::kThrow,
278
5091
                               v8::SideEffectType::kHasSideEffect)
279
15273
          ->GetFunction(context)
280
10182
          .ToLocalChecked();
281
5091
  auto name_string = FIXED_ONE_BYTE_STRING(env->isolate(), "consoleCall");
282
10182
  target->Set(context, name_string, consoleCallFunc).Check();
283
5091
  consoleCallFunc->SetName(name_string);
284
285
  env->SetMethod(
286
5091
      target, "setConsoleExtensionInstaller", SetConsoleExtensionInstaller);
287
5091
  env->SetMethod(target, "callAndPauseOnStart", CallAndPauseOnStart);
288
5091
  env->SetMethod(target, "open", Open);
289
5091
  env->SetMethodNoSideEffect(target, "url", Url);
290
5091
  env->SetMethod(target, "waitForDebugger", WaitForDebugger);
291
292
5091
  env->SetMethod(target, "asyncTaskScheduled", AsyncTaskScheduledWrapper);
293
  env->SetMethod(target, "asyncTaskCanceled",
294
5091
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskCanceled>);
295
  env->SetMethod(target, "asyncTaskStarted",
296
5091
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskStarted>);
297
  env->SetMethod(target, "asyncTaskFinished",
298
5091
      InvokeAsyncTaskFnWithId<&Agent::AsyncTaskFinished>);
299
300
5091
  env->SetMethod(target, "registerAsyncHook", RegisterAsyncHookWrapper);
301
5091
  env->SetMethodNoSideEffect(target, "isEnabled", IsEnabled);
302
303
5091
  auto conn_str = FIXED_ONE_BYTE_STRING(env->isolate(), "Connection");
304
  Local<FunctionTemplate> tmpl =
305
5091
      env->NewFunctionTemplate(JSBindingsConnection::New);
306
10182
  tmpl->InstanceTemplate()->SetInternalFieldCount(1);
307
5091
  tmpl->SetClassName(conn_str);
308
10182
  tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env));
309
5091
  env->SetProtoMethod(tmpl, "dispatch", JSBindingsConnection::Dispatch);
310
5091
  env->SetProtoMethod(tmpl, "disconnect", JSBindingsConnection::Disconnect);
311
  target
312
      ->Set(env->context(), conn_str,
313
25455
            tmpl->GetFunction(env->context()).ToLocalChecked())
314
10182
      .ToChecked();
315
5091
}
316
317
}  // namespace
318
}  // namespace inspector
319
}  // namespace node
320
321
4948
NODE_MODULE_CONTEXT_AWARE_INTERNAL(inspector,
322
                                  node::inspector::Initialize)