GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — C-stacktrace/c-stacktrace.h at master · Dexter9313/C-stacktrace Expand file tree/ Copy pathc-stacktrace.hMore file actions 199 lines (171 loc) · 4.81 KB / Copy pathc-stacktrace.hFile metadata and controls199 lines (171 loc) · 4.81 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 /* Copyright (C) 2016 Florian Cabot This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #ifndef EXCEPTIONS #define EXCEPTIONS #include <execinfo.h> #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <stdint.h> #include <err.h> #define CRITICAL(str) \ {print_stacktrace(0); \ fprintf(stderr, "%s (in %s at %s:%i)\n", str, __func__, __FILE__, __LINE__); \ exit(EXIT_FAILURE); \ } #define MAX_BACKTRACE_LINES 64 char* _programName; void print_stacktrace(int calledFromSigInt); void posix_signal_handler(int sig); void set_signal_handler(sig_t handler); void init_exceptions(char* programName); int addr2line(char const * const program_name, void const * const addr, int lineNb); //prints formated stack trace with most information as possible //parameter indicates if the function is called by the signal handler or not //(to hide the call to the signal handler) void print_stacktrace(int calledFromSigInt) { void* buffer[MAX_BACKTRACE_LINES]; char** strings; int nptrs = backtrace(buffer, MAX_BACKTRACE_LINES); strings = backtrace_symbols(buffer, nptrs); if(strings == NULL) { perror("backtrace_symbols"); exit(EXIT_FAILURE); } unsigned int i = 1; if(calledFromSigInt != 0) ++i; for(; i < (unsigned int) (nptrs-2); ++i) { //if addr2line failed, print what we can if(addr2line(_programName, buffer[i], nptrs-2-i-1) != 0) fprintf(stderr, "[%i] %s\n", nptrs-2-i-1, strings[i]); } free(strings); } void posix_signal_handler(int sig) { print_stacktrace(1); switch(sig) { case SIGABRT: fputs("Caught SIGABRT: usually caused by an abort() or assert()\n", stderr); break; case SIGFPE: fputs("Caught SIGFPE: arithmetic exception, such as divide by zero\n", stderr); break; case SIGILL: fputs("Caught SIGILL: illegal instruction\n", stderr); break; case SIGINT: fputs("Caught SIGINT: interactive attention signal, probably a ctrl+c\n", stderr); break; case SIGSEGV: fputs("Caught SIGSEGV: segfault\n", stderr); break; case SIGTERM: default: fputs("Caught SIGTERM: a termination request was sent to the program\n", stderr); break; } _Exit(EXIT_FAILURE); } void set_signal_handler(sig_t handler) { signal(SIGABRT, handler); signal(SIGFPE, handler); signal(SIGILL, handler); signal(SIGINT, handler); signal(SIGSEGV, handler); signal(SIGTERM, handler); } //lib activation, first thing to do in main //programName should be argv[0] void init_exceptions(char* programName) { set_signal_handler(posix_signal_handler); _programName = programName; } /* Resolve symbol name and source location given the path to the executable and an address */ //returns 0 if address has been resolved and a message has been printed; else returns 1 int addr2line(char const * const program_name, void const * const addr, int lineNb) { char addr2line_cmd[512] = {0}; /* have addr2line map the address to the relent line in the code */ #ifdef __APPLE__ /* apple does things differently... */ sprintf(addr2line_cmd,"atos -o %.256s %p", program_name, addr); #else sprintf(addr2line_cmd,"addr2line -f -e %.256s %p", program_name, addr); #endif /* This will print a nicely formatted string specifying the function and source line of the address */ FILE *fp; char outLine1[1035]; char outLine2[1035]; /* Open the command for reading. */ fp = popen(addr2line_cmd, "r"); if (fp == NULL) return 1; while (fgets(outLine1, sizeof(outLine1)-1, fp) != NULL) { //if we have a pair of lines if(fgets(outLine2, sizeof(outLine2)-1, fp) != NULL) { //if symbols are readable if(outLine2[0] != '?') { //only let func name in outLine1 int i; for(i = 0; i < 1035; ++i) { if(outLine1[i] == '\r' || outLine1[i] == '\n') { outLine1[i] = '\0'; break; } } //don't display the whole path int lastSlashPos=0; for(i = 0; i < 1035; ++i) { if(outLine2[i] == '\0') break; if(outLine2[i] == '/') lastSlashPos = i+1; } fprintf(stderr, "[%i] %p in %s at %s", lineNb, addr, outLine1, outLine2+lastSlashPos); fflush(stderr); } else { pclose(fp); return 1; } } else { pclose(fp); return 1; } } /* close */ pclose(fp); return 0; } #endif |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |