简体   繁体   English

使用 gdb 调试时干净地退出 valgrind

[英]Quit valgrind cleanly when debugging with gdb

I am debugging a program using valgrind and gdb.我正在使用 valgrind 和 gdb 调试程序。 However I terminate those debugging sessions in a barbaric way… Is it really how it is meant to be done ?然而,我以一种野蛮的方式终止了这些调试会话……这真的是它的本意吗?

Setting up the debugging session设置调试会话

Following the instructions from the official valgrind website I do the following to run the program :按照官方 valgrind 网站的说明,我执行以下操作来运行程序:

  1. I run valgrind by entering我通过输入运行 valgrind

     valgrind --tool=memcheck --vgdb=yes --vgdb-error=0 ./prgm.run
  2. From another terminal session, I run gdb using从另一个终端会话,我运行 gdb 使用

    gdb ./prgm.run
  3. I connect gdb to valgrind我将 gdb 连接到 valgrind

     (gdb) target remote | vgdb
  4. I run the program from gdb CLI我从 gdb CLI 运行程序

    (gdb) c

So far so good : the program runs in both terminals (the one used for valgrind and the one used for gdb).到目前为止一切顺利:程序在两个终端中运行(一个用于 valgrind,一个用于 gdb)。 Then valgrind finds an error, for instance an invalid read, and the program execution is paused.然后 valgrind 发现错误,例如无效读取,程序执行暂停。

Terminating the session终止会话

At that point, I want to fiddle with my code : perhaps fix something or comment/uncomment stuff from the program's source.那时,我想摆弄我的代码:也许从程序的源代码中修复某些内容或注释/取消注释内容。 As a consequence, the program needs to be compiled anew.因此,程序需要重新编译。 A new binary is generated.生成一个新的二进制文件。 Following that, I want to stop the on-going valgrind and gdb sessions (that were using the old binary) and start new valgrind and gdb sessions that will use the new binary.之后,我想停止正在进行的 valgrind 和 gdb 会话(使用旧二进制文件)并启动将使用新二进制文件的新 valgrind 和 gdb 会话。

To stop an on-going session, I quit gdb为了停止正在进行的会话,我退出了 gdb

(gdb) q

Sometimes valgrind notices that gdb is no longer there and quits too.有时 valgrind 注意到 gdb 不再存在并且也退出了。 But other times valgrind keeps going even-though no gdb process exist anymore…但是其他时候 valgrind 会继续运行——尽管不再存在 gdb 进程……

In that case I kill the "memcheck-amd64-" process corresponding to my valgrind session.在这种情况下,我会终止与我的 valgrind 会话对应的“memcheck-amd64-”进程。 The number of that process is indicated in the valgrind messages eg 16195 in ==16195== Invalid read of size 8 ).该进程的数量在 valgrind 消息中指示,例如16195 in ==16195== Invalid read of size 8 )。

kill -9 16195

A regular kill is not enough : I need to use the -9 option.常规杀死是不够的:我需要使用-9选项。

I don't think invoking kill -9 is how it is meant to be done… Am I missing something ?我不认为调用kill -9是应该做的......我错过了什么吗?

valgrind version : 3.10.1 valgrind 版本:3.10.1

gdb version : 7.7.1 gdb 版本:7.7.1

you can also use the comand你也可以使用命令

(gdb)monitor v.kill

it was listed on monitor help on gdb.它列在 gdb 的监视器帮助中。

The previous answers did not work for me, so I found this which did the trick.以前的答案对我不起作用,所以我发现可以解决问题。

  1. (gdb) info inferiors Should list all inferiors in gdb session, find the one with 'remote target' as its name, take note of the number on the left of it (will be 1 if no other inferiors running in gdb) (gdb) info inferiors应该列出 gdb 会话中的所有劣化,找到名称为 'remote target' 的那个,记下它左边的数字(如果没有其他劣化在 gdb 中运行,则为 1)
  2. (gdb) kill inferiors <number> Replace <number> with inferior number. (gdb) kill inferiors <number><number> > 替换为下等数字。
  3. (gdb) quit

Just use kill command or simply只需使用kill命令或干脆

k
  • to get rid of asking use set confirm off in .gdbinit file摆脱在.gdbinit文件中询问使用set confirm off

The following works for me: 以下适用于我:

  1. Detach from gdb first: (gdb) detach . 首先从gdb分离:( (gdb) detach
  2. Then quit gdb : (gdb) quit . 然后退出gdb(gdb) quit
  3. Then CTRL + C valgrind. 然后CTRL + C valgrind。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM