From d56ae60460444b37d1cb3921d7c9fdbdc1bfc91d Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Tue, 29 Jul 2014 20:29:50 -0700 Subject: [PATCH] More mac fixups. --- src/alloy/backend/x64/x64_assembler.cc | 2 +- src/alloy/backend/x64/x64_code_cache_posix.cc | 4 +- src/alloy/backend/x64/x64_emitter.cc | 2 +- src/alloy/frontend/ppc/ppc_hir_builder.cc | 4 +- src/alloy/frontend/ppc/ppc_translator.cc | 5 +- src/alloy/runtime/entry_table.cc | 5 +- src/alloy/runtime/entry_table.h | 1 + src/alloy/runtime/function.cc | 5 +- src/alloy/runtime/thread_state.h | 5 +- src/alloy/string_buffer.cc | 2 +- src/alloy/string_buffer.h | 1 + src/poly/cxx_compat.h | 12 ++ src/poly/debugging.h | 29 +++++ src/poly/debugging_mac.cc | 29 +++++ src/poly/debugging_win.cc | 20 ++++ src/poly/platform.h | 105 +++++++++--------- src/poly/sources.gypi | 3 + tools/alloy-sandbox/alloy-sandbox.cc | 1 + 18 files changed, 165 insertions(+), 70 deletions(-) create mode 100644 src/poly/debugging.h create mode 100644 src/poly/debugging_mac.cc create mode 100644 src/poly/debugging_win.cc diff --git a/src/alloy/backend/x64/x64_assembler.cc b/src/alloy/backend/x64/x64_assembler.cc index be3777b1c..1fd0e2223 100644 --- a/src/alloy/backend/x64/x64_assembler.cc +++ b/src/alloy/backend/x64/x64_assembler.cc @@ -19,7 +19,7 @@ namespace BE { #include -} +} // namespace BE namespace alloy { namespace backend { diff --git a/src/alloy/backend/x64/x64_code_cache_posix.cc b/src/alloy/backend/x64/x64_code_cache_posix.cc index 09dbeccb5..7570e119d 100644 --- a/src/alloy/backend/x64/x64_code_cache_posix.cc +++ b/src/alloy/backend/x64/x64_code_cache_posix.cc @@ -9,9 +9,9 @@ #include -#include +#include -#include +#include namespace alloy { namespace backend { diff --git a/src/alloy/backend/x64/x64_emitter.cc b/src/alloy/backend/x64/x64_emitter.cc index bb409622c..a30ccf053 100644 --- a/src/alloy/backend/x64/x64_emitter.cc +++ b/src/alloy/backend/x64/x64_emitter.cc @@ -370,7 +370,7 @@ void X64Emitter::CallIndirect(const hir::Instr* instr, const Reg64& reg) { uint64_t UndefinedCallExtern(void* raw_context, uint64_t symbol_info_ptr) { auto symbol_info = reinterpret_cast(symbol_info_ptr); XELOGW("undefined extern call to %.8llX %s", symbol_info->address(), - symbol_info->name()); + symbol_info->name().c_str()); return 0; } void X64Emitter::CallExtern(const hir::Instr* instr, diff --git a/src/alloy/frontend/ppc/ppc_hir_builder.cc b/src/alloy/frontend/ppc/ppc_hir_builder.cc index 8f0977992..da30e8971 100644 --- a/src/alloy/frontend/ppc/ppc_hir_builder.cc +++ b/src/alloy/frontend/ppc/ppc_hir_builder.cc @@ -55,9 +55,9 @@ int PPCHIRBuilder::Emit(FunctionInfo* symbol_info, bool with_debug_info) { with_debug_info_ = with_debug_info; if (with_debug_info_) { - Comment("%s fn %.8X-%.8X %s", symbol_info->module()->name(), + Comment("%s fn %.8X-%.8X %s", symbol_info->module()->name().c_str(), symbol_info->address(), symbol_info->end_address(), - symbol_info->name()); + symbol_info->name().c_str()); } // Allocate offset list. diff --git a/src/alloy/frontend/ppc/ppc_translator.cc b/src/alloy/frontend/ppc/ppc_translator.cc index ab7404b61..ff6145256 100644 --- a/src/alloy/frontend/ppc/ppc_translator.cc +++ b/src/alloy/frontend/ppc/ppc_translator.cc @@ -161,9 +161,10 @@ void PPCTranslator::DumpSource(runtime::FunctionInfo* symbol_info, Memory* memory = frontend_->memory(); const uint8_t* p = memory->membase(); - string_buffer->Append("%s fn %.8X-%.8X %s\n", symbol_info->module()->name(), + string_buffer->Append("%s fn %.8X-%.8X %s\n", + symbol_info->module()->name().c_str(), symbol_info->address(), symbol_info->end_address(), - symbol_info->name()); + symbol_info->name().c_str()); auto blocks = scanner_->FindBlocks(symbol_info); diff --git a/src/alloy/runtime/entry_table.cc b/src/alloy/runtime/entry_table.cc index 043623f4d..17c737bb9 100644 --- a/src/alloy/runtime/entry_table.cc +++ b/src/alloy/runtime/entry_table.cc @@ -18,9 +18,8 @@ EntryTable::EntryTable() = default; EntryTable::~EntryTable() { std::lock_guard guard(lock_); - auto& it = map_.begin(); - for (; it != map_.end(); ++it) { - Entry* entry = it->second; + for (auto it : map_) { + Entry* entry = it.second; delete entry; } } diff --git a/src/alloy/runtime/entry_table.h b/src/alloy/runtime/entry_table.h index d2a0fa225..1fe466a8a 100644 --- a/src/alloy/runtime/entry_table.h +++ b/src/alloy/runtime/entry_table.h @@ -12,6 +12,7 @@ #include #include +#include #include diff --git a/src/alloy/runtime/function.cc b/src/alloy/runtime/function.cc index 4be19f45e..06c0a68c2 100644 --- a/src/alloy/runtime/function.cc +++ b/src/alloy/runtime/function.cc @@ -17,8 +17,7 @@ namespace alloy { namespace runtime { Function::Function(FunctionInfo* symbol_info) - : address_(symbol_info->address()), - symbol_info_(symbol_info) {} + : address_(symbol_info->address()), symbol_info_(symbol_info) {} Function::~Function() = default; @@ -81,7 +80,7 @@ int Function::Call(ThreadState* thread_state, uint64_t return_address) { symbol_info_->extern_arg1()); } else { XELOGW("undefined extern call to %.8llX %s", symbol_info_->address(), - symbol_info_->name()); + symbol_info_->name().c_str()); result = 1; } } else { diff --git a/src/alloy/runtime/thread_state.h b/src/alloy/runtime/thread_state.h index be6655438..9cb2f7e96 100644 --- a/src/alloy/runtime/thread_state.h +++ b/src/alloy/runtime/thread_state.h @@ -10,6 +10,8 @@ #ifndef ALLOY_RUNTIME_THREAD_STATE_H_ #define ALLOY_RUNTIME_THREAD_STATE_H_ +#include + #include #include @@ -32,7 +34,8 @@ class ThreadState { void* backend_data() const { return backend_data_; } void* raw_context() const { return raw_context_; } - virtual int Suspend(uint32_t timeout_ms = UINT_MAX) { return 1; } + int Suspend() { return Suspend(~0); } + virtual int Suspend(uint32_t timeout_ms) { return 1; } virtual int Resume(bool force = false) { return 1; } static void Bind(ThreadState* thread_state); diff --git a/src/alloy/string_buffer.cc b/src/alloy/string_buffer.cc index ca667f997..c35885e95 100644 --- a/src/alloy/string_buffer.cc +++ b/src/alloy/string_buffer.cc @@ -23,7 +23,7 @@ void StringBuffer::Reset() { } void StringBuffer::Append(const std::string& value) { - Append(value.c_str()); + AppendBytes(reinterpret_cast(value.data()), value.size()); } void StringBuffer::Append(const char* format, ...) { diff --git a/src/alloy/string_buffer.h b/src/alloy/string_buffer.h index ca553d343..16f6bab76 100644 --- a/src/alloy/string_buffer.h +++ b/src/alloy/string_buffer.h @@ -10,6 +10,7 @@ #ifndef ALLOY_STRING_BUFFER_H_ #define ALLOY_STRING_BUFFER_H_ +#include #include #include diff --git a/src/poly/cxx_compat.h b/src/poly/cxx_compat.h index ba68ca0a8..fa471eca0 100644 --- a/src/poly/cxx_compat.h +++ b/src/poly/cxx_compat.h @@ -10,6 +10,8 @@ #ifndef POLY_CXX_COMPAT_H_ #define POLY_CXX_COMPAT_H_ +#include + #include // C++11 thread local storage. @@ -30,6 +32,16 @@ #define alignas(N) __declspec(align(N)) #endif // XE_COMPILER_MSVC +// C++1y make_unique. +// http://herbsutter.com/2013/05/29/gotw-89-solution-smart-pointers/ +// This is present in clang with -std=c++1y, but not otherwise. +namespace std { +template +unique_ptr make_unique(Args&&... args) { + return unique_ptr(new T(forward(args)...)); +} +} // namespace std + namespace poly {} // namespace poly #endif // POLY_CXX_COMPAT_H_ diff --git a/src/poly/debugging.h b/src/poly/debugging.h new file mode 100644 index 000000000..f784e4c29 --- /dev/null +++ b/src/poly/debugging.h @@ -0,0 +1,29 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2014 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#ifndef POLY_DEBUGGING_H_ +#define POLY_DEBUGGING_H_ + +#include + +#include + +namespace poly { +namespace debugging { + +// Returns true if a debugger is attached to this process. +// The state may change at any time (attach after launch, etc), so do not +// cache this value. Determining if the debugger is attached is expensive, +// though, so avoid calling it frequently. +bool IsDebuggerAttached(); + +} // namespace debugging +} // namespace poly + +#endif // POLY_DEBUGGING_H_ diff --git a/src/poly/debugging_mac.cc b/src/poly/debugging_mac.cc new file mode 100644 index 000000000..ec060db86 --- /dev/null +++ b/src/poly/debugging_mac.cc @@ -0,0 +1,29 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2014 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#include + +#include +#include + +namespace poly { +namespace debugging { + +bool IsDebuggerAttached() { + // https://developer.apple.com/library/mac/qa/qa1361/_index.html + kinfo_proc info; + info.kp_proc.p_flag = 0; + int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()}; + size_t size = sizeof(info); + sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, nullptr, 0); + return (info.kp_proc.p_flag & P_TRACED) != 0; +} + +} // namespace debugging +} // namespace poly diff --git a/src/poly/debugging_win.cc b/src/poly/debugging_win.cc new file mode 100644 index 000000000..0c6bad97f --- /dev/null +++ b/src/poly/debugging_win.cc @@ -0,0 +1,20 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2014 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#include + +namespace poly { +namespace debugging { + +bool IsDebuggerAttached() { + return IsDebuggerPresent() ? true : false; +} + +} // namespace debugging +} // namespace poly diff --git a/src/poly/platform.h b/src/poly/platform.h index 10efb54c3..b0463c0bc 100644 --- a/src/poly/platform.h +++ b/src/poly/platform.h @@ -27,109 +27,106 @@ XE_CPU: 32BIT | 64BIT | BIGENDIAN | LITTLEENDIAN #include #endif -#if (defined(TARGET_OS_EMBEDDED) && TARGET_OS_EMBEDDED ) || \ - (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE ) || \ +#if (defined(TARGET_OS_EMBEDDED) && TARGET_OS_EMBEDDED) || \ + (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) || \ (defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR) -#define XE_PLATFORM_IOS 1 -#define XE_LIKE_OSX 1 -#define XE_PROFILE_EMBEDDED 1 +#define XE_PLATFORM_IOS 1 +#define XE_LIKE_OSX 1 +#define XE_PROFILE_EMBEDDED 1 #if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR // EMBEDDED *and* SIMULATOR -#define XE_PROFILE_SIMULATOR 1 +#define XE_PROFILE_SIMULATOR 1 #endif #elif defined(TARGET_OS_MAC) && TARGET_OS_MAC -#define XE_PLATFORM_OSX 1 -#define XE_LIKE_OSX 1 -#define XE_PROFILE_DESKTOP 1 +#define XE_PLATFORM_OSX 1 +#define XE_LIKE_OSX 1 +#define XE_PROFILE_DESKTOP 1 #elif defined(_XBOX) -#define XE_PLATFORM_XBOX360 1 -#define XE_LIKE_WIN32 1 -#define XE_PROFILE_EMBEDDED 1 +#define XE_PLATFORM_XBOX360 1 +#define XE_LIKE_WIN32 1 +#define XE_PROFILE_EMBEDDED 1 #elif defined(_WIN32_WCE) -#define XE_PLATFORM_WINCE 1 -#define XE_LIKE_WIN32 1 -#define XE_PROFILE_EMBEDDED 1 +#define XE_PLATFORM_WINCE 1 +#define XE_LIKE_WIN32 1 +#define XE_PROFILE_EMBEDDED 1 #elif defined(__CYGWIN__) -#define XE_PLATFORM_CYGWIN 1 -#define XE_LIKE_POSIX 1 -#define XE_PROFILE_DESKTOP 1 +#define XE_PLATFORM_CYGWIN 1 +#define XE_LIKE_POSIX 1 +#define XE_PROFILE_DESKTOP 1 #elif defined(WIN32) || defined(_WIN32) -#define XE_PLATFORM_WIN32 1 -#define XE_LIKE_WIN32 1 -#define XE_PROFILE_DESKTOP 1 +#define XE_PLATFORM_WIN32 1 +#define XE_LIKE_WIN32 1 +#define XE_PROFILE_DESKTOP 1 #elif defined(ANDROID) -#define XE_PLATFORM_ANDROID 1 -#define XE_LIKE_POSIX 1 -#define XE_PROFILE_EMBEDDED 1 +#define XE_PLATFORM_ANDROID 1 +#define XE_LIKE_POSIX 1 +#define XE_PROFILE_EMBEDDED 1 #elif defined(__native_client__) -#define XE_PLATFORM_NACL 1 -#define XE_LIKE_POSIX 1 -#define XE_PROFILE_DESKTOP 1 +#define XE_PLATFORM_NACL 1 +#define XE_LIKE_POSIX 1 +#define XE_PROFILE_DESKTOP 1 #else -#define XE_PLATFORM_UNIX 1 -#define XE_LIKE_POSIX 1 -#define XE_PROFILE_DESKTOP 1 +#define XE_PLATFORM_UNIX 1 +#define XE_LIKE_POSIX 1 +#define XE_PROFILE_DESKTOP 1 #endif #if defined(__clang__) -#define XE_COMPILER_CLANG 1 +#define XE_COMPILER_CLANG 1 #elif defined(__GNUC__) -#define XE_COMPILER_GNUC 1 +#define XE_COMPILER_GNUC 1 #elif defined(_MSC_VER) -#define XE_COMPILER_MSVC 1 +#define XE_COMPILER_MSVC 1 #elif defined(__MINGW32) -#define XE_COMPILER_MINGW32 1 +#define XE_COMPILER_MINGW32 1 #elif defined(__INTEL_COMPILER) -#define XE_COMPILER_INTEL 1 +#define XE_COMPILER_INTEL 1 #else -#define XE_COMPILER_UNKNOWN 1 +#define XE_COMPILER_UNKNOWN 1 #endif -#if defined(__ia64__) || defined(_M_IA64) || \ - defined(__ppc64__) || defined(__PPC64__) || \ - defined(__arch64__) || \ - defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || \ - defined(__LP64__) || defined(__LLP64) || \ - defined(_WIN64) || \ - (__WORDSIZE == 64) -#define XE_CPU_64BIT 1 +#if defined(__ia64__) || defined(_M_IA64) || defined(__ppc64__) || \ + defined(__PPC64__) || defined(__arch64__) || defined(__x86_64__) || \ + defined(_M_X64) || defined(_M_AMD64) || defined(__LP64__) || \ + defined(__LLP64) || defined(_WIN64) || (__WORDSIZE == 64) +#define XE_CPU_64BIT 1 #else -#define XE_CPU_32BIT 1 +#define XE_CPU_32BIT 1 #endif // [64bit flags] -#if defined(__ppc__) || defined(__PPC__) || defined(__powerpc__) || defined(__powerpc) || defined(__POWERPC__) || defined(_M_PPC) || defined(__PPC) || \ - defined(__ppc64__) || defined(__PPC64__) || \ - defined(__ARMEB__) || defined(__THUMBEB__) || \ - defined(__AARCH64EB__) || \ +#if defined(__ppc__) || defined(__PPC__) || defined(__powerpc__) || \ + defined(__powerpc) || defined(__POWERPC__) || defined(_M_PPC) || \ + defined(__PPC) || defined(__ppc64__) || defined(__PPC64__) || \ + defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \ defined(__BIG_ENDIAN) || defined(__BIG_ENDIAN__) -#define XE_CPU_BIGENDIAN 1 +#define XE_CPU_BIGENDIAN 1 #else -#define XE_CPU_LITTLEENDIAN 1 +#define XE_CPU_LITTLEENDIAN 1 #endif // [big endian flags] #if XE_CPU_32BIT -#define XE_ALIGNMENT 8 +#define XE_ALIGNMENT 8 #else -#define XE_ALIGNMENT 16 +#define XE_ALIGNMENT 16 #endif // 32BIT #if XE_PLATFORM_WINCE || XE_PLATFORM_WIN32 @@ -168,7 +165,7 @@ const char path_separator = '\\'; const size_t max_path = _MAX_PATH; #else const char path_separator = '/'; -const size_t max_path = PATH_MAX; +const size_t max_path = 1024; // PATH_MAX #endif // WIN32 } // namespace poly diff --git a/src/poly/sources.gypi b/src/poly/sources.gypi index 49154f817..6765121fa 100644 --- a/src/poly/sources.gypi +++ b/src/poly/sources.gypi @@ -4,6 +4,7 @@ 'assert.h', 'atomic.h', 'byte_order.h', + 'debugging.h', 'config.h', 'cxx_compat.h', 'math.cc', @@ -28,11 +29,13 @@ }], ['OS == "mac"', { 'sources': [ + 'debugging_mac.cc', 'threading_mac.cc', ], }], ['OS == "win"', { 'sources': [ + 'debugging_win.cc', 'threading_win.cc', ], }], diff --git a/tools/alloy-sandbox/alloy-sandbox.cc b/tools/alloy-sandbox/alloy-sandbox.cc index 437204d2d..db1886d26 100644 --- a/tools/alloy-sandbox/alloy-sandbox.cc +++ b/tools/alloy-sandbox/alloy-sandbox.cc @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include