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 $config_help{"MACHINE"} = << "EOF"
140 The machine hostname that you will test.
143 $config_help{"SSH_USER"} = << "EOF"
144 The box is expected to have ssh on normal bootup, provide the user
145 (most likely root, since you need privileged operations)
148 $config_help{"BUILD_DIR"} = << "EOF"
149 The directory that contains the Linux source code (full path).
152 $config_help{"OUTPUT_DIR"} = << "EOF"
153 The directory that the objects will be built (full path).
154 (can not be same as BUILD_DIR)
157 $config_help{"BUILD_TARGET"} = << "EOF"
158 The location of the compiled file to copy to the target.
159 (relative to OUTPUT_DIR)
162 $config_help{"TARGET_IMAGE"} = << "EOF"
163 The place to put your image on the test machine.
166 $config_help{"POWER_CYCLE"} = << "EOF"
167 A script or command to reboot the box.
169 Here is a digital loggers power switch example
170 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
172 Here is an example to reboot a virtual box on the current host
173 with the name "Guest".
174 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
177 $config_help{"CONSOLE"} = << "EOF"
178 The script or command that reads the console
180 If you use ttywatch server, something like the following would work.
181 CONSOLE = nc -d localhost 3001
183 For a virtual machine with guest name "Guest".
184 CONSOLE = virsh console Guest
187 $config_help{"LOCALVERSION"} = << "EOF"
188 Required version ending to differentiate the test
189 from other linux builds on the system.
192 $config_help{"REBOOT_TYPE"} = << "EOF"
193 Way to reboot the box to the test kernel.
194 Only valid options so far are "grub" and "script".
196 If you specify grub, it will assume grub version 1
197 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
198 and select that target to reboot to the kernel. If this is not
199 your setup, then specify "script" and have a command or script
200 specified in REBOOT_SCRIPT to boot to the target.
202 The entry in /boot/grub/menu.lst must be entered in manually.
203 The test will not modify that file.
206 $config_help{"GRUB_MENU"} = << "EOF"
207 The grub title name for the test kernel to boot
208 (Only mandatory if REBOOT_TYPE = grub)
210 Note, ktest.pl will not update the grub menu.lst, you need to
211 manually add an option for the test. ktest.pl will search
212 the grub menu.lst for this option to find what kernel to
215 For example, if in the /boot/grub/menu.lst the test kernel title has:
218 GRUB_MENU = Test Kernel
221 $config_help{"REBOOT_SCRIPT"} = << "EOF"
222 A script to reboot the target into the test kernel
223 (Only mandatory if REBOOT_TYPE = script)
233 print "$prompt [Y/n] ";
236 if ($ans =~ /^\s*$/) {
239 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
240 print "Please answer either 'y' or 'n'.\n";
242 if ($ans !~ /^y$/i) {
248 sub get_ktest_config {
251 return if (defined($opt{$config}));
253 if (defined($config_help{$config})) {
255 print $config_help{$config};
260 if (defined($default{$config})) {
261 print "\[$default{$config}\] ";
263 $entered_configs{$config} = <STDIN>;
264 $entered_configs{$config} =~ s/^\s*(.*\S)\s*$/$1/;
265 if ($entered_configs{$config} =~ /^\s*$/) {
266 if ($default{$config}) {
267 $entered_configs{$config} = $default{$config};
269 print "Your answer can not be blank\n";
277 sub get_ktest_configs {
278 get_ktest_config("MACHINE");
279 get_ktest_config("SSH_USER");
280 get_ktest_config("BUILD_DIR");
281 get_ktest_config("OUTPUT_DIR");
282 get_ktest_config("BUILD_TARGET");
283 get_ktest_config("TARGET_IMAGE");
284 get_ktest_config("POWER_CYCLE");
285 get_ktest_config("CONSOLE");
286 get_ktest_config("LOCALVERSION");
288 my $rtype = $opt{"REBOOT_TYPE"};
290 if (!defined($rtype)) {
291 if (!defined($opt{"GRUB_MENU"})) {
292 get_ktest_config("REBOOT_TYPE");
293 $rtype = $entered_configs{"REBOOT_TYPE"};
299 if ($rtype eq "grub") {
300 get_ktest_config("GRUB_MENU");
302 get_ktest_config("REBOOT_SCRIPT");
306 sub process_variables {
307 my ($value, $remove_undef) = @_;
310 # We want to check for '\', and it is just easier
311 # to check the previous characet of '$' and not need
312 # to worry if '$' is the first character. By adding
313 # a space to $value, we can just check [^\\]\$ and
314 # it will still work.
317 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
321 # append beginning of value to retval
322 $retval = "$retval$begin";
323 if (defined($variable{$var})) {
324 $retval = "$retval$variable{$var}";
325 } elsif (defined($remove_undef) && $remove_undef) {
326 # for if statements, any variable that is not defined,
327 # we simple convert to 0
328 $retval = "${retval}0";
330 # put back the origin piece.
331 $retval = "$retval\$\{$var\}";
335 $retval = "$retval$value";
337 # remove the space added in the beginning
344 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
346 if (defined($opt{$lvalue})) {
347 if (!$override || defined(${$overrides}{$lvalue})) {
350 $extra = "In the same override section!\n";
352 die "$name: $.: Option $lvalue defined more than once!\n$extra";
354 ${$overrides}{$lvalue} = $rvalue;
356 if ($rvalue =~ /^\s*$/) {
357 delete $opt{$lvalue};
359 $rvalue = process_variables($rvalue);
360 $opt{$lvalue} = $rvalue;
365 my ($lvalue, $rvalue) = @_;
367 if ($rvalue =~ /^\s*$/) {
368 delete $variable{$lvalue};
370 $rvalue = process_variables($rvalue);
371 $variable{$lvalue} = $rvalue;
375 sub process_compare {
376 my ($lval, $cmp, $rval) = @_;
387 return $lval eq $rval;
388 } elsif ($cmp eq "!=") {
389 return $lval ne $rval;
392 my $statement = "$lval $cmp $rval";
393 my $ret = eval $statement;
395 # $@ stores error of eval
406 return defined($variable{$2}) ||
411 sub process_expression {
412 my ($name, $val) = @_;
416 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
419 if (process_expression($name, $express)) {
420 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
422 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
430 while ($val =~ s/^(.*?)($OR|$AND)//) {
434 if (process_expression($name, $express)) {
445 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
446 my $ret = process_compare($1, $2, $3);
448 die "$name: $.: Unable to process comparison\n";
453 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
455 return !value_defined($2);
457 return value_defined($2);
461 if ($val =~ /^\s*0\s*$/) {
463 } elsif ($val =~ /^\s*\d+\s*$/) {
467 die ("$name: $.: Undefined content $val in if statement\n");
471 my ($name, $value) = @_;
473 # Convert variables and replace undefined ones with 0
474 my $val = process_variables($value, 1);
475 my $ret = process_expression $name, $val;
481 my ($config, $current_test_num) = @_;
484 open($in, $config) || die "can't read file $config";
487 $name =~ s,.*/(.*),$1,;
489 my $test_num = $$current_test_num;
492 my $num_tests_set = 0;
505 # ignore blank lines and comments
506 next if (/^\s*$/ || /\s*\#/);
508 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
518 if ($type eq "TEST_START") {
520 if ($num_tests_set) {
521 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
524 $old_test_num = $test_num;
525 $old_repeat = $repeat;
527 $test_num += $repeat;
534 # If SKIP is anywhere in the line, the command will be skipped
535 if ($rest =~ s/\s+SKIP\b//) {
542 if ($rest =~ s/\sELSE\b//) {
544 die "$name: $.: ELSE found with out matching IF section\n$_";
555 if ($rest =~ s/\sIF\s+(.*)//) {
556 if (process_if($name, $1)) {
568 if ($type eq "TEST_START") {
569 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
571 $repeat_tests{"$test_num"} = $repeat;
573 } elsif ($rest =~ s/\sOVERRIDE\b//) {
576 # Clear previous overrides
581 if (!$skip && $rest !~ /^\s*$/) {
582 die "$name: $.: Gargbage found after $type\n$_";
585 if ($skip && $type eq "TEST_START") {
586 $test_num = $old_test_num;
587 $repeat = $old_repeat;
590 } elsif (/^\s*ELSE\b(.*)$/) {
592 die "$name: $.: ELSE found with out matching IF section\n$_";
601 if ($rest =~ /\sIF\s+(.*)/) {
602 # May be a ELSE IF section.
603 if (!process_if($name, $1)) {
612 if ($rest !~ /^\s*$/) {
613 die "$name: $.: Gargbage found after DEFAULTS\n$_";
616 } elsif (/^\s*INCLUDE\s+(\S+)/) {
621 die "$name: $.: INCLUDE can only be done in default sections\n$_";
624 my $file = process_variables($1);
626 if ($file !~ m,^/,) {
627 # check the path of the config file first
628 if ($config =~ m,(.*)/,) {
636 die "$name: $.: Can't read file $file\n$_";
639 if (__read_config($file, \$test_num)) {
643 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
651 ($lvalue eq "NUM_TESTS" ||
652 $lvalue eq "LOG_FILE" ||
653 $lvalue eq "CLEAR_LOG")) {
654 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
657 if ($lvalue eq "NUM_TESTS") {
659 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
662 die "$name: $.: NUM_TESTS must be set in default section\n";
667 if ($default || $lvalue =~ /\[\d+\]$/) {
668 set_value($lvalue, $rvalue, $override, \%overrides, $name);
670 my $val = "$lvalue\[$test_num\]";
671 set_value($val, $rvalue, $override, \%overrides, $name);
674 $repeats{$val} = $repeat;
677 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
683 # process config variables.
684 # Config variables are only active while reading the
685 # config and can be defined anywhere. They also ignore
686 # TEST_START and DEFAULTS, but are skipped if they are in
687 # on of these sections that have SKIP defined.
688 # The save variable can be
689 # defined multiple times and the new one simply overrides
691 set_variable($lvalue, $rvalue);
694 die "$name: $.: Garbage found in config\n$_";
699 $test_num += $repeat - 1;
700 $opt{"NUM_TESTS"} = $test_num;
705 $$current_test_num = $test_num;
716 $test_case = __read_config $config, \$test_num;
718 # make sure we have all mandatory configs
721 # was a test specified?
723 print "No test case specified.\n";
724 print "What test case would you like to run?\n";
727 $default{"TEST_TYPE"} = $ans;
732 foreach my $default (keys %default) {
733 if (!defined($opt{$default})) {
734 $opt{$default} = $default{$default};
740 my ($option, $i) = @_;
742 # Add space to evaluate the character before $
743 $option = " $option";
746 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
751 # Append beginning of line
752 $retval = "$retval$start";
754 # If the iteration option OPT[$i] exists, then use that.
755 # otherwise see if the default OPT (without [$i]) exists.
757 my $o = "$var\[$i\]";
759 if (defined($opt{$o})) {
761 $retval = "$retval$o";
762 } elsif (defined($opt{$var})) {
764 $retval = "$retval$o";
766 $retval = "$retval\$\{$var\}";
772 $retval = "$retval$option";
780 my ($option, $i) = @_;
784 # Since an option can evaluate to another option,
785 # keep iterating until we do not evaluate any more
788 while ($prev ne $option) {
789 # Check for recursive evaluations.
790 # 100 deep should be more than enough.
792 die "Over 100 evaluations accurred with $option\n" .
793 "Check for recursive variables\n";
796 $option = __eval_option($option, $i);
803 if (defined($opt{"LOG_FILE"})) {
804 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
811 if (defined($opt{"LOG_FILE"})) {
826 sub wait_for_monitor;
831 if (defined($time)) {
833 # flush out current monitor
834 # May contain the reboot success line
838 # try to reboot normally
839 if (run_command $reboot) {
840 if (defined($powercycle_after_reboot)) {
841 sleep $powercycle_after_reboot;
842 run_command "$power_cycle";
845 # nope? power cycle it.
846 run_command "$power_cycle";
849 if (defined($time)) {
850 wait_for_monitor($time, $reboot_success_line);
858 return $test_type eq "build" || $no_reboot ||
859 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
860 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
864 doprint "CRITICAL FAILURE... ", @_, "\n";
868 if ($reboot_on_error && !do_not_reboot) {
870 doprint "REBOOTING\n";
873 } elsif ($poweroff_on_error && defined($power_off)) {
874 doprint "POWERING OFF\n";
878 if (defined($opt{"LOG_FILE"})) {
879 print " See $opt{LOG_FILE} for more info.\n";
890 my $pid = open($fp, "$console|") or
891 dodie "Can't open console $console";
893 $flags = fcntl($fp, F_GETFL, 0) or
894 dodie "Can't get flags for the socket: $!";
895 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
896 dodie "Can't set flags for the socket: $!";
904 doprint "kill child process $pid\n";
912 if ($monitor_cnt++) {
915 $monitor_fp = \*MONFD;
916 $monitor_pid = open_console $monitor_fp;
920 open(MONFD, "Stop perl from warning about single use of MONFD");
924 if (--$monitor_cnt) {
927 close_console($monitor_fp, $monitor_pid);
930 sub wait_for_monitor {
931 my ($time, $stop) = @_;
936 doprint "** Wait for monitor to settle down **\n";
938 # read the monitor and wait for the system to calm down
940 $line = wait_for_input($monitor_fp, $time);
941 last if (!defined($line));
945 if (defined($stop) && $full_line =~ /$stop/) {
946 doprint "wait for monitor detected $stop\n";
954 print "** Monitor flushed **\n";
959 if ($die_on_failure) {
967 # no need to reboot for just building.
968 if (!do_not_reboot) {
969 doprint "REBOOTING\n";
975 if (defined($test_name)) {
976 $name = " ($test_name)";
979 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
980 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
981 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
982 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
983 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
985 return 1 if (!defined($store_failures));
988 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
989 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
991 my $type = $build_type;
992 if ($type =~ /useconfig/) {
996 my $dir = "$machine-$test_type-$type-fail-$date";
997 my $faildir = "$store_failures/$dir";
1001 die "can't create $faildir";
1003 if (-f "$output_config") {
1004 cp "$output_config", "$faildir/config" or
1005 die "failed to copy .config";
1008 cp $buildlog, "$faildir/buildlog" or
1009 die "failed to move $buildlog";
1012 cp $dmesg, "$faildir/dmesg" or
1013 die "failed to move $dmesg";
1016 doprint "*** Saved info to $faildir ***\n";
1027 $command =~ s/\$SSH_USER/$ssh_user/g;
1028 $command =~ s/\$MACHINE/$machine/g;
1030 doprint("$command ... ");
1032 $pid = open(CMD, "$command 2>&1 |") or
1033 (fail "unable to exec $command" and return 0);
1035 if (defined($opt{"LOG_FILE"})) {
1036 open(LOG, ">>$opt{LOG_FILE}") or
1037 dodie "failed to write to log";
1041 if (defined($redirect)) {
1042 open (RD, ">$redirect") or
1043 dodie "failed to write to redirect $redirect";
1048 print LOG if ($dolog);
1049 print RD if ($dord);
1056 close(LOG) if ($dolog);
1057 close(RD) if ($dord);
1060 doprint "FAILED!\n";
1062 doprint "SUCCESS\n";
1070 my $cp_exec = $ssh_exec;
1072 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1073 return run_command "$cp_exec";
1077 my ($src, $dst) = @_;
1078 my $cp_scp = $scp_to_target;
1080 $cp_scp =~ s/\$SRC_FILE/$src/g;
1081 $cp_scp =~ s/\$DST_FILE/$dst/g;
1083 return run_command "$cp_scp";
1086 sub get_grub_index {
1088 if ($reboot_type ne "grub") {
1091 return if (defined($grub_number));
1093 doprint "Find grub menu ... ";
1096 my $ssh_grub = $ssh_exec;
1097 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1099 open(IN, "$ssh_grub |")
1100 or die "unable to get menu.lst";
1105 if (/^\s*title\s+$grub_menu\s*$/) {
1109 } elsif (/^\s*title\s/) {
1115 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
1117 doprint "$grub_number\n";
1122 my ($fp, $time) = @_;
1128 if (!defined($time)) {
1133 vec($rin, fileno($fp), 1) = 1;
1134 $ready = select($rin, undef, undef, $time);
1138 # try to read one char at a time
1139 while (sysread $fp, $ch, 1) {
1141 last if ($ch eq "\n");
1144 if (!length($line)) {
1152 if ($reboot_type eq "grub") {
1153 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1158 run_command "$reboot_script";
1164 doprint "git rev-list --max-count=1 $commit ... ";
1165 my $sha1 = `git rev-list --max-count=1 $commit`;
1172 dodie "Failed to get git $commit";
1185 my $skip_call_trace = 0;
1193 open(DMESG, "> $dmesg") or
1194 die "unable to write to $dmesg";
1200 my $monitor_start = time;
1202 my $version_found = 0;
1206 if ($bug && defined($stop_after_failure) &&
1207 $stop_after_failure >= 0) {
1208 my $time = $stop_after_failure - (time - $failure_start);
1209 $line = wait_for_input($monitor_fp, $time);
1210 if (!defined($line)) {
1211 doprint "bug timed out after $booted_timeout seconds\n";
1212 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1216 $line = wait_for_input($monitor_fp, $booted_timeout);
1217 if (!defined($line)) {
1218 my $s = $booted_timeout == 1 ? "" : "s";
1219 doprint "Successful boot found: break after $booted_timeout second$s\n";
1223 $line = wait_for_input($monitor_fp);
1224 if (!defined($line)) {
1225 my $s = $timeout == 1 ? "" : "s";
1226 doprint "Timed out after $timeout second$s\n";
1234 # we are not guaranteed to get a full line
1235 $full_line .= $line;
1237 if ($full_line =~ /$success_line/) {
1239 $success_start = time;
1242 if ($booted && defined($stop_after_success) &&
1243 $stop_after_success >= 0) {
1245 if ($now - $success_start >= $stop_after_success) {
1246 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1251 if ($full_line =~ /\[ backtrace testing \]/) {
1252 $skip_call_trace = 1;
1255 if ($full_line =~ /call trace:/i) {
1256 if (!$bug && !$skip_call_trace) {
1258 $failure_start = time;
1262 if ($bug && defined($stop_after_failure) &&
1263 $stop_after_failure >= 0) {
1265 if ($now - $failure_start >= $stop_after_failure) {
1266 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1271 if ($full_line =~ /\[ end of backtrace testing \]/) {
1272 $skip_call_trace = 0;
1275 if ($full_line =~ /Kernel panic -/) {
1276 $failure_start = time;
1280 # Detect triple faults by testing the banner
1281 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1282 if ($1 eq $version) {
1284 } elsif ($version_found && $detect_triplefault) {
1285 # We already booted into the kernel we are testing,
1286 # but now we booted into another kernel?
1287 # Consider this a triple fault.
1288 doprint "Aleady booted in Linux kernel $version, but now\n";
1289 doprint "we booted into Linux kernel $1.\n";
1290 doprint "Assuming that this is a triple fault.\n";
1291 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1296 if ($line =~ /\n/) {
1300 if ($stop_test_after > 0 && !$booted && !$bug) {
1301 if (time - $monitor_start > $stop_test_after) {
1302 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
1311 return 0 if ($in_bisect);
1312 fail "failed - got a bug report" and return 0;
1316 return 0 if ($in_bisect);
1317 fail "failed - never got a boot prompt." and return 0;
1323 sub do_post_install {
1325 return if (!defined($post_install));
1327 my $cp_post_install = $post_install;
1328 $cp_post_install =~ s/\$KERNEL_VERSION/$version/g;
1329 run_command "$cp_post_install" or
1330 dodie "Failed to run post install";
1335 return if ($no_install);
1337 run_scp "$outputdir/$build_target", "$target_image" or
1338 dodie "failed to copy image";
1340 my $install_mods = 0;
1342 # should we process modules?
1344 open(IN, "$output_config") or dodie("Can't read config file");
1346 if (/CONFIG_MODULES(=y)?/) {
1347 $install_mods = 1 if (defined($1));
1353 if (!$install_mods) {
1355 doprint "No modules needed\n";
1359 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
1360 dodie "Failed to install modules";
1362 my $modlib = "/lib/modules/$version";
1363 my $modtar = "ktest-mods.tar.bz2";
1365 run_ssh "rm -rf $modlib" or
1366 dodie "failed to remove old mods: $modlib";
1368 # would be nice if scp -r did not follow symbolic links
1369 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
1370 dodie "making tarball";
1372 run_scp "$tmpdir/$modtar", "/tmp" or
1373 dodie "failed to copy modules";
1375 unlink "$tmpdir/$modtar";
1377 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
1378 dodie "failed to tar modules";
1380 run_ssh "rm -f /tmp/$modtar";
1386 # get the release name
1387 doprint "$make kernelrelease ... ";
1388 $version = `$make kernelrelease | tail -1`;
1390 doprint "$version\n";
1393 sub start_monitor_and_boot {
1402 sub check_buildlog {
1405 my @files = `git show $patch | diffstat -l`;
1407 open(IN, "git show $patch |") or
1408 dodie "failed to show $patch";
1410 if (m,^--- a/(.*),) {
1412 $files[$#files] = $1;
1417 open(IN, $buildlog) or dodie "Can't open $buildlog";
1419 if (/^\s*(.*?):.*(warning|error)/) {
1421 foreach my $file (@files) {
1422 my $fullpath = "$builddir/$file";
1423 if ($file eq $err || $fullpath eq $err) {
1424 fail "$file built with warnings" and return 0;
1434 sub apply_min_config {
1435 my $outconfig = "$output_config.new";
1437 # Read the config file and remove anything that
1438 # is in the force_config hash (from minconfig and others)
1439 # then add the force config back.
1441 doprint "Applying minimum configurations into $output_config.new\n";
1443 open (OUT, ">$outconfig") or
1444 dodie "Can't create $outconfig";
1446 if (-f $output_config) {
1447 open (IN, $output_config) or
1448 dodie "Failed to open $output_config";
1450 if (/^(# )?(CONFIG_[^\s=]*)/) {
1451 next if (defined($force_config{$2}));
1457 foreach my $config (keys %force_config) {
1458 print OUT "$force_config{$config}\n";
1462 run_command "mv $outconfig $output_config";
1465 sub make_oldconfig {
1467 my @force_list = keys %force_config;
1469 if ($#force_list >= 0) {
1473 if (!run_command "$make oldnoconfig") {
1474 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1475 # try a yes '' | oldconfig
1476 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
1477 run_command "yes '' | $make oldconfig" or
1478 dodie "failed make config oldconfig";
1482 # read a config file and use this to force new configs.
1483 sub load_force_config {
1486 open(IN, $config) or
1487 dodie "failed to read $config";
1490 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1491 $force_config{$1} = $_;
1492 } elsif (/^# (CONFIG_\S*) is not set/) {
1493 $force_config{$1} = $_;
1504 # Failed builds should not reboot the target
1505 my $save_no_reboot = $no_reboot;
1508 if (defined($pre_build)) {
1509 my $ret = run_command $pre_build;
1510 if (!$ret && defined($pre_build_die) &&
1512 dodie "failed to pre_build\n";
1516 if ($type =~ /^useconfig:(.*)/) {
1517 run_command "cp $1 $output_config" or
1518 dodie "could not copy $1 to .config";
1520 $type = "oldconfig";
1523 # old config can ask questions
1524 if ($type eq "oldconfig") {
1525 $type = "oldnoconfig";
1527 # allow for empty configs
1528 run_command "touch $output_config";
1531 run_command "mv $output_config $outputdir/config_temp" or
1532 dodie "moving .config";
1534 run_command "$make mrproper" or dodie "make mrproper";
1536 run_command "mv $outputdir/config_temp $output_config" or
1537 dodie "moving config_temp";
1540 } elsif (!$noclean) {
1541 unlink "$output_config";
1542 run_command "$make mrproper" or
1543 dodie "make mrproper";
1546 # add something to distinguish this build
1547 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1548 print OUT "$localversion\n";
1551 if (defined($minconfig)) {
1552 load_force_config($minconfig);
1555 if ($type ne "oldnoconfig") {
1556 run_command "$make $type" or
1557 dodie "failed make config";
1559 # Run old config regardless, to enforce min configurations
1562 $redirect = "$buildlog";
1563 my $build_ret = run_command "$make $build_options";
1566 if (defined($post_build)) {
1567 my $ret = run_command $post_build;
1568 if (!$ret && defined($post_build_die) &&
1570 dodie "failed to post_build\n";
1575 # bisect may need this to pass
1577 $no_reboot = $save_no_reboot;
1580 fail "failed build" and return 0;
1583 $no_reboot = $save_no_reboot;
1589 if (!run_ssh "halt" or defined($power_off)) {
1590 if (defined($poweroff_after_halt)) {
1591 sleep $poweroff_after_halt;
1592 run_command "$power_off";
1596 run_command "$power_off";
1607 if (defined($test_name)) {
1608 $name = " ($test_name)";
1611 doprint "\n\n*******************************************\n";
1612 doprint "*******************************************\n";
1613 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
1614 doprint "*******************************************\n";
1615 doprint "*******************************************\n";
1617 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
1618 doprint "Reboot and wait $sleep_time seconds\n";
1625 doprint "Pass or fail? [p/f]";
1628 if ($ans eq "p" || $ans eq "P") {
1630 } elsif ($ans eq "f" || $ans eq "F") {
1633 print "Please answer 'P' or 'F'\n";
1638 sub child_run_test {
1641 # child should have no power
1642 $reboot_on_error = 0;
1643 $poweroff_on_error = 0;
1644 $die_on_failure = 1;
1646 run_command $run_test or $failed = 1;
1652 sub child_finished {
1665 doprint "run test $run_test\n";
1669 $SIG{CHLD} = qw(child_finished);
1673 child_run_test if (!$child_pid);
1678 $line = wait_for_input($monitor_fp, 1);
1679 if (defined($line)) {
1681 # we are not guaranteed to get a full line
1682 $full_line .= $line;
1685 if ($full_line =~ /call trace:/i) {
1689 if ($full_line =~ /Kernel panic -/) {
1693 if ($line =~ /\n/) {
1697 } while (!$child_done && !$bug);
1700 my $failure_start = time;
1703 $line = wait_for_input($monitor_fp, 1);
1704 if (defined($line)) {
1708 if ($now - $failure_start >= $stop_after_failure) {
1711 } while (defined($line));
1713 doprint "Detected kernel crash!\n";
1714 # kill the child with extreme prejudice
1718 waitpid $child_pid, 0;
1721 if ($bug || $child_exit) {
1722 return 0 if $in_bisect;
1723 fail "test failed" and return 0;
1728 sub run_git_bisect {
1731 doprint "$command ... ";
1733 my $output = `$command 2>&1`;
1740 dodie "Failed to git bisect";
1743 doprint "SUCCESS\n";
1744 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1745 doprint "$1 [$2]\n";
1746 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
1748 doprint "Found bad commit... $1\n";
1751 # we already logged it, just print it now.
1759 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
1760 reboot $bisect_sleep_time;
1763 # returns 1 on success, 0 on failure, -1 on skip
1764 sub run_bisect_test {
1765 my ($type, $buildtype) = @_;
1774 build $buildtype or $failed = 1;
1776 if ($type ne "build") {
1777 if ($failed && $bisect_skip) {
1781 dodie "Failed on build" if $failed;
1784 start_monitor_and_boot or $failed = 1;
1786 if ($type ne "boot") {
1787 if ($failed && $bisect_skip) {
1793 dodie "Failed on boot" if $failed;
1795 do_run_test or $failed = 1;
1806 # reboot the box to a kernel we can ssh to
1807 if ($type ne "build") {
1817 my $buildtype = "oldconfig";
1819 # We should have a minconfig to use?
1820 if (defined($minconfig)) {
1821 $buildtype = "useconfig:$minconfig";
1824 my $ret = run_bisect_test $type, $buildtype;
1826 if ($bisect_manual) {
1827 $ret = answer_bisect;
1830 # Are we looking for where it worked, not failed?
1831 if ($reverse_bisect) {
1837 } elsif ($ret == 0) {
1839 } elsif ($bisect_skip) {
1840 doprint "HIT A BAD COMMIT ... SKIPPING\n";
1850 die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
1851 die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
1852 die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
1854 my $good = $opt{"BISECT_GOOD[$i]"};
1855 my $bad = $opt{"BISECT_BAD[$i]"};
1856 my $type = $opt{"BISECT_TYPE[$i]"};
1857 my $start = $opt{"BISECT_START[$i]"};
1858 my $replay = $opt{"BISECT_REPLAY[$i]"};
1859 my $start_files = $opt{"BISECT_FILES[$i]"};
1861 if (defined($start_files)) {
1862 $start_files = " -- " . $start_files;
1867 # convert to true sha1's
1868 $good = get_sha1($good);
1869 $bad = get_sha1($bad);
1871 if (defined($opt{"BISECT_REVERSE[$i]"}) &&
1872 $opt{"BISECT_REVERSE[$i]"} == 1) {
1873 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
1874 $reverse_bisect = 1;
1876 $reverse_bisect = 0;
1879 # Can't have a test without having a test to run
1880 if ($type eq "test" && !defined($run_test)) {
1884 my $check = $opt{"BISECT_CHECK[$i]"};
1885 if (defined($check) && $check ne "0") {
1888 my $head = get_sha1("HEAD");
1890 if ($check ne "good") {
1891 doprint "TESTING BISECT BAD [$bad]\n";
1892 run_command "git checkout $bad" or
1893 die "Failed to checkout $bad";
1895 $result = run_bisect $type;
1897 if ($result ne "bad") {
1898 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
1902 if ($check ne "bad") {
1903 doprint "TESTING BISECT GOOD [$good]\n";
1904 run_command "git checkout $good" or
1905 die "Failed to checkout $good";
1907 $result = run_bisect $type;
1909 if ($result ne "good") {
1910 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
1914 # checkout where we started
1915 run_command "git checkout $head" or
1916 die "Failed to checkout $head";
1919 run_command "git bisect start$start_files" or
1920 dodie "could not start bisect";
1922 run_command "git bisect good $good" or
1923 dodie "could not set bisect good to $good";
1925 run_git_bisect "git bisect bad $bad" or
1926 dodie "could not set bisect bad to $bad";
1928 if (defined($replay)) {
1929 run_command "git bisect replay $replay" or
1930 dodie "failed to run replay";
1933 if (defined($start)) {
1934 run_command "git checkout $start" or
1935 dodie "failed to checkout $start";
1940 $result = run_bisect $type;
1941 $test = run_git_bisect "git bisect $result";
1944 run_command "git bisect log" or
1945 dodie "could not capture git bisect log";
1947 run_command "git bisect reset" or
1948 dodie "could not reset git bisect";
1950 doprint "Bad commit was [$bisect_bad]\n";
1963 sub assign_configs {
1964 my ($hash, $config) = @_;
1967 or dodie "Failed to read $config";
1970 if (/^((CONFIG\S*)=.*)/) {
1978 sub process_config_ignore {
1981 assign_configs \%config_ignore, $config;
1984 sub read_current_config {
1985 my ($config_ref) = @_;
1987 %{$config_ref} = ();
1988 undef %{$config_ref};
1990 my @key = keys %{$config_ref};
1992 print "did not delete!\n";
1995 open (IN, "$output_config");
1998 if (/^(CONFIG\S+)=(.*)/) {
1999 ${$config_ref}{$1} = $2;
2005 sub get_dependencies {
2008 my $arr = $dependency{$config};
2009 if (!defined($arr)) {
2015 foreach my $dep (@{$arr}) {
2016 print "ADD DEP $dep\n";
2017 @deps = (@deps, get_dependencies $dep);
2026 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2028 foreach my $config (@configs) {
2029 print OUT "$config_set{$config}\n";
2030 my @deps = get_dependencies $config;
2031 foreach my $dep (@deps) {
2032 print OUT "$config_set{$dep}\n";
2036 foreach my $config (keys %config_ignore) {
2037 print OUT "$config_ignore{$config}\n";
2045 sub compare_configs {
2048 foreach my $item (keys %a) {
2049 if (!defined($b{$item})) {
2050 print "diff $item\n";
2058 print "diff2 $keys[0]\n";
2060 return -1 if ($#keys >= 0);
2065 sub run_config_bisect_test {
2068 return run_bisect_test $type, "oldconfig";
2071 sub process_passed {
2074 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2075 # Passed! All these configs are part of a good compile.
2076 # Add them to the min options.
2077 foreach my $config (keys %configs) {
2078 if (defined($config_list{$config})) {
2079 doprint " removing $config\n";
2080 $config_ignore{$config} = $config_list{$config};
2081 delete $config_list{$config};
2084 doprint "config copied to $outputdir/config_good\n";
2085 run_command "cp -f $output_config $outputdir/config_good";
2088 sub process_failed {
2091 doprint "\n\n***************************************\n";
2092 doprint "Found bad config: $config\n";
2093 doprint "***************************************\n\n";
2096 sub run_config_bisect {
2098 my @start_list = keys %config_list;
2100 if ($#start_list < 0) {
2101 doprint "No more configs to test!!!\n";
2105 doprint "***** RUN TEST ***\n";
2106 my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
2110 my $count = $#start_list + 1;
2111 doprint " $count configs to test\n";
2113 my $half = int($#start_list / 2);
2116 my @tophalf = @start_list[0 .. $half];
2118 create_config @tophalf;
2119 read_current_config \%current_config;
2121 $count = $#tophalf + 1;
2122 doprint "Testing $count configs\n";
2124 # make sure we test something
2125 foreach my $config (@tophalf) {
2126 if (defined($current_config{$config})) {
2132 # try the other half
2133 doprint "Top half produced no set configs, trying bottom half\n";
2134 @tophalf = @start_list[$half + 1 .. $#start_list];
2135 create_config @tophalf;
2136 read_current_config \%current_config;
2137 foreach my $config (@tophalf) {
2138 if (defined($current_config{$config})) {
2144 doprint "Failed: Can't make new config with current configs\n";
2145 foreach my $config (@start_list) {
2146 doprint " CONFIG: $config\n";
2150 $count = $#tophalf + 1;
2151 doprint "Testing $count configs\n";
2154 $ret = run_config_bisect_test $type;
2155 if ($bisect_manual) {
2156 $ret = answer_bisect;
2159 process_passed %current_config;
2163 doprint "This config had a failure.\n";
2164 doprint "Removing these configs that were not set in this config:\n";
2165 doprint "config copied to $outputdir/config_bad\n";
2166 run_command "cp -f $output_config $outputdir/config_bad";
2168 # A config exists in this group that was bad.
2169 foreach my $config (keys %config_list) {
2170 if (!defined($current_config{$config})) {
2171 doprint " removing $config\n";
2172 delete $config_list{$config};
2176 @start_list = @tophalf;
2178 if ($#start_list == 0) {
2179 process_failed $start_list[0];
2183 # remove half the configs we are looking at and see if
2185 $half = int($#start_list / 2);
2186 } while ($#start_list > 0);
2188 # we found a single config, try it again unless we are running manually
2190 if ($bisect_manual) {
2191 process_failed $start_list[0];
2195 my @tophalf = @start_list[0 .. 0];
2197 $ret = run_config_bisect_test $type;
2199 process_passed %current_config;
2203 process_failed $start_list[0];
2210 my $start_config = $opt{"CONFIG_BISECT[$i]"};
2212 my $tmpconfig = "$tmpdir/use_config";
2214 if (defined($config_bisect_good)) {
2215 process_config_ignore $config_bisect_good;
2218 # Make the file with the bad config and the min config
2219 if (defined($minconfig)) {
2220 # read the min config for things to ignore
2221 run_command "cp $minconfig $tmpconfig" or
2222 dodie "failed to copy $minconfig to $tmpconfig";
2227 if (-f $tmpconfig) {
2228 load_force_config($tmpconfig);
2229 process_config_ignore $tmpconfig;
2232 # now process the start config
2233 run_command "cp $start_config $output_config" or
2234 dodie "failed to copy $start_config to $output_config";
2236 # read directly what we want to check
2238 open (IN, $output_config)
2239 or dodie "faied to open $output_config";
2242 if (/^((CONFIG\S*)=.*)/) {
2243 $config_check{$2} = $1;
2248 # Now run oldconfig with the minconfig
2251 # check to see what we lost (or gained)
2252 open (IN, $output_config)
2253 or dodie "Failed to read $start_config";
2255 my %removed_configs;
2259 if (/^((CONFIG\S*)=.*)/) {
2260 # save off all options
2261 $config_set{$2} = $1;
2262 if (defined($config_check{$2})) {
2263 if (defined($config_ignore{$2})) {
2264 $removed_configs{$2} = $1;
2266 $config_list{$2} = $1;
2268 } elsif (!defined($config_ignore{$2})) {
2269 $added_configs{$2} = $1;
2270 $config_list{$2} = $1;
2276 my @confs = keys %removed_configs;
2278 doprint "Configs overridden by default configs and removed from check:\n";
2279 foreach my $config (@confs) {
2280 doprint " $config\n";
2283 @confs = keys %added_configs;
2285 doprint "Configs appearing in make oldconfig and added:\n";
2286 foreach my $config (@confs) {
2287 doprint " $config\n";
2294 # Sometimes kconfig does weird things. We must make sure
2295 # that the config we autocreate has everything we need
2296 # to test, otherwise we may miss testing configs, or
2297 # may not be able to create a new config.
2298 # Here we create a config with everything set.
2299 create_config (keys %config_list);
2300 read_current_config \%config_test;
2301 foreach my $config (keys %config_list) {
2302 if (!defined($config_test{$config})) {
2305 doprint "Configs not produced by kconfig (will not be checked):\n";
2307 doprint " $config\n";
2308 delete $config_list{$config};
2313 $ret = run_config_bisect;
2316 return $ret if ($ret < 0);
2321 sub patchcheck_reboot {
2322 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
2323 reboot $patchcheck_sleep_time;
2329 die "PATCHCHECK_START[$i] not defined\n"
2330 if (!defined($opt{"PATCHCHECK_START[$i]"}));
2331 die "PATCHCHECK_TYPE[$i] not defined\n"
2332 if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
2334 my $start = $opt{"PATCHCHECK_START[$i]"};
2337 if (defined($opt{"PATCHCHECK_END[$i]"})) {
2338 $end = $opt{"PATCHCHECK_END[$i]"};
2341 # Get the true sha1's since we can use things like HEAD~3
2342 $start = get_sha1($start);
2343 $end = get_sha1($end);
2345 my $type = $opt{"PATCHCHECK_TYPE[$i]"};
2347 # Can't have a test without having a test to run
2348 if ($type eq "test" && !defined($run_test)) {
2352 open (IN, "git log --pretty=oneline $end|") or
2353 dodie "could not get git list";
2359 $list[$#list+1] = $_;
2360 last if (/^$start/);
2364 if ($list[$#list] !~ /^$start/) {
2365 fail "SHA1 $start not found";
2368 # go backwards in the list
2369 @list = reverse @list;
2371 my $save_clean = $noclean;
2372 my %ignored_warnings;
2374 if (defined($ignore_warnings)) {
2375 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2376 $ignored_warnings{$sha1} = 1;
2381 foreach my $item (@list) {
2383 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2385 doprint "\nProcessing commit $item\n\n";
2387 run_command "git checkout $sha1" or
2388 die "Failed to checkout $sha1";
2390 # only clean on the first and last patch
2391 if ($item eq $list[0] ||
2392 $item eq $list[$#list]) {
2393 $noclean = $save_clean;
2398 if (defined($minconfig)) {
2399 build "useconfig:$minconfig" or return 0;
2401 # ?? no config to use?
2402 build "oldconfig" or return 0;
2406 if (!defined($ignored_warnings{$sha1})) {
2407 check_buildlog $sha1 or return 0;
2410 next if ($type eq "build");
2414 start_monitor_and_boot or $failed = 1;
2416 if (!$failed && $type ne "boot"){
2417 do_run_test or $failed = 1;
2420 return 0 if ($failed);
2440 # $config depends on $dep
2441 my ($config, $dep) = @_;
2443 if (defined($depends{$config})) {
2444 $depends{$config} .= " " . $dep;
2446 $depends{$config} = $dep;
2449 # record the number of configs depending on $dep
2450 if (defined $depcount{$dep}) {
2453 $depcount{$dep} = 1;
2457 # taken from streamline_config.pl
2469 if (! -f $kconfig) {
2470 doprint "file $kconfig does not exist, skipping\n";
2474 open(KIN, "$kconfig")
2475 or die "Can't open $kconfig";
2479 # Make sure that lines ending with \ continue
2481 $_ = $line . " " . $_;
2492 # collect any Kconfig sources
2493 if (/^source\s*"(.*)"/) {
2494 $kconfigs[$#kconfigs+1] = $1;
2498 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2502 for (my $i = 0; $i < $iflevel; $i++) {
2503 add_dep $config, $ifdeps[$i];
2506 # collect the depends for the config
2507 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2509 add_dep $config, $1;
2511 # Get the configs that select this config
2512 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2514 # selected by depends on config
2515 add_dep $1, $config;
2517 # Check for if statements
2518 } elsif (/^if\s+(.*\S)\s*$/) {
2520 # remove beginning and ending non text
2521 $deps =~ s/^[^a-zA-Z0-9_]*//;
2522 $deps =~ s/[^a-zA-Z0-9_]*$//;
2524 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2526 $ifdeps[$iflevel++] = join ':', @deps;
2528 } elsif (/^endif/) {
2530 $iflevel-- if ($iflevel);
2533 } elsif (/^\s*help\s*$/) {
2539 # read in any configs that were found.
2540 foreach $kconfig (@kconfigs) {
2541 if (!defined($read_kconfigs{$kconfig})) {
2542 $read_kconfigs{$kconfig} = 1;
2543 read_kconfig("$builddir/$kconfig");
2549 # find out which arch this is by the kconfig file
2550 open (IN, $output_config)
2551 or dodie "Failed to read $output_config";
2554 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2561 if (!defined($arch)) {
2562 doprint "Could not find arch from config file\n";
2563 doprint "no dependencies used\n";
2567 # arch is really the subarch, we need to know
2568 # what directory to look at.
2569 if ($arch eq "i386" || $arch eq "x86_64") {
2571 } elsif ($arch =~ /^tile/) {
2575 my $kconfig = "$builddir/arch/$arch/Kconfig";
2577 if (! -f $kconfig && $arch =~ /\d$/) {
2579 # some subarchs have numbers, truncate them
2581 $kconfig = "$builddir/arch/$arch/Kconfig";
2582 if (! -f $kconfig) {
2583 doprint "No idea what arch dir $orig is for\n";
2584 doprint "no dependencies used\n";
2589 read_kconfig($kconfig);
2592 sub read_config_list {
2596 or dodie "Failed to read $config";
2599 if (/^((CONFIG\S*)=.*)/) {
2600 if (!defined($config_ignore{$2})) {
2601 $config_list{$2} = $1;
2609 sub read_output_config {
2612 assign_configs \%config_ignore, $config;
2615 sub make_new_config {
2618 open (OUT, ">$output_config")
2619 or dodie "Failed to write $output_config";
2621 foreach my $config (@configs) {
2622 print OUT "$config\n";
2630 $config =~ s/CONFIG_//;
2638 my $kconfig = chomp_config $dep;
2640 $dep = $depends{"$kconfig"};
2642 # the dep string we have saves the dependencies as they
2643 # were found, including expressions like ! && ||. We
2644 # want to split this out into just an array of configs.
2646 my $valid = "A-Za-z_0-9";
2650 while ($dep =~ /[$valid]/) {
2652 if ($dep =~ /^[^$valid]*([$valid]+)/) {
2653 my $conf = "CONFIG_" . $1;
2655 $configs[$#configs + 1] = $conf;
2657 $dep =~ s/^[^$valid]*[$valid]+//;
2659 die "this should never happen";
2669 my %processed_configs;
2670 my %nochange_config;
2672 sub test_this_config {
2677 # if we already processed this config, skip it
2678 if (defined($processed_configs{$config})) {
2681 $processed_configs{$config} = 1;
2683 # if this config failed during this round, skip it
2684 if (defined($nochange_config{$config})) {
2688 my $kconfig = chomp_config $config;
2690 # Test dependencies first
2691 if (defined($depends{"$kconfig"})) {
2692 my @parents = get_depends $config;
2693 foreach my $parent (@parents) {
2694 # if the parent is in the min config, check it first
2695 next if (!defined($min_configs{$parent}));
2696 $found = test_this_config($parent);
2697 if (defined($found)) {
2703 # Remove this config from the list of configs
2704 # do a make oldnoconfig and then read the resulting
2705 # .config to make sure it is missing the config that
2707 my %configs = %min_configs;
2708 delete $configs{$config};
2709 make_new_config ((values %configs), (values %keep_configs));
2712 assign_configs \%configs, $output_config;
2714 return $config if (!defined($configs{$config}));
2716 doprint "disabling config $config did not change .config\n";
2718 $nochange_config{$config} = 1;
2723 sub make_min_config {
2726 if (!defined($output_minconfig)) {
2727 fail "OUTPUT_MIN_CONFIG not defined" and return;
2730 # If output_minconfig exists, and the start_minconfig
2731 # came from min_config, than ask if we should use
2733 if (-f $output_minconfig && !$start_minconfig_defined) {
2734 print "$output_minconfig exists\n";
2735 if (read_yn " Use it as minconfig?") {
2736 $start_minconfig = $output_minconfig;
2740 if (!defined($start_minconfig)) {
2741 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
2744 my $temp_config = "$tmpdir/temp_config";
2746 # First things first. We build an allnoconfig to find
2747 # out what the defaults are that we can't touch.
2748 # Some are selections, but we really can't handle selections.
2750 my $save_minconfig = $minconfig;
2753 run_command "$make allnoconfig" or return 0;
2757 process_config_ignore $output_config;
2759 undef %save_configs;
2762 if (defined($ignore_config)) {
2763 # make sure the file exists
2764 `touch $ignore_config`;
2765 assign_configs \%save_configs, $ignore_config;
2768 %keep_configs = %save_configs;
2770 doprint "Load initial configs from $start_minconfig\n";
2772 # Look at the current min configs, and save off all the
2773 # ones that were set via the allnoconfig
2774 assign_configs \%min_configs, $start_minconfig;
2776 my @config_keys = keys %min_configs;
2778 # All configs need a depcount
2779 foreach my $config (@config_keys) {
2780 my $kconfig = chomp_config $config;
2781 if (!defined $depcount{$kconfig}) {
2782 $depcount{$kconfig} = 0;
2786 # Remove anything that was set by the make allnoconfig
2787 # we shouldn't need them as they get set for us anyway.
2788 foreach my $config (@config_keys) {
2789 # Remove anything in the ignore_config
2790 if (defined($keep_configs{$config})) {
2791 my $file = $ignore_config;
2792 $file =~ s,.*/(.*?)$,$1,;
2793 doprint "$config set by $file ... ignored\n";
2794 delete $min_configs{$config};
2797 # But make sure the settings are the same. If a min config
2798 # sets a selection, we do not want to get rid of it if
2799 # it is not the same as what we have. Just move it into
2801 if (defined($config_ignore{$config})) {
2802 if ($config_ignore{$config} ne $min_configs{$config}) {
2803 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
2804 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
2805 $keep_configs{$config} = $min_configs{$config};
2807 doprint "$config set by allnoconfig ... ignored\n";
2809 delete $min_configs{$config};
2821 # Now disable each config one by one and do a make oldconfig
2822 # till we find a config that changes our list.
2824 my @test_configs = keys %min_configs;
2826 # Sort keys by who is most dependent on
2827 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
2830 # Put configs that did not modify the config at the end.
2832 for (my $i = 0; $i < $#test_configs; $i++) {
2833 if (!defined($nochange_config{$test_configs[0]})) {
2837 # This config didn't change the .config last time.
2838 # Place it at the end
2839 my $config = shift @test_configs;
2840 push @test_configs, $config;
2843 # if every test config has failed to modify the .config file
2844 # in the past, then reset and start over.
2846 undef %nochange_config;
2849 undef %processed_configs;
2851 foreach my $config (@test_configs) {
2853 $found = test_this_config $config;
2855 last if (defined($found));
2857 # oh well, try another config
2860 if (!defined($found)) {
2861 # we could have failed due to the nochange_config hash
2862 # reset and try again
2864 undef %nochange_config;
2868 doprint "No more configs found that we can disable\n";
2876 doprint "Test with $config disabled\n";
2878 # set in_bisect to keep build and monitor from dieing
2883 start_monitor_and_boot or $failed = 1;
2889 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
2890 # this config is needed, add it to the ignore list.
2891 $keep_configs{$config} = $min_configs{$config};
2892 $save_configs{$config} = $min_configs{$config};
2893 delete $min_configs{$config};
2895 # update new ignore configs
2896 if (defined($ignore_config)) {
2897 open (OUT, ">$temp_config")
2898 or die "Can't write to $temp_config";
2899 foreach my $config (keys %save_configs) {
2900 print OUT "$save_configs{$config}\n";
2903 run_command "mv $temp_config $ignore_config" or
2904 dodie "failed to copy update to $ignore_config";
2908 # We booted without this config, remove it from the minconfigs.
2909 doprint "$config is not needed, disabling\n";
2911 delete $min_configs{$config};
2913 # Also disable anything that is not enabled in this config
2915 assign_configs \%configs, $output_config;
2916 my @config_keys = keys %min_configs;
2917 foreach my $config (@config_keys) {
2918 if (!defined($configs{$config})) {
2919 doprint "$config is not set, disabling\n";
2920 delete $min_configs{$config};
2924 # Save off all the current mandidory configs
2925 open (OUT, ">$temp_config")
2926 or die "Can't write to $temp_config";
2927 foreach my $config (keys %keep_configs) {
2928 print OUT "$keep_configs{$config}\n";
2930 foreach my $config (keys %min_configs) {
2931 print OUT "$min_configs{$config}\n";
2935 run_command "mv $temp_config $output_minconfig" or
2936 dodie "failed to copy update to $output_minconfig";
2939 doprint "Reboot and wait $sleep_time seconds\n";
2947 $#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
2950 $ktest_config = $ARGV[0];
2951 if (! -f $ktest_config) {
2952 print "$ktest_config does not exist.\n";
2953 if (!read_yn "Create it?") {
2958 $ktest_config = "ktest.conf";
2961 if (! -f $ktest_config) {
2962 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
2964 # Generated by ktest.pl
2966 # Define each test with TEST_START
2967 # The config options below it will override the defaults
2975 read_config $ktest_config;
2977 if (defined($opt{"LOG_FILE"})) {
2978 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
2981 # Append any configs entered in manually to the config file.
2982 my @new_configs = keys %entered_configs;
2983 if ($#new_configs >= 0) {
2984 print "\nAppending entered in configs to $ktest_config\n";
2985 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
2986 foreach my $config (@new_configs) {
2987 print OUT "$config = $entered_configs{$config}\n";
2988 $opt{$config} = $entered_configs{$config};
2992 if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
2993 unlink $opt{"LOG_FILE"};
2996 doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
2998 for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3001 doprint "DEFAULT OPTIONS:\n";
3003 doprint "\nTEST $i OPTIONS";
3004 if (defined($repeat_tests{$i})) {
3005 $repeat = $repeat_tests{$i};
3006 doprint " ITERATE $repeat";
3011 foreach my $option (sort keys %opt) {
3013 if ($option =~ /\[(\d+)\]$/) {
3019 doprint "$option = $opt{$option}\n";
3023 sub __set_test_option {
3024 my ($name, $i) = @_;
3026 my $option = "$name\[$i\]";
3028 if (defined($opt{$option})) {
3029 return $opt{$option};
3032 foreach my $test (keys %repeat_tests) {
3034 $i < $test + $repeat_tests{$test}) {
3035 $option = "$name\[$test\]";
3036 if (defined($opt{$option})) {
3037 return $opt{$option};
3042 if (defined($opt{$name})) {
3049 sub set_test_option {
3050 my ($name, $i) = @_;
3052 my $option = __set_test_option($name, $i);
3053 return $option if (!defined($option));
3055 return eval_option($option, $i);
3058 # First we need to do is the builds
3059 for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
3061 # Do not reboot on failing test options
3066 my $makecmd = set_test_option("MAKE_CMD", $i);
3068 $machine = set_test_option("MACHINE", $i);
3069 $ssh_user = set_test_option("SSH_USER", $i);
3070 $tmpdir = set_test_option("TMP_DIR", $i);
3071 $outputdir = set_test_option("OUTPUT_DIR", $i);
3072 $builddir = set_test_option("BUILD_DIR", $i);
3073 $test_type = set_test_option("TEST_TYPE", $i);
3074 $build_type = set_test_option("BUILD_TYPE", $i);
3075 $build_options = set_test_option("BUILD_OPTIONS", $i);
3076 $pre_build = set_test_option("PRE_BUILD", $i);
3077 $post_build = set_test_option("POST_BUILD", $i);
3078 $pre_build_die = set_test_option("PRE_BUILD_DIE", $i);
3079 $post_build_die = set_test_option("POST_BUILD_DIE", $i);
3080 $power_cycle = set_test_option("POWER_CYCLE", $i);
3081 $reboot = set_test_option("REBOOT", $i);
3082 $noclean = set_test_option("BUILD_NOCLEAN", $i);
3083 $minconfig = set_test_option("MIN_CONFIG", $i);
3084 $output_minconfig = set_test_option("OUTPUT_MIN_CONFIG", $i);
3085 $start_minconfig = set_test_option("START_MIN_CONFIG", $i);
3086 $ignore_config = set_test_option("IGNORE_CONFIG", $i);
3087 $run_test = set_test_option("TEST", $i);
3088 $addconfig = set_test_option("ADD_CONFIG", $i);
3089 $reboot_type = set_test_option("REBOOT_TYPE", $i);
3090 $grub_menu = set_test_option("GRUB_MENU", $i);
3091 $post_install = set_test_option("POST_INSTALL", $i);
3092 $no_install = set_test_option("NO_INSTALL", $i);
3093 $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
3094 $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
3095 $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
3096 $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
3097 $power_off = set_test_option("POWER_OFF", $i);
3098 $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
3099 $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
3100 $sleep_time = set_test_option("SLEEP_TIME", $i);
3101 $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
3102 $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
3103 $ignore_warnings = set_test_option("IGNORE_WARNINGS", $i);
3104 $bisect_manual = set_test_option("BISECT_MANUAL", $i);
3105 $bisect_skip = set_test_option("BISECT_SKIP", $i);
3106 $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
3107 $store_failures = set_test_option("STORE_FAILURES", $i);
3108 $test_name = set_test_option("TEST_NAME", $i);
3109 $timeout = set_test_option("TIMEOUT", $i);
3110 $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
3111 $console = set_test_option("CONSOLE", $i);
3112 $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
3113 $success_line = set_test_option("SUCCESS_LINE", $i);
3114 $reboot_success_line = set_test_option("REBOOT_SUCCESS_LINE", $i);
3115 $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
3116 $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
3117 $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
3118 $build_target = set_test_option("BUILD_TARGET", $i);
3119 $ssh_exec = set_test_option("SSH_EXEC", $i);
3120 $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
3121 $target_image = set_test_option("TARGET_IMAGE", $i);
3122 $localversion = set_test_option("LOCALVERSION", $i);
3124 $start_minconfig_defined = 1;
3126 if (!defined($start_minconfig)) {
3127 $start_minconfig_defined = 0;
3128 $start_minconfig = $minconfig;
3131 chdir $builddir || die "can't change directory to $builddir";
3133 foreach my $dir ($tmpdir, $outputdir) {
3136 die "can't create $dir";
3140 $ENV{"SSH_USER"} = $ssh_user;
3141 $ENV{"MACHINE"} = $machine;
3143 $target = "$ssh_user\@$machine";
3145 $buildlog = "$tmpdir/buildlog-$machine";
3146 $dmesg = "$tmpdir/dmesg-$machine";
3147 $make = "$makecmd O=$outputdir";
3148 $output_config = "$outputdir/.config";
3150 if ($reboot_type eq "grub") {
3151 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3152 } elsif (!defined($reboot_script)) {
3153 dodie "REBOOT_SCRIPT not defined"
3156 my $run_type = $build_type;
3157 if ($test_type eq "patchcheck") {
3158 $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
3159 } elsif ($test_type eq "bisect") {
3160 $run_type = $opt{"BISECT_TYPE[$i]"};
3161 } elsif ($test_type eq "config_bisect") {
3162 $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
3165 if ($test_type eq "make_min_config") {
3169 # mistake in config file?
3170 if (!defined($run_type)) {
3171 $run_type = "ERROR";
3175 $installme = " no_install" if ($no_install);
3178 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
3183 if (defined($addconfig)) {
3184 my $min = $minconfig;
3185 if (!defined($minconfig)) {
3188 run_command "cat $addconfig $min > $tmpdir/add_config" or
3189 dodie "Failed to create temp config";
3190 $minconfig = "$tmpdir/add_config";
3193 my $checkout = $opt{"CHECKOUT[$i]"};
3194 if (defined($checkout)) {
3195 run_command "git checkout $checkout" or
3196 die "failed to checkout $checkout";
3202 if ($test_type eq "bisect") {
3205 } elsif ($test_type eq "config_bisect") {
3208 } elsif ($test_type eq "patchcheck") {
3211 } elsif ($test_type eq "make_min_config") {
3216 if ($build_type ne "nobuild") {
3217 build $build_type or next;
3220 if ($test_type eq "install") {
3227 if ($test_type ne "build") {
3229 start_monitor_and_boot or $failed = 1;
3231 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3232 do_run_test or $failed = 1;
3241 if ($opt{"POWEROFF_ON_SUCCESS"}) {
3243 } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
3247 doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";