| 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 | // signal.c --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Thierry Delisle
 | 
|---|
| 10 | // Created On       : Mon Jun 5 14:20:42 2017
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Fri Jul 21 22:36:05 2017
 | 
|---|
| 13 | // Update Count     : 2
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "preemption.h"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | extern "C" {
 | 
|---|
| 19 | #include <errno.h>
 | 
|---|
| 20 | #include <execinfo.h>
 | 
|---|
| 21 | #define __USE_GNU
 | 
|---|
| 22 | #include <signal.h>
 | 
|---|
| 23 | #undef __USE_GNU
 | 
|---|
| 24 | #include <stdio.h>
 | 
|---|
| 25 | #include <string.h>
 | 
|---|
| 26 | #include <unistd.h>
 | 
|---|
| 27 | }
 | 
|---|
| 28 | 
 | 
|---|
| 29 | 
 | 
|---|
| 30 | #ifdef __USE_STREAM__
 | 
|---|
| 31 | #include "fstream"
 | 
|---|
| 32 | #endif
 | 
|---|
| 33 | 
 | 
|---|
| 34 | //TODO move to defaults
 | 
|---|
| 35 | #define __CFA_DEFAULT_PREEMPTION__ 10000
 | 
|---|
| 36 | 
 | 
|---|
| 37 | //TODO move to defaults
 | 
|---|
| 38 | __attribute__((weak)) unsigned int default_preemption() {
 | 
|---|
| 39 |         return __CFA_DEFAULT_PREEMPTION__;
 | 
|---|
| 40 | }
 | 
|---|
| 41 | 
 | 
|---|
| 42 | // Short hands for signal context information
 | 
|---|
| 43 | #define __CFA_SIGCXT__ ucontext_t *
 | 
|---|
| 44 | #define __CFA_SIGPARMS__ __attribute__((unused)) int sig, __attribute__((unused)) siginfo_t *sfp, __attribute__((unused)) __CFA_SIGCXT__ cxt
 | 
|---|
| 45 | 
 | 
|---|
| 46 | // FwdDeclarations : timeout handlers
 | 
|---|
| 47 | static void preempt( processor   * this );
 | 
|---|
| 48 | static void timeout( thread_desc * this );
 | 
|---|
| 49 | 
 | 
|---|
| 50 | // FwdDeclarations : Signal handlers
 | 
|---|
| 51 | void sigHandler_ctxSwitch( __CFA_SIGPARMS__ );
 | 
|---|
| 52 | void sigHandler_segv     ( __CFA_SIGPARMS__ );
 | 
|---|
| 53 | void sigHandler_abort    ( __CFA_SIGPARMS__ );
 | 
|---|
| 54 | 
 | 
|---|
| 55 | // FwdDeclarations : sigaction wrapper
 | 
|---|
| 56 | static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags );
 | 
|---|
| 57 | 
 | 
|---|
| 58 | // FwdDeclarations : alarm thread main
 | 
|---|
| 59 | void * alarm_loop( __attribute__((unused)) void * args );
 | 
|---|
| 60 | 
 | 
|---|
| 61 | // Machine specific register name
 | 
|---|
| 62 | #ifdef __x86_64__
 | 
|---|
| 63 | #define CFA_REG_IP REG_RIP
 | 
|---|
| 64 | #else
 | 
|---|
| 65 | #define CFA_REG_IP REG_EIP
 | 
|---|
| 66 | #endif
 | 
|---|
| 67 | 
 | 
|---|
| 68 | KERNEL_STORAGE(event_kernel_t, event_kernel);         // private storage for event kernel
 | 
|---|
| 69 | event_kernel_t * event_kernel;                        // kernel public handle to even kernel
 | 
|---|
| 70 | static pthread_t alarm_thread;                        // pthread handle to alarm thread
 | 
|---|
| 71 | 
 | 
|---|
| 72 | void ?{}(event_kernel_t & this) {
 | 
|---|
| 73 |         (this.alarms){};
 | 
|---|
| 74 |         (this.lock){};
 | 
|---|
| 75 | }
 | 
|---|
| 76 | 
 | 
|---|
| 77 | //=============================================================================================
 | 
|---|
| 78 | // Kernel Preemption logic
 | 
|---|
| 79 | //=============================================================================================
 | 
|---|
| 80 | 
 | 
|---|
| 81 | // Get next expired node
 | 
|---|
| 82 | static inline alarm_node_t * get_expired( alarm_list_t * alarms, __cfa_time_t currtime ) {
 | 
|---|
| 83 |         if( !alarms->head ) return NULL;                          // If no alarms return null
 | 
|---|
| 84 |         if( alarms->head->alarm >= currtime ) return NULL;        // If alarms head not expired return null
 | 
|---|
| 85 |         return pop(alarms);                                       // Otherwise just pop head
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | // Tick one frame of the Discrete Event Simulation for alarms
 | 
|---|
| 89 | void tick_preemption() {
 | 
|---|
| 90 |         alarm_node_t * node = NULL;                     // Used in the while loop but cannot be declared in the while condition
 | 
|---|
| 91 |         alarm_list_t * alarms = &event_kernel->alarms;  // Local copy for ease of reading
 | 
|---|
| 92 |         __cfa_time_t currtime = __kernel_get_time();    // Check current time once so we everything "happens at once"
 | 
|---|
| 93 | 
 | 
|---|
| 94 |         //Loop throught every thing expired
 | 
|---|
| 95 |         while( node = get_expired( alarms, currtime ) ) {
 | 
|---|
| 96 | 
 | 
|---|
| 97 |                 // Check if this is a kernel
 | 
|---|
| 98 |                 if( node->kernel_alarm ) {
 | 
|---|
| 99 |                         preempt( node->proc );
 | 
|---|
| 100 |                 }
 | 
|---|
| 101 |                 else {
 | 
|---|
| 102 |                         timeout( node->thrd );
 | 
|---|
| 103 |                 }
 | 
|---|
| 104 | 
 | 
|---|
| 105 |                 // Check if this is a periodic alarm
 | 
|---|
| 106 |                 __cfa_time_t period = node->period;
 | 
|---|
| 107 |                 if( period > 0 ) {
 | 
|---|
| 108 |                         node->alarm = currtime + period;    // Alarm is periodic, add currtime to it (used cached current time)
 | 
|---|
| 109 |                         insert( alarms, node );             // Reinsert the node for the next time it triggers
 | 
|---|
| 110 |                 }
 | 
|---|
| 111 |                 else {
 | 
|---|
| 112 |                         node->set = false;                  // Node is one-shot, just mark it as not pending
 | 
|---|
| 113 |                 }
 | 
|---|
| 114 |         }
 | 
|---|
| 115 | 
 | 
|---|
| 116 |         // If there are still alarms pending, reset the timer
 | 
|---|
| 117 |         if( alarms->head ) { __kernel_set_timer( alarms->head->alarm - currtime ); }
 | 
|---|
| 118 | }
 | 
|---|
| 119 | 
 | 
|---|
| 120 | // Update the preemption of a processor and notify interested parties
 | 
|---|
| 121 | void update_preemption( processor * this, __cfa_time_t duration ) {
 | 
|---|
| 122 |         alarm_node_t * alarm = this->preemption_alarm;
 | 
|---|
| 123 | 
 | 
|---|
| 124 |         // Alarms need to be enabled
 | 
|---|
| 125 |         if ( duration > 0 && !alarm->set ) {
 | 
|---|
| 126 |                 alarm->alarm = __kernel_get_time() + duration;
 | 
|---|
| 127 |                 alarm->period = duration;
 | 
|---|
| 128 |                 register_self( alarm );
 | 
|---|
| 129 |         }
 | 
|---|
| 130 |         // Zero duraction but alarm is set
 | 
|---|
| 131 |         else if ( duration == 0 && alarm->set ) {
 | 
|---|
| 132 |                 unregister_self( alarm );
 | 
|---|
| 133 |                 alarm->alarm = 0;
 | 
|---|
| 134 |                 alarm->period = 0;
 | 
|---|
| 135 |         }
 | 
|---|
| 136 |         // If alarm is different from previous, change it
 | 
|---|
| 137 |         else if ( duration > 0 && alarm->period != duration ) {
 | 
|---|
| 138 |                 unregister_self( alarm );
 | 
|---|
| 139 |                 alarm->alarm = __kernel_get_time() + duration;
 | 
|---|
| 140 |                 alarm->period = duration;
 | 
|---|
| 141 |                 register_self( alarm );
 | 
|---|
| 142 |         }
 | 
|---|
| 143 | }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | //=============================================================================================
 | 
|---|
| 146 | // Kernel Signal Tools
 | 
|---|
| 147 | //=============================================================================================
 | 
|---|
| 148 | 
 | 
|---|
| 149 | __cfaabi_dbg_debug_do( static thread_local void * last_interrupt = 0; )
 | 
|---|
| 150 | 
 | 
|---|
| 151 | extern "C" {
 | 
|---|
| 152 |         // Disable interrupts by incrementing the counter
 | 
|---|
| 153 |         void disable_interrupts() {
 | 
|---|
| 154 |                 __attribute__((unused)) unsigned short new_val = __atomic_add_fetch_2( &disable_preempt_count, 1, __ATOMIC_SEQ_CST );
 | 
|---|
| 155 |                 verify( new_val < 65_000u );              // If this triggers someone is disabling interrupts without enabling them
 | 
|---|
| 156 |         }
 | 
|---|
| 157 | 
 | 
|---|
| 158 |         // Enable interrupts by decrementing the counter
 | 
|---|
| 159 |         // If counter reaches 0, execute any pending CtxSwitch
 | 
|---|
| 160 |         void enable_interrupts( __cfaabi_dbg_ctx_param ) {
 | 
|---|
| 161 |                 processor * proc   = this_processor;      // Cache the processor now since interrupts can start happening after the atomic add
 | 
|---|
| 162 |                 thread_desc * thrd = this_thread;         // Cache the thread now since interrupts can start happening after the atomic add
 | 
|---|
| 163 | 
 | 
|---|
| 164 |                 unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
 | 
|---|
| 165 |                 verify( prev != 0u );                     // If this triggers someone is enabled already enabled interruptsverify( prev != 0u );
 | 
|---|
| 166 | 
 | 
|---|
| 167 |                 // Check if we need to prempt the thread because an interrupt was missed
 | 
|---|
| 168 |                 if( prev == 1 && proc->pending_preemption ) {
 | 
|---|
| 169 |                         proc->pending_preemption = false;
 | 
|---|
| 170 |                         BlockInternal( thrd );
 | 
|---|
| 171 |                 }
 | 
|---|
| 172 | 
 | 
|---|
| 173 |                 // For debugging purposes : keep track of the last person to enable the interrupts
 | 
|---|
| 174 |                 __cfaabi_dbg_debug_do( proc->last_enable = caller; )
 | 
|---|
| 175 |         }
 | 
|---|
| 176 | 
 | 
|---|
| 177 |         // Disable interrupts by incrementint the counter
 | 
|---|
| 178 |         // Don't execute any pending CtxSwitch even if counter reaches 0
 | 
|---|
| 179 |         void enable_interrupts_noPoll() {
 | 
|---|
| 180 |                 __attribute__((unused)) unsigned short prev = __atomic_fetch_add_2( &disable_preempt_count, -1, __ATOMIC_SEQ_CST );
 | 
|---|
| 181 |                 verify( prev != 0u );                     // If this triggers someone is enabled already enabled interrupts
 | 
|---|
| 182 |         }
 | 
|---|
| 183 | }
 | 
|---|
| 184 | 
 | 
|---|
| 185 | // sigprocmask wrapper : unblock a single signal
 | 
|---|
| 186 | static inline void signal_unblock( int sig ) {
 | 
|---|
| 187 |         sigset_t mask;
 | 
|---|
| 188 |         sigemptyset( &mask );
 | 
|---|
| 189 |         sigaddset( &mask, sig );
 | 
|---|
| 190 | 
 | 
|---|
| 191 |         if ( pthread_sigmask( SIG_UNBLOCK, &mask, NULL ) == -1 ) {
 | 
|---|
| 192 |             abortf( "internal error, pthread_sigmask" );
 | 
|---|
| 193 |         }
 | 
|---|
| 194 | }
 | 
|---|
| 195 | 
 | 
|---|
| 196 | // sigprocmask wrapper : block a single signal
 | 
|---|
| 197 | static inline void signal_block( int sig ) {
 | 
|---|
| 198 |         sigset_t mask;
 | 
|---|
| 199 |         sigemptyset( &mask );
 | 
|---|
| 200 |         sigaddset( &mask, sig );
 | 
|---|
| 201 | 
 | 
|---|
| 202 |         if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {
 | 
|---|
| 203 |             abortf( "internal error, pthread_sigmask" );
 | 
|---|
| 204 |         }
 | 
|---|
| 205 | }
 | 
|---|
| 206 | 
 | 
|---|
| 207 | // kill wrapper : signal a processor
 | 
|---|
| 208 | static void preempt( processor * this ) {
 | 
|---|
| 209 |         pthread_kill( this->kernel_thread, SIGUSR1 );
 | 
|---|
| 210 | }
 | 
|---|
| 211 | 
 | 
|---|
| 212 | // reserved for future use
 | 
|---|
| 213 | static void timeout( thread_desc * this ) {
 | 
|---|
| 214 |         //TODO : implement waking threads
 | 
|---|
| 215 | }
 | 
|---|
| 216 | 
 | 
|---|
| 217 | 
 | 
|---|
| 218 | // Check if a CtxSwitch signal handler shoud defer
 | 
|---|
| 219 | // If true  : preemption is safe
 | 
|---|
| 220 | // If false : preemption is unsafe and marked as pending
 | 
|---|
| 221 | static inline bool preemption_ready() {
 | 
|---|
| 222 |         bool ready = disable_preempt_count == 0 && !preemption_in_progress; // Check if preemption is safe
 | 
|---|
| 223 |         this_processor->pending_preemption = !ready;                        // Adjust the pending flag accordingly
 | 
|---|
| 224 |         return ready;
 | 
|---|
| 225 | }
 | 
|---|
| 226 | 
 | 
|---|
| 227 | //=============================================================================================
 | 
|---|
| 228 | // Kernel Signal Startup/Shutdown logic
 | 
|---|
| 229 | //=============================================================================================
 | 
|---|
| 230 | 
 | 
|---|
| 231 | // Startup routine to activate preemption
 | 
|---|
| 232 | // Called from kernel_startup
 | 
|---|
| 233 | void kernel_start_preemption() {
 | 
|---|
| 234 |         __cfaabi_dbg_print_safe("Kernel : Starting preemption\n");
 | 
|---|
| 235 | 
 | 
|---|
| 236 |         // Start with preemption disabled until ready
 | 
|---|
| 237 |         disable_preempt_count = 1;
 | 
|---|
| 238 | 
 | 
|---|
| 239 |         // Initialize the event kernel
 | 
|---|
| 240 |         event_kernel = (event_kernel_t *)&storage_event_kernel;
 | 
|---|
| 241 |         (*event_kernel){};
 | 
|---|
| 242 | 
 | 
|---|
| 243 |         // Setup proper signal handlers
 | 
|---|
| 244 |         __kernel_sigaction( SIGUSR1, sigHandler_ctxSwitch, SA_SIGINFO | SA_RESTART );         // CtxSwitch handler
 | 
|---|
| 245 |         // __kernel_sigaction( SIGSEGV, sigHandler_segv     , SA_SIGINFO );      // Failure handler
 | 
|---|
| 246 |         // __kernel_sigaction( SIGBUS , sigHandler_segv     , SA_SIGINFO );      // Failure handler
 | 
|---|
| 247 | 
 | 
|---|
| 248 |         signal_block( SIGALRM );
 | 
|---|
| 249 | 
 | 
|---|
| 250 |         pthread_create( &alarm_thread, NULL, alarm_loop, NULL );
 | 
|---|
| 251 | }
 | 
|---|
| 252 | 
 | 
|---|
| 253 | // Shutdown routine to deactivate preemption
 | 
|---|
| 254 | // Called from kernel_shutdown
 | 
|---|
| 255 | void kernel_stop_preemption() {
 | 
|---|
| 256 |         __cfaabi_dbg_print_safe("Kernel : Preemption stopping\n");
 | 
|---|
| 257 | 
 | 
|---|
| 258 |         // Block all signals since we are already shutting down
 | 
|---|
| 259 |         sigset_t mask;
 | 
|---|
| 260 |         sigfillset( &mask );
 | 
|---|
| 261 |         sigprocmask( SIG_BLOCK, &mask, NULL );
 | 
|---|
| 262 | 
 | 
|---|
| 263 |         // Notify the alarm thread of the shutdown
 | 
|---|
| 264 |         sigval val = { 1 };
 | 
|---|
| 265 |         pthread_sigqueue( alarm_thread, SIGALRM, val );
 | 
|---|
| 266 | 
 | 
|---|
| 267 |         // Wait for the preemption thread to finish
 | 
|---|
| 268 |         pthread_join( alarm_thread, NULL );
 | 
|---|
| 269 | 
 | 
|---|
| 270 |         // Preemption is now fully stopped
 | 
|---|
| 271 | 
 | 
|---|
| 272 |         __cfaabi_dbg_print_safe("Kernel : Preemption stopped\n");
 | 
|---|
| 273 | }
 | 
|---|
| 274 | 
 | 
|---|
| 275 | // Raii ctor/dtor for the preemption_scope
 | 
|---|
| 276 | // Used by thread to control when they want to receive preemption signals
 | 
|---|
| 277 | void ?{}( preemption_scope & this, processor * proc ) {
 | 
|---|
| 278 |         (this.alarm){ proc, zero_time, zero_time };
 | 
|---|
| 279 |         this.proc = proc;
 | 
|---|
| 280 |         this.proc->preemption_alarm = &this.alarm;
 | 
|---|
| 281 | 
 | 
|---|
| 282 |         update_preemption( this.proc, from_us(this.proc->cltr->preemption) );
 | 
|---|
| 283 | }
 | 
|---|
| 284 | 
 | 
|---|
| 285 | void ^?{}( preemption_scope & this ) {
 | 
|---|
| 286 |         disable_interrupts();
 | 
|---|
| 287 | 
 | 
|---|
| 288 |         update_preemption( this.proc, zero_time );
 | 
|---|
| 289 | }
 | 
|---|
| 290 | 
 | 
|---|
| 291 | //=============================================================================================
 | 
|---|
| 292 | // Kernel Signal Handlers
 | 
|---|
| 293 | //=============================================================================================
 | 
|---|
| 294 | 
 | 
|---|
| 295 | // Context switch signal handler
 | 
|---|
| 296 | // Receives SIGUSR1 signal and causes the current thread to yield
 | 
|---|
| 297 | void sigHandler_ctxSwitch( __CFA_SIGPARMS__ ) {
 | 
|---|
| 298 |         __cfaabi_dbg_debug_do( last_interrupt = (void *)(cxt->uc_mcontext.gregs[CFA_REG_IP]); )
 | 
|---|
| 299 | 
 | 
|---|
| 300 |         // Check if it is safe to preempt here
 | 
|---|
| 301 |         if( !preemption_ready() ) { return; }
 | 
|---|
| 302 | 
 | 
|---|
| 303 |         preemption_in_progress = true;                      // Sync flag : prevent recursive calls to the signal handler
 | 
|---|
| 304 |         signal_unblock( SIGUSR1 );                          // We are about to CtxSwitch out of the signal handler, let other handlers in
 | 
|---|
| 305 |         preemption_in_progress = false;                     // Clear the in progress flag
 | 
|---|
| 306 | 
 | 
|---|
| 307 |         // Preemption can occur here
 | 
|---|
| 308 | 
 | 
|---|
| 309 |         BlockInternal( (thread_desc*)this_thread );         // Do the actual CtxSwitch
 | 
|---|
| 310 | }
 | 
|---|
| 311 | 
 | 
|---|
| 312 | // Main of the alarm thread
 | 
|---|
| 313 | // Waits on SIGALRM and send SIGUSR1 to whom ever needs it
 | 
|---|
| 314 | void * alarm_loop( __attribute__((unused)) void * args ) {
 | 
|---|
| 315 |         // Block sigalrms to control when they arrive
 | 
|---|
| 316 |         sigset_t mask;
 | 
|---|
| 317 |         sigemptyset( &mask );
 | 
|---|
| 318 |         sigaddset( &mask, SIGALRM );
 | 
|---|
| 319 | 
 | 
|---|
| 320 |         if ( pthread_sigmask( SIG_BLOCK, &mask, NULL ) == -1 ) {
 | 
|---|
| 321 |             abortf( "internal error, pthread_sigmask" );
 | 
|---|
| 322 |         }
 | 
|---|
| 323 | 
 | 
|---|
| 324 |         // Main loop
 | 
|---|
| 325 |         while( true ) {
 | 
|---|
| 326 |                 // Wait for a sigalrm
 | 
|---|
| 327 |                 siginfo_t info;
 | 
|---|
| 328 |                 int sig = sigwaitinfo( &mask, &info );
 | 
|---|
| 329 | 
 | 
|---|
| 330 |                 if( sig < 0 ) {
 | 
|---|
| 331 |                         //Error!
 | 
|---|
| 332 |                         int err = errno;
 | 
|---|
| 333 |                         switch( err ) {
 | 
|---|
| 334 |                                 case EAGAIN :
 | 
|---|
| 335 |                                 case EINTR :
 | 
|---|
| 336 |                                         continue;
 | 
|---|
| 337 |                         case EINVAL :
 | 
|---|
| 338 |                                         abortf("Timeout was invalid.");
 | 
|---|
| 339 |                                 default:
 | 
|---|
| 340 |                                         abortf("Unhandled error %d", err);
 | 
|---|
| 341 |                         }
 | 
|---|
| 342 |                 }
 | 
|---|
| 343 | 
 | 
|---|
| 344 |                 // If another signal arrived something went wrong
 | 
|---|
| 345 |                 assertf(sig == SIGALRM, "Kernel Internal Error, sigwait: Unexpected signal %d (%d : %d)\n", sig, info.si_code, info.si_value.sival_int);
 | 
|---|
| 346 | 
 | 
|---|
| 347 |                 // __cfaabi_dbg_print_safe("Kernel : Caught alarm from %d with %d\n", info.si_code, info.si_value.sival_int );
 | 
|---|
| 348 |                 // Switch on the code (a.k.a. the sender) to
 | 
|---|
| 349 |                 switch( info.si_code )
 | 
|---|
| 350 |                 {
 | 
|---|
| 351 |                 // Timers can apparently be marked as sent for the kernel
 | 
|---|
| 352 |                 // In either case, tick preemption
 | 
|---|
| 353 |                 case SI_TIMER:
 | 
|---|
| 354 |                 case SI_KERNEL:
 | 
|---|
| 355 |                         // __cfaabi_dbg_print_safe("Kernel : Preemption thread tick\n");
 | 
|---|
| 356 |                         lock( event_kernel->lock __cfaabi_dbg_ctx2 );
 | 
|---|
| 357 |                         tick_preemption();
 | 
|---|
| 358 |                         unlock( event_kernel->lock );
 | 
|---|
| 359 |                         break;
 | 
|---|
| 360 |                 // Signal was not sent by the kernel but by an other thread
 | 
|---|
| 361 |                 case SI_QUEUE:
 | 
|---|
| 362 |                         // For now, other thread only signal the alarm thread to shut it down
 | 
|---|
| 363 |                         // If this needs to change use info.si_value and handle the case here
 | 
|---|
| 364 |                         goto EXIT;
 | 
|---|
| 365 |                 }
 | 
|---|
| 366 |         }
 | 
|---|
| 367 | 
 | 
|---|
| 368 | EXIT:
 | 
|---|
| 369 |         __cfaabi_dbg_print_safe("Kernel : Preemption thread stopping\n");
 | 
|---|
| 370 |         return NULL;
 | 
|---|
| 371 | }
 | 
|---|
| 372 | 
 | 
|---|
| 373 | // Sigaction wrapper : register an signal handler
 | 
|---|
| 374 | static void __kernel_sigaction( int sig, void (*handler)(__CFA_SIGPARMS__), int flags ) {
 | 
|---|
| 375 |         struct sigaction act;
 | 
|---|
| 376 | 
 | 
|---|
| 377 |         act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;
 | 
|---|
| 378 |         act.sa_flags = flags;
 | 
|---|
| 379 | 
 | 
|---|
| 380 |         if ( sigaction( sig, &act, NULL ) == -1 ) {
 | 
|---|
| 381 |                 __cfaabi_dbg_print_buffer_decl(
 | 
|---|
| 382 |                         " __kernel_sigaction( sig:%d, handler:%p, flags:%d ), problem installing signal handler, error(%d) %s.\n",
 | 
|---|
| 383 |                         sig, handler, flags, errno, strerror( errno )
 | 
|---|
| 384 |                 );
 | 
|---|
| 385 |                 _exit( EXIT_FAILURE );
 | 
|---|
| 386 |         }
 | 
|---|
| 387 | }
 | 
|---|
| 388 | 
 | 
|---|
| 389 | // Sigaction wrapper : restore default handler
 | 
|---|
| 390 | static void __kernel_sigdefault( int sig ) {
 | 
|---|
| 391 |         struct sigaction act;
 | 
|---|
| 392 | 
 | 
|---|
| 393 |         act.sa_handler = SIG_DFL;
 | 
|---|
| 394 |         act.sa_flags = 0;
 | 
|---|
| 395 |         sigemptyset( &act.sa_mask );
 | 
|---|
| 396 | 
 | 
|---|
| 397 |         if ( sigaction( sig, &act, NULL ) == -1 ) {
 | 
|---|
| 398 |                 __cfaabi_dbg_print_buffer_decl(
 | 
|---|
| 399 |                         " __kernel_sigdefault( sig:%d ), problem reseting signal handler, error(%d) %s.\n",
 | 
|---|
| 400 |                         sig, errno, strerror( errno )
 | 
|---|
| 401 |                 );
 | 
|---|
| 402 |                 _exit( EXIT_FAILURE );
 | 
|---|
| 403 |         }
 | 
|---|
| 404 | }
 | 
|---|
| 405 | 
 | 
|---|
| 406 | //=============================================================================================
 | 
|---|
| 407 | // Terminating Signals logic
 | 
|---|
| 408 | //=============================================================================================
 | 
|---|
| 409 | 
 | 
|---|
| 410 | __cfaabi_dbg_debug_do(
 | 
|---|
| 411 |         static void __kernel_backtrace( int start ) {
 | 
|---|
| 412 |                 // skip first N stack frames
 | 
|---|
| 413 | 
 | 
|---|
| 414 |                 enum { Frames = 50 };
 | 
|---|
| 415 |                 void * array[Frames];
 | 
|---|
| 416 |                 int size = backtrace( array, Frames );
 | 
|---|
| 417 |                 char ** messages = backtrace_symbols( array, size );
 | 
|---|
| 418 | 
 | 
|---|
| 419 |                 // find executable name
 | 
|---|
| 420 |                 *index( messages[0], '(' ) = '\0';
 | 
|---|
| 421 |                 #ifdef __USE_STREAM__
 | 
|---|
| 422 |                 serr | "Stack back trace for:" | messages[0] | endl;
 | 
|---|
| 423 |                 #else
 | 
|---|
| 424 |                 fprintf( stderr, "Stack back trace for: %s\n", messages[0]);
 | 
|---|
| 425 |                 #endif
 | 
|---|
| 426 | 
 | 
|---|
| 427 |                 // skip last 2 stack frames after main
 | 
|---|
| 428 |                 for ( int i = start; i < size && messages != NULL; i += 1 ) {
 | 
|---|
| 429 |                         char * name = NULL;
 | 
|---|
| 430 |                         char * offset_begin = NULL;
 | 
|---|
| 431 |                         char * offset_end = NULL;
 | 
|---|
| 432 | 
 | 
|---|
| 433 |                         for ( char *p = messages[i]; *p; ++p ) {
 | 
|---|
| 434 |                                 // find parantheses and +offset
 | 
|---|
| 435 |                                 if ( *p == '(' ) {
 | 
|---|
| 436 |                                         name = p;
 | 
|---|
| 437 |                                 }
 | 
|---|
| 438 |                                 else if ( *p == '+' ) {
 | 
|---|
| 439 |                                         offset_begin = p;
 | 
|---|
| 440 |                                 }
 | 
|---|
| 441 |                                 else if ( *p == ')' ) {
 | 
|---|
| 442 |                                         offset_end = p;
 | 
|---|
| 443 |                                         break;
 | 
|---|
| 444 |                                 }
 | 
|---|
| 445 |                         }
 | 
|---|
| 446 | 
 | 
|---|
| 447 |                         // if line contains symbol print it
 | 
|---|
| 448 |                         int frameNo = i - start;
 | 
|---|
| 449 |                         if ( name && offset_begin && offset_end && name < offset_begin ) {
 | 
|---|
| 450 |                                 // delimit strings
 | 
|---|
| 451 |                                 *name++ = '\0';
 | 
|---|
| 452 |                                 *offset_begin++ = '\0';
 | 
|---|
| 453 |                                 *offset_end++ = '\0';
 | 
|---|
| 454 | 
 | 
|---|
| 455 |                                 #ifdef __USE_STREAM__
 | 
|---|
| 456 |                                 serr    | "("  | frameNo | ")" | messages[i] | ":"
 | 
|---|
| 457 |                                         | name | "+" | offset_begin | offset_end | endl;
 | 
|---|
| 458 |                                 #else
 | 
|---|
| 459 |                                 fprintf( stderr, "(%i) %s : %s + %s %s\n", frameNo, messages[i], name, offset_begin, offset_end);
 | 
|---|
| 460 |                                 #endif
 | 
|---|
| 461 |                         }
 | 
|---|
| 462 |                         // otherwise, print the whole line
 | 
|---|
| 463 |                         else {
 | 
|---|
| 464 |                                 #ifdef __USE_STREAM__
 | 
|---|
| 465 |                                 serr | "(" | frameNo | ")" | messages[i] | endl;
 | 
|---|
| 466 |                                 #else
 | 
|---|
| 467 |                                 fprintf( stderr, "(%i) %s\n", frameNo, messages[i] );
 | 
|---|
| 468 |                                 #endif
 | 
|---|
| 469 |                         }
 | 
|---|
| 470 |                 }
 | 
|---|
| 471 | 
 | 
|---|
| 472 |                 free( messages );
 | 
|---|
| 473 |         }
 | 
|---|
| 474 | )
 | 
|---|
| 475 | 
 | 
|---|
| 476 | // void sigHandler_segv( __CFA_SIGPARMS__ ) {
 | 
|---|
| 477 | //      __cfaabi_dbg_debug_do(
 | 
|---|
| 478 | //              #ifdef __USE_STREAM__
 | 
|---|
| 479 | //              serr    | "*CFA runtime error* program cfa-cpp terminated with"
 | 
|---|
| 480 | //                      | (sig == SIGSEGV ? "segment fault." : "bus error.")
 | 
|---|
| 481 | //                      | endl;
 | 
|---|
| 482 | //              #else
 | 
|---|
| 483 | //              fprintf( stderr, "*CFA runtime error* program cfa-cpp terminated with %s\n", sig == SIGSEGV ? "segment fault." : "bus error." );
 | 
|---|
| 484 | //              #endif
 | 
|---|
| 485 | 
 | 
|---|
| 486 | //              // skip first 2 stack frames
 | 
|---|
| 487 | //              __kernel_backtrace( 1 );
 | 
|---|
| 488 | //      )
 | 
|---|
| 489 | //      exit( EXIT_FAILURE );
 | 
|---|
| 490 | // }
 | 
|---|
| 491 | 
 | 
|---|
| 492 | // void sigHandler_abort( __CFA_SIGPARMS__ ) {
 | 
|---|
| 493 | //      // skip first 6 stack frames
 | 
|---|
| 494 | //      __cfaabi_dbg_debug_do( __kernel_backtrace( 6 ); )
 | 
|---|
| 495 | 
 | 
|---|
| 496 | //      // reset default signal handler
 | 
|---|
| 497 | //      __kernel_sigdefault( SIGABRT );
 | 
|---|
| 498 | 
 | 
|---|
| 499 | //      raise( SIGABRT );
 | 
|---|
| 500 | // }
 | 
|---|
| 501 | 
 | 
|---|
| 502 | // Local Variables: //
 | 
|---|
| 503 | // mode: c //
 | 
|---|
| 504 | // tab-width: 4 //
 | 
|---|
| 505 | // End: //
 | 
|---|