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