我们使用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
是否还有其他任何有助于实现预期结果的选择?
我怀疑退出状态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_group
的coregrind/m_syswrap/syswrap-linux.c
包装器设置的,没有任何方法可以调整它。
考虑到这一点,我认为你最好的选择(没有修补valgrind)是选择一个不同于程序可能使用的退出状态的退出状态,并将其用作valgrind错误的指示器。