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