source: rtems-docs/c-user/fatal_error.rst @ 3bb3e57

5
Last change on this file since 3bb3e57 was 674b714, checked in by Sebastian Huber <sebastian.huber@…>, on 11/22/17 at 12:42:18

c-user: Document global construction

Close #3243.

  • Property mode set to 100644
File size: 21.2 KB
Line 
1.. comment SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. COMMENT: COPYRIGHT (c) 1988-2008.
4.. COMMENT: On-Line Applications Research Corporation (OAR).
5.. COMMENT: All rights reserved.
6
7.. index:: fatal errors
8
9.. _fatal_error_manager:
10
11Fatal Error Manager
12*******************
13
14Introduction
15============
16
17The fatal error manager processes all fatal or irrecoverable errors and other
18sources of system termination (for example after :c:func:`exit()`).  Fatal
19errors are identified by the (fatal source, error code) pair.  The directives
20provided by the fatal error manager are:
21
22- rtems_fatal_ - Invoke the fatal error handler
23
24- rtems_panic_ - Print a message and invoke the fatal error handler
25
26- rtems_shutdown_executive_ - Shutdown RTEMS
27
28- rtems_exception_frame_print_ - Print the CPU exception frame
29
30- rtems_fatal_source_text_ - Return the fatal source text
31
32- rtems_internal_error_text_ - Return the error code text
33
34- rtems_fatal_error_occurred_ - Invoke the fatal error handler (deprecated)
35
36Background
37==========
38
39.. index:: fatal error detection
40.. index:: fatal error processing
41.. index:: fatal error user extension
42
43Overview
44--------
45
46The fatal error manager is called upon detection of an irrecoverable error
47condition by either RTEMS or the application software.  Fatal errors are also
48used in case it is difficult or impossible to return an error condition by
49other means, e.g. a return value of a directive call.  Fatal errors can be
50detected from various sources, for example
51
52- the executive (RTEMS),
53- support libraries,
54- user system code, and
55- user application code.
56
57RTEMS automatically invokes the fatal error manager upon detection of an error
58it considers to be fatal.  Similarly, the user should invoke the fatal error
59manager upon detection of a fatal error.
60
61Each static or dynamic user extension set may include a fatal error handler.
62The fatal error handler in the static extension set can be used to provide
63access to debuggers and monitors which may be present on the target hardware.
64If any user-supplied fatal error handlers are installed, the fatal error
65manager will invoke them.  Usually, the board support package provides a fatal
66error extesion which resets the board.  If no user handlers are configured or
67if all the user handler return control to the fatal error manager, then the
68RTEMS default fatal error handler is invoked.  If the default fatal error
69handler is invoked, then the system state is marked as failed.
70
71Although the precise behavior of the default fatal error handler is processor
72specific, in general, it will disable all maskable interrupts, place the error
73code in a known processor dependent place (generally either on the stack or in
74a register), and halt the processor.  The precise actions of the RTEMS fatal
75error are discussed in the Default Fatal Error Processing chapter of the
76Applications Supplement document for a specific target processor.
77
78Fatal Sources
79-------------
80
81The following fatal sources are defined for RTEMS via the
82:c:type:`rtems_fatal_source` enumeration.  Each symbolic name has the
83corresponding numeric fatal source in parenthesis.
84
85INTERNAL_ERROR_CORE (0)
86    Errors of the core operating system.  See :ref:`internal_errors`.
87
88INTERNAL_ERROR_RTEMS_API (1)
89    Errors of the Classic API.
90
91INTERNAL_ERROR_POSIX_API (2)
92    Errors of the POSIX API.
93
94RTEMS_FATAL_SOURCE_BDBUF (3)
95    Fatal source for the block device cache.  See
96    :c:type:`rtems_bdbuf_fatal_code`.
97
98RTEMS_FATAL_SOURCE_APPLICATION (4)
99    Fatal source for application-specific errors.  The fatal code is
100    application-specific.
101
102RTEMS_FATAL_SOURCE_EXIT (5)
103    Fatal source of :c:func:`exit()`.  The fatal code is the :c:func:`exit()`
104    status code.
105
106RTEMS_FATAL_SOURCE_BSP (6)
107    Fatal source for BSP errors.  The fatal codes are defined in
108    :file:`<bsp/fatal.h>`.  Examples are interrupt and exception
109    initialization.  See :c:type:`bsp_fatal_code` and :c:func:`bsp_fatal()`.
110
111RTEMS_FATAL_SOURCE_ASSERT (7)
112    Fatal source of :c:macro:`assert()`.  The fatal code is the pointer value
113    of the assert context.  See :c:type:`rtems_assert_context`.
114
115RTEMS_FATAL_SOURCE_STACK_CHECKER (8)
116    Fatal source of the stack checker.  The fatal code is the object name of
117    the executing task.
118
119RTEMS_FATAL_SOURCE_EXCEPTION (9)
120    Fatal source of the exceptions.  The fatal code is the pointer value of the
121    exception frame pointer.  See :c:type:`rtems_exception_frame` and
122    :ref:`rtems_exception_frame_print`.
123
124RTEMS_FATAL_SOURCE_SMP (10)
125    Fatal source of SMP domain.  See :c:type:`SMP_Fatal_code`.
126
127RTEMS_FATAL_SOURCE_PANIC (11)
128    Fatal source of :c:func:`rtems_panic`, see :ref:`rtems_panic`.
129
130.. _internal_errors:
131
132Internal Error Codes
133--------------------
134
135The following error codes are defined for the :c:data:`INTERNAL_ERROR_CORE`
136fatal source.  Each symbolic name has the corresponding numeric error code in
137parenthesis.
138
139INTERNAL_ERROR_TOO_LITTLE_WORKSPACE (2)
140    There is not enough memory for the workspace.  This fatal error may occur
141    during system initialization.  It is an application configuration error.
142
143INTERNAL_ERROR_WORKSPACE_ALLOCATION (3)
144    An allocation from the workspace failed.  This fatal error may occur during
145    system initialization.  It is an application configuration error.
146
147INTERNAL_ERROR_INTERRUPT_STACK_TOO_SMALL (4)
148    The configured interrupt stack size is too small.  This fatal error may
149    occur during system initialization.  It is an application configuration
150    error.
151
152INTERNAL_ERROR_THREAD_EXITTED (5)
153    A non-POSIX thread entry function returned.  This is an API usage error.
154
155    An example code to provoke this fatal error is:
156
157    .. code-block:: c
158
159        void task( rtems_arg arg )
160        {
161          /* Classic API tasks must not return */
162        }
163
164        void create_bad_task( void )
165        {
166          rtems_status_code sc;
167          rtems_id          task_id;
168
169          sc = rtems_task_create(
170            rtems_build_name('T', 'A', 'S', 'K'),
171            1,
172            RTEMS_DEFAULT_MODES,
173            RTEMS_DEFAULT_ATTRIBUTES,
174            &task_id
175          );
176          assert( sc == RTEMS_SUCCESSFUL );
177
178          sc = rtems_task_start( task_id, task, 0 );
179          assert( sc == RTEMS_SUCCESSFUL );
180        }
181
182INTERNAL_ERROR_INCONSISTENT_MP_INFORMATION (6)
183    This fatal error can only occur on MPCI configurations.  The MPCI nodes or
184    global objects configuration is inconsistent.  This fatal error may occur
185    during system initialization.  It is an application configuration error.
186
187INTERNAL_ERROR_INVALID_NODE (7)
188    This fatal error can only occur on MPCI configurations.  The own MPCI node
189    number is invalid.  This fatal error may occur during system
190    initialization.  It is an application configuration error.
191
192INTERNAL_ERROR_NO_MPCI (8)
193    This fatal error can only occur on MPCI configurations.  There is no MPCI
194    configuration table.  This fatal error may occur during system
195    initialization.  It is an application configuration error.
196
197INTERNAL_ERROR_BAD_PACKET (9)
198    This fatal error can only occur on MPCI configurations.  The MPCI server
199    thread received a bad packet.
200
201INTERNAL_ERROR_OUT_OF_PACKETS (10)
202    This fatal error can only occur on MPCI configurations.  The MPCI packet
203    pool is empty.  It is an application configuration error.
204
205INTERNAL_ERROR_OUT_OF_GLOBAL_OBJECTS (11)
206    This fatal error can only occur on MPCI configurations.  The MPCI global
207    objects pool is empty.  It is an application configuration error.
208
209INTERNAL_ERROR_OUT_OF_PROXIES (12)
210    This fatal error can only occur on MPCI configurations.  The MPCI thread
211    proxy pool is empty.  It is an application configuration error.
212
213INTERNAL_ERROR_INVALID_GLOBAL_ID (13)
214    This fatal error can only occur on MPCI configurations.  The system cannot
215    find the global object for a specific object identifier.  In case this
216    happens, then this is probably an operating system bug.
217
218INTERNAL_ERROR_BAD_STACK_HOOK (14)
219    The stack allocator hook or stack free hook is NULL.  This fatal error may
220    occur during system initialization.  It is an application configuration
221    error.
222
223INTERNAL_ERROR_UNLIMITED_AND_MAXIMUM_IS_0 (19)
224    An object class is configured to use the unlimited objects option, however,
225    the count of objects for each extension is zero.  This fatal error may
226    occur during system initialization.  It is an application configuration
227    error.
228
229INTERNAL_ERROR_NO_MEMORY_FOR_HEAP (23)
230    There is not enough memory for the C program heap.  This fatal error may
231    occur during system initialization.  It is an application configuration
232    error.
233
234INTERNAL_ERROR_CPU_ISR_INSTALL_VECTOR (24)
235    The use of :c:func:`_CPU_ISR_install_vector()` is illegal on this system.
236
237INTERNAL_ERROR_RESOURCE_IN_USE (25)
238    This fatal error can only occur on debug configurations.  It happens in
239    case a thread which owns mutexes is deleted.  Mutexes owned by a deleted
240    thread are in an inconsistent state.
241
242INTERNAL_ERROR_RTEMS_INIT_TASK_ENTRY_IS_NULL (26)
243    An RTEMS initialization task entry function is NULL.  This fatal error may
244    occur during system initialization.  It is an application configuration
245    error.
246
247INTERNAL_ERROR_THREAD_QUEUE_DEADLOCK (28)
248    A deadlock was detected during a thread queue enqueue operation.
249
250INTERNAL_ERROR_THREAD_QUEUE_ENQUEUE_STICKY_FROM_BAD_STATE (29)
251    This fatal error can only happen in SMP configurations.  It is not allowed
252    to obtain MrsP semaphores in a context with thread dispatching disabled,
253    for example interrupt context.
254
255    An example code to provoke this fatal error is:
256
257    .. code-block:: c
258
259        void bad( rtems_id timer_id, void *arg )
260        {
261          rtems_id *sem_id;
262
263          sem_id = arg;
264
265          rtems_semaphore_obtain( *sem_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
266          assert( 0 );
267        }
268
269        void fire_bad_timer( rtems_task_argument arg )
270        {
271          rtems_status_code sc;
272          rtems_id          sem_id;
273          rtems_id          timer_id;
274
275          sc = rtems_semaphore_create(
276            rtems_build_name('M', 'R', 'S', 'P'),
277            1,
278            RTEMS_MULTIPROCESSOR_RESOURCE_SHARING
279              | RTEMS_BINARY_SEMAPHORE,
280            1,
281            &sem_id
282          );
283          assert( sc == RTEMS_SUCCESSFUL );
284
285          sc = rtems_timer_create(
286            rtems_build_name( 'E', 'V', 'I', 'L' ),
287            &timer_id
288          );
289          assert( sc == RTEMS_SUCCESSFUL );
290
291          sc = rtems_semaphore_obtain( sem_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
292          assert( sc == RTEMS_SUCCESSFUL );
293
294          sc = rtems_timer_fire_after( timer_id, 1, bad, &sem_id );
295          assert( sc == RTEMS_SUCCESSFUL );
296
297          rtems_task_wake_after( 2 );
298          assert( 0 );
299        }
300
301INTERNAL_ERROR_BAD_THREAD_DISPATCH_DISABLE_LEVEL (30)
302    It is illegal to call blocking operating system services with thread
303    dispatching disabled, for example in interrupt context.
304
305    An example code to provoke this fatal error is:
306
307    .. code-block:: c
308
309        void bad( rtems_id id, void *arg )
310        {
311          rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
312          assert( 0 );
313        }
314
315        void fire_bad_timer( void )
316        {
317          rtems_status_code sc;
318          rtems_id          id;
319
320          sc = rtems_timer_create(
321            rtems_build_name( 'E', 'V', 'I', 'L' ),
322            &id
323          );
324          assert( sc == RTEMS_SUCCESSFUL );
325
326          sc = rtems_timer_fire_after( id, 1, bad, NULL );
327          assert( sc == RTEMS_SUCCESSFUL );
328
329          rtems_task_wake_after( 2 );
330          assert( 0 );
331        }
332
333INTERNAL_ERROR_BAD_THREAD_DISPATCH_ENVIRONMENT (31)
334    In SMP configurations, it is a fatal error to call blocking operating
335    system with interrupts disabled, since this prevents delivery of
336    inter-processor interrupts.  This could lead to executing threads which are
337    not allowed to execute resulting in undefined system behaviour.
338
339    Some CPU ports, for example the ARM Cortex-M port, have a similar problem,
340    since the interrupt state is not a part of the thread context.
341
342    This fatal error is detected in the operating system core function
343    :c:func:`_Thread_Do_dispatch()` responsible to carry out a thread dispatch.
344
345    An example code to provoke this fatal error is:
346
347    .. code-block:: c
348
349        void bad( void )
350        {
351          rtems_interrupt_level level;
352
353          rtems_interrupt_local_disable( level );
354          rtems_task_suspend( RTEMS_SELF );
355          rtems_interrupt_local_enable( level  );
356        }
357
358INTERNAL_ERROR_RTEMS_INIT_TASK_CREATE_FAILED (32)
359    Creation of an RTEMS initialization task failed.  This fatal error may
360    occur during system initialization.  It is an application configuration
361    error.
362
363INTERNAL_ERROR_POSIX_INIT_THREAD_CREATE_FAILED (33)
364    Creation of a POSIX initialization thread failed.  This fatal error may
365    occur during system initialization.  It is an application configuration
366    error.
367
368INTERNAL_ERROR_LIBIO_USER_ENV_KEY_CREATE_FAILED (34)
369    Creation of the IO library user environment POSIX key failed.  This fatal
370    error may occur during system initialization.  It is an application
371    configuration error.
372
373INTERNAL_ERROR_LIBIO_SEM_CREATE_FAILED (35)
374    Creation of the IO library semaphore failed.  This fatal error may occur
375    during system initialization.  It is an application configuration error.
376
377INTERNAL_ERROR_LIBIO_STDOUT_FD_OPEN_FAILED (36)
378    Open of the standard output file descriptor failed or resulted in an
379    unexpected file descriptor number.  This fatal error may occur during
380    system initialization.  It is an application configuration error.
381
382INTERNAL_ERROR_LIBIO_STDERR_FD_OPEN_FAILED (37)
383    Open of the standard error file descriptor failed or resulted in an
384    unexpected file descriptor number.  This fatal error may occur during
385    system initialization.  It is an application configuration error.
386
387INTERNAL_ERROR_ILLEGAL_USE_OF_FLOATING_POINT_UNIT (38)
388    The floating point unit was used illegally, for example in interrupt
389    context on some architectures.
390
391INTERNAL_ERROR_ARC4RANDOM_GETENTROPY_FAIL (39)
392    A :c:func:`getentropy` system call failed in one of the `ARC4RANDOM(3)
393    <https://man.openbsd.org/arc4random.3>`_ functions.  This fatal error can
394    only be fixed with a different implementation of :c:func:`getentropy`.
395
396Operations
397==========
398
399.. index:: _Terminate
400
401.. _Terminate:
402
403Announcing a Fatal Error
404------------------------
405
406The :c:func:`_Terminate()` internal error handler is invoked when the
407application or the executive itself determines that a fatal error has occurred
408or a final system state is reached (for example after :c:func:`rtems_fatal()`
409or :c:func:`exit()`).
410
411The first action of the internal error handler is to call the fatal extension of
412the user extensions.  For the initial extensions the following conditions are
413required
414
415- a valid stack pointer and enough stack space,
416
417- a valid code memory, and
418
419- valid read-only data.
420
421For the initial extensions the read-write data (including .bss segment) is not
422required on single processor configurations.  In SMP configurations, however,
423the read-write data must be initialized since this function must determine the
424state of the other processors and request them to shut-down if necessary.
425
426Non-initial extensions require in addition valid read-write data.  The board
427support package (BSP) may install an initial extension that performs a system
428reset.  In this case the non-initial extensions will be not called.
429
430The fatal extensions are called with three parameters:
431
432- the fatal source,
433
434- a legacy parameter which is always false, and
435
436- an error code with a fatal source dependent content.
437
438Once all fatal extensions executed, the error information will be stored to
439:c:data:`_Internal_errors_What_happened` and the system state is set to
440:c:macro:`SYSTEM_STATE_TERMINATED`.
441
442The final step is to call the CPU port specific :c:func:`_CPU_Fatal_halt()`.
443
444Directives
445==========
446
447This section details the fatal error manager's directives.  A subsection is
448dedicated to each of this manager's directives and describes the calling
449sequence, related constants, usage, and status codes.
450
451.. raw:: latex
452
453   \clearpage
454
455.. index:: announce fatal error
456.. index:: fatal error, announce
457.. index:: rtems_fatal
458
459.. _rtems_fatal:
460
461FATAL - Invoke the fatal error handler
462--------------------------------------
463
464CALLING SEQUENCE:
465    .. code-block:: c
466
467        void rtems_fatal(
468          rtems_fatal_source fatal_source,
469          rtems_fatal_code   error_code
470        ) RTEMS_NO_RETURN;
471
472DIRECTIVE STATUS CODES:
473    NONE - This function will not return to the caller.
474
475DESCRIPTION:
476    This directive terminates the system.
477
478NOTE:
479    Registered :c:func:`atexit()` or :c:func:`on_exit()` handlers are not
480    called.  Use :c:func:`exit()` in case these handlers should be invoked.
481
482.. raw:: latex
483
484   \clearpage
485
486.. index:: panic
487.. index:: rtems_panic
488
489.. _rtems_panic:
490
491PANIC - Print a message and invoke the fatal error handler
492----------------------------------------------------------
493
494CALLING SEQUENCE:
495    .. code-block:: c
496
497        void rtems_panic(
498          const char *fmt,
499          ...
500        ) RTEMS_NO_RETURN RTEMS_PRINTFLIKE( 1, 2 );
501
502DIRECTIVE STATUS CODES:
503    NONE - This function will not return to the caller.
504
505DESCRIPTION:
506    This directive prints a message via :c:func:`printk` specified by the
507    format and optional parameters and then terminates the system with the
508    :c:macro:`RTEMS_FATAL_SOURCE_PANIC` fatal source.  The fatal code is set to
509    the format string address.
510
511NOTE:
512    Registered :c:func:`atexit()` or :c:func:`on_exit()` handlers are not
513    called.  Use :c:func:`exit()` in case these handlers should be invoked.
514
515.. raw:: latex
516
517   \clearpage
518
519.. index:: shutdown RTEMS
520.. index:: rtems_shutdown_executive
521
522.. _rtems_shutdown_executive:
523
524SHUTDOWN_EXECUTIVE - Shutdown RTEMS
525-----------------------------------
526
527CALLING SEQUENCE:
528    .. code-block:: c
529
530        void rtems_shutdown_executive(
531            uint32_t result
532        );
533
534DIRECTIVE STATUS CODES:
535    NONE - This function will not return to the caller.
536
537DESCRIPTION:
538    This directive is called when the application wishes to shutdown RTEMS.
539    The system is terminated with a fatal source of ``RTEMS_FATAL_SOURCE_EXIT``
540    and the specified ``result`` code.
541
542NOTES:
543    This directive *must* be the last RTEMS directive invoked by an application
544    and it *does not return* to the caller.
545
546    This directive may be called any time.
547
548.. raw:: latex
549
550   \clearpage
551
552.. index:: exception frame
553.. index:: rtems_exception_frame_print
554
555.. _rtems_exception_frame_print:
556
557EXCEPTION_FRAME_PRINT - Prints the exception frame
558--------------------------------------------------
559
560CALLING SEQUENCE:
561    .. code-block:: c
562
563        void rtems_exception_frame_print(
564            const rtems_exception_frame *frame
565        );
566
567DIRECTIVE STATUS CODES:
568    NONE
569
570DESCRIPTION:
571    Prints the exception frame via ``printk()``.
572
573.. raw:: latex
574
575   \clearpage
576
577.. index:: fatal error
578.. index:: rtems_fatal_source_text
579
580.. _rtems_fatal_source_text:
581
582FATAL_SOURCE_TEXT - Returns a text for a fatal source
583-----------------------------------------------------
584
585CALLING SEQUENCE:
586    .. code-block:: c
587
588        const char *rtems_fatal_source_text(
589            rtems_fatal_source source
590        );
591
592DIRECTIVE STATUS CODES:
593    The fatal source text or "?" in case the passed fatal source is invalid.
594
595DESCRIPTION:
596    Returns a text for a fatal source.  The text for fatal source is the
597    enumerator constant.
598
599.. raw:: latex
600
601   \clearpage
602
603.. index:: fatal error
604.. index:: rtems_internal_error_text
605
606.. _rtems_internal_error_text:
607
608INTERNAL_ERROR_TEXT - Returns a text for an internal error code
609---------------------------------------------------------------
610
611CALLING SEQUENCE:
612    .. code-block:: c
613
614        const char *rtems_internal_error_text(
615            rtems_fatal_code error
616        );
617
618DIRECTIVE STATUS CODES:
619    The error code text or "?" in case the passed error code is invalid.
620
621DESCRIPTION:
622    Returns a text for an internal error code.  The text for each internal
623    error code is the enumerator constant.
624
625.. raw:: latex
626
627   \clearpage
628
629.. index:: announce fatal error
630.. index:: fatal error, announce
631.. index:: rtems_fatal_error_occurred
632
633.. _rtems_fatal_error_occurred:
634
635FATAL_ERROR_OCCURRED - Invoke the fatal error handler (deprecated)
636------------------------------------------------------------------
637
638CALLING SEQUENCE:
639    .. code-block:: c
640
641        void rtems_fatal_error_occurred(
642            uint32_t  the_error
643        ) RTEMS_NO_RETURN;
644
645DIRECTIVE STATUS CODES:
646    NONE - This function will not return to the caller.
647
648DESCRIPTION:
649    This directive processes fatal errors.  If the FATAL error extension is
650    defined in the configuration table, then the user-defined error extension
651    is called.  If configured and the provided FATAL error extension returns,
652    then the RTEMS default error handler is invoked.  This directive can be
653    invoked by RTEMS or by the user's application code including initialization
654    tasks, other tasks, and ISRs.
655
656NOTES:
657    This directive is deprecated and should not be used in new code.
658
659    This directive supports local operations only.
660
661    Unless the user-defined error extension takes special actions such as
662    restarting the calling task, this directive WILL NOT RETURN to the caller.
663
664    The user-defined extension for this directive may wish to initiate a global
665    shutdown.
Note: See TracBrowser for help on using the repository browser.