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