olivier
olivier

Reputation: 51

valgrind xml output malformed when using popen

When I run valgrind on my program below I get a malformed XML output on Linux.

#include <stdio.h>

int main(int argc, char ** argv)
{
   FILE *pf = popen("echo test","r");
   if( pf != NULL )
   {
       char data[512];
       while (fgets(data,512,pf) != NULL)
       {
           printf("%s\n",data);
       }
       pclose(pf);
   }
   return 0;
}

I run valgrind with the command below:

valgrind --tool=memcheck --error-limit=no --gen-suppressions=no --num-callers=50 --leak-check=full --trace-children=yes --xml=yes --xml-file=test.xml ./myProgam

it seems that the use of popen generates a malformed XML output. Here is the output a get, in the preambule I get the shell command "echo test" instead of my program

<?xml version="1.0"?>

<valgrindoutput>

<protocolversion>4</protocolversion>
<protocoltool>memcheck</protocoltool>

<preamble>
  <line>Memcheck, a memory error detector</line>
  <line>Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.</line>
  <line>Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info</line>
  <line>Command: /bin/sh -c echo test</line>
</preamble>

<pid>39938</pid>
<ppid>39877</ppid>
<tool>memcheck</tool>

<args>
  <vargv>
    <exe>/usr/bin/valgrind</exe>
    <arg>--tool=memcheck</arg>
    <arg>--error-limit=no</arg>
    <arg>--gen-suppressions=no</arg>
    <arg>--num-callers=50</arg>
    <arg>--leak-check=full</arg>
    <arg>--trace-children=yes</arg>
    <arg>--xml=yes</arg>
    <arg>--xml-file=test.xml</arg>
  </vargv>
  <argv>
    <exe>/bin/sh</exe>
    <arg>-c</arg>
    <arg>echo test</arg>
  </argv>
</args>

<status>
  <state>RUN
<status>
  <state>FINISHED</state>
  <time>00:00:00:00.805 </time>
</status>

<errorcounts>
</errorcounts>

<suppcounts>
  <pair>
    <count>4</count>
    <name>U1004-ARM-_dl_relocate_object</name>
  </pair>
  <pair>
    <count>2</count>
    <name>glibc-2.5.x-on-SUSE-10.2-(PPC)-2a</name>
  </pair>
</suppcounts>

</valgrindoutput>

)-2a</name>
  </pair>
</suppcounts>

</valgrindoutput>

Can I use popen and valgrind, is there an incompatibility issue ?

As a workaround I have set the option --trace-children=no and I don't have the issue. But If my program fork a new process it will be not analyzed by Valgrind.

Upvotes: 1

Views: 766

Answers (1)

Martin
Martin

Reputation: 506

The corrupt output comes from the combined use of --trace-children=yes and --xml=yes --xml-file=test.xml

In this case the analysis result of your program and of your popen("echo test") are both output into the same file, resulting in a corrupt output. You can also see later in the xml file that the <status> section is corrupt.

The solution in this case is to output separate xml files for each process with the "%p" template in the output filename: --xml-file=test_%p.xml for example.

From http://cs.swan.ac.uk/~csoliver/ok-sat-library/internet_html/doc/doc/Valgrind/3.8.1/html/manual-core.html --log-file and --xml-file documentation:

%p is replaced with the current process ID. This is very useful for program that
invoke multiple processes. WARNING: If you use --trace-children=yes and your
program invokes multiple processes OR your program forks without calling exec
afterwards, and you don't use this specifier (or the %q specifier below), the
Valgrind output from all those processes will go into one file, possibly jumbled
up, and possibly incomplete.

Upvotes: 0

Related Questions