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

时间:2019-11-21 03:44:35

标签: c++ bash grep valgrind

我试图制作一个脚本,该脚本将编译一个.cpp文件,对输出进行泄漏检查,然后提示用户是否自动运行输出,并且所有这些都从这个角度出发。 / p>

问题是我只想要

LEAK SUMMARY:
    definitely lost: ...
    possibly lost: ...
    still reachable: ...
    suppressed: ...
输出的

部分。我尝试通过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

输出:

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

尽管grep语句被删除,但脚本的其余部分仍能正常运行,所以似乎出现错误的行是grep造成的。

所需的输出:

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.

1 个答案:

答案 0 :(得分:0)

我已使用以下代码修复了代码:

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