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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rtems/] [c/] [src/] [lib/] [libc/] [error.c] - Blame information for rev 1767

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

Line No. Rev Author Line
1 158 chris
/*
2
 *  report errors and panics to RTEMS' stderr.
3
 *  Currently just used by RTEMS monitor.
4
 *
5 208 chris
 *  $Id: error.c,v 1.2 2001-09-27 12:01:15 chris Exp $
6 158 chris
 */
7
 
8
 
9
/*
10
 * These routines provide general purpose error reporting.
11
 * rtems_error reports an error to stderr and allows use of
12
 * printf style formatting.  A newline is appended to all messages.
13
 *
14
 * error_flag can be specified as any of the following:
15
 *
16
 *      RTEMS_ERROR_ERRNO       -- include errno text in output
17
 *      RTEMS_ERROR_PANIC       -- halts local system after output
18
 *      RTEMS_ERROR_ABORT       -- abort after output
19
 *
20
 * It can also include a rtems_status value which can be OR'd
21
 * with the above flags. *
22
 *
23
 * EXAMPLE
24
 *      #include <rtems.h>
25
 *      #include <rtems/error.h>
26
 *      rtems_error(0, "stray interrupt %d", intr);
27
 *
28
 * EXAMPLE
29
 *        if ((status = rtems_task_create(...)) != RTEMS_SUCCCESSFUL)
30
 *        {
31
 *            rtems_error(status | RTEMS_ERROR_ABORT,
32
 *                        "could not create task");
33
 *        }
34
 *
35
 * EXAMPLE
36
 *        if ((fd = open(pathname, O_RDNLY)) < 0)
37
 *        {
38
 *            rtems_error(RTEMS_ERROR_ERRNO, "open of '%s' failed", pathname);
39
 *            goto failed;
40
 *        }
41
 */
42
 
43
#define __RTEMS_VIOLATE_KERNEL_VISIBILITY__
44
#include <rtems.h>
45
 
46
#include "error.h"
47
#include <rtems/assoc.h>
48
 
49
#include <stdio.h>
50
#include <stdarg.h>
51
#include <errno.h>
52
#include <stdlib.h>
53
#include <string.h>
54
#include <unistd.h>             /* _exit() */
55
 
56
/* bug in hpux <errno.h>: no prototypes unless you are C++ */
57
#ifdef hpux9
58
char *strerror(int);
59
#endif
60
 
61
extern char *rtems_progname;
62
int          rtems_panic_in_progress;
63
 
64
rtems_assoc_t rtems_status_assoc[] = {
65
    { "successful completion",              RTEMS_SUCCESSFUL, },
66
    { "returned from a thread",             RTEMS_TASK_EXITTED, },
67
    { "multiprocessing not configured",     RTEMS_MP_NOT_CONFIGURED, },
68
    { "invalid object name",                RTEMS_INVALID_NAME, },
69
    { "invalid object id",                  RTEMS_INVALID_ID, },
70
    { "too many",                           RTEMS_TOO_MANY, },
71
    { "timed out waiting",                  RTEMS_TIMEOUT, },
72
    { "object deleted while waiting",       RTEMS_OBJECT_WAS_DELETED, },
73
    { "specified size was invalid",         RTEMS_INVALID_SIZE, },
74
    { "address specified is invalid",       RTEMS_INVALID_ADDRESS, },
75
    { "number was invalid",                 RTEMS_INVALID_NUMBER, },
76
    { "item has not been initialized",      RTEMS_NOT_DEFINED, },
77
    { "resources still outstanding",        RTEMS_RESOURCE_IN_USE, },
78
    { "request not satisfied",              RTEMS_UNSATISFIED, },
79
    { "thread is in wrong state",           RTEMS_INCORRECT_STATE, },
80
    { "thread already in state",            RTEMS_ALREADY_SUSPENDED, },
81
    { "illegal on calling thread",          RTEMS_ILLEGAL_ON_SELF, },
82
    { "illegal for remote object",          RTEMS_ILLEGAL_ON_REMOTE_OBJECT, },
83
    { "called from wrong environment",      RTEMS_CALLED_FROM_ISR, },
84
    { "invalid thread priority",            RTEMS_INVALID_PRIORITY, },
85
    { "invalid date/time",                  RTEMS_INVALID_CLOCK, },
86
    { "invalid node id",                    RTEMS_INVALID_NODE, },
87
    { "directive not configured",           RTEMS_NOT_CONFIGURED, },
88
    { "not owner of resource",              RTEMS_NOT_OWNER_OF_RESOURCE , },
89
    { "directive not implemented",          RTEMS_NOT_IMPLEMENTED, },
90
    { "RTEMS inconsistency detected",       RTEMS_INTERNAL_ERROR, },
91
    { "could not get enough memory",        RTEMS_NO_MEMORY, },
92
    { "driver IO error",                    RTEMS_IO_ERROR, },
93
    { "internal multiprocessing only",      THREAD_STATUS_PROXY_BLOCKING, },
94
    { 0, 0, 0 },
95
};
96
 
97
 
98
const char *
99
rtems_status_text(
100
    rtems_status_code status
101
)
102
{
103
    return rtems_assoc_name_by_local(rtems_status_assoc, status);
104
}
105
 
106
 
107
static int rtems_verror(
108
    unsigned32   error_flag,
109
    const char   *printf_format,
110
    va_list      arglist
111
)
112
{
113
    int               local_errno = 0;
114
    int               chars_written = 0;
115
    rtems_status_code status;
116
 
117
    if (error_flag & RTEMS_ERROR_PANIC)
118
    {
119
        if (rtems_panic_in_progress++)
120
            _Thread_Disable_dispatch();       /* disable task switches */
121
 
122
        /* don't aggravate things */
123
        if (rtems_panic_in_progress > 2)
124
            return 0;
125
    }
126
 
127
    (void) fflush(stdout);          /* in case stdout/stderr same */
128
 
129
    status = error_flag & ~RTEMS_ERROR_MASK;
130
    if (error_flag & RTEMS_ERROR_ERRNO)     /* include errno? */
131
        local_errno = errno;
132
 
133
    if (_System_state_Is_multiprocessing)
134
        fprintf(stderr, "[%d] ", _Configuration_MP_table->node);
135
 
136
    if (rtems_progname && *rtems_progname)
137
        chars_written += fprintf(stderr, "%s: ", rtems_progname);
138
    chars_written += vfprintf(stderr, printf_format, arglist);
139
 
140
    if (status)
141
        chars_written += fprintf(stderr, " (status: %s)", rtems_status_text(status));
142
 
143
    if (local_errno)
144
    {
145
      if ((local_errno > 0) && *strerror(local_errno))
146
        chars_written += fprintf(stderr, " (errno: %s)", strerror(local_errno));
147
      else
148
        chars_written += fprintf(stderr, " (unknown errno=%d)", local_errno);
149
    }
150
 
151
    chars_written += fprintf(stderr, "\n");
152
 
153
    (void) fflush(stderr);
154
 
155
    if (error_flag & (RTEMS_ERROR_PANIC | RTEMS_ERROR_ABORT))
156
    {
157
        if (error_flag & RTEMS_ERROR_PANIC)
158
        {
159
            rtems_error(0, "fatal error, exiting");
160
            _exit(local_errno);
161
        }
162
        else
163
        {
164
            rtems_error(0, "fatal error, aborting");
165
            abort();
166
        }
167
    }
168
    return chars_written;
169
}
170
 
171
 
172
/*
173
 * Report an error.
174
 * error_flag is as above; printf_format is a normal
175
 * printf(3) format string, with its concommitant arguments.
176
 *
177
 * Returns the number of characters written.
178
 */
179
 
180
int rtems_error(
181
    int   error_flag,
182
    const char *printf_format,
183
    ...
184
  )
185
{
186
    va_list arglist;
187
    int chars_written;
188
 
189
    va_start(arglist, printf_format);
190
    chars_written = rtems_verror(error_flag, printf_format, arglist);
191
    va_end(arglist);
192
 
193
    return chars_written;
194
}
195
 
196
/*
197
 * rtems_panic is shorthand for rtems_error(RTEMS_ERROR_PANIC, ...)
198
 */
199
 
200
void rtems_panic(
201
    const char *printf_format,
202
    ...
203
  )
204
{
205
    va_list arglist;
206
 
207
    va_start(arglist, printf_format);
208
    (void) rtems_verror(RTEMS_ERROR_PANIC, printf_format, arglist);
209
    va_end(arglist);
210
}

powered by: WebSVN 2.1.0

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