kgdb: Wrap gdb with a nice set of getopt flags

We don't always need sel4 extensions, and symbol autoswitching can get us into
trouble, so this rewrites kgdb to use getopt and allow us to control which flags
are enabled/disabled at will.

Change-Id: I8acd8672570d6342827b44af9e725a2cca582006
diff --git a/kgdb.sh b/kgdb.sh
index 6f0ab4e..b3a745a 100755
--- a/kgdb.sh
+++ b/kgdb.sh
@@ -18,22 +18,75 @@
 KATA_OUT=out/kata/${TARGET}/debug
 MATCHA_OUT=out/matcha/riscv32imc-unknown-none-elf/debug
 
+USE_SEL4_EXTENSIONS="true"
+USE_SEL4_SYMBOL_AUTOSWITCHING="false"
+
 export SOURCE_DIR=${ROOTDIR}/kata
 export BUILD_DIR=$KATA_OUT
 
-# NB: -q suppresses the banner to workaround the banner msg triggering the pager
-# NB: auto-start cpu0 & cpu1 but leave cpu2 (VC) halted
-exec "${GDB}" -q -cd "${ROOTDIR}" \
-  -ex "set pagination off" \
-  -ex "directory sw/tock" \
-  -ex "file ${PROGRAM}" \
-  -ex "set confirm off" \
-  -ex "add-symbol-file ${PROGRAM}" \
-  -ex "add-symbol-file ${MATCHA_OUT}/matcha_platform" \
-  -ex "add-symbol-file ${MATCHA_OUT}/matcha_app" \
-  -ex "set pagination on" \
-  -ex "target remote ${REMOTE}" \
-  -ex "monitor cpu0 IsHalted false" \
-  -ex "monitor cpu1 CreateSeL4 0xffffffee" \
-  -ex "source sim/renode/tools/sel4_extensions/gdbscript.py" \
-  -ex "sel4 symbol-autoswitching false"
+function parseargv {
+    local usage="Usage: kgdb.sh [-h|--help] [-S|--no-sel4-extensions] [-a|--sel4-symbol-autoswitching]"
+    local args=$(getopt -o hSa --long no-sel4-extensions,symbol-autoswitching,help -n kgdb.sh -- "$@")
+
+    set -- $args
+
+    for i; do
+        case "$1" in
+            -S|--no-sel4-extensions)
+                echo "*** Disabling sel4 extensions"
+                USE_SEL4_EXTENSIONS="false"
+                shift
+                ;;
+
+            -a|--symbol-autoswitching)
+                echo "*** Enabling sel4 symbol autoswitching"
+                echo "*** Warning: this can cause unexpected behaviors."
+                USE_SEL4_EXTENSIONS="true"
+                USE_SEL4_SYMBOL_AUTOSWITCHING="true"
+                shift
+                ;;
+
+            --)
+                shift
+                break
+                ;;
+
+            -h|--help|*)
+                echo "$usage" >/dev/stderr
+                exit 1
+                ;;
+        esac
+    done
+}
+
+function main {
+    local -a gdbargs=(
+         -ex "set pagination off"
+         -ex "directory sw/tock"
+         -ex "file ${PROGRAM}"
+         -ex "set confirm off"
+         -ex "add-symbol-file ${PROGRAM}"
+         -ex "add-symbol-file ${MATCHA_OUT}/matcha_platform"
+         -ex "add-symbol-file ${MATCHA_OUT}/matcha_app"
+         -ex "set pagination on"
+         -ex "target remote ${REMOTE}"
+         -ex "monitor cpu0 IsHalted false"
+    )
+
+    parseargv "$@"
+
+    if [[ "${USE_SEL4_EXTENSIONS}" == "true" ]]; then
+        gdbargs+=(
+            -ex "monitor cpu1 CreateSeL4 0xffffffee"
+            -ex "source sim/renode/tools/sel4_extensions/gdbscript.py"
+            -ex "sel4 symbol-autoswitching ${USE_SEL4_SYMBOL_AUTOSWITCHING}"
+        )
+    fi
+
+
+    # NB: -q suppresses the banner to workaround the banner msg triggering the pager
+    # NB: auto-start cpu0 & cpu1 but leave cpu2 (VC) halted
+    exec "${GDB}" -q -cd "${ROOTDIR}" "${gdbargs[@]}"
+}
+
+main "$@"