fail.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #! /bin/sh
  2. progname="$0"
  3. # Print usage message
  4. function usage()
  5. {
  6. echo "Usage: ${progname} [r] <interval> <realcmd> <realarg1> ... <realargN>"
  7. echo " r: fail randomly, 1/<interval> of the time"
  8. echo " <interval>: integer periodicity of malloc failures"
  9. echo " <realcmd> <realarg1> ... <realargN>: command line into which to inject failures"
  10. echo
  11. echo " Examples:"
  12. echo " ${progname} 50 /usr/local/bin/mcell $HOME/models/nmj_recon_1/nmj_recon.main.mdl"
  13. echo " Run mcell, failing the 50-th call to malloc"
  14. echo
  15. echo " ${progname} r 50 /usr/local/bin/mcell $HOME/models/nmj_recon_1/nmj_recon.main.mdl"
  16. echo " Run mcell, failing malloc with probability 1/50 == 0.02"
  17. exit 1
  18. }
  19. # Check arguments
  20. if test $# -lt 2; then
  21. usage
  22. elif test "$1" == "r" -a $# -lt 3; then
  23. usage
  24. fi
  25. # Prepare options
  26. random=0
  27. interval=$1; shift
  28. if test "$interval" == "r"; then
  29. random=1
  30. interval=$1; shift
  31. fi
  32. shimdir="`dirname $progname`"
  33. if test -n "$MALLOC_FAIL_TRAP"; then
  34. shim="$shimdir"/malloc_fail_gdb.so
  35. else
  36. shim="$shimdir"/malloc_fail.so
  37. fi
  38. # If we can't find the shim library, first try to build it, and if that fails,
  39. # die with an error
  40. if ! test -r "$shim"; then
  41. if test -r "$shimdir/Makefile"; then
  42. tmpcwd="`pwd`"
  43. cd "$shimdir" && make
  44. cd "$tmpcwd"
  45. fi
  46. if ! test -r "$shim"; then
  47. echo "Couldn't find malloc_fail.so shim. (looking for it at ${shim})."
  48. echo "Did you remember to build the library? (run make)"
  49. exit 1
  50. fi
  51. fi
  52. if test "$random" == "1"; then
  53. echo "Expect random failure of 1 out of every $interval calls to malloc:"
  54. else
  55. echo "Expect failure in $interval-th call to malloc:"
  56. fi
  57. exec /usr/bin/env LD_PRELOAD="$shim" INJECT_MALLOC_FAIL_RANDOM="$random" INJECT_MALLOC_FAIL_INTERVAL="$interval" "$@"