Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/interpose.cfa

    r6011658 r8a13c47  
    1010// Created On       : Wed Mar 29 16:10:31 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Mar 13 17:35:37 2020
    13 // Update Count     : 178
     12// Last Modified On : Thu Jan 30 17:47:32 2020
     13// Update Count     : 156
    1414//
    1515
    1616#include <stdarg.h>                                                                             // va_start, va_end
    17 #include <stdio.h>
    1817#include <string.h>                                                                             // strlen
    1918#include <unistd.h>                                                                             // _exit, getpid
     
    3029#include "bits/signal.hfa"                                                              // sigHandler_?
    3130#include "startup.hfa"                                                                  // STARTUP_PRIORITY_CORE
    32 #include <assert.h>
    3331
    3432//=============================================================================================
     
    4240
    4341typedef void (* generic_fptr_t)(void);
    44 generic_fptr_t interpose_symbol( const char symbol[], const char version[] ) {
     42generic_fptr_t interpose_symbol( const char * symbol, const char * version ) {
    4543        const char * error;
    4644
     
    144142void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
    145143void abort( bool signalAbort, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
    146 void __abort( bool signalAbort, const char fmt[], va_list args ) __attribute__(( __nothrow__, __leaf__, __noreturn__ ));
    147144
    148145extern "C" {
    149146        void abort( void ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
    150                 abort( false, "%s", "" );
     147                abort( false, NULL ); // FIX ME: 0p does not work
    151148        }
    152149
     
    154151                va_list argp;
    155152                va_start( argp, fmt );
    156                 __abort( false, fmt, argp );
     153                abort( false, fmt, argp );
    157154                va_end( argp );
    158155        }
     
    164161
    165162void * kernel_abort( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 0p; }
    166 void kernel_abort_msg( void * data, char buffer[], int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
     163void kernel_abort_msg( void * data, char * buffer, int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
    167164// See concurrency/kernel.cfa for strong definition used in multi-processor mode.
    168165int kernel_abort_lastframe( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 4; }
     
    172169
    173170static void __cfaabi_backtrace( int start ) {
    174         enum { Frames = 50, };                                                          // maximum number of stack frames
     171        enum {
     172                Frames = 50,                                                                    // maximum number of stack frames
     173        };
    175174        int last = kernel_abort_lastframe();                            // skip last N stack frames
    176175
    177176        void * array[Frames];
    178177        size_t size = backtrace( array, Frames );
    179         char ** messages = backtrace_symbols( array, size ); // does not demangle names
     178        char ** messages = backtrace_symbols( array, size );
    180179
    181180        *index( messages[0], '(' ) = '\0';                                      // find executable name
     
    185184                char * name = 0p, * offset_begin = 0p, * offset_end = 0p;
    186185
    187                 for ( char * p = messages[i]; *p; p += 1 ) {    // find parantheses and +offset
     186                for ( char * p = messages[i]; *p; ++p ) {               // find parantheses and +offset
    188187                        //__cfaabi_bits_print_nolock( "X %s\n", p);
    189188                        if ( *p == '(' ) {
     
    220219}
    221220
    222 static volatile int __abort_stage = 0;
    223 
    224 // Cannot forward va_list.
    225 void __abort( bool signalAbort, const char fmt[], va_list args ) {
    226         int stage = __atomic_add_fetch( &__abort_stage, 1, __ATOMIC_SEQ_CST );
    227 
    228         // First stage: stop the cforall kernel and print
    229         if(stage == 1) {
    230                 // increment stage
    231                 stage = __atomic_add_fetch( &__abort_stage, 1, __ATOMIC_SEQ_CST );
    232 
    233                 // must be done here to lock down kernel
    234                 void * kernel_data = kernel_abort();
    235                 int len;
    236 
    237                 signal( SIGABRT, SIG_DFL );                                                     // prevent final "real" abort from recursing to handler
    238 
    239                 len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid)
     221void abort( bool signalAbort, const char fmt[], ... ) {
     222        void * kernel_data = kernel_abort();                            // must be done here to lock down kernel
     223        int len;
     224
     225        signal( SIGABRT, SIG_DFL );                                                     // prevent final "real" abort from recursing to handler
     226
     227        len = snprintf( abort_text, abort_text_size, "Cforall Runtime error (UNIX pid:%ld) ", (long int)getpid() ); // use UNIX pid (versus getPid)
     228        __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
     229
     230        if ( fmt ) {
     231                va_list args;
     232                va_start( args, fmt );
     233
     234                len = vsnprintf( abort_text, abort_text_size, fmt, args );
     235                va_end( args );
    240236                __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
    241237
    242                 assert( fmt );
    243                 len = vsnprintf( abort_text, abort_text_size, fmt, args );
    244                 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
    245 
    246                 // add optional newline if missing at the end of the format text
    247                 if ( fmt[strlen( fmt ) - 1] != '\n' ) {
    248                         __cfaabi_bits_write( STDERR_FILENO, "\n", 1 );
    249                 } // if
    250                 kernel_abort_msg( kernel_data, abort_text, abort_text_size );
    251         }
    252 
    253         // Second stage: print the backtrace
    254         if(stage == 2) {
    255                 // increment stage
    256                 stage = __atomic_add_fetch( &__abort_stage, 1, __ATOMIC_SEQ_CST );
    257 
    258                 // print stack trace in handler
    259                 __cfaabi_backtrace( signalAbort ? 4 : 2 );
    260         }
    261 
    262         do {
    263                 // Finally call abort
    264                 __cabi_libc.abort();
    265 
    266                 // Loop so that we never return
    267         } while(true);
     238                if ( fmt[strlen( fmt ) - 1] != '\n' ) {                 // add optional newline if missing at the end of the format text
     239                        __cfaabi_dbg_write( "\n", 1 );
     240                }
     241        }
     242
     243        kernel_abort_msg( kernel_data, abort_text, abort_text_size );
     244        __cfaabi_backtrace( signalAbort ? 4 : 3 );
     245
     246        __cabi_libc.abort();                                                            // print stack trace in handler
    268247}
    269248
     
    271250        va_list args;
    272251        va_start( args, fmt );
    273         __abort( false, fmt, args );
    274     // CONTROL NEVER REACHES HERE!
     252        abort( false, fmt, args );
    275253        va_end( args );
    276 }
    277 
    278 void abort( bool signalAbort, const char fmt[], ... ) {
    279     va_list args;
    280     va_start( args, fmt );
    281     __abort( signalAbort, fmt, args );
    282     // CONTROL NEVER REACHES HERE!
    283     va_end( args );
    284254}
    285255
Note: See TracChangeset for help on using the changeset viewer.