说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C3-C_debug/valgrind.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
3.3. Debugging Memory with Valgrind
Valgrind 的 Memcheck 调试工具能指出程序中的堆内存错误。在 C 程序中,堆内存是运行中程序的内存里由 malloc() 调用动态分配、由 free() 调用释放的那一部分。Valgrind 能发现的内存错误类型包括:
Valgrind’s Memcheck debugging tool highlights heap memory errors in programs. Heap memory is the part of a running program’s memory that is dynamically allocated by calls to
malloc()and freed by calls tofree()in C programs. The types of memory errors that Valgrind finds include:
从未初始化的内存中读取(获取)值。例如:
1
2
3int *ptr, x;
ptr = malloc(sizeof(int) * 10);
x = ptr[3]; // reading from uninitialized memory
Reading (getting) a value from uninitialized memory. For example:
1
2
3 int *ptr, x;
ptr = malloc(sizeof(int) * 10);
x = ptr[3]; // reading from uninitialized memory
在未分配的内存位置读取(获取)或写入(设置)值,这通常意味着数组越界错误。例如:
1
2ptr[11] = 100; // writing to unallocated memory (no 11th element)
x = ptr[11]; // reading from unallocated memory
Reading (getting) or writing (setting) a value at an unallocated memory location, which often indicates an array out-of-bounds error. For example:
1
2 ptr[11] = 100; // writing to unallocated memory (no 11th element)
x = ptr[11]; // reading from unallocated memory
释放已经释放过的内存。例如:
1
2free(ptr);
free(ptr); // freeing the same pointer a second time
Freeing already freed memory. For example:
1
2 free(ptr);
free(ptr); // freeing the same pointer a second time
内存泄漏。内存泄漏(memory leak)是一块已分配的堆内存空间,程序中已经没有任何指针变量引用它,因此它再也无法被释放。也就是说,当程序丢失了一块已分配堆空间的地址时,就发生了内存泄漏。例如:
1
2ptr = malloc(sizeof(int) * 10);
ptr = malloc(sizeof(int) * 5); // memory leak of first malloc of 10 ints
Memory leaks. A memory leak is a chunk of allocated heap memory space that is not referred to by any pointer variable in the program, and thus it cannot be freed. That is, a memory leak occurs when a program loses the address of an allocated chunk of heap space. For example:
1
2 ptr = malloc(sizeof(int) * 10);
ptr = malloc(sizeof(int) * 5); // memory leak of first malloc of 10 ints
内存泄漏最终可能导致程序耗尽堆内存空间,使之后的 malloc() 调用失败。其他类型的内存访问错误,例如无效读和无效写,可能导致程序崩溃,也可能让程序内存中的某些内容以看似莫名其妙的方式被修改。
Memory leaks can eventually cause the program to run out of heap memory space, resulting in subsequent calls to
malloc()failing. The other types of memory access errors, such as invalid reads and writes, can lead to the program crashing or can result in some program memory contents being modified in seemingly mysterious ways.
内存访问错误是程序中最难发现的缺陷之一。内存访问错误往往不会立即在程序执行中造成明显的错误,而是可能引发一个在执行后期才出现的错误,而且出错的地方常常位于看上去与错误根源毫不相干的程序部分。另一些时候,含有内存访问错误的程序可能在某些输入下运行正确,在另一些输入下崩溃,这使得错误的原因很难找到和修复。
Memory access errors are some of the most difficult bugs to find in programs. Often a memory access error does not immediately result in a noticeable error in the program’s execution. Instead, it may trigger an error that occurs later in the execution, often in a part of the program that seemingly has little to do with the source of the error. At other times, a program with a memory access error may run correctly on some inputs and crash on other inputs, making the cause of the error difficult to find and fix.
使用 Valgrind 可以帮助程序员识别这些难以发现和修复的堆内存访问错误,节省大量调试时间和精力。Valgrind 还能帮助程序员识别出那些在测试和调试代码时没有被发现的、潜伏着的堆内存错误。
Using Valgrind helps a programmer identify these difficult to find and fix heap memory access errors, saving significant amounts of debugging time and effort. Valgrind also assists the programmer in identifying any lurking heap memory errors that were not discovered in the testing and debugging of their code.
3.3.1. 一个含有堆内存访问错误的示例程序
3.3.1. An Example Program with a Heap Memory Access Error
为了说明含有内存访问错误的程序有多难发现和修复,来看下面这个小程序(bigfish.c)。这个程序在第二个 for 循环中给 bigfish 数组边界之外的位置赋值,表现出一个「写入未分配的堆内存」错误(注意:清单中带有源代码行号,print_array() 函数的定义没有给出,但它的行为如描述所示):
As an example of how difficult it can be to discover and fix programs with memory access errors, consider the following small program (bigfish.c). This program exhibits a “write to unallocated heap memory” error in the second
forloop, when it assigns values beyond the bounds of thebigfisharray (note: the listing includes source code line numbers, and theprint_array()function definition is not shown, but it behaves as described):
1 | 1 #include <stdio.h> |
在 main() 函数中,第二个 for 循环写入了 bigfish 数组边界之外的三个下标位置(下标 10、11 和 12),造成了堆内存访问错误。程序并没有在错误发生的地方(执行第二个 for 循环时)崩溃,而是在执行后期调用 free(littlefish) 时才崩溃:
In the
main()function, the secondforloop causes a heap memory access error when it writes to three indices beyond the bounds of thebigfisharray (to indices 10, 11, and 12). The program does not crash at the point where the error occurs (at the execution of the secondforloop); instead, it crashes later in its execution at the call tofree(littlefish):
1 | bigfish: |
在 GDB 中运行这个程序会发现,程序在调用 free(littlefish) 时因段错误而崩溃。在这个位置崩溃,可能会让程序员怀疑对 littlefish 数组的访问有缺陷。然而,错误的原因是对 bigfish 数组的写入,与程序访问 littlefish 数组的方式毫无关系。
Running this program in GDB indicates that the program crashes with a segfault at the call to
free(littlefish). Crashing at this point may make the programmer suspect that there is a bug with accesses to thelittlefisharray. However, the cause of the error is due to writes to thebigfisharray and has nothing to do with errors in how the program accesses thelittlefisharray.
程序崩溃最可能的原因是:for 循环越过了 bigfish 数组的边界,覆盖了 bigfish 最后一个已分配元素与 littlefish 第一个已分配元素之间的那段堆内存。两者之间(紧挨在 littlefish 第一个元素之前)的堆内存位置被 malloc() 用来存放为 littlefish 数组分配的堆内存的元数据。free() 函数在内部用这些元数据来确定要释放多少堆内存。对 bigfish 下标 10 和 11 的修改覆盖了这些元数据,导致程序在调用 free(littlefish) 时崩溃。不过我们要指出,并非所有 malloc() 函数的实现都采用这种策略。
The most likely reason that the program crashes is that the
forloop goes beyond the bounds of thebigfisharray and overwrites memory between the heap memory location of the last allocated element ofbigfishand the first allocated element oflittlefish. The heap memory locations between the two (and right before the first element oflittlefish) are used bymalloc()to store meta-data about the heap memory allocated for thelittlefisharray. Internally, thefree()function uses this meta-data to determine how much heap memory to free. The modifications to indices10and11ofbigfishoverwrite these meta-data values, resulting in the program crash on the call tofree(littlefish). We note, however, that not all implementations of themalloc()function use this strategy.
由于程序在对 bigfish 进行错误的内存访问之后打印出了 littlefish,错误的原因对程序员来说可能会更明显一些:第二个 for 循环不知怎么地修改了 littlefish 数组的内容(循环之后,它第 0 个元素的值「莫名其妙地」从 0 变成了 78)。然而,即使在这个非常小的程序中,要找到真正的错误也可能很困难:如果程序在含有内存访问错误的第二个 for 循环之后没有打印 littlefish,或者 for 循环的上界是 12 而不是 13,程序变量的值就不会出现可见的、莫名其妙的变化,也就没有线索帮助程序员看出程序访问 bigfish 数组的方式有问题。
Because the program includes code to print out
littlefishafter the memory access error tobigfish, the cause of the error may be more obvious to the programmer: the secondforloop is somehow modifying the contents of thelittlefisharray (its element 0 value “mysteriously” changes from0to78after the loop). However, even in this very small program, it may be difficult to find the real error: if the program didn’t print outlittlefishafter the secondforloop with the memory access error, or if theforloop upper bound was12instead of13, there would be no visible mysterious change to program variable values that could help a programmer see that there is an error with how the program accesses thebigfisharray.
在更大的程序中,这类内存访问错误可能位于与崩溃之处截然不同的代码部分。被破坏的堆内存所对应的访问变量,与错误覆盖这块内存时所用的变量之间,可能也没有任何逻辑上的关联;它们唯一的关联只是恰好指向在堆中彼此相邻分配的内存地址。注意,这种情况可能因程序的每次运行而异,而且这类行为往往对程序员是隐藏的。同样,有时错误的内存访问对某次程序运行没有任何明显影响,这让这些错误很难被发现。只要一个程序对某些输入看起来运行正常、对另一些输入却会崩溃,这就是程序中存在内存访问错误的迹象。
In larger programs, a memory access error of this type could be in a very different part of the program code than the part that crashes. There also may be no logical association between variables used to access heap memory that has been corrupted and the variables that were used to erroneously overwrite that same memory; instead, their only association is that they happen to refer to memory addresses that are allocated close together in the heap. Note that this situation can vary from run to run of a program and that such behavior is often hidden from the programmer. Similarly, sometimes bad memory accesses will have no noticeable effect on a run of the program, making these errors hard to discover. Whenever a program seems to run fine for some input, but crashes on other input, this is a sign of a memory access error in the program.
像 Valgrind 这样的工具,能迅速把程序员引向代码中堆内存访问错误的根源和类型,节省好几天的调试时间。在前面的程序中,Valgrind 会指出错误发生的位置(即程序访问 bigfish 数组边界之外元素的地方)。Valgrind 的错误消息包括错误类型、程序中发生错误的位置,以及错误访问附近的那块堆内存是在程序中什么地方分配的。例如,下面是程序执行第 27 行时 Valgrind 显示的信息(省略了实际 Valgrind 错误消息中的部分细节):
Tools like Valgrind can save days of debugging time by quickly pointing programmers to the source and type of heap memory access errors in their code. In the previous program, Valgrind delineates the point where the error occurs (when the program accesses elements beyond the bounds of the
bigfisharray). The Valgrind error message includes the type of error, the point in the program where the error occurs, and where in the program the heap memory near the bad memory access was allocated. For example, here is the information Valgrind will display when the program executes line 27 (some details from the actual Valgrind error message are omitted):
1 | Invalid write |
这条 Valgrind 错误消息说明:程序在第 27 行向无效的(未分配的)堆内存写入,并且这块无效内存紧跟在第 11 行分配的一块内存之后,这表明循环访问了 bigfish 所指向的堆空间中已分配内存边界之外的某些元素。修复这个缺陷的办法,要么是增加传给 malloc() 的字节数,要么是修改第二个 for 循环的边界,避免写到已分配堆内存空间的边界之外。
This Valgrind error message says that the program is writing to invalid (unallocated) heap memory at line 27 and that this invalid memory is located immediately after a block of memory that was allocated at line 11, indicating that the loop is accessing some elements beyond the bounds of the allocated memory in heap space to which
bigfishpoints. A potential fix to this bug is to either increase the number of bytes passed tomalloc()or change the secondforloop bounds to avoid writing beyond the bounds of the allocated heap memory space.
除了发现堆内存中的内存访问错误之外,Valgrind 还能发现一些栈内存访问错误,比如使用未初始化的局部变量,或试图访问超出当前栈边界的栈内存位置。不过,Valgrind 检测栈内存访问错误的粒度不如堆内存那样细,而且它不检测全局数据内存的访问错误。
In addition to finding memory access errors in heap memory, Valgrind can also find some errors with stack memory accesses, such as using uninitialized local variables or trying to access stack memory locations that are beyond the bounds of the current stack. However, Valgrind does not detect stack memory access errors at the same granularity as it does with heap memory, and it does not detect memory access errors with global data memory.
程序中可能存在 Valgrind 发现不了的栈内存和全局内存访问错误。不过,这些错误导致的程序错误行为或崩溃,与堆内存访问错误可能导致的行为类似。例如,覆盖栈上某个静态声明数组边界之外的内存位置,可能会「莫名其妙地」改变其他局部变量的值,也可能覆盖栈上保存的、用于从函数调用返回的状态,导致函数返回时崩溃。用 Valgrind 排查堆内存错误的经验,能帮助程序员识别和修复访问栈内存和全局内存时的类似错误。
A program can have memory access errors with stack and global memory that Valgrind cannot find. However, these errors result in erroneous program behavior or program crashing that is similar to the behavior that can occur with heap memory access errors. For example, overwriting memory locations beyond the bounds of a statically declared array on the stack may result in “mysteriously” changing the values of other local variables or may overwrite state saved on the stack that is used for returning from a function call, leading to a crash when the function returns. Experience using Valgrind for heap memory errors can help a programmer identify and fix similar errors with accesses to stack and global memory.
3.3.2. 如何使用 Memcheck
3.3.2. How to Use Memcheck
我们用一个示例程序 valgrindbadprog.c 来演示 Valgrind 的 Memcheck 内存分析工具的一些主要功能,这个程序含有若干错误的内存访问(代码中的注释说明了错误的类型)。Valgrind 默认运行 Memcheck 工具,下面的代码片段依赖的就是这一默认行为。你也可以用 --tool=memcheck 选项显式指定 Memcheck 工具。在后面的章节中,我们会用 --tool 选项调用 Valgrind 的其他剖析工具。
We illustrate some of the main features of Valgrind’s Memcheck memory analysis tool on an example program, valgrindbadprog.c, which contains several bad memory access errors (comments in the code describe the type of error). Valgrind runs the Memcheck tool by default; we depend on this default behavior in the code snippets that follow. You can explicitly specify the Memcheck tool by using the
--tool=memcheckoption. In later sections, we will invoke other Valgrind profiling tools by invoking the--tooloption.
要运行 Memcheck,首先用 -g 标志编译 valgrindbadprog.c 程序,为可执行文件(例如 a.out)加入调试信息。然后用 valgrind 运行这个可执行文件。注意,对于非交互式程序,把 Valgrind 的输出重定向到一个文件中、等程序退出后再查看,可能会有帮助:
To run Memcheck, first compile the
valgrindbadprog.cprogram with the-gflag to add debugging information to the executable (e.g.,a.out) file. Then, run the executable withvalgrind. Note that for non-interactive programs, it may be helpful to redirect Valgrind’s output to a file for viewing after the program exits:
1 | $ gcc -g valgrindbadprog.c |
Valgrind 的 Memcheck 工具会在程序执行过程中随时打印出发生的内存访问错误和警告。在程序执行结束时,Memcheck 还会打印程序中所有内存泄漏的摘要。尽管修复内存泄漏很重要,但其他类型的内存访问错误对程序的正确性要关键得多。因此,除非内存泄漏导致程序耗尽堆内存空间而崩溃,程序员应当先集中精力修复其他类型的内存访问错误,再考虑内存泄漏。要查看每个内存泄漏的详细信息,使用 --leak-check=yes 选项。
Valgrind’s Memcheck tool prints out memory access errors and warnings as they occur during the program’s execution. At the end of the program’s execution, Memcheck also prints out a summary about any memory leaks in the program. Even though memory leaks are important to fix, the other types of memory access errors are much more critical to a program’s correctness. As a result, unless memory leaks are causing a program to run out of heap memory space and crash, a programmer should focus first on fixing these other types of memory access errors before considering memory leaks. To view details of individual memory leaks, use the
--leak-check=yesoption.
初次使用 Valgrind 时,它的输出可能显得有点难以解读。不过,所有输出都遵循相同的基本格式,一旦了解了这种格式,就更容易理解 Valgrind 显示的关于堆内存访问错误和警告的信息了。下面是运行 valgrindbadprog.c 程序时 Valgrind 给出的一条错误示例:
When first using Valgrind, its output may seem a bit difficult to parse. However, the output all follows the same basic format, and once you know this format, it’s easier to understand the information that Valgrind is displaying about heap memory access errors and warnings. Here is an example Valgrind error from a run of the
valgrindbadprog.cprogram:
1 | ==31059== Invalid write of size 1 |
Valgrind 输出的每一行都以进程的 ID(PID)编号作为前缀(本例中是 31059):
Each line of Valgrind output is prefixed with the process’s ID (PID) number (31059 in this example):
1 | ==31059== |
大多数 Valgrind 错误和警告都具有以下格式:
Most Valgrind errors and warnings have the following format:
- 错误或警告的类型。
- The type of error or warning.
- 错误发生的位置(错误发生时程序执行点上的栈回溯)。
- Where the error occurred (a stack trace at the point in the program’s execution when the error occurs.)
- 错误附近的堆内存是在哪里分配的(通常是与该错误相关的那次内存分配)。
- Where heap memory around the error was allocated (usually the memory allocation related to the error.)
在前面的示例错误中,第一行表明发生了一次无效的内存写入(写入堆中未分配的内存——一个非常严重的错误!):
In the preceding example error, the first line indicates an invalid write to memory (writing to unallocated memory in the heap — a very bad error!):
1 | ==31059== Invalid write of size 1 |
接下来几行给出了错误发生时的栈回溯。它们表明,无效写入发生在函数 foo() 的第 29 行,而 foo() 是在 main() 函数的第 56 行被调用的:
The next few lines show the stack trace where the error occurred. These indicate an invalid write occurred at line 29 in function
foo(), which was called from functionmain()at line 56:
1 | ==31059== Invalid write of size 1 |
其余几行指出了无效写入附近的堆空间是在程序中的什么地方分配的。Valgrind 输出的这一部分说明,无效写入的位置紧跟在(0 bytes after)一块 5 字节的堆内存空间之后,这块空间是由函数 foo() 第 18 行的 malloc() 调用分配的,而 foo() 是由 main() 在第 56 行调用的:
The remaining lines indicate where the heap space near the invalid write was allocated in the program. This section of Valgrind’s output says that the invalid write was immediately after (
0 bytes after) a block of 5 bytes of heap memory space that was allocated by a call tomalloc()at line 18 in functionfoo(), called bymain()at line 56:
1 | ==31059== Address 0x52045c5 is 0 bytes after a block of size 5 alloc'd |
这条错误给出的信息表明,程序中存在写入未分配堆内存的错误,并把用户引向程序中发生错误的具体位置(第 29 行)以及错误附近的内存被分配的位置(第 18 行)。查看程序中的这些位置,程序员也许就能看出错误的原因和修复方法:
The information from this error identifies that there is an unallocated heap memory write error in the program, and it directs the user to specific parts of the program where the error occurs (line 29) and where memory around the error was allocated (line 18). By looking at these points in the program, the programmer may see the cause of and the fix for the error:
1 | 18 c = (char *)malloc(sizeof(char) * 5); |
原因在于 for 循环多执行了一次,访问了 c[5],这已经超出了数组 c 的末尾。修复方法是要么修改第 29 行的循环边界,要么在第 18 行分配一个更大的数组。
The cause is that the
forloop executes one time too many, accessingc[5], which is beyond the end of arrayc. The fix is to either change the loop bounds at line 29 or to allocate a larger array at line 18.
译注
循环条件 i <= 5 写在第 28 行,所以准确地说要修改的是第 28 行(第 29 行是循环体)。
如果查看 Valgrind 错误附近的代码还不足以让程序员理解或修复错误,使用 GDB 可能会有帮助。在与 Valgrind 错误相关的代码位置附近设置断点,可以帮助程序员检查程序的运行时状态,理解 Valgrind 错误的原因。例如,在第 29 行设置断点并打印 i 和 str 的值,程序员就能看到 i 为 5 时发生的数组越界错误。在这种情况下,结合使用 Valgrind 和 GDB,可以帮助程序员确定如何修复 Valgrind 发现的内存访问缺陷。
If examining the code around a Valgrind error is not sufficient for a programmer to understand or fix the error, using GDB might be helpful. Setting breakpoints around the points in the code associated with the Valgrind errors can help a programmer evaluate the program’s runtime state and understand the cause of the Valgrind error. For example, by putting a breakpoint at line 29 and printing the values of
iandstr, the programmer can see the array out-of-bounds error wheniis 5. In this case, the combination of using Valgrind and GDB helps the programmer determine how to fix the memory access bugs that Valgrind finds.
尽管本章重点介绍的是 Valgrind 默认的 Memcheck 工具,本书后面还会介绍 Valgrind 的其他一些功能,包括 Cachegrind 缓存剖析工具(第 11 章)、Callgrind 代码剖析工具(第 12 章),以及 Massif 内存剖析工具(第 12 章)。关于使用 Valgrind 的更多信息,请参见 Valgrind 主页及其在线手册。
Although this chapter has focused on Valgrind’s default Memcheck tool, we characterize some of Valgrind’s other capabilities later in the book, including the Cachegrind cache profiling tool (Chapter 11), the Callgrind code profiling tool (Chapter 12), and the Massif memory profiling tool (Chapter 12). For more information about using Valgrind, see the Valgrind homepage, and its online manual.