使用popen时valgrind xml输出格式错误

时间:2016-03-07 16:53:19

标签: c valgrind

当我在下面的程序中运行valgrind时,我在Linux上得到格式错误的XML输出。

#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;
}

我使用以下命令运行valgrind:

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

似乎使用popen会生成格式错误的XML输出。 这是输出a get,在前导中我得到shell命令&#34; echo test&#34;而不是我的程序

<?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>

我可以使用popen和valgrind,是否存在不兼容问题?

作为一种解决方法,我设置了选项--trace-children = no,我没有遇到问题。但是如果我的程序分叉一个新进程,那么Valgrind就不会对它进行分析。

1 个答案:

答案 0 :(得分:0)

损坏的输出来自--trace-children=yes--xml=yes --xml-file=test.xml的组合使用

在这种情况下,程序和popen(“回声测试”)的分析结果都输出到同一文件中,从而导致输出损坏。稍后您还可以在xml文件中看到<status>部分已损坏。

在这种情况下,解决方案是为每个进程输出单独的xml文件,并在输出文件名中使用“%p”模板:例如--xml-file=test_%p.xml

来自http://cs.swan.ac.uk/~csoliver/ok-sat-library/internet_html/doc/doc/Valgrind/3.8.1/html/manual-core.html --log-file--xml-file文档:

%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.