NCBI C++ ToolKit
test_assert_impl.h
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef COMMON__TEST_ASSERT_IMPL__H
2 #define COMMON__TEST_ASSERT_IMPL__H
3 
4 /* $Id: test_assert_impl.h 89177 2020-03-04 16:31:32Z gouriano $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Denis Vakatov
30  *
31  * File Description:
32  * Setup #NDEBUG and #_DEBUG preprocessor macro in a way that ASSERTs
33  * will be active even in the "Release" mode (it's useful for test apps).
34  *
35  */
36 
37 #ifndef TEST_ASSERT__H
38 # error "Must not use this header alone, but from a proper wrapper."
39 #endif /*TEST_ASSERT__H*/
40 
41 #if defined(NCBI_OS_MSWIN)
42 # ifndef NCBI_MSWIN_NO_POPUP
43 # ifdef _ASSERT
44 # undef _ASSERT
45 # endif
46 # define Type aType
47 # endif
48 # include <crtdbg.h>
49 # include <stdio.h>
50 # define NOMINMAX
51 # include <windows.h>
52 # include <dbghelp.h>
53 # ifndef NCBI_MSWIN_NO_POPUP
54 # undef Type
55 # endif
56 
57 /* Suppress popup messages on execution errors.
58  * NOTE: Windows-specific, suppresses all error message boxes in both runtime
59  * and in debug libraries, as well as all General Protection Fault messages.
60  * Environment variable DIAG_SILENT_ABORT must be set to "Y" or "y".
61  */
62 
63 /* Handler for "Unhandled" exceptions */
64 static LONG CALLBACK _SEH_Handler(EXCEPTION_POINTERS* ep)
65 {
66 #ifdef __cplusplus
67 #ifdef _UNICODE
68  wchar_t dumpname[64], tmp[64];
69  wcscat( wcscat( wcscpy(dumpname, L"core."), _ltow(GetCurrentProcessId(), tmp, 10)), L".dmp");
70 #else
71  char dumpname[64], tmp[64];
72  strcat( strcat( strcpy(dumpname, "core."), _ltoa(GetCurrentProcessId(), tmp, 10)), ".dmp");
73 #endif
74  HANDLE hf = CreateFile(dumpname,
75  GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
76  if (hf != INVALID_HANDLE_VALUE)
77  {
78  MINIDUMP_EXCEPTION_INFORMATION ei;
79  ei.ThreadId = GetCurrentThreadId();
80  ei.ExceptionPointers = ep;
81  ei.ClientPointers = FALSE;
82  MiniDumpWriteDump(
83  GetCurrentProcess(), GetCurrentProcessId(),
84  hf, MiniDumpNormal, &ei, NULL, NULL);
85  CloseHandle(hf);
86  fprintf(stderr, "Unhandled exception: %lx at %p", ep->ExceptionRecord->ExceptionCode, ep->ExceptionRecord->ExceptionAddress);
87  }
88 #endif
89  // Always terminate a program
90  return EXCEPTION_EXECUTE_HANDLER;
91 }
92 
94 {
95 #ifndef NCBI_MSWIN_NO_POPUP_EVER
96  /* Check environment variable for silent abort app at error */
97  const char* value = getenv("DIAG_SILENT_ABORT");
98  if (value && (*value == 'Y' || *value == 'y')) {
99 #endif
100  /* Windows GPF errors */
101  SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
102  SEM_NOOPENFILEERRORBOX);
103 
104  /* Runtime library */
105  _set_error_mode(_OUT_TO_STDERR);
106 
107  /* Debug library */
108  _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
109  _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
110  _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
111  _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
112  _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
113  _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
114 
115  /* Exceptions */
116  SetUnhandledExceptionFilter(_SEH_Handler);
117 #ifndef NCBI_MSWIN_NO_POPUP_EVER
118  }
119 #endif
120  return 0;
121 }
122 
123 /* Put this function at startup init level 'V', far enough not to mess up with
124  * base RTL init, which happens at preceding levels in alphabetical order.
125  */
126 # if _MSC_VER >= 1400
127 # pragma section(".CRT$XIV", read)
128 # endif
129 # pragma data_seg(".CRT$XIV")
131 # pragma data_seg()
132 
133 #endif /*defined(NCBI_OS_...)*/
134 
135 
136 /* Emulate <corelib/mswin_no_popup.h> if specified
137  */
138 #ifndef NCBI_MSWIN_NO_POPUP
139 
140 #ifdef NDEBUG
141 # undef NDEBUG
142 #endif
143 #ifdef assert
144 # undef assert
145 #endif
146 
147 /* IRIX stdlib fix (MIPSpro compiler tested): assert.h already included above*/
148 #ifdef NCBI_OS_IRIX
149 # ifdef __ASSERT_H__
150 # undef __ASSERT_H__
151 # endif
152 #endif
153 
154 /* Likewise on OSF/1 (at least with GCC 3, but this never hurts) */
155 #ifdef NCBI_OS_OSF1
156 # ifdef _ASSERT_H_
157 # undef _ASSERT_H_
158 # endif
159 #endif
160 
161 /* ...and on Darwin (at least with GCC 3, but this never hurts) */
162 #ifdef NCBI_OS_DARWIN
163 # ifdef FIXINC_BROKEN_ASSERT_STDLIB_CHECK
164 # undef FIXINC_BROKEN_ASSERT_STDLIB_CHECK
165 # endif
166 #endif
167 
168 #include <assert.h>
169 
170 #ifdef _ASSERT
171 # undef _ASSERT
172 #endif
173 #define _ASSERT assert
174 
175 #ifdef _TROUBLE
176 # undef _TROUBLE
177 #endif
178 #define _TROUBLE do { assert(0); abort(); } while (0)
179 
180 /* Often, both _DEBUG_ARG and _ASSERT are used together
181  Once we redefine one, we should redefine another as well
182 */
183 #ifndef _DEBUG
184 # ifdef _DEBUG_ARG
185 # undef _DEBUG_ARG
186 # endif
187 # define _DEBUG_ARG(x) x
188 #endif
189 
190 #endif /* NCBI_MSWIN_NO_POPUP */
191 
192 #endif /* COMMON__TEST_ASSERT_IMPL__H */
#define strcat(s, k)
static char tmp[3200]
Definition: utf8.c:42
#define NULL
Definition: ncbistd.hpp:225
#define INVALID_HANDLE_VALUE
A value for an invalid file handle.
Definition: mdb.c:389
#define HANDLE
An abstraction for a file handle.
Definition: mdb.c:383
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
#define FALSE
bool replacment for C indicating false.
Definition: ncbi_std.h:101
#define CALLBACK
Definition: sqltypes.h:65
static int _SuppressDiagPopupMessages(void)
static LONG CALLBACK _SEH_Handler(EXCEPTION_POINTERS *ep)
static int(* _SDPM)(void)
Modified on Wed Apr 17 13:10:03 2024 by modify_doxy.py rev. 669887