source: rtems-docs/c_user/stack_bounds_checker.rst @ d389819

4.115
Last change on this file since d389819 was d389819, checked in by Amar Takhar <amar@…>, on 01/18/16 at 05:37:40

Convert all Unicode to ASCII(128)

  • Property mode set to 100644
File size: 6.9 KB
Line 
1Stack Bounds Checker
2####################
3
4Introduction
5============
6
7The stack bounds checker is an RTEMS support component that determines
8if a task has overrun its run-time stack.  The routines provided
9by the stack bounds checker manager are:
10
11- ``rtems_stack_checker_is_blown`` - Has the Current Task Blown its Stack
12
13- ``rtems_stack_checker_report_usage`` - Report Task Stack Usage
14
15Background
16==========
17
18Task Stack
19----------
20
21Each task in a system has a fixed size stack associated with it.  This
22stack is allocated when the task is created.  As the task executes, the
23stack is used to contain parameters, return addresses, saved registers,
24and local variables.  The amount of stack space required by a task
25is dependent on the exact set of routines used.  The peak stack usage
26reflects the worst case of subroutine pushing information on the stack.
27For example, if a subroutine allocates a local buffer of 1024 bytes, then
28this data must be accounted for in the stack of every task that invokes that
29routine.
30
31Recursive routines make calculating peak stack usage difficult, if not
32impossible.  Each call to the recursive routine consumes *n* bytes
33of stack space.  If the routine recursives 1000 times, then ``1000 * *n*`` bytes of stack space are required.
34
35Execution
36---------
37
38The stack bounds checker operates as a set of task extensions.  At
39task creation time, the task's stack is filled with a pattern to
40indicate the stack is unused.  As the task executes, it will overwrite
41this pattern in memory.  At each task switch, the stack bounds checker's
42task switch extension is executed.  This extension checks that:
43
44- the last ``n`` bytes of the task's stack have
45  not been overwritten.  If this pattern has been damaged, it
46  indicates that at some point since this task was context
47  switch to the CPU, it has used too much stack space.
48
49- the current stack pointer of the task is not within
50  the address range allocated for use as the task's stack.
51
52If either of these conditions is detected, then a blown stack
53error is reported using the ``printk`` routine.
54
55The number of bytes checked for an overwrite is processor family dependent.
56The minimum stack frame per subroutine call varies widely between processor
57families.  On CISC families like the Motorola MC68xxx and Intel ix86, all
58that is needed is a return address.  On more complex RISC processors,
59the minimum stack frame per subroutine call may include space to save
60a significant number of registers.
61
62Another processor dependent feature that must be taken into account by
63the stack bounds checker is the direction that the stack grows.  On some
64processor families, the stack grows up or to higher addresses as the
65task executes.  On other families, it grows down to lower addresses.  The
66stack bounds checker implementation uses the stack description definitions
67provided by every RTEMS port to get for this information.
68
69Operations
70==========
71
72Initializing the Stack Bounds Checker
73-------------------------------------
74
75The stack checker is initialized automatically when its task
76create extension runs for the first time.
77
78The application must include the stack bounds checker extension set
79in its set of Initial Extensions.  This set of extensions is
80defined as ``STACK_CHECKER_EXTENSION``.  If using ``<rtems/confdefs.h>``
81for Configuration Table generation, then all that is necessary is
82to define the macro ``CONFIGURE_STACK_CHECKER_ENABLED`` before including``<rtems/confdefs.h>`` as shown below:
83.. code:: c
84
85    #define CONFIGURE_STACK_CHECKER_ENABLED
86    ...
87    #include <rtems/confdefs.h>
88
89Checking for Blown Task Stack
90-----------------------------
91
92The application may check whether the stack pointer of currently
93executing task is within proper bounds at any time by calling
94the ``rtems_stack_checker_is_blown`` method.  This
95method return ``FALSE`` if the task is operating within its
96stack bounds and has not damaged its pattern area.
97
98Reporting Task Stack Usage
99--------------------------
100
101The application may dynamically report the stack usage for every task
102in the system by calling the``rtems_stack_checker_report_usage`` routine.
103This routine prints a table with the peak usage and stack size of
104every task in the system.  The following is an example of the
105report generated:
106.. code:: c
107
108    ID      NAME       LOW        HIGH     AVAILABLE      USED
109    0x04010001  IDLE  0x003e8a60  0x003e9667       2952        200
110    0x08010002  TA1   0x003e5750  0x003e7b57       9096       1168
111    0x08010003  TA2   0x003e31c8  0x003e55cf       9096       1168
112    0x08010004  TA3   0x003e0c40  0x003e3047       9096       1104
113    0xffffffff  INTR  0x003ecfc0  0x003effbf      12160        128
114
115Notice the last time.  The task id is 0xffffffff and its name is "INTR".
116This is not actually a task, it is the interrupt stack.
117
118When a Task Overflows the Stack
119-------------------------------
120
121When the stack bounds checker determines that a stack overflow has occurred,
122it will attempt to print a message using ``printk`` identifying the
123task and then shut the system down.  If the stack overflow has caused
124corruption, then it is possible that the message cannot be printed.
125
126The following is an example of the output generated:
127.. code:: c
128
129    BLOWN STACK!!! Offending task(0x3eb360): id=0x08010002; name=0x54413120
130    stack covers range 0x003e5750 - 0x003e7b57 (9224 bytes)
131    Damaged pattern begins at 0x003e5758 and is 128 bytes long
132
133The above includes the task id and a pointer to the task control block as
134well as enough information so one can look at the task's stack and
135see what was happening.
136
137Routines
138========
139
140This section details the stack bounds checker's routines.
141A subsection is dedicated to each of routines
142and describes the calling sequence, related constants, usage,
143and status codes.
144
145.. COMMENT: rtems_stack_checker_is_blown
146
147STACK_CHECKER_IS_BLOWN - Has Current Task Blown Its Stack
148---------------------------------------------------------
149
150**CALLING SEQUENCE:**
151
152.. code:: c
153
154    bool rtems_stack_checker_is_blown( void );
155
156**STATUS CODES:**
157
158``TRUE`` - Stack is operating within its stack limits
159``FALSE`` - Current stack pointer is outside allocated area
160
161**DESCRIPTION:**
162
163This method is used to determine if the current stack pointer
164of the currently executing task is within bounds.
165
166**NOTES:**
167
168This method checks the current stack pointer against
169the high and low addresses of the stack memory allocated when
170the task was created and it looks for damage to the high water
171mark pattern for the worst case usage of the task being called.
172
173STACK_CHECKER_REPORT_USAGE - Report Task Stack Usage
174----------------------------------------------------
175
176**CALLING SEQUENCE:**
177
178.. code:: c
179
180    void rtems_stack_checker_report_usage( void );
181
182**STATUS CODES: NONE**
183
184**DESCRIPTION:**
185
186This routine prints a table with the peak stack usage and stack space
187allocation of every task in the system.
188
189**NOTES:**
190
191NONE
192
193.. COMMENT: COPYRIGHT (c) 1988-2007.
194
195.. COMMENT: On-Line Applications Research Corporation (OAR).
196
197.. COMMENT: All rights reserved.
198
Note: See TracBrowser for help on using the repository browser.