11.2. 使用 memstomp
要在您要分析的程序中运行 memstomp 工具:
$ scl enable devtoolset-11 'memstomp program argument...'
要在检测到问题时立即终止分析的程序,请使用 - kill (或用于短)命令行选项运行该工具:
$ scl enable devtoolset-11 'memstomp --kill program argument...'
当您分析多线程程序时,尤其建议使用- kill 选项; backtraces 的内部实现不是线程安全,并在没有此命令行选项的情况下在多线程程序上运行 memstomp 工具。
另外,如果您使用调试信息编译分析的程序,或者可以使用这个调试信息,您可以使用 --debug-info (或 )命令行选项生成更详细的回溯追踪:
$ scl enable devtoolset-11 'memstomp --debug-info program argument...'
有关如何使用二进制文件中构建的调试信息编译程序的详细信息,请参阅 第 8.2 节 “准备用于调试的程序”。有关如何为任何 Red Hat Developer Toolset 软件包安装调试信息的详情,请参考 第 1.5.4 节 “安装调试信息”。
请注意,您可以使用 scl 工具执行任何命令,从而导致它使用等效的 Red Hat Developer Toolset 二进制文件来运行它。这可让您默认使用 Red Hat Developer Toolset memstomp 运行 shell 会话:
$ scl enable devtoolset-11 'bash'
例 11.1. 使用 memstomp
在当前工作目录中,创建名为 employee.c 的源文件,其内容如下:
#include <stdio.h>
#include <string.h>
#define BUFSIZE 80
int main(int argc, char *argv[]) {
char employee[BUFSIZE] = "John,Doe,john@example.com";
char name[BUFSIZE] = {0};
char surname[BUFSIZE] = {0};
char *email;
size_t length;
/* Extract the information: */
memccpy(name, employee, ',', BUFSIZE);
length = strlen(name);
memccpy(surname, employee + length, ',', BUFSIZE);
length += strlen(surname);
email = employee + length;
/* Compose the new entry: */
strcat(employee, surname);
strcpy(employee, name);
strcat(employee, email);
/* Print the result: */
puts(employee);
return 0;
}
将该程序编译成一个名为 employee 的二进制文件:
$ scl enable devtoolset-11 'gcc -rdynamic -g -o employee employee.c'
识别带有重叠内存区域的错误函数调用:
$ scl enable devtoolset-11 'memstomp --debug-info ./employee'
memstomp: 0.1.4 successfully initialized for process employee (pid 14887).
strcat(dest=0x7fff13afc265, src=0x7fff13afc269, bytes=21) overlap for employee(14887)
??:0 strcpy()
??:0 strcpy()
??:0 _Exit()
??:0 strcat()
employee.c:26 main()
??:0 __libc_start_main()
??:0 _start()
John,john@example.comDoe,