【问题标题】:valgrind xml output malformed when using popen使用popen时valgrind xml输出格式错误
【发布时间】:2016-06-21 08:10:13
【问题描述】:

当我在下面的程序上运行 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 输出。 这是一个get的输出,在序言中我得到了shell命令“echo test”而不是我的程序

<?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 并且我没有遇到此问题。但是如果我的程序 fork 一个新进程,Valgrind 将不会对其进行分析。

【问题讨论】:

  • popen() 输出什么?你没有发。您也没有检查来自fgets() 的返回值。
  • 我为popen()fgets() 添加了一个返回值检查。 echo test 在标准输出上显示 test

标签: c valgrind


【解决方案1】:

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

在这种情况下,您的程序和您的 popen("echo test") 的分析结果都输出到同一个文件中,导致输出损坏。您还可以稍后在 xml 文件中看到 &lt;status&gt; 部分已损坏。

这种情况下的解决方案是为每个进程输出单独的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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 2011-07-02
    • 2023-03-25
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    相关资源
    最近更新 更多