SuperAPI AI Directory

memory-analysis

General & other

View repository

Updated 10-15 23:58

About this skill

Firmware Memory Analysis

Applicable Scenarios

  • After the build completes, you need to understand the firmware's Flash/RAM usage.
  • You need to find the functions or variables that occupy the most space.
  • You need to compare the memory changes between two builds and track code bloat.
  • You need to confirm whether the firmware is about to exceed the chip's memory limit.
  • You need to parse the linker script to obtain the chip's total Flash/RAM capacity.

Required Inputs

  • .map file path (generated by the GCC/ARM linker), or ELF file path.
  • Optional linker script path (used to calculate the usage percentage).
  • Optional warning threshold and Top-N symbol count.

Automatic Detection

  • --scan mode automatically searches the build directory for .map and ELF files.
  • Parse the .map block in the Memory Configuration file to obtain the total FLASH/RAM capacity.
  • If a linker script is provided, parse the capacity information from the MEMORY {} block.
  • Automatically detect the availability of arm-none-eabi-size and arm-none-eabi-readelf .

Execution Steps

  1. First read references/usage.md, and confirm whether this is environment detection, build directory scanning, or performing analysis.
  2. If you are unsure whether the tool is available, first run the scripts/memory_analyzer.py mode of --detect to confirm.
  3. Use --scan <build_dir> to automatically find files that can be analyzed.
  4. Use --map-file or --elf to perform memory analysis.
  5. Optionally use --linker-script to obtain the total capacity and calculate the usage percentage.
  6. Use --top <N> to view the symbols that occupy the most space.
  7. Use --compare to compare the differences between two .map files.

Failure Triage

  • When arm-none-eabi-size or arm-none-eabi-readelf is unavailable and ELF analysis is needed, return environment-missing。
  • When the specified .map or ELF file does not exist, return artifact-missing。
  • When the .map file format cannot be recognized or parsing fails, return project-config-error。
  • When the memory usage exceeds the warning threshold, include warning information in the successful result.

Platform Notes

  • .map file parsing uses pure regular expressions, requires no external tools, and is available on all platforms.
  • ELF analysis requires arm-none-eabi-size, which is usually installed with the cross-compilation toolchain.
  • Linker script parsing uses pure regular expressions and supports the GCC LD format.

Output Conventions

  • Output the size and address of each section (.text, .rodata, .data, .bss).
  • Output Flash usage (.text + .rodata + .data) and RAM usage (.data + .bss).
  • If total capacity information is available, output the usage percentage and warning status.
  • Output the Top-N symbol list sorted by size.
  • Comparison mode outputs the increases and decreases of each section and symbol.

Handoff Relationships

  • When memory is about to overflow and optimization is needed, recommend that the user review the Top-N largest symbols.
  • When you need to locate the position of a specific function in the code, you can use it together with the IDE or grep .