gdb/contrib/cc-with-tweaks.sh - gdb

  1. #! /bin/sh
  2. # Wrapper around gcc to tweak the output in various ways when running
  3. # the testsuite.

  4. # Copyright (C) 2010-2015 Free Software Foundation, Inc.
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program.  If not, see <http://www.gnu.org/licenses/>.

  17. # This program requires gdb and objcopy in addition to gcc.
  18. # The default values are gdb from the build tree and objcopy from $PATH.
  19. # They may be overridden by setting environment variables GDB and OBJCOPY
  20. # respectively.  Note that GDB should contain the gdb binary as well as the
  21. # -data-directory flag, e.g., "foo/gdb -data-directory foo/data-directory".
  22. # We assume the current directory is either $obj/gdb or $obj/gdb/testsuite.
  23. #
  24. # Example usage:
  25. #
  26. # bash$ cd $objdir/gdb/testsuite
  27. # bash$ runtest \
  28. #   CC_FOR_TARGET="/bin/sh $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS gcc" \
  29. #   CXX_FOR_TARGET="/bin/sh $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS g++"
  30. #
  31. # For documentation on Fission and dwp files:
  32. #     http://gcc.gnu.org/wiki/DebugFission
  33. #     http://gcc.gnu.org/wiki/DebugFissionDWP
  34. # For documentation on index files: info -f gdb.info -n "Index Files"
  35. # For information about 'dwz', see the announcement:
  36. #     http://gcc.gnu.org/ml/gcc/2012-04/msg00686.html
  37. # (More documentation is to come.)

  38. # ARGS determine what is done.  They can be:
  39. # -Z invoke objcopy --compress-debug-sections
  40. # -z compress using dwz
  41. # -m compress using dwz -m
  42. # -i make an index
  43. # -p create .dwp files (Fission), you need to also use gcc option -gsplit-dwarf
  44. # If nothing is given, no changes are made

  45. myname=cc-with-tweaks.sh

  46. if [ -z "$GDB" ]
  47. then
  48.     if [ -f ./gdb ]
  49.     then
  50.         GDB="./gdb -data-directory data-directory"
  51.     elif [ -f ../gdb ]
  52.     then
  53.         GDB="../gdb -data-directory ../data-directory"
  54.     elif [ -f ../../gdb ]
  55.     then
  56.         GDB="../../gdb -data-directory ../../data-directory"
  57.     else
  58.         echo "$myname: unable to find usable gdb" >&2
  59.         exit 1
  60.     fi
  61. fi

  62. OBJCOPY=${OBJCOPY:-objcopy}
  63. READELF=${READELF:-readelf}

  64. DWZ=${DWZ:-dwz}
  65. DWP=${DWP:-dwp}

  66. have_link=unknown
  67. next_is_output_file=no
  68. output_file=a.out

  69. want_index=false
  70. want_dwz=false
  71. want_multi=false
  72. want_dwp=false
  73. want_objcopy_compress=false

  74. while [ $# -gt 0 ]; do
  75.     case "$1" in
  76.         -Z) want_objcopy_compress=true ;;
  77.         -z) want_dwz=true ;;
  78.         -i) want_index=true ;;
  79.         -m) want_multi=true ;;
  80.         -p) want_dwp=true ;;
  81.         *) break ;;
  82.     esac
  83.     shift
  84. done

  85. for arg in "$@"
  86. do
  87.     if [ "$next_is_output_file" = "yes" ]
  88.     then
  89.         output_file="$arg"
  90.         next_is_output_file=no
  91.         continue
  92.     fi

  93.     # Poor man's gcc argument parser.
  94.     # We don't need to handle all arguments, we just need to know if we're
  95.     # doing a link and what the output file is.
  96.     # It's not perfect, but it seems to work well enough for the task at hand.
  97.     case "$arg" in
  98.     "-c") have_link=no ;;
  99.     "-E") have_link=no ;;
  100.     "-S") have_link=no ;;
  101.     "-o") next_is_output_file=yes ;;
  102.     esac
  103. done

  104. if [ "$next_is_output_file" = "yes" ]
  105. then
  106.     echo "$myname: Unable to find output file" >&2
  107.     exit 1
  108. fi

  109. if [ "$have_link" = "no" ]
  110. then
  111.     "$@"
  112.     exit $?
  113. fi

  114. index_file="${output_file}.gdb-index"
  115. if [ "$want_index" = true ] && [ -f "$index_file" ]
  116. then
  117.     echo "$myname: Index file $index_file exists, won't clobber." >&2
  118.     exit 1
  119. fi

  120. output_dir="${output_file%/*}"
  121. [ "$output_dir" = "$output_file" ] && output_dir="."

  122. "$@"
  123. rc=$?
  124. [ $rc != 0 ] && exit $rc
  125. if [ ! -f "$output_file" ]
  126. then
  127.     echo "$myname: Internal error: $output_file missing." >&2
  128.     exit 1
  129. fi

  130. if [ "$want_objcopy_compress" = true ]; then
  131.     $OBJCOPY --compress-debug-sections "$output_file"
  132.     rc=$?
  133.     [ $rc != 0 ] && exit $rc
  134. fi

  135. if [ "$want_index" = true ]; then
  136.     $GDB --batch-silent -nx -ex "set auto-load no" -ex "file $output_file" -ex "save gdb-index $output_dir"
  137.     rc=$?
  138.     [ $rc != 0 ] && exit $rc

  139.     # GDB might not always create an index.  Cope.
  140.     if [ -f "$index_file" ]
  141.     then
  142.         $OBJCOPY --add-section .gdb_index="$index_file" \
  143.             --set-section-flags .gdb_index=readonly \
  144.             "$output_file" "$output_file"
  145.         rc=$?
  146.     else
  147.         rc=0
  148.     fi
  149.     [ $rc != 0 ] && exit $rc
  150. fi

  151. if [ "$want_dwz" = true ]; then
  152.     $DWZ "$output_file" > /dev/null 2>&1
  153. elif [ "$want_multi" = true ]; then
  154.     cp $output_file ${output_file}.alt
  155.     $DWZ -m ${output_file}.dwz "$output_file" ${output_file}.alt > /dev/null 2>&1
  156. fi

  157. if [ "$want_dwp" = true ]; then
  158.     dwo_files=$($READELF -wi "${output_file}" | grep _dwo_name | \
  159.         sed -e 's/^.*: //' | sort | uniq)
  160.     rc=0
  161.     if [ -n "$dwo_files" ]; then
  162.         $DWP -o "${output_file}.dwp" ${dwo_files} > /dev/null
  163.         rc=$?
  164.         [ $rc != 0 ] && exit $rc
  165.         rm -f ${dwo_files}
  166.     fi
  167. fi

  168. rm -f "$index_file"
  169. exit $rc