当前位置:网站首页>A simple and general method to obtain the size of function stack space
A simple and general method to obtain the size of function stack space
2022-07-29 03:15:00 【Stars sink and earth move】
Sometimes we need to evaluate the stack space used by a function , For example, when creating threads , We need to estimate the stack space required by this thread function , Allocate the appropriate thread stack .
This paper introduces a simple method , To get the stack space occupied by a function .
principle :
For the decreasing stack memory model , When you call a function , The stack pointer will move down , The distance moved depends on the local variable of the function and the calling sub function , That is, the stack space used by this function , When the function returns , The stack pointer will return to the position before the call .
According to this characteristic , We can fill the memory at the bottom of the stack with a specific value before calling the function , After the call, check whether the value at the bottom of the stack changes , At the end of the search 1 The changed address is the bottom of the stack used by this function . By subtracting the pointer at the bottom of the stack from the current stack pointer, the actual stack space used by this function can be calculated .
The implementation of :
I don't say much nonsense , Test the code directly :
// use 0xAA Fill the bottom of the stack 512 byte
void stack_test_begin(void)
{
register int i;
unsigned char mem[512];
for(i = 0; i < 512; i++)
{
mem[i] = 0xAA;
}
}
// Check the bottom of the stack , Return stack size
int stack_test_end(void)
{
register int i;
unsigned char mem[512];
for(i = 0; i < 512; i++)
{
if(mem[i] != 0xAA)
{
return 512 - i;
}
}
return 0;
}
// Tested function , The stack space occupied by this function theory is 128 byte
void stack_use_128(void)
{
register int i;
char mem[128];
for(i=0; i<128; i++)
{
mem[i] = 0;
}
}
// test
void main(void)
{
int stack_size;
stack_test_begin();
stack_use_128();
stack_size = stack_test_end();
printf("stack: %d\n", stack_size);
}
Code reading :
For the convenience of analysis , hypothesis main The initial stack pointer of the function is SP=0x00001000.
1. call stack_test_begin() , This function local variable mem Occupy 512 byte , this 512 For byte 0xAA fill , It will actually lead to 0x00000E00 - 0x00001000 Filled with 0xAA. This completes the step of filling a specific value at the bottom of the stack .
2. call stack_use_128(), This function is the target function that we want to obtain stack space , The operation of this function on local variables , Will cause us to fill 0xAA Change to other values .
3. call stack_test_end(), This function also uses 512 Local variables of bytes , We search this 512 Byte space , Look back before , Find not 0xAA Value , It means here is stack_use_128() Stack boundary used by function . This boundary was previously unused stack memory , So with 512 Subtracting this boundary value is the actual stack space used by the function .
4. If the stack space of the function under test exceeds 512, Need to put 512 Change to a larger value , Add more fill depth .
summary :
In this method ARM and x86 The test on the platform can work normally . Independent of system calls , It's universal .
But filling memory and searching memory will consume CPU resources , Affect overall performance , Therefore, it is only suitable for use in the code debugging phase .
End
边栏推荐
- Unable to start after idea installation
- 三子棋(玩家+电脑)
- Photo scale correction tool: DxO viewpoint 3 direct mount version
- 一种简单通用的获取函数栈空间大小的方法
- 4000 多字学懂弄通 js 中 this 指向问题,顺便手写实现 call、apply 和 bind
- 万字详解 Google Play 上架应用标准包格式 AAB
- Typescript learning (I)
- Redis configuration cache expiration listening event trigger
- Summary of basic knowledge points of C language
- 增量实时灾备笔记
猜你喜欢
随机推荐
01-SDRAM:初始化模块的代码
西瓜书学习第六章---SVM
Minesweeping simple version
My approval function of conference OA project
数字图像处理 第10章——图像分割
三子棋(玩家+电脑)
Alibaba Sentinel - workflow and principle analysis
「PHP基础知识」输出圆周率的近似值
What if MySQL forgets the password
融云实时社区解决方案
C陷阱与缺陷 第3章 语义“陷阱” 3.4 避免“举偶法”
STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)
扫雷简单版
Score addition and subtraction of force deduction and brushing questions (one question per day 7/27)
Sanzi chess (player + computer)
《QA离业务代码能有多近?》通过codediff直接暴露缺陷
Shell编程规范与变量
C陷阱与缺陷 第3章 语义“陷阱” 3.6 边界计算与不对称边界
GJB常见混淆概念
爆肝整理JVM十大模块知识点总结,不信你还不懂







