Keil Logo

µVISION DEBUGGER: Accessing Stack Usage Watermarking Results

Information in this knowledgebase article applies to:

  • MDK-ARM Version 5.14 and higher


The Stack usage watermarking feature of µVision's Debugger determines the maximum stack usage for each thread.

The results are visible in the Stack Usage column, demonstrated in this screenshot:

Is it possible to access this information from within the application at runtime?


Yes. The stack of each thread is initialized with a "magic pattern" at thread creation time.

The task control block(TCB) data for each thread can be accessed to determine stack size and location.

First, you need access to some non-public symbols of the RTX kernel.

Include the following code snippet in the source module that will be accessing the stack information:

#undef NULL  //Workaround for conflicting macros in rt_TypeDef.h and stdio.h
#include <../SRC/rt_TypeDef.h>
extern uint32_t const os_stackinfo;
extern uint32_t const os_maxtaskrun;
extern void          *os_active_TCB[256];

To calculate the stack load for a task with the task control block p_TCB, create a function similar to this:

uint32_t rt_check_stack_load(P_TCB p_TCB)
    uint32_t *stk, size, used = 0;

    size = p_TCB->priv_stack >> 2;
    if (size == 0)
        size = (uint16_t) os_stackinfo >> 2;

    stk = &p_TCB->stack[1];

    while (*stk == MAGIC_PATTERN)
    return((size - ((used + 1))) * sizeof(stk));

This example loop runs through all active task control blocks and returns the maximum stack load determined by stack watermarking.

if (os_stackinfo & 0x10000000U)  //Stack Usage Watermarking is enabled
  uint32_t tcb_idx;
  for (tcb_idx = 0; tcb_idx < os_maxtaskrun; tcb_idx++)
    uint32_t stackleft = 0;
    if (os_active_TCB[tcb_idx] != NULL)    //TCB block is used by active task?
      stackleft = rt_check_stack_load(os_active_TCB[tcb_idx]);
      printf("Task %d stack left: %d\n", tcb_idx+1, stackleft);

Note - this is not an officially supported functionality of the CMSIS-RTOS RTX. To make sure that updates of the RTX version do not introduce incompatibilities, use a macro to test for the correct version.

#if (osCMSIS_RTX != ((4<<16)|78))
  #warning Wrong RTX kernel version for stack testing

Increment or extend the approved version numbers according to your test coverage as required.




The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Friday, February 24, 2017

Did this article provide the answer you needed?
Not Sure
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.