source: rtems/c/src/lib/libmisc/error/error.c @ 6661eaa1

4.104.114.84.95
Last change on this file since 6661eaa1 was 6661eaa1, checked in by Joel Sherrill <joel.sherrill@…>, on Jan 15, 1996 at 11:06:16 PM

Split error handler into its own directory.

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