首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在gdb脚本中使用“返回”命令

在gdb脚本中使用“返回”命令
EN

Stack Overflow用户
提问于 2018-09-05 18:36:39
回答 1查看 1.2K关注 0票数 1

我正在尝试自动化一个测试,其中我做了一个函数,返回一个错误。但是gdb“返回(int) 22”命令似乎没有被执行,程序退出时有一个成功的代码。要使gdb脚本中的“返回”命令工作,是否需要做一些特殊的工作?下面是我使用的gdb脚本:

代码语言:javascript
复制
set width 0
set height 0
set confirm off
set pagination off
set verbose off
set breakpoint pending on

set args the program args

break file.c:function_in_which_to_break
commands 1
    return 22
    continue
end

run the program args
quit

我将使用以下gdb命令行运行该程序:

代码语言:javascript
复制
gdb --batch --command=rc my_program

所需的行为是"function_in_which_to_break“返回22。其余的代码将一直在调用堆栈中传递,直到程序退出,程序的退出代码应该是22。

实际的行为是程序的退出代码是成功的。

当我在终端中运行gdb下的程序时(使用命令

代码语言:javascript
复制
gdb --args my_program the program args

),输入“file.c:function_in_which_to_break file.c:function_in_which_to_break”并运行,程序就会中断。然后,当我输入“返回22”和“继续”时,程序的行为与我预期的一样,函数返回22,程序返回22,我期望的失败显示出来。

更新:当我说程序返回“成功”时,我的意思是gdb报告说孩子返回了成功。当我说程序返回22时,我的意思是gdb报告子程序返回22 (实际上它说的是“用代码026退出”)。在这两种情况下,gdb本身都返回成功。

更新2:我在gdb调用前后的自动化中发现了一些错误--在拼写错误的文件中查找"exited 026“,调用拼写错误的gdb脚本,诸如此类的蠢事。一旦修复了这些命令,带有return语句的gdb脚本文件就可以工作了。因此,一旦有人写出一个类似于“返回命令应该和所有其他命令一样工作”的答案,我就会接受它。@ks1322 1322

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-06 14:30:29

实际的行为是程序的退出代码是成功的。

根据文档,您还应该运行带有-return-child-result选项的gdb,以获得正在调试的进程的返回代码:

代码语言:javascript
复制
-return-child-result

    The return code from GDB will be the return code from the child process (the process being debugged), with the following exceptions:

        GDB exits abnormally. E.g., due to an incorrect argument or an internal error. In this case the exit code is the same as it would have been without ‘-return-child-result’.
        The user quits with an explicit value. E.g., ‘quit 1’.
        The child process never runs, or is not allowed to terminate, in which case the exit code will be -1. 

    This option is useful in conjunction with ‘-batch’ or ‘-batch-silent’, when GDB is being used as a remote program loader or simulator interface.

更新:

看来,程序成功的真正原因是分析gdb输出时出现了一些错误。无论如何,我想使用-return-child-result和分析gdb退出代码比解析文本输出更容易出错。

要使gdb脚本中的“返回”命令工作,是否需要做一些特殊的工作?

看起来不是,但恢复执行的命令有例外,有关详细信息,请参阅如何在gdb命令中打印输入和离开函数?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52191529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档