5 //===----------------------------------------------------------------------===//
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
11 //===----------------------------------------------------------------------===//
19 #include <sys/types.h>
22 #include "kmp.h" // KMP_GTID_DNE, __kmp_debug_buf, etc
31 #pragma warning(disable : 271 310)
39 /* ------------------------------------------------------------------------ */
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 */
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
;
53 void __kmp_close_console(void) {
54 /* wait until user presses return before closing window */
55 /* TODO only close if a window was opened */
56 if (__kmp_console_exists
) {
59 __kmp_str_buf_free(&__kmp_console_buf
);
60 __kmp_console_exists
= FALSE
;
64 /* For windows, call this before stdout, stderr, or stdin are used.
65 It opens a console window and starts processing */
66 static void __kmp_redirect_output(void) {
67 __kmp_acquire_bootstrap_lock(&__kmp_console_lock
);
69 if (!__kmp_console_exists
) {
73 __kmp_str_buf_init(&__kmp_console_buf
);
76 // We do not check the result of AllocConsole because
77 // 1. the call is harmless
78 // 2. it is not clear how to communicate failue
79 // 3. we will detect failure later when we get handle(s)
81 ho
= GetStdHandle(STD_OUTPUT_HANDLE
);
82 if (ho
== INVALID_HANDLE_VALUE
|| ho
== NULL
) {
84 DWORD err
= GetLastError();
85 // TODO: output error somehow (maybe message box)
91 __kmp_stdout
= ho
; // temporary code, need new global for ho
93 he
= GetStdHandle(STD_ERROR_HANDLE
);
94 if (he
== INVALID_HANDLE_VALUE
|| he
== NULL
) {
96 DWORD err
= GetLastError();
97 // TODO: output error somehow (maybe message box)
103 __kmp_stderr
= he
; // temporary code, need new global
105 __kmp_console_exists
= TRUE
;
107 __kmp_release_bootstrap_lock(&__kmp_console_lock
);
111 #define __kmp_stderr (stderr)
112 #define __kmp_stdout (stdout)
113 #endif /* KMP_OS_WINDOWS */
115 void __kmp_vprintf(enum kmp_io out_stream
, char const *format
, va_list ap
) {
117 if (!__kmp_console_exists
) {
118 __kmp_redirect_output();
120 if (!__kmp_stderr
&& out_stream
== kmp_err
) {
123 if (!__kmp_stdout
&& out_stream
== kmp_out
) {
126 #endif /* KMP_OS_WINDOWS */
127 auto stream
= ((out_stream
== kmp_out
) ? __kmp_stdout
: __kmp_stderr
);
129 if (__kmp_debug_buf
&& __kmp_debug_buffer
!= NULL
) {
131 int dc
= __kmp_debug_count
++ % __kmp_debug_buf_lines
;
132 char *db
= &__kmp_debug_buffer
[dc
* __kmp_debug_buf_chars
];
135 #ifdef KMP_DEBUG_PIDS
136 chars
= KMP_SNPRINTF(db
, __kmp_debug_buf_chars
,
137 "pid=%d: ", (kmp_int32
)getpid());
139 chars
+= KMP_VSNPRINTF(db
, __kmp_debug_buf_chars
, format
, ap
);
141 if (chars
+ 1 > __kmp_debug_buf_chars
) {
142 if (chars
+ 1 > __kmp_debug_buf_warn_chars
) {
145 __kmp_str_buf_print(&__kmp_console_buf
,
146 "OMP warning: Debugging buffer "
147 "overflow; increase "
148 "KMP_DEBUG_BUF_CHARS to %d\n",
150 WriteFile(stream
, __kmp_console_buf
.str
, __kmp_console_buf
.used
, &count
,
152 __kmp_str_buf_clear(&__kmp_console_buf
);
155 "OMP warning: Debugging buffer overflow; "
156 "increase KMP_DEBUG_BUF_CHARS to %d\n",
160 __kmp_debug_buf_warn_chars
= chars
+ 1;
162 /* terminate string if overflow occurred */
163 db
[__kmp_debug_buf_chars
- 2] = '\n';
164 db
[__kmp_debug_buf_chars
- 1] = '\0';
169 #ifdef KMP_DEBUG_PIDS
170 __kmp_str_buf_print(&__kmp_console_buf
, "pid=%d: ", (kmp_int32
)getpid());
172 __kmp_str_buf_vprint(&__kmp_console_buf
, format
, ap
);
173 WriteFile(stream
, __kmp_console_buf
.str
, __kmp_console_buf
.used
, &count
,
175 __kmp_str_buf_clear(&__kmp_console_buf
);
177 #ifdef KMP_DEBUG_PIDS
178 fprintf(stream
, "pid=%d: ", (kmp_int32
)getpid());
180 vfprintf(stream
, format
, ap
);
186 void __kmp_printf(char const *format
, ...) {
188 va_start(ap
, format
);
190 __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock
);
191 __kmp_vprintf(kmp_err
, format
, ap
);
192 __kmp_release_bootstrap_lock(&__kmp_stdio_lock
);
197 void __kmp_printf_no_lock(char const *format
, ...) {
199 va_start(ap
, format
);
201 __kmp_vprintf(kmp_err
, format
, ap
);
206 void __kmp_fprintf(enum kmp_io stream
, char const *format
, ...) {
208 va_start(ap
, format
);
210 __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock
);
211 __kmp_vprintf(stream
, format
, ap
);
212 __kmp_release_bootstrap_lock(&__kmp_stdio_lock
);