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#include <inttypes.h>
48
49#include "sys/log.h"
50#define LOG_MODULE "Stack"
51#define LOG_LEVEL LOG_LEVEL_MAIN
52
53/*---------------------------------------------------------------------------*/
54/* linker will provide a symbol for the end of the .bss segment */
55extern uint8_t _stack;
56
57#if STACK_CHECK_PERIODIC_CHECKS
58PROCESS(stack_check_process, "Stack check");
59#endif
60/*---------------------------------------------------------------------------*/
61/* The symbol with which the stack memory is initially filled */
62#define STACK_FILL 0xcd
63/*---------------------------------------------------------------------------*/
64#ifdef STACK_ORIGIN
65/* use the #defined value */
66#define GET_STACK_ORIGIN() STACK_ORIGIN
67#else
68/* use the value provided by the linker script */
69extern int _stack_origin;
70#define GET_STACK_ORIGIN() (&_stack_origin)
71#endif
72/*---------------------------------------------------------------------------*/
73void
75{
76 /* Make this volatile to prevent the compiler from optimising the while loop */
77 volatile uint8_t *p;
78
79 /* Make this static to avoid destroying it in the while loop */
80 static void *stack_top;
81 /* Use address of this local variable as a boundary */
82 stack_top = &p;
83
84 /* Note: this is expected to be called before the WDT is started! */
85 p = &_stack;
86 while(p < (uint8_t *)stack_top) {
87 *p++ = STACK_FILL;
88 }
89
90#if STACK_CHECK_PERIODIC_CHECKS
91 /* Start the periodic checker process */
92 process_start(&stack_check_process, NULL);
93#endif
94}
95/*---------------------------------------------------------------------------*/
96int32_t
98{
99 uint8_t *p = &_stack;
100
101 /* Make sure WDT is not triggered */
103
104 /* Skip the bytes used after heap; it's 1 byte by default for _stack,
105 * more than that means dynamic memory allocation is used somewhere.
106 */
107 while(*p != STACK_FILL && p < (uint8_t *)GET_STACK_ORIGIN()) {
108 p++;
109 }
110
111 /* Skip memory-region reserved for the stack not used yet by the program */
112 while(*p == STACK_FILL && p < (uint8_t *)GET_STACK_ORIGIN()) {
113 p++;
114 }
115
116 /* Make sure WDT is not triggered */
118
119 if(p >= (uint8_t*)GET_STACK_ORIGIN()) {
120 /* This means the stack is screwed. */
121 return -1;
122 }
123
124 return (uint8_t *)GET_STACK_ORIGIN() - p;
125}
126/*---------------------------------------------------------------------------*/
127int32_t
129{
130 return (uint8_t *)GET_STACK_ORIGIN() - &_stack;
131}
132/*---------------------------------------------------------------------------*/
133#if STACK_CHECK_PERIODIC_CHECKS
134/*---------------------------------------------------------------------------*/
135PROCESS_THREAD(stack_check_process, ev, data)
136{
137 static struct etimer et;
138
140
141 etimer_set(&et, STACK_CHECK_PERIOD);
142
143 while(1) {
144 int32_t actual, allowed;
145
147
148 actual = stack_check_get_usage();
150 if(actual < 0 || allowed < 0) {
151 LOG_ERR("Check in inconsistent state: %" PRId32 " vs. %" PRId32 "\n",
152 actual, allowed);
153 } else if(actual > allowed) {
154 LOG_ERR("Check failed: %" PRId32 " vs. %" PRId32 "\n", actual, allowed);
155 } else {
156 LOG_DBG("Check ok: %" PRId32 " vs. %" PRId32 "\n", actual, allowed);
157 }
158
159 etimer_reset(&et);
160 }
161
162 PROCESS_END();
163}
164/*---------------------------------------------------------------------------*/
165#endif /* STACK_CHECK_PERIODIC_CHECKS */
166/*---------------------------------------------------------------------------*/
167/** @} */
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1154
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:85
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition: etimer.c:192
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
int32_t stack_check_get_reserved_size(void)
Calculate the maximal permitted stack usage.
Definition: stack-check.c:128
int32_t stack_check_get_usage(void)
Calculate the maximal stack usage so far.
Definition: stack-check.c:97
void stack_check_init(void)
Initialize the stack area with a known pattern.
Definition: stack-check.c:74
Header file for the logging system.
Stack checker library header file.
A timer.
Definition: etimer.h:76