]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - tools/src/infra/assert.cxx
Starterkit 5 Release 1.5 Bugfix
[karo-tx-redboot.git] / tools / src / infra / assert.cxx
1 //{{{  Banner                                           
2
3 //============================================================================
4 //
5 //      assert.cxx
6 //
7 //      Host side implementation of the infrastructure assertions.
8 //
9 //============================================================================
10 //####COPYRIGHTBEGIN####
11 //                                                                          
12 // ----------------------------------------------------------------------------
13 // Copyright (C) 2002 Bart Veer
14 // Copyright (C) 1998, 1999, 2000, 2001 Red Hat, Inc.
15 //
16 // This file is part of the eCos host tools.
17 //
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) 
21 // any later version.
22 // 
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 
26 // more details.
27 // 
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.
31 //
32 // ----------------------------------------------------------------------------
33 //                                                                          
34 //####COPYRIGHTEND####
35 //============================================================================
36 //#####DESCRIPTIONBEGIN####
37 //
38 // Author(s):   bartv
39 // Contact(s):  bartv
40 // Date:        1998/11/27
41 // Version:     0.01
42 // Purpose:     To provide a host-side implementation of the eCos assertion
43 //              facilities.
44 //
45 //####DESCRIPTIONEND####
46 //============================================================================
47
48 //}}}
49 //{{{  #include's                                       
50
51 #include <string.h>
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
57 // as well.
58 #define CYG_DECLARE_HOST_ASSERTION_SUPPORT
59 #include "cyg/infra/cyg_ass.h"
60
61 // STDIO is needed for the default assertion handler.
62 // STDLIB is needed for exit() and the status codes.
63 #include <cstdio>
64 #include <cstdlib>
65
66 #if defined(__unix__) || defined(__CYGWIN32__)
67 extern "C" {
68 #include <unistd.h>             // Needed for _exit()
69 }
70 #endif
71
72 // These are needed for the table of callbacks.
73 #include <utility>
74 #include <iterator>
75 #include <vector>
76
77 //}}}
78
79 // -------------------------------------------------------------------------
80 // Statics. The host-side assertion code requires two bits of data.
81 //
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.
85 //
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.
89
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
93 // away.
94 typedef void (*cyg_callback_fn)(void (*)(const char*));
95
96                                         // The current assertion handler
97 static bool (*current_handler)( const char*, const char*, cyg_uint32, const char*) = 0;
98
99                                         // The callback table.
100 static std::vector<std::pair<const char*, cyg_callback_fn> > callbacks;
101
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.
107
108 extern "C" void
109 cyg_assert_install_failure_handler( bool(*fn)(const char*, const char*, cyg_uint32, const char*) )
110 {
111     current_handler = fn;
112 }
113
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.
119 //
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.
123 //
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).
127 extern "C" void
128 cyg_assert_install_failure_callback( const char* name, void (*fn)(void (*)(const char*)) )
129 {
130     callbacks.push_back(std::make_pair(name, fn));
131 }
132
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.
138 extern "C" void
139 cyg_assert_failure_invoke_callbacks(
140     void (*first_fn)(const char*),
141     void (*data_fn)(const char*),
142     void (*final_fn)(void) )
143 {
144     std::vector<std::pair<const char*, cyg_callback_fn> >::const_iterator i;
145
146     for ( i = callbacks.begin(); i != callbacks.end(); i++ ) {
147
148         if (0 != first_fn) {
149             (*first_fn)(i->first);
150         }
151         if (0 != data_fn) {
152             (*(i->second))(data_fn);
153         }
154         if (0 != final_fn) {
155             (*final_fn)();
156         }
157     }
158 }
159
160 // ----------------------------------------------------------------------------
161 // The default assertion handler. This assumes that the application is 
162 // a console application with a sensible stderr stream.
163 //
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.
167 //
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.
175 //
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.
181
182 static FILE * default_handler_output_file = 0;
183 static bool   body_contains_data          = false;
184
185                                         // output the callback name
186 static void
187 default_handler_first_fn(const char* name)
188 {
189     if (0 != default_handler_output_file) {
190         fprintf(default_handler_output_file, "# {{{  %s\n\n", name);
191     }
192     body_contains_data = false;
193 }
194
195                                         // output some actual text.
196 static void
197 default_handler_second_fn(const char* data)
198 {
199     body_contains_data = true;
200     if (0 != default_handler_output_file) {
201         fputs(data, default_handler_output_file);
202     }
203 }
204
205                                         // the end of a callback.
206 static void
207 default_handler_final_fn( void )
208 {
209     
210     if (0 != default_handler_output_file) {
211         if (body_contains_data) {
212             fputs("\n", default_handler_output_file);
213         }
214         fputs("# }}}\n", default_handler_output_file);
215     }
216 }
217
218
219 static void
220 default_handler(const char* fn, const char* file, cyg_uint32 lineno, const char* msg)
221 {
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) {
226         invoke_count++;
227         fprintf(stderr, "Recursive assertion failure.\n");
228         return;
229     } else {
230         invoke_count = 1;
231     }
232     
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
235     // attempted.
236     if (0 == msg)
237         msg ="<unknown>";
238     if (0 == file)
239         file = "<unknown>";
240     
241     fprintf(stderr, "Assertion failure: %s\n", msg);
242     fprintf(stderr, "File %s, line number %lu\n", file, (unsigned long) lineno);
243     if (0 != fn)
244         fprintf(stderr, "Function %s\n", fn);
245     
246     // Only create a logfile if more information is available.
247     if (0 != callbacks.size() ) {
248
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)
252         char filename[32];
253         int  fd;
254         strcpy(filename, "/tmp/ecosdump.XXXXXX");
255         fd = mkstemp(filename);
256         if (-1 == fd) {
257             fprintf(stderr, "Unable to create a suitable output file for additional data.\n");
258         } else {
259             default_handler_output_file = fdopen(fd, "w");
260             if (0 == default_handler_output_file) {
261                 close(fd);
262             }
263         }
264 #else
265         char filename[L_tmpnam];
266         if (0 == tmpnam(filename)) {
267             fprintf(stderr, "Unable to create a suitable output file for additional data.\n");
268         } else {
269
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
273             // to worry about.
274             default_handler_output_file = fopen(filename, "w");
275         }
276 #endif
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);
280         } else {
281             fprintf(stderr, "Writing additional output to %s\n", filename);
282                 
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);
286             if (0 != fn)
287                 fprintf(default_handler_output_file, "Function %s\n", fn);
288             fputs("\n", default_handler_output_file);
289
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 );
293
294             // And close the file.
295             fputs("\nEnd of assertion data.\n", default_handler_output_file);
296             fclose(default_handler_output_file);
297         }
298     }
299     fflush(stderr);
300 }
301
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.
312
313 extern "C" void
314 cyg_assert_fail( const char* fn, const char* file, cyg_uint32 lineno, const char* msg )
315 {
316
317     if ((0 == current_handler) || !(*current_handler)(fn, file, lineno, msg)) {
318         default_handler(fn, file, lineno, msg);
319     }
320     abort();
321     _exit(0);
322 }
323
324 // ----------------------------------------------------------------------------
325 // A utility function, primarily intended to be called from inside gdb.
326 extern "C" void
327 cyg_assert_quickfail(void)
328 {
329     cyg_assert_fail("gdb", "<no file>", 0, "manual call");
330 }