Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2266,17 +2266,29 @@ static int GetDebugSignalHandlerMappingName(DWORD pid, wchar_t* buf,
static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = args.GetIsolate();

if (args.Length() != 1) {
env->ThrowError("Invalid number of arguments.");
return;
}

HANDLE process = nullptr;
HANDLE thread = nullptr;
HANDLE mapping = nullptr;
wchar_t mapping_name[32];
LPTHREAD_START_ROUTINE* handler = nullptr;
DWORD pid = 0;

if (args.Length() != 1) {
env->ThrowError("Invalid number of arguments.");
goto out;
}
OnScopeLeave cleanup([&]() {
if (process != nullptr)
CloseHandle(process);
if (thread != nullptr)
CloseHandle(thread);
if (handler != nullptr)
UnmapViewOfFile(handler);
if (mapping != nullptr)
CloseHandle(mapping);
});

CHECK(args[0]->IsNumber());
pid = args[0].As<Integer>()->Value();
Expand All @@ -2289,22 +2301,22 @@ static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
if (process == nullptr) {
isolate->ThrowException(
WinapiErrnoException(isolate, GetLastError(), "OpenProcess"));
goto out;
return;
}

if (GetDebugSignalHandlerMappingName(pid,
mapping_name,
arraysize(mapping_name)) < 0) {
env->ThrowErrnoException(errno, "sprintf");
goto out;
return;
}

mapping = OpenFileMappingW(FILE_MAP_READ, FALSE, mapping_name);
if (mapping == nullptr) {
isolate->ThrowException(WinapiErrnoException(isolate,
GetLastError(),
"OpenFileMappingW"));
goto out;
return;
}

handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
Expand All @@ -2316,7 +2328,7 @@ static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
if (handler == nullptr || *handler == nullptr) {
isolate->ThrowException(
WinapiErrnoException(isolate, GetLastError(), "MapViewOfFile"));
goto out;
return;
}

thread = CreateRemoteThread(process,
Expand All @@ -2330,26 +2342,16 @@ static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
isolate->ThrowException(WinapiErrnoException(isolate,
GetLastError(),
"CreateRemoteThread"));
goto out;
return;
}

// Wait for the thread to terminate
if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) {
isolate->ThrowException(WinapiErrnoException(isolate,
GetLastError(),
"WaitForSingleObject"));
goto out;
}

out:
if (process != nullptr)
CloseHandle(process);
if (thread != nullptr)
CloseHandle(thread);
if (handler != nullptr)
UnmapViewOfFile(handler);
if (mapping != nullptr)
CloseHandle(mapping);
return;
}
}
#endif // _WIN32

Expand Down