17cc577a4SJonathan Peyton /*
2de4749b7SJonathan Peyton * kmp_debug.cpp -- debug utilities for the Guide library
37cc577a4SJonathan Peyton */
47cc577a4SJonathan Peyton
57cc577a4SJonathan Peyton //===----------------------------------------------------------------------===//
67cc577a4SJonathan Peyton //
7*57b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8*57b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
9*57b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
107cc577a4SJonathan Peyton //
117cc577a4SJonathan Peyton //===----------------------------------------------------------------------===//
127cc577a4SJonathan Peyton
137cc577a4SJonathan Peyton #include "kmp.h"
147cc577a4SJonathan Peyton #include "kmp_debug.h" /* really necessary? */
157cc577a4SJonathan Peyton #include "kmp_i18n.h"
167cc577a4SJonathan Peyton #include "kmp_io.h"
177cc577a4SJonathan Peyton
187cc577a4SJonathan Peyton #ifdef KMP_DEBUG
__kmp_debug_printf_stdout(char const * format,...)193041982dSJonathan Peyton void __kmp_debug_printf_stdout(char const *format, ...) {
207cc577a4SJonathan Peyton va_list ap;
217cc577a4SJonathan Peyton va_start(ap, format);
227cc577a4SJonathan Peyton
237cc577a4SJonathan Peyton __kmp_vprintf(kmp_out, format, ap);
247cc577a4SJonathan Peyton
257cc577a4SJonathan Peyton va_end(ap);
267cc577a4SJonathan Peyton }
277cc577a4SJonathan Peyton #endif
287cc577a4SJonathan Peyton
__kmp_debug_printf(char const * format,...)293041982dSJonathan Peyton void __kmp_debug_printf(char const *format, ...) {
307cc577a4SJonathan Peyton va_list ap;
317cc577a4SJonathan Peyton va_start(ap, format);
327cc577a4SJonathan Peyton
337cc577a4SJonathan Peyton __kmp_vprintf(kmp_err, format, ap);
347cc577a4SJonathan Peyton
357cc577a4SJonathan Peyton va_end(ap);
367cc577a4SJonathan Peyton }
377cc577a4SJonathan Peyton
387cc577a4SJonathan Peyton #ifdef KMP_USE_ASSERT
__kmp_debug_assert(char const * msg,char const * file,int line)393041982dSJonathan Peyton int __kmp_debug_assert(char const *msg, char const *file, int line) {
407cc577a4SJonathan Peyton
417cc577a4SJonathan Peyton if (file == NULL) {
427cc577a4SJonathan Peyton file = KMP_I18N_STR(UnknownFile);
437cc577a4SJonathan Peyton } else {
443041982dSJonathan Peyton // Remove directories from path, leave only file name. File name is enough,
453041982dSJonathan Peyton // there is no need in bothering developers and customers with full paths.
467cc577a4SJonathan Peyton char const *slash = strrchr(file, '/');
477cc577a4SJonathan Peyton if (slash != NULL) {
487cc577a4SJonathan Peyton file = slash + 1;
49bd3a7633SJonathan Peyton }
50bd3a7633SJonathan Peyton }
517cc577a4SJonathan Peyton
527cc577a4SJonathan Peyton #ifdef KMP_DEBUG
537cc577a4SJonathan Peyton __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock);
547cc577a4SJonathan Peyton __kmp_debug_printf("Assertion failure at %s(%d): %s.\n", file, line, msg);
557cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&__kmp_stdio_lock);
567cc577a4SJonathan Peyton #ifdef USE_ASSERT_BREAK
577cc577a4SJonathan Peyton #if KMP_OS_WINDOWS
587cc577a4SJonathan Peyton DebugBreak();
597cc577a4SJonathan Peyton #endif
607cc577a4SJonathan Peyton #endif // USE_ASSERT_BREAK
617cc577a4SJonathan Peyton #ifdef USE_ASSERT_STALL
627cc577a4SJonathan Peyton /* __kmp_infinite_loop(); */
633041982dSJonathan Peyton for (;;)
643041982dSJonathan Peyton ;
657cc577a4SJonathan Peyton #endif // USE_ASSERT_STALL
667cc577a4SJonathan Peyton #ifdef USE_ASSERT_SEG
677cc577a4SJonathan Peyton {
687cc577a4SJonathan Peyton int volatile *ZERO = (int *)0;
697cc577a4SJonathan Peyton ++(*ZERO);
707cc577a4SJonathan Peyton }
717cc577a4SJonathan Peyton #endif // USE_ASSERT_SEG
727cc577a4SJonathan Peyton #endif
737cc577a4SJonathan Peyton
746a393f75SJonathan Peyton __kmp_fatal(KMP_MSG(AssertionFailure, file, line), KMP_HNT(SubmitBugReport),
756a393f75SJonathan Peyton __kmp_msg_null);
767cc577a4SJonathan Peyton
777cc577a4SJonathan Peyton return 0;
787cc577a4SJonathan Peyton
797cc577a4SJonathan Peyton } // __kmp_debug_assert
807cc577a4SJonathan Peyton
817cc577a4SJonathan Peyton #endif // KMP_USE_ASSERT
827cc577a4SJonathan Peyton
837cc577a4SJonathan Peyton /* Dump debugging buffer to stderr */
__kmp_dump_debug_buffer(void)843041982dSJonathan Peyton void __kmp_dump_debug_buffer(void) {
857cc577a4SJonathan Peyton if (__kmp_debug_buffer != NULL) {
867cc577a4SJonathan Peyton int i;
877cc577a4SJonathan Peyton int dc = __kmp_debug_count;
883041982dSJonathan Peyton char *db = &__kmp_debug_buffer[(dc % __kmp_debug_buf_lines) *
893041982dSJonathan Peyton __kmp_debug_buf_chars];
903041982dSJonathan Peyton char *db_end =
913041982dSJonathan Peyton &__kmp_debug_buffer[__kmp_debug_buf_lines * __kmp_debug_buf_chars];
927cc577a4SJonathan Peyton char *db2;
937cc577a4SJonathan Peyton
947cc577a4SJonathan Peyton __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock);
957cc577a4SJonathan Peyton __kmp_printf_no_lock("\nStart dump of debugging buffer (entry=%d):\n",
967cc577a4SJonathan Peyton dc % __kmp_debug_buf_lines);
977cc577a4SJonathan Peyton
987cc577a4SJonathan Peyton for (i = 0; i < __kmp_debug_buf_lines; i++) {
997cc577a4SJonathan Peyton
1007cc577a4SJonathan Peyton if (*db != '\0') {
1017cc577a4SJonathan Peyton /* Fix up where no carriage return before string termination char */
1027cc577a4SJonathan Peyton for (db2 = db + 1; db2 < db + __kmp_debug_buf_chars - 1; db2++) {
1037cc577a4SJonathan Peyton if (*db2 == '\0') {
1043041982dSJonathan Peyton if (*(db2 - 1) != '\n') {
1053041982dSJonathan Peyton *db2 = '\n';
1063041982dSJonathan Peyton *(db2 + 1) = '\0';
1073041982dSJonathan Peyton }
1087cc577a4SJonathan Peyton break;
1097cc577a4SJonathan Peyton }
1107cc577a4SJonathan Peyton }
1113041982dSJonathan Peyton /* Handle case at end by shortening the printed message by one char if
1123041982dSJonathan Peyton * necessary */
1133041982dSJonathan Peyton if (db2 == db + __kmp_debug_buf_chars - 1 && *db2 == '\0' &&
1143041982dSJonathan Peyton *(db2 - 1) != '\n') {
1157cc577a4SJonathan Peyton *(db2 - 1) = '\n';
1167cc577a4SJonathan Peyton }
1177cc577a4SJonathan Peyton
1187cc577a4SJonathan Peyton __kmp_printf_no_lock("%4d: %.*s", i, __kmp_debug_buf_chars, db);
1197cc577a4SJonathan Peyton *db = '\0'; /* only let it print once! */
1207cc577a4SJonathan Peyton }
1217cc577a4SJonathan Peyton
1227cc577a4SJonathan Peyton db += __kmp_debug_buf_chars;
1237cc577a4SJonathan Peyton if (db >= db_end)
1247cc577a4SJonathan Peyton db = __kmp_debug_buffer;
1257cc577a4SJonathan Peyton }
1267cc577a4SJonathan Peyton
1277cc577a4SJonathan Peyton __kmp_printf_no_lock("End dump of debugging buffer (entry=%d).\n\n",
1287cc577a4SJonathan Peyton (dc + i - 1) % __kmp_debug_buf_lines);
1297cc577a4SJonathan Peyton __kmp_release_bootstrap_lock(&__kmp_stdio_lock);
1307cc577a4SJonathan Peyton }
1317cc577a4SJonathan Peyton }
132