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