State Inspection Script

Asset Tracker Template

tags
Asset Tracker Template

The inspect_state.py script allows you to inspect the current state of the application's state machines and internal data structures on a running device. It supports two modes of operation:

  • Live Inspection: It connects to the device using J-Link, parses the ELF file to find symbol locations and types, and reads the memory to display the current state.
  • Coredump Analysis: It can also analyze a coredump file generated by the device, allowing you to inspect the state at the time of the crash.

This is particularly useful for debugging when the application is stuck or behaving unexpectedly, and you want to see the exact state of each module without halting the CPU or adding extensive logging, or when analyzing crashes.

Prerequisites:

  • Python 3 installed.
  • Required Python packages: pyelftools (>=0.30) and pylink-square.
    pip install "pyelftools>=0.30" pylink-square
    
  • J-Link debug probe connected to the device.
  • The zephyr.elf file corresponding to the running firmware.

Usage:

Run the script from the scripts directory (or adjust the path), providing the path to your ELF file and optionally the J-Link device name:

# Live inspection
python3 Asset-Tracker-Template/scripts/inspect_state.py --elf build/app/zephyr/zephyr.elf
# Coredump analysis
python3 Asset-Tracker-Template/scripts/inspect_state.py --elf path/to/symbols.elf --coredump path/to/coredump.elf

Example Output:

The script first shows a summary table of all modules and their current state:

Connecting to J-Link (Cortex-M33)...

...

Module          | Current State                                                | Details
----------------------------------------------------------------------------------------------------
Main            | STATE_CONNECTED_WAITING                                      | Ptr: 0x00094D80
Cloud           | STATE_CONNECTED_READY                                        | Ptr: 0x000966F0
Location        | STATE_LOCATION_SEARCH_INACTIVE                               | Ptr: 0x00096628
Network         | STATE_CONNECTED                                              | Ptr: 0x00094E70
FOTA            | STATE_WAITING_FOR_POLL_REQUEST                               | Ptr: 0x000967A8
Env             | STATE_RUNNING                                                | Ptr: 0x000965BC
Power           | STATE_RUNNING                                                | Ptr: 0x00094F68
Storage         | STATE_BUFFER_IDLE                                            | Ptr: 0x00096848

Options:
  q: Quit
  r: Refresh summary
  1: Inspect Main
  2: Inspect Cloud
  3: Inspect Location
  4: Inspect Network
  5: Inspect FOTA
  6: Inspect Env
  7: Inspect Power
  8: Inspect Storage

Select option: 2

Selecting a module (for example, 2 for Cloud) reveals the detailed structure of its state variable, including all internal members:

--- Cloud State Details ---
Address: 0x0008C8C0
Type: cloud_state
----------------------------------------
ctx                 : STATE_CONNECTED_READY
chan                : 0x0008BE5C
msg_buf             : Array[584] (too large to display)
network_connected   : 1 (0x1)
provisioning_ongoing: 0 (0x0)
connection_attempts : 2 (0x2)
backoff_time        : 60 (0x3C)
----------------------------------------