Retrieving Exit Code from a Program Run with GDB in GitLab CI/CD Pipeline

46 views Asked by At

I have a custom test binary, which I use to run specific system tests (e.g. ./test test_case_1.yaml.

The exit code of ./testindicates if the system test succeeded.

I want to guard myself against future cases of segfaults that are extremely hard to reproduce segfaults by wrapping the program call with gdb. In case the program crashes I can then automatically (no user input: see this question) print the stacktrace and see where the error originated from.

My current issue is that once started with gdb, and with no segmentation fault occurring, I cannot retrieve the exit code of the system test and use it with my gitlab-ci runner. How can I get the gdb backtrace if the program crashes and otherwise just the exit code of ./test (as if gdb wasn't used)?

1

There are 1 answers

0
Flo Ryan On

Based on oguz's answer i came up with the following script:

if [ $? -eq 139 ]; then
  # Get the core dump location
  core_dump_location=$(cat /proc/sys/kernel/core_pattern)
  echo "Core dumps are stored in: $core_dump_location"

  # If it did, find the most recently created coredump and copy it to the current directory
  latest_coredump=$(ls -t ${core_dump_location}* | head -n 1)
  cp "$latest_coredump" .
  
  # Open the coredump with gdb and print the backtrace
  gdb -ex "bt" -batch ./test "$latest_coredump" 
fi