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

时间:2018-09-05 18:36:39

标签: automation gdb

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

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命令行运行程序:

gdb --batch --command=rc my_program

所需的行为是“ function_in_which_to_break”返回22。其余代码将继续将该调用传递到调用堆栈,直到程序退出并且该程序的退出代码应为22。

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

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

gdb --args my_program the program args

),通过键入“ break file.c:function_in_which_to_break”在file.c:function_in_which_to_break处中断并运行,程序确实在此处中断。然后,当我键入“ return 22”和“ continue”时,程序将按照我的预期运行,函数将返回22,程序将返回22,并显示出我期望的失败。

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

更新2:我发现有关gdb调用的自动化存在一些错误-在名称拼写错误的文件中查找“代码退出026”,调用了拼写错误的gdb-rc脚本,这很愚蠢。修复这些错误后,命令中包含return语句的gdb脚本文件似乎可以正常工作。因此,只要有人写下“返回命令应与所有其他命令相同的答案”之类的答案,我就会接受。 @ ks1322

1 个答案:

答案 0 :(得分:1)

  

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

根据documentation,还应该使用带有-return-child-result选项的gdb来获取正在调试的进程的返回代码:

-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脚本中执行命令?

似乎没有,但是恢复执行的命令有例外,有关详细信息,请参见How to print Entering and Leaving for a function in gdb command?