diff --git a/docs/debugging-tests.md b/docs/debugging-tests.md index 984da80e1..18407f688 100644 --- a/docs/debugging-tests.md +++ b/docs/debugging-tests.md @@ -55,7 +55,7 @@ cmake -DCMAKE_BUILD_TYPE=Debug -DLLAMA_CUDA=1 -DLLAMA_FATAL_WARNINGS=ON .. make -j ``` -#### Step 3.1: Identify Test Command for Debugging +#### Step 3: Find all tests available that matches REGEX The output of this command will give you the command & arguments needed to run GDB. @@ -83,11 +83,13 @@ Labels: main ... ``` -So for test #1 we can tell these two pieces of relevant information: +#### Step 4: Identify Test Command for Debugging + +So for test #1 above we can tell these two pieces of relevant information: * Test Binary: `~/llama.cpp/build-ci-debug/bin/test-tokenizer-0` * Test GGUF Model: `~/llama.cpp/tests/../models/ggml-vocab-llama-spm.gguf` -#### Step 3.2: Run GDB on test command +#### Step 5: Run GDB on test command Based on the ctest 'test command' report above we can then run a gdb session via this command below: diff --git a/scripts/debug-test.sh b/scripts/debug-test.sh index 20c2f43a6..8c6fa24d5 100755 --- a/scripts/debug-test.sh +++ b/scripts/debug-test.sh @@ -8,6 +8,8 @@ red=$(tput setaf 1) green=$(tput setaf 2) yellow=$(tput setaf 3) blue=$(tput setaf 4) +magenta=$(tput setaf 5) +cyan=$(tput setaf 6) normal=$(tput sgr0) print_full_help() { @@ -39,6 +41,15 @@ EOF exit 1 } +check_dependency() { + command -v "$1" >/dev/null 2>&1 || { + abort "$1 is required but not found. Please install it and try again." + } +} + +check_dependency ctest +check_dependency cmake + if [ x"$1" = x"-h" ] || [ x"$1" = x"--help" ]; then print_full_help >&2 exit 0 @@ -60,121 +71,124 @@ shift $((OPTIND - 1)) # Step 0: Check the args if [ -z "${1}" ]; then - echo "Usage: $PROG [OPTION]... (test_number)" - echo "Supply one regex to the script to filter tests," - echo "and optionally a test number to run a specific test." - echo "Use --help flag for full instructions" - exit 1 + abort "Test regex is required" else test_suite=${1:-} fi test_number=${2:-} -# Function to select and debug a test -function select_test() { - test_suite=${1:-test} - test_number=${2:-} - repo_root=${3:-} - - # Sanity Check If Tests Is Detected - printf "\n\nGathering tests that fit REGEX: ${test_suite} ...\n" - tests=($(ctest -R ${test_suite} -V -N | grep -E " +Test +#[0-9]+*" | cut -d':' -f2 | awk '{$1=$1};1')) - if [ ${#tests[@]} -eq 0 ] - then - echo "No tests avaliable... check your compliation process..." - echo "Exiting." - exit 1 - fi - - if [ -z $test_number ] - then - # List out avaliable tests - printf "Which test would you like to debug?\n" - id=0 - for s in "${tests[@]}" - do - echo "Test# ${id}" - echo " $s" - ((id++)) - done - - # Prompt user which test they wanted to run - printf "\nRun test#? " - read test_number - else - printf "\nUser Already Requested #${test_number}\n" - fi - - # Start GDB with the requested test binary and arguments - printf "Debugging(GDB) test: ${tests[test_number]}\n" - # Change IFS (Internal Field Separator) - sIFS=$IFS - IFS=$'\n' - - # Get test args - test_args=($(ctest -R ${test_suite} -V -N | grep "Test command" | cut -d':' -f3 | awk '{$1=$1};1' )) - IFS=$sIFS - - printf "${blue}Running Test #${test_number}: ${tests[test_number]}${normal}\n" - printf "${blue}test_args[test_number]: ${test_args[test_number]}${normal}\n" - - if [ "$gdb_mode" = "true" ]; then - # Expand paths if needed - gdb_args=() - for x in $(echo ${test_args[test_number]} | sed -e 's/"\/\"//') - do - gdb_args+=($(echo $x | sed -e 's/.*\/..\//..\//')) - done - - printf "${blue}gdb_args @: ${gdb_args[@]}${normal}\n" - - # Execute debugger - pushd "$repo_root" || exit 1 - gdb --args ${gdb_args[@]} - popd > /dev/null || exit 1 - else - - # Execute Test - pushd "$repo_root" || exit 1 - eval "${test_args[test_number]}" - exit_code=$? - popd > /dev/null || exit 1 - - # Print Result - printf "${blue}Ran Test #${test_number}: ${tests[test_number]}${normal}\n" - printf "${yellow}Command: ${test_args[test_number]}${normal}\n" - if [ $exit_code -eq 0 ]; then - printf "${green}TEST PASS${normal}\n" - else - printf "${red}TEST FAIL${normal}\n" - fi - fi -} - - # Step 1: Reset and Setup folder context +######################################## + ## Sanity check that we are actually in a git repo repo_root=$(git rev-parse --show-toplevel) if [ ! -d "$repo_root" ]; then - echo "Error: Not in a Git repository." - exit 1 + abort "Not in a Git repository." fi -## Reset folder to root context of git repo -pushd "$repo_root" || exit 1 - -## Create and enter build directory -rm -rf "$build_dir" && mkdir "$build_dir" || exit 1 +## Reset folder to root context of git repo and Create and enter build directory +pushd "$repo_root" +rm -rf "$build_dir" && mkdir "$build_dir" || abort "Failed to make $build_dir" # Step 2: Setup Build Environment and Compile Test Binaries +########################################################### + # Note: test-eval-callback requires -DLLAMA_CURL -cmake -B "./$build_dir" -DCMAKE_BUILD_TYPE=Debug -DLLAMA_CUDA=1 -DLLAMA_FATAL_WARNINGS=ON -DLLAMA_CURL=1 || exit 1 -pushd "$build_dir" && make -j || exit 1 - -# Step 3: Debug the Test -select_test "$test_suite" "$test_number" "$repo_root" - -# Step 4: Return to the directory from which the user ran the command. +cmake -B "./$build_dir" -DCMAKE_BUILD_TYPE=Debug -DLLAMA_CUDA=1 -DLLAMA_FATAL_WARNINGS=ON -DLLAMA_CURL=1 || abort "Failed to build enviroment" +pushd "$build_dir" +make -j || abort "Failed to compile" popd > /dev/null || exit 1 + +# Step 3: Find all tests available that matches REGEX +#################################################### + +# Ctest Gather Tests +# `-R test-tokenizer` : looks for all the test files named `test-tokenizer*` (R=Regex) +# `-N` : "show-only" disables test execution & shows test commands that you can feed to GDB. +# `-V` : Verbose Mode +printf "\n\nGathering tests that fit REGEX: ${test_suite} ...\n" +pushd "$build_dir" +tests=($(ctest -R ${test_suite} -V -N | grep -E " +Test +#[0-9]+*" | cut -d':' -f2 | awk '{$1=$1};1')) +if [ ${#tests[@]} -eq 0 ]; then + abort "No tests avaliable... check your compliation process..." +fi +popd > /dev/null || exit 1 + +# Step 4: Identify Test Command for Debugging +############################################# + +# Select test number +if [ -z $test_number ]; then + # List out avaliable tests + printf "Which test would you like to debug?\n" + id=0 + for s in "${tests[@]}" + do + echo "Test# ${id}" + echo " $s" + ((id++)) + done + + # Prompt user which test they wanted to run + printf "\nRun test#? " + read test_number + +else + printf "\nUser Already Requested #${test_number}\n" + +fi + +# Grab all tests commands +pushd "$build_dir" +sIFS=$IFS # Save Initial IFS (Internal Field Separator) +IFS=$'\n' # Change IFS (Internal Field Separator) (So we split ctest output by newline rather than by spaces) +test_args=($(ctest -R ${test_suite} -V -N | grep "Test command" | cut -d':' -f3 | awk '{$1=$1};1' )) # Get test args +IFS=$sIFS # Reset IFS (Internal Field Separator) +popd > /dev/null || exit 1 + +# Grab specific test command +single_test_name="${tests[test_number]}" +single_test_command="${test_args[test_number]}" + +# Step 5: Execute or GDB Debug +############################## +printf "${magenta}Running Test #${test_number}: ${single_test_name}${normal}\n" +printf "${cyan}single_test_command: ${single_test_command}${normal}\n" + +if [ "$gdb_mode" = "true" ]; then + printf "${blue}Replacing single test command string that may have /../ in them with the relative path, i.e. path/../path -> ../path.${normal}\n" + gdb_args=() + for piece in $(echo ${single_test_command} | sed -e 's/"\/\"//') + do + transformed_piece=($(echo $piece | sed -e 's/.*\/..\//..\//')) + printf "${blue} - '${piece}' --> '${transformed_piece}' ${normal}\n" + gdb_args+="$transformed_piece" + done + + printf "${yellow}gdb_args[@]: ${gdb_args[@]}${normal}\n" + + # Execute debugger + pushd "$repo_root" || exit 1 + gdb --args ${gdb_args[@]} + popd > /dev/null || exit 1 + +else + # Execute Test + pushd "$repo_root" || exit 1 + eval "${single_test_command}" + exit_code=$? + popd > /dev/null || exit 1 + + # Print Result + printf "${blue}Ran Test #${test_number}: ${single_test_name}${normal}\n" + printf "${yellow}Command: ${single_test_command}${normal}\n" + if [ $exit_code -eq 0 ]; then + printf "${green}TEST PASS${normal}\n" + else + printf "${red}TEST FAIL${normal}\n" + fi + +fi + popd > /dev/null || exit 1