ktest: Add options SWITCH_TO_GOOD and SWITCH_TO_TEST
[linux-2.6-block.git] / tools / testing / ktest / ktest.pl
CommitLineData
2545eb61 1#!/usr/bin/perl -w
d6ce2a0b 2#
cce1dac8 3# Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
d6ce2a0b
SR
4# Licensed under the terms of the GNU GPL License version 2
5#
2545eb61
SR
6
7use strict;
8use IPC::Open2;
9use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
7faafbd6
SR
10use File::Path qw(mkpath);
11use File::Copy qw(cp);
2545eb61
SR
12use FileHandle;
13
e48c5293
SR
14my $VERSION = "0.2";
15
2545eb61
SR
16$| = 1;
17
18my %opt;
a57419b3
SR
19my %repeat_tests;
20my %repeats;
a75fecec 21my %default;
2545eb61
SR
22
23#default opts
a57419b3 24$default{"NUM_TESTS"} = 1;
bb8474b1 25$default{"TEST_TYPE"} = "build";
a75fecec
SR
26$default{"BUILD_TYPE"} = "randconfig";
27$default{"MAKE_CMD"} = "make";
28$default{"TIMEOUT"} = 120;
48920630 29$default{"TMP_DIR"} = "/tmp/ktest/\${MACHINE}";
a75fecec
SR
30$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
27d934b2 38$default{"PATCHCHECK_SLEEP_TIME"} = 60; # sleep time between patch checks
a75fecec 39$default{"CLEAR_LOG"} = 0;
c960bb9f 40$default{"BISECT_MANUAL"} = 0;
c23dca7c 41$default{"BISECT_SKIP"} = 1;
a75fecec 42$default{"SUCCESS_LINE"} = "login:";
f1a5b962 43$default{"DETECT_TRIPLE_FAULT"} = 1;
e0a8742e 44$default{"NO_INSTALL"} = 0;
a75fecec
SR
45$default{"BOOTED_TIMEOUT"} = 1;
46$default{"DIE_ON_FAILURE"} = 1;
e48c5293
SR
47$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";
1c8a617a
SR
50$default{"STOP_AFTER_SUCCESS"} = 10;
51$default{"STOP_AFTER_FAILURE"} = 60;
2d01b26a 52$default{"STOP_TEST_AFTER"} = 600;
600bbf0a
SR
53
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";
8d1491ba 57$default{"LOCALVERSION"} = "-test";
600bbf0a
SR
58$default{"SSH_USER"} = "root";
59$default{"BUILD_TARGET"} = "arch/x86/boot/bzImage";
60$default{"TARGET_IMAGE"} = "/boot/vmlinuz-test";
2545eb61 61
8d1491ba 62my $ktest_config;
2545eb61 63my $version;
a75fecec 64my $machine;
e48c5293 65my $ssh_user;
a75fecec
SR
66my $tmpdir;
67my $builddir;
68my $outputdir;
51ad1dd1 69my $output_config;
a75fecec 70my $test_type;
7faafbd6 71my $build_type;
a75fecec 72my $build_options;
0bd6c1a3
SR
73my $pre_build;
74my $post_build;
75my $pre_build_die;
76my $post_build_die;
a75fecec
SR
77my $reboot_type;
78my $reboot_script;
79my $power_cycle;
e48c5293 80my $reboot;
a75fecec 81my $reboot_on_error;
bc7c5803
SR
82my $switch_to_good;
83my $switch_to_test;
a75fecec
SR
84my $poweroff_on_error;
85my $die_on_failure;
576f627c
SR
86my $powercycle_after_reboot;
87my $poweroff_after_halt;
e48c5293
SR
88my $ssh_exec;
89my $scp_to_target;
a75fecec
SR
90my $power_off;
91my $grub_menu;
2545eb61
SR
92my $grub_number;
93my $target;
94my $make;
8b37ca8c 95my $post_install;
e0a8742e 96my $no_install;
5c42fc5b 97my $noclean;
5f9b6ced 98my $minconfig;
4c4ab120 99my $start_minconfig;
35ce5952 100my $start_minconfig_defined;
4c4ab120
SR
101my $output_minconfig;
102my $ignore_config;
2b7d9b21 103my $addconfig;
5f9b6ced
SR
104my $in_bisect = 0;
105my $bisect_bad = "";
d6ce2a0b 106my $reverse_bisect;
c960bb9f 107my $bisect_manual;
c23dca7c 108my $bisect_skip;
30f75da5 109my $config_bisect_good;
c5dacb88
SR
110my $bisect_ret_good;
111my $bisect_ret_bad;
112my $bisect_ret_skip;
113my $bisect_ret_abort;
114my $bisect_ret_default;
6c5ee0be 115my $in_patchcheck = 0;
5a391fbf 116my $run_test;
6c5ee0be 117my $redirect;
7faafbd6 118my $buildlog;
a9dd5d63 119my $testlog;
7faafbd6
SR
120my $dmesg;
121my $monitor_fp;
122my $monitor_pid;
123my $monitor_cnt = 0;
a75fecec
SR
124my $sleep_time;
125my $bisect_sleep_time;
27d934b2 126my $patchcheck_sleep_time;
1990207d 127my $ignore_warnings;
a75fecec 128my $store_failures;
de5b6e3b 129my $store_successes;
9064af52 130my $test_name;
a75fecec
SR
131my $timeout;
132my $booted_timeout;
f1a5b962 133my $detect_triplefault;
a75fecec 134my $console;
2b803365 135my $reboot_success_line;
a75fecec 136my $success_line;
1c8a617a
SR
137my $stop_after_success;
138my $stop_after_failure;
2d01b26a 139my $stop_test_after;
a75fecec
SR
140my $build_target;
141my $target_image;
142my $localversion;
576f627c 143my $iteration = 0;
e48c5293 144my $successes = 0;
2545eb61 145
165708b2 146# set when a test is something other that just building or install
bb8474b1
SR
147# which would require more options.
148my $buildonly = 1;
149
dbd3783b
SR
150# set when creating a new config
151my $newconfig = 0;
152
8d1491ba
SR
153my %entered_configs;
154my %config_help;
77d942ce 155my %variable;
fcb3f16a 156my %force_config;
8d1491ba 157
4ab1cce5
SR
158# do not force reboots on config problems
159my $no_reboot = 1;
160
7bf51073
SR
161# default variables that can be used
162chomp ($variable{"PWD"} = `pwd`);
163
8d1491ba
SR
164$config_help{"MACHINE"} = << "EOF"
165 The machine hostname that you will test.
bb8474b1 166 For build only tests, it is still needed to differentiate log files.
8d1491ba
SR
167EOF
168 ;
169$config_help{"SSH_USER"} = << "EOF"
170 The box is expected to have ssh on normal bootup, provide the user
171 (most likely root, since you need privileged operations)
172EOF
173 ;
174$config_help{"BUILD_DIR"} = << "EOF"
175 The directory that contains the Linux source code (full path).
0e7a22de
SR
176 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.
8d1491ba
SR
178EOF
179 ;
180$config_help{"OUTPUT_DIR"} = << "EOF"
181 The directory that the objects will be built (full path).
182 (can not be same as BUILD_DIR)
0e7a22de
SR
183 You can use \${PWD} that will be the path where ktest.pl is run, or use
184 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
8d1491ba
SR
185EOF
186 ;
187$config_help{"BUILD_TARGET"} = << "EOF"
188 The location of the compiled file to copy to the target.
189 (relative to OUTPUT_DIR)
190EOF
191 ;
dbd3783b
SR
192$config_help{"BUILD_OPTIONS"} = << "EOF"
193 Options to add to \"make\" when building.
194 i.e. -j20
195EOF
196 ;
8d1491ba
SR
197$config_help{"TARGET_IMAGE"} = << "EOF"
198 The place to put your image on the test machine.
199EOF
200 ;
201$config_help{"POWER_CYCLE"} = << "EOF"
202 A script or command to reboot the box.
203
204 Here is a digital loggers power switch example
205 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
206
207 Here is an example to reboot a virtual box on the current host
208 with the name "Guest".
209 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
210EOF
211 ;
212$config_help{"CONSOLE"} = << "EOF"
213 The script or command that reads the console
214
215 If you use ttywatch server, something like the following would work.
216CONSOLE = nc -d localhost 3001
217
218 For a virtual machine with guest name "Guest".
219CONSOLE = virsh console Guest
220EOF
221 ;
222$config_help{"LOCALVERSION"} = << "EOF"
223 Required version ending to differentiate the test
224 from other linux builds on the system.
225EOF
226 ;
227$config_help{"REBOOT_TYPE"} = << "EOF"
228 Way to reboot the box to the test kernel.
229 Only valid options so far are "grub" and "script".
230
231 If you specify grub, it will assume grub version 1
232 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
233 and select that target to reboot to the kernel. If this is not
234 your setup, then specify "script" and have a command or script
235 specified in REBOOT_SCRIPT to boot to the target.
236
237 The entry in /boot/grub/menu.lst must be entered in manually.
238 The test will not modify that file.
239EOF
240 ;
241$config_help{"GRUB_MENU"} = << "EOF"
242 The grub title name for the test kernel to boot
243 (Only mandatory if REBOOT_TYPE = grub)
244
245 Note, ktest.pl will not update the grub menu.lst, you need to
246 manually add an option for the test. ktest.pl will search
247 the grub menu.lst for this option to find what kernel to
248 reboot into.
249
250 For example, if in the /boot/grub/menu.lst the test kernel title has:
251 title Test Kernel
252 kernel vmlinuz-test
253 GRUB_MENU = Test Kernel
254EOF
255 ;
256$config_help{"REBOOT_SCRIPT"} = << "EOF"
257 A script to reboot the target into the test kernel
258 (Only mandatory if REBOOT_TYPE = script)
259EOF
260 ;
261
dad98754
SR
262sub read_prompt {
263 my ($cancel, $prompt) = @_;
35ce5952
SR
264
265 my $ans;
266
267 for (;;) {
dad98754
SR
268 if ($cancel) {
269 print "$prompt [y/n/C] ";
270 } else {
271 print "$prompt [Y/n] ";
272 }
35ce5952
SR
273 $ans = <STDIN>;
274 chomp $ans;
275 if ($ans =~ /^\s*$/) {
dad98754
SR
276 if ($cancel) {
277 $ans = "c";
278 } else {
279 $ans = "y";
280 }
35ce5952
SR
281 }
282 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
dad98754
SR
283 if ($cancel) {
284 last if ($ans =~ /^c$/i);
285 print "Please answer either 'y', 'n' or 'c'.\n";
286 } else {
287 print "Please answer either 'y' or 'n'.\n";
288 }
289 }
290 if ($ans =~ /^c/i) {
291 exit;
35ce5952
SR
292 }
293 if ($ans !~ /^y$/i) {
294 return 0;
295 }
296 return 1;
297}
8d1491ba 298
dad98754
SR
299sub read_yn {
300 my ($prompt) = @_;
301
302 return read_prompt 0, $prompt;
303}
304
305sub read_ync {
306 my ($prompt) = @_;
307
308 return read_prompt 1, $prompt;
309}
310
8d1491ba
SR
311sub get_ktest_config {
312 my ($config) = @_;
815e2bd7 313 my $ans;
8d1491ba
SR
314
315 return if (defined($opt{$config}));
316
317 if (defined($config_help{$config})) {
318 print "\n";
319 print $config_help{$config};
320 }
321
322 for (;;) {
323 print "$config = ";
dbd3783b 324 if (defined($default{$config}) && length($default{$config})) {
8d1491ba
SR
325 print "\[$default{$config}\] ";
326 }
815e2bd7
SR
327 $ans = <STDIN>;
328 $ans =~ s/^\s*(.*\S)\s*$/$1/;
329 if ($ans =~ /^\s*$/) {
8d1491ba 330 if ($default{$config}) {
815e2bd7 331 $ans = $default{$config};
8d1491ba
SR
332 } else {
333 print "Your answer can not be blank\n";
334 next;
335 }
336 }
0e7a22de 337 $entered_configs{$config} = ${ans};
8d1491ba
SR
338 last;
339 }
340}
341
342sub get_ktest_configs {
343 get_ktest_config("MACHINE");
8d1491ba
SR
344 get_ktest_config("BUILD_DIR");
345 get_ktest_config("OUTPUT_DIR");
bb8474b1 346
dbd3783b
SR
347 if ($newconfig) {
348 get_ktest_config("BUILD_OPTIONS");
349 }
350
bb8474b1
SR
351 # options required for other than just building a kernel
352 if (!$buildonly) {
165708b2
SR
353 get_ktest_config("POWER_CYCLE");
354 get_ktest_config("CONSOLE");
355 }
356
357 # options required for install and more
358 if ($buildonly != 1) {
bb8474b1
SR
359 get_ktest_config("SSH_USER");
360 get_ktest_config("BUILD_TARGET");
361 get_ktest_config("TARGET_IMAGE");
bb8474b1
SR
362 }
363
8d1491ba
SR
364 get_ktest_config("LOCALVERSION");
365
bb8474b1
SR
366 return if ($buildonly);
367
8d1491ba
SR
368 my $rtype = $opt{"REBOOT_TYPE"};
369
370 if (!defined($rtype)) {
371 if (!defined($opt{"GRUB_MENU"})) {
372 get_ktest_config("REBOOT_TYPE");
373 $rtype = $entered_configs{"REBOOT_TYPE"};
374 } else {
375 $rtype = "grub";
376 }
377 }
378
379 if ($rtype eq "grub") {
380 get_ktest_config("GRUB_MENU");
381 } else {
382 get_ktest_config("REBOOT_SCRIPT");
383 }
384}
385
77d942ce 386sub process_variables {
8d735212 387 my ($value, $remove_undef) = @_;
77d942ce
SR
388 my $retval = "";
389
390 # We want to check for '\', and it is just easier
391 # to check the previous characet of '$' and not need
392 # to worry if '$' is the first character. By adding
393 # a space to $value, we can just check [^\\]\$ and
394 # it will still work.
395 $value = " $value";
396
397 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
398 my $begin = $1;
399 my $var = $2;
400 my $end = $3;
401 # append beginning of value to retval
402 $retval = "$retval$begin";
403 if (defined($variable{$var})) {
404 $retval = "$retval$variable{$var}";
8d735212
SR
405 } elsif (defined($remove_undef) && $remove_undef) {
406 # for if statements, any variable that is not defined,
407 # we simple convert to 0
408 $retval = "${retval}0";
77d942ce
SR
409 } else {
410 # put back the origin piece.
411 $retval = "$retval\$\{$var\}";
412 }
413 $value = $end;
414 }
415 $retval = "$retval$value";
416
417 # remove the space added in the beginning
418 $retval =~ s/ //;
419
420 return "$retval"
421}
422
a57419b3 423sub set_value {
3d1cc414 424 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
a57419b3 425
cad96669
SR
426 my $prvalue = process_variables($rvalue);
427
428 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
bb8474b1
SR
429 # Note if a test is something other than build, then we
430 # will need other manditory options.
cad96669 431 if ($prvalue ne "install") {
165708b2
SR
432 $buildonly = 0;
433 } else {
434 # install still limits some manditory options.
435 $buildonly = 2;
436 }
bb8474b1
SR
437 }
438
a57419b3 439 if (defined($opt{$lvalue})) {
3d1cc414
SR
440 if (!$override || defined(${$overrides}{$lvalue})) {
441 my $extra = "";
442 if ($override) {
443 $extra = "In the same override section!\n";
444 }
445 die "$name: $.: Option $lvalue defined more than once!\n$extra";
446 }
cad96669 447 ${$overrides}{$lvalue} = $prvalue;
a57419b3 448 }
21a9679f
SR
449 if ($rvalue =~ /^\s*$/) {
450 delete $opt{$lvalue};
451 } else {
cad96669 452 $opt{$lvalue} = $prvalue;
21a9679f 453 }
a57419b3
SR
454}
455
77d942ce
SR
456sub set_variable {
457 my ($lvalue, $rvalue) = @_;
458
459 if ($rvalue =~ /^\s*$/) {
460 delete $variable{$lvalue};
461 } else {
462 $rvalue = process_variables($rvalue);
463 $variable{$lvalue} = $rvalue;
464 }
465}
466
ab7a3f52
SR
467sub process_compare {
468 my ($lval, $cmp, $rval) = @_;
469
470 # remove whitespace
471
472 $lval =~ s/^\s*//;
473 $lval =~ s/\s*$//;
474
475 $rval =~ s/^\s*//;
476 $rval =~ s/\s*$//;
477
478 if ($cmp eq "==") {
479 return $lval eq $rval;
480 } elsif ($cmp eq "!=") {
481 return $lval ne $rval;
482 }
483
484 my $statement = "$lval $cmp $rval";
485 my $ret = eval $statement;
486
487 # $@ stores error of eval
488 if ($@) {
489 return -1;
490 }
491
492 return $ret;
493}
494
9900b5dc
SR
495sub value_defined {
496 my ($val) = @_;
497
498 return defined($variable{$2}) ||
499 defined($opt{$2});
500}
501
8d735212
SR
502my $d = 0;
503sub process_expression {
504 my ($name, $val) = @_;
505
506 my $c = $d++;
507
508 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
509 my $express = $1;
510
511 if (process_expression($name, $express)) {
512 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
513 } else {
514 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
515 }
516 }
517
518 $d--;
519 my $OR = "\\|\\|";
520 my $AND = "\\&\\&";
45d73a5d 521
8d735212
SR
522 while ($val =~ s/^(.*?)($OR|$AND)//) {
523 my $express = $1;
524 my $op = $2;
525
526 if (process_expression($name, $express)) {
527 if ($op eq "||") {
528 return 1;
529 }
530 } else {
531 if ($op eq "&&") {
532 return 0;
533 }
534 }
535 }
45d73a5d 536
ab7a3f52
SR
537 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
538 my $ret = process_compare($1, $2, $3);
539 if ($ret < 0) {
540 die "$name: $.: Unable to process comparison\n";
541 }
542 return $ret;
543 }
544
9900b5dc
SR
545 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
546 if (defined $1) {
547 return !value_defined($2);
548 } else {
549 return value_defined($2);
550 }
551 }
552
45d73a5d
SR
553 if ($val =~ /^\s*0\s*$/) {
554 return 0;
555 } elsif ($val =~ /^\s*\d+\s*$/) {
556 return 1;
557 }
558
9900b5dc 559 die ("$name: $.: Undefined content $val in if statement\n");
8d735212
SR
560}
561
562sub process_if {
563 my ($name, $value) = @_;
564
565 # Convert variables and replace undefined ones with 0
566 my $val = process_variables($value, 1);
567 my $ret = process_expression $name, $val;
568
569 return $ret;
45d73a5d
SR
570}
571
2ed3b161
SR
572sub __read_config {
573 my ($config, $current_test_num) = @_;
2545eb61 574
2ed3b161
SR
575 my $in;
576 open($in, $config) || die "can't read file $config";
2545eb61 577
a57419b3
SR
578 my $name = $config;
579 $name =~ s,.*/(.*),$1,;
580
2ed3b161 581 my $test_num = $$current_test_num;
a57419b3
SR
582 my $default = 1;
583 my $repeat = 1;
584 my $num_tests_set = 0;
585 my $skip = 0;
586 my $rest;
a9f84424 587 my $line;
0df213ca 588 my $test_case = 0;
45d73a5d
SR
589 my $if = 0;
590 my $if_set = 0;
3d1cc414
SR
591 my $override = 0;
592
593 my %overrides;
a57419b3 594
2ed3b161 595 while (<$in>) {
2545eb61
SR
596
597 # ignore blank lines and comments
598 next if (/^\s*$/ || /\s*\#/);
599
0050b6bb 600 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
a57419b3 601
0050b6bb
SR
602 my $type = $1;
603 $rest = $2;
a9f84424 604 $line = $2;
a57419b3 605
0050b6bb
SR
606 my $old_test_num;
607 my $old_repeat;
3d1cc414 608 $override = 0;
0050b6bb
SR
609
610 if ($type eq "TEST_START") {
611
612 if ($num_tests_set) {
613 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
614 }
a57419b3 615
0050b6bb
SR
616 $old_test_num = $test_num;
617 $old_repeat = $repeat;
a57419b3 618
0050b6bb
SR
619 $test_num += $repeat;
620 $default = 0;
621 $repeat = 1;
622 } else {
623 $default = 1;
624 }
a57419b3 625
a9f84424
SR
626 # If SKIP is anywhere in the line, the command will be skipped
627 if ($rest =~ s/\s+SKIP\b//) {
a57419b3
SR
628 $skip = 1;
629 } else {
0df213ca 630 $test_case = 1;
a57419b3
SR
631 $skip = 0;
632 }
633
a9f84424
SR
634 if ($rest =~ s/\sELSE\b//) {
635 if (!$if) {
636 die "$name: $.: ELSE found with out matching IF section\n$_";
637 }
638 $if = 0;
639
640 if ($if_set) {
641 $skip = 1;
642 } else {
643 $skip = 0;
3d1cc414 644 }
a57419b3
SR
645 }
646
a9f84424 647 if ($rest =~ s/\sIF\s+(.*)//) {
45d73a5d
SR
648 if (process_if($name, $1)) {
649 $if_set = 1;
650 } else {
651 $skip = 1;
652 }
653 $if = 1;
654 } else {
655 $if = 0;
a9f84424
SR
656 $if_set = 0;
657 }
658
659 if (!$skip) {
660 if ($type eq "TEST_START") {
661 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
662 $repeat = $1;
663 $repeat_tests{"$test_num"} = $repeat;
664 }
665 } elsif ($rest =~ s/\sOVERRIDE\b//) {
666 # DEFAULT only
667 $override = 1;
668 # Clear previous overrides
669 %overrides = ();
670 }
a57419b3
SR
671 }
672
a9f84424 673 if (!$skip && $rest !~ /^\s*$/) {
0050b6bb 674 die "$name: $.: Gargbage found after $type\n$_";
a57419b3
SR
675 }
676
0050b6bb 677 if ($skip && $type eq "TEST_START") {
a57419b3 678 $test_num = $old_test_num;
e48c5293 679 $repeat = $old_repeat;
a57419b3
SR
680 }
681
ab7a3f52 682 } elsif (/^\s*ELSE\b(.*)$/) {
45d73a5d
SR
683 if (!$if) {
684 die "$name: $.: ELSE found with out matching IF section\n$_";
685 }
686 $rest = $1;
687 if ($if_set) {
688 $skip = 1;
ab7a3f52 689 $rest = "";
45d73a5d
SR
690 } else {
691 $skip = 0;
692
ab7a3f52 693 if ($rest =~ /\sIF\s+(.*)/) {
45d73a5d
SR
694 # May be a ELSE IF section.
695 if (!process_if($name, $1)) {
696 $skip = 1;
697 }
ab7a3f52 698 $rest = "";
45d73a5d
SR
699 } else {
700 $if = 0;
701 }
702 }
703
ab7a3f52
SR
704 if ($rest !~ /^\s*$/) {
705 die "$name: $.: Gargbage found after DEFAULTS\n$_";
706 }
707
2ed3b161
SR
708 } elsif (/^\s*INCLUDE\s+(\S+)/) {
709
710 next if ($skip);
711
712 if (!$default) {
713 die "$name: $.: INCLUDE can only be done in default sections\n$_";
714 }
715
716 my $file = process_variables($1);
717
718 if ($file !~ m,^/,) {
719 # check the path of the config file first
720 if ($config =~ m,(.*)/,) {
721 if (-f "$1/$file") {
722 $file = "$1/$file";
723 }
724 }
725 }
726
727 if ( ! -r $file ) {
728 die "$name: $.: Can't read file $file\n$_";
729 }
730
731 if (__read_config($file, \$test_num)) {
732 $test_case = 1;
733 }
734
a57419b3
SR
735 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
736
737 next if ($skip);
738
2545eb61
SR
739 my $lvalue = $1;
740 my $rvalue = $2;
741
a57419b3
SR
742 if (!$default &&
743 ($lvalue eq "NUM_TESTS" ||
744 $lvalue eq "LOG_FILE" ||
745 $lvalue eq "CLEAR_LOG")) {
746 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
747 }
748
749 if ($lvalue eq "NUM_TESTS") {
750 if ($test_num) {
751 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
752 }
753 if (!$default) {
754 die "$name: $.: NUM_TESTS must be set in default section\n";
755 }
756 $num_tests_set = 1;
757 }
758
759 if ($default || $lvalue =~ /\[\d+\]$/) {
3d1cc414 760 set_value($lvalue, $rvalue, $override, \%overrides, $name);
a57419b3
SR
761 } else {
762 my $val = "$lvalue\[$test_num\]";
3d1cc414 763 set_value($val, $rvalue, $override, \%overrides, $name);
a57419b3
SR
764
765 if ($repeat > 1) {
766 $repeats{$val} = $repeat;
767 }
a75fecec 768 }
77d942ce
SR
769 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
770 next if ($skip);
771
772 my $lvalue = $1;
773 my $rvalue = $2;
774
775 # process config variables.
776 # Config variables are only active while reading the
777 # config and can be defined anywhere. They also ignore
778 # TEST_START and DEFAULTS, but are skipped if they are in
779 # on of these sections that have SKIP defined.
780 # The save variable can be
781 # defined multiple times and the new one simply overrides
782 # the prevous one.
783 set_variable($lvalue, $rvalue);
784
a57419b3
SR
785 } else {
786 die "$name: $.: Garbage found in config\n$_";
2545eb61
SR
787 }
788 }
789
a57419b3
SR
790 if ($test_num) {
791 $test_num += $repeat - 1;
792 $opt{"NUM_TESTS"} = $test_num;
793 }
794
2ed3b161
SR
795 close($in);
796
797 $$current_test_num = $test_num;
798
799 return $test_case;
800}
801
c4261d0f
SR
802sub get_test_case {
803 print "What test case would you like to run?\n";
804 print " (build, install or boot)\n";
805 print " Other tests are available but require editing the config file\n";
806 my $ans = <STDIN>;
807 chomp $ans;
808 $default{"TEST_TYPE"} = $ans;
809}
810
2ed3b161
SR
811sub read_config {
812 my ($config) = @_;
813
814 my $test_case;
815 my $test_num = 0;
816
817 $test_case = __read_config $config, \$test_num;
818
8d1491ba
SR
819 # make sure we have all mandatory configs
820 get_ktest_configs;
821
0df213ca
SR
822 # was a test specified?
823 if (!$test_case) {
824 print "No test case specified.\n";
c4261d0f 825 get_test_case;
0df213ca
SR
826 }
827
a75fecec
SR
828 # set any defaults
829
830 foreach my $default (keys %default) {
831 if (!defined($opt{$default})) {
832 $opt{$default} = $default{$default};
833 }
834 }
2545eb61
SR
835}
836
23715c3c
SR
837sub __eval_option {
838 my ($option, $i) = @_;
839
840 # Add space to evaluate the character before $
841 $option = " $option";
842 my $retval = "";
f9dfb65b
RV
843 my $repeated = 0;
844 my $parent = 0;
845
846 foreach my $test (keys %repeat_tests) {
847 if ($i >= $test &&
848 $i < $test + $repeat_tests{$test}) {
849
850 $repeated = 1;
851 $parent = $test;
852 last;
853 }
854 }
23715c3c
SR
855
856 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
857 my $start = $1;
858 my $var = $2;
859 my $end = $3;
860
861 # Append beginning of line
862 $retval = "$retval$start";
863
864 # If the iteration option OPT[$i] exists, then use that.
865 # otherwise see if the default OPT (without [$i]) exists.
866
867 my $o = "$var\[$i\]";
f9dfb65b 868 my $parento = "$var\[$parent\]";
23715c3c
SR
869
870 if (defined($opt{$o})) {
871 $o = $opt{$o};
872 $retval = "$retval$o";
f9dfb65b
RV
873 } elsif ($repeated && defined($opt{$parento})) {
874 $o = $opt{$parento};
875 $retval = "$retval$o";
23715c3c
SR
876 } elsif (defined($opt{$var})) {
877 $o = $opt{$var};
878 $retval = "$retval$o";
879 } else {
880 $retval = "$retval\$\{$var\}";
881 }
882
883 $option = $end;
884 }
885
886 $retval = "$retval$option";
887
888 $retval =~ s/^ //;
889
890 return $retval;
891}
892
893sub eval_option {
894 my ($option, $i) = @_;
895
896 my $prev = "";
897
898 # Since an option can evaluate to another option,
899 # keep iterating until we do not evaluate any more
900 # options.
901 my $r = 0;
902 while ($prev ne $option) {
903 # Check for recursive evaluations.
904 # 100 deep should be more than enough.
905 if ($r++ > 100) {
906 die "Over 100 evaluations accurred with $option\n" .
907 "Check for recursive variables\n";
908 }
909 $prev = $option;
910 $option = __eval_option($option, $i);
911 }
912
913 return $option;
914}
915
d1e2f22a 916sub _logit {
2545eb61
SR
917 if (defined($opt{"LOG_FILE"})) {
918 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
919 print OUT @_;
920 close(OUT);
921 }
922}
923
d1e2f22a
SR
924sub logit {
925 if (defined($opt{"LOG_FILE"})) {
926 _logit @_;
927 } else {
928 print @_;
929 }
930}
931
5f9b6ced
SR
932sub doprint {
933 print @_;
d1e2f22a 934 _logit @_;
5f9b6ced
SR
935}
936
7faafbd6 937sub run_command;
2728be41
AJ
938sub start_monitor;
939sub end_monitor;
940sub wait_for_monitor;
7faafbd6
SR
941
942sub reboot {
2728be41
AJ
943 my ($time) = @_;
944
2b803365
SR
945 if (defined($time)) {
946 start_monitor;
947 # flush out current monitor
948 # May contain the reboot success line
949 wait_for_monitor 1;
950 }
951
7faafbd6 952 # try to reboot normally
e48c5293 953 if (run_command $reboot) {
576f627c
SR
954 if (defined($powercycle_after_reboot)) {
955 sleep $powercycle_after_reboot;
956 run_command "$power_cycle";
957 }
958 } else {
7faafbd6 959 # nope? power cycle it.
a75fecec 960 run_command "$power_cycle";
7faafbd6 961 }
2728be41
AJ
962
963 if (defined($time)) {
2b803365 964 wait_for_monitor($time, $reboot_success_line);
2728be41
AJ
965 end_monitor;
966 }
7faafbd6
SR
967}
968
bc7c5803
SR
969sub reboot_to_good {
970 my ($time) = @_;
971
972 if (defined($switch_to_good)) {
973 run_command $switch_to_good;
974 return;
975 }
976
977 reboot $time;
978}
979
576f627c
SR
980sub do_not_reboot {
981 my $i = $iteration;
982
4ab1cce5 983 return $test_type eq "build" || $no_reboot ||
576f627c
SR
984 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
985 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
986}
987
5c42fc5b 988sub dodie {
5a391fbf 989 doprint "CRITICAL FAILURE... ", @_, "\n";
5c42fc5b 990
576f627c
SR
991 my $i = $iteration;
992
993 if ($reboot_on_error && !do_not_reboot) {
994
75c3fda7 995 doprint "REBOOTING\n";
bc7c5803 996 reboot_to_good;
75c3fda7 997
a75fecec 998 } elsif ($poweroff_on_error && defined($power_off)) {
5c42fc5b 999 doprint "POWERING OFF\n";
a75fecec 1000 `$power_off`;
5c42fc5b 1001 }
75c3fda7 1002
f80802cb
SR
1003 if (defined($opt{"LOG_FILE"})) {
1004 print " See $opt{LOG_FILE} for more info.\n";
1005 }
1006
576f627c 1007 die @_, "\n";
5c42fc5b
SR
1008}
1009
7faafbd6
SR
1010sub open_console {
1011 my ($fp) = @_;
1012
1013 my $flags;
1014
a75fecec
SR
1015 my $pid = open($fp, "$console|") or
1016 dodie "Can't open console $console";
7faafbd6
SR
1017
1018 $flags = fcntl($fp, F_GETFL, 0) or
576f627c 1019 dodie "Can't get flags for the socket: $!";
7faafbd6 1020 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
576f627c 1021 dodie "Can't set flags for the socket: $!";
7faafbd6
SR
1022
1023 return $pid;
1024}
1025
1026sub close_console {
1027 my ($fp, $pid) = @_;
1028
1029 doprint "kill child process $pid\n";
1030 kill 2, $pid;
1031
1032 print "closing!\n";
1033 close($fp);
1034}
1035
1036sub start_monitor {
1037 if ($monitor_cnt++) {
1038 return;
1039 }
1040 $monitor_fp = \*MONFD;
1041 $monitor_pid = open_console $monitor_fp;
a75fecec
SR
1042
1043 return;
1044
1045 open(MONFD, "Stop perl from warning about single use of MONFD");
7faafbd6
SR
1046}
1047
1048sub end_monitor {
1049 if (--$monitor_cnt) {
1050 return;
1051 }
1052 close_console($monitor_fp, $monitor_pid);
1053}
1054
1055sub wait_for_monitor {
2b803365
SR
1056 my ($time, $stop) = @_;
1057 my $full_line = "";
7faafbd6 1058 my $line;
2b803365 1059 my $booted = 0;
7faafbd6 1060
a75fecec 1061 doprint "** Wait for monitor to settle down **\n";
7faafbd6
SR
1062
1063 # read the monitor and wait for the system to calm down
2b803365 1064 while (!$booted) {
7faafbd6 1065 $line = wait_for_input($monitor_fp, $time);
2b803365
SR
1066 last if (!defined($line));
1067 print "$line";
1068 $full_line .= $line;
1069
1070 if (defined($stop) && $full_line =~ /$stop/) {
1071 doprint "wait for monitor detected $stop\n";
1072 $booted = 1;
1073 }
1074
1075 if ($line =~ /\n/) {
1076 $full_line = "";
1077 }
1078 }
a75fecec 1079 print "** Monitor flushed **\n";
7faafbd6
SR
1080}
1081
de5b6e3b
RV
1082sub save_logs {
1083 my ($result, $basedir) = @_;
1084 my @t = localtime;
1085 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1086 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1087
1088 my $type = $build_type;
1089 if ($type =~ /useconfig/) {
1090 $type = "useconfig";
1091 }
1092
1093 my $dir = "$machine-$test_type-$type-$result-$date";
1094
1095 $dir = "$basedir/$dir";
1096
1097 if (!-d $dir) {
1098 mkpath($dir) or
1099 die "can't create $dir";
1100 }
1101
1102 my %files = (
1103 "config" => $output_config,
1104 "buildlog" => $buildlog,
1105 "dmesg" => $dmesg,
1106 "testlog" => $testlog,
1107 );
1108
1109 while (my ($name, $source) = each(%files)) {
1110 if (-f "$source") {
1111 cp "$source", "$dir/$name" or
1112 die "failed to copy $source";
1113 }
1114 }
1115
1116 doprint "*** Saved info to $dir ***\n";
1117}
1118
2b7d9b21
SR
1119sub fail {
1120
a75fecec 1121 if ($die_on_failure) {
2b7d9b21
SR
1122 dodie @_;
1123 }
1124
a75fecec 1125 doprint "FAILED\n";
7faafbd6 1126
576f627c
SR
1127 my $i = $iteration;
1128
a75fecec 1129 # no need to reboot for just building.
576f627c 1130 if (!do_not_reboot) {
a75fecec 1131 doprint "REBOOTING\n";
bc7c5803 1132 reboot_to_good $sleep_time;
a75fecec 1133 }
7faafbd6 1134
9064af52
SR
1135 my $name = "";
1136
1137 if (defined($test_name)) {
1138 $name = " ($test_name)";
1139 }
1140
576f627c
SR
1141 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1142 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
9064af52 1143 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
576f627c
SR
1144 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1145 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
a75fecec 1146
de5b6e3b
RV
1147 if (defined($store_failures)) {
1148 save_logs "fail", $store_failures;
1149 }
7faafbd6 1150
2b7d9b21
SR
1151 return 1;
1152}
1153
2545eb61
SR
1154sub run_command {
1155 my ($command) = @_;
d6ce2a0b
SR
1156 my $dolog = 0;
1157 my $dord = 0;
1158 my $pid;
1159
e48c5293
SR
1160 $command =~ s/\$SSH_USER/$ssh_user/g;
1161 $command =~ s/\$MACHINE/$machine/g;
1162
d6ce2a0b
SR
1163 doprint("$command ... ");
1164
1165 $pid = open(CMD, "$command 2>&1 |") or
2b7d9b21 1166 (fail "unable to exec $command" and return 0);
2545eb61
SR
1167
1168 if (defined($opt{"LOG_FILE"})) {
d6ce2a0b
SR
1169 open(LOG, ">>$opt{LOG_FILE}") or
1170 dodie "failed to write to log";
1171 $dolog = 1;
6c5ee0be
SR
1172 }
1173
1174 if (defined($redirect)) {
d6ce2a0b
SR
1175 open (RD, ">$redirect") or
1176 dodie "failed to write to redirect $redirect";
1177 $dord = 1;
2545eb61
SR
1178 }
1179
d6ce2a0b
SR
1180 while (<CMD>) {
1181 print LOG if ($dolog);
1182 print RD if ($dord);
1183 }
2545eb61 1184
d6ce2a0b 1185 waitpid($pid, 0);
2545eb61
SR
1186 my $failed = $?;
1187
d6ce2a0b
SR
1188 close(CMD);
1189 close(LOG) if ($dolog);
1190 close(RD) if ($dord);
1191
2545eb61
SR
1192 if ($failed) {
1193 doprint "FAILED!\n";
1194 } else {
1195 doprint "SUCCESS\n";
1196 }
1197
5f9b6ced
SR
1198 return !$failed;
1199}
1200
e48c5293
SR
1201sub run_ssh {
1202 my ($cmd) = @_;
1203 my $cp_exec = $ssh_exec;
1204
1205 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1206 return run_command "$cp_exec";
1207}
1208
1209sub run_scp {
1210 my ($src, $dst) = @_;
1211 my $cp_scp = $scp_to_target;
1212
1213 $cp_scp =~ s/\$SRC_FILE/$src/g;
1214 $cp_scp =~ s/\$DST_FILE/$dst/g;
1215
1216 return run_command "$cp_scp";
1217}
1218
5f9b6ced
SR
1219sub get_grub_index {
1220
a75fecec
SR
1221 if ($reboot_type ne "grub") {
1222 return;
1223 }
5a391fbf 1224 return if (defined($grub_number));
5f9b6ced
SR
1225
1226 doprint "Find grub menu ... ";
1227 $grub_number = -1;
e48c5293
SR
1228
1229 my $ssh_grub = $ssh_exec;
1230 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1231
1232 open(IN, "$ssh_grub |")
5f9b6ced 1233 or die "unable to get menu.lst";
e48c5293 1234
eaa1fe25
SR
1235 my $found = 0;
1236
5f9b6ced 1237 while (<IN>) {
a75fecec 1238 if (/^\s*title\s+$grub_menu\s*$/) {
5f9b6ced 1239 $grub_number++;
eaa1fe25 1240 $found = 1;
5f9b6ced
SR
1241 last;
1242 } elsif (/^\s*title\s/) {
1243 $grub_number++;
1244 }
1245 }
1246 close(IN);
1247
a75fecec 1248 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
eaa1fe25 1249 if (!$found);
5f9b6ced 1250 doprint "$grub_number\n";
2545eb61
SR
1251}
1252
2545eb61
SR
1253sub wait_for_input
1254{
1255 my ($fp, $time) = @_;
1256 my $rin;
1257 my $ready;
1258 my $line;
1259 my $ch;
1260
1261 if (!defined($time)) {
1262 $time = $timeout;
1263 }
1264
1265 $rin = '';
1266 vec($rin, fileno($fp), 1) = 1;
1267 $ready = select($rin, undef, undef, $time);
1268
1269 $line = "";
1270
1271 # try to read one char at a time
1272 while (sysread $fp, $ch, 1) {
1273 $line .= $ch;
1274 last if ($ch eq "\n");
1275 }
1276
1277 if (!length($line)) {
1278 return undef;
1279 }
1280
1281 return $line;
1282}
1283
75c3fda7 1284sub reboot_to {
bc7c5803
SR
1285 if (defined($switch_to_test)) {
1286 run_command $switch_to_test;
1287 }
1288
a75fecec 1289 if ($reboot_type eq "grub") {
c54367f9
SR
1290 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1291 reboot;
a75fecec
SR
1292 return;
1293 }
1294
1295 run_command "$reboot_script";
2545eb61
SR
1296}
1297
a57419b3
SR
1298sub get_sha1 {
1299 my ($commit) = @_;
1300
1301 doprint "git rev-list --max-count=1 $commit ... ";
1302 my $sha1 = `git rev-list --max-count=1 $commit`;
1303 my $ret = $?;
1304
1305 logit $sha1;
1306
1307 if ($ret) {
1308 doprint "FAILED\n";
1309 dodie "Failed to get git $commit";
1310 }
1311
1312 print "SUCCESS\n";
1313
1314 chomp $sha1;
1315
1316 return $sha1;
1317}
1318
5a391fbf 1319sub monitor {
2545eb61
SR
1320 my $booted = 0;
1321 my $bug = 0;
5c42fc5b 1322 my $skip_call_trace = 0;
2b7d9b21 1323 my $loops;
2545eb61 1324
7faafbd6 1325 wait_for_monitor 5;
2545eb61
SR
1326
1327 my $line;
1328 my $full_line = "";
1329
7faafbd6
SR
1330 open(DMESG, "> $dmesg") or
1331 die "unable to write to $dmesg";
2545eb61 1332
75c3fda7 1333 reboot_to;
2545eb61 1334
1c8a617a
SR
1335 my $success_start;
1336 my $failure_start;
2d01b26a
SR
1337 my $monitor_start = time;
1338 my $done = 0;
f1a5b962 1339 my $version_found = 0;
1c8a617a 1340
2d01b26a 1341 while (!$done) {
2545eb61 1342
ecaf8e52
SR
1343 if ($bug && defined($stop_after_failure) &&
1344 $stop_after_failure >= 0) {
1345 my $time = $stop_after_failure - (time - $failure_start);
1346 $line = wait_for_input($monitor_fp, $time);
1347 if (!defined($line)) {
1348 doprint "bug timed out after $booted_timeout seconds\n";
1349 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1350 last;
1351 }
1352 } elsif ($booted) {
a75fecec 1353 $line = wait_for_input($monitor_fp, $booted_timeout);
cd4f1d53
SR
1354 if (!defined($line)) {
1355 my $s = $booted_timeout == 1 ? "" : "s";
1356 doprint "Successful boot found: break after $booted_timeout second$s\n";
1357 last;
1358 }
2b7d9b21 1359 } else {
7faafbd6 1360 $line = wait_for_input($monitor_fp);
cd4f1d53
SR
1361 if (!defined($line)) {
1362 my $s = $timeout == 1 ? "" : "s";
1363 doprint "Timed out after $timeout second$s\n";
1364 last;
1365 }
2b7d9b21 1366 }
2545eb61 1367
2545eb61 1368 doprint $line;
7faafbd6 1369 print DMESG $line;
2545eb61
SR
1370
1371 # we are not guaranteed to get a full line
1372 $full_line .= $line;
1373
a75fecec 1374 if ($full_line =~ /$success_line/) {
2545eb61 1375 $booted = 1;
1c8a617a
SR
1376 $success_start = time;
1377 }
1378
1379 if ($booted && defined($stop_after_success) &&
1380 $stop_after_success >= 0) {
1381 my $now = time;
1382 if ($now - $success_start >= $stop_after_success) {
1383 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1384 last;
1385 }
2545eb61
SR
1386 }
1387
5c42fc5b
SR
1388 if ($full_line =~ /\[ backtrace testing \]/) {
1389 $skip_call_trace = 1;
1390 }
1391
2545eb61 1392 if ($full_line =~ /call trace:/i) {
4651920e 1393 if (!$bug && !$skip_call_trace) {
1c8a617a
SR
1394 $bug = 1;
1395 $failure_start = time;
1396 }
1397 }
1398
1399 if ($bug && defined($stop_after_failure) &&
1400 $stop_after_failure >= 0) {
1401 my $now = time;
1402 if ($now - $failure_start >= $stop_after_failure) {
1403 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1404 last;
1405 }
5c42fc5b
SR
1406 }
1407
1408 if ($full_line =~ /\[ end of backtrace testing \]/) {
1409 $skip_call_trace = 0;
1410 }
1411
1412 if ($full_line =~ /Kernel panic -/) {
10abf118 1413 $failure_start = time;
2545eb61
SR
1414 $bug = 1;
1415 }
1416
f1a5b962
SR
1417 # Detect triple faults by testing the banner
1418 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1419 if ($1 eq $version) {
1420 $version_found = 1;
1421 } elsif ($version_found && $detect_triplefault) {
1422 # We already booted into the kernel we are testing,
1423 # but now we booted into another kernel?
1424 # Consider this a triple fault.
1425 doprint "Aleady booted in Linux kernel $version, but now\n";
1426 doprint "we booted into Linux kernel $1.\n";
1427 doprint "Assuming that this is a triple fault.\n";
1428 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1429 last;
1430 }
1431 }
1432
2545eb61
SR
1433 if ($line =~ /\n/) {
1434 $full_line = "";
1435 }
2d01b26a
SR
1436
1437 if ($stop_test_after > 0 && !$booted && !$bug) {
1438 if (time - $monitor_start > $stop_test_after) {
4d62bf51 1439 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
2d01b26a
SR
1440 $done = 1;
1441 }
1442 }
2545eb61
SR
1443 }
1444
7faafbd6 1445 close(DMESG);
2545eb61 1446
a75fecec 1447 if ($bug) {
2b7d9b21 1448 return 0 if ($in_bisect);
576f627c 1449 fail "failed - got a bug report" and return 0;
2545eb61
SR
1450 }
1451
a75fecec 1452 if (!$booted) {
2b7d9b21 1453 return 0 if ($in_bisect);
576f627c 1454 fail "failed - never got a boot prompt." and return 0;
2545eb61 1455 }
5f9b6ced 1456
2b7d9b21 1457 return 1;
2545eb61
SR
1458}
1459
2b29b2f8
SR
1460sub eval_kernel_version {
1461 my ($option) = @_;
1462
1463 $option =~ s/\$KERNEL_VERSION/$version/g;
1464
1465 return $option;
1466}
1467
db05cfef
SR
1468sub do_post_install {
1469
1470 return if (!defined($post_install));
1471
2b29b2f8 1472 my $cp_post_install = eval_kernel_version $post_install;
db05cfef
SR
1473 run_command "$cp_post_install" or
1474 dodie "Failed to run post install";
1475}
1476
2545eb61
SR
1477sub install {
1478
e0a8742e
SR
1479 return if ($no_install);
1480
2b29b2f8
SR
1481 my $cp_target = eval_kernel_version $target_image;
1482
1483 run_scp "$outputdir/$build_target", "$cp_target" or
5c42fc5b 1484 dodie "failed to copy image";
2545eb61 1485
5f9b6ced 1486 my $install_mods = 0;
2545eb61 1487
5f9b6ced
SR
1488 # should we process modules?
1489 $install_mods = 0;
51ad1dd1 1490 open(IN, "$output_config") or dodie("Can't read config file");
5f9b6ced
SR
1491 while (<IN>) {
1492 if (/CONFIG_MODULES(=y)?/) {
1493 $install_mods = 1 if (defined($1));
1494 last;
5c42fc5b 1495 }
5f9b6ced
SR
1496 }
1497 close(IN);
5c42fc5b 1498
5f9b6ced 1499 if (!$install_mods) {
db05cfef 1500 do_post_install;
5f9b6ced
SR
1501 doprint "No modules needed\n";
1502 return;
1503 }
2545eb61 1504
a75fecec 1505 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
5f9b6ced 1506 dodie "Failed to install modules";
5c42fc5b 1507
5f9b6ced 1508 my $modlib = "/lib/modules/$version";
a57419b3 1509 my $modtar = "ktest-mods.tar.bz2";
5c42fc5b 1510
e48c5293 1511 run_ssh "rm -rf $modlib" or
5f9b6ced 1512 dodie "failed to remove old mods: $modlib";
5c42fc5b 1513
5f9b6ced 1514 # would be nice if scp -r did not follow symbolic links
a75fecec 1515 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
5f9b6ced
SR
1516 dodie "making tarball";
1517
e48c5293 1518 run_scp "$tmpdir/$modtar", "/tmp" or
5f9b6ced
SR
1519 dodie "failed to copy modules";
1520
a75fecec 1521 unlink "$tmpdir/$modtar";
5f9b6ced 1522
e7b13441 1523 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
5f9b6ced 1524 dodie "failed to tar modules";
2545eb61 1525
e48c5293 1526 run_ssh "rm -f /tmp/$modtar";
8b37ca8c 1527
db05cfef 1528 do_post_install;
2545eb61
SR
1529}
1530
ddf607e5
SR
1531sub get_version {
1532 # get the release name
1533 doprint "$make kernelrelease ... ";
1534 $version = `$make kernelrelease | tail -1`;
1535 chomp($version);
1536 doprint "$version\n";
1537}
1538
1539sub start_monitor_and_boot {
9f7424cc
SR
1540 # Make sure the stable kernel has finished booting
1541 start_monitor;
1542 wait_for_monitor 5;
1543 end_monitor;
1544
ddf607e5
SR
1545 get_grub_index;
1546 get_version;
1547 install;
1548
1549 start_monitor;
1550 return monitor;
1551}
1552
6c5ee0be
SR
1553sub check_buildlog {
1554 my ($patch) = @_;
1555
6c5ee0be
SR
1556 my @files = `git show $patch | diffstat -l`;
1557
1558 open(IN, "git show $patch |") or
1559 dodie "failed to show $patch";
1560 while (<IN>) {
1561 if (m,^--- a/(.*),) {
1562 chomp $1;
1563 $files[$#files] = $1;
1564 }
1565 }
1566 close(IN);
1567
1568 open(IN, $buildlog) or dodie "Can't open $buildlog";
1569 while (<IN>) {
1570 if (/^\s*(.*?):.*(warning|error)/) {
1571 my $err = $1;
1572 foreach my $file (@files) {
a75fecec 1573 my $fullpath = "$builddir/$file";
6c5ee0be 1574 if ($file eq $err || $fullpath eq $err) {
2b7d9b21 1575 fail "$file built with warnings" and return 0;
6c5ee0be
SR
1576 }
1577 }
1578 }
1579 }
1580 close(IN);
2b7d9b21
SR
1581
1582 return 1;
6c5ee0be
SR
1583}
1584
fcb3f16a
SR
1585sub apply_min_config {
1586 my $outconfig = "$output_config.new";
1587
1588 # Read the config file and remove anything that
1589 # is in the force_config hash (from minconfig and others)
1590 # then add the force config back.
1591
1592 doprint "Applying minimum configurations into $output_config.new\n";
1593
1594 open (OUT, ">$outconfig") or
1595 dodie "Can't create $outconfig";
1596
1597 if (-f $output_config) {
1598 open (IN, $output_config) or
1599 dodie "Failed to open $output_config";
1600 while (<IN>) {
1601 if (/^(# )?(CONFIG_[^\s=]*)/) {
1602 next if (defined($force_config{$2}));
1603 }
1604 print OUT;
1605 }
1606 close IN;
1607 }
1608 foreach my $config (keys %force_config) {
1609 print OUT "$force_config{$config}\n";
1610 }
1611 close OUT;
1612
1613 run_command "mv $outconfig $output_config";
1614}
1615
612b9e9b 1616sub make_oldconfig {
612b9e9b 1617
4c4ab120
SR
1618 my @force_list = keys %force_config;
1619
1620 if ($#force_list >= 0) {
1621 apply_min_config;
1622 }
fcb3f16a
SR
1623
1624 if (!run_command "$make oldnoconfig") {
612b9e9b
SR
1625 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1626 # try a yes '' | oldconfig
1627 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
fcb3f16a 1628 run_command "yes '' | $make oldconfig" or
612b9e9b
SR
1629 dodie "failed make config oldconfig";
1630 }
1631}
1632
fcb3f16a
SR
1633# read a config file and use this to force new configs.
1634sub load_force_config {
1635 my ($config) = @_;
1636
1637 open(IN, $config) or
1638 dodie "failed to read $config";
1639 while (<IN>) {
1640 chomp;
1641 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1642 $force_config{$1} = $_;
1643 } elsif (/^# (CONFIG_\S*) is not set/) {
1644 $force_config{$1} = $_;
1645 }
1646 }
1647 close IN;
1648}
1649
2545eb61
SR
1650sub build {
1651 my ($type) = @_;
5c42fc5b 1652
7faafbd6
SR
1653 unlink $buildlog;
1654
4ab1cce5
SR
1655 # Failed builds should not reboot the target
1656 my $save_no_reboot = $no_reboot;
1657 $no_reboot = 1;
1658
0bd6c1a3
SR
1659 if (defined($pre_build)) {
1660 my $ret = run_command $pre_build;
1661 if (!$ret && defined($pre_build_die) &&
1662 $pre_build_die) {
1663 dodie "failed to pre_build\n";
1664 }
1665 }
1666
75c3fda7 1667 if ($type =~ /^useconfig:(.*)/) {
51ad1dd1 1668 run_command "cp $1 $output_config" or
75c3fda7 1669 dodie "could not copy $1 to .config";
5f9b6ced 1670
75c3fda7
SR
1671 $type = "oldconfig";
1672 }
1673
5c42fc5b
SR
1674 # old config can ask questions
1675 if ($type eq "oldconfig") {
9386c6ab 1676 $type = "oldnoconfig";
75c3fda7
SR
1677
1678 # allow for empty configs
51ad1dd1 1679 run_command "touch $output_config";
75c3fda7 1680
13488231
AJ
1681 if (!$noclean) {
1682 run_command "mv $output_config $outputdir/config_temp" or
1683 dodie "moving .config";
2545eb61 1684
13488231 1685 run_command "$make mrproper" or dodie "make mrproper";
2545eb61 1686
13488231
AJ
1687 run_command "mv $outputdir/config_temp $output_config" or
1688 dodie "moving config_temp";
1689 }
5c42fc5b
SR
1690
1691 } elsif (!$noclean) {
51ad1dd1 1692 unlink "$output_config";
5f9b6ced 1693 run_command "$make mrproper" or
5c42fc5b 1694 dodie "make mrproper";
5c42fc5b 1695 }
2545eb61
SR
1696
1697 # add something to distinguish this build
a75fecec
SR
1698 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1699 print OUT "$localversion\n";
2545eb61
SR
1700 close(OUT);
1701
5f9b6ced 1702 if (defined($minconfig)) {
fcb3f16a 1703 load_force_config($minconfig);
2545eb61
SR
1704 }
1705
fcb3f16a
SR
1706 if ($type ne "oldnoconfig") {
1707 run_command "$make $type" or
612b9e9b
SR
1708 dodie "failed make config";
1709 }
fcb3f16a
SR
1710 # Run old config regardless, to enforce min configurations
1711 make_oldconfig;
2545eb61 1712
a75fecec 1713 $redirect = "$buildlog";
0bd6c1a3
SR
1714 my $build_ret = run_command "$make $build_options";
1715 undef $redirect;
1716
1717 if (defined($post_build)) {
1718 my $ret = run_command $post_build;
1719 if (!$ret && defined($post_build_die) &&
1720 $post_build_die) {
1721 dodie "failed to post_build\n";
1722 }
1723 }
1724
1725 if (!$build_ret) {
5f9b6ced 1726 # bisect may need this to pass
4ab1cce5
SR
1727 if ($in_bisect) {
1728 $no_reboot = $save_no_reboot;
1729 return 0;
1730 }
2b7d9b21 1731 fail "failed build" and return 0;
2545eb61 1732 }
5f9b6ced 1733
4ab1cce5
SR
1734 $no_reboot = $save_no_reboot;
1735
2b7d9b21 1736 return 1;
2545eb61
SR
1737}
1738
75c3fda7 1739sub halt {
e48c5293 1740 if (!run_ssh "halt" or defined($power_off)) {
576f627c
SR
1741 if (defined($poweroff_after_halt)) {
1742 sleep $poweroff_after_halt;
1743 run_command "$power_off";
1744 }
1745 } else {
75c3fda7 1746 # nope? the zap it!
a75fecec 1747 run_command "$power_off";
75c3fda7
SR
1748 }
1749}
1750
5f9b6ced
SR
1751sub success {
1752 my ($i) = @_;
1753
e48c5293
SR
1754 $successes++;
1755
9064af52
SR
1756 my $name = "";
1757
1758 if (defined($test_name)) {
1759 $name = " ($test_name)";
1760 }
1761
5f9b6ced
SR
1762 doprint "\n\n*******************************************\n";
1763 doprint "*******************************************\n";
9064af52 1764 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
5f9b6ced
SR
1765 doprint "*******************************************\n";
1766 doprint "*******************************************\n";
1767
de5b6e3b
RV
1768 if (defined($store_successes)) {
1769 save_logs "success", $store_successes;
1770 }
1771
576f627c 1772 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
a75fecec 1773 doprint "Reboot and wait $sleep_time seconds\n";
bc7c5803 1774 reboot_to_good $sleep_time;
5f9b6ced
SR
1775 }
1776}
1777
c960bb9f
SR
1778sub answer_bisect {
1779 for (;;) {
1780 doprint "Pass or fail? [p/f]";
1781 my $ans = <STDIN>;
1782 chomp $ans;
1783 if ($ans eq "p" || $ans eq "P") {
1784 return 1;
1785 } elsif ($ans eq "f" || $ans eq "F") {
1786 return 0;
1787 } else {
1788 print "Please answer 'P' or 'F'\n";
1789 }
1790 }
1791}
1792
5a391fbf 1793sub child_run_test {
7faafbd6 1794 my $failed = 0;
5a391fbf 1795
7faafbd6 1796 # child should have no power
a75fecec
SR
1797 $reboot_on_error = 0;
1798 $poweroff_on_error = 0;
1799 $die_on_failure = 1;
7faafbd6 1800
a9dd5d63 1801 $redirect = "$testlog";
7faafbd6 1802 run_command $run_test or $failed = 1;
a9dd5d63
RV
1803 undef $redirect;
1804
5a391fbf
SR
1805 exit $failed;
1806}
1807
1808my $child_done;
1809
1810sub child_finished {
1811 $child_done = 1;
1812}
1813
1814sub do_run_test {
1815 my $child_pid;
1816 my $child_exit;
5a391fbf
SR
1817 my $line;
1818 my $full_line;
1819 my $bug = 0;
5a391fbf 1820
7faafbd6 1821 wait_for_monitor 1;
5a391fbf 1822
7faafbd6 1823 doprint "run test $run_test\n";
5a391fbf
SR
1824
1825 $child_done = 0;
1826
1827 $SIG{CHLD} = qw(child_finished);
1828
1829 $child_pid = fork;
1830
1831 child_run_test if (!$child_pid);
1832
1833 $full_line = "";
1834
1835 do {
7faafbd6 1836 $line = wait_for_input($monitor_fp, 1);
5a391fbf
SR
1837 if (defined($line)) {
1838
1839 # we are not guaranteed to get a full line
1840 $full_line .= $line;
8ea0e063 1841 doprint $line;
5a391fbf
SR
1842
1843 if ($full_line =~ /call trace:/i) {
1844 $bug = 1;
1845 }
1846
1847 if ($full_line =~ /Kernel panic -/) {
1848 $bug = 1;
1849 }
1850
1851 if ($line =~ /\n/) {
1852 $full_line = "";
1853 }
1854 }
1855 } while (!$child_done && !$bug);
1856
1857 if ($bug) {
8ea0e063
SR
1858 my $failure_start = time;
1859 my $now;
1860 do {
1861 $line = wait_for_input($monitor_fp, 1);
1862 if (defined($line)) {
1863 doprint $line;
1864 }
1865 $now = time;
1866 if ($now - $failure_start >= $stop_after_failure) {
1867 last;
1868 }
1869 } while (defined($line));
1870
5a391fbf
SR
1871 doprint "Detected kernel crash!\n";
1872 # kill the child with extreme prejudice
1873 kill 9, $child_pid;
1874 }
1875
1876 waitpid $child_pid, 0;
1877 $child_exit = $?;
1878
c5dacb88
SR
1879 if (!$bug && $in_bisect) {
1880 if (defined($bisect_ret_good)) {
1881 if ($child_exit == $bisect_ret_good) {
1882 return 1;
1883 }
1884 }
1885 if (defined($bisect_ret_skip)) {
1886 if ($child_exit == $bisect_ret_skip) {
1887 return -1;
1888 }
1889 }
1890 if (defined($bisect_ret_abort)) {
1891 if ($child_exit == $bisect_ret_abort) {
1892 fail "test abort" and return -2;
1893 }
1894 }
1895 if (defined($bisect_ret_bad)) {
1896 if ($child_exit == $bisect_ret_skip) {
1897 return 0;
1898 }
1899 }
1900 if (defined($bisect_ret_default)) {
1901 if ($bisect_ret_default eq "good") {
1902 return 1;
1903 } elsif ($bisect_ret_default eq "bad") {
1904 return 0;
1905 } elsif ($bisect_ret_default eq "skip") {
1906 return -1;
1907 } elsif ($bisect_ret_default eq "abort") {
1908 return -2;
1909 } else {
1910 fail "unknown default action: $bisect_ret_default"
1911 and return -2;
1912 }
1913 }
1914 }
1915
5a391fbf 1916 if ($bug || $child_exit) {
2b7d9b21
SR
1917 return 0 if $in_bisect;
1918 fail "test failed" and return 0;
5a391fbf 1919 }
2b7d9b21 1920 return 1;
5a391fbf
SR
1921}
1922
a75fecec
SR
1923sub run_git_bisect {
1924 my ($command) = @_;
1925
1926 doprint "$command ... ";
1927
1928 my $output = `$command 2>&1`;
1929 my $ret = $?;
1930
1931 logit $output;
1932
1933 if ($ret) {
1934 doprint "FAILED\n";
1935 dodie "Failed to git bisect";
1936 }
1937
1938 doprint "SUCCESS\n";
1939 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1940 doprint "$1 [$2]\n";
1941 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
1942 $bisect_bad = $1;
1943 doprint "Found bad commit... $1\n";
1944 return 0;
1945 } else {
1946 # we already logged it, just print it now.
1947 print $output;
1948 }
1949
1950 return 1;
1951}
1952
c23dca7c
SR
1953sub bisect_reboot {
1954 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
bc7c5803 1955 reboot_to_good $bisect_sleep_time;
c23dca7c
SR
1956}
1957
1958# returns 1 on success, 0 on failure, -1 on skip
0a05c769
SR
1959sub run_bisect_test {
1960 my ($type, $buildtype) = @_;
5f9b6ced 1961
2b7d9b21 1962 my $failed = 0;
5f9b6ced
SR
1963 my $result;
1964 my $output;
1965 my $ret;
1966
0a05c769
SR
1967 $in_bisect = 1;
1968
1969 build $buildtype or $failed = 1;
5f9b6ced
SR
1970
1971 if ($type ne "build") {
c23dca7c
SR
1972 if ($failed && $bisect_skip) {
1973 $in_bisect = 0;
1974 return -1;
1975 }
7faafbd6 1976 dodie "Failed on build" if $failed;
5f9b6ced
SR
1977
1978 # Now boot the box
ddf607e5 1979 start_monitor_and_boot or $failed = 1;
5f9b6ced
SR
1980
1981 if ($type ne "boot") {
c23dca7c
SR
1982 if ($failed && $bisect_skip) {
1983 end_monitor;
1984 bisect_reboot;
1985 $in_bisect = 0;
1986 return -1;
1987 }
7faafbd6 1988 dodie "Failed on boot" if $failed;
5a391fbf 1989
2b7d9b21 1990 do_run_test or $failed = 1;
5f9b6ced 1991 }
7faafbd6 1992 end_monitor;
5f9b6ced
SR
1993 }
1994
1995 if ($failed) {
0a05c769 1996 $result = 0;
5f9b6ced 1997 } else {
0a05c769
SR
1998 $result = 1;
1999 }
4025bc62
SR
2000
2001 # reboot the box to a kernel we can ssh to
2002 if ($type ne "build") {
2003 bisect_reboot;
2004 }
0a05c769
SR
2005 $in_bisect = 0;
2006
2007 return $result;
2008}
2009
2010sub run_bisect {
2011 my ($type) = @_;
2012 my $buildtype = "oldconfig";
2013
2014 # We should have a minconfig to use?
2015 if (defined($minconfig)) {
2016 $buildtype = "useconfig:$minconfig";
5f9b6ced
SR
2017 }
2018
0a05c769
SR
2019 my $ret = run_bisect_test $type, $buildtype;
2020
c960bb9f
SR
2021 if ($bisect_manual) {
2022 $ret = answer_bisect;
2023 }
0a05c769 2024
d6ce2a0b
SR
2025 # Are we looking for where it worked, not failed?
2026 if ($reverse_bisect) {
0a05c769 2027 $ret = !$ret;
d6ce2a0b
SR
2028 }
2029
c23dca7c 2030 if ($ret > 0) {
0a05c769 2031 return "good";
c23dca7c 2032 } elsif ($ret == 0) {
0a05c769 2033 return "bad";
c23dca7c
SR
2034 } elsif ($bisect_skip) {
2035 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2036 return "skip";
0a05c769 2037 }
5f9b6ced
SR
2038}
2039
dad98754
SR
2040sub update_bisect_replay {
2041 my $tmp_log = "$tmpdir/ktest_bisect_log";
2042 run_command "git bisect log > $tmp_log" or
2043 die "can't create bisect log";
2044 return $tmp_log;
2045}
2046
5f9b6ced
SR
2047sub bisect {
2048 my ($i) = @_;
2049
2050 my $result;
2051
2052 die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
2053 die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
2054 die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
2055
2056 my $good = $opt{"BISECT_GOOD[$i]"};
2057 my $bad = $opt{"BISECT_BAD[$i]"};
2058 my $type = $opt{"BISECT_TYPE[$i]"};
a75fecec
SR
2059 my $start = $opt{"BISECT_START[$i]"};
2060 my $replay = $opt{"BISECT_REPLAY[$i]"};
3410f6fd
SR
2061 my $start_files = $opt{"BISECT_FILES[$i]"};
2062
2063 if (defined($start_files)) {
2064 $start_files = " -- " . $start_files;
2065 } else {
2066 $start_files = "";
2067 }
5f9b6ced 2068
a57419b3
SR
2069 # convert to true sha1's
2070 $good = get_sha1($good);
2071 $bad = get_sha1($bad);
2072
d6ce2a0b
SR
2073 if (defined($opt{"BISECT_REVERSE[$i]"}) &&
2074 $opt{"BISECT_REVERSE[$i]"} == 1) {
2075 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2076 $reverse_bisect = 1;
2077 } else {
2078 $reverse_bisect = 0;
2079 }
2080
a75fecec
SR
2081 # Can't have a test without having a test to run
2082 if ($type eq "test" && !defined($run_test)) {
2083 $type = "boot";
2084 }
2085
dad98754
SR
2086 # Check if a bisect was running
2087 my $bisect_start_file = "$builddir/.git/BISECT_START";
2088
a75fecec 2089 my $check = $opt{"BISECT_CHECK[$i]"};
dad98754
SR
2090 my $do_check = defined($check) && $check ne "0";
2091
2092 if ( -f $bisect_start_file ) {
2093 print "Bisect in progress found\n";
2094 if ($do_check) {
2095 print " If you say yes, then no checks of good or bad will be done\n";
2096 }
2097 if (defined($replay)) {
2098 print "** BISECT_REPLAY is defined in config file **";
2099 print " Ignore config option and perform new git bisect log?\n";
2100 if (read_ync " (yes, no, or cancel) ") {
2101 $replay = update_bisect_replay;
2102 $do_check = 0;
2103 }
2104 } elsif (read_yn "read git log and continue?") {
2105 $replay = update_bisect_replay;
2106 $do_check = 0;
2107 }
2108 }
2109
2110 if ($do_check) {
a75fecec
SR
2111
2112 # get current HEAD
a57419b3 2113 my $head = get_sha1("HEAD");
a75fecec
SR
2114
2115 if ($check ne "good") {
2116 doprint "TESTING BISECT BAD [$bad]\n";
2117 run_command "git checkout $bad" or
2118 die "Failed to checkout $bad";
2119
2120 $result = run_bisect $type;
2121
2122 if ($result ne "bad") {
2123 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2124 }
2125 }
2126
2127 if ($check ne "bad") {
2128 doprint "TESTING BISECT GOOD [$good]\n";
2129 run_command "git checkout $good" or
2130 die "Failed to checkout $good";
2131
2132 $result = run_bisect $type;
2133
2134 if ($result ne "good") {
2135 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2136 }
2137 }
2138
2139 # checkout where we started
2140 run_command "git checkout $head" or
2141 die "Failed to checkout $head";
2142 }
2143
3410f6fd 2144 run_command "git bisect start$start_files" or
a75fecec 2145 dodie "could not start bisect";
5f9b6ced
SR
2146
2147 run_command "git bisect good $good" or
a75fecec 2148 dodie "could not set bisect good to $good";
5f9b6ced 2149
a75fecec
SR
2150 run_git_bisect "git bisect bad $bad" or
2151 dodie "could not set bisect bad to $bad";
5f9b6ced 2152
a75fecec
SR
2153 if (defined($replay)) {
2154 run_command "git bisect replay $replay" or
2155 dodie "failed to run replay";
5a391fbf
SR
2156 }
2157
a75fecec
SR
2158 if (defined($start)) {
2159 run_command "git checkout $start" or
2160 dodie "failed to checkout $start";
2161 }
2162
2163 my $test;
5f9b6ced
SR
2164 do {
2165 $result = run_bisect $type;
a75fecec
SR
2166 $test = run_git_bisect "git bisect $result";
2167 } while ($test);
5f9b6ced
SR
2168
2169 run_command "git bisect log" or
2170 dodie "could not capture git bisect log";
2171
2172 run_command "git bisect reset" or
2173 dodie "could not reset git bisect";
2174
2175 doprint "Bad commit was [$bisect_bad]\n";
2176
0a05c769
SR
2177 success $i;
2178}
2179
2180my %config_ignore;
2181my %config_set;
2182
2183my %config_list;
2184my %null_config;
2185
2186my %dependency;
2187
4c4ab120
SR
2188sub assign_configs {
2189 my ($hash, $config) = @_;
0a05c769
SR
2190
2191 open (IN, $config)
2192 or dodie "Failed to read $config";
2193
2194 while (<IN>) {
9bf71749 2195 if (/^((CONFIG\S*)=.*)/) {
4c4ab120 2196 ${$hash}{$2} = $1;
0a05c769
SR
2197 }
2198 }
2199
2200 close(IN);
2201}
2202
4c4ab120
SR
2203sub process_config_ignore {
2204 my ($config) = @_;
2205
2206 assign_configs \%config_ignore, $config;
2207}
2208
0a05c769
SR
2209sub read_current_config {
2210 my ($config_ref) = @_;
2211
2212 %{$config_ref} = ();
2213 undef %{$config_ref};
2214
2215 my @key = keys %{$config_ref};
2216 if ($#key >= 0) {
2217 print "did not delete!\n";
2218 exit;
2219 }
2220 open (IN, "$output_config");
2221
2222 while (<IN>) {
2223 if (/^(CONFIG\S+)=(.*)/) {
2224 ${$config_ref}{$1} = $2;
2225 }
2226 }
2227 close(IN);
2228}
2229
2230sub get_dependencies {
2231 my ($config) = @_;
2232
2233 my $arr = $dependency{$config};
2234 if (!defined($arr)) {
2235 return ();
2236 }
2237
2238 my @deps = @{$arr};
2239
2240 foreach my $dep (@{$arr}) {
2241 print "ADD DEP $dep\n";
2242 @deps = (@deps, get_dependencies $dep);
2243 }
2244
2245 return @deps;
2246}
2247
2248sub create_config {
2249 my @configs = @_;
2250
2251 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2252
2253 foreach my $config (@configs) {
2254 print OUT "$config_set{$config}\n";
2255 my @deps = get_dependencies $config;
2256 foreach my $dep (@deps) {
2257 print OUT "$config_set{$dep}\n";
2258 }
2259 }
2260
2261 foreach my $config (keys %config_ignore) {
2262 print OUT "$config_ignore{$config}\n";
2263 }
2264 close(OUT);
2265
2266# exit;
fcb3f16a 2267 make_oldconfig;
0a05c769
SR
2268}
2269
2270sub compare_configs {
2271 my (%a, %b) = @_;
2272
2273 foreach my $item (keys %a) {
2274 if (!defined($b{$item})) {
2275 print "diff $item\n";
2276 return 1;
2277 }
2278 delete $b{$item};
2279 }
2280
2281 my @keys = keys %b;
2282 if ($#keys) {
2283 print "diff2 $keys[0]\n";
2284 }
2285 return -1 if ($#keys >= 0);
2286
2287 return 0;
2288}
2289
2290sub run_config_bisect_test {
2291 my ($type) = @_;
2292
2293 return run_bisect_test $type, "oldconfig";
2294}
2295
2296sub process_passed {
2297 my (%configs) = @_;
2298
2299 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2300 # Passed! All these configs are part of a good compile.
2301 # Add them to the min options.
2302 foreach my $config (keys %configs) {
2303 if (defined($config_list{$config})) {
2304 doprint " removing $config\n";
2305 $config_ignore{$config} = $config_list{$config};
2306 delete $config_list{$config};
2307 }
2308 }
f1a27850
SR
2309 doprint "config copied to $outputdir/config_good\n";
2310 run_command "cp -f $output_config $outputdir/config_good";
0a05c769
SR
2311}
2312
2313sub process_failed {
2314 my ($config) = @_;
2315
2316 doprint "\n\n***************************************\n";
2317 doprint "Found bad config: $config\n";
2318 doprint "***************************************\n\n";
2319}
2320
2321sub run_config_bisect {
2322
2323 my @start_list = keys %config_list;
2324
2325 if ($#start_list < 0) {
2326 doprint "No more configs to test!!!\n";
2327 return -1;
2328 }
2329
2330 doprint "***** RUN TEST ***\n";
2331 my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
2332 my $ret;
2333 my %current_config;
2334
2335 my $count = $#start_list + 1;
2336 doprint " $count configs to test\n";
2337
2338 my $half = int($#start_list / 2);
2339
2340 do {
2341 my @tophalf = @start_list[0 .. $half];
2342
2343 create_config @tophalf;
2344 read_current_config \%current_config;
2345
2346 $count = $#tophalf + 1;
2347 doprint "Testing $count configs\n";
2348 my $found = 0;
2349 # make sure we test something
2350 foreach my $config (@tophalf) {
2351 if (defined($current_config{$config})) {
2352 logit " $config\n";
2353 $found = 1;
2354 }
2355 }
2356 if (!$found) {
2357 # try the other half
2358 doprint "Top half produced no set configs, trying bottom half\n";
4c8cc55b 2359 @tophalf = @start_list[$half + 1 .. $#start_list];
0a05c769
SR
2360 create_config @tophalf;
2361 read_current_config \%current_config;
2362 foreach my $config (@tophalf) {
2363 if (defined($current_config{$config})) {
2364 logit " $config\n";
2365 $found = 1;
2366 }
2367 }
2368 if (!$found) {
2369 doprint "Failed: Can't make new config with current configs\n";
2370 foreach my $config (@start_list) {
2371 doprint " CONFIG: $config\n";
2372 }
2373 return -1;
2374 }
2375 $count = $#tophalf + 1;
2376 doprint "Testing $count configs\n";
2377 }
2378
2379 $ret = run_config_bisect_test $type;
c960bb9f
SR
2380 if ($bisect_manual) {
2381 $ret = answer_bisect;
2382 }
0a05c769
SR
2383 if ($ret) {
2384 process_passed %current_config;
2385 return 0;
2386 }
2387
2388 doprint "This config had a failure.\n";
2389 doprint "Removing these configs that were not set in this config:\n";
f1a27850
SR
2390 doprint "config copied to $outputdir/config_bad\n";
2391 run_command "cp -f $output_config $outputdir/config_bad";
0a05c769
SR
2392
2393 # A config exists in this group that was bad.
2394 foreach my $config (keys %config_list) {
2395 if (!defined($current_config{$config})) {
2396 doprint " removing $config\n";
2397 delete $config_list{$config};
2398 }
2399 }
2400
2401 @start_list = @tophalf;
2402
2403 if ($#start_list == 0) {
2404 process_failed $start_list[0];
2405 return 1;
2406 }
2407
2408 # remove half the configs we are looking at and see if
2409 # they are good.
2410 $half = int($#start_list / 2);
4c8cc55b 2411 } while ($#start_list > 0);
0a05c769 2412
c960bb9f
SR
2413 # we found a single config, try it again unless we are running manually
2414
2415 if ($bisect_manual) {
2416 process_failed $start_list[0];
2417 return 1;
2418 }
2419
0a05c769
SR
2420 my @tophalf = @start_list[0 .. 0];
2421
2422 $ret = run_config_bisect_test $type;
2423 if ($ret) {
2424 process_passed %current_config;
2425 return 0;
2426 }
2427
2428 process_failed $start_list[0];
2429 return 1;
2430}
2431
2432sub config_bisect {
2433 my ($i) = @_;
2434
2435 my $start_config = $opt{"CONFIG_BISECT[$i]"};
2436
2437 my $tmpconfig = "$tmpdir/use_config";
2438
30f75da5
SR
2439 if (defined($config_bisect_good)) {
2440 process_config_ignore $config_bisect_good;
2441 }
2442
0a05c769
SR
2443 # Make the file with the bad config and the min config
2444 if (defined($minconfig)) {
2445 # read the min config for things to ignore
2446 run_command "cp $minconfig $tmpconfig" or
2447 dodie "failed to copy $minconfig to $tmpconfig";
2448 } else {
2449 unlink $tmpconfig;
2450 }
2451
0a05c769 2452 if (-f $tmpconfig) {
fcb3f16a 2453 load_force_config($tmpconfig);
0a05c769
SR
2454 process_config_ignore $tmpconfig;
2455 }
2456
2457 # now process the start config
2458 run_command "cp $start_config $output_config" or
2459 dodie "failed to copy $start_config to $output_config";
2460
2461 # read directly what we want to check
2462 my %config_check;
2463 open (IN, $output_config)
2464 or dodie "faied to open $output_config";
2465
2466 while (<IN>) {
2467 if (/^((CONFIG\S*)=.*)/) {
2468 $config_check{$2} = $1;
2469 }
2470 }
2471 close(IN);
2472
250bae8b 2473 # Now run oldconfig with the minconfig
fcb3f16a 2474 make_oldconfig;
0a05c769
SR
2475
2476 # check to see what we lost (or gained)
2477 open (IN, $output_config)
2478 or dodie "Failed to read $start_config";
2479
2480 my %removed_configs;
2481 my %added_configs;
2482
2483 while (<IN>) {
2484 if (/^((CONFIG\S*)=.*)/) {
2485 # save off all options
2486 $config_set{$2} = $1;
2487 if (defined($config_check{$2})) {
2488 if (defined($config_ignore{$2})) {
2489 $removed_configs{$2} = $1;
2490 } else {
2491 $config_list{$2} = $1;
2492 }
2493 } elsif (!defined($config_ignore{$2})) {
2494 $added_configs{$2} = $1;
2495 $config_list{$2} = $1;
2496 }
2497 }
2498 }
2499 close(IN);
2500
2501 my @confs = keys %removed_configs;
2502 if ($#confs >= 0) {
2503 doprint "Configs overridden by default configs and removed from check:\n";
2504 foreach my $config (@confs) {
2505 doprint " $config\n";
2506 }
2507 }
2508 @confs = keys %added_configs;
2509 if ($#confs >= 0) {
2510 doprint "Configs appearing in make oldconfig and added:\n";
2511 foreach my $config (@confs) {
2512 doprint " $config\n";
2513 }
2514 }
2515
2516 my %config_test;
2517 my $once = 0;
2518
2519 # Sometimes kconfig does weird things. We must make sure
2520 # that the config we autocreate has everything we need
2521 # to test, otherwise we may miss testing configs, or
2522 # may not be able to create a new config.
2523 # Here we create a config with everything set.
2524 create_config (keys %config_list);
2525 read_current_config \%config_test;
2526 foreach my $config (keys %config_list) {
2527 if (!defined($config_test{$config})) {
2528 if (!$once) {
2529 $once = 1;
2530 doprint "Configs not produced by kconfig (will not be checked):\n";
2531 }
2532 doprint " $config\n";
2533 delete $config_list{$config};
2534 }
2535 }
2536 my $ret;
2537 do {
2538 $ret = run_config_bisect;
2539 } while (!$ret);
2540
2541 return $ret if ($ret < 0);
5f9b6ced
SR
2542
2543 success $i;
2544}
2545
27d934b2
SR
2546sub patchcheck_reboot {
2547 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
bc7c5803 2548 reboot_to_good $patchcheck_sleep_time;
27d934b2
SR
2549}
2550
6c5ee0be
SR
2551sub patchcheck {
2552 my ($i) = @_;
2553
2554 die "PATCHCHECK_START[$i] not defined\n"
2555 if (!defined($opt{"PATCHCHECK_START[$i]"}));
2556 die "PATCHCHECK_TYPE[$i] not defined\n"
2557 if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
2558
2559 my $start = $opt{"PATCHCHECK_START[$i]"};
2560
2561 my $end = "HEAD";
2562 if (defined($opt{"PATCHCHECK_END[$i]"})) {
2563 $end = $opt{"PATCHCHECK_END[$i]"};
2564 }
2565
a57419b3
SR
2566 # Get the true sha1's since we can use things like HEAD~3
2567 $start = get_sha1($start);
2568 $end = get_sha1($end);
2569
6c5ee0be
SR
2570 my $type = $opt{"PATCHCHECK_TYPE[$i]"};
2571
2572 # Can't have a test without having a test to run
2573 if ($type eq "test" && !defined($run_test)) {
2574 $type = "boot";
2575 }
2576
2577 open (IN, "git log --pretty=oneline $end|") or
2578 dodie "could not get git list";
2579
2580 my @list;
2581
2582 while (<IN>) {
2583 chomp;
2584 $list[$#list+1] = $_;
2585 last if (/^$start/);
2586 }
2587 close(IN);
2588
2589 if ($list[$#list] !~ /^$start/) {
2b7d9b21 2590 fail "SHA1 $start not found";
6c5ee0be
SR
2591 }
2592
2593 # go backwards in the list
2594 @list = reverse @list;
2595
2596 my $save_clean = $noclean;
1990207d
SR
2597 my %ignored_warnings;
2598
2599 if (defined($ignore_warnings)) {
2600 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2601 $ignored_warnings{$sha1} = 1;
2602 }
2603 }
6c5ee0be
SR
2604
2605 $in_patchcheck = 1;
2606 foreach my $item (@list) {
2607 my $sha1 = $item;
2608 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2609
2610 doprint "\nProcessing commit $item\n\n";
2611
2612 run_command "git checkout $sha1" or
2613 die "Failed to checkout $sha1";
2614
2615 # only clean on the first and last patch
2616 if ($item eq $list[0] ||
2617 $item eq $list[$#list]) {
2618 $noclean = $save_clean;
2619 } else {
2620 $noclean = 1;
2621 }
2622
2623 if (defined($minconfig)) {
2b7d9b21 2624 build "useconfig:$minconfig" or return 0;
6c5ee0be
SR
2625 } else {
2626 # ?? no config to use?
2b7d9b21 2627 build "oldconfig" or return 0;
6c5ee0be
SR
2628 }
2629
1990207d
SR
2630
2631 if (!defined($ignored_warnings{$sha1})) {
2632 check_buildlog $sha1 or return 0;
2633 }
6c5ee0be
SR
2634
2635 next if ($type eq "build");
2636
7faafbd6
SR
2637 my $failed = 0;
2638
ddf607e5 2639 start_monitor_and_boot or $failed = 1;
7faafbd6
SR
2640
2641 if (!$failed && $type ne "boot"){
2642 do_run_test or $failed = 1;
2643 }
2644 end_monitor;
2645 return 0 if ($failed);
2646
27d934b2
SR
2647 patchcheck_reboot;
2648
6c5ee0be
SR
2649 }
2650 $in_patchcheck = 0;
2651 success $i;
2b7d9b21
SR
2652
2653 return 1;
6c5ee0be
SR
2654}
2655
b9066f6c 2656my %depends;
ac6974c7 2657my %depcount;
b9066f6c
SR
2658my $iflevel = 0;
2659my @ifdeps;
2660
2661# prevent recursion
2662my %read_kconfigs;
2663
ac6974c7
SR
2664sub add_dep {
2665 # $config depends on $dep
2666 my ($config, $dep) = @_;
2667
2668 if (defined($depends{$config})) {
2669 $depends{$config} .= " " . $dep;
2670 } else {
2671 $depends{$config} = $dep;
2672 }
2673
2674 # record the number of configs depending on $dep
2675 if (defined $depcount{$dep}) {
2676 $depcount{$dep}++;
2677 } else {
2678 $depcount{$dep} = 1;
2679 }
2680}
2681
b9066f6c
SR
2682# taken from streamline_config.pl
2683sub read_kconfig {
2684 my ($kconfig) = @_;
2685
2686 my $state = "NONE";
2687 my $config;
2688 my @kconfigs;
2689
2690 my $cont = 0;
2691 my $line;
2692
2693
2694 if (! -f $kconfig) {
2695 doprint "file $kconfig does not exist, skipping\n";
2696 return;
2697 }
2698
2699 open(KIN, "$kconfig")
2700 or die "Can't open $kconfig";
2701 while (<KIN>) {
2702 chomp;
2703
2704 # Make sure that lines ending with \ continue
2705 if ($cont) {
2706 $_ = $line . " " . $_;
2707 }
2708
2709 if (s/\\$//) {
2710 $cont = 1;
2711 $line = $_;
2712 next;
2713 }
2714
2715 $cont = 0;
2716
2717 # collect any Kconfig sources
2718 if (/^source\s*"(.*)"/) {
2719 $kconfigs[$#kconfigs+1] = $1;
2720 }
2721
2722 # configs found
2723 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2724 $state = "NEW";
2725 $config = $2;
2726
2727 for (my $i = 0; $i < $iflevel; $i++) {
ac6974c7 2728 add_dep $config, $ifdeps[$i];
b9066f6c
SR
2729 }
2730
2731 # collect the depends for the config
2732 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2733
ac6974c7 2734 add_dep $config, $1;
b9066f6c
SR
2735
2736 # Get the configs that select this config
ac6974c7
SR
2737 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2738
2739 # selected by depends on config
2740 add_dep $1, $config;
b9066f6c
SR
2741
2742 # Check for if statements
2743 } elsif (/^if\s+(.*\S)\s*$/) {
2744 my $deps = $1;
2745 # remove beginning and ending non text
2746 $deps =~ s/^[^a-zA-Z0-9_]*//;
2747 $deps =~ s/[^a-zA-Z0-9_]*$//;
2748
2749 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2750
2751 $ifdeps[$iflevel++] = join ':', @deps;
2752
2753 } elsif (/^endif/) {
2754
2755 $iflevel-- if ($iflevel);
2756
2757 # stop on "help"
2758 } elsif (/^\s*help\s*$/) {
2759 $state = "NONE";
2760 }
2761 }
2762 close(KIN);
2763
2764 # read in any configs that were found.
2765 foreach $kconfig (@kconfigs) {
2766 if (!defined($read_kconfigs{$kconfig})) {
2767 $read_kconfigs{$kconfig} = 1;
2768 read_kconfig("$builddir/$kconfig");
2769 }
2770 }
2771}
2772
2773sub read_depends {
2774 # find out which arch this is by the kconfig file
2775 open (IN, $output_config)
2776 or dodie "Failed to read $output_config";
2777 my $arch;
2778 while (<IN>) {
2779 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2780 $arch = $1;
2781 last;
2782 }
2783 }
2784 close IN;
2785
2786 if (!defined($arch)) {
2787 doprint "Could not find arch from config file\n";
2788 doprint "no dependencies used\n";
2789 return;
2790 }
2791
2792 # arch is really the subarch, we need to know
2793 # what directory to look at.
2794 if ($arch eq "i386" || $arch eq "x86_64") {
2795 $arch = "x86";
2796 } elsif ($arch =~ /^tile/) {
2797 $arch = "tile";
2798 }
2799
2800 my $kconfig = "$builddir/arch/$arch/Kconfig";
2801
2802 if (! -f $kconfig && $arch =~ /\d$/) {
2803 my $orig = $arch;
2804 # some subarchs have numbers, truncate them
2805 $arch =~ s/\d*$//;
2806 $kconfig = "$builddir/arch/$arch/Kconfig";
2807 if (! -f $kconfig) {
2808 doprint "No idea what arch dir $orig is for\n";
2809 doprint "no dependencies used\n";
2810 return;
2811 }
2812 }
2813
2814 read_kconfig($kconfig);
2815}
2816
4c4ab120
SR
2817sub read_config_list {
2818 my ($config) = @_;
2819
2820 open (IN, $config)
2821 or dodie "Failed to read $config";
2822
2823 while (<IN>) {
2824 if (/^((CONFIG\S*)=.*)/) {
2825 if (!defined($config_ignore{$2})) {
2826 $config_list{$2} = $1;
2827 }
2828 }
2829 }
2830
2831 close(IN);
2832}
2833
2834sub read_output_config {
2835 my ($config) = @_;
2836
2837 assign_configs \%config_ignore, $config;
2838}
2839
2840sub make_new_config {
2841 my @configs = @_;
2842
2843 open (OUT, ">$output_config")
2844 or dodie "Failed to write $output_config";
2845
2846 foreach my $config (@configs) {
2847 print OUT "$config\n";
2848 }
2849 close OUT;
2850}
2851
ac6974c7
SR
2852sub chomp_config {
2853 my ($config) = @_;
2854
2855 $config =~ s/CONFIG_//;
2856
2857 return $config;
2858}
2859
b9066f6c
SR
2860sub get_depends {
2861 my ($dep) = @_;
2862
ac6974c7 2863 my $kconfig = chomp_config $dep;
b9066f6c
SR
2864
2865 $dep = $depends{"$kconfig"};
2866
2867 # the dep string we have saves the dependencies as they
2868 # were found, including expressions like ! && ||. We
2869 # want to split this out into just an array of configs.
2870
2871 my $valid = "A-Za-z_0-9";
2872
2873 my @configs;
2874
2875 while ($dep =~ /[$valid]/) {
2876
2877 if ($dep =~ /^[^$valid]*([$valid]+)/) {
2878 my $conf = "CONFIG_" . $1;
2879
2880 $configs[$#configs + 1] = $conf;
2881
2882 $dep =~ s/^[^$valid]*[$valid]+//;
2883 } else {
2884 die "this should never happen";
2885 }
2886 }
2887
2888 return @configs;
2889}
2890
2891my %min_configs;
2892my %keep_configs;
43d1b651 2893my %save_configs;
b9066f6c
SR
2894my %processed_configs;
2895my %nochange_config;
2896
2897sub test_this_config {
2898 my ($config) = @_;
2899
2900 my $found;
2901
2902 # if we already processed this config, skip it
2903 if (defined($processed_configs{$config})) {
2904 return undef;
2905 }
2906 $processed_configs{$config} = 1;
2907
2908 # if this config failed during this round, skip it
2909 if (defined($nochange_config{$config})) {
2910 return undef;
2911 }
2912
ac6974c7 2913 my $kconfig = chomp_config $config;
b9066f6c
SR
2914
2915 # Test dependencies first
2916 if (defined($depends{"$kconfig"})) {
2917 my @parents = get_depends $config;
2918 foreach my $parent (@parents) {
2919 # if the parent is in the min config, check it first
2920 next if (!defined($min_configs{$parent}));
2921 $found = test_this_config($parent);
2922 if (defined($found)) {
2923 return $found;
2924 }
2925 }
2926 }
2927
2928 # Remove this config from the list of configs
2929 # do a make oldnoconfig and then read the resulting
2930 # .config to make sure it is missing the config that
2931 # we had before
2932 my %configs = %min_configs;
2933 delete $configs{$config};
2934 make_new_config ((values %configs), (values %keep_configs));
2935 make_oldconfig;
2936 undef %configs;
2937 assign_configs \%configs, $output_config;
2938
2939 return $config if (!defined($configs{$config}));
2940
2941 doprint "disabling config $config did not change .config\n";
2942
2943 $nochange_config{$config} = 1;
2944
2945 return undef;
2946}
2947
4c4ab120
SR
2948sub make_min_config {
2949 my ($i) = @_;
2950
2951 if (!defined($output_minconfig)) {
2952 fail "OUTPUT_MIN_CONFIG not defined" and return;
2953 }
35ce5952
SR
2954
2955 # If output_minconfig exists, and the start_minconfig
2956 # came from min_config, than ask if we should use
2957 # that instead.
2958 if (-f $output_minconfig && !$start_minconfig_defined) {
2959 print "$output_minconfig exists\n";
2960 if (read_yn " Use it as minconfig?") {
2961 $start_minconfig = $output_minconfig;
2962 }
2963 }
2964
4c4ab120
SR
2965 if (!defined($start_minconfig)) {
2966 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
2967 }
2968
35ce5952
SR
2969 my $temp_config = "$tmpdir/temp_config";
2970
4c4ab120
SR
2971 # First things first. We build an allnoconfig to find
2972 # out what the defaults are that we can't touch.
2973 # Some are selections, but we really can't handle selections.
2974
2975 my $save_minconfig = $minconfig;
2976 undef $minconfig;
2977
2978 run_command "$make allnoconfig" or return 0;
2979
b9066f6c
SR
2980 read_depends;
2981
4c4ab120 2982 process_config_ignore $output_config;
b9066f6c 2983
43d1b651 2984 undef %save_configs;
b9066f6c 2985 undef %min_configs;
4c4ab120
SR
2986
2987 if (defined($ignore_config)) {
2988 # make sure the file exists
2989 `touch $ignore_config`;
43d1b651 2990 assign_configs \%save_configs, $ignore_config;
4c4ab120
SR
2991 }
2992
43d1b651
SR
2993 %keep_configs = %save_configs;
2994
4c4ab120
SR
2995 doprint "Load initial configs from $start_minconfig\n";
2996
2997 # Look at the current min configs, and save off all the
2998 # ones that were set via the allnoconfig
4c4ab120
SR
2999 assign_configs \%min_configs, $start_minconfig;
3000
3001 my @config_keys = keys %min_configs;
3002
ac6974c7
SR
3003 # All configs need a depcount
3004 foreach my $config (@config_keys) {
3005 my $kconfig = chomp_config $config;
3006 if (!defined $depcount{$kconfig}) {
3007 $depcount{$kconfig} = 0;
3008 }
3009 }
3010
4c4ab120
SR
3011 # Remove anything that was set by the make allnoconfig
3012 # we shouldn't need them as they get set for us anyway.
3013 foreach my $config (@config_keys) {
3014 # Remove anything in the ignore_config
3015 if (defined($keep_configs{$config})) {
3016 my $file = $ignore_config;
3017 $file =~ s,.*/(.*?)$,$1,;
3018 doprint "$config set by $file ... ignored\n";
3019 delete $min_configs{$config};
3020 next;
3021 }
3022 # But make sure the settings are the same. If a min config
3023 # sets a selection, we do not want to get rid of it if
3024 # it is not the same as what we have. Just move it into
3025 # the keep configs.
3026 if (defined($config_ignore{$config})) {
3027 if ($config_ignore{$config} ne $min_configs{$config}) {
3028 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3029 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3030 $keep_configs{$config} = $min_configs{$config};
3031 } else {
3032 doprint "$config set by allnoconfig ... ignored\n";
3033 }
3034 delete $min_configs{$config};
3035 }
3036 }
3037
4c4ab120 3038 my $done = 0;
b9066f6c 3039 my $take_two = 0;
4c4ab120
SR
3040
3041 while (!$done) {
3042
3043 my $config;
3044 my $found;
3045
3046 # Now disable each config one by one and do a make oldconfig
3047 # till we find a config that changes our list.
3048
4c4ab120 3049 my @test_configs = keys %min_configs;
ac6974c7
SR
3050
3051 # Sort keys by who is most dependent on
3052 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3053 @test_configs ;
3054
3055 # Put configs that did not modify the config at the end.
4c4ab120
SR
3056 my $reset = 1;
3057 for (my $i = 0; $i < $#test_configs; $i++) {
3058 if (!defined($nochange_config{$test_configs[0]})) {
3059 $reset = 0;
3060 last;
3061 }
3062 # This config didn't change the .config last time.
3063 # Place it at the end
3064 my $config = shift @test_configs;
3065 push @test_configs, $config;
3066 }
3067
3068 # if every test config has failed to modify the .config file
3069 # in the past, then reset and start over.
3070 if ($reset) {
3071 undef %nochange_config;
3072 }
3073
b9066f6c
SR
3074 undef %processed_configs;
3075
4c4ab120
SR
3076 foreach my $config (@test_configs) {
3077
b9066f6c 3078 $found = test_this_config $config;
4c4ab120 3079
b9066f6c 3080 last if (defined($found));
4c4ab120
SR
3081
3082 # oh well, try another config
4c4ab120
SR
3083 }
3084
3085 if (!defined($found)) {
b9066f6c
SR
3086 # we could have failed due to the nochange_config hash
3087 # reset and try again
3088 if (!$take_two) {
3089 undef %nochange_config;
3090 $take_two = 1;
3091 next;
3092 }
4c4ab120
SR
3093 doprint "No more configs found that we can disable\n";
3094 $done = 1;
3095 last;
3096 }
b9066f6c 3097 $take_two = 0;
4c4ab120
SR
3098
3099 $config = $found;
3100
3101 doprint "Test with $config disabled\n";
3102
3103 # set in_bisect to keep build and monitor from dieing
3104 $in_bisect = 1;
3105
3106 my $failed = 0;
3107 build "oldconfig";
3108 start_monitor_and_boot or $failed = 1;
3109 end_monitor;
3110
3111 $in_bisect = 0;
3112
3113 if ($failed) {
b9066f6c 3114 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
4c4ab120
SR
3115 # this config is needed, add it to the ignore list.
3116 $keep_configs{$config} = $min_configs{$config};
43d1b651 3117 $save_configs{$config} = $min_configs{$config};
4c4ab120 3118 delete $min_configs{$config};
35ce5952
SR
3119
3120 # update new ignore configs
3121 if (defined($ignore_config)) {
3122 open (OUT, ">$temp_config")
3123 or die "Can't write to $temp_config";
43d1b651
SR
3124 foreach my $config (keys %save_configs) {
3125 print OUT "$save_configs{$config}\n";
35ce5952
SR
3126 }
3127 close OUT;
3128 run_command "mv $temp_config $ignore_config" or
3129 dodie "failed to copy update to $ignore_config";
3130 }
3131
4c4ab120
SR
3132 } else {
3133 # We booted without this config, remove it from the minconfigs.
3134 doprint "$config is not needed, disabling\n";
3135
3136 delete $min_configs{$config};
3137
3138 # Also disable anything that is not enabled in this config
3139 my %configs;
3140 assign_configs \%configs, $output_config;
3141 my @config_keys = keys %min_configs;
3142 foreach my $config (@config_keys) {
3143 if (!defined($configs{$config})) {
3144 doprint "$config is not set, disabling\n";
3145 delete $min_configs{$config};
3146 }
3147 }
3148
3149 # Save off all the current mandidory configs
35ce5952
SR
3150 open (OUT, ">$temp_config")
3151 or die "Can't write to $temp_config";
4c4ab120
SR
3152 foreach my $config (keys %keep_configs) {
3153 print OUT "$keep_configs{$config}\n";
3154 }
3155 foreach my $config (keys %min_configs) {
3156 print OUT "$min_configs{$config}\n";
3157 }
3158 close OUT;
35ce5952
SR
3159
3160 run_command "mv $temp_config $output_minconfig" or
3161 dodie "failed to copy update to $output_minconfig";
4c4ab120
SR
3162 }
3163
3164 doprint "Reboot and wait $sleep_time seconds\n";
bc7c5803 3165 reboot_to_good $sleep_time;
4c4ab120
SR
3166 }
3167
3168 success $i;
3169 return 1;
3170}
3171
8d1491ba
SR
3172$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
3173
3174if ($#ARGV == 0) {
3175 $ktest_config = $ARGV[0];
3176 if (! -f $ktest_config) {
3177 print "$ktest_config does not exist.\n";
35ce5952 3178 if (!read_yn "Create it?") {
8d1491ba
SR
3179 exit 0;
3180 }
3181 }
3182} else {
3183 $ktest_config = "ktest.conf";
3184}
3185
3186if (! -f $ktest_config) {
dbd3783b 3187 $newconfig = 1;
c4261d0f 3188 get_test_case;
8d1491ba
SR
3189 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3190 print OUT << "EOF"
3191# Generated by ktest.pl
3192#
0e7a22de
SR
3193
3194# PWD is a ktest.pl variable that will result in the process working
3195# directory that ktest.pl is executed in.
3196
3197# THIS_DIR is automatically assigned the PWD of the path that generated
3198# the config file. It is best to use this variable when assigning other
3199# directory paths within this directory. This allows you to easily
3200# move the test cases to other locations or to other machines.
3201#
3202THIS_DIR := $variable{"PWD"}
3203
8d1491ba
SR
3204# Define each test with TEST_START
3205# The config options below it will override the defaults
3206TEST_START
c4261d0f 3207TEST_TYPE = $default{"TEST_TYPE"}
8d1491ba
SR
3208
3209DEFAULTS
3210EOF
3211;
3212 close(OUT);
3213}
3214read_config $ktest_config;
3215
23715c3c
SR
3216if (defined($opt{"LOG_FILE"})) {
3217 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3218}
3219
8d1491ba
SR
3220# Append any configs entered in manually to the config file.
3221my @new_configs = keys %entered_configs;
3222if ($#new_configs >= 0) {
3223 print "\nAppending entered in configs to $ktest_config\n";
3224 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3225 foreach my $config (@new_configs) {
3226 print OUT "$config = $entered_configs{$config}\n";
0e7a22de 3227 $opt{$config} = process_variables($entered_configs{$config});
8d1491ba
SR
3228 }
3229}
2545eb61 3230
2b7d9b21
SR
3231if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3232 unlink $opt{"LOG_FILE"};
3233}
2545eb61 3234
2b7d9b21
SR
3235doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3236
a57419b3
SR
3237for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3238
3239 if (!$i) {
3240 doprint "DEFAULT OPTIONS:\n";
3241 } else {
3242 doprint "\nTEST $i OPTIONS";
3243 if (defined($repeat_tests{$i})) {
3244 $repeat = $repeat_tests{$i};
3245 doprint " ITERATE $repeat";
3246 }
3247 doprint "\n";
3248 }
3249
3250 foreach my $option (sort keys %opt) {
3251
3252 if ($option =~ /\[(\d+)\]$/) {
3253 next if ($i != $1);
3254 } else {
3255 next if ($i);
3256 }
3257
3258 doprint "$option = $opt{$option}\n";
3259 }
2b7d9b21 3260}
2545eb61 3261
2a62512b 3262sub __set_test_option {
5a391fbf 3263 my ($name, $i) = @_;
2545eb61 3264
5a391fbf 3265 my $option = "$name\[$i\]";
5c42fc5b 3266
5a391fbf
SR
3267 if (defined($opt{$option})) {
3268 return $opt{$option};
5f9b6ced
SR
3269 }
3270
a57419b3
SR
3271 foreach my $test (keys %repeat_tests) {
3272 if ($i >= $test &&
3273 $i < $test + $repeat_tests{$test}) {
3274 $option = "$name\[$test\]";
3275 if (defined($opt{$option})) {
3276 return $opt{$option};
3277 }
3278 }
3279 }
3280
5a391fbf
SR
3281 if (defined($opt{$name})) {
3282 return $opt{$name};
2545eb61
SR
3283 }
3284
5a391fbf
SR
3285 return undef;
3286}
3287
2a62512b
SR
3288sub set_test_option {
3289 my ($name, $i) = @_;
3290
3291 my $option = __set_test_option($name, $i);
3292 return $option if (!defined($option));
3293
23715c3c 3294 return eval_option($option, $i);
2a62512b
SR
3295}
3296
5a391fbf 3297# First we need to do is the builds
a75fecec
SR
3298for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
3299
4ab1cce5
SR
3300 # Do not reboot on failing test options
3301 $no_reboot = 1;
3302
576f627c
SR
3303 $iteration = $i;
3304
a75fecec
SR
3305 my $makecmd = set_test_option("MAKE_CMD", $i);
3306
3307 $machine = set_test_option("MACHINE", $i);
e48c5293 3308 $ssh_user = set_test_option("SSH_USER", $i);
a75fecec
SR
3309 $tmpdir = set_test_option("TMP_DIR", $i);
3310 $outputdir = set_test_option("OUTPUT_DIR", $i);
3311 $builddir = set_test_option("BUILD_DIR", $i);
3312 $test_type = set_test_option("TEST_TYPE", $i);
3313 $build_type = set_test_option("BUILD_TYPE", $i);
3314 $build_options = set_test_option("BUILD_OPTIONS", $i);
0bd6c1a3
SR
3315 $pre_build = set_test_option("PRE_BUILD", $i);
3316 $post_build = set_test_option("POST_BUILD", $i);
3317 $pre_build_die = set_test_option("PRE_BUILD_DIE", $i);
3318 $post_build_die = set_test_option("POST_BUILD_DIE", $i);
a75fecec 3319 $power_cycle = set_test_option("POWER_CYCLE", $i);
e48c5293 3320 $reboot = set_test_option("REBOOT", $i);
a75fecec
SR
3321 $noclean = set_test_option("BUILD_NOCLEAN", $i);
3322 $minconfig = set_test_option("MIN_CONFIG", $i);
4c4ab120
SR
3323 $output_minconfig = set_test_option("OUTPUT_MIN_CONFIG", $i);
3324 $start_minconfig = set_test_option("START_MIN_CONFIG", $i);
3325 $ignore_config = set_test_option("IGNORE_CONFIG", $i);
a75fecec
SR
3326 $run_test = set_test_option("TEST", $i);
3327 $addconfig = set_test_option("ADD_CONFIG", $i);
3328 $reboot_type = set_test_option("REBOOT_TYPE", $i);
3329 $grub_menu = set_test_option("GRUB_MENU", $i);
8b37ca8c 3330 $post_install = set_test_option("POST_INSTALL", $i);
e0a8742e 3331 $no_install = set_test_option("NO_INSTALL", $i);
a75fecec
SR
3332 $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
3333 $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
bc7c5803
SR
3334 $switch_to_good = set_test_option("SWITCH_TO_GOOD", $i);
3335 $switch_to_test = set_test_option("SWITCH_TO_TEST", $i);
a75fecec
SR
3336 $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
3337 $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
3338 $power_off = set_test_option("POWER_OFF", $i);
576f627c
SR
3339 $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
3340 $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
a75fecec
SR
3341 $sleep_time = set_test_option("SLEEP_TIME", $i);
3342 $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
27d934b2 3343 $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
1990207d 3344 $ignore_warnings = set_test_option("IGNORE_WARNINGS", $i);
c960bb9f 3345 $bisect_manual = set_test_option("BISECT_MANUAL", $i);
c23dca7c 3346 $bisect_skip = set_test_option("BISECT_SKIP", $i);
30f75da5 3347 $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
c5dacb88
SR
3348 $bisect_ret_good = set_test_option("BISECT_RET_GOOD", $i);
3349 $bisect_ret_bad = set_test_option("BISECT_RET_BAD", $i);
3350 $bisect_ret_skip = set_test_option("BISECT_RET_SKIP", $i);
3351 $bisect_ret_abort = set_test_option("BISECT_RET_ABORT", $i);
3352 $bisect_ret_default = set_test_option("BISECT_RET_DEFAULT", $i);
a75fecec 3353 $store_failures = set_test_option("STORE_FAILURES", $i);
de5b6e3b 3354 $store_successes = set_test_option("STORE_SUCCESSES", $i);
9064af52 3355 $test_name = set_test_option("TEST_NAME", $i);
a75fecec
SR
3356 $timeout = set_test_option("TIMEOUT", $i);
3357 $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
3358 $console = set_test_option("CONSOLE", $i);
f1a5b962 3359 $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
a75fecec 3360 $success_line = set_test_option("SUCCESS_LINE", $i);
2b803365 3361 $reboot_success_line = set_test_option("REBOOT_SUCCESS_LINE", $i);
1c8a617a
SR
3362 $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
3363 $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
2d01b26a 3364 $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
a75fecec 3365 $build_target = set_test_option("BUILD_TARGET", $i);
e48c5293
SR
3366 $ssh_exec = set_test_option("SSH_EXEC", $i);
3367 $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
a75fecec
SR
3368 $target_image = set_test_option("TARGET_IMAGE", $i);
3369 $localversion = set_test_option("LOCALVERSION", $i);
3370
35ce5952
SR
3371 $start_minconfig_defined = 1;
3372
4c4ab120 3373 if (!defined($start_minconfig)) {
35ce5952 3374 $start_minconfig_defined = 0;
4c4ab120
SR
3375 $start_minconfig = $minconfig;
3376 }
3377
a75fecec
SR
3378 chdir $builddir || die "can't change directory to $builddir";
3379
a908a665
AJ
3380 foreach my $dir ($tmpdir, $outputdir) {
3381 if (!-d $dir) {
3382 mkpath($dir) or
3383 die "can't create $dir";
3384 }
a75fecec 3385 }
1a5cfce3 3386
e48c5293
SR
3387 $ENV{"SSH_USER"} = $ssh_user;
3388 $ENV{"MACHINE"} = $machine;
3389
a75fecec 3390 $buildlog = "$tmpdir/buildlog-$machine";
a9dd5d63 3391 $testlog = "$tmpdir/testlog-$machine";
a75fecec
SR
3392 $dmesg = "$tmpdir/dmesg-$machine";
3393 $make = "$makecmd O=$outputdir";
51ad1dd1 3394 $output_config = "$outputdir/.config";
a75fecec 3395
bb8474b1
SR
3396 if (!$buildonly) {
3397 $target = "$ssh_user\@$machine";
3398 if ($reboot_type eq "grub") {
3399 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3400 } elsif (!defined($reboot_script)) {
3401 dodie "REBOOT_SCRIPT not defined"
3402 }
a75fecec
SR
3403 }
3404
3405 my $run_type = $build_type;
3406 if ($test_type eq "patchcheck") {
3407 $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
3408 } elsif ($test_type eq "bisect") {
3409 $run_type = $opt{"BISECT_TYPE[$i]"};
0a05c769
SR
3410 } elsif ($test_type eq "config_bisect") {
3411 $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
a75fecec
SR
3412 }
3413
4c4ab120
SR
3414 if ($test_type eq "make_min_config") {
3415 $run_type = "";
3416 }
3417
a75fecec
SR
3418 # mistake in config file?
3419 if (!defined($run_type)) {
3420 $run_type = "ERROR";
3421 }
5a391fbf 3422
e0a8742e
SR
3423 my $installme = "";
3424 $installme = " no_install" if ($no_install);
3425
2545eb61 3426 doprint "\n\n";
e0a8742e 3427 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
7faafbd6
SR
3428
3429 unlink $dmesg;
3430 unlink $buildlog;
a9dd5d63 3431 unlink $testlog;
2545eb61 3432
250bae8b
SR
3433 if (defined($addconfig)) {
3434 my $min = $minconfig;
3435 if (!defined($minconfig)) {
3436 $min = "";
3437 }
3438 run_command "cat $addconfig $min > $tmpdir/add_config" or
2b7d9b21 3439 dodie "Failed to create temp config";
9be2e6b5 3440 $minconfig = "$tmpdir/add_config";
2b7d9b21
SR
3441 }
3442
6c5ee0be
SR
3443 my $checkout = $opt{"CHECKOUT[$i]"};
3444 if (defined($checkout)) {
3445 run_command "git checkout $checkout" or
3446 die "failed to checkout $checkout";
3447 }
3448
4ab1cce5
SR
3449 $no_reboot = 0;
3450
3451
a75fecec 3452 if ($test_type eq "bisect") {
5f9b6ced
SR
3453 bisect $i;
3454 next;
0a05c769
SR
3455 } elsif ($test_type eq "config_bisect") {
3456 config_bisect $i;
3457 next;
a75fecec 3458 } elsif ($test_type eq "patchcheck") {
6c5ee0be
SR
3459 patchcheck $i;
3460 next;
4c4ab120
SR
3461 } elsif ($test_type eq "make_min_config") {
3462 make_min_config $i;
3463 next;
2545eb61 3464 }
2545eb61 3465
7faafbd6
SR
3466 if ($build_type ne "nobuild") {
3467 build $build_type or next;
2545eb61
SR
3468 }
3469
cd8e368f
SR
3470 if ($test_type eq "install") {
3471 get_version;
3472 install;
3473 success $i;
3474 next;
3475 }
3476
a75fecec 3477 if ($test_type ne "build") {
a75fecec 3478 my $failed = 0;
ddf607e5 3479 start_monitor_and_boot or $failed = 1;
a75fecec
SR
3480
3481 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3482 do_run_test or $failed = 1;
3483 }
3484 end_monitor;
3485 next if ($failed);
5a391fbf
SR
3486 }
3487
5f9b6ced 3488 success $i;
2545eb61
SR
3489}
3490
5c42fc5b 3491if ($opt{"POWEROFF_ON_SUCCESS"}) {
75c3fda7 3492 halt;
576f627c 3493} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
bc7c5803 3494 reboot_to_good;
5c42fc5b 3495}
75c3fda7 3496
e48c5293
SR
3497doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3498
2545eb61 3499exit 0;