2023-07-20 01:53 AM - edited 2023-07-20 02:00 AM
Hello. I have been programming for a couple of years but never really used proper debugging/monitor tools. I have been using printf method for debugging most of my projects.
I am now learning about different and more powerful debugging tools that are easily available in STM32CubeIDE. Particularly, I am interested in looking at stack/heap memory. Is there a possibility to keep track of stack and heap memory during run time?
I have created a simple test project for the STM32L031 nucleo board:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
uint8_t array_test_global[2000] = {0};
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
uint8_t array_test_local[1000] = {0};
array_test_local[5] = 5;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
int i = 0;
i = 5;
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = 0;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* file: pointer to the source file name
* line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
The example project above is basically just declaring global array array_test_global and then later I declare array_test_local
I place breakpoints in various positions and monitoring via Build Analyzer tool:
But I cant see the variables that I declare in the memory.
I have also tried to look at Static Stack analyzer but cant see anything usefull there either:
Can someone point me in the right direction and help me understand how to properly monitor stack and heap memory (see which variables are declared in stack memory and their address, also see which variables are declared in heap memory and etc).
Also, maybe you have some other tools that you use and could suggest ? :)
Thanks in advance.
2023-07-20 02:46 AM
Hi @LPetr.1,
for heap and stack use, basically only the peak usage is interesting. For that I usually initialize the memory with a pattern (0x5A5A, but anything works) and execute a test, ideally one with high branch coverage. After the test concludes, there's endless loop or a breakpoint.
Simply observing the SRAM and noting the address where the pattern was overwritten will give you the exact stack and heap ceiling. The advantage is that this method works the same regardless of development tools used.
The stack usage increases with lots of function calls and many parameters passed to the functions. It's generally better to limit the number of parameters or pass a pointer to a structure. Local variables also add up to the stack size, in some cases.
Many embedded app don't use heap at all. Heap is dynamically allocated memory (malloc) and lots of embedded developers prefer to place static declarations, which are then visible in a map file produced by the linker.
CubeIDE has some pretty advanced monitoring tools, I hope someone else will fill in with details about that.
BR,
Jarda
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2023-07-31 04:27 AM
Hi @LPetr.1 ,
I can add that you can make use of STM32CubeIDE Static Stack Analyzer. Please refer to UM2609 STM32CubeIDE user guide for more details on the way to use it.
-Amel
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2023-07-31 09:20 PM
Please see my example project and how I tried to use the static stack analyzer.
You can see in my example project that I create a few large arrays:
uint8_t array_test_global[2000] = {0};
uint8_t array_test_local[1000] = {0};
but they are not showing up on the static stack analyzer.
2023-07-31 10:38 PM
That's probably because your variables are not used in the project and optimized away by the compiler&linker. Make the definitions volatile and/or add some code using the arrays in a nontrivial way, like computing a checksum,...
hth
KnarfB
2023-08-01 03:14 AM - edited 2023-08-01 03:20 AM
I have thought about the fact that maybe this is because I am not using my variables in the project so they are optimized away. That is exactly why I have added:
array_test_global[4] = 2;
array_test_local[5] = 10;
To make sure they are being used. Is that not enough? I would assume at least something should show up in stack analyzer.
2023-08-01 03:19 AM
No. The optimizer is smart, so if you never use those variables, it won't create the array.
Even if you use it in some simple way, it may figure out that the array is not needed.
Use volatile.
JW
2023-08-01 05:37 AM
Thank you for explaining.
From what I understood, static task analyzer is for analyzing functions.
I have created a 1KB buffer:
volatile uint8_t test_buffer[1024];
and then I perform some actions in while loop:
while (1)
{
HAL_Delay(1500);
/* USER CODE END WHILE */
static uint8_t counter = 0;
test_buffer[counter] = 5;
counter++;
/* USER CODE BEGIN 3 */
}
I can confirm that I can see the variables in memory details:
2023-08-01 06:16 AM
test_buffer is a global variable. It's in the .bss section (which are implicitly zeroed global variables) so you won't see it in the stack analysis, as it's not created on the stack.
JW