parse_valgrind_supp.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #! /usr/bin/awk -f
  2. # Copyright: 2022 David Hart
  3. # Licence: wxWindows licence
  4. #
  5. # A script to extract the actual suppression info from the output of (for example) valgrind --leak-check=full --show-reachable=yes --error-limit=no --gen-suppressions=all ./minimal
  6. # The desired bits are between ^{ and ^} (including the braces themselves).
  7. # The combined output should either be appended to /usr/lib/valgrind/default.supp, or placed in a .supp of its own
  8. # If the latter, either tell valgrind about it each time with --suppressions=<filename>, or add that line to ~/.valgrindrc
  9. # NB This script uses the |& operator, which I believe is gawk-specific. In case of failure, check that you're using gawk rather than some other awk
  10. # The script looks for suppressions. When it finds one it stores it temporarily in an array,
  11. # and also feeds it line by line to the external app 'md5sum' which generates a unique checksum for it.
  12. # The checksum is used as an index in a different array. If an item with that index already exists the suppression must be a duplicate and is discarded.
  13. BEGIN { suppression=0; md5sum = "md5sum" }
  14. # If the line begins with '{', it's the start of a supression; so set the var and initialise things
  15. /^{/ {
  16. suppression=1; i=0; next
  17. }
  18. # If the line begins with '}' its the end of a suppression
  19. /^}/ {
  20. if (suppression)
  21. { suppression=0;
  22. close(md5sum, "to") # We've finished sending data to md5sum, so close that part of the pipe
  23. ProcessInput() # Do the slightly-complicated stuff in functions
  24. delete supparray # We don't want subsequent suppressions to append to it!
  25. }
  26. }
  27. # Otherwise, it's a normal line. If we're inside a supression, store it, and pipe it to md5sum. Otherwise it's cruft, so ignore it
  28. { if (suppression)
  29. {
  30. supparray[++i] = $0
  31. print |& md5sum
  32. }
  33. }
  34. function ProcessInput()
  35. {
  36. # Pipe the result from md5sum, then close it
  37. md5sum |& getline result
  38. close(md5sum)
  39. # gawk can't cope with enormous ints like $result would be, so stringify it first by prefixing a definite string
  40. resultstring = "prefix"result
  41. if (! (resultstring in chksum_array) )
  42. { chksum_array[resultstring] = 0; # This checksum hasn't been seen before, so add it to the array
  43. OutputSuppression() # and output the contents of the suppression
  44. }
  45. }
  46. function OutputSuppression()
  47. {
  48. # A suppression is surrounded by '{' and '}'. Its data was stored line by line in the array
  49. print "{"
  50. for (n=1; n <= i; ++n)
  51. { print supparray[n] }
  52. print "}"
  53. }