Home Learning Paths ECU Lab Assessments Interview Preparation Arena Pricing Log In Sign Up

ARM Stack Frame Structure (AAPCS)

Cortex-M Exception Stack Frame (hw-pushed on exception entry)
  Higher address ┐
  xPSR            ← SP+28  (Program Status Register including Thumb state)
  PC (ReturnAddr) ← SP+24  (address to return to after exception handler)
  LR  (R14)       ← SP+20  (link register at point of exception)
  R12             ← SP+16
  R3              ← SP+12
  R2              ← SP+8
  R1              ← SP+4
  R0              ← SP+0   ← current SP after exception entry
  Lower address  ┘

  Function prologue (AAPCS callee-saved):
  PUSH {R4-R11, LR}  -- saved by function; restored on return
  SUB  SP, # -- local variables below saved registers

  Stack grows downward: lower address = newer frame
  LR=0xFFFFFFF9 = return to Thread mode, use MSP (Main Stack Pointer)
  LR=0xFFFFFFFD = return to Thread mode, use PSP (Process Stack Pointer / task stack)

TRACE32 Backtrace and Frame Navigation

CMMcall_stack.cmm
// Call stack analysis in TRACE32

// Display call stack (backtrace) — requires DWARF frame info in ELF
Frame.view /Locals /Caller    // shows call chain + local variables per frame

// Navigate between frames
Frame.Up                      // move to caller frame (set register context)
Frame.Down                    // move back to callee frame
Frame /Count 20               // show up to 20 frames

// Print frame 0 (innermost) locals
Frame.LOCAL 0.                // print local variables of frame 0

// Extract PC and LR for manual analysis
PRINT "PC=" Register(PC) " LR=" Register(LR) " SP=" Register(SP)

// Reconstruct stack manually if DWARF unwind fails
// (e.g., optimised code, missing debug info)
// Search for saved LR values on the stack:
Data.FIND Register(SP)--Register(SP)+0x200 0xFFFFFF?? /Long
// Each 0xFFFFFFF9/FD found is a potential saved LR (exception return magic)

// Verify stack pointer is within task stack bounds
LOCAL &sp &stack_base &stack_size
&sp=Register(SP)
&stack_base=Var.VALUE(task0_stack)
&stack_size=0x1000
IF (&sp<&stack_base)||(&sp>&stack_base+&stack_size)
  PRINT %ERROR "SP OUT OF TASK STACK BOUNDS!"
ELSE
  PRINT "SP OK: " FORMAT.HEX(8.,&sp)

Diagnosing a Corrupted Call Stack

Stack Corruption Investigation Flow
  Symptom: Frame.view shows garbage frames or wrong symbol names
       │
       ▼
  Step 1: Verify SP is valid
    PRINT Register(SP)   -- in range? within task stack?
       │
       ├── SP out of range → stack overflow (see stack-overflow-memory-corruption lesson)
       └── SP in range → frame pointers corrupted
             │
             ▼
  Step 2: Manual LR/PC scan
    Data.FIND Register(SP)--Register(SP)+0x400 0xFFFFFF?? /Long
    -- locate hardware-pushed PC values; cross-reference to map file
       │
       ▼
  Step 3: Find last known-good frame
    -- set temporary breakpoint 3 levels up in expected call chain
    -- re-run with same conditions; capture stack at breakpoint
       │
       ▼
  Step 4: Set write watchpoint on corrupted stack region
    Break.Set  /Write /Hardware
    -- captures the exact instruction that overwrote the stack

RTOS Per-Task Stack Inspection

CMMrtos_stacks.cmm
// AUTOSAR OS / FreeRTOS per-task stack analysis
// Requires TRACE32 OS-awareness (AUTOSAR package or FreeRTOS plugin)

// Enable AUTOSAR OS awareness (must match ORTI file)
TASK.CONFIG OS_config.t32    // load ORTI task configuration
TASK.List                    // list tasks: name, state, priority, stack used/total

// Navigate to a specific task's context
TASK.select "EcuM_Task"      // switch register context to that task
Frame.view /Locals /Caller   // backtrace within that task's stack

// Check stack high-water mark (filled with 0xCD at init)
Var.View OsTask_EcuM_Task.StackMem[0..255]  // look for last non-0xCD value

// Calculate stack usage %
LOCAL &base &top &used &size
&base=Var.VALUE(OsTask_EcuM_Task.StackMem)
&size=Var.VALUE(sizeof(OsTask_EcuM_Task.StackMem))
&top=&base+&size
// Scan for first non-0xCD from bottom
// Stack used = top - first_dirty_address

Summary

The ARM Cortex-M hardware-pushes 8 registers (R0-R3, R12, LR, PC, xPSR) on exception entry — these are the most reliable frame data after a hard fault. Frame.view with /Locals works well for unoptimised code; for release builds with inlined functions and frame-pointer elimination, a manual LR scan on the raw stack is often more reliable. Always verify SP is within the active task's stack bounds first — an out-of-range SP invalidates everything the debugger shows for the call chain.

🔬 Deep Dive — Core Concepts Expanded

This section builds on the foundational concepts covered above with additional technical depth, edge cases, and configuration nuances that separate competent engineers from experts. When working on production ECU projects, the details covered here are the ones most commonly responsible for integration delays and late-phase defects.

Key principles to reinforce:

  • Configuration over coding: In AUTOSAR and automotive middleware environments, correctness is largely determined by ARXML configuration, not application code. A correctly implemented algorithm can produce wrong results due to a single misconfigured parameter.
  • Traceability as a first-class concern: Every configuration decision should be traceable to a requirement, safety goal, or architecture decision. Undocumented configuration choices are a common source of regression defects when ECUs are updated.
  • Cross-module dependencies: In tightly integrated automotive software stacks, changing one module's configuration often requires corresponding updates in dependent modules. Always perform a dependency impact analysis before submitting configuration changes.

🏭 How This Topic Appears in Production Projects

  • Project integration phase: The concepts covered in this lesson are most commonly encountered during ECU integration testing — when multiple software components from different teams are combined for the first time. Issues that were invisible in unit tests frequently surface at this stage.
  • Supplier/OEM interface: This is a topic that frequently appears in technical discussions between Tier-1 ECU suppliers and OEM system integrators. Engineers who can speak fluently about these details earn credibility and are often brought into critical design review meetings.
  • Automotive tool ecosystem: Vector CANoe/CANalyzer, dSPACE tools, and ETAS INCA are the standard tools used to validate and measure the correct behaviour of the systems described in this lesson. Familiarity with these tools alongside the conceptual knowledge dramatically accelerates debugging in real projects.

⚠️ Common Mistakes and How to Avoid Them

  1. Assuming default configuration is correct: Automotive software tools ship with default configurations that are designed to compile and link, not to meet project-specific requirements. Every configuration parameter needs to be consciously set. 'It compiled' is not the same as 'it is correctly configured'.
  2. Skipping documentation of configuration rationale: In a 3-year ECU project with team turnover, undocumented configuration choices become tribal knowledge that disappears when engineers leave. Document why a parameter is set to a specific value, not just what it is set to.
  3. Testing only the happy path: Automotive ECUs must behave correctly under fault conditions, voltage variations, and communication errors. Always test the error handling paths as rigorously as the nominal operation. Many production escapes originate in untested error branches.
  4. Version mismatches between teams: In a multi-team project, the BSW team, SWC team, and system integration team may use different versions of the same ARXML file. Version management of all ARXML files in a shared repository is mandatory, not optional.

📊 Industry Note

Engineers who master both the theoretical concepts and the practical toolchain skills covered in this course are among the most sought-after professionals in the automotive software industry. The combination of AUTOSAR standards knowledge, safety engineering understanding, and hands-on configuration experience commands premium salaries at OEMs and Tier-1 suppliers globally.

← PreviousMemory View & Register InspectionNext →Hands-On: First Debug Session