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 |
|
439 |
std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate, |
35 |
|
|
Local<Value> value) { |
36 |
|
439 |
TwoByteValue buffer(isolate, value); |
37 |
|
439 |
return StringBuffer::create(StringView(*buffer, buffer.length())); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
struct LocalConnection { |
41 |
|
210 |
static std::unique_ptr<InspectorSession> Connect( |
42 |
|
|
Agent* inspector, std::unique_ptr<InspectorSessionDelegate> delegate) { |
43 |
|
210 |
return inspector->Connect(std::move(delegate), false); |
44 |
|
|
} |
45 |
|
|
|
46 |
|
5095 |
static Local<String> GetClassName(Environment* env) { |
47 |
|
5095 |
return FIXED_ONE_BYTE_STRING(env->isolate(), "Connection"); |
48 |
|
|
} |
49 |
|
|
}; |
50 |
|
|
|
51 |
|
|
struct MainThreadConnection { |
52 |
|
2 |
static std::unique_ptr<InspectorSession> Connect( |
53 |
|
|
Agent* inspector, std::unique_ptr<InspectorSessionDelegate> delegate) { |
54 |
|
2 |
return inspector->ConnectToMainThread(std::move(delegate), true); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
5095 |
static Local<String> GetClassName(Environment* env) { |
58 |
|
5095 |
return FIXED_ONE_BYTE_STRING(env->isolate(), "MainThreadConnection"); |
59 |
|
|
} |
60 |
|
|
}; |
61 |
|
|
|
62 |
|
|
template <typename ConnectionType> |
63 |
✗✗✓✓
|
636 |
class JSBindingsConnection : public AsyncWrap { |
64 |
|
|
public: |
65 |
✗✓✗✓
|
424 |
class JSBindingsSessionDelegate : public InspectorSessionDelegate { |
66 |
|
|
public: |
67 |
|
212 |
JSBindingsSessionDelegate(Environment* env, |
68 |
|
|
JSBindingsConnection* connection) |
69 |
|
|
: env_(env), |
70 |
|
212 |
connection_(connection) { |
71 |
|
212 |
} |
72 |
|
|
|
73 |
|
4000 |
void SendMessageToFrontend(const v8_inspector::StringView& message) |
74 |
|
|
override { |
75 |
|
4000 |
Isolate* isolate = env_->isolate(); |
76 |
|
4000 |
HandleScope handle_scope(isolate); |
77 |
|
4000 |
Context::Scope context_scope(env_->context()); |
78 |
|
|
MaybeLocal<String> v8string = |
79 |
|
|
String::NewFromTwoByte(isolate, message.characters16(), |
80 |
|
4000 |
NewStringType::kNormal, message.length()); |
81 |
|
8000 |
Local<Value> argument = v8string.ToLocalChecked().As<Value>(); |
82 |
|
8000 |
connection_->OnMessage(argument); |
83 |
|
4000 |
} |
84 |
|
|
|
85 |
|
|
private: |
86 |
|
|
Environment* env_; |
87 |
|
|
JSBindingsConnection* connection_; |
88 |
|
|
}; |
89 |
|
|
|
90 |
|
212 |
JSBindingsConnection(Environment* env, |
91 |
|
|
Local<Object> wrap, |
92 |
|
|
Local<Function> callback) |
93 |
|
|
: AsyncWrap(env, wrap, PROVIDER_INSPECTORJSBINDING), |
94 |
|
212 |
callback_(env->isolate(), callback) { |
95 |
|
212 |
Agent* inspector = env->inspector_agent(); |
96 |
|
212 |
session_ = ConnectionType::Connect( |
97 |
|
|
inspector, std::make_unique<JSBindingsSessionDelegate>(env, this)); |
98 |
|
212 |
} |
99 |
|
|
|
100 |
|
4000 |
void OnMessage(Local<Value> value) { |
101 |
|
8000 |
MakeCallback(callback_.Get(env()->isolate()), 1, &value); |
102 |
|
4000 |
} |
103 |
|
|
|
104 |
|
10190 |
static void Bind(Environment* env, Local<Object> target) { |
105 |
|
10190 |
Local<String> class_name = ConnectionType::GetClassName(env); |
106 |
|
|
Local<FunctionTemplate> tmpl = |
107 |
|
10190 |
env->NewFunctionTemplate(JSBindingsConnection::New); |
108 |
|
20380 |
tmpl->InstanceTemplate()->SetInternalFieldCount(1); |
109 |
|
10190 |
tmpl->SetClassName(class_name); |
110 |
|
20380 |
tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
111 |
|
10190 |
env->SetProtoMethod(tmpl, "dispatch", JSBindingsConnection::Dispatch); |
112 |
|
10190 |
env->SetProtoMethod(tmpl, "disconnect", JSBindingsConnection::Disconnect); |
113 |
|
|
target->Set(env->context(), |
114 |
|
|
class_name, |
115 |
|
50950 |
tmpl->GetFunction(env->context()).ToLocalChecked()) |
116 |
|
20380 |
.ToChecked(); |
117 |
|
10190 |
} |
118 |
|
|
|
119 |
|
212 |
static void New(const FunctionCallbackInfo<Value>& info) { |
120 |
|
212 |
Environment* env = Environment::GetCurrent(info); |
121 |
✗✓✗✓
|
424 |
CHECK(info[0]->IsFunction()); |
122 |
|
424 |
Local<Function> callback = info[0].As<Function>(); |
123 |
|
212 |
new JSBindingsConnection(env, info.This(), callback); |
124 |
|
212 |
} |
125 |
|
|
|
126 |
|
200 |
void Disconnect() { |
127 |
|
200 |
session_.reset(); |
128 |
✗✗✓✗
|
200 |
delete this; |
129 |
|
200 |
} |
130 |
|
|
|
131 |
|
200 |
static void Disconnect(const FunctionCallbackInfo<Value>& info) { |
132 |
|
|
JSBindingsConnection* session; |
133 |
✗✗✗✓
|
400 |
ASSIGN_OR_RETURN_UNWRAP(&session, info.Holder()); |
134 |
|
200 |
session->Disconnect(); |
135 |
|
|
} |
136 |
|
|
|
137 |
|
439 |
static void Dispatch(const FunctionCallbackInfo<Value>& info) { |
138 |
|
439 |
Environment* env = Environment::GetCurrent(info); |
139 |
|
|
JSBindingsConnection* session; |
140 |
✗✓✗✓
|
878 |
ASSIGN_OR_RETURN_UNWRAP(&session, info.Holder()); |
141 |
✗✓✗✓
|
1317 |
CHECK(info[0]->IsString()); |
142 |
|
|
|
143 |
✓✗✓✗
|
439 |
if (session->session_) { |
144 |
|
878 |
session->session_->Dispatch( |
145 |
|
878 |
ToProtocolString(env->isolate(), info[0])->string()); |
146 |
|
|
} |
147 |
|
|
} |
148 |
|
|
|
149 |
|
2 |
void MemoryInfo(MemoryTracker* tracker) const override { |
150 |
|
2 |
tracker->TrackField("callback", callback_); |
151 |
|
2 |
tracker->TrackFieldWithSize( |
152 |
|
|
"session", sizeof(*session_), "InspectorSession"); |
153 |
|
2 |
} |
154 |
|
|
|
155 |
|
2 |
SET_MEMORY_INFO_NAME(JSBindingsConnection) |
156 |
|
2 |
SET_SELF_SIZE(JSBindingsConnection) |
157 |
|
|
|
158 |
|
|
private: |
159 |
|
|
std::unique_ptr<InspectorSession> session_; |
160 |
|
|
Global<Function> callback_; |
161 |
|
|
}; |
162 |
|
|
|
163 |
|
226619 |
static bool InspectorEnabled(Environment* env) { |
164 |
|
226619 |
Agent* agent = env->inspector_agent(); |
165 |
|
226619 |
return agent->IsActive(); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
5095 |
void SetConsoleExtensionInstaller(const FunctionCallbackInfo<Value>& info) { |
169 |
|
5095 |
auto env = Environment::GetCurrent(info); |
170 |
|
|
|
171 |
✗✓ |
5095 |
CHECK_EQ(info.Length(), 1); |
172 |
✗✓ |
10190 |
CHECK(info[0]->IsFunction()); |
173 |
|
|
|
174 |
|
10190 |
env->set_inspector_console_extension_installer(info[0].As<Function>()); |
175 |
|
5095 |
} |
176 |
|
|
|
177 |
|
6 |
void CallAndPauseOnStart(const FunctionCallbackInfo<v8::Value>& args) { |
178 |
|
6 |
Environment* env = Environment::GetCurrent(args); |
179 |
✗✓ |
6 |
CHECK_GT(args.Length(), 1); |
180 |
✗✓ |
12 |
CHECK(args[0]->IsFunction()); |
181 |
|
6 |
SlicedArguments call_args(args, /* start */ 2); |
182 |
|
6 |
env->inspector_agent()->PauseOnNextJavascriptStatement("Break on start"); |
183 |
|
|
v8::MaybeLocal<v8::Value> retval = |
184 |
|
11 |
args[0].As<v8::Function>()->Call(env->context(), args[1], |
185 |
|
24 |
call_args.length(), call_args.out()); |
186 |
✓✓ |
5 |
if (!retval.IsEmpty()) { |
187 |
|
8 |
args.GetReturnValue().Set(retval.ToLocalChecked()); |
188 |
|
5 |
} |
189 |
|
5 |
} |
190 |
|
|
|
191 |
|
226617 |
void InspectorConsoleCall(const FunctionCallbackInfo<Value>& info) { |
192 |
|
226617 |
Environment* env = Environment::GetCurrent(info); |
193 |
|
226617 |
Isolate* isolate = env->isolate(); |
194 |
|
226617 |
Local<Context> context = isolate->GetCurrentContext(); |
195 |
✗✓ |
226617 |
CHECK_GE(info.Length(), 2); |
196 |
|
226617 |
SlicedArguments call_args(info, /* start */ 2); |
197 |
✓✓ |
226617 |
if (InspectorEnabled(env)) { |
198 |
|
226558 |
Local<Value> inspector_method = info[0]; |
199 |
✗✓ |
226558 |
CHECK(inspector_method->IsFunction()); |
200 |
✓✓ |
226558 |
if (!env->is_in_inspector_console_call()) { |
201 |
|
226557 |
env->set_is_in_inspector_console_call(true); |
202 |
|
|
MaybeLocal<Value> ret = |
203 |
|
|
inspector_method.As<Function>()->Call(context, |
204 |
|
|
info.Holder(), |
205 |
|
226557 |
call_args.length(), |
206 |
|
906228 |
call_args.out()); |
207 |
|
226557 |
env->set_is_in_inspector_console_call(false); |
208 |
✓✓ |
226557 |
if (ret.IsEmpty()) |
209 |
|
226626 |
return; |
210 |
|
|
} |
211 |
|
|
} |
212 |
|
|
|
213 |
|
226608 |
Local<Value> node_method = info[1]; |
214 |
✗✓ |
226608 |
CHECK(node_method->IsFunction()); |
215 |
|
|
node_method.As<Function>()->Call(context, |
216 |
|
|
info.Holder(), |
217 |
|
226608 |
call_args.length(), |
218 |
✓✓ |
1133040 |
call_args.out()).FromMaybe(Local<Value>()); |
219 |
|
|
} |
220 |
|
|
|
221 |
|
7 |
static void* GetAsyncTask(int64_t asyncId) { |
222 |
|
|
// The inspector assumes that when other clients use its asyncTask* API, |
223 |
|
|
// they use real pointers, or at least something aligned like real pointer. |
224 |
|
|
// In general it means that our task_id should always be even. |
225 |
|
|
// |
226 |
|
|
// On 32bit platforms, the 64bit asyncId would get truncated when converted |
227 |
|
|
// to a 32bit pointer. However, the javascript part will never enable |
228 |
|
|
// the async_hook on 32bit platforms, therefore the truncation will never |
229 |
|
|
// happen in practice. |
230 |
|
7 |
return reinterpret_cast<void*>(asyncId << 1); |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
template <void (Agent::*asyncTaskFn)(void*)> |
234 |
|
6 |
static void InvokeAsyncTaskFnWithId(const FunctionCallbackInfo<Value>& args) { |
235 |
|
6 |
Environment* env = Environment::GetCurrent(args); |
236 |
✗✓✗✓ ✗✓ |
12 |
CHECK(args[0]->IsNumber()); |
237 |
|
24 |
int64_t task_id = args[0]->IntegerValue(env->context()).FromJust(); |
238 |
|
6 |
(env->inspector_agent()->*asyncTaskFn)(GetAsyncTask(task_id)); |
239 |
|
6 |
} |
240 |
|
|
|
241 |
|
1 |
static void AsyncTaskScheduledWrapper(const FunctionCallbackInfo<Value>& args) { |
242 |
|
1 |
Environment* env = Environment::GetCurrent(args); |
243 |
|
|
|
244 |
✗✓ |
3 |
CHECK(args[0]->IsString()); |
245 |
|
2 |
Local<String> task_name = args[0].As<String>(); |
246 |
|
1 |
String::Value task_name_value(args.GetIsolate(), task_name); |
247 |
|
1 |
StringView task_name_view(*task_name_value, task_name_value.length()); |
248 |
|
|
|
249 |
✗✓ |
2 |
CHECK(args[1]->IsNumber()); |
250 |
|
4 |
int64_t task_id = args[1]->IntegerValue(env->context()).FromJust(); |
251 |
|
1 |
void* task = GetAsyncTask(task_id); |
252 |
|
|
|
253 |
✗✓ |
2 |
CHECK(args[2]->IsBoolean()); |
254 |
|
3 |
bool recurring = args[2]->BooleanValue(args.GetIsolate()); |
255 |
|
|
|
256 |
|
1 |
env->inspector_agent()->AsyncTaskScheduled(task_name_view, task, recurring); |
257 |
|
1 |
} |
258 |
|
|
|
259 |
|
5093 |
static void RegisterAsyncHookWrapper(const FunctionCallbackInfo<Value>& args) { |
260 |
|
5093 |
Environment* env = Environment::GetCurrent(args); |
261 |
|
|
|
262 |
✗✓ |
10186 |
CHECK(args[0]->IsFunction()); |
263 |
|
10186 |
Local<Function> enable_function = args[0].As<Function>(); |
264 |
✗✓ |
10186 |
CHECK(args[1]->IsFunction()); |
265 |
|
10186 |
Local<Function> disable_function = args[1].As<Function>(); |
266 |
|
|
env->inspector_agent()->RegisterAsyncHook(env->isolate(), |
267 |
|
5093 |
enable_function, disable_function); |
268 |
|
5093 |
} |
269 |
|
|
|
270 |
|
2 |
void IsEnabled(const FunctionCallbackInfo<Value>& args) { |
271 |
|
2 |
Environment* env = Environment::GetCurrent(args); |
272 |
|
6 |
args.GetReturnValue().Set(InspectorEnabled(env)); |
273 |
|
2 |
} |
274 |
|
|
|
275 |
|
4 |
void Open(const FunctionCallbackInfo<Value>& args) { |
276 |
|
4 |
Environment* env = Environment::GetCurrent(args); |
277 |
|
4 |
Agent* agent = env->inspector_agent(); |
278 |
|
|
|
279 |
✓✗✓✓ ✓✗✓✓
|
16 |
if (args.Length() > 0 && args[0]->IsUint32()) { |
280 |
|
6 |
uint32_t port = args[0].As<Uint32>()->Value(); |
281 |
|
2 |
agent->host_port()->set_port(static_cast<int>(port)); |
282 |
|
|
} |
283 |
|
|
|
284 |
✓✗✗✓ ✓✗✗✓
|
20 |
if (args.Length() > 1 && args[1]->IsString()) { |
285 |
|
|
Utf8Value host(env->isolate(), args[1].As<String>()); |
286 |
|
|
agent->host_port()->set_host(*host); |
287 |
|
|
} |
288 |
|
|
|
289 |
|
4 |
agent->StartIoThread(); |
290 |
|
4 |
} |
291 |
|
|
|
292 |
|
2 |
void WaitForDebugger(const FunctionCallbackInfo<Value>& args) { |
293 |
|
2 |
Environment* env = Environment::GetCurrent(args); |
294 |
|
2 |
Agent* agent = env->inspector_agent(); |
295 |
✓✗ |
2 |
if (agent->IsActive()) |
296 |
|
2 |
agent->WaitForConnect(); |
297 |
|
6 |
args.GetReturnValue().Set(agent->IsActive()); |
298 |
|
2 |
} |
299 |
|
|
|
300 |
|
9 |
void Url(const FunctionCallbackInfo<Value>& args) { |
301 |
|
9 |
Environment* env = Environment::GetCurrent(args); |
302 |
|
9 |
std::string url = env->inspector_agent()->GetWsUrl(); |
303 |
✓✓ |
9 |
if (url.length() == 0) { |
304 |
|
11 |
return; |
305 |
|
|
} |
306 |
✓✓ |
21 |
args.GetReturnValue().Set(OneByteString(env->isolate(), url.c_str())); |
307 |
|
|
} |
308 |
|
|
|
309 |
|
5095 |
void Initialize(Local<Object> target, Local<Value> unused, |
310 |
|
|
Local<Context> context, void* priv) { |
311 |
|
5095 |
Environment* env = Environment::GetCurrent(context); |
312 |
|
|
|
313 |
|
|
v8::Local<v8::Function> consoleCallFunc = |
314 |
|
|
env->NewFunctionTemplate(InspectorConsoleCall, v8::Local<v8::Signature>(), |
315 |
|
|
v8::ConstructorBehavior::kThrow, |
316 |
|
5095 |
v8::SideEffectType::kHasSideEffect) |
317 |
|
15285 |
->GetFunction(context) |
318 |
|
10190 |
.ToLocalChecked(); |
319 |
|
5095 |
auto name_string = FIXED_ONE_BYTE_STRING(env->isolate(), "consoleCall"); |
320 |
|
10190 |
target->Set(context, name_string, consoleCallFunc).Check(); |
321 |
|
5095 |
consoleCallFunc->SetName(name_string); |
322 |
|
|
|
323 |
|
|
env->SetMethod( |
324 |
|
5095 |
target, "setConsoleExtensionInstaller", SetConsoleExtensionInstaller); |
325 |
|
5095 |
env->SetMethod(target, "callAndPauseOnStart", CallAndPauseOnStart); |
326 |
|
5095 |
env->SetMethod(target, "open", Open); |
327 |
|
5095 |
env->SetMethodNoSideEffect(target, "url", Url); |
328 |
|
5095 |
env->SetMethod(target, "waitForDebugger", WaitForDebugger); |
329 |
|
|
|
330 |
|
5095 |
env->SetMethod(target, "asyncTaskScheduled", AsyncTaskScheduledWrapper); |
331 |
|
|
env->SetMethod(target, "asyncTaskCanceled", |
332 |
|
5095 |
InvokeAsyncTaskFnWithId<&Agent::AsyncTaskCanceled>); |
333 |
|
|
env->SetMethod(target, "asyncTaskStarted", |
334 |
|
5095 |
InvokeAsyncTaskFnWithId<&Agent::AsyncTaskStarted>); |
335 |
|
|
env->SetMethod(target, "asyncTaskFinished", |
336 |
|
5095 |
InvokeAsyncTaskFnWithId<&Agent::AsyncTaskFinished>); |
337 |
|
|
|
338 |
|
5095 |
env->SetMethod(target, "registerAsyncHook", RegisterAsyncHookWrapper); |
339 |
|
5095 |
env->SetMethodNoSideEffect(target, "isEnabled", IsEnabled); |
340 |
|
|
|
341 |
|
5095 |
JSBindingsConnection<LocalConnection>::Bind(env, target); |
342 |
|
5095 |
JSBindingsConnection<MainThreadConnection>::Bind(env, target); |
343 |
|
5095 |
} |
344 |
|
|
|
345 |
|
|
} // namespace |
346 |
|
|
} // namespace inspector |
347 |
|
|
} // namespace node |
348 |
|
|
|
349 |
|
4948 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(inspector, |
350 |
|
|
node::inspector::Initialize) |