gpt4 book ai didi

linux - 使用 bash 脚本使用 valgrind 检查内存

转载 作者:太空宇宙 更新时间:2023-11-04 03:49:51 25 4
gpt4 key购买 nike

尝试编写一个脚本来检查是否存在内存泄漏或试图访问坏内存

正在尝试这个脚本但不幸的是,当 prog 编译时带有警告时,valgrind 给了我“BAD”状态,即使不存在内存泄漏

#!/bin/bash
#some varibles for the future
output=${1}
progname=${2}
input=${3}
trash="/dev/null"

# execute the prog and putting output to out file
valgrind --error-exitcode=1 ./${progname} < ${input} >${output} 2>&1

# grab exit status of valgrind (0 if all good ,1 if not)
ret=$?

# write appropriate message as per return status value
((ret == 0)) && echo "Memory V" || echo "Memory X"

# return the exit status of testMemory
exit $ret

我想也许是这样的代码

# cheking for patters that indicate err
#if [[ grep "ERROR SUMMARY: 0 errors" "$output" ]] # >"$trash" 2>&1
#then
# ret=0
#else
# ret=1
#fi


any ideas?

最佳答案

我就是这样做的我希望它能检查所有错误

#!/bin/bash

#some varibles for the future
output=${1}
progname=${2}
input=${3}
trash="/dev/null"

# execute the prog and putting output to out file
valgrind ./${progname} < ${input} >${output} 2>&1
# cheking for patters that indicate err
grep -q "ERROR SUMMARY: 0 errors" "$output"

# grab exit status of grep (0 if all good ,1 if not)
ret=$?

# write appropriate message as per return status value
((ret == 0)) && echo "Memory V" || echo "Memory X"

# return the exit status of testMem
exit $ret

关于linux - 使用 bash 脚本使用 valgrind 检查内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26711216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com