source: rtems/c/src/lib/libmisc/stackchk/check.c @ 3a4ae6c

4.104.114.84.95
Last change on this file since 3a4ae6c was 3a4ae6c, checked in by Joel Sherrill <joel.sherrill@…>, on Sep 11, 1995 at 7:35:39 PM

The word "RTEMS" almost completely removed from the core.

Configuration Table Template file added and all tests
modified to use this. All gvar.h and conftbl.h files
removed from test directories.

Configuration parameter maximum_devices added.

Core semaphore and mutex handlers added and RTEMS API Semaphore
Manager updated to reflect this.

Initialization sequence changed to invoke API specific initialization
routines. Initialization tasks table now owned by RTEMS Tasks Manager.

Added user extension for post-switch.

Utilized user extensions to implement API specific functionality
like signal dispatching.

Added extensions to the System Initialization Thread so that an
API can register a function to be invoked while the system
is being initialized. These are largely equivalent to the
pre-driver and post-driver hooks.

Added the Modules file oar-go32_p5, modified oar-go32, and modified
the file make/custom/go32.cfg to look at an environment varable which
determines what CPU model is being used.

All BSPs updated to reflect named devices and clock driver's IOCTL
used by the Shared Memory Driver. Also merged clock isr into
main file and removed ckisr.c where possible.

Updated spsize to reflect new and moved variables.

Makefiles for the executive source and include files updated to show
break down of files into Core, RTEMS API, and Neither.

Header and inline files installed into subdirectory based on whether
logically in the Core or a part of the RTEMS API.

  • Property mode set to 100644
File size: 11.2 KB
Line 
1/*
2 *  Stack Overflow Check User Extension Set
3 *
4 *  NOTE:  This extension set automatically determines at
5 *         initialization time whether the stack for this
6 *         CPU grows up or down and installs the correct
7 *         extension routines for that direction.
8 *
9 *  COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994.
10 *  On-Line Applications Research Corporation (OAR).
11 *  All rights assigned to U.S. Government, 1994.
12 *
13 *  This material may be reproduced by or for the U.S. Government pursuant
14 *  to the copyright license under the clause at DFARS 252.227-7013.  This
15 *  notice must appear in all copies of this file and its derivatives.
16 *
17 *  $Id$
18 *
19 */
20
21#include <rtems.h>
22
23extern rtems_configuration_table BSP_Configuration;
24
25#include <assert.h>
26#include <stdio.h>
27#include <string.h>
28#include <stdlib.h>
29
30#include "stackchk.h"
31#include "internal.h"
32
33/*
34 *  This variable contains the name of the task which "blew" the stack.
35 *  It is NULL if the system is all right.
36 */
37
38Thread_Control *Stack_check_Blown_task;
39
40/*
41 *  The extension table for the stack checker.
42 */
43
44rtems_extensions_table Stack_check_Extension_table = {
45  Stack_check_Create_extension,     /* rtems_task_create  */
46  0,                                /* rtems_task_start   */
47  0,                                /* rtems_task_restart */
48  0,                                /* rtems_task_delete  */
49  Stack_check_Switch_extension,     /* task_switch  */
50  0,                                /* task_post_switch  */
51  Stack_check_Begin_extension,      /* task_begin   */
52  0,                                /* task_exitted */
53  Stack_check_Fatal_extension,      /* fatal        */
54};
55
56/*
57 *  The "magic pattern" used to mark the end of the stack.
58 */
59
60Stack_check_Control Stack_check_Pattern;
61
62/*
63 *  Where the pattern goes in the stack area is dependent upon
64 *  whether the stack grow to the high or low area of the memory.
65 *
66 */
67
68#if ( CPU_STACK_GROWS_UP == TRUE )
69
70#define Stack_check_Get_pattern_area( _the_stack ) \
71  ((Stack_check_Control *) \
72    ((_the_stack)->area + (_the_stack)->size - sizeof( Stack_check_Control ) ))
73
74#define Stack_check_Calculate_used( _low, _size, _high_water ) \
75    ((_high_water) - (_low))
76
77#define Stack_check_usable_stack_start(_the_stack) \
78    ((_the_stack)->area)
79
80#else
81
82#define Stack_check_Get_pattern_area( _the_stack ) \
83  ((Stack_check_Control *) ((_the_stack)->area + HEAP_OVERHEAD))
84
85#define Stack_check_Calculate_used( _low, _size, _high_water) \
86    ( ((_low) + (_size)) - (_high_water) )
87
88#define Stack_check_usable_stack_start(_the_stack) \
89    ((_the_stack)->area + sizeof(Stack_check_Control))
90
91#endif
92
93#define Stack_check_usable_stack_size(_the_stack) \
94    ((_the_stack)->size - sizeof(Stack_check_Control))
95
96
97/*
98 * Do we have an interrupt stack?
99 * XXX it would sure be nice if the interrupt stack were also
100 *     stored in a "stack" structure!
101 */
102
103
104Stack_Control stack_check_interrupt_stack;
105
106/*
107 * Fill an entire stack area with BYTE_PATTERN.
108 * This will be used by a Fatal extension to check for
109 * amount of actual stack used
110 */
111
112void
113stack_check_dope_stack(Stack_Control *stack)
114{
115    memset(stack->area, BYTE_PATTERN, stack->size);
116}
117
118
119/*PAGE
120 *
121 *  Stack_check_Initialize
122 */
123
124unsigned32 stack_check_initialized = 0;
125
126void Stack_check_Initialize( void )
127{
128  rtems_status_code    status;
129  Objects_Id           id_ignored;
130  unsigned32          *p;
131  unsigned32           i;
132  unsigned32           class_index;
133  Thread_Control      *the_thread;
134  Objects_Information *information;
135
136  if (stack_check_initialized)
137      return;
138
139  /*
140   * Dope the pattern and fill areas
141   */
142
143  for ( p = Stack_check_Pattern.pattern;
144        p < &Stack_check_Pattern.pattern[PATTERN_SIZE_WORDS];
145        p += 4
146      )
147  {
148      p[0] = 0xFEEDF00D;          /* FEED FOOD to BAD DOG */
149      p[1] = 0x0BAD0D06;
150      p[2] = 0xDEADF00D;          /* DEAD FOOD GOOD DOG */
151      p[3] = 0x600D0D06;
152  };
153
154  status = rtems_extension_create(
155    rtems_build_name( 'S', 'T', 'C', 'K' ),
156    &Stack_check_Extension_table,
157    &id_ignored
158  );
159  assert ( status == RTEMS_SUCCESSFUL );
160
161  Stack_check_Blown_task = 0;
162
163  /*
164   * If installed by a task, that task will not get setup properly
165   * since it missed out on the create hook.  This will cause a
166   * failure on first switch out of that task.
167   * So pretend here that we actually ran create and begin extensions.
168   */
169
170  /* XXX
171   *
172   *  Technically this has not been done for any task created before this
173   *  happened.  So just run through them and fix the situation.
174   */
175#if 0
176  if (_Thread_Executing)
177  {
178      Stack_check_Create_extension(_Thread_Executing, _Thread_Executing);
179  }
180#endif
181
182#if 0
183  for ( class_index = OBJECTS_CLASSES_FIRST ;
184        class_index <= OBJECTS_CLASSES_LAST ;
185        class_index++ ) {
186    information = _Objects_Information_table[ class_index ];
187    if ( information && information->is_thread ) {
188      for ( i=1 ; i <= information->maximum ; i++ ) {
189        the_thread = (Thread_Control *)information->local_table[ i ];
190        Stack_check_Create_extension( the_thread, the_thread );
191      }
192    }
193  }
194#endif
195
196  /*
197   * If appropriate, setup the interrupt stack for high water testing
198   * also.
199   */
200  if (_CPU_Interrupt_stack_low && _CPU_Interrupt_stack_high)
201  {
202      stack_check_interrupt_stack.area = _CPU_Interrupt_stack_low;
203      stack_check_interrupt_stack.size = _CPU_Interrupt_stack_high -
204                                               _CPU_Interrupt_stack_low;
205
206      stack_check_dope_stack(&stack_check_interrupt_stack);
207  }
208
209  stack_check_initialized = 1;
210}
211
212/*PAGE
213 *
214 *  Stack_check_Create_extension
215 */
216
217boolean Stack_check_Create_extension(
218  Thread_Control *running,
219  Thread_Control *the_thread
220)
221{
222    if (the_thread /* XXX && (the_thread != _Thread_Executing) */ )
223        stack_check_dope_stack(&the_thread->Start.Initial_stack);
224
225    return TRUE;
226}
227
228/*PAGE
229 *
230 *  Stack_check_Begin_extension
231 */
232
233void Stack_check_Begin_extension(
234  Thread_Control *the_thread
235)
236{
237  Stack_check_Control  *the_pattern;
238
239  if ( the_thread->Object.id == 0 )        /* skip system tasks */
240    return;
241
242  the_pattern = Stack_check_Get_pattern_area(&the_thread->Start.Initial_stack);
243
244  *the_pattern = Stack_check_Pattern;
245}
246
247/*PAGE
248 *
249 *  Stack_check_report_blown_task
250 *  Report a blown stack.  Needs to be a separate routine
251 *  so that interrupt handlers can use this too.
252 *
253 *  Caller must have set the Stack_check_Blown_task.
254 *
255 *  NOTE: The system is in a questionable state... we may not get
256 *        the following message out.
257 */
258
259void Stack_check_report_blown_task(void)
260{
261    Stack_Control *stack;
262    Thread_Control *running;
263
264    running = Stack_check_Blown_task;
265    stack = &running->Start.Initial_stack;
266
267    fprintf(
268        stderr,
269        "BLOWN STACK!!! Offending task(%p): id=0x%08x; name=0x%08x",
270        running,
271        running->Object.id,
272        *(unsigned32 *)running->Object.name
273    );
274    fflush(stderr);
275
276    if (BSP_Configuration.User_multiprocessing_table)
277        fprintf(
278          stderr,
279          "; node=%d\n",
280          BSP_Configuration.User_multiprocessing_table->node
281       );
282    else
283        fprintf(stderr, "\n");
284    fflush(stderr);
285
286    fprintf(
287        stderr,
288        "  stack covers range 0x%08x - 0x%08x (%d bytes)\n",
289        (unsigned32) stack->area,
290        (unsigned32) stack->area + stack->size - 1,
291        (unsigned32) stack->size);
292    fflush(stderr);
293
294    fprintf(
295        stderr,
296        "  Damaged pattern begins at 0x%08x and is %d bytes long\n",
297        (unsigned32) Stack_check_Get_pattern_area(stack), PATTERN_SIZE_BYTES);
298    fflush(stderr);
299
300    rtems_fatal_error_occurred( (unsigned32) "STACK BLOWN" );
301}
302
303/*PAGE
304 *
305 *  Stack_check_Switch_extension
306 */
307
308void Stack_check_Switch_extension(
309  Thread_Control *running,
310  Thread_Control *heir
311)
312{
313  if ( running->Object.id == 0 )        /* skip system tasks */
314    return;
315
316  if (0 != memcmp( (void *) Stack_check_Get_pattern_area( &running->Start.Initial_stack)->pattern,
317                  (void *) Stack_check_Pattern.pattern,
318                  PATTERN_SIZE_BYTES))
319  {
320      Stack_check_Blown_task = running;
321      Stack_check_report_blown_task();
322  }
323}
324
325void *Stack_check_find_high_water_mark(
326  const void *s,
327  size_t n
328)
329{
330  const unsigned32 *base, *ebase;
331  unsigned32 length;
332
333  base = s;
334  length = n/4;
335
336#if ( CPU_STACK_GROWS_UP == TRUE )
337  /*
338   * start at higher memory and find first word that does not
339   * match pattern
340   */
341
342  base += length - 1;
343  for (ebase = s; base > ebase; base--)
344      if (*base != U32_PATTERN)
345          return (void *) base;
346#else
347  /*
348   * start at lower memory and find first word that does not
349   * match pattern
350   */
351
352  base += PATTERN_SIZE_WORDS;
353  for (ebase = base + length; base < ebase; base++)
354      if (*base != U32_PATTERN)
355          return (void *) base;
356#endif
357
358  return (void *)0;
359}
360
361/*PAGE
362 *
363 *  Stack_check_Dump_threads_usage
364 *  Try to print out how much stack was actually used by the task.
365 *
366 */
367
368void Stack_check_Dump_threads_usage(
369  Thread_Control *the_thread
370)
371{
372  unsigned32      size, used;
373  void           *low;
374  void           *high_water_mark;
375  Stack_Control  *stack;
376
377  if ( !the_thread )
378    return;
379
380  /*
381   * XXX HACK to get to interrupt stack
382   */
383
384  if (the_thread == (Thread_Control *) -1)
385  {
386      if (stack_check_interrupt_stack.area)
387      {
388          stack = &stack_check_interrupt_stack;
389          the_thread = 0;
390      }
391      else
392          return;
393  }
394  else
395      stack = &the_thread->Start.Initial_stack;
396
397  low  = Stack_check_usable_stack_start(stack);
398  size = Stack_check_usable_stack_size(stack);
399
400  high_water_mark = Stack_check_find_high_water_mark(low, size);
401
402  if ( high_water_mark )
403    used = Stack_check_Calculate_used( low, size, high_water_mark );
404  else
405    used = 0;
406
407  printf( "0x%08x  0x%08x  0x%08x  0x%08x   %8d   %8d\n",
408          the_thread ? the_thread->Object.id : ~0,
409          the_thread ? *(unsigned32 *)the_thread->Object.name :
410                       rtems_build_name('I', 'N', 'T', 'R'),
411          (unsigned32) stack->area,
412          (unsigned32) stack->area + (unsigned32) stack->size - 1,
413          size,
414          used
415  );
416}
417
418/*PAGE
419 *
420 *  Stack_check_Fatal_extension
421 */
422
423void Stack_check_Fatal_extension( unsigned32 status )
424{
425    if (status == 0)
426        Stack_check_Dump_usage();
427}
428
429
430/*PAGE
431 *
432 *  Stack_check_Dump_usage
433 */
434
435void Stack_check_Dump_usage( void )
436{
437  unsigned32           i;
438  unsigned32           class_index;
439  Thread_Control      *the_thread;
440  unsigned32           hit_running = 0;
441  Objects_Information *information;
442
443  if (stack_check_initialized == 0)
444      return;
445
446  printf(
447    "   ID          NAME         LOW        HIGH      AVAILABLE     USED\n"
448  );
449
450  for ( class_index = OBJECTS_CLASSES_FIRST ; 
451        class_index <= OBJECTS_CLASSES_LAST ; 
452        class_index++ ) {
453    information = _Objects_Information_table[ class_index ];
454    if ( information && information->is_thread ) {
455      for ( i=1 ; i <= information->maximum ; i++ ) {
456        the_thread = (Thread_Control *)information->local_table[ i ];
457        Stack_check_Dump_threads_usage( the_thread );
458        if ( the_thread == _Thread_Executing )
459          hit_running = 1;
460      }
461    }
462  }
463
464  if ( !hit_running )
465    Stack_check_Dump_threads_usage( _Thread_Executing );
466
467  /* dump interrupt stack info if any */
468  Stack_check_Dump_threads_usage((Thread_Control *) -1);
469}
470
Note: See TracBrowser for help on using the repository browser.