26#include "llvm/ADT/StringRef.h"
27#include "llvm/Support/ConvertUTF.h"
35using llvm::sys::windows::UTF8ToUTF16;
38 llvm::Triple &triple) {
44 return llvm::errorToBool(imageBinaryP.takeError());
45 File &imageBinary = *imageBinaryP.get();
49 uint16_t machineType = 0;
50 size_t readSize =
sizeof(peOffset);
51 imageBinary.
Read(&peOffset, readSize);
53 imageBinary.
Read(&peHead, readSize);
54 if (peHead != 0x00004550)
57 imageBinary.
Read(&machineType, readSize);
58 triple.setVendor(llvm::Triple::PC);
59 triple.setOS(llvm::Triple::Win32);
60 triple.setArch(llvm::Triple::UnknownArch);
61 if (machineType == 0x8664)
62 triple.setArch(llvm::Triple::x86_64);
63 else if (machineType == 0x14c)
64 triple.setArch(llvm::Triple::x86);
65 else if (machineType == 0x1c4)
66 triple.setArch(llvm::Triple::arm);
67 else if (machineType == 0xaa64)
68 triple.setArch(llvm::Triple::aarch64);
77 std::vector<wchar_t> buffer(
PATH_MAX);
78 DWORD dwSize = buffer.size();
79 if (!::QueryFullProcessImageNameW(handle.
get(), 0, &buffer[0], &dwSize))
81 if (!llvm::convertWideToUTF8(buffer.data(), path))
94 std::string executable;
96 triple.setVendor(llvm::Triple::PC);
97 triple.setOS(llvm::Triple::Win32);
98 triple.setArch(llvm::Triple::UnknownArch);
100 FileSpec executableFile(executable.c_str());
114 AutoHandle handle(::OpenProcess(PROCESS_TERMINATE, FALSE, pid),
nullptr);
115 if (handle.IsValid())
116 ::TerminateProcess(handle.get(), 1);
122 FileSpec module_filespec;
124 HMODULE hmodule =
nullptr;
125 if (!::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
126 (LPCTSTR)host_addr, &hmodule))
127 return module_filespec;
129 std::vector<wchar_t> buffer(
PATH_MAX);
130 DWORD chars_copied = 0;
132 chars_copied = ::GetModuleFileNameW(hmodule, &buffer[0], buffer.size());
133 if (chars_copied == buffer.size() &&
134 ::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
135 buffer.resize(buffer.size() * 2);
136 }
while (chars_copied >= buffer.size());
138 if (!llvm::convertWideToUTF8(buffer.data(), path))
139 return module_filespec;
140 module_filespec.
SetFile(path, FileSpec::Style::native);
141 return module_filespec;
148 constexpr int max_attempts = 10;
149 for (
int attempt = 0; attempt < max_attempts; ++attempt) {
150 HANDLE snapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
151 if (snapshot != INVALID_HANDLE_VALUE ||
152 ::GetLastError() != ERROR_BAD_LENGTH)
155 return INVALID_HANDLE_VALUE;
160 process_infos.clear();
163 if (!snapshot.IsValid())
166 PROCESSENTRY32W pe = {};
167 pe.dwSize =
sizeof(PROCESSENTRY32W);
168 if (Process32FirstW(snapshot.get(), &pe)) {
170 AutoHandle handle(::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE,
174 ProcessInstanceInfo process;
176 llvm::convertWideToUTF8(pe.szExeFile, exeFile);
183 process_infos.push_back(process);
184 }
while (Process32NextW(snapshot.get(), &pe));
186 return process_infos.size();
190 process_info.
Clear();
193 ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid),
195 if (!handle.IsValid())
202 if (!snapshot.IsValid())
206 pe.dwSize =
sizeof(PROCESSENTRY32W);
207 if (!Process32FirstW(snapshot.get(), &pe))
211 if (pe.th32ProcessID == pid) {
215 }
while (Process32NextW(snapshot.get(), &pe));
228 if (launch_info.
GetFlags().
Test(eLaunchFlagShellExpandArguments)) {
229 FileSpec expand_tool_spec = HostInfo::GetSupportExeDir();
230 if (!expand_tool_spec) {
232 "could not find support executable directory for "
233 "the lldb-argdumper tool");
242 std::string quoted_cmd_string;
244 std::replace(quoted_cmd_string.begin(), quoted_cmd_string.end(),
'\\',
'/');
245 StreamString expand_command;
247 expand_command.
Printf(
"\"%s\" %s", expand_tool_spec.
GetPath().c_str(),
248 quoted_cmd_string.c_str());
252 std::string command = expand_command.
GetString().str();
255 &status,
nullptr, &output,
nullptr, timeout);
262 "lldb-argdumper exited with error %d", status);
272 auto dict_sp = data_sp->GetAsDictionary();
278 auto args_sp = dict_sp->GetObjectForDotSeparatedPath(
"arguments");
284 auto args_array_sp = args_sp->GetAsArray();
285 if (!args_array_sp) {
292 for (
size_t i = 0; i < args_array_sp->GetSize(); i++) {
293 auto item_sp = args_array_sp->GetItemAtIndex(i);
296 auto str_sp = item_sp->GetAsString();
312 LPWCH environment_block = ::GetEnvironmentStringsW();
313 while (*environment_block != L
'\0') {
314 std::string current_var;
315 auto current_var_size = wcslen(environment_block) + 1;
316 if (!llvm::convertWideToUTF8(environment_block, current_var)) {
317 environment_block += current_var_size;
320 if (current_var[0] !=
'=')
323 environment_block += current_var_size;
333 llvm::raw_string_ostream stream(log_msg);
337 stream <<
"[Warning] ";
340 stream <<
"[Error] ";
350 OutputDebugStringA(log_msg.c_str());
static llvm::raw_ostream & error(Stream &strm)
An architecture specification class.
void AppendArgument(llvm::StringRef arg_str, char quote_char='\0')
Appends a new argument to the end of the list argument list.
bool GetQuotedCommandString(std::string &command) const
void Clear()
Clear the arguments.
std::pair< iterator, bool > insert(llvm::StringRef KeyEqValue)
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
void AppendPathComponent(llvm::StringRef component)
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
int Open(const char *path, int flags, int mode=0600)
Wraps open in a platform-independent way.
static FileSystem & Instance()
An abstract base class for files.
Status Read(void *buf, size_t &num_bytes) override
Read bytes from a file from the current file position into buf.
virtual off_t SeekFromStart(off_t offset, Status *error_ptr=nullptr)
Seek to an offset relative to the beginning of the file.
bool Test(ValueType bit) const
Test a single flag bit.
static void SystemLog(lldb::Severity severity, llvm::StringRef message)
Emit the given message to the operating system log.
static lldb::thread_t GetCurrentThread()
Get the thread token (the one returned by ThreadCreate when the thread was created) for the calling t...
static Status ShellExpandArguments(ProcessLaunchInfo &launch_info, const Timeout< std::micro > &timeout)
Perform expansion of the command-line for this launch info This can potentially involve wildcard expa...
static Environment GetEnvironment()
static Status RunShellCommand(llvm::StringRef command, const FileSpec &working_dir, int *status_ptr, int *signo_ptr, std::string *command_output, std::string *error_output, const Timeout< std::micro > &timeout, bool run_in_shell=true)
Run a shell command.
static uint32_t FindProcessesImpl(const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &proc_infos)
static bool GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &proc_info)
static FileSpec GetModuleFileSpecForHostAddress(const void *host_addr)
Given an address in the current process (the process that is running the LLDB code),...
std::function< void(lldb::pid_t pid, int signal, int status)> MonitorChildProcessCallback
static llvm::Expected< HostThread > StartMonitoringChildProcess(const MonitorChildProcessCallback &callback, lldb::pid_t pid)
Start monitoring a child process.
static void Kill(lldb::pid_t pid, int signo)
static const char * GetSignalAsCString(int signo)
void SetExecutableFile(const FileSpec &exe_file, bool add_exe_file_as_first_arg)
void SetArchitecture(const ArchSpec &arch)
void SetProcessID(lldb::pid_t pid)
bool MatchAllProcesses() const
bool Matches(const ProcessInstanceInfo &proc_info) const
void SetParentProcessID(lldb::pid_t pid)
const FileSpec & GetWorkingDirectory() const
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status FromErrorString(const char *str)
bool Fail() const
Test for error condition.
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
static ObjectSP ParseJSON(llvm::StringRef json_text)
A class that represents a running process on the host machine.
std::string StripExtendedLengthPrefix(llvm::StringRef path)
Convert an extended-length path to a regular DOS path: "\\?\C:\a" becomes "C:\a" and "\\?...
std::vector< ProcessInstanceInfo > ProcessInstanceInfoList
Severity
Used for expressing severity in logs and diagnostics.
static void GetProcessExecutableAndTriple(const AutoHandle &handle, ProcessInstanceInfo &process)
static bool GetTripleForProcess(const FileSpec &executable, llvm::Triple &triple)
static HANDLE CreateProcessSnapshot()
static bool GetExecutableForProcess(const AutoHandle &handle, std::string &path)