| 1 | //
|
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
|---|
| 3 | //
|
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
|---|
| 5 | // file "LICENCE" distributed with Cforall.
|
|---|
| 6 | //
|
|---|
| 7 | // interpose.c --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Thierry Delisle
|
|---|
| 10 | // Created On : Wed Mar 29 16:10:31 2017
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Fri Jul 21 22:27:33 2017
|
|---|
| 13 | // Update Count : 1
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <stdarg.h>
|
|---|
| 17 | #include <stddef.h>
|
|---|
| 18 |
|
|---|
| 19 | extern "C" {
|
|---|
| 20 | #include <stdio.h>
|
|---|
| 21 | #include <string.h>
|
|---|
| 22 | #include <dlfcn.h>
|
|---|
| 23 | #include <unistd.h>
|
|---|
| 24 | #define __USE_GNU
|
|---|
| 25 | #include <signal.h>
|
|---|
| 26 | #undef __USE_GNU
|
|---|
| 27 | #include <execinfo.h>
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | #include "bits/debug.h"
|
|---|
| 31 | #include "bits/defs.h"
|
|---|
| 32 | #include "bits/signal.h"
|
|---|
| 33 | #include "startup.h"
|
|---|
| 34 |
|
|---|
| 35 | void __cfaabi_interpose_startup(void) __attribute__(( constructor( STARTUP_PRIORITY_CORE ) ));
|
|---|
| 36 |
|
|---|
| 37 | typedef void (*generic_fptr_t)(void);
|
|---|
| 38 | generic_fptr_t interpose_symbol( const char* symbol, const char *version ) {
|
|---|
| 39 | const char * error;
|
|---|
| 40 |
|
|---|
| 41 | static void * library;
|
|---|
| 42 | if ( ! library ) {
|
|---|
| 43 | #if defined( RTLD_NEXT )
|
|---|
| 44 | library = RTLD_NEXT;
|
|---|
| 45 | #else
|
|---|
| 46 | // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
|
|---|
| 47 | library = dlopen( "libc.so.6", RTLD_LAZY );
|
|---|
| 48 | error = dlerror();
|
|---|
| 49 | if ( error ) {
|
|---|
| 50 | abortf( "interpose_symbol : failed to open libc, %s\n", error );
|
|---|
| 51 | }
|
|---|
| 52 | #endif
|
|---|
| 53 | } // if
|
|---|
| 54 |
|
|---|
| 55 | union { generic_fptr_t fptr; void* ptr; } originalFunc;
|
|---|
| 56 |
|
|---|
| 57 | #if defined( _GNU_SOURCE )
|
|---|
| 58 | if ( version ) {
|
|---|
| 59 | originalFunc.ptr = dlvsym( library, symbol, version );
|
|---|
| 60 | } else {
|
|---|
| 61 | originalFunc.ptr = dlsym( library, symbol );
|
|---|
| 62 | }
|
|---|
| 63 | #else
|
|---|
| 64 | originalFunc.ptr = dlsym( library, symbol );
|
|---|
| 65 | #endif // _GNU_SOURCE
|
|---|
| 66 |
|
|---|
| 67 | error = dlerror();
|
|---|
| 68 | if ( error ) abortf( "interpose_symbol : internal error, %s\n", error );
|
|---|
| 69 |
|
|---|
| 70 | return originalFunc.fptr;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 | __typeof__( exit ) libc_exit __attribute__(( noreturn ));
|
|---|
| 75 | __typeof__( abort ) libc_abort __attribute__(( noreturn ));
|
|---|
| 76 |
|
|---|
| 77 | forall(dtype T)
|
|---|
| 78 | static inline void assign_ptr( T** symbol_ptr, const char * symbol_name, const char * version) {
|
|---|
| 79 | union {
|
|---|
| 80 | generic_fptr_t gp;
|
|---|
| 81 | T* tp;
|
|---|
| 82 | } u;
|
|---|
| 83 |
|
|---|
| 84 | u.gp = interpose_symbol( symbol_name, version );
|
|---|
| 85 |
|
|---|
| 86 | *symbol_ptr = u.tp;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | #define INIT_REALRTN( x, ver ) assign_ptr( (void**)&libc_##x, #x, ver)
|
|---|
| 90 |
|
|---|
| 91 | void sigHandler_segv ( __CFA_SIGPARMS__ );
|
|---|
| 92 | void sigHandler_abort( __CFA_SIGPARMS__ );
|
|---|
| 93 |
|
|---|
| 94 | void __cfaabi_interpose_startup() {
|
|---|
| 95 | const char *version = NULL;
|
|---|
| 96 |
|
|---|
| 97 | INIT_REALRTN( abort, version );
|
|---|
| 98 | INIT_REALRTN( exit, version );
|
|---|
| 99 |
|
|---|
| 100 | __kernel_sigaction( SIGSEGV, sigHandler_segv , SA_SIGINFO ); // Failure handler
|
|---|
| 101 | __kernel_sigaction( SIGBUS , sigHandler_segv , SA_SIGINFO ); // Failure handler
|
|---|
| 102 | __kernel_sigaction( SIGABRT, sigHandler_abort, SA_SIGINFO ); // Failure handler
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | //=============================================================================================
|
|---|
| 106 | // Terminating Signals logic
|
|---|
| 107 | //=============================================================================================
|
|---|
| 108 |
|
|---|
| 109 | extern "C" {
|
|---|
| 110 | void abort (void) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
|
|---|
| 111 | abortf( NULL );
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | void exit (int __status) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
|
|---|
| 115 | libc_exit(__status);
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | void abort( const char *fmt, va_list argp ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
|
|---|
| 120 | abortf( fmt, argp );
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | void * kernel_abort (void) __attribute__ ((__nothrow__, __leaf__, __weak__)) { return NULL; }
|
|---|
| 124 | void kernel_abort_msg(void * data, char * buffer, int size) __attribute__ ((__nothrow__, __leaf__, __weak__)) {}
|
|---|
| 125 |
|
|---|
| 126 | enum { abort_text_size = 1024 };
|
|---|
| 127 | static char abort_text[ abort_text_size ];
|
|---|
| 128 |
|
|---|
| 129 | extern "C" {
|
|---|
| 130 | void abortf( const char fmt[], ... ) __attribute__ ((__nothrow__, __leaf__, __noreturn__)) {
|
|---|
| 131 | void * kernel_data = kernel_abort();
|
|---|
| 132 |
|
|---|
| 133 | int len;
|
|---|
| 134 |
|
|---|
| 135 | if( fmt ) {
|
|---|
| 136 | va_list args;
|
|---|
| 137 | va_start( args, fmt );
|
|---|
| 138 |
|
|---|
| 139 | len = vsnprintf( abort_text, abort_text_size, fmt, args );
|
|---|
| 140 |
|
|---|
| 141 | va_end( args );
|
|---|
| 142 |
|
|---|
| 143 | __cfaabi_dbg_bits_write( abort_text, len );
|
|---|
| 144 | __cfaabi_dbg_bits_write( "\n", 1 );
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld)\n", (long int)getpid() ); // use UNIX pid (versus getPid)
|
|---|
| 148 | __cfaabi_dbg_bits_write( abort_text, len );
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | kernel_abort_msg( kernel_data, abort_text, abort_text_size );
|
|---|
| 152 |
|
|---|
| 153 | libc_abort();
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | // skip first 6 stack frames by default
|
|---|
| 158 | static void __kernel_backtrace() {
|
|---|
| 159 | // skip first N stack frames
|
|---|
| 160 | int start = 6;
|
|---|
| 161 |
|
|---|
| 162 | enum { Frames = 50 };
|
|---|
| 163 | void * array[Frames];
|
|---|
| 164 | int size = backtrace( array, Frames );
|
|---|
| 165 | char ** messages = backtrace_symbols( array, size );
|
|---|
| 166 |
|
|---|
| 167 | // find executable name
|
|---|
| 168 | *index( messages[0], '(' ) = '\0';
|
|---|
| 169 | __cfaabi_dbg_bits_print_nolock( "Stack back trace for: %s\n", messages[0]);
|
|---|
| 170 |
|
|---|
| 171 | // skip last 2 stack frames after main
|
|---|
| 172 | for ( int i = start; i < size && messages != NULL; i += 1 ) {
|
|---|
| 173 | char * name = NULL;
|
|---|
| 174 | char * offset_begin = NULL;
|
|---|
| 175 | char * offset_end = NULL;
|
|---|
| 176 |
|
|---|
| 177 | for ( char *p = messages[i]; *p; ++p ) {
|
|---|
| 178 | // find parantheses and +offset
|
|---|
| 179 | if ( *p == '(' ) {
|
|---|
| 180 | name = p;
|
|---|
| 181 | }
|
|---|
| 182 | else if ( *p == '+' ) {
|
|---|
| 183 | offset_begin = p;
|
|---|
| 184 | }
|
|---|
| 185 | else if ( *p == ')' ) {
|
|---|
| 186 | offset_end = p;
|
|---|
| 187 | break;
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | // if line contains symbol print it
|
|---|
| 192 | int frameNo = i - start;
|
|---|
| 193 | if ( name && offset_begin && offset_end && name < offset_begin ) {
|
|---|
| 194 | // delimit strings
|
|---|
| 195 | *name++ = '\0';
|
|---|
| 196 | *offset_begin++ = '\0';
|
|---|
| 197 | *offset_end++ = '\0';
|
|---|
| 198 |
|
|---|
| 199 | __cfaabi_dbg_bits_print_nolock( "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
|
|---|
| 200 | }
|
|---|
| 201 | // otherwise, print the whole line
|
|---|
| 202 | else {
|
|---|
| 203 | __cfaabi_dbg_bits_print_nolock( "(%i) %s\n", frameNo, messages[i] );
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | free( messages );
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | void sigHandler_segv( __CFA_SIGPARMS__ ) {
|
|---|
| 211 | // skip first only 1 stack frames in case of segfault.
|
|---|
| 212 | abortf( "*CFA runtime error* program cfa-cpp terminated with %s\n", sig == SIGSEGV ? "segment fault." : "bus error." );
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | void sigHandler_abort( __CFA_SIGPARMS__ ) {
|
|---|
| 216 | __kernel_backtrace();
|
|---|
| 217 |
|
|---|
| 218 | // reset default signal handler
|
|---|
| 219 | __kernel_sigdefault( SIGABRT );
|
|---|
| 220 |
|
|---|
| 221 | raise( SIGABRT );
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | // Local Variables: //
|
|---|
| 225 | // mode: c //
|
|---|
| 226 | // tab-width: 4 //
|
|---|
| 227 | // End: //
|
|---|