Contiki-NG
stack-check.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, University of Bristol - http://www.bris.ac.uk/
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 /**
32  * \addtogroup stack
33  * @{
34  */
35 
36 /**
37  * \file
38  * Implementation of the stack checker library.
39  * \author
40  * Atis Elsts <atis.elsts@bristol.ac.uk>
41  */
42 
43 #include "contiki.h"
44 #include "sys/stack-check.h"
45 #include "dev/watchdog.h"
46 #include <string.h>
47 
48 #include "sys/log.h"
49 #define LOG_MODULE "Stack"
50 #define LOG_LEVEL LOG_LEVEL_MAIN
51 
52 /*---------------------------------------------------------------------------*/
53 /* linker will provide a symbol for the end of the .bss segment */
54 extern uint8_t _stack;
55 
56 #if STACK_CHECK_PERIODIC_CHECKS
57 PROCESS(stack_check_process, "Stack check");
58 #endif
59 /*---------------------------------------------------------------------------*/
60 /* The symbol with which the stack memory is initially filled */
61 #define STACK_FILL 0xcd
62 /*---------------------------------------------------------------------------*/
63 #ifdef STACK_ORIGIN
64 /* use the #defined value */
65 #define GET_STACK_ORIGIN() STACK_ORIGIN
66 #else
67 /* use the value provided by the linker script */
68 extern int _stack_origin;
69 #define GET_STACK_ORIGIN() (&_stack_origin)
70 #endif
71 /*---------------------------------------------------------------------------*/
72 void
74 {
75  uint8_t *p;
76 
77  /* Make this static to avoid destroying it in the while loop */
78  static void *stack_top;
79  /* Use address of this local variable as a boundary */
80  stack_top = &p;
81 
82  /* Note: this is expected to be called before the WDT is started! */
83  p = &_stack;
84  while(p < (uint8_t *)stack_top) {
85  *p++ = STACK_FILL;
86  }
87 
88 #if STACK_CHECK_PERIODIC_CHECKS
89  /* Start the periodic checker process */
90  process_start(&stack_check_process, NULL);
91 #endif
92 }
93 /*---------------------------------------------------------------------------*/
94 uint16_t
96 {
97  uint8_t *p = &_stack;
98 
99  /* Make sure WDT is not triggered */
101 
102  /* Skip the bytes used after heap; it's 1 byte by default for _stack,
103  * more than that means dynamic memory allocation is used somewhere.
104  */
105  while(*p != STACK_FILL && p < (uint8_t *)GET_STACK_ORIGIN()) {
106  p++;
107  }
108 
109  /* Skip the region of the memory reserved for the stack not used yet by the program */
110  while(*p == STACK_FILL && p < (uint8_t *)GET_STACK_ORIGIN()) {
111  p++;
112  }
113 
114  /* Make sure WDT is not triggered */
116 
117  if(p >= (uint8_t*)GET_STACK_ORIGIN()) {
118  /* This means the stack is screwed. */
119  return 0xffff;
120  }
121 
122  return (uint8_t *)GET_STACK_ORIGIN() - p;
123 }
124 /*---------------------------------------------------------------------------*/
125 uint16_t
127 {
128  return (uint8_t *)GET_STACK_ORIGIN() - &_stack;
129 }
130 /*---------------------------------------------------------------------------*/
131 #if STACK_CHECK_PERIODIC_CHECKS
132 /*---------------------------------------------------------------------------*/
133 PROCESS_THREAD(stack_check_process, ev, data)
134 {
135  static struct etimer et;
136 
137  PROCESS_BEGIN();
138 
139  etimer_set(&et, STACK_CHECK_PERIOD);
140 
141  while(1) {
142  uint16_t actual, allowed;
143 
145 
146  actual = stack_check_get_usage();
147  allowed = stack_check_get_reserved_size();
148  if(actual > allowed) {
149  LOG_ERR("Check failed: %u vs. %u\n", actual, allowed);
150  } else {
151  LOG_DBG("Check ok: %u vs. %u\n", actual, allowed);
152  }
153 
154  etimer_reset(&et);
155  }
156 
157  PROCESS_END();
158 }
159 /*---------------------------------------------------------------------------*/
160 #endif /* STACK_CHECK_PERIODIC_CHECKS */
161 /*---------------------------------------------------------------------------*/
162 /** @} */
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
uint16_t stack_check_get_usage(void)
Calculate the maximal stack usage so far.
Definition: stack-check.c:95
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
uint16_t stack_check_get_reserved_size(void)
Calculate the maximal permitted stack usage.
Definition: stack-check.c:126
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
void stack_check_init(void)
Initialize the stack area with a known pattern.
Definition: stack-check.c:73
A timer.
Definition: etimer.h:75
Stack checker library header file.
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:85
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1035
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition: etimer.c:192
Header file for the logging system
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99