bug.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* bug.cpp -*-C++-*-
  2. *
  3. *************************************************************************
  4. *
  5. * @copyright
  6. * Copyright (C) 2009-2013, Intel Corporation
  7. * All rights reserved.
  8. *
  9. * @copyright
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. * * Neither the name of Intel Corporation nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * @copyright
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  32. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  33. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  35. * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. **************************************************************************/
  38. #include "bug.h"
  39. #include <exception>
  40. #include <stdio.h>
  41. #include <stdarg.h>
  42. #include <stdlib.h>
  43. #ifdef _WIN32
  44. # include "windows-clean.h"
  45. # include "internal/abi.h"
  46. # include "cilktools/cilkscreen.h"
  47. # include <crtdbg.h>
  48. #endif
  49. __CILKRTS_BEGIN_EXTERN_C
  50. COMMON_PORTABLE const char *const __cilkrts_assertion_failed =
  51. "%s:%d: cilk assertion failed: %s\n";
  52. COMMON_PORTABLE void __cilkrts_bug(const char *fmt,...) cilk_nothrow
  53. {
  54. #if defined (_WIN32) && defined(_DEBUG)
  55. _CRTIMP void __cdecl _wassert(__in_z const wchar_t * _Message,
  56. __in_z const wchar_t *_File,
  57. __in unsigned _Line);
  58. char message[256];
  59. wchar_t wmessage[256];
  60. va_list l;
  61. va_start(l, fmt);
  62. _vsnprintf_s(message, 256, _TRUNCATE, fmt, l);
  63. va_end(l);
  64. _snwprintf_s(wmessage, 256, _TRUNCATE, _CRT_WIDE("%S"),
  65. message); /* widen */
  66. // Force asserts to go to stderr and the debugger. This isn't polite, but
  67. // we're about to kill the app anyway and it will prevent our tests from
  68. // hanging
  69. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE| _CRTDBG_MODE_DEBUG);
  70. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
  71. _wassert(wmessage, _CRT_WIDE(__FILE__), __LINE__);
  72. // If there's a debugger attached, give it a chance to look at the failure
  73. if (IsDebuggerPresent())
  74. DebugBreak();
  75. abort();
  76. /* __asm int 3 */
  77. #else
  78. /* To reduce user confusion, write all user-generated output
  79. before the system-generated error message. */
  80. va_list l;
  81. fflush(NULL);
  82. va_start(l, fmt);
  83. vfprintf(stderr, fmt, l);
  84. va_end(l);
  85. fflush(stderr);
  86. #ifndef _WIN32
  87. abort();
  88. #endif
  89. #endif
  90. exit(1);
  91. }
  92. COMMON_PORTABLE void cilkbug_assert_no_uncaught_exception(void)
  93. {
  94. bool uncaught = std::uncaught_exception();
  95. CILK_ASSERT(!uncaught);
  96. }
  97. COMMON_SYSDEP void abort_because_rts_is_corrupted(void)
  98. {
  99. __cilkrts_bug("The Cilk Plus runtime system detected a corruption "
  100. "in its data structures. This is most likely caused "
  101. "by an application bug. Aborting execution.\n");
  102. }
  103. #ifdef WIN32
  104. COMMON_SYSDEP void __cilkrts_dbgprintf(const char *fmt,...)
  105. {
  106. char message[2048];
  107. va_list l;
  108. // Cilkscreen shouldn't watch this
  109. __cilkscreen_disable_checking();
  110. va_start(l, fmt);
  111. _vsnprintf_s(message, 2048, _TRUNCATE, fmt, l);
  112. va_end(l);
  113. OutputDebugStringA (message);
  114. // Re-enable Cilkscreen
  115. __cilkscreen_enable_checking();
  116. }
  117. #endif
  118. __CILKRTS_END_EXTERN_C
  119. /* End bug.cpp */