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