linux-c编程-valgrind调试方法.md

valgrind调试方法

用法:

valgrind [options] prog-and-args 
options: 常用选项,适用于所有Valgrind工具

选择调试器

  • -tool= 最常用的选项。运行valgrind中名为toolname的工具。默认memcheck。

    • memcheck ------> 这是valgrind应用最广泛的工具,一个重量级的内存检查器,能够发现开发中绝大多数内存错误使用情况,比如:使用未初始化的内存,使用已经释放了的内存,内存访问越界等。
    • callgrind ------> 它主要用来检查程序中函数调用过程中出现的问题。
    • cachegrind ------> 它主要用来检查程序中缓存使用出现的问题。
    • helgrind ------> 它主要用来检查多线程程序中出现的竞争问题。
    • massif ------> 它主要用来检查程序中堆栈使用中出现的问题。
    • extension ------> 可以利用core提供的功能,自己编写特定的内存调试工具
  • -h –help 显示帮助信息。
  • -version 显示valgrind内核的版本,每个工具都有各自的版本。
  • -q –quiet 安静地运行,只打印错误信息。
  • -v –verbose 更详细的信息, 增加错误数统计。
  • -trace-children=no|yes 跟踪子线程? [no]
  • -track-fds=no|yes 跟踪打开的文件描述?[no]
  • -time-stamp=no|yes 增加时间戳到LOG信息? [no]
  • -log-fd= 输出LOG到描述符文件 [2=stderr]
  • -log-file= 将输出的信息写入到指定文件中
  • -log-file-exactly= 输出LOG信息到file
  • -log-file-qualifier= 取得环境变量的值来做为输出信息的文件名。 [none]
  • -log-socket=ipaddr:port 输出LOG到socket,ipaddr:port

LOG信息输出

  • -xml=yes 将信息以xml格式输出,只有memcheck可用
  • -num-callers= show callers in stack traces [12]
  • -error-limit=no|yes 如果太多错误,则停止显示新错误 [yes]
  • -error-exitcode= 如果发现错误则返回错误代码 [0=disable]
  • -db-attach=no|yes 当出现错误,valgrind会自动启动调试器gdb。[no]
  • -db-command= 启动调试器的命令行选项[gdb -nw %f %p]

适用于Memcheck工具的相关选项:

  • -leak-check=no|summary|full 要求对leak给出详细信息 [summary]
  • -leak-resolution=low|med|high how much bt merging in leak check [low]
  • -show-reachable=no|yes show reachable blocks in leak check? [no]

最常用的命令格式:

valgrind --tool=memcheck --leak-check=full ./app

cache检测示例

源码参考如下:

gprof_demo.c

#include 
#include 

#define BUFF_SIZE 10000

long table[BUFF_SIZE][BUFF_SIZE];
long col[BUFF_SIZE];
long row[BUFF_SIZE];

void initTable()
{
    int i, j;
    
    for (i = 0; i < BUFF_SIZE; i++)
        for (j = 0; j < BUFF_SIZE; j++)
            table[i][j] = random();
}

void sumCol()
{
    int i, j;
    
    for (j = 0; j < BUFF_SIZE; j++)
        for (i = 0; i < BUFF_SIZE; i++)
            col[j] = table[i][j];
}
void sumRow()
 {
    int i, j;
    
    for (i = 0; i < BUFF_SIZE; i++)
        for (j=0; j < BUFF_SIZE; j++)
            row[i] = table[i][j];
}
void printResult()
{
    int i;
    printf(" RAW\tCol\n");
    
    for (i = 0; i < BUFF_SIZE; i++)
        printf("%8ld\t%8ld\n", row[i], col[i]);
}

int main()
{
    printf("hello\n");
    
    initTable();
    sumRow();
    sumCol();
    printResult();
    
    return 0;
}

使用valgrind运行如下:

$ valgrind --tool=callgrind ./gprof_demo
==56533== Callgrind, a call-graph generating cache profiler
==56533== Copyright (C) 2002-2015, and GNU GPL'd, by Josef Weidendorfer et al.
==56533== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==56533== Command: ./gprof_demo
==56533==
==56533== For interactive control, run 'callgrind_control -h'.
hello
 RAW    Col
1908609430      1202955135
1712047379      1506019591
1864115174      1980735621
1322828066      1544018146
1395062755      1681928442
1721671889      999389080
...
1792209575      408530109
2123570486      2123570486
==56533==
==56533== Events    : Ir
==56533== Collected : 8212408375
==56533==
==56533== I   refs:      8,212,408,375

使用kcachegrind查看调用时间消耗

kcachegrind callgrind.out.52835
callgrind.out.52835替换为具体的生成结果

如图:
gprof.png

如上,某些时候,由于valgrind使用模拟的方式进行程序运行,因此并不能准确的统计性能信息。

内存泄漏检测示例

源码参考如下:

/* gcc mem_check.c -o mem_check -g -O0 */

#include 
#include 

void test(void)
{
    int *buf = malloc(10 * sizeof(int));
    buf[10] = 0x55;
}

int main(void)
{
    test();
    return 0;
}

执行结果如下:

$ valgrind --leak-check=yes ./mem_check
==1984== Memcheck, a memory error detector
==1984== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1984== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==1984== Command: ./mem_check
==1984==
==1984== Invalid write of size 4
==1984==    at 0x1086CE: test (mem_check.c:9)
==1984==    by 0x1086DF: main (mem_check.c:14)
==1984==  Address 0x522d068 is 0 bytes after a block of size 40 alloc'd
==1984==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1984==    by 0x1086C1: test (mem_check.c:8)
==1984==    by 0x1086DF: main (mem_check.c:14)
==1984==
==1984==
==1984== HEAP SUMMARY:
==1984==     in use at exit: 40 bytes in 1 blocks
==1984==   total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==1984==
==1984== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==1984==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1984==    by 0x1086C1: test (mem_check.c:8)
==1984==    by 0x1086DF: main (mem_check.c:14)
==1984==
==1984== LEAK SUMMARY:
==1984==    definitely lost: 40 bytes in 1 blocks
==1984==    indirectly lost: 0 bytes in 0 blocks
==1984==      possibly lost: 0 bytes in 0 blocks
==1984==    still reachable: 0 bytes in 0 blocks
==1984==         suppressed: 0 bytes in 0 blocks
==1984==
==1984== For counts of detected and suppressed errors, rerun with: -v
==1984== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

可以看到valgrind找到两个错误:

  • 第9行代码存在无效的写入数据即越界访问;
  • 内存泄漏,分配了40字节没有释放。

email: [email protected]

你可能感兴趣的:(linux,linux编程,c,调试技巧,调试工具)