kmp_debug.cpp 3.5 KB

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