3 //============================================================================
7 // Host side implementation of the infrastructure assertions.
9 //============================================================================
10 //####COPYRIGHTBEGIN####
12 // ----------------------------------------------------------------------------
13 // Copyright (C) 2002 Bart Veer
14 // Copyright (C) 1998, 1999, 2000, 2001 Red Hat, Inc.
16 // This file is part of the eCos host tools.
18 // This program is free software; you can redistribute it and/or modify it
19 // under the terms of the GNU General Public License as published by the Free
20 // Software Foundation; either version 2 of the License, or (at your option)
23 // This program is distributed in the hope that it will be useful, but WITHOUT
24 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
28 // You should have received a copy of the GNU General Public License along with
29 // this program; if not, write to the Free Software Foundation, Inc.,
30 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 // ----------------------------------------------------------------------------
34 //####COPYRIGHTEND####
35 //============================================================================
36 //#####DESCRIPTIONBEGIN####
42 // Purpose: To provide a host-side implementation of the eCos assertion
45 //####DESCRIPTIONEND####
46 //============================================================================
52 #include "pkgconf/infra.h"
53 #include "cyg/infra/cyg_type.h"
54 // Without this symbol the header file has no effect
55 #define CYGDBG_USE_TRACING
56 // Make sure that the host-side extensions get prototyped
58 #define CYG_DECLARE_HOST_ASSERTION_SUPPORT
59 #include "cyg/infra/cyg_ass.h"
61 // STDIO is needed for the default assertion handler.
62 // STDLIB is needed for exit() and the status codes.
66 #if defined(__unix__) || defined(__CYGWIN32__)
68 #include <unistd.h> // Needed for _exit()
72 // These are needed for the table of callbacks.
79 // -------------------------------------------------------------------------
80 // Statics. The host-side assertion code requires two bits of data.
82 // The first identifies the function that should actually get invoked
83 // when an assertion is triggered. A default implementation is defined
84 // in this module, but applications may install a replacement.
86 // The second is a table of callback functions that various libraries
87 // or bits of application code may install. Each such callback gets invoked
88 // when an assertion triggers.
90 // VC++ bogosity. Using a full function pointer prototype in a template
91 // confuses the compiler. It is still possible to declare the callbacks vector,
92 // but not any iterators for that vector. A typedef makes the problem go
94 typedef void (*cyg_callback_fn)(void (*)(const char*));
96 // The current assertion handler
97 static bool (*current_handler)( const char*, const char*, cyg_uint32, const char*) = 0;
99 // The callback table.
100 static std::vector<std::pair<const char*, cyg_callback_fn> > callbacks;
102 // ----------------------------------------------------------------------------
103 // Many applications will want to handle assertion failures differently
104 // from the default, for example pipe the output into an emacs buffer
105 // rather than just generate a file. This routine allows a suitable
106 // function to be installed.
109 cyg_assert_install_failure_handler( bool(*fn)(const char*, const char*, cyg_uint32, const char*) )
111 current_handler = fn;
114 // ----------------------------------------------------------------------------
115 // Various different bits of the system may want to register callback functions
116 // that get invoked during an assertion failure and that output useful
117 // data. Typically this might happen in the constructor for a static object.
118 // A good example of such a callback is the implementation of the trace code.
120 // The implementation requires creating a new entry in the static vector.
121 // A memory exhaustion exception could occur but there is no sensible way of
122 // handling it at this level.
124 // Multiple callbacks with the same name are legal. Multiple callbacks with
125 // the same function are unlikely, but it is probably not worthwhile raising
126 // an exception (especially since this code may be called from C).
128 cyg_assert_install_failure_callback( const char* name, void (*fn)(void (*)(const char*)) )
130 callbacks.push_back(std::make_pair(name, fn));
133 // -------------------------------------------------------------------------
134 // Once an assertion has triggered either the default handler or the
135 // installed handler will want to invoke all the callbacks. Rather than
136 // provide direct access to the callback table and require the calling
137 // code to be in C++, a functional interface is provided instead.
139 cyg_assert_failure_invoke_callbacks(
140 void (*first_fn)(const char*),
141 void (*data_fn)(const char*),
142 void (*final_fn)(void) )
144 std::vector<std::pair<const char*, cyg_callback_fn> >::const_iterator i;
146 for ( i = callbacks.begin(); i != callbacks.end(); i++ ) {
149 (*first_fn)(i->first);
152 (*(i->second))(data_fn);
160 // ----------------------------------------------------------------------------
161 // The default assertion handler. This assumes that the application is
162 // a console application with a sensible stderr stream.
164 // First some initial diagnostics are output immediately, in case
165 // subsequent attempts to output more data cause additional failures. It
166 // is worthwhile detecting recursive assertion failures.
168 // Assuming the table of callbacks is not empty it is possible to
169 // output some more data to a file. If possible mkstemp() is used to
170 // create this file. If mkstemp() is not available then tmpnam() is
171 // used instead. That function has security problems, albeit not ones
172 // likely to affect dump files. Once the file is opened the callbacks
173 // are invoked. Three utilities have to be provided to do the real
174 // work, and a static is used to keep track of the FILE * pointer.
176 // The testcase tassert8, and in particular the associated Tcl proc
177 // tassert8_filter in testsuite/cyginfra/assert.exp, has detailed
178 // knowledge of the output format. Any changes here may need to be
179 // reflected in that test case. There are also support routines in
180 // hosttest.exp which may need to be updated.
182 static FILE * default_handler_output_file = 0;
183 static bool body_contains_data = false;
185 // output the callback name
187 default_handler_first_fn(const char* name)
189 if (0 != default_handler_output_file) {
190 fprintf(default_handler_output_file, "# {{{ %s\n\n", name);
192 body_contains_data = false;
195 // output some actual text.
197 default_handler_second_fn(const char* data)
199 body_contains_data = true;
200 if (0 != default_handler_output_file) {
201 fputs(data, default_handler_output_file);
205 // the end of a callback.
207 default_handler_final_fn( void )
210 if (0 != default_handler_output_file) {
211 if (body_contains_data) {
212 fputs("\n", default_handler_output_file);
214 fputs("# }}}\n", default_handler_output_file);
220 default_handler(const char* fn, const char* file, cyg_uint32 lineno, const char* msg)
222 static int invoke_count = 0;
223 if (2 == invoke_count) {
224 // The fprintf() immediately below causes an assertion failure
225 } else if (1 == invoke_count) {
227 fprintf(stderr, "Recursive assertion failure.\n");
233 // There is an argument for using write() rather than fprintf() here,
234 // in case the C library has been corrupted. For now this has not been
241 fprintf(stderr, "Assertion failure: %s\n", msg);
242 fprintf(stderr, "File %s, line number %lu\n", file, (unsigned long) lineno);
244 fprintf(stderr, "Function %s\n", fn);
246 // Only create a logfile if more information is available.
247 if (0 != callbacks.size() ) {
249 // Use mkstemp() if possible, but only when running on a platform where /tmp
250 // is likely to be available.
251 #if defined(HAVE_MKSTEMP) && !defined(_MSC_VER)
254 strcpy(filename, "/tmp/ecosdump.XXXXXX");
255 fd = mkstemp(filename);
257 fprintf(stderr, "Unable to create a suitable output file for additional data.\n");
259 default_handler_output_file = fdopen(fd, "w");
260 if (0 == default_handler_output_file) {
265 char filename[L_tmpnam];
266 if (0 == tmpnam(filename)) {
267 fprintf(stderr, "Unable to create a suitable output file for additional data.\n");
270 // No attempt is made to ensure that the file does not already
271 // exist. This would require POSIX calls rather than ISO C ones.
272 // The probability of a problem is considered to be too small
274 default_handler_output_file = fopen(filename, "w");
277 if (0 == default_handler_output_file) {
278 fprintf(stderr, "Unable to open output file %s\n", filename);
279 fputs("No further assertion information is available.\n", stderr);
281 fprintf(stderr, "Writing additional output to %s\n", filename);
283 // Repeat the information about the assertion itself.
284 fprintf(default_handler_output_file, "Assertion failure: %s\n", msg);
285 fprintf(default_handler_output_file, "File %s, line number %lu\n", file, (unsigned long) lineno);
287 fprintf(default_handler_output_file, "Function %s\n", fn);
288 fputs("\n", default_handler_output_file);
290 // Now for the various callbacks.
291 cyg_assert_failure_invoke_callbacks( &default_handler_first_fn,
292 &default_handler_second_fn, &default_handler_final_fn );
294 // And close the file.
295 fputs("\nEnd of assertion data.\n", default_handler_output_file);
296 fclose(default_handler_output_file);
302 // ----------------------------------------------------------------------------
303 // The assertion handler. This is the function that gets invoked when
304 // an assertion triggers. If a special assertion handler has been installed
305 // then this gets called. If it returns false or if no special handler is
306 // available then the default handler gets called instead. Typically the
307 // user will now have a lot of information about what happened to cause the
308 // assertion failure. The next stage is to invoke abort() which should
309 // terminate the program and generate a core dump for subsequent inspection
310 // (unless of course the application is already running in a debugger session).
311 // A final call to _exit() should be completely redundant.
314 cyg_assert_fail( const char* fn, const char* file, cyg_uint32 lineno, const char* msg )
317 if ((0 == current_handler) || !(*current_handler)(fn, file, lineno, msg)) {
318 default_handler(fn, file, lineno, msg);
324 // ----------------------------------------------------------------------------
325 // A utility function, primarily intended to be called from inside gdb.
327 cyg_assert_quickfail(void)
329 cyg_assert_fail("gdb", "<no file>", 0, "manual call");