| [9d944b2] | 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 | //
 | 
|---|
| [c2b9f21] | 7 | // debug.c --
 | 
|---|
| [9d944b2] | 8 | //
 | 
|---|
 | 9 | // Author           : Thierry Delisle
 | 
|---|
 | 10 | // Created On       : Thu Mar 30 12:30:01 2017
 | 
|---|
| [ec72861] | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| [1c40091] | 12 | // Last Modified On : Thu Nov 21 17:16:30 2019
 | 
|---|
 | 13 | // Update Count     : 10
 | 
|---|
| [9d944b2] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | extern "C" {
 | 
|---|
 | 17 | #include <stdio.h>
 | 
|---|
 | 18 | #include <stdlib.h>
 | 
|---|
 | 19 | #include <string.h>
 | 
|---|
 | 20 | #include <errno.h>
 | 
|---|
 | 21 | #include <stdarg.h>
 | 
|---|
 | 22 | #include <unistd.h>
 | 
|---|
 | 23 | }
 | 
|---|
 | 24 | 
 | 
|---|
| [ec72861] | 25 | enum { buffer_size = 4096 };
 | 
|---|
| [9d944b2] | 26 | static char buffer[ buffer_size ];
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | extern "C" {
 | 
|---|
 | 29 | 
 | 
|---|
| [1c40091] | 30 |         void __cfaabi_bits_write( int fd, const char *in_buffer, int len ) {
 | 
|---|
| [9d944b2] | 31 |                 // ensure all data is written
 | 
|---|
| [6a5be52] | 32 |                 for ( int count = 0, retcode; count < len; count += retcode ) {
 | 
|---|
| [9d944b2] | 33 |                         in_buffer += count;
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 |                         for ( ;; ) {
 | 
|---|
| [1c40091] | 36 |                                 retcode = write( fd, in_buffer, len - count );
 | 
|---|
| [9d944b2] | 37 | 
 | 
|---|
 | 38 |                                 // not a timer interrupt ?
 | 
|---|
| [6a5be52] | 39 |                                 if ( retcode != -1 || errno != EINTR ) break;
 | 
|---|
| [9d944b2] | 40 |                         }
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 |                         if ( retcode == -1 ) _exit( EXIT_FAILURE );
 | 
|---|
 | 43 |                 }
 | 
|---|
 | 44 |         }
 | 
|---|
 | 45 | 
 | 
|---|
| [1c40091] | 46 |         void __cfaabi_bits_acquire() __attribute__((__weak__)) {}
 | 
|---|
 | 47 |         void __cfaabi_bits_release() __attribute__((__weak__)) {}
 | 
|---|
| [9d944b2] | 48 | 
 | 
|---|
| [1c40091] | 49 |         void __cfaabi_bits_print_safe  ( int fd, const char fmt[], ... ) __attribute__(( format(printf, 2, 3) )) {
 | 
|---|
| [9d944b2] | 50 |                 va_list args;
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 |                 va_start( args, fmt );
 | 
|---|
| [1c40091] | 53 |                 __cfaabi_bits_acquire();
 | 
|---|
| [6a5be52] | 54 | 
 | 
|---|
| [9d944b2] | 55 |                 int len = vsnprintf( buffer, buffer_size, fmt, args );
 | 
|---|
| [1c40091] | 56 |                 __cfaabi_bits_write( fd, buffer, len );
 | 
|---|
| [9d944b2] | 57 | 
 | 
|---|
| [1c40091] | 58 |                 __cfaabi_bits_release();
 | 
|---|
| [9d944b2] | 59 |                 va_end( args );
 | 
|---|
 | 60 |         }
 | 
|---|
 | 61 | 
 | 
|---|
| [1c40091] | 62 |         void __cfaabi_bits_print_nolock( int fd, const char fmt[], ... ) __attribute__(( format(printf, 2, 3) )) {
 | 
|---|
| [9d944b2] | 63 |                 va_list args;
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 |                 va_start( args, fmt );
 | 
|---|
| [6a5be52] | 66 | 
 | 
|---|
| [9d944b2] | 67 |                 int len = vsnprintf( buffer, buffer_size, fmt, args );
 | 
|---|
| [1c40091] | 68 |                 __cfaabi_bits_write( fd, buffer, len );
 | 
|---|
| [9d944b2] | 69 | 
 | 
|---|
 | 70 |                 va_end( args );
 | 
|---|
 | 71 |         }
 | 
|---|
 | 72 | 
 | 
|---|
| [1c40091] | 73 |         void __cfaabi_bits_print_vararg( int fd, const char fmt[], va_list args ) {
 | 
|---|
| [9d944b2] | 74 |                 int len = vsnprintf( buffer, buffer_size, fmt, args );
 | 
|---|
| [1c40091] | 75 |                 __cfaabi_bits_write( fd, buffer, len );
 | 
|---|
| [9d944b2] | 76 |         }
 | 
|---|
 | 77 | 
 | 
|---|
| [1c40091] | 78 |         void __cfaabi_bits_print_buffer( int fd, char in_buffer[], int in_buffer_size, const char fmt[], ... ) __attribute__(( format(printf, 4, 5) )) {
 | 
|---|
| [9d944b2] | 79 |                 va_list args;
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 |                 va_start( args, fmt );
 | 
|---|
| [6a5be52] | 82 | 
 | 
|---|
| [9d944b2] | 83 |                 int len = vsnprintf( in_buffer, in_buffer_size, fmt, args );
 | 
|---|
| [1c40091] | 84 |                 __cfaabi_bits_write( fd, in_buffer, len );
 | 
|---|
| [9d944b2] | 85 | 
 | 
|---|
 | 86 |                 va_end( args );
 | 
|---|
 | 87 |         }
 | 
|---|
| [169d944] | 88 | }
 | 
|---|