Contiki-NG
Loading...
Searching...
No Matches
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#if defined __GNUC__ && __GNUC__ >= 13
82#pragma GCC diagnostic push
83#pragma GCC diagnostic ignored "-Wdangling-pointer"
84#endif
85 /* Use address of this local variable as a boundary */
86 stack_top = &p;
87#if defined __GNUC__ && __GNUC__ >= 13
88#pragma GCC diagnostic pop
89#endif
90
91 /* Note: this is expected to be called before the WDT is started! */
92 p = &_stack;
93 while(p < (uint8_t *)stack_top) {
94 *p++ = STACK_FILL;
95 }
96
97#if STACK_CHECK_PERIODIC_CHECKS
98 /* Start the periodic checker process */
99 process_start(&stack_check_process, NULL);
100#endif
101}
102/*---------------------------------------------------------------------------*/
103size_t
105{
106 uint8_t *p = &_stack;
107
108 /* Make sure WDT is not triggered */
110
111 /* Skip the bytes used after heap; it's 1 byte by default for _stack,
112 * more than that means dynamic memory allocation is used somewhere.
113 */
114 while(*p != STACK_FILL && p < (uint8_t *)GET_STACK_ORIGIN()) {
115 p++;
116 }
117
118 /* Skip memory-region reserved for the stack not used yet by the program */
119 while(*p == STACK_FILL && p < (uint8_t *)GET_STACK_ORIGIN()) {
120 p++;
121 }
122
123 /* Make sure WDT is not triggered */
125
126 if(p >= (uint8_t*)GET_STACK_ORIGIN()) {
127 /* This means the stack is screwed. */
128 return SIZE_MAX;
129 }
130
131 return (uint8_t *)GET_STACK_ORIGIN() - p;
132}
133/*---------------------------------------------------------------------------*/
134size_t
136{
137 return (uint8_t *)GET_STACK_ORIGIN() - &_stack;
138}
139/*---------------------------------------------------------------------------*/
140#if STACK_CHECK_PERIODIC_CHECKS
141/*---------------------------------------------------------------------------*/
142PROCESS_THREAD(stack_check_process, ev, data)
143{
144 static struct etimer et;
145
147
148 etimer_set(&et, STACK_CHECK_PERIOD);
149
150 while(1) {
152
153 size_t actual = stack_check_get_usage();
154 size_t allowed = stack_check_get_reserved_size();
155 if(actual > allowed) {
156 LOG_ERR("Check failed: %u vs. %u\n", (unsigned)actual,
157 (unsigned)allowed);
158 } else {
159 LOG_DBG("Check ok: %u vs. %u\n", (unsigned)actual, (unsigned)allowed);
160 }
161
162 etimer_reset(&et);
163 }
164
165 PROCESS_END();
166}
167/*---------------------------------------------------------------------------*/
168#endif /* STACK_CHECK_PERIODIC_CHECKS */
169/*---------------------------------------------------------------------------*/
170/** @} */
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
static bool etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition etimer.h:201
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:107
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition process.h:273
size_t stack_check_get_usage(void)
Calculate the maximal stack usage so far.
size_t stack_check_get_reserved_size(void)
Calculate the maximal permitted stack usage.
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:79