gdb 如何检索目标程序的退出代码?

How does gdb retrieve the exit code of target program?

在命令行下,我知道用echo $?给我退出代码。在gdb中,我通过程序使用"r"到运行,程序就终止了,那么gdb是怎么得到这个exit code的呢? gdb 中有任何命令吗?

谢谢!

它只是在程序终止时在调试会话结束时打印退出代码。或者为 0 退出代码打印 exited normally。查看此测试程序的测试调试会话:

#include <stdlib.h>

int main(int argc, char *argv[]) {
    return atoi(argv[1]);
}

调试会话:

[ksemenov@NB824RIH ~]$ gdb -q ./a.out
Reading symbols from ./a.out...(no debugging symbols found)...done.
(gdb) r 0
Starting program: /home/ksemenov/a.out 0
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.23.1-10.fc24.x86_64
[Inferior 1 (process 19162) exited normally]
(gdb) r 1
Starting program: /home/ksemenov/a.out 1
[Inferior 1 (process 19166) exited with code 01]
(gdb) r 6
Starting program: /home/ksemenov/a.out 6
[Inferior 1 (process 19167) exited with code 06]
(gdb) 

当程序退出时,gdb 将便利变量 $_exitcode 设置为退出代码。

所以给出:

int main() {
  return 23;
}

运行 它在 gdb 中,我得到:

(gdb) run
Starting program: /tmp/q 
[Inferior 1 (process 3677) exited with code 027]
(gdb) print $_exitcode
 = 23