ktest: Implement our own force min config
[platform/adaptation/renesas_rcar/renesas_kernel.git] / tools / testing / ktest / ktest.pl
1 #!/usr/bin/perl -w
2 #
3 # Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
4 # Licensed under the terms of the GNU GPL License version 2
5 #
6
7 use strict;
8 use IPC::Open2;
9 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
10 use File::Path qw(mkpath);
11 use File::Copy qw(cp);
12 use FileHandle;
13
14 my $VERSION = "0.2";
15
16 $| = 1;
17
18 my %opt;
19 my %repeat_tests;
20 my %repeats;
21 my %default;
22
23 #default opts
24 $default{"NUM_TESTS"}           = 1;
25 $default{"REBOOT_TYPE"}         = "grub";
26 $default{"TEST_TYPE"}           = "test";
27 $default{"BUILD_TYPE"}          = "randconfig";
28 $default{"MAKE_CMD"}            = "make";
29 $default{"TIMEOUT"}             = 120;
30 $default{"TMP_DIR"}             = "/tmp/ktest";
31 $default{"SLEEP_TIME"}          = 60;   # sleep time between tests
32 $default{"BUILD_NOCLEAN"}       = 0;
33 $default{"REBOOT_ON_ERROR"}     = 0;
34 $default{"POWEROFF_ON_ERROR"}   = 0;
35 $default{"REBOOT_ON_SUCCESS"}   = 1;
36 $default{"POWEROFF_ON_SUCCESS"} = 0;
37 $default{"BUILD_OPTIONS"}       = "";
38 $default{"BISECT_SLEEP_TIME"}   = 60;   # sleep time between bisects
39 $default{"PATCHCHECK_SLEEP_TIME"} = 60; # sleep time between patch checks
40 $default{"CLEAR_LOG"}           = 0;
41 $default{"BISECT_MANUAL"}       = 0;
42 $default{"BISECT_SKIP"}         = 1;
43 $default{"SUCCESS_LINE"}        = "login:";
44 $default{"DETECT_TRIPLE_FAULT"} = 1;
45 $default{"BOOTED_TIMEOUT"}      = 1;
46 $default{"DIE_ON_FAILURE"}      = 1;
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";
50 $default{"STOP_AFTER_SUCCESS"}  = 10;
51 $default{"STOP_AFTER_FAILURE"}  = 60;
52 $default{"STOP_TEST_AFTER"}     = 600;
53 $default{"LOCALVERSION"}        = "-test";
54
55 my $ktest_config;
56 my $version;
57 my $machine;
58 my $ssh_user;
59 my $tmpdir;
60 my $builddir;
61 my $outputdir;
62 my $output_config;
63 my $test_type;
64 my $build_type;
65 my $build_options;
66 my $reboot_type;
67 my $reboot_script;
68 my $power_cycle;
69 my $reboot;
70 my $reboot_on_error;
71 my $poweroff_on_error;
72 my $die_on_failure;
73 my $powercycle_after_reboot;
74 my $poweroff_after_halt;
75 my $ssh_exec;
76 my $scp_to_target;
77 my $power_off;
78 my $grub_menu;
79 my $grub_number;
80 my $target;
81 my $make;
82 my $post_install;
83 my $noclean;
84 my $minconfig;
85 my $addconfig;
86 my $in_bisect = 0;
87 my $bisect_bad = "";
88 my $reverse_bisect;
89 my $bisect_manual;
90 my $bisect_skip;
91 my $config_bisect_good;
92 my $in_patchcheck = 0;
93 my $run_test;
94 my $redirect;
95 my $buildlog;
96 my $dmesg;
97 my $monitor_fp;
98 my $monitor_pid;
99 my $monitor_cnt = 0;
100 my $sleep_time;
101 my $bisect_sleep_time;
102 my $patchcheck_sleep_time;
103 my $store_failures;
104 my $test_name;
105 my $timeout;
106 my $booted_timeout;
107 my $detect_triplefault;
108 my $console;
109 my $success_line;
110 my $stop_after_success;
111 my $stop_after_failure;
112 my $stop_test_after;
113 my $build_target;
114 my $target_image;
115 my $localversion;
116 my $iteration = 0;
117 my $successes = 0;
118
119 my %entered_configs;
120 my %config_help;
121 my %variable;
122 my %force_config;
123
124 $config_help{"MACHINE"} = << "EOF"
125  The machine hostname that you will test.
126 EOF
127     ;
128 $config_help{"SSH_USER"} = << "EOF"
129  The box is expected to have ssh on normal bootup, provide the user
130   (most likely root, since you need privileged operations)
131 EOF
132     ;
133 $config_help{"BUILD_DIR"} = << "EOF"
134  The directory that contains the Linux source code (full path).
135 EOF
136     ;
137 $config_help{"OUTPUT_DIR"} = << "EOF"
138  The directory that the objects will be built (full path).
139  (can not be same as BUILD_DIR)
140 EOF
141     ;
142 $config_help{"BUILD_TARGET"} = << "EOF"
143  The location of the compiled file to copy to the target.
144  (relative to OUTPUT_DIR)
145 EOF
146     ;
147 $config_help{"TARGET_IMAGE"} = << "EOF"
148  The place to put your image on the test machine.
149 EOF
150     ;
151 $config_help{"POWER_CYCLE"} = << "EOF"
152  A script or command to reboot the box.
153
154  Here is a digital loggers power switch example
155  POWER_CYCLE = wget --no-proxy -O /dev/null -q  --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
156
157  Here is an example to reboot a virtual box on the current host
158  with the name "Guest".
159  POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
160 EOF
161     ;
162 $config_help{"CONSOLE"} = << "EOF"
163  The script or command that reads the console
164
165   If you use ttywatch server, something like the following would work.
166 CONSOLE = nc -d localhost 3001
167
168  For a virtual machine with guest name "Guest".
169 CONSOLE =  virsh console Guest
170 EOF
171     ;
172 $config_help{"LOCALVERSION"} = << "EOF"
173  Required version ending to differentiate the test
174  from other linux builds on the system.
175 EOF
176     ;
177 $config_help{"REBOOT_TYPE"} = << "EOF"
178  Way to reboot the box to the test kernel.
179  Only valid options so far are "grub" and "script".
180
181  If you specify grub, it will assume grub version 1
182  and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
183  and select that target to reboot to the kernel. If this is not
184  your setup, then specify "script" and have a command or script
185  specified in REBOOT_SCRIPT to boot to the target.
186
187  The entry in /boot/grub/menu.lst must be entered in manually.
188  The test will not modify that file.
189 EOF
190     ;
191 $config_help{"GRUB_MENU"} = << "EOF"
192  The grub title name for the test kernel to boot
193  (Only mandatory if REBOOT_TYPE = grub)
194
195  Note, ktest.pl will not update the grub menu.lst, you need to
196  manually add an option for the test. ktest.pl will search
197  the grub menu.lst for this option to find what kernel to
198  reboot into.
199
200  For example, if in the /boot/grub/menu.lst the test kernel title has:
201  title Test Kernel
202  kernel vmlinuz-test
203  GRUB_MENU = Test Kernel
204 EOF
205     ;
206 $config_help{"REBOOT_SCRIPT"} = << "EOF"
207  A script to reboot the target into the test kernel
208  (Only mandatory if REBOOT_TYPE = script)
209 EOF
210     ;
211
212
213 sub get_ktest_config {
214     my ($config) = @_;
215
216     return if (defined($opt{$config}));
217
218     if (defined($config_help{$config})) {
219         print "\n";
220         print $config_help{$config};
221     }
222
223     for (;;) {
224         print "$config = ";
225         if (defined($default{$config})) {
226             print "\[$default{$config}\] ";
227         }
228         $entered_configs{$config} = <STDIN>;
229         $entered_configs{$config} =~ s/^\s*(.*\S)\s*$/$1/;
230         if ($entered_configs{$config} =~ /^\s*$/) {
231             if ($default{$config}) {
232                 $entered_configs{$config} = $default{$config};
233             } else {
234                 print "Your answer can not be blank\n";
235                 next;
236             }
237         }
238         last;
239     }
240 }
241
242 sub get_ktest_configs {
243     get_ktest_config("MACHINE");
244     get_ktest_config("SSH_USER");
245     get_ktest_config("BUILD_DIR");
246     get_ktest_config("OUTPUT_DIR");
247     get_ktest_config("BUILD_TARGET");
248     get_ktest_config("TARGET_IMAGE");
249     get_ktest_config("POWER_CYCLE");
250     get_ktest_config("CONSOLE");
251     get_ktest_config("LOCALVERSION");
252
253     my $rtype = $opt{"REBOOT_TYPE"};
254
255     if (!defined($rtype)) {
256         if (!defined($opt{"GRUB_MENU"})) {
257             get_ktest_config("REBOOT_TYPE");
258             $rtype = $entered_configs{"REBOOT_TYPE"};
259         } else {
260             $rtype = "grub";
261         }
262     }
263
264     if ($rtype eq "grub") {
265         get_ktest_config("GRUB_MENU");
266     } else {
267         get_ktest_config("REBOOT_SCRIPT");
268     }
269 }
270
271 sub process_variables {
272     my ($value) = @_;
273     my $retval = "";
274
275     # We want to check for '\', and it is just easier
276     # to check the previous characet of '$' and not need
277     # to worry if '$' is the first character. By adding
278     # a space to $value, we can just check [^\\]\$ and
279     # it will still work.
280     $value = " $value";
281
282     while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
283         my $begin = $1;
284         my $var = $2;
285         my $end = $3;
286         # append beginning of value to retval
287         $retval = "$retval$begin";
288         if (defined($variable{$var})) {
289             $retval = "$retval$variable{$var}";
290         } else {
291             # put back the origin piece.
292             $retval = "$retval\$\{$var\}";
293         }
294         $value = $end;
295     }
296     $retval = "$retval$value";
297
298     # remove the space added in the beginning
299     $retval =~ s/ //;
300
301     return "$retval"
302 }
303
304 sub set_value {
305     my ($lvalue, $rvalue) = @_;
306
307     if (defined($opt{$lvalue})) {
308         die "Error: Option $lvalue defined more than once!\n";
309     }
310     if ($rvalue =~ /^\s*$/) {
311         delete $opt{$lvalue};
312     } else {
313         $rvalue = process_variables($rvalue);
314         $opt{$lvalue} = $rvalue;
315     }
316 }
317
318 sub set_variable {
319     my ($lvalue, $rvalue) = @_;
320
321     if ($rvalue =~ /^\s*$/) {
322         delete $variable{$lvalue};
323     } else {
324         $rvalue = process_variables($rvalue);
325         $variable{$lvalue} = $rvalue;
326     }
327 }
328
329 sub read_config {
330     my ($config) = @_;
331
332     open(IN, $config) || die "can't read file $config";
333
334     my $name = $config;
335     $name =~ s,.*/(.*),$1,;
336
337     my $test_num = 0;
338     my $default = 1;
339     my $repeat = 1;
340     my $num_tests_set = 0;
341     my $skip = 0;
342     my $rest;
343
344     while (<IN>) {
345
346         # ignore blank lines and comments
347         next if (/^\s*$/ || /\s*\#/);
348
349         if (/^\s*TEST_START(.*)/) {
350
351             $rest = $1;
352
353             if ($num_tests_set) {
354                 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
355             }
356
357             my $old_test_num = $test_num;
358             my $old_repeat = $repeat;
359
360             $test_num += $repeat;
361             $default = 0;
362             $repeat = 1;
363
364             if ($rest =~ /\s+SKIP(.*)/) {
365                 $rest = $1;
366                 $skip = 1;
367             } else {
368                 $skip = 0;
369             }
370
371             if ($rest =~ /\s+ITERATE\s+(\d+)(.*)$/) {
372                 $repeat = $1;
373                 $rest = $2;
374                 $repeat_tests{"$test_num"} = $repeat;
375             }
376
377             if ($rest =~ /\s+SKIP(.*)/) {
378                 $rest = $1;
379                 $skip = 1;
380             }
381
382             if ($rest !~ /^\s*$/) {
383                 die "$name: $.: Gargbage found after TEST_START\n$_";
384             }
385
386             if ($skip) {
387                 $test_num = $old_test_num;
388                 $repeat = $old_repeat;
389             }
390
391         } elsif (/^\s*DEFAULTS(.*)$/) {
392             $default = 1;
393
394             $rest = $1;
395
396             if ($rest =~ /\s+SKIP(.*)/) {
397                 $rest = $1;
398                 $skip = 1;
399             } else {
400                 $skip = 0;
401             }
402
403             if ($rest !~ /^\s*$/) {
404                 die "$name: $.: Gargbage found after DEFAULTS\n$_";
405             }
406
407         } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
408
409             next if ($skip);
410
411             my $lvalue = $1;
412             my $rvalue = $2;
413
414             if (!$default &&
415                 ($lvalue eq "NUM_TESTS" ||
416                  $lvalue eq "LOG_FILE" ||
417                  $lvalue eq "CLEAR_LOG")) {
418                 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
419             }
420
421             if ($lvalue eq "NUM_TESTS") {
422                 if ($test_num) {
423                     die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
424                 }
425                 if (!$default) {
426                     die "$name: $.: NUM_TESTS must be set in default section\n";
427                 }
428                 $num_tests_set = 1;
429             }
430
431             if ($default || $lvalue =~ /\[\d+\]$/) {
432                 set_value($lvalue, $rvalue);
433             } else {
434                 my $val = "$lvalue\[$test_num\]";
435                 set_value($val, $rvalue);
436
437                 if ($repeat > 1) {
438                     $repeats{$val} = $repeat;
439                 }
440             }
441         } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
442             next if ($skip);
443
444             my $lvalue = $1;
445             my $rvalue = $2;
446
447             # process config variables.
448             # Config variables are only active while reading the
449             # config and can be defined anywhere. They also ignore
450             # TEST_START and DEFAULTS, but are skipped if they are in
451             # on of these sections that have SKIP defined.
452             # The save variable can be
453             # defined multiple times and the new one simply overrides
454             # the prevous one.
455             set_variable($lvalue, $rvalue);
456
457         } else {
458             die "$name: $.: Garbage found in config\n$_";
459         }
460     }
461
462     close(IN);
463
464     if ($test_num) {
465         $test_num += $repeat - 1;
466         $opt{"NUM_TESTS"} = $test_num;
467     }
468
469     # make sure we have all mandatory configs
470     get_ktest_configs;
471
472     # set any defaults
473
474     foreach my $default (keys %default) {
475         if (!defined($opt{$default})) {
476             $opt{$default} = $default{$default};
477         }
478     }
479 }
480
481 sub _logit {
482     if (defined($opt{"LOG_FILE"})) {
483         open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
484         print OUT @_;
485         close(OUT);
486     }
487 }
488
489 sub logit {
490     if (defined($opt{"LOG_FILE"})) {
491         _logit @_;
492     } else {
493         print @_;
494     }
495 }
496
497 sub doprint {
498     print @_;
499     _logit @_;
500 }
501
502 sub run_command;
503
504 sub reboot {
505     # try to reboot normally
506     if (run_command $reboot) {
507         if (defined($powercycle_after_reboot)) {
508             sleep $powercycle_after_reboot;
509             run_command "$power_cycle";
510         }
511     } else {
512         # nope? power cycle it.
513         run_command "$power_cycle";
514     }
515 }
516
517 sub do_not_reboot {
518     my $i = $iteration;
519
520     return $test_type eq "build" ||
521         ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
522         ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
523 }
524
525 sub dodie {
526     doprint "CRITICAL FAILURE... ", @_, "\n";
527
528     my $i = $iteration;
529
530     if ($reboot_on_error && !do_not_reboot) {
531
532         doprint "REBOOTING\n";
533         reboot;
534
535     } elsif ($poweroff_on_error && defined($power_off)) {
536         doprint "POWERING OFF\n";
537         `$power_off`;
538     }
539
540     if (defined($opt{"LOG_FILE"})) {
541         print " See $opt{LOG_FILE} for more info.\n";
542     }
543
544     die @_, "\n";
545 }
546
547 sub open_console {
548     my ($fp) = @_;
549
550     my $flags;
551
552     my $pid = open($fp, "$console|") or
553         dodie "Can't open console $console";
554
555     $flags = fcntl($fp, F_GETFL, 0) or
556         dodie "Can't get flags for the socket: $!";
557     $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
558         dodie "Can't set flags for the socket: $!";
559
560     return $pid;
561 }
562
563 sub close_console {
564     my ($fp, $pid) = @_;
565
566     doprint "kill child process $pid\n";
567     kill 2, $pid;
568
569     print "closing!\n";
570     close($fp);
571 }
572
573 sub start_monitor {
574     if ($monitor_cnt++) {
575         return;
576     }
577     $monitor_fp = \*MONFD;
578     $monitor_pid = open_console $monitor_fp;
579
580     return;
581
582     open(MONFD, "Stop perl from warning about single use of MONFD");
583 }
584
585 sub end_monitor {
586     if (--$monitor_cnt) {
587         return;
588     }
589     close_console($monitor_fp, $monitor_pid);
590 }
591
592 sub wait_for_monitor {
593     my ($time) = @_;
594     my $line;
595
596     doprint "** Wait for monitor to settle down **\n";
597
598     # read the monitor and wait for the system to calm down
599     do {
600         $line = wait_for_input($monitor_fp, $time);
601         print "$line" if (defined($line));
602     } while (defined($line));
603     print "** Monitor flushed **\n";
604 }
605
606 sub fail {
607
608         if ($die_on_failure) {
609                 dodie @_;
610         }
611
612         doprint "FAILED\n";
613
614         my $i = $iteration;
615
616         # no need to reboot for just building.
617         if (!do_not_reboot) {
618             doprint "REBOOTING\n";
619             reboot;
620             start_monitor;
621             wait_for_monitor $sleep_time;
622             end_monitor;
623         }
624
625         my $name = "";
626
627         if (defined($test_name)) {
628             $name = " ($test_name)";
629         }
630
631         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
632         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
633         doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
634         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
635         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
636
637         return 1 if (!defined($store_failures));
638
639         my @t = localtime;
640         my $date = sprintf "%04d%02d%02d%02d%02d%02d",
641                 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
642
643         my $type = $build_type;
644         if ($type =~ /useconfig/) {
645             $type = "useconfig";
646         }
647
648         my $dir = "$machine-$test_type-$type-fail-$date";
649         my $faildir = "$store_failures/$dir";
650
651         if (!-d $faildir) {
652             mkpath($faildir) or
653                 die "can't create $faildir";
654         }
655         if (-f "$output_config") {
656             cp "$output_config", "$faildir/config" or
657                 die "failed to copy .config";
658         }
659         if (-f $buildlog) {
660             cp $buildlog, "$faildir/buildlog" or
661                 die "failed to move $buildlog";
662         }
663         if (-f $dmesg) {
664             cp $dmesg, "$faildir/dmesg" or
665                 die "failed to move $dmesg";
666         }
667
668         doprint "*** Saved info to $faildir ***\n";
669
670         return 1;
671 }
672
673 sub run_command {
674     my ($command) = @_;
675     my $dolog = 0;
676     my $dord = 0;
677     my $pid;
678
679     $command =~ s/\$SSH_USER/$ssh_user/g;
680     $command =~ s/\$MACHINE/$machine/g;
681
682     doprint("$command ... ");
683
684     $pid = open(CMD, "$command 2>&1 |") or
685         (fail "unable to exec $command" and return 0);
686
687     if (defined($opt{"LOG_FILE"})) {
688         open(LOG, ">>$opt{LOG_FILE}") or
689             dodie "failed to write to log";
690         $dolog = 1;
691     }
692
693     if (defined($redirect)) {
694         open (RD, ">$redirect") or
695             dodie "failed to write to redirect $redirect";
696         $dord = 1;
697     }
698
699     while (<CMD>) {
700         print LOG if ($dolog);
701         print RD  if ($dord);
702     }
703
704     waitpid($pid, 0);
705     my $failed = $?;
706
707     close(CMD);
708     close(LOG) if ($dolog);
709     close(RD)  if ($dord);
710
711     if ($failed) {
712         doprint "FAILED!\n";
713     } else {
714         doprint "SUCCESS\n";
715     }
716
717     return !$failed;
718 }
719
720 sub run_ssh {
721     my ($cmd) = @_;
722     my $cp_exec = $ssh_exec;
723
724     $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
725     return run_command "$cp_exec";
726 }
727
728 sub run_scp {
729     my ($src, $dst) = @_;
730     my $cp_scp = $scp_to_target;
731
732     $cp_scp =~ s/\$SRC_FILE/$src/g;
733     $cp_scp =~ s/\$DST_FILE/$dst/g;
734
735     return run_command "$cp_scp";
736 }
737
738 sub get_grub_index {
739
740     if ($reboot_type ne "grub") {
741         return;
742     }
743     return if (defined($grub_number));
744
745     doprint "Find grub menu ... ";
746     $grub_number = -1;
747
748     my $ssh_grub = $ssh_exec;
749     $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
750
751     open(IN, "$ssh_grub |")
752         or die "unable to get menu.lst";
753
754     while (<IN>) {
755         if (/^\s*title\s+$grub_menu\s*$/) {
756             $grub_number++;
757             last;
758         } elsif (/^\s*title\s/) {
759             $grub_number++;
760         }
761     }
762     close(IN);
763
764     die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
765         if ($grub_number < 0);
766     doprint "$grub_number\n";
767 }
768
769 sub wait_for_input
770 {
771     my ($fp, $time) = @_;
772     my $rin;
773     my $ready;
774     my $line;
775     my $ch;
776
777     if (!defined($time)) {
778         $time = $timeout;
779     }
780
781     $rin = '';
782     vec($rin, fileno($fp), 1) = 1;
783     $ready = select($rin, undef, undef, $time);
784
785     $line = "";
786
787     # try to read one char at a time
788     while (sysread $fp, $ch, 1) {
789         $line .= $ch;
790         last if ($ch eq "\n");
791     }
792
793     if (!length($line)) {
794         return undef;
795     }
796
797     return $line;
798 }
799
800 sub reboot_to {
801     if ($reboot_type eq "grub") {
802         run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch && reboot)'";
803         return;
804     }
805
806     run_command "$reboot_script";
807 }
808
809 sub get_sha1 {
810     my ($commit) = @_;
811
812     doprint "git rev-list --max-count=1 $commit ... ";
813     my $sha1 = `git rev-list --max-count=1 $commit`;
814     my $ret = $?;
815
816     logit $sha1;
817
818     if ($ret) {
819         doprint "FAILED\n";
820         dodie "Failed to get git $commit";
821     }
822
823     print "SUCCESS\n";
824
825     chomp $sha1;
826
827     return $sha1;
828 }
829
830 sub monitor {
831     my $booted = 0;
832     my $bug = 0;
833     my $skip_call_trace = 0;
834     my $loops;
835
836     wait_for_monitor 5;
837
838     my $line;
839     my $full_line = "";
840
841     open(DMESG, "> $dmesg") or
842         die "unable to write to $dmesg";
843
844     reboot_to;
845
846     my $success_start;
847     my $failure_start;
848     my $monitor_start = time;
849     my $done = 0;
850     my $version_found = 0;
851
852     while (!$done) {
853
854         if ($booted) {
855             $line = wait_for_input($monitor_fp, $booted_timeout);
856             if (!defined($line)) {
857                 my $s = $booted_timeout == 1 ? "" : "s";
858                 doprint "Successful boot found: break after $booted_timeout second$s\n";
859                 last;
860             }
861         } else {
862             $line = wait_for_input($monitor_fp);
863             if (!defined($line)) {
864                 my $s = $timeout == 1 ? "" : "s";
865                 doprint "Timed out after $timeout second$s\n";
866                 last;
867             }
868         }
869
870         doprint $line;
871         print DMESG $line;
872
873         # we are not guaranteed to get a full line
874         $full_line .= $line;
875
876         if ($full_line =~ /$success_line/) {
877             $booted = 1;
878             $success_start = time;
879         }
880
881         if ($booted && defined($stop_after_success) &&
882             $stop_after_success >= 0) {
883             my $now = time;
884             if ($now - $success_start >= $stop_after_success) {
885                 doprint "Test forced to stop after $stop_after_success seconds after success\n";
886                 last;
887             }
888         }
889
890         if ($full_line =~ /\[ backtrace testing \]/) {
891             $skip_call_trace = 1;
892         }
893
894         if ($full_line =~ /call trace:/i) {
895             if (!$bug && !$skip_call_trace) {
896                 $bug = 1;
897                 $failure_start = time;
898             }
899         }
900
901         if ($bug && defined($stop_after_failure) &&
902             $stop_after_failure >= 0) {
903             my $now = time;
904             if ($now - $failure_start >= $stop_after_failure) {
905                 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
906                 last;
907             }
908         }
909
910         if ($full_line =~ /\[ end of backtrace testing \]/) {
911             $skip_call_trace = 0;
912         }
913
914         if ($full_line =~ /Kernel panic -/) {
915             $failure_start = time;
916             $bug = 1;
917         }
918
919         # Detect triple faults by testing the banner
920         if ($full_line =~ /\bLinux version (\S+).*\n/) {
921             if ($1 eq $version) {
922                 $version_found = 1;
923             } elsif ($version_found && $detect_triplefault) {
924                 # We already booted into the kernel we are testing,
925                 # but now we booted into another kernel?
926                 # Consider this a triple fault.
927                 doprint "Aleady booted in Linux kernel $version, but now\n";
928                 doprint "we booted into Linux kernel $1.\n";
929                 doprint "Assuming that this is a triple fault.\n";
930                 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
931                 last;
932             }
933         }
934
935         if ($line =~ /\n/) {
936             $full_line = "";
937         }
938
939         if ($stop_test_after > 0 && !$booted && !$bug) {
940             if (time - $monitor_start > $stop_test_after) {
941                 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
942                 $done = 1;
943             }
944         }
945     }
946
947     close(DMESG);
948
949     if ($bug) {
950         return 0 if ($in_bisect);
951         fail "failed - got a bug report" and return 0;
952     }
953
954     if (!$booted) {
955         return 0 if ($in_bisect);
956         fail "failed - never got a boot prompt." and return 0;
957     }
958
959     return 1;
960 }
961
962 sub install {
963
964     run_scp "$outputdir/$build_target", "$target_image" or
965         dodie "failed to copy image";
966
967     my $install_mods = 0;
968
969     # should we process modules?
970     $install_mods = 0;
971     open(IN, "$output_config") or dodie("Can't read config file");
972     while (<IN>) {
973         if (/CONFIG_MODULES(=y)?/) {
974             $install_mods = 1 if (defined($1));
975             last;
976         }
977     }
978     close(IN);
979
980     if (!$install_mods) {
981         doprint "No modules needed\n";
982         return;
983     }
984
985     run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
986         dodie "Failed to install modules";
987
988     my $modlib = "/lib/modules/$version";
989     my $modtar = "ktest-mods.tar.bz2";
990
991     run_ssh "rm -rf $modlib" or
992         dodie "failed to remove old mods: $modlib";
993
994     # would be nice if scp -r did not follow symbolic links
995     run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
996         dodie "making tarball";
997
998     run_scp "$tmpdir/$modtar", "/tmp" or
999         dodie "failed to copy modules";
1000
1001     unlink "$tmpdir/$modtar";
1002
1003     run_ssh "'(cd / && tar xf /tmp/$modtar)'" or
1004         dodie "failed to tar modules";
1005
1006     run_ssh "rm -f /tmp/$modtar";
1007
1008     return if (!defined($post_install));
1009
1010     my $cp_post_install = $post_install;
1011     $cp_post_install =~ s/\$KERNEL_VERSION/$version/g;
1012     run_command "$cp_post_install" or
1013         dodie "Failed to run post install";
1014 }
1015
1016 sub check_buildlog {
1017     my ($patch) = @_;
1018
1019     my @files = `git show $patch | diffstat -l`;
1020
1021     open(IN, "git show $patch |") or
1022         dodie "failed to show $patch";
1023     while (<IN>) {
1024         if (m,^--- a/(.*),) {
1025             chomp $1;
1026             $files[$#files] = $1;
1027         }
1028     }
1029     close(IN);
1030
1031     open(IN, $buildlog) or dodie "Can't open $buildlog";
1032     while (<IN>) {
1033         if (/^\s*(.*?):.*(warning|error)/) {
1034             my $err = $1;
1035             foreach my $file (@files) {
1036                 my $fullpath = "$builddir/$file";
1037                 if ($file eq $err || $fullpath eq $err) {
1038                     fail "$file built with warnings" and return 0;
1039                 }
1040             }
1041         }
1042     }
1043     close(IN);
1044
1045     return 1;
1046 }
1047
1048 sub apply_min_config {
1049     my $outconfig = "$output_config.new";
1050
1051     # Read the config file and remove anything that
1052     # is in the force_config hash (from minconfig and others)
1053     # then add the force config back.
1054
1055     doprint "Applying minimum configurations into $output_config.new\n";
1056
1057     open (OUT, ">$outconfig") or
1058         dodie "Can't create $outconfig";
1059
1060     if (-f $output_config) {
1061         open (IN, $output_config) or
1062             dodie "Failed to open $output_config";
1063         while (<IN>) {
1064             if (/^(# )?(CONFIG_[^\s=]*)/) {
1065                 next if (defined($force_config{$2}));
1066             }
1067             print OUT;
1068         }
1069         close IN;
1070     }
1071     foreach my $config (keys %force_config) {
1072         print OUT "$force_config{$config}\n";
1073     }
1074     close OUT;
1075
1076     run_command "mv $outconfig $output_config";
1077 }
1078
1079 sub make_oldconfig {
1080
1081     apply_min_config;
1082
1083     if (!run_command "$make oldnoconfig") {
1084         # Perhaps oldnoconfig doesn't exist in this version of the kernel
1085         # try a yes '' | oldconfig
1086         doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
1087         run_command "yes '' | $make oldconfig" or
1088             dodie "failed make config oldconfig";
1089     }
1090 }
1091
1092 # read a config file and use this to force new configs.
1093 sub load_force_config {
1094     my ($config) = @_;
1095
1096     open(IN, $config) or
1097         dodie "failed to read $config";
1098     while (<IN>) {
1099         chomp;
1100         if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1101             $force_config{$1} = $_;
1102         } elsif (/^# (CONFIG_\S*) is not set/) {
1103             $force_config{$1} = $_;
1104         }
1105     }
1106     close IN;
1107 }
1108
1109 sub build {
1110     my ($type) = @_;
1111
1112     unlink $buildlog;
1113
1114     if ($type =~ /^useconfig:(.*)/) {
1115         run_command "cp $1 $output_config" or
1116             dodie "could not copy $1 to .config";
1117
1118         $type = "oldconfig";
1119     }
1120
1121     # old config can ask questions
1122     if ($type eq "oldconfig") {
1123         $type = "oldnoconfig";
1124
1125         # allow for empty configs
1126         run_command "touch $output_config";
1127
1128         run_command "mv $output_config $outputdir/config_temp" or
1129             dodie "moving .config";
1130
1131         if (!$noclean && !run_command "$make mrproper") {
1132             dodie "make mrproper";
1133         }
1134
1135         run_command "mv $outputdir/config_temp $output_config" or
1136             dodie "moving config_temp";
1137
1138     } elsif (!$noclean) {
1139         unlink "$output_config";
1140         run_command "$make mrproper" or
1141             dodie "make mrproper";
1142     }
1143
1144     # add something to distinguish this build
1145     open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1146     print OUT "$localversion\n";
1147     close(OUT);
1148
1149     if (defined($minconfig)) {
1150         load_force_config($minconfig);
1151     }
1152
1153     if ($type ne "oldnoconfig") {
1154         run_command "$make $type" or
1155             dodie "failed make config";
1156     }
1157     # Run old config regardless, to enforce min configurations
1158     make_oldconfig;
1159
1160     $redirect = "$buildlog";
1161     if (!run_command "$make $build_options") {
1162         undef $redirect;
1163         # bisect may need this to pass
1164         return 0 if ($in_bisect);
1165         fail "failed build" and return 0;
1166     }
1167     undef $redirect;
1168
1169     return 1;
1170 }
1171
1172 sub halt {
1173     if (!run_ssh "halt" or defined($power_off)) {
1174         if (defined($poweroff_after_halt)) {
1175             sleep $poweroff_after_halt;
1176             run_command "$power_off";
1177         }
1178     } else {
1179         # nope? the zap it!
1180         run_command "$power_off";
1181     }
1182 }
1183
1184 sub success {
1185     my ($i) = @_;
1186
1187     $successes++;
1188
1189     my $name = "";
1190
1191     if (defined($test_name)) {
1192         $name = " ($test_name)";
1193     }
1194
1195     doprint "\n\n*******************************************\n";
1196     doprint     "*******************************************\n";
1197     doprint     "KTEST RESULT: TEST $i$name SUCCESS!!!!         **\n";
1198     doprint     "*******************************************\n";
1199     doprint     "*******************************************\n";
1200
1201     if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
1202         doprint "Reboot and wait $sleep_time seconds\n";
1203         reboot;
1204         start_monitor;
1205         wait_for_monitor $sleep_time;
1206         end_monitor;
1207     }
1208 }
1209
1210 sub get_version {
1211     # get the release name
1212     doprint "$make kernelrelease ... ";
1213     $version = `$make kernelrelease | tail -1`;
1214     chomp($version);
1215     doprint "$version\n";
1216 }
1217
1218 sub answer_bisect {
1219     for (;;) {
1220         doprint "Pass or fail? [p/f]";
1221         my $ans = <STDIN>;
1222         chomp $ans;
1223         if ($ans eq "p" || $ans eq "P") {
1224             return 1;
1225         } elsif ($ans eq "f" || $ans eq "F") {
1226             return 0;
1227         } else {
1228             print "Please answer 'P' or 'F'\n";
1229         }
1230     }
1231 }
1232
1233 sub child_run_test {
1234     my $failed = 0;
1235
1236     # child should have no power
1237     $reboot_on_error = 0;
1238     $poweroff_on_error = 0;
1239     $die_on_failure = 1;
1240
1241     run_command $run_test or $failed = 1;
1242     exit $failed;
1243 }
1244
1245 my $child_done;
1246
1247 sub child_finished {
1248     $child_done = 1;
1249 }
1250
1251 sub do_run_test {
1252     my $child_pid;
1253     my $child_exit;
1254     my $line;
1255     my $full_line;
1256     my $bug = 0;
1257
1258     wait_for_monitor 1;
1259
1260     doprint "run test $run_test\n";
1261
1262     $child_done = 0;
1263
1264     $SIG{CHLD} = qw(child_finished);
1265
1266     $child_pid = fork;
1267
1268     child_run_test if (!$child_pid);
1269
1270     $full_line = "";
1271
1272     do {
1273         $line = wait_for_input($monitor_fp, 1);
1274         if (defined($line)) {
1275
1276             # we are not guaranteed to get a full line
1277             $full_line .= $line;
1278             doprint $line;
1279
1280             if ($full_line =~ /call trace:/i) {
1281                 $bug = 1;
1282             }
1283
1284             if ($full_line =~ /Kernel panic -/) {
1285                 $bug = 1;
1286             }
1287
1288             if ($line =~ /\n/) {
1289                 $full_line = "";
1290             }
1291         }
1292     } while (!$child_done && !$bug);
1293
1294     if ($bug) {
1295         my $failure_start = time;
1296         my $now;
1297         do {
1298             $line = wait_for_input($monitor_fp, 1);
1299             if (defined($line)) {
1300                 doprint $line;
1301             }
1302             $now = time;
1303             if ($now - $failure_start >= $stop_after_failure) {
1304                 last;
1305             }
1306         } while (defined($line));
1307
1308         doprint "Detected kernel crash!\n";
1309         # kill the child with extreme prejudice
1310         kill 9, $child_pid;
1311     }
1312
1313     waitpid $child_pid, 0;
1314     $child_exit = $?;
1315
1316     if ($bug || $child_exit) {
1317         return 0 if $in_bisect;
1318         fail "test failed" and return 0;
1319     }
1320     return 1;
1321 }
1322
1323 sub run_git_bisect {
1324     my ($command) = @_;
1325
1326     doprint "$command ... ";
1327
1328     my $output = `$command 2>&1`;
1329     my $ret = $?;
1330
1331     logit $output;
1332
1333     if ($ret) {
1334         doprint "FAILED\n";
1335         dodie "Failed to git bisect";
1336     }
1337
1338     doprint "SUCCESS\n";
1339     if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1340         doprint "$1 [$2]\n";
1341     } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
1342         $bisect_bad = $1;
1343         doprint "Found bad commit... $1\n";
1344         return 0;
1345     } else {
1346         # we already logged it, just print it now.
1347         print $output;
1348     }
1349
1350     return 1;
1351 }
1352
1353 sub bisect_reboot {
1354     doprint "Reboot and sleep $bisect_sleep_time seconds\n";
1355     reboot;
1356     start_monitor;
1357     wait_for_monitor $bisect_sleep_time;
1358     end_monitor;
1359 }
1360
1361 # returns 1 on success, 0 on failure, -1 on skip
1362 sub run_bisect_test {
1363     my ($type, $buildtype) = @_;
1364
1365     my $failed = 0;
1366     my $result;
1367     my $output;
1368     my $ret;
1369
1370     $in_bisect = 1;
1371
1372     build $buildtype or $failed = 1;
1373
1374     if ($type ne "build") {
1375         if ($failed && $bisect_skip) {
1376             $in_bisect = 0;
1377             return -1;
1378         }
1379         dodie "Failed on build" if $failed;
1380
1381         # Now boot the box
1382         get_grub_index;
1383         get_version;
1384         install;
1385
1386         start_monitor;
1387         monitor or $failed = 1;
1388
1389         if ($type ne "boot") {
1390             if ($failed && $bisect_skip) {
1391                 end_monitor;
1392                 bisect_reboot;
1393                 $in_bisect = 0;
1394                 return -1;
1395             }
1396             dodie "Failed on boot" if $failed;
1397
1398             do_run_test or $failed = 1;
1399         }
1400         end_monitor;
1401     }
1402
1403     if ($failed) {
1404         $result = 0;
1405     } else {
1406         $result = 1;
1407     }
1408
1409     # reboot the box to a kernel we can ssh to
1410     if ($type ne "build") {
1411         bisect_reboot;
1412     }
1413     $in_bisect = 0;
1414
1415     return $result;
1416 }
1417
1418 sub run_bisect {
1419     my ($type) = @_;
1420     my $buildtype = "oldconfig";
1421
1422     # We should have a minconfig to use?
1423     if (defined($minconfig)) {
1424         $buildtype = "useconfig:$minconfig";
1425     }
1426
1427     my $ret = run_bisect_test $type, $buildtype;
1428
1429     if ($bisect_manual) {
1430         $ret = answer_bisect;
1431     }
1432
1433     # Are we looking for where it worked, not failed?
1434     if ($reverse_bisect) {
1435         $ret = !$ret;
1436     }
1437
1438     if ($ret > 0) {
1439         return "good";
1440     } elsif ($ret == 0) {
1441         return  "bad";
1442     } elsif ($bisect_skip) {
1443         doprint "HIT A BAD COMMIT ... SKIPPING\n";
1444         return "skip";
1445     }
1446 }
1447
1448 sub bisect {
1449     my ($i) = @_;
1450
1451     my $result;
1452
1453     die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
1454     die "BISECT_BAD[$i] not defined\n"  if (!defined($opt{"BISECT_BAD[$i]"}));
1455     die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
1456
1457     my $good = $opt{"BISECT_GOOD[$i]"};
1458     my $bad = $opt{"BISECT_BAD[$i]"};
1459     my $type = $opt{"BISECT_TYPE[$i]"};
1460     my $start = $opt{"BISECT_START[$i]"};
1461     my $replay = $opt{"BISECT_REPLAY[$i]"};
1462     my $start_files = $opt{"BISECT_FILES[$i]"};
1463
1464     if (defined($start_files)) {
1465         $start_files = " -- " . $start_files;
1466     } else {
1467         $start_files = "";
1468     }
1469
1470     # convert to true sha1's
1471     $good = get_sha1($good);
1472     $bad = get_sha1($bad);
1473
1474     if (defined($opt{"BISECT_REVERSE[$i]"}) &&
1475         $opt{"BISECT_REVERSE[$i]"} == 1) {
1476         doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
1477         $reverse_bisect = 1;
1478     } else {
1479         $reverse_bisect = 0;
1480     }
1481
1482     # Can't have a test without having a test to run
1483     if ($type eq "test" && !defined($run_test)) {
1484         $type = "boot";
1485     }
1486
1487     my $check = $opt{"BISECT_CHECK[$i]"};
1488     if (defined($check) && $check ne "0") {
1489
1490         # get current HEAD
1491         my $head = get_sha1("HEAD");
1492
1493         if ($check ne "good") {
1494             doprint "TESTING BISECT BAD [$bad]\n";
1495             run_command "git checkout $bad" or
1496                 die "Failed to checkout $bad";
1497
1498             $result = run_bisect $type;
1499
1500             if ($result ne "bad") {
1501                 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
1502             }
1503         }
1504
1505         if ($check ne "bad") {
1506             doprint "TESTING BISECT GOOD [$good]\n";
1507             run_command "git checkout $good" or
1508                 die "Failed to checkout $good";
1509
1510             $result = run_bisect $type;
1511
1512             if ($result ne "good") {
1513                 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
1514             }
1515         }
1516
1517         # checkout where we started
1518         run_command "git checkout $head" or
1519             die "Failed to checkout $head";
1520     }
1521
1522     run_command "git bisect start$start_files" or
1523         dodie "could not start bisect";
1524
1525     run_command "git bisect good $good" or
1526         dodie "could not set bisect good to $good";
1527
1528     run_git_bisect "git bisect bad $bad" or
1529         dodie "could not set bisect bad to $bad";
1530
1531     if (defined($replay)) {
1532         run_command "git bisect replay $replay" or
1533             dodie "failed to run replay";
1534     }
1535
1536     if (defined($start)) {
1537         run_command "git checkout $start" or
1538             dodie "failed to checkout $start";
1539     }
1540
1541     my $test;
1542     do {
1543         $result = run_bisect $type;
1544         $test = run_git_bisect "git bisect $result";
1545     } while ($test);
1546
1547     run_command "git bisect log" or
1548         dodie "could not capture git bisect log";
1549
1550     run_command "git bisect reset" or
1551         dodie "could not reset git bisect";
1552
1553     doprint "Bad commit was [$bisect_bad]\n";
1554
1555     success $i;
1556 }
1557
1558 my %config_ignore;
1559 my %config_set;
1560
1561 my %config_list;
1562 my %null_config;
1563
1564 my %dependency;
1565
1566 sub process_config_ignore {
1567     my ($config) = @_;
1568
1569     open (IN, $config)
1570         or dodie "Failed to read $config";
1571
1572     while (<IN>) {
1573         if (/^((CONFIG\S*)=.*)/) {
1574             $config_ignore{$2} = $1;
1575         }
1576     }
1577
1578     close(IN);
1579 }
1580
1581 sub read_current_config {
1582     my ($config_ref) = @_;
1583
1584     %{$config_ref} = ();
1585     undef %{$config_ref};
1586
1587     my @key = keys %{$config_ref};
1588     if ($#key >= 0) {
1589         print "did not delete!\n";
1590         exit;
1591     }
1592     open (IN, "$output_config");
1593
1594     while (<IN>) {
1595         if (/^(CONFIG\S+)=(.*)/) {
1596             ${$config_ref}{$1} = $2;
1597         }
1598     }
1599     close(IN);
1600 }
1601
1602 sub get_dependencies {
1603     my ($config) = @_;
1604
1605     my $arr = $dependency{$config};
1606     if (!defined($arr)) {
1607         return ();
1608     }
1609
1610     my @deps = @{$arr};
1611
1612     foreach my $dep (@{$arr}) {
1613         print "ADD DEP $dep\n";
1614         @deps = (@deps, get_dependencies $dep);
1615     }
1616
1617     return @deps;
1618 }
1619
1620 sub create_config {
1621     my @configs = @_;
1622
1623     open(OUT, ">$output_config") or dodie "Can not write to $output_config";
1624
1625     foreach my $config (@configs) {
1626         print OUT "$config_set{$config}\n";
1627         my @deps = get_dependencies $config;
1628         foreach my $dep (@deps) {
1629             print OUT "$config_set{$dep}\n";
1630         }
1631     }
1632
1633     foreach my $config (keys %config_ignore) {
1634         print OUT "$config_ignore{$config}\n";
1635     }
1636     close(OUT);
1637
1638 #    exit;
1639     make_oldconfig;
1640 }
1641
1642 sub compare_configs {
1643     my (%a, %b) = @_;
1644
1645     foreach my $item (keys %a) {
1646         if (!defined($b{$item})) {
1647             print "diff $item\n";
1648             return 1;
1649         }
1650         delete $b{$item};
1651     }
1652
1653     my @keys = keys %b;
1654     if ($#keys) {
1655         print "diff2 $keys[0]\n";
1656     }
1657     return -1 if ($#keys >= 0);
1658
1659     return 0;
1660 }
1661
1662 sub run_config_bisect_test {
1663     my ($type) = @_;
1664
1665     return run_bisect_test $type, "oldconfig";
1666 }
1667
1668 sub process_passed {
1669     my (%configs) = @_;
1670
1671     doprint "These configs had no failure: (Enabling them for further compiles)\n";
1672     # Passed! All these configs are part of a good compile.
1673     # Add them to the min options.
1674     foreach my $config (keys %configs) {
1675         if (defined($config_list{$config})) {
1676             doprint " removing $config\n";
1677             $config_ignore{$config} = $config_list{$config};
1678             delete $config_list{$config};
1679         }
1680     }
1681     doprint "config copied to $outputdir/config_good\n";
1682     run_command "cp -f $output_config $outputdir/config_good";
1683 }
1684
1685 sub process_failed {
1686     my ($config) = @_;
1687
1688     doprint "\n\n***************************************\n";
1689     doprint "Found bad config: $config\n";
1690     doprint "***************************************\n\n";
1691 }
1692
1693 sub run_config_bisect {
1694
1695     my @start_list = keys %config_list;
1696
1697     if ($#start_list < 0) {
1698         doprint "No more configs to test!!!\n";
1699         return -1;
1700     }
1701
1702     doprint "***** RUN TEST ***\n";
1703     my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
1704     my $ret;
1705     my %current_config;
1706
1707     my $count = $#start_list + 1;
1708     doprint "  $count configs to test\n";
1709
1710     my $half = int($#start_list / 2);
1711
1712     do {
1713         my @tophalf = @start_list[0 .. $half];
1714
1715         create_config @tophalf;
1716         read_current_config \%current_config;
1717
1718         $count = $#tophalf + 1;
1719         doprint "Testing $count configs\n";
1720         my $found = 0;
1721         # make sure we test something
1722         foreach my $config (@tophalf) {
1723             if (defined($current_config{$config})) {
1724                 logit " $config\n";
1725                 $found = 1;
1726             }
1727         }
1728         if (!$found) {
1729             # try the other half
1730             doprint "Top half produced no set configs, trying bottom half\n";
1731             @tophalf = @start_list[$half + 1 .. $#start_list];
1732             create_config @tophalf;
1733             read_current_config \%current_config;
1734             foreach my $config (@tophalf) {
1735                 if (defined($current_config{$config})) {
1736                     logit " $config\n";
1737                     $found = 1;
1738                 }
1739             }
1740             if (!$found) {
1741                 doprint "Failed: Can't make new config with current configs\n";
1742                 foreach my $config (@start_list) {
1743                     doprint "  CONFIG: $config\n";
1744                 }
1745                 return -1;
1746             }
1747             $count = $#tophalf + 1;
1748             doprint "Testing $count configs\n";
1749         }
1750
1751         $ret = run_config_bisect_test $type;
1752         if ($bisect_manual) {
1753             $ret = answer_bisect;
1754         }
1755         if ($ret) {
1756             process_passed %current_config;
1757             return 0;
1758         }
1759
1760         doprint "This config had a failure.\n";
1761         doprint "Removing these configs that were not set in this config:\n";
1762         doprint "config copied to $outputdir/config_bad\n";
1763         run_command "cp -f $output_config $outputdir/config_bad";
1764
1765         # A config exists in this group that was bad.
1766         foreach my $config (keys %config_list) {
1767             if (!defined($current_config{$config})) {
1768                 doprint " removing $config\n";
1769                 delete $config_list{$config};
1770             }
1771         }
1772
1773         @start_list = @tophalf;
1774
1775         if ($#start_list == 0) {
1776             process_failed $start_list[0];
1777             return 1;
1778         }
1779
1780         # remove half the configs we are looking at and see if
1781         # they are good.
1782         $half = int($#start_list / 2);
1783     } while ($#start_list > 0);
1784
1785     # we found a single config, try it again unless we are running manually
1786
1787     if ($bisect_manual) {
1788         process_failed $start_list[0];
1789         return 1;
1790     }
1791
1792     my @tophalf = @start_list[0 .. 0];
1793
1794     $ret = run_config_bisect_test $type;
1795     if ($ret) {
1796         process_passed %current_config;
1797         return 0;
1798     }
1799
1800     process_failed $start_list[0];
1801     return 1;
1802 }
1803
1804 sub config_bisect {
1805     my ($i) = @_;
1806
1807     my $start_config = $opt{"CONFIG_BISECT[$i]"};
1808
1809     my $tmpconfig = "$tmpdir/use_config";
1810
1811     if (defined($config_bisect_good)) {
1812         process_config_ignore $config_bisect_good;
1813     }
1814
1815     # Make the file with the bad config and the min config
1816     if (defined($minconfig)) {
1817         # read the min config for things to ignore
1818         run_command "cp $minconfig $tmpconfig" or
1819             dodie "failed to copy $minconfig to $tmpconfig";
1820     } else {
1821         unlink $tmpconfig;
1822     }
1823
1824     # Add other configs
1825     if (defined($addconfig)) {
1826         run_command "cat $addconfig >> $tmpconfig" or
1827             dodie "failed to append $addconfig";
1828     }
1829
1830     if (-f $tmpconfig) {
1831         load_force_config($tmpconfig);
1832         process_config_ignore $tmpconfig;
1833     }
1834
1835     # now process the start config
1836     run_command "cp $start_config $output_config" or
1837         dodie "failed to copy $start_config to $output_config";
1838
1839     # read directly what we want to check
1840     my %config_check;
1841     open (IN, $output_config)
1842         or dodie "faied to open $output_config";
1843
1844     while (<IN>) {
1845         if (/^((CONFIG\S*)=.*)/) {
1846             $config_check{$2} = $1;
1847         }
1848     }
1849     close(IN);
1850
1851     # Now run oldconfig with the minconfig (and addconfigs)
1852     make_oldconfig;
1853
1854     # check to see what we lost (or gained)
1855     open (IN, $output_config)
1856         or dodie "Failed to read $start_config";
1857
1858     my %removed_configs;
1859     my %added_configs;
1860
1861     while (<IN>) {
1862         if (/^((CONFIG\S*)=.*)/) {
1863             # save off all options
1864             $config_set{$2} = $1;
1865             if (defined($config_check{$2})) {
1866                 if (defined($config_ignore{$2})) {
1867                     $removed_configs{$2} = $1;
1868                 } else {
1869                     $config_list{$2} = $1;
1870                 }
1871             } elsif (!defined($config_ignore{$2})) {
1872                 $added_configs{$2} = $1;
1873                 $config_list{$2} = $1;
1874             }
1875         }
1876     }
1877     close(IN);
1878
1879     my @confs = keys %removed_configs;
1880     if ($#confs >= 0) {
1881         doprint "Configs overridden by default configs and removed from check:\n";
1882         foreach my $config (@confs) {
1883             doprint " $config\n";
1884         }
1885     }
1886     @confs = keys %added_configs;
1887     if ($#confs >= 0) {
1888         doprint "Configs appearing in make oldconfig and added:\n";
1889         foreach my $config (@confs) {
1890             doprint " $config\n";
1891         }
1892     }
1893
1894     my %config_test;
1895     my $once = 0;
1896
1897     # Sometimes kconfig does weird things. We must make sure
1898     # that the config we autocreate has everything we need
1899     # to test, otherwise we may miss testing configs, or
1900     # may not be able to create a new config.
1901     # Here we create a config with everything set.
1902     create_config (keys %config_list);
1903     read_current_config \%config_test;
1904     foreach my $config (keys %config_list) {
1905         if (!defined($config_test{$config})) {
1906             if (!$once) {
1907                 $once = 1;
1908                 doprint "Configs not produced by kconfig (will not be checked):\n";
1909             }
1910             doprint "  $config\n";
1911             delete $config_list{$config};
1912         }
1913     }
1914     my $ret;
1915     do {
1916         $ret = run_config_bisect;
1917     } while (!$ret);
1918
1919     return $ret if ($ret < 0);
1920
1921     success $i;
1922 }
1923
1924 sub patchcheck_reboot {
1925     doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
1926     reboot;
1927     start_monitor;
1928     wait_for_monitor $patchcheck_sleep_time;
1929     end_monitor;
1930 }
1931
1932 sub patchcheck {
1933     my ($i) = @_;
1934
1935     die "PATCHCHECK_START[$i] not defined\n"
1936         if (!defined($opt{"PATCHCHECK_START[$i]"}));
1937     die "PATCHCHECK_TYPE[$i] not defined\n"
1938         if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
1939
1940     my $start = $opt{"PATCHCHECK_START[$i]"};
1941
1942     my $end = "HEAD";
1943     if (defined($opt{"PATCHCHECK_END[$i]"})) {
1944         $end = $opt{"PATCHCHECK_END[$i]"};
1945     }
1946
1947     # Get the true sha1's since we can use things like HEAD~3
1948     $start = get_sha1($start);
1949     $end = get_sha1($end);
1950
1951     my $type = $opt{"PATCHCHECK_TYPE[$i]"};
1952
1953     # Can't have a test without having a test to run
1954     if ($type eq "test" && !defined($run_test)) {
1955         $type = "boot";
1956     }
1957
1958     open (IN, "git log --pretty=oneline $end|") or
1959         dodie "could not get git list";
1960
1961     my @list;
1962
1963     while (<IN>) {
1964         chomp;
1965         $list[$#list+1] = $_;
1966         last if (/^$start/);
1967     }
1968     close(IN);
1969
1970     if ($list[$#list] !~ /^$start/) {
1971         fail "SHA1 $start not found";
1972     }
1973
1974     # go backwards in the list
1975     @list = reverse @list;
1976
1977     my $save_clean = $noclean;
1978
1979     $in_patchcheck = 1;
1980     foreach my $item (@list) {
1981         my $sha1 = $item;
1982         $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
1983
1984         doprint "\nProcessing commit $item\n\n";
1985
1986         run_command "git checkout $sha1" or
1987             die "Failed to checkout $sha1";
1988
1989         # only clean on the first and last patch
1990         if ($item eq $list[0] ||
1991             $item eq $list[$#list]) {
1992             $noclean = $save_clean;
1993         } else {
1994             $noclean = 1;
1995         }
1996
1997         if (defined($minconfig)) {
1998             build "useconfig:$minconfig" or return 0;
1999         } else {
2000             # ?? no config to use?
2001             build "oldconfig" or return 0;
2002         }
2003
2004         check_buildlog $sha1 or return 0;
2005
2006         next if ($type eq "build");
2007
2008         get_grub_index;
2009         get_version;
2010         install;
2011
2012         my $failed = 0;
2013
2014         start_monitor;
2015         monitor or $failed = 1;
2016
2017         if (!$failed && $type ne "boot"){
2018             do_run_test or $failed = 1;
2019         }
2020         end_monitor;
2021         return 0 if ($failed);
2022
2023         patchcheck_reboot;
2024
2025     }
2026     $in_patchcheck = 0;
2027     success $i;
2028
2029     return 1;
2030 }
2031
2032 $#ARGV < 1 or die "ktest.pl version: $VERSION\n   usage: ktest.pl config-file\n";
2033
2034 if ($#ARGV == 0) {
2035     $ktest_config = $ARGV[0];
2036     if (! -f $ktest_config) {
2037         print "$ktest_config does not exist.\n";
2038         my $ans;
2039         for (;;) {
2040             print "Create it? [Y/n] ";
2041             $ans = <STDIN>;
2042             chomp $ans;
2043             if ($ans =~ /^\s*$/) {
2044                 $ans = "y";
2045             }
2046             last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
2047             print "Please answer either 'y' or 'n'.\n";
2048         }
2049         if ($ans !~ /^y$/i) {
2050             exit 0;
2051         }
2052     }
2053 } else {
2054     $ktest_config = "ktest.conf";
2055 }
2056
2057 if (! -f $ktest_config) {
2058     open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
2059     print OUT << "EOF"
2060 # Generated by ktest.pl
2061 #
2062 # Define each test with TEST_START
2063 # The config options below it will override the defaults
2064 TEST_START
2065
2066 DEFAULTS
2067 EOF
2068 ;
2069     close(OUT);
2070 }
2071 read_config $ktest_config;
2072
2073 # Append any configs entered in manually to the config file.
2074 my @new_configs = keys %entered_configs;
2075 if ($#new_configs >= 0) {
2076     print "\nAppending entered in configs to $ktest_config\n";
2077     open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
2078     foreach my $config (@new_configs) {
2079         print OUT "$config = $entered_configs{$config}\n";
2080         $opt{$config} = $entered_configs{$config};
2081     }
2082 }
2083
2084 if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
2085     unlink $opt{"LOG_FILE"};
2086 }
2087
2088 doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
2089
2090 for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
2091
2092     if (!$i) {
2093         doprint "DEFAULT OPTIONS:\n";
2094     } else {
2095         doprint "\nTEST $i OPTIONS";
2096         if (defined($repeat_tests{$i})) {
2097             $repeat = $repeat_tests{$i};
2098             doprint " ITERATE $repeat";
2099         }
2100         doprint "\n";
2101     }
2102
2103     foreach my $option (sort keys %opt) {
2104
2105         if ($option =~ /\[(\d+)\]$/) {
2106             next if ($i != $1);
2107         } else {
2108             next if ($i);
2109         }
2110
2111         doprint "$option = $opt{$option}\n";
2112     }
2113 }
2114
2115 sub __set_test_option {
2116     my ($name, $i) = @_;
2117
2118     my $option = "$name\[$i\]";
2119
2120     if (defined($opt{$option})) {
2121         return $opt{$option};
2122     }
2123
2124     foreach my $test (keys %repeat_tests) {
2125         if ($i >= $test &&
2126             $i < $test + $repeat_tests{$test}) {
2127             $option = "$name\[$test\]";
2128             if (defined($opt{$option})) {
2129                 return $opt{$option};
2130             }
2131         }
2132     }
2133
2134     if (defined($opt{$name})) {
2135         return $opt{$name};
2136     }
2137
2138     return undef;
2139 }
2140
2141 sub eval_option {
2142     my ($option, $i) = @_;
2143
2144     # Add space to evaluate the character before $
2145     $option = " $option";
2146     my $retval = "";
2147
2148     while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
2149         my $start = $1;
2150         my $var = $2;
2151         my $end = $3;
2152
2153         # Append beginning of line
2154         $retval = "$retval$start";
2155
2156         # If the iteration option OPT[$i] exists, then use that.
2157         # otherwise see if the default OPT (without [$i]) exists.
2158
2159         my $o = "$var\[$i\]";
2160
2161         if (defined($opt{$o})) {
2162             $o = $opt{$o};
2163             $retval = "$retval$o";
2164         } elsif (defined($opt{$var})) {
2165             $o = $opt{$var};
2166             $retval = "$retval$o";
2167         } else {
2168             $retval = "$retval\$\{$var\}";
2169         }
2170
2171         $option = $end;
2172     }
2173
2174     $retval = "$retval$option";
2175
2176     $retval =~ s/^ //;
2177
2178     return $retval;
2179 }
2180
2181 sub set_test_option {
2182     my ($name, $i) = @_;
2183
2184     my $option = __set_test_option($name, $i);
2185     return $option if (!defined($option));
2186
2187     my $prev = "";
2188
2189     # Since an option can evaluate to another option,
2190     # keep iterating until we do not evaluate any more
2191     # options.
2192     my $r = 0;
2193     while ($prev ne $option) {
2194         # Check for recursive evaluations.
2195         # 100 deep should be more than enough.
2196         if ($r++ > 100) {
2197             die "Over 100 evaluations accurred with $name\n" .
2198                 "Check for recursive variables\n";
2199         }
2200         $prev = $option;
2201         $option = eval_option($option, $i);
2202     }
2203
2204     return $option;
2205 }
2206
2207 # First we need to do is the builds
2208 for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
2209
2210     $iteration = $i;
2211
2212     my $makecmd = set_test_option("MAKE_CMD", $i);
2213
2214     $machine = set_test_option("MACHINE", $i);
2215     $ssh_user = set_test_option("SSH_USER", $i);
2216     $tmpdir = set_test_option("TMP_DIR", $i);
2217     $outputdir = set_test_option("OUTPUT_DIR", $i);
2218     $builddir = set_test_option("BUILD_DIR", $i);
2219     $test_type = set_test_option("TEST_TYPE", $i);
2220     $build_type = set_test_option("BUILD_TYPE", $i);
2221     $build_options = set_test_option("BUILD_OPTIONS", $i);
2222     $power_cycle = set_test_option("POWER_CYCLE", $i);
2223     $reboot = set_test_option("REBOOT", $i);
2224     $noclean = set_test_option("BUILD_NOCLEAN", $i);
2225     $minconfig = set_test_option("MIN_CONFIG", $i);
2226     $run_test = set_test_option("TEST", $i);
2227     $addconfig = set_test_option("ADD_CONFIG", $i);
2228     $reboot_type = set_test_option("REBOOT_TYPE", $i);
2229     $grub_menu = set_test_option("GRUB_MENU", $i);
2230     $post_install = set_test_option("POST_INSTALL", $i);
2231     $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
2232     $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
2233     $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
2234     $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
2235     $power_off = set_test_option("POWER_OFF", $i);
2236     $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
2237     $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
2238     $sleep_time = set_test_option("SLEEP_TIME", $i);
2239     $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
2240     $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
2241     $bisect_manual = set_test_option("BISECT_MANUAL", $i);
2242     $bisect_skip = set_test_option("BISECT_SKIP", $i);
2243     $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
2244     $store_failures = set_test_option("STORE_FAILURES", $i);
2245     $test_name = set_test_option("TEST_NAME", $i);
2246     $timeout = set_test_option("TIMEOUT", $i);
2247     $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
2248     $console = set_test_option("CONSOLE", $i);
2249     $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
2250     $success_line = set_test_option("SUCCESS_LINE", $i);
2251     $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
2252     $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
2253     $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
2254     $build_target = set_test_option("BUILD_TARGET", $i);
2255     $ssh_exec = set_test_option("SSH_EXEC", $i);
2256     $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
2257     $target_image = set_test_option("TARGET_IMAGE", $i);
2258     $localversion = set_test_option("LOCALVERSION", $i);
2259
2260     chdir $builddir || die "can't change directory to $builddir";
2261
2262     if (!-d $tmpdir) {
2263         mkpath($tmpdir) or
2264             die "can't create $tmpdir";
2265     }
2266
2267     $ENV{"SSH_USER"} = $ssh_user;
2268     $ENV{"MACHINE"} = $machine;
2269
2270     $target = "$ssh_user\@$machine";
2271
2272     $buildlog = "$tmpdir/buildlog-$machine";
2273     $dmesg = "$tmpdir/dmesg-$machine";
2274     $make = "$makecmd O=$outputdir";
2275     $output_config = "$outputdir/.config";
2276
2277     if ($reboot_type eq "grub") {
2278         dodie "GRUB_MENU not defined" if (!defined($grub_menu));
2279     } elsif (!defined($reboot_script)) {
2280         dodie "REBOOT_SCRIPT not defined"
2281     }
2282
2283     my $run_type = $build_type;
2284     if ($test_type eq "patchcheck") {
2285         $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
2286     } elsif ($test_type eq "bisect") {
2287         $run_type = $opt{"BISECT_TYPE[$i]"};
2288     } elsif ($test_type eq "config_bisect") {
2289         $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
2290     }
2291
2292     # mistake in config file?
2293     if (!defined($run_type)) {
2294         $run_type = "ERROR";
2295     }
2296
2297     doprint "\n\n";
2298     doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type\n\n";
2299
2300     unlink $dmesg;
2301     unlink $buildlog;
2302
2303     if (!defined($minconfig)) {
2304         $minconfig = $addconfig;
2305
2306     } elsif (defined($addconfig)) {
2307         run_command "cat $addconfig $minconfig > $tmpdir/add_config" or
2308             dodie "Failed to create temp config";
2309         $minconfig = "$tmpdir/add_config";
2310     }
2311
2312     my $checkout = $opt{"CHECKOUT[$i]"};
2313     if (defined($checkout)) {
2314         run_command "git checkout $checkout" or
2315             die "failed to checkout $checkout";
2316     }
2317
2318     if ($test_type eq "bisect") {
2319         bisect $i;
2320         next;
2321     } elsif ($test_type eq "config_bisect") {
2322         config_bisect $i;
2323         next;
2324     } elsif ($test_type eq "patchcheck") {
2325         patchcheck $i;
2326         next;
2327     }
2328
2329     if ($build_type ne "nobuild") {
2330         build $build_type or next;
2331     }
2332
2333     if ($test_type ne "build") {
2334         get_grub_index;
2335         get_version;
2336         install;
2337
2338         my $failed = 0;
2339         start_monitor;
2340         monitor or $failed = 1;;
2341
2342         if (!$failed && $test_type ne "boot" && defined($run_test)) {
2343             do_run_test or $failed = 1;
2344         }
2345         end_monitor;
2346         next if ($failed);
2347     }
2348
2349     success $i;
2350 }
2351
2352 if ($opt{"POWEROFF_ON_SUCCESS"}) {
2353     halt;
2354 } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
2355     reboot;
2356 }
2357
2358 doprint "\n    $successes of $opt{NUM_TESTS} tests were successful\n\n";
2359
2360 exit 0;