| 1 | #!/bin/bash |
| 2 | # cgreen-debug |
| 3 | # |
| 4 | # Script to start cgreen-runner under gdb, load a library and break |
| 5 | # on a named test. |
| 6 | # |
| 7 | |
| 8 | # If you are viewing 'cgreen-debug' this *is* that script and you'll |
| 9 | # find the logic at the end of this file. To update it, edit |
| 10 | # `cgreen-debug.argbash` instead. |
| 11 | |
| 12 | # But if you are looking at 'cgreen-debug.argbash' then this is |
| 13 | # actually a template for argbash (https://argbash.io) which generates |
| 14 | # argument parsing capable bash scripts from a template just like |
| 15 | # this. |
| 16 | |
| 17 | # To generate a new 'cgreen-debug' use |
| 18 | # |
| 19 | # 'argbash cgreen-debug.argbash -o cgreen-debug' |
| 20 | # |
| 21 | # if you have argbash installed, or go to https://argbash.io and paste |
| 22 | # this file into the online version. |
| 23 | |
| 24 | # ARG_HELP([Start cgreen-runner under GDB (or other debugger) and break at a specific test]) |
| 25 | # ARG_OPTIONAL_SINGLE([debugger],[d],[The debugger to use],[cgdb]) |
| 26 | # ARG_POSITIONAL_SINGLE([library],[Dynamically loadable library with Cgreen tests],[]) |
| 27 | # ARG_POSITIONAL_SINGLE([testname],[The test to debug, in Cgreen notation ('<Context>:<test>')],[]) |
| 28 | # ARGBASH_GO() |
| 29 | # needed because of Argbash --> m4_ignore([ |
| 30 | ### START OF CODE GENERATED BY Argbash v2.9.0 one line above ### |
| 31 | # Argbash is a bash code generator used to get arguments parsing right. |
| 32 | # Argbash is FREE SOFTWARE, see https://argbash.io for more info |
| 33 | # Generated online by https://argbash.io/generate |
| 34 | |
| 35 | |
| 36 | die() |
| 37 | { |
| 38 | local _ret="${2:-1}" |
| 39 | test "${_PRINT_HELP:-no}" = yes && print_help >&2 |
| 40 | echo "$1" >&2 |
| 41 | exit "${_ret}" |
| 42 | } |
| 43 | |
| 44 | |
| 45 | begins_with_short_option() |
| 46 | { |
| 47 | local first_option all_short_options='hd' |
| 48 | first_option="${1:0:1}" |
| 49 | test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0 |
| 50 | } |
| 51 | |
| 52 | # THE DEFAULTS INITIALIZATION - POSITIONALS |
| 53 | _positionals=() |
| 54 | # THE DEFAULTS INITIALIZATION - OPTIONALS |
| 55 | _arg_debugger="cgdb" |
| 56 | |
| 57 | |
| 58 | print_help() |
| 59 | { |
| 60 | printf '%s\n' "Start cgreen-runner under GDB (or other debugger) and break at a specific test" |
| 61 | printf 'Usage: %s [-h|--help] [-d|--debugger <arg>] <library> <testname>\n' "$0" |
| 62 | printf '\t%s\n' "<library>: Dynamically loadable library with Cgreen tests" |
| 63 | printf '\t%s\n' "<testname>: The test to debug, in Cgreen notation ('<Context>:<test>')" |
| 64 | printf '\t%s\n' "-h, --help: Prints help" |
| 65 | printf '\t%s\n' "-d, --debugger: The debugger to use (default: 'cgdb')" |
| 66 | } |
| 67 | |
| 68 | |
| 69 | parse_commandline() |
| 70 | { |
| 71 | _positionals_count=0 |
| 72 | while test $# -gt 0 |
| 73 | do |
| 74 | _key="$1" |
| 75 | case "$_key" in |
| 76 | -h|--help) |
| 77 | print_help |
| 78 | exit 0 |
| 79 | ;; |
| 80 | -h*) |
| 81 | print_help |
| 82 | exit 0 |
| 83 | ;; |
| 84 | -d|--debugger) |
| 85 | test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 |
| 86 | _arg_debugger="$2" |
| 87 | shift |
| 88 | ;; |
| 89 | --debugger=*) |
| 90 | _arg_debugger="${_key##--debugger=}" |
| 91 | ;; |
| 92 | -d*) |
| 93 | _arg_debugger="${_key##-d}" |
| 94 | ;; |
| 95 | *) |
| 96 | _last_positional="$1" |
| 97 | _positionals+=("$_last_positional") |
| 98 | _positionals_count=$((_positionals_count + 1)) |
| 99 | ;; |
| 100 | esac |
| 101 | shift |
| 102 | done |
| 103 | } |
| 104 | |
| 105 | |
| 106 | handle_passed_args_count() |
| 107 | { |
| 108 | local _required_args_string="'library' and 'testname'" |
| 109 | test "${_positionals_count}" -ge 2 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require exactly 2 (namely: $_required_args_string), but got only ${_positionals_count}." 1 |
| 110 | test "${_positionals_count}" -le 2 || _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect exactly 2 (namely: $_required_args_string), but got ${_positionals_count} (the last one was: '${_last_positional}')." 1 |
| 111 | } |
| 112 | |
| 113 | |
| 114 | assign_positional_args() |
| 115 | { |
| 116 | local _positional_name _shift_for=$1 |
| 117 | _positional_names="_arg_library _arg_testname " |
| 118 | |
| 119 | shift "$_shift_for" |
| 120 | for _positional_name in ${_positional_names} |
| 121 | do |
| 122 | test $# -gt 0 || break |
| 123 | eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1 |
| 124 | shift |
| 125 | done |
| 126 | } |
| 127 | |
| 128 | parse_commandline "$@" |
| 129 | handle_passed_args_count |
| 130 | assign_positional_args 1 "${_positionals[@]}" |
| 131 | |
| 132 | # OTHER STUFF GENERATED BY Argbash |
| 133 | |
| 134 | ### END OF CODE GENERATED BY Argbash (sortof) ### ]) |
| 135 | # [ <-- needed because of Argbash |
| 136 | |
| 137 | if [ "$_arg_debugger" == "" ]; then |
| 138 | if command -v cgdb > /dev/null 2>&1 ; then |
| 139 | debugger=cgdb |
| 140 | else |
| 141 | debugger=gdb |
| 142 | fi |
| 143 | else |
| 144 | if command -v $_arg_debugger > /dev/null 2>&1 ; then |
| 145 | debugger=$_arg_debugger |
| 146 | else |
| 147 | echo "No such debugger: $_arg_debugger" |
| 148 | exit 1 |
| 149 | fi |
| 150 | fi |
| 151 | |
| 152 | # Figure out where to place breakpoint by replacing ':' with '__' |
| 153 | bp=${_arg_testname//:/__} |
| 154 | |
| 155 | if [ "$debugger" == "lldb" ] ; then |
| 156 | echo break set -n $bp > .cgreen-debug-commands |
| 157 | echo run $_arg_library $_arg_testname >> .cgreen-debug-commands |
| 158 | $debugger cgreen-runner --source .cgreen-debug-commands |
| 159 | else |
| 160 | echo break $bp > .cgreen-debug-commands |
| 161 | echo run $_arg_library $_arg_testname >> .cgreen-debug-commands |
| 162 | $debugger -ex "set breakpoint pending on" cgreen-runner --command=.cgreen-debug-commands |
| 163 | fi |
| 164 | |
| 165 | #rm .cgreen-debug-commands |
| 166 | |
| 167 | # ] <-- needed because of Argbash |