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 {
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}";
326 # put back the origin piece.
327 $retval = "$retval\$\{$var\}";
331 $retval = "$retval$value";
333 # remove the space added in the beginning
340 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
342 if (defined($opt{$lvalue})) {
343 if (!$override || defined(${$overrides}{$lvalue})) {
346 $extra = "In the same override section!\n";
348 die "$name: $.: Option $lvalue defined more than once!\n$extra";
350 ${$overrides}{$lvalue} = $rvalue;
352 if ($rvalue =~ /^\s*$/) {
353 delete $opt{$lvalue};
355 $rvalue = process_variables($rvalue);
356 $opt{$lvalue} = $rvalue;
361 my ($lvalue, $rvalue) = @_;
363 if ($rvalue =~ /^\s*$/) {
364 delete $variable{$lvalue};
366 $rvalue = process_variables($rvalue);
367 $variable{$lvalue} = $rvalue;
371 sub process_compare {
372 my ($lval, $cmp, $rval) = @_;
383 return $lval eq $rval;
384 } elsif ($cmp eq "!=") {
385 return $lval ne $rval;
388 my $statement = "$lval $cmp $rval";
389 my $ret = eval $statement;
391 # $@ stores error of eval
402 return defined($variable{$2}) ||
407 my ($name, $value) = @_;
409 my $val = process_variables($value);
411 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
412 my $ret = process_compare($1, $2, $3);
414 die "$name: $.: Unable to process comparison\n";
419 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
421 return !value_defined($2);
423 return value_defined($2);
427 if ($val =~ /^\s*0\s*$/) {
429 } elsif ($val =~ /^\s*\d+\s*$/) {
433 die ("$name: $.: Undefined content $val in if statement\n");
438 my ($config, $current_test_num) = @_;
441 open($in, $config) || die "can't read file $config";
444 $name =~ s,.*/(.*),$1,;
446 my $test_num = $$current_test_num;
449 my $num_tests_set = 0;
462 # ignore blank lines and comments
463 next if (/^\s*$/ || /\s*\#/);
465 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
475 if ($type eq "TEST_START") {
477 if ($num_tests_set) {
478 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
481 $old_test_num = $test_num;
482 $old_repeat = $repeat;
484 $test_num += $repeat;
491 # If SKIP is anywhere in the line, the command will be skipped
492 if ($rest =~ s/\s+SKIP\b//) {
499 if ($rest =~ s/\sELSE\b//) {
501 die "$name: $.: ELSE found with out matching IF section\n$_";
512 if ($rest =~ s/\sIF\s+(.*)//) {
513 if (process_if($name, $1)) {
525 if ($type eq "TEST_START") {
526 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
528 $repeat_tests{"$test_num"} = $repeat;
530 } elsif ($rest =~ s/\sOVERRIDE\b//) {
533 # Clear previous overrides
538 if (!$skip && $rest !~ /^\s*$/) {
539 die "$name: $.: Gargbage found after $type\n$_";
542 if ($skip && $type eq "TEST_START") {
543 $test_num = $old_test_num;
544 $repeat = $old_repeat;
547 } elsif (/^\s*ELSE\b(.*)$/) {
549 die "$name: $.: ELSE found with out matching IF section\n$_";
558 if ($rest =~ /\sIF\s+(.*)/) {
559 # May be a ELSE IF section.
560 if (!process_if($name, $1)) {
569 if ($rest !~ /^\s*$/) {
570 die "$name: $.: Gargbage found after DEFAULTS\n$_";
573 } elsif (/^\s*INCLUDE\s+(\S+)/) {
578 die "$name: $.: INCLUDE can only be done in default sections\n$_";
581 my $file = process_variables($1);
583 if ($file !~ m,^/,) {
584 # check the path of the config file first
585 if ($config =~ m,(.*)/,) {
593 die "$name: $.: Can't read file $file\n$_";
596 if (__read_config($file, \$test_num)) {
600 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
608 ($lvalue eq "NUM_TESTS" ||
609 $lvalue eq "LOG_FILE" ||
610 $lvalue eq "CLEAR_LOG")) {
611 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
614 if ($lvalue eq "NUM_TESTS") {
616 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
619 die "$name: $.: NUM_TESTS must be set in default section\n";
624 if ($default || $lvalue =~ /\[\d+\]$/) {
625 set_value($lvalue, $rvalue, $override, \%overrides, $name);
627 my $val = "$lvalue\[$test_num\]";
628 set_value($val, $rvalue, $override, \%overrides, $name);
631 $repeats{$val} = $repeat;
634 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
640 # process config variables.
641 # Config variables are only active while reading the
642 # config and can be defined anywhere. They also ignore
643 # TEST_START and DEFAULTS, but are skipped if they are in
644 # on of these sections that have SKIP defined.
645 # The save variable can be
646 # defined multiple times and the new one simply overrides
648 set_variable($lvalue, $rvalue);
651 die "$name: $.: Garbage found in config\n$_";
656 $test_num += $repeat - 1;
657 $opt{"NUM_TESTS"} = $test_num;
662 $$current_test_num = $test_num;
673 $test_case = __read_config $config, \$test_num;
675 # make sure we have all mandatory configs
678 # was a test specified?
680 print "No test case specified.\n";
681 print "What test case would you like to run?\n";
684 $default{"TEST_TYPE"} = $ans;
689 foreach my $default (keys %default) {
690 if (!defined($opt{$default})) {
691 $opt{$default} = $default{$default};
697 my ($option, $i) = @_;
699 # Add space to evaluate the character before $
700 $option = " $option";
703 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
708 # Append beginning of line
709 $retval = "$retval$start";
711 # If the iteration option OPT[$i] exists, then use that.
712 # otherwise see if the default OPT (without [$i]) exists.
714 my $o = "$var\[$i\]";
716 if (defined($opt{$o})) {
718 $retval = "$retval$o";
719 } elsif (defined($opt{$var})) {
721 $retval = "$retval$o";
723 $retval = "$retval\$\{$var\}";
729 $retval = "$retval$option";
737 my ($option, $i) = @_;
741 # Since an option can evaluate to another option,
742 # keep iterating until we do not evaluate any more
745 while ($prev ne $option) {
746 # Check for recursive evaluations.
747 # 100 deep should be more than enough.
749 die "Over 100 evaluations accurred with $option\n" .
750 "Check for recursive variables\n";
753 $option = __eval_option($option, $i);
760 if (defined($opt{"LOG_FILE"})) {
761 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
768 if (defined($opt{"LOG_FILE"})) {
783 sub wait_for_monitor;
788 if (defined($time)) {
790 # flush out current monitor
791 # May contain the reboot success line
795 # try to reboot normally
796 if (run_command $reboot) {
797 if (defined($powercycle_after_reboot)) {
798 sleep $powercycle_after_reboot;
799 run_command "$power_cycle";
802 # nope? power cycle it.
803 run_command "$power_cycle";
806 if (defined($time)) {
807 wait_for_monitor($time, $reboot_success_line);
815 return $test_type eq "build" || $no_reboot ||
816 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
817 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
821 doprint "CRITICAL FAILURE... ", @_, "\n";
825 if ($reboot_on_error && !do_not_reboot) {
827 doprint "REBOOTING\n";
830 } elsif ($poweroff_on_error && defined($power_off)) {
831 doprint "POWERING OFF\n";
835 if (defined($opt{"LOG_FILE"})) {
836 print " See $opt{LOG_FILE} for more info.\n";
847 my $pid = open($fp, "$console|") or
848 dodie "Can't open console $console";
850 $flags = fcntl($fp, F_GETFL, 0) or
851 dodie "Can't get flags for the socket: $!";
852 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
853 dodie "Can't set flags for the socket: $!";
861 doprint "kill child process $pid\n";
869 if ($monitor_cnt++) {
872 $monitor_fp = \*MONFD;
873 $monitor_pid = open_console $monitor_fp;
877 open(MONFD, "Stop perl from warning about single use of MONFD");
881 if (--$monitor_cnt) {
884 close_console($monitor_fp, $monitor_pid);
887 sub wait_for_monitor {
888 my ($time, $stop) = @_;
893 doprint "** Wait for monitor to settle down **\n";
895 # read the monitor and wait for the system to calm down
897 $line = wait_for_input($monitor_fp, $time);
898 last if (!defined($line));
902 if (defined($stop) && $full_line =~ /$stop/) {
903 doprint "wait for monitor detected $stop\n";
911 print "** Monitor flushed **\n";
916 if ($die_on_failure) {
924 # no need to reboot for just building.
925 if (!do_not_reboot) {
926 doprint "REBOOTING\n";
932 if (defined($test_name)) {
933 $name = " ($test_name)";
936 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
937 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
938 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
939 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
940 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
942 return 1 if (!defined($store_failures));
945 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
946 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
948 my $type = $build_type;
949 if ($type =~ /useconfig/) {
953 my $dir = "$machine-$test_type-$type-fail-$date";
954 my $faildir = "$store_failures/$dir";
958 die "can't create $faildir";
960 if (-f "$output_config") {
961 cp "$output_config", "$faildir/config" or
962 die "failed to copy .config";
965 cp $buildlog, "$faildir/buildlog" or
966 die "failed to move $buildlog";
969 cp $dmesg, "$faildir/dmesg" or
970 die "failed to move $dmesg";
973 doprint "*** Saved info to $faildir ***\n";
984 $command =~ s/\$SSH_USER/$ssh_user/g;
985 $command =~ s/\$MACHINE/$machine/g;
987 doprint("$command ... ");
989 $pid = open(CMD, "$command 2>&1 |") or
990 (fail "unable to exec $command" and return 0);
992 if (defined($opt{"LOG_FILE"})) {
993 open(LOG, ">>$opt{LOG_FILE}") or
994 dodie "failed to write to log";
998 if (defined($redirect)) {
999 open (RD, ">$redirect") or
1000 dodie "failed to write to redirect $redirect";
1005 print LOG if ($dolog);
1006 print RD if ($dord);
1013 close(LOG) if ($dolog);
1014 close(RD) if ($dord);
1017 doprint "FAILED!\n";
1019 doprint "SUCCESS\n";
1027 my $cp_exec = $ssh_exec;
1029 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1030 return run_command "$cp_exec";
1034 my ($src, $dst) = @_;
1035 my $cp_scp = $scp_to_target;
1037 $cp_scp =~ s/\$SRC_FILE/$src/g;
1038 $cp_scp =~ s/\$DST_FILE/$dst/g;
1040 return run_command "$cp_scp";
1043 sub get_grub_index {
1045 if ($reboot_type ne "grub") {
1048 return if (defined($grub_number));
1050 doprint "Find grub menu ... ";
1053 my $ssh_grub = $ssh_exec;
1054 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1056 open(IN, "$ssh_grub |")
1057 or die "unable to get menu.lst";
1062 if (/^\s*title\s+$grub_menu\s*$/) {
1066 } elsif (/^\s*title\s/) {
1072 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
1074 doprint "$grub_number\n";
1079 my ($fp, $time) = @_;
1085 if (!defined($time)) {
1090 vec($rin, fileno($fp), 1) = 1;
1091 $ready = select($rin, undef, undef, $time);
1095 # try to read one char at a time
1096 while (sysread $fp, $ch, 1) {
1098 last if ($ch eq "\n");
1101 if (!length($line)) {
1109 if ($reboot_type eq "grub") {
1110 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch && reboot)'";
1114 run_command "$reboot_script";
1120 doprint "git rev-list --max-count=1 $commit ... ";
1121 my $sha1 = `git rev-list --max-count=1 $commit`;
1128 dodie "Failed to get git $commit";
1141 my $skip_call_trace = 0;
1149 open(DMESG, "> $dmesg") or
1150 die "unable to write to $dmesg";
1156 my $monitor_start = time;
1158 my $version_found = 0;
1162 if ($bug && defined($stop_after_failure) &&
1163 $stop_after_failure >= 0) {
1164 my $time = $stop_after_failure - (time - $failure_start);
1165 $line = wait_for_input($monitor_fp, $time);
1166 if (!defined($line)) {
1167 doprint "bug timed out after $booted_timeout seconds\n";
1168 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1172 $line = wait_for_input($monitor_fp, $booted_timeout);
1173 if (!defined($line)) {
1174 my $s = $booted_timeout == 1 ? "" : "s";
1175 doprint "Successful boot found: break after $booted_timeout second$s\n";
1179 $line = wait_for_input($monitor_fp);
1180 if (!defined($line)) {
1181 my $s = $timeout == 1 ? "" : "s";
1182 doprint "Timed out after $timeout second$s\n";
1190 # we are not guaranteed to get a full line
1191 $full_line .= $line;
1193 if ($full_line =~ /$success_line/) {
1195 $success_start = time;
1198 if ($booted && defined($stop_after_success) &&
1199 $stop_after_success >= 0) {
1201 if ($now - $success_start >= $stop_after_success) {
1202 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1207 if ($full_line =~ /\[ backtrace testing \]/) {
1208 $skip_call_trace = 1;
1211 if ($full_line =~ /call trace:/i) {
1212 if (!$bug && !$skip_call_trace) {
1214 $failure_start = time;
1218 if ($bug && defined($stop_after_failure) &&
1219 $stop_after_failure >= 0) {
1221 if ($now - $failure_start >= $stop_after_failure) {
1222 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1227 if ($full_line =~ /\[ end of backtrace testing \]/) {
1228 $skip_call_trace = 0;
1231 if ($full_line =~ /Kernel panic -/) {
1232 $failure_start = time;
1236 # Detect triple faults by testing the banner
1237 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1238 if ($1 eq $version) {
1240 } elsif ($version_found && $detect_triplefault) {
1241 # We already booted into the kernel we are testing,
1242 # but now we booted into another kernel?
1243 # Consider this a triple fault.
1244 doprint "Aleady booted in Linux kernel $version, but now\n";
1245 doprint "we booted into Linux kernel $1.\n";
1246 doprint "Assuming that this is a triple fault.\n";
1247 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1252 if ($line =~ /\n/) {
1256 if ($stop_test_after > 0 && !$booted && !$bug) {
1257 if (time - $monitor_start > $stop_test_after) {
1258 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
1267 return 0 if ($in_bisect);
1268 fail "failed - got a bug report" and return 0;
1272 return 0 if ($in_bisect);
1273 fail "failed - never got a boot prompt." and return 0;
1279 sub do_post_install {
1281 return if (!defined($post_install));
1283 my $cp_post_install = $post_install;
1284 $cp_post_install =~ s/\$KERNEL_VERSION/$version/g;
1285 run_command "$cp_post_install" or
1286 dodie "Failed to run post install";
1291 return if ($no_install);
1293 run_scp "$outputdir/$build_target", "$target_image" or
1294 dodie "failed to copy image";
1296 my $install_mods = 0;
1298 # should we process modules?
1300 open(IN, "$output_config") or dodie("Can't read config file");
1302 if (/CONFIG_MODULES(=y)?/) {
1303 $install_mods = 1 if (defined($1));
1309 if (!$install_mods) {
1311 doprint "No modules needed\n";
1315 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
1316 dodie "Failed to install modules";
1318 my $modlib = "/lib/modules/$version";
1319 my $modtar = "ktest-mods.tar.bz2";
1321 run_ssh "rm -rf $modlib" or
1322 dodie "failed to remove old mods: $modlib";
1324 # would be nice if scp -r did not follow symbolic links
1325 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
1326 dodie "making tarball";
1328 run_scp "$tmpdir/$modtar", "/tmp" or
1329 dodie "failed to copy modules";
1331 unlink "$tmpdir/$modtar";
1333 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
1334 dodie "failed to tar modules";
1336 run_ssh "rm -f /tmp/$modtar";
1342 # get the release name
1343 doprint "$make kernelrelease ... ";
1344 $version = `$make kernelrelease | tail -1`;
1346 doprint "$version\n";
1349 sub start_monitor_and_boot {
1358 sub check_buildlog {
1361 my @files = `git show $patch | diffstat -l`;
1363 open(IN, "git show $patch |") or
1364 dodie "failed to show $patch";
1366 if (m,^--- a/(.*),) {
1368 $files[$#files] = $1;
1373 open(IN, $buildlog) or dodie "Can't open $buildlog";
1375 if (/^\s*(.*?):.*(warning|error)/) {
1377 foreach my $file (@files) {
1378 my $fullpath = "$builddir/$file";
1379 if ($file eq $err || $fullpath eq $err) {
1380 fail "$file built with warnings" and return 0;
1390 sub apply_min_config {
1391 my $outconfig = "$output_config.new";
1393 # Read the config file and remove anything that
1394 # is in the force_config hash (from minconfig and others)
1395 # then add the force config back.
1397 doprint "Applying minimum configurations into $output_config.new\n";
1399 open (OUT, ">$outconfig") or
1400 dodie "Can't create $outconfig";
1402 if (-f $output_config) {
1403 open (IN, $output_config) or
1404 dodie "Failed to open $output_config";
1406 if (/^(# )?(CONFIG_[^\s=]*)/) {
1407 next if (defined($force_config{$2}));
1413 foreach my $config (keys %force_config) {
1414 print OUT "$force_config{$config}\n";
1418 run_command "mv $outconfig $output_config";
1421 sub make_oldconfig {
1423 my @force_list = keys %force_config;
1425 if ($#force_list >= 0) {
1429 if (!run_command "$make oldnoconfig") {
1430 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1431 # try a yes '' | oldconfig
1432 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
1433 run_command "yes '' | $make oldconfig" or
1434 dodie "failed make config oldconfig";
1438 # read a config file and use this to force new configs.
1439 sub load_force_config {
1442 open(IN, $config) or
1443 dodie "failed to read $config";
1446 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1447 $force_config{$1} = $_;
1448 } elsif (/^# (CONFIG_\S*) is not set/) {
1449 $force_config{$1} = $_;
1460 # Failed builds should not reboot the target
1461 my $save_no_reboot = $no_reboot;
1464 if (defined($pre_build)) {
1465 my $ret = run_command $pre_build;
1466 if (!$ret && defined($pre_build_die) &&
1468 dodie "failed to pre_build\n";
1472 if ($type =~ /^useconfig:(.*)/) {
1473 run_command "cp $1 $output_config" or
1474 dodie "could not copy $1 to .config";
1476 $type = "oldconfig";
1479 # old config can ask questions
1480 if ($type eq "oldconfig") {
1481 $type = "oldnoconfig";
1483 # allow for empty configs
1484 run_command "touch $output_config";
1487 run_command "mv $output_config $outputdir/config_temp" or
1488 dodie "moving .config";
1490 run_command "$make mrproper" or dodie "make mrproper";
1492 run_command "mv $outputdir/config_temp $output_config" or
1493 dodie "moving config_temp";
1496 } elsif (!$noclean) {
1497 unlink "$output_config";
1498 run_command "$make mrproper" or
1499 dodie "make mrproper";
1502 # add something to distinguish this build
1503 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1504 print OUT "$localversion\n";
1507 if (defined($minconfig)) {
1508 load_force_config($minconfig);
1511 if ($type ne "oldnoconfig") {
1512 run_command "$make $type" or
1513 dodie "failed make config";
1515 # Run old config regardless, to enforce min configurations
1518 $redirect = "$buildlog";
1519 my $build_ret = run_command "$make $build_options";
1522 if (defined($post_build)) {
1523 my $ret = run_command $post_build;
1524 if (!$ret && defined($post_build_die) &&
1526 dodie "failed to post_build\n";
1531 # bisect may need this to pass
1533 $no_reboot = $save_no_reboot;
1536 fail "failed build" and return 0;
1539 $no_reboot = $save_no_reboot;
1545 if (!run_ssh "halt" or defined($power_off)) {
1546 if (defined($poweroff_after_halt)) {
1547 sleep $poweroff_after_halt;
1548 run_command "$power_off";
1552 run_command "$power_off";
1563 if (defined($test_name)) {
1564 $name = " ($test_name)";
1567 doprint "\n\n*******************************************\n";
1568 doprint "*******************************************\n";
1569 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
1570 doprint "*******************************************\n";
1571 doprint "*******************************************\n";
1573 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
1574 doprint "Reboot and wait $sleep_time seconds\n";
1581 doprint "Pass or fail? [p/f]";
1584 if ($ans eq "p" || $ans eq "P") {
1586 } elsif ($ans eq "f" || $ans eq "F") {
1589 print "Please answer 'P' or 'F'\n";
1594 sub child_run_test {
1597 # child should have no power
1598 $reboot_on_error = 0;
1599 $poweroff_on_error = 0;
1600 $die_on_failure = 1;
1602 run_command $run_test or $failed = 1;
1608 sub child_finished {
1621 doprint "run test $run_test\n";
1625 $SIG{CHLD} = qw(child_finished);
1629 child_run_test if (!$child_pid);
1634 $line = wait_for_input($monitor_fp, 1);
1635 if (defined($line)) {
1637 # we are not guaranteed to get a full line
1638 $full_line .= $line;
1641 if ($full_line =~ /call trace:/i) {
1645 if ($full_line =~ /Kernel panic -/) {
1649 if ($line =~ /\n/) {
1653 } while (!$child_done && !$bug);
1656 my $failure_start = time;
1659 $line = wait_for_input($monitor_fp, 1);
1660 if (defined($line)) {
1664 if ($now - $failure_start >= $stop_after_failure) {
1667 } while (defined($line));
1669 doprint "Detected kernel crash!\n";
1670 # kill the child with extreme prejudice
1674 waitpid $child_pid, 0;
1677 if ($bug || $child_exit) {
1678 return 0 if $in_bisect;
1679 fail "test failed" and return 0;
1684 sub run_git_bisect {
1687 doprint "$command ... ";
1689 my $output = `$command 2>&1`;
1696 dodie "Failed to git bisect";
1699 doprint "SUCCESS\n";
1700 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1701 doprint "$1 [$2]\n";
1702 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
1704 doprint "Found bad commit... $1\n";
1707 # we already logged it, just print it now.
1715 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
1716 reboot $bisect_sleep_time;
1719 # returns 1 on success, 0 on failure, -1 on skip
1720 sub run_bisect_test {
1721 my ($type, $buildtype) = @_;
1730 build $buildtype or $failed = 1;
1732 if ($type ne "build") {
1733 if ($failed && $bisect_skip) {
1737 dodie "Failed on build" if $failed;
1740 start_monitor_and_boot or $failed = 1;
1742 if ($type ne "boot") {
1743 if ($failed && $bisect_skip) {
1749 dodie "Failed on boot" if $failed;
1751 do_run_test or $failed = 1;
1762 # reboot the box to a kernel we can ssh to
1763 if ($type ne "build") {
1773 my $buildtype = "oldconfig";
1775 # We should have a minconfig to use?
1776 if (defined($minconfig)) {
1777 $buildtype = "useconfig:$minconfig";
1780 my $ret = run_bisect_test $type, $buildtype;
1782 if ($bisect_manual) {
1783 $ret = answer_bisect;
1786 # Are we looking for where it worked, not failed?
1787 if ($reverse_bisect) {
1793 } elsif ($ret == 0) {
1795 } elsif ($bisect_skip) {
1796 doprint "HIT A BAD COMMIT ... SKIPPING\n";
1806 die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
1807 die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
1808 die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
1810 my $good = $opt{"BISECT_GOOD[$i]"};
1811 my $bad = $opt{"BISECT_BAD[$i]"};
1812 my $type = $opt{"BISECT_TYPE[$i]"};
1813 my $start = $opt{"BISECT_START[$i]"};
1814 my $replay = $opt{"BISECT_REPLAY[$i]"};
1815 my $start_files = $opt{"BISECT_FILES[$i]"};
1817 if (defined($start_files)) {
1818 $start_files = " -- " . $start_files;
1823 # convert to true sha1's
1824 $good = get_sha1($good);
1825 $bad = get_sha1($bad);
1827 if (defined($opt{"BISECT_REVERSE[$i]"}) &&
1828 $opt{"BISECT_REVERSE[$i]"} == 1) {
1829 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
1830 $reverse_bisect = 1;
1832 $reverse_bisect = 0;
1835 # Can't have a test without having a test to run
1836 if ($type eq "test" && !defined($run_test)) {
1840 my $check = $opt{"BISECT_CHECK[$i]"};
1841 if (defined($check) && $check ne "0") {
1844 my $head = get_sha1("HEAD");
1846 if ($check ne "good") {
1847 doprint "TESTING BISECT BAD [$bad]\n";
1848 run_command "git checkout $bad" or
1849 die "Failed to checkout $bad";
1851 $result = run_bisect $type;
1853 if ($result ne "bad") {
1854 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
1858 if ($check ne "bad") {
1859 doprint "TESTING BISECT GOOD [$good]\n";
1860 run_command "git checkout $good" or
1861 die "Failed to checkout $good";
1863 $result = run_bisect $type;
1865 if ($result ne "good") {
1866 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
1870 # checkout where we started
1871 run_command "git checkout $head" or
1872 die "Failed to checkout $head";
1875 run_command "git bisect start$start_files" or
1876 dodie "could not start bisect";
1878 run_command "git bisect good $good" or
1879 dodie "could not set bisect good to $good";
1881 run_git_bisect "git bisect bad $bad" or
1882 dodie "could not set bisect bad to $bad";
1884 if (defined($replay)) {
1885 run_command "git bisect replay $replay" or
1886 dodie "failed to run replay";
1889 if (defined($start)) {
1890 run_command "git checkout $start" or
1891 dodie "failed to checkout $start";
1896 $result = run_bisect $type;
1897 $test = run_git_bisect "git bisect $result";
1900 run_command "git bisect log" or
1901 dodie "could not capture git bisect log";
1903 run_command "git bisect reset" or
1904 dodie "could not reset git bisect";
1906 doprint "Bad commit was [$bisect_bad]\n";
1919 sub assign_configs {
1920 my ($hash, $config) = @_;
1923 or dodie "Failed to read $config";
1926 if (/^((CONFIG\S*)=.*)/) {
1934 sub process_config_ignore {
1937 assign_configs \%config_ignore, $config;
1940 sub read_current_config {
1941 my ($config_ref) = @_;
1943 %{$config_ref} = ();
1944 undef %{$config_ref};
1946 my @key = keys %{$config_ref};
1948 print "did not delete!\n";
1951 open (IN, "$output_config");
1954 if (/^(CONFIG\S+)=(.*)/) {
1955 ${$config_ref}{$1} = $2;
1961 sub get_dependencies {
1964 my $arr = $dependency{$config};
1965 if (!defined($arr)) {
1971 foreach my $dep (@{$arr}) {
1972 print "ADD DEP $dep\n";
1973 @deps = (@deps, get_dependencies $dep);
1982 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
1984 foreach my $config (@configs) {
1985 print OUT "$config_set{$config}\n";
1986 my @deps = get_dependencies $config;
1987 foreach my $dep (@deps) {
1988 print OUT "$config_set{$dep}\n";
1992 foreach my $config (keys %config_ignore) {
1993 print OUT "$config_ignore{$config}\n";
2001 sub compare_configs {
2004 foreach my $item (keys %a) {
2005 if (!defined($b{$item})) {
2006 print "diff $item\n";
2014 print "diff2 $keys[0]\n";
2016 return -1 if ($#keys >= 0);
2021 sub run_config_bisect_test {
2024 return run_bisect_test $type, "oldconfig";
2027 sub process_passed {
2030 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2031 # Passed! All these configs are part of a good compile.
2032 # Add them to the min options.
2033 foreach my $config (keys %configs) {
2034 if (defined($config_list{$config})) {
2035 doprint " removing $config\n";
2036 $config_ignore{$config} = $config_list{$config};
2037 delete $config_list{$config};
2040 doprint "config copied to $outputdir/config_good\n";
2041 run_command "cp -f $output_config $outputdir/config_good";
2044 sub process_failed {
2047 doprint "\n\n***************************************\n";
2048 doprint "Found bad config: $config\n";
2049 doprint "***************************************\n\n";
2052 sub run_config_bisect {
2054 my @start_list = keys %config_list;
2056 if ($#start_list < 0) {
2057 doprint "No more configs to test!!!\n";
2061 doprint "***** RUN TEST ***\n";
2062 my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
2066 my $count = $#start_list + 1;
2067 doprint " $count configs to test\n";
2069 my $half = int($#start_list / 2);
2072 my @tophalf = @start_list[0 .. $half];
2074 create_config @tophalf;
2075 read_current_config \%current_config;
2077 $count = $#tophalf + 1;
2078 doprint "Testing $count configs\n";
2080 # make sure we test something
2081 foreach my $config (@tophalf) {
2082 if (defined($current_config{$config})) {
2088 # try the other half
2089 doprint "Top half produced no set configs, trying bottom half\n";
2090 @tophalf = @start_list[$half + 1 .. $#start_list];
2091 create_config @tophalf;
2092 read_current_config \%current_config;
2093 foreach my $config (@tophalf) {
2094 if (defined($current_config{$config})) {
2100 doprint "Failed: Can't make new config with current configs\n";
2101 foreach my $config (@start_list) {
2102 doprint " CONFIG: $config\n";
2106 $count = $#tophalf + 1;
2107 doprint "Testing $count configs\n";
2110 $ret = run_config_bisect_test $type;
2111 if ($bisect_manual) {
2112 $ret = answer_bisect;
2115 process_passed %current_config;
2119 doprint "This config had a failure.\n";
2120 doprint "Removing these configs that were not set in this config:\n";
2121 doprint "config copied to $outputdir/config_bad\n";
2122 run_command "cp -f $output_config $outputdir/config_bad";
2124 # A config exists in this group that was bad.
2125 foreach my $config (keys %config_list) {
2126 if (!defined($current_config{$config})) {
2127 doprint " removing $config\n";
2128 delete $config_list{$config};
2132 @start_list = @tophalf;
2134 if ($#start_list == 0) {
2135 process_failed $start_list[0];
2139 # remove half the configs we are looking at and see if
2141 $half = int($#start_list / 2);
2142 } while ($#start_list > 0);
2144 # we found a single config, try it again unless we are running manually
2146 if ($bisect_manual) {
2147 process_failed $start_list[0];
2151 my @tophalf = @start_list[0 .. 0];
2153 $ret = run_config_bisect_test $type;
2155 process_passed %current_config;
2159 process_failed $start_list[0];
2166 my $start_config = $opt{"CONFIG_BISECT[$i]"};
2168 my $tmpconfig = "$tmpdir/use_config";
2170 if (defined($config_bisect_good)) {
2171 process_config_ignore $config_bisect_good;
2174 # Make the file with the bad config and the min config
2175 if (defined($minconfig)) {
2176 # read the min config for things to ignore
2177 run_command "cp $minconfig $tmpconfig" or
2178 dodie "failed to copy $minconfig to $tmpconfig";
2183 if (-f $tmpconfig) {
2184 load_force_config($tmpconfig);
2185 process_config_ignore $tmpconfig;
2188 # now process the start config
2189 run_command "cp $start_config $output_config" or
2190 dodie "failed to copy $start_config to $output_config";
2192 # read directly what we want to check
2194 open (IN, $output_config)
2195 or dodie "faied to open $output_config";
2198 if (/^((CONFIG\S*)=.*)/) {
2199 $config_check{$2} = $1;
2204 # Now run oldconfig with the minconfig
2207 # check to see what we lost (or gained)
2208 open (IN, $output_config)
2209 or dodie "Failed to read $start_config";
2211 my %removed_configs;
2215 if (/^((CONFIG\S*)=.*)/) {
2216 # save off all options
2217 $config_set{$2} = $1;
2218 if (defined($config_check{$2})) {
2219 if (defined($config_ignore{$2})) {
2220 $removed_configs{$2} = $1;
2222 $config_list{$2} = $1;
2224 } elsif (!defined($config_ignore{$2})) {
2225 $added_configs{$2} = $1;
2226 $config_list{$2} = $1;
2232 my @confs = keys %removed_configs;
2234 doprint "Configs overridden by default configs and removed from check:\n";
2235 foreach my $config (@confs) {
2236 doprint " $config\n";
2239 @confs = keys %added_configs;
2241 doprint "Configs appearing in make oldconfig and added:\n";
2242 foreach my $config (@confs) {
2243 doprint " $config\n";
2250 # Sometimes kconfig does weird things. We must make sure
2251 # that the config we autocreate has everything we need
2252 # to test, otherwise we may miss testing configs, or
2253 # may not be able to create a new config.
2254 # Here we create a config with everything set.
2255 create_config (keys %config_list);
2256 read_current_config \%config_test;
2257 foreach my $config (keys %config_list) {
2258 if (!defined($config_test{$config})) {
2261 doprint "Configs not produced by kconfig (will not be checked):\n";
2263 doprint " $config\n";
2264 delete $config_list{$config};
2269 $ret = run_config_bisect;
2272 return $ret if ($ret < 0);
2277 sub patchcheck_reboot {
2278 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
2279 reboot $patchcheck_sleep_time;
2285 die "PATCHCHECK_START[$i] not defined\n"
2286 if (!defined($opt{"PATCHCHECK_START[$i]"}));
2287 die "PATCHCHECK_TYPE[$i] not defined\n"
2288 if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
2290 my $start = $opt{"PATCHCHECK_START[$i]"};
2293 if (defined($opt{"PATCHCHECK_END[$i]"})) {
2294 $end = $opt{"PATCHCHECK_END[$i]"};
2297 # Get the true sha1's since we can use things like HEAD~3
2298 $start = get_sha1($start);
2299 $end = get_sha1($end);
2301 my $type = $opt{"PATCHCHECK_TYPE[$i]"};
2303 # Can't have a test without having a test to run
2304 if ($type eq "test" && !defined($run_test)) {
2308 open (IN, "git log --pretty=oneline $end|") or
2309 dodie "could not get git list";
2315 $list[$#list+1] = $_;
2316 last if (/^$start/);
2320 if ($list[$#list] !~ /^$start/) {
2321 fail "SHA1 $start not found";
2324 # go backwards in the list
2325 @list = reverse @list;
2327 my $save_clean = $noclean;
2328 my %ignored_warnings;
2330 if (defined($ignore_warnings)) {
2331 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2332 $ignored_warnings{$sha1} = 1;
2337 foreach my $item (@list) {
2339 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2341 doprint "\nProcessing commit $item\n\n";
2343 run_command "git checkout $sha1" or
2344 die "Failed to checkout $sha1";
2346 # only clean on the first and last patch
2347 if ($item eq $list[0] ||
2348 $item eq $list[$#list]) {
2349 $noclean = $save_clean;
2354 if (defined($minconfig)) {
2355 build "useconfig:$minconfig" or return 0;
2357 # ?? no config to use?
2358 build "oldconfig" or return 0;
2362 if (!defined($ignored_warnings{$sha1})) {
2363 check_buildlog $sha1 or return 0;
2366 next if ($type eq "build");
2370 start_monitor_and_boot or $failed = 1;
2372 if (!$failed && $type ne "boot"){
2373 do_run_test or $failed = 1;
2376 return 0 if ($failed);
2396 # $config depends on $dep
2397 my ($config, $dep) = @_;
2399 if (defined($depends{$config})) {
2400 $depends{$config} .= " " . $dep;
2402 $depends{$config} = $dep;
2405 # record the number of configs depending on $dep
2406 if (defined $depcount{$dep}) {
2409 $depcount{$dep} = 1;
2413 # taken from streamline_config.pl
2425 if (! -f $kconfig) {
2426 doprint "file $kconfig does not exist, skipping\n";
2430 open(KIN, "$kconfig")
2431 or die "Can't open $kconfig";
2435 # Make sure that lines ending with \ continue
2437 $_ = $line . " " . $_;
2448 # collect any Kconfig sources
2449 if (/^source\s*"(.*)"/) {
2450 $kconfigs[$#kconfigs+1] = $1;
2454 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2458 for (my $i = 0; $i < $iflevel; $i++) {
2459 add_dep $config, $ifdeps[$i];
2462 # collect the depends for the config
2463 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2465 add_dep $config, $1;
2467 # Get the configs that select this config
2468 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2470 # selected by depends on config
2471 add_dep $1, $config;
2473 # Check for if statements
2474 } elsif (/^if\s+(.*\S)\s*$/) {
2476 # remove beginning and ending non text
2477 $deps =~ s/^[^a-zA-Z0-9_]*//;
2478 $deps =~ s/[^a-zA-Z0-9_]*$//;
2480 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2482 $ifdeps[$iflevel++] = join ':', @deps;
2484 } elsif (/^endif/) {
2486 $iflevel-- if ($iflevel);
2489 } elsif (/^\s*help\s*$/) {
2495 # read in any configs that were found.
2496 foreach $kconfig (@kconfigs) {
2497 if (!defined($read_kconfigs{$kconfig})) {
2498 $read_kconfigs{$kconfig} = 1;
2499 read_kconfig("$builddir/$kconfig");
2505 # find out which arch this is by the kconfig file
2506 open (IN, $output_config)
2507 or dodie "Failed to read $output_config";
2510 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2517 if (!defined($arch)) {
2518 doprint "Could not find arch from config file\n";
2519 doprint "no dependencies used\n";
2523 # arch is really the subarch, we need to know
2524 # what directory to look at.
2525 if ($arch eq "i386" || $arch eq "x86_64") {
2527 } elsif ($arch =~ /^tile/) {
2531 my $kconfig = "$builddir/arch/$arch/Kconfig";
2533 if (! -f $kconfig && $arch =~ /\d$/) {
2535 # some subarchs have numbers, truncate them
2537 $kconfig = "$builddir/arch/$arch/Kconfig";
2538 if (! -f $kconfig) {
2539 doprint "No idea what arch dir $orig is for\n";
2540 doprint "no dependencies used\n";
2545 read_kconfig($kconfig);
2548 sub read_config_list {
2552 or dodie "Failed to read $config";
2555 if (/^((CONFIG\S*)=.*)/) {
2556 if (!defined($config_ignore{$2})) {
2557 $config_list{$2} = $1;
2565 sub read_output_config {
2568 assign_configs \%config_ignore, $config;
2571 sub make_new_config {
2574 open (OUT, ">$output_config")
2575 or dodie "Failed to write $output_config";
2577 foreach my $config (@configs) {
2578 print OUT "$config\n";
2586 $config =~ s/CONFIG_//;
2594 my $kconfig = chomp_config $dep;
2596 $dep = $depends{"$kconfig"};
2598 # the dep string we have saves the dependencies as they
2599 # were found, including expressions like ! && ||. We
2600 # want to split this out into just an array of configs.
2602 my $valid = "A-Za-z_0-9";
2606 while ($dep =~ /[$valid]/) {
2608 if ($dep =~ /^[^$valid]*([$valid]+)/) {
2609 my $conf = "CONFIG_" . $1;
2611 $configs[$#configs + 1] = $conf;
2613 $dep =~ s/^[^$valid]*[$valid]+//;
2615 die "this should never happen";
2625 my %processed_configs;
2626 my %nochange_config;
2628 sub test_this_config {
2633 # if we already processed this config, skip it
2634 if (defined($processed_configs{$config})) {
2637 $processed_configs{$config} = 1;
2639 # if this config failed during this round, skip it
2640 if (defined($nochange_config{$config})) {
2644 my $kconfig = chomp_config $config;
2646 # Test dependencies first
2647 if (defined($depends{"$kconfig"})) {
2648 my @parents = get_depends $config;
2649 foreach my $parent (@parents) {
2650 # if the parent is in the min config, check it first
2651 next if (!defined($min_configs{$parent}));
2652 $found = test_this_config($parent);
2653 if (defined($found)) {
2659 # Remove this config from the list of configs
2660 # do a make oldnoconfig and then read the resulting
2661 # .config to make sure it is missing the config that
2663 my %configs = %min_configs;
2664 delete $configs{$config};
2665 make_new_config ((values %configs), (values %keep_configs));
2668 assign_configs \%configs, $output_config;
2670 return $config if (!defined($configs{$config}));
2672 doprint "disabling config $config did not change .config\n";
2674 $nochange_config{$config} = 1;
2679 sub make_min_config {
2682 if (!defined($output_minconfig)) {
2683 fail "OUTPUT_MIN_CONFIG not defined" and return;
2686 # If output_minconfig exists, and the start_minconfig
2687 # came from min_config, than ask if we should use
2689 if (-f $output_minconfig && !$start_minconfig_defined) {
2690 print "$output_minconfig exists\n";
2691 if (read_yn " Use it as minconfig?") {
2692 $start_minconfig = $output_minconfig;
2696 if (!defined($start_minconfig)) {
2697 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
2700 my $temp_config = "$tmpdir/temp_config";
2702 # First things first. We build an allnoconfig to find
2703 # out what the defaults are that we can't touch.
2704 # Some are selections, but we really can't handle selections.
2706 my $save_minconfig = $minconfig;
2709 run_command "$make allnoconfig" or return 0;
2713 process_config_ignore $output_config;
2715 undef %save_configs;
2718 if (defined($ignore_config)) {
2719 # make sure the file exists
2720 `touch $ignore_config`;
2721 assign_configs \%save_configs, $ignore_config;
2724 %keep_configs = %save_configs;
2726 doprint "Load initial configs from $start_minconfig\n";
2728 # Look at the current min configs, and save off all the
2729 # ones that were set via the allnoconfig
2730 assign_configs \%min_configs, $start_minconfig;
2732 my @config_keys = keys %min_configs;
2734 # All configs need a depcount
2735 foreach my $config (@config_keys) {
2736 my $kconfig = chomp_config $config;
2737 if (!defined $depcount{$kconfig}) {
2738 $depcount{$kconfig} = 0;
2742 # Remove anything that was set by the make allnoconfig
2743 # we shouldn't need them as they get set for us anyway.
2744 foreach my $config (@config_keys) {
2745 # Remove anything in the ignore_config
2746 if (defined($keep_configs{$config})) {
2747 my $file = $ignore_config;
2748 $file =~ s,.*/(.*?)$,$1,;
2749 doprint "$config set by $file ... ignored\n";
2750 delete $min_configs{$config};
2753 # But make sure the settings are the same. If a min config
2754 # sets a selection, we do not want to get rid of it if
2755 # it is not the same as what we have. Just move it into
2757 if (defined($config_ignore{$config})) {
2758 if ($config_ignore{$config} ne $min_configs{$config}) {
2759 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
2760 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
2761 $keep_configs{$config} = $min_configs{$config};
2763 doprint "$config set by allnoconfig ... ignored\n";
2765 delete $min_configs{$config};
2777 # Now disable each config one by one and do a make oldconfig
2778 # till we find a config that changes our list.
2780 my @test_configs = keys %min_configs;
2782 # Sort keys by who is most dependent on
2783 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
2786 # Put configs that did not modify the config at the end.
2788 for (my $i = 0; $i < $#test_configs; $i++) {
2789 if (!defined($nochange_config{$test_configs[0]})) {
2793 # This config didn't change the .config last time.
2794 # Place it at the end
2795 my $config = shift @test_configs;
2796 push @test_configs, $config;
2799 # if every test config has failed to modify the .config file
2800 # in the past, then reset and start over.
2802 undef %nochange_config;
2805 undef %processed_configs;
2807 foreach my $config (@test_configs) {
2809 $found = test_this_config $config;
2811 last if (defined($found));
2813 # oh well, try another config
2816 if (!defined($found)) {
2817 # we could have failed due to the nochange_config hash
2818 # reset and try again
2820 undef %nochange_config;
2824 doprint "No more configs found that we can disable\n";
2832 doprint "Test with $config disabled\n";
2834 # set in_bisect to keep build and monitor from dieing
2839 start_monitor_and_boot or $failed = 1;
2845 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
2846 # this config is needed, add it to the ignore list.
2847 $keep_configs{$config} = $min_configs{$config};
2848 $save_configs{$config} = $min_configs{$config};
2849 delete $min_configs{$config};
2851 # update new ignore configs
2852 if (defined($ignore_config)) {
2853 open (OUT, ">$temp_config")
2854 or die "Can't write to $temp_config";
2855 foreach my $config (keys %save_configs) {
2856 print OUT "$save_configs{$config}\n";
2859 run_command "mv $temp_config $ignore_config" or
2860 dodie "failed to copy update to $ignore_config";
2864 # We booted without this config, remove it from the minconfigs.
2865 doprint "$config is not needed, disabling\n";
2867 delete $min_configs{$config};
2869 # Also disable anything that is not enabled in this config
2871 assign_configs \%configs, $output_config;
2872 my @config_keys = keys %min_configs;
2873 foreach my $config (@config_keys) {
2874 if (!defined($configs{$config})) {
2875 doprint "$config is not set, disabling\n";
2876 delete $min_configs{$config};
2880 # Save off all the current mandidory configs
2881 open (OUT, ">$temp_config")
2882 or die "Can't write to $temp_config";
2883 foreach my $config (keys %keep_configs) {
2884 print OUT "$keep_configs{$config}\n";
2886 foreach my $config (keys %min_configs) {
2887 print OUT "$min_configs{$config}\n";
2891 run_command "mv $temp_config $output_minconfig" or
2892 dodie "failed to copy update to $output_minconfig";
2895 doprint "Reboot and wait $sleep_time seconds\n";
2903 $#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
2906 $ktest_config = $ARGV[0];
2907 if (! -f $ktest_config) {
2908 print "$ktest_config does not exist.\n";
2909 if (!read_yn "Create it?") {
2914 $ktest_config = "ktest.conf";
2917 if (! -f $ktest_config) {
2918 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
2920 # Generated by ktest.pl
2922 # Define each test with TEST_START
2923 # The config options below it will override the defaults
2931 read_config $ktest_config;
2933 if (defined($opt{"LOG_FILE"})) {
2934 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
2937 # Append any configs entered in manually to the config file.
2938 my @new_configs = keys %entered_configs;
2939 if ($#new_configs >= 0) {
2940 print "\nAppending entered in configs to $ktest_config\n";
2941 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
2942 foreach my $config (@new_configs) {
2943 print OUT "$config = $entered_configs{$config}\n";
2944 $opt{$config} = $entered_configs{$config};
2948 if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
2949 unlink $opt{"LOG_FILE"};
2952 doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
2954 for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
2957 doprint "DEFAULT OPTIONS:\n";
2959 doprint "\nTEST $i OPTIONS";
2960 if (defined($repeat_tests{$i})) {
2961 $repeat = $repeat_tests{$i};
2962 doprint " ITERATE $repeat";
2967 foreach my $option (sort keys %opt) {
2969 if ($option =~ /\[(\d+)\]$/) {
2975 doprint "$option = $opt{$option}\n";
2979 sub __set_test_option {
2980 my ($name, $i) = @_;
2982 my $option = "$name\[$i\]";
2984 if (defined($opt{$option})) {
2985 return $opt{$option};
2988 foreach my $test (keys %repeat_tests) {
2990 $i < $test + $repeat_tests{$test}) {
2991 $option = "$name\[$test\]";
2992 if (defined($opt{$option})) {
2993 return $opt{$option};
2998 if (defined($opt{$name})) {
3005 sub set_test_option {
3006 my ($name, $i) = @_;
3008 my $option = __set_test_option($name, $i);
3009 return $option if (!defined($option));
3011 return eval_option($option, $i);
3014 # First we need to do is the builds
3015 for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
3017 # Do not reboot on failing test options
3022 my $makecmd = set_test_option("MAKE_CMD", $i);
3024 $machine = set_test_option("MACHINE", $i);
3025 $ssh_user = set_test_option("SSH_USER", $i);
3026 $tmpdir = set_test_option("TMP_DIR", $i);
3027 $outputdir = set_test_option("OUTPUT_DIR", $i);
3028 $builddir = set_test_option("BUILD_DIR", $i);
3029 $test_type = set_test_option("TEST_TYPE", $i);
3030 $build_type = set_test_option("BUILD_TYPE", $i);
3031 $build_options = set_test_option("BUILD_OPTIONS", $i);
3032 $pre_build = set_test_option("PRE_BUILD", $i);
3033 $post_build = set_test_option("POST_BUILD", $i);
3034 $pre_build_die = set_test_option("PRE_BUILD_DIE", $i);
3035 $post_build_die = set_test_option("POST_BUILD_DIE", $i);
3036 $power_cycle = set_test_option("POWER_CYCLE", $i);
3037 $reboot = set_test_option("REBOOT", $i);
3038 $noclean = set_test_option("BUILD_NOCLEAN", $i);
3039 $minconfig = set_test_option("MIN_CONFIG", $i);
3040 $output_minconfig = set_test_option("OUTPUT_MIN_CONFIG", $i);
3041 $start_minconfig = set_test_option("START_MIN_CONFIG", $i);
3042 $ignore_config = set_test_option("IGNORE_CONFIG", $i);
3043 $run_test = set_test_option("TEST", $i);
3044 $addconfig = set_test_option("ADD_CONFIG", $i);
3045 $reboot_type = set_test_option("REBOOT_TYPE", $i);
3046 $grub_menu = set_test_option("GRUB_MENU", $i);
3047 $post_install = set_test_option("POST_INSTALL", $i);
3048 $no_install = set_test_option("NO_INSTALL", $i);
3049 $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
3050 $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
3051 $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
3052 $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
3053 $power_off = set_test_option("POWER_OFF", $i);
3054 $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
3055 $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
3056 $sleep_time = set_test_option("SLEEP_TIME", $i);
3057 $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
3058 $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
3059 $ignore_warnings = set_test_option("IGNORE_WARNINGS", $i);
3060 $bisect_manual = set_test_option("BISECT_MANUAL", $i);
3061 $bisect_skip = set_test_option("BISECT_SKIP", $i);
3062 $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
3063 $store_failures = set_test_option("STORE_FAILURES", $i);
3064 $test_name = set_test_option("TEST_NAME", $i);
3065 $timeout = set_test_option("TIMEOUT", $i);
3066 $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
3067 $console = set_test_option("CONSOLE", $i);
3068 $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
3069 $success_line = set_test_option("SUCCESS_LINE", $i);
3070 $reboot_success_line = set_test_option("REBOOT_SUCCESS_LINE", $i);
3071 $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
3072 $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
3073 $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
3074 $build_target = set_test_option("BUILD_TARGET", $i);
3075 $ssh_exec = set_test_option("SSH_EXEC", $i);
3076 $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
3077 $target_image = set_test_option("TARGET_IMAGE", $i);
3078 $localversion = set_test_option("LOCALVERSION", $i);
3080 $start_minconfig_defined = 1;
3082 if (!defined($start_minconfig)) {
3083 $start_minconfig_defined = 0;
3084 $start_minconfig = $minconfig;
3087 chdir $builddir || die "can't change directory to $builddir";
3089 foreach my $dir ($tmpdir, $outputdir) {
3092 die "can't create $dir";
3096 $ENV{"SSH_USER"} = $ssh_user;
3097 $ENV{"MACHINE"} = $machine;
3099 $target = "$ssh_user\@$machine";
3101 $buildlog = "$tmpdir/buildlog-$machine";
3102 $dmesg = "$tmpdir/dmesg-$machine";
3103 $make = "$makecmd O=$outputdir";
3104 $output_config = "$outputdir/.config";
3106 if ($reboot_type eq "grub") {
3107 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3108 } elsif (!defined($reboot_script)) {
3109 dodie "REBOOT_SCRIPT not defined"
3112 my $run_type = $build_type;
3113 if ($test_type eq "patchcheck") {
3114 $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
3115 } elsif ($test_type eq "bisect") {
3116 $run_type = $opt{"BISECT_TYPE[$i]"};
3117 } elsif ($test_type eq "config_bisect") {
3118 $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
3121 if ($test_type eq "make_min_config") {
3125 # mistake in config file?
3126 if (!defined($run_type)) {
3127 $run_type = "ERROR";
3131 $installme = " no_install" if ($no_install);
3134 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
3139 if (defined($addconfig)) {
3140 my $min = $minconfig;
3141 if (!defined($minconfig)) {
3144 run_command "cat $addconfig $min > $tmpdir/add_config" or
3145 dodie "Failed to create temp config";
3146 $minconfig = "$tmpdir/add_config";
3149 my $checkout = $opt{"CHECKOUT[$i]"};
3150 if (defined($checkout)) {
3151 run_command "git checkout $checkout" or
3152 die "failed to checkout $checkout";
3158 if ($test_type eq "bisect") {
3161 } elsif ($test_type eq "config_bisect") {
3164 } elsif ($test_type eq "patchcheck") {
3167 } elsif ($test_type eq "make_min_config") {
3172 if ($build_type ne "nobuild") {
3173 build $build_type or next;
3176 if ($test_type eq "install") {
3183 if ($test_type ne "build") {
3185 start_monitor_and_boot or $failed = 1;
3187 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3188 do_run_test or $failed = 1;
3197 if ($opt{"POWEROFF_ON_SUCCESS"}) {
3199 } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
3203 doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";