// (and hopefully tells someone about it).
//
// This also provides another really hacky method to prevent assert dialog boxes
-// from poping up. When --no-user32 is passed, if any process loads user32.dll,
+// from popping up. When --no-user32 is passed, if any process loads user32.dll,
// we assume it is trying to call MessageBoxEx and terminate it. The proper way
// to do this would be to actually set a break point, but there's quite a bit
// of code involved to get the address of MessageBoxEx in the remote process's
// address space due to Address space layout randomization (ASLR). This can be
// added if it's ever actually needed.
//
-// If the subprocess exits for any reason other than sucessful termination, -1
+// If the subprocess exits for any reason other than successful termination, -1
// is returned. If the process exits normally the value it returned is returned.
//
// I hate Windows.
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/PrettyStackTrace.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/WindowsError.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/type_traits.h"
-#include "llvm/System/Signals.h"
-#include "llvm/System/system_error.h"
#include <algorithm>
#include <cerrno>
#include <cstdlib>
#include <map>
#include <string>
+#include <system_error>
+
+// These includes must be last.
#include <Windows.h>
#include <WinError.h>
#include <Dbghelp.h>
#include <psapi.h>
+
using namespace llvm;
#undef max
typedef ScopedHandle<ThreadHandle> ThreadScopedHandle;
typedef ScopedHandle<TokenHandle> TokenScopedHandle;
typedef ScopedHandle<FileHandle> FileScopedHandle;
-
- error_code get_windows_last_error() {
- return make_error_code(windows_error(::GetLastError()));
- }
}
-static error_code GetFileNameFromHandle(HANDLE FileHandle,
- std::string& Name) {
+static std::error_code windows_error(DWORD E) { return mapWindowsError(E); }
+
+static std::error_code GetFileNameFromHandle(HANDLE FileHandle,
+ std::string &Name) {
char Filename[MAX_PATH+1];
- bool Sucess = false;
+ bool Success = false;
Name.clear();
// Get the file size.
LARGE_INTEGER FileSize;
- Sucess = ::GetFileSizeEx(FileHandle, &FileSize);
+ Success = ::GetFileSizeEx(FileHandle, &FileSize);
- if (!Sucess)
- return get_windows_last_error();
+ if (!Success)
+ return windows_error(::GetLastError());
// Create a file mapping object.
FileMappingScopedHandle FileMapping(
NULL));
if (!FileMapping)
- return get_windows_last_error();
+ return windows_error(::GetLastError());
// Create a file mapping to get the file name.
MappedViewOfFileScopedHandle MappedFile(
::MapViewOfFile(FileMapping, FILE_MAP_READ, 0, 0, 1));
if (!MappedFile)
- return get_windows_last_error();
+ return windows_error(::GetLastError());
- Sucess = ::GetMappedFileNameA(::GetCurrentProcess(),
+ Success = ::GetMappedFileNameA(::GetCurrentProcess(),
MappedFile,
Filename,
array_lengthof(Filename) - 1);
- if (!Sucess)
- return get_windows_last_error();
+ if (!Success)
+ return windows_error(::GetLastError());
else {
Name = Filename;
- return windows_error::success;
- }
-}
-
-static std::string QuoteProgramPathIfNeeded(StringRef Command) {
- if (Command.find_first_of(' ') == StringRef::npos)
- return Command;
- else {
- std::string ret;
- ret.reserve(Command.size() + 3);
- ret.push_back('"');
- ret.append(Command.begin(), Command.end());
- ret.push_back('"');
- return ret;
+ return std::error_code();
}
}
/// extension is present, try all extensions in PATHEXT.
/// @return If ec == errc::success, The absolute path to the program. Otherwise
/// the return value is undefined.
-static std::string FindProgram(const std::string &Program, error_code &ec) {
+static std::string FindProgram(const std::string &Program,
+ std::error_code &ec) {
char PathName[MAX_PATH + 1];
typedef SmallVector<StringRef, 12> pathext_t;
pathext_t pathext;
PathName,
NULL);
if (length == 0)
- ec = get_windows_last_error();
+ ec = windows_error(::GetLastError());
else if (length > array_lengthof(PathName)) {
// This may have been the file, return with error.
- ec = error_code(windows_error::buffer_overflow);
+ ec = windows_error(ERROR_BUFFER_OVERFLOW);
break;
} else {
// We found the path! Return it.
- ec = windows_error::success;
+ ec = std::error_code();
break;
}
}
return PathName;
}
-static error_code EnableDebugPrivileges() {
- HANDLE TokenHandle;
- BOOL success = ::OpenProcessToken(::GetCurrentProcess(),
- TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
- &TokenHandle);
- if (!success)
- return error_code(::GetLastError(), system_category());
-
- TokenScopedHandle Token(TokenHandle);
- TOKEN_PRIVILEGES TokenPrivileges;
- LUID LocallyUniqueID;
-
- success = ::LookupPrivilegeValueA(NULL,
- SE_DEBUG_NAME,
- &LocallyUniqueID);
- if (!success)
- return error_code(::GetLastError(), system_category());
-
- TokenPrivileges.PrivilegeCount = 1;
- TokenPrivileges.Privileges[0].Luid = LocallyUniqueID;
- TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
-
- success = ::AdjustTokenPrivileges(Token,
- FALSE,
- &TokenPrivileges,
- sizeof(TOKEN_PRIVILEGES),
- NULL,
- NULL);
- // The value of success is basically useless. Either way we are just returning
- // the value of ::GetLastError().
- return error_code(::GetLastError(), system_category());
-}
-
static StringRef ExceptionCodeToString(DWORD ExceptionCode) {
switch(ExceptionCode) {
case EXCEPTION_ACCESS_VIOLATION: return "EXCEPTION_ACCESS_VIOLATION";
std::string CommandLine(ProgramToRun);
- error_code ec;
+ std::error_code ec;
ProgramToRun = FindProgram(ProgramToRun, ec);
if (ec) {
errs() << ToolName << ": Failed to find program: '" << CommandLine
StartupInfo.cb = sizeof(StartupInfo);
std::memset(&ProcessInfo, 0, sizeof(ProcessInfo));
- // Set error mode to not display any message boxes. The child process inherets
+ // Set error mode to not display any message boxes. The child process inherits
// this.
::SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
::_set_error_mode(_OUT_TO_STDERR);
&StartupInfo,
&ProcessInfo);
if (!success) {
- errs() << ToolName << ": Failed to run program: '" << ProgramToRun
- << "': " << error_code(::GetLastError(), system_category()).message()
+ errs() << ToolName << ": Failed to run program: '" << ProgramToRun << "': "
+ << std::error_code(windows_error(::GetLastError())).message()
<< '\n';
return -1;
}
&KernelTime,
&UserTime);
if (!success) {
- ec = error_code(::GetLastError(), system_category());
+ ec = windows_error(::GetLastError());
errs() << ToolName << ": Failed to get process times: "
<< ec.message() << '\n';
a.HighPart = KernelTime.dwHighDateTime;
b.LowPart = UserTime.dwLowDateTime;
b.HighPart = UserTime.dwHighDateTime;
- // Convert 100-nanosecond units to miliseconds.
+ // Convert 100-nanosecond units to milliseconds.
uint64_t TotalTimeMiliseconds = (a.QuadPart + b.QuadPart) / 10000;
// Handle the case where the process has been running for more than 49
// days.
success = WaitForDebugEvent(&DebugEvent, TimeLeft);
if (!success) {
- ec = error_code(::GetLastError(), system_category());
+ DWORD LastError = ::GetLastError();
+ ec = windows_error(LastError);
- if (ec == error_condition(errc::timed_out)) {
+ if (LastError == ERROR_SEM_TIMEOUT || LastError == WSAETIMEDOUT) {
errs() << ToolName << ": Process timed out.\n";
::TerminateProcess(ProcessInfo.hProcess, -1);
// Otherwise other stuff starts failing...
if (TraceExecution)
errs() << ToolName << ": Debug Event: EXIT_PROCESS_DEBUG_EVENT\n";
- // If this is the process we origionally created, exit with its exit
+ // If this is the process we originally created, exit with its exit
// code.
if (DebugEvent.dwProcessId == ProcessInfo.dwProcessId)
return DebugEvent.u.ExitProcess.dwExitCode;
errs().indent(ToolName.size()) << ": DLL Name : " << DLLName << '\n';
}
- if (NoUser32 && sys::Path(DLLName).getBasename() == "user32") {
+ if (NoUser32 && sys::path::stem(DLLName) == "user32") {
// Program is loading user32.dll, in the applications we are testing,
// this only happens if an assert has fired. By now the message has
// already been printed, so simply close the program.
errs() << ToolName << ": user32.dll loaded!\n";
errs().indent(ToolName.size())
<< ": This probably means that assert was called. Closing "
- "program to prevent message box from poping up.\n";
+ "program to prevent message box from popping up.\n";
dwContinueStatus = DBG_CONTINUE;
::TerminateProcess(ProcessIDToHandle[DebugEvent.dwProcessId], -1);
return -1;
DebugEvent.dwThreadId,
dwContinueStatus);
if (!success) {
- ec = error_code(::GetLastError(), system_category());
+ ec = windows_error(::GetLastError());
errs() << ToolName << ": Failed to continue debugging program: '"
<< ProgramToRun << "': " << ec.message() << '\n';
return -1;