内存错误搜索工具 (discover) 是用于检测程序中内存访问错误的高级开发工具。使用 –g 选项编译二进制文件使 discover 可以在报告错误和警告时显示源代码和行号信息。
discover 实用程序的用法很简单。在使用 –g 选项编译二进制文件后,您可以对二进制文件运行 discover 命令已对其进行检测。然后运行经检测的二进制文件,以创建 discover 报告。您可以请求 HTML 格式、文本格式或者这两种格式的 discover 报告。该报告显示内存错误、警告和内存泄漏,您可以显示每个错误或警告所对应的源代码和堆栈跟踪。
以下示例来自 discover(1) 手册页,该示例说明了如何准备、检测及运行可执行文件,以生成一份 discover 报告,用于检测内存访问错误。discover 命令行上的 –w 选项指示报告应采用文本格式,–o 选项则指示应输出到屏幕上。
% cc -g -O2 test.c -o test.prep % discover -w - -o test.disc test.prep % ./test.disc ERROR (UMR): accessing uninitialized data from address 0x5000c (4 bytes) at: foo() + 0xdc <ui.c:6> 3: int *t; 4: foo() { 5: t = malloc(5*sizeof(int)); 6:=> printf("%d0, t[1]); 7: } 8: 9: main() main() + 0x1c _start() + 0x108 block at 0x50008 (20 bytes long) was allocated at: malloc() + 0x260 foo() + 0x24 <ui.c:5> 2: 3: int *t; 4: foo() { 5:=> t = malloc(5*sizeof(int)); 6: printf("%d0, t[1]); 7: } 8: main() + 0x1c _start() + 0x108 ***************** Discover Memory Report ***************** 1 block at 1 location left allocated on heap with a total size of 20 bytes 1 block with total size of 20 bytes malloc() + 0x260 foo() + 0x24 <ui.c:5> 2: 3: int *t; 4: foo() { 5:=> t = malloc(5*sizeof(int)); 6: printf("%d0, t[1]); 7: } 8: main() + 0x1c _start() + 0x108
有关更多信息,请参见 discover(1) 手册页和Oracle Solaris Studio 12.4:Discover 和 Uncover 用户指南 。