Search code examples
gdbechoexitterminate

How does gdb retrieve the exit code of target program?


Under command line, I know that using echo $? gets me the exit code. In gdb, I use "r" to run through the program and the program terminates, so how does gdb gets this exit code? Any commands inside gdb?

Thanks!


Solution

  • When a program exits, gdb sets the convenience variable $_exitcode to the exit code.

    So given:

    int main() {
      return 23;
    }
    

    Running it in gdb, I get:

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