Memory section placement for state objects

Asset Tracker Template

tags
Asset Tracker Template

The template places all module state objects, which are the Main, Cloud, Location, Network, and Storage module objects, explicitly in the .data section of memory using the __attribute__((section(".data"))) compiler attribute:

/* Place state object in .data section to ensure it is captured in coredumps
 * and can be inspected by external tools during state analysis. */
__attribute__((section(".data"))) static struct main_state main_state = {
    .sample_interval_sec = CONFIG_APP_SAMPLING_INTERVAL_SECONDS,
    .storage_threshold = CONFIG_APP_STORAGE_INITIAL_THRESHOLD,
    .first_sample_pending = true,
};

By default, Memfault coredumps do not include the .bss section. Without explicit initialization or attribute placement in the .data section, the compiler would place the state objects in .bss, and they would not appear in coredumps. Placing these objects in .data captures them in the Memfault coredumps, so the inspect_state.py script can inspect them when you analyze crashes after the fact. As a result, you can access the complete state of all module state machines at the time of the crash or event, even in post-mortem debugging scenarios where the device is not under a live debugger.