This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Stack checking with CMSIS-RTOS RTX

So I've run into this before, and wasn't able to figure it out, other than very painful trial-and-error guesswork.

I'm using CMSIS-RTOS RTX on Energy Micro EFM32GG devices, and I now have a blown stack. When the stack checker finds a blown stack, it calls os_error(uint32_t err_code). However, I am not able to figure out which thread blew its stack!. I tried the following in my os_error function, but it doesn't work; id is always NULL.

  static volatile osThreadId id;
  id = osThreadGetId();

Also, is there any way to get stack usage to help in optimizing their allocation?

BTW - I'm using the CodeSourcery GCC with Eclipse.

Any advice? Thanks!

Parents
  • I've got the same issue here. It's quite difficult to track which task that contributes to stack overflow.

    The issue is:
    1) os_error() is called within ISR context
    2) The osThreadGetId() implementation rejects calling this function within ISR context.

    From rt_CMSIS.c:
    /// Return the thread ID of the current running thread
    osThreadId osThreadGetId (void) {
      if (__get_IPSR() != 0) return NULL;           // Not allowed in ISR
      return __svcThreadGetId();
    }
    

    I've tracked the source code. Trying to implement this in my os_error()

    /* Error Codes */
    #define OS_ERR_STK_OVF          1
    #define OS_ERR_FIFO_OVF         2
    #define OS_ERR_MBX_OVF          3
    
    extern osThreadId svcThreadGetId (void);
    
    void os_error (uint32_t err_code) {
      /* This function is called when a runtime error is detected. Parameter */
      /* 'err_code' holds the runtime error code (defined in RTL.H).         */
    
      osThreadId err_task = svcThreadGetId();
      printf("CMSIS RTX err_code: 0x%X ", err_code);
      printf("TASK_ID = 0x%X\r\n", (int)err_task);
    
      /* HERE: include optional code to be executed on runtime error. */
      for (;;);
    }
    

    Haven't tried it yet, waiting for my board to be ready. But hopefully works

Reply
  • I've got the same issue here. It's quite difficult to track which task that contributes to stack overflow.

    The issue is:
    1) os_error() is called within ISR context
    2) The osThreadGetId() implementation rejects calling this function within ISR context.

    From rt_CMSIS.c:
    /// Return the thread ID of the current running thread
    osThreadId osThreadGetId (void) {
      if (__get_IPSR() != 0) return NULL;           // Not allowed in ISR
      return __svcThreadGetId();
    }
    

    I've tracked the source code. Trying to implement this in my os_error()

    /* Error Codes */
    #define OS_ERR_STK_OVF          1
    #define OS_ERR_FIFO_OVF         2
    #define OS_ERR_MBX_OVF          3
    
    extern osThreadId svcThreadGetId (void);
    
    void os_error (uint32_t err_code) {
      /* This function is called when a runtime error is detected. Parameter */
      /* 'err_code' holds the runtime error code (defined in RTL.H).         */
    
      osThreadId err_task = svcThreadGetId();
      printf("CMSIS RTX err_code: 0x%X ", err_code);
      printf("TASK_ID = 0x%X\r\n", (int)err_task);
    
      /* HERE: include optional code to be executed on runtime error. */
      for (;;);
    }
    

    Haven't tried it yet, waiting for my board to be ready. But hopefully works

Children
No data