stm32栈空间分布分析——让你能更合理的设置栈空间,防止栈溢出
[color=rgba(0, 0, 0, 0.749019607843137)]下面是我们要使用的测试代码,先贴出来, 为了更好地分析栈空间我们把栈空间用串口dump出来
[color=rgba(0, 0, 0, 0.749019607843137)]
[color=rgba(0, 0, 0, 0.749019607843137)]void hex_dump(unsigned char *p, int len)
[color=rgba(0, 0, 0, 0.749019607843137)]{
[color=rgba(0, 0, 0, 0.749019607843137)] uint32_t i, j;
[color=rgba(0, 0, 0, 0.749019607843137)]
[color=rgba(0, 0, 0, 0.749019607843137)] //printf("\n===============dump start====================\n");
[color=rgba(0, 0, 0, 0.749019607843137)] printf("\r\n[");
[color=rgba(0, 0, 0, 0.749019607843137)] for (i = 0, j = 0; i < len; i++)
[color=rgba(0, 0, 0, 0.749019607843137)] {
[color=rgba(0, 0, 0, 0.749019607843137)] printf("%02x ", p);
[color=rgba(0, 0, 0, 0.749019607843137)]
[color=rgba(0, 0, 0, 0.749019607843137)] if (j < 15)
[color=rgba(0, 0, 0, 0.749019607843137)] {
[color=rgba(0, 0, 0, 0.749019607843137)] j++;
[color=rgba(0, 0, 0, 0.749019607843137)] }
[color=rgba(0, 0, 0, 0.749019607843137)] else
[color=rgba(0, 0, 0, 0.749019607843137)] {
[color=rgba(0, 0, 0, 0.749019607843137)] j = 0;
[color=rgba(0, 0, 0, 0.749019607843137)] printf("\r\n");
[color=rgba(0, 0, 0, 0.749019607843137)] }
[color=rgba(0, 0, 0, 0.749019607843137)] }
[color=rgba(0, 0, 0, 0.749019607843137)] printf("]\r\n");
[color=rgba(0, 0, 0, 0.749019607843137)] //printf("\n===============dump end====================\n");
[color=rgba(0, 0, 0, 0.749019607843137)]}
[color=rgba(0, 0, 0, 0.749019607843137)]
[color=rgba(0, 0, 0, 0.749019607843137)]void statck_dump()
[color=rgba(0, 0, 0, 0.749019607843137)]{
[color=rgba(0, 0, 0, 0.749019607843137)] hex_dump((char *)(0x20000460-0x400), 0x400);
[color=rgba(0, 0, 0, 0.749019607843137)]}
[color=rgba(0, 0, 0, 0.749019607843137)]
[color=rgba(0, 0, 0, 0.749019607843137)]void test_func(int v)
[color=rgba(0, 0, 0, 0.749019607843137)]{
[color=rgba(0, 0, 0, 0.749019607843137)] printf("%d\r\n", v);
[color=rgba(0, 0, 0, 0.749019607843137)] statck_dump();
[color=rgba(0, 0, 0, 0.749019607843137)]}
[color=rgba(0, 0, 0, 0.749019607843137)]
[color=rgba(0, 0, 0, 0.749019607843137)]void func(void)
[color=rgba(0, 0, 0, 0.749019607843137)]{
[color=rgba(0, 0, 0, 0.749019607843137)] char buf[50] = {0};
[color=rgba(0, 0, 0, 0.749019607843137)] memset(buf, 0x22, sizeof(buf));
[color=rgba(0, 0, 0, 0.749019607843137)] buf[0] = 0xFF;
[color=rgba(0, 0, 0, 0.749019607843137)] statck_dump();
[color=rgba(0, 0, 0, 0.749019607843137)]}
[color=rgba(0, 0, 0, 0.749019607843137)]
[color=rgba(0, 0, 0, 0.749019607843137)]/**
[color=rgba(0, 0, 0, 0.749019607843137)] * @brief The application entry point.
[color=rgba(0, 0, 0, 0.749019607843137)] * @retval int
[color=rgba(0, 0, 0, 0.749019607843137)] */
[color=rgba(0, 0, 0, 0.749019607843137)]int main(void)
[color=rgba(0, 0, 0, 0.749019607843137)]{
[color=rgba(0, 0, 0, 0.749019607843137)] int a = 10;
[color=rgba(0, 0, 0, 0.749019607843137)] int b = 20;
[color=rgba(0, 0, 0, 0.749019607843137)] int c = a + b;
[color=rgba(0, 0, 0, 0.749019607843137)] char buf[100] = {0};
[color=rgba(0, 0, 0, 0.749019607843137)] memset(buf, 0x55, sizeof(buf));
[color=rgba(0, 0, 0, 0.749019607843137)] buf[0] = 1;
[color=rgba(0, 0, 0, 0.749019607843137)] buf[1] = 2;
[color=rgba(0, 0, 0, 0.749019607843137)] buf[2] = 3;
[color=rgba(0, 0, 0, 0.749019607843137)]
[color=rgba(0, 0, 0, 0.749019607843137)] HAL_Init();
[color=rgba(0, 0, 0, 0.749019607843137)] SystemClock_Config();
[color=rgba(0, 0, 0, 0.749019607843137)] MX_GPIO_Init();
[color=rgba(0, 0, 0, 0.749019607843137)] MX_USART1_UART_Init();
[color=rgba(0, 0, 0, 0.749019607843137)]
[color=rgba(0, 0, 0, 0.749019607843137)] printf("<-- start -->\r\n");
[color=rgba(0, 0, 0, 0.749019607843137)] printf("&a = %p\r\n", &a);
[color=rgba(0, 0, 0, 0.749019607843137)] printf("&b = %p\r\n", &b);
[color=rgba(0, 0, 0, 0.749019607843137)] printf("&c = %p\r\n", &c);
[color=rgba(0, 0, 0, 0.749019607843137)] test_func(c);
[color=rgba(0, 0, 0, 0.749019607843137)]
[color=rgba(0, 0, 0, 0.749019607843137)] while (1)
[color=rgba(0, 0, 0, 0.749019607843137)] {
[color=rgba(0, 0, 0, 0.749019607843137)] printf("<-- run -->\r\n");
[color=rgba(0, 0, 0, 0.749019607843137)] statck_dump();
[color=rgba(0, 0, 0, 0.749019607843137)] func();
[color=rgba(0, 0, 0, 0.749019607843137)] while(1);
[color=rgba(0, 0, 0, 0.749019607843137)] }
[color=rgba(0, 0, 0, 0.749019607843137)]}
[color=rgba(0, 0, 0, 0.75)]
|
|