[英]How can I execute external commands from the gdb command prompt?
I am debugging a program using gdb. 我正在使用gdb调试程序。 Whenever I miss a breakpoint or decide to add another watchpoint, I have to kill the process and rerun it.
每当我错过断点或决定添加另一个观察点时,我必须终止该过程并重新运行它。 In order to attach the existing gdb to it, I use
attach <pid>
. 为了将现有的gdb附加到它,我使用
attach <pid>
。 However, I have to find out the pid of the new process. 但是,我必须找出新流程的pid。
The way I do that today is to suspend gdb, get the pid with ps -C <program_name>
and then return to gdb to attach to it. 我今天这样做的方法是暂停gdb,使用
ps -C <program_name>
获取pid,然后返回到gdb以附加到它。
Is there any way to run a unix command from the gdb command prompt without exiting to the shell, so that I could do something like this from inside gdb: 有没有办法从gdb命令提示符运行unix命令而不退出到shell,所以我可以从gdb内部执行类似这样的操作:
attach `ps -C <program_name>`
I am working on linux. 我正在研究linux。
(gdb) help shell
(gdb)帮助shell
Execute the rest of the line as a shell command.作为shell命令执行该行的其余部分。 With no arguments, run an inferior shell.
没有参数,运行劣质shell。
After finish, you can use 'exit' to return to gdb. 完成后,您可以使用'exit'返回gdb。
gdb$ shell
shell$ execute_your_commands
shell$ exit
gdb$ you_have_returned_back_to_gdb_prompt
You can just use shell
followed by the command you want to execute, eg: 您可以使用
shell
后跟要执行的命令,例如:
gdb$ shell g++ tsort.c -o tsort
This is just a short form of the previous answer. 这只是前一个答案的简短形式。
Just to expand on the accepted answer, the shortcut for shell
is !
只是为了扩展已接受的答案,
shell
的快捷方式是!
. 。
(gdb) help !
(gdb)帮忙!
Execute the rest of the line as a shell command.
作为shell命令执行该行的其余部分。 With no arguments, run an inferior shell.
没有参数,运行劣质shell。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.