bash 如何在脚本中获取 valgrind 是否发现内存泄漏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19246470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:13:24  来源:igfitidea点击:

How to get in script whether valgrind found memory leaks?

linuxbashvalgrind

提问by B?ови?

I am running valgrind in a bash script, and directing the valgrind's output to a file. Like this :

我在 bash 脚本中运行 valgrind,并将 valgrind 的输出定向到一个文件。像这样 :

valgrind --leak-check=full --show-reachable=yes --xml=yes --xml-file=unit_tests_valgrind.out.xml ./unit_tests_runner

The RET_VALUE=$?is going to put the return value of the program (unit_tests_runner above), not from valgrind. And the only way to check whether the valgrind found memory problems is to open the log.

RET_VALUE=$?打算把程序(unit_tests_runner以上)的返回值,而不是从Valgrind的。而检查valgrind是否发现内存问题的唯一方法就是打开日志。

Is there a way to check in a script whether there are memory problems? If yes, how?

有没有办法检查脚本是否存在内存问题?如果是,如何?

回答by devnull

You seem to be looking for the --error-exitcodeoption.

你似乎在寻找这个--error-exitcode选项。

Since it defaults to 0, the return code from Valgrind is the same as that of the process. Set it to a non-zero value instead.

由于它默认为0,因此 Valgrind 的返回码与进程的返回码相同。将其设置为非零值。

From Valgrind core manual:

来自Valgrind 核心手册

--error-exitcode=<number> [default: 0]

Specifies an alternative exit code to return if Valgrind reported any errors in the run. When set to the default value (zero), the return value from Valgrind will always be the return value of the process being simulated. When set to a nonzero value, that value is returned instead, if Valgrind detects any errors. This is useful for using Valgrind as part of an automated test suite, since it makes it easy to detect test cases for which Valgrind has reported errors, just by inspecting return codes.

--error-exitcode=<number> [default: 0]

如果 Valgrind 在运行中报告任何错误,则指定要返回的替代退出代码。当设置为默认值(零)时,Valgrind 的返回值将始终是被模拟过程的返回值。当设置为非零值时,如果 Valgrind 检测到任何错误,则返回该值。这对于将 Valgrind 用作自动化测试套件的一部分非常有用,因为它可以轻松检测 Valgrind 报告错误的测试用例,只需检查返回代码即可。