如果在 valgrind 的输出中发现错误,我想使用程序的调试版本生成输出。然而,在调试中进行数千次运行是非常耗时的。
所以我想做的是在发布模式下运行,如果发现错误中止运行并在调试中完成测试。
缺少监控输出和手动杀死进程,有更好的解决方案吗?
You can use --db-attach=yes
and --db-command=
to execute specific command you want to stop your Valgrind execution. But for normal debug process, --db-command
invokes gdb
with child process. So you cannot stop the execution by killing the process using --db-command=kill -9 %p
, because it just kills the child process, not the Valgrind itself.
If you are using Linux and has /proc
file system support, you can get the parent process number in the 4th column in /proc/PID/stat
. Such that you have the chance to kill the parent process to stop Valgrind.
For example,
valgrind --db-attach=yes --db-command="cat /proc/%p/stat | cut -d' ' -f4 | xargs kill -9" ./a.out
When the first error appears, you will be asked
---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ----
And when you press Y
, it will try to invoke debug command. So in this case, it will get the parent process ID, which is valgrind, and send KILL signal to the process. Therefore, Valgrind shall be immediately stopped.
所以我想做的是在发布模式下运行,如果发现错误中止运行并在调试中完成测试。
假设您有两个可执行文件:a.out
和a.out-g
,并且您想使用不同的参数集运行它们,这应该适用于bash
:
# Arguments to try
args=(
"-foo"
"-foo -bar"
"-bar -baz"
...
)
for a in "${args[@]}"; do
if valgrind -q --error-exitcode=1 \
--db-attach=yes --db-command="kill -9 %p" ./a.out $a; then
echo PASS: ./a.out $a
else
echo FAIL: ./a.out $a
valgrind ./a.out-g $a
fi
done
--db-attach=yes
论据不存在了。
当前使用是通过vgdb
二进制调解的:
valgrind --vgdb=yes --vgdb-error=0 my_prog
它显示下一步要做什么:
==2466== Memcheck, a memory error detector
==2466== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2466== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==2466== Command: ./my_prog
==2466==
==2466== (action at startup) vgdb me ...
==2466==
==2466== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==2466== /path/to/gdb ./my_prog
==2466== and then give GDB the following command
==2466== target remote | /usr/bin/vgdb --pid=2466
gdb ~/Sources/my_prog
(gdb) target remote | /usr/bin/vgdb --pid=2604
您可以使用
valgrind --gen-suppressions=no|yes|all
就目前而言 - 它用于抑制,但我相信它会满足您的需求。