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

5
Last change on this file since b1e3b75 was b1e3b75, checked in by Sebastian Huber <sebastian.huber@…>, on 01/26/17 at 14:10:49

c-user: Rework user extensions chapter

Update #2692.
Update #2752.

  • Property mode set to 100644
File size: 19.9 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.. _fatal_error_manager:
8
9Fatal Error Manager
10*******************
11
12.. index:: fatal errors
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_shutdown_executive_ - Shutdown RTEMS
25
26- rtems_exception_frame_print_ - Print the CPU exception frame
27
28- rtems_fatal_source_text_ - Return the fatal source text
29
30- rtems_internal_error_text_ - Return the error code text
31
32- rtems_fatal_error_occurred_ - Invoke the fatal error handler (deprecated)
33
34Background
35==========
36
37Overview
38--------
39
40.. index:: fatal error detection
41.. index:: fatal error processing
42.. index:: fatal error user extension
43
44The fatal error manager is called upon detection of an irrecoverable error
45condition by either RTEMS or the application software.  Fatal errors are also
46used in case it is difficult or impossible to return an error condition by
47other means, e.g. a return value of a directive call.  Fatal errors can be
48detected from various sources, for example
49
50- the executive (RTEMS),
51- support libraries,
52- user system code, and
53- user application code.
54
55RTEMS automatically invokes the fatal error manager upon detection of an error
56it considers to be fatal.  Similarly, the user should invoke the fatal error
57manager upon detection of a fatal error.
58
59Each static or dynamic user extension set may include a fatal error handler.
60The fatal error handler in the static extension set can be used to provide
61access to debuggers and monitors which may be present on the target hardware.
62If any user-supplied fatal error handlers are installed, the fatal error
63manager will invoke them.  Usually, the board support package provides a fatal
64error extesion which resets the board.  If no user handlers are configured or
65if all the user handler return control to the fatal error manager, then the
66RTEMS default fatal error handler is invoked.  If the default fatal error
67handler is invoked, then the system state is marked as failed.
68
69Although the precise behavior of the default fatal error handler is processor
70specific, in general, it will disable all maskable interrupts, place the error
71code in a known processor dependent place (generally either on the stack or in
72a register), and halt the processor.  The precise actions of the RTEMS fatal
73error are discussed in the Default Fatal Error Processing chapter of the
74Applications Supplement document for a specific target processor.
75
76Fatal Sources
77-------------
78
79The following fatal sources are defined for RTEMS via the
80:c:type:`rtems_fatal_source` enumeration.  Each symbolic name has the
81corresponding numeric fatal source in parenthesis.
82
83INTERNAL_ERROR_CORE (0)
84    Errors of the core operating system.  See :ref:`internal_errors`.
85
86INTERNAL_ERROR_RTEMS_API (1)
87    Errors of the Classic API.
88
89INTERNAL_ERROR_POSIX_API (2)
90    Errors of the POSIX API.
91
92RTEMS_FATAL_SOURCE_BDBUF (3)
93    Fatal source for the block device cache.  See
94    :c:type:`rtems_bdbuf_fatal_code`.
95
96RTEMS_FATAL_SOURCE_APPLICATION (4)
97    Fatal source for application-specific errors.  The fatal code is
98    application-specific.
99
100RTEMS_FATAL_SOURCE_EXIT (5)
101    Fatal source of :c:func:`exit()`.  The fatal code is the :c:func:`exit()`
102    status code.
103
104RTEMS_FATAL_SOURCE_BSP (6)
105    Fatal source for BSP errors.  The fatal codes are defined in
106    :file:`<bsp/fatal.h>`.  Examples are interrupt and exception
107    initialization.  See :c:type:`bsp_fatal_code` and :c:func:`bsp_fatal()`.
108
109RTEMS_FATAL_SOURCE_ASSERT (7)
110    Fatal source of :c:macro:`assert()`.  The fatal code is the pointer value
111    of the assert context.  See :c:type:`rtems_assert_context`.
112
113RTEMS_FATAL_SOURCE_STACK_CHECKER (8)
114    Fatal source of the stack checker.  The fatal code is the object name of
115    the executing task.
116
117RTEMS_FATAL_SOURCE_EXCEPTION (9)
118    Fatal source of the exceptions.  The fatal code is the pointer value of the
119    exception frame pointer.  See :c:type:`rtems_exception_frame` and
120    :ref:`rtems_exception_frame_print`.
121
122RTEMS_FATAL_SOURCE_SMP (10)
123    Fatal source of SMP domain.  See :c:type:`SMP_Fatal_code`.
124
125.. _internal_errors:
126
127Internal Error Codes
128--------------------
129
130The following error codes are defined for the :c:data:`INTERNAL_ERROR_CORE`
131fatal source.  Each symbolic name has the corresponding numeric error code in
132parenthesis.
133
134INTERNAL_ERROR_TOO_LITTLE_WORKSPACE (2)
135    There is not enough memory for the workspace.  This fatal error may occur
136    during system initialization.  It is an application configuration error.
137
138INTERNAL_ERROR_WORKSPACE_ALLOCATION (3)
139    An allocation from the workspace failed.  This fatal error may occur during
140    system initialization.  It is an application configuration error.
141
142INTERNAL_ERROR_INTERRUPT_STACK_TOO_SMALL (4)
143    The configured interrupt stack size is too small.  This fatal error may
144    occur during system initialization.  It is an application configuration
145    error.
146
147INTERNAL_ERROR_THREAD_EXITTED (5)
148    A non-POSIX thread entry function returned.  This is an API usage error.
149
150    An example code to provoke this fatal error is:
151
152    .. code-block:: c
153
154        void task( rtems_arg arg )
155        {
156          /* Classic API tasks must not return */
157        }
158
159        void create_bad_task( void )
160        {
161          rtems_status_code sc;
162          rtems_id          task_id;
163
164          sc = rtems_task_create(
165            rtems_build_name('T', 'A', 'S', 'K'),
166            1,
167            RTEMS_DEFAULT_MODES,
168            RTEMS_DEFAULT_ATTRIBUTES,
169            &task_id
170          );
171          assert( sc == RTEMS_SUCCESSFUL );
172
173          sc = rtems_task_start( task_id, task, 0 );
174          assert( sc == RTEMS_SUCCESSFUL );
175        }
176
177INTERNAL_ERROR_INCONSISTENT_MP_INFORMATION (6)
178    This fatal error can only occur on MPCI configurations.  The MPCI nodes or
179    global objects configuration is inconsistent.  This fatal error may occur
180    during system initialization.  It is an application configuration error.
181
182INTERNAL_ERROR_INVALID_NODE (7)
183    This fatal error can only occur on MPCI configurations.  The own MPCI node
184    number is invalid.  This fatal error may occur during system
185    initialization.  It is an application configuration error.
186
187INTERNAL_ERROR_NO_MPCI (8)
188    This fatal error can only occur on MPCI configurations.  There is no MPCI
189    configuration table.  This fatal error may occur during system
190    initialization.  It is an application configuration error.
191
192INTERNAL_ERROR_BAD_PACKET (9)
193    This fatal error can only occur on MPCI configurations.  The MPCI server
194    thread received a bad packet.
195
196INTERNAL_ERROR_OUT_OF_PACKETS (10)
197    This fatal error can only occur on MPCI configurations.  The MPCI packet
198    pool is empty.  It is an application configuration error.
199
200INTERNAL_ERROR_OUT_OF_GLOBAL_OBJECTS (11)
201    This fatal error can only occur on MPCI configurations.  The MPCI global
202    objects pool is empty.  It is an application configuration error.
203
204INTERNAL_ERROR_OUT_OF_PROXIES (12)
205    This fatal error can only occur on MPCI configurations.  The MPCI thread
206    proxy pool is empty.  It is an application configuration error.
207
208INTERNAL_ERROR_INVALID_GLOBAL_ID (13)
209    This fatal error can only occur on MPCI configurations.  The system cannot
210    find the global object for a specific object identifier.  In case this
211    happens, then this is probably an operating system bug.
212
213INTERNAL_ERROR_BAD_STACK_HOOK (14)
214    The stack allocator hook or stack free hook is NULL.  This fatal error may
215    occur during system initialization.  It is an application configuration
216    error.
217
218INTERNAL_ERROR_UNLIMITED_AND_MAXIMUM_IS_0 (19)
219    An object class is configured to use the unlimited objects option, however,
220    the count of objects for each extension is zero.  This fatal error may
221    occur during system initialization.  It is an application configuration
222    error.
223
224INTERNAL_ERROR_NO_MEMORY_FOR_HEAP (23)
225    There is not enough memory for the C program heap.  This fatal error may
226    occur during system initialization.  It is an application configuration
227    error.
228
229INTERNAL_ERROR_CPU_ISR_INSTALL_VECTOR (24)
230    The use of :c:func:`_CPU_ISR_install_vector()` is illegal on this system.
231
232INTERNAL_ERROR_RESOURCE_IN_USE (25)
233    This fatal error can only occur on debug configurations.  It happens in
234    case a thread which owns mutexes is deleted.  Mutexes owned by a deleted
235    thread are in an inconsistent state.
236
237INTERNAL_ERROR_RTEMS_INIT_TASK_ENTRY_IS_NULL (26)
238    An RTEMS initialization task entry function is NULL.  This fatal error may
239    occur during system initialization.  It is an application configuration
240    error.
241
242INTERNAL_ERROR_POSIX_INIT_THREAD_ENTRY_IS_NULL (27)
243    A POSIX initialization thread 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 on 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    On 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
387Operations
388==========
389
390.. _Terminate:
391
392Announcing a Fatal Error
393------------------------
394.. index:: _Terminate
395
396The :c:func:`_Terminate()` internal error handler is invoked when the
397application or the executive itself determines that a fatal error has occurred
398or a final system state is reached (for example after :c:func:`rtems_fatal()`
399or :c:func:`exit()`).
400
401The first action of the internal error handler is to call the fatal extension of
402the user extensions.  For the initial extensions the following conditions are
403required
404
405- a valid stack pointer and enough stack space,
406
407- a valid code memory, and
408
409- valid read-only data.
410
411For the initial extensions the read-write data (including .bss segment) is not
412required on single processor configurations.  On SMP configurations, however,
413the read-write data must be initialized since this function must determine the
414state of the other processors and request them to shut-down if necessary.
415
416Non-initial extensions require in addition valid read-write data.  The board
417support package (BSP) may install an initial extension that performs a system
418reset.  In this case the non-initial extensions will be not called.
419
420The fatal extensions are called with three parameters:
421
422- the fatal source,
423
424- a legacy parameter which is always false, and
425
426- an error code with a fatal source dependent content.
427
428Once all fatal extensions executed, the error information will be stored to
429:c:data:`_Internal_errors_What_happened` and the system state is set to
430:c:macro:`SYSTEM_STATE_TERMINATED`.
431
432The final step is to call the CPU port specific :c:func:`_CPU_Fatal_halt()`.
433
434Directives
435==========
436
437This section details the fatal error manager's directives.  A subsection is
438dedicated to each of this manager's directives and describes the calling
439sequence, related constants, usage, and status codes.
440
441.. raw:: latex
442
443   \clearpage
444
445.. _rtems_fatal:
446
447FATAL - Invoke the fatal error
448------------------------------
449.. index:: announce fatal error
450.. index:: fatal error, announce
451.. index:: rtems_fatal
452
453CALLING SEQUENCE:
454    .. code-block:: c
455
456        void rtems_fatal(
457           rtems_fatal_source fatal_source,
458           rtems_fatal_code   error_code
459        ) RTEMS_NO_RETURN;
460
461DIRECTIVE STATUS CODES:
462    NONE - This function will not return to the caller.
463
464DESCRIPTION:
465    This directive terminates the system.
466
467NOTE:
468    Registered :c:func:`atexit()` or :c:func:`on_exit()` handlers are not
469    called.  Use :c:func:`exit()` in case these handlers should be invoked.
470
471.. raw:: latex
472
473   \clearpage
474
475.. _rtems_shutdown_executive:
476
477SHUTDOWN_EXECUTIVE - Shutdown RTEMS
478-----------------------------------
479.. index:: shutdown RTEMS
480
481.. index:: rtems_shutdown_executive
482CALLING SEQUENCE:
483    .. code-block:: c
484
485        void rtems_shutdown_executive(
486            uint32_t result
487        );
488
489DIRECTIVE STATUS CODES:
490    NONE - This function will not return to the caller.
491
492DESCRIPTION:
493    This directive is called when the application wishes to shutdown RTEMS.
494    The system is terminated with a fatal source of ``RTEMS_FATAL_SOURCE_EXIT``
495    and the specified ``result`` code.
496
497NOTES:
498    This directive *must* be the last RTEMS directive invoked by an application
499    and it *does not return* to the caller.
500
501    This directive may be called any time.
502
503.. raw:: latex
504
505   \clearpage
506
507.. _rtems_exception_frame_print:
508
509EXCEPTION_FRAME_PRINT - Prints the exception frame
510--------------------------------------------------
511.. index:: exception frame
512.. index:: rtems_exception_frame_print
513
514CALLING SEQUENCE:
515    .. code-block:: c
516
517        void rtems_exception_frame_print(
518            const rtems_exception_frame *frame
519        );
520
521DIRECTIVE STATUS CODES:
522    NONE
523
524DESCRIPTION:
525    Prints the exception frame via ``printk()``.
526
527.. raw:: latex
528
529   \clearpage
530
531.. _rtems_fatal_source_text:
532
533FATAL_SOURCE_TEXT - Returns a text for a fatal source
534-----------------------------------------------------
535.. index:: fatal error
536.. index:: rtems_fatal_source_text
537
538CALLING SEQUENCE:
539    .. code-block:: c
540
541        const char *rtems_fatal_source_text(
542            rtems_fatal_source source
543        );
544
545DIRECTIVE STATUS CODES:
546    The fatal source text or "?" in case the passed fatal source is invalid.
547
548DESCRIPTION:
549    Returns a text for a fatal source.  The text for fatal source is the
550    enumerator constant.
551
552.. raw:: latex
553
554   \clearpage
555
556.. _rtems_internal_error_text:
557
558INTERNAL_ERROR_TEXT - Returns a text for an internal error code
559---------------------------------------------------------------
560.. index:: fatal error
561.. index:: rtems_internal_error_text
562
563CALLING SEQUENCE:
564    .. code-block:: c
565
566        const char *rtems_internal_error_text(
567            rtems_fatal_code error
568        );
569
570DIRECTIVE STATUS CODES:
571    The error code text or "?" in case the passed error code is invalid.
572
573DESCRIPTION:
574    Returns a text for an internal error code.  The text for each internal
575    error code is the enumerator constant.
576
577.. raw:: latex
578
579   \clearpage
580
581.. _rtems_fatal_error_occurred:
582
583FATAL_ERROR_OCCURRED - Invoke the fatal error handler (deprecated)
584------------------------------------------------------------------
585.. index:: announce fatal error
586.. index:: fatal error, announce
587.. index:: rtems_fatal_error_occurred
588
589CALLING SEQUENCE:
590    .. code-block:: c
591
592        void rtems_fatal_error_occurred(
593            uint32_t  the_error
594        ) RTEMS_NO_RETURN;
595
596DIRECTIVE STATUS CODES:
597    NONE - This function will not return to the caller.
598
599DESCRIPTION:
600    This directive processes fatal errors.  If the FATAL error extension is
601    defined in the configuration table, then the user-defined error extension
602    is called.  If configured and the provided FATAL error extension returns,
603    then the RTEMS default error handler is invoked.  This directive can be
604    invoked by RTEMS or by the user's application code including initialization
605    tasks, other tasks, and ISRs.
606
607NOTES:
608    This directive is deprecated and should not be used in new code.
609
610    This directive supports local operations only.
611
612    Unless the user-defined error extension takes special actions such as
613    restarting the calling task, this directive WILL NOT RETURN to the caller.
614
615    The user-defined extension for this directive may wish to initiate a global
616    shutdown.
Note: See TracBrowser for help on using the repository browser.