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 //============================================================================
51 #include "pkgconf/infra.h"
52 #include "cyg/infra/cyg_type.h"
53 // Without this symbol the header file has no effect
54 #define CYGDBG_USE_TRACING
55 // Make sure that the host-side extensions get prototyped
57 #define CYG_DECLARE_HOST_ASSERTION_SUPPORT
58 #include "cyg/infra/cyg_ass.h"
60 // STDIO is needed for the default assertion handler.
61 // STDLIB is needed for exit() and the status codes.
65 #if defined(__unix__) || defined(__CYGWIN32__)
67 #include <unistd.h> // Needed for _exit()
71 // These are needed for the table of callbacks.
78 // -------------------------------------------------------------------------
79 // Statics. The host-side assertion code requires two bits of data.
81 // The first identifies the function that should actually get invoked
82 // when an assertion is triggered. A default implementation is defined
83 // in this module, but applications may install a replacement.
85 // The second is a table of callback functions that various libraries
86 // or bits of application code may install. Each such callback gets invoked
87 // when an assertion triggers.
89 // VC++ bogosity. Using a full function pointer prototype in a template
90 // confuses the compiler. It is still possible to declare the callbacks vector,
91 // but not any iterators for that vector. A typedef makes the problem go
93 typedef void (*cyg_callback_fn)(void (*)(const char*));
95 // The current assertion handler
96 static bool (*current_handler)( const char*, const char*, cyg_uint32, const char*) = 0;
98 // The callback table.
99 static std::vector<std::pair<const char*, cyg_callback_fn> > callbacks;
101 // ----------------------------------------------------------------------------
102 // Many applications will want to handle assertion failures differently
103 // from the default, for example pipe the output into an emacs buffer
104 // rather than just generate a file. This routine allows a suitable
105 // function to be installed.
108 cyg_assert_install_failure_handler( bool(*fn)(const char*, const char*, cyg_uint32, const char*) )
110 current_handler = fn;
113 // ----------------------------------------------------------------------------
114 // Various different bits of the system may want to register callback functions
115 // that get invoked during an assertion failure and that output useful
116 // data. Typically this might happen in the constructor for a static object.
117 // A good example of such a callback is the implementation of the trace code.
119 // The implementation requires creating a new entry in the static vector.
120 // A memory exhaustion exception could occur but there is no sensible way of
121 // handling it at this level.
123 // Multiple callbacks with the same name are legal. Multiple callbacks with
124 // the same function are unlikely, but it is probably not worthwhile raising
125 // an exception (especially since this code may be called from C).
127 cyg_assert_install_failure_callback( const char* name, void (*fn)(void (*)(const char*)) )
129 callbacks.push_back(std::make_pair(name, fn));
132 // -------------------------------------------------------------------------
133 // Once an assertion has triggered either the default handler or the
134 // installed handler will want to invoke all the callbacks. Rather than
135 // provide direct access to the callback table and require the calling
136 // code to be in C++, a functional interface is provided instead.
138 cyg_assert_failure_invoke_callbacks(
139 void (*first_fn)(const char*),
140 void (*data_fn)(const char*),
141 void (*final_fn)(void) )
143 std::vector<std::pair<const char*, cyg_callback_fn> >::const_iterator i;
145 for ( i = callbacks.begin(); i != callbacks.end(); i++ ) {
148 (*first_fn)(i->first);
151 (*(i->second))(data_fn);
159 // ----------------------------------------------------------------------------
160 // The default assertion handler. This assumes that the application is
161 // a console application with a sensible stderr stream.
163 // First some initial diagnostics are output immediately, in case
164 // subsequent attempts to output more data cause additional failures. It
165 // is worthwhile detecting recursive assertion failures.
167 // Assuming the table of callbacks is not empty it is possible to
168 // output some more data to a file. If possible mkstemp() is used to
169 // create this file. If mkstemp() is not available then tmpnam() is
170 // used instead. That function has security problems, albeit not ones
171 // likely to affect dump files. Once the file is opened the callbacks
172 // are invoked. Three utilities have to be provided to do the real
173 // work, and a static is used to keep track of the FILE * pointer.
175 // The testcase tassert8, and in particular the associated Tcl proc
176 // tassert8_filter in testsuite/cyginfra/assert.exp, has detailed
177 // knowledge of the output format. Any changes here may need to be
178 // reflected in that test case. There are also support routines in
179 // hosttest.exp which may need to be updated.
181 static FILE * default_handler_output_file = 0;
182 static bool body_contains_data = false;
184 // output the callback name
186 default_handler_first_fn(const char* name)
188 if (0 != default_handler_output_file) {
189 fprintf(default_handler_output_file, "# {{{ %s\n\n", name);
191 body_contains_data = false;
194 // output some actual text.
196 default_handler_second_fn(const char* data)
198 body_contains_data = true;
199 if (0 != default_handler_output_file) {
200 fputs(data, default_handler_output_file);
204 // the end of a callback.
206 default_handler_final_fn( void )
209 if (0 != default_handler_output_file) {
210 if (body_contains_data) {
211 fputs("\n", default_handler_output_file);
213 fputs("# }}}\n", default_handler_output_file);
219 default_handler(const char* fn, const char* file, cyg_uint32 lineno, const char* msg)
221 static int invoke_count = 0;
222 if (2 == invoke_count) {
223 // The fprintf() immediately below causes an assertion failure
224 } else if (1 == invoke_count) {
226 fprintf(stderr, "Recursive assertion failure.\n");
232 // There is an argument for using write() rather than fprintf() here,
233 // in case the C library has been corrupted. For now this has not been
240 fprintf(stderr, "Assertion failure: %s\n", msg);
241 fprintf(stderr, "File %s, line number %lu\n", file, (unsigned long) lineno);
243 fprintf(stderr, "Function %s\n", fn);
245 // Only create a logfile if more information is available.
246 if (0 != callbacks.size() ) {
248 // Use mkstemp() if possible, but only when running on a platform where /tmp
249 // is likely to be available.
250 #if defined(HAVE_MKSTEMP) && !defined(_MSC_VER)
253 strcpy(filename, "/tmp/ecosdump.XXXXXX");
254 fd = mkstemp(filename);
256 fprintf(stderr, "Unable to create a suitable output file for additional data.\n");
258 default_handler_output_file = fdopen(fd, "w");
259 if (0 == default_handler_output_file) {
264 char filename[L_tmpnam];
265 if (0 == tmpnam(filename)) {
266 fprintf(stderr, "Unable to create a suitable output file for additional data.\n");
269 // No attempt is made to ensure that the file does not already
270 // exist. This would require POSIX calls rather than ISO C ones.
271 // The probability of a problem is considered to be too small
273 default_handler_output_file = fopen(filename, "w");
276 if (0 == default_handler_output_file) {
277 fprintf(stderr, "Unable to open output file %s\n", filename);
278 fputs("No further assertion information is available.\n", stderr);
280 fprintf(stderr, "Writing additional output to %s\n", filename);
282 // Repeat the information about the assertion itself.
283 fprintf(default_handler_output_file, "Assertion failure: %s\n", msg);
284 fprintf(default_handler_output_file, "File %s, line number %lu\n", file, (unsigned long) lineno);
286 fprintf(default_handler_output_file, "Function %s\n", fn);
287 fputs("\n", default_handler_output_file);
289 // Now for the various callbacks.
290 cyg_assert_failure_invoke_callbacks( &default_handler_first_fn,
291 &default_handler_second_fn, &default_handler_final_fn );
293 // And close the file.
294 fputs("\nEnd of assertion data.\n", default_handler_output_file);
295 fclose(default_handler_output_file);
301 // ----------------------------------------------------------------------------
302 // The assertion handler. This is the function that gets invoked when
303 // an assertion triggers. If a special assertion handler has been installed
304 // then this gets called. If it returns false or if no special handler is
305 // available then the default handler gets called instead. Typically the
306 // user will now have a lot of information about what happened to cause the
307 // assertion failure. The next stage is to invoke abort() which should
308 // terminate the program and generate a core dump for subsequent inspection
309 // (unless of course the application is already running in a debugger session).
310 // A final call to _exit() should be completely redundant.
313 cyg_assert_fail( const char* fn, const char* file, cyg_uint32 lineno, const char* msg )
316 if ((0 == current_handler) || !(*current_handler)(fn, file, lineno, msg)) {
317 default_handler(fn, file, lineno, msg);
323 // ----------------------------------------------------------------------------
324 // A utility function, primarily intended to be called from inside gdb.
326 cyg_assert_quickfail(void)
328 cyg_assert_fail("gdb", "<no file>", 0, "manual call");