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;
112 my $bisect_sleep_time;
113 my $patchcheck_sleep_time;
119 my $detect_triplefault;
121 my $reboot_success_line;
123 my $stop_after_success;
124 my $stop_after_failure;
137 # do not force reboots on config problems
140 # default variables that can be used
141 chomp ($variable{"PWD"} = `pwd`);
143 $config_help{"MACHINE"} = << "EOF"
144 The machine hostname that you will test.
147 $config_help{"SSH_USER"} = << "EOF"
148 The box is expected to have ssh on normal bootup, provide the user
149 (most likely root, since you need privileged operations)
152 $config_help{"BUILD_DIR"} = << "EOF"
153 The directory that contains the Linux source code (full path).
156 $config_help{"OUTPUT_DIR"} = << "EOF"
157 The directory that the objects will be built (full path).
158 (can not be same as BUILD_DIR)
161 $config_help{"BUILD_TARGET"} = << "EOF"
162 The location of the compiled file to copy to the target.
163 (relative to OUTPUT_DIR)
166 $config_help{"TARGET_IMAGE"} = << "EOF"
167 The place to put your image on the test machine.
170 $config_help{"POWER_CYCLE"} = << "EOF"
171 A script or command to reboot the box.
173 Here is a digital loggers power switch example
174 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
176 Here is an example to reboot a virtual box on the current host
177 with the name "Guest".
178 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
181 $config_help{"CONSOLE"} = << "EOF"
182 The script or command that reads the console
184 If you use ttywatch server, something like the following would work.
185 CONSOLE = nc -d localhost 3001
187 For a virtual machine with guest name "Guest".
188 CONSOLE = virsh console Guest
191 $config_help{"LOCALVERSION"} = << "EOF"
192 Required version ending to differentiate the test
193 from other linux builds on the system.
196 $config_help{"REBOOT_TYPE"} = << "EOF"
197 Way to reboot the box to the test kernel.
198 Only valid options so far are "grub" and "script".
200 If you specify grub, it will assume grub version 1
201 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
202 and select that target to reboot to the kernel. If this is not
203 your setup, then specify "script" and have a command or script
204 specified in REBOOT_SCRIPT to boot to the target.
206 The entry in /boot/grub/menu.lst must be entered in manually.
207 The test will not modify that file.
210 $config_help{"GRUB_MENU"} = << "EOF"
211 The grub title name for the test kernel to boot
212 (Only mandatory if REBOOT_TYPE = grub)
214 Note, ktest.pl will not update the grub menu.lst, you need to
215 manually add an option for the test. ktest.pl will search
216 the grub menu.lst for this option to find what kernel to
219 For example, if in the /boot/grub/menu.lst the test kernel title has:
222 GRUB_MENU = Test Kernel
225 $config_help{"REBOOT_SCRIPT"} = << "EOF"
226 A script to reboot the target into the test kernel
227 (Only mandatory if REBOOT_TYPE = script)
237 print "$prompt [Y/n] ";
240 if ($ans =~ /^\s*$/) {
243 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
244 print "Please answer either 'y' or 'n'.\n";
246 if ($ans !~ /^y$/i) {
252 sub get_ktest_config {
256 return if (defined($opt{$config}));
258 if (defined($config_help{$config})) {
260 print $config_help{$config};
265 if (defined($default{$config})) {
266 print "\[$default{$config}\] ";
269 $ans =~ s/^\s*(.*\S)\s*$/$1/;
270 if ($ans =~ /^\s*$/) {
271 if ($default{$config}) {
272 $ans = $default{$config};
274 print "Your answer can not be blank\n";
278 $entered_configs{$config} = process_variables($ans);
283 sub get_ktest_configs {
284 get_ktest_config("MACHINE");
285 get_ktest_config("SSH_USER");
286 get_ktest_config("BUILD_DIR");
287 get_ktest_config("OUTPUT_DIR");
288 get_ktest_config("BUILD_TARGET");
289 get_ktest_config("TARGET_IMAGE");
290 get_ktest_config("POWER_CYCLE");
291 get_ktest_config("CONSOLE");
292 get_ktest_config("LOCALVERSION");
294 my $rtype = $opt{"REBOOT_TYPE"};
296 if (!defined($rtype)) {
297 if (!defined($opt{"GRUB_MENU"})) {
298 get_ktest_config("REBOOT_TYPE");
299 $rtype = $entered_configs{"REBOOT_TYPE"};
305 if ($rtype eq "grub") {
306 get_ktest_config("GRUB_MENU");
308 get_ktest_config("REBOOT_SCRIPT");
312 sub process_variables {
313 my ($value, $remove_undef) = @_;
316 # We want to check for '\', and it is just easier
317 # to check the previous characet of '$' and not need
318 # to worry if '$' is the first character. By adding
319 # a space to $value, we can just check [^\\]\$ and
320 # it will still work.
323 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
327 # append beginning of value to retval
328 $retval = "$retval$begin";
329 if (defined($variable{$var})) {
330 $retval = "$retval$variable{$var}";
331 } elsif (defined($remove_undef) && $remove_undef) {
332 # for if statements, any variable that is not defined,
333 # we simple convert to 0
334 $retval = "${retval}0";
336 # put back the origin piece.
337 $retval = "$retval\$\{$var\}";
341 $retval = "$retval$value";
343 # remove the space added in the beginning
350 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
352 if (defined($opt{$lvalue})) {
353 if (!$override || defined(${$overrides}{$lvalue})) {
356 $extra = "In the same override section!\n";
358 die "$name: $.: Option $lvalue defined more than once!\n$extra";
360 ${$overrides}{$lvalue} = $rvalue;
362 if ($rvalue =~ /^\s*$/) {
363 delete $opt{$lvalue};
365 $rvalue = process_variables($rvalue);
366 $opt{$lvalue} = $rvalue;
371 my ($lvalue, $rvalue) = @_;
373 if ($rvalue =~ /^\s*$/) {
374 delete $variable{$lvalue};
376 $rvalue = process_variables($rvalue);
377 $variable{$lvalue} = $rvalue;
381 sub process_compare {
382 my ($lval, $cmp, $rval) = @_;
393 return $lval eq $rval;
394 } elsif ($cmp eq "!=") {
395 return $lval ne $rval;
398 my $statement = "$lval $cmp $rval";
399 my $ret = eval $statement;
401 # $@ stores error of eval
412 return defined($variable{$2}) ||
417 sub process_expression {
418 my ($name, $val) = @_;
422 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
425 if (process_expression($name, $express)) {
426 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
428 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
436 while ($val =~ s/^(.*?)($OR|$AND)//) {
440 if (process_expression($name, $express)) {
451 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
452 my $ret = process_compare($1, $2, $3);
454 die "$name: $.: Unable to process comparison\n";
459 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
461 return !value_defined($2);
463 return value_defined($2);
467 if ($val =~ /^\s*0\s*$/) {
469 } elsif ($val =~ /^\s*\d+\s*$/) {
473 die ("$name: $.: Undefined content $val in if statement\n");
477 my ($name, $value) = @_;
479 # Convert variables and replace undefined ones with 0
480 my $val = process_variables($value, 1);
481 my $ret = process_expression $name, $val;
487 my ($config, $current_test_num) = @_;
490 open($in, $config) || die "can't read file $config";
493 $name =~ s,.*/(.*),$1,;
495 my $test_num = $$current_test_num;
498 my $num_tests_set = 0;
511 # ignore blank lines and comments
512 next if (/^\s*$/ || /\s*\#/);
514 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
524 if ($type eq "TEST_START") {
526 if ($num_tests_set) {
527 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
530 $old_test_num = $test_num;
531 $old_repeat = $repeat;
533 $test_num += $repeat;
540 # If SKIP is anywhere in the line, the command will be skipped
541 if ($rest =~ s/\s+SKIP\b//) {
548 if ($rest =~ s/\sELSE\b//) {
550 die "$name: $.: ELSE found with out matching IF section\n$_";
561 if ($rest =~ s/\sIF\s+(.*)//) {
562 if (process_if($name, $1)) {
574 if ($type eq "TEST_START") {
575 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
577 $repeat_tests{"$test_num"} = $repeat;
579 } elsif ($rest =~ s/\sOVERRIDE\b//) {
582 # Clear previous overrides
587 if (!$skip && $rest !~ /^\s*$/) {
588 die "$name: $.: Gargbage found after $type\n$_";
591 if ($skip && $type eq "TEST_START") {
592 $test_num = $old_test_num;
593 $repeat = $old_repeat;
596 } elsif (/^\s*ELSE\b(.*)$/) {
598 die "$name: $.: ELSE found with out matching IF section\n$_";
607 if ($rest =~ /\sIF\s+(.*)/) {
608 # May be a ELSE IF section.
609 if (!process_if($name, $1)) {
618 if ($rest !~ /^\s*$/) {
619 die "$name: $.: Gargbage found after DEFAULTS\n$_";
622 } elsif (/^\s*INCLUDE\s+(\S+)/) {
627 die "$name: $.: INCLUDE can only be done in default sections\n$_";
630 my $file = process_variables($1);
632 if ($file !~ m,^/,) {
633 # check the path of the config file first
634 if ($config =~ m,(.*)/,) {
642 die "$name: $.: Can't read file $file\n$_";
645 if (__read_config($file, \$test_num)) {
649 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
657 ($lvalue eq "NUM_TESTS" ||
658 $lvalue eq "LOG_FILE" ||
659 $lvalue eq "CLEAR_LOG")) {
660 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
663 if ($lvalue eq "NUM_TESTS") {
665 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
668 die "$name: $.: NUM_TESTS must be set in default section\n";
673 if ($default || $lvalue =~ /\[\d+\]$/) {
674 set_value($lvalue, $rvalue, $override, \%overrides, $name);
676 my $val = "$lvalue\[$test_num\]";
677 set_value($val, $rvalue, $override, \%overrides, $name);
680 $repeats{$val} = $repeat;
683 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
689 # process config variables.
690 # Config variables are only active while reading the
691 # config and can be defined anywhere. They also ignore
692 # TEST_START and DEFAULTS, but are skipped if they are in
693 # on of these sections that have SKIP defined.
694 # The save variable can be
695 # defined multiple times and the new one simply overrides
697 set_variable($lvalue, $rvalue);
700 die "$name: $.: Garbage found in config\n$_";
705 $test_num += $repeat - 1;
706 $opt{"NUM_TESTS"} = $test_num;
711 $$current_test_num = $test_num;
722 $test_case = __read_config $config, \$test_num;
724 # make sure we have all mandatory configs
727 # was a test specified?
729 print "No test case specified.\n";
730 print "What test case would you like to run?\n";
733 $default{"TEST_TYPE"} = $ans;
738 foreach my $default (keys %default) {
739 if (!defined($opt{$default})) {
740 $opt{$default} = $default{$default};
746 my ($option, $i) = @_;
748 # Add space to evaluate the character before $
749 $option = " $option";
754 foreach my $test (keys %repeat_tests) {
756 $i < $test + $repeat_tests{$test}) {
764 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
769 # Append beginning of line
770 $retval = "$retval$start";
772 # If the iteration option OPT[$i] exists, then use that.
773 # otherwise see if the default OPT (without [$i]) exists.
775 my $o = "$var\[$i\]";
776 my $parento = "$var\[$parent\]";
778 if (defined($opt{$o})) {
780 $retval = "$retval$o";
781 } elsif ($repeated && defined($opt{$parento})) {
783 $retval = "$retval$o";
784 } elsif (defined($opt{$var})) {
786 $retval = "$retval$o";
788 $retval = "$retval\$\{$var\}";
794 $retval = "$retval$option";
802 my ($option, $i) = @_;
806 # Since an option can evaluate to another option,
807 # keep iterating until we do not evaluate any more
810 while ($prev ne $option) {
811 # Check for recursive evaluations.
812 # 100 deep should be more than enough.
814 die "Over 100 evaluations accurred with $option\n" .
815 "Check for recursive variables\n";
818 $option = __eval_option($option, $i);
825 if (defined($opt{"LOG_FILE"})) {
826 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
833 if (defined($opt{"LOG_FILE"})) {
848 sub wait_for_monitor;
853 if (defined($time)) {
855 # flush out current monitor
856 # May contain the reboot success line
860 # try to reboot normally
861 if (run_command $reboot) {
862 if (defined($powercycle_after_reboot)) {
863 sleep $powercycle_after_reboot;
864 run_command "$power_cycle";
867 # nope? power cycle it.
868 run_command "$power_cycle";
871 if (defined($time)) {
872 wait_for_monitor($time, $reboot_success_line);
880 return $test_type eq "build" || $no_reboot ||
881 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
882 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
886 doprint "CRITICAL FAILURE... ", @_, "\n";
890 if ($reboot_on_error && !do_not_reboot) {
892 doprint "REBOOTING\n";
895 } elsif ($poweroff_on_error && defined($power_off)) {
896 doprint "POWERING OFF\n";
900 if (defined($opt{"LOG_FILE"})) {
901 print " See $opt{LOG_FILE} for more info.\n";
912 my $pid = open($fp, "$console|") or
913 dodie "Can't open console $console";
915 $flags = fcntl($fp, F_GETFL, 0) or
916 dodie "Can't get flags for the socket: $!";
917 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
918 dodie "Can't set flags for the socket: $!";
926 doprint "kill child process $pid\n";
934 if ($monitor_cnt++) {
937 $monitor_fp = \*MONFD;
938 $monitor_pid = open_console $monitor_fp;
942 open(MONFD, "Stop perl from warning about single use of MONFD");
946 if (--$monitor_cnt) {
949 close_console($monitor_fp, $monitor_pid);
952 sub wait_for_monitor {
953 my ($time, $stop) = @_;
958 doprint "** Wait for monitor to settle down **\n";
960 # read the monitor and wait for the system to calm down
962 $line = wait_for_input($monitor_fp, $time);
963 last if (!defined($line));
967 if (defined($stop) && $full_line =~ /$stop/) {
968 doprint "wait for monitor detected $stop\n";
976 print "** Monitor flushed **\n";
981 if ($die_on_failure) {
989 # no need to reboot for just building.
990 if (!do_not_reboot) {
991 doprint "REBOOTING\n";
997 if (defined($test_name)) {
998 $name = " ($test_name)";
1001 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1002 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1003 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
1004 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1005 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1007 return 1 if (!defined($store_failures));
1010 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1011 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1013 my $type = $build_type;
1014 if ($type =~ /useconfig/) {
1015 $type = "useconfig";
1018 my $dir = "$machine-$test_type-$type-fail-$date";
1019 my $faildir = "$store_failures/$dir";
1023 die "can't create $faildir";
1027 "config" => $output_config,
1028 "buildlog" => $buildlog,
1030 "testlog" => $testlog,
1033 while (my ($name, $source) = each(%files)) {
1035 cp "$source", "$faildir/$name" or
1036 die "failed to copy $source";
1040 doprint "*** Saved info to $faildir ***\n";
1051 $command =~ s/\$SSH_USER/$ssh_user/g;
1052 $command =~ s/\$MACHINE/$machine/g;
1054 doprint("$command ... ");
1056 $pid = open(CMD, "$command 2>&1 |") or
1057 (fail "unable to exec $command" and return 0);
1059 if (defined($opt{"LOG_FILE"})) {
1060 open(LOG, ">>$opt{LOG_FILE}") or
1061 dodie "failed to write to log";
1065 if (defined($redirect)) {
1066 open (RD, ">$redirect") or
1067 dodie "failed to write to redirect $redirect";
1072 print LOG if ($dolog);
1073 print RD if ($dord);
1080 close(LOG) if ($dolog);
1081 close(RD) if ($dord);
1084 doprint "FAILED!\n";
1086 doprint "SUCCESS\n";
1094 my $cp_exec = $ssh_exec;
1096 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1097 return run_command "$cp_exec";
1101 my ($src, $dst) = @_;
1102 my $cp_scp = $scp_to_target;
1104 $cp_scp =~ s/\$SRC_FILE/$src/g;
1105 $cp_scp =~ s/\$DST_FILE/$dst/g;
1107 return run_command "$cp_scp";
1110 sub get_grub_index {
1112 if ($reboot_type ne "grub") {
1115 return if (defined($grub_number));
1117 doprint "Find grub menu ... ";
1120 my $ssh_grub = $ssh_exec;
1121 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1123 open(IN, "$ssh_grub |")
1124 or die "unable to get menu.lst";
1129 if (/^\s*title\s+$grub_menu\s*$/) {
1133 } elsif (/^\s*title\s/) {
1139 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
1141 doprint "$grub_number\n";
1146 my ($fp, $time) = @_;
1152 if (!defined($time)) {
1157 vec($rin, fileno($fp), 1) = 1;
1158 $ready = select($rin, undef, undef, $time);
1162 # try to read one char at a time
1163 while (sysread $fp, $ch, 1) {
1165 last if ($ch eq "\n");
1168 if (!length($line)) {
1176 if ($reboot_type eq "grub") {
1177 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1182 run_command "$reboot_script";
1188 doprint "git rev-list --max-count=1 $commit ... ";
1189 my $sha1 = `git rev-list --max-count=1 $commit`;
1196 dodie "Failed to get git $commit";
1209 my $skip_call_trace = 0;
1217 open(DMESG, "> $dmesg") or
1218 die "unable to write to $dmesg";
1224 my $monitor_start = time;
1226 my $version_found = 0;
1230 if ($bug && defined($stop_after_failure) &&
1231 $stop_after_failure >= 0) {
1232 my $time = $stop_after_failure - (time - $failure_start);
1233 $line = wait_for_input($monitor_fp, $time);
1234 if (!defined($line)) {
1235 doprint "bug timed out after $booted_timeout seconds\n";
1236 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1240 $line = wait_for_input($monitor_fp, $booted_timeout);
1241 if (!defined($line)) {
1242 my $s = $booted_timeout == 1 ? "" : "s";
1243 doprint "Successful boot found: break after $booted_timeout second$s\n";
1247 $line = wait_for_input($monitor_fp);
1248 if (!defined($line)) {
1249 my $s = $timeout == 1 ? "" : "s";
1250 doprint "Timed out after $timeout second$s\n";
1258 # we are not guaranteed to get a full line
1259 $full_line .= $line;
1261 if ($full_line =~ /$success_line/) {
1263 $success_start = time;
1266 if ($booted && defined($stop_after_success) &&
1267 $stop_after_success >= 0) {
1269 if ($now - $success_start >= $stop_after_success) {
1270 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1275 if ($full_line =~ /\[ backtrace testing \]/) {
1276 $skip_call_trace = 1;
1279 if ($full_line =~ /call trace:/i) {
1280 if (!$bug && !$skip_call_trace) {
1282 $failure_start = time;
1286 if ($bug && defined($stop_after_failure) &&
1287 $stop_after_failure >= 0) {
1289 if ($now - $failure_start >= $stop_after_failure) {
1290 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1295 if ($full_line =~ /\[ end of backtrace testing \]/) {
1296 $skip_call_trace = 0;
1299 if ($full_line =~ /Kernel panic -/) {
1300 $failure_start = time;
1304 # Detect triple faults by testing the banner
1305 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1306 if ($1 eq $version) {
1308 } elsif ($version_found && $detect_triplefault) {
1309 # We already booted into the kernel we are testing,
1310 # but now we booted into another kernel?
1311 # Consider this a triple fault.
1312 doprint "Aleady booted in Linux kernel $version, but now\n";
1313 doprint "we booted into Linux kernel $1.\n";
1314 doprint "Assuming that this is a triple fault.\n";
1315 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1320 if ($line =~ /\n/) {
1324 if ($stop_test_after > 0 && !$booted && !$bug) {
1325 if (time - $monitor_start > $stop_test_after) {
1326 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
1335 return 0 if ($in_bisect);
1336 fail "failed - got a bug report" and return 0;
1340 return 0 if ($in_bisect);
1341 fail "failed - never got a boot prompt." and return 0;
1347 sub do_post_install {
1349 return if (!defined($post_install));
1351 my $cp_post_install = $post_install;
1352 $cp_post_install =~ s/\$KERNEL_VERSION/$version/g;
1353 run_command "$cp_post_install" or
1354 dodie "Failed to run post install";
1359 return if ($no_install);
1361 run_scp "$outputdir/$build_target", "$target_image" or
1362 dodie "failed to copy image";
1364 my $install_mods = 0;
1366 # should we process modules?
1368 open(IN, "$output_config") or dodie("Can't read config file");
1370 if (/CONFIG_MODULES(=y)?/) {
1371 $install_mods = 1 if (defined($1));
1377 if (!$install_mods) {
1379 doprint "No modules needed\n";
1383 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
1384 dodie "Failed to install modules";
1386 my $modlib = "/lib/modules/$version";
1387 my $modtar = "ktest-mods.tar.bz2";
1389 run_ssh "rm -rf $modlib" or
1390 dodie "failed to remove old mods: $modlib";
1392 # would be nice if scp -r did not follow symbolic links
1393 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
1394 dodie "making tarball";
1396 run_scp "$tmpdir/$modtar", "/tmp" or
1397 dodie "failed to copy modules";
1399 unlink "$tmpdir/$modtar";
1401 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
1402 dodie "failed to tar modules";
1404 run_ssh "rm -f /tmp/$modtar";
1410 # get the release name
1411 doprint "$make kernelrelease ... ";
1412 $version = `$make kernelrelease | tail -1`;
1414 doprint "$version\n";
1417 sub start_monitor_and_boot {
1418 # Make sure the stable kernel has finished booting
1431 sub check_buildlog {
1434 my @files = `git show $patch | diffstat -l`;
1436 open(IN, "git show $patch |") or
1437 dodie "failed to show $patch";
1439 if (m,^--- a/(.*),) {
1441 $files[$#files] = $1;
1446 open(IN, $buildlog) or dodie "Can't open $buildlog";
1448 if (/^\s*(.*?):.*(warning|error)/) {
1450 foreach my $file (@files) {
1451 my $fullpath = "$builddir/$file";
1452 if ($file eq $err || $fullpath eq $err) {
1453 fail "$file built with warnings" and return 0;
1463 sub apply_min_config {
1464 my $outconfig = "$output_config.new";
1466 # Read the config file and remove anything that
1467 # is in the force_config hash (from minconfig and others)
1468 # then add the force config back.
1470 doprint "Applying minimum configurations into $output_config.new\n";
1472 open (OUT, ">$outconfig") or
1473 dodie "Can't create $outconfig";
1475 if (-f $output_config) {
1476 open (IN, $output_config) or
1477 dodie "Failed to open $output_config";
1479 if (/^(# )?(CONFIG_[^\s=]*)/) {
1480 next if (defined($force_config{$2}));
1486 foreach my $config (keys %force_config) {
1487 print OUT "$force_config{$config}\n";
1491 run_command "mv $outconfig $output_config";
1494 sub make_oldconfig {
1496 my @force_list = keys %force_config;
1498 if ($#force_list >= 0) {
1502 if (!run_command "$make oldnoconfig") {
1503 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1504 # try a yes '' | oldconfig
1505 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
1506 run_command "yes '' | $make oldconfig" or
1507 dodie "failed make config oldconfig";
1511 # read a config file and use this to force new configs.
1512 sub load_force_config {
1515 open(IN, $config) or
1516 dodie "failed to read $config";
1519 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1520 $force_config{$1} = $_;
1521 } elsif (/^# (CONFIG_\S*) is not set/) {
1522 $force_config{$1} = $_;
1533 # Failed builds should not reboot the target
1534 my $save_no_reboot = $no_reboot;
1537 if (defined($pre_build)) {
1538 my $ret = run_command $pre_build;
1539 if (!$ret && defined($pre_build_die) &&
1541 dodie "failed to pre_build\n";
1545 if ($type =~ /^useconfig:(.*)/) {
1546 run_command "cp $1 $output_config" or
1547 dodie "could not copy $1 to .config";
1549 $type = "oldconfig";
1552 # old config can ask questions
1553 if ($type eq "oldconfig") {
1554 $type = "oldnoconfig";
1556 # allow for empty configs
1557 run_command "touch $output_config";
1560 run_command "mv $output_config $outputdir/config_temp" or
1561 dodie "moving .config";
1563 run_command "$make mrproper" or dodie "make mrproper";
1565 run_command "mv $outputdir/config_temp $output_config" or
1566 dodie "moving config_temp";
1569 } elsif (!$noclean) {
1570 unlink "$output_config";
1571 run_command "$make mrproper" or
1572 dodie "make mrproper";
1575 # add something to distinguish this build
1576 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1577 print OUT "$localversion\n";
1580 if (defined($minconfig)) {
1581 load_force_config($minconfig);
1584 if ($type ne "oldnoconfig") {
1585 run_command "$make $type" or
1586 dodie "failed make config";
1588 # Run old config regardless, to enforce min configurations
1591 $redirect = "$buildlog";
1592 my $build_ret = run_command "$make $build_options";
1595 if (defined($post_build)) {
1596 my $ret = run_command $post_build;
1597 if (!$ret && defined($post_build_die) &&
1599 dodie "failed to post_build\n";
1604 # bisect may need this to pass
1606 $no_reboot = $save_no_reboot;
1609 fail "failed build" and return 0;
1612 $no_reboot = $save_no_reboot;
1618 if (!run_ssh "halt" or defined($power_off)) {
1619 if (defined($poweroff_after_halt)) {
1620 sleep $poweroff_after_halt;
1621 run_command "$power_off";
1625 run_command "$power_off";
1636 if (defined($test_name)) {
1637 $name = " ($test_name)";
1640 doprint "\n\n*******************************************\n";
1641 doprint "*******************************************\n";
1642 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
1643 doprint "*******************************************\n";
1644 doprint "*******************************************\n";
1646 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
1647 doprint "Reboot and wait $sleep_time seconds\n";
1654 doprint "Pass or fail? [p/f]";
1657 if ($ans eq "p" || $ans eq "P") {
1659 } elsif ($ans eq "f" || $ans eq "F") {
1662 print "Please answer 'P' or 'F'\n";
1667 sub child_run_test {
1670 # child should have no power
1671 $reboot_on_error = 0;
1672 $poweroff_on_error = 0;
1673 $die_on_failure = 1;
1675 $redirect = "$testlog";
1676 run_command $run_test or $failed = 1;
1684 sub child_finished {
1697 doprint "run test $run_test\n";
1701 $SIG{CHLD} = qw(child_finished);
1705 child_run_test if (!$child_pid);
1710 $line = wait_for_input($monitor_fp, 1);
1711 if (defined($line)) {
1713 # we are not guaranteed to get a full line
1714 $full_line .= $line;
1717 if ($full_line =~ /call trace:/i) {
1721 if ($full_line =~ /Kernel panic -/) {
1725 if ($line =~ /\n/) {
1729 } while (!$child_done && !$bug);
1732 my $failure_start = time;
1735 $line = wait_for_input($monitor_fp, 1);
1736 if (defined($line)) {
1740 if ($now - $failure_start >= $stop_after_failure) {
1743 } while (defined($line));
1745 doprint "Detected kernel crash!\n";
1746 # kill the child with extreme prejudice
1750 waitpid $child_pid, 0;
1753 if ($bug || $child_exit) {
1754 return 0 if $in_bisect;
1755 fail "test failed" and return 0;
1760 sub run_git_bisect {
1763 doprint "$command ... ";
1765 my $output = `$command 2>&1`;
1772 dodie "Failed to git bisect";
1775 doprint "SUCCESS\n";
1776 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1777 doprint "$1 [$2]\n";
1778 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
1780 doprint "Found bad commit... $1\n";
1783 # we already logged it, just print it now.
1791 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
1792 reboot $bisect_sleep_time;
1795 # returns 1 on success, 0 on failure, -1 on skip
1796 sub run_bisect_test {
1797 my ($type, $buildtype) = @_;
1806 build $buildtype or $failed = 1;
1808 if ($type ne "build") {
1809 if ($failed && $bisect_skip) {
1813 dodie "Failed on build" if $failed;
1816 start_monitor_and_boot or $failed = 1;
1818 if ($type ne "boot") {
1819 if ($failed && $bisect_skip) {
1825 dodie "Failed on boot" if $failed;
1827 do_run_test or $failed = 1;
1838 # reboot the box to a kernel we can ssh to
1839 if ($type ne "build") {
1849 my $buildtype = "oldconfig";
1851 # We should have a minconfig to use?
1852 if (defined($minconfig)) {
1853 $buildtype = "useconfig:$minconfig";
1856 my $ret = run_bisect_test $type, $buildtype;
1858 if ($bisect_manual) {
1859 $ret = answer_bisect;
1862 # Are we looking for where it worked, not failed?
1863 if ($reverse_bisect) {
1869 } elsif ($ret == 0) {
1871 } elsif ($bisect_skip) {
1872 doprint "HIT A BAD COMMIT ... SKIPPING\n";
1882 die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
1883 die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
1884 die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
1886 my $good = $opt{"BISECT_GOOD[$i]"};
1887 my $bad = $opt{"BISECT_BAD[$i]"};
1888 my $type = $opt{"BISECT_TYPE[$i]"};
1889 my $start = $opt{"BISECT_START[$i]"};
1890 my $replay = $opt{"BISECT_REPLAY[$i]"};
1891 my $start_files = $opt{"BISECT_FILES[$i]"};
1893 if (defined($start_files)) {
1894 $start_files = " -- " . $start_files;
1899 # convert to true sha1's
1900 $good = get_sha1($good);
1901 $bad = get_sha1($bad);
1903 if (defined($opt{"BISECT_REVERSE[$i]"}) &&
1904 $opt{"BISECT_REVERSE[$i]"} == 1) {
1905 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
1906 $reverse_bisect = 1;
1908 $reverse_bisect = 0;
1911 # Can't have a test without having a test to run
1912 if ($type eq "test" && !defined($run_test)) {
1916 my $check = $opt{"BISECT_CHECK[$i]"};
1917 if (defined($check) && $check ne "0") {
1920 my $head = get_sha1("HEAD");
1922 if ($check ne "good") {
1923 doprint "TESTING BISECT BAD [$bad]\n";
1924 run_command "git checkout $bad" or
1925 die "Failed to checkout $bad";
1927 $result = run_bisect $type;
1929 if ($result ne "bad") {
1930 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
1934 if ($check ne "bad") {
1935 doprint "TESTING BISECT GOOD [$good]\n";
1936 run_command "git checkout $good" or
1937 die "Failed to checkout $good";
1939 $result = run_bisect $type;
1941 if ($result ne "good") {
1942 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
1946 # checkout where we started
1947 run_command "git checkout $head" or
1948 die "Failed to checkout $head";
1951 run_command "git bisect start$start_files" or
1952 dodie "could not start bisect";
1954 run_command "git bisect good $good" or
1955 dodie "could not set bisect good to $good";
1957 run_git_bisect "git bisect bad $bad" or
1958 dodie "could not set bisect bad to $bad";
1960 if (defined($replay)) {
1961 run_command "git bisect replay $replay" or
1962 dodie "failed to run replay";
1965 if (defined($start)) {
1966 run_command "git checkout $start" or
1967 dodie "failed to checkout $start";
1972 $result = run_bisect $type;
1973 $test = run_git_bisect "git bisect $result";
1976 run_command "git bisect log" or
1977 dodie "could not capture git bisect log";
1979 run_command "git bisect reset" or
1980 dodie "could not reset git bisect";
1982 doprint "Bad commit was [$bisect_bad]\n";
1995 sub assign_configs {
1996 my ($hash, $config) = @_;
1999 or dodie "Failed to read $config";
2002 if (/^((CONFIG\S*)=.*)/) {
2010 sub process_config_ignore {
2013 assign_configs \%config_ignore, $config;
2016 sub read_current_config {
2017 my ($config_ref) = @_;
2019 %{$config_ref} = ();
2020 undef %{$config_ref};
2022 my @key = keys %{$config_ref};
2024 print "did not delete!\n";
2027 open (IN, "$output_config");
2030 if (/^(CONFIG\S+)=(.*)/) {
2031 ${$config_ref}{$1} = $2;
2037 sub get_dependencies {
2040 my $arr = $dependency{$config};
2041 if (!defined($arr)) {
2047 foreach my $dep (@{$arr}) {
2048 print "ADD DEP $dep\n";
2049 @deps = (@deps, get_dependencies $dep);
2058 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2060 foreach my $config (@configs) {
2061 print OUT "$config_set{$config}\n";
2062 my @deps = get_dependencies $config;
2063 foreach my $dep (@deps) {
2064 print OUT "$config_set{$dep}\n";
2068 foreach my $config (keys %config_ignore) {
2069 print OUT "$config_ignore{$config}\n";
2077 sub compare_configs {
2080 foreach my $item (keys %a) {
2081 if (!defined($b{$item})) {
2082 print "diff $item\n";
2090 print "diff2 $keys[0]\n";
2092 return -1 if ($#keys >= 0);
2097 sub run_config_bisect_test {
2100 return run_bisect_test $type, "oldconfig";
2103 sub process_passed {
2106 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2107 # Passed! All these configs are part of a good compile.
2108 # Add them to the min options.
2109 foreach my $config (keys %configs) {
2110 if (defined($config_list{$config})) {
2111 doprint " removing $config\n";
2112 $config_ignore{$config} = $config_list{$config};
2113 delete $config_list{$config};
2116 doprint "config copied to $outputdir/config_good\n";
2117 run_command "cp -f $output_config $outputdir/config_good";
2120 sub process_failed {
2123 doprint "\n\n***************************************\n";
2124 doprint "Found bad config: $config\n";
2125 doprint "***************************************\n\n";
2128 sub run_config_bisect {
2130 my @start_list = keys %config_list;
2132 if ($#start_list < 0) {
2133 doprint "No more configs to test!!!\n";
2137 doprint "***** RUN TEST ***\n";
2138 my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
2142 my $count = $#start_list + 1;
2143 doprint " $count configs to test\n";
2145 my $half = int($#start_list / 2);
2148 my @tophalf = @start_list[0 .. $half];
2150 create_config @tophalf;
2151 read_current_config \%current_config;
2153 $count = $#tophalf + 1;
2154 doprint "Testing $count configs\n";
2156 # make sure we test something
2157 foreach my $config (@tophalf) {
2158 if (defined($current_config{$config})) {
2164 # try the other half
2165 doprint "Top half produced no set configs, trying bottom half\n";
2166 @tophalf = @start_list[$half + 1 .. $#start_list];
2167 create_config @tophalf;
2168 read_current_config \%current_config;
2169 foreach my $config (@tophalf) {
2170 if (defined($current_config{$config})) {
2176 doprint "Failed: Can't make new config with current configs\n";
2177 foreach my $config (@start_list) {
2178 doprint " CONFIG: $config\n";
2182 $count = $#tophalf + 1;
2183 doprint "Testing $count configs\n";
2186 $ret = run_config_bisect_test $type;
2187 if ($bisect_manual) {
2188 $ret = answer_bisect;
2191 process_passed %current_config;
2195 doprint "This config had a failure.\n";
2196 doprint "Removing these configs that were not set in this config:\n";
2197 doprint "config copied to $outputdir/config_bad\n";
2198 run_command "cp -f $output_config $outputdir/config_bad";
2200 # A config exists in this group that was bad.
2201 foreach my $config (keys %config_list) {
2202 if (!defined($current_config{$config})) {
2203 doprint " removing $config\n";
2204 delete $config_list{$config};
2208 @start_list = @tophalf;
2210 if ($#start_list == 0) {
2211 process_failed $start_list[0];
2215 # remove half the configs we are looking at and see if
2217 $half = int($#start_list / 2);
2218 } while ($#start_list > 0);
2220 # we found a single config, try it again unless we are running manually
2222 if ($bisect_manual) {
2223 process_failed $start_list[0];
2227 my @tophalf = @start_list[0 .. 0];
2229 $ret = run_config_bisect_test $type;
2231 process_passed %current_config;
2235 process_failed $start_list[0];
2242 my $start_config = $opt{"CONFIG_BISECT[$i]"};
2244 my $tmpconfig = "$tmpdir/use_config";
2246 if (defined($config_bisect_good)) {
2247 process_config_ignore $config_bisect_good;
2250 # Make the file with the bad config and the min config
2251 if (defined($minconfig)) {
2252 # read the min config for things to ignore
2253 run_command "cp $minconfig $tmpconfig" or
2254 dodie "failed to copy $minconfig to $tmpconfig";
2259 if (-f $tmpconfig) {
2260 load_force_config($tmpconfig);
2261 process_config_ignore $tmpconfig;
2264 # now process the start config
2265 run_command "cp $start_config $output_config" or
2266 dodie "failed to copy $start_config to $output_config";
2268 # read directly what we want to check
2270 open (IN, $output_config)
2271 or dodie "faied to open $output_config";
2274 if (/^((CONFIG\S*)=.*)/) {
2275 $config_check{$2} = $1;
2280 # Now run oldconfig with the minconfig
2283 # check to see what we lost (or gained)
2284 open (IN, $output_config)
2285 or dodie "Failed to read $start_config";
2287 my %removed_configs;
2291 if (/^((CONFIG\S*)=.*)/) {
2292 # save off all options
2293 $config_set{$2} = $1;
2294 if (defined($config_check{$2})) {
2295 if (defined($config_ignore{$2})) {
2296 $removed_configs{$2} = $1;
2298 $config_list{$2} = $1;
2300 } elsif (!defined($config_ignore{$2})) {
2301 $added_configs{$2} = $1;
2302 $config_list{$2} = $1;
2308 my @confs = keys %removed_configs;
2310 doprint "Configs overridden by default configs and removed from check:\n";
2311 foreach my $config (@confs) {
2312 doprint " $config\n";
2315 @confs = keys %added_configs;
2317 doprint "Configs appearing in make oldconfig and added:\n";
2318 foreach my $config (@confs) {
2319 doprint " $config\n";
2326 # Sometimes kconfig does weird things. We must make sure
2327 # that the config we autocreate has everything we need
2328 # to test, otherwise we may miss testing configs, or
2329 # may not be able to create a new config.
2330 # Here we create a config with everything set.
2331 create_config (keys %config_list);
2332 read_current_config \%config_test;
2333 foreach my $config (keys %config_list) {
2334 if (!defined($config_test{$config})) {
2337 doprint "Configs not produced by kconfig (will not be checked):\n";
2339 doprint " $config\n";
2340 delete $config_list{$config};
2345 $ret = run_config_bisect;
2348 return $ret if ($ret < 0);
2353 sub patchcheck_reboot {
2354 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
2355 reboot $patchcheck_sleep_time;
2361 die "PATCHCHECK_START[$i] not defined\n"
2362 if (!defined($opt{"PATCHCHECK_START[$i]"}));
2363 die "PATCHCHECK_TYPE[$i] not defined\n"
2364 if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
2366 my $start = $opt{"PATCHCHECK_START[$i]"};
2369 if (defined($opt{"PATCHCHECK_END[$i]"})) {
2370 $end = $opt{"PATCHCHECK_END[$i]"};
2373 # Get the true sha1's since we can use things like HEAD~3
2374 $start = get_sha1($start);
2375 $end = get_sha1($end);
2377 my $type = $opt{"PATCHCHECK_TYPE[$i]"};
2379 # Can't have a test without having a test to run
2380 if ($type eq "test" && !defined($run_test)) {
2384 open (IN, "git log --pretty=oneline $end|") or
2385 dodie "could not get git list";
2391 $list[$#list+1] = $_;
2392 last if (/^$start/);
2396 if ($list[$#list] !~ /^$start/) {
2397 fail "SHA1 $start not found";
2400 # go backwards in the list
2401 @list = reverse @list;
2403 my $save_clean = $noclean;
2404 my %ignored_warnings;
2406 if (defined($ignore_warnings)) {
2407 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2408 $ignored_warnings{$sha1} = 1;
2413 foreach my $item (@list) {
2415 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2417 doprint "\nProcessing commit $item\n\n";
2419 run_command "git checkout $sha1" or
2420 die "Failed to checkout $sha1";
2422 # only clean on the first and last patch
2423 if ($item eq $list[0] ||
2424 $item eq $list[$#list]) {
2425 $noclean = $save_clean;
2430 if (defined($minconfig)) {
2431 build "useconfig:$minconfig" or return 0;
2433 # ?? no config to use?
2434 build "oldconfig" or return 0;
2438 if (!defined($ignored_warnings{$sha1})) {
2439 check_buildlog $sha1 or return 0;
2442 next if ($type eq "build");
2446 start_monitor_and_boot or $failed = 1;
2448 if (!$failed && $type ne "boot"){
2449 do_run_test or $failed = 1;
2452 return 0 if ($failed);
2472 # $config depends on $dep
2473 my ($config, $dep) = @_;
2475 if (defined($depends{$config})) {
2476 $depends{$config} .= " " . $dep;
2478 $depends{$config} = $dep;
2481 # record the number of configs depending on $dep
2482 if (defined $depcount{$dep}) {
2485 $depcount{$dep} = 1;
2489 # taken from streamline_config.pl
2501 if (! -f $kconfig) {
2502 doprint "file $kconfig does not exist, skipping\n";
2506 open(KIN, "$kconfig")
2507 or die "Can't open $kconfig";
2511 # Make sure that lines ending with \ continue
2513 $_ = $line . " " . $_;
2524 # collect any Kconfig sources
2525 if (/^source\s*"(.*)"/) {
2526 $kconfigs[$#kconfigs+1] = $1;
2530 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2534 for (my $i = 0; $i < $iflevel; $i++) {
2535 add_dep $config, $ifdeps[$i];
2538 # collect the depends for the config
2539 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2541 add_dep $config, $1;
2543 # Get the configs that select this config
2544 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2546 # selected by depends on config
2547 add_dep $1, $config;
2549 # Check for if statements
2550 } elsif (/^if\s+(.*\S)\s*$/) {
2552 # remove beginning and ending non text
2553 $deps =~ s/^[^a-zA-Z0-9_]*//;
2554 $deps =~ s/[^a-zA-Z0-9_]*$//;
2556 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2558 $ifdeps[$iflevel++] = join ':', @deps;
2560 } elsif (/^endif/) {
2562 $iflevel-- if ($iflevel);
2565 } elsif (/^\s*help\s*$/) {
2571 # read in any configs that were found.
2572 foreach $kconfig (@kconfigs) {
2573 if (!defined($read_kconfigs{$kconfig})) {
2574 $read_kconfigs{$kconfig} = 1;
2575 read_kconfig("$builddir/$kconfig");
2581 # find out which arch this is by the kconfig file
2582 open (IN, $output_config)
2583 or dodie "Failed to read $output_config";
2586 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2593 if (!defined($arch)) {
2594 doprint "Could not find arch from config file\n";
2595 doprint "no dependencies used\n";
2599 # arch is really the subarch, we need to know
2600 # what directory to look at.
2601 if ($arch eq "i386" || $arch eq "x86_64") {
2603 } elsif ($arch =~ /^tile/) {
2607 my $kconfig = "$builddir/arch/$arch/Kconfig";
2609 if (! -f $kconfig && $arch =~ /\d$/) {
2611 # some subarchs have numbers, truncate them
2613 $kconfig = "$builddir/arch/$arch/Kconfig";
2614 if (! -f $kconfig) {
2615 doprint "No idea what arch dir $orig is for\n";
2616 doprint "no dependencies used\n";
2621 read_kconfig($kconfig);
2624 sub read_config_list {
2628 or dodie "Failed to read $config";
2631 if (/^((CONFIG\S*)=.*)/) {
2632 if (!defined($config_ignore{$2})) {
2633 $config_list{$2} = $1;
2641 sub read_output_config {
2644 assign_configs \%config_ignore, $config;
2647 sub make_new_config {
2650 open (OUT, ">$output_config")
2651 or dodie "Failed to write $output_config";
2653 foreach my $config (@configs) {
2654 print OUT "$config\n";
2662 $config =~ s/CONFIG_//;
2670 my $kconfig = chomp_config $dep;
2672 $dep = $depends{"$kconfig"};
2674 # the dep string we have saves the dependencies as they
2675 # were found, including expressions like ! && ||. We
2676 # want to split this out into just an array of configs.
2678 my $valid = "A-Za-z_0-9";
2682 while ($dep =~ /[$valid]/) {
2684 if ($dep =~ /^[^$valid]*([$valid]+)/) {
2685 my $conf = "CONFIG_" . $1;
2687 $configs[$#configs + 1] = $conf;
2689 $dep =~ s/^[^$valid]*[$valid]+//;
2691 die "this should never happen";
2701 my %processed_configs;
2702 my %nochange_config;
2704 sub test_this_config {
2709 # if we already processed this config, skip it
2710 if (defined($processed_configs{$config})) {
2713 $processed_configs{$config} = 1;
2715 # if this config failed during this round, skip it
2716 if (defined($nochange_config{$config})) {
2720 my $kconfig = chomp_config $config;
2722 # Test dependencies first
2723 if (defined($depends{"$kconfig"})) {
2724 my @parents = get_depends $config;
2725 foreach my $parent (@parents) {
2726 # if the parent is in the min config, check it first
2727 next if (!defined($min_configs{$parent}));
2728 $found = test_this_config($parent);
2729 if (defined($found)) {
2735 # Remove this config from the list of configs
2736 # do a make oldnoconfig and then read the resulting
2737 # .config to make sure it is missing the config that
2739 my %configs = %min_configs;
2740 delete $configs{$config};
2741 make_new_config ((values %configs), (values %keep_configs));
2744 assign_configs \%configs, $output_config;
2746 return $config if (!defined($configs{$config}));
2748 doprint "disabling config $config did not change .config\n";
2750 $nochange_config{$config} = 1;
2755 sub make_min_config {
2758 if (!defined($output_minconfig)) {
2759 fail "OUTPUT_MIN_CONFIG not defined" and return;
2762 # If output_minconfig exists, and the start_minconfig
2763 # came from min_config, than ask if we should use
2765 if (-f $output_minconfig && !$start_minconfig_defined) {
2766 print "$output_minconfig exists\n";
2767 if (read_yn " Use it as minconfig?") {
2768 $start_minconfig = $output_minconfig;
2772 if (!defined($start_minconfig)) {
2773 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
2776 my $temp_config = "$tmpdir/temp_config";
2778 # First things first. We build an allnoconfig to find
2779 # out what the defaults are that we can't touch.
2780 # Some are selections, but we really can't handle selections.
2782 my $save_minconfig = $minconfig;
2785 run_command "$make allnoconfig" or return 0;
2789 process_config_ignore $output_config;
2791 undef %save_configs;
2794 if (defined($ignore_config)) {
2795 # make sure the file exists
2796 `touch $ignore_config`;
2797 assign_configs \%save_configs, $ignore_config;
2800 %keep_configs = %save_configs;
2802 doprint "Load initial configs from $start_minconfig\n";
2804 # Look at the current min configs, and save off all the
2805 # ones that were set via the allnoconfig
2806 assign_configs \%min_configs, $start_minconfig;
2808 my @config_keys = keys %min_configs;
2810 # All configs need a depcount
2811 foreach my $config (@config_keys) {
2812 my $kconfig = chomp_config $config;
2813 if (!defined $depcount{$kconfig}) {
2814 $depcount{$kconfig} = 0;
2818 # Remove anything that was set by the make allnoconfig
2819 # we shouldn't need them as they get set for us anyway.
2820 foreach my $config (@config_keys) {
2821 # Remove anything in the ignore_config
2822 if (defined($keep_configs{$config})) {
2823 my $file = $ignore_config;
2824 $file =~ s,.*/(.*?)$,$1,;
2825 doprint "$config set by $file ... ignored\n";
2826 delete $min_configs{$config};
2829 # But make sure the settings are the same. If a min config
2830 # sets a selection, we do not want to get rid of it if
2831 # it is not the same as what we have. Just move it into
2833 if (defined($config_ignore{$config})) {
2834 if ($config_ignore{$config} ne $min_configs{$config}) {
2835 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
2836 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
2837 $keep_configs{$config} = $min_configs{$config};
2839 doprint "$config set by allnoconfig ... ignored\n";
2841 delete $min_configs{$config};
2853 # Now disable each config one by one and do a make oldconfig
2854 # till we find a config that changes our list.
2856 my @test_configs = keys %min_configs;
2858 # Sort keys by who is most dependent on
2859 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
2862 # Put configs that did not modify the config at the end.
2864 for (my $i = 0; $i < $#test_configs; $i++) {
2865 if (!defined($nochange_config{$test_configs[0]})) {
2869 # This config didn't change the .config last time.
2870 # Place it at the end
2871 my $config = shift @test_configs;
2872 push @test_configs, $config;
2875 # if every test config has failed to modify the .config file
2876 # in the past, then reset and start over.
2878 undef %nochange_config;
2881 undef %processed_configs;
2883 foreach my $config (@test_configs) {
2885 $found = test_this_config $config;
2887 last if (defined($found));
2889 # oh well, try another config
2892 if (!defined($found)) {
2893 # we could have failed due to the nochange_config hash
2894 # reset and try again
2896 undef %nochange_config;
2900 doprint "No more configs found that we can disable\n";
2908 doprint "Test with $config disabled\n";
2910 # set in_bisect to keep build and monitor from dieing
2915 start_monitor_and_boot or $failed = 1;
2921 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
2922 # this config is needed, add it to the ignore list.
2923 $keep_configs{$config} = $min_configs{$config};
2924 $save_configs{$config} = $min_configs{$config};
2925 delete $min_configs{$config};
2927 # update new ignore configs
2928 if (defined($ignore_config)) {
2929 open (OUT, ">$temp_config")
2930 or die "Can't write to $temp_config";
2931 foreach my $config (keys %save_configs) {
2932 print OUT "$save_configs{$config}\n";
2935 run_command "mv $temp_config $ignore_config" or
2936 dodie "failed to copy update to $ignore_config";
2940 # We booted without this config, remove it from the minconfigs.
2941 doprint "$config is not needed, disabling\n";
2943 delete $min_configs{$config};
2945 # Also disable anything that is not enabled in this config
2947 assign_configs \%configs, $output_config;
2948 my @config_keys = keys %min_configs;
2949 foreach my $config (@config_keys) {
2950 if (!defined($configs{$config})) {
2951 doprint "$config is not set, disabling\n";
2952 delete $min_configs{$config};
2956 # Save off all the current mandidory configs
2957 open (OUT, ">$temp_config")
2958 or die "Can't write to $temp_config";
2959 foreach my $config (keys %keep_configs) {
2960 print OUT "$keep_configs{$config}\n";
2962 foreach my $config (keys %min_configs) {
2963 print OUT "$min_configs{$config}\n";
2967 run_command "mv $temp_config $output_minconfig" or
2968 dodie "failed to copy update to $output_minconfig";
2971 doprint "Reboot and wait $sleep_time seconds\n";
2979 $#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
2982 $ktest_config = $ARGV[0];
2983 if (! -f $ktest_config) {
2984 print "$ktest_config does not exist.\n";
2985 if (!read_yn "Create it?") {
2990 $ktest_config = "ktest.conf";
2993 if (! -f $ktest_config) {
2994 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
2996 # Generated by ktest.pl
2998 # Define each test with TEST_START
2999 # The config options below it will override the defaults
3007 read_config $ktest_config;
3009 if (defined($opt{"LOG_FILE"})) {
3010 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3013 # Append any configs entered in manually to the config file.
3014 my @new_configs = keys %entered_configs;
3015 if ($#new_configs >= 0) {
3016 print "\nAppending entered in configs to $ktest_config\n";
3017 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3018 foreach my $config (@new_configs) {
3019 print OUT "$config = $entered_configs{$config}\n";
3020 $opt{$config} = $entered_configs{$config};
3024 if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3025 unlink $opt{"LOG_FILE"};
3028 doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3030 for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3033 doprint "DEFAULT OPTIONS:\n";
3035 doprint "\nTEST $i OPTIONS";
3036 if (defined($repeat_tests{$i})) {
3037 $repeat = $repeat_tests{$i};
3038 doprint " ITERATE $repeat";
3043 foreach my $option (sort keys %opt) {
3045 if ($option =~ /\[(\d+)\]$/) {
3051 doprint "$option = $opt{$option}\n";
3055 sub __set_test_option {
3056 my ($name, $i) = @_;
3058 my $option = "$name\[$i\]";
3060 if (defined($opt{$option})) {
3061 return $opt{$option};
3064 foreach my $test (keys %repeat_tests) {
3066 $i < $test + $repeat_tests{$test}) {
3067 $option = "$name\[$test\]";
3068 if (defined($opt{$option})) {
3069 return $opt{$option};
3074 if (defined($opt{$name})) {
3081 sub set_test_option {
3082 my ($name, $i) = @_;
3084 my $option = __set_test_option($name, $i);
3085 return $option if (!defined($option));
3087 return eval_option($option, $i);
3090 # First we need to do is the builds
3091 for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
3093 # Do not reboot on failing test options
3098 my $makecmd = set_test_option("MAKE_CMD", $i);
3100 $machine = set_test_option("MACHINE", $i);
3101 $ssh_user = set_test_option("SSH_USER", $i);
3102 $tmpdir = set_test_option("TMP_DIR", $i);
3103 $outputdir = set_test_option("OUTPUT_DIR", $i);
3104 $builddir = set_test_option("BUILD_DIR", $i);
3105 $test_type = set_test_option("TEST_TYPE", $i);
3106 $build_type = set_test_option("BUILD_TYPE", $i);
3107 $build_options = set_test_option("BUILD_OPTIONS", $i);
3108 $pre_build = set_test_option("PRE_BUILD", $i);
3109 $post_build = set_test_option("POST_BUILD", $i);
3110 $pre_build_die = set_test_option("PRE_BUILD_DIE", $i);
3111 $post_build_die = set_test_option("POST_BUILD_DIE", $i);
3112 $power_cycle = set_test_option("POWER_CYCLE", $i);
3113 $reboot = set_test_option("REBOOT", $i);
3114 $noclean = set_test_option("BUILD_NOCLEAN", $i);
3115 $minconfig = set_test_option("MIN_CONFIG", $i);
3116 $output_minconfig = set_test_option("OUTPUT_MIN_CONFIG", $i);
3117 $start_minconfig = set_test_option("START_MIN_CONFIG", $i);
3118 $ignore_config = set_test_option("IGNORE_CONFIG", $i);
3119 $run_test = set_test_option("TEST", $i);
3120 $addconfig = set_test_option("ADD_CONFIG", $i);
3121 $reboot_type = set_test_option("REBOOT_TYPE", $i);
3122 $grub_menu = set_test_option("GRUB_MENU", $i);
3123 $post_install = set_test_option("POST_INSTALL", $i);
3124 $no_install = set_test_option("NO_INSTALL", $i);
3125 $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
3126 $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
3127 $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
3128 $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
3129 $power_off = set_test_option("POWER_OFF", $i);
3130 $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
3131 $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
3132 $sleep_time = set_test_option("SLEEP_TIME", $i);
3133 $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
3134 $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
3135 $ignore_warnings = set_test_option("IGNORE_WARNINGS", $i);
3136 $bisect_manual = set_test_option("BISECT_MANUAL", $i);
3137 $bisect_skip = set_test_option("BISECT_SKIP", $i);
3138 $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
3139 $store_failures = set_test_option("STORE_FAILURES", $i);
3140 $test_name = set_test_option("TEST_NAME", $i);
3141 $timeout = set_test_option("TIMEOUT", $i);
3142 $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
3143 $console = set_test_option("CONSOLE", $i);
3144 $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
3145 $success_line = set_test_option("SUCCESS_LINE", $i);
3146 $reboot_success_line = set_test_option("REBOOT_SUCCESS_LINE", $i);
3147 $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
3148 $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
3149 $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
3150 $build_target = set_test_option("BUILD_TARGET", $i);
3151 $ssh_exec = set_test_option("SSH_EXEC", $i);
3152 $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
3153 $target_image = set_test_option("TARGET_IMAGE", $i);
3154 $localversion = set_test_option("LOCALVERSION", $i);
3156 $start_minconfig_defined = 1;
3158 if (!defined($start_minconfig)) {
3159 $start_minconfig_defined = 0;
3160 $start_minconfig = $minconfig;
3163 chdir $builddir || die "can't change directory to $builddir";
3165 foreach my $dir ($tmpdir, $outputdir) {
3168 die "can't create $dir";
3172 $ENV{"SSH_USER"} = $ssh_user;
3173 $ENV{"MACHINE"} = $machine;
3175 $target = "$ssh_user\@$machine";
3177 $buildlog = "$tmpdir/buildlog-$machine";
3178 $testlog = "$tmpdir/testlog-$machine";
3179 $dmesg = "$tmpdir/dmesg-$machine";
3180 $make = "$makecmd O=$outputdir";
3181 $output_config = "$outputdir/.config";
3183 if ($reboot_type eq "grub") {
3184 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3185 } elsif (!defined($reboot_script)) {
3186 dodie "REBOOT_SCRIPT not defined"
3189 my $run_type = $build_type;
3190 if ($test_type eq "patchcheck") {
3191 $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
3192 } elsif ($test_type eq "bisect") {
3193 $run_type = $opt{"BISECT_TYPE[$i]"};
3194 } elsif ($test_type eq "config_bisect") {
3195 $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
3198 if ($test_type eq "make_min_config") {
3202 # mistake in config file?
3203 if (!defined($run_type)) {
3204 $run_type = "ERROR";
3208 $installme = " no_install" if ($no_install);
3211 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
3217 if (defined($addconfig)) {
3218 my $min = $minconfig;
3219 if (!defined($minconfig)) {
3222 run_command "cat $addconfig $min > $tmpdir/add_config" or
3223 dodie "Failed to create temp config";
3224 $minconfig = "$tmpdir/add_config";
3227 my $checkout = $opt{"CHECKOUT[$i]"};
3228 if (defined($checkout)) {
3229 run_command "git checkout $checkout" or
3230 die "failed to checkout $checkout";
3236 if ($test_type eq "bisect") {
3239 } elsif ($test_type eq "config_bisect") {
3242 } elsif ($test_type eq "patchcheck") {
3245 } elsif ($test_type eq "make_min_config") {
3250 if ($build_type ne "nobuild") {
3251 build $build_type or next;
3254 if ($test_type eq "install") {
3261 if ($test_type ne "build") {
3263 start_monitor_and_boot or $failed = 1;
3265 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3266 do_run_test or $failed = 1;
3275 if ($opt{"POWEROFF_ON_SUCCESS"}) {
3277 } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
3281 doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";