Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * kmp_io.c -- RTL IO |
| 3 | * $Revision: 42150 $ |
| 4 | * $Date: 2013-03-15 15:40:38 -0500 (Fri, 15 Mar 2013) $ |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // The LLVM Compiler Infrastructure |
| 11 | // |
| 12 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 13 | // Source Licenses. See LICENSE.txt for details. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <stddef.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <string.h> |
| 23 | #ifndef __ABSOFT_WIN |
| 24 | # include <sys/types.h> |
| 25 | #endif |
| 26 | |
| 27 | #include "kmp_os.h" |
| 28 | #include "kmp_lock.h" |
| 29 | #include "kmp_str.h" |
| 30 | #include "kmp_io.h" |
| 31 | #include "kmp.h" // KMP_GTID_DNE, __kmp_debug_buf, etc |
| 32 | |
| 33 | #if KMP_OS_WINDOWS |
| 34 | # pragma warning( push ) |
| 35 | # pragma warning( disable: 271 310 ) |
| 36 | # include <windows.h> |
| 37 | # pragma warning( pop ) |
| 38 | #endif |
| 39 | |
| 40 | /* ------------------------------------------------------------------------ */ |
| 41 | /* ------------------------------------------------------------------------ */ |
| 42 | |
| 43 | kmp_bootstrap_lock_t __kmp_stdio_lock = KMP_BOOTSTRAP_LOCK_INITIALIZER( __kmp_stdio_lock ); /* Control stdio functions */ |
| 44 | kmp_bootstrap_lock_t __kmp_console_lock = KMP_BOOTSTRAP_LOCK_INITIALIZER( __kmp_console_lock ); /* Control console initialization */ |
| 45 | |
| 46 | #if KMP_OS_WINDOWS |
| 47 | |
| 48 | # ifdef KMP_DEBUG |
| 49 | /* __kmp_stdout is used only for dev build */ |
| 50 | static HANDLE __kmp_stdout = NULL; |
| 51 | # endif |
| 52 | static HANDLE __kmp_stderr = NULL; |
| 53 | static int __kmp_console_exists = FALSE; |
| 54 | static kmp_str_buf_t __kmp_console_buf; |
| 55 | |
| 56 | static int |
| 57 | is_console( void ) |
| 58 | { |
| 59 | char buffer[ 128 ]; |
| 60 | DWORD rc = 0; |
| 61 | DWORD err = 0; |
| 62 | // Try to get console title. |
| 63 | SetLastError( 0 ); |
| 64 | // GetConsoleTitle does not reset last error in case of success or short buffer, |
| 65 | // so we need to clear it explicitly. |
| 66 | rc = GetConsoleTitle( buffer, sizeof( buffer ) ); |
| 67 | if ( rc == 0 ) { |
| 68 | // rc == 0 means getting console title failed. Let us find out why. |
| 69 | err = GetLastError(); |
| 70 | // err == 0 means buffer too short (we suppose console exists). |
| 71 | // In Window applications we usually have err == 6 (invalid handle). |
| 72 | }; // if |
| 73 | return rc > 0 || err == 0; |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | __kmp_close_console( void ) |
| 78 | { |
| 79 | /* wait until user presses return before closing window */ |
| 80 | /* TODO only close if a window was opened */ |
| 81 | if( __kmp_console_exists ) { |
| 82 | #ifdef KMP_DEBUG |
| 83 | /* standard out is used only in dev build */ |
| 84 | __kmp_stdout = NULL; |
| 85 | #endif |
| 86 | __kmp_stderr = NULL; |
| 87 | __kmp_str_buf_free( &__kmp_console_buf ); |
| 88 | __kmp_console_exists = FALSE; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* For windows, call this before stdout, stderr, or stdin are used. |
| 93 | * It opens a console window and starts processing */ |
| 94 | static void |
| 95 | __kmp_redirect_output( void ) |
| 96 | { |
| 97 | __kmp_acquire_bootstrap_lock( &__kmp_console_lock ); |
| 98 | |
| 99 | if( ! __kmp_console_exists ) { |
| 100 | #ifdef KMP_DEBUG |
| 101 | /* standard out is used only in dev build */ |
| 102 | HANDLE ho; |
| 103 | #endif |
| 104 | HANDLE he; |
| 105 | |
| 106 | __kmp_str_buf_init( &__kmp_console_buf ); |
| 107 | |
| 108 | AllocConsole(); |
| 109 | // We do not check the result of AllocConsole because |
| 110 | // 1. the call is harmless |
| 111 | // 2. it is not clear how to communicate failue |
| 112 | // 3. we will detect failure later when we get handle(s) |
| 113 | |
| 114 | #ifdef KMP_DEBUG |
| 115 | ho = GetStdHandle( STD_OUTPUT_HANDLE ); |
| 116 | if ( ho == INVALID_HANDLE_VALUE || ho == NULL ) { |
| 117 | |
| 118 | DWORD err = GetLastError(); |
| 119 | // TODO: output error somehow (maybe message box) |
| 120 | __kmp_stdout = NULL; |
| 121 | |
| 122 | } else { |
| 123 | |
| 124 | __kmp_stdout = ho; // temporary code, need new global for ho |
| 125 | |
| 126 | } |
| 127 | #endif |
| 128 | he = GetStdHandle( STD_ERROR_HANDLE ); |
| 129 | if ( he == INVALID_HANDLE_VALUE || he == NULL ) { |
| 130 | |
| 131 | DWORD err = GetLastError(); |
| 132 | // TODO: output error somehow (maybe message box) |
| 133 | __kmp_stderr = NULL; |
| 134 | |
| 135 | } else { |
| 136 | |
| 137 | __kmp_stderr = he; // temporary code, need new global |
| 138 | } |
| 139 | __kmp_console_exists = TRUE; |
| 140 | } |
| 141 | __kmp_release_bootstrap_lock( &__kmp_console_lock ); |
| 142 | } |
| 143 | |
| 144 | #else |
| 145 | #define __kmp_stderr (stderr) |
| 146 | #endif /* KMP_OS_WINDOWS */ |
| 147 | |
| 148 | void |
| 149 | __kmp_vprintf( enum kmp_io __kmp_io, char const * format, va_list ap ) |
| 150 | { |
| 151 | #if KMP_OS_WINDOWS |
| 152 | if( !__kmp_console_exists ) { |
| 153 | __kmp_redirect_output(); |
| 154 | } |
| 155 | if( ! __kmp_stderr && __kmp_io == kmp_err ) { |
| 156 | return; |
| 157 | } |
| 158 | #ifdef KMP_DEBUG |
| 159 | if( ! __kmp_stdout && __kmp_io == kmp_out ) { |
| 160 | return; |
| 161 | } |
| 162 | #endif |
| 163 | #endif /* KMP_OS_WINDOWS */ |
| 164 | |
| 165 | if ( __kmp_debug_buf && __kmp_debug_buffer != NULL ) { |
| 166 | |
| 167 | int dc = ( __kmp_debug_buf_atomic ? |
| 168 | KMP_TEST_THEN_INC32( & __kmp_debug_count) : __kmp_debug_count++ ) |
| 169 | % __kmp_debug_buf_lines; |
| 170 | char *db = & __kmp_debug_buffer[ dc * __kmp_debug_buf_chars ]; |
| 171 | int chars = 0; |
| 172 | |
| 173 | #ifdef KMP_DEBUG_PIDS |
| 174 | chars = sprintf( db, "pid=%d: ", getpid() ); |
| 175 | #endif |
| 176 | chars += vsprintf( db, format, ap ); |
| 177 | |
| 178 | if ( chars + 1 > __kmp_debug_buf_chars ) { |
| 179 | if ( chars + 1 > __kmp_debug_buf_warn_chars ) { |
| 180 | #if KMP_OS_WINDOWS |
| 181 | DWORD count; |
| 182 | __kmp_str_buf_print( &__kmp_console_buf, |
| 183 | "OMP warning: Debugging buffer overflow; increase KMP_DEBUG_BUF_CHARS to %d\n", |
| 184 | chars + 1 ); |
| 185 | WriteFile( __kmp_stderr, __kmp_console_buf.str, __kmp_console_buf.used, &count, NULL ); |
| 186 | __kmp_str_buf_clear( &__kmp_console_buf ); |
| 187 | #else |
| 188 | fprintf( __kmp_stderr, |
| 189 | "OMP warning: Debugging buffer overflow; increase KMP_DEBUG_BUF_CHARS to %d\n", |
| 190 | chars + 1 ); |
| 191 | fflush( __kmp_stderr ); |
| 192 | #endif |
| 193 | __kmp_debug_buf_warn_chars = chars + 1; |
| 194 | } |
| 195 | /* terminate string if overflow occurred */ |
| 196 | db[ __kmp_debug_buf_chars - 2 ] = '\n'; |
| 197 | db[ __kmp_debug_buf_chars - 1 ] = '\0'; |
| 198 | } |
| 199 | } else { |
| 200 | #if KMP_OS_WINDOWS |
| 201 | DWORD count; |
| 202 | #ifdef KMP_DEBUG_PIDS |
| 203 | __kmp_str_buf_print( &__kmp_console_buf, "pid=%d: ", getpid() ); |
| 204 | #endif |
| 205 | __kmp_str_buf_vprint( &__kmp_console_buf, format, ap ); |
| 206 | WriteFile( |
| 207 | __kmp_stderr, |
| 208 | __kmp_console_buf.str, |
| 209 | __kmp_console_buf.used, |
| 210 | &count, |
| 211 | NULL |
| 212 | ); |
| 213 | __kmp_str_buf_clear( &__kmp_console_buf ); |
| 214 | #else |
| 215 | #ifdef KMP_DEBUG_PIDS |
| 216 | fprintf( __kmp_stderr, "pid=%d: ", getpid() ); |
| 217 | #endif |
| 218 | vfprintf( __kmp_stderr, format, ap ); |
| 219 | fflush( __kmp_stderr ); |
| 220 | #endif |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | __kmp_printf( char const * format, ... ) |
| 226 | { |
| 227 | va_list ap; |
| 228 | va_start( ap, format ); |
| 229 | |
| 230 | __kmp_acquire_bootstrap_lock( & __kmp_stdio_lock ); |
| 231 | __kmp_vprintf( kmp_err, format, ap ); |
| 232 | __kmp_release_bootstrap_lock( & __kmp_stdio_lock ); |
| 233 | |
| 234 | va_end( ap ); |
| 235 | } |
| 236 | |
| 237 | void |
| 238 | __kmp_printf_no_lock( char const * format, ... ) |
| 239 | { |
| 240 | va_list ap; |
| 241 | va_start( ap, format ); |
| 242 | |
| 243 | __kmp_vprintf( kmp_err, format, ap ); |
| 244 | |
| 245 | va_end( ap ); |
| 246 | } |
| 247 | |
| 248 | /* ------------------------------------------------------------------------ */ |
| 249 | /* ------------------------------------------------------------------------ */ |