#include "Backtrace.hpp" #include "../filesystem/path.hpp" #include "../collections/Array.hpp" #include <execinfo.h> #include <unistd.h> #include <cxxabi.h> #include <bfd.h> #include <link.h> #include <algorithm> namespace BacktraceStatic { bool initialized_ = false; struct Symbol { bfd_vma pc; char const* filename; char const* functionName; bfd_vma offset; unsigned int line; }; static bool findSymbol_( bfd* bfdPtr, asymbol** syms, Symbol& symbol ) { for(asection* section = bfdPtr->sections; section != nullptr; section = section->next) { auto& flags = bfd_get_section_flags(bfdPtr, section); if( ( flags & SEC_ALLOC ) == 0 ) continue; bfd_vma vma = bfd_get_section_vma( bfdPtr, section ); if(symbol.pc < vma) continue; bfd_size_type size = bfd_get_section_size(section); if(symbol.pc >= vma + size) continue; bfd_vma offset = symbol.pc - vma; if(bfd_find_nearest_line( bfdPtr, section, syms, offset, &symbol.filename, &symbol.functionName, &symbol.line )) { symbol.offset = offset; return true; } } return false; } struct Module { uintptr_t baseAddress; char const* path; char const* baseFilename; bfd* bfdPtr; asymbol** symbols; Module() : baseAddress(0) , path(nullptr) , baseFilename(nullptr) , bfdPtr(nullptr) , symbols(nullptr) { } Module(Module&& other) : baseAddress(other.baseAddress) , path(other.path) , baseFilename(other.baseFilename) , bfdPtr(other.bfdPtr) , symbols(other.symbols) { other.baseAddress = 0; other.path = nullptr; other.baseFilename = nullptr; other.bfdPtr = nullptr; other.symbols = nullptr; } Module& operator=(Module&& other) { baseAddress = other.baseAddress; path = other.path; baseFilename = other.baseFilename; bfdPtr = other.bfdPtr; symbols = other.symbols; other.baseAddress = 0; other.path = nullptr; other.baseFilename = nullptr; other.bfdPtr = nullptr; other.symbols = nullptr; return *this; } Module(char const* path_, uintptr_t addr) : baseAddress(addr) , path(path_) , baseFilename(path_) , bfdPtr(nullptr) , symbols(nullptr) { bfdPtr = bfd_openr(path_, nullptr); if(!bfdPtr) return; if (bfd_check_format(bfdPtr, bfd_archive)) { bfd_close(bfdPtr); bfdPtr = nullptr; return; } char** matching = NULL; if (!bfd_check_format_matches(bfdPtr, bfd_object, &matching)) { bfd_close(bfdPtr); bfdPtr = nullptr; return; } if ((bfd_get_file_flags(bfdPtr) & HAS_SYMS) == 0) { bfd_close(bfdPtr); bfdPtr = nullptr; return; } unsigned int size; long symcount = bfd_read_minisymbols( bfdPtr, FALSE, (void**)&symbols, &size ); if(symcount == 0) { symcount = bfd_read_minisymbols( bfdPtr, TRUE, (void**)&symbols, &size ); } if(symcount < 0) { bfd_close(bfdPtr); bfdPtr = nullptr; symbols = nullptr; } char const* ptr = path; char const sep = sprawl::path::Separator(); char const altSep = sprawl::path::AltSeparator(); while(*ptr != '\0') { if(*ptr == sep || *ptr == altSep) { baseFilename = ptr + 1; } ++ptr; } } ~Module() { if(symbols) { free(symbols); } if(bfdPtr) { bfd_close(bfdPtr); } } bool Good() { return bfdPtr != nullptr && symbols != nullptr; } }; char moduleBuffer_[256 * sizeof(Module)]; Module* modules_ = reinterpret_cast<Module*>(moduleBuffer_); static size_t nModules_ = 0; int dlCallback_(struct dl_phdr_info* info, size_t /*size*/, void* /*data*/) { size_t idx = nModules_; Module& module = modules_[idx]; new(&module) Module(info->dlpi_name, info->dlpi_addr); if(module.Good()) { ++nModules_; } else { module.~Module(); } return 0; } static Module* findModule_(uintptr_t address, int start = 0, int end = nModules_- 1) { if(end < start) { return nullptr; } size_t mid = (start + end) / 2; Module& module = modules_[mid]; if( module.baseAddress < address ) { if(mid < nModules_ - 1 && reinterpret_cast<Module&>(modules_[mid+1]).baseAddress > address) return &module; return findModule_(address, mid+1, end); } return findModule_(address, start, mid-1); } static bool demangle_(char const* input, char* outputBuffer, size_t outputLength) { ///@TODO: Implement this directly so we can avoid heap allocations. int status; size_t length = outputLength; char* buffer = abi::__cxa_demangle(input, nullptr, &length, &status); if(buffer == nullptr) { return false; } strncpy(outputBuffer, buffer, outputLength); free(buffer); return true; } } void sprawl::logging::Backtrace::Init() { if(BacktraceStatic::initialized_) return; BacktraceStatic::initialized_ = true; bfd_init(); static char path[1024]; readlink("/proc/self/exe", path, 1024); size_t idx = BacktraceStatic::nModules_; BacktraceStatic::Module& module = BacktraceStatic::modules_[idx]; new(&module) BacktraceStatic::Module(path, 0); if(module.Good()) { ++BacktraceStatic::nModules_; } else { module.~Module(); } dl_iterate_phdr(&BacktraceStatic::dlCallback_, nullptr); std::sort(BacktraceStatic::modules_, BacktraceStatic::modules_ + BacktraceStatic::nModules_, [](BacktraceStatic::Module const& module, BacktraceStatic::Module const& other) { return module.baseAddress < other.baseAddress; }); } void sprawl::logging::Backtrace::ShutDown() { for(size_t i = 0; i < BacktraceStatic::nModules_; ++i) { BacktraceStatic::modules_[i].~Module(); } BacktraceStatic::nModules_ = 0; BacktraceStatic::initialized_ = false; } sprawl::logging::Backtrace sprawl::logging::Backtrace::Get(int skipFrames) { // We could use libUnwind here instead of backtrace(), and in fact, we originally did. // libUnwind is very very very very very slightly faster than backtrace (to the tune of 1 us per call on average) // But since we don't use libUnwind for symbolification and libUnwind's primary performance gain is when compared to // backtrace_symbols, the 1us performance improvement does not seem worth the dependency. Backtrace stack; void* bt[SPRAWL_BACKTRACE_MAX_STACK]; stack.m_size = backtrace(bt, SPRAWL_BACKTRACE_MAX_STACK) - skipFrames; memcpy(stack.m_stack, bt + skipFrames, SPRAWL_BACKTRACE_MAX_STACK - skipFrames); return std::move(stack); } sprawl::logging::Backtrace::Frame sprawl::logging::Backtrace::GetFrame(size_t index) const { Frame frame; uintptr_t addr = m_stack[index]; frame.address = addr; BacktraceStatic::Module* module = BacktraceStatic::findModule_(addr); if(module != nullptr) { strncpy(frame.module, module->baseFilename, SPRAWL_BACKTRACE_STRING_MAX); BacktraceStatic::Symbol sym; sym.pc = addr - module->baseAddress; if(BacktraceStatic::findSymbol_(module->bfdPtr, module->symbols, sym)) { frame.offset = sym.offset; if(!BacktraceStatic::demangle_( sym.functionName, frame.func, SPRAWL_BACKTRACE_STRING_MAX )) { strncpy(frame.func, sym.functionName, SPRAWL_BACKTRACE_STRING_MAX); } if(sym.filename != nullptr) { strncpy(frame.file, sym.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 = sym.line; } else { strncpy(frame.file, "Unknown", SPRAWL_BACKTRACE_STRING_MAX); frame.baseFile = frame.file; frame.line = -1; } } } return std::move(frame); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 23398 | ququlala | "Forking branch Mainline of shadauxcat-libsprawl to ququlala-libsprawl." | ||
//guest/ShadauxCat/Sprawl/Mainline/logging/Backtrace_linux.cpp | |||||
#2 | 16283 | ShadauxCat |
Changed linux backtrace library to use backtrace() instead of libunwind. backtrace() costs about 1 us more per call than libunwind does, but saves a dependency, so that extra 1us seems not to matter much in the face of external dependency requirements. This ensures the only external dependency is libbfd, which is more readily available on more systems than libunwind and also more likely to already be installed. libunwind's major performance benefit over glibc is in symbolification, and we don't use libunwind for symbolification because libbfd gives us more information (i.e., filename and line numbers). So the dependency has very very little real benefit. #review-16284 |
||
#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 |