[英]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.