[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 |
---|
[16cc9f7] | 12 | // Last Modified On : Fri Apr 22 18:20:26 2022 |
---|
| 13 | // Update Count : 13 |
---|
[9d944b2] | 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 | |
---|
[ec72861] | 23 | enum { buffer_size = 4096 }; |
---|
[9d944b2] | 24 | static char buffer[ buffer_size ]; |
---|
| 25 | |
---|
| 26 | extern "C" { |
---|
[e3fea42] | 27 | void __cfaabi_bits_write( int fd, const char in_buffer[], int len ) { |
---|
[9d944b2] | 28 | // ensure all data is written |
---|
[6a5be52] | 29 | for ( int count = 0, retcode; count < len; count += retcode ) { |
---|
[9d944b2] | 30 | in_buffer += count; |
---|
| 31 | |
---|
| 32 | for ( ;; ) { |
---|
[1c40091] | 33 | retcode = write( fd, in_buffer, len - count ); |
---|
[9d944b2] | 34 | |
---|
| 35 | // not a timer interrupt ? |
---|
[6a5be52] | 36 | if ( retcode != -1 || errno != EINTR ) break; |
---|
[9d944b2] | 37 | } |
---|
| 38 | |
---|
| 39 | if ( retcode == -1 ) _exit( EXIT_FAILURE ); |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | |
---|
[1c40091] | 43 | void __cfaabi_bits_acquire() __attribute__((__weak__)) {} |
---|
| 44 | void __cfaabi_bits_release() __attribute__((__weak__)) {} |
---|
[9d944b2] | 45 | |
---|
[16cc9f7] | 46 | int __cfaabi_bits_print_safe ( int fd, const char fmt[], ... ) __attribute__(( format(printf, 2, 3) )) { |
---|
[9d944b2] | 47 | va_list args; |
---|
| 48 | |
---|
| 49 | va_start( args, fmt ); |
---|
[1c40091] | 50 | __cfaabi_bits_acquire(); |
---|
[6a5be52] | 51 | |
---|
[9d944b2] | 52 | int len = vsnprintf( buffer, buffer_size, fmt, args ); |
---|
[1c40091] | 53 | __cfaabi_bits_write( fd, buffer, len ); |
---|
[9d944b2] | 54 | |
---|
[1c40091] | 55 | __cfaabi_bits_release(); |
---|
[9d944b2] | 56 | va_end( args ); |
---|
[16cc9f7] | 57 | return len; |
---|
[9d944b2] | 58 | } |
---|
| 59 | |
---|
[16cc9f7] | 60 | int __cfaabi_bits_print_nolock( int fd, const char fmt[], ... ) __attribute__(( format(printf, 2, 3) )) { |
---|
[9d944b2] | 61 | va_list args; |
---|
| 62 | |
---|
| 63 | va_start( args, fmt ); |
---|
[6a5be52] | 64 | |
---|
[9d944b2] | 65 | int len = vsnprintf( buffer, buffer_size, fmt, args ); |
---|
[1c40091] | 66 | __cfaabi_bits_write( fd, buffer, len ); |
---|
[9d944b2] | 67 | |
---|
| 68 | va_end( args ); |
---|
[16cc9f7] | 69 | return len; |
---|
[9d944b2] | 70 | } |
---|
| 71 | |
---|
[16cc9f7] | 72 | int __cfaabi_bits_print_vararg( int fd, const char fmt[], va_list args ) { |
---|
[9d944b2] | 73 | int len = vsnprintf( buffer, buffer_size, fmt, args ); |
---|
[1c40091] | 74 | __cfaabi_bits_write( fd, buffer, len ); |
---|
[16cc9f7] | 75 | return len; |
---|
[9d944b2] | 76 | } |
---|
| 77 | |
---|
[16cc9f7] | 78 | int __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 ); |
---|
[16cc9f7] | 87 | return len; |
---|
[9d944b2] | 88 | } |
---|
[169d944] | 89 | } |
---|