source: rtems/cpukit/libmisc/stackchk/check.c @ 65e8f25

4.104.115
Last change on this file since 65e8f25 was 65e8f25, checked in by Joel Sherrill <joel.sherrill@…>, on 02/11/09 at 19:33:44

2009-02-11 Joel Sherrill <joel.sherrill@…>

PR 1374/misc

  • libmisc/stackchk/check.c: Fix printk formatting string.
  • Property mode set to 100644
File size: 11.3 KB
RevLine 
[ac7d5ef0]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 *
[d8ec87b4]9 *  COPYRIGHT (c) 1989-2007.
[ac7d5ef0]10 *  On-Line Applications Research Corporation (OAR).
11 *
[98e4ebf5]12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
[3160ff6]14 *  http://www.rtems.com/license/LICENSE.
[ac7d5ef0]15 *
16 *  $Id$
17 *
18 */
19
[550c3df7]20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
[3a4ae6c]24#include <rtems.h>
[3523321]25#include <inttypes.h>
[3a4ae6c]26
[8389628]27/*
[4da36c1a]28 * The stack dump information may be printed by a "fatal" extension.
[8389628]29 * Fatal extensions only get called via rtems_fatal_error_occurred()
30 * and not when rtems_shutdown_executive() is called.
31 * When that happens, this #define should be deleted and all the code
32 * it marks.
33 */
34#define DONT_USE_FATAL_EXTENSION
35
[ac7d5ef0]36#include <assert.h>
37#include <string.h>
38#include <stdlib.h>
39
[d8ec87b4]40#include <rtems/bspIo.h>
[bfc3533]41#include <rtems/stackchk.h>
[ac7d5ef0]42#include "internal.h"
43
44/*
[4da36c1a]45 *  Variable to indicate when the stack checker has been initialized.
[ac7d5ef0]46 */
[4da36c1a]47static int   Stack_check_Initialized = 0;
[ac7d5ef0]48
49/*
50 *  The "magic pattern" used to mark the end of the stack.
51 */
52Stack_check_Control Stack_check_Pattern;
53
[d8ec87b4]54/*
55 * Helper function to report if the actual stack pointer is in range.
56 *
57 * NOTE: This uses a GCC specific method.
58 */
[937ec60]59static inline bool Stack_check_Frame_pointer_in_range(
[d8ec87b4]60  Stack_Control *the_stack
61)
62{
63  void *sp = __builtin_frame_address(0);
64
65  #if defined(__GNUC__)
66    if ( sp < the_stack->area ) {
[937ec60]67      return false;
[d8ec87b4]68    }
69    if ( sp > (the_stack->area + the_stack->size) ) {
[937ec60]70      return false;
[d8ec87b4]71    }
[4da36c1a]72  #else
73    #error "How do I check stack bounds on a non-GNU compiler?"
[d8ec87b4]74  #endif
[937ec60]75  return true;
[d8ec87b4]76}
77
[ac7d5ef0]78/*
79 *  Where the pattern goes in the stack area is dependent upon
80 *  whether the stack grow to the high or low area of the memory.
81 */
[4da36c1a]82#if (CPU_STACK_GROWS_UP == TRUE)
83  #define Stack_check_Get_pattern_area( _the_stack ) \
84    ((Stack_check_Control *) ((char *)(_the_stack)->area + \
85         (_the_stack)->size - sizeof( Stack_check_Control ) ))
[ac7d5ef0]86
[4da36c1a]87  #define Stack_check_Calculate_used( _low, _size, _high_water ) \
88      ((char *)(_high_water) - (char *)(_low))
[ac7d5ef0]89
[4da36c1a]90  #define Stack_check_usable_stack_start(_the_stack) \
[ac7d5ef0]91    ((_the_stack)->area)
92
93#else
[4da36c1a]94  #define Stack_check_Get_pattern_area( _the_stack ) \
95    ((Stack_check_Control *) ((char *)(_the_stack)->area + HEAP_OVERHEAD))
[ac7d5ef0]96
[4da36c1a]97  #define Stack_check_Calculate_used( _low, _size, _high_water) \
98      ( ((char *)(_low) + (_size)) - (char *)(_high_water) )
[d8ec87b4]99
[4da36c1a]100  #define Stack_check_usable_stack_start(_the_stack) \
101      ((char *)(_the_stack)->area + sizeof(Stack_check_Control))
[ac7d5ef0]102
103#endif
104
[4da36c1a]105/*
106 *  The assumption is that if the pattern gets overwritten, the task
107 *  is too close.  This defines the usable stack memory.
108 */
[ac7d5ef0]109#define Stack_check_usable_stack_size(_the_stack) \
110    ((_the_stack)->size - sizeof(Stack_check_Control))
111
112/*
113 * Do we have an interrupt stack?
114 * XXX it would sure be nice if the interrupt stack were also
115 *     stored in a "stack" structure!
116 */
[4da36c1a]117Stack_Control Stack_check_Interrupt_stack;
[ac7d5ef0]118
119/*
[4da36c1a]120 *  Fill an entire stack area with BYTE_PATTERN.  This will be used
121 *  to check for amount of actual stack used.
[ac7d5ef0]122 */
[4da36c1a]123#define Stack_check_Dope_stack(_stack) \
124  memset((_stack)->area, BYTE_PATTERN, (_stack)->size)
[ac7d5ef0]125
[4da36c1a]126/*
127 *  Stack_check_Initialize
[ac7d5ef0]128 */
[4da36c1a]129void Stack_check_Initialize( void )
[ac7d5ef0]130{
[4da36c1a]131  uint32_t *p;
[ac7d5ef0]132
[4da36c1a]133  if (Stack_check_Initialized)
[d8ec87b4]134    return;
[ac7d5ef0]135
136  /*
137   * Dope the pattern and fill areas
138   */
139
140  for ( p = Stack_check_Pattern.pattern;
141        p < &Stack_check_Pattern.pattern[PATTERN_SIZE_WORDS];
142        p += 4
[4da36c1a]143      ) {
[ac7d5ef0]144      p[0] = 0xFEEDF00D;          /* FEED FOOD to BAD DOG */
145      p[1] = 0x0BAD0D06;
146      p[2] = 0xDEADF00D;          /* DEAD FOOD GOOD DOG */
147      p[3] = 0x600D0D06;
[4da36c1a]148  }
149 
[ac7d5ef0]150  /*
151   * If appropriate, setup the interrupt stack for high water testing
152   * also.
153   */
[4da36c1a]154  #if (CPU_ALLOCATE_INTERRUPT_STACK == TRUE)
155    if (_CPU_Interrupt_stack_low && _CPU_Interrupt_stack_high) {
156      Stack_check_Interrupt_stack.area = _CPU_Interrupt_stack_low;
157      Stack_check_Interrupt_stack.size = (char *) _CPU_Interrupt_stack_high -
158                                  (char *) _CPU_Interrupt_stack_low;
159      Stack_check_Dope_stack(&Stack_check_Interrupt_stack);
[ac7d5ef0]160  }
[4da36c1a]161  #endif
[8389628]162
[4da36c1a]163  Stack_check_Initialized = 1;
[ac7d5ef0]164}
165
[4da36c1a]166/*
[7ac4ae9]167 *  rtems_stack_checker_create_extension
[ac7d5ef0]168 */
[937ec60]169bool rtems_stack_checker_create_extension(
[031deada]170  Thread_Control *running __attribute__((unused)),
[ac7d5ef0]171  Thread_Control *the_thread
172)
173{
[4da36c1a]174  Stack_check_Initialize();
[71f4beb]175
[4da36c1a]176  if (the_thread)
177    Stack_check_Dope_stack(&the_thread->Start.Initial_stack);
[3a4ae6c]178
[937ec60]179  return true;
[ac7d5ef0]180}
181
[4da36c1a]182/*
[7ac4ae9]183 *  rtems_stack_checker_Begin_extension
[ac7d5ef0]184 */
[7ac4ae9]185void rtems_stack_checker_begin_extension(
[ac7d5ef0]186  Thread_Control *the_thread
187)
188{
189  Stack_check_Control  *the_pattern;
190
191  if ( the_thread->Object.id == 0 )        /* skip system tasks */
192    return;
193
194  the_pattern = Stack_check_Get_pattern_area(&the_thread->Start.Initial_stack);
195
196  *the_pattern = Stack_check_Pattern;
197}
198
[4da36c1a]199/*
[ac7d5ef0]200 *  Stack_check_report_blown_task
[4da36c1a]201 *
[ac7d5ef0]202 *  Report a blown stack.  Needs to be a separate routine
203 *  so that interrupt handlers can use this too.
204 *
205 *  NOTE: The system is in a questionable state... we may not get
206 *        the following message out.
207 */
[d8ec87b4]208void Stack_check_report_blown_task(
209  Thread_Control *running,
[937ec60]210  bool         pattern_ok
[d8ec87b4]211)
[ac7d5ef0]212{
[4da36c1a]213  Stack_Control *stack = &running->Start.Initial_stack;
[ac7d5ef0]214
[4da36c1a]215  printk(
[c3330a8]216    "BLOWN STACK!!! Offending task(0x%p): "
217        "id=0x%08" PRIx32 "; name=0x%08" PRIx32,
[4da36c1a]218    running,
219    running->Object.id,
[ce19f1fa]220    running->Object.name.name_u32
[4da36c1a]221  );
[ac7d5ef0]222
[4da36c1a]223  #if defined(RTEMS_MULTIPROCESSING)
224    if (rtems_configuration_get_user_multiprocessing_table()) {
[d8ec87b4]225      printk(
[0faa8b11]226        "; node=%d",
[4da36c1a]227        rtems_configuration_get_user_multiprocessing_table()->node
228      );
[d8ec87b4]229    }
[4da36c1a]230  #endif
231
232  printk(
[0faa8b11]233    "\n  stack covers range 0x%p - 0x%p (%d bytes)\n",
[4da36c1a]234    stack->area,
235    stack->area + stack->size - 1,
236    stack->size
237  );
[ac7d5ef0]238
[4da36c1a]239  if ( !pattern_ok ) {
240    printk(
[65e8f25]241      "  Damaged pattern begins at 0x%08lx and is %d bytes long\n",
[4da36c1a]242      (unsigned long) Stack_check_Get_pattern_area(stack),
[937ec60]243      PATTERN_SIZE_BYTES);
[4da36c1a]244  }
245
246  rtems_fatal_error_occurred( 0x81 );
[ac7d5ef0]247}
248
[4da36c1a]249/*
[7ac4ae9]250 *  rtems_stack_checker_switch_extension
[ac7d5ef0]251 */
[7ac4ae9]252void rtems_stack_checker_switch_extension(
[031deada]253  Thread_Control *running __attribute__((unused)),
254  Thread_Control *heir __attribute__((unused))
[ac7d5ef0]255)
256{
[d8ec87b4]257  Stack_Control *the_stack = &running->Start.Initial_stack;
258  void          *pattern;
[937ec60]259  bool        sp_ok;
260  bool        pattern_ok = true;
[d8ec87b4]261
262  pattern = (void *) Stack_check_Get_pattern_area(the_stack)->pattern;
263
264  /*
[2b596c69]265   *  Check for an out of bounds stack pointer or an overwrite
[d8ec87b4]266   */
267  sp_ok = Stack_check_Frame_pointer_in_range( the_stack );
[2b596c69]268
[d8ec87b4]269  pattern_ok = (!memcmp( pattern,
270            (void *) Stack_check_Pattern.pattern, PATTERN_SIZE_BYTES));
271
272  if ( !sp_ok || !pattern_ok ) {
273    Stack_check_report_blown_task( running, pattern_ok );
[ac7d5ef0]274  }
275}
276
[4da36c1a]277/*
278 *  Check if blown
279 */
[937ec60]280bool rtems_stack_checker_is_blown( void )
[4da36c1a]281{
282  Stack_Control *the_stack = &_Thread_Executing->Start.Initial_stack;
[937ec60]283  bool           sp_ok;
284  bool           pattern_ok = true;
[4da36c1a]285
286  /*
[2b596c69]287   *  Check for an out of bounds stack pointer
[4da36c1a]288   */
289
290  sp_ok = Stack_check_Frame_pointer_in_range( the_stack );
[2b596c69]291
292  /*
293   * The stack checker must be initialized before the pattern is there
294   * to check.
295   */
296  if ( Stack_check_Initialized ) {
297    pattern_ok = (!memcmp(
298      (void *) Stack_check_Get_pattern_area(the_stack)->pattern,
299      (void *) Stack_check_Pattern.pattern,
300      PATTERN_SIZE_BYTES
301    ));
302  }
[4da36c1a]303
[0faa8b11]304  /*
[937ec60]305   * The Stack Pointer and the Pattern Area are OK so return false.
[0faa8b11]306   */
307  if ( sp_ok && pattern_ok )
[937ec60]308    return false;
[0faa8b11]309
310  /*
311   * Let's report as much as we can.
312   */
313  Stack_check_report_blown_task( _Thread_Executing, pattern_ok );
[937ec60]314  return true;
[4da36c1a]315}
316
317/*
318 * Stack_check_find_high_water_mark
319 */
[ac7d5ef0]320void *Stack_check_find_high_water_mark(
321  const void *s,
[4da36c1a]322  size_t      n
[ac7d5ef0]323)
324{
[3e08d4e]325  const uint32_t   *base, *ebase;
326  uint32_t   length;
[ac7d5ef0]327
328  base = s;
329  length = n/4;
330
[4da36c1a]331  #if ( CPU_STACK_GROWS_UP == TRUE )
332    /*
333     * start at higher memory and find first word that does not
334     * match pattern
335     */
[ac7d5ef0]336
[4da36c1a]337    base += length - 1;
338    for (ebase = s; base > ebase; base--)
[c3330a8]339      if (*base != U32_PATTERN)
340        return (void *) base;
[4da36c1a]341  #else
342    /*
343     * start at lower memory and find first word that does not
344     * match pattern
345     */
[ac7d5ef0]346
[4da36c1a]347    base += PATTERN_SIZE_WORDS;
348    for (ebase = base + length; base < ebase; base++)
[c3330a8]349      if (*base != U32_PATTERN)
350        return (void *) base;
[4da36c1a]351  #endif
[ac7d5ef0]352
353  return (void *)0;
354}
355
[4da36c1a]356/*
[90a5d194]357 *  Stack_check_Dump_threads_usage
[ac7d5ef0]358 *
[4da36c1a]359 *  Try to print out how much stack was actually used by the task.
[ac7d5ef0]360 */
[90a5d194]361static void                   *print_context;
362static rtems_printk_plugin_t   print_handler;
363
[ac7d5ef0]364void Stack_check_Dump_threads_usage(
365  Thread_Control *the_thread
366)
367{
[3e08d4e]368  uint32_t        size, used;
[ac7d5ef0]369  void           *low;
370  void           *high_water_mark;
[dbfc895e]371  void           *current;
[ac7d5ef0]372  Stack_Control  *stack;
[c3330a8]373  char            name[5];
[ac7d5ef0]374
375  if ( !the_thread )
376    return;
377
[90a5d194]378  if ( !print_handler )
379    return;
380
[ac7d5ef0]381  /*
[dbfc895e]382   *  Obtain interrupt stack information
[ac7d5ef0]383   */
384
[4da36c1a]385  if (the_thread == (Thread_Control *) -1) {
386    if (Stack_check_Interrupt_stack.area) {
387      stack = &Stack_check_Interrupt_stack;
388      the_thread = 0;
[dbfc895e]389      current = 0;
[4da36c1a]390    }
391    else
392      return;
[dbfc895e]393  } else {
394    stack  = &the_thread->Start.Initial_stack;
395    current = (void *)_CPU_Context_Get_SP( &the_thread->Registers );
396  }
[ac7d5ef0]397
398  low  = Stack_check_usable_stack_start(stack);
399  size = Stack_check_usable_stack_size(stack);
400
401  high_water_mark = Stack_check_find_high_water_mark(low, size);
402
403  if ( high_water_mark )
404    used = Stack_check_Calculate_used( low, size, high_water_mark );
405  else
406    used = 0;
407
[e819fefc]408  if ( the_thread ) {
[dbfc895e]409    (*print_handler)(
410      print_context,
411      "0x%08" PRIx32 "  %4s",
412      the_thread->Object.id,
413      rtems_object_get_name( the_thread->Object.id, sizeof(name), name )
414    );
[e819fefc]415  } else {
[dbfc895e]416    (*print_handler)( print_context, "0x%08" PRIx32 "  INTR", ~0 );
[e819fefc]417  }
[c46ce854]418
[90a5d194]419  (*print_handler)(
420    print_context,
[dbfc895e]421    " %010p - %010p %010p  %8" PRId32 "   ",
[4da36c1a]422    stack->area,
423    stack->area + stack->size - 1,
[dbfc895e]424    current,
425    size
[ac7d5ef0]426  );
[dbfc895e]427
428  if (Stack_check_Initialized == 0) {
429    (*print_handler)( print_context, "Unavailable\n" );
430  } else {
431    (*print_handler)( print_context, "%8" PRId32 "\n", used );
432  }
433   
434
[ac7d5ef0]435}
436
[4da36c1a]437/*
[7ac4ae9]438 *  rtems_stack_checker_fatal_extension
[ac7d5ef0]439 */
[4da36c1a]440#ifndef DONT_USE_FATAL_EXTENSION
441  void rtems_stack_checker_fatal_extension(
[11290355]442    Internal_errors_Source  source,
[937ec60]443    bool                    is_internal,
[3e08d4e]444    uint32_t                status
[4da36c1a]445  )
446  {
[ac7d5ef0]447    if (status == 0)
[4da36c1a]448      rtems_stack_checker_report_usage();
449  }
[8389628]450#endif
[ac7d5ef0]451
452/*PAGE
453 *
[8583f82]454 *  rtems_stack_checker_report_usage
[ac7d5ef0]455 */
456
[90a5d194]457void rtems_stack_checker_report_usage_with_plugin(
458  void                  *context,
459  rtems_printk_plugin_t  print
460)
[ac7d5ef0]461{
[90a5d194]462  print_context = context;
463  print_handler = print;
464
465  (*print)( context, "Stack usage by thread\n");
466  (*print)( context,
[dbfc895e]467"    ID      NAME    LOW          HIGH     CURRENT     AVAILABLE     USED\n"
[ac7d5ef0]468  );
[5250ff39]469
[d8ec87b4]470  /* iterate over all threads and dump the usage */
471  rtems_iterate_over_all_threads( Stack_check_Dump_threads_usage );
[ac7d5ef0]472
473  /* dump interrupt stack info if any */
474  Stack_check_Dump_threads_usage((Thread_Control *) -1);
[90a5d194]475
476  print_context = NULL;
477  print_handler = NULL;
478
479}
480
481void rtems_stack_checker_report_usage( void )
482{
483  rtems_stack_checker_report_usage_with_plugin( NULL, printk_plugin );
[ac7d5ef0]484}
Note: See TracBrowser for help on using the repository browser.