Hi again,
I figured that the `chome-devtools://` part doesn't work in Chromium. Using `devtools://` instead works. However, I get a segmentation violation.
Here is the code I wrote to connect the V8 inspector:
struct ServerChannel : v8_inspector::V8Inspector::Channel
{
private:
sockpp::tcp_socket *socket_;
public:
ServerChannel() { }
void set_socket(sockpp::tcp_socket *socket) { socket_ = socket; }
private:
void sendResponse(int, std::unique_ptr<v8_inspector::StringBuffer> message) override {
auto v8_sv = message->string();
socket_->write_n(v8_sv.characters8(), v8_sv.length());
}
void sendNotification(std::unique_ptr<v8_inspector::StringBuffer> message) override {
auto v8_sv = message->string();
socket_->write_n(v8_sv.characters8(), v8_sv.length());
}
void flushProtocolNotifications() override { }
};
struct MyV8InspectorClient : v8_inspector::V8InspectorClient
{
private:
sockpp::tcp_acceptor acceptor_;
std::unique_ptr<ServerChannel> channel_;
std::unique_ptr<v8_inspector::V8Inspector> inspector_;
std::unique_ptr<v8_inspector::V8InspectorSession> session_;
int16_t port_;
public:
MyV8InspectorClient(int16_t port, v8::Isolate *isolate)
: acceptor_(port)
, port_(port)
{
if (not acceptor_)
throw std::runtime_error("failed to initalize TCP server");
channel_ = std::make_unique<ServerChannel>();
inspector_ = v8_inspector::V8Inspector::create(isolate, this);
std::string state("mutable");
v8_inspector::StringView sv(reinterpret_cast<const uint8_t*>(state.c_str()), state.length());
session_ = inspector_->connect(
/* contextGroupId= */ 1,
/* channel= */ channel_.get(),
/* state= */ sv
);
}
MyV8InspectorClient(const MyV8InspectorClient&) = delete;
MyV8InspectorClient(MyV8InspectorClient&&) = default;
~MyV8InspectorClient() {
session_.reset();
inspector_.reset();
channel_.reset();
}
void onMessage(std::string_view sv) {
v8_inspector::StringView msg(reinterpret_cast<const uint8_t*>(sv.data()), sv.length());
session_->dispatchProtocolMessage(msg);
}
void launch() {
std::cout << "WebSocket based Inspector Agent started.\n"
<< "Open the following link in your Chrome/Chromium browser:\n\n\t"
<< "devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=
127.0.0.1:" << port_
<< '\n' << std::endl;
auto socket = std::make_unique<sockpp::tcp_socket>(acceptor_.accept());
if (not *socket)
throw std::runtime_error("failed to establish connection to TCP client");
channel_->set_socket(socket.get());
auto thread = std::thread([this](std::unique_ptr<sockpp::tcp_socket> socket) {
uint8_t buf[512];
std::vector<uint8_t> msg;
msg.reserve(512);
std::size_t n;
for (;;) {
std::cerr << "receiving bytes..." << std::endl;
while ((n = socket->read_n(buf, ARR_SIZE(buf))) > 0)
msg.insert(msg.end(), buf, buf + n); // append recently read bytes to msg
if (not msg.empty()) {
std::cerr << "the received message is " << msg.size() << " bytes" << std::endl;
std::string_view sv(reinterpret_cast<char*>(&msg[0]), msg.size());
this->onMessage(sv);
msg.clear();
}
std::this_thread::yield();
}
},
std::move(socket));
thread.detach();
}
};
It's mostly taken from your example and condensed into a single class. Instead of boost, I use sockpp for socket programming.i
After creating a `v8::Isolate`, I do the following:
inspector_ = std::make_unique<MyV8InspectorClient>(cdt_port, isolate_);
inspector_->launch();
However, when connecting to the inspector it says "WebSocket disconnected" and the application crashes with a segmentation violation:
AddressSanitizer:DEADLYSIGNAL
=================================================================
==13072==ERROR: AddressSanitizer: SEGV on unknown address 0x1d580000a838 (pc 0x561c8173e6b4 bp 0x7fff75fa6430 sp 0x7fff75fa6430 T0)
==13072==The signal is caused by a READ memory access.
#0 0x561c8173e6b4 in v8::Isolate::GetHeapProfiler() ../../../../../third-party/v8/v8/src/execution/isolate.h:1059:48
#1 0x561c82d3fd77 in v8_inspector::V8HeapProfilerAgentImpl::stopTrackingHeapObjectsInternal() ../../../../../third-party/v8/v8/src/inspector/v8-heap-profiler-agent-impl.cc:316:14
#2 0x561c82d3feb9 in v8_inspector::V8HeapProfilerAgentImpl::disable() ../../../../../third-party/v8/v8/src/inspector/v8-heap-profiler-agent-impl.cc:204:3
#3 0x561c8208f095 in v8_inspector::V8InspectorSessionImpl::~V8InspectorSessionImpl() ../../../../../third-party/v8/v8/src/inspector/v8-inspector-session-impl.cc:151:24
#4 0x561c8208f33d in v8_inspector::V8InspectorSessionImpl::~V8InspectorSessionImpl() ../../../../../third-party/v8/v8/src/inspector/v8-inspector-session-impl.cc:147:51
#5 0x561c80ec9eff in std::default_delete<v8_inspector::V8InspectorSession>::operator()(v8_inspector::V8InspectorSession*) const /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../include/c++/10.1.0/bits/unique_ptr.h:84:2
#6 0x561c80ec9d3e in std::__uniq_ptr_impl<v8_inspector::V8InspectorSession, std::default_delete<v8_inspector::V8InspectorSession> >::reset(v8_inspector::V8InspectorSession*) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../include/c++/10.1.0/bits/unique_ptr.h:181:4
#7 0x561c80eca183 in std::unique_ptr<v8_inspector::V8InspectorSession, std::default_delete<v8_inspector::V8InspectorSession> >::reset(v8_inspector::V8InspectorSession*) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../include/c++/10.1.0/bits/unique_ptr.h:455:7
#8 0x561c80ec7193 in db::v8_helper::MyV8InspectorClient::~MyV8InspectorClient() /home/immanuel/Documents/Work/PhD/mutable/mutable/build/debug/../../src/backend/V8Platform.hpp:74:18
#9 0x561c80ec723b in db::v8_helper::MyV8InspectorClient::~MyV8InspectorClient() /home/immanuel/Documents/Work/PhD/mutable/mutable/build/debug/../../src/backend/V8Platform.hpp:73:28
#10 0x561c80eb361f in std::default_delete<db::v8_helper::MyV8InspectorClient>::operator()(db::v8_helper::MyV8InspectorClient*) const /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../include/c++/10.1.0/bits/unique_ptr.h:84:2
#11 0x561c80eaede6 in std::unique_ptr<db::v8_helper::MyV8InspectorClient, std::default_delete<db::v8_helper::MyV8InspectorClient> >::~unique_ptr() /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../include/c++/10.1.0/bits/unique_ptr.h:360:4
#12 0x561c80ea6e97 in db::V8Platform::~V8Platform() /home/immanuel/Documents/Work/PhD/mutable/mutable/build/debug/../../src/backend/V8Platform.cpp:221:1
#13 0x561c80ea6f2b in db::V8Platform::~V8Platform() /home/immanuel/Documents/Work/PhD/mutable/mutable/build/debug/../../src/backend/V8Platform.cpp:218:1
#14 0x561c80e9ea3f in std::default_delete<db::WasmPlatform>::operator()(db::WasmPlatform*) const /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../include/c++/10.1.0/bits/unique_ptr.h:84:2
#15 0x561c80e9e8f6 in std::unique_ptr<db::WasmPlatform, std::default_delete<db::WasmPlatform> >::~unique_ptr() /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../include/c++/10.1.0/bits/unique_ptr.h:360:4
#16 0x561c80e8efbd in db::WasmBackend::~WasmBackend() /home/immanuel/Documents/Work/PhD/mutable/mutable/build/debug/../../src/backend/WebAssembly.hpp:110:8
#17 0x561c80e8efeb in db::WasmBackend::~WasmBackend() /home/immanuel/Documents/Work/PhD/mutable/mutable/build/debug/../../src/backend/WebAssembly.hpp:110:8
#18 0x561c80ccf1ff in std::default_delete<db::Backend>::operator()(db::Backend*) const /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../include/c++/10.1.0/bits/unique_ptr.h:84:2
#19 0x561c80ca8b16 in std::unique_ptr<db::Backend, std::default_delete<db::Backend> >::~unique_ptr() /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../include/c++/10.1.0/bits/unique_ptr.h:360:4
#20 0x561c80c86dd9 in process_stream(std::istream&, char const*, db::Diagnostic) /home/immanuel/Documents/Work/PhD/mutable/mutable/build/debug/../../src/shell.cpp:151:13
#21 0x561c80c9a265 in main /home/immanuel/Documents/Work/PhD/mutable/mutable/build/debug/../../src/shell.cpp:675:13
#22 0x7f6259a37001 in __libc_start_main (/usr/lib/libc.so.6+0x27001)
#23 0x561c80ba722d in _start (/home/immanuel/Documents/Work/PhD/mutable/mutable/build/debug/bin/shell+0x117222d)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV ../../../../../third-party/v8/v8/src/execution/isolate.h:1059:48 in v8::Isolate::GetHeapProfiler()
Can you spot what's going wrong? I did not override `V8InspectorClientImpl::runMessageLoopOnPause` like you did. Is this necessary?
Kind regards,
Immanuel