1 /*
2 * kmp_debug.cpp -- debug utilities for the Guide library
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 "kmp.h"
15 #include "kmp_debug.h" /* really necessary? */
16 #include "kmp_i18n.h"
17 #include "kmp_io.h"
18
19 #ifdef KMP_DEBUG
__kmp_debug_printf_stdout(char const * format,...)20 void __kmp_debug_printf_stdout(char const *format, ...) {
21 va_list ap;
22 va_start(ap, format);
23
24 __kmp_vprintf(kmp_out, format, ap);
25
26 va_end(ap);
27 }
28 #endif
29
__kmp_debug_printf(char const * format,...)30 void __kmp_debug_printf(char const *format, ...) {
31 va_list ap;
32 va_start(ap, format);
33
34 __kmp_vprintf(kmp_err, format, ap);
35
36 va_end(ap);
37 }
38
39 #ifdef KMP_USE_ASSERT
__kmp_debug_assert(char const * msg,char const * file,int line)40 int __kmp_debug_assert(char const *msg, char const *file, int line) {
41
42 if (file == NULL) {
43 file = KMP_I18N_STR(UnknownFile);
44 } else {
45 // Remove directories from path, leave only file name. File name is enough,
46 // there is no need in bothering developers and customers with full paths.
47 char const *slash = strrchr(file, '/');
48 if (slash != NULL) {
49 file = slash + 1;
50 }
51 }
52
53 #ifdef KMP_DEBUG
54 __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock);
55 __kmp_debug_printf("Assertion failure at %s(%d): %s.\n", file, line, msg);
56 __kmp_release_bootstrap_lock(&__kmp_stdio_lock);
57 #ifdef USE_ASSERT_BREAK
58 #if KMP_OS_WINDOWS
59 DebugBreak();
60 #endif
61 #endif // USE_ASSERT_BREAK
62 #ifdef USE_ASSERT_STALL
63 /* __kmp_infinite_loop(); */
64 for (;;)
65 ;
66 #endif // USE_ASSERT_STALL
67 #ifdef USE_ASSERT_SEG
68 {
69 int volatile *ZERO = (int *)0;
70 ++(*ZERO);
71 }
72 #endif // USE_ASSERT_SEG
73 #endif
74
75 __kmp_fatal(KMP_MSG(AssertionFailure, file, line), KMP_HNT(SubmitBugReport),
76 __kmp_msg_null);
77
78 return 0;
79
80 } // __kmp_debug_assert
81
82 #endif // KMP_USE_ASSERT
83
84 /* Dump debugging buffer to stderr */
__kmp_dump_debug_buffer(void)85 void __kmp_dump_debug_buffer(void) {
86 if (__kmp_debug_buffer != NULL) {
87 int i;
88 int dc = __kmp_debug_count;
89 char *db = &__kmp_debug_buffer[(dc % __kmp_debug_buf_lines) *
90 __kmp_debug_buf_chars];
91 char *db_end =
92 &__kmp_debug_buffer[__kmp_debug_buf_lines * __kmp_debug_buf_chars];
93 char *db2;
94
95 __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock);
96 __kmp_printf_no_lock("\nStart dump of debugging buffer (entry=%d):\n",
97 dc % __kmp_debug_buf_lines);
98
99 for (i = 0; i < __kmp_debug_buf_lines; i++) {
100
101 if (*db != '\0') {
102 /* Fix up where no carriage return before string termination char */
103 for (db2 = db + 1; db2 < db + __kmp_debug_buf_chars - 1; db2++) {
104 if (*db2 == '\0') {
105 if (*(db2 - 1) != '\n') {
106 *db2 = '\n';
107 *(db2 + 1) = '\0';
108 }
109 break;
110 }
111 }
112 /* Handle case at end by shortening the printed message by one char if
113 * necessary */
114 if (db2 == db + __kmp_debug_buf_chars - 1 && *db2 == '\0' &&
115 *(db2 - 1) != '\n') {
116 *(db2 - 1) = '\n';
117 }
118
119 __kmp_printf_no_lock("%4d: %.*s", i, __kmp_debug_buf_chars, db);
120 *db = '\0'; /* only let it print once! */
121 }
122
123 db += __kmp_debug_buf_chars;
124 if (db >= db_end)
125 db = __kmp_debug_buffer;
126 }
127
128 __kmp_printf_no_lock("End dump of debugging buffer (entry=%d).\n\n",
129 (dc + i - 1) % __kmp_debug_buf_lines);
130 __kmp_release_bootstrap_lock(&__kmp_stdio_lock);
131 }
132 }
133