Ignore:
Timestamp:
Jan 7, 2021, 2:55:57 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
58fe85a
Parents:
bdfc032 (diff), 44e37ef (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into dkobets-vector

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/interpose.cfa

    rbdfc032 reef8dfb  
    1010// Created On       : Wed Mar 29 16:10:31 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jan 30 17:47:32 2020
    13 // Update Count     : 156
     12// Last Modified On : Fri Mar 13 17:35:37 2020
     13// Update Count     : 178
    1414//
    1515
    1616#include <stdarg.h>                                                                             // va_start, va_end
     17#include <stdio.h>
    1718#include <string.h>                                                                             // strlen
    1819#include <unistd.h>                                                                             // _exit, getpid
     
    2930#include "bits/signal.hfa"                                                              // sigHandler_?
    3031#include "startup.hfa"                                                                  // STARTUP_PRIORITY_CORE
     32#include <assert.h>
    3133
    3234//=============================================================================================
     
    4042
    4143typedef void (* generic_fptr_t)(void);
    42 generic_fptr_t interpose_symbol( const char * symbol, const char * version ) {
     44generic_fptr_t interpose_symbol( const char symbol[], const char version[] ) {
    4345        const char * error;
    4446
     
    142144void abort( const char fmt[], ... ) __attribute__(( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
    143145void abort( bool signalAbort, const char fmt[], ... ) __attribute__(( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
     146void __abort( bool signalAbort, const char fmt[], va_list args ) __attribute__(( __nothrow__, __leaf__, __noreturn__ ));
    144147
    145148extern "C" {
    146149        void abort( void ) __attribute__(( __nothrow__, __leaf__, __noreturn__ )) {
    147                 abort( false, NULL ); // FIX ME: 0p does not work
     150                abort( false, "%s", "" );
    148151        }
    149152
     
    151154                va_list argp;
    152155                va_start( argp, fmt );
    153                 abort( false, fmt, argp );
     156                __abort( false, fmt, argp );
    154157                va_end( argp );
    155158        }
     
    161164
    162165void * kernel_abort( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 0p; }
    163 void kernel_abort_msg( void * data, char * buffer, int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
     166void kernel_abort_msg( void * data, char buffer[], int size ) __attribute__(( __nothrow__, __leaf__, __weak__ )) {}
    164167// See concurrency/kernel.cfa for strong definition used in multi-processor mode.
    165168int kernel_abort_lastframe( void ) __attribute__(( __nothrow__, __leaf__, __weak__ )) { return 4; }
     
    169172
    170173static void __cfaabi_backtrace( int start ) {
    171         enum {
    172                 Frames = 50,                                                                    // maximum number of stack frames
    173         };
     174        enum { Frames = 50, };                                                          // maximum number of stack frames
    174175        int last = kernel_abort_lastframe();                            // skip last N stack frames
    175176
    176177        void * array[Frames];
    177178        size_t size = backtrace( array, Frames );
    178         char ** messages = backtrace_symbols( array, size );
     179        char ** messages = backtrace_symbols( array, size ); // does not demangle names
    179180
    180181        *index( messages[0], '(' ) = '\0';                                      // find executable name
     
    184185                char * name = 0p, * offset_begin = 0p, * offset_end = 0p;
    185186
    186                 for ( char * p = messages[i]; *p; ++p ) {               // find parantheses and +offset
     187                for ( char * p = messages[i]; *p; p += 1 ) {    // find parantheses and +offset
    187188                        //__cfaabi_bits_print_nolock( "X %s\n", p);
    188189                        if ( *p == '(' ) {
     
    219220}
    220221
    221 void 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 
     222static volatile int __abort_stage = 0;
     223
     224// Cannot forward va_list.
     225void __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)
     240                __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
     241
     242                assert( fmt );
    234243                len = vsnprintf( abort_text, abort_text_size, fmt, args );
    235                 va_end( args );
    236244                __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
    237245
    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
     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);
    247268}
    248269
     
    250271        va_list args;
    251272        va_start( args, fmt );
    252         abort( false, fmt, args );
     273        __abort( false, fmt, args );
     274    // CONTROL NEVER REACHES HERE!
    253275        va_end( args );
     276}
     277
     278void 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 );
    254284}
    255285
Note: See TracChangeset for help on using the changeset viewer.