blob: 50d3876abf8401c4aab4c6d6e218dc31c4032361 [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
Jim Cownie5e8470a2013-09-27 10:38:44 +00005//===----------------------------------------------------------------------===//
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 Cownie5e8470a2013-09-27 10:38:44 +000014#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 Peyton30419822017-05-12 18:01:32 +000020void __kmp_debug_printf_stdout(char const *format, ...) {
21 va_list ap;
22 va_start(ap, format);
Jim Cownie5e8470a2013-09-27 10:38:44 +000023
Jonathan Peyton30419822017-05-12 18:01:32 +000024 __kmp_vprintf(kmp_out, format, ap);
Jim Cownie5e8470a2013-09-27 10:38:44 +000025
Jonathan Peyton30419822017-05-12 18:01:32 +000026 va_end(ap);
Jim Cownie5e8470a2013-09-27 10:38:44 +000027}
28#endif
29
Jonathan Peyton30419822017-05-12 18:01:32 +000030void __kmp_debug_printf(char const *format, ...) {
31 va_list ap;
32 va_start(ap, format);
Jim Cownie5e8470a2013-09-27 10:38:44 +000033
Jonathan Peyton30419822017-05-12 18:01:32 +000034 __kmp_vprintf(kmp_err, format, ap);
Jim Cownie5e8470a2013-09-27 10:38:44 +000035
Jonathan Peyton30419822017-05-12 18:01:32 +000036 va_end(ap);
Jim Cownie5e8470a2013-09-27 10:38:44 +000037}
38
39#ifdef KMP_USE_ASSERT
Jonathan Peyton30419822017-05-12 18:01:32 +000040int __kmp_debug_assert(char const *msg, char const *file, int line) {
Jim Cownie5e8470a2013-09-27 10:38:44 +000041
Jonathan Peyton30419822017-05-12 18:01:32 +000042 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 Peytonbd3a7632017-09-27 20:36:27 +000050 }
51 }
Jim Cownie5e8470a2013-09-27 10:38:44 +000052
Jonathan Peyton30419822017-05-12 18:01:32 +000053#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 Cownie5e8470a2013-09-27 10:38:44 +000074
Jonathan Peyton6a393f72017-09-05 15:43:58 +000075 __kmp_fatal(KMP_MSG(AssertionFailure, file, line), KMP_HNT(SubmitBugReport),
76 __kmp_msg_null);
Jim Cownie5e8470a2013-09-27 10:38:44 +000077
Jonathan Peyton30419822017-05-12 18:01:32 +000078 return 0;
Jim Cownie5e8470a2013-09-27 10:38:44 +000079
Jonathan Peyton30419822017-05-12 18:01:32 +000080} // __kmp_debug_assert
Jim Cownie5e8470a2013-09-27 10:38:44 +000081
82#endif // KMP_USE_ASSERT
83
84/* Dump debugging buffer to stderr */
Jonathan Peyton30419822017-05-12 18:01:32 +000085void __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 Cownie5e8470a2013-09-27 10:38:44 +000094
Jonathan Peyton30419822017-05-12 18:01:32 +000095 __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 Cownie5e8470a2013-09-27 10:38:44 +000098
Jonathan Peyton30419822017-05-12 18:01:32 +000099 for (i = 0; i < __kmp_debug_buf_lines; i++) {
Jim Cownie5e8470a2013-09-27 10:38:44 +0000100
Jonathan Peyton30419822017-05-12 18:01:32 +0000101 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 Cownie5e8470a2013-09-27 10:38:44 +0000108 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000109 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 Cownie5e8470a2013-09-27 10:38:44 +0000117 }
118
Jonathan Peyton30419822017-05-12 18:01:32 +0000119 __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 Cownie5e8470a2013-09-27 10:38:44 +0000126 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000127
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 Cownie5e8470a2013-09-27 10:38:44 +0000132}