Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dump valgrind data

I am using valgrind on a program which runs an infinite loop.

As memcheck displays the memory leaks after the end of the program, but as my program has infinite loop it will never end.

So is there any way i can forcefully dump the data from valgrind time to time.

Thanks

like image 838
user414209 Avatar asked Jun 01 '12 07:06

user414209


People also ask

How do I Count the number of instructions in Valgrind?

Counting instructions with callgrind To profile, you run your program under Valgrind and explicitly request the callgrind tool (if unspecified, the tool defaults to memcheck). valgrind --tool=callgrind program-to-run program-arguments The above command starts up valgrind and runs the program inside of it.

How to make Valgrind use additional suppression files?

Please correct any mistakes you find. When valgrind runs its default tool, Memcheck, it automatically tries to read a file called $PREFIX/lib/valgrind/default.supp ($PREFIX will normally be /usr). However you can make it use additional suppression files of your choice by adding --suppressions=<filename> to your command-line invocation.

How do I use callgrind with Valgrind?

To profile, you run your program under Valgrind and explicitly request the callgrind tool (if unspecified, the tool defaults to memcheck). The above command starts up valgrind and runs the program inside of it.

What is GDB in Valgrind?

The GNU Project Debugger (GDB), is a popular tool for use with C/C++ and other languages. This article explains how to use Valgrind and GDB together along with vgdb, a small program that serves as an intermediary between Valgrind and GDB.


1 Answers

Have a look at the client requests feature of memcheck. You can probably use VALGRIND_DO_LEAK_CHECK or similar.

EDIT:

In response to the statement above that this doesn't work. Here is an example program which loops forever:

#include <valgrind/memcheck.h>
#include <unistd.h>
#include <cstdlib>

int main(int argc, char* argv[])
{

  while(true) {
    char* leaked = new char[1];
    VALGRIND_DO_LEAK_CHECK;
    sleep(1);
  }

  return EXIT_SUCCESS;
}

When I run this in valgrind, I get an endless output of new leaks:

$ valgrind ./a.out
==16082== Memcheck, a memory error detector
==16082== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==16082== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==16082== Command: ./a.out
==16082== 
==16082== LEAK SUMMARY:
==16082==    definitely lost: 0 bytes in 0 blocks
==16082==    indirectly lost: 0 bytes in 0 blocks
==16082==      possibly lost: 0 bytes in 0 blocks
==16082==    still reachable: 1 bytes in 1 blocks
==16082==         suppressed: 0 bytes in 0 blocks
==16082== Reachable blocks (those to which a pointer was found) are not shown.
==16082== To see them, rerun with: --leak-check=full --show-reachable=yes
==16082== 
==16082== 1 bytes in 1 blocks are definitely lost in loss record 2 of 2
==16082==    at 0x4C2BF77: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==16082==    by 0x4007EE: main (testme.cc:9)
==16082== 
==16082== LEAK SUMMARY:
==16082==    definitely lost: 1 bytes in 1 blocks
==16082==    indirectly lost: 0 bytes in 0 blocks
==16082==      possibly lost: 0 bytes in 0 blocks
==16082==    still reachable: 1 bytes in 1 blocks
==16082==         suppressed: 0 bytes in 0 blocks
==16082== Reachable blocks (those to which a pointer was found) are not shown.
==16082== To see them, rerun with: --leak-check=full --show-reachable=yes
==16082== 
==16082== 2 bytes in 2 blocks are definitely lost in loss record 2 of 2
==16082==    at 0x4C2BF77: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==16082==    by 0x4007EE: main (testme.cc:9)
==16082== 
==16082== LEAK SUMMARY:
==16082==    definitely lost: 2 bytes in 2 blocks
==16082==    indirectly lost: 0 bytes in 0 blocks
==16082==      possibly lost: 0 bytes in 0 blocks
==16082==    still reachable: 1 bytes in 1 blocks
==16082==         suppressed: 0 bytes in 0 blocks
==16082== Reachable blocks (those to which a pointer was found) are not shown.
==16082== To see them, rerun with: --leak-check=full --show-reachable=yes

The program does not terminate.

like image 122
acm Avatar answered Dec 14 '22 20:12

acm