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 : Fri Apr 22 18:20:26 2022 |
---|
13 | // Update Count : 13 |
---|
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 | #include "bits/defs.hfa" |
---|
24 | |
---|
25 | enum { buffer_size = 4096 }; |
---|
26 | static char buffer[ buffer_size ]; |
---|
27 | |
---|
28 | extern "C" { |
---|
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 { |
---|
31 | // ensure all data is written |
---|
32 | for ( int count = 0, retcode; count < len; count += retcode ) { |
---|
33 | in_buffer += count; |
---|
34 | |
---|
35 | for ( ;; ) { |
---|
36 | retcode = write( fd, in_buffer, len - count ); |
---|
37 | |
---|
38 | // not a timer interrupt ? |
---|
39 | if ( retcode != -1 || errno != EINTR ) break; |
---|
40 | } |
---|
41 | |
---|
42 | if ( retcode == -1 ) _exit( EXIT_FAILURE ); |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | void __cfaabi_bits_acquire() __attribute__((__weak__)) {} |
---|
47 | void __cfaabi_bits_release() __attribute__((__weak__)) {} |
---|
48 | |
---|
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 { |
---|
51 | va_list args; |
---|
52 | |
---|
53 | va_start( args, fmt ); |
---|
54 | __cfaabi_bits_acquire(); |
---|
55 | |
---|
56 | int len = vsnprintf( buffer, buffer_size, fmt, args ); |
---|
57 | __cfaabi_bits_write( fd, buffer, len ); |
---|
58 | |
---|
59 | __cfaabi_bits_release(); |
---|
60 | va_end( args ); |
---|
61 | return len; |
---|
62 | } |
---|
63 | |
---|
64 | int __cfaabi_bits_print_nolock( int fd, const char fmt[], ... ) __attribute__(( format(printf, 2, 3) )) { |
---|
65 | va_list args; |
---|
66 | |
---|
67 | va_start( args, fmt ); |
---|
68 | |
---|
69 | int len = vsnprintf( buffer, buffer_size, fmt, args ); |
---|
70 | __cfaabi_bits_write( fd, buffer, len ); |
---|
71 | |
---|
72 | va_end( args ); |
---|
73 | return len; |
---|
74 | } |
---|
75 | |
---|
76 | int __cfaabi_bits_print_vararg( int fd, const char fmt[], va_list args ) { |
---|
77 | int len = vsnprintf( buffer, buffer_size, fmt, args ); |
---|
78 | __cfaabi_bits_write( fd, buffer, len ); |
---|
79 | return len; |
---|
80 | } |
---|
81 | |
---|
82 | int __cfaabi_bits_print_buffer( int fd, char in_buffer[], int in_buffer_size, const char fmt[], ... ) __attribute__(( format(printf, 4, 5) )) { |
---|
83 | va_list args; |
---|
84 | |
---|
85 | va_start( args, fmt ); |
---|
86 | |
---|
87 | int len = vsnprintf( in_buffer, in_buffer_size, fmt, args ); |
---|
88 | __cfaabi_bits_write( fd, in_buffer, len ); |
---|
89 | |
---|
90 | va_end( args ); |
---|
91 | return len; |
---|
92 | } |
---|
93 | } |
---|