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