GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: inspector_io.cc Lines: 178 179 99.4 %
Date: 2022-09-11 04:22:34 Branches: 32 44 72.7 %

Line Branch Exec Source
1
#include "inspector_io.h"
2
3
#include "inspector_socket_server.h"
4
#include "inspector/main_thread_interface.h"
5
#include "inspector/node_string.h"
6
#include "crypto/crypto_util.h"
7
#include "base_object-inl.h"
8
#include "debug_utils-inl.h"
9
#include "node.h"
10
#include "node_internals.h"
11
#include "node_mutex.h"
12
#include "v8-inspector.h"
13
#include "util-inl.h"
14
#include "zlib.h"
15
16
#include <deque>
17
#include <cstring>
18
#include <vector>
19
20
namespace node {
21
namespace inspector {
22
namespace {
23
using v8_inspector::StringBuffer;
24
using v8_inspector::StringView;
25
26
// kKill closes connections and stops the server, kStop only stops the server
27
enum class TransportAction { kKill, kSendMessage, kStop };
28
29
113
std::string ScriptPath(uv_loop_t* loop, const std::string& script_name) {
30
113
  std::string script_path;
31
32
113
  if (!script_name.empty()) {
33
    uv_fs_t req;
34
96
    req.ptr = nullptr;
35
96
    if (0 == uv_fs_realpath(loop, &req, script_name.c_str(), nullptr)) {
36
95
      CHECK_NOT_NULL(req.ptr);
37
95
      script_path = std::string(static_cast<char*>(req.ptr));
38
    }
39
96
    uv_fs_req_cleanup(&req);
40
  }
41
42
113
  return script_path;
43
}
44
45
// UUID RFC: https://www.ietf.org/rfc/rfc4122.txt
46
// Used ver 4 - with numbers
47
113
std::string GenerateID() {
48
  uint16_t buffer[8];
49
113
  CHECK(crypto::EntropySource(reinterpret_cast<unsigned char*>(buffer),
50
                              sizeof(buffer)));
51
52
  char uuid[256];
53
904
  snprintf(uuid, sizeof(uuid), "%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
54
113
           buffer[0],  // time_low
55
113
           buffer[1],  // time_mid
56
113
           buffer[2],  // time_low
57
113
           (buffer[3] & 0x0fff) | 0x4000,  // time_hi_and_version
58
113
           (buffer[4] & 0x3fff) | 0x8000,  // clk_seq_hi clk_seq_low
59
113
           buffer[5],  // node
60
113
           buffer[6],
61
113
           buffer[7]);
62
113
  return uuid;
63
}
64
65
class RequestToServer {
66
 public:
67
2296
  RequestToServer(TransportAction action,
68
                  int session_id,
69
                  std::unique_ptr<v8_inspector::StringBuffer> message)
70
2296
                  : action_(action),
71
                    session_id_(session_id),
72
2296
                    message_(std::move(message)) {}
73
74
2284
  void Dispatch(InspectorSocketServer* server) const {
75

2284
    switch (action_) {
76
50
      case TransportAction::kKill:
77
50
        server->TerminateConnections();
78
        // Fallthrough
79
158
      case TransportAction::kStop:
80
158
        server->Stop();
81
158
        break;
82
2126
      case TransportAction::kSendMessage:
83
2126
        server->Send(
84
2126
            session_id_,
85
4252
            protocol::StringUtil::StringViewToUtf8(message_->string()));
86
2126
        break;
87
    }
88
2284
  }
89
90
 private:
91
  TransportAction action_;
92
  int session_id_;
93
  std::unique_ptr<v8_inspector::StringBuffer> message_;
94
};
95
96
class RequestQueueData {
97
 public:
98
  using MessageQueue = std::deque<RequestToServer>;
99
100
113
  explicit RequestQueueData(uv_loop_t* loop)
101
113
                            : handle_(std::make_shared<RequestQueue>(this)) {
102
113
    int err = uv_async_init(loop, &async_, [](uv_async_t* async) {
103
      RequestQueueData* wrapper =
104
2210
          node::ContainerOf(&RequestQueueData::async_, async);
105
2210
      wrapper->DoDispatch();
106
2323
    });
107
113
    CHECK_EQ(0, err);
108
113
  }
109
110
  static void CloseAndFree(RequestQueueData* queue);
111
112
2296
  void Post(int session_id,
113
            TransportAction action,
114
            std::unique_ptr<StringBuffer> message) {
115
4592
    Mutex::ScopedLock scoped_lock(state_lock_);
116
2296
    bool notify = messages_.empty();
117
2296
    messages_.emplace_back(action, session_id, std::move(message));
118
2296
    if (notify) {
119
2222
      CHECK_EQ(0, uv_async_send(&async_));
120
2222
      incoming_message_cond_.Broadcast(scoped_lock);
121
    }
122
2296
  }
123
124
  void Wait() {
125
    Mutex::ScopedLock scoped_lock(state_lock_);
126
    if (messages_.empty()) {
127
      incoming_message_cond_.Wait(scoped_lock);
128
    }
129
  }
130
131
113
  void SetServer(InspectorSocketServer* server) {
132
113
    server_ = server;
133
113
  }
134
135
131
  std::shared_ptr<RequestQueue> handle() {
136
131
    return handle_;
137
  }
138
139
 private:
140
113
  ~RequestQueueData() = default;
141
142
2210
  MessageQueue GetMessages() {
143
4420
    Mutex::ScopedLock scoped_lock(state_lock_);
144
2210
    MessageQueue messages;
145
2210
    messages_.swap(messages);
146
2210
    return messages;
147
  }
148
149
2210
  void DoDispatch() {
150
2210
    if (server_ == nullptr)
151
      return;
152
4494
    for (const auto& request : GetMessages()) {
153
2284
      request.Dispatch(server_);
154
    }
155
  }
156
157
  std::shared_ptr<RequestQueue> handle_;
158
  uv_async_t async_;
159
  InspectorSocketServer* server_ = nullptr;
160
  MessageQueue messages_;
161
  Mutex state_lock_;  // Locked before mutating the queue.
162
  ConditionVariable incoming_message_cond_;
163
};
164
}  // namespace
165
166
class RequestQueue {
167
 public:
168
113
  explicit RequestQueue(RequestQueueData* data) : data_(data) {}
169
170
113
  void Reset() {
171
113
    Mutex::ScopedLock scoped_lock(lock_);
172
113
    data_ = nullptr;
173
113
  }
174
175
2312
  void Post(int session_id,
176
            TransportAction action,
177
            std::unique_ptr<StringBuffer> message) {
178
4624
    Mutex::ScopedLock scoped_lock(lock_);
179
2312
    if (data_ != nullptr)
180
2296
      data_->Post(session_id, action, std::move(message));
181
2312
  }
182
183
113
  bool Expired() {
184
113
    Mutex::ScopedLock scoped_lock(lock_);
185
113
    return data_ == nullptr;
186
  }
187
188
 private:
189
  RequestQueueData* data_;
190
  Mutex lock_;
191
};
192
193
class IoSessionDelegate : public InspectorSessionDelegate {
194
 public:
195
18
  explicit IoSessionDelegate(std::shared_ptr<RequestQueue> queue, int id)
196
18
                             : request_queue_(queue), id_(id) { }
197
2126
  void SendMessageToFrontend(const v8_inspector::StringView& message) override {
198
4252
    request_queue_->Post(id_, TransportAction::kSendMessage,
199
4252
                         StringBuffer::create(message));
200
2126
  }
201
202
 private:
203
  std::shared_ptr<RequestQueue> request_queue_;
204
  int id_;
205
};
206
207
// Passed to InspectorSocketServer to handle WS inspector protocol events,
208
// mostly session start, message received, and session end.
209
class InspectorIoDelegate: public node::inspector::SocketServerDelegate {
210
 public:
211
  InspectorIoDelegate(std::shared_ptr<RequestQueueData> queue,
212
                      std::shared_ptr<MainThreadHandle> main_thread,
213
                      const std::string& target_id,
214
                      const std::string& script_path,
215
                      const std::string& script_name);
216
452
  ~InspectorIoDelegate() override = default;
217
218
  void StartSession(int session_id, const std::string& target_id) override;
219
  void MessageReceived(int session_id, const std::string& message) override;
220
  void EndSession(int session_id) override;
221
222
  std::vector<std::string> GetTargetIds() override;
223
  std::string GetTargetTitle(const std::string& id) override;
224
  std::string GetTargetUrl(const std::string& id) override;
225
113
  void AssignServer(InspectorSocketServer* server) override {
226
113
    request_queue_->SetServer(server);
227
113
  }
228
229
 private:
230
  std::shared_ptr<RequestQueueData> request_queue_;
231
  std::shared_ptr<MainThreadHandle> main_thread_;
232
  std::unordered_map<int, std::unique_ptr<InspectorSession>> sessions_;
233
  const std::string script_name_;
234
  const std::string script_path_;
235
  const std::string target_id_;
236
};
237
238
// static
239
113
std::unique_ptr<InspectorIo> InspectorIo::Start(
240
    std::shared_ptr<MainThreadHandle> main_thread,
241
    const std::string& path,
242
    std::shared_ptr<ExclusiveAccess<HostPort>> host_port,
243
    const InspectPublishUid& inspect_publish_uid) {
244
  auto io = std::unique_ptr<InspectorIo>(
245
      new InspectorIo(main_thread,
246
                      path,
247
                      host_port,
248
339
                      inspect_publish_uid));
249
113
  if (io->request_queue_->Expired()) {  // Thread is not running
250
1
    return nullptr;
251
  }
252
112
  return io;
253
}
254
255
113
InspectorIo::InspectorIo(std::shared_ptr<MainThreadHandle> main_thread,
256
                         const std::string& path,
257
                         std::shared_ptr<ExclusiveAccess<HostPort>> host_port,
258
113
                         const InspectPublishUid& inspect_publish_uid)
259
    : main_thread_(main_thread),
260
      host_port_(host_port),
261
      inspect_publish_uid_(inspect_publish_uid),
262
      thread_(),
263
      script_name_(path),
264
113
      id_(GenerateID()) {
265
226
  Mutex::ScopedLock scoped_lock(thread_start_lock_);
266
113
  CHECK_EQ(uv_thread_create(&thread_, InspectorIo::ThreadMain, this), 0);
267
113
  thread_start_condition_.Wait(scoped_lock);
268
113
}
269
270
76
InspectorIo::~InspectorIo() {
271
76
  request_queue_->Post(0, TransportAction::kKill, nullptr);
272
76
  int err = uv_thread_join(&thread_);
273
76
  CHECK_EQ(err, 0);
274
76
}
275
276
110
void InspectorIo::StopAcceptingNewConnections() {
277
110
  request_queue_->Post(0, TransportAction::kStop, nullptr);
278
110
}
279
280
// static
281
113
void InspectorIo::ThreadMain(void* io) {
282
113
  static_cast<InspectorIo*>(io)->ThreadMain();
283
113
}
284
285
113
void InspectorIo::ThreadMain() {
286
  uv_loop_t loop;
287
113
  loop.data = nullptr;
288
113
  int err = uv_loop_init(&loop);
289
113
  CHECK_EQ(err, 0);
290
113
  std::shared_ptr<RequestQueueData> queue(new RequestQueueData(&loop),
291
226
                                          RequestQueueData::CloseAndFree);
292
226
  std::string script_path = ScriptPath(&loop, script_name_);
293
  std::unique_ptr<InspectorIoDelegate> delegate(
294
226
      new InspectorIoDelegate(queue, main_thread_, id_,
295
339
                              script_path, script_name_));
296
226
  std::string host;
297
  int port;
298
  {
299
226
    ExclusiveAccess<HostPort>::Scoped host_port(host_port_);
300
113
    host = host_port->host();
301
113
    port = host_port->port();
302
  }
303
113
  InspectorSocketServer server(std::move(delegate),
304
                               &loop,
305
113
                               std::move(host),
306
                               port,
307
339
                               inspect_publish_uid_);
308
113
  request_queue_ = queue->handle();
309
  // Its lifetime is now that of the server delegate
310
113
  queue.reset();
311
  {
312
226
    Mutex::ScopedLock scoped_lock(thread_start_lock_);
313
113
    if (server.Start()) {
314
224
      ExclusiveAccess<HostPort>::Scoped host_port(host_port_);
315
112
      host_port->set_port(server.Port());
316
    }
317
113
    thread_start_condition_.Broadcast(scoped_lock);
318
  }
319
113
  uv_run(&loop, UV_RUN_DEFAULT);
320
113
  CheckedUvLoopClose(&loop);
321
113
}
322
323
5
std::string InspectorIo::GetWsUrl() const {
324
10
  ExclusiveAccess<HostPort>::Scoped host_port(host_port_);
325
5
  return FormatWsAddress(host_port->host(), host_port->port(), id_, true);
326
}
327
328
113
InspectorIoDelegate::InspectorIoDelegate(
329
    std::shared_ptr<RequestQueueData> queue,
330
    std::shared_ptr<MainThreadHandle> main_thread,
331
    const std::string& target_id,
332
    const std::string& script_path,
333
113
    const std::string& script_name)
334
    : request_queue_(queue), main_thread_(main_thread),
335
      script_name_(script_name), script_path_(script_path),
336
113
      target_id_(target_id) {}
337
338
18
void InspectorIoDelegate::StartSession(int session_id,
339
                                       const std::string& target_id) {
340
18
  auto session = main_thread_->Connect(
341
36
      std::unique_ptr<InspectorSessionDelegate>(
342
54
          new IoSessionDelegate(request_queue_->handle(), session_id)), true);
343
18
  if (session) {
344
18
    sessions_[session_id] = std::move(session);
345
18
    fprintf(stderr, "Debugger attached.\n");
346
  }
347
18
}
348
349
135
void InspectorIoDelegate::MessageReceived(int session_id,
350
                                          const std::string& message) {
351
135
  auto session = sessions_.find(session_id);
352
135
  if (session != sessions_.end())
353
135
    session->second->Dispatch(Utf8ToStringView(message)->string());
354
135
}
355
356
18
void InspectorIoDelegate::EndSession(int session_id) {
357
18
  sessions_.erase(session_id);
358
18
}
359
360
154
std::vector<std::string> InspectorIoDelegate::GetTargetIds() {
361
308
  return { target_id_ };
362
}
363
364
22
std::string InspectorIoDelegate::GetTargetTitle(const std::string& id) {
365
22
  return script_name_.empty() ? GetHumanReadableProcessName() : script_name_;
366
}
367
368
22
std::string InspectorIoDelegate::GetTargetUrl(const std::string& id) {
369
22
  return "file://" + script_path_;
370
}
371
372
// static
373
113
void RequestQueueData::CloseAndFree(RequestQueueData* queue) {
374
113
  queue->handle_->Reset();
375
113
  queue->handle_.reset();
376
113
  uv_close(reinterpret_cast<uv_handle_t*>(&queue->async_),
377
113
           [](uv_handle_t* handle) {
378
113
    uv_async_t* async = reinterpret_cast<uv_async_t*>(handle);
379
    RequestQueueData* wrapper =
380
113
        node::ContainerOf(&RequestQueueData::async_, async);
381
113
    delete wrapper;
382
113
  });
383
113
}
384
}  // namespace inspector
385
}  // namespace node