Author: Charles Zablit Date: 2026-01-08T18:08:27Z New Revision: 65f39fde2bb97ef9e7d4a59c4d08a9df77be985e
URL: https://github.com/llvm/llvm-project/commit/65f39fde2bb97ef9e7d4a59c4d08a9df77be985e DIFF: https://github.com/llvm/llvm-project/commit/65f39fde2bb97ef9e7d4a59c4d08a9df77be985e.diff LOG: [NFC][lldb][windows] extract the InitializeProcThreadAttributeList logic (#175016) Added: Modified: lldb/include/lldb/Host/windows/ProcessLauncherWindows.h lldb/source/Host/windows/ProcessLauncherWindows.cpp Removed: ################################################################################ diff --git a/lldb/include/lldb/Host/windows/ProcessLauncherWindows.h b/lldb/include/lldb/Host/windows/ProcessLauncherWindows.h index 553263e2f5a72..b835885dd47c6 100644 --- a/lldb/include/lldb/Host/windows/ProcessLauncherWindows.h +++ b/lldb/include/lldb/Host/windows/ProcessLauncherWindows.h @@ -17,6 +17,52 @@ namespace lldb_private { class ProcessLaunchInfo; +/// This class manages the lifetime of a PROC_THREAD_ATTRIBUTE_LIST, which is +/// used with STARTUPINFOEX. +/// +/// The attribute list is automatically cleaned up when this object is +/// destroyed. +class ProcThreadAttributeList { +public: + /// Allocate memory for the attribute list, initialize it, and sets the + /// lpAttributeList member of STARTUPINFOEXW structure. + /// + /// \param[in,out] startupinfoex + /// The STARTUPINFOEXW structure whose lpAttributeList member will be set + /// to point to the attribute list. The caller must ensure + /// this structure remains valid for the lifetime of the returned object. + /// + /// \return + /// A ProcThreadAttributeList object on success, or an error code on + /// failure. + static llvm::ErrorOr<ProcThreadAttributeList> + Create(STARTUPINFOEXW &startupinfoex); + + ~ProcThreadAttributeList() { + if (lpAttributeList) { + DeleteProcThreadAttributeList(lpAttributeList); + free(lpAttributeList); + } + } + + /// ProcThreadAttributeList is not copyable. + /// @{ + ProcThreadAttributeList(const ProcThreadAttributeList &) = delete; + ProcThreadAttributeList &operator=(const ProcThreadAttributeList &) = delete; + /// @} + + ProcThreadAttributeList(ProcThreadAttributeList &&other) noexcept + : lpAttributeList(other.lpAttributeList) { + other.lpAttributeList = nullptr; + } + +private: + explicit ProcThreadAttributeList(LPPROC_THREAD_ATTRIBUTE_LIST list) + : lpAttributeList(list) {} + + LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList; +}; + class ProcessLauncherWindows : public ProcessLauncher { public: HostProcess LaunchProcess(const ProcessLaunchInfo &launch_info, diff --git a/lldb/source/Host/windows/ProcessLauncherWindows.cpp b/lldb/source/Host/windows/ProcessLauncherWindows.cpp index 4b7cbec828dd8..76feceadf46f3 100644 --- a/lldb/source/Host/windows/ProcessLauncherWindows.cpp +++ b/lldb/source/Host/windows/ProcessLauncherWindows.cpp @@ -83,6 +83,29 @@ GetFlattenedWindowsCommandStringW(Args args) { return llvm::sys::flattenWindowsCommandLine(args_ref); } +llvm::ErrorOr<ProcThreadAttributeList> +ProcThreadAttributeList::Create(STARTUPINFOEXW &startupinfoex) { + SIZE_T attributelist_size = 0; + InitializeProcThreadAttributeList(/*lpAttributeList=*/nullptr, + /*dwAttributeCount=*/1, /*dwFlags=*/0, + &attributelist_size); + + startupinfoex.lpAttributeList = + static_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(malloc(attributelist_size)); + + if (!startupinfoex.lpAttributeList) + return llvm::mapWindowsError(ERROR_OUTOFMEMORY); + + if (!InitializeProcThreadAttributeList(startupinfoex.lpAttributeList, + /*dwAttributeCount=*/1, + /*dwFlags=*/0, &attributelist_size)) { + free(startupinfoex.lpAttributeList); + return llvm::mapWindowsError(GetLastError()); + } + + return ProcThreadAttributeList(startupinfoex.lpAttributeList); +} + HostProcess ProcessLauncherWindows::LaunchProcess(const ProcessLaunchInfo &launch_info, Status &error) { @@ -109,19 +132,9 @@ ProcessLauncherWindows::LaunchProcess(const ProcessLaunchInfo &launch_info, ::CloseHandle(stderr_handle); }); - SIZE_T attributelist_size = 0; - InitializeProcThreadAttributeList(/*lpAttributeList=*/nullptr, - /*dwAttributeCount=*/1, /*dwFlags=*/0, - &attributelist_size); - - startupinfoex.lpAttributeList = - static_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(malloc(attributelist_size)); - llvm::scope_exit free_attributelist( - [&] { free(startupinfoex.lpAttributeList); }); - if (!InitializeProcThreadAttributeList(startupinfoex.lpAttributeList, - /*dwAttributeCount=*/1, /*dwFlags=*/0, - &attributelist_size)) { - error = Status(::GetLastError(), eErrorTypeWin32); + auto attributelist_or_err = ProcThreadAttributeList::Create(startupinfoex); + if (!attributelist_or_err) { + error = attributelist_or_err.getError(); return HostProcess(); } llvm::scope_exit delete_attributelist( _______________________________________________ lldb-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
