Import gdb 7.3 into vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / cc-with-index.sh
1 #! /bin/sh
2 # Wrapper around gcc to add the .gdb_index section when running the testsuite.
3
4 # Copyright (C) 2010, 2011 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
18 # This program requires gdb and objcopy in addition to gcc.
19 # The default values are gdb from the build tree and objdump from $PATH.
20 # They may be overridden by setting environment variables GDB and OBJDUMP
21 # respectively.
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/cc-with-index.sh gcc" \
29 #   CXX_FOR_TARGET="/bin/sh $srcdir/cc-with-index.sh g++"
30 #
31 # For documentation on index files: info -f gdb.info -n "Index Files"
32
33 myname=cc-with-index.sh
34
35 if [ -z "$GDB" ]
36 then
37     if [ -f ./gdb ]
38     then
39         GDB="./gdb"
40     elif [ -f ../gdb ]
41     then
42         GDB="../gdb"
43     else
44         echo "$myname: unable to find usable gdb" >&2
45         exit 1
46     fi
47 fi
48
49 OBJCOPY=${OBJCOPY:-objcopy}
50
51 have_link=unknown
52 next_is_output_file=no
53 output_file=a.out
54
55 for arg in "$@"
56 do
57     if [ "$next_is_output_file" = "yes" ]
58     then
59         output_file="$arg"
60         next_is_output_file=no
61         continue
62     fi
63
64     # Poor man's gcc argument parser.
65     # We don't need to handle all arguments, we just need to know if we're
66     # doing a link and what the output file is.
67     # It's not perfect, but it seems to work well enough for the task at hand.
68     case "$arg" in
69     "-c") have_link=no ;;
70     "-E") have_link=no ;;
71     "-S") have_link=no ;;
72     "-o") next_is_output_file=yes ;;
73     esac
74 done
75
76 if [ "$next_is_output_file" = "yes" ]
77 then
78     echo "$myname: Unable to find output file" >&2
79     exit 1
80 fi
81
82 if [ "$have_link" = "no" ]
83 then
84     "$@"
85     exit $?
86 fi
87
88 index_file="${output_file}.gdb-index"
89 if [ -f "$index_file" ]
90 then
91     echo "$myname: Index file $index_file exists, won't clobber." >&2
92     exit 1
93 fi
94
95 output_dir="${output_file%/*}"
96 [ "$output_dir" = "$output_file" ] && output_dir="."
97
98 "$@"
99 rc=$?
100 [ $rc != 0 ] && exit $rc
101 if [ ! -f "$output_file" ]
102 then
103     echo "$myname: Internal error: $output_file missing." >&2
104     exit 1
105 fi
106
107 $GDB --batch-silent -nx -ex "file $output_file" -ex "save gdb-index $output_dir"
108 rc=$?
109 [ $rc != 0 ] && exit $rc
110
111 # GDB might not always create an index.  Cope.
112 if [ -f "$index_file" ]
113 then
114     $OBJCOPY --add-section .gdb_index="$index_file" \
115         --set-section-flags .gdb_index=readonly \
116         "$output_file" "$output_file"
117     rc=$?
118 else
119     rc=0
120 fi
121
122 rm -f "$index_file"
123 exit $rc