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