3 # Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
4 # Licensed under the terms of the GNU GPL License version 2
9 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
10 use File::Path qw(mkpath);
11 use File::Copy qw(cp);
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/\${MACHINE}";
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{"NO_INSTALL"} = 0;
46 $default{"BOOTED_TIMEOUT"} = 1;
47 $default{"DIE_ON_FAILURE"} = 1;
48 $default{"SSH_EXEC"} = "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND";
49 $default{"SCP_TO_TARGET"} = "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE";
50 $default{"REBOOT"} = "ssh \$SSH_USER\@\$MACHINE reboot";
51 $default{"STOP_AFTER_SUCCESS"} = 10;
52 $default{"STOP_AFTER_FAILURE"} = 60;
53 $default{"STOP_TEST_AFTER"} = 600;
54 $default{"LOCALVERSION"} = "-test";
76 my $poweroff_on_error;
78 my $powercycle_after_reboot;
79 my $poweroff_after_halt;
92 my $start_minconfig_defined;
101 my $config_bisect_good;
102 my $in_patchcheck = 0;
111 my $bisect_sleep_time;
112 my $patchcheck_sleep_time;
118 my $detect_triplefault;
120 my $reboot_success_line;
122 my $stop_after_success;
123 my $stop_after_failure;
136 # do not force reboots on config problems
139 # default variables that can be used
140 chomp ($variable{"PWD"} = `pwd`);
142 $config_help{"MACHINE"} = << "EOF"
143 The machine hostname that you will test.
146 $config_help{"SSH_USER"} = << "EOF"
147 The box is expected to have ssh on normal bootup, provide the user
148 (most likely root, since you need privileged operations)
151 $config_help{"BUILD_DIR"} = << "EOF"
152 The directory that contains the Linux source code (full path).
155 $config_help{"OUTPUT_DIR"} = << "EOF"
156 The directory that the objects will be built (full path).
157 (can not be same as BUILD_DIR)
160 $config_help{"BUILD_TARGET"} = << "EOF"
161 The location of the compiled file to copy to the target.
162 (relative to OUTPUT_DIR)
165 $config_help{"TARGET_IMAGE"} = << "EOF"
166 The place to put your image on the test machine.
169 $config_help{"POWER_CYCLE"} = << "EOF"
170 A script or command to reboot the box.
172 Here is a digital loggers power switch example
173 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
175 Here is an example to reboot a virtual box on the current host
176 with the name "Guest".
177 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
180 $config_help{"CONSOLE"} = << "EOF"
181 The script or command that reads the console
183 If you use ttywatch server, something like the following would work.
184 CONSOLE = nc -d localhost 3001
186 For a virtual machine with guest name "Guest".
187 CONSOLE = virsh console Guest
190 $config_help{"LOCALVERSION"} = << "EOF"
191 Required version ending to differentiate the test
192 from other linux builds on the system.
195 $config_help{"REBOOT_TYPE"} = << "EOF"
196 Way to reboot the box to the test kernel.
197 Only valid options so far are "grub" and "script".
199 If you specify grub, it will assume grub version 1
200 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
201 and select that target to reboot to the kernel. If this is not
202 your setup, then specify "script" and have a command or script
203 specified in REBOOT_SCRIPT to boot to the target.
205 The entry in /boot/grub/menu.lst must be entered in manually.
206 The test will not modify that file.
209 $config_help{"GRUB_MENU"} = << "EOF"
210 The grub title name for the test kernel to boot
211 (Only mandatory if REBOOT_TYPE = grub)
213 Note, ktest.pl will not update the grub menu.lst, you need to
214 manually add an option for the test. ktest.pl will search
215 the grub menu.lst for this option to find what kernel to
218 For example, if in the /boot/grub/menu.lst the test kernel title has:
221 GRUB_MENU = Test Kernel
224 $config_help{"REBOOT_SCRIPT"} = << "EOF"
225 A script to reboot the target into the test kernel
226 (Only mandatory if REBOOT_TYPE = script)
236 print "$prompt [Y/n] ";
239 if ($ans =~ /^\s*$/) {
242 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
243 print "Please answer either 'y' or 'n'.\n";
245 if ($ans !~ /^y$/i) {
251 sub get_ktest_config {
255 return if (defined($opt{$config}));
257 if (defined($config_help{$config})) {
259 print $config_help{$config};
264 if (defined($default{$config})) {
265 print "\[$default{$config}\] ";
268 $ans =~ s/^\s*(.*\S)\s*$/$1/;
269 if ($ans =~ /^\s*$/) {
270 if ($default{$config}) {
271 $ans = $default{$config};
273 print "Your answer can not be blank\n";
277 $entered_configs{$config} = process_variables($ans);
282 sub get_ktest_configs {
283 get_ktest_config("MACHINE");
284 get_ktest_config("SSH_USER");
285 get_ktest_config("BUILD_DIR");
286 get_ktest_config("OUTPUT_DIR");
287 get_ktest_config("BUILD_TARGET");
288 get_ktest_config("TARGET_IMAGE");
289 get_ktest_config("POWER_CYCLE");
290 get_ktest_config("CONSOLE");
291 get_ktest_config("LOCALVERSION");
293 my $rtype = $opt{"REBOOT_TYPE"};
295 if (!defined($rtype)) {
296 if (!defined($opt{"GRUB_MENU"})) {
297 get_ktest_config("REBOOT_TYPE");
298 $rtype = $entered_configs{"REBOOT_TYPE"};
304 if ($rtype eq "grub") {
305 get_ktest_config("GRUB_MENU");
307 get_ktest_config("REBOOT_SCRIPT");
311 sub process_variables {
312 my ($value, $remove_undef) = @_;
315 # We want to check for '\', and it is just easier
316 # to check the previous characet of '$' and not need
317 # to worry if '$' is the first character. By adding
318 # a space to $value, we can just check [^\\]\$ and
319 # it will still work.
322 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
326 # append beginning of value to retval
327 $retval = "$retval$begin";
328 if (defined($variable{$var})) {
329 $retval = "$retval$variable{$var}";
330 } elsif (defined($remove_undef) && $remove_undef) {
331 # for if statements, any variable that is not defined,
332 # we simple convert to 0
333 $retval = "${retval}0";
335 # put back the origin piece.
336 $retval = "$retval\$\{$var\}";
340 $retval = "$retval$value";
342 # remove the space added in the beginning
349 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
351 if (defined($opt{$lvalue})) {
352 if (!$override || defined(${$overrides}{$lvalue})) {
355 $extra = "In the same override section!\n";
357 die "$name: $.: Option $lvalue defined more than once!\n$extra";
359 ${$overrides}{$lvalue} = $rvalue;
361 if ($rvalue =~ /^\s*$/) {
362 delete $opt{$lvalue};
364 $rvalue = process_variables($rvalue);
365 $opt{$lvalue} = $rvalue;
370 my ($lvalue, $rvalue) = @_;
372 if ($rvalue =~ /^\s*$/) {
373 delete $variable{$lvalue};
375 $rvalue = process_variables($rvalue);
376 $variable{$lvalue} = $rvalue;
380 sub process_compare {
381 my ($lval, $cmp, $rval) = @_;
392 return $lval eq $rval;
393 } elsif ($cmp eq "!=") {
394 return $lval ne $rval;
397 my $statement = "$lval $cmp $rval";
398 my $ret = eval $statement;
400 # $@ stores error of eval
411 return defined($variable{$2}) ||
416 sub process_expression {
417 my ($name, $val) = @_;
421 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
424 if (process_expression($name, $express)) {
425 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
427 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
435 while ($val =~ s/^(.*?)($OR|$AND)//) {
439 if (process_expression($name, $express)) {
450 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
451 my $ret = process_compare($1, $2, $3);
453 die "$name: $.: Unable to process comparison\n";
458 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
460 return !value_defined($2);
462 return value_defined($2);
466 if ($val =~ /^\s*0\s*$/) {
468 } elsif ($val =~ /^\s*\d+\s*$/) {
472 die ("$name: $.: Undefined content $val in if statement\n");
476 my ($name, $value) = @_;
478 # Convert variables and replace undefined ones with 0
479 my $val = process_variables($value, 1);
480 my $ret = process_expression $name, $val;
486 my ($config, $current_test_num) = @_;
489 open($in, $config) || die "can't read file $config";
492 $name =~ s,.*/(.*),$1,;
494 my $test_num = $$current_test_num;
497 my $num_tests_set = 0;
510 # ignore blank lines and comments
511 next if (/^\s*$/ || /\s*\#/);
513 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
523 if ($type eq "TEST_START") {
525 if ($num_tests_set) {
526 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
529 $old_test_num = $test_num;
530 $old_repeat = $repeat;
532 $test_num += $repeat;
539 # If SKIP is anywhere in the line, the command will be skipped
540 if ($rest =~ s/\s+SKIP\b//) {
547 if ($rest =~ s/\sELSE\b//) {
549 die "$name: $.: ELSE found with out matching IF section\n$_";
560 if ($rest =~ s/\sIF\s+(.*)//) {
561 if (process_if($name, $1)) {
573 if ($type eq "TEST_START") {
574 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
576 $repeat_tests{"$test_num"} = $repeat;
578 } elsif ($rest =~ s/\sOVERRIDE\b//) {
581 # Clear previous overrides
586 if (!$skip && $rest !~ /^\s*$/) {
587 die "$name: $.: Gargbage found after $type\n$_";
590 if ($skip && $type eq "TEST_START") {
591 $test_num = $old_test_num;
592 $repeat = $old_repeat;
595 } elsif (/^\s*ELSE\b(.*)$/) {
597 die "$name: $.: ELSE found with out matching IF section\n$_";
606 if ($rest =~ /\sIF\s+(.*)/) {
607 # May be a ELSE IF section.
608 if (!process_if($name, $1)) {
617 if ($rest !~ /^\s*$/) {
618 die "$name: $.: Gargbage found after DEFAULTS\n$_";
621 } elsif (/^\s*INCLUDE\s+(\S+)/) {
626 die "$name: $.: INCLUDE can only be done in default sections\n$_";
629 my $file = process_variables($1);
631 if ($file !~ m,^/,) {
632 # check the path of the config file first
633 if ($config =~ m,(.*)/,) {
641 die "$name: $.: Can't read file $file\n$_";
644 if (__read_config($file, \$test_num)) {
648 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
656 ($lvalue eq "NUM_TESTS" ||
657 $lvalue eq "LOG_FILE" ||
658 $lvalue eq "CLEAR_LOG")) {
659 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
662 if ($lvalue eq "NUM_TESTS") {
664 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
667 die "$name: $.: NUM_TESTS must be set in default section\n";
672 if ($default || $lvalue =~ /\[\d+\]$/) {
673 set_value($lvalue, $rvalue, $override, \%overrides, $name);
675 my $val = "$lvalue\[$test_num\]";
676 set_value($val, $rvalue, $override, \%overrides, $name);
679 $repeats{$val} = $repeat;
682 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
688 # process config variables.
689 # Config variables are only active while reading the
690 # config and can be defined anywhere. They also ignore
691 # TEST_START and DEFAULTS, but are skipped if they are in
692 # on of these sections that have SKIP defined.
693 # The save variable can be
694 # defined multiple times and the new one simply overrides
696 set_variable($lvalue, $rvalue);
699 die "$name: $.: Garbage found in config\n$_";
704 $test_num += $repeat - 1;
705 $opt{"NUM_TESTS"} = $test_num;
710 $$current_test_num = $test_num;
721 $test_case = __read_config $config, \$test_num;
723 # make sure we have all mandatory configs
726 # was a test specified?
728 print "No test case specified.\n";
729 print "What test case would you like to run?\n";
732 $default{"TEST_TYPE"} = $ans;
737 foreach my $default (keys %default) {
738 if (!defined($opt{$default})) {
739 $opt{$default} = $default{$default};
745 my ($option, $i) = @_;
747 # Add space to evaluate the character before $
748 $option = " $option";
751 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
756 # Append beginning of line
757 $retval = "$retval$start";
759 # If the iteration option OPT[$i] exists, then use that.
760 # otherwise see if the default OPT (without [$i]) exists.
762 my $o = "$var\[$i\]";
764 if (defined($opt{$o})) {
766 $retval = "$retval$o";
767 } elsif (defined($opt{$var})) {
769 $retval = "$retval$o";
771 $retval = "$retval\$\{$var\}";
777 $retval = "$retval$option";
785 my ($option, $i) = @_;
789 # Since an option can evaluate to another option,
790 # keep iterating until we do not evaluate any more
793 while ($prev ne $option) {
794 # Check for recursive evaluations.
795 # 100 deep should be more than enough.
797 die "Over 100 evaluations accurred with $option\n" .
798 "Check for recursive variables\n";
801 $option = __eval_option($option, $i);
808 if (defined($opt{"LOG_FILE"})) {
809 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
816 if (defined($opt{"LOG_FILE"})) {
831 sub wait_for_monitor;
836 if (defined($time)) {
838 # flush out current monitor
839 # May contain the reboot success line
843 # try to reboot normally
844 if (run_command $reboot) {
845 if (defined($powercycle_after_reboot)) {
846 sleep $powercycle_after_reboot;
847 run_command "$power_cycle";
850 # nope? power cycle it.
851 run_command "$power_cycle";
854 if (defined($time)) {
855 wait_for_monitor($time, $reboot_success_line);
863 return $test_type eq "build" || $no_reboot ||
864 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
865 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
869 doprint "CRITICAL FAILURE... ", @_, "\n";
873 if ($reboot_on_error && !do_not_reboot) {
875 doprint "REBOOTING\n";
878 } elsif ($poweroff_on_error && defined($power_off)) {
879 doprint "POWERING OFF\n";
883 if (defined($opt{"LOG_FILE"})) {
884 print " See $opt{LOG_FILE} for more info.\n";
895 my $pid = open($fp, "$console|") or
896 dodie "Can't open console $console";
898 $flags = fcntl($fp, F_GETFL, 0) or
899 dodie "Can't get flags for the socket: $!";
900 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
901 dodie "Can't set flags for the socket: $!";
909 doprint "kill child process $pid\n";
917 if ($monitor_cnt++) {
920 $monitor_fp = \*MONFD;
921 $monitor_pid = open_console $monitor_fp;
925 open(MONFD, "Stop perl from warning about single use of MONFD");
929 if (--$monitor_cnt) {
932 close_console($monitor_fp, $monitor_pid);
935 sub wait_for_monitor {
936 my ($time, $stop) = @_;
941 doprint "** Wait for monitor to settle down **\n";
943 # read the monitor and wait for the system to calm down
945 $line = wait_for_input($monitor_fp, $time);
946 last if (!defined($line));
950 if (defined($stop) && $full_line =~ /$stop/) {
951 doprint "wait for monitor detected $stop\n";
959 print "** Monitor flushed **\n";
964 if ($die_on_failure) {
972 # no need to reboot for just building.
973 if (!do_not_reboot) {
974 doprint "REBOOTING\n";
980 if (defined($test_name)) {
981 $name = " ($test_name)";
984 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
985 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
986 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
987 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
988 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
990 return 1 if (!defined($store_failures));
993 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
994 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
996 my $type = $build_type;
997 if ($type =~ /useconfig/) {
1001 my $dir = "$machine-$test_type-$type-fail-$date";
1002 my $faildir = "$store_failures/$dir";
1006 die "can't create $faildir";
1008 if (-f "$output_config") {
1009 cp "$output_config", "$faildir/config" or
1010 die "failed to copy .config";
1013 cp $buildlog, "$faildir/buildlog" or
1014 die "failed to move $buildlog";
1017 cp $dmesg, "$faildir/dmesg" or
1018 die "failed to move $dmesg";
1021 doprint "*** Saved info to $faildir ***\n";
1032 $command =~ s/\$SSH_USER/$ssh_user/g;
1033 $command =~ s/\$MACHINE/$machine/g;
1035 doprint("$command ... ");
1037 $pid = open(CMD, "$command 2>&1 |") or
1038 (fail "unable to exec $command" and return 0);
1040 if (defined($opt{"LOG_FILE"})) {
1041 open(LOG, ">>$opt{LOG_FILE}") or
1042 dodie "failed to write to log";
1046 if (defined($redirect)) {
1047 open (RD, ">$redirect") or
1048 dodie "failed to write to redirect $redirect";
1053 print LOG if ($dolog);
1054 print RD if ($dord);
1061 close(LOG) if ($dolog);
1062 close(RD) if ($dord);
1065 doprint "FAILED!\n";
1067 doprint "SUCCESS\n";
1075 my $cp_exec = $ssh_exec;
1077 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1078 return run_command "$cp_exec";
1082 my ($src, $dst) = @_;
1083 my $cp_scp = $scp_to_target;
1085 $cp_scp =~ s/\$SRC_FILE/$src/g;
1086 $cp_scp =~ s/\$DST_FILE/$dst/g;
1088 return run_command "$cp_scp";
1091 sub get_grub_index {
1093 if ($reboot_type ne "grub") {
1096 return if (defined($grub_number));
1098 doprint "Find grub menu ... ";
1101 my $ssh_grub = $ssh_exec;
1102 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1104 open(IN, "$ssh_grub |")
1105 or die "unable to get menu.lst";
1110 if (/^\s*title\s+$grub_menu\s*$/) {
1114 } elsif (/^\s*title\s/) {
1120 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
1122 doprint "$grub_number\n";
1127 my ($fp, $time) = @_;
1133 if (!defined($time)) {
1138 vec($rin, fileno($fp), 1) = 1;
1139 $ready = select($rin, undef, undef, $time);
1143 # try to read one char at a time
1144 while (sysread $fp, $ch, 1) {
1146 last if ($ch eq "\n");
1149 if (!length($line)) {
1157 if ($reboot_type eq "grub") {
1158 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1163 run_command "$reboot_script";
1169 doprint "git rev-list --max-count=1 $commit ... ";
1170 my $sha1 = `git rev-list --max-count=1 $commit`;
1177 dodie "Failed to get git $commit";
1190 my $skip_call_trace = 0;
1198 open(DMESG, "> $dmesg") or
1199 die "unable to write to $dmesg";
1205 my $monitor_start = time;
1207 my $version_found = 0;
1211 if ($bug && defined($stop_after_failure) &&
1212 $stop_after_failure >= 0) {
1213 my $time = $stop_after_failure - (time - $failure_start);
1214 $line = wait_for_input($monitor_fp, $time);
1215 if (!defined($line)) {
1216 doprint "bug timed out after $booted_timeout seconds\n";
1217 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1221 $line = wait_for_input($monitor_fp, $booted_timeout);
1222 if (!defined($line)) {
1223 my $s = $booted_timeout == 1 ? "" : "s";
1224 doprint "Successful boot found: break after $booted_timeout second$s\n";
1228 $line = wait_for_input($monitor_fp);
1229 if (!defined($line)) {
1230 my $s = $timeout == 1 ? "" : "s";
1231 doprint "Timed out after $timeout second$s\n";
1239 # we are not guaranteed to get a full line
1240 $full_line .= $line;
1242 if ($full_line =~ /$success_line/) {
1244 $success_start = time;
1247 if ($booted && defined($stop_after_success) &&
1248 $stop_after_success >= 0) {
1250 if ($now - $success_start >= $stop_after_success) {
1251 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1256 if ($full_line =~ /\[ backtrace testing \]/) {
1257 $skip_call_trace = 1;
1260 if ($full_line =~ /call trace:/i) {
1261 if (!$bug && !$skip_call_trace) {
1263 $failure_start = time;
1267 if ($bug && defined($stop_after_failure) &&
1268 $stop_after_failure >= 0) {
1270 if ($now - $failure_start >= $stop_after_failure) {
1271 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1276 if ($full_line =~ /\[ end of backtrace testing \]/) {
1277 $skip_call_trace = 0;
1280 if ($full_line =~ /Kernel panic -/) {
1281 $failure_start = time;
1285 # Detect triple faults by testing the banner
1286 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1287 if ($1 eq $version) {
1289 } elsif ($version_found && $detect_triplefault) {
1290 # We already booted into the kernel we are testing,
1291 # but now we booted into another kernel?
1292 # Consider this a triple fault.
1293 doprint "Aleady booted in Linux kernel $version, but now\n";
1294 doprint "we booted into Linux kernel $1.\n";
1295 doprint "Assuming that this is a triple fault.\n";
1296 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1301 if ($line =~ /\n/) {
1305 if ($stop_test_after > 0 && !$booted && !$bug) {
1306 if (time - $monitor_start > $stop_test_after) {
1307 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
1316 return 0 if ($in_bisect);
1317 fail "failed - got a bug report" and return 0;
1321 return 0 if ($in_bisect);
1322 fail "failed - never got a boot prompt." and return 0;
1328 sub do_post_install {
1330 return if (!defined($post_install));
1332 my $cp_post_install = $post_install;
1333 $cp_post_install =~ s/\$KERNEL_VERSION/$version/g;
1334 run_command "$cp_post_install" or
1335 dodie "Failed to run post install";
1340 return if ($no_install);
1342 run_scp "$outputdir/$build_target", "$target_image" or
1343 dodie "failed to copy image";
1345 my $install_mods = 0;
1347 # should we process modules?
1349 open(IN, "$output_config") or dodie("Can't read config file");
1351 if (/CONFIG_MODULES(=y)?/) {
1352 $install_mods = 1 if (defined($1));
1358 if (!$install_mods) {
1360 doprint "No modules needed\n";
1364 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
1365 dodie "Failed to install modules";
1367 my $modlib = "/lib/modules/$version";
1368 my $modtar = "ktest-mods.tar.bz2";
1370 run_ssh "rm -rf $modlib" or
1371 dodie "failed to remove old mods: $modlib";
1373 # would be nice if scp -r did not follow symbolic links
1374 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
1375 dodie "making tarball";
1377 run_scp "$tmpdir/$modtar", "/tmp" or
1378 dodie "failed to copy modules";
1380 unlink "$tmpdir/$modtar";
1382 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
1383 dodie "failed to tar modules";
1385 run_ssh "rm -f /tmp/$modtar";
1391 # get the release name
1392 doprint "$make kernelrelease ... ";
1393 $version = `$make kernelrelease | tail -1`;
1395 doprint "$version\n";
1398 sub start_monitor_and_boot {
1399 # Make sure the stable kernel has finished booting
1412 sub check_buildlog {
1415 my @files = `git show $patch | diffstat -l`;
1417 open(IN, "git show $patch |") or
1418 dodie "failed to show $patch";
1420 if (m,^--- a/(.*),) {
1422 $files[$#files] = $1;
1427 open(IN, $buildlog) or dodie "Can't open $buildlog";
1429 if (/^\s*(.*?):.*(warning|error)/) {
1431 foreach my $file (@files) {
1432 my $fullpath = "$builddir/$file";
1433 if ($file eq $err || $fullpath eq $err) {
1434 fail "$file built with warnings" and return 0;
1444 sub apply_min_config {
1445 my $outconfig = "$output_config.new";
1447 # Read the config file and remove anything that
1448 # is in the force_config hash (from minconfig and others)
1449 # then add the force config back.
1451 doprint "Applying minimum configurations into $output_config.new\n";
1453 open (OUT, ">$outconfig") or
1454 dodie "Can't create $outconfig";
1456 if (-f $output_config) {
1457 open (IN, $output_config) or
1458 dodie "Failed to open $output_config";
1460 if (/^(# )?(CONFIG_[^\s=]*)/) {
1461 next if (defined($force_config{$2}));
1467 foreach my $config (keys %force_config) {
1468 print OUT "$force_config{$config}\n";
1472 run_command "mv $outconfig $output_config";
1475 sub make_oldconfig {
1477 my @force_list = keys %force_config;
1479 if ($#force_list >= 0) {
1483 if (!run_command "$make oldnoconfig") {
1484 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1485 # try a yes '' | oldconfig
1486 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
1487 run_command "yes '' | $make oldconfig" or
1488 dodie "failed make config oldconfig";
1492 # read a config file and use this to force new configs.
1493 sub load_force_config {
1496 open(IN, $config) or
1497 dodie "failed to read $config";
1500 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1501 $force_config{$1} = $_;
1502 } elsif (/^# (CONFIG_\S*) is not set/) {
1503 $force_config{$1} = $_;
1514 # Failed builds should not reboot the target
1515 my $save_no_reboot = $no_reboot;
1518 if (defined($pre_build)) {
1519 my $ret = run_command $pre_build;
1520 if (!$ret && defined($pre_build_die) &&
1522 dodie "failed to pre_build\n";
1526 if ($type =~ /^useconfig:(.*)/) {
1527 run_command "cp $1 $output_config" or
1528 dodie "could not copy $1 to .config";
1530 $type = "oldconfig";
1533 # old config can ask questions
1534 if ($type eq "oldconfig") {
1535 $type = "oldnoconfig";
1537 # allow for empty configs
1538 run_command "touch $output_config";
1541 run_command "mv $output_config $outputdir/config_temp" or
1542 dodie "moving .config";
1544 run_command "$make mrproper" or dodie "make mrproper";
1546 run_command "mv $outputdir/config_temp $output_config" or
1547 dodie "moving config_temp";
1550 } elsif (!$noclean) {
1551 unlink "$output_config";
1552 run_command "$make mrproper" or
1553 dodie "make mrproper";
1556 # add something to distinguish this build
1557 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1558 print OUT "$localversion\n";
1561 if (defined($minconfig)) {
1562 load_force_config($minconfig);
1565 if ($type ne "oldnoconfig") {
1566 run_command "$make $type" or
1567 dodie "failed make config";
1569 # Run old config regardless, to enforce min configurations
1572 $redirect = "$buildlog";
1573 my $build_ret = run_command "$make $build_options";
1576 if (defined($post_build)) {
1577 my $ret = run_command $post_build;
1578 if (!$ret && defined($post_build_die) &&
1580 dodie "failed to post_build\n";
1585 # bisect may need this to pass
1587 $no_reboot = $save_no_reboot;
1590 fail "failed build" and return 0;
1593 $no_reboot = $save_no_reboot;
1599 if (!run_ssh "halt" or defined($power_off)) {
1600 if (defined($poweroff_after_halt)) {
1601 sleep $poweroff_after_halt;
1602 run_command "$power_off";
1606 run_command "$power_off";
1617 if (defined($test_name)) {
1618 $name = " ($test_name)";
1621 doprint "\n\n*******************************************\n";
1622 doprint "*******************************************\n";
1623 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
1624 doprint "*******************************************\n";
1625 doprint "*******************************************\n";
1627 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
1628 doprint "Reboot and wait $sleep_time seconds\n";
1635 doprint "Pass or fail? [p/f]";
1638 if ($ans eq "p" || $ans eq "P") {
1640 } elsif ($ans eq "f" || $ans eq "F") {
1643 print "Please answer 'P' or 'F'\n";
1648 sub child_run_test {
1651 # child should have no power
1652 $reboot_on_error = 0;
1653 $poweroff_on_error = 0;
1654 $die_on_failure = 1;
1656 run_command $run_test or $failed = 1;
1662 sub child_finished {
1675 doprint "run test $run_test\n";
1679 $SIG{CHLD} = qw(child_finished);
1683 child_run_test if (!$child_pid);
1688 $line = wait_for_input($monitor_fp, 1);
1689 if (defined($line)) {
1691 # we are not guaranteed to get a full line
1692 $full_line .= $line;
1695 if ($full_line =~ /call trace:/i) {
1699 if ($full_line =~ /Kernel panic -/) {
1703 if ($line =~ /\n/) {
1707 } while (!$child_done && !$bug);
1710 my $failure_start = time;
1713 $line = wait_for_input($monitor_fp, 1);
1714 if (defined($line)) {
1718 if ($now - $failure_start >= $stop_after_failure) {
1721 } while (defined($line));
1723 doprint "Detected kernel crash!\n";
1724 # kill the child with extreme prejudice
1728 waitpid $child_pid, 0;
1731 if ($bug || $child_exit) {
1732 return 0 if $in_bisect;
1733 fail "test failed" and return 0;
1738 sub run_git_bisect {
1741 doprint "$command ... ";
1743 my $output = `$command 2>&1`;
1750 dodie "Failed to git bisect";
1753 doprint "SUCCESS\n";
1754 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1755 doprint "$1 [$2]\n";
1756 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
1758 doprint "Found bad commit... $1\n";
1761 # we already logged it, just print it now.
1769 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
1770 reboot $bisect_sleep_time;
1773 # returns 1 on success, 0 on failure, -1 on skip
1774 sub run_bisect_test {
1775 my ($type, $buildtype) = @_;
1784 build $buildtype or $failed = 1;
1786 if ($type ne "build") {
1787 if ($failed && $bisect_skip) {
1791 dodie "Failed on build" if $failed;
1794 start_monitor_and_boot or $failed = 1;
1796 if ($type ne "boot") {
1797 if ($failed && $bisect_skip) {
1803 dodie "Failed on boot" if $failed;
1805 do_run_test or $failed = 1;
1816 # reboot the box to a kernel we can ssh to
1817 if ($type ne "build") {
1827 my $buildtype = "oldconfig";
1829 # We should have a minconfig to use?
1830 if (defined($minconfig)) {
1831 $buildtype = "useconfig:$minconfig";
1834 my $ret = run_bisect_test $type, $buildtype;
1836 if ($bisect_manual) {
1837 $ret = answer_bisect;
1840 # Are we looking for where it worked, not failed?
1841 if ($reverse_bisect) {
1847 } elsif ($ret == 0) {
1849 } elsif ($bisect_skip) {
1850 doprint "HIT A BAD COMMIT ... SKIPPING\n";
1860 die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
1861 die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
1862 die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
1864 my $good = $opt{"BISECT_GOOD[$i]"};
1865 my $bad = $opt{"BISECT_BAD[$i]"};
1866 my $type = $opt{"BISECT_TYPE[$i]"};
1867 my $start = $opt{"BISECT_START[$i]"};
1868 my $replay = $opt{"BISECT_REPLAY[$i]"};
1869 my $start_files = $opt{"BISECT_FILES[$i]"};
1871 if (defined($start_files)) {
1872 $start_files = " -- " . $start_files;
1877 # convert to true sha1's
1878 $good = get_sha1($good);
1879 $bad = get_sha1($bad);
1881 if (defined($opt{"BISECT_REVERSE[$i]"}) &&
1882 $opt{"BISECT_REVERSE[$i]"} == 1) {
1883 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
1884 $reverse_bisect = 1;
1886 $reverse_bisect = 0;
1889 # Can't have a test without having a test to run
1890 if ($type eq "test" && !defined($run_test)) {
1894 my $check = $opt{"BISECT_CHECK[$i]"};
1895 if (defined($check) && $check ne "0") {
1898 my $head = get_sha1("HEAD");
1900 if ($check ne "good") {
1901 doprint "TESTING BISECT BAD [$bad]\n";
1902 run_command "git checkout $bad" or
1903 die "Failed to checkout $bad";
1905 $result = run_bisect $type;
1907 if ($result ne "bad") {
1908 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
1912 if ($check ne "bad") {
1913 doprint "TESTING BISECT GOOD [$good]\n";
1914 run_command "git checkout $good" or
1915 die "Failed to checkout $good";
1917 $result = run_bisect $type;
1919 if ($result ne "good") {
1920 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
1924 # checkout where we started
1925 run_command "git checkout $head" or
1926 die "Failed to checkout $head";
1929 run_command "git bisect start$start_files" or
1930 dodie "could not start bisect";
1932 run_command "git bisect good $good" or
1933 dodie "could not set bisect good to $good";
1935 run_git_bisect "git bisect bad $bad" or
1936 dodie "could not set bisect bad to $bad";
1938 if (defined($replay)) {
1939 run_command "git bisect replay $replay" or
1940 dodie "failed to run replay";
1943 if (defined($start)) {
1944 run_command "git checkout $start" or
1945 dodie "failed to checkout $start";
1950 $result = run_bisect $type;
1951 $test = run_git_bisect "git bisect $result";
1954 run_command "git bisect log" or
1955 dodie "could not capture git bisect log";
1957 run_command "git bisect reset" or
1958 dodie "could not reset git bisect";
1960 doprint "Bad commit was [$bisect_bad]\n";
1973 sub assign_configs {
1974 my ($hash, $config) = @_;
1977 or dodie "Failed to read $config";
1980 if (/^((CONFIG\S*)=.*)/) {
1988 sub process_config_ignore {
1991 assign_configs \%config_ignore, $config;
1994 sub read_current_config {
1995 my ($config_ref) = @_;
1997 %{$config_ref} = ();
1998 undef %{$config_ref};
2000 my @key = keys %{$config_ref};
2002 print "did not delete!\n";
2005 open (IN, "$output_config");
2008 if (/^(CONFIG\S+)=(.*)/) {
2009 ${$config_ref}{$1} = $2;
2015 sub get_dependencies {
2018 my $arr = $dependency{$config};
2019 if (!defined($arr)) {
2025 foreach my $dep (@{$arr}) {
2026 print "ADD DEP $dep\n";
2027 @deps = (@deps, get_dependencies $dep);
2036 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2038 foreach my $config (@configs) {
2039 print OUT "$config_set{$config}\n";
2040 my @deps = get_dependencies $config;
2041 foreach my $dep (@deps) {
2042 print OUT "$config_set{$dep}\n";
2046 foreach my $config (keys %config_ignore) {
2047 print OUT "$config_ignore{$config}\n";
2055 sub compare_configs {
2058 foreach my $item (keys %a) {
2059 if (!defined($b{$item})) {
2060 print "diff $item\n";
2068 print "diff2 $keys[0]\n";
2070 return -1 if ($#keys >= 0);
2075 sub run_config_bisect_test {
2078 return run_bisect_test $type, "oldconfig";
2081 sub process_passed {
2084 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2085 # Passed! All these configs are part of a good compile.
2086 # Add them to the min options.
2087 foreach my $config (keys %configs) {
2088 if (defined($config_list{$config})) {
2089 doprint " removing $config\n";
2090 $config_ignore{$config} = $config_list{$config};
2091 delete $config_list{$config};
2094 doprint "config copied to $outputdir/config_good\n";
2095 run_command "cp -f $output_config $outputdir/config_good";
2098 sub process_failed {
2101 doprint "\n\n***************************************\n";
2102 doprint "Found bad config: $config\n";
2103 doprint "***************************************\n\n";
2106 sub run_config_bisect {
2108 my @start_list = keys %config_list;
2110 if ($#start_list < 0) {
2111 doprint "No more configs to test!!!\n";
2115 doprint "***** RUN TEST ***\n";
2116 my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
2120 my $count = $#start_list + 1;
2121 doprint " $count configs to test\n";
2123 my $half = int($#start_list / 2);
2126 my @tophalf = @start_list[0 .. $half];
2128 create_config @tophalf;
2129 read_current_config \%current_config;
2131 $count = $#tophalf + 1;
2132 doprint "Testing $count configs\n";
2134 # make sure we test something
2135 foreach my $config (@tophalf) {
2136 if (defined($current_config{$config})) {
2142 # try the other half
2143 doprint "Top half produced no set configs, trying bottom half\n";
2144 @tophalf = @start_list[$half + 1 .. $#start_list];
2145 create_config @tophalf;
2146 read_current_config \%current_config;
2147 foreach my $config (@tophalf) {
2148 if (defined($current_config{$config})) {
2154 doprint "Failed: Can't make new config with current configs\n";
2155 foreach my $config (@start_list) {
2156 doprint " CONFIG: $config\n";
2160 $count = $#tophalf + 1;
2161 doprint "Testing $count configs\n";
2164 $ret = run_config_bisect_test $type;
2165 if ($bisect_manual) {
2166 $ret = answer_bisect;
2169 process_passed %current_config;
2173 doprint "This config had a failure.\n";
2174 doprint "Removing these configs that were not set in this config:\n";
2175 doprint "config copied to $outputdir/config_bad\n";
2176 run_command "cp -f $output_config $outputdir/config_bad";
2178 # A config exists in this group that was bad.
2179 foreach my $config (keys %config_list) {
2180 if (!defined($current_config{$config})) {
2181 doprint " removing $config\n";
2182 delete $config_list{$config};
2186 @start_list = @tophalf;
2188 if ($#start_list == 0) {
2189 process_failed $start_list[0];
2193 # remove half the configs we are looking at and see if
2195 $half = int($#start_list / 2);
2196 } while ($#start_list > 0);
2198 # we found a single config, try it again unless we are running manually
2200 if ($bisect_manual) {
2201 process_failed $start_list[0];
2205 my @tophalf = @start_list[0 .. 0];
2207 $ret = run_config_bisect_test $type;
2209 process_passed %current_config;
2213 process_failed $start_list[0];
2220 my $start_config = $opt{"CONFIG_BISECT[$i]"};
2222 my $tmpconfig = "$tmpdir/use_config";
2224 if (defined($config_bisect_good)) {
2225 process_config_ignore $config_bisect_good;
2228 # Make the file with the bad config and the min config
2229 if (defined($minconfig)) {
2230 # read the min config for things to ignore
2231 run_command "cp $minconfig $tmpconfig" or
2232 dodie "failed to copy $minconfig to $tmpconfig";
2237 if (-f $tmpconfig) {
2238 load_force_config($tmpconfig);
2239 process_config_ignore $tmpconfig;
2242 # now process the start config
2243 run_command "cp $start_config $output_config" or
2244 dodie "failed to copy $start_config to $output_config";
2246 # read directly what we want to check
2248 open (IN, $output_config)
2249 or dodie "faied to open $output_config";
2252 if (/^((CONFIG\S*)=.*)/) {
2253 $config_check{$2} = $1;
2258 # Now run oldconfig with the minconfig
2261 # check to see what we lost (or gained)
2262 open (IN, $output_config)
2263 or dodie "Failed to read $start_config";
2265 my %removed_configs;
2269 if (/^((CONFIG\S*)=.*)/) {
2270 # save off all options
2271 $config_set{$2} = $1;
2272 if (defined($config_check{$2})) {
2273 if (defined($config_ignore{$2})) {
2274 $removed_configs{$2} = $1;
2276 $config_list{$2} = $1;
2278 } elsif (!defined($config_ignore{$2})) {
2279 $added_configs{$2} = $1;
2280 $config_list{$2} = $1;
2286 my @confs = keys %removed_configs;
2288 doprint "Configs overridden by default configs and removed from check:\n";
2289 foreach my $config (@confs) {
2290 doprint " $config\n";
2293 @confs = keys %added_configs;
2295 doprint "Configs appearing in make oldconfig and added:\n";
2296 foreach my $config (@confs) {
2297 doprint " $config\n";
2304 # Sometimes kconfig does weird things. We must make sure
2305 # that the config we autocreate has everything we need
2306 # to test, otherwise we may miss testing configs, or
2307 # may not be able to create a new config.
2308 # Here we create a config with everything set.
2309 create_config (keys %config_list);
2310 read_current_config \%config_test;
2311 foreach my $config (keys %config_list) {
2312 if (!defined($config_test{$config})) {
2315 doprint "Configs not produced by kconfig (will not be checked):\n";
2317 doprint " $config\n";
2318 delete $config_list{$config};
2323 $ret = run_config_bisect;
2326 return $ret if ($ret < 0);
2331 sub patchcheck_reboot {
2332 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
2333 reboot $patchcheck_sleep_time;
2339 die "PATCHCHECK_START[$i] not defined\n"
2340 if (!defined($opt{"PATCHCHECK_START[$i]"}));
2341 die "PATCHCHECK_TYPE[$i] not defined\n"
2342 if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
2344 my $start = $opt{"PATCHCHECK_START[$i]"};
2347 if (defined($opt{"PATCHCHECK_END[$i]"})) {
2348 $end = $opt{"PATCHCHECK_END[$i]"};
2351 # Get the true sha1's since we can use things like HEAD~3
2352 $start = get_sha1($start);
2353 $end = get_sha1($end);
2355 my $type = $opt{"PATCHCHECK_TYPE[$i]"};
2357 # Can't have a test without having a test to run
2358 if ($type eq "test" && !defined($run_test)) {
2362 open (IN, "git log --pretty=oneline $end|") or
2363 dodie "could not get git list";
2369 $list[$#list+1] = $_;
2370 last if (/^$start/);
2374 if ($list[$#list] !~ /^$start/) {
2375 fail "SHA1 $start not found";
2378 # go backwards in the list
2379 @list = reverse @list;
2381 my $save_clean = $noclean;
2382 my %ignored_warnings;
2384 if (defined($ignore_warnings)) {
2385 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2386 $ignored_warnings{$sha1} = 1;
2391 foreach my $item (@list) {
2393 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2395 doprint "\nProcessing commit $item\n\n";
2397 run_command "git checkout $sha1" or
2398 die "Failed to checkout $sha1";
2400 # only clean on the first and last patch
2401 if ($item eq $list[0] ||
2402 $item eq $list[$#list]) {
2403 $noclean = $save_clean;
2408 if (defined($minconfig)) {
2409 build "useconfig:$minconfig" or return 0;
2411 # ?? no config to use?
2412 build "oldconfig" or return 0;
2416 if (!defined($ignored_warnings{$sha1})) {
2417 check_buildlog $sha1 or return 0;
2420 next if ($type eq "build");
2424 start_monitor_and_boot or $failed = 1;
2426 if (!$failed && $type ne "boot"){
2427 do_run_test or $failed = 1;
2430 return 0 if ($failed);
2450 # $config depends on $dep
2451 my ($config, $dep) = @_;
2453 if (defined($depends{$config})) {
2454 $depends{$config} .= " " . $dep;
2456 $depends{$config} = $dep;
2459 # record the number of configs depending on $dep
2460 if (defined $depcount{$dep}) {
2463 $depcount{$dep} = 1;
2467 # taken from streamline_config.pl
2479 if (! -f $kconfig) {
2480 doprint "file $kconfig does not exist, skipping\n";
2484 open(KIN, "$kconfig")
2485 or die "Can't open $kconfig";
2489 # Make sure that lines ending with \ continue
2491 $_ = $line . " " . $_;
2502 # collect any Kconfig sources
2503 if (/^source\s*"(.*)"/) {
2504 $kconfigs[$#kconfigs+1] = $1;
2508 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2512 for (my $i = 0; $i < $iflevel; $i++) {
2513 add_dep $config, $ifdeps[$i];
2516 # collect the depends for the config
2517 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2519 add_dep $config, $1;
2521 # Get the configs that select this config
2522 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2524 # selected by depends on config
2525 add_dep $1, $config;
2527 # Check for if statements
2528 } elsif (/^if\s+(.*\S)\s*$/) {
2530 # remove beginning and ending non text
2531 $deps =~ s/^[^a-zA-Z0-9_]*//;
2532 $deps =~ s/[^a-zA-Z0-9_]*$//;
2534 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2536 $ifdeps[$iflevel++] = join ':', @deps;
2538 } elsif (/^endif/) {
2540 $iflevel-- if ($iflevel);
2543 } elsif (/^\s*help\s*$/) {
2549 # read in any configs that were found.
2550 foreach $kconfig (@kconfigs) {
2551 if (!defined($read_kconfigs{$kconfig})) {
2552 $read_kconfigs{$kconfig} = 1;
2553 read_kconfig("$builddir/$kconfig");
2559 # find out which arch this is by the kconfig file
2560 open (IN, $output_config)
2561 or dodie "Failed to read $output_config";
2564 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2571 if (!defined($arch)) {
2572 doprint "Could not find arch from config file\n";
2573 doprint "no dependencies used\n";
2577 # arch is really the subarch, we need to know
2578 # what directory to look at.
2579 if ($arch eq "i386" || $arch eq "x86_64") {
2581 } elsif ($arch =~ /^tile/) {
2585 my $kconfig = "$builddir/arch/$arch/Kconfig";
2587 if (! -f $kconfig && $arch =~ /\d$/) {
2589 # some subarchs have numbers, truncate them
2591 $kconfig = "$builddir/arch/$arch/Kconfig";
2592 if (! -f $kconfig) {
2593 doprint "No idea what arch dir $orig is for\n";
2594 doprint "no dependencies used\n";
2599 read_kconfig($kconfig);
2602 sub read_config_list {
2606 or dodie "Failed to read $config";
2609 if (/^((CONFIG\S*)=.*)/) {
2610 if (!defined($config_ignore{$2})) {
2611 $config_list{$2} = $1;
2619 sub read_output_config {
2622 assign_configs \%config_ignore, $config;
2625 sub make_new_config {
2628 open (OUT, ">$output_config")
2629 or dodie "Failed to write $output_config";
2631 foreach my $config (@configs) {
2632 print OUT "$config\n";
2640 $config =~ s/CONFIG_//;
2648 my $kconfig = chomp_config $dep;
2650 $dep = $depends{"$kconfig"};
2652 # the dep string we have saves the dependencies as they
2653 # were found, including expressions like ! && ||. We
2654 # want to split this out into just an array of configs.
2656 my $valid = "A-Za-z_0-9";
2660 while ($dep =~ /[$valid]/) {
2662 if ($dep =~ /^[^$valid]*([$valid]+)/) {
2663 my $conf = "CONFIG_" . $1;
2665 $configs[$#configs + 1] = $conf;
2667 $dep =~ s/^[^$valid]*[$valid]+//;
2669 die "this should never happen";
2679 my %processed_configs;
2680 my %nochange_config;
2682 sub test_this_config {
2687 # if we already processed this config, skip it
2688 if (defined($processed_configs{$config})) {
2691 $processed_configs{$config} = 1;
2693 # if this config failed during this round, skip it
2694 if (defined($nochange_config{$config})) {
2698 my $kconfig = chomp_config $config;
2700 # Test dependencies first
2701 if (defined($depends{"$kconfig"})) {
2702 my @parents = get_depends $config;
2703 foreach my $parent (@parents) {
2704 # if the parent is in the min config, check it first
2705 next if (!defined($min_configs{$parent}));
2706 $found = test_this_config($parent);
2707 if (defined($found)) {
2713 # Remove this config from the list of configs
2714 # do a make oldnoconfig and then read the resulting
2715 # .config to make sure it is missing the config that
2717 my %configs = %min_configs;
2718 delete $configs{$config};
2719 make_new_config ((values %configs), (values %keep_configs));
2722 assign_configs \%configs, $output_config;
2724 return $config if (!defined($configs{$config}));
2726 doprint "disabling config $config did not change .config\n";
2728 $nochange_config{$config} = 1;
2733 sub make_min_config {
2736 if (!defined($output_minconfig)) {
2737 fail "OUTPUT_MIN_CONFIG not defined" and return;
2740 # If output_minconfig exists, and the start_minconfig
2741 # came from min_config, than ask if we should use
2743 if (-f $output_minconfig && !$start_minconfig_defined) {
2744 print "$output_minconfig exists\n";
2745 if (read_yn " Use it as minconfig?") {
2746 $start_minconfig = $output_minconfig;
2750 if (!defined($start_minconfig)) {
2751 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
2754 my $temp_config = "$tmpdir/temp_config";
2756 # First things first. We build an allnoconfig to find
2757 # out what the defaults are that we can't touch.
2758 # Some are selections, but we really can't handle selections.
2760 my $save_minconfig = $minconfig;
2763 run_command "$make allnoconfig" or return 0;
2767 process_config_ignore $output_config;
2769 undef %save_configs;
2772 if (defined($ignore_config)) {
2773 # make sure the file exists
2774 `touch $ignore_config`;
2775 assign_configs \%save_configs, $ignore_config;
2778 %keep_configs = %save_configs;
2780 doprint "Load initial configs from $start_minconfig\n";
2782 # Look at the current min configs, and save off all the
2783 # ones that were set via the allnoconfig
2784 assign_configs \%min_configs, $start_minconfig;
2786 my @config_keys = keys %min_configs;
2788 # All configs need a depcount
2789 foreach my $config (@config_keys) {
2790 my $kconfig = chomp_config $config;
2791 if (!defined $depcount{$kconfig}) {
2792 $depcount{$kconfig} = 0;
2796 # Remove anything that was set by the make allnoconfig
2797 # we shouldn't need them as they get set for us anyway.
2798 foreach my $config (@config_keys) {
2799 # Remove anything in the ignore_config
2800 if (defined($keep_configs{$config})) {
2801 my $file = $ignore_config;
2802 $file =~ s,.*/(.*?)$,$1,;
2803 doprint "$config set by $file ... ignored\n";
2804 delete $min_configs{$config};
2807 # But make sure the settings are the same. If a min config
2808 # sets a selection, we do not want to get rid of it if
2809 # it is not the same as what we have. Just move it into
2811 if (defined($config_ignore{$config})) {
2812 if ($config_ignore{$config} ne $min_configs{$config}) {
2813 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
2814 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
2815 $keep_configs{$config} = $min_configs{$config};
2817 doprint "$config set by allnoconfig ... ignored\n";
2819 delete $min_configs{$config};
2831 # Now disable each config one by one and do a make oldconfig
2832 # till we find a config that changes our list.
2834 my @test_configs = keys %min_configs;
2836 # Sort keys by who is most dependent on
2837 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
2840 # Put configs that did not modify the config at the end.
2842 for (my $i = 0; $i < $#test_configs; $i++) {
2843 if (!defined($nochange_config{$test_configs[0]})) {
2847 # This config didn't change the .config last time.
2848 # Place it at the end
2849 my $config = shift @test_configs;
2850 push @test_configs, $config;
2853 # if every test config has failed to modify the .config file
2854 # in the past, then reset and start over.
2856 undef %nochange_config;
2859 undef %processed_configs;
2861 foreach my $config (@test_configs) {
2863 $found = test_this_config $config;
2865 last if (defined($found));
2867 # oh well, try another config
2870 if (!defined($found)) {
2871 # we could have failed due to the nochange_config hash
2872 # reset and try again
2874 undef %nochange_config;
2878 doprint "No more configs found that we can disable\n";
2886 doprint "Test with $config disabled\n";
2888 # set in_bisect to keep build and monitor from dieing
2893 start_monitor_and_boot or $failed = 1;
2899 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
2900 # this config is needed, add it to the ignore list.
2901 $keep_configs{$config} = $min_configs{$config};
2902 $save_configs{$config} = $min_configs{$config};
2903 delete $min_configs{$config};
2905 # update new ignore configs
2906 if (defined($ignore_config)) {
2907 open (OUT, ">$temp_config")
2908 or die "Can't write to $temp_config";
2909 foreach my $config (keys %save_configs) {
2910 print OUT "$save_configs{$config}\n";
2913 run_command "mv $temp_config $ignore_config" or
2914 dodie "failed to copy update to $ignore_config";
2918 # We booted without this config, remove it from the minconfigs.
2919 doprint "$config is not needed, disabling\n";
2921 delete $min_configs{$config};
2923 # Also disable anything that is not enabled in this config
2925 assign_configs \%configs, $output_config;
2926 my @config_keys = keys %min_configs;
2927 foreach my $config (@config_keys) {
2928 if (!defined($configs{$config})) {
2929 doprint "$config is not set, disabling\n";
2930 delete $min_configs{$config};
2934 # Save off all the current mandidory configs
2935 open (OUT, ">$temp_config")
2936 or die "Can't write to $temp_config";
2937 foreach my $config (keys %keep_configs) {
2938 print OUT "$keep_configs{$config}\n";
2940 foreach my $config (keys %min_configs) {
2941 print OUT "$min_configs{$config}\n";
2945 run_command "mv $temp_config $output_minconfig" or
2946 dodie "failed to copy update to $output_minconfig";
2949 doprint "Reboot and wait $sleep_time seconds\n";
2957 $#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
2960 $ktest_config = $ARGV[0];
2961 if (! -f $ktest_config) {
2962 print "$ktest_config does not exist.\n";
2963 if (!read_yn "Create it?") {
2968 $ktest_config = "ktest.conf";
2971 if (! -f $ktest_config) {
2972 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
2974 # Generated by ktest.pl
2976 # Define each test with TEST_START
2977 # The config options below it will override the defaults
2985 read_config $ktest_config;
2987 if (defined($opt{"LOG_FILE"})) {
2988 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
2991 # Append any configs entered in manually to the config file.
2992 my @new_configs = keys %entered_configs;
2993 if ($#new_configs >= 0) {
2994 print "\nAppending entered in configs to $ktest_config\n";
2995 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
2996 foreach my $config (@new_configs) {
2997 print OUT "$config = $entered_configs{$config}\n";
2998 $opt{$config} = $entered_configs{$config};
3002 if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3003 unlink $opt{"LOG_FILE"};
3006 doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3008 for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3011 doprint "DEFAULT OPTIONS:\n";
3013 doprint "\nTEST $i OPTIONS";
3014 if (defined($repeat_tests{$i})) {
3015 $repeat = $repeat_tests{$i};
3016 doprint " ITERATE $repeat";
3021 foreach my $option (sort keys %opt) {
3023 if ($option =~ /\[(\d+)\]$/) {
3029 doprint "$option = $opt{$option}\n";
3033 sub __set_test_option {
3034 my ($name, $i) = @_;
3036 my $option = "$name\[$i\]";
3038 if (defined($opt{$option})) {
3039 return $opt{$option};
3042 foreach my $test (keys %repeat_tests) {
3044 $i < $test + $repeat_tests{$test}) {
3045 $option = "$name\[$test\]";
3046 if (defined($opt{$option})) {
3047 return $opt{$option};
3052 if (defined($opt{$name})) {
3059 sub set_test_option {
3060 my ($name, $i) = @_;
3062 my $option = __set_test_option($name, $i);
3063 return $option if (!defined($option));
3065 return eval_option($option, $i);
3068 # First we need to do is the builds
3069 for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
3071 # Do not reboot on failing test options
3076 my $makecmd = set_test_option("MAKE_CMD", $i);
3078 $machine = set_test_option("MACHINE", $i);
3079 $ssh_user = set_test_option("SSH_USER", $i);
3080 $tmpdir = set_test_option("TMP_DIR", $i);
3081 $outputdir = set_test_option("OUTPUT_DIR", $i);
3082 $builddir = set_test_option("BUILD_DIR", $i);
3083 $test_type = set_test_option("TEST_TYPE", $i);
3084 $build_type = set_test_option("BUILD_TYPE", $i);
3085 $build_options = set_test_option("BUILD_OPTIONS", $i);
3086 $pre_build = set_test_option("PRE_BUILD", $i);
3087 $post_build = set_test_option("POST_BUILD", $i);
3088 $pre_build_die = set_test_option("PRE_BUILD_DIE", $i);
3089 $post_build_die = set_test_option("POST_BUILD_DIE", $i);
3090 $power_cycle = set_test_option("POWER_CYCLE", $i);
3091 $reboot = set_test_option("REBOOT", $i);
3092 $noclean = set_test_option("BUILD_NOCLEAN", $i);
3093 $minconfig = set_test_option("MIN_CONFIG", $i);
3094 $output_minconfig = set_test_option("OUTPUT_MIN_CONFIG", $i);
3095 $start_minconfig = set_test_option("START_MIN_CONFIG", $i);
3096 $ignore_config = set_test_option("IGNORE_CONFIG", $i);
3097 $run_test = set_test_option("TEST", $i);
3098 $addconfig = set_test_option("ADD_CONFIG", $i);
3099 $reboot_type = set_test_option("REBOOT_TYPE", $i);
3100 $grub_menu = set_test_option("GRUB_MENU", $i);
3101 $post_install = set_test_option("POST_INSTALL", $i);
3102 $no_install = set_test_option("NO_INSTALL", $i);
3103 $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
3104 $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
3105 $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
3106 $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
3107 $power_off = set_test_option("POWER_OFF", $i);
3108 $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
3109 $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
3110 $sleep_time = set_test_option("SLEEP_TIME", $i);
3111 $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
3112 $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
3113 $ignore_warnings = set_test_option("IGNORE_WARNINGS", $i);
3114 $bisect_manual = set_test_option("BISECT_MANUAL", $i);
3115 $bisect_skip = set_test_option("BISECT_SKIP", $i);
3116 $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
3117 $store_failures = set_test_option("STORE_FAILURES", $i);
3118 $test_name = set_test_option("TEST_NAME", $i);
3119 $timeout = set_test_option("TIMEOUT", $i);
3120 $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
3121 $console = set_test_option("CONSOLE", $i);
3122 $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
3123 $success_line = set_test_option("SUCCESS_LINE", $i);
3124 $reboot_success_line = set_test_option("REBOOT_SUCCESS_LINE", $i);
3125 $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
3126 $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
3127 $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
3128 $build_target = set_test_option("BUILD_TARGET", $i);
3129 $ssh_exec = set_test_option("SSH_EXEC", $i);
3130 $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
3131 $target_image = set_test_option("TARGET_IMAGE", $i);
3132 $localversion = set_test_option("LOCALVERSION", $i);
3134 $start_minconfig_defined = 1;
3136 if (!defined($start_minconfig)) {
3137 $start_minconfig_defined = 0;
3138 $start_minconfig = $minconfig;
3141 chdir $builddir || die "can't change directory to $builddir";
3143 foreach my $dir ($tmpdir, $outputdir) {
3146 die "can't create $dir";
3150 $ENV{"SSH_USER"} = $ssh_user;
3151 $ENV{"MACHINE"} = $machine;
3153 $target = "$ssh_user\@$machine";
3155 $buildlog = "$tmpdir/buildlog-$machine";
3156 $dmesg = "$tmpdir/dmesg-$machine";
3157 $make = "$makecmd O=$outputdir";
3158 $output_config = "$outputdir/.config";
3160 if ($reboot_type eq "grub") {
3161 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3162 } elsif (!defined($reboot_script)) {
3163 dodie "REBOOT_SCRIPT not defined"
3166 my $run_type = $build_type;
3167 if ($test_type eq "patchcheck") {
3168 $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
3169 } elsif ($test_type eq "bisect") {
3170 $run_type = $opt{"BISECT_TYPE[$i]"};
3171 } elsif ($test_type eq "config_bisect") {
3172 $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
3175 if ($test_type eq "make_min_config") {
3179 # mistake in config file?
3180 if (!defined($run_type)) {
3181 $run_type = "ERROR";
3185 $installme = " no_install" if ($no_install);
3188 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
3193 if (defined($addconfig)) {
3194 my $min = $minconfig;
3195 if (!defined($minconfig)) {
3198 run_command "cat $addconfig $min > $tmpdir/add_config" or
3199 dodie "Failed to create temp config";
3200 $minconfig = "$tmpdir/add_config";
3203 my $checkout = $opt{"CHECKOUT[$i]"};
3204 if (defined($checkout)) {
3205 run_command "git checkout $checkout" or
3206 die "failed to checkout $checkout";
3212 if ($test_type eq "bisect") {
3215 } elsif ($test_type eq "config_bisect") {
3218 } elsif ($test_type eq "patchcheck") {
3221 } elsif ($test_type eq "make_min_config") {
3226 if ($build_type ne "nobuild") {
3227 build $build_type or next;
3230 if ($test_type eq "install") {
3237 if ($test_type ne "build") {
3239 start_monitor_and_boot or $failed = 1;
3241 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3242 do_run_test or $failed = 1;
3251 if ($opt{"POWEROFF_ON_SUCCESS"}) {
3253 } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
3257 doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";