source: rtems-docs/c-user/fatal_error.rst @ 97c51c8

5
Last change on this file since 97c51c8 was 97c51c8, checked in by Sebastian Huber <sebastian.huber@…>, on 11/22/17 at 13:09:28

c-user: Document rtems_panic()

Close #3244.

  • Property mode set to 100644
File size: 21.4 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_POSIX_INIT_THREAD_ENTRY_IS_NULL (27)
248    A POSIX initialization thread entry function is NULL.  This fatal error may
249    occur during system initialization.  It is an application configuration
250    error.
251
252INTERNAL_ERROR_THREAD_QUEUE_DEADLOCK (28)
253    A deadlock was detected during a thread queue enqueue operation.
254
255INTERNAL_ERROR_THREAD_QUEUE_ENQUEUE_STICKY_FROM_BAD_STATE (29)
256    This fatal error can only happen in SMP configurations.  It is not allowed
257    to obtain MrsP semaphores in a context with thread dispatching disabled,
258    for example interrupt context.
259
260    An example code to provoke this fatal error is:
261
262    .. code-block:: c
263
264        void bad( rtems_id timer_id, void *arg )
265        {
266          rtems_id *sem_id;
267
268          sem_id = arg;
269
270          rtems_semaphore_obtain( *sem_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
271          assert( 0 );
272        }
273
274        void fire_bad_timer( rtems_task_argument arg )
275        {
276          rtems_status_code sc;
277          rtems_id          sem_id;
278          rtems_id          timer_id;
279
280          sc = rtems_semaphore_create(
281            rtems_build_name('M', 'R', 'S', 'P'),
282            1,
283            RTEMS_MULTIPROCESSOR_RESOURCE_SHARING
284              | RTEMS_BINARY_SEMAPHORE,
285            1,
286            &sem_id
287          );
288          assert( sc == RTEMS_SUCCESSFUL );
289
290          sc = rtems_timer_create(
291            rtems_build_name( 'E', 'V', 'I', 'L' ),
292            &timer_id
293          );
294          assert( sc == RTEMS_SUCCESSFUL );
295
296          sc = rtems_semaphore_obtain( sem_id, RTEMS_WAIT, RTEMS_NO_TIMEOUT );
297          assert( sc == RTEMS_SUCCESSFUL );
298
299          sc = rtems_timer_fire_after( timer_id, 1, bad, &sem_id );
300          assert( sc == RTEMS_SUCCESSFUL );
301
302          rtems_task_wake_after( 2 );
303          assert( 0 );
304        }
305
306INTERNAL_ERROR_BAD_THREAD_DISPATCH_DISABLE_LEVEL (30)
307    It is illegal to call blocking operating system services with thread
308    dispatching disabled, for example in interrupt context.
309
310    An example code to provoke this fatal error is:
311
312    .. code-block:: c
313
314        void bad( rtems_id id, void *arg )
315        {
316          rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
317          assert( 0 );
318        }
319
320        void fire_bad_timer( void )
321        {
322          rtems_status_code sc;
323          rtems_id          id;
324
325          sc = rtems_timer_create(
326            rtems_build_name( 'E', 'V', 'I', 'L' ),
327            &id
328          );
329          assert( sc == RTEMS_SUCCESSFUL );
330
331          sc = rtems_timer_fire_after( id, 1, bad, NULL );
332          assert( sc == RTEMS_SUCCESSFUL );
333
334          rtems_task_wake_after( 2 );
335          assert( 0 );
336        }
337
338INTERNAL_ERROR_BAD_THREAD_DISPATCH_ENVIRONMENT (31)
339    In SMP configurations, it is a fatal error to call blocking operating
340    system with interrupts disabled, since this prevents delivery of
341    inter-processor interrupts.  This could lead to executing threads which are
342    not allowed to execute resulting in undefined system behaviour.
343
344    Some CPU ports, for example the ARM Cortex-M port, have a similar problem,
345    since the interrupt state is not a part of the thread context.
346
347    This fatal error is detected in the operating system core function
348    :c:func:`_Thread_Do_dispatch()` responsible to carry out a thread dispatch.
349
350    An example code to provoke this fatal error is:
351
352    .. code-block:: c
353
354        void bad( void )
355        {
356          rtems_interrupt_level level;
357
358          rtems_interrupt_local_disable( level );
359          rtems_task_suspend( RTEMS_SELF );
360          rtems_interrupt_local_enable( level  );
361        }
362
363INTERNAL_ERROR_RTEMS_INIT_TASK_CREATE_FAILED (32)
364    Creation of an RTEMS initialization task failed.  This fatal error may
365    occur during system initialization.  It is an application configuration
366    error.
367
368INTERNAL_ERROR_POSIX_INIT_THREAD_CREATE_FAILED (33)
369    Creation of a POSIX initialization thread failed.  This fatal error may
370    occur during system initialization.  It is an application configuration
371    error.
372
373INTERNAL_ERROR_LIBIO_USER_ENV_KEY_CREATE_FAILED (34)
374    Creation of the IO library user environment POSIX key failed.  This fatal
375    error may occur during system initialization.  It is an application
376    configuration error.
377
378INTERNAL_ERROR_LIBIO_SEM_CREATE_FAILED (35)
379    Creation of the IO library semaphore failed.  This fatal error may occur
380    during system initialization.  It is an application configuration error.
381
382INTERNAL_ERROR_LIBIO_STDOUT_FD_OPEN_FAILED (36)
383    Open of the standard output 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_LIBIO_STDERR_FD_OPEN_FAILED (37)
388    Open of the standard error file descriptor failed or resulted in an
389    unexpected file descriptor number.  This fatal error may occur during
390    system initialization.  It is an application configuration error.
391
392INTERNAL_ERROR_ILLEGAL_USE_OF_FLOATING_POINT_UNIT (38)
393    The floating point unit was used illegally, for example in interrupt
394    context on some architectures.
395
396INTERNAL_ERROR_ARC4RANDOM_GETENTROPY_FAIL (39)
397    A :c:func:`getentropy` system call failed in one of the `ARC4RANDOM(3)
398    <https://man.openbsd.org/arc4random.3>`_ functions.  This fatal error can
399    only be fixed with a different implementation of :c:func:`getentropy`.
400
401Operations
402==========
403
404.. index:: _Terminate
405
406.. _Terminate:
407
408Announcing a Fatal Error
409------------------------
410
411The :c:func:`_Terminate()` internal error handler is invoked when the
412application or the executive itself determines that a fatal error has occurred
413or a final system state is reached (for example after :c:func:`rtems_fatal()`
414or :c:func:`exit()`).
415
416The first action of the internal error handler is to call the fatal extension of
417the user extensions.  For the initial extensions the following conditions are
418required
419
420- a valid stack pointer and enough stack space,
421
422- a valid code memory, and
423
424- valid read-only data.
425
426For the initial extensions the read-write data (including .bss segment) is not
427required on single processor configurations.  In SMP configurations, however,
428the read-write data must be initialized since this function must determine the
429state of the other processors and request them to shut-down if necessary.
430
431Non-initial extensions require in addition valid read-write data.  The board
432support package (BSP) may install an initial extension that performs a system
433reset.  In this case the non-initial extensions will be not called.
434
435The fatal extensions are called with three parameters:
436
437- the fatal source,
438
439- a legacy parameter which is always false, and
440
441- an error code with a fatal source dependent content.
442
443Once all fatal extensions executed, the error information will be stored to
444:c:data:`_Internal_errors_What_happened` and the system state is set to
445:c:macro:`SYSTEM_STATE_TERMINATED`.
446
447The final step is to call the CPU port specific :c:func:`_CPU_Fatal_halt()`.
448
449Directives
450==========
451
452This section details the fatal error manager's directives.  A subsection is
453dedicated to each of this manager's directives and describes the calling
454sequence, related constants, usage, and status codes.
455
456.. raw:: latex
457
458   \clearpage
459
460.. index:: announce fatal error
461.. index:: fatal error, announce
462.. index:: rtems_fatal
463
464.. _rtems_fatal:
465
466FATAL - Invoke the fatal error handler
467--------------------------------------
468
469CALLING SEQUENCE:
470    .. code-block:: c
471
472        void rtems_fatal(
473          rtems_fatal_source fatal_source,
474          rtems_fatal_code   error_code
475        ) RTEMS_NO_RETURN;
476
477DIRECTIVE STATUS CODES:
478    NONE - This function will not return to the caller.
479
480DESCRIPTION:
481    This directive terminates the system.
482
483NOTE:
484    Registered :c:func:`atexit()` or :c:func:`on_exit()` handlers are not
485    called.  Use :c:func:`exit()` in case these handlers should be invoked.
486
487.. raw:: latex
488
489   \clearpage
490
491.. index:: panic
492.. index:: rtems_panic
493
494.. _rtems_panic:
495
496PANIC - Print a message and invoke the fatal error handler
497----------------------------------------------------------
498
499CALLING SEQUENCE:
500    .. code-block:: c
501
502        void rtems_panic(
503          const char *fmt,
504          ...
505        ) RTEMS_NO_RETURN RTEMS_PRINTFLIKE( 1, 2 );
506
507DIRECTIVE STATUS CODES:
508    NONE - This function will not return to the caller.
509
510DESCRIPTION:
511    This directive prints a message via :c:func:`printk` specified by the
512    format and optional parameters and then terminates the system with the
513    :c:macro:`RTEMS_FATAL_SOURCE_PANIC` fatal source.  The fatal code is set to
514    the format string address.
515
516NOTE:
517    Registered :c:func:`atexit()` or :c:func:`on_exit()` handlers are not
518    called.  Use :c:func:`exit()` in case these handlers should be invoked.
519
520.. raw:: latex
521
522   \clearpage
523
524.. index:: shutdown RTEMS
525.. index:: rtems_shutdown_executive
526
527.. _rtems_shutdown_executive:
528
529SHUTDOWN_EXECUTIVE - Shutdown RTEMS
530-----------------------------------
531
532CALLING SEQUENCE:
533    .. code-block:: c
534
535        void rtems_shutdown_executive(
536            uint32_t result
537        );
538
539DIRECTIVE STATUS CODES:
540    NONE - This function will not return to the caller.
541
542DESCRIPTION:
543    This directive is called when the application wishes to shutdown RTEMS.
544    The system is terminated with a fatal source of ``RTEMS_FATAL_SOURCE_EXIT``
545    and the specified ``result`` code.
546
547NOTES:
548    This directive *must* be the last RTEMS directive invoked by an application
549    and it *does not return* to the caller.
550
551    This directive may be called any time.
552
553.. raw:: latex
554
555   \clearpage
556
557.. index:: exception frame
558.. index:: rtems_exception_frame_print
559
560.. _rtems_exception_frame_print:
561
562EXCEPTION_FRAME_PRINT - Prints the exception frame
563--------------------------------------------------
564
565CALLING SEQUENCE:
566    .. code-block:: c
567
568        void rtems_exception_frame_print(
569            const rtems_exception_frame *frame
570        );
571
572DIRECTIVE STATUS CODES:
573    NONE
574
575DESCRIPTION:
576    Prints the exception frame via ``printk()``.
577
578.. raw:: latex
579
580   \clearpage
581
582.. index:: fatal error
583.. index:: rtems_fatal_source_text
584
585.. _rtems_fatal_source_text:
586
587FATAL_SOURCE_TEXT - Returns a text for a fatal source
588-----------------------------------------------------
589
590CALLING SEQUENCE:
591    .. code-block:: c
592
593        const char *rtems_fatal_source_text(
594            rtems_fatal_source source
595        );
596
597DIRECTIVE STATUS CODES:
598    The fatal source text or "?" in case the passed fatal source is invalid.
599
600DESCRIPTION:
601    Returns a text for a fatal source.  The text for fatal source is the
602    enumerator constant.
603
604.. raw:: latex
605
606   \clearpage
607
608.. index:: fatal error
609.. index:: rtems_internal_error_text
610
611.. _rtems_internal_error_text:
612
613INTERNAL_ERROR_TEXT - Returns a text for an internal error code
614---------------------------------------------------------------
615
616CALLING SEQUENCE:
617    .. code-block:: c
618
619        const char *rtems_internal_error_text(
620            rtems_fatal_code error
621        );
622
623DIRECTIVE STATUS CODES:
624    The error code text or "?" in case the passed error code is invalid.
625
626DESCRIPTION:
627    Returns a text for an internal error code.  The text for each internal
628    error code is the enumerator constant.
629
630.. raw:: latex
631
632   \clearpage
633
634.. index:: announce fatal error
635.. index:: fatal error, announce
636.. index:: rtems_fatal_error_occurred
637
638.. _rtems_fatal_error_occurred:
639
640FATAL_ERROR_OCCURRED - Invoke the fatal error handler (deprecated)
641------------------------------------------------------------------
642
643CALLING SEQUENCE:
644    .. code-block:: c
645
646        void rtems_fatal_error_occurred(
647            uint32_t  the_error
648        ) RTEMS_NO_RETURN;
649
650DIRECTIVE STATUS CODES:
651    NONE - This function will not return to the caller.
652
653DESCRIPTION:
654    This directive processes fatal errors.  If the FATAL error extension is
655    defined in the configuration table, then the user-defined error extension
656    is called.  If configured and the provided FATAL error extension returns,
657    then the RTEMS default error handler is invoked.  This directive can be
658    invoked by RTEMS or by the user's application code including initialization
659    tasks, other tasks, and ISRs.
660
661NOTES:
662    This directive is deprecated and should not be used in new code.
663
664    This directive supports local operations only.
665
666    Unless the user-defined error extension takes special actions such as
667    restarting the calling task, this directive WILL NOT RETURN to the caller.
668
669    The user-defined extension for this directive may wish to initiate a global
670    shutdown.
Note: See TracBrowser for help on using the repository browser.