Instrumenting GCC to reduce stack usage

I was hunting down a stack overflow recently, and during investigation I added EXTRA_CFLAGS += -fstack-usage to my projects build.mk, but I couldn't find the expected .su files containing the estimated per-function stack usage estimates.

I looked in both the binary output dir: ${workspace}/target/6.2.0/boron/
and where the intermediate .o and .d files get stored: ~/.particle/toolchains/deviceOS/6.2.0/build/target/user/platform-13-m/my_proj/src. Neither contained the expected .su files.

It appears there was a bug in GCC a while back [1] that prevented the .su output if LTO (Link Time Optimization) is enabled. So this is possibly the issue?

Is there some cleanup by the build script that's deleting the .su files, or another setting that the Particle build is setting that's preventing gcc from outputting these files? Has anybody else successfully used -fstack-usage

I'm confidant my build.mk is getting processed correctly, because I fixed my overflow by setting EXTRA_CFLAGS += -Wstack-usage=XXX and compiling and walking XXX down to find the offending function.

I have avoided delving into the Particle build system as I'm admittedly not well-versed with Make and my eyes quickly gloss over when trying to track how it works from the chain of included files.

Related: Is it possible to call FreeRTOS kernel utilities from within my application thread? Specifically, I'd like to call uxTaskGetStackHighWaterMark [2] so I can decide how much effort I should allocate to walking my application away from the stack overflow cliff.

[1] c - ARM-gcc stack usage files empty - Stack Overflow
[2] uxTaskGetStackHighWaterMark, uxTaskGetStackHighWaterMark2 - FreeRTOS™

1 Like

I don't know the answer for sure, but my guess is that you cannot override that option in build.mk. Only a subset of the options can be set from build.mk.

My guess is that you will need to modify the Makefile for the user part in order enable that option. My second guess is that the combination of device and Device OS version is using LTO and an affected version of the gcc compiler.

Indeed, it's the usage of LTO, and the above-mentioned GCC bug still present in 10.2.1 that prevents the .su files from being generated. Alas.

To test: modify toolchains/deviceOS/<VERSION>/build/arm-tools.mk, to not use LTO. The .su files will be generated as expected.