Valgrind:只有当内存肯定丢失时才会出现非零退出代码

问题描述 投票:1回答:1

我们使用valgrind作为CI过程的一部分。如果存在一些内存问题,valgrind必须返回非零代码,并报告此事件。这就是我们运行它的方式:

valgrind --error-exitcode=1 --tool=memcheck --leak-check=full \
    --errors-for-leak-kinds=definite --show-leak-kinds=definite \
    --track-origins=yes ./some_cgo_application

(...)

==25182== HEAP SUMMARY:
==25182==     in use at exit: 2,416,970 bytes in 34,296 blocks
==25182==   total heap usage: 83,979 allocs, 49,684 frees, 5,168,335 bytes allocated
==25182== 
==25182== LEAK SUMMARY:
==25182==    definitely lost: 0 bytes in 0 blocks
==25182==    indirectly lost: 0 bytes in 0 blocks
==25182==      possibly lost: 3,024 bytes in 7 blocks
==25182==    still reachable: 2,413,946 bytes in 34,289 blocks
==25182==                       of which reachable via heuristic:
==25182==                         newarray           : 520 bytes in 1 blocks
==25182==         suppressed: 0 bytes in 0 blocks
==25182== Reachable blocks (those to which a pointer was found) are not shown.
==25182== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==25182== 
==25182== For counts of detected and suppressed errors, rerun with: -v
==25182== ERROR SUMMARY: 20 errors from 5 contexts (suppressed: 0 from 0)

目前我们只对definitly lost的记忆感兴趣。如果没有绝对丢失的块,valgrind的退出代码预计为零。然而,尽管有1选项,它仍会返回--errors-for-leak-kinds=definite --show-leak-kinds=definite

echo $?
1

是否还有其他任何有助于实现预期结果的选择?

memory-leaks valgrind exit-code
1个回答
1
投票

我怀疑退出状态1来自程序本身。我可以用以下方法重现:

$ valgrind --error-exitcode=1 --tool=memcheck --leak-check=full \
  --errors-for-leak-kinds=definite --show-leak-kinds=definite \
  --track-origins=yes /bin/false

这看起来不像当前来源中可以更改的内容:

   case VgSrc_ExitProcess: /* the normal way out (Darwin) */
      /* Change the application return code to user's return code,
         if an error was found */
      if (VG_(clo_error_exitcode) > 0 
          && VG_(get_n_errs_found)() > 0) {
         VG_(client_exit)( VG_(clo_error_exitcode) );
      } else {
         /* otherwise, return the client's exit code, in the normal
            way. */
         VG_(client_exit)( VG_(threads)[tid].os_state.exitcode );
      }

而这个exitcode成员是从sys_exit_groupcoregrind/m_syswrap/syswrap-linux.c包装器设置的,没有任何方法可以调整它。

考虑到这一点,我认为你最好的选择(没有修补valgrind)是选择一个不同于程序可能使用的退出状态的退出状态,并将其用作valgrind错误的指示器。

© www.soinside.com 2019 - 2024. All rights reserved.