1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
//===-- source/Host/windows/Host.cpp ----------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// C Includes
#include <stdio.h>
#include "lldb/Host/windows/windows.h"
#include "lldb/Host/windows/AutoHandle.h"
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Core/Error.h"
#include "lldb/Core/Log.h"
#include "lldb/Target/Process.h"
#include "lldb/Host/Host.h"
#include "lldb/Core/DataBufferHeap.h"
#include "lldb/Core/DataExtractor.h"
#include "lldb/Core/StreamFile.h"
// Windows includes
#include <TlHelp32.h>
using namespace lldb;
using namespace lldb_private;
namespace
{
bool GetTripleForProcess(const FileSpec &executable, llvm::Triple &triple)
{
// Open the PE File as a binary file, and parse just enough information to determine the
// machine type.
File imageBinary(
executable.GetPath().c_str(),
File::eOpenOptionRead,
lldb::eFilePermissionsUserRead);
imageBinary.SeekFromStart(0x3c);
int32_t peOffset = 0;
uint32_t peHead = 0;
uint16_t machineType = 0;
size_t readSize = sizeof(peOffset);
imageBinary.Read(&peOffset, readSize);
imageBinary.SeekFromStart(peOffset);
imageBinary.Read(&peHead, readSize);
if (peHead != 0x00004550) // "PE\0\0", little-endian
return false; // Error: Can't find PE header
readSize = 2;
imageBinary.Read(&machineType, readSize);
triple.setVendor(llvm::Triple::PC);
triple.setOS(llvm::Triple::Win32);
triple.setArch(llvm::Triple::UnknownArch);
if (machineType == 0x8664)
triple.setArch(llvm::Triple::x86_64);
else if (machineType == 0x14c)
triple.setArch(llvm::Triple::x86);
return true;
}
bool GetExecutableForProcess(const AutoHandle &handle, std::string &path)
{
// Get the process image path. MAX_PATH isn't long enough, paths can actually be up to 32KB.
std::vector<char> buffer(32768);
DWORD dwSize = buffer.size();
if (!::QueryFullProcessImageNameA(handle.get(), 0, &buffer[0], &dwSize))
return false;
path.assign(&buffer[0]);
return true;
}
void GetProcessExecutableAndTriple(const AutoHandle &handle, ProcessInstanceInfo &process)
{
// We may not have permissions to read the path from the process. So start off by
// setting the executable file to whatever Toolhelp32 gives us, and then try to
// enhance this with more detailed information, but fail gracefully.
std::string executable;
llvm::Triple triple;
triple.setVendor(llvm::Triple::PC);
triple.setOS(llvm::Triple::Win32);
triple.setArch(llvm::Triple::UnknownArch);
if (GetExecutableForProcess(handle, executable))
{
FileSpec executableFile(executable.c_str(), false);
process.SetExecutableFile(executableFile, true);
GetTripleForProcess(executableFile, triple);
}
process.SetArchitecture(ArchSpec(triple));
// TODO(zturner): Add the ability to get the process user name.
}
}
bool
Host::GetOSVersion(uint32_t &major,
uint32_t &minor,
uint32_t &update)
{
OSVERSIONINFOEX info;
ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
#pragma warning(push)
#pragma warning(disable: 4996)
// Starting with Microsoft SDK for Windows 8.1, this function is deprecated in favor of the
// new Windows Version Helper APIs. Since we don't specify a minimum SDK version, it's easier
// to simply disable the warning rather than try to support both APIs.
if (GetVersionEx((LPOSVERSIONINFO) &info) == 0) {
return false;
}
#pragma warning(pop)
major = (uint32_t) info.dwMajorVersion;
minor = (uint32_t) info.dwMinorVersion;
update = (uint32_t) info.wServicePackMajor;
return true;
}
Error
Host::LaunchProcess (ProcessLaunchInfo &launch_info)
{
Error error;
assert(!"Not implemented yet!!!");
return error;
}
lldb::DataBufferSP
Host::GetAuxvData(lldb_private::Process *process)
{
return 0;
}
std::string
Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
{
return std::string();
}
lldb::tid_t
Host::GetCurrentThreadID()
{
return lldb::tid_t(::GetCurrentThreadId());
}
lldb::thread_t
Host::GetCurrentThread ()
{
return lldb::thread_t(::GetCurrentThread());
}
bool
Host::ThreadCancel (lldb::thread_t thread, Error *error)
{
int err = ::TerminateThread((HANDLE)thread, 0);
return err == 0;
}
bool
Host::ThreadDetach (lldb::thread_t thread, Error *error)
{
return ThreadCancel(thread, error);
}
bool
Host::ThreadJoin (lldb::thread_t thread, thread_result_t *thread_result_ptr, Error *error)
{
WaitForSingleObject((HANDLE) thread, INFINITE);
return true;
}
lldb::thread_key_t
Host::ThreadLocalStorageCreate(ThreadLocalStorageCleanupCallback callback)
{
return TlsAlloc();
}
void*
Host::ThreadLocalStorageGet(lldb::thread_key_t key)
{
return ::TlsGetValue (key);
}
void
Host::ThreadLocalStorageSet(lldb::thread_key_t key, void *value)
{
::TlsSetValue (key, value);
}
bool
Host::SetThreadName (lldb::pid_t pid, lldb::tid_t tid, const char *name)
{
return false;
}
bool
Host::SetShortThreadName (lldb::pid_t pid, lldb::tid_t tid,
const char *thread_name, size_t len)
{
return false;
}
void
Host::Kill(lldb::pid_t pid, int signo)
{
TerminateProcess((HANDLE) pid, 1);
}
uint32_t
Host::GetNumberCPUS()
{
static uint32_t g_num_cores = UINT32_MAX;
if (g_num_cores == UINT32_MAX)
{
SYSTEM_INFO system_info;
::GetSystemInfo(&system_info);
g_num_cores = system_info.dwNumberOfProcessors;
}
return g_num_cores;
}
size_t
Host::GetPageSize()
{
static long g_pagesize = 0;
if (!g_pagesize)
{
SYSTEM_INFO systemInfo;
GetNativeSystemInfo(&systemInfo);
g_pagesize = systemInfo.dwPageSize;
}
return g_pagesize;
}
const char *
Host::GetSignalAsCString(int signo)
{
return NULL;
}
FileSpec
Host::GetModuleFileSpecForHostAddress (const void *host_addr)
{
FileSpec module_filespec;
HMODULE hmodule = NULL;
if (!::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)host_addr, &hmodule))
return module_filespec;
std::vector<char> buffer(MAX_PATH);
DWORD chars_copied = 0;
do {
chars_copied = ::GetModuleFileName(hmodule, &buffer[0], buffer.size());
if (chars_copied == buffer.size() && ::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
buffer.resize(buffer.size() * 2);
} while (chars_copied >= buffer.size());
module_filespec.SetFile(&buffer[0], false);
return module_filespec;
}
void *
Host::DynamicLibraryOpen(const FileSpec &file_spec, uint32_t options, Error &error)
{
error.SetErrorString("not implemented");
return NULL;
}
Error
Host::DynamicLibraryClose (void *opaque)
{
Error error;
error.SetErrorString("not implemented");
return error;
}
void *
Host::DynamicLibraryGetSymbol(void *opaque, const char *symbol_name, Error &error)
{
error.SetErrorString("not implemented");
return NULL;
}
const char *
Host::GetUserName (uint32_t uid, std::string &user_name)
{
return NULL;
}
const char *
Host::GetGroupName (uint32_t gid, std::string &group_name)
{
llvm_unreachable("Windows does not support group name");
return NULL;
}
uint32_t
Host::GetUserID ()
{
llvm_unreachable("Windows does not support uid");
}
uint32_t
Host::GetGroupID ()
{
llvm_unreachable("Windows does not support gid");
return 0;
}
uint32_t
Host::GetEffectiveUserID ()
{
llvm_unreachable("Windows does not support euid");
return 0;
}
uint32_t
Host::GetEffectiveGroupID ()
{
llvm_unreachable("Windows does not support egid");
return 0;
}
uint32_t
Host::FindProcesses (const ProcessInstanceInfoMatch &match_info, ProcessInstanceInfoList &process_infos)
{
process_infos.Clear();
AutoHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
if (!snapshot.IsValid())
return 0;
PROCESSENTRY32 pe = {0};
pe.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(snapshot.get(), &pe))
{
do
{
AutoHandle handle(::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pe.th32ProcessID), nullptr);
ProcessInstanceInfo process;
process.SetExecutableFile(FileSpec(pe.szExeFile, false), true);
process.SetProcessID(pe.th32ProcessID);
process.SetParentProcessID(pe.th32ParentProcessID);
GetProcessExecutableAndTriple(handle, process);
if (match_info.MatchAllProcesses() || match_info.Matches(process))
process_infos.Append(process);
} while (Process32Next(snapshot.get(), &pe));
}
return process_infos.GetSize();
}
bool
Host::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
{
process_info.Clear();
AutoHandle handle(::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid),
nullptr);
if (!handle.IsValid())
return false;
process_info.SetProcessID(pid);
GetProcessExecutableAndTriple(handle, process_info);
// Need to read the PEB to get parent process and command line arguments.
return true;
}
lldb::thread_t
Host::StartMonitoringChildProcess
(
Host::MonitorChildProcessCallback callback,
void *callback_baton,
lldb::pid_t pid,
bool monitor_signals
)
{
return LLDB_INVALID_HOST_THREAD;
}
|