简体   繁体   English

我可以使用Valgrind查找静态内存错误吗?

[英]Can I use Valgrind to find static memory errors?

I'm working on an embedded program. 我正在开发嵌入式程序。 I use the avr-gcc tool chain to compile the C source from my MacBook Pro. 我使用avr-gcc工具链从MacBook Pro编译C源代码。 Until recently things have been going pretty well. 直到最近,一切进展顺利。 In my latest development iteration though, I seem to have introduced some sort of intermittent bug that I'm suspecting is some sort of stack or other memory corruption error. 但是,在我最新的开发迭代中,我似乎引入了某种间歇性错误,我怀疑这是某种堆栈或其他内存损坏错误。

I've never used Valgrind, but it seems it gets rave reviews, but most of the references seem to refer to malloc/free types of errors. 我从未使用过Valgrind,但是似乎它得到了好评如潮,但是大多数参考似乎都指向malloc / free类型的错误。 I don't do any malloc'ing. 我不做任何分配。 It's a smallish embedded program, no OS. 这是一个很小的嵌入式程序,没有操作系统。 Can Valgrind help me? 瓦尔格朗德可以帮助我吗? Any pointers on how I would use it to help find static memory mismanagement errors in a cross-compiled scenario would be really helpful! 在交叉编译的情况下,任何有关如何使用它来帮助查找静态内存管理错误的指示都将非常有帮助!

Or is there a different tool or technique I should look at to validate my code's memory management? 还是我应该考虑使用其他工具或技术来验证代码的内存管理?

Yes, valgrind can definitely help you. 是的,valgrind绝对可以为您提供帮助。 In addition to a lot of heap-based analysis (illegal frees, memory leaks, etc.) its memcheck tool detects illegal reads and writes, ie situations when your program accesses memory that it should not access. 除了很多基于堆的分析(非法释放,内存泄漏等)之外,它的memcheck工具还检测非法读写,即程序访问不应访问的内存的情况。 This analysis does not differentiate between static and dynamic memory: it would report accesses outside of a stack frame, accesses beyond bounds of a static array, and so on. 这种分析不能区分静态内存和动态内存:它将报告堆栈帧之外的访问,超出静态数组范围的访问等等。 It also detects access to variables that have not been onitialized previously. 它还会检测对以前尚未初始化的变量的访问。 Both situations are undefined behavior, and can lead to crash. 两种情况都是未定义的行为,并且可能导致崩溃。

Frama-C is a static analysis framework (as opposed to Valgrind which provides dynamic analysis). Frama-C是静态分析框架(与提供动态分析的Valgrind相对)。 It was originally designed with embedded, possibly low-level code in mind. 它最初是在设计时考虑到嵌入式的,可能是底层代码。 Frama-C's “value analysis“ plug-in basically detects all C undefined behaviors that you may want to know about in embedded code (including accessing an invalid pointer). Frama-C的“值分析”插件基本上可以检测您可能想在嵌入式代码中了解的所有C未定义行为(包括访问无效指针)。

Since it is a static analyzer, it does not execute the code(*) and is thus ideal in a cross-compiled context. 由于它是静态分析器,因此它不执行code(*),因此在交叉编译的环境中非常理想。 Look for option -machdep . 寻找选项-machdep Values for this option include x86_64 x86_32 ppc_32 x86_16. 此选项的值包括x86_64 x86_32 ppc_32 x86_16。

Disclaimer: I am one of the contributors of Frama-C's “value analysis” plug-in. 免责声明:我是Frama-C的“价值分析”插件的贡献者之一。

(*) though if you provide all inputs and set precision on maximum, it can interpret the source code as precisely as any cross-compilation+execution would . (*)尽管您提供所有输入并设置最大精度,但是它可以像任何交叉编译+执行一样精确地解释源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM