]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - script/test.bash
Import Intel Processor Trace library.
[FreeBSD/FreeBSD.git] / script / test.bash
1 #! /bin/bash
2 #
3 # Copyright (c) 2013-2018, Intel Corporation
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 #
8 #  * Redistributions of source code must retain the above copyright notice,
9 #    this list of conditions and the following disclaimer.
10 #  * Redistributions in binary form must reproduce the above copyright notice,
11 #    this list of conditions and the following disclaimer in the documentation
12 #    and/or other materials provided with the distribution.
13 #  * Neither the name of Intel Corporation nor the names of its contributors
14 #    may be used to endorse or promote products derived from this software
15 #    without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28 #
29 # This script executes ptt tests and compares the output of tools, like
30 # ptxed or ptdump, with the expected output from the ptt testfile.
31
32 info() {
33         [[ $verbose != 0 ]] && echo -e "$@" >&2
34 }
35
36 run() {
37         info "$@"
38         "$@"
39 }
40
41 asm2addr() {
42         local line
43         line=`grep -i ^org "$1"`
44         [[ $? != 0 ]] && return $?
45         echo $line | sed "s/org *//"
46 }
47
48 usage() {
49         cat <<EOF
50 usage: $0 [<options>] <pttfile>...
51
52 options:
53   -h            this text
54   -v            print commands as they are executed
55   -c cpu[,cpu]  comma-separated list of cpu's for the tests (see pttc -h, for valid values)
56   -f            exit with 1 if any of the tests failed
57   -l            only list .diff files
58   -g            specify the pttc command (default: pttc)
59   -G            specify additional arguments to pttc
60   -d            specify the ptdump command (default: ptdump)
61   -D            specify additional arguments to ptdump
62   -x            specify the ptxed command (default: ptxed)
63   -X            specify additional arguments to ptxed
64
65   <pttfile>     annotated yasm file ending in .ptt
66 EOF
67 }
68
69 pttc_cmd=pttc
70 pttc_arg=""
71 ptdump_cmd=ptdump
72 ptdump_arg=""
73 ptxed_cmd=ptxed
74 ptxed_arg=""
75 exit_fails=0
76 list=0
77 verbose=0
78 while getopts "hvc:flg:G:d:D:x:X:" option; do
79         case $option in
80         h)
81                 usage
82                 exit 0
83                 ;;
84         v)
85                 verbose=1
86                 ;;
87         c)
88                 cpus=`echo $OPTARG | sed "s/,/ /g"`
89                 ;;
90         f)
91                 exit_fails=1
92                 ;;
93         l)
94                 list=1
95                 ;;
96         g)
97                 pttc_cmd=$OPTARG
98                 ;;
99         G)
100                 pttc_arg=$OPTARG
101                 ;;
102         d)
103                 ptdump_cmd=$OPTARG
104                 ;;
105         D)
106                 ptdump_arg=$OPTARG
107                 ;;
108         x)
109                 ptxed_cmd=$OPTARG
110                 ;;
111         X)
112                 ptxed_arg=$OPTARG
113                 ;;
114         \?)
115                 exit 1
116                 ;;
117         esac
118 done
119
120 shift $(($OPTIND-1))
121
122 if [[ $# == 0 ]]; then
123         usage
124         exit 1
125 fi
126
127 # the exit status
128 status=0
129
130 ptt-ptdump-opts() {
131         sed -n 's/[ \t]*;[ \t]*opt:ptdump[ \t][ \t]*\(.*\)[ \t]*/\1/p' "$1"
132 }
133
134 ptt-ptxed-opts() {
135         sed -n 's/[ \t]*;[ \t]*opt:ptxed[ \t][ \t]*\(.*\)[ \t]*/\1/p' "$1"
136 }
137
138 run-ptt-test() {
139         info "\n# run-ptt-test $@"
140
141         ptt="$1"
142         cpu="$2"
143         base=`basename "${ptt%%.ptt}"`
144
145         if [[ -n "$cpu" ]]; then
146                 cpu="--cpu $cpu"
147         fi
148
149         # the following are the files that are generated by pttc
150         pt=$base.pt
151         bin=$base.bin
152         lst=$base.lst
153
154
155         # execute pttc - remove the extra \r in Windows line endings
156         files=`run "$pttc_cmd" $pttc_arg $cpu "$ptt" | sed 's/\r\n/\n/g'`
157         ret=$?
158         if [[ $ret != 0 ]]; then
159                 echo "$ptt: $pttc_cmd $pttc_arg failed with $ret" >&2
160                 status=1
161                 return
162         fi
163
164         exps=""
165         sb=""
166         for file in $files; do
167                 case $file in
168                 *.sb)
169                         sb_base=${file%.sb}
170                         sb_part=${sb_base#$base-}
171                         sb_prefix=${sb_part%%,*}
172                         sb_options=${sb_part#$sb_prefix}
173                         sb_prio=${sb_prefix##*-}
174                         sb_prefix2=${sb_prefix%-$sb_prio}
175                         sb_format=${sb_prefix2##*-}
176
177                         sb+=`echo $sb_options | sed -e "s/,/ --$sb_format:/g" -e "s/=/ /g"`
178                         sb+=" --$sb_format:$sb_prio $file"
179                         ;;
180                 *.exp)
181                         exps+=" $file"
182                         ;;
183                 *)
184                         echo "$ptt: unexpected $pttc_cmd output '$file'"
185                         status=1
186                         continue
187                         ;;
188                 esac
189         done
190
191         if [[ -z $exps ]]; then
192                 echo "$ptt: $pttc_cmd $pttc_arg did not produce any .exp file" >&2
193                 status=1
194                 return
195         fi
196
197         # loop over all .exp files determine the tool, generate .out
198         # files and compare .exp and .out file with diff.
199         # all differences will be
200         for exp in $exps; do
201                 exp_base=${exp%%.exp}
202                 out=$exp_base.out
203                 diff=$exp_base.diff
204                 tool=${exp_base##$base-}
205                 tool=${tool%%-cpu_*}
206                 case $tool in
207                 ptxed)
208                         addr=`asm2addr "$ptt"`
209                         if [[ $? != 0 ]]; then
210                                 echo "$ptt: org directive not found in test file" >&2
211                                 status=1
212                                 continue
213                         fi
214                         local opts=`ptt-ptxed-opts "$ptt"`
215                         opts+=" --no-inst --check"
216                         run "$ptxed_cmd" $ptxed_arg --raw $bin:$addr $cpu $opts --pt $pt $sb > $out
217                         ;;
218                 ptdump)
219                         local opts=`ptt-ptdump-opts "$ptt"`
220                         run "$ptdump_cmd" $ptdump_arg $cpu $opts $sb $pt > $out
221                         ;;
222                 *)
223                         echo "$ptt: unknown tool '$tool'"
224                         status=1
225                         continue
226                         ;;
227                 esac
228                 if run diff -ub $exp $out > $diff; then
229                         run rm $diff
230                 else
231                         if [[ $exit_fails != 0 ]]; then
232                                 status=1
233                         fi
234
235                         if [[ $list != 0 ]]; then
236                                 echo $diff
237                         else
238                                 cat $diff
239                         fi
240                 fi
241         done
242 }
243
244 ptt-cpus() {
245         sed -n 's/[ \t]*;[ \t]*cpu[ \t][ \t]*\(.*\)[ \t]*/\1/p' "$1"
246 }
247
248 run-ptt-tests() {
249         local ptt="$1"
250         local cpus=$cpus
251
252         # if no cpus are given on the command-line,
253         # use the cpu directives from the pttfile.
254         if [[ -z $cpus ]]; then
255                 cpus=`ptt-cpus "$ptt"`
256         fi
257
258         # if there are no cpu directives in the pttfile,
259         # run the test without any cpu settings.
260         if [[ -z $cpus ]]; then
261                 run-ptt-test "$ptt"
262                 return
263         fi
264
265         # otherwise run for each cpu the test.
266         for i in $cpus; do
267                 run-ptt-test "$ptt" $i
268         done
269 }
270
271 for ptt in "$@"; do
272         run-ptt-tests "$ptt"
273 done
274
275 exit $status