-
Notifications
You must be signed in to change notification settings - Fork 15
/
grade-functions.sh
258 lines (227 loc) · 4.42 KB
/
grade-functions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
verbose=false
if [ "x$1" = "x-v" ]
then
verbose=true
out=/dev/stdout
err=/dev/stderr
else
out=/dev/null
err=/dev/null
fi
if gmake --version >/dev/null 2>&1; then make=gmake; else make=make; fi
#
# QEMU
#
timeout=30
preservefs=n
qemu=`$make -s --no-print-directory print-qemu`
gdbport=`$make -s --no-print-directory print-gdbport`
brkfn=readline
echo_n () {
# suns can't echo -n, and Mac OS X can't echo "x\c"
# assume argument has no doublequotes
awk 'BEGIN { printf("'"$*"'"); }' </dev/null
}
# Run QEMU with serial output redirected to jos.out. If $brkfn is
# non-empty, wait until $brkfn is reached or $timeout expires, then
# kill QEMU.
run () {
qemuextra=
if [ "$brkfn" ]; then
qemuextra="-S -gdb tcp::$gdbport"
fi
qemucommand="$qemu -nographic $qemuopts -serial file:jos.out -monitor null -no-reboot $qemuextra"
# qemucommand="$qemu -nographic $qemuopts -serial file:jos.out -no-reboot"
if $verbose; then
echo $qemucommand 1>&2
fi
t0=`date +%s.%N 2>/dev/null`
# t=`date +%s.%N 2>/dev/null`
(
ulimit -t $timeout
exec $qemucommand
) >$out 2>$err &
PID=$!
# Wait for QEMU to start
sleep 1
if [ "$brkfn" ]; then
# Find the address of the kernel $brkfn function,
# which is typically what the kernel monitor uses to
# read commands interactively.
brkaddr=`grep " $brkfn\$" obj/kern/kernel.sym | sed -e's/ .*$//g'`
(
echo "target remote localhost:$gdbport"
echo "br *0x$brkaddr"
echo c
) > jos.in
gdb -batch -nx -x jos.in > /dev/null 2>&1
# Make sure QEMU is dead. On OS X, exiting gdb
# doesn't always exit QEMU.
kill $PID > /dev/null 2>&1
fi
}
#
# Scoring
#
pts=5
part=0
partpos=0
total=0
totalpos=0
showpart () {
echo "Part $1 score: $part/$partpos"
echo
total=`expr $total + $part`
totalpos=`expr $totalpos + $partpos`
part=0
partpos=0
}
showfinal () {
total=`expr $total + $part`
totalpos=`expr $totalpos + $partpos`
echo "Score: $total/$totalpos"
if [ $total -lt $totalpos ]; then
exit 1
fi
}
passfailmsg () {
msg="$1"
shift
if [ $# -gt 0 ]; then
msg="$msg,"
fi
t1=`date +%s.%N 2>/dev/null`
time=`echo "scale=1; ($t1-$t0)/1" | sed 's/.N/.0/g' | bc 2>/dev/null`
echo $msg "$@" "(${time}s)"
}
pass () {
passfailmsg OK "$@"
part=`expr $part + $pts`
partpos=`expr $partpos + $pts`
}
fail () {
passfailmsg WRONG "$@"
partpos=`expr $partpos + $pts`
if $verbose; then
exit 1
fi
}
#
# User tests
#
# Usage: runtest <tagname> <defs> <check fn> <check args...>
runtest () {
perl -e "print '$1: '"
rm -f obj/kern/init.o obj/kern/kernel obj/kern/kernel.img
[ "$preservefs" = y ] || rm -f obj/fs/fs.img
if $verbose
then
echo "$make $2... "
fi
$make $2 >$out
if [ $? -ne 0 ]
then
rm -f obj/kern/init.o
echo $make $2 failed
exit 1
fi
# We just built a weird init.o that runs a specific test. As
# a result, 'make qemu' will run the last graded test and
# 'make clean; make qemu' will run the user-specified
# environment. Remove our weird init.o to fix this.
rm -f obj/kern/init.o
run
# Give qemu some more time to run (for asynchronous mode).
# This way, we get the small 1 second wait for most tests
# and a longer wait (5 seconds) in case qemu needs that
# time to load.
if [ ! -s jos.out ]
then
sleep 4
fi
if [ ! -s jos.out ]
then
fail > /dev/null # Still increment number of possible points
echo 'no jos.out'
else
shift
shift
check=$1
shift
$check "$@"
fi
}
quicktest () {
perl -e "print '$1: '"
shift
checkregexps "$@"
}
checkregexps () {
okay=yes
not=false
for i
do
if [ "x$i" = "x!" ]
then
not=true
elif $not
then
if egrep "^$i\$" jos.out >/dev/null
then
echo "got unexpected line '$i'"
okay=no
fi
not=false
else
egrep "^$i\$" jos.out >/dev/null
if [ $? -ne 0 ]
then
echo "missing '$i'"
okay=no
fi
not=false
fi
done
if [ "$okay" = "yes" ]
then
pass
else
fail
fi
}
# Usage: runtest1 [-tag <tagname>] [-dir <dirname>] <progname> [-Ddef...] [-check checkfn] checkargs...
runtest1 () {
tag=
dir=user
check=checkregexps
while true; do
if [ $1 = -tag ]
then
tag=$2
elif [ $1 = -dir ]
then
dir=$2
else
break
fi
shift
shift
done
prog=$1
shift
if [ "x$tag" = x ]
then
tag=$prog
fi
runtest1_defs=
while expr "x$1" : 'x-D.*' >/dev/null; do
runtest1_defs="DEFS+='$1' $runtest1_defs"
shift
done
if [ "x$1" = "x-check" ]; then
check=$2
shift
shift
fi
runtest "$tag" "DEFS='-DTEST=${dir}_${prog}' $runtest1_defs" "$check" "$@"
}