GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: inspector_agent.cc Lines: 466 513 90.8 %
Date: 2022-10-14 05:16:24 Branches: 170 254 66.9 %

Line Branch Exec Source
1
#include "inspector_agent.h"
2
3
#include "env-inl.h"
4
#include "inspector/main_thread_interface.h"
5
#include "inspector/node_string.h"
6
#include "inspector/runtime_agent.h"
7
#include "inspector/tracing_agent.h"
8
#include "inspector/worker_agent.h"
9
#include "inspector/worker_inspector.h"
10
#include "inspector_io.h"
11
#include "node/inspector/protocol/Protocol.h"
12
#include "node_errors.h"
13
#include "node_internals.h"
14
#include "node_options-inl.h"
15
#include "node_process-inl.h"
16
#include "node_url.h"
17
#include "util-inl.h"
18
#include "timer_wrap-inl.h"
19
#include "v8-inspector.h"
20
#include "v8-platform.h"
21
22
#include "libplatform/libplatform.h"
23
24
#ifdef __POSIX__
25
#include <pthread.h>
26
#include <climits>  // PTHREAD_STACK_MIN
27
#endif  // __POSIX__
28
29
#include <algorithm>
30
#include <cstring>
31
#include <sstream>
32
#include <unordered_map>
33
#include <vector>
34
35
namespace node {
36
namespace inspector {
37
namespace {
38
39
using node::FatalError;
40
41
using v8::Context;
42
using v8::Function;
43
using v8::HandleScope;
44
using v8::Isolate;
45
using v8::Local;
46
using v8::Message;
47
using v8::Object;
48
using v8::Value;
49
50
using v8_inspector::StringBuffer;
51
using v8_inspector::StringView;
52
using v8_inspector::V8Inspector;
53
using v8_inspector::V8InspectorClient;
54
55
static uv_sem_t start_io_thread_semaphore;
56
static uv_async_t start_io_thread_async;
57
// This is just an additional check to make sure start_io_thread_async
58
// is not accidentally re-used or used when uninitialized.
59
static std::atomic_bool start_io_thread_async_initialized { false };
60
// Protects the Agent* stored in start_io_thread_async.data.
61
5623
static Mutex start_io_thread_async_mutex;
62
63
4
std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate,
64
                                               Local<Value> value) {
65
8
  TwoByteValue buffer(isolate, value);
66
8
  return StringBuffer::create(StringView(*buffer, buffer.length()));
67
}
68
69
// Called on the main thread.
70
3
void StartIoThreadAsyncCallback(uv_async_t* handle) {
71
3
  static_cast<Agent*>(handle->data)->StartIoThread();
72
3
}
73
74
75
#ifdef __POSIX__
76
2
static void StartIoThreadWakeup(int signo, siginfo_t* info, void* ucontext) {
77
2
  uv_sem_post(&start_io_thread_semaphore);
78
2
}
79
80
5598
inline void* StartIoThreadMain(void* unused) {
81
  for (;;) {
82
5598
    uv_sem_wait(&start_io_thread_semaphore);
83
4
    Mutex::ScopedLock lock(start_io_thread_async_mutex);
84
85
2
    CHECK(start_io_thread_async_initialized);
86
2
    Agent* agent = static_cast<Agent*>(start_io_thread_async.data);
87
2
    if (agent != nullptr)
88
2
      agent->RequestIoThreadStart();
89
2
  }
90
}
91
92
5596
static int StartDebugSignalHandler() {
93
  // Start a watchdog thread for calling v8::Debug::DebugBreak() because
94
  // it's not safe to call directly from the signal handler, it can
95
  // deadlock with the thread it interrupts.
96
5596
  CHECK_EQ(0, uv_sem_init(&start_io_thread_semaphore, 0));
97
  pthread_attr_t attr;
98
5596
  CHECK_EQ(0, pthread_attr_init(&attr));
99
#if defined(PTHREAD_STACK_MIN) && !defined(__FreeBSD__)
100
  // PTHREAD_STACK_MIN is 2 KiB with musl libc, which is too small to safely
101
  // receive signals. PTHREAD_STACK_MIN + MINSIGSTKSZ is 8 KiB on arm64, which
102
  // is the musl architecture with the biggest MINSIGSTKSZ so let's use that
103
  // as a lower bound and let's quadruple it just in case. The goal is to avoid
104
  // creating a big 2 or 4 MiB address space gap (problematic on 32 bits
105
  // because of fragmentation), not squeeze out every last byte.
106
  // Omitted on FreeBSD because it doesn't seem to like small stacks.
107
5596
  const size_t stack_size = std::max(static_cast<size_t>(4 * 8192),
108
                                     static_cast<size_t>(PTHREAD_STACK_MIN));
109
5596
  CHECK_EQ(0, pthread_attr_setstacksize(&attr, stack_size));
110
#endif  // defined(PTHREAD_STACK_MIN) && !defined(__FreeBSD__)
111
5596
  CHECK_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
112
  sigset_t sigmask;
113
  // Mask all signals.
114
5596
  sigfillset(&sigmask);
115
  sigset_t savemask;
116
5596
  CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, &savemask));
117
5596
  sigmask = savemask;
118
  pthread_t thread;
119
  const int err = pthread_create(&thread, &attr,
120
5596
                                 StartIoThreadMain, nullptr);
121
  // Restore original mask
122
5596
  CHECK_EQ(0, pthread_sigmask(SIG_SETMASK, &sigmask, nullptr));
123
5596
  CHECK_EQ(0, pthread_attr_destroy(&attr));
124
5596
  if (err != 0) {
125
    fprintf(stderr, "node[%u]: pthread_create: %s\n",
126
            uv_os_getpid(), strerror(err));
127
    fflush(stderr);
128
    // Leave SIGUSR1 blocked.  We don't install a signal handler,
129
    // receiving the signal would terminate the process.
130
    return -err;
131
  }
132
5596
  RegisterSignalHandler(SIGUSR1, StartIoThreadWakeup);
133
  // Unblock SIGUSR1.  A pending SIGUSR1 signal will now be delivered.
134
5596
  sigemptyset(&sigmask);
135
5596
  sigaddset(&sigmask, SIGUSR1);
136
5596
  CHECK_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sigmask, nullptr));
137
5596
  return 0;
138
}
139
#endif  // __POSIX__
140
141
142
#ifdef _WIN32
143
DWORD WINAPI StartIoThreadProc(void* arg) {
144
  Mutex::ScopedLock lock(start_io_thread_async_mutex);
145
  CHECK(start_io_thread_async_initialized);
146
  Agent* agent = static_cast<Agent*>(start_io_thread_async.data);
147
  if (agent != nullptr)
148
    agent->RequestIoThreadStart();
149
  return 0;
150
}
151
152
static int GetDebugSignalHandlerMappingName(DWORD pid, wchar_t* buf,
153
                                            size_t buf_len) {
154
  return _snwprintf(buf, buf_len, L"node-debug-handler-%u", pid);
155
}
156
157
static int StartDebugSignalHandler() {
158
  wchar_t mapping_name[32];
159
  HANDLE mapping_handle;
160
  DWORD pid;
161
  LPTHREAD_START_ROUTINE* handler;
162
163
  pid = uv_os_getpid();
164
165
  if (GetDebugSignalHandlerMappingName(pid,
166
                                       mapping_name,
167
                                       arraysize(mapping_name)) < 0) {
168
    return -1;
169
  }
170
171
  mapping_handle = CreateFileMappingW(INVALID_HANDLE_VALUE,
172
                                      nullptr,
173
                                      PAGE_READWRITE,
174
                                      0,
175
                                      sizeof *handler,
176
                                      mapping_name);
177
  if (mapping_handle == nullptr) {
178
    return -1;
179
  }
180
181
  handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
182
      MapViewOfFile(mapping_handle,
183
                    FILE_MAP_ALL_ACCESS,
184
                    0,
185
                    0,
186
                    sizeof *handler));
187
  if (handler == nullptr) {
188
    CloseHandle(mapping_handle);
189
    return -1;
190
  }
191
192
  *handler = StartIoThreadProc;
193
194
  UnmapViewOfFile(static_cast<void*>(handler));
195
196
  return 0;
197
}
198
#endif  // _WIN32
199
200
201
const int CONTEXT_GROUP_ID = 1;
202
203
727
std::string GetWorkerLabel(node::Environment* env) {
204
1454
  std::ostringstream result;
205
727
  result << "Worker[" << env->thread_id() << "]";
206
1454
  return result.str();
207
}
208
209
class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
210
                          public protocol::FrontendChannel {
211
 public:
212
7297
  explicit ChannelImpl(Environment* env,
213
                       const std::unique_ptr<V8Inspector>& inspector,
214
                       std::shared_ptr<WorkerManager> worker_manager,
215
                       std::unique_ptr<InspectorSessionDelegate> delegate,
216
                       std::shared_ptr<MainThreadHandle> main_thread_,
217
                       bool prevent_shutdown)
218
14594
      : delegate_(std::move(delegate)), prevent_shutdown_(prevent_shutdown),
219
14594
        retaining_context_(false) {
220
21891
    session_ = inspector->connect(CONTEXT_GROUP_ID,
221
                                  this,
222
                                  StringView(),
223
14594
                                  V8Inspector::ClientTrustLevel::kFullyTrusted);
224
7297
    node_dispatcher_ = std::make_unique<protocol::UberDispatcher>(this);
225
7297
    tracing_agent_ =
226
14594
        std::make_unique<protocol::TracingAgent>(env, main_thread_);
227
7297
    tracing_agent_->Wire(node_dispatcher_.get());
228
7297
    if (worker_manager) {
229
6555
      worker_agent_ = std::make_unique<protocol::WorkerAgent>(worker_manager);
230
6555
      worker_agent_->Wire(node_dispatcher_.get());
231
    }
232
7297
    runtime_agent_ = std::make_unique<protocol::RuntimeAgent>();
233
7297
    runtime_agent_->Wire(node_dispatcher_.get());
234
7297
  }
235
236
19866
  ~ChannelImpl() override {
237
6622
    tracing_agent_->disable();
238
6622
    tracing_agent_.reset();  // Dispose before the dispatchers
239
6622
    if (worker_agent_) {
240
5880
      worker_agent_->disable();
241
5880
      worker_agent_.reset();  // Dispose before the dispatchers
242
    }
243
6622
    runtime_agent_->disable();
244
6622
    runtime_agent_.reset();  // Dispose before the dispatchers
245
13244
  }
246
247
20416
  void dispatchProtocolMessage(const StringView& message) {
248
40832
    std::string raw_message = protocol::StringUtil::StringViewToUtf8(message);
249
    std::unique_ptr<protocol::DictionaryValue> value =
250
40832
        protocol::DictionaryValue::cast(protocol::StringUtil::parseMessage(
251
40832
            raw_message, false));
252
    int call_id;
253
40832
    std::string method;
254
20416
    node_dispatcher_->parseCommand(value.get(), &call_id, &method);
255
40832
    if (v8_inspector::V8InspectorSession::canDispatchMethod(
256
40832
            Utf8ToStringView(method)->string())) {
257
20377
      session_->dispatchProtocolMessage(message);
258
    } else {
259
39
      node_dispatcher_->dispatch(call_id, method, std::move(value),
260
                                 raw_message);
261
    }
262
20416
  }
263
264
38
  void schedulePauseOnNextStatement(const std::string& reason) {
265
76
    std::unique_ptr<StringBuffer> buffer = Utf8ToStringView(reason);
266
38
    session_->schedulePauseOnNextStatement(buffer->string(), buffer->string());
267
38
  }
268
269
6628
  bool preventShutdown() {
270
6628
    return prevent_shutdown_;
271
  }
272
273
6369
  bool notifyWaitingForDisconnect() {
274
6369
    retaining_context_ = runtime_agent_->notifyWaitingForDisconnect();
275
6369
    return retaining_context_;
276
  }
277
278
936
  bool retainingContext() {
279
936
    return retaining_context_;
280
  }
281
282
 private:
283
20377
  void sendResponse(
284
      int callId,
285
      std::unique_ptr<v8_inspector::StringBuffer> message) override {
286
20377
    sendMessageToFrontend(message->string());
287
20377
  }
288
289
7965
  void sendNotification(
290
      std::unique_ptr<v8_inspector::StringBuffer> message) override {
291
7965
    sendMessageToFrontend(message->string());
292
7965
  }
293
294
1348
  void flushProtocolNotifications() override { }
295
296
29014
  void sendMessageToFrontend(const StringView& message) {
297
29014
    delegate_->SendMessageToFrontend(message);
298
29014
  }
299
300
672
  void sendMessageToFrontend(const std::string& message) {
301
672
    sendMessageToFrontend(Utf8ToStringView(message)->string());
302
672
  }
303
304
  using Serializable = protocol::Serializable;
305
306
39
  void sendProtocolResponse(int callId,
307
                            std::unique_ptr<Serializable> message) override {
308
39
    sendMessageToFrontend(message->serializeToJSON());
309
39
  }
310
633
  void sendProtocolNotification(
311
      std::unique_ptr<Serializable> message) override {
312
633
    sendMessageToFrontend(message->serializeToJSON());
313
633
  }
314
315
  void fallThrough(int callId,
316
                   const std::string& method,
317
                   const std::string& message) override {
318
    DCHECK(false);
319
  }
320
321
  std::unique_ptr<protocol::RuntimeAgent> runtime_agent_;
322
  std::unique_ptr<protocol::TracingAgent> tracing_agent_;
323
  std::unique_ptr<protocol::WorkerAgent> worker_agent_;
324
  std::unique_ptr<InspectorSessionDelegate> delegate_;
325
  std::unique_ptr<v8_inspector::V8InspectorSession> session_;
326
  std::unique_ptr<protocol::UberDispatcher> node_dispatcher_;
327
  bool prevent_shutdown_;
328
  bool retaining_context_;
329
};
330
331
class SameThreadInspectorSession : public InspectorSession {
332
 public:
333
7297
  SameThreadInspectorSession(
334
      int session_id, std::shared_ptr<NodeInspectorClient> client)
335
7297
      : session_id_(session_id), client_(client) {}
336
  ~SameThreadInspectorSession() override;
337
  void Dispatch(const v8_inspector::StringView& message) override;
338
339
 private:
340
  int session_id_;
341
  std::weak_ptr<NodeInspectorClient> client_;
342
};
343
344
112
void NotifyClusterWorkersDebugEnabled(Environment* env) {
345
112
  Isolate* isolate = env->isolate();
346
224
  HandleScope handle_scope(isolate);
347
112
  Local<Context> context = env->context();
348
349
  // Send message to enable debug in cluster workers
350
112
  Local<Object> message = Object::New(isolate);
351
224
  message->Set(context, FIXED_ONE_BYTE_STRING(isolate, "cmd"),
352
336
               FIXED_ONE_BYTE_STRING(isolate, "NODE_DEBUG_ENABLED")).Check();
353
112
  ProcessEmit(env, "internalMessage", message);
354
112
}
355
356
#ifdef _WIN32
357
bool IsFilePath(const std::string& path) {
358
  // '\\'
359
  if (path.length() > 2 && path[0] == '\\' && path[1] == '\\')
360
    return true;
361
  // '[A-Z]:[/\\]'
362
  if (path.length() < 3)
363
    return false;
364
  if ((path[0] >= 'A' && path[0] <= 'Z') || (path[0] >= 'a' && path[0] <= 'z'))
365
    return path[1] == ':' && (path[2] == '/' || path[2] == '\\');
366
  return false;
367
}
368
#else
369
738757
bool IsFilePath(const std::string& path) {
370

738757
  return !path.empty() && path[0] == '/';
371
}
372
#endif  // __POSIX__
373
374
void ThrowUninitializedInspectorError(Environment* env) {
375
  HandleScope scope(env->isolate());
376
377
  const char* msg = "This Environment was initialized without a V8::Inspector";
378
  Local<Value> exception =
379
    v8::String::NewFromUtf8(env->isolate(), msg).ToLocalChecked();
380
381
  env->isolate()->ThrowException(exception);
382
}
383
384
}  // namespace
385
386
5652
class NodeInspectorClient : public V8InspectorClient {
387
 public:
388
6325
  explicit NodeInspectorClient(node::Environment* env, bool is_main)
389
6325
      : env_(env), is_main_(is_main) {
390
6325
    client_ = V8Inspector::create(env->isolate(), this);
391
    // TODO(bnoordhuis) Make name configurable from src/node.cc.
392
    std::string name =
393
12650
        is_main_ ? GetHumanReadableProcessName() : GetWorkerLabel(env);
394
12650
    ContextInfo info(name);
395
6325
    info.is_default = true;
396
6325
    contextCreated(env->context(), info);
397
6325
  }
398
399
40
  void runMessageLoopOnPause(int context_group_id) override {
400
40
    waiting_for_resume_ = true;
401
40
    runMessageLoop();
402
40
  }
403
404
112
  void waitForSessionsDisconnect() {
405
112
    waiting_for_sessions_disconnect_ = true;
406
112
    runMessageLoop();
407
112
  }
408
409
20
  void waitForFrontend() {
410
20
    waiting_for_frontend_ = true;
411
20
    runMessageLoop();
412
20
  }
413
414
9
  void maxAsyncCallStackDepthChanged(int depth) override {
415
9
    if (waiting_for_sessions_disconnect_) {
416
      // V8 isolate is mostly done and is only letting Inspector protocol
417
      // clients gather data.
418
4
      return;
419
    }
420
5
    if (auto agent = env_->inspector_agent()) {
421
5
      if (depth == 0) {
422
1
        agent->DisableAsyncHook();
423
      } else {
424
4
        agent->EnableAsyncHook();
425
      }
426
    }
427
  }
428
429
6947
  void contextCreated(Local<Context> context, const ContextInfo& info) {
430
13894
    auto name_buffer = Utf8ToStringView(info.name);
431
13894
    auto origin_buffer = Utf8ToStringView(info.origin);
432
13894
    std::unique_ptr<StringBuffer> aux_data_buffer;
433
434
    v8_inspector::V8ContextInfo v8info(
435
6947
        context, CONTEXT_GROUP_ID, name_buffer->string());
436
6947
    v8info.origin = origin_buffer->string();
437
438
6947
    if (info.is_default) {
439
6325
      aux_data_buffer = Utf8ToStringView("{\"isDefault\":true}");
440
    } else {
441
622
      aux_data_buffer = Utf8ToStringView("{\"isDefault\":false}");
442
    }
443
6947
    v8info.auxData = aux_data_buffer->string();
444
445
6947
    client_->contextCreated(v8info);
446
6947
  }
447
448
6308
  void contextDestroyed(Local<Context> context) {
449
6308
    client_->contextDestroyed(context);
450
6308
  }
451
452
28
  void quitMessageLoopOnPause() override {
453
28
    waiting_for_resume_ = false;
454
28
  }
455
456
21
  void runIfWaitingForDebugger(int context_group_id) override {
457
21
    waiting_for_frontend_ = false;
458
21
  }
459
460
7297
  int connectFrontend(std::unique_ptr<InspectorSessionDelegate> delegate,
461
                      bool prevent_shutdown) {
462
7297
    int session_id = next_session_id_++;
463
7297
    channels_[session_id] = std::make_unique<ChannelImpl>(env_,
464
                                                          client_,
465
14594
                                                          getWorkerManager(),
466
7297
                                                          std::move(delegate),
467
14594
                                                          getThreadHandle(),
468
                                                          prevent_shutdown);
469
7297
    return session_id;
470
  }
471
472
933
  void disconnectFrontend(int session_id) {
473
933
    auto it = channels_.find(session_id);
474
933
    if (it == channels_.end())
475
      return;
476
933
    bool retaining_context = it->second->retainingContext();
477
933
    channels_.erase(it);
478
933
    if (retaining_context) {
479
6
      for (const auto& id_channel : channels_) {
480
3
        if (id_channel.second->retainingContext())
481
          return;
482
      }
483
3
      contextDestroyed(env_->context());
484
    }
485

933
    if (waiting_for_sessions_disconnect_ && !is_main_)
486
2
      waiting_for_sessions_disconnect_ = false;
487
  }
488
489
20416
  void dispatchMessageFromFrontend(int session_id, const StringView& message) {
490
20416
    channels_[session_id]->dispatchProtocolMessage(message);
491
20416
  }
492
493
310
  Local<Context> ensureDefaultContextInGroup(int contextGroupId) override {
494
310
    return env_->context();
495
  }
496
497
3
  void installAdditionalCommandLineAPI(Local<Context> context,
498
                                       Local<Object> target) override {
499
3
    Local<Function> installer = env_->inspector_console_extension_installer();
500
3
    if (!installer.IsEmpty()) {
501
6
      Local<Value> argv[] = {target};
502
      // If there is an exception, proceed in JS land
503
6
      USE(installer->Call(context, target, arraysize(argv), argv));
504
    }
505
3
  }
506
507
2
  void ReportUncaughtException(Local<Value> error, Local<Message> message) {
508
2
    Isolate* isolate = env_->isolate();
509
2
    Local<Context> context = env_->context();
510
511
4
    int script_id = message->GetScriptOrigin().ScriptId();
512
513
2
    Local<v8::StackTrace> stack_trace = message->GetStackTrace();
514
515


6
    if (!stack_trace.IsEmpty() && stack_trace->GetFrameCount() > 0 &&
516
4
        script_id == stack_trace->GetFrame(isolate, 0)->GetScriptId()) {
517
1
      script_id = 0;
518
    }
519
520
2
    const uint8_t DETAILS[] = "Uncaught";
521
522
10
    client_->exceptionThrown(
523
        context,
524
        StringView(DETAILS, sizeof(DETAILS) - 1),
525
        error,
526
6
        ToProtocolString(isolate, message->Get())->string(),
527
4
        ToProtocolString(isolate, message->GetScriptResourceName())->string(),
528
6
        message->GetLineNumber(context).FromMaybe(0),
529
6
        message->GetStartColumn(context).FromMaybe(0),
530
4
        client_->createStackTrace(stack_trace),
531
2
        script_id);
532
2
  }
533
534
2
  void startRepeatingTimer(double interval_s,
535
                           TimerCallback callback,
536
                           void* data) override {
537
    auto result =
538
2
        timers_.emplace(std::piecewise_construct, std::make_tuple(data),
539
9
                        std::make_tuple(env_, [=]() { callback(data); }));
540
2
    CHECK(result.second);
541
2
    uint64_t interval = static_cast<uint64_t>(1000 * interval_s);
542
2
    result.first->second.Update(interval, interval);
543
2
  }
544
545
2
  void cancelTimer(void* data) override {
546
2
    timers_.erase(data);
547
2
  }
548
549
  // Async stack traces instrumentation.
550
3
  void AsyncTaskScheduled(const StringView& task_name, void* task,
551
                          bool recurring) {
552
3
    client_->asyncTaskScheduled(task_name, task, recurring);
553
3
  }
554
555
4
  void AsyncTaskCanceled(void* task) {
556
4
    client_->asyncTaskCanceled(task);
557
4
  }
558
559
6
  void AsyncTaskStarted(void* task) {
560
6
    client_->asyncTaskStarted(task);
561
6
  }
562
563
10
  void AsyncTaskFinished(void* task) {
564
10
    client_->asyncTaskFinished(task);
565
10
  }
566
567
  void AllAsyncTasksCanceled() {
568
    client_->allAsyncTasksCanceled();
569
  }
570
571
19
  void schedulePauseOnNextStatement(const std::string& reason) {
572
57
    for (const auto& id_channel : channels_) {
573
38
      id_channel.second->schedulePauseOnNextStatement(reason);
574
    }
575
19
  }
576
577
6500
  bool hasConnectedSessions() {
578
13033
    for (const auto& id_channel : channels_) {
579
      // Other sessions are "invisible" more most purposes
580
6628
      if (id_channel.second->preventShutdown())
581
95
        return true;
582
    }
583
6405
    return false;
584
  }
585
586
6308
  bool notifyWaitingForDisconnect() {
587
6308
    bool retaining_context = false;
588
12677
    for (const auto& id_channel : channels_) {
589
6369
      if (id_channel.second->notifyWaitingForDisconnect())
590
3
        retaining_context = true;
591
    }
592
6308
    return retaining_context;
593
  }
594
595
13732
  std::shared_ptr<MainThreadHandle> getThreadHandle() {
596
13732
    if (!interface_) {
597
12644
      interface_ = std::make_shared<MainThreadInterface>(
598
18966
          env_->inspector_agent());
599
    }
600
13732
    return interface_->GetHandle();
601
  }
602
603
9926
  std::shared_ptr<WorkerManager> getWorkerManager() {
604
9926
    if (!is_main_) {
605
742
      return nullptr;
606
    }
607
9184
    if (worker_manager_ == nullptr) {
608
5593
      worker_manager_ =
609
11186
          std::make_shared<WorkerManager>(getThreadHandle());
610
    }
611
9184
    return worker_manager_;
612
  }
613
614
78018
  bool IsActive() {
615
78018
    return !channels_.empty();
616
  }
617
618
 private:
619
290
  bool shouldRunMessageLoop() {
620
290
    if (waiting_for_frontend_)
621
48
      return true;
622

242
    if (waiting_for_sessions_disconnect_ || waiting_for_resume_) {
623
192
      return hasConnectedSessions();
624
    }
625
50
    return false;
626
  }
627
628
172
  void runMessageLoop() {
629
172
    if (running_nested_loop_)
630
      return;
631
632
172
    running_nested_loop_ = true;
633
634
408
    while (shouldRunMessageLoop()) {
635
118
      if (interface_) interface_->WaitForFrontendEvent();
636
118
      env_->RunAndClearInterrupts();
637
    }
638
172
    running_nested_loop_ = false;
639
  }
640
641
72870
  double currentTimeMS() override {
642
72870
    return env_->isolate_data()->platform()->CurrentClockTimeMillis();
643
  }
644
645
738757
  std::unique_ptr<StringBuffer> resourceNameToUrl(
646
      const StringView& resource_name_view) override {
647
    std::string resource_name =
648
1477514
        protocol::StringUtil::StringViewToUtf8(resource_name_view);
649
738757
    if (!IsFilePath(resource_name))
650
700074
      return nullptr;
651
77366
    node::url::URL url = node::url::URL::FromFilePath(resource_name);
652
38683
    return Utf8ToStringView(url.href());
653
  }
654
655
  node::Environment* env_;
656
  bool is_main_;
657
  bool running_nested_loop_ = false;
658
  std::unique_ptr<V8Inspector> client_;
659
  // Note: ~ChannelImpl may access timers_ so timers_ has to come first.
660
  std::unordered_map<void*, TimerWrapHandle> timers_;
661
  std::unordered_map<int, std::unique_ptr<ChannelImpl>> channels_;
662
  int next_session_id_ = 1;
663
  bool waiting_for_resume_ = false;
664
  bool waiting_for_frontend_ = false;
665
  bool waiting_for_sessions_disconnect_ = false;
666
  // Allows accessing Inspector from non-main threads
667
  std::shared_ptr<MainThreadInterface> interface_;
668
  std::shared_ptr<WorkerManager> worker_manager_;
669
};
670
671
6331
Agent::Agent(Environment* env)
672
    : parent_env_(env),
673
12662
      debug_options_(env->options()->debug_options()),
674
18993
      host_port_(env->inspector_host_port()) {}
675
676
5658
Agent::~Agent() {}
677
678
6325
bool Agent::Start(const std::string& path,
679
                  const DebugOptions& options,
680
                  std::shared_ptr<ExclusiveAccess<HostPort>> host_port,
681
                  bool is_main) {
682
6325
  if (!options.allow_attaching_debugger) {
683
    return false;
684
  }
685
6325
  path_ = path;
686
6325
  debug_options_ = options;
687
6325
  CHECK_NOT_NULL(host_port);
688
6325
  host_port_ = host_port;
689
690
6325
  client_ = std::make_shared<NodeInspectorClient>(parent_env_, is_main);
691
6325
  if (parent_env_->owns_inspector()) {
692
11192
    Mutex::ScopedLock lock(start_io_thread_async_mutex);
693
5596
    CHECK_EQ(start_io_thread_async_initialized.exchange(true), false);
694
5596
    CHECK_EQ(0, uv_async_init(parent_env_->event_loop(),
695
                              &start_io_thread_async,
696
                              StartIoThreadAsyncCallback));
697
5596
    uv_unref(reinterpret_cast<uv_handle_t*>(&start_io_thread_async));
698
5596
    start_io_thread_async.data = this;
699
    // Ignore failure, SIGUSR1 won't work, but that should not block node start.
700
5596
    StartDebugSignalHandler();
701
702
26634
    parent_env_->AddCleanupHook([](void* data) {
703
4923
      Environment* env = static_cast<Environment*>(data);
704
705
      {
706
9846
        Mutex::ScopedLock lock(start_io_thread_async_mutex);
707
4923
        start_io_thread_async.data = nullptr;
708
      }
709
710
      // This is global, will never get freed
711
9846
      env->CloseHandle(&start_io_thread_async, [](uv_async_t*) {
712
4923
        CHECK(start_io_thread_async_initialized.exchange(false));
713
4923
      });
714
21038
    }, parent_env_);
715
  }
716
717
25272
  AtExit(parent_env_, [](void* env) {
718
6311
    Agent* agent = static_cast<Environment*>(env)->inspector_agent();
719
6311
    if (agent->IsActive()) {
720
6306
      agent->WaitForDisconnect();
721
    }
722
25272
  }, parent_env_);
723
724
6325
  bool wait_for_connect = options.wait_for_connect();
725
6325
  if (parent_handle_) {
726
727
    wait_for_connect = parent_handle_->WaitForConnect();
727
727
    parent_handle_->WorkerStarted(client_->getThreadHandle(), wait_for_connect);
728

5598
  } else if (!options.inspector_enabled || !StartIoThread()) {
729
5488
    return false;
730
  }
731
732
  // Patch the debug options to implement waitForDebuggerOnStart for
733
  // the NodeWorker.enable method.
734
837
  if (wait_for_connect) {
735
20
    CHECK(!parent_env_->has_serialized_options());
736
20
    debug_options_.EnableBreakFirstLine();
737
20
    parent_env_->options()->get_debug_options()->EnableBreakFirstLine();
738
20
    client_->waitForFrontend();
739
  }
740
837
  return true;
741
}
742
743
116
bool Agent::StartIoThread() {
744
116
  if (io_ != nullptr)
745
3
    return true;
746
747

113
  if (!parent_env_->should_create_inspector() && !client_) {
748
    ThrowUninitializedInspectorError(parent_env_);
749
    return false;
750
  }
751
752
113
  CHECK_NOT_NULL(client_);
753
754
226
  io_ = InspectorIo::Start(client_->getThreadHandle(),
755
                           path_,
756
                           host_port_,
757
113
                           debug_options_.inspect_publish_uid);
758
113
  if (io_ == nullptr) {
759
1
    return false;
760
  }
761
112
  NotifyClusterWorkersDebugEnabled(parent_env_);
762
112
  return true;
763
}
764
765
4
void Agent::Stop() {
766
4
  io_.reset();
767
4
}
768
769
7297
std::unique_ptr<InspectorSession> Agent::Connect(
770
    std::unique_ptr<InspectorSessionDelegate> delegate,
771
    bool prevent_shutdown) {
772

7297
  if (!parent_env_->should_create_inspector() && !client_) {
773
    ThrowUninitializedInspectorError(parent_env_);
774
    return std::unique_ptr<InspectorSession>{};
775
  }
776
777
7297
  CHECK_NOT_NULL(client_);
778
779
14594
  int session_id = client_->connectFrontend(std::move(delegate),
780
7297
                                            prevent_shutdown);
781
  return std::unique_ptr<InspectorSession>(
782
7297
      new SameThreadInspectorSession(session_id, client_));
783
}
784
785
2
std::unique_ptr<InspectorSession> Agent::ConnectToMainThread(
786
    std::unique_ptr<InspectorSessionDelegate> delegate,
787
    bool prevent_shutdown) {
788

2
  if (!parent_env_->should_create_inspector() && !client_) {
789
    ThrowUninitializedInspectorError(parent_env_);
790
    return std::unique_ptr<InspectorSession>{};
791
  }
792
793
2
  CHECK_NOT_NULL(parent_handle_);
794
2
  CHECK_NOT_NULL(client_);
795
  auto thread_safe_delegate =
796
4
      client_->getThreadHandle()->MakeDelegateThreadSafe(std::move(delegate));
797
2
  return parent_handle_->Connect(std::move(thread_safe_delegate),
798
4
                                 prevent_shutdown);
799
}
800
801
6308
void Agent::WaitForDisconnect() {
802

6308
  if (!parent_env_->should_create_inspector() && !client_) {
803
    ThrowUninitializedInspectorError(parent_env_);
804
    return;
805
  }
806
807
6308
  CHECK_NOT_NULL(client_);
808
6308
  bool is_worker = parent_handle_ != nullptr;
809
6308
  parent_handle_.reset();
810

6308
  if (client_->hasConnectedSessions() && !is_worker) {
811
13
    fprintf(stderr, "Waiting for the debugger to disconnect...\n");
812
13
    fflush(stderr);
813
  }
814
6308
  if (!client_->notifyWaitingForDisconnect()) {
815
6305
    client_->contextDestroyed(parent_env_->context());
816
3
  } else if (is_worker) {
817
2
    client_->waitForSessionsDisconnect();
818
  }
819
6308
  if (io_ != nullptr) {
820
110
    io_->StopAcceptingNewConnections();
821
110
    client_->waitForSessionsDisconnect();
822
  }
823
}
824
825
276
void Agent::ReportUncaughtException(Local<Value> error,
826
                                    Local<Message> message) {
827
276
  if (!IsListening())
828
274
    return;
829
2
  client_->ReportUncaughtException(error, message);
830
2
  WaitForDisconnect();
831
}
832
833
19
void Agent::PauseOnNextJavascriptStatement(const std::string& reason) {
834
19
  client_->schedulePauseOnNextStatement(reason);
835
19
}
836
837
6297
void Agent::RegisterAsyncHook(Isolate* isolate,
838
                              Local<Function> enable_function,
839
                              Local<Function> disable_function) {
840
6297
  parent_env_->set_inspector_enable_async_hooks(enable_function);
841
6297
  parent_env_->set_inspector_disable_async_hooks(disable_function);
842
6297
  if (pending_enable_async_hook_) {
843
2
    CHECK(!pending_disable_async_hook_);
844
2
    pending_enable_async_hook_ = false;
845
2
    EnableAsyncHook();
846
6295
  } else if (pending_disable_async_hook_) {
847
    CHECK(!pending_enable_async_hook_);
848
    pending_disable_async_hook_ = false;
849
    DisableAsyncHook();
850
  }
851
6297
}
852
853
6
void Agent::EnableAsyncHook() {
854
12
  HandleScope scope(parent_env_->isolate());
855
6
  Local<Function> enable = parent_env_->inspector_enable_async_hooks();
856
6
  if (!enable.IsEmpty()) {
857
4
    ToggleAsyncHook(parent_env_->isolate(), enable);
858
2
  } else if (pending_disable_async_hook_) {
859
    CHECK(!pending_enable_async_hook_);
860
    pending_disable_async_hook_ = false;
861
  } else {
862
2
    pending_enable_async_hook_ = true;
863
  }
864
6
}
865
866
1
void Agent::DisableAsyncHook() {
867
2
  HandleScope scope(parent_env_->isolate());
868
1
  Local<Function> disable = parent_env_->inspector_enable_async_hooks();
869
1
  if (!disable.IsEmpty()) {
870
1
    ToggleAsyncHook(parent_env_->isolate(), disable);
871
  } else if (pending_enable_async_hook_) {
872
    CHECK(!pending_disable_async_hook_);
873
    pending_enable_async_hook_ = false;
874
  } else {
875
    pending_disable_async_hook_ = true;
876
  }
877
1
}
878
879
5
void Agent::ToggleAsyncHook(Isolate* isolate, Local<Function> fn) {
880
  // Guard against running this during cleanup -- no async events will be
881
  // emitted anyway at that point anymore, and calling into JS is not possible.
882
  // This should probably not be something we're attempting in the first place,
883
  // Refs: https://github.com/nodejs/node/pull/34362#discussion_r456006039
884
5
  if (!parent_env_->can_call_into_js()) return;
885
5
  CHECK(parent_env_->has_run_bootstrapping_code());
886
10
  HandleScope handle_scope(isolate);
887
5
  CHECK(!fn.IsEmpty());
888
5
  auto context = parent_env_->context();
889
10
  v8::TryCatch try_catch(isolate);
890
10
  USE(fn->Call(context, Undefined(isolate), 0, nullptr));
891

5
  if (try_catch.HasCaught() && !try_catch.HasTerminated()) {
892
    PrintCaughtException(isolate, context, try_catch);
893
    FatalError("\nnode::inspector::Agent::ToggleAsyncHook",
894
               "Cannot toggle Inspector's AsyncHook, please report this.");
895
  }
896
}
897
898
3
void Agent::AsyncTaskScheduled(const StringView& task_name, void* task,
899
                               bool recurring) {
900
3
  client_->AsyncTaskScheduled(task_name, task, recurring);
901
3
}
902
903
4
void Agent::AsyncTaskCanceled(void* task) {
904
4
  client_->AsyncTaskCanceled(task);
905
4
}
906
907
6
void Agent::AsyncTaskStarted(void* task) {
908
6
  client_->AsyncTaskStarted(task);
909
6
}
910
911
10
void Agent::AsyncTaskFinished(void* task) {
912
10
  client_->AsyncTaskFinished(task);
913
10
}
914
915
void Agent::AllAsyncTasksCanceled() {
916
  client_->AllAsyncTasksCanceled();
917
}
918
919
2
void Agent::RequestIoThreadStart() {
920
  // We need to attempt to interrupt V8 flow (in case Node is running
921
  // continuous JS code) and to wake up libuv thread (in case Node is waiting
922
  // for IO events)
923
2
  CHECK(start_io_thread_async_initialized);
924
2
  uv_async_send(&start_io_thread_async);
925
4
  parent_env_->RequestInterrupt([this](Environment*) {
926
2
    StartIoThread();
927
2
  });
928
929
2
  CHECK(start_io_thread_async_initialized);
930
2
  uv_async_send(&start_io_thread_async);
931
2
}
932
933
6953
void Agent::ContextCreated(Local<Context> context, const ContextInfo& info) {
934
6953
  if (client_ == nullptr)  // This happens for a main context
935
6331
    return;
936
622
  client_->contextCreated(context, info);
937
}
938
939
78164
bool Agent::IsActive() {
940
78164
  if (client_ == nullptr)
941
    return false;
942

78164
  return io_ != nullptr || client_->IsActive();
943
}
944
945
727
void Agent::SetParentHandle(
946
    std::unique_ptr<ParentInspectorHandle> parent_handle) {
947
727
  parent_handle_ = std::move(parent_handle);
948
727
}
949
950
967
std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle(
951
    uint64_t thread_id, const std::string& url) {
952

967
  if (!parent_env_->should_create_inspector() && !client_) {
953
    ThrowUninitializedInspectorError(parent_env_);
954
    return std::unique_ptr<ParentInspectorHandle>{};
955
  }
956
957
967
  CHECK_NOT_NULL(client_);
958
967
  if (!parent_handle_) {
959
957
    return client_->getWorkerManager()->NewParentHandle(thread_id, url);
960
  } else {
961
10
    return parent_handle_->NewParentInspectorHandle(thread_id, url);
962
  }
963
}
964
965
void Agent::WaitForConnect() {
966
  if (!parent_env_->should_create_inspector() && !client_) {
967
    ThrowUninitializedInspectorError(parent_env_);
968
    return;
969
  }
970
971
  CHECK_NOT_NULL(client_);
972
  client_->waitForFrontend();
973
}
974
975
1672
std::shared_ptr<WorkerManager> Agent::GetWorkerManager() {
976

1672
  if (!parent_env_->should_create_inspector() && !client_) {
977
    ThrowUninitializedInspectorError(parent_env_);
978
    return std::unique_ptr<WorkerManager>{};
979
  }
980
981
1672
  CHECK_NOT_NULL(client_);
982
1672
  return client_->getWorkerManager();
983
}
984
985
6
std::string Agent::GetWsUrl() const {
986
6
  if (io_ == nullptr)
987
1
    return "";
988
5
  return io_->GetWsUrl();
989
}
990
991
19866
SameThreadInspectorSession::~SameThreadInspectorSession() {
992
13244
  auto client = client_.lock();
993
6622
  if (client)
994
933
    client->disconnectFrontend(session_id_);
995
13244
}
996
997
20416
void SameThreadInspectorSession::Dispatch(
998
    const v8_inspector::StringView& message) {
999
40832
  auto client = client_.lock();
1000
20416
  if (client)
1001
20416
    client->dispatchMessageFromFrontend(session_id_, message);
1002
20416
}
1003
1004
}  // namespace inspector
1005

16869
}  // namespace node