1 | Stack Bounds Checker |
---|
2 | #################### |
---|
3 | |
---|
4 | Introduction |
---|
5 | ============ |
---|
6 | |
---|
7 | The stack bounds checker is an RTEMS support component that determines |
---|
8 | if a task has overrun its run-time stack. The routines provided |
---|
9 | by 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 | |
---|
15 | Background |
---|
16 | ========== |
---|
17 | |
---|
18 | Task Stack |
---|
19 | ---------- |
---|
20 | |
---|
21 | Each task in a system has a fixed size stack associated with it. This |
---|
22 | stack is allocated when the task is created. As the task executes, the |
---|
23 | stack is used to contain parameters, return addresses, saved registers, |
---|
24 | and local variables. The amount of stack space required by a task |
---|
25 | is dependent on the exact set of routines used. The peak stack usage |
---|
26 | reflects the worst case of subroutine pushing information on the stack. |
---|
27 | For example, if a subroutine allocates a local buffer of 1024 bytes, then |
---|
28 | this data must be accounted for in the stack of every task that invokes that |
---|
29 | routine. |
---|
30 | |
---|
31 | Recursive routines make calculating peak stack usage difficult, if not |
---|
32 | impossible. Each call to the recursive routine consumes *n* bytes |
---|
33 | of stack space. If the routine recursives 1000 times, then ``1000 * *n*`` bytes of stack space are required. |
---|
34 | |
---|
35 | Execution |
---|
36 | --------- |
---|
37 | |
---|
38 | The stack bounds checker operates as a set of task extensions. At |
---|
39 | task creation time, the task's stack is filled with a pattern to |
---|
40 | indicate the stack is unused. As the task executes, it will overwrite |
---|
41 | this pattern in memory. At each task switch, the stack bounds checker's |
---|
42 | task 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 | |
---|
52 | If either of these conditions is detected, then a blown stack |
---|
53 | error is reported using the ``printk`` routine. |
---|
54 | |
---|
55 | The number of bytes checked for an overwrite is processor family dependent. |
---|
56 | The minimum stack frame per subroutine call varies widely between processor |
---|
57 | families. On CISC families like the Motorola MC68xxx and Intel ix86, all |
---|
58 | that is needed is a return address. On more complex RISC processors, |
---|
59 | the minimum stack frame per subroutine call may include space to save |
---|
60 | a significant number of registers. |
---|
61 | |
---|
62 | Another processor dependent feature that must be taken into account by |
---|
63 | the stack bounds checker is the direction that the stack grows. On some |
---|
64 | processor families, the stack grows up or to higher addresses as the |
---|
65 | task executes. On other families, it grows down to lower addresses. The |
---|
66 | stack bounds checker implementation uses the stack description definitions |
---|
67 | provided by every RTEMS port to get for this information. |
---|
68 | |
---|
69 | Operations |
---|
70 | ========== |
---|
71 | |
---|
72 | Initializing the Stack Bounds Checker |
---|
73 | ------------------------------------- |
---|
74 | |
---|
75 | The stack checker is initialized automatically when its task |
---|
76 | create extension runs for the first time. |
---|
77 | |
---|
78 | The application must include the stack bounds checker extension set |
---|
79 | in its set of Initial Extensions. This set of extensions is |
---|
80 | defined as ``STACK_CHECKER_EXTENSION``. If using ``<rtems/confdefs.h>`` |
---|
81 | for Configuration Table generation, then all that is necessary is |
---|
82 | to 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 | |
---|
89 | Checking for Blown Task Stack |
---|
90 | ----------------------------- |
---|
91 | |
---|
92 | The application may check whether the stack pointer of currently |
---|
93 | executing task is within proper bounds at any time by calling |
---|
94 | the ``rtems_stack_checker_is_blown`` method. This |
---|
95 | method return ``FALSE`` if the task is operating within its |
---|
96 | stack bounds and has not damaged its pattern area. |
---|
97 | |
---|
98 | Reporting Task Stack Usage |
---|
99 | -------------------------- |
---|
100 | |
---|
101 | The application may dynamically report the stack usage for every task |
---|
102 | in the system by calling the``rtems_stack_checker_report_usage`` routine. |
---|
103 | This routine prints a table with the peak usage and stack size of |
---|
104 | every task in the system. The following is an example of the |
---|
105 | report 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 | |
---|
115 | Notice the last time. The task id is 0xffffffff and its name is "INTR". |
---|
116 | This is not actually a task, it is the interrupt stack. |
---|
117 | |
---|
118 | When a Task Overflows the Stack |
---|
119 | ------------------------------- |
---|
120 | |
---|
121 | When the stack bounds checker determines that a stack overflow has occurred, |
---|
122 | it will attempt to print a message using ``printk`` identifying the |
---|
123 | task and then shut the system down. If the stack overflow has caused |
---|
124 | corruption, then it is possible that the message cannot be printed. |
---|
125 | |
---|
126 | The 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 | |
---|
133 | The above includes the task id and a pointer to the task control block as |
---|
134 | well as enough information so one can look at the task's stack and |
---|
135 | see what was happening. |
---|
136 | |
---|
137 | Routines |
---|
138 | ======== |
---|
139 | |
---|
140 | This section details the stack bounds checker's routines. |
---|
141 | A subsection is dedicated to each of routines |
---|
142 | and describes the calling sequence, related constants, usage, |
---|
143 | and status codes. |
---|
144 | |
---|
145 | .. COMMENT: rtems_stack_checker_is_blown |
---|
146 | |
---|
147 | STACK_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 | |
---|
163 | This method is used to determine if the current stack pointer |
---|
164 | of the currently executing task is within bounds. |
---|
165 | |
---|
166 | **NOTES:** |
---|
167 | |
---|
168 | This method checks the current stack pointer against |
---|
169 | the high and low addresses of the stack memory allocated when |
---|
170 | the task was created and it looks for damage to the high water |
---|
171 | mark pattern for the worst case usage of the task being called. |
---|
172 | |
---|
173 | STACK_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 | |
---|
186 | This routine prints a table with the peak stack usage and stack space |
---|
187 | allocation of every task in the system. |
---|
188 | |
---|
189 | **NOTES:** |
---|
190 | |
---|
191 | NONE |
---|
192 | |
---|
193 | .. COMMENT: COPYRIGHT (c) 1988-2007. |
---|
194 | |
---|
195 | .. COMMENT: On-Line Applications Research Corporation (OAR). |
---|
196 | |
---|
197 | .. COMMENT: All rights reserved. |
---|
198 | |
---|