Line data Source code
1 : #pragma once 2 : 3 : #include <string> 4 : #include <functional> 5 : #include <vector> 6 : 7 : namespace elsa::detail 8 : { 9 : 10 : /** 11 : * Demangles a symbol name. 12 : * 13 : * On failure, the mangled symbol name is returned. 14 : */ 15 : std::string symbolDemangle(const char* symbol); 16 : 17 : /** 18 : * Convert a pointer address to string. 19 : */ 20 : std::string addrToString(const void* addr); 21 : 22 : /** 23 : * Return the demangled symbol name for a given code address. 24 : */ 25 : std::string symbolName(const void* addr, bool require_exact_addr = true, 26 : bool no_pure_addrs = false); 27 : 28 : } // namespace elsa::detail 29 : 30 : namespace elsa 31 : { 32 : /** 33 : * A single symbol, as determined from a program counter, and returned by 34 : * Backtrace::getSymbols(). 35 : */ 36 : struct backtrace_symbol { 37 : std::string functionname; // empty if unknown 38 : void* pc; // nullptr if unknown 39 : }; 40 : 41 : /** 42 : * Provide execution backtrace information through getSymbols(). 43 : */ 44 : class Backtrace 45 : { 46 : public: 47 383 : Backtrace() = default; 48 : 49 383 : virtual ~Backtrace() = default; 50 : 51 : /** 52 : * Analyzes the current stack, and stores the program counter values in 53 : * this->stack_addrs. 54 : */ 55 : void analyze(); 56 : 57 : /** 58 : * Provide the names for all stack frames via the callback. 59 : * 60 : * The most recent call is returned last (alike Python). 61 : * 62 : * @param cb 63 : * is called for every symbol in the backtrace, 64 : * starting with the top-most frame. 65 : * @param reversed 66 : * if true, the most recent call is given last. 67 : */ 68 : void getSymbols(const std::function<void(const backtrace_symbol*)>& cb, 69 : bool reversed = true) const; 70 : 71 : /** 72 : * Removes all the lower frames that are also present 73 : * in the current stack. 74 : * 75 : * Designed to be used in catch clauses, 76 : * to simulate stack trace collection 77 : * from throw to catch, instead of from throw to the process entry point. 78 : */ 79 : void trimToCurrentStackFrame(); 80 : 81 : protected: 82 : /** 83 : * All program counters of this backtrace. 84 : */ 85 : std::vector<void*> stack_addrs; 86 : }; 87 : } // namespace elsa