forked from BabylonJS/JsRuntimeHost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV8InspectorAgent.cpp
More file actions
677 lines (581 loc) · 20.7 KB
/
V8InspectorAgent.cpp
File metadata and controls
677 lines (581 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
// This code is based on the old node inspector implementation. See NOTICE.md for Node.js' project license details
#include <V8InspectorAgent.h>
#include "V8InspectorSocketServer.h"
#include "V8InspectorUtils.h"
#include <v8-inspector.h>
#include <v8-platform.h>
#if __has_include(<v8-version.h>)
#include <v8-version.h>
#endif
#include <string.h>
#include <chrono>
#include <map>
#include <sstream>
#include <thread>
#include <utility>
#include <vector>
#include <condition_variable>
#include <array>
#include <random>
#include <algorithm>
namespace Babylon
{
const char TAG_CONNECT[] = "#connect";
const char TAG_DISCONNECT[] = "#disconnect";
std::string GetProcessTitle()
{
return "BabylonNative";
}
class V8NodeInspector;
class AgentImpl
{
public:
explicit AgentImpl(
v8::Platform& platform,
v8::Isolate* isolate,
v8::Local<v8::Context> context,
const char* context_name);
~AgentImpl();
void Start(const unsigned short port, const std::string& appName);
void Stop();
void WaitForDebugger();
bool IsStarted();
bool IsConnected();
void PostIncomingMessage(int session_id, const std::string& message);
private:
using MessageQueue =
std::vector<std::pair<int, std::unique_ptr<v8_inspector::StringBuffer>>>;
enum class State
{
kNew,
kAccepting,
kConnected,
kDone,
kError
};
void DispatchMessages();
void Write(
int session_id,
std::unique_ptr<v8_inspector::StringBuffer> message);
bool AppendMessage(
MessageQueue* vector,
int session_id,
std::unique_ptr<v8_inspector::StringBuffer> buffer);
void SwapBehindLock(MessageQueue* vector1, MessageQueue* vector2);
void WaitForFrontendMessage();
void NotifyMessageReceived();
std::mutex incoming_message_cond_m_;
std::condition_variable incoming_message_cond_;
std::mutex state_m;
unsigned short port_;
bool waiting_for_frontend_ = true;
std::unique_ptr<V8NodeInspector> inspector_;
v8::Isolate* isolate_;
MessageQueue incoming_message_queue_;
MessageQueue outgoing_message_queue_;
bool dispatching_messages_;
int session_id_;
std::unique_ptr<InspectorSocketServer> server_;
std::string script_name_;
v8::Platform& platform_;
friend class ChannelImpl;
friend class DispatchOnInspectorBackendTask;
friend class SetConnectedTask;
friend class V8NodeInspector;
friend void InterruptCallback(v8::Isolate*, void* agent);
public:
};
void InterruptCallback(v8::Isolate*, void* agent)
{
static_cast<AgentImpl*>(agent)->DispatchMessages();
}
class DispatchOnInspectorBackendTask : public v8::Task
{
public:
explicit DispatchOnInspectorBackendTask(AgentImpl& agent)
: agent_(agent)
{
}
private:
// ------------------
// v8::Task overrides
// ------------------
void Run() override
{
agent_.DispatchMessages();
}
private:
AgentImpl& agent_;
};
class ChannelImpl final : public v8_inspector::V8Inspector::Channel
{
public:
explicit ChannelImpl(AgentImpl& agent)
: agent_(agent)
{
}
virtual ~ChannelImpl() {}
private:
// --------------------------------------------
// v8_inspector::V8Inspector::Channel overrides
// --------------------------------------------
void sendResponse(
int /*callId*/,
std::unique_ptr<v8_inspector::StringBuffer> message) override
{
agent_.Write(agent_.session_id_, std::move(message));
}
void sendNotification(
std::unique_ptr<v8_inspector::StringBuffer> message) override
{
agent_.Write(agent_.session_id_, std::move(message));
}
void flushProtocolNotifications() override {}
private:
AgentImpl& agent_;
};
using V8Inspector = v8_inspector::V8Inspector;
class V8NodeInspector : public v8_inspector::V8InspectorClient
{
public:
V8NodeInspector(AgentImpl& agent)
: agent_(agent)
, waiting_for_resume_(false)
, running_nested_loop_(false)
, inspector_(V8Inspector::create(agent.isolate_, this))
{
}
void SetupContext(
v8::Local<v8::Context> context,
const char* context_name /*must be null terminated*/)
{
std::unique_ptr<v8_inspector::StringBuffer> name_buffer = utils::Utf8ToStringView(context_name);
v8_inspector::V8ContextInfo info(context, 1, name_buffer->string());
std::unique_ptr<v8_inspector::StringBuffer> aux_data_buffer = utils::Utf8ToStringView("{\"isDefault\":true}");
info.auxData = aux_data_buffer->string();
inspector_->contextCreated(info);
}
void ConnectFrontend()
{
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION >= 11)
session_ = inspector_->connect(
1, new ChannelImpl(agent_), v8_inspector::StringView(),
v8_inspector::V8Inspector::kFullyTrusted);
#else
session_ = inspector_->connect(
1, new ChannelImpl(agent_), v8_inspector::StringView());
#endif
}
void DisconnectFrontend()
{
session_.reset();
}
void DispatchMessageFromFrontend(const v8_inspector::StringView& message)
{
std::string messagestr = utils::StringViewToUtf8(message);
if (agent_.waiting_for_frontend_)
agent_.waiting_for_frontend_ =
messagestr.find("Runtime.runIfWaitingForDebugger") !=
std::string::npos;
session_->dispatchProtocolMessage(message);
}
V8Inspector* Inspector()
{
return inspector_.get();
}
bool IsWaitingForResume()
{
return waiting_for_resume_;
}
void SchedulePauseOnNextStatement(const v8_inspector::StringView& reason, const v8_inspector::StringView& details)
{
session_->schedulePauseOnNextStatement(reason, details);
}
private:
// -----------------------------------------
// v8_inspector::V8InspectorClient overrides
// -----------------------------------------
void runMessageLoopOnPause(int /*context_group_id*/) override
{
waiting_for_resume_ = true;
if (running_nested_loop_)
return;
running_nested_loop_ = true;
while (waiting_for_resume_)
{
agent_.WaitForFrontendMessage();
agent_.DispatchMessages();
}
waiting_for_resume_ = false;
running_nested_loop_ = false;
}
double currentTimeMS() override
{
auto duration = std::chrono::system_clock::now().time_since_epoch();
return static_cast<double>(std::chrono::duration_cast<std::chrono::milliseconds>(duration)
.count());
}
void quitMessageLoopOnPause() override
{
waiting_for_resume_ = false;
}
v8::Local<v8::Context> ensureDefaultContextInGroup(
int /*contextGroupId*/) override
{
return v8::Isolate::GetCurrent()->GetCurrentContext();
}
private:
AgentImpl& agent_;
std::atomic<bool> waiting_for_resume_{false};
bool running_nested_loop_;
std::unique_ptr<V8Inspector> inspector_;
std::unique_ptr<v8_inspector::V8InspectorSession> session_;
};
AgentImpl::AgentImpl(
v8::Platform& platform,
v8::Isolate* isolate,
v8::Local<v8::Context> context,
const char* context_name)
: inspector_(nullptr)
, isolate_(isolate)
, dispatching_messages_(false)
, session_id_(0)
, server_(nullptr)
, platform_(platform)
{
inspector_ = std::make_unique<V8NodeInspector>(*this);
inspector_->SetupContext(context, context_name);
}
AgentImpl::~AgentImpl() {}
void InspectorConsoleCall(const v8::FunctionCallbackInfo<v8::Value>& info)
{
v8::Isolate* isolate = info.GetIsolate();
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Array> args = info.Data().As<v8::Array>();
CHECK_EQ(args->Length(), 3);
v8::Local<v8::Value> inspector_method =
args->Get(context, 0).ToLocalChecked();
CHECK(inspector_method->IsFunction());
v8::Local<v8::Value> node_method = args->Get(context, 1).ToLocalChecked();
CHECK(node_method->IsFunction());
v8::Local<v8::Value> config_value = args->Get(context, 2).ToLocalChecked();
CHECK(config_value->IsObject());
v8::Local<v8::Object> config_object = config_value.As<v8::Object>();
std::vector<v8::Local<v8::Value>> call_args(info.Length());
for (int i = 0; i < info.Length(); ++i)
{
call_args[i] = info[i];
}
v8::Local<v8::String> in_call_key = utils::OneByteString(isolate, "in_call");
bool in_call = config_object->Has(context, in_call_key).FromMaybe(false);
if (!in_call)
{
CHECK(
config_object->Set(context, in_call_key, v8::True(isolate)).FromJust());
CHECK(
!inspector_method.As<v8::Function>()
->Call(context, info.Holder(), static_cast<int>(call_args.size()), call_args.data())
.IsEmpty());
}
v8::TryCatch try_catch(info.GetIsolate());
static_cast<void>(node_method.As<v8::Function>()->Call(
context, info.Holder(), static_cast<int>(call_args.size()), call_args.data()));
CHECK(config_object->Delete(context, in_call_key).FromJust());
if (try_catch.HasCaught())
try_catch.ReThrow();
}
void InspectorWrapConsoleCall(const v8::FunctionCallbackInfo<v8::Value>& args)
{
v8::Local<v8::Array> array =
v8::Array::New(v8::Isolate::GetCurrent(), args.Length());
CHECK(array->Set(v8::Isolate::GetCurrent()->GetCurrentContext(), 0, args[0])
.FromJust());
CHECK(array->Set(v8::Isolate::GetCurrent()->GetCurrentContext(), 1, args[1])
.FromJust());
CHECK(array->Set(v8::Isolate::GetCurrent()->GetCurrentContext(), 2, args[2])
.FromJust());
args.GetReturnValue().Set(v8::Function::New(
v8::Isolate::GetCurrent()->GetCurrentContext(),
InspectorConsoleCall,
array)
.ToLocalChecked());
}
void AgentImpl::Start(const unsigned short port, const std::string& appName)
{
port_ = port;
script_name_ = appName;
// be sure server_ is not still in use here or its allocation will be replace in the thread func
// this can happen if reusing the same AgentImpl object, stopping and restarting before the InspectorSocketServer is properly pulled down
if (server_)
{
throw std::runtime_error("can't start again the server as previous InspectorSocketServer is still active.");
}
auto delegate = std::make_unique<InspectorAgentDelegate>(*this, "", script_name_, false);
server_ = std::make_unique<InspectorSocketServer>(std::move(delegate), port_);
server_->Start();
}
void AgentImpl::WaitForDebugger()
{
WaitForFrontendMessage();
while (waiting_for_frontend_)
DispatchMessages();
std::string reasonstr("Break on start");
v8_inspector::StringView reason(
reinterpret_cast<const uint8_t*>(reasonstr.c_str()), reasonstr.size()),
details(
reinterpret_cast<const uint8_t*>(reasonstr.c_str()),
reasonstr.size());
inspector_->SchedulePauseOnNextStatement(reason, details);
}
void AgentImpl::Stop()
{
if (inspector_)
{
inspector_->DisconnectFrontend();
inspector_.reset();
}
if (server_)
{
server_->Stop();
}
}
bool AgentImpl::IsConnected()
{
return server_ && server_->IsConnected();
}
bool AgentImpl::IsStarted()
{
return !!server_;
}
std::unique_ptr<v8_inspector::StringBuffer> ToProtocolString(
v8::Local<v8::Value> value)
{
if (value.IsEmpty() || value->IsNull() || value->IsUndefined() ||
!value->IsString())
{
return v8_inspector::StringBuffer::create(v8_inspector::StringView());
}
v8::Local<v8::String> string_value = v8::Local<v8::String>::Cast(value);
int len = string_value->Length();
std::vector<uint16_t> buffer(len);
string_value->Write(v8::Isolate::GetCurrent(), buffer.data(), 0, len);
return v8_inspector::StringBuffer::create(
v8_inspector::StringView(buffer.data(), len));
}
bool AgentImpl::AppendMessage(
MessageQueue* queue,
int session_id,
std::unique_ptr<v8_inspector::StringBuffer> buffer)
{
std::unique_lock<std::mutex> lock(state_m);
bool trigger_pumping = queue->empty();
queue->push_back(std::make_pair(session_id, std::move(buffer)));
return trigger_pumping;
}
void AgentImpl::SwapBehindLock(MessageQueue* vector1, MessageQueue* vector2)
{
std::unique_lock<std::mutex> lock(state_m);
vector1->swap(*vector2);
}
void AgentImpl::PostIncomingMessage(
int session_id,
const std::string& message)
{
if (AppendMessage(&incoming_message_queue_, session_id, utils::Utf8ToStringView(message)))
{
std::shared_ptr<v8::TaskRunner> foregroundTaskRunner;
#ifdef USE_DEFAULT_PLATFORM
// Need to get the foreground runner from the isolate data slot
v8runtime::IsolateData* isolate_data = reinterpret_cast<v8runtime::IsolateData*>(isolate_->GetData(v8runtime::ISOLATE_DATA_SLOT));
foregroundTaskRunner = isolate_data->foreground_task_runner_;
#else
foregroundTaskRunner = platform_.GetForegroundTaskRunner(isolate_);
#endif
foregroundTaskRunner->PostTask(std::make_unique<DispatchOnInspectorBackendTask>(*this));
isolate_->RequestInterrupt(InterruptCallback, this);
}
NotifyMessageReceived();
}
void AgentImpl::WaitForFrontendMessage()
{
std::unique_lock<std::mutex> lock(incoming_message_cond_m_);
if (incoming_message_queue_.empty())
incoming_message_cond_.wait(
lock, [this] { return !incoming_message_queue_.empty(); });
}
void AgentImpl::NotifyMessageReceived()
{
incoming_message_cond_.notify_all();
}
void AgentImpl::DispatchMessages()
{
// This function can be reentered if there was an incoming message while
// V8 was processing another inspector request (e.g. if the user is
// evaluating a long-running JS code snippet). This can happen only at
// specific points (e.g. the lines that call inspector_ methods)
if (dispatching_messages_)
return;
dispatching_messages_ = true;
MessageQueue tasks;
do
{
tasks.clear();
SwapBehindLock(&incoming_message_queue_, &tasks);
for (const MessageQueue::value_type& pair : tasks)
{
v8_inspector::StringView message = pair.second->string();
std::string tag;
if (message.length() == sizeof(TAG_CONNECT) - 1 ||
message.length() == sizeof(TAG_DISCONNECT) - 1)
{
tag = utils::StringViewToUtf8(message);
}
if (tag == TAG_CONNECT)
{
session_id_ = pair.first;
inspector_->ConnectFrontend();
}
else if (tag == TAG_DISCONNECT)
{
inspector_->DisconnectFrontend();
}
else if (inspector_)
{
inspector_->DispatchMessageFromFrontend(message);
}
else
{
#ifdef WIN32
OutputDebugStringW(L"Warning: V8 inspector message dropped - ");
if (message.is8Bit())
{
OutputDebugStringA(reinterpret_cast<const char*>(message.characters8()));
}
else
{
OutputDebugStringW(reinterpret_cast<const wchar_t*>(message.characters16()));
}
OutputDebugStringW(L"\n");
#endif
}
}
}
while (!tasks.empty());
dispatching_messages_ = false;
}
void AgentImpl::Write(
int session_id,
std::unique_ptr<v8_inspector::StringBuffer> inspector_message)
{
AppendMessage(
&outgoing_message_queue_, session_id, std::move(inspector_message));
MessageQueue outgoing_messages;
SwapBehindLock(&outgoing_message_queue_, &outgoing_messages);
for (const MessageQueue::value_type& outgoing : outgoing_messages)
{
v8_inspector::StringView view = outgoing.second->string();
assert(server_);
if (server_)
{
if (view.length() == 0)
{
server_->Stop();
}
else
{
server_->Send(
outgoing.first, utils::StringViewToUtf8(outgoing.second->string()));
}
}
}
}
// Exported class Agent
V8InspectorAgent::V8InspectorAgent(
v8::Platform& platform,
v8::Isolate* isolate,
v8::Local<v8::Context> context,
const char* context_name)
: impl(std::make_unique<AgentImpl>(platform, isolate, context, context_name))
{
}
V8InspectorAgent::~V8InspectorAgent()
{
}
void V8InspectorAgent::WaitForDebugger()
{
impl->WaitForDebugger();
}
void V8InspectorAgent::Stop()
{
impl->Stop();
}
void V8InspectorAgent::Start(const unsigned short port, const std::string& appName)
{
impl->Start(port, appName);
}
bool V8InspectorAgent::IsStarted()
{
return impl->IsStarted();
}
bool V8InspectorAgent::IsConnected()
{
return impl->IsConnected();
}
InspectorAgentDelegate::InspectorAgentDelegate(
AgentImpl& agent,
const std::string& script_path,
const std::string& script_name,
bool wait)
: agent_(agent)
, connected_(false)
, script_name_(script_name)
, script_path_(script_path)
, target_id_(utils::GenerateUniqueID())
, waiting_(wait)
{
}
void InspectorAgentDelegate::StartSession(
int session_id,
const std::string& /*target_id*/)
{
connected_ = true;
agent_.PostIncomingMessage(session_id, TAG_CONNECT);
}
void InspectorAgentDelegate::MessageReceived(
int session_id,
const std::string& message)
{
// TODO(pfeldman): Instead of blocking execution while debugger
// engages, node should wait for the run callback from the remote client
// and initiate its startup. This is a change to node.cc that should be
// upstreamed separately.
if (waiting_)
{
if (message.find("\"Runtime.runIfWaitingForDebugger\"") !=
std::string::npos)
{
waiting_ = false;
}
}
agent_.PostIncomingMessage(session_id, message);
}
void InspectorAgentDelegate::EndSession(int session_id)
{
connected_ = false;
agent_.PostIncomingMessage(session_id, TAG_DISCONNECT);
}
std::vector<std::string> InspectorAgentDelegate::GetTargetIds()
{
return {target_id_};
}
std::string InspectorAgentDelegate::GetTargetTitle(const std::string& /*id*/)
{
return script_name_.empty() ? GetProcessTitle() : script_name_;
}
std::string InspectorAgentDelegate::GetTargetUrl(const std::string& /*id*/)
{
return "file://" + script_path_;
}
} // namespace inspector