debug-test.sh 3.3 KB

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