Bug 336976

Summary: Invalid XML output when tracing into child processes
Product: [Developer tools] valgrind Reporter: Dave Taflin <DTaflin>
Component: memcheckAssignee: Julian Seward <jseward>
Status: RESOLVED NOT A BUG    
Severity: normal    
Priority: NOR    
Version: 3.9.0   
Target Milestone: ---   
Platform: Compiled Sources   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Dave Taflin 2014-07-02 20:20:54 UTC
I execute the command:

valgrind --leak-check=yes --gen-suppressions=all --xml=yes --xml-file=valgrind.xml --trace-children=yes ./tsh.sh

with file tsh.sh:

#!/bin/sh
./tc

and executable tc built from file tc.cpp:

#include <stdlib.h>
main(int argc, char **argv)
{
    malloc(23);
}

The resulting file valgrind.xml has the following invalid contents. Note in particular the repeated instances of </valgrindoutput>:

<?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.9.0.SVN and LibVEX; rerun with -h for copyright info</line>
  <line>Command: ./tc</line>
</preamble>

<pid>4928</pid>
<ppid>4927</ppid>
<tool>memcheck</tool>

<args>
  <vargv>
    <exe>/usr/local/bin/valgrind</exe>
    <arg>--leak-check=yes</arg>
    <arg>--gen-suppressions=all</arg>
    <arg>--xml=yes</arg>
    <arg>--xml-file=valgrind.xml</arg>
    <arg>--trace-children=yes</arg>
  </vargv>
  <argv>
    <exe>./tc</exe>
  </argv>
</args>

<status>
  <state>RUNNING</state>
  <time>00:00:00:00.125 </time>
</status>


<status>
<status>
  <state>FINISHED</state>
  <time>00:00:00:02.819 </time>
</status>

<errorcounts>
</errorcounts>

<suppcounts>
  <pair>
    <count>2</count>
    <name>Fedora-Core-5-hack3-ld24</name>
  </pair>
</suppcounts>

</valgrindoutput>

t>
    <leakedbytes>23</leakedbytes>
    <leakedblocks>1</leakedblocks>
  </xwhat>
  <stack>
    <frame>
      <ip>0x4A2448E</ip>
      <obj>/usr/local/lib/valgrind/vgpreload_memcheck-amd64-linux.so</obj>
      <fn>malloc</fn>
      <dir>/workgroups/Software-Development/Users/dave/src/valgrind-variant-read-only/valgrind/coregrind/m_replacemalloc</dir>
      <file>vg_replace_malloc.c</file>
      <line>270</line>
    </frame>
    <frame>
      <ip>0x4005C0</ip>
      <obj>/home/dave/tmp/tc</obj>
      <fn>main</fn>
    </frame>
  </stack>
  <suppression>
    <sname>insert_a_suppression_name_here</sname>
    <skind>Memcheck:Leak</skind>
    <sframe> <fun>malloc</fun> </sframe>
    <sframe> <fun>main</fun> </sframe>
    <rawtext>
<![CDATA[
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   fun:malloc
   fun:main
}
]]>
    </rawtext>
  </suppression>
</error>

<errorcounts>
</errorcounts>

<suppcounts>
  <pair>
    <count>2</count>
    <name>Fedora-Core-5-hack3-ld24</name>
  </pair>
</suppcounts>

</valgrindoutput>




Reproducible: Always

Steps to Reproduce:
1. Create files tsh.sh and tc.cpp from the Details above.
2. chmod +x ./tsh.sh; make tc
3. valgrind --leak-check=yes --gen-suppressions=all --xml=yes --xml-file=valgrind.xml --trace-children=yes ./tsh.sh
Actual Results:  
Output file valgrind.xml has invalid XML. It appears that two processes were simultaneously outputting to the same XML file, with one process overwriting part of another process's output.

Expected Results:  
Valid XML output

I tried this same test with valgrind-variant (downloaded from code.google.com) with the same result.
Comment 1 Julian Seward 2014-08-30 09:16:12 UTC
This is not surprising considering that you're asking for the XML for
both parent and children to be dumped into the same file.  Try using
%p in the file name, eg --xml-file=valgrind%p.xml to get output in
different files.  Also it's worth trying --child-silent-after-fork=yes.

Closing.  Please reopen if you are still having problems with this.