enumag
enumag

Reputation: 916

GDB how to log exit code?

I'm trying to debug PHP segmentation fault in my CI. I managed to get the GDB stacktrace by running phpunit like this:

gdb php -ex "run vendor/bin/phpunit" -ex bt -ex quit

The problem is that this way I do not know if phpunit succeeded or failed (doesn't matter if it failed with segfault or with test failure). I need to somehow log the exit code of the vendor/bin/phpunit command and use it in an if statement later - to decide whether or not the build succeeded.

Upvotes: 0

Views: 370

Answers (1)

ks1322
ks1322

Reputation: 35716

The problem is that this way I do not know if phpunit succeeded or failed (doesn't matter if it failed with segfault or with test failure)

You can print the value of $_exitsignal variable. In case of SIGSEGV signal it would be equal to 11. Also you can use $_isvoid function to know if the program exited or signalled. For this code:

#include <signal.h>

int main (int argc, char *argv[])
{
  raise (SIGSEGV);
  return 0;
}

you can invoke gdb this way:

$ gdb -q -ex run -ex bt -ex c -ex "print \$_isvoid(\$_exitsignal)" -ex quit a.out 
Reading symbols from a.out...
Starting program: /tmp/a.out 

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7e1e9e5 in raise () from /lib64/libc.so.6
#0  0x00007ffff7e1e9e5 in raise () from /lib64/libc.so.6
#1  0x000000000040113f in main (argc=1, argv=0x7fffffffd6e8) at 1.c:5
Continuing.

Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
$1 = 0

At the end of output you can see that the program ended with signal, $_isvoid($_exitsignal) returned 0:

$1 = 0

Upvotes: 1

Related Questions