• valgrind--help手册字段及例子


    Valgrind 使用

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

    1. -tool=<name> 最常用的选项。运行 valgrind中名为toolname的工具。默认memcheck。
    2. h –help 显示帮助信息。
    3. -version 显示 valgrind内核的版本,每个工具都有各自的版本。
    4. q –quiet 安静地运行,只打印错误信息。
    5. v –verbose 更详细的信息, 增加错误数统计。
    6. -trace-children=no|yes 跟踪子线程? [no]
    7. -track-fds=no|yes 跟踪打开的文件描述?[no]
    8. -time-stamp=no|yes 增加时间戳到LOG信息? [no]
    9. -log-fd=<number> 输出LOG到描述符文件 [2=stderr]
    10. -log-file=<file> 将输出的信息写入到filename.PID的文件里,PID是运行程序的进行ID
    11. -log-file-exactly=<file> 输出LOG信息到 file
    12. -log-file-qualifier=<VAR> 取得环境变量的值来做为输出信息的文件名。 [none]
    13. -log-socket=ipaddr:port 输出LOG到socket ,ipaddr:port

    LOG信息输出

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

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

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

    Valgrind 使用举例

    下面是一段有问题的C程序代码test.c

    #include <stdlib.h>
    void f(void)
    {
    int* x = malloc(10 * sizeof(int));
    x[10] = 0; //问题1: 数组下标越界
    } //问题2: 内存没有释放
    int main(void)
    {
    f();
    return 0;
    }
    1、 编译程序test.c
    gcc -Wall test.c -g -o test
    2、 使用Valgrind检查程序BUG
    valgrind --tool=memcheck --leak-check=full ./test
    3、 分析输出的调试信息
    ==3908== Memcheck, a memory error detector.
    ==3908== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
    ==3908== Using LibVEX rev 1732, a library for dynamic binary translation.
    ==3908== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
    ==3908== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
    ==3908== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
    ==3908== For more details, rerun with: -v
    ==3908==
    --3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50
    --3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50
    /*数组越界错误*/
    ==3908== Invalid write of size 4
    ==3908== at 0x8048384: f (test.c:6)
    ==3908== by 0x80483AC: main (test.c:11)
    ==3908== Address 0x400C050 is 0 bytes after a block of size 40 alloc'd
    ==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)
    ==3908== by 0x8048377: f (test.c:5)
    ==3908== by 0x80483AC: main (test.c:11)
    ==3908==
    ==3908== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 14 from 1)
    ==3908== malloc/free: in use at exit: 40 bytes in 1 blocks.
    ==3908== malloc/free: 1 allocs, 0 frees, 40 bytes allocated.
    ==3908== For counts of detected errors, rerun with: -v
    ==3908== searching for pointers to 1 not-freed blocks.
    ==3908== checked 59,124 bytes.
    ==3908==
    ==3908==
    /*有内存空间没有释放*/
    ==3908== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
    ==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)
    ==3908== by 0x8048377: f (test.c:5)
    ==3908== by 0x80483AC: main (test.c:11)
    ==3908==
    ==3908== LEAK SUMMARY:
    ==3908== definitely lost: 40 bytes in 1 blocks.
    ==3908== possibly lost: 0 bytes in 0 blocks.
    ==3908== still reachable: 0 bytes in 0 blocks.
    ==3908== suppressed: 0 bytes in 0 blocks.
    有时候,不小心知道了一些事,才发现自己所在乎的事是那么可笑。
  • 相关阅读:
    冲刺计划安排
    团队作业7——Alpha冲刺之事后诸葛亮
    团队作业6--展示博客(Alpha版本)
    团队作业5——测试与发布(Alpha版本)
    团队项目汇总
    【Java】五种常见排序之------------------快速排序
    【Java】五种常见排序之----------------归并排序
    【Java】五种常见排序之----------------------插入排序
    【Java】五种常见排序之---------选择排序
    【Java】五种常见排序之-----------冒泡排序
  • 原文地址:https://www.cnblogs.com/axjlxy/p/15200483.html
Copyright © 2020-2023  润新知