0

I am making a testing script which needs to check the exit code of valgrind,

valgrind exits with the exit code of the program no matter what (which is not what I need) is there a way to check if valgrind finds a memory leak or not?

It's an automated script:

#!/usr/bin/env bash

. scripts/test/noroot.lib.sh

main() {
    export __CXXFLAGS="${CXXFLAGS:-} -Og -g"

    log_file="${LOGFILE:-valgrind.log}"
    tools=(memcheck cachegrind callgrind helgrind drd massif dhat lackey none exp-bbv)

    base_cmd='valgrind --trace-children=yes --log-file=valgrind.log --verbose --leak-check=full --show-leak-kinds=all --track-origins=yes'
    end_cmd="cat -- '$log_file' && head -n 1 -- '$log_file' && exit 127"

    for tool in "${tools[@]}"; do
        log "Trying tool $tool"

        cmd_1="$base_cmd --tool='$tool' ./kos && $end_cmd"
        cmd_2="$base_cmd --tool='$tool' -s ./kos && $end_cmd"

        for cmd in "$cmd_1" "$cmd_2"; do
            log "Trying with command '$cmd'"

            export CXXFLAGS="$__CXXFLAGS"
            rm -rf ./*.out.*

            log 'Compilation'
            compile "$cmd"

            log 'Optimisation flags'
            optimising "$cmd"

            log 'Command line arguments'
            flags "$cmd"
        done

        rm -rf ./*.out.*
    done
}

main "$@"

If anyone knows if there's a flag I could pass or check some other way please reply,

thanks in advance

Ari157
  • 95
  • 4
  • 16
  • 1
    There seems to be an [`--error-exitcode`](https://stackoverflow.com/questions/19246470/how-to-get-in-script-whether-valgrind-found-memory-leaks) option. – M Oehm Mar 27 '22 at 11:45
  • @MOehm oh it works out of the box? i thought it didn't idk why, thanks :) – Ari157 Mar 27 '22 at 12:16
  • `base_cmd='...` Do not store commands in strings. Use bash arrays. Check your script with shellcheck. `end_cmd="cat -- '$log_file' &&` Use functions to store commands together for later use. Use `declare -f` to serialize a function (to text, for example) – KamilCuk Mar 27 '22 at 12:16
  • @KamilCuk I use shellcheck, it runs automatically in my vim using ALE or CoC (idk I have too many plugins) – Ari157 Mar 27 '22 at 12:20

0 Answers0