SingingCat 0
application
function-instr.c
1#include "main-header.h"
2#include "constants.h"
3#include "loader-api.h"
4#include "function-instr.h"
5
6static byte pc_listing_enabled = 0;
7
8void __cyg_profile_func_enter(void *func, void *callsite) __attribute__((no_instrument_function));
9void __cyg_profile_func_exit(void *func, void *callsite) __attribute__((no_instrument_function));
10volatile byte overflow_check_active;
11extern void *_Stack_Limit;
12static struct traceinfo mytraceinfo;
13void stack_overflow(void *sp) __attribute__((no_instrument_function));
14
28void __cyg_profile_func_enter(void *func, void *callsite) {
29 byte spb;
30
31 if (!pc_listing_enabled) {
32 return;
33 }
34 //constants()->last_function = func;
35 constants()->last_return_address = __builtin_return_address(0);
36 if (!overflow_check_active) {
37 return;
38 }
39 // if address of spb < stack_limit, we got a problem...
40 if (((void *)(&spb)) < ((void *)(&_Stack_Limit))) {
41 stack_overflow(&spb);
42 }
43}
44void __cyg_profile_func_exit(void *func, void *callsite) {
45}
46
47void stack_overflow(void *sp) {
48 overflow_check_active = 0;
49 printf("A stack overflow has been detected (sp=%p, limit=%p).\r\n", sp, &_Stack_Limit);
50 printf("Return Address: %p\r\n", constants()->last_return_address);
51 long l = mculib_get_seconds_since_boot();
52
53 while ((mculib_get_seconds_since_boot() - l) < 3) {
54 ;;
55 }
56 printf("Reseting application\r\n");
57 restart_app();
58}
59
60void enable_pc_tracing() {
61 pc_listing_enabled = 1;
62}
63
64struct traceinfo *get_reboot_info() {
65 return &mytraceinfo;
66}