Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1 | /* |
Jonathan Peyton | de4749b | 2016-12-14 23:01:24 +0000 | [diff] [blame] | 2 | * kmp_debug.cpp -- debug utilities for the Guide library |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3 | */ |
| 4 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 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 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 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 |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 20 | void __kmp_debug_printf_stdout(char const *format, ...) { |
| 21 | va_list ap; |
| 22 | va_start(ap, format); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 23 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 24 | __kmp_vprintf(kmp_out, format, ap); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 25 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 26 | va_end(ap); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 27 | } |
| 28 | #endif |
| 29 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 30 | void __kmp_debug_printf(char const *format, ...) { |
| 31 | va_list ap; |
| 32 | va_start(ap, format); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 33 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 34 | __kmp_vprintf(kmp_err, format, ap); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 35 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 36 | va_end(ap); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | #ifdef KMP_USE_ASSERT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 40 | int __kmp_debug_assert(char const *msg, char const *file, int line) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 41 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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; |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 52 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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 |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 74 | |
Jonathan Peyton | 6a393f7 | 2017-09-05 15:43:58 +0000 | [diff] [blame] | 75 | __kmp_fatal(KMP_MSG(AssertionFailure, file, line), KMP_HNT(SubmitBugReport), |
| 76 | __kmp_msg_null); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 77 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 78 | return 0; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 79 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 80 | } // __kmp_debug_assert |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 81 | |
| 82 | #endif // KMP_USE_ASSERT |
| 83 | |
| 84 | /* Dump debugging buffer to stderr */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 94 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 98 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 99 | for (i = 0; i < __kmp_debug_buf_lines; i++) { |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 100 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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'; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 108 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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'; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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; |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 126 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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 | } |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 132 | } |