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
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): ______________________________________.
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
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
107 #include <ptlib/pprocess.h>
112 static BOOL
PAssertAction(int c
, const char * msg
)
117 PError
<< "\nAborting.\n";
124 PError
<< "\nThrowing exception\n";
125 throw std::runtime_error(msg
);
133 PString cmd
= "gdb " + PProcess::Current().GetFile();
134 cmd
.sprintf(" %d", getpid());
135 system((const char *)cmd
);
142 PError
<< "\nDumping core.\n";
143 kill(getpid(), SIGABRT
);
148 PError
<< "\nIgnoring.\n";
156 void PAssertFunc(const char * msg
)
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.
169 static BOOL inAssert
;
174 ostream
& trace
= PTrace::Begin(0, __FILE__
, __LINE__
);
175 trace
<< "PWLib\t" << msg
<< PTrace::End
;
177 if (&trace
!= &PError
)
178 PError
<< msg
<< endl
;
181 //Throw a runtime exception if the environment variable PWLIB_ASSERT_EXCEPTION is set
182 char * env
= ::getenv("PWLIB_ASSERT_EXCEPTION");
184 throw std::runtime_error(msg
);
189 char * env
= ::getenv("PWLIB_ASSERT_ACTION");
190 if (env
!= NULL
&& *env
!= EOF
&& PAssertAction(*env
, msg
)) {
195 // Check for if stdin is not a TTY and just ignore the assert if so.
196 if (!isatty(STDIN_FILENO
)) {
202 PError
<< "\n<A>bort, <C>ore dump"
204 << ", <I>gnore <T>hrow exception"
213 if (PAssertAction(c
, msg
))
220 PThread::Trace(); // Get debugging dump
222 kill(taskIdSelf(), SIGABRT
);
228 // End Of File ///////////////////////////////////////////////////////////////