source: rtems/c/src/lib/libc/error.c @ 4de102cc

4.104.114.84.95
Last change on this file since 4de102cc was 9c49db4, checked in by Joel Sherrill <joel.sherrill@…>, on 01/08/01 at 18:26:44

2001-01-08 Ralf Corsepius <corsepiu@…>

  • configure.in: Add libc/config.h
  • libc/Makefile.am: Add INCLUDES += -I. to pickup config.h
  • libc/.cvsignore: Add config.h and stamp-h
  • libc/*.c: Add config.h support.
  • 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
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
64extern char *rtems_progname;
65int          rtems_panic_in_progress;
66
67rtems_assoc_t rtems_status_assoc[] = {
68    { "successful completion",              RTEMS_SUCCESSFUL, },
69    { "returned from a thread",             RTEMS_TASK_EXITTED, },
70    { "multiprocessing not configured",     RTEMS_MP_NOT_CONFIGURED, },
71    { "invalid object name",                RTEMS_INVALID_NAME, },
72    { "invalid object id",                  RTEMS_INVALID_ID, },
73    { "too many",                           RTEMS_TOO_MANY, },
74    { "timed out waiting",                  RTEMS_TIMEOUT, },
75    { "object deleted while waiting",       RTEMS_OBJECT_WAS_DELETED, },
76    { "specified size was invalid",         RTEMS_INVALID_SIZE, },
77    { "address specified is invalid",       RTEMS_INVALID_ADDRESS, },
78    { "number was invalid",                 RTEMS_INVALID_NUMBER, },
79    { "item has not been initialized",      RTEMS_NOT_DEFINED, },
80    { "resources still outstanding",        RTEMS_RESOURCE_IN_USE, },
81    { "request not satisfied",              RTEMS_UNSATISFIED, },
82    { "thread is in wrong state",           RTEMS_INCORRECT_STATE, },
83    { "thread already in state",            RTEMS_ALREADY_SUSPENDED, },
84    { "illegal on calling thread",          RTEMS_ILLEGAL_ON_SELF, },
85    { "illegal for remote object",          RTEMS_ILLEGAL_ON_REMOTE_OBJECT, },
86    { "called from wrong environment",      RTEMS_CALLED_FROM_ISR, },
87    { "invalid thread priority",            RTEMS_INVALID_PRIORITY, },
88    { "invalid date/time",                  RTEMS_INVALID_CLOCK, },
89    { "invalid node id",                    RTEMS_INVALID_NODE, },
90    { "directive not configured",           RTEMS_NOT_CONFIGURED, },
91    { "not owner of resource",              RTEMS_NOT_OWNER_OF_RESOURCE , },
92    { "directive not implemented",          RTEMS_NOT_IMPLEMENTED, },
93    { "RTEMS inconsistency detected",       RTEMS_INTERNAL_ERROR, },
94    { "could not get enough memory",        RTEMS_NO_MEMORY, },
95    { "driver IO error",                    RTEMS_IO_ERROR, },
96    { "internal multiprocessing only",      THREAD_STATUS_PROXY_BLOCKING, },
97    { 0, 0, 0 },
98};
99
100
101const char *
102rtems_status_text(
103    rtems_status_code status
104)
105{
106    return rtems_assoc_name_by_local(rtems_status_assoc, status);
107}
108
109
110static int rtems_verror(
111    unsigned32   error_flag,
112    const char   *printf_format,
113    va_list      arglist
114)
115{
116    int               local_errno = 0;
117    int               chars_written = 0;
118    rtems_status_code status;
119
120    if (error_flag & RTEMS_ERROR_PANIC)
121    {
122        if (rtems_panic_in_progress++)
123            _Thread_Disable_dispatch();       /* disable task switches */
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    {
148      if ((local_errno > 0) && *strerror(local_errno))
149        chars_written += fprintf(stderr, " (errno: %s)", strerror(local_errno));
150      else
151        chars_written += fprintf(stderr, " (unknown errno=%d)", local_errno);
152    }
153   
154    chars_written += fprintf(stderr, "\n");
155
156    (void) fflush(stderr);
157
158    if (error_flag & (RTEMS_ERROR_PANIC | RTEMS_ERROR_ABORT))
159    {
160        if (error_flag & RTEMS_ERROR_PANIC)
161        {
162            rtems_error(0, "fatal error, exiting");
163            _exit(local_errno);
164        }
165        else
166        {
167            rtems_error(0, "fatal error, aborting");
168            abort();
169        }
170    }
171    return chars_written;
172}
173
174
175/*
176 * Report an error.
177 * error_flag is as above; printf_format is a normal
178 * printf(3) format string, with its concommitant arguments.
179 *
180 * Returns the number of characters written.
181 */
182
183int rtems_error(
184    int   error_flag,
185    const char *printf_format,
186    ...
187  )
188{
189    va_list arglist;
190    int chars_written;
191
192    va_start(arglist, printf_format);
193    chars_written = rtems_verror(error_flag, printf_format, arglist);
194    va_end(arglist);
195   
196    return chars_written;
197}
198
199/*
200 * rtems_panic is shorthand for rtems_error(RTEMS_ERROR_PANIC, ...)
201 */
202
203void rtems_panic(
204    const char *printf_format,
205    ...
206  )
207{
208    va_list arglist;
209
210    va_start(arglist, printf_format);
211    (void) rtems_verror(RTEMS_ERROR_PANIC, printf_format, arglist);
212    va_end(arglist);
213}
Note: See TracBrowser for help on using the repository browser.