blob: 6db07741b9640eb0e6ecafdefaab5ddae121f75a [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
Jonathan Peytonde4749b2016-12-14 23:01:24 +00002 * kmp_debug.cpp -- debug utilities for the Guide library
Jim Cownie5e8470a2013-09-27 10:38:44 +00003 */
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
Jonathan Peyton30419822017-05-12 18:01:32 +000022void __kmp_debug_printf_stdout(char const *format, ...) {
23 va_list ap;
24 va_start(ap, format);
Jim Cownie5e8470a2013-09-27 10:38:44 +000025
Jonathan Peyton30419822017-05-12 18:01:32 +000026 __kmp_vprintf(kmp_out, format, ap);
Jim Cownie5e8470a2013-09-27 10:38:44 +000027
Jonathan Peyton30419822017-05-12 18:01:32 +000028 va_end(ap);
Jim Cownie5e8470a2013-09-27 10:38:44 +000029}
30#endif
31
Jonathan Peyton30419822017-05-12 18:01:32 +000032void __kmp_debug_printf(char const *format, ...) {
33 va_list ap;
34 va_start(ap, format);
Jim Cownie5e8470a2013-09-27 10:38:44 +000035
Jonathan Peyton30419822017-05-12 18:01:32 +000036 __kmp_vprintf(kmp_err, format, ap);
Jim Cownie5e8470a2013-09-27 10:38:44 +000037
Jonathan Peyton30419822017-05-12 18:01:32 +000038 va_end(ap);
Jim Cownie5e8470a2013-09-27 10:38:44 +000039}
40
41#ifdef KMP_USE_ASSERT
Jonathan Peyton30419822017-05-12 18:01:32 +000042int __kmp_debug_assert(char const *msg, char const *file, int line) {
Jim Cownie5e8470a2013-09-27 10:38:44 +000043
Jonathan Peyton30419822017-05-12 18:01:32 +000044 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
Jim Cownie5e8470a2013-09-27 10:38:44 +000054
Jonathan Peyton30419822017-05-12 18:01:32 +000055#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
Jim Cownie5e8470a2013-09-27 10:38:44 +000076
Jonathan Peyton30419822017-05-12 18:01:32 +000077 __kmp_msg(kmp_ms_fatal, KMP_MSG(AssertionFailure, file, line),
78 KMP_HNT(SubmitBugReport), __kmp_msg_null);
Jim Cownie5e8470a2013-09-27 10:38:44 +000079
Jonathan Peyton30419822017-05-12 18:01:32 +000080 return 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +000081
Jonathan Peyton30419822017-05-12 18:01:32 +000082} // __kmp_debug_assert
Jim Cownie5e8470a2013-09-27 10:38:44 +000083
84#endif // KMP_USE_ASSERT
85
86/* Dump debugging buffer to stderr */
Jonathan Peyton30419822017-05-12 18:01:32 +000087void __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;
Jim Cownie5e8470a2013-09-27 10:38:44 +000096
Jonathan Peyton30419822017-05-12 18:01:32 +000097 __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);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000100
Jonathan Peyton30419822017-05-12 18:01:32 +0000101 for (i = 0; i < __kmp_debug_buf_lines; i++) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000102
Jonathan Peyton30419822017-05-12 18:01:32 +0000103 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';
Jim Cownie5e8470a2013-09-27 10:38:44 +0000110 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000111 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';
Jim Cownie5e8470a2013-09-27 10:38:44 +0000119 }
120
Jonathan Peyton30419822017-05-12 18:01:32 +0000121 __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;
Jim Cownie5e8470a2013-09-27 10:38:44 +0000128 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000129
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 }
Jim Cownie5e8470a2013-09-27 10:38:44 +0000134}