├── README.md ├── LICENSE └── index.h /README.md: -------------------------------------------------------------------------------- 1 | # SYNOPSIS 2 | Quick/Simple stacktrace hack. 3 | 4 | # MOTIVATION 5 | [`This`](https://medium.com/@hij1nx/how-to-make-debugging-c-errors-easier-7824922a8cc6) blog post. 6 | 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Paolo Fragomeni 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_TRACE_H 2 | #define STACK_TRACE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | struct Trace { 13 | 14 | struct sigaction sa; 15 | 16 | Trace() { 17 | 18 | using namespace std; 19 | auto backtrace_handler = [](int sig) { 20 | 21 | string white = "\033[1;37m"; 22 | string blue = "\033[1;34m"; 23 | 24 | cout << white << "Error: "; 25 | 26 | switch (sig) { 27 | case SIGSEGV: 28 | cout << "Segmentation fault" << endl; 29 | break; 30 | case SIGFPE: 31 | cout << "Floating point exception" << endl; 32 | break; 33 | case SIGBUS: 34 | cout << "An invalid pointer was dereferenced" << endl; 35 | break; 36 | } 37 | 38 | void* callstack[128]; 39 | size_t size = backtrace(callstack, 128); 40 | char** symbols = backtrace_symbols(callstack, size); 41 | 42 | for (size_t i = 0; i < size; i++) { 43 | 44 | Dl_info info; 45 | int res = dladdr(callstack[i], &info); 46 | 47 | string s = symbols[i]; 48 | int f = s.find_last_of(" + "); 49 | s = s.substr(0, f - 2); 50 | f = s.find_last_of(" "); 51 | s = s.substr(f + 1, s.size()); 52 | 53 | int status = 0; 54 | char* name = abi::__cxa_demangle(s.c_str(), NULL, NULL, &status); 55 | 56 | if (status == 0 && string(name).find("Trace::Trace()") == string::npos) { 57 | cout 58 | << white << " at " << blue << name 59 | << white << " in " << info.dli_fname << endl; 60 | } 61 | } 62 | abort(); 63 | }; 64 | 65 | sa.sa_handler = backtrace_handler; 66 | sigemptyset(&sa.sa_mask); 67 | sa.sa_flags = SA_RESTART; 68 | sigaction(SIGFPE, &sa, NULL); 69 | sigaction(SIGSEGV, &sa, NULL); 70 | sigaction(SIGBUS, &sa, NULL); 71 | } 72 | } trace; 73 | 74 | #endif 75 | 76 | --------------------------------------------------------------------------------