tracing

flok

Well-known member
Hi,

I was missing a stack-trace thing for when my program crashes.
I came up with something that works reasonably well for my use-case (not really a stack-trace but it'll do for now).
Every interesting point in the source-code, I add a PUSH_BC();
Then when the Teensy4.1 reboots, I get:


Code:
Code was executing from address 0x41F82
...
  Breadcrumb #1 was 5751 (0x1677)
  Breadcrumb #2 was 9787 (0x263B)
  Breadcrumb #3 was 12379 (0x305B)
  Breadcrumb #4 was 9787 (0x263B)
  Breadcrumb #5 was 12379 (0x305B)
  Breadcrumb #6 was 55795 (0xD9F3)

In this case, 0x41F82 was the point where the crash was (here somewhere in memset()), the step before was at 0xD9F3 and then 0x305B and so on.
This helped me :)

It would be even more usefull if I could put it in __cyg_profile_func_enter and __cyg_profile_func_exit but sofar this only led to crashes (tips are welcome! see https://github.com/folkertvanheusden/arduinostacktraceexperiment ).

To use this, add the following to a header-file which you include in the files where you want to invoke PUSH_BC().

C++:
__attribute__ ((__noinline__)) void * get_pc();

extern std::vector<uint32_t> crumbs;
#define PUSH_BC() do { \
    crumbs.erase(crumbs.begin()); \
    crumbs.push_back(uint32_t(get_pc())); \
    for(int i=0; i<6; i++) \
        CrashReport.breadcrumb(i + 1, crumbs.at(i)); \
    } while(0)

Then in some .cpp/.ino file add:

C++:
#include <cstdint>
#include <vector>

std::vector<uint32_t> crumbs { 0, 0, 0, 0, 0, 0 };
__attribute__ ((__noinline__)) void * get_pc () { return __builtin_return_address(0); }
 
Last edited:
ARM thumb doesn't have a very well defined stack layout which makes it infeasible to unwind the stack without metadata (which is also infeasible to include on an embedded platform).
 
ARM thumb doesn't have a very well defined stack layout which makes it infeasible to unwind the stack without metadata (which is also infeasible to include on an embedded platform).

I don't think I agree with the embedded platform in general. E.g. the esp32 dumps a stacktrace when something bad happens.
 
Back
Top