linuxc编程valgrind调试方法md

9次阅读

共计 4175 个字符,预计需要花费 11 分钟才能阅读完成。

valgrind 调试办法

用法:

valgrind [options] prog-and-args 

options: 罕用选项,实用于所有 Valgrind 工具

抉择调试器

  • -tool=<name> 最罕用的选项。运行 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=<number> 输入 LOG 到描述符文件 [2=stderr]
  • -log-file=<file> 将输入的信息写入到指定文件中
  • -log-file-exactly=<file> 输入 LOG 信息到 file
  • -log-file-qualifier=<VAR> 获得环境变量的值来做为输入信息的文件名。[none]
  • -log-socket=ipaddr:port 输入 LOG 到 socket,ipaddr:port

LOG 信息输入

  • -xml=yes 将信息以 xml 格局输入,只有 memcheck 可用
  • -num-callers=<number> show <number> callers in stack traces [12]
  • -error-limit=no|yes 如果太多谬误,则进行显示新谬误 [yes]
  • -error-exitcode=<number> 如果发现错误则返回错误代码 [0=disable]
  • -db-attach=no|yes 当呈现谬误,valgrind 会主动启动调试器 gdb。[no]
  • -db-command=<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 <stdio.h>
#include <stdlib.h>

#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 替换为具体的生成后果

如图:

如上,某些时候,因为 valgrind 应用模仿的形式进行程序运行,因而并不能精确的统计性能信息。

内存透露检测示例

源码参考如下:

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

#include <stdio.h>
#include <stdlib.h>

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: MingruiZhou@outlook.com

正文完
 0