简体   繁体   English

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

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

I am trying to make a script that will compile a.cpp file, run a leak check on the output, and then prompt the user whether to run the output or not automatically, and all of it works from that standpoint.我正在尝试制作一个脚本来编译 a.cpp 文件,对 output 运行泄漏检查,然后提示用户是否自动运行 output,所有这些都从这个角度工作。

The problem is that I just want the问题是我只想要

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

section of the output. output 的部分。 I tried piping valgrind through grep, but it's not filtering properly.我尝试通过 grep 管道 valgrind,但它没有正确过滤。 Any ideas?有任何想法吗?

Code (.sh)代码 (.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: Output:

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

The something went wrong line seems to be caused by grep though since the rest of the script functions properly if the grep statement is removed.出现问题的行似乎是由 grep 引起的,尽管如果删除 grep 语句,脚本的 rest 会正常运行。

Desired output:所需的 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.

I have fixed the code with this:我已经用这个修复了代码:

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

EDIT: I updated the code, so I thought I'd share my current (smarter) version.编辑:我更新了代码,所以我想我会分享我当前的(更智能的)版本。 :) :)

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