Fixed DevStudio 2003 build with memory check code.
[pwlib.git] / src / ptlib / unix / assert.cxx
blobed80041208994c0e10ae0f0e61a73124a76dd3ab
1 /*
2 * assert.cxx
4 * Assert function implementation.
6 * Portable Windows Library
8 * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25 * All Rights Reserved.
27 * Contributor(s): ______________________________________.
29 * $Log$
30 * Revision 1.22 2007/06/02 12:50:05 dsandras
31 * Removed redundant header.
33 * Revision 1.21 2007/02/12 23:46:34 csoutheren
34 * Applied 1611486 - The environment variable PWLIB_ASSERT_ACTION
35 * Thanks to Vyacheslav Frolov
37 * Revision 1.20 2007/02/01 05:04:20 csoutheren
38 * Allow compilation without C++ exceptions enabled
40 * Revision 1.19 2006/12/08 05:02:24 csoutheren
41 * Apply 1602184 - assert can also throw exception
42 * Thanks to Frederic Heem
44 * Revision 1.18 2006/06/21 03:28:44 csoutheren
45 * Various cleanups thanks for Frederic Heem
47 * Revision 1.17 2004/07/11 07:56:36 csoutheren
48 * Applied jumbo VxWorks patch, thanks to Eize Slange
50 * Revision 1.16 2002/06/25 04:05:19 robertj
51 * Fixed new assert function that does not have file/line parameters.
53 * Revision 1.15 2002/06/25 02:26:05 robertj
54 * Improved assertion system to allow C++ class name to be displayed if
55 * desired, especially relevant to container classes.
57 * Revision 1.14 2001/09/03 08:13:54 robertj
58 * Changed "stack dump" option to "run debugger" when get assert.
60 * Revision 1.13 2001/08/30 08:58:09 robertj
61 * Added explicit output to trace file if get assert.
63 * Revision 1.12 2001/07/03 04:41:25 yurik
64 * Corrections to Jac's submission from 6/28
66 * Revision 1.11 2001/06/30 06:59:07 yurik
67 * Jac Goudsmit from Be submit these changes 6/28. Implemented by Yuri Kiryanov
69 * Revision 1.10 2001/05/03 01:14:09 robertj
70 * Put in check to ignore assert if stdin not TTY or not open.
71 * Changed default action on assert to ignore if get EOF.
73 * Revision 1.9 2001/04/20 10:13:02 robertj
74 * Made sure cannot have nested asserts.
76 * Revision 1.8 2000/03/27 18:20:09 craigs
77 * Added the ability to get a stack dump on assert
79 * Revision 1.7 2000/03/21 03:09:54 craigs
80 * Fixed the fix with EOF
82 * Revision 1.6 2000/03/20 22:59:18 craigs
83 * Fixed problem with asserts generating unlimited output when input is redirected
85 * Revision 1.5 2000/02/18 01:49:18 craigs
86 * Added VxWorks code
88 * Revision 1.4 1999/06/23 14:19:46 robertj
89 * Fixed core dump problem with SIGINT/SIGTERM terminating process.
91 * Revision 1.3 1998/09/24 04:12:08 robertj
92 * Added open software license.
94 * Revision 1.2 1998/06/17 14:47:47 robertj
95 * Fixed continuous display of assert if input is from /dev/null
97 * Revision 1.1 1996/01/26 11:09:15 craigs
98 * Initial revision
103 #include <ptlib.h>
105 #include <ctype.h>
106 #include <signal.h>
107 #include <ptlib/pprocess.h>
109 #ifndef __BEOS__
110 #ifndef P_VXWORKS
112 static BOOL PAssertAction(int c, const char * msg)
114 switch (c) {
115 case 'a' :
116 case 'A' :
117 PError << "\nAborting.\n";
118 _exit(1);
119 break;
121 #if P_EXCEPTIONS
122 case 't' :
123 case 'T' :
124 PError << "\nThrowing exception\n";
125 throw std::runtime_error(msg);
126 return TRUE;
127 #endif
129 #ifdef _DEBUG
130 case 'd' :
131 case 'D' :
133 PString cmd = "gdb " + PProcess::Current().GetFile();
134 cmd.sprintf(" %d", getpid());
135 system((const char *)cmd);
137 break;
138 #endif
140 case 'c' :
141 case 'C' :
142 PError << "\nDumping core.\n";
143 kill(getpid(), SIGABRT);
145 case 'i' :
146 case 'I' :
147 case EOF :
148 PError << "\nIgnoring.\n";
149 return TRUE;
151 return FALSE;
153 #endif
154 #endif
156 void PAssertFunc(const char * msg)
159 #ifdef __BEOS__
160 // Print location in Eddie-compatible format
161 PError << msg << endl;
162 // Pop up the debugger dialog that gives the user the necessary choices
163 // "Ignore" is not supported on BeOS but you can instruct the
164 // debugger to continue executing.
165 // Note: if you choose "debug" you get a debug prompt. Type bdb to
166 // start the Be Debugger.
167 debugger(msg);
168 #else
169 static BOOL inAssert;
170 if (inAssert)
171 return;
172 inAssert = TRUE;
174 ostream & trace = PTrace::Begin(0, __FILE__, __LINE__);
175 trace << "PWLib\t" << msg << PTrace::End;
177 if (&trace != &PError)
178 PError << msg << endl;
180 #if P_EXCEPTIONS
181 //Throw a runtime exception if the environment variable PWLIB_ASSERT_EXCEPTION is set
182 char * env = ::getenv("PWLIB_ASSERT_EXCEPTION");
183 if (env != NULL){
184 throw std::runtime_error(msg);
186 #endif
188 #ifndef P_VXWORKS
189 char * env = ::getenv("PWLIB_ASSERT_ACTION");
190 if (env != NULL && *env != EOF && PAssertAction(*env, msg)) {
191 inAssert = FALSE;
192 return;
195 // Check for if stdin is not a TTY and just ignore the assert if so.
196 if (!isatty(STDIN_FILENO)) {
197 inAssert = FALSE;
198 return;
201 for(;;) {
202 PError << "\n<A>bort, <C>ore dump"
203 #if P_EXCEPTIONS
204 << ", <I>gnore <T>hrow exception"
205 #endif
206 #ifdef _DEBUG
207 << ", <D>ebug"
208 #endif
209 << "? " << flush;
211 int c = getchar();
213 if (PAssertAction(c, msg))
214 break;
216 inAssert = FALSE;
218 #else // P_VXWORKS
220 PThread::Trace(); // Get debugging dump
221 exit(1);
222 kill(taskIdSelf(), SIGABRT);
224 #endif // P_VXWORKS
225 #endif // __BEOS__
228 // End Of File ///////////////////////////////////////////////////////////////