source: rtems/cpukit/libcsupport/src/error.c @ ccec63e

4.104.114.95
Last change on this file since ccec63e was ccec63e, checked in by Joel Sherrill <joel.sherrill@…>, on 08/20/08 at 19:31:24

2008-08-20 Joel Sherrill <joel.sherrill@…>

  • libcsupport/src/error.c, libmisc/dummy/dummy.c, rtems/src/rtemsobjectgetapiclassname.c, rtems/src/rtemsobjectgetapiname.c: Add 0 for missing initializers.
  • Property mode set to 100644
File size: 6.2 KB
Line 
1/*
2 *  report errors and panics to RTEMS' stderr.
3 *  Currently just used by RTEMS monitor.
4 *
5 *  $Id$
6 */
7
8#if HAVE_CONFIG_H
9#include "config.h"
10#endif
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#define __RTEMS_VIOLATE_KERNEL_VISIBILITY__
47#include <rtems.h>
48
49#include <rtems/error.h>
50#include <rtems/assoc.h>
51#include <inttypes.h>
52#include <stdio.h>
53#include <stdarg.h>
54#include <errno.h>
55#include <stdlib.h>
56#include <string.h>
57#include <unistd.h>             /* _exit() */
58
59/* bug in hpux <errno.h>: no prototypes unless you are C++ */
60#ifdef hpux9
61char *strerror(int);
62#endif
63
64int          rtems_panic_in_progress;
65
66const rtems_assoc_t rtems_status_assoc[] = {
67    { "successful completion",              RTEMS_SUCCESSFUL, 0 },
68    { "returned from a thread",             RTEMS_TASK_EXITTED, 0 },
69    { "multiprocessing not configured",     RTEMS_MP_NOT_CONFIGURED, 0 },
70    { "invalid object name",                RTEMS_INVALID_NAME, 0 },
71    { "invalid object id",                  RTEMS_INVALID_ID, 0 },
72    { "too many",                           RTEMS_TOO_MANY, 0 },
73    { "timed out waiting",                  RTEMS_TIMEOUT, 0 },
74    { "object deleted while waiting",       RTEMS_OBJECT_WAS_DELETED, 0 },
75    { "specified size was invalid",         RTEMS_INVALID_SIZE, 0 },
76    { "address specified is invalid",       RTEMS_INVALID_ADDRESS, 0 },
77    { "number was invalid",                 RTEMS_INVALID_NUMBER, 0 },
78    { "item has not been initialized",      RTEMS_NOT_DEFINED, 0 },
79    { "resources still outstanding",        RTEMS_RESOURCE_IN_USE, 0 },
80    { "request not satisfied",              RTEMS_UNSATISFIED, 0 },
81    { "thread is in wrong state",           RTEMS_INCORRECT_STATE, 0 },
82    { "thread already in state",            RTEMS_ALREADY_SUSPENDED, 0 },
83    { "illegal on calling thread",          RTEMS_ILLEGAL_ON_SELF, 0 },
84    { "illegal for remote object",          RTEMS_ILLEGAL_ON_REMOTE_OBJECT, 0 },
85    { "called from wrong environment",      RTEMS_CALLED_FROM_ISR, 0 },
86    { "invalid thread priority",            RTEMS_INVALID_PRIORITY, 0 },
87    { "invalid date/time",                  RTEMS_INVALID_CLOCK, 0 },
88    { "invalid node id",                    RTEMS_INVALID_NODE, 0 },
89    { "directive not configured",           RTEMS_NOT_CONFIGURED, 0 },
90    { "not owner of resource",              RTEMS_NOT_OWNER_OF_RESOURCE , 0 },
91    { "directive not implemented",          RTEMS_NOT_IMPLEMENTED, 0 },
92    { "RTEMS inconsistency detected",       RTEMS_INTERNAL_ERROR, 0 },
93    { "could not get enough memory",        RTEMS_NO_MEMORY, 0 },
94    { "driver IO error",                    RTEMS_IO_ERROR, 0 },
95    { "internal multiprocessing only",      THREAD_STATUS_PROXY_BLOCKING, 0 },
96    { 0, 0, 0 },
97};
98
99
100const char *
101rtems_status_text(
102    rtems_status_code status
103)
104{
105    return rtems_assoc_name_by_local(rtems_status_assoc, status);
106}
107
108
109static int rtems_verror(
110    uint32_t     error_flag,
111    const char   *printf_format,
112    va_list      arglist
113)
114{
115    int               local_errno = 0;
116    int               chars_written = 0;
117    rtems_status_code status;
118
119    if (error_flag & RTEMS_ERROR_PANIC)
120    {
121        if (rtems_panic_in_progress++)
122            _Thread_Disable_dispatch();       /* disable task switches */
123
124        /* don't aggravate things */
125        if (rtems_panic_in_progress > 2)
126            return 0;
127    }
128
129    (void) fflush(stdout);          /* in case stdout/stderr same */
130
131    status = error_flag & ~RTEMS_ERROR_MASK;
132    if (error_flag & RTEMS_ERROR_ERRNO)     /* include errno? */
133        local_errno = errno;
134
135    #if defined(RTEMS_MULTIPROCESSING)
136      if (_System_state_Is_multiprocessing)
137        fprintf(stderr, "[%" PRIu32 "] ", _Configuration_MP_table->node);
138    #endif
139
140    chars_written += vfprintf(stderr, printf_format, arglist);
141
142    if (status)
143        chars_written += fprintf(stderr, " (status: %s)", rtems_status_text(status));
144
145    if (local_errno)
146    {
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
153    chars_written += fprintf(stderr, "\n");
154
155    (void) fflush(stderr);
156
157    if (error_flag & (RTEMS_ERROR_PANIC | RTEMS_ERROR_ABORT))
158    {
159        if (error_flag & RTEMS_ERROR_PANIC)
160        {
161            rtems_error(0, "fatal error, exiting");
162            _exit(local_errno);
163        }
164        else
165        {
166            rtems_error(0, "fatal error, aborting");
167            abort();
168        }
169    }
170    return chars_written;
171}
172
173
174/*
175 * Report an error.
176 * error_flag is as above; printf_format is a normal
177 * printf(3) format string, with its concommitant arguments.
178 *
179 * Returns the number of characters written.
180 */
181
182int rtems_error(
183    int   error_flag,
184    const char *printf_format,
185    ...
186  )
187{
188    va_list arglist;
189    int chars_written;
190
191    va_start(arglist, printf_format);
192    chars_written = rtems_verror(error_flag, printf_format, arglist);
193    va_end(arglist);
194
195    return chars_written;
196}
197
198/*
199 * rtems_panic is shorthand for rtems_error(RTEMS_ERROR_PANIC, ...)
200 */
201
202void rtems_panic(
203    const char *printf_format,
204    ...
205  )
206{
207    va_list arglist;
208
209    va_start(arglist, printf_format);
210    (void) rtems_verror(RTEMS_ERROR_PANIC, printf_format, arglist);
211    va_end(arglist);
212}
Note: See TracBrowser for help on using the repository browser.