gfp-translate 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/bin/bash
  2. # Translate the bits making up a GFP mask
  3. # (c) 2009, Mel Gorman <mel@csn.ul.ie>
  4. # Licensed under the terms of the GNU GPL License version 2
  5. SOURCE=
  6. GFPMASK=none
  7. # Helper function to report failures and exit
  8. die() {
  9. echo ERROR: $@
  10. if [ "$TMPFILE" != "" ]; then
  11. rm -f $TMPFILE
  12. fi
  13. exit -1
  14. }
  15. usage() {
  16. echo "usage: gfp-translate [-h] [ --source DIRECTORY ] gfpmask"
  17. exit 0
  18. }
  19. # Parse command-line arguments
  20. while [ $# -gt 0 ]; do
  21. case $1 in
  22. --source)
  23. SOURCE=$2
  24. shift 2
  25. ;;
  26. -h)
  27. usage
  28. ;;
  29. --help)
  30. usage
  31. ;;
  32. *)
  33. GFPMASK=$1
  34. shift
  35. ;;
  36. esac
  37. done
  38. # Guess the kernel source directory if it's not set. Preference is in order of
  39. # o current directory
  40. # o /usr/src/linux
  41. if [ "$SOURCE" = "" ]; then
  42. if [ -r "/usr/src/linux/Makefile" ]; then
  43. SOURCE=/usr/src/linux
  44. fi
  45. if [ -r "`pwd`/Makefile" ]; then
  46. SOURCE=`pwd`
  47. fi
  48. fi
  49. # Confirm that a source directory exists
  50. if [ ! -r "$SOURCE/Makefile" ]; then
  51. die "Could not locate kernel source directory or it is invalid"
  52. fi
  53. # Confirm that a GFP mask has been specified
  54. if [ "$GFPMASK" = "none" ]; then
  55. usage
  56. fi
  57. # Extract GFP flags from the kernel source
  58. TMPFILE=`mktemp -t gfptranslate-XXXXXX` || exit 1
  59. grep -q ___GFP $SOURCE/include/linux/gfp.h
  60. if [ $? -eq 0 ]; then
  61. grep "^#define ___GFP" $SOURCE/include/linux/gfp.h | sed -e 's/u$//' | grep -v GFP_BITS > $TMPFILE
  62. else
  63. grep "^#define __GFP" $SOURCE/include/linux/gfp.h | sed -e 's/(__force gfp_t)//' | sed -e 's/u)/)/' | grep -v GFP_BITS | sed -e 's/)\//) \//' > $TMPFILE
  64. fi
  65. # Parse the flags
  66. IFS="
  67. "
  68. echo Source: $SOURCE
  69. echo Parsing: $GFPMASK
  70. for LINE in `cat $TMPFILE`; do
  71. MASK=`echo $LINE | awk '{print $3}'`
  72. if [ $(($GFPMASK&$MASK)) -ne 0 ]; then
  73. echo $LINE
  74. fi
  75. done
  76. rm -f $TMPFILE
  77. exit 0