【问题标题】:Count valgrind errors without reporting them计算 valgrind 错误而不报告它们
【发布时间】:2019-10-06 14:32:12
【问题描述】:

我目前正在维护一个内存池。我最近在这个池中添加了 valgrind 函数调用,以便更有用地检测通过使用所述池而发生的 valgrind 错误。我想要做的是编写一个单元测试来检查我对 valgrind 函数的调用是否正常工作。例如,

int main(void)
{
  int * test = pool_malloc(sizeof(*test)); // details not important
  *test = 3;
  pool_free(test); // details not important

  if (*test == 2)
  {
    printf("HERE");
  }

  assert(VALGRIND_COUNT_ERRORS == 1);
}

这个代码现在正确地给了我一个无效的读取错误,而以前它不会因为即使内存被返回到池中,它实际上不是free-d。但是,我不能使用这个确切的代码,因为我们的单元测试框架假定任何 valgrind 错误都意味着测试失败,因此我的上述测试将失败。我试过使用VALGRIND_DISABLE_ERROR_REPORTING,但这似乎不仅禁用了报告,还禁用了错误检查——即VALGRIND_COUNT_ERRORS现在返回0。我真正想要的是VALGRIND_DISABLE_ERROR_REPORTING_BUT_KEEP_COUNTING_ERRORS_THAT_OCCUR之类的东西——确实存在类似的东西?有没有更好的方法来完成我想做的事情?

【问题讨论】:

    标签: c valgrind


    【解决方案1】:

    您可以做的是使用 valgrind 客户端请求 VALGRIND_COUNT_ERRORS。

    valgrind.h 说:

        ...
                 /* Can be useful in regression testing suites -- eg. can
                     send Valgrind's output to /dev/null and still count
                     errors. */
                  VG_USERREQ__COUNT_ERRORS = 0x1201,
        ...
        /* Counts the number of errors that have been recorded by a tool.  Nb:
           the tool must record the errors with VG_(maybe_record_error)() or
           VG_(unique_error)() for them to be counted. */
    

    所以,类似: valgrind --log-file=/dev/null your_program 将使valgrind错误报告给/dev/null,然后your_program可以 如果错误计数不符合预期,则输出错误。

    【讨论】:

      猜你喜欢
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多