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