]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/infra/assert.cxx
Initial revision
[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 "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
56 // as well.
57 #define CYG_DECLARE_HOST_ASSERTION_SUPPORT
58 #include "cyg/infra/cyg_ass.h"
59
60 // STDIO is needed for the default assertion handler.
61 // STDLIB is needed for exit() and the status codes.
62 #include <cstdio>
63 #include <cstdlib>
64
65 #if defined(__unix__) || defined(__CYGWIN32__)
66 extern "C" {
67 #include <unistd.h>             // Needed for _exit()
68 }
69 #endif
70
71 // These are needed for the table of callbacks.
72 #include <utility>
73 #include <iterator>
74 #include <vector>
75
76 //}}}
77
78 // -------------------------------------------------------------------------
79 // Statics. The host-side assertion code requires two bits of data.
80 //
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.
84 //
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.
88
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
92 // away.
93 typedef void (*cyg_callback_fn)(void (*)(const char*));
94
95                                         // The current assertion handler
96 static bool (*current_handler)( const char*, const char*, cyg_uint32, const char*) = 0;
97
98                                         // The callback table.
99 static std::vector<std::pair<const char*, cyg_callback_fn> > callbacks;
100
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.
106
107 extern "C" void
108 cyg_assert_install_failure_handler( bool(*fn)(const char*, const char*, cyg_uint32, const char*) )
109 {
110     current_handler = fn;
111 }
112
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.
118 //
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.
122 //
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).
126 extern "C" void
127 cyg_assert_install_failure_callback( const char* name, void (*fn)(void (*)(const char*)) )
128 {
129     callbacks.push_back(std::make_pair(name, fn));
130 }
131
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.
137 extern "C" void
138 cyg_assert_failure_invoke_callbacks(
139     void (*first_fn)(const char*),
140     void (*data_fn)(const char*),
141     void (*final_fn)(void) )
142 {
143     std::vector<std::pair<const char*, cyg_callback_fn> >::const_iterator i;
144
145     for ( i = callbacks.begin(); i != callbacks.end(); i++ ) {
146
147         if (0 != first_fn) {
148             (*first_fn)(i->first);
149         }
150         if (0 != data_fn) {
151             (*(i->second))(data_fn);
152         }
153         if (0 != final_fn) {
154             (*final_fn)();
155         }
156     }
157 }
158
159 // ----------------------------------------------------------------------------
160 // The default assertion handler. This assumes that the application is 
161 // a console application with a sensible stderr stream.
162 //
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.
166 //
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.
174 //
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.
180
181 static FILE * default_handler_output_file = 0;
182 static bool   body_contains_data          = false;
183
184                                         // output the callback name
185 static void
186 default_handler_first_fn(const char* name)
187 {
188     if (0 != default_handler_output_file) {
189         fprintf(default_handler_output_file, "# {{{  %s\n\n", name);
190     }
191     body_contains_data = false;
192 }
193
194                                         // output some actual text.
195 static void
196 default_handler_second_fn(const char* data)
197 {
198     body_contains_data = true;
199     if (0 != default_handler_output_file) {
200         fputs(data, default_handler_output_file);
201     }
202 }
203
204                                         // the end of a callback.
205 static void
206 default_handler_final_fn( void )
207 {
208     
209     if (0 != default_handler_output_file) {
210         if (body_contains_data) {
211             fputs("\n", default_handler_output_file);
212         }
213         fputs("# }}}\n", default_handler_output_file);
214     }
215 }
216
217
218 static void
219 default_handler(const char* fn, const char* file, cyg_uint32 lineno, const char* msg)
220 {
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) {
225         invoke_count++;
226         fprintf(stderr, "Recursive assertion failure.\n");
227         return;
228     } else {
229         invoke_count = 1;
230     }
231     
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
234     // attempted.
235     if (0 == msg)
236         msg ="<unknown>";
237     if (0 == file)
238         file = "<unknown>";
239     
240     fprintf(stderr, "Assertion failure: %s\n", msg);
241     fprintf(stderr, "File %s, line number %lu\n", file, (unsigned long) lineno);
242     if (0 != fn)
243         fprintf(stderr, "Function %s\n", fn);
244     
245     // Only create a logfile if more information is available.
246     if (0 != callbacks.size() ) {
247
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)
251         char filename[32];
252         int  fd;
253         strcpy(filename, "/tmp/ecosdump.XXXXXX");
254         fd = mkstemp(filename);
255         if (-1 == fd) {
256             fprintf(stderr, "Unable to create a suitable output file for additional data.\n");
257         } else {
258             default_handler_output_file = fdopen(fd, "w");
259             if (0 == default_handler_output_file) {
260                 close(fd);
261             }
262         }
263 #else
264         char filename[L_tmpnam];
265         if (0 == tmpnam(filename)) {
266             fprintf(stderr, "Unable to create a suitable output file for additional data.\n");
267         } else {
268
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
272             // to worry about.
273             default_handler_output_file = fopen(filename, "w");
274         }
275 #endif
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);
279         } else {
280             fprintf(stderr, "Writing additional output to %s\n", filename);
281                 
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);
285             if (0 != fn)
286                 fprintf(default_handler_output_file, "Function %s\n", fn);
287             fputs("\n", default_handler_output_file);
288
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 );
292
293             // And close the file.
294             fputs("\nEnd of assertion data.\n", default_handler_output_file);
295             fclose(default_handler_output_file);
296         }
297     }
298     fflush(stderr);
299 }
300
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.
311
312 extern "C" void
313 cyg_assert_fail( const char* fn, const char* file, cyg_uint32 lineno, const char* msg )
314 {
315
316     if ((0 == current_handler) || !(*current_handler)(fn, file, lineno, msg)) {
317         default_handler(fn, file, lineno, msg);
318     }
319     abort();
320     _exit(0);
321 }
322
323 // ----------------------------------------------------------------------------
324 // A utility function, primarily intended to be called from inside gdb.
325 extern "C" void
326 cyg_assert_quickfail(void)
327 {
328     cyg_assert_fail("gdb", "<no file>", 0, "manual call");
329 }