在 main 开始之后使用 (0xABABABAB) 模式绘制嵌入式堆栈的 c 代码?
我正在使用堆栈绘画/脚打印分析方法进行动态内存分析。
nofollow noreferrer“ -footprint-Analysis
基本上的想法是在应用程序开始执行之前,用专用填充值(例如0xabababab)填充分配给堆栈区域的整个内存量。每当执行停止时,都可以从堆栈的末端向上搜索堆栈内存,直到找到0 Xabababis的值,这被认为是使用堆栈已使用的距离。如果找不到专用值,则该堆栈消耗了所有堆栈空间,并且很可能已经溢出。
我希望AC代码用图案从上到下填充堆栈。
void FillSystemStack()
{
extern char __stack_start,_Stack_bottom;
}
请注意
- ,我正在使用Eclipse Qemu模拟的STM32F407VG板。
- 堆栈从较高地址到较低地址
- 的堆栈开始增长为0x20020000
- 堆栈的底部是OX2001FC00
I am working on dynamic memory analysis using stack painting/foot print analysis method.
dynamic-stack-depth-determination-using-footprint-analysis
basically the idea is to fill the entire amount of memory allocated to the stack area with a dedicated fill value, for example 0xABABABAB, before the application starts executing. Whenever the execution stops, the stack memory can be searched upwards from the end of the stack until a value that is not 0xABABABABis found, which is assumed to be how far the stack has been used. If the dedicated value cannot be found, the stack has consumed all stack space and most likely has overflowed.
I want a c code to fill the stack from top to bottom with a pattern.
void FillSystemStack()
{
extern char __stack_start,_Stack_bottom;
}
NOTE
- I am using STM32F407VG board emulated with QEMU on eclipse.
- stack is growing from higher address to lower address
- start of the stack is 0x20020000
- bottom of the stack is Ox2001fc00
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
main() 开始后完全不应该填满堆栈,因为
main()
堆栈已被使用一次代码>开始。完全填充堆栈将覆盖已使用的堆栈位,并可能导致未定义的行为。我想您可以在main()
开始后立即填充堆栈的部分,只要您小心不要覆盖已经使用的部分即可。但更好的计划是在调用 main() 之前使用模式填充堆栈。查看工具链的启动代码。启动代码在调用
main()
之前初始化变量值并设置堆栈指针。启动代码可能处于汇编状态,具体取决于您的工具链。初始化变量的代码可能是一个简单的循环,它将字节或字从适当的 ROM 复制到 RAM 部分。您可以使用此代码作为示例来编写一个新循环,该循环将使用模式填充堆栈内存范围。You shouldn't completely fill the stack after
main()
begins, because the stack is in use oncemain()
begins. Completely filling the stack would overwrite the bit of stack that has already been used and could lead to undefined behavior. I suppose you could fill a portion of the stack soon aftermain()
begins as long as you're careful not to overwrite the portion that has been used already.But a better plan is to fill the stack with a pattern before main() is called. Review the startup code for your tool chain. The startup code initializes variable values and sets the stack pointer before calling
main()
. The startup code may be in assembly depending on your tool chain. The code that initializes variables is probably a simple loop that copies bytes or words from the appropriate ROM to RAM sections. You can probably use this code as an example to write a new loop that will fill the stack memory range with a pattern.这是一个皮层M,因此将堆栈指针从重置中列出。这意味着几乎可以立即准备使用C代码。如果您的重置向量是用C编写的并执行堆叠/C功能调用,那么在很早的阶段填充堆栈就为时已晚。这意味着您不应该从应用程序C代码中进行操作。
执行您描述的技巧的正常方法是通过电路调试器。下载程序,命中重置,在调试器的帮助下填充堆栈。将有一些方便的调试器命令可以做到这一点。执行程序,尝试使用尽可能多的程序,观察调试器内存图中的堆栈。
This is a Cortex M so it gets the stack pointer set out of reset. Meaning it's pretty much instantly ready to go for C code. If your reset vector is written in C and performs stacking/C function calls, it will be too late to fill the stack at a very early stage. Meaning you shouldn't do it from application C code.
The normal way to do the trick you describe is through an in-circuit debugger. Download the program, hit reset, fill the stack with the help of the debugger. There will be some convenient debugger command available to do that. Execute the program, try to use as much of it as possible, observe the stack in the memory map of your debugger.
答案的见解,我试图在Main开始之后绘制堆栈,以确保我不覆盖已经使用的部分。
有了@kkrambo stackpaint
stackcount
With the insights from @kkrambo answer, I tried to paint the stack just after the start of main by taking care that I do not overwrite the portion that has been used already.My stack paint and stack count functions are given below:
StackPaint
StackCount