#include "Backtrace.hpp" #include <Windows.h> #include <DbgHelp.h> #include "../filesystem/filesystem.hpp" #include "../filesystem/path.hpp" namespace BacktraceStatic { bool initialized_ = false; } void sprawl::logging::Backtrace::Init() { if(BacktraceStatic::initialized_) return; BacktraceStatic::initialized_ = true; HANDLE process = GetCurrentProcess(); char path[MAX_PATH]; char symbolPath[MAX_PATH]; char altPath[MAX_PATH]; GetEnvironmentVariableA("_NT_SYMBOL_PATH", symbolPath, sizeof(symbolPath)); GetEnvironmentVariableA("_NT_ALTERNATE_SYMBOL_PATH", altPath, sizeof(altPath)); SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES | SYMOPT_OMAP_FIND_NEAREST | SYMOPT_LOAD_ANYTHING); _snprintf(path, sizeof(path), "%s;%s;%s", sprawl::filesystem::GetCwd().c_str(), symbolPath, altPath); SymInitialize( process, path, TRUE ); } void sprawl::logging::Backtrace::ShutDown() { HANDLE process = GetCurrentProcess(); SymCleanup(process); BacktraceStatic::initialized_ = false; } sprawl::logging::Backtrace sprawl::logging::Backtrace::Get(int skipFrames) { Backtrace backtrace; void** stack = reinterpret_cast<void**>(backtrace.m_stack); backtrace.m_size = CaptureStackBackTrace(skipFrames + 1, SPRAWL_BACKTRACE_MAX_STACK, stack, nullptr); return std::move(backtrace); } sprawl::logging::Backtrace::Frame sprawl::logging::Backtrace::GetFrame(size_t index) const { Frame frame; uintptr_t addr = m_stack[index]; HANDLE process = GetCurrentProcess(); char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)]; PSYMBOL_INFO symbol = reinterpret_cast<PSYMBOL_INFO>(buffer); symbol->SizeOfStruct = sizeof(SYMBOL_INFO); symbol->MaxNameLen = MAX_SYM_NAME; if(SymFromAddr( process, addr, 0, symbol )) { strncpy(frame.func, symbol->Name, SPRAWL_BACKTRACE_STRING_MAX); frame.offset = (addr - symbol->Address); } else { strncpy(frame.func, "Unknown", SPRAWL_BACKTRACE_STRING_MAX); } IMAGEHLP_MODULE64 module; memset(&module, 0, sizeof(IMAGEHLP_MODULE64) ); module.SizeOfStruct = sizeof(IMAGEHLP_MODULE64); if(SymGetModuleInfo64(process, addr, &module)) { strncpy(frame.module, module.ModuleName, SPRAWL_BACKTRACE_STRING_MAX); } else { strncpy(frame.module, "Unknown", SPRAWL_BACKTRACE_STRING_MAX); } DWORD displacement; IMAGEHLP_LINE64 line; memset(&line, 0, sizeof(IMAGEHLP_LINE64)); line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); if (SymGetLineFromAddr64(process, addr, &displacement, &line)) { strncpy(frame.file, line.FileName, SPRAWL_BACKTRACE_STRING_MAX); char const* ptr = frame.file; char sep = sprawl::path::Separator(); char altSep = sprawl::path::AltSeparator(); while(*ptr != '\0') { if(*ptr == sep || *ptr == altSep) { frame.baseFile = ptr + 1; } ++ptr; } frame.line = line.LineNumber; } else { strncpy(frame.file, "Unknown", sizeof(frame.file)); frame.baseFile = frame.file; frame.line = -1; } return std::move(frame); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 16066 | ShadauxCat |
Fixed a bug with Backtrace::ShutDown() on Windows. After shutting down the backtrace system it could not be reinitialized. #review-16067 |
||
#1 | 16052 | ShadauxCat |
- Changed default block size for concurrent queue to a more reasonable value - Changed some memory orders to memory_order_seq_cst when they don't actually need to be that to get around a bug in visual studio 2013 - debug builds assert when memory_order_acq_rel is used for a compare_exchange_strong (this is a standard library bug and is fixed in VS2015) - Added Event API - events are an alternative to condition variables that do not require a mutex and are guaranteed not to miss any signals, even if the signal comes while the thread is not listening for it. Unlike condition variables, however, they do not support broadcasting (and in fact, in general, are not safe to use with multiple threads listening for the same event simultaneously - though notifying on the same event is fine) - Rewrote ThreadManager around ConcurrentQueue and Event API so it is now lock-free. Also improved some behaviors of the staged thread manager operation so it now supports tasks that can be run on multiple stages via a bitmask. - Fixed an issue where the Coroutine copy constructor was calling the std::function constructor instead and another where initializing with a stack might try to call the wrong constructor and vice-versa - Fixed Coroutine never calling munmap() on its stack in linux and causing a memory leak - Added default arguments to time functions - Attempted to fix some issues with BinaryTree. Fixed some but not all. It's currently not suitable for use, sadly. - Logging Improvements: - - Added thread ID to logging - - Fixed some issues with category handlers - - Added backtraces - - Added the following additional log macros: - - - LOG_IF - - - LOG_EVERY_N - - - LOG_FIRST_N - - - LOG_IF_EVERY_N - - - LOG_IF_FIRST_N - - - LOG_ASSERT - - Added the ability to set extra info callbacks to get data such as script backtraces - - Removed the thread-related handlers and replaced them with RunHandler_Threaded and RunHandler_ThreadManager, which will enable any passed-in handler to be run in a threaded fashion - Removed StaticPoolAllocator and renamed DynamicPoolAllocator to PoolAllocator; adjusted unit tests accordingly - PoolAllocator now allocates its pool with mmap and VirtualAlloc, rather than with malloc - Fixed a bug with Vector copy assignment operator - Improved performance of StringBuilder considerably for cases where there are no modifier strings - Removed Copy-On-Write behavior of JSONToken as it was broken; copies are now performed with explicit DeepCopy() and ShallowCopy() functions - Fixed some parser bugs with JSONToken - Added iteration to JSONToken to iterate its children - Fixed crash when reading a negative number of bytes from a file - Changed StringBuilder to favor speed instead of memory by default - Added some performance unit tests for JSON token #review-16053 |