Skip to content

Instantly share code, notes, and snippets.

@l0ki000
Created June 13, 2025 12:36
Show Gist options
  • Save l0ki000/dc365e5a03a01f23854a3d4056fa5110 to your computer and use it in GitHub Desktop.
Save l0ki000/dc365e5a03a01f23854a3d4056fa5110 to your computer and use it in GitHub Desktop.
continuous run for tests (optionally with gdb)
#!/bin/bash
# set -x
CMD_DBG_NAME="commands.dbg"
COUNT_TO_RUN=100
if [[ $# == 0 ]]
then
echo "Run the specified command continuously (optionally in gdb), until:
* count (default is 100)
* non-0 exit code
* SIGSEGV or SIGTRAP (breakpoint) in gdb. You can add your conditional breakpoints to "$CMD_DBG_NAME".
It's also reasonable to add "--break" at the very end if using with catch2 excutables.
Usage: $(basename "$0") [-v|--verbose] [-g|--gdb] [-c|--count=N] -- <command_to_run>
Example: $(basename "$0") --verbose --gdb --count=10 ./UnitTest \"My buggy test name\" --break"
exit 0
fi
tmp_opt=$(getopt --options gvc: --long gdb,verbose,count: --name "$(basename "$0")" -- "$@")
ret=$?
if [[ $ret != 0 ]]
then
echo "Failed to recognize options." >&2
exit $ret
fi
eval set -- "$tmp_opt"
opt_gdb=false
opt_verbose=false
opt_count=$COUNT_TO_RUN
while true
do
case "$1" in
-v | --verbose ) opt_verbose=true; shift ;;
-g | --gdb ) opt_gdb=true; shift ;;
-c | --count ) opt_count="$2"; shift 2 ;;
-- ) shift ; break ;;
* ) break ;;
esac
done
if [[ $opt_verbose == true ]]
then
set -x
fi
# check that cores are enabled
# ulimit=$(ulimit -c)
# if [[ $ulimit != "unlimited" ]]
# then
# echo "Wrong ulimit: $ulimit"
# exit 1
# fi
# create gdb script
if [[ $opt_gdb == true ]]
then
if [[ -f $CMD_DBG_NAME ]]
then
echo "Local GDB script found \"$CMD_DBG_NAME\", hopefully you know what you are doing."
else
cat > $CMD_DBG_NAME << EOL
set pagination off
set confirm on
run
if \$_isvoid (\$_exitcode) == 0
if \$_exitcode == 0
quit
end
end
EOL
fi
fi
ret=0
count=0
while [[ $ret == 0 && $count -lt $opt_count ]]
do
if [[ $opt_gdb == true ]]
then
gdb --return-child-result --command=./$CMD_DBG_NAME --args "$@"
else
"$@"
fi
ret=$?
[[ $ret == 0 ]] && (( count+=1 ))
echo "Count of successful runs: $count"
done
echo "Return code: $ret"
if [[ $opt_verbose == true ]]
then
set +x
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment