summaryrefslogtreecommitdiffstats
path: root/cpukit/libcsupport/src/error.c
blob: 0e63fa40220eff07cf479f23332699e16213a810 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 *  @file
 *
 *  @brief Error and Panic Report Support
 *  @ingroup ErrorPanicSupport
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems.h>
#include <rtems/error.h>
#include <rtems/assoc.h>
#include <rtems/score/sysstate.h>
#include <rtems/score/threadimpl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>     /* _exit() */

int          rtems_panic_in_progress;

const rtems_assoc_t rtems_status_assoc[] = {
  { "successful completion",              RTEMS_SUCCESSFUL, 0 },
  { "returned from a thread",             RTEMS_TASK_EXITTED, 0 },
  { "multiprocessing not configured",     RTEMS_MP_NOT_CONFIGURED, 0 },
  { "invalid object name",                RTEMS_INVALID_NAME, 0 },
  { "invalid object id",                  RTEMS_INVALID_ID, 0 },
  { "too many",                           RTEMS_TOO_MANY, 0 },
  { "timed out waiting",                  RTEMS_TIMEOUT, 0 },
  { "object deleted while waiting",       RTEMS_OBJECT_WAS_DELETED, 0 },
  { "specified size was invalid",         RTEMS_INVALID_SIZE, 0 },
  { "address specified is invalid",       RTEMS_INVALID_ADDRESS, 0 },
  { "number was invalid",                 RTEMS_INVALID_NUMBER, 0 },
  { "item has not been initialized",      RTEMS_NOT_DEFINED, 0 },
  { "resources still outstanding",        RTEMS_RESOURCE_IN_USE, 0 },
  { "request not satisfied",              RTEMS_UNSATISFIED, 0 },
  { "thread is in wrong state",           RTEMS_INCORRECT_STATE, 0 },
  { "thread already in state",            RTEMS_ALREADY_SUSPENDED, 0 },
  { "illegal on calling thread",          RTEMS_ILLEGAL_ON_SELF, 0 },
  { "illegal for remote object",          RTEMS_ILLEGAL_ON_REMOTE_OBJECT, 0 },
  { "called from wrong environment",      RTEMS_CALLED_FROM_ISR, 0 },
  { "invalid thread priority",            RTEMS_INVALID_PRIORITY, 0 },
  { "invalid date/time",                  RTEMS_INVALID_CLOCK, 0 },
  { "invalid node id",                    RTEMS_INVALID_NODE, 0 },
  { "directive not configured",           RTEMS_NOT_CONFIGURED, 0 },
  { "not owner of resource",              RTEMS_NOT_OWNER_OF_RESOURCE , 0 },
  { "directive not implemented",          RTEMS_NOT_IMPLEMENTED, 0 },
  { "RTEMS inconsistency detected",       RTEMS_INTERNAL_ERROR, 0 },
  { "could not get enough memory",        RTEMS_NO_MEMORY, 0 },
  { "driver IO error",                    RTEMS_IO_ERROR, 0 },
  { "internal multiprocessing only",      THREAD_STATUS_PROXY_BLOCKING, 0 },
  { 0, 0, 0 },
};


const char *rtems_status_text(
  rtems_status_code status
)
{
  return rtems_assoc_name_by_local(rtems_status_assoc, status);
}


int rtems_verror(
  rtems_error_code_t  error_flag,
  const char         *printf_format,
  va_list             arglist
)
{
  int               local_errno = 0;
  int               chars_written = 0;
  rtems_status_code status;

  if (error_flag & RTEMS_ERROR_PANIC) {
    if (rtems_panic_in_progress++)
      _Thread_Disable_dispatch();       /* disable task switches */

    /* don't aggravate things */
    if (rtems_panic_in_progress > 2)
      return 0;
  }

  (void) fflush(stdout);            /* in case stdout/stderr same */

  status = error_flag & ~RTEMS_ERROR_MASK;
  if (error_flag & RTEMS_ERROR_ERRNO)     /* include errno? */
    local_errno = errno;

  #if defined(RTEMS_MULTIPROCESSING)
    if (_System_state_Is_multiprocessing)
      fprintf(stderr, "[%" PRIu32 "] ", _Configuration_MP_table->node);
  #endif

  chars_written += vfprintf(stderr, printf_format, arglist);

  if (status)
    chars_written +=
      fprintf(stderr, " (status: %s)", rtems_status_text(status));

  if (local_errno) {
    if ((local_errno > 0) && *strerror(local_errno))
      chars_written += fprintf(stderr, " (errno: %s)", strerror(local_errno));
    else
      chars_written += fprintf(stderr, " (unknown errno=%d)", local_errno);
  }

  chars_written += fprintf(stderr, "\n");

  (void) fflush(stderr);

  return chars_written;
}

int rtems_error(
  rtems_error_code_t error_flag,
  const char *printf_format,
  ...
)
{
  va_list arglist;
  int chars_written;

  va_start(arglist, printf_format);
  chars_written = rtems_verror(error_flag, printf_format, arglist);
  va_end(arglist);

  if (error_flag & RTEMS_ERROR_PANIC) {
    rtems_error(0, "fatal error, exiting");
    _exit(errno);
  }
  if (error_flag & RTEMS_ERROR_ABORT) {
    rtems_error(0, "fatal error, aborting");
    abort();
  }

  return chars_written;
}

void rtems_panic(
  const char *printf_format,
  ...
)
{
  va_list arglist;

  va_start(arglist, printf_format);
  (void) rtems_verror(RTEMS_ERROR_PANIC, printf_format, arglist);
  va_end(arglist);

  rtems_error(0, "fatal error, exiting");
  _exit(errno);
}