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