25#include "llvm/ADT/StringRef.h"
26#include "llvm/Support/ConvertUTF.h"
34using llvm::sys::windows::UTF8ToUTF16;
37 llvm::Triple &triple) {
43 return llvm::errorToBool(imageBinaryP.takeError());
44 File &imageBinary = *imageBinaryP.get();
48 uint16_t machineType = 0;
49 size_t readSize =
sizeof(peOffset);
50 imageBinary.
Read(&peOffset, readSize);
52 imageBinary.
Read(&peHead, readSize);
53 if (peHead != 0x00004550)
56 imageBinary.
Read(&machineType, readSize);
57 triple.setVendor(llvm::Triple::PC);
58 triple.setOS(llvm::Triple::Win32);
59 triple.setArch(llvm::Triple::UnknownArch);
60 if (machineType == 0x8664)
61 triple.setArch(llvm::Triple::x86_64);
62 else if (machineType == 0x14c)
63 triple.setArch(llvm::Triple::x86);
64 else if (machineType == 0x1c4)
65 triple.setArch(llvm::Triple::arm);
66 else if (machineType == 0xaa64)
67 triple.setArch(llvm::Triple::aarch64);
76 std::vector<wchar_t> buffer(
PATH_MAX);
77 DWORD dwSize = buffer.size();
78 if (!::QueryFullProcessImageNameW(handle.
get(), 0, &buffer[0], &dwSize))
80 return llvm::convertWideToUTF8(buffer.data(), path);
89 std::string executable;
91 triple.setVendor(llvm::Triple::PC);
92 triple.setOS(llvm::Triple::Win32);
93 triple.setArch(llvm::Triple::UnknownArch);
95 FileSpec executableFile(executable.c_str());
109 AutoHandle handle(::OpenProcess(PROCESS_TERMINATE, FALSE, pid),
nullptr);
110 if (handle.IsValid())
111 ::TerminateProcess(handle.get(), 1);
117 FileSpec module_filespec;
119 HMODULE hmodule = NULL;
120 if (!::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
121 (LPCTSTR)host_addr, &hmodule))
122 return module_filespec;
124 std::vector<wchar_t> buffer(
PATH_MAX);
125 DWORD chars_copied = 0;
127 chars_copied = ::GetModuleFileNameW(hmodule, &buffer[0], buffer.size());
128 if (chars_copied == buffer.size() &&
129 ::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
130 buffer.resize(buffer.size() * 2);
131 }
while (chars_copied >= buffer.size());
133 if (!llvm::convertWideToUTF8(buffer.data(), path))
134 return module_filespec;
135 module_filespec.
SetFile(path, FileSpec::Style::native);
136 return module_filespec;
141 process_infos.clear();
143 AutoHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
144 if (!snapshot.IsValid())
147 PROCESSENTRY32W pe = {};
148 pe.dwSize =
sizeof(PROCESSENTRY32W);
149 if (Process32FirstW(snapshot.get(), &pe)) {
151 AutoHandle handle(::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE,
155 ProcessInstanceInfo process;
157 llvm::convertWideToUTF8(pe.szExeFile, exeFile);
164 process_infos.push_back(process);
165 }
while (Process32NextW(snapshot.get(), &pe));
167 return process_infos.size();
171 process_info.
Clear();
174 ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid),
176 if (!handle.IsValid())
184 AutoHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
185 if (!snapshot.IsValid())
189 pe.dwSize =
sizeof(PROCESSENTRY32W);
190 if (Process32FirstW(snapshot.get(), &pe)) {
192 if (pe.th32ProcessID == pid) {
196 }
while (Process32NextW(snapshot.get(), &pe));
209 if (launch_info.
GetFlags().
Test(eLaunchFlagShellExpandArguments)) {
210 FileSpec expand_tool_spec = HostInfo::GetSupportExeDir();
211 if (!expand_tool_spec) {
213 "could not find support executable directory for "
214 "the lldb-argdumper tool");
223 std::string quoted_cmd_string;
225 std::replace(quoted_cmd_string.begin(), quoted_cmd_string.end(),
'\\',
'/');
226 StreamString expand_command;
228 expand_command.
Printf(
"\"%s\" %s", expand_tool_spec.
GetPath().c_str(),
229 quoted_cmd_string.c_str());
233 std::string command = expand_command.
GetString().str();
236 &status,
nullptr, &output, std::chrono::seconds(10));
243 "lldb-argdumper exited with error %d", status);
253 auto dict_sp = data_sp->GetAsDictionary();
259 auto args_sp = dict_sp->GetObjectForDotSeparatedPath(
"arguments");
265 auto args_array_sp = args_sp->GetAsArray();
266 if (!args_array_sp) {
273 for (
size_t i = 0; i < args_array_sp->GetSize(); i++) {
274 auto item_sp = args_array_sp->GetItemAtIndex(i);
277 auto str_sp = item_sp->GetAsString();
293 LPWCH environment_block = ::GetEnvironmentStringsW();
294 while (*environment_block != L
'\0') {
295 std::string current_var;
296 auto current_var_size = wcslen(environment_block) + 1;
297 if (!llvm::convertWideToUTF8(environment_block, current_var)) {
298 environment_block += current_var_size;
301 if (current_var[0] !=
'=')
304 environment_block += current_var_size;
314 llvm::raw_string_ostream stream(log_msg);
318 stream <<
"[Warning] ";
321 stream <<
"[Error] ";
331 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 Status ShellExpandArguments(ProcessLaunchInfo &launch_info)
Perform expansion of the command-line for this launch info This can potentially involve wildcard expa...
static Status RunShellCommand(llvm::StringRef command, const FileSpec &working_dir, int *status_ptr, int *signo_ptr, std::string *command_output, const Timeout< std::micro > &timeout, bool run_in_shell=true, bool hide_stderr=false)
Run a shell command.
static lldb::thread_t GetCurrentThread()
Get the thread token (the one returned by ThreadCreate when the thread was created) for the calling t...
static Environment GetEnvironment()
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::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 bool GetExecutableForProcess(const AutoHandle &handle, std::string &path)