OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [infra/] [assert.cxx] - Blame information for rev 790

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//{{{  Banner                                           
2
 
3
//============================================================================
4
//
5
//      assert.cxx
6
//
7
//      Host side implementation of the infrastructure assertions.
8
//
9
//============================================================================
10
// ####ECOSHOSTGPLCOPYRIGHTBEGIN####                                        
11
// -------------------------------------------                              
12
// This file is part of the eCos host tools.                                
13
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
14
//
15
// This program is free software; you can redistribute it and/or modify     
16
// it under the terms of the GNU General Public License as published by     
17
// the Free Software Foundation; either version 2 or (at your option) any   
18
// later version.                                                           
19
//
20
// This program is distributed in the hope that it will be useful, but      
21
// WITHOUT ANY WARRANTY; without even the implied warranty of               
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        
23
// General Public License for more details.                                 
24
//
25
// You should have received a copy of the GNU General Public License        
26
// along with this program; if not, write to the                            
27
// Free Software Foundation, Inc., 51 Franklin Street,                      
28
// Fifth Floor, Boston, MA  02110-1301, USA.                                
29
// -------------------------------------------                              
30
// ####ECOSHOSTGPLCOPYRIGHTEND####                                          
31
//============================================================================
32
//#####DESCRIPTIONBEGIN####
33
//
34
// Author(s):   bartv
35
// Contact(s):  bartv
36
// Date:        1998/11/27
37
// Version:     0.01
38
// Purpose:     To provide a host-side implementation of the eCos assertion
39
//              facilities.
40
//
41
//####DESCRIPTIONEND####
42
//============================================================================
43
 
44
//}}}
45
//{{{  #include's                                       
46
 
47
#include "pkgconf/infra.h"
48
#include "cyg/infra/cyg_type.h"
49
// Without this symbol the header file has no effect
50
#define CYGDBG_USE_TRACING
51
// Make sure that the host-side extensions get prototyped
52
// as well.
53
#define CYG_DECLARE_HOST_ASSERTION_SUPPORT
54
#include "cyg/infra/cyg_ass.h"
55
 
56
// STDIO is needed for the default assertion handler.
57
// STDLIB is needed for exit() and the status codes.
58
// STRING is needed for strcpy()
59
#include <cstdio>
60
#include <cstdlib>
61
#include <cstring>
62
 
63
#if defined(__unix__) || defined(__CYGWIN32__)
64
extern "C" {
65
#include <unistd.h>             // Needed for _exit()
66
}
67
#endif
68
 
69
// These are needed for the table of callbacks.
70
#include <utility>
71
#include <iterator>
72
#include <vector>
73
 
74
//}}}
75
 
76
// -------------------------------------------------------------------------
77
// Statics. The host-side assertion code requires two bits of data.
78
//
79
// The first identifies the function that should actually get invoked
80
// when an assertion is triggered. A default implementation is defined
81
// in this module, but applications may install a replacement.
82
//
83
// The second is a table of callback functions that various libraries
84
// or bits of application code may install. Each such callback gets invoked
85
// when an assertion triggers.
86
 
87
// VC++ bogosity. Using a full function pointer prototype in a template
88
// confuses the compiler. It is still possible to declare the callbacks vector,
89
// but not any iterators for that vector. A typedef makes the problem go
90
// away.
91
typedef void (*cyg_callback_fn)(void (*)(const char*));
92
 
93
                                        // The current assertion handler
94
static bool (*current_handler)( const char*, const char*, cyg_uint32, const char*) = 0;
95
 
96
                                        // The callback table.
97
static std::vector<std::pair<const char*, cyg_callback_fn> > callbacks;
98
 
99
// ----------------------------------------------------------------------------
100
// Many applications will want to handle assertion failures differently
101
// from the default, for example pipe the output into an emacs buffer
102
// rather than just generate a file. This routine allows a suitable
103
// function to be installed.
104
 
105
extern "C" void
106
cyg_assert_install_failure_handler( bool(*fn)(const char*, const char*, cyg_uint32, const char*) )
107
{
108
    current_handler = fn;
109
}
110
 
111
// ----------------------------------------------------------------------------
112
// Various different bits of the system may want to register callback functions
113
// that get invoked during an assertion failure and that output useful
114
// data. Typically this might happen in the constructor for a static object.
115
// A good example of such a callback is the implementation of the trace code.
116
//
117
// The implementation requires creating a new entry in the static vector.
118
// A memory exhaustion exception could occur but there is no sensible way of
119
// handling it at this level.
120
//
121
// Multiple callbacks with the same name are legal. Multiple callbacks with
122
// the same function are unlikely, but it is probably not worthwhile raising
123
// an exception (especially since this code may be called from C).
124
extern "C" void
125
cyg_assert_install_failure_callback( const char* name, void (*fn)(void (*)(const char*)) )
126
{
127
    callbacks.push_back(std::make_pair(name, fn));
128
}
129
 
130
// -------------------------------------------------------------------------
131
// Once an assertion has triggered either the default handler or the
132
// installed handler will want to invoke all the callbacks. Rather than
133
// provide direct access to the callback table and require the calling
134
// code to be in C++, a functional interface is provided instead.
135
extern "C" void
136
cyg_assert_failure_invoke_callbacks(
137
    void (*first_fn)(const char*),
138
    void (*data_fn)(const char*),
139
    void (*final_fn)(void) )
140
{
141
    std::vector<std::pair<const char*, cyg_callback_fn> >::const_iterator i;
142
 
143
    for ( i = callbacks.begin(); i != callbacks.end(); i++ ) {
144
 
145
        if (0 != first_fn) {
146
            (*first_fn)(i->first);
147
        }
148
        if (0 != data_fn) {
149
            (*(i->second))(data_fn);
150
        }
151
        if (0 != final_fn) {
152
            (*final_fn)();
153
        }
154
    }
155
}
156
 
157
// ----------------------------------------------------------------------------
158
// The default assertion handler. This assumes that the application is 
159
// a console application with a sensible stderr stream.
160
//
161
// First some initial diagnostics are output immediately, in case
162
// subsequent attempts to output more data cause additional failures. It
163
// is worthwhile detecting recursive assertion failures.
164
//
165
// Assuming the table of callbacks is not empty it is possible to
166
// output some more data to a file. If possible mkstemp() is used to
167
// create this file. If mkstemp() is not available then tmpnam() is
168
// used instead. That function has security problems, albeit not ones
169
// likely to affect dump files. Once the file is opened the callbacks
170
// are invoked. Three utilities have to be provided to do the real
171
// work, and a static is used to keep track of the FILE * pointer.
172
//
173
// The testcase tassert8, and in particular the associated Tcl proc
174
// tassert8_filter in testsuite/cyginfra/assert.exp, has detailed
175
// knowledge of the output format. Any changes here may need to be
176
// reflected in that test case. There are also support routines in
177
// hosttest.exp which may need to be updated.
178
 
179
static FILE * default_handler_output_file = 0;
180
static bool   body_contains_data          = false;
181
 
182
                                        // output the callback name
183
static void
184
default_handler_first_fn(const char* name)
185
{
186
    if (0 != default_handler_output_file) {
187
        fprintf(default_handler_output_file, "# {{{  %s\n\n", name);
188
    }
189
    body_contains_data = false;
190
}
191
 
192
                                        // output some actual text.
193
static void
194
default_handler_second_fn(const char* data)
195
{
196
    body_contains_data = true;
197
    if (0 != default_handler_output_file) {
198
        fputs(data, default_handler_output_file);
199
    }
200
}
201
 
202
                                        // the end of a callback.
203
static void
204
default_handler_final_fn( void )
205
{
206
 
207
    if (0 != default_handler_output_file) {
208
        if (body_contains_data) {
209
            fputs("\n", default_handler_output_file);
210
        }
211
        fputs("# }}}\n", default_handler_output_file);
212
    }
213
}
214
 
215
 
216
static void
217
default_handler(const char* fn, const char* file, cyg_uint32 lineno, const char* msg)
218
{
219
    static int invoke_count = 0;
220
    if (2 == invoke_count) {
221
        // The fprintf() immediately below causes an assertion failure
222
    } else if (1 == invoke_count) {
223
        invoke_count++;
224
        fprintf(stderr, "Recursive assertion failure.\n");
225
        return;
226
    } else {
227
        invoke_count = 1;
228
    }
229
 
230
    // There is an argument for using write() rather than fprintf() here,
231
    // in case the C library has been corrupted. For now this has not been
232
    // attempted.
233
    if (0 == msg)
234
        msg ="<unknown>";
235
    if (0 == file)
236
        file = "<unknown>";
237
 
238
    fprintf(stderr, "Assertion failure: %s\n", msg);
239
    fprintf(stderr, "File %s, line number %lu\n", file, (unsigned long) lineno);
240
    if (0 != fn)
241
        fprintf(stderr, "Function %s\n", fn);
242
 
243
    // Only create a logfile if more information is available.
244
    if (0 != callbacks.size() ) {
245
 
246
        // Use mkstemp() if possible, but only when running on a platform where /tmp
247
        // is likely to be available.
248
#if defined(HAVE_MKSTEMP) && !defined(_MSC_VER)
249
        char filename[32];
250
        int  fd;
251
        strcpy(filename, "/tmp/ecosdump.XXXXXX");
252
        fd = mkstemp(filename);
253
        if (-1 == fd) {
254
            fprintf(stderr, "Unable to create a suitable output file for additional data.\n");
255
        } else {
256
            default_handler_output_file = fdopen(fd, "w");
257
            if (0 == default_handler_output_file) {
258
                close(fd);
259
            }
260
        }
261
#else
262
        char filename[L_tmpnam];
263
        if (0 == tmpnam(filename)) {
264
            fprintf(stderr, "Unable to create a suitable output file for additional data.\n");
265
        } else {
266
 
267
            // No attempt is made to ensure that the file does not already
268
            // exist. This would require POSIX calls rather than ISO C ones.
269
            // The probability of a problem is considered to be too small
270
            // to worry about.
271
            default_handler_output_file = fopen(filename, "w");
272
        }
273
#endif
274
        if (0 == default_handler_output_file) {
275
            fprintf(stderr, "Unable to open output file %s\n", filename);
276
            fputs("No further assertion information is available.\n", stderr);
277
        } else {
278
            fprintf(stderr, "Writing additional output to %s\n", filename);
279
 
280
            // Repeat the information about the assertion itself.
281
            fprintf(default_handler_output_file, "Assertion failure: %s\n", msg);
282
            fprintf(default_handler_output_file, "File %s, line number %lu\n", file, (unsigned long) lineno);
283
            if (0 != fn)
284
                fprintf(default_handler_output_file, "Function %s\n", fn);
285
            fputs("\n", default_handler_output_file);
286
 
287
            // Now for the various callbacks.
288
            cyg_assert_failure_invoke_callbacks( &default_handler_first_fn,
289
                                                 &default_handler_second_fn, &default_handler_final_fn );
290
 
291
            // And close the file.
292
            fputs("\nEnd of assertion data.\n", default_handler_output_file);
293
            fclose(default_handler_output_file);
294
        }
295
    }
296
    fflush(stderr);
297
}
298
 
299
// ----------------------------------------------------------------------------
300
// The assertion handler. This is the function that gets invoked when
301
// an assertion triggers. If a special assertion handler has been installed
302
// then this gets called. If it returns false or if no special handler is
303
// available then the default handler gets called instead. Typically the
304
// user will now have a lot of information about what happened to cause the
305
// assertion failure. The next stage is to invoke abort() which should
306
// terminate the program and generate a core dump for subsequent inspection
307
// (unless of course the application is already running in a debugger session).
308
// A final call to _exit() should be completely redundant.
309
 
310
extern "C" void
311
cyg_assert_fail( const char* fn, const char* file, cyg_uint32 lineno, const char* msg )
312
{
313
 
314
    if ((0 == current_handler) || !(*current_handler)(fn, file, lineno, msg)) {
315
        default_handler(fn, file, lineno, msg);
316
    }
317
    abort();
318
    _exit(0);
319
}
320
 
321
// ----------------------------------------------------------------------------
322
// A utility function, primarily intended to be called from inside gdb.
323
extern "C" void
324
cyg_assert_quickfail(void)
325
{
326
    cyg_assert_fail("gdb", "<no file>", 0, "manual call");
327
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.