blob: 04a7bb573daa4f0ee1188860caaa159a9c70b749 [file] [log] [blame]
Steven Rostedt2545eb62010-11-02 15:01:32 -04001#!/usr/bin/perl -w
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002#
Uwe Kleine-Königcce1dac2011-01-24 21:12:01 +01003# Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04004# Licensed under the terms of the GNU GPL License version 2
5#
Steven Rostedt2545eb62010-11-02 15:01:32 -04006
7use strict;
8use IPC::Open2;
9use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Steven Rostedt7faafbd2010-11-02 14:58:22 -040010use File::Path qw(mkpath);
11use File::Copy qw(cp);
Steven Rostedt2545eb62010-11-02 15:01:32 -040012use FileHandle;
13
Steven Rostedte48c5292010-11-02 14:35:37 -040014my $VERSION = "0.2";
15
Steven Rostedt2545eb62010-11-02 15:01:32 -040016$| = 1;
17
18my %opt;
Steven Rostedta57419b2010-11-02 15:13:54 -040019my %repeat_tests;
20my %repeats;
Steven Rostedta75fece2010-11-02 14:58:27 -040021my %default;
Steven Rostedt2545eb62010-11-02 15:01:32 -040022
23#default opts
Steven Rostedta57419b2010-11-02 15:13:54 -040024$default{"NUM_TESTS"} = 1;
Steven Rostedtbb8474b2011-11-23 15:58:00 -050025$default{"TEST_TYPE"} = "build";
Steven Rostedta75fece2010-11-02 14:58:27 -040026$default{"BUILD_TYPE"} = "randconfig";
27$default{"MAKE_CMD"} = "make";
28$default{"TIMEOUT"} = 120;
Steven Rostedt48920632011-06-14 20:42:19 -040029$default{"TMP_DIR"} = "/tmp/ktest/\${MACHINE}";
Steven Rostedta75fece2010-11-02 14:58:27 -040030$default{"SLEEP_TIME"} = 60; # sleep time between tests
31$default{"BUILD_NOCLEAN"} = 0;
32$default{"REBOOT_ON_ERROR"} = 0;
33$default{"POWEROFF_ON_ERROR"} = 0;
34$default{"REBOOT_ON_SUCCESS"} = 1;
35$default{"POWEROFF_ON_SUCCESS"} = 0;
36$default{"BUILD_OPTIONS"} = "";
37$default{"BISECT_SLEEP_TIME"} = 60; # sleep time between bisects
Steven Rostedt27d934b2011-05-20 09:18:18 -040038$default{"PATCHCHECK_SLEEP_TIME"} = 60; # sleep time between patch checks
Steven Rostedta75fece2010-11-02 14:58:27 -040039$default{"CLEAR_LOG"} = 0;
Steven Rostedtc960bb92011-03-08 09:22:39 -050040$default{"BISECT_MANUAL"} = 0;
Steven Rostedtc23dca72011-03-08 09:26:31 -050041$default{"BISECT_SKIP"} = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -040042$default{"SUCCESS_LINE"} = "login:";
Steven Rostedtf1a5b962011-06-13 10:30:00 -040043$default{"DETECT_TRIPLE_FAULT"} = 1;
Steven Rostedte0a87422011-09-30 17:50:48 -040044$default{"NO_INSTALL"} = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -040045$default{"BOOTED_TIMEOUT"} = 1;
46$default{"DIE_ON_FAILURE"} = 1;
Steven Rostedte48c5292010-11-02 14:35:37 -040047$default{"SSH_EXEC"} = "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND";
48$default{"SCP_TO_TARGET"} = "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE";
49$default{"REBOOT"} = "ssh \$SSH_USER\@\$MACHINE reboot";
Steven Rostedt1c8a6172010-11-09 12:55:40 -050050$default{"STOP_AFTER_SUCCESS"} = 10;
51$default{"STOP_AFTER_FAILURE"} = 60;
Steven Rostedt2d01b262011-03-08 09:47:54 -050052$default{"STOP_TEST_AFTER"} = 600;
Steven Rostedt600bbf02011-11-21 20:12:04 -050053
54# required, and we will ask users if they don't have them but we keep the default
55# value something that is common.
56$default{"REBOOT_TYPE"} = "grub";
Steven Rostedt8d1491b2010-11-18 15:39:48 -050057$default{"LOCALVERSION"} = "-test";
Steven Rostedt600bbf02011-11-21 20:12:04 -050058$default{"SSH_USER"} = "root";
59$default{"BUILD_TARGET"} = "arch/x86/boot/bzImage";
60$default{"TARGET_IMAGE"} = "/boot/vmlinuz-test";
Steven Rostedt2545eb62010-11-02 15:01:32 -040061
Steven Rostedt8d1491b2010-11-18 15:39:48 -050062my $ktest_config;
Steven Rostedt2545eb62010-11-02 15:01:32 -040063my $version;
Steven Rostedta75fece2010-11-02 14:58:27 -040064my $machine;
Steven Rostedte48c5292010-11-02 14:35:37 -040065my $ssh_user;
Steven Rostedta75fece2010-11-02 14:58:27 -040066my $tmpdir;
67my $builddir;
68my $outputdir;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -050069my $output_config;
Steven Rostedta75fece2010-11-02 14:58:27 -040070my $test_type;
Steven Rostedt7faafbd2010-11-02 14:58:22 -040071my $build_type;
Steven Rostedta75fece2010-11-02 14:58:27 -040072my $build_options;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -040073my $pre_build;
74my $post_build;
75my $pre_build_die;
76my $post_build_die;
Steven Rostedta75fece2010-11-02 14:58:27 -040077my $reboot_type;
78my $reboot_script;
79my $power_cycle;
Steven Rostedte48c5292010-11-02 14:35:37 -040080my $reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -040081my $reboot_on_error;
82my $poweroff_on_error;
83my $die_on_failure;
Steven Rostedt576f6272010-11-02 14:58:38 -040084my $powercycle_after_reboot;
85my $poweroff_after_halt;
Steven Rostedte48c5292010-11-02 14:35:37 -040086my $ssh_exec;
87my $scp_to_target;
Steven Rostedta75fece2010-11-02 14:58:27 -040088my $power_off;
89my $grub_menu;
Steven Rostedt2545eb62010-11-02 15:01:32 -040090my $grub_number;
91my $target;
92my $make;
Steven Rostedt8b37ca82010-11-02 14:58:33 -040093my $post_install;
Steven Rostedte0a87422011-09-30 17:50:48 -040094my $no_install;
Steven Rostedt5c42fc52010-11-02 14:57:01 -040095my $noclean;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -040096my $minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -040097my $start_minconfig;
Steven Rostedt35ce5952011-07-15 21:57:25 -040098my $start_minconfig_defined;
Steven Rostedt4c4ab122011-07-15 21:16:17 -040099my $output_minconfig;
100my $ignore_config;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -0400101my $addconfig;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400102my $in_bisect = 0;
103my $bisect_bad = "";
Steven Rostedtd6ce2a02010-11-02 14:58:05 -0400104my $reverse_bisect;
Steven Rostedtc960bb92011-03-08 09:22:39 -0500105my $bisect_manual;
Steven Rostedtc23dca72011-03-08 09:26:31 -0500106my $bisect_skip;
Steven Rostedt30f75da2011-06-13 10:35:35 -0400107my $config_bisect_good;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400108my $in_patchcheck = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400109my $run_test;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400110my $redirect;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400111my $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +0530112my $testlog;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400113my $dmesg;
114my $monitor_fp;
115my $monitor_pid;
116my $monitor_cnt = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -0400117my $sleep_time;
118my $bisect_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -0400119my $patchcheck_sleep_time;
Steven Rostedt19902072011-06-14 20:46:25 -0400120my $ignore_warnings;
Steven Rostedta75fece2010-11-02 14:58:27 -0400121my $store_failures;
Rabin Vincentde5b6e32011-11-18 17:05:31 +0530122my $store_successes;
Steven Rostedt9064af52011-06-13 10:38:48 -0400123my $test_name;
Steven Rostedta75fece2010-11-02 14:58:27 -0400124my $timeout;
125my $booted_timeout;
Steven Rostedtf1a5b962011-06-13 10:30:00 -0400126my $detect_triplefault;
Steven Rostedta75fece2010-11-02 14:58:27 -0400127my $console;
Steven Rostedt2b803362011-09-30 18:00:23 -0400128my $reboot_success_line;
Steven Rostedta75fece2010-11-02 14:58:27 -0400129my $success_line;
Steven Rostedt1c8a6172010-11-09 12:55:40 -0500130my $stop_after_success;
131my $stop_after_failure;
Steven Rostedt2d01b262011-03-08 09:47:54 -0500132my $stop_test_after;
Steven Rostedta75fece2010-11-02 14:58:27 -0400133my $build_target;
134my $target_image;
135my $localversion;
Steven Rostedt576f6272010-11-02 14:58:38 -0400136my $iteration = 0;
Steven Rostedte48c5292010-11-02 14:35:37 -0400137my $successes = 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400138
Steven Rostedt165708b2011-11-26 20:56:52 -0500139# set when a test is something other that just building or install
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500140# which would require more options.
141my $buildonly = 1;
142
Steven Rostedtdbd37832011-11-23 16:00:48 -0500143# set when creating a new config
144my $newconfig = 0;
145
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500146my %entered_configs;
147my %config_help;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400148my %variable;
Steven Rostedtfcb3f162011-06-13 10:40:58 -0400149my %force_config;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500150
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400151# do not force reboots on config problems
152my $no_reboot = 1;
153
Steven Rostedt7bf51072011-10-22 09:07:03 -0400154# default variables that can be used
155chomp ($variable{"PWD"} = `pwd`);
156
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500157$config_help{"MACHINE"} = << "EOF"
158 The machine hostname that you will test.
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500159 For build only tests, it is still needed to differentiate log files.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500160EOF
161 ;
162$config_help{"SSH_USER"} = << "EOF"
163 The box is expected to have ssh on normal bootup, provide the user
164 (most likely root, since you need privileged operations)
165EOF
166 ;
167$config_help{"BUILD_DIR"} = << "EOF"
168 The directory that contains the Linux source code (full path).
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500169 You can use \${PWD} that will be the path where ktest.pl is run, or use
170 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500171EOF
172 ;
173$config_help{"OUTPUT_DIR"} = << "EOF"
174 The directory that the objects will be built (full path).
175 (can not be same as BUILD_DIR)
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500176 You can use \${PWD} that will be the path where ktest.pl is run, or use
177 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500178EOF
179 ;
180$config_help{"BUILD_TARGET"} = << "EOF"
181 The location of the compiled file to copy to the target.
182 (relative to OUTPUT_DIR)
183EOF
184 ;
Steven Rostedtdbd37832011-11-23 16:00:48 -0500185$config_help{"BUILD_OPTIONS"} = << "EOF"
186 Options to add to \"make\" when building.
187 i.e. -j20
188EOF
189 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500190$config_help{"TARGET_IMAGE"} = << "EOF"
191 The place to put your image on the test machine.
192EOF
193 ;
194$config_help{"POWER_CYCLE"} = << "EOF"
195 A script or command to reboot the box.
196
197 Here is a digital loggers power switch example
198 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
199
200 Here is an example to reboot a virtual box on the current host
201 with the name "Guest".
202 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
203EOF
204 ;
205$config_help{"CONSOLE"} = << "EOF"
206 The script or command that reads the console
207
208 If you use ttywatch server, something like the following would work.
209CONSOLE = nc -d localhost 3001
210
211 For a virtual machine with guest name "Guest".
212CONSOLE = virsh console Guest
213EOF
214 ;
215$config_help{"LOCALVERSION"} = << "EOF"
216 Required version ending to differentiate the test
217 from other linux builds on the system.
218EOF
219 ;
220$config_help{"REBOOT_TYPE"} = << "EOF"
221 Way to reboot the box to the test kernel.
222 Only valid options so far are "grub" and "script".
223
224 If you specify grub, it will assume grub version 1
225 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
226 and select that target to reboot to the kernel. If this is not
227 your setup, then specify "script" and have a command or script
228 specified in REBOOT_SCRIPT to boot to the target.
229
230 The entry in /boot/grub/menu.lst must be entered in manually.
231 The test will not modify that file.
232EOF
233 ;
234$config_help{"GRUB_MENU"} = << "EOF"
235 The grub title name for the test kernel to boot
236 (Only mandatory if REBOOT_TYPE = grub)
237
238 Note, ktest.pl will not update the grub menu.lst, you need to
239 manually add an option for the test. ktest.pl will search
240 the grub menu.lst for this option to find what kernel to
241 reboot into.
242
243 For example, if in the /boot/grub/menu.lst the test kernel title has:
244 title Test Kernel
245 kernel vmlinuz-test
246 GRUB_MENU = Test Kernel
247EOF
248 ;
249$config_help{"REBOOT_SCRIPT"} = << "EOF"
250 A script to reboot the target into the test kernel
251 (Only mandatory if REBOOT_TYPE = script)
252EOF
253 ;
254
Steven Rostedtdad98752011-11-22 20:48:57 -0500255sub read_prompt {
256 my ($cancel, $prompt) = @_;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400257
258 my $ans;
259
260 for (;;) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500261 if ($cancel) {
262 print "$prompt [y/n/C] ";
263 } else {
264 print "$prompt [Y/n] ";
265 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400266 $ans = <STDIN>;
267 chomp $ans;
268 if ($ans =~ /^\s*$/) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500269 if ($cancel) {
270 $ans = "c";
271 } else {
272 $ans = "y";
273 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400274 }
275 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
Steven Rostedtdad98752011-11-22 20:48:57 -0500276 if ($cancel) {
277 last if ($ans =~ /^c$/i);
278 print "Please answer either 'y', 'n' or 'c'.\n";
279 } else {
280 print "Please answer either 'y' or 'n'.\n";
281 }
282 }
283 if ($ans =~ /^c/i) {
284 exit;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400285 }
286 if ($ans !~ /^y$/i) {
287 return 0;
288 }
289 return 1;
290}
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500291
Steven Rostedtdad98752011-11-22 20:48:57 -0500292sub read_yn {
293 my ($prompt) = @_;
294
295 return read_prompt 0, $prompt;
296}
297
298sub read_ync {
299 my ($prompt) = @_;
300
301 return read_prompt 1, $prompt;
302}
303
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500304sub get_ktest_config {
305 my ($config) = @_;
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400306 my $ans;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500307
308 return if (defined($opt{$config}));
309
310 if (defined($config_help{$config})) {
311 print "\n";
312 print $config_help{$config};
313 }
314
315 for (;;) {
316 print "$config = ";
Steven Rostedtdbd37832011-11-23 16:00:48 -0500317 if (defined($default{$config}) && length($default{$config})) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500318 print "\[$default{$config}\] ";
319 }
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400320 $ans = <STDIN>;
321 $ans =~ s/^\s*(.*\S)\s*$/$1/;
322 if ($ans =~ /^\s*$/) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500323 if ($default{$config}) {
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400324 $ans = $default{$config};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500325 } else {
326 print "Your answer can not be blank\n";
327 next;
328 }
329 }
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500330 $entered_configs{$config} = ${ans};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500331 last;
332 }
333}
334
335sub get_ktest_configs {
336 get_ktest_config("MACHINE");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500337 get_ktest_config("BUILD_DIR");
338 get_ktest_config("OUTPUT_DIR");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500339
Steven Rostedtdbd37832011-11-23 16:00:48 -0500340 if ($newconfig) {
341 get_ktest_config("BUILD_OPTIONS");
342 }
343
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500344 # options required for other than just building a kernel
345 if (!$buildonly) {
Steven Rostedt165708b2011-11-26 20:56:52 -0500346 get_ktest_config("POWER_CYCLE");
347 get_ktest_config("CONSOLE");
348 }
349
350 # options required for install and more
351 if ($buildonly != 1) {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500352 get_ktest_config("SSH_USER");
353 get_ktest_config("BUILD_TARGET");
354 get_ktest_config("TARGET_IMAGE");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500355 }
356
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500357 get_ktest_config("LOCALVERSION");
358
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500359 return if ($buildonly);
360
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500361 my $rtype = $opt{"REBOOT_TYPE"};
362
363 if (!defined($rtype)) {
364 if (!defined($opt{"GRUB_MENU"})) {
365 get_ktest_config("REBOOT_TYPE");
366 $rtype = $entered_configs{"REBOOT_TYPE"};
367 } else {
368 $rtype = "grub";
369 }
370 }
371
372 if ($rtype eq "grub") {
373 get_ktest_config("GRUB_MENU");
374 } else {
375 get_ktest_config("REBOOT_SCRIPT");
376 }
377}
378
Steven Rostedt77d942c2011-05-20 13:36:58 -0400379sub process_variables {
Steven Rostedt8d735212011-10-17 11:36:44 -0400380 my ($value, $remove_undef) = @_;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400381 my $retval = "";
382
383 # We want to check for '\', and it is just easier
384 # to check the previous characet of '$' and not need
385 # to worry if '$' is the first character. By adding
386 # a space to $value, we can just check [^\\]\$ and
387 # it will still work.
388 $value = " $value";
389
390 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
391 my $begin = $1;
392 my $var = $2;
393 my $end = $3;
394 # append beginning of value to retval
395 $retval = "$retval$begin";
396 if (defined($variable{$var})) {
397 $retval = "$retval$variable{$var}";
Steven Rostedt8d735212011-10-17 11:36:44 -0400398 } elsif (defined($remove_undef) && $remove_undef) {
399 # for if statements, any variable that is not defined,
400 # we simple convert to 0
401 $retval = "${retval}0";
Steven Rostedt77d942c2011-05-20 13:36:58 -0400402 } else {
403 # put back the origin piece.
404 $retval = "$retval\$\{$var\}";
405 }
406 $value = $end;
407 }
408 $retval = "$retval$value";
409
410 # remove the space added in the beginning
411 $retval =~ s/ //;
412
413 return "$retval"
414}
415
Steven Rostedta57419b2010-11-02 15:13:54 -0400416sub set_value {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400417 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
Steven Rostedta57419b2010-11-02 15:13:54 -0400418
Steven Rostedtcad96662011-12-22 11:32:52 -0500419 my $prvalue = process_variables($rvalue);
420
421 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500422 # Note if a test is something other than build, then we
423 # will need other manditory options.
Steven Rostedtcad96662011-12-22 11:32:52 -0500424 if ($prvalue ne "install") {
Steven Rostedt165708b2011-11-26 20:56:52 -0500425 $buildonly = 0;
426 } else {
427 # install still limits some manditory options.
428 $buildonly = 2;
429 }
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500430 }
431
Steven Rostedta57419b2010-11-02 15:13:54 -0400432 if (defined($opt{$lvalue})) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400433 if (!$override || defined(${$overrides}{$lvalue})) {
434 my $extra = "";
435 if ($override) {
436 $extra = "In the same override section!\n";
437 }
438 die "$name: $.: Option $lvalue defined more than once!\n$extra";
439 }
Steven Rostedtcad96662011-12-22 11:32:52 -0500440 ${$overrides}{$lvalue} = $prvalue;
Steven Rostedta57419b2010-11-02 15:13:54 -0400441 }
Steven Rostedt21a96792010-11-08 16:45:50 -0500442 if ($rvalue =~ /^\s*$/) {
443 delete $opt{$lvalue};
444 } else {
Steven Rostedtcad96662011-12-22 11:32:52 -0500445 $opt{$lvalue} = $prvalue;
Steven Rostedt21a96792010-11-08 16:45:50 -0500446 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400447}
448
Steven Rostedt77d942c2011-05-20 13:36:58 -0400449sub set_variable {
450 my ($lvalue, $rvalue) = @_;
451
452 if ($rvalue =~ /^\s*$/) {
453 delete $variable{$lvalue};
454 } else {
455 $rvalue = process_variables($rvalue);
456 $variable{$lvalue} = $rvalue;
457 }
458}
459
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400460sub process_compare {
461 my ($lval, $cmp, $rval) = @_;
462
463 # remove whitespace
464
465 $lval =~ s/^\s*//;
466 $lval =~ s/\s*$//;
467
468 $rval =~ s/^\s*//;
469 $rval =~ s/\s*$//;
470
471 if ($cmp eq "==") {
472 return $lval eq $rval;
473 } elsif ($cmp eq "!=") {
474 return $lval ne $rval;
475 }
476
477 my $statement = "$lval $cmp $rval";
478 my $ret = eval $statement;
479
480 # $@ stores error of eval
481 if ($@) {
482 return -1;
483 }
484
485 return $ret;
486}
487
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400488sub value_defined {
489 my ($val) = @_;
490
491 return defined($variable{$2}) ||
492 defined($opt{$2});
493}
494
Steven Rostedt8d735212011-10-17 11:36:44 -0400495my $d = 0;
496sub process_expression {
497 my ($name, $val) = @_;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400498
Steven Rostedt8d735212011-10-17 11:36:44 -0400499 my $c = $d++;
500
501 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
502 my $express = $1;
503
504 if (process_expression($name, $express)) {
505 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
506 } else {
507 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
508 }
509 }
510
511 $d--;
512 my $OR = "\\|\\|";
513 my $AND = "\\&\\&";
514
515 while ($val =~ s/^(.*?)($OR|$AND)//) {
516 my $express = $1;
517 my $op = $2;
518
519 if (process_expression($name, $express)) {
520 if ($op eq "||") {
521 return 1;
522 }
523 } else {
524 if ($op eq "&&") {
525 return 0;
526 }
527 }
528 }
Steven Rostedt45d73a52011-09-30 19:44:53 -0400529
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400530 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
531 my $ret = process_compare($1, $2, $3);
532 if ($ret < 0) {
533 die "$name: $.: Unable to process comparison\n";
534 }
535 return $ret;
536 }
537
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400538 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
539 if (defined $1) {
540 return !value_defined($2);
541 } else {
542 return value_defined($2);
543 }
544 }
545
Steven Rostedt45d73a52011-09-30 19:44:53 -0400546 if ($val =~ /^\s*0\s*$/) {
547 return 0;
548 } elsif ($val =~ /^\s*\d+\s*$/) {
549 return 1;
550 }
551
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400552 die ("$name: $.: Undefined content $val in if statement\n");
Steven Rostedt8d735212011-10-17 11:36:44 -0400553}
554
555sub process_if {
556 my ($name, $value) = @_;
557
558 # Convert variables and replace undefined ones with 0
559 my $val = process_variables($value, 1);
560 my $ret = process_expression $name, $val;
561
562 return $ret;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400563}
564
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400565sub __read_config {
566 my ($config, $current_test_num) = @_;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400567
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400568 my $in;
569 open($in, $config) || die "can't read file $config";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400570
Steven Rostedta57419b2010-11-02 15:13:54 -0400571 my $name = $config;
572 $name =~ s,.*/(.*),$1,;
573
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400574 my $test_num = $$current_test_num;
Steven Rostedta57419b2010-11-02 15:13:54 -0400575 my $default = 1;
576 my $repeat = 1;
577 my $num_tests_set = 0;
578 my $skip = 0;
579 my $rest;
Steven Rostedta9f84422011-10-17 11:06:29 -0400580 my $line;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400581 my $test_case = 0;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400582 my $if = 0;
583 my $if_set = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400584 my $override = 0;
585
586 my %overrides;
Steven Rostedta57419b2010-11-02 15:13:54 -0400587
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400588 while (<$in>) {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400589
590 # ignore blank lines and comments
591 next if (/^\s*$/ || /\s*\#/);
592
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400593 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400594
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400595 my $type = $1;
596 $rest = $2;
Steven Rostedta9f84422011-10-17 11:06:29 -0400597 $line = $2;
Steven Rostedta57419b2010-11-02 15:13:54 -0400598
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400599 my $old_test_num;
600 my $old_repeat;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400601 $override = 0;
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400602
603 if ($type eq "TEST_START") {
604
605 if ($num_tests_set) {
606 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
607 }
608
609 $old_test_num = $test_num;
610 $old_repeat = $repeat;
611
612 $test_num += $repeat;
613 $default = 0;
614 $repeat = 1;
615 } else {
616 $default = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400617 }
618
Steven Rostedta9f84422011-10-17 11:06:29 -0400619 # If SKIP is anywhere in the line, the command will be skipped
620 if ($rest =~ s/\s+SKIP\b//) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400621 $skip = 1;
622 } else {
Steven Rostedt0df213c2011-06-14 20:51:37 -0400623 $test_case = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400624 $skip = 0;
625 }
626
Steven Rostedta9f84422011-10-17 11:06:29 -0400627 if ($rest =~ s/\sELSE\b//) {
628 if (!$if) {
629 die "$name: $.: ELSE found with out matching IF section\n$_";
630 }
631 $if = 0;
632
633 if ($if_set) {
634 $skip = 1;
635 } else {
636 $skip = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400637 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400638 }
639
Steven Rostedta9f84422011-10-17 11:06:29 -0400640 if ($rest =~ s/\sIF\s+(.*)//) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400641 if (process_if($name, $1)) {
642 $if_set = 1;
643 } else {
644 $skip = 1;
645 }
646 $if = 1;
647 } else {
648 $if = 0;
Steven Rostedta9f84422011-10-17 11:06:29 -0400649 $if_set = 0;
Steven Rostedta57419b2010-11-02 15:13:54 -0400650 }
651
Steven Rostedta9f84422011-10-17 11:06:29 -0400652 if (!$skip) {
653 if ($type eq "TEST_START") {
654 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
655 $repeat = $1;
656 $repeat_tests{"$test_num"} = $repeat;
657 }
658 } elsif ($rest =~ s/\sOVERRIDE\b//) {
659 # DEFAULT only
660 $override = 1;
661 # Clear previous overrides
662 %overrides = ();
663 }
664 }
665
666 if (!$skip && $rest !~ /^\s*$/) {
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400667 die "$name: $.: Gargbage found after $type\n$_";
Steven Rostedta57419b2010-11-02 15:13:54 -0400668 }
669
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400670 if ($skip && $type eq "TEST_START") {
Steven Rostedta57419b2010-11-02 15:13:54 -0400671 $test_num = $old_test_num;
Steven Rostedte48c5292010-11-02 14:35:37 -0400672 $repeat = $old_repeat;
Steven Rostedta57419b2010-11-02 15:13:54 -0400673 }
674
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400675 } elsif (/^\s*ELSE\b(.*)$/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400676 if (!$if) {
677 die "$name: $.: ELSE found with out matching IF section\n$_";
678 }
679 $rest = $1;
680 if ($if_set) {
681 $skip = 1;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400682 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400683 } else {
684 $skip = 0;
685
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400686 if ($rest =~ /\sIF\s+(.*)/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400687 # May be a ELSE IF section.
688 if (!process_if($name, $1)) {
689 $skip = 1;
690 }
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400691 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400692 } else {
693 $if = 0;
694 }
695 }
696
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400697 if ($rest !~ /^\s*$/) {
698 die "$name: $.: Gargbage found after DEFAULTS\n$_";
699 }
700
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400701 } elsif (/^\s*INCLUDE\s+(\S+)/) {
702
703 next if ($skip);
704
705 if (!$default) {
706 die "$name: $.: INCLUDE can only be done in default sections\n$_";
707 }
708
709 my $file = process_variables($1);
710
711 if ($file !~ m,^/,) {
712 # check the path of the config file first
713 if ($config =~ m,(.*)/,) {
714 if (-f "$1/$file") {
715 $file = "$1/$file";
716 }
717 }
718 }
719
720 if ( ! -r $file ) {
721 die "$name: $.: Can't read file $file\n$_";
722 }
723
724 if (__read_config($file, \$test_num)) {
725 $test_case = 1;
726 }
727
Steven Rostedta57419b2010-11-02 15:13:54 -0400728 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
729
730 next if ($skip);
731
Steven Rostedt2545eb62010-11-02 15:01:32 -0400732 my $lvalue = $1;
733 my $rvalue = $2;
734
Steven Rostedta57419b2010-11-02 15:13:54 -0400735 if (!$default &&
736 ($lvalue eq "NUM_TESTS" ||
737 $lvalue eq "LOG_FILE" ||
738 $lvalue eq "CLEAR_LOG")) {
739 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400740 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400741
742 if ($lvalue eq "NUM_TESTS") {
743 if ($test_num) {
744 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
745 }
746 if (!$default) {
747 die "$name: $.: NUM_TESTS must be set in default section\n";
748 }
749 $num_tests_set = 1;
750 }
751
752 if ($default || $lvalue =~ /\[\d+\]$/) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400753 set_value($lvalue, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400754 } else {
755 my $val = "$lvalue\[$test_num\]";
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400756 set_value($val, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400757
758 if ($repeat > 1) {
759 $repeats{$val} = $repeat;
760 }
761 }
Steven Rostedt77d942c2011-05-20 13:36:58 -0400762 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
763 next if ($skip);
764
765 my $lvalue = $1;
766 my $rvalue = $2;
767
768 # process config variables.
769 # Config variables are only active while reading the
770 # config and can be defined anywhere. They also ignore
771 # TEST_START and DEFAULTS, but are skipped if they are in
772 # on of these sections that have SKIP defined.
773 # The save variable can be
774 # defined multiple times and the new one simply overrides
775 # the prevous one.
776 set_variable($lvalue, $rvalue);
777
Steven Rostedta57419b2010-11-02 15:13:54 -0400778 } else {
779 die "$name: $.: Garbage found in config\n$_";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400780 }
781 }
782
Steven Rostedta57419b2010-11-02 15:13:54 -0400783 if ($test_num) {
784 $test_num += $repeat - 1;
785 $opt{"NUM_TESTS"} = $test_num;
786 }
787
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400788 close($in);
789
790 $$current_test_num = $test_num;
791
792 return $test_case;
793}
794
Steven Rostedtc4261d02011-11-23 13:41:18 -0500795sub get_test_case {
796 print "What test case would you like to run?\n";
797 print " (build, install or boot)\n";
798 print " Other tests are available but require editing the config file\n";
799 my $ans = <STDIN>;
800 chomp $ans;
801 $default{"TEST_TYPE"} = $ans;
802}
803
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400804sub read_config {
805 my ($config) = @_;
806
807 my $test_case;
808 my $test_num = 0;
809
810 $test_case = __read_config $config, \$test_num;
811
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500812 # make sure we have all mandatory configs
813 get_ktest_configs;
814
Steven Rostedt0df213c2011-06-14 20:51:37 -0400815 # was a test specified?
816 if (!$test_case) {
817 print "No test case specified.\n";
Steven Rostedtc4261d02011-11-23 13:41:18 -0500818 get_test_case;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400819 }
820
Steven Rostedta75fece2010-11-02 14:58:27 -0400821 # set any defaults
822
823 foreach my $default (keys %default) {
824 if (!defined($opt{$default})) {
825 $opt{$default} = $default{$default};
826 }
827 }
Steven Rostedt2545eb62010-11-02 15:01:32 -0400828}
829
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400830sub __eval_option {
831 my ($option, $i) = @_;
832
833 # Add space to evaluate the character before $
834 $option = " $option";
835 my $retval = "";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530836 my $repeated = 0;
837 my $parent = 0;
838
839 foreach my $test (keys %repeat_tests) {
840 if ($i >= $test &&
841 $i < $test + $repeat_tests{$test}) {
842
843 $repeated = 1;
844 $parent = $test;
845 last;
846 }
847 }
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400848
849 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
850 my $start = $1;
851 my $var = $2;
852 my $end = $3;
853
854 # Append beginning of line
855 $retval = "$retval$start";
856
857 # If the iteration option OPT[$i] exists, then use that.
858 # otherwise see if the default OPT (without [$i]) exists.
859
860 my $o = "$var\[$i\]";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530861 my $parento = "$var\[$parent\]";
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400862
863 if (defined($opt{$o})) {
864 $o = $opt{$o};
865 $retval = "$retval$o";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530866 } elsif ($repeated && defined($opt{$parento})) {
867 $o = $opt{$parento};
868 $retval = "$retval$o";
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400869 } elsif (defined($opt{$var})) {
870 $o = $opt{$var};
871 $retval = "$retval$o";
872 } else {
873 $retval = "$retval\$\{$var\}";
874 }
875
876 $option = $end;
877 }
878
879 $retval = "$retval$option";
880
881 $retval =~ s/^ //;
882
883 return $retval;
884}
885
886sub eval_option {
887 my ($option, $i) = @_;
888
889 my $prev = "";
890
891 # Since an option can evaluate to another option,
892 # keep iterating until we do not evaluate any more
893 # options.
894 my $r = 0;
895 while ($prev ne $option) {
896 # Check for recursive evaluations.
897 # 100 deep should be more than enough.
898 if ($r++ > 100) {
899 die "Over 100 evaluations accurred with $option\n" .
900 "Check for recursive variables\n";
901 }
902 $prev = $option;
903 $option = __eval_option($option, $i);
904 }
905
906 return $option;
907}
908
Steven Rostedtd1e2f222010-11-08 16:39:57 -0500909sub _logit {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400910 if (defined($opt{"LOG_FILE"})) {
911 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
912 print OUT @_;
913 close(OUT);
914 }
915}
916
Steven Rostedtd1e2f222010-11-08 16:39:57 -0500917sub logit {
918 if (defined($opt{"LOG_FILE"})) {
919 _logit @_;
920 } else {
921 print @_;
922 }
923}
924
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400925sub doprint {
926 print @_;
Steven Rostedtd1e2f222010-11-08 16:39:57 -0500927 _logit @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400928}
929
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400930sub run_command;
Andrew Jones2728be42011-08-12 15:32:05 +0200931sub start_monitor;
932sub end_monitor;
933sub wait_for_monitor;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400934
935sub reboot {
Andrew Jones2728be42011-08-12 15:32:05 +0200936 my ($time) = @_;
937
Steven Rostedt2b803362011-09-30 18:00:23 -0400938 if (defined($time)) {
939 start_monitor;
940 # flush out current monitor
941 # May contain the reboot success line
942 wait_for_monitor 1;
943 }
944
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400945 # try to reboot normally
Steven Rostedte48c5292010-11-02 14:35:37 -0400946 if (run_command $reboot) {
Steven Rostedt576f6272010-11-02 14:58:38 -0400947 if (defined($powercycle_after_reboot)) {
948 sleep $powercycle_after_reboot;
949 run_command "$power_cycle";
950 }
951 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400952 # nope? power cycle it.
Steven Rostedta75fece2010-11-02 14:58:27 -0400953 run_command "$power_cycle";
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400954 }
Andrew Jones2728be42011-08-12 15:32:05 +0200955
956 if (defined($time)) {
Steven Rostedt2b803362011-09-30 18:00:23 -0400957 wait_for_monitor($time, $reboot_success_line);
Andrew Jones2728be42011-08-12 15:32:05 +0200958 end_monitor;
959 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400960}
961
Steven Rostedt576f6272010-11-02 14:58:38 -0400962sub do_not_reboot {
963 my $i = $iteration;
964
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400965 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -0400966 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
967 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
968}
969
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400970sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400971 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400972
Steven Rostedt576f6272010-11-02 14:58:38 -0400973 my $i = $iteration;
974
975 if ($reboot_on_error && !do_not_reboot) {
976
Steven Rostedt75c3fda72010-11-02 14:57:21 -0400977 doprint "REBOOTING\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400978 reboot;
Steven Rostedt75c3fda72010-11-02 14:57:21 -0400979
Steven Rostedta75fece2010-11-02 14:58:27 -0400980 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400981 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400982 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400983 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -0400984
Steven Rostedtf80802c2011-03-07 13:18:47 -0500985 if (defined($opt{"LOG_FILE"})) {
986 print " See $opt{LOG_FILE} for more info.\n";
987 }
988
Steven Rostedt576f6272010-11-02 14:58:38 -0400989 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400990}
991
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400992sub open_console {
993 my ($fp) = @_;
994
995 my $flags;
996
Steven Rostedta75fece2010-11-02 14:58:27 -0400997 my $pid = open($fp, "$console|") or
998 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400999
1000 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001001 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001002 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001003 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001004
1005 return $pid;
1006}
1007
1008sub close_console {
1009 my ($fp, $pid) = @_;
1010
1011 doprint "kill child process $pid\n";
1012 kill 2, $pid;
1013
1014 print "closing!\n";
1015 close($fp);
1016}
1017
1018sub start_monitor {
1019 if ($monitor_cnt++) {
1020 return;
1021 }
1022 $monitor_fp = \*MONFD;
1023 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001024
1025 return;
1026
1027 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001028}
1029
1030sub end_monitor {
1031 if (--$monitor_cnt) {
1032 return;
1033 }
1034 close_console($monitor_fp, $monitor_pid);
1035}
1036
1037sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001038 my ($time, $stop) = @_;
1039 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001040 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001041 my $booted = 0;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001042
Steven Rostedta75fece2010-11-02 14:58:27 -04001043 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001044
1045 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001046 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001047 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001048 last if (!defined($line));
1049 print "$line";
1050 $full_line .= $line;
1051
1052 if (defined($stop) && $full_line =~ /$stop/) {
1053 doprint "wait for monitor detected $stop\n";
1054 $booted = 1;
1055 }
1056
1057 if ($line =~ /\n/) {
1058 $full_line = "";
1059 }
1060 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001061 print "** Monitor flushed **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001062}
1063
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301064sub save_logs {
1065 my ($result, $basedir) = @_;
1066 my @t = localtime;
1067 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1068 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1069
1070 my $type = $build_type;
1071 if ($type =~ /useconfig/) {
1072 $type = "useconfig";
1073 }
1074
1075 my $dir = "$machine-$test_type-$type-$result-$date";
1076
1077 $dir = "$basedir/$dir";
1078
1079 if (!-d $dir) {
1080 mkpath($dir) or
1081 die "can't create $dir";
1082 }
1083
1084 my %files = (
1085 "config" => $output_config,
1086 "buildlog" => $buildlog,
1087 "dmesg" => $dmesg,
1088 "testlog" => $testlog,
1089 );
1090
1091 while (my ($name, $source) = each(%files)) {
1092 if (-f "$source") {
1093 cp "$source", "$dir/$name" or
1094 die "failed to copy $source";
1095 }
1096 }
1097
1098 doprint "*** Saved info to $dir ***\n";
1099}
1100
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001101sub fail {
1102
Steven Rostedta75fece2010-11-02 14:58:27 -04001103 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001104 dodie @_;
1105 }
1106
Steven Rostedta75fece2010-11-02 14:58:27 -04001107 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001108
Steven Rostedt576f6272010-11-02 14:58:38 -04001109 my $i = $iteration;
1110
Steven Rostedta75fece2010-11-02 14:58:27 -04001111 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001112 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001113 doprint "REBOOTING\n";
Andrew Jones2728be42011-08-12 15:32:05 +02001114 reboot $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001115 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001116
Steven Rostedt9064af52011-06-13 10:38:48 -04001117 my $name = "";
1118
1119 if (defined($test_name)) {
1120 $name = " ($test_name)";
1121 }
1122
Steven Rostedt576f6272010-11-02 14:58:38 -04001123 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1124 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001125 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001126 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1127 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001128
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301129 if (defined($store_failures)) {
1130 save_logs "fail", $store_failures;
1131 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001132
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001133 return 1;
1134}
1135
Steven Rostedt2545eb62010-11-02 15:01:32 -04001136sub run_command {
1137 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001138 my $dolog = 0;
1139 my $dord = 0;
1140 my $pid;
1141
Steven Rostedte48c5292010-11-02 14:35:37 -04001142 $command =~ s/\$SSH_USER/$ssh_user/g;
1143 $command =~ s/\$MACHINE/$machine/g;
1144
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001145 doprint("$command ... ");
1146
1147 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001148 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001149
1150 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001151 open(LOG, ">>$opt{LOG_FILE}") or
1152 dodie "failed to write to log";
1153 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001154 }
1155
1156 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001157 open (RD, ">$redirect") or
1158 dodie "failed to write to redirect $redirect";
1159 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001160 }
1161
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001162 while (<CMD>) {
1163 print LOG if ($dolog);
1164 print RD if ($dord);
1165 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001166
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001167 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001168 my $failed = $?;
1169
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001170 close(CMD);
1171 close(LOG) if ($dolog);
1172 close(RD) if ($dord);
1173
Steven Rostedt2545eb62010-11-02 15:01:32 -04001174 if ($failed) {
1175 doprint "FAILED!\n";
1176 } else {
1177 doprint "SUCCESS\n";
1178 }
1179
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001180 return !$failed;
1181}
1182
Steven Rostedte48c5292010-11-02 14:35:37 -04001183sub run_ssh {
1184 my ($cmd) = @_;
1185 my $cp_exec = $ssh_exec;
1186
1187 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1188 return run_command "$cp_exec";
1189}
1190
1191sub run_scp {
1192 my ($src, $dst) = @_;
1193 my $cp_scp = $scp_to_target;
1194
1195 $cp_scp =~ s/\$SRC_FILE/$src/g;
1196 $cp_scp =~ s/\$DST_FILE/$dst/g;
1197
1198 return run_command "$cp_scp";
1199}
1200
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001201sub get_grub_index {
1202
Steven Rostedta75fece2010-11-02 14:58:27 -04001203 if ($reboot_type ne "grub") {
1204 return;
1205 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001206 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001207
1208 doprint "Find grub menu ... ";
1209 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001210
1211 my $ssh_grub = $ssh_exec;
1212 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1213
1214 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001215 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001216
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001217 my $found = 0;
1218
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001219 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001220 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001221 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001222 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001223 last;
1224 } elsif (/^\s*title\s/) {
1225 $grub_number++;
1226 }
1227 }
1228 close(IN);
1229
Steven Rostedta75fece2010-11-02 14:58:27 -04001230 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001231 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001232 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001233}
1234
Steven Rostedt2545eb62010-11-02 15:01:32 -04001235sub wait_for_input
1236{
1237 my ($fp, $time) = @_;
1238 my $rin;
1239 my $ready;
1240 my $line;
1241 my $ch;
1242
1243 if (!defined($time)) {
1244 $time = $timeout;
1245 }
1246
1247 $rin = '';
1248 vec($rin, fileno($fp), 1) = 1;
1249 $ready = select($rin, undef, undef, $time);
1250
1251 $line = "";
1252
1253 # try to read one char at a time
1254 while (sysread $fp, $ch, 1) {
1255 $line .= $ch;
1256 last if ($ch eq "\n");
1257 }
1258
1259 if (!length($line)) {
1260 return undef;
1261 }
1262
1263 return $line;
1264}
1265
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001266sub reboot_to {
Steven Rostedta75fece2010-11-02 14:58:27 -04001267 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001268 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1269 reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -04001270 return;
1271 }
1272
1273 run_command "$reboot_script";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001274}
1275
Steven Rostedta57419b2010-11-02 15:13:54 -04001276sub get_sha1 {
1277 my ($commit) = @_;
1278
1279 doprint "git rev-list --max-count=1 $commit ... ";
1280 my $sha1 = `git rev-list --max-count=1 $commit`;
1281 my $ret = $?;
1282
1283 logit $sha1;
1284
1285 if ($ret) {
1286 doprint "FAILED\n";
1287 dodie "Failed to get git $commit";
1288 }
1289
1290 print "SUCCESS\n";
1291
1292 chomp $sha1;
1293
1294 return $sha1;
1295}
1296
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001297sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001298 my $booted = 0;
1299 my $bug = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001300 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001301 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001302
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001303 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001304
1305 my $line;
1306 my $full_line = "";
1307
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001308 open(DMESG, "> $dmesg") or
1309 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001310
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001311 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001312
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001313 my $success_start;
1314 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001315 my $monitor_start = time;
1316 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001317 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001318
Steven Rostedt2d01b262011-03-08 09:47:54 -05001319 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001320
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001321 if ($bug && defined($stop_after_failure) &&
1322 $stop_after_failure >= 0) {
1323 my $time = $stop_after_failure - (time - $failure_start);
1324 $line = wait_for_input($monitor_fp, $time);
1325 if (!defined($line)) {
1326 doprint "bug timed out after $booted_timeout seconds\n";
1327 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1328 last;
1329 }
1330 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001331 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001332 if (!defined($line)) {
1333 my $s = $booted_timeout == 1 ? "" : "s";
1334 doprint "Successful boot found: break after $booted_timeout second$s\n";
1335 last;
1336 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001337 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001338 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001339 if (!defined($line)) {
1340 my $s = $timeout == 1 ? "" : "s";
1341 doprint "Timed out after $timeout second$s\n";
1342 last;
1343 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001344 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001345
Steven Rostedt2545eb62010-11-02 15:01:32 -04001346 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001347 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001348
1349 # we are not guaranteed to get a full line
1350 $full_line .= $line;
1351
Steven Rostedta75fece2010-11-02 14:58:27 -04001352 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001353 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001354 $success_start = time;
1355 }
1356
1357 if ($booted && defined($stop_after_success) &&
1358 $stop_after_success >= 0) {
1359 my $now = time;
1360 if ($now - $success_start >= $stop_after_success) {
1361 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1362 last;
1363 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001364 }
1365
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001366 if ($full_line =~ /\[ backtrace testing \]/) {
1367 $skip_call_trace = 1;
1368 }
1369
Steven Rostedt2545eb62010-11-02 15:01:32 -04001370 if ($full_line =~ /call trace:/i) {
Steven Rostedt46519202011-03-08 09:40:31 -05001371 if (!$bug && !$skip_call_trace) {
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001372 $bug = 1;
1373 $failure_start = time;
1374 }
1375 }
1376
1377 if ($bug && defined($stop_after_failure) &&
1378 $stop_after_failure >= 0) {
1379 my $now = time;
1380 if ($now - $failure_start >= $stop_after_failure) {
1381 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1382 last;
1383 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001384 }
1385
1386 if ($full_line =~ /\[ end of backtrace testing \]/) {
1387 $skip_call_trace = 0;
1388 }
1389
1390 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001391 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001392 $bug = 1;
1393 }
1394
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001395 # Detect triple faults by testing the banner
1396 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1397 if ($1 eq $version) {
1398 $version_found = 1;
1399 } elsif ($version_found && $detect_triplefault) {
1400 # We already booted into the kernel we are testing,
1401 # but now we booted into another kernel?
1402 # Consider this a triple fault.
1403 doprint "Aleady booted in Linux kernel $version, but now\n";
1404 doprint "we booted into Linux kernel $1.\n";
1405 doprint "Assuming that this is a triple fault.\n";
1406 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1407 last;
1408 }
1409 }
1410
Steven Rostedt2545eb62010-11-02 15:01:32 -04001411 if ($line =~ /\n/) {
1412 $full_line = "";
1413 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001414
1415 if ($stop_test_after > 0 && !$booted && !$bug) {
1416 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001417 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001418 $done = 1;
1419 }
1420 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001421 }
1422
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001423 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001424
Steven Rostedt2545eb62010-11-02 15:01:32 -04001425 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001426 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001427 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001428 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001429
Steven Rostedta75fece2010-11-02 14:58:27 -04001430 if (!$booted) {
1431 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001432 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001433 }
1434
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001435 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001436}
1437
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001438sub eval_kernel_version {
1439 my ($option) = @_;
1440
1441 $option =~ s/\$KERNEL_VERSION/$version/g;
1442
1443 return $option;
1444}
1445
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001446sub do_post_install {
1447
1448 return if (!defined($post_install));
1449
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001450 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001451 run_command "$cp_post_install" or
1452 dodie "Failed to run post install";
1453}
1454
Steven Rostedt2545eb62010-11-02 15:01:32 -04001455sub install {
1456
Steven Rostedte0a87422011-09-30 17:50:48 -04001457 return if ($no_install);
1458
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001459 my $cp_target = eval_kernel_version $target_image;
1460
1461 run_scp "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001462 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001463
1464 my $install_mods = 0;
1465
1466 # should we process modules?
1467 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001468 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001469 while (<IN>) {
1470 if (/CONFIG_MODULES(=y)?/) {
1471 $install_mods = 1 if (defined($1));
1472 last;
1473 }
1474 }
1475 close(IN);
1476
1477 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001478 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001479 doprint "No modules needed\n";
1480 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001481 }
1482
Steven Rostedta75fece2010-11-02 14:58:27 -04001483 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001484 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001485
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001486 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001487 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001488
Steven Rostedte48c5292010-11-02 14:35:37 -04001489 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001490 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001491
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001492 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001493 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001494 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001495
Steven Rostedte48c5292010-11-02 14:35:37 -04001496 run_scp "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001497 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001498
Steven Rostedta75fece2010-11-02 14:58:27 -04001499 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001500
Steven Rostedte7b13442011-06-14 20:44:36 -04001501 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001502 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001503
Steven Rostedte48c5292010-11-02 14:35:37 -04001504 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001505
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001506 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001507}
1508
Steven Rostedtddf607e2011-06-14 20:49:13 -04001509sub get_version {
1510 # get the release name
1511 doprint "$make kernelrelease ... ";
1512 $version = `$make kernelrelease | tail -1`;
1513 chomp($version);
1514 doprint "$version\n";
1515}
1516
1517sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001518 # Make sure the stable kernel has finished booting
1519 start_monitor;
1520 wait_for_monitor 5;
1521 end_monitor;
1522
Steven Rostedtddf607e2011-06-14 20:49:13 -04001523 get_grub_index;
1524 get_version;
1525 install;
1526
1527 start_monitor;
1528 return monitor;
1529}
1530
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001531sub check_buildlog {
1532 my ($patch) = @_;
1533
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001534 my @files = `git show $patch | diffstat -l`;
1535
1536 open(IN, "git show $patch |") or
1537 dodie "failed to show $patch";
1538 while (<IN>) {
1539 if (m,^--- a/(.*),) {
1540 chomp $1;
1541 $files[$#files] = $1;
1542 }
1543 }
1544 close(IN);
1545
1546 open(IN, $buildlog) or dodie "Can't open $buildlog";
1547 while (<IN>) {
1548 if (/^\s*(.*?):.*(warning|error)/) {
1549 my $err = $1;
1550 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001551 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001552 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001553 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001554 }
1555 }
1556 }
1557 }
1558 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001559
1560 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001561}
1562
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001563sub apply_min_config {
1564 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05001565
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001566 # Read the config file and remove anything that
1567 # is in the force_config hash (from minconfig and others)
1568 # then add the force config back.
1569
1570 doprint "Applying minimum configurations into $output_config.new\n";
1571
1572 open (OUT, ">$outconfig") or
1573 dodie "Can't create $outconfig";
1574
1575 if (-f $output_config) {
1576 open (IN, $output_config) or
1577 dodie "Failed to open $output_config";
1578 while (<IN>) {
1579 if (/^(# )?(CONFIG_[^\s=]*)/) {
1580 next if (defined($force_config{$2}));
1581 }
1582 print OUT;
1583 }
1584 close IN;
1585 }
1586 foreach my $config (keys %force_config) {
1587 print OUT "$force_config{$config}\n";
1588 }
1589 close OUT;
1590
1591 run_command "mv $outconfig $output_config";
1592}
1593
1594sub make_oldconfig {
1595
Steven Rostedt4c4ab122011-07-15 21:16:17 -04001596 my @force_list = keys %force_config;
1597
1598 if ($#force_list >= 0) {
1599 apply_min_config;
1600 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001601
1602 if (!run_command "$make oldnoconfig") {
Steven Rostedt612b9e92011-03-07 13:27:43 -05001603 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1604 # try a yes '' | oldconfig
1605 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001606 run_command "yes '' | $make oldconfig" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001607 dodie "failed make config oldconfig";
1608 }
1609}
1610
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001611# read a config file and use this to force new configs.
1612sub load_force_config {
1613 my ($config) = @_;
1614
1615 open(IN, $config) or
1616 dodie "failed to read $config";
1617 while (<IN>) {
1618 chomp;
1619 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1620 $force_config{$1} = $_;
1621 } elsif (/^# (CONFIG_\S*) is not set/) {
1622 $force_config{$1} = $_;
1623 }
1624 }
1625 close IN;
1626}
1627
Steven Rostedt2545eb62010-11-02 15:01:32 -04001628sub build {
1629 my ($type) = @_;
1630
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001631 unlink $buildlog;
1632
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001633 # Failed builds should not reboot the target
1634 my $save_no_reboot = $no_reboot;
1635 $no_reboot = 1;
1636
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001637 if (defined($pre_build)) {
1638 my $ret = run_command $pre_build;
1639 if (!$ret && defined($pre_build_die) &&
1640 $pre_build_die) {
1641 dodie "failed to pre_build\n";
1642 }
1643 }
1644
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001645 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001646 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001647 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001648
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001649 $type = "oldconfig";
1650 }
1651
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001652 # old config can ask questions
1653 if ($type eq "oldconfig") {
Steven Rostedt9386c6a2010-11-08 16:35:48 -05001654 $type = "oldnoconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001655
1656 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001657 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001658
Andrew Jones13488232011-08-12 15:32:04 +02001659 if (!$noclean) {
1660 run_command "mv $output_config $outputdir/config_temp" or
1661 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001662
Andrew Jones13488232011-08-12 15:32:04 +02001663 run_command "$make mrproper" or dodie "make mrproper";
1664
1665 run_command "mv $outputdir/config_temp $output_config" or
1666 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001667 }
1668
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001669 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001670 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001671 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001672 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001673 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001674
1675 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04001676 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1677 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001678 close(OUT);
1679
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001680 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001681 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001682 }
1683
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001684 if ($type ne "oldnoconfig") {
1685 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001686 dodie "failed make config";
1687 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001688 # Run old config regardless, to enforce min configurations
1689 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001690
Steven Rostedta75fece2010-11-02 14:58:27 -04001691 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001692 my $build_ret = run_command "$make $build_options";
1693 undef $redirect;
1694
1695 if (defined($post_build)) {
1696 my $ret = run_command $post_build;
1697 if (!$ret && defined($post_build_die) &&
1698 $post_build_die) {
1699 dodie "failed to post_build\n";
1700 }
1701 }
1702
1703 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001704 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001705 if ($in_bisect) {
1706 $no_reboot = $save_no_reboot;
1707 return 0;
1708 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001709 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001710 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001711
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001712 $no_reboot = $save_no_reboot;
1713
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001714 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001715}
1716
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001717sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04001718 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001719 if (defined($poweroff_after_halt)) {
1720 sleep $poweroff_after_halt;
1721 run_command "$power_off";
1722 }
1723 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001724 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04001725 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001726 }
1727}
1728
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001729sub success {
1730 my ($i) = @_;
1731
Steven Rostedte48c5292010-11-02 14:35:37 -04001732 $successes++;
1733
Steven Rostedt9064af52011-06-13 10:38:48 -04001734 my $name = "";
1735
1736 if (defined($test_name)) {
1737 $name = " ($test_name)";
1738 }
1739
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001740 doprint "\n\n*******************************************\n";
1741 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001742 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001743 doprint "*******************************************\n";
1744 doprint "*******************************************\n";
1745
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301746 if (defined($store_successes)) {
1747 save_logs "success", $store_successes;
1748 }
1749
Steven Rostedt576f6272010-11-02 14:58:38 -04001750 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001751 doprint "Reboot and wait $sleep_time seconds\n";
Andrew Jones2728be42011-08-12 15:32:05 +02001752 reboot $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001753 }
1754}
1755
Steven Rostedtc960bb92011-03-08 09:22:39 -05001756sub answer_bisect {
1757 for (;;) {
1758 doprint "Pass or fail? [p/f]";
1759 my $ans = <STDIN>;
1760 chomp $ans;
1761 if ($ans eq "p" || $ans eq "P") {
1762 return 1;
1763 } elsif ($ans eq "f" || $ans eq "F") {
1764 return 0;
1765 } else {
1766 print "Please answer 'P' or 'F'\n";
1767 }
1768 }
1769}
1770
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001771sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001772 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001773
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001774 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04001775 $reboot_on_error = 0;
1776 $poweroff_on_error = 0;
1777 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001778
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301779 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001780 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301781 undef $redirect;
1782
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001783 exit $failed;
1784}
1785
1786my $child_done;
1787
1788sub child_finished {
1789 $child_done = 1;
1790}
1791
1792sub do_run_test {
1793 my $child_pid;
1794 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001795 my $line;
1796 my $full_line;
1797 my $bug = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001798
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001799 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001800
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001801 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001802
1803 $child_done = 0;
1804
1805 $SIG{CHLD} = qw(child_finished);
1806
1807 $child_pid = fork;
1808
1809 child_run_test if (!$child_pid);
1810
1811 $full_line = "";
1812
1813 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001814 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001815 if (defined($line)) {
1816
1817 # we are not guaranteed to get a full line
1818 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05001819 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001820
1821 if ($full_line =~ /call trace:/i) {
1822 $bug = 1;
1823 }
1824
1825 if ($full_line =~ /Kernel panic -/) {
1826 $bug = 1;
1827 }
1828
1829 if ($line =~ /\n/) {
1830 $full_line = "";
1831 }
1832 }
1833 } while (!$child_done && !$bug);
1834
1835 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05001836 my $failure_start = time;
1837 my $now;
1838 do {
1839 $line = wait_for_input($monitor_fp, 1);
1840 if (defined($line)) {
1841 doprint $line;
1842 }
1843 $now = time;
1844 if ($now - $failure_start >= $stop_after_failure) {
1845 last;
1846 }
1847 } while (defined($line));
1848
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001849 doprint "Detected kernel crash!\n";
1850 # kill the child with extreme prejudice
1851 kill 9, $child_pid;
1852 }
1853
1854 waitpid $child_pid, 0;
1855 $child_exit = $?;
1856
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001857 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001858 return 0 if $in_bisect;
1859 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001860 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001861 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001862}
1863
Steven Rostedta75fece2010-11-02 14:58:27 -04001864sub run_git_bisect {
1865 my ($command) = @_;
1866
1867 doprint "$command ... ";
1868
1869 my $output = `$command 2>&1`;
1870 my $ret = $?;
1871
1872 logit $output;
1873
1874 if ($ret) {
1875 doprint "FAILED\n";
1876 dodie "Failed to git bisect";
1877 }
1878
1879 doprint "SUCCESS\n";
1880 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1881 doprint "$1 [$2]\n";
1882 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
1883 $bisect_bad = $1;
1884 doprint "Found bad commit... $1\n";
1885 return 0;
1886 } else {
1887 # we already logged it, just print it now.
1888 print $output;
1889 }
1890
1891 return 1;
1892}
1893
Steven Rostedtc23dca72011-03-08 09:26:31 -05001894sub bisect_reboot {
1895 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Andrew Jones2728be42011-08-12 15:32:05 +02001896 reboot $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05001897}
1898
1899# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05001900sub run_bisect_test {
1901 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001902
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001903 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001904 my $result;
1905 my $output;
1906 my $ret;
1907
Steven Rostedt0a05c762010-11-08 11:14:10 -05001908 $in_bisect = 1;
1909
1910 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001911
1912 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05001913 if ($failed && $bisect_skip) {
1914 $in_bisect = 0;
1915 return -1;
1916 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001917 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001918
1919 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04001920 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001921
1922 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05001923 if ($failed && $bisect_skip) {
1924 end_monitor;
1925 bisect_reboot;
1926 $in_bisect = 0;
1927 return -1;
1928 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001929 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001930
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001931 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001932 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001933 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001934 }
1935
1936 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05001937 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001938 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05001939 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001940 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04001941
1942 # reboot the box to a kernel we can ssh to
1943 if ($type ne "build") {
1944 bisect_reboot;
1945 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05001946 $in_bisect = 0;
1947
1948 return $result;
1949}
1950
1951sub run_bisect {
1952 my ($type) = @_;
1953 my $buildtype = "oldconfig";
1954
1955 # We should have a minconfig to use?
1956 if (defined($minconfig)) {
1957 $buildtype = "useconfig:$minconfig";
1958 }
1959
1960 my $ret = run_bisect_test $type, $buildtype;
1961
Steven Rostedtc960bb92011-03-08 09:22:39 -05001962 if ($bisect_manual) {
1963 $ret = answer_bisect;
1964 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001965
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001966 # Are we looking for where it worked, not failed?
1967 if ($reverse_bisect) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05001968 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001969 }
1970
Steven Rostedtc23dca72011-03-08 09:26:31 -05001971 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05001972 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05001973 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05001974 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05001975 } elsif ($bisect_skip) {
1976 doprint "HIT A BAD COMMIT ... SKIPPING\n";
1977 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05001978 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001979}
1980
Steven Rostedtdad98752011-11-22 20:48:57 -05001981sub update_bisect_replay {
1982 my $tmp_log = "$tmpdir/ktest_bisect_log";
1983 run_command "git bisect log > $tmp_log" or
1984 die "can't create bisect log";
1985 return $tmp_log;
1986}
1987
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001988sub bisect {
1989 my ($i) = @_;
1990
1991 my $result;
1992
1993 die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
1994 die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
1995 die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
1996
1997 my $good = $opt{"BISECT_GOOD[$i]"};
1998 my $bad = $opt{"BISECT_BAD[$i]"};
1999 my $type = $opt{"BISECT_TYPE[$i]"};
Steven Rostedta75fece2010-11-02 14:58:27 -04002000 my $start = $opt{"BISECT_START[$i]"};
2001 my $replay = $opt{"BISECT_REPLAY[$i]"};
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002002 my $start_files = $opt{"BISECT_FILES[$i]"};
2003
2004 if (defined($start_files)) {
2005 $start_files = " -- " . $start_files;
2006 } else {
2007 $start_files = "";
2008 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002009
Steven Rostedta57419b2010-11-02 15:13:54 -04002010 # convert to true sha1's
2011 $good = get_sha1($good);
2012 $bad = get_sha1($bad);
2013
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002014 if (defined($opt{"BISECT_REVERSE[$i]"}) &&
2015 $opt{"BISECT_REVERSE[$i]"} == 1) {
2016 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2017 $reverse_bisect = 1;
2018 } else {
2019 $reverse_bisect = 0;
2020 }
2021
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002022 # Can't have a test without having a test to run
2023 if ($type eq "test" && !defined($run_test)) {
2024 $type = "boot";
2025 }
2026
Steven Rostedtdad98752011-11-22 20:48:57 -05002027 # Check if a bisect was running
2028 my $bisect_start_file = "$builddir/.git/BISECT_START";
2029
Steven Rostedta75fece2010-11-02 14:58:27 -04002030 my $check = $opt{"BISECT_CHECK[$i]"};
Steven Rostedtdad98752011-11-22 20:48:57 -05002031 my $do_check = defined($check) && $check ne "0";
2032
2033 if ( -f $bisect_start_file ) {
2034 print "Bisect in progress found\n";
2035 if ($do_check) {
2036 print " If you say yes, then no checks of good or bad will be done\n";
2037 }
2038 if (defined($replay)) {
2039 print "** BISECT_REPLAY is defined in config file **";
2040 print " Ignore config option and perform new git bisect log?\n";
2041 if (read_ync " (yes, no, or cancel) ") {
2042 $replay = update_bisect_replay;
2043 $do_check = 0;
2044 }
2045 } elsif (read_yn "read git log and continue?") {
2046 $replay = update_bisect_replay;
2047 $do_check = 0;
2048 }
2049 }
2050
2051 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002052
2053 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002054 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002055
2056 if ($check ne "good") {
2057 doprint "TESTING BISECT BAD [$bad]\n";
2058 run_command "git checkout $bad" or
2059 die "Failed to checkout $bad";
2060
2061 $result = run_bisect $type;
2062
2063 if ($result ne "bad") {
2064 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2065 }
2066 }
2067
2068 if ($check ne "bad") {
2069 doprint "TESTING BISECT GOOD [$good]\n";
2070 run_command "git checkout $good" or
2071 die "Failed to checkout $good";
2072
2073 $result = run_bisect $type;
2074
2075 if ($result ne "good") {
2076 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2077 }
2078 }
2079
2080 # checkout where we started
2081 run_command "git checkout $head" or
2082 die "Failed to checkout $head";
2083 }
2084
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002085 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002086 dodie "could not start bisect";
2087
2088 run_command "git bisect good $good" or
2089 dodie "could not set bisect good to $good";
2090
2091 run_git_bisect "git bisect bad $bad" or
2092 dodie "could not set bisect bad to $bad";
2093
2094 if (defined($replay)) {
2095 run_command "git bisect replay $replay" or
2096 dodie "failed to run replay";
2097 }
2098
2099 if (defined($start)) {
2100 run_command "git checkout $start" or
2101 dodie "failed to checkout $start";
2102 }
2103
2104 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002105 do {
2106 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002107 $test = run_git_bisect "git bisect $result";
2108 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002109
2110 run_command "git bisect log" or
2111 dodie "could not capture git bisect log";
2112
2113 run_command "git bisect reset" or
2114 dodie "could not reset git bisect";
2115
2116 doprint "Bad commit was [$bisect_bad]\n";
2117
Steven Rostedt0a05c762010-11-08 11:14:10 -05002118 success $i;
2119}
2120
2121my %config_ignore;
2122my %config_set;
2123
2124my %config_list;
2125my %null_config;
2126
2127my %dependency;
2128
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002129sub assign_configs {
2130 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002131
2132 open (IN, $config)
2133 or dodie "Failed to read $config";
2134
2135 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002136 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002137 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002138 }
2139 }
2140
2141 close(IN);
2142}
2143
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002144sub process_config_ignore {
2145 my ($config) = @_;
2146
2147 assign_configs \%config_ignore, $config;
2148}
2149
Steven Rostedt0a05c762010-11-08 11:14:10 -05002150sub read_current_config {
2151 my ($config_ref) = @_;
2152
2153 %{$config_ref} = ();
2154 undef %{$config_ref};
2155
2156 my @key = keys %{$config_ref};
2157 if ($#key >= 0) {
2158 print "did not delete!\n";
2159 exit;
2160 }
2161 open (IN, "$output_config");
2162
2163 while (<IN>) {
2164 if (/^(CONFIG\S+)=(.*)/) {
2165 ${$config_ref}{$1} = $2;
2166 }
2167 }
2168 close(IN);
2169}
2170
2171sub get_dependencies {
2172 my ($config) = @_;
2173
2174 my $arr = $dependency{$config};
2175 if (!defined($arr)) {
2176 return ();
2177 }
2178
2179 my @deps = @{$arr};
2180
2181 foreach my $dep (@{$arr}) {
2182 print "ADD DEP $dep\n";
2183 @deps = (@deps, get_dependencies $dep);
2184 }
2185
2186 return @deps;
2187}
2188
2189sub create_config {
2190 my @configs = @_;
2191
2192 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2193
2194 foreach my $config (@configs) {
2195 print OUT "$config_set{$config}\n";
2196 my @deps = get_dependencies $config;
2197 foreach my $dep (@deps) {
2198 print OUT "$config_set{$dep}\n";
2199 }
2200 }
2201
2202 foreach my $config (keys %config_ignore) {
2203 print OUT "$config_ignore{$config}\n";
2204 }
2205 close(OUT);
2206
2207# exit;
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002208 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002209}
2210
2211sub compare_configs {
2212 my (%a, %b) = @_;
2213
2214 foreach my $item (keys %a) {
2215 if (!defined($b{$item})) {
2216 print "diff $item\n";
2217 return 1;
2218 }
2219 delete $b{$item};
2220 }
2221
2222 my @keys = keys %b;
2223 if ($#keys) {
2224 print "diff2 $keys[0]\n";
2225 }
2226 return -1 if ($#keys >= 0);
2227
2228 return 0;
2229}
2230
2231sub run_config_bisect_test {
2232 my ($type) = @_;
2233
2234 return run_bisect_test $type, "oldconfig";
2235}
2236
2237sub process_passed {
2238 my (%configs) = @_;
2239
2240 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2241 # Passed! All these configs are part of a good compile.
2242 # Add them to the min options.
2243 foreach my $config (keys %configs) {
2244 if (defined($config_list{$config})) {
2245 doprint " removing $config\n";
2246 $config_ignore{$config} = $config_list{$config};
2247 delete $config_list{$config};
2248 }
2249 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002250 doprint "config copied to $outputdir/config_good\n";
2251 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002252}
2253
2254sub process_failed {
2255 my ($config) = @_;
2256
2257 doprint "\n\n***************************************\n";
2258 doprint "Found bad config: $config\n";
2259 doprint "***************************************\n\n";
2260}
2261
2262sub run_config_bisect {
2263
2264 my @start_list = keys %config_list;
2265
2266 if ($#start_list < 0) {
2267 doprint "No more configs to test!!!\n";
2268 return -1;
2269 }
2270
2271 doprint "***** RUN TEST ***\n";
2272 my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
2273 my $ret;
2274 my %current_config;
2275
2276 my $count = $#start_list + 1;
2277 doprint " $count configs to test\n";
2278
2279 my $half = int($#start_list / 2);
2280
2281 do {
2282 my @tophalf = @start_list[0 .. $half];
2283
2284 create_config @tophalf;
2285 read_current_config \%current_config;
2286
2287 $count = $#tophalf + 1;
2288 doprint "Testing $count configs\n";
2289 my $found = 0;
2290 # make sure we test something
2291 foreach my $config (@tophalf) {
2292 if (defined($current_config{$config})) {
2293 logit " $config\n";
2294 $found = 1;
2295 }
2296 }
2297 if (!$found) {
2298 # try the other half
2299 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002300 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedt0a05c762010-11-08 11:14:10 -05002301 create_config @tophalf;
2302 read_current_config \%current_config;
2303 foreach my $config (@tophalf) {
2304 if (defined($current_config{$config})) {
2305 logit " $config\n";
2306 $found = 1;
2307 }
2308 }
2309 if (!$found) {
2310 doprint "Failed: Can't make new config with current configs\n";
2311 foreach my $config (@start_list) {
2312 doprint " CONFIG: $config\n";
2313 }
2314 return -1;
2315 }
2316 $count = $#tophalf + 1;
2317 doprint "Testing $count configs\n";
2318 }
2319
2320 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002321 if ($bisect_manual) {
2322 $ret = answer_bisect;
2323 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002324 if ($ret) {
2325 process_passed %current_config;
2326 return 0;
2327 }
2328
2329 doprint "This config had a failure.\n";
2330 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002331 doprint "config copied to $outputdir/config_bad\n";
2332 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002333
2334 # A config exists in this group that was bad.
2335 foreach my $config (keys %config_list) {
2336 if (!defined($current_config{$config})) {
2337 doprint " removing $config\n";
2338 delete $config_list{$config};
2339 }
2340 }
2341
2342 @start_list = @tophalf;
2343
2344 if ($#start_list == 0) {
2345 process_failed $start_list[0];
2346 return 1;
2347 }
2348
2349 # remove half the configs we are looking at and see if
2350 # they are good.
2351 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002352 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002353
Steven Rostedtc960bb92011-03-08 09:22:39 -05002354 # we found a single config, try it again unless we are running manually
2355
2356 if ($bisect_manual) {
2357 process_failed $start_list[0];
2358 return 1;
2359 }
2360
Steven Rostedt0a05c762010-11-08 11:14:10 -05002361 my @tophalf = @start_list[0 .. 0];
2362
2363 $ret = run_config_bisect_test $type;
2364 if ($ret) {
2365 process_passed %current_config;
2366 return 0;
2367 }
2368
2369 process_failed $start_list[0];
2370 return 1;
2371}
2372
2373sub config_bisect {
2374 my ($i) = @_;
2375
2376 my $start_config = $opt{"CONFIG_BISECT[$i]"};
2377
2378 my $tmpconfig = "$tmpdir/use_config";
2379
Steven Rostedt30f75da2011-06-13 10:35:35 -04002380 if (defined($config_bisect_good)) {
2381 process_config_ignore $config_bisect_good;
2382 }
2383
Steven Rostedt0a05c762010-11-08 11:14:10 -05002384 # Make the file with the bad config and the min config
2385 if (defined($minconfig)) {
2386 # read the min config for things to ignore
2387 run_command "cp $minconfig $tmpconfig" or
2388 dodie "failed to copy $minconfig to $tmpconfig";
2389 } else {
2390 unlink $tmpconfig;
2391 }
2392
Steven Rostedt0a05c762010-11-08 11:14:10 -05002393 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002394 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002395 process_config_ignore $tmpconfig;
2396 }
2397
2398 # now process the start config
2399 run_command "cp $start_config $output_config" or
2400 dodie "failed to copy $start_config to $output_config";
2401
2402 # read directly what we want to check
2403 my %config_check;
2404 open (IN, $output_config)
2405 or dodie "faied to open $output_config";
2406
2407 while (<IN>) {
2408 if (/^((CONFIG\S*)=.*)/) {
2409 $config_check{$2} = $1;
2410 }
2411 }
2412 close(IN);
2413
Steven Rostedt250bae82011-07-15 22:05:59 -04002414 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002415 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002416
2417 # check to see what we lost (or gained)
2418 open (IN, $output_config)
2419 or dodie "Failed to read $start_config";
2420
2421 my %removed_configs;
2422 my %added_configs;
2423
2424 while (<IN>) {
2425 if (/^((CONFIG\S*)=.*)/) {
2426 # save off all options
2427 $config_set{$2} = $1;
2428 if (defined($config_check{$2})) {
2429 if (defined($config_ignore{$2})) {
2430 $removed_configs{$2} = $1;
2431 } else {
2432 $config_list{$2} = $1;
2433 }
2434 } elsif (!defined($config_ignore{$2})) {
2435 $added_configs{$2} = $1;
2436 $config_list{$2} = $1;
2437 }
2438 }
2439 }
2440 close(IN);
2441
2442 my @confs = keys %removed_configs;
2443 if ($#confs >= 0) {
2444 doprint "Configs overridden by default configs and removed from check:\n";
2445 foreach my $config (@confs) {
2446 doprint " $config\n";
2447 }
2448 }
2449 @confs = keys %added_configs;
2450 if ($#confs >= 0) {
2451 doprint "Configs appearing in make oldconfig and added:\n";
2452 foreach my $config (@confs) {
2453 doprint " $config\n";
2454 }
2455 }
2456
2457 my %config_test;
2458 my $once = 0;
2459
2460 # Sometimes kconfig does weird things. We must make sure
2461 # that the config we autocreate has everything we need
2462 # to test, otherwise we may miss testing configs, or
2463 # may not be able to create a new config.
2464 # Here we create a config with everything set.
2465 create_config (keys %config_list);
2466 read_current_config \%config_test;
2467 foreach my $config (keys %config_list) {
2468 if (!defined($config_test{$config})) {
2469 if (!$once) {
2470 $once = 1;
2471 doprint "Configs not produced by kconfig (will not be checked):\n";
2472 }
2473 doprint " $config\n";
2474 delete $config_list{$config};
2475 }
2476 }
2477 my $ret;
2478 do {
2479 $ret = run_config_bisect;
2480 } while (!$ret);
2481
2482 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002483
2484 success $i;
2485}
2486
Steven Rostedt27d934b2011-05-20 09:18:18 -04002487sub patchcheck_reboot {
2488 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Andrew Jones2728be42011-08-12 15:32:05 +02002489 reboot $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04002490}
2491
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002492sub patchcheck {
2493 my ($i) = @_;
2494
2495 die "PATCHCHECK_START[$i] not defined\n"
2496 if (!defined($opt{"PATCHCHECK_START[$i]"}));
2497 die "PATCHCHECK_TYPE[$i] not defined\n"
2498 if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
2499
2500 my $start = $opt{"PATCHCHECK_START[$i]"};
2501
2502 my $end = "HEAD";
2503 if (defined($opt{"PATCHCHECK_END[$i]"})) {
2504 $end = $opt{"PATCHCHECK_END[$i]"};
2505 }
2506
Steven Rostedta57419b2010-11-02 15:13:54 -04002507 # Get the true sha1's since we can use things like HEAD~3
2508 $start = get_sha1($start);
2509 $end = get_sha1($end);
2510
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002511 my $type = $opt{"PATCHCHECK_TYPE[$i]"};
2512
2513 # Can't have a test without having a test to run
2514 if ($type eq "test" && !defined($run_test)) {
2515 $type = "boot";
2516 }
2517
2518 open (IN, "git log --pretty=oneline $end|") or
2519 dodie "could not get git list";
2520
2521 my @list;
2522
2523 while (<IN>) {
2524 chomp;
2525 $list[$#list+1] = $_;
2526 last if (/^$start/);
2527 }
2528 close(IN);
2529
2530 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002531 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002532 }
2533
2534 # go backwards in the list
2535 @list = reverse @list;
2536
2537 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04002538 my %ignored_warnings;
2539
2540 if (defined($ignore_warnings)) {
2541 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2542 $ignored_warnings{$sha1} = 1;
2543 }
2544 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002545
2546 $in_patchcheck = 1;
2547 foreach my $item (@list) {
2548 my $sha1 = $item;
2549 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2550
2551 doprint "\nProcessing commit $item\n\n";
2552
2553 run_command "git checkout $sha1" or
2554 die "Failed to checkout $sha1";
2555
2556 # only clean on the first and last patch
2557 if ($item eq $list[0] ||
2558 $item eq $list[$#list]) {
2559 $noclean = $save_clean;
2560 } else {
2561 $noclean = 1;
2562 }
2563
2564 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002565 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002566 } else {
2567 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002568 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002569 }
2570
Steven Rostedt19902072011-06-14 20:46:25 -04002571
2572 if (!defined($ignored_warnings{$sha1})) {
2573 check_buildlog $sha1 or return 0;
2574 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002575
2576 next if ($type eq "build");
2577
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002578 my $failed = 0;
2579
Steven Rostedtddf607e2011-06-14 20:49:13 -04002580 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002581
2582 if (!$failed && $type ne "boot"){
2583 do_run_test or $failed = 1;
2584 }
2585 end_monitor;
2586 return 0 if ($failed);
2587
Steven Rostedt27d934b2011-05-20 09:18:18 -04002588 patchcheck_reboot;
2589
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002590 }
2591 $in_patchcheck = 0;
2592 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002593
2594 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002595}
2596
Steven Rostedtb9066f62011-07-15 21:25:24 -04002597my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04002598my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002599my $iflevel = 0;
2600my @ifdeps;
2601
2602# prevent recursion
2603my %read_kconfigs;
2604
Steven Rostedtac6974c2011-10-04 09:40:17 -04002605sub add_dep {
2606 # $config depends on $dep
2607 my ($config, $dep) = @_;
2608
2609 if (defined($depends{$config})) {
2610 $depends{$config} .= " " . $dep;
2611 } else {
2612 $depends{$config} = $dep;
2613 }
2614
2615 # record the number of configs depending on $dep
2616 if (defined $depcount{$dep}) {
2617 $depcount{$dep}++;
2618 } else {
2619 $depcount{$dep} = 1;
2620 }
2621}
2622
Steven Rostedtb9066f62011-07-15 21:25:24 -04002623# taken from streamline_config.pl
2624sub read_kconfig {
2625 my ($kconfig) = @_;
2626
2627 my $state = "NONE";
2628 my $config;
2629 my @kconfigs;
2630
2631 my $cont = 0;
2632 my $line;
2633
2634
2635 if (! -f $kconfig) {
2636 doprint "file $kconfig does not exist, skipping\n";
2637 return;
2638 }
2639
2640 open(KIN, "$kconfig")
2641 or die "Can't open $kconfig";
2642 while (<KIN>) {
2643 chomp;
2644
2645 # Make sure that lines ending with \ continue
2646 if ($cont) {
2647 $_ = $line . " " . $_;
2648 }
2649
2650 if (s/\\$//) {
2651 $cont = 1;
2652 $line = $_;
2653 next;
2654 }
2655
2656 $cont = 0;
2657
2658 # collect any Kconfig sources
2659 if (/^source\s*"(.*)"/) {
2660 $kconfigs[$#kconfigs+1] = $1;
2661 }
2662
2663 # configs found
2664 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2665 $state = "NEW";
2666 $config = $2;
2667
2668 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04002669 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04002670 }
2671
2672 # collect the depends for the config
2673 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2674
Steven Rostedtac6974c2011-10-04 09:40:17 -04002675 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002676
2677 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04002678 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2679
2680 # selected by depends on config
2681 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002682
2683 # Check for if statements
2684 } elsif (/^if\s+(.*\S)\s*$/) {
2685 my $deps = $1;
2686 # remove beginning and ending non text
2687 $deps =~ s/^[^a-zA-Z0-9_]*//;
2688 $deps =~ s/[^a-zA-Z0-9_]*$//;
2689
2690 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2691
2692 $ifdeps[$iflevel++] = join ':', @deps;
2693
2694 } elsif (/^endif/) {
2695
2696 $iflevel-- if ($iflevel);
2697
2698 # stop on "help"
2699 } elsif (/^\s*help\s*$/) {
2700 $state = "NONE";
2701 }
2702 }
2703 close(KIN);
2704
2705 # read in any configs that were found.
2706 foreach $kconfig (@kconfigs) {
2707 if (!defined($read_kconfigs{$kconfig})) {
2708 $read_kconfigs{$kconfig} = 1;
2709 read_kconfig("$builddir/$kconfig");
2710 }
2711 }
2712}
2713
2714sub read_depends {
2715 # find out which arch this is by the kconfig file
2716 open (IN, $output_config)
2717 or dodie "Failed to read $output_config";
2718 my $arch;
2719 while (<IN>) {
2720 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2721 $arch = $1;
2722 last;
2723 }
2724 }
2725 close IN;
2726
2727 if (!defined($arch)) {
2728 doprint "Could not find arch from config file\n";
2729 doprint "no dependencies used\n";
2730 return;
2731 }
2732
2733 # arch is really the subarch, we need to know
2734 # what directory to look at.
2735 if ($arch eq "i386" || $arch eq "x86_64") {
2736 $arch = "x86";
2737 } elsif ($arch =~ /^tile/) {
2738 $arch = "tile";
2739 }
2740
2741 my $kconfig = "$builddir/arch/$arch/Kconfig";
2742
2743 if (! -f $kconfig && $arch =~ /\d$/) {
2744 my $orig = $arch;
2745 # some subarchs have numbers, truncate them
2746 $arch =~ s/\d*$//;
2747 $kconfig = "$builddir/arch/$arch/Kconfig";
2748 if (! -f $kconfig) {
2749 doprint "No idea what arch dir $orig is for\n";
2750 doprint "no dependencies used\n";
2751 return;
2752 }
2753 }
2754
2755 read_kconfig($kconfig);
2756}
2757
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002758sub read_config_list {
2759 my ($config) = @_;
2760
2761 open (IN, $config)
2762 or dodie "Failed to read $config";
2763
2764 while (<IN>) {
2765 if (/^((CONFIG\S*)=.*)/) {
2766 if (!defined($config_ignore{$2})) {
2767 $config_list{$2} = $1;
2768 }
2769 }
2770 }
2771
2772 close(IN);
2773}
2774
2775sub read_output_config {
2776 my ($config) = @_;
2777
2778 assign_configs \%config_ignore, $config;
2779}
2780
2781sub make_new_config {
2782 my @configs = @_;
2783
2784 open (OUT, ">$output_config")
2785 or dodie "Failed to write $output_config";
2786
2787 foreach my $config (@configs) {
2788 print OUT "$config\n";
2789 }
2790 close OUT;
2791}
2792
Steven Rostedtac6974c2011-10-04 09:40:17 -04002793sub chomp_config {
2794 my ($config) = @_;
2795
2796 $config =~ s/CONFIG_//;
2797
2798 return $config;
2799}
2800
Steven Rostedtb9066f62011-07-15 21:25:24 -04002801sub get_depends {
2802 my ($dep) = @_;
2803
Steven Rostedtac6974c2011-10-04 09:40:17 -04002804 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002805
2806 $dep = $depends{"$kconfig"};
2807
2808 # the dep string we have saves the dependencies as they
2809 # were found, including expressions like ! && ||. We
2810 # want to split this out into just an array of configs.
2811
2812 my $valid = "A-Za-z_0-9";
2813
2814 my @configs;
2815
2816 while ($dep =~ /[$valid]/) {
2817
2818 if ($dep =~ /^[^$valid]*([$valid]+)/) {
2819 my $conf = "CONFIG_" . $1;
2820
2821 $configs[$#configs + 1] = $conf;
2822
2823 $dep =~ s/^[^$valid]*[$valid]+//;
2824 } else {
2825 die "this should never happen";
2826 }
2827 }
2828
2829 return @configs;
2830}
2831
2832my %min_configs;
2833my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04002834my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002835my %processed_configs;
2836my %nochange_config;
2837
2838sub test_this_config {
2839 my ($config) = @_;
2840
2841 my $found;
2842
2843 # if we already processed this config, skip it
2844 if (defined($processed_configs{$config})) {
2845 return undef;
2846 }
2847 $processed_configs{$config} = 1;
2848
2849 # if this config failed during this round, skip it
2850 if (defined($nochange_config{$config})) {
2851 return undef;
2852 }
2853
Steven Rostedtac6974c2011-10-04 09:40:17 -04002854 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002855
2856 # Test dependencies first
2857 if (defined($depends{"$kconfig"})) {
2858 my @parents = get_depends $config;
2859 foreach my $parent (@parents) {
2860 # if the parent is in the min config, check it first
2861 next if (!defined($min_configs{$parent}));
2862 $found = test_this_config($parent);
2863 if (defined($found)) {
2864 return $found;
2865 }
2866 }
2867 }
2868
2869 # Remove this config from the list of configs
2870 # do a make oldnoconfig and then read the resulting
2871 # .config to make sure it is missing the config that
2872 # we had before
2873 my %configs = %min_configs;
2874 delete $configs{$config};
2875 make_new_config ((values %configs), (values %keep_configs));
2876 make_oldconfig;
2877 undef %configs;
2878 assign_configs \%configs, $output_config;
2879
2880 return $config if (!defined($configs{$config}));
2881
2882 doprint "disabling config $config did not change .config\n";
2883
2884 $nochange_config{$config} = 1;
2885
2886 return undef;
2887}
2888
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002889sub make_min_config {
2890 my ($i) = @_;
2891
2892 if (!defined($output_minconfig)) {
2893 fail "OUTPUT_MIN_CONFIG not defined" and return;
2894 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04002895
2896 # If output_minconfig exists, and the start_minconfig
2897 # came from min_config, than ask if we should use
2898 # that instead.
2899 if (-f $output_minconfig && !$start_minconfig_defined) {
2900 print "$output_minconfig exists\n";
2901 if (read_yn " Use it as minconfig?") {
2902 $start_minconfig = $output_minconfig;
2903 }
2904 }
2905
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002906 if (!defined($start_minconfig)) {
2907 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
2908 }
2909
Steven Rostedt35ce5952011-07-15 21:57:25 -04002910 my $temp_config = "$tmpdir/temp_config";
2911
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002912 # First things first. We build an allnoconfig to find
2913 # out what the defaults are that we can't touch.
2914 # Some are selections, but we really can't handle selections.
2915
2916 my $save_minconfig = $minconfig;
2917 undef $minconfig;
2918
2919 run_command "$make allnoconfig" or return 0;
2920
Steven Rostedtb9066f62011-07-15 21:25:24 -04002921 read_depends;
2922
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002923 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002924
Steven Rostedt43d1b652011-07-15 22:01:56 -04002925 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002926 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002927
2928 if (defined($ignore_config)) {
2929 # make sure the file exists
2930 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04002931 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002932 }
2933
Steven Rostedt43d1b652011-07-15 22:01:56 -04002934 %keep_configs = %save_configs;
2935
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002936 doprint "Load initial configs from $start_minconfig\n";
2937
2938 # Look at the current min configs, and save off all the
2939 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002940 assign_configs \%min_configs, $start_minconfig;
2941
2942 my @config_keys = keys %min_configs;
2943
Steven Rostedtac6974c2011-10-04 09:40:17 -04002944 # All configs need a depcount
2945 foreach my $config (@config_keys) {
2946 my $kconfig = chomp_config $config;
2947 if (!defined $depcount{$kconfig}) {
2948 $depcount{$kconfig} = 0;
2949 }
2950 }
2951
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002952 # Remove anything that was set by the make allnoconfig
2953 # we shouldn't need them as they get set for us anyway.
2954 foreach my $config (@config_keys) {
2955 # Remove anything in the ignore_config
2956 if (defined($keep_configs{$config})) {
2957 my $file = $ignore_config;
2958 $file =~ s,.*/(.*?)$,$1,;
2959 doprint "$config set by $file ... ignored\n";
2960 delete $min_configs{$config};
2961 next;
2962 }
2963 # But make sure the settings are the same. If a min config
2964 # sets a selection, we do not want to get rid of it if
2965 # it is not the same as what we have. Just move it into
2966 # the keep configs.
2967 if (defined($config_ignore{$config})) {
2968 if ($config_ignore{$config} ne $min_configs{$config}) {
2969 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
2970 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
2971 $keep_configs{$config} = $min_configs{$config};
2972 } else {
2973 doprint "$config set by allnoconfig ... ignored\n";
2974 }
2975 delete $min_configs{$config};
2976 }
2977 }
2978
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002979 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002980 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002981
2982 while (!$done) {
2983
2984 my $config;
2985 my $found;
2986
2987 # Now disable each config one by one and do a make oldconfig
2988 # till we find a config that changes our list.
2989
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002990 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04002991
2992 # Sort keys by who is most dependent on
2993 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
2994 @test_configs ;
2995
2996 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002997 my $reset = 1;
2998 for (my $i = 0; $i < $#test_configs; $i++) {
2999 if (!defined($nochange_config{$test_configs[0]})) {
3000 $reset = 0;
3001 last;
3002 }
3003 # This config didn't change the .config last time.
3004 # Place it at the end
3005 my $config = shift @test_configs;
3006 push @test_configs, $config;
3007 }
3008
3009 # if every test config has failed to modify the .config file
3010 # in the past, then reset and start over.
3011 if ($reset) {
3012 undef %nochange_config;
3013 }
3014
Steven Rostedtb9066f62011-07-15 21:25:24 -04003015 undef %processed_configs;
3016
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003017 foreach my $config (@test_configs) {
3018
Steven Rostedtb9066f62011-07-15 21:25:24 -04003019 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003020
Steven Rostedtb9066f62011-07-15 21:25:24 -04003021 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003022
3023 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003024 }
3025
3026 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003027 # we could have failed due to the nochange_config hash
3028 # reset and try again
3029 if (!$take_two) {
3030 undef %nochange_config;
3031 $take_two = 1;
3032 next;
3033 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003034 doprint "No more configs found that we can disable\n";
3035 $done = 1;
3036 last;
3037 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003038 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003039
3040 $config = $found;
3041
3042 doprint "Test with $config disabled\n";
3043
3044 # set in_bisect to keep build and monitor from dieing
3045 $in_bisect = 1;
3046
3047 my $failed = 0;
3048 build "oldconfig";
3049 start_monitor_and_boot or $failed = 1;
3050 end_monitor;
3051
3052 $in_bisect = 0;
3053
3054 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003055 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003056 # this config is needed, add it to the ignore list.
3057 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003058 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003059 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003060
3061 # update new ignore configs
3062 if (defined($ignore_config)) {
3063 open (OUT, ">$temp_config")
3064 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003065 foreach my $config (keys %save_configs) {
3066 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003067 }
3068 close OUT;
3069 run_command "mv $temp_config $ignore_config" or
3070 dodie "failed to copy update to $ignore_config";
3071 }
3072
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003073 } else {
3074 # We booted without this config, remove it from the minconfigs.
3075 doprint "$config is not needed, disabling\n";
3076
3077 delete $min_configs{$config};
3078
3079 # Also disable anything that is not enabled in this config
3080 my %configs;
3081 assign_configs \%configs, $output_config;
3082 my @config_keys = keys %min_configs;
3083 foreach my $config (@config_keys) {
3084 if (!defined($configs{$config})) {
3085 doprint "$config is not set, disabling\n";
3086 delete $min_configs{$config};
3087 }
3088 }
3089
3090 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003091 open (OUT, ">$temp_config")
3092 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003093 foreach my $config (keys %keep_configs) {
3094 print OUT "$keep_configs{$config}\n";
3095 }
3096 foreach my $config (keys %min_configs) {
3097 print OUT "$min_configs{$config}\n";
3098 }
3099 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003100
3101 run_command "mv $temp_config $output_minconfig" or
3102 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003103 }
3104
3105 doprint "Reboot and wait $sleep_time seconds\n";
Andrew Jones2728be42011-08-12 15:32:05 +02003106 reboot $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003107 }
3108
3109 success $i;
3110 return 1;
3111}
3112
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003113$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003114
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003115if ($#ARGV == 0) {
3116 $ktest_config = $ARGV[0];
3117 if (! -f $ktest_config) {
3118 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003119 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003120 exit 0;
3121 }
3122 }
3123} else {
3124 $ktest_config = "ktest.conf";
3125}
3126
3127if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003128 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003129 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003130 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3131 print OUT << "EOF"
3132# Generated by ktest.pl
3133#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003134
3135# PWD is a ktest.pl variable that will result in the process working
3136# directory that ktest.pl is executed in.
3137
3138# THIS_DIR is automatically assigned the PWD of the path that generated
3139# the config file. It is best to use this variable when assigning other
3140# directory paths within this directory. This allows you to easily
3141# move the test cases to other locations or to other machines.
3142#
3143THIS_DIR := $variable{"PWD"}
3144
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003145# Define each test with TEST_START
3146# The config options below it will override the defaults
3147TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003148TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003149
3150DEFAULTS
3151EOF
3152;
3153 close(OUT);
3154}
3155read_config $ktest_config;
3156
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003157if (defined($opt{"LOG_FILE"})) {
3158 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3159}
3160
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003161# Append any configs entered in manually to the config file.
3162my @new_configs = keys %entered_configs;
3163if ($#new_configs >= 0) {
3164 print "\nAppending entered in configs to $ktest_config\n";
3165 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3166 foreach my $config (@new_configs) {
3167 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003168 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003169 }
3170}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003171
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003172if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3173 unlink $opt{"LOG_FILE"};
3174}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003175
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003176doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3177
Steven Rostedta57419b2010-11-02 15:13:54 -04003178for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3179
3180 if (!$i) {
3181 doprint "DEFAULT OPTIONS:\n";
3182 } else {
3183 doprint "\nTEST $i OPTIONS";
3184 if (defined($repeat_tests{$i})) {
3185 $repeat = $repeat_tests{$i};
3186 doprint " ITERATE $repeat";
3187 }
3188 doprint "\n";
3189 }
3190
3191 foreach my $option (sort keys %opt) {
3192
3193 if ($option =~ /\[(\d+)\]$/) {
3194 next if ($i != $1);
3195 } else {
3196 next if ($i);
3197 }
3198
3199 doprint "$option = $opt{$option}\n";
3200 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003201}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003202
Steven Rostedt2a625122011-05-20 15:48:59 -04003203sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003204 my ($name, $i) = @_;
3205
3206 my $option = "$name\[$i\]";
3207
3208 if (defined($opt{$option})) {
3209 return $opt{$option};
3210 }
3211
Steven Rostedta57419b2010-11-02 15:13:54 -04003212 foreach my $test (keys %repeat_tests) {
3213 if ($i >= $test &&
3214 $i < $test + $repeat_tests{$test}) {
3215 $option = "$name\[$test\]";
3216 if (defined($opt{$option})) {
3217 return $opt{$option};
3218 }
3219 }
3220 }
3221
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003222 if (defined($opt{$name})) {
3223 return $opt{$name};
3224 }
3225
3226 return undef;
3227}
3228
Steven Rostedt2a625122011-05-20 15:48:59 -04003229sub set_test_option {
3230 my ($name, $i) = @_;
3231
3232 my $option = __set_test_option($name, $i);
3233 return $option if (!defined($option));
3234
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003235 return eval_option($option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003236}
3237
Steven Rostedt2545eb62010-11-02 15:01:32 -04003238# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003239for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003240
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003241 # Do not reboot on failing test options
3242 $no_reboot = 1;
3243
Steven Rostedt576f6272010-11-02 14:58:38 -04003244 $iteration = $i;
3245
Steven Rostedta75fece2010-11-02 14:58:27 -04003246 my $makecmd = set_test_option("MAKE_CMD", $i);
3247
3248 $machine = set_test_option("MACHINE", $i);
Steven Rostedte48c5292010-11-02 14:35:37 -04003249 $ssh_user = set_test_option("SSH_USER", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003250 $tmpdir = set_test_option("TMP_DIR", $i);
3251 $outputdir = set_test_option("OUTPUT_DIR", $i);
3252 $builddir = set_test_option("BUILD_DIR", $i);
3253 $test_type = set_test_option("TEST_TYPE", $i);
3254 $build_type = set_test_option("BUILD_TYPE", $i);
3255 $build_options = set_test_option("BUILD_OPTIONS", $i);
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04003256 $pre_build = set_test_option("PRE_BUILD", $i);
3257 $post_build = set_test_option("POST_BUILD", $i);
3258 $pre_build_die = set_test_option("PRE_BUILD_DIE", $i);
3259 $post_build_die = set_test_option("POST_BUILD_DIE", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003260 $power_cycle = set_test_option("POWER_CYCLE", $i);
Steven Rostedte48c5292010-11-02 14:35:37 -04003261 $reboot = set_test_option("REBOOT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003262 $noclean = set_test_option("BUILD_NOCLEAN", $i);
3263 $minconfig = set_test_option("MIN_CONFIG", $i);
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003264 $output_minconfig = set_test_option("OUTPUT_MIN_CONFIG", $i);
3265 $start_minconfig = set_test_option("START_MIN_CONFIG", $i);
3266 $ignore_config = set_test_option("IGNORE_CONFIG", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003267 $run_test = set_test_option("TEST", $i);
3268 $addconfig = set_test_option("ADD_CONFIG", $i);
3269 $reboot_type = set_test_option("REBOOT_TYPE", $i);
3270 $grub_menu = set_test_option("GRUB_MENU", $i);
Steven Rostedt8b37ca82010-11-02 14:58:33 -04003271 $post_install = set_test_option("POST_INSTALL", $i);
Steven Rostedte0a87422011-09-30 17:50:48 -04003272 $no_install = set_test_option("NO_INSTALL", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003273 $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
3274 $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
3275 $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
3276 $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
3277 $power_off = set_test_option("POWER_OFF", $i);
Steven Rostedt576f6272010-11-02 14:58:38 -04003278 $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
3279 $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003280 $sleep_time = set_test_option("SLEEP_TIME", $i);
3281 $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
Steven Rostedt27d934b2011-05-20 09:18:18 -04003282 $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
Steven Rostedt19902072011-06-14 20:46:25 -04003283 $ignore_warnings = set_test_option("IGNORE_WARNINGS", $i);
Steven Rostedtc960bb92011-03-08 09:22:39 -05003284 $bisect_manual = set_test_option("BISECT_MANUAL", $i);
Steven Rostedtc23dca72011-03-08 09:26:31 -05003285 $bisect_skip = set_test_option("BISECT_SKIP", $i);
Steven Rostedt30f75da2011-06-13 10:35:35 -04003286 $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003287 $store_failures = set_test_option("STORE_FAILURES", $i);
Rabin Vincentde5b6e32011-11-18 17:05:31 +05303288 $store_successes = set_test_option("STORE_SUCCESSES", $i);
Steven Rostedt9064af52011-06-13 10:38:48 -04003289 $test_name = set_test_option("TEST_NAME", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003290 $timeout = set_test_option("TIMEOUT", $i);
3291 $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
3292 $console = set_test_option("CONSOLE", $i);
Steven Rostedtf1a5b962011-06-13 10:30:00 -04003293 $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003294 $success_line = set_test_option("SUCCESS_LINE", $i);
Steven Rostedt2b803362011-09-30 18:00:23 -04003295 $reboot_success_line = set_test_option("REBOOT_SUCCESS_LINE", $i);
Steven Rostedt1c8a6172010-11-09 12:55:40 -05003296 $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
3297 $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
Steven Rostedt2d01b262011-03-08 09:47:54 -05003298 $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003299 $build_target = set_test_option("BUILD_TARGET", $i);
Steven Rostedte48c5292010-11-02 14:35:37 -04003300 $ssh_exec = set_test_option("SSH_EXEC", $i);
3301 $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003302 $target_image = set_test_option("TARGET_IMAGE", $i);
3303 $localversion = set_test_option("LOCALVERSION", $i);
3304
Steven Rostedt35ce5952011-07-15 21:57:25 -04003305 $start_minconfig_defined = 1;
3306
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003307 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003308 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003309 $start_minconfig = $minconfig;
3310 }
3311
Steven Rostedta75fece2010-11-02 14:58:27 -04003312 chdir $builddir || die "can't change directory to $builddir";
3313
Andrew Jonesa908a662011-08-12 15:32:03 +02003314 foreach my $dir ($tmpdir, $outputdir) {
3315 if (!-d $dir) {
3316 mkpath($dir) or
3317 die "can't create $dir";
3318 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003319 }
3320
Steven Rostedte48c5292010-11-02 14:35:37 -04003321 $ENV{"SSH_USER"} = $ssh_user;
3322 $ENV{"MACHINE"} = $machine;
3323
Steven Rostedta75fece2010-11-02 14:58:27 -04003324 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303325 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003326 $dmesg = "$tmpdir/dmesg-$machine";
3327 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003328 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003329
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003330 if (!$buildonly) {
3331 $target = "$ssh_user\@$machine";
3332 if ($reboot_type eq "grub") {
3333 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3334 } elsif (!defined($reboot_script)) {
3335 dodie "REBOOT_SCRIPT not defined"
3336 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003337 }
3338
3339 my $run_type = $build_type;
3340 if ($test_type eq "patchcheck") {
3341 $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
3342 } elsif ($test_type eq "bisect") {
3343 $run_type = $opt{"BISECT_TYPE[$i]"};
Steven Rostedt0a05c762010-11-08 11:14:10 -05003344 } elsif ($test_type eq "config_bisect") {
3345 $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
Steven Rostedta75fece2010-11-02 14:58:27 -04003346 }
3347
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003348 if ($test_type eq "make_min_config") {
3349 $run_type = "";
3350 }
3351
Steven Rostedta75fece2010-11-02 14:58:27 -04003352 # mistake in config file?
3353 if (!defined($run_type)) {
3354 $run_type = "ERROR";
3355 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003356
Steven Rostedte0a87422011-09-30 17:50:48 -04003357 my $installme = "";
3358 $installme = " no_install" if ($no_install);
3359
Steven Rostedt2545eb62010-11-02 15:01:32 -04003360 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003361 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003362
3363 unlink $dmesg;
3364 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303365 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003366
Steven Rostedt250bae82011-07-15 22:05:59 -04003367 if (defined($addconfig)) {
3368 my $min = $minconfig;
3369 if (!defined($minconfig)) {
3370 $min = "";
3371 }
3372 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003373 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003374 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003375 }
3376
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003377 my $checkout = $opt{"CHECKOUT[$i]"};
3378 if (defined($checkout)) {
3379 run_command "git checkout $checkout" or
3380 die "failed to checkout $checkout";
3381 }
3382
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003383 $no_reboot = 0;
3384
3385
Steven Rostedta75fece2010-11-02 14:58:27 -04003386 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003387 bisect $i;
3388 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003389 } elsif ($test_type eq "config_bisect") {
3390 config_bisect $i;
3391 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003392 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003393 patchcheck $i;
3394 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003395 } elsif ($test_type eq "make_min_config") {
3396 make_min_config $i;
3397 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003398 }
3399
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003400 if ($build_type ne "nobuild") {
3401 build $build_type or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003402 }
3403
Steven Rostedtcd8e3682011-08-18 16:35:44 -04003404 if ($test_type eq "install") {
3405 get_version;
3406 install;
3407 success $i;
3408 next;
3409 }
3410
Steven Rostedta75fece2010-11-02 14:58:27 -04003411 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04003412 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04003413 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04003414
3415 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3416 do_run_test or $failed = 1;
3417 }
3418 end_monitor;
3419 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003420 }
3421
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003422 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003423}
3424
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003425if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003426 halt;
Steven Rostedt576f6272010-11-02 14:58:38 -04003427} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003428 reboot;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003429}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003430
Steven Rostedte48c5292010-11-02 14:35:37 -04003431doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3432
Steven Rostedt2545eb62010-11-02 15:01:32 -04003433exit 0;