| 
 | 1 | +#!/bin/bash  | 
 | 2 | +test_suite=${1:-}  | 
 | 3 | +test_number=${2:-}  | 
 | 4 | + | 
 | 5 | +PROG=${0##*/}  | 
 | 6 | +build_dir="build-ci-debug"  | 
 | 7 | + | 
 | 8 | +if [ x"$1" = x"-h" ] || [ x"$1" = x"--help" ]; then  | 
 | 9 | +    echo "Usage: $PROG [OPTION]... <test_regex> (test_number)"  | 
 | 10 | +    echo "Debug specific ctest program."  | 
 | 11 | +    echo  | 
 | 12 | +    echo "Options:"  | 
 | 13 | +    echo "  -h, --help       Display this help and exit"  | 
 | 14 | +    echo  | 
 | 15 | +    echo "Arguments:"  | 
 | 16 | +    echo "  <test_regex>     (Mandatory) Supply one regex to the script to filter tests"  | 
 | 17 | +    echo "  (test_number)    (Optional) Test number to run a specific test"  | 
 | 18 | +    echo  | 
 | 19 | +    echo "Example:"  | 
 | 20 | +    echo "  $PROG test-tokenizer"  | 
 | 21 | +    echo "  $PROG test-tokenizer 3"  | 
 | 22 | +    echo  | 
 | 23 | +    exit 0  | 
 | 24 | +fi  | 
 | 25 | + | 
 | 26 | +# Function to select and debug a test  | 
 | 27 | +function select_test() {  | 
 | 28 | +    test_suite=${1:-test}  | 
 | 29 | +    test_number=${2:-}  | 
 | 30 | + | 
 | 31 | +    # Sanity Check If Tests Is Detected  | 
 | 32 | +    printf "\n\nGathering tests that fit REGEX: ${test_suite} ...\n"  | 
 | 33 | +    tests=($(ctest -R ${test_suite} -V -N | grep -E " +Test +#[0-9]+*" | cut -d':' -f2 | awk '{$1=$1};1'))  | 
 | 34 | +    if [ ${#tests[@]} -eq 0 ]  | 
 | 35 | +    then  | 
 | 36 | +        echo "No tests avaliable... check your compliation process..."  | 
 | 37 | +        echo "Exiting."  | 
 | 38 | +        exit 1  | 
 | 39 | +    fi  | 
 | 40 | + | 
 | 41 | +    if [ -z $test_number ]  | 
 | 42 | +    then  | 
 | 43 | +        # List out avaliable tests  | 
 | 44 | +        printf "Which test would you like to debug?\n"  | 
 | 45 | +        id=0  | 
 | 46 | +        for s in "${tests[@]}"  | 
 | 47 | +        do  | 
 | 48 | +            echo "Test# ${id}"  | 
 | 49 | +            echo "  $s"  | 
 | 50 | +            ((id++))  | 
 | 51 | +        done  | 
 | 52 | + | 
 | 53 | +        # Prompt user which test they wanted to run  | 
 | 54 | +        printf "\nRun test#? "  | 
 | 55 | +        read test_number  | 
 | 56 | +    else  | 
 | 57 | +        printf "\nUser Already Requested #${test_number}"  | 
 | 58 | +    fi  | 
 | 59 | + | 
 | 60 | +    # Start GDB with the requested test binary and arguments  | 
 | 61 | +    printf "Debugging(GDB) test: ${tests[test_number]}\n"  | 
 | 62 | +    # Change IFS (Internal Field Separator)  | 
 | 63 | +    sIFS=$IFS  | 
 | 64 | +    IFS=$'\n'  | 
 | 65 | + | 
 | 66 | +    # Get test args  | 
 | 67 | +    gdb_args=($(ctest -R ${test_suite} -V -N | grep "Test command" | cut -d':' -f3 | awk '{$1=$1};1' ))  | 
 | 68 | +    IFS=$sIFS  | 
 | 69 | +    printf "Debug arguments: ${gdb_args[test_number]}\n\n"  | 
 | 70 | + | 
 | 71 | +    # Expand paths if needed  | 
 | 72 | +    args=()  | 
 | 73 | +    for x in $(echo ${gdb_args[test_number]} | sed -e 's/"\/\<//' -e 's/\>"//')  | 
 | 74 | +    do  | 
 | 75 | +        args+=($(echo $x | sed -e 's/.*\/..\//..\//'))  | 
 | 76 | +    done  | 
 | 77 | + | 
 | 78 | +    # Execute debugger  | 
 | 79 | +    echo "gdb args: ${args[@]}"  | 
 | 80 | +    gdb --args ${args[@]}  | 
 | 81 | +}  | 
 | 82 | + | 
 | 83 | +# Step 0: Check the args  | 
 | 84 | +if [ -z "$test_suite" ]  | 
 | 85 | +then  | 
 | 86 | +    echo "Usage: $PROG [OPTION]... <test_regex> (test_number)"  | 
 | 87 | +    echo "Supply one regex to the script to filter tests,"  | 
 | 88 | +    echo "and optionally a test number to run a specific test."  | 
 | 89 | +    echo "Use --help flag for full instructions"  | 
 | 90 | +    exit 1  | 
 | 91 | +fi  | 
 | 92 | + | 
 | 93 | +# Step 1: Reset and Setup folder context  | 
 | 94 | +## Sanity check that we are actually in a git repo  | 
 | 95 | +repo_root=$(git rev-parse --show-toplevel)  | 
 | 96 | +if [ ! -d "$repo_root" ]; then  | 
 | 97 | +    echo "Error: Not in a Git repository."  | 
 | 98 | +    exit 1  | 
 | 99 | +fi  | 
 | 100 | + | 
 | 101 | +## Reset folder to root context of git repo  | 
 | 102 | +pushd "$repo_root" || exit 1  | 
 | 103 | + | 
 | 104 | +## Create and enter build directory  | 
 | 105 | +rm -rf "$build_dir" && mkdir "$build_dir" || exit 1  | 
 | 106 | + | 
 | 107 | +# Step 2: Setup Build Environment and Compile Test Binaries  | 
 | 108 | +cmake -B "./$build_dir" -DCMAKE_BUILD_TYPE=Debug -DLLAMA_CUDA=1 -DLLAMA_FATAL_WARNINGS=ON || exit 1  | 
 | 109 | +pushd "$build_dir" && make -j || exit 1  | 
 | 110 | + | 
 | 111 | +# Step 3: Debug the Test  | 
 | 112 | +select_test "$test_suite" "$test_number"  | 
 | 113 | + | 
 | 114 | +# Step 4: Return to the directory from which the user ran the command.  | 
 | 115 | +popd || exit 1  | 
 | 116 | +popd || exit 1  | 
 | 117 | +popd || exit 1  | 
0 commit comments