25#include "llvm/Support/ConvertUTF.h"
34 llvm::Triple &triple) {
40 return llvm::errorToBool(imageBinaryP.takeError());
41 File &imageBinary = *imageBinaryP.get();
45 uint16_t machineType = 0;
46 size_t readSize =
sizeof(peOffset);
47 imageBinary.
Read(&peOffset, readSize);
49 imageBinary.
Read(&peHead, readSize);
50 if (peHead != 0x00004550)
53 imageBinary.
Read(&machineType, readSize);
54 triple.setVendor(llvm::Triple::PC);
55 triple.setOS(llvm::Triple::Win32);
56 triple.setArch(llvm::Triple::UnknownArch);
57 if (machineType == 0x8664)
58 triple.setArch(llvm::Triple::x86_64);
59 else if (machineType == 0x14c)
60 triple.setArch(llvm::Triple::x86);
61 else if (machineType == 0x1c4)
62 triple.setArch(llvm::Triple::arm);
63 else if (machineType == 0xaa64)
64 triple.setArch(llvm::Triple::aarch64);
73 std::vector<wchar_t> buffer(
PATH_MAX);
74 DWORD dwSize = buffer.size();
75 if (!::QueryFullProcessImageNameW(handle.
get(), 0, &buffer[0], &dwSize))
77 return llvm::convertWideToUTF8(buffer.data(), path);
86 std::string executable;
88 triple.setVendor(llvm::Triple::PC);
89 triple.setOS(llvm::Triple::Win32);
90 triple.setArch(llvm::Triple::UnknownArch);
92 FileSpec executableFile(executable.c_str());
106 AutoHandle handle(::OpenProcess(PROCESS_TERMINATE, FALSE, pid),
nullptr);
107 if (handle.IsValid())
108 ::TerminateProcess(handle.get(), 1);
116 HMODULE hmodule = NULL;
117 if (!::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
118 (LPCTSTR)host_addr, &hmodule))
119 return module_filespec;
121 std::vector<wchar_t> buffer(
PATH_MAX);
122 DWORD chars_copied = 0;
124 chars_copied = ::GetModuleFileNameW(hmodule, &buffer[0], buffer.size());
125 if (chars_copied == buffer.size() &&
126 ::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
127 buffer.resize(buffer.size() * 2);
128 }
while (chars_copied >= buffer.size());
130 if (!llvm::convertWideToUTF8(buffer.data(), path))
131 return module_filespec;
132 module_filespec.
SetFile(path, FileSpec::Style::native);
133 return module_filespec;
138 process_infos.clear();
140 AutoHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
141 if (!snapshot.IsValid())
144 PROCESSENTRY32W pe = {};
145 pe.dwSize =
sizeof(PROCESSENTRY32W);
146 if (Process32FirstW(snapshot.get(), &pe)) {
148 AutoHandle handle(::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE,
154 llvm::convertWideToUTF8(pe.szExeFile, exeFile);
161 process_infos.push_back(process);
162 }
while (Process32NextW(snapshot.get(), &pe));
164 return process_infos.size();
168 process_info.
Clear();
171 ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid),
173 if (!handle.IsValid())
181 AutoHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
182 if (!snapshot.IsValid())
186 pe.dwSize =
sizeof(PROCESSENTRY32W);
187 if (Process32FirstW(snapshot.get(), &pe)) {
189 if (pe.th32ProcessID == pid) {
193 }
while (Process32NextW(snapshot.get(), &pe));
206 if (launch_info.
GetFlags().
Test(eLaunchFlagShellExpandArguments)) {
207 FileSpec expand_tool_spec = HostInfo::GetSupportExeDir();
208 if (!expand_tool_spec) {
209 error.SetErrorString(
"could not find support executable directory for "
210 "the lldb-argdumper tool");
215 error.SetErrorString(
"could not find the lldb-argdumper tool");
219 std::string quoted_cmd_string;
221 std::replace(quoted_cmd_string.begin(), quoted_cmd_string.end(),
'\\',
'/');
224 expand_command.
Printf(
"\"%s\" %s", expand_tool_spec.
GetPath().c_str(),
225 quoted_cmd_string.c_str());
229 std::string command = expand_command.
GetString().str();
232 &status,
nullptr, &output, std::chrono::seconds(10));
238 error.SetErrorStringWithFormat(
"lldb-argdumper exited with error %d",
245 error.SetErrorString(
"invalid JSON");
249 auto dict_sp = data_sp->GetAsDictionary();
251 error.SetErrorString(
"invalid JSON");
255 auto args_sp = dict_sp->GetObjectForDotSeparatedPath(
"arguments");
257 error.SetErrorString(
"invalid JSON");
261 auto args_array_sp = args_sp->GetAsArray();
262 if (!args_array_sp) {
263 error.SetErrorString(
"invalid JSON");
269 for (
size_t i = 0; i < args_array_sp->GetSize(); i++) {
270 auto item_sp = args_array_sp->GetItemAtIndex(i);
273 auto str_sp = item_sp->GetAsString();
289 LPWCH environment_block = ::GetEnvironmentStringsW();
290 while (*environment_block != L
'\0') {
291 std::string current_var;
292 auto current_var_size = wcslen(environment_block) + 1;
293 if (!llvm::convertWideToUTF8(environment_block, current_var)) {
294 environment_block += current_var_size;
297 if (current_var[0] !=
'=')
300 environment_block += current_var_size;
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 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
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
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)