================
@@ -285,6 +304,94 @@ static llvm::Error
LaunchRunInTerminalTarget(llvm::opt::Arg &target_arg,
#endif
lldb_private::FileSystem::Initialize();
+
+#ifdef _WIN32
+ RunInTerminalLauncherCommChannel comm_channel(comm_file);
+
+ auto wcommandLineOrErr =
+ lldb_private::GetFlattenedWindowsCommandStringW(argv);
+ if (!wcommandLineOrErr)
+ return notifyError(comm_channel, "Failed to process arguments");
+
+ STARTUPINFOEXW startupinfoex = {};
+ startupinfoex.StartupInfo.cb = sizeof(STARTUPINFOEXW);
+ startupinfoex.StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
+
+ HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
+ HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
+ HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
+ llvm::scope_exit close_handles([&] {
+ if (stdin_handle)
+ ::CloseHandle(stdin_handle);
+ if (stdout_handle)
+ ::CloseHandle(stdout_handle);
+ if (stderr_handle)
+ ::CloseHandle(stderr_handle);
+ });
+
+ auto attributelist_cleanup_or_err =
+ lldb_private::SetupProcThreadAttributeList(startupinfoex);
+ if (!attributelist_cleanup_or_err) {
+ return notifyError(comm_channel, "Could not open inherited handles",
+ attributelist_cleanup_or_err.getError());
+ }
+ auto attributelist_cleanup = std::move(*attributelist_cleanup_or_err);
+
+ if (!stdio.empty()) {
+ llvm::SmallVector<llvm::StringRef, 3> files;
+ stdio.split(files, ':');
+ while (files.size() < 3)
+ files.push_back(files.back());
+
+ stdin_handle = lldb_private::ProcessLauncherWindows::GetStdioHandle(
+ files[0], STDIN_FILENO);
+ stdout_handle = lldb_private::ProcessLauncherWindows::GetStdioHandle(
+ files[1], STDOUT_FILENO);
+ stderr_handle = lldb_private::ProcessLauncherWindows::GetStdioHandle(
+ files[2], STDERR_FILENO);
+ }
+ auto inherited_handles_or_err =
+ lldb_private::ProcessLauncherWindows::GetInheritedHandles(
+ startupinfoex, /*launch_info*=*/nullptr, stdout_handle,
stderr_handle,
+ stdin_handle);
+
+ if (!inherited_handles_or_err)
+ return notifyError(comm_channel, "Failed to get inherited handles",
+ inherited_handles_or_err.getError());
+ std::vector<HANDLE> inherited_handles = std::move(*inherited_handles_or_err);
+
+ PROCESS_INFORMATION pi = {};
+
+ // Start the process in a suspended state, while we attach the debugger.
+ BOOL result = CreateProcessW(
+ /*lpApplicationName=*/NULL, /*lpCommandLine=*/&(*wcommandLineOrErr)[0],
+ /*lpProcessAttributes=*/NULL, /*lpThreadAttributes=*/NULL,
+ /*bInheritHandles=*/!inherited_handles.empty(),
+ /*dwCreationFlags=*/CREATE_SUSPENDED, /*lpEnvironment=*/NULL,
+ /*lpCurrentDirectory=*/NULL,
+ /*lpStartupInfo=*/reinterpret_cast<STARTUPINFOW *>(&startupinfoex),
+ /*lpProcessInformation=*/&pi);
+
+ if (!result)
+ return notifyError(comm_channel, "Failed to launch target process");
+
+ // Notify the pid of the process to debug to the debugger. It will attach to
+ // the newly created process.
+ if (llvm::Error err = comm_channel.NotifyPid(pi.dwProcessId))
+ return err;
+
+ if (llvm::Error err = comm_channel.WaitUntilDebugAdapterAttaches(
+ std::chrono::milliseconds(timeout_in_ms)))
+ return err;
+
+ // The debugger attached to the process. We can resume it.
+ if (!ResumeThread(pi.hThread))
+ return notifyError(comm_channel, "Failed to resume the target process");
+
+ // Wait for child to complete to match POSIX behavior.
+ WaitForSingleObject(pi.hProcess, INFINITE);
----------------
ashgti wrote:
I am not sure if we need to wait here or not.
The posix version is using `execvp`, which would exec the new process and
replace the current process so the `execvp` won't actually return unless an
error occurs (e.g. there is a bad path or something).
https://github.com/llvm/llvm-project/pull/174635
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits