Debugging pthreads

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Debugging pthreads

Postby PeterR » Fri Jun 07, 2019 12:26 pm

Hi,
I am using C++11 std::thread for portability. Sometimes I see:

Code: Select all

***ERROR*** A stack overflow in task pthread has been detected.
abort() was called at PC 0x4008496c on core 0
0x4008496c: vApplicationStackOverflowHook at C:/msys32/home/Pete/esp/esp-idf/components/esp32/panic.c:716
The error report does not seem to include an Id or name. How would I find out which task is at fault?
& I also believe that IDF CAN should be fixed.

mikemoy
Posts: 605
Joined: Fri Jan 12, 2018 9:10 pm

Re: Debugging pthreads

Postby mikemoy » Fri Jun 07, 2019 1:23 pm

You want to look into GetTaskHighWaterMark.
This is what I use on all my tasks to see how close they come to running out of stack.

Code: Select all

#define STACK_SIZE_IdleLoop 2048		// stack size for idleLoop

TaskHandle_t TaskHandle_IdleLoop;

/* -----------------------------------------------------------------------------
  GetTaskHighWaterMark( TaskHandle_t task_handle )

 	Input Params:
    - task_handle: The task name you want to examine

  Returns:  high water mark for tasks stack
  Example:  printf("Stack Water Mark: %u\r\n", GetTaskHighWaterMark( xTask1 ) );
  Notes:    The lower the result, means the closer you are to running out of
            stack space.
 -----------------------------------------------------------------------------*/
UBaseType_t GetTaskHighWaterMark( TaskHandle_t task_handle )
{
  UBaseType_t uxHighWaterMark;
  uxHighWaterMark = uxTaskGetStackHighWaterMark( task_handle );
  
  return uxHighWaterMark;
}
/* -----------------------------------------------------------------------------
  GetTaskHighWaterMarkPercent( TaskHandle_t task_handle, uint32_t stack_allotment )

 	Input Params:
    - task_handle: The task name you want to examine
   	- stack_allotment:  How much stack space did you allocate to it when you created it

  Returns: float with the % of stacke used
  Example:   printf("Stack Used %04.1f%%\r\n", GetTaskHighWaterMarkPercent(xTask1, 2048) );
  Notes:
 -----------------------------------------------------------------------------*/
float GetTaskHighWaterMarkPercent( TaskHandle_t task_handle, uint32_t stack_allotment )
{
  UBaseType_t uxHighWaterMark;

  uxHighWaterMark = uxTaskGetStackHighWaterMark( task_handle );

  uint32_t diff = stack_allotment - uxHighWaterMark;

  float result = ((float)diff / (float)stack_allotment) * 100.0;

  return result;
}
/* -----------------------------------------------------------------------------
  	idleloop()
 -----------------------------------------------------------------------------*/
void idleloop(void * parameter)
{
	
	for (;;)
	{
		printf("Stack Used %04.1f%%\r\n", GetTaskHighWaterMarkPercent(TaskHandle_IdleLoop, STACK_SIZE_IdleLoop) );
			
		vTaskDelay(500 / portTICK_PERIOD_MS);
	}	
}
/* -----------------------------------------------------------------------------
   app_main
 -----------------------------------------------------------------------------*/
void app_main()
{

	xTaskCreate(idleloop, "idleloop", STACK_SIZE_IdleLoop, NULL, 1, &TaskHandle_IdleLoop);
	
}

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: Debugging pthreads

Postby PeterR » Fri Jun 07, 2019 1:55 pm

Thanks, always nice to get some neat, commented code. I will use that for my FreeRTOS code.

But... I am using C++11 'tasks' not FreeRTOS.
So I am looking for a way to get std::thread.id out onto console.
& I also believe that IDF CAN should be fixed.

Who is online

Users browsing this forum: CatNoir, Google [Bot] and 114 guests