gdb如何从脚本执行目标程序 [英] gdb how to execute target program from script

查看:474
本文介绍了gdb如何从脚本执行目标程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用gdb调试程序。但是我通过脚本调用该程序。



如何使用gdb?
脚本很长,我无法使用gdb中的命令行参数直接调用该程序。



此外,脚本创建的过程启动是短暂的,所以不能将gdb附加到进程。



我想要做的是像使用该程序启动gdb,然后添加我的断点,然后
执行脚本(FYI - 它也需要参数),然后当它点击断点做任何我想要的。



我看了gdb中的shell选项,但是这个一个新的shell如果我没有错,返回gdb完成后。这不是我想要的。



请让我知道如果有更好的方法。

解决方案

有几种方法。



真正的老校方法是将程序的 main 中的一个循环,如:

  volatile int zzz 
...
int main(){
while(!zzz)sleep(1);

然后运行脚本。在单独的窗口中,在要调试的程序上运行gdb,并使用 attach 附加到休眠程序。然后你可以设置断点等,最后:

 (gdb)set var zzz = 1 
(gdb) cont

稍微较新的(new,如gdb至少10年)方法是在调用要调试的程序之前编辑脚本并放入一个 gdb --args 。然而,这种方法并不总是奏效。它不能正确地处理重定向。



最后,您可以使用多级调试。这是所有人的最新方法。在这里,我正在关注以下主题的我自己的博客帖子

  $ gdb / bin / sh#或任何运行脚本
(gdb)set args arguments-to-the-script
gdb)set detach-on-fork off
(gdb)在
(gdb)上设置target-async在
(gdb)上设置不间断设置pagination off

然后,您可以执行以下操作:

 (gdb)add-inferior -exec程序 - 你想要的调试

。然后切换到那个劣质和设置断点。现在切换回第一个劣势,运行 - 它应该都可以工作!


I want to debug a program, using gdb. But I invoke that program through a script.

How can I use gdb? The script is long and its not possible for me to invoke the program directly using command line arguments from gdb.

Also the process that is created when the script starts is short lived so can't attach gdb to the process.

What I want to do is something like, start gdb with that program, add my breakpoints then execute the script (FYI - it also takes arguments), then when it hits breakpoint do whatever I want.

I looked at shell option in gdb, but that spawns a new shell if I am not wrong and return to gdb when done. This is not what I want.

Please let me know if there is a better way.

解决方案

There are several ways.

The truly old-school way is to hack a loop into your program's main like:

volatile int zzz;
...
int main() {
  while (!zzz) sleep (1);

Then, run your script. In a separate window, run gdb on the program you want to debug, and use attach to attach to the sleeping program. Then you can set breakpoints, etc, and finally:

(gdb) set var zzz = 1
(gdb) cont

A slightly newer ("new" as in "it's been in gdb at least 10 years") way is to edit your script and put a gdb --args before the invocation of the program you want to debug. This method doesn't always work, though. It doesn't handle redirections properly, among other things.

Finally, you can use multi-inferior debugging. This is the "newest" approach of all. Here I'm following my own blog post on the topic:

$ gdb /bin/sh  # or whatever runs your script
(gdb) set args arguments-to-the-script
(gdb) set detach-on-fork off
(gdb) set target-async on
(gdb) set non-stop on
(gdb) set pagination off

Then you can do something like:

(gdb) add-inferior -exec program-you-want-to-debug

... then switch to that inferior and set breakpoints. Now switch back to the first inferior and run -- it should all work!

这篇关于gdb如何从脚本执行目标程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆