繁体   English   中英

有没有办法从 valgrind 中获取泄漏摘要?

[英]Is there a way to get just the leak summary out of valgrind?

我正在尝试制作一个脚本来编译 a.cpp 文件,对 output 运行泄漏检查,然后提示用户是否自动运行 output,所有这些都从这个角度工作。

问题是我只想要

LEAK SUMMARY:
    definitely lost: ...
    possibly lost: ...
    still reachable: ...
    suppressed: ...

output 的部分。 我尝试通过 grep 管道 valgrind,但它没有正确过滤。 有任何想法吗?

代码 (.sh)

clear; clear; clear; g++ *.cpp -o outputFile && echo "Compilation Successful!" && ( valgrind --leak-check=full ./output | grep  "LEAK\|lost:\|reachable\|suppressed\|possible" ) && {
echo "Do you want to run this file as compiled? (y/n)"
read decision
if [ "$decision" == "y" ]
then
./output
fi
echo "Script ended."
exit
}
echo "Something went wrong!"
exit

Output:

Compilation Successful!
{full valgrind output}
Something went wrong!

出现问题的行似乎是由 grep 引起的,尽管如果删除 grep 语句,脚本的 rest 会正常运行。

所需的 output:

Compilation Successful!
LEAK SUMMARY:
    definitely lost: ...
    possibly lost: ...
    still reachable: ...
    suppressed: ...
Do you want to run this file as compiled? (y/n)
>y
{program output displayed}
Script ended.

我已经用这个修复了代码:

clear; clear; clear; g++ *.cpp -o outputFile && echo "Compilation Successful!" && valgrind --leak-check=full ./outputFile &> CompileTempFile.txt && grep -A5 "LEAK SUMMARY:" CompileTempFile$
echo "Do you want to run this file as compiled? (y/n)"
read decision
if [ "$decision" == "y" ]
then
./output
fi
rm CompileTempFile.txt
echo "Script ended."
exit
}
rm CompileTempFile.txt
echo "Something went wrong!"
exit

编辑:我更新了代码,所以我想我会分享我当前的(更智能的)版本。 :)

clear; clear; clear; g++ *.cpp -o outputFile && echo "Compilation Successful!" && echo "Running a leak test" && valgrind --leak-check=full ./outputFile &> CompileTempFile.txt && echo "Memory test completed!" && egrep -A5 "LEAK SUMMARY|no leaks are possible" CompileTempFile.txt && {
echo "Do you want to run this file as compiled? (y/n)"
read decision
if [ "$decision" == "y" ]
then
./outputFile
fi
rm CompileTempFile.txt
echo "Script ended."
exit
}
rm CompileTempFile.txt
echo "Something went wrong!"
exit

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM