source: rtems/c/src/exec/libcsupport/src/error.c @ 2936b425

4.104.114.84.95
Last change on this file since 2936b425 was 2936b425, checked in by Joel Sherrill <joel.sherrill@…>, on 01/23/98 at 17:45:05

Solaris port updates from Chris Johns

  • Property mode set to 100644
File size: 6.1 KB
Line 
1/*
2 *  report errors and panics to RTEMS' stderr.
3 *  Currently just used by RTEMS monitor.
4 *
5 *  $Id$
6 */
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
58char *strerror(int);
59#endif
60
61extern char *rtems_progname;
62int          rtems_panic_in_progress;
63
64rtems_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    { "internal multiprocessing only",      THREAD_STATUS_PROXY_BLOCKING, },
93    { 0, 0, 0 },
94};
95
96
97const char *
98rtems_status_text(
99    rtems_status_code status
100)
101{
102    return rtems_assoc_name_by_local(rtems_status_assoc, status);
103}
104
105
106static int rtems_verror(
107    unsigned32   error_flag,
108    const char   *printf_format,
109    va_list      arglist
110)
111{
112    int               local_errno = 0;
113    int               chars_written = 0;
114    rtems_status_code status;
115
116    if (error_flag & RTEMS_ERROR_PANIC)
117    {
118        rtems_panic_in_progress++;
119
120        /* disable task switches */
121        _Thread_Disable_dispatch();
122
123        /* don't aggravate things */
124        if (rtems_panic_in_progress > 2)
125            return 0;
126    }
127
128    (void) fflush(stdout);          /* in case stdout/stderr same */
129
130    status = error_flag & ~RTEMS_ERROR_MASK;
131    if (error_flag & RTEMS_ERROR_ERRNO)     /* include errno? */
132        local_errno = errno;
133
134    if (_System_state_Is_multiprocessing)
135        fprintf(stderr, "[%d] ", _Configuration_MP_table->node);
136   
137    if (rtems_progname && *rtems_progname)
138        chars_written += fprintf(stderr, "%s: ", rtems_progname);
139    chars_written += vfprintf(stderr, printf_format, arglist);
140   
141    if (status)
142        chars_written += fprintf(stderr, " (status: %s)", rtems_status_text(status));
143
144    if (local_errno)
145    {
146      if ((local_errno > 0) && *strerror(local_errno))
147        chars_written += fprintf(stderr, " (errno: %s)", strerror(local_errno));
148      else
149        chars_written += fprintf(stderr, " (unknown errno=%d)", local_errno);
150    }
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.