[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 | |
---|
[4bb5d36] | 23 | #include "bits/defs.hfa" |
---|
| 24 | |
---|
[ec72861] | 25 | enum { buffer_size = 4096 }; |
---|
[9d944b2] | 26 | static char buffer[ buffer_size ]; |
---|
| 27 | |
---|
| 28 | extern "C" { |
---|
[4bb5d36] | 29 | // would be cool to remove libcfa_public but it's needed for libcfathread |
---|
| 30 | void __cfaabi_bits_write( int fd, const char in_buffer[], int len ) libcfa_public { |
---|
[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 | |
---|
[4bb5d36] | 49 | // would be cool to remove libcfa_public but it's needed for libcfathread |
---|
| 50 | int __cfaabi_bits_print_safe ( int fd, const char fmt[], ... ) __attribute__(( format(printf, 2, 3) )) libcfa_public { |
---|
[9d944b2] | 51 | va_list args; |
---|
| 52 | |
---|
| 53 | va_start( args, fmt ); |
---|
[1c40091] | 54 | __cfaabi_bits_acquire(); |
---|
[6a5be52] | 55 | |
---|
[9d944b2] | 56 | int len = vsnprintf( buffer, buffer_size, fmt, args ); |
---|
[1c40091] | 57 | __cfaabi_bits_write( fd, buffer, len ); |
---|
[9d944b2] | 58 | |
---|
[1c40091] | 59 | __cfaabi_bits_release(); |
---|
[9d944b2] | 60 | va_end( args ); |
---|
[16cc9f7] | 61 | return len; |
---|
[9d944b2] | 62 | } |
---|
| 63 | |
---|
[16cc9f7] | 64 | int __cfaabi_bits_print_nolock( int fd, const char fmt[], ... ) __attribute__(( format(printf, 2, 3) )) { |
---|
[9d944b2] | 65 | va_list args; |
---|
| 66 | |
---|
| 67 | va_start( args, fmt ); |
---|
[6a5be52] | 68 | |
---|
[9d944b2] | 69 | int len = vsnprintf( buffer, buffer_size, fmt, args ); |
---|
[1c40091] | 70 | __cfaabi_bits_write( fd, buffer, len ); |
---|
[9d944b2] | 71 | |
---|
| 72 | va_end( args ); |
---|
[16cc9f7] | 73 | return len; |
---|
[9d944b2] | 74 | } |
---|
| 75 | |
---|
[16cc9f7] | 76 | int __cfaabi_bits_print_vararg( int fd, const char fmt[], va_list args ) { |
---|
[9d944b2] | 77 | int len = vsnprintf( buffer, buffer_size, fmt, args ); |
---|
[1c40091] | 78 | __cfaabi_bits_write( fd, buffer, len ); |
---|
[16cc9f7] | 79 | return len; |
---|
[9d944b2] | 80 | } |
---|
| 81 | |
---|
[16cc9f7] | 82 | int __cfaabi_bits_print_buffer( int fd, char in_buffer[], int in_buffer_size, const char fmt[], ... ) __attribute__(( format(printf, 4, 5) )) { |
---|
[9d944b2] | 83 | va_list args; |
---|
| 84 | |
---|
| 85 | va_start( args, fmt ); |
---|
[6a5be52] | 86 | |
---|
[9d944b2] | 87 | int len = vsnprintf( in_buffer, in_buffer_size, fmt, args ); |
---|
[1c40091] | 88 | __cfaabi_bits_write( fd, in_buffer, len ); |
---|
[9d944b2] | 89 | |
---|
| 90 | va_end( args ); |
---|
[16cc9f7] | 91 | return len; |
---|
[9d944b2] | 92 | } |
---|
[169d944] | 93 | } |
---|