-1

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)?

6
  • 1
    Add exit $_exitcode at the end of your script to exit gdb with the exit code of the program. Commented Mar 18, 2024 at 8:19
  • 1
    I don't think this is a good idea. Make your testing program dump core on segmentation fault instead and only run gdb if it crashes. Commented Mar 18, 2024 at 8:21
  • @oguzismail phuuu I thought about that and found it more difficult to retrieve the core dump as an artifact from my gitlab runners (in an automated way) than to just read the console output. Commented Mar 18, 2024 at 9:21
  • You don't have to retrieve it as an artifact. gdb -batch -ex bt ./test /path/to/coredump should work. Commented Mar 18, 2024 at 9:36
  • @oguzismail I edited the question. Feel free to review the solution :) Commented Mar 18, 2024 at 10:36

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.