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{"TEST_TYPE"} = "test";
26 $default{"BUILD_TYPE"} = "randconfig";
27 $default{"MAKE_CMD"} = "make";
28 $default{"TIMEOUT"} = 120;
29 $default{"TMP_DIR"} = "/tmp/ktest/\${MACHINE}";
30 $default{"SLEEP_TIME"} = 60; # sleep time between tests
31 $default{"BUILD_NOCLEAN"} = 0;
32 $default{"REBOOT_ON_ERROR"} = 0;
33 $default{"POWEROFF_ON_ERROR"} = 0;
34 $default{"REBOOT_ON_SUCCESS"} = 1;
35 $default{"POWEROFF_ON_SUCCESS"} = 0;
36 $default{"BUILD_OPTIONS"} = "";
37 $default{"BISECT_SLEEP_TIME"} = 60; # sleep time between bisects
38 $default{"PATCHCHECK_SLEEP_TIME"} = 60; # sleep time between patch checks
39 $default{"CLEAR_LOG"} = 0;
40 $default{"BISECT_MANUAL"} = 0;
41 $default{"BISECT_SKIP"} = 1;
42 $default{"SUCCESS_LINE"} = "login:";
43 $default{"DETECT_TRIPLE_FAULT"} = 1;
44 $default{"NO_INSTALL"} = 0;
45 $default{"BOOTED_TIMEOUT"} = 1;
46 $default{"DIE_ON_FAILURE"} = 1;
47 $default{"SSH_EXEC"} = "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND";
48 $default{"SCP_TO_TARGET"} = "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE";
49 $default{"REBOOT"} = "ssh \$SSH_USER\@\$MACHINE reboot";
50 $default{"STOP_AFTER_SUCCESS"} = 10;
51 $default{"STOP_AFTER_FAILURE"} = 60;
52 $default{"STOP_TEST_AFTER"} = 600;
54 # required, and we will ask users if they don't have them but we keep the default
55 # value something that is common.
56 $default{"REBOOT_TYPE"} = "grub";
57 $default{"LOCALVERSION"} = "-test";
58 $default{"SSH_USER"} = "root";
59 $default{"BUILD_TARGET"} = "arch/x86/boot/bzImage";
60 $default{"TARGET_IMAGE"} = "/boot/vmlinuz-test";
82 my $poweroff_on_error;
84 my $powercycle_after_reboot;
85 my $poweroff_after_halt;
98 my $start_minconfig_defined;
107 my $config_bisect_good;
108 my $in_patchcheck = 0;
118 my $bisect_sleep_time;
119 my $patchcheck_sleep_time;
126 my $detect_triplefault;
128 my $reboot_success_line;
130 my $stop_after_success;
131 my $stop_after_failure;
144 # do not force reboots on config problems
147 # default variables that can be used
148 chomp ($variable{"PWD"} = `pwd`);
150 $config_help{"MACHINE"} = << "EOF"
151 The machine hostname that you will test.
154 $config_help{"SSH_USER"} = << "EOF"
155 The box is expected to have ssh on normal bootup, provide the user
156 (most likely root, since you need privileged operations)
159 $config_help{"BUILD_DIR"} = << "EOF"
160 The directory that contains the Linux source code (full path).
161 You can use \${PWD} that will be the path where ktest.pl is run, or use
162 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
165 $config_help{"OUTPUT_DIR"} = << "EOF"
166 The directory that the objects will be built (full path).
167 (can not be same as BUILD_DIR)
168 You can use \${PWD} that will be the path where ktest.pl is run, or use
169 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
172 $config_help{"BUILD_TARGET"} = << "EOF"
173 The location of the compiled file to copy to the target.
174 (relative to OUTPUT_DIR)
177 $config_help{"TARGET_IMAGE"} = << "EOF"
178 The place to put your image on the test machine.
181 $config_help{"POWER_CYCLE"} = << "EOF"
182 A script or command to reboot the box.
184 Here is a digital loggers power switch example
185 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
187 Here is an example to reboot a virtual box on the current host
188 with the name "Guest".
189 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
192 $config_help{"CONSOLE"} = << "EOF"
193 The script or command that reads the console
195 If you use ttywatch server, something like the following would work.
196 CONSOLE = nc -d localhost 3001
198 For a virtual machine with guest name "Guest".
199 CONSOLE = virsh console Guest
202 $config_help{"LOCALVERSION"} = << "EOF"
203 Required version ending to differentiate the test
204 from other linux builds on the system.
207 $config_help{"REBOOT_TYPE"} = << "EOF"
208 Way to reboot the box to the test kernel.
209 Only valid options so far are "grub" and "script".
211 If you specify grub, it will assume grub version 1
212 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
213 and select that target to reboot to the kernel. If this is not
214 your setup, then specify "script" and have a command or script
215 specified in REBOOT_SCRIPT to boot to the target.
217 The entry in /boot/grub/menu.lst must be entered in manually.
218 The test will not modify that file.
221 $config_help{"GRUB_MENU"} = << "EOF"
222 The grub title name for the test kernel to boot
223 (Only mandatory if REBOOT_TYPE = grub)
225 Note, ktest.pl will not update the grub menu.lst, you need to
226 manually add an option for the test. ktest.pl will search
227 the grub menu.lst for this option to find what kernel to
230 For example, if in the /boot/grub/menu.lst the test kernel title has:
233 GRUB_MENU = Test Kernel
236 $config_help{"REBOOT_SCRIPT"} = << "EOF"
237 A script to reboot the target into the test kernel
238 (Only mandatory if REBOOT_TYPE = script)
248 print "$prompt [Y/n] ";
251 if ($ans =~ /^\s*$/) {
254 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
255 print "Please answer either 'y' or 'n'.\n";
257 if ($ans !~ /^y$/i) {
263 sub get_ktest_config {
267 return if (defined($opt{$config}));
269 if (defined($config_help{$config})) {
271 print $config_help{$config};
276 if (defined($default{$config})) {
277 print "\[$default{$config}\] ";
280 $ans =~ s/^\s*(.*\S)\s*$/$1/;
281 if ($ans =~ /^\s*$/) {
282 if ($default{$config}) {
283 $ans = $default{$config};
285 print "Your answer can not be blank\n";
289 $entered_configs{$config} = ${ans};
294 sub get_ktest_configs {
295 get_ktest_config("MACHINE");
296 get_ktest_config("SSH_USER");
297 get_ktest_config("BUILD_DIR");
298 get_ktest_config("OUTPUT_DIR");
299 get_ktest_config("BUILD_TARGET");
300 get_ktest_config("TARGET_IMAGE");
301 get_ktest_config("POWER_CYCLE");
302 get_ktest_config("CONSOLE");
303 get_ktest_config("LOCALVERSION");
305 my $rtype = $opt{"REBOOT_TYPE"};
307 if (!defined($rtype)) {
308 if (!defined($opt{"GRUB_MENU"})) {
309 get_ktest_config("REBOOT_TYPE");
310 $rtype = $entered_configs{"REBOOT_TYPE"};
316 if ($rtype eq "grub") {
317 get_ktest_config("GRUB_MENU");
319 get_ktest_config("REBOOT_SCRIPT");
323 sub process_variables {
324 my ($value, $remove_undef) = @_;
327 # We want to check for '\', and it is just easier
328 # to check the previous characet of '$' and not need
329 # to worry if '$' is the first character. By adding
330 # a space to $value, we can just check [^\\]\$ and
331 # it will still work.
334 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
338 # append beginning of value to retval
339 $retval = "$retval$begin";
340 if (defined($variable{$var})) {
341 $retval = "$retval$variable{$var}";
342 } elsif (defined($remove_undef) && $remove_undef) {
343 # for if statements, any variable that is not defined,
344 # we simple convert to 0
345 $retval = "${retval}0";
347 # put back the origin piece.
348 $retval = "$retval\$\{$var\}";
352 $retval = "$retval$value";
354 # remove the space added in the beginning
361 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
363 if (defined($opt{$lvalue})) {
364 if (!$override || defined(${$overrides}{$lvalue})) {
367 $extra = "In the same override section!\n";
369 die "$name: $.: Option $lvalue defined more than once!\n$extra";
371 ${$overrides}{$lvalue} = $rvalue;
373 if ($rvalue =~ /^\s*$/) {
374 delete $opt{$lvalue};
376 $rvalue = process_variables($rvalue);
377 $opt{$lvalue} = $rvalue;
382 my ($lvalue, $rvalue) = @_;
384 if ($rvalue =~ /^\s*$/) {
385 delete $variable{$lvalue};
387 $rvalue = process_variables($rvalue);
388 $variable{$lvalue} = $rvalue;
392 sub process_compare {
393 my ($lval, $cmp, $rval) = @_;
404 return $lval eq $rval;
405 } elsif ($cmp eq "!=") {
406 return $lval ne $rval;
409 my $statement = "$lval $cmp $rval";
410 my $ret = eval $statement;
412 # $@ stores error of eval
423 return defined($variable{$2}) ||
428 sub process_expression {
429 my ($name, $val) = @_;
433 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
436 if (process_expression($name, $express)) {
437 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
439 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
447 while ($val =~ s/^(.*?)($OR|$AND)//) {
451 if (process_expression($name, $express)) {
462 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
463 my $ret = process_compare($1, $2, $3);
465 die "$name: $.: Unable to process comparison\n";
470 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
472 return !value_defined($2);
474 return value_defined($2);
478 if ($val =~ /^\s*0\s*$/) {
480 } elsif ($val =~ /^\s*\d+\s*$/) {
484 die ("$name: $.: Undefined content $val in if statement\n");
488 my ($name, $value) = @_;
490 # Convert variables and replace undefined ones with 0
491 my $val = process_variables($value, 1);
492 my $ret = process_expression $name, $val;
498 my ($config, $current_test_num) = @_;
501 open($in, $config) || die "can't read file $config";
504 $name =~ s,.*/(.*),$1,;
506 my $test_num = $$current_test_num;
509 my $num_tests_set = 0;
522 # ignore blank lines and comments
523 next if (/^\s*$/ || /\s*\#/);
525 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
535 if ($type eq "TEST_START") {
537 if ($num_tests_set) {
538 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
541 $old_test_num = $test_num;
542 $old_repeat = $repeat;
544 $test_num += $repeat;
551 # If SKIP is anywhere in the line, the command will be skipped
552 if ($rest =~ s/\s+SKIP\b//) {
559 if ($rest =~ s/\sELSE\b//) {
561 die "$name: $.: ELSE found with out matching IF section\n$_";
572 if ($rest =~ s/\sIF\s+(.*)//) {
573 if (process_if($name, $1)) {
585 if ($type eq "TEST_START") {
586 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
588 $repeat_tests{"$test_num"} = $repeat;
590 } elsif ($rest =~ s/\sOVERRIDE\b//) {
593 # Clear previous overrides
598 if (!$skip && $rest !~ /^\s*$/) {
599 die "$name: $.: Gargbage found after $type\n$_";
602 if ($skip && $type eq "TEST_START") {
603 $test_num = $old_test_num;
604 $repeat = $old_repeat;
607 } elsif (/^\s*ELSE\b(.*)$/) {
609 die "$name: $.: ELSE found with out matching IF section\n$_";
618 if ($rest =~ /\sIF\s+(.*)/) {
619 # May be a ELSE IF section.
620 if (!process_if($name, $1)) {
629 if ($rest !~ /^\s*$/) {
630 die "$name: $.: Gargbage found after DEFAULTS\n$_";
633 } elsif (/^\s*INCLUDE\s+(\S+)/) {
638 die "$name: $.: INCLUDE can only be done in default sections\n$_";
641 my $file = process_variables($1);
643 if ($file !~ m,^/,) {
644 # check the path of the config file first
645 if ($config =~ m,(.*)/,) {
653 die "$name: $.: Can't read file $file\n$_";
656 if (__read_config($file, \$test_num)) {
660 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
668 ($lvalue eq "NUM_TESTS" ||
669 $lvalue eq "LOG_FILE" ||
670 $lvalue eq "CLEAR_LOG")) {
671 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
674 if ($lvalue eq "NUM_TESTS") {
676 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
679 die "$name: $.: NUM_TESTS must be set in default section\n";
684 if ($default || $lvalue =~ /\[\d+\]$/) {
685 set_value($lvalue, $rvalue, $override, \%overrides, $name);
687 my $val = "$lvalue\[$test_num\]";
688 set_value($val, $rvalue, $override, \%overrides, $name);
691 $repeats{$val} = $repeat;
694 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
700 # process config variables.
701 # Config variables are only active while reading the
702 # config and can be defined anywhere. They also ignore
703 # TEST_START and DEFAULTS, but are skipped if they are in
704 # on of these sections that have SKIP defined.
705 # The save variable can be
706 # defined multiple times and the new one simply overrides
708 set_variable($lvalue, $rvalue);
711 die "$name: $.: Garbage found in config\n$_";
716 $test_num += $repeat - 1;
717 $opt{"NUM_TESTS"} = $test_num;
722 $$current_test_num = $test_num;
733 $test_case = __read_config $config, \$test_num;
735 # make sure we have all mandatory configs
738 # was a test specified?
740 print "No test case specified.\n";
741 print "What test case would you like to run?\n";
744 $default{"TEST_TYPE"} = $ans;
749 foreach my $default (keys %default) {
750 if (!defined($opt{$default})) {
751 $opt{$default} = $default{$default};
757 my ($option, $i) = @_;
759 # Add space to evaluate the character before $
760 $option = " $option";
765 foreach my $test (keys %repeat_tests) {
767 $i < $test + $repeat_tests{$test}) {
775 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
780 # Append beginning of line
781 $retval = "$retval$start";
783 # If the iteration option OPT[$i] exists, then use that.
784 # otherwise see if the default OPT (without [$i]) exists.
786 my $o = "$var\[$i\]";
787 my $parento = "$var\[$parent\]";
789 if (defined($opt{$o})) {
791 $retval = "$retval$o";
792 } elsif ($repeated && defined($opt{$parento})) {
794 $retval = "$retval$o";
795 } elsif (defined($opt{$var})) {
797 $retval = "$retval$o";
799 $retval = "$retval\$\{$var\}";
805 $retval = "$retval$option";
813 my ($option, $i) = @_;
817 # Since an option can evaluate to another option,
818 # keep iterating until we do not evaluate any more
821 while ($prev ne $option) {
822 # Check for recursive evaluations.
823 # 100 deep should be more than enough.
825 die "Over 100 evaluations accurred with $option\n" .
826 "Check for recursive variables\n";
829 $option = __eval_option($option, $i);
836 if (defined($opt{"LOG_FILE"})) {
837 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
844 if (defined($opt{"LOG_FILE"})) {
859 sub wait_for_monitor;
864 if (defined($time)) {
866 # flush out current monitor
867 # May contain the reboot success line
871 # try to reboot normally
872 if (run_command $reboot) {
873 if (defined($powercycle_after_reboot)) {
874 sleep $powercycle_after_reboot;
875 run_command "$power_cycle";
878 # nope? power cycle it.
879 run_command "$power_cycle";
882 if (defined($time)) {
883 wait_for_monitor($time, $reboot_success_line);
891 return $test_type eq "build" || $no_reboot ||
892 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
893 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
897 doprint "CRITICAL FAILURE... ", @_, "\n";
901 if ($reboot_on_error && !do_not_reboot) {
903 doprint "REBOOTING\n";
906 } elsif ($poweroff_on_error && defined($power_off)) {
907 doprint "POWERING OFF\n";
911 if (defined($opt{"LOG_FILE"})) {
912 print " See $opt{LOG_FILE} for more info.\n";
923 my $pid = open($fp, "$console|") or
924 dodie "Can't open console $console";
926 $flags = fcntl($fp, F_GETFL, 0) or
927 dodie "Can't get flags for the socket: $!";
928 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
929 dodie "Can't set flags for the socket: $!";
937 doprint "kill child process $pid\n";
945 if ($monitor_cnt++) {
948 $monitor_fp = \*MONFD;
949 $monitor_pid = open_console $monitor_fp;
953 open(MONFD, "Stop perl from warning about single use of MONFD");
957 if (--$monitor_cnt) {
960 close_console($monitor_fp, $monitor_pid);
963 sub wait_for_monitor {
964 my ($time, $stop) = @_;
969 doprint "** Wait for monitor to settle down **\n";
971 # read the monitor and wait for the system to calm down
973 $line = wait_for_input($monitor_fp, $time);
974 last if (!defined($line));
978 if (defined($stop) && $full_line =~ /$stop/) {
979 doprint "wait for monitor detected $stop\n";
987 print "** Monitor flushed **\n";
991 my ($result, $basedir) = @_;
993 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
994 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
996 my $type = $build_type;
997 if ($type =~ /useconfig/) {
1001 my $dir = "$machine-$test_type-$type-$result-$date";
1003 $dir = "$basedir/$dir";
1007 die "can't create $dir";
1011 "config" => $output_config,
1012 "buildlog" => $buildlog,
1014 "testlog" => $testlog,
1017 while (my ($name, $source) = each(%files)) {
1019 cp "$source", "$dir/$name" or
1020 die "failed to copy $source";
1024 doprint "*** Saved info to $dir ***\n";
1029 if ($die_on_failure) {
1037 # no need to reboot for just building.
1038 if (!do_not_reboot) {
1039 doprint "REBOOTING\n";
1045 if (defined($test_name)) {
1046 $name = " ($test_name)";
1049 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1050 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1051 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
1052 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1053 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1055 if (defined($store_failures)) {
1056 save_logs "fail", $store_failures;
1068 $command =~ s/\$SSH_USER/$ssh_user/g;
1069 $command =~ s/\$MACHINE/$machine/g;
1071 doprint("$command ... ");
1073 $pid = open(CMD, "$command 2>&1 |") or
1074 (fail "unable to exec $command" and return 0);
1076 if (defined($opt{"LOG_FILE"})) {
1077 open(LOG, ">>$opt{LOG_FILE}") or
1078 dodie "failed to write to log";
1082 if (defined($redirect)) {
1083 open (RD, ">$redirect") or
1084 dodie "failed to write to redirect $redirect";
1089 print LOG if ($dolog);
1090 print RD if ($dord);
1097 close(LOG) if ($dolog);
1098 close(RD) if ($dord);
1101 doprint "FAILED!\n";
1103 doprint "SUCCESS\n";
1111 my $cp_exec = $ssh_exec;
1113 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1114 return run_command "$cp_exec";
1118 my ($src, $dst) = @_;
1119 my $cp_scp = $scp_to_target;
1121 $cp_scp =~ s/\$SRC_FILE/$src/g;
1122 $cp_scp =~ s/\$DST_FILE/$dst/g;
1124 return run_command "$cp_scp";
1127 sub get_grub_index {
1129 if ($reboot_type ne "grub") {
1132 return if (defined($grub_number));
1134 doprint "Find grub menu ... ";
1137 my $ssh_grub = $ssh_exec;
1138 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1140 open(IN, "$ssh_grub |")
1141 or die "unable to get menu.lst";
1146 if (/^\s*title\s+$grub_menu\s*$/) {
1150 } elsif (/^\s*title\s/) {
1156 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
1158 doprint "$grub_number\n";
1163 my ($fp, $time) = @_;
1169 if (!defined($time)) {
1174 vec($rin, fileno($fp), 1) = 1;
1175 $ready = select($rin, undef, undef, $time);
1179 # try to read one char at a time
1180 while (sysread $fp, $ch, 1) {
1182 last if ($ch eq "\n");
1185 if (!length($line)) {
1193 if ($reboot_type eq "grub") {
1194 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1199 run_command "$reboot_script";
1205 doprint "git rev-list --max-count=1 $commit ... ";
1206 my $sha1 = `git rev-list --max-count=1 $commit`;
1213 dodie "Failed to get git $commit";
1226 my $skip_call_trace = 0;
1234 open(DMESG, "> $dmesg") or
1235 die "unable to write to $dmesg";
1241 my $monitor_start = time;
1243 my $version_found = 0;
1247 if ($bug && defined($stop_after_failure) &&
1248 $stop_after_failure >= 0) {
1249 my $time = $stop_after_failure - (time - $failure_start);
1250 $line = wait_for_input($monitor_fp, $time);
1251 if (!defined($line)) {
1252 doprint "bug timed out after $booted_timeout seconds\n";
1253 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1257 $line = wait_for_input($monitor_fp, $booted_timeout);
1258 if (!defined($line)) {
1259 my $s = $booted_timeout == 1 ? "" : "s";
1260 doprint "Successful boot found: break after $booted_timeout second$s\n";
1264 $line = wait_for_input($monitor_fp);
1265 if (!defined($line)) {
1266 my $s = $timeout == 1 ? "" : "s";
1267 doprint "Timed out after $timeout second$s\n";
1275 # we are not guaranteed to get a full line
1276 $full_line .= $line;
1278 if ($full_line =~ /$success_line/) {
1280 $success_start = time;
1283 if ($booted && defined($stop_after_success) &&
1284 $stop_after_success >= 0) {
1286 if ($now - $success_start >= $stop_after_success) {
1287 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1292 if ($full_line =~ /\[ backtrace testing \]/) {
1293 $skip_call_trace = 1;
1296 if ($full_line =~ /call trace:/i) {
1297 if (!$bug && !$skip_call_trace) {
1299 $failure_start = time;
1303 if ($bug && defined($stop_after_failure) &&
1304 $stop_after_failure >= 0) {
1306 if ($now - $failure_start >= $stop_after_failure) {
1307 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1312 if ($full_line =~ /\[ end of backtrace testing \]/) {
1313 $skip_call_trace = 0;
1316 if ($full_line =~ /Kernel panic -/) {
1317 $failure_start = time;
1321 # Detect triple faults by testing the banner
1322 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1323 if ($1 eq $version) {
1325 } elsif ($version_found && $detect_triplefault) {
1326 # We already booted into the kernel we are testing,
1327 # but now we booted into another kernel?
1328 # Consider this a triple fault.
1329 doprint "Aleady booted in Linux kernel $version, but now\n";
1330 doprint "we booted into Linux kernel $1.\n";
1331 doprint "Assuming that this is a triple fault.\n";
1332 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1337 if ($line =~ /\n/) {
1341 if ($stop_test_after > 0 && !$booted && !$bug) {
1342 if (time - $monitor_start > $stop_test_after) {
1343 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
1352 return 0 if ($in_bisect);
1353 fail "failed - got a bug report" and return 0;
1357 return 0 if ($in_bisect);
1358 fail "failed - never got a boot prompt." and return 0;
1364 sub do_post_install {
1366 return if (!defined($post_install));
1368 my $cp_post_install = $post_install;
1369 $cp_post_install =~ s/\$KERNEL_VERSION/$version/g;
1370 run_command "$cp_post_install" or
1371 dodie "Failed to run post install";
1376 return if ($no_install);
1378 run_scp "$outputdir/$build_target", "$target_image" or
1379 dodie "failed to copy image";
1381 my $install_mods = 0;
1383 # should we process modules?
1385 open(IN, "$output_config") or dodie("Can't read config file");
1387 if (/CONFIG_MODULES(=y)?/) {
1388 $install_mods = 1 if (defined($1));
1394 if (!$install_mods) {
1396 doprint "No modules needed\n";
1400 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
1401 dodie "Failed to install modules";
1403 my $modlib = "/lib/modules/$version";
1404 my $modtar = "ktest-mods.tar.bz2";
1406 run_ssh "rm -rf $modlib" or
1407 dodie "failed to remove old mods: $modlib";
1409 # would be nice if scp -r did not follow symbolic links
1410 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
1411 dodie "making tarball";
1413 run_scp "$tmpdir/$modtar", "/tmp" or
1414 dodie "failed to copy modules";
1416 unlink "$tmpdir/$modtar";
1418 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
1419 dodie "failed to tar modules";
1421 run_ssh "rm -f /tmp/$modtar";
1427 # get the release name
1428 doprint "$make kernelrelease ... ";
1429 $version = `$make kernelrelease | tail -1`;
1431 doprint "$version\n";
1434 sub start_monitor_and_boot {
1435 # Make sure the stable kernel has finished booting
1448 sub check_buildlog {
1451 my @files = `git show $patch | diffstat -l`;
1453 open(IN, "git show $patch |") or
1454 dodie "failed to show $patch";
1456 if (m,^--- a/(.*),) {
1458 $files[$#files] = $1;
1463 open(IN, $buildlog) or dodie "Can't open $buildlog";
1465 if (/^\s*(.*?):.*(warning|error)/) {
1467 foreach my $file (@files) {
1468 my $fullpath = "$builddir/$file";
1469 if ($file eq $err || $fullpath eq $err) {
1470 fail "$file built with warnings" and return 0;
1480 sub apply_min_config {
1481 my $outconfig = "$output_config.new";
1483 # Read the config file and remove anything that
1484 # is in the force_config hash (from minconfig and others)
1485 # then add the force config back.
1487 doprint "Applying minimum configurations into $output_config.new\n";
1489 open (OUT, ">$outconfig") or
1490 dodie "Can't create $outconfig";
1492 if (-f $output_config) {
1493 open (IN, $output_config) or
1494 dodie "Failed to open $output_config";
1496 if (/^(# )?(CONFIG_[^\s=]*)/) {
1497 next if (defined($force_config{$2}));
1503 foreach my $config (keys %force_config) {
1504 print OUT "$force_config{$config}\n";
1508 run_command "mv $outconfig $output_config";
1511 sub make_oldconfig {
1513 my @force_list = keys %force_config;
1515 if ($#force_list >= 0) {
1519 if (!run_command "$make oldnoconfig") {
1520 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1521 # try a yes '' | oldconfig
1522 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
1523 run_command "yes '' | $make oldconfig" or
1524 dodie "failed make config oldconfig";
1528 # read a config file and use this to force new configs.
1529 sub load_force_config {
1532 open(IN, $config) or
1533 dodie "failed to read $config";
1536 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1537 $force_config{$1} = $_;
1538 } elsif (/^# (CONFIG_\S*) is not set/) {
1539 $force_config{$1} = $_;
1550 # Failed builds should not reboot the target
1551 my $save_no_reboot = $no_reboot;
1554 if (defined($pre_build)) {
1555 my $ret = run_command $pre_build;
1556 if (!$ret && defined($pre_build_die) &&
1558 dodie "failed to pre_build\n";
1562 if ($type =~ /^useconfig:(.*)/) {
1563 run_command "cp $1 $output_config" or
1564 dodie "could not copy $1 to .config";
1566 $type = "oldconfig";
1569 # old config can ask questions
1570 if ($type eq "oldconfig") {
1571 $type = "oldnoconfig";
1573 # allow for empty configs
1574 run_command "touch $output_config";
1577 run_command "mv $output_config $outputdir/config_temp" or
1578 dodie "moving .config";
1580 run_command "$make mrproper" or dodie "make mrproper";
1582 run_command "mv $outputdir/config_temp $output_config" or
1583 dodie "moving config_temp";
1586 } elsif (!$noclean) {
1587 unlink "$output_config";
1588 run_command "$make mrproper" or
1589 dodie "make mrproper";
1592 # add something to distinguish this build
1593 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1594 print OUT "$localversion\n";
1597 if (defined($minconfig)) {
1598 load_force_config($minconfig);
1601 if ($type ne "oldnoconfig") {
1602 run_command "$make $type" or
1603 dodie "failed make config";
1605 # Run old config regardless, to enforce min configurations
1608 $redirect = "$buildlog";
1609 my $build_ret = run_command "$make $build_options";
1612 if (defined($post_build)) {
1613 my $ret = run_command $post_build;
1614 if (!$ret && defined($post_build_die) &&
1616 dodie "failed to post_build\n";
1621 # bisect may need this to pass
1623 $no_reboot = $save_no_reboot;
1626 fail "failed build" and return 0;
1629 $no_reboot = $save_no_reboot;
1635 if (!run_ssh "halt" or defined($power_off)) {
1636 if (defined($poweroff_after_halt)) {
1637 sleep $poweroff_after_halt;
1638 run_command "$power_off";
1642 run_command "$power_off";
1653 if (defined($test_name)) {
1654 $name = " ($test_name)";
1657 doprint "\n\n*******************************************\n";
1658 doprint "*******************************************\n";
1659 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
1660 doprint "*******************************************\n";
1661 doprint "*******************************************\n";
1663 if (defined($store_successes)) {
1664 save_logs "success", $store_successes;
1667 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
1668 doprint "Reboot and wait $sleep_time seconds\n";
1675 doprint "Pass or fail? [p/f]";
1678 if ($ans eq "p" || $ans eq "P") {
1680 } elsif ($ans eq "f" || $ans eq "F") {
1683 print "Please answer 'P' or 'F'\n";
1688 sub child_run_test {
1691 # child should have no power
1692 $reboot_on_error = 0;
1693 $poweroff_on_error = 0;
1694 $die_on_failure = 1;
1696 $redirect = "$testlog";
1697 run_command $run_test or $failed = 1;
1705 sub child_finished {
1718 doprint "run test $run_test\n";
1722 $SIG{CHLD} = qw(child_finished);
1726 child_run_test if (!$child_pid);
1731 $line = wait_for_input($monitor_fp, 1);
1732 if (defined($line)) {
1734 # we are not guaranteed to get a full line
1735 $full_line .= $line;
1738 if ($full_line =~ /call trace:/i) {
1742 if ($full_line =~ /Kernel panic -/) {
1746 if ($line =~ /\n/) {
1750 } while (!$child_done && !$bug);
1753 my $failure_start = time;
1756 $line = wait_for_input($monitor_fp, 1);
1757 if (defined($line)) {
1761 if ($now - $failure_start >= $stop_after_failure) {
1764 } while (defined($line));
1766 doprint "Detected kernel crash!\n";
1767 # kill the child with extreme prejudice
1771 waitpid $child_pid, 0;
1774 if ($bug || $child_exit) {
1775 return 0 if $in_bisect;
1776 fail "test failed" and return 0;
1781 sub run_git_bisect {
1784 doprint "$command ... ";
1786 my $output = `$command 2>&1`;
1793 dodie "Failed to git bisect";
1796 doprint "SUCCESS\n";
1797 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1798 doprint "$1 [$2]\n";
1799 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
1801 doprint "Found bad commit... $1\n";
1804 # we already logged it, just print it now.
1812 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
1813 reboot $bisect_sleep_time;
1816 # returns 1 on success, 0 on failure, -1 on skip
1817 sub run_bisect_test {
1818 my ($type, $buildtype) = @_;
1827 build $buildtype or $failed = 1;
1829 if ($type ne "build") {
1830 if ($failed && $bisect_skip) {
1834 dodie "Failed on build" if $failed;
1837 start_monitor_and_boot or $failed = 1;
1839 if ($type ne "boot") {
1840 if ($failed && $bisect_skip) {
1846 dodie "Failed on boot" if $failed;
1848 do_run_test or $failed = 1;
1859 # reboot the box to a kernel we can ssh to
1860 if ($type ne "build") {
1870 my $buildtype = "oldconfig";
1872 # We should have a minconfig to use?
1873 if (defined($minconfig)) {
1874 $buildtype = "useconfig:$minconfig";
1877 my $ret = run_bisect_test $type, $buildtype;
1879 if ($bisect_manual) {
1880 $ret = answer_bisect;
1883 # Are we looking for where it worked, not failed?
1884 if ($reverse_bisect) {
1890 } elsif ($ret == 0) {
1892 } elsif ($bisect_skip) {
1893 doprint "HIT A BAD COMMIT ... SKIPPING\n";
1903 die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
1904 die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
1905 die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
1907 my $good = $opt{"BISECT_GOOD[$i]"};
1908 my $bad = $opt{"BISECT_BAD[$i]"};
1909 my $type = $opt{"BISECT_TYPE[$i]"};
1910 my $start = $opt{"BISECT_START[$i]"};
1911 my $replay = $opt{"BISECT_REPLAY[$i]"};
1912 my $start_files = $opt{"BISECT_FILES[$i]"};
1914 if (defined($start_files)) {
1915 $start_files = " -- " . $start_files;
1920 # convert to true sha1's
1921 $good = get_sha1($good);
1922 $bad = get_sha1($bad);
1924 if (defined($opt{"BISECT_REVERSE[$i]"}) &&
1925 $opt{"BISECT_REVERSE[$i]"} == 1) {
1926 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
1927 $reverse_bisect = 1;
1929 $reverse_bisect = 0;
1932 # Can't have a test without having a test to run
1933 if ($type eq "test" && !defined($run_test)) {
1937 my $check = $opt{"BISECT_CHECK[$i]"};
1938 if (defined($check) && $check ne "0") {
1941 my $head = get_sha1("HEAD");
1943 if ($check ne "good") {
1944 doprint "TESTING BISECT BAD [$bad]\n";
1945 run_command "git checkout $bad" or
1946 die "Failed to checkout $bad";
1948 $result = run_bisect $type;
1950 if ($result ne "bad") {
1951 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
1955 if ($check ne "bad") {
1956 doprint "TESTING BISECT GOOD [$good]\n";
1957 run_command "git checkout $good" or
1958 die "Failed to checkout $good";
1960 $result = run_bisect $type;
1962 if ($result ne "good") {
1963 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
1967 # checkout where we started
1968 run_command "git checkout $head" or
1969 die "Failed to checkout $head";
1972 run_command "git bisect start$start_files" or
1973 dodie "could not start bisect";
1975 run_command "git bisect good $good" or
1976 dodie "could not set bisect good to $good";
1978 run_git_bisect "git bisect bad $bad" or
1979 dodie "could not set bisect bad to $bad";
1981 if (defined($replay)) {
1982 run_command "git bisect replay $replay" or
1983 dodie "failed to run replay";
1986 if (defined($start)) {
1987 run_command "git checkout $start" or
1988 dodie "failed to checkout $start";
1993 $result = run_bisect $type;
1994 $test = run_git_bisect "git bisect $result";
1997 run_command "git bisect log" or
1998 dodie "could not capture git bisect log";
2000 run_command "git bisect reset" or
2001 dodie "could not reset git bisect";
2003 doprint "Bad commit was [$bisect_bad]\n";
2016 sub assign_configs {
2017 my ($hash, $config) = @_;
2020 or dodie "Failed to read $config";
2023 if (/^((CONFIG\S*)=.*)/) {
2031 sub process_config_ignore {
2034 assign_configs \%config_ignore, $config;
2037 sub read_current_config {
2038 my ($config_ref) = @_;
2040 %{$config_ref} = ();
2041 undef %{$config_ref};
2043 my @key = keys %{$config_ref};
2045 print "did not delete!\n";
2048 open (IN, "$output_config");
2051 if (/^(CONFIG\S+)=(.*)/) {
2052 ${$config_ref}{$1} = $2;
2058 sub get_dependencies {
2061 my $arr = $dependency{$config};
2062 if (!defined($arr)) {
2068 foreach my $dep (@{$arr}) {
2069 print "ADD DEP $dep\n";
2070 @deps = (@deps, get_dependencies $dep);
2079 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2081 foreach my $config (@configs) {
2082 print OUT "$config_set{$config}\n";
2083 my @deps = get_dependencies $config;
2084 foreach my $dep (@deps) {
2085 print OUT "$config_set{$dep}\n";
2089 foreach my $config (keys %config_ignore) {
2090 print OUT "$config_ignore{$config}\n";
2098 sub compare_configs {
2101 foreach my $item (keys %a) {
2102 if (!defined($b{$item})) {
2103 print "diff $item\n";
2111 print "diff2 $keys[0]\n";
2113 return -1 if ($#keys >= 0);
2118 sub run_config_bisect_test {
2121 return run_bisect_test $type, "oldconfig";
2124 sub process_passed {
2127 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2128 # Passed! All these configs are part of a good compile.
2129 # Add them to the min options.
2130 foreach my $config (keys %configs) {
2131 if (defined($config_list{$config})) {
2132 doprint " removing $config\n";
2133 $config_ignore{$config} = $config_list{$config};
2134 delete $config_list{$config};
2137 doprint "config copied to $outputdir/config_good\n";
2138 run_command "cp -f $output_config $outputdir/config_good";
2141 sub process_failed {
2144 doprint "\n\n***************************************\n";
2145 doprint "Found bad config: $config\n";
2146 doprint "***************************************\n\n";
2149 sub run_config_bisect {
2151 my @start_list = keys %config_list;
2153 if ($#start_list < 0) {
2154 doprint "No more configs to test!!!\n";
2158 doprint "***** RUN TEST ***\n";
2159 my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
2163 my $count = $#start_list + 1;
2164 doprint " $count configs to test\n";
2166 my $half = int($#start_list / 2);
2169 my @tophalf = @start_list[0 .. $half];
2171 create_config @tophalf;
2172 read_current_config \%current_config;
2174 $count = $#tophalf + 1;
2175 doprint "Testing $count configs\n";
2177 # make sure we test something
2178 foreach my $config (@tophalf) {
2179 if (defined($current_config{$config})) {
2185 # try the other half
2186 doprint "Top half produced no set configs, trying bottom half\n";
2187 @tophalf = @start_list[$half + 1 .. $#start_list];
2188 create_config @tophalf;
2189 read_current_config \%current_config;
2190 foreach my $config (@tophalf) {
2191 if (defined($current_config{$config})) {
2197 doprint "Failed: Can't make new config with current configs\n";
2198 foreach my $config (@start_list) {
2199 doprint " CONFIG: $config\n";
2203 $count = $#tophalf + 1;
2204 doprint "Testing $count configs\n";
2207 $ret = run_config_bisect_test $type;
2208 if ($bisect_manual) {
2209 $ret = answer_bisect;
2212 process_passed %current_config;
2216 doprint "This config had a failure.\n";
2217 doprint "Removing these configs that were not set in this config:\n";
2218 doprint "config copied to $outputdir/config_bad\n";
2219 run_command "cp -f $output_config $outputdir/config_bad";
2221 # A config exists in this group that was bad.
2222 foreach my $config (keys %config_list) {
2223 if (!defined($current_config{$config})) {
2224 doprint " removing $config\n";
2225 delete $config_list{$config};
2229 @start_list = @tophalf;
2231 if ($#start_list == 0) {
2232 process_failed $start_list[0];
2236 # remove half the configs we are looking at and see if
2238 $half = int($#start_list / 2);
2239 } while ($#start_list > 0);
2241 # we found a single config, try it again unless we are running manually
2243 if ($bisect_manual) {
2244 process_failed $start_list[0];
2248 my @tophalf = @start_list[0 .. 0];
2250 $ret = run_config_bisect_test $type;
2252 process_passed %current_config;
2256 process_failed $start_list[0];
2263 my $start_config = $opt{"CONFIG_BISECT[$i]"};
2265 my $tmpconfig = "$tmpdir/use_config";
2267 if (defined($config_bisect_good)) {
2268 process_config_ignore $config_bisect_good;
2271 # Make the file with the bad config and the min config
2272 if (defined($minconfig)) {
2273 # read the min config for things to ignore
2274 run_command "cp $minconfig $tmpconfig" or
2275 dodie "failed to copy $minconfig to $tmpconfig";
2280 if (-f $tmpconfig) {
2281 load_force_config($tmpconfig);
2282 process_config_ignore $tmpconfig;
2285 # now process the start config
2286 run_command "cp $start_config $output_config" or
2287 dodie "failed to copy $start_config to $output_config";
2289 # read directly what we want to check
2291 open (IN, $output_config)
2292 or dodie "faied to open $output_config";
2295 if (/^((CONFIG\S*)=.*)/) {
2296 $config_check{$2} = $1;
2301 # Now run oldconfig with the minconfig
2304 # check to see what we lost (or gained)
2305 open (IN, $output_config)
2306 or dodie "Failed to read $start_config";
2308 my %removed_configs;
2312 if (/^((CONFIG\S*)=.*)/) {
2313 # save off all options
2314 $config_set{$2} = $1;
2315 if (defined($config_check{$2})) {
2316 if (defined($config_ignore{$2})) {
2317 $removed_configs{$2} = $1;
2319 $config_list{$2} = $1;
2321 } elsif (!defined($config_ignore{$2})) {
2322 $added_configs{$2} = $1;
2323 $config_list{$2} = $1;
2329 my @confs = keys %removed_configs;
2331 doprint "Configs overridden by default configs and removed from check:\n";
2332 foreach my $config (@confs) {
2333 doprint " $config\n";
2336 @confs = keys %added_configs;
2338 doprint "Configs appearing in make oldconfig and added:\n";
2339 foreach my $config (@confs) {
2340 doprint " $config\n";
2347 # Sometimes kconfig does weird things. We must make sure
2348 # that the config we autocreate has everything we need
2349 # to test, otherwise we may miss testing configs, or
2350 # may not be able to create a new config.
2351 # Here we create a config with everything set.
2352 create_config (keys %config_list);
2353 read_current_config \%config_test;
2354 foreach my $config (keys %config_list) {
2355 if (!defined($config_test{$config})) {
2358 doprint "Configs not produced by kconfig (will not be checked):\n";
2360 doprint " $config\n";
2361 delete $config_list{$config};
2366 $ret = run_config_bisect;
2369 return $ret if ($ret < 0);
2374 sub patchcheck_reboot {
2375 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
2376 reboot $patchcheck_sleep_time;
2382 die "PATCHCHECK_START[$i] not defined\n"
2383 if (!defined($opt{"PATCHCHECK_START[$i]"}));
2384 die "PATCHCHECK_TYPE[$i] not defined\n"
2385 if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
2387 my $start = $opt{"PATCHCHECK_START[$i]"};
2390 if (defined($opt{"PATCHCHECK_END[$i]"})) {
2391 $end = $opt{"PATCHCHECK_END[$i]"};
2394 # Get the true sha1's since we can use things like HEAD~3
2395 $start = get_sha1($start);
2396 $end = get_sha1($end);
2398 my $type = $opt{"PATCHCHECK_TYPE[$i]"};
2400 # Can't have a test without having a test to run
2401 if ($type eq "test" && !defined($run_test)) {
2405 open (IN, "git log --pretty=oneline $end|") or
2406 dodie "could not get git list";
2412 $list[$#list+1] = $_;
2413 last if (/^$start/);
2417 if ($list[$#list] !~ /^$start/) {
2418 fail "SHA1 $start not found";
2421 # go backwards in the list
2422 @list = reverse @list;
2424 my $save_clean = $noclean;
2425 my %ignored_warnings;
2427 if (defined($ignore_warnings)) {
2428 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2429 $ignored_warnings{$sha1} = 1;
2434 foreach my $item (@list) {
2436 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2438 doprint "\nProcessing commit $item\n\n";
2440 run_command "git checkout $sha1" or
2441 die "Failed to checkout $sha1";
2443 # only clean on the first and last patch
2444 if ($item eq $list[0] ||
2445 $item eq $list[$#list]) {
2446 $noclean = $save_clean;
2451 if (defined($minconfig)) {
2452 build "useconfig:$minconfig" or return 0;
2454 # ?? no config to use?
2455 build "oldconfig" or return 0;
2459 if (!defined($ignored_warnings{$sha1})) {
2460 check_buildlog $sha1 or return 0;
2463 next if ($type eq "build");
2467 start_monitor_and_boot or $failed = 1;
2469 if (!$failed && $type ne "boot"){
2470 do_run_test or $failed = 1;
2473 return 0 if ($failed);
2493 # $config depends on $dep
2494 my ($config, $dep) = @_;
2496 if (defined($depends{$config})) {
2497 $depends{$config} .= " " . $dep;
2499 $depends{$config} = $dep;
2502 # record the number of configs depending on $dep
2503 if (defined $depcount{$dep}) {
2506 $depcount{$dep} = 1;
2510 # taken from streamline_config.pl
2522 if (! -f $kconfig) {
2523 doprint "file $kconfig does not exist, skipping\n";
2527 open(KIN, "$kconfig")
2528 or die "Can't open $kconfig";
2532 # Make sure that lines ending with \ continue
2534 $_ = $line . " " . $_;
2545 # collect any Kconfig sources
2546 if (/^source\s*"(.*)"/) {
2547 $kconfigs[$#kconfigs+1] = $1;
2551 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2555 for (my $i = 0; $i < $iflevel; $i++) {
2556 add_dep $config, $ifdeps[$i];
2559 # collect the depends for the config
2560 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2562 add_dep $config, $1;
2564 # Get the configs that select this config
2565 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2567 # selected by depends on config
2568 add_dep $1, $config;
2570 # Check for if statements
2571 } elsif (/^if\s+(.*\S)\s*$/) {
2573 # remove beginning and ending non text
2574 $deps =~ s/^[^a-zA-Z0-9_]*//;
2575 $deps =~ s/[^a-zA-Z0-9_]*$//;
2577 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2579 $ifdeps[$iflevel++] = join ':', @deps;
2581 } elsif (/^endif/) {
2583 $iflevel-- if ($iflevel);
2586 } elsif (/^\s*help\s*$/) {
2592 # read in any configs that were found.
2593 foreach $kconfig (@kconfigs) {
2594 if (!defined($read_kconfigs{$kconfig})) {
2595 $read_kconfigs{$kconfig} = 1;
2596 read_kconfig("$builddir/$kconfig");
2602 # find out which arch this is by the kconfig file
2603 open (IN, $output_config)
2604 or dodie "Failed to read $output_config";
2607 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2614 if (!defined($arch)) {
2615 doprint "Could not find arch from config file\n";
2616 doprint "no dependencies used\n";
2620 # arch is really the subarch, we need to know
2621 # what directory to look at.
2622 if ($arch eq "i386" || $arch eq "x86_64") {
2624 } elsif ($arch =~ /^tile/) {
2628 my $kconfig = "$builddir/arch/$arch/Kconfig";
2630 if (! -f $kconfig && $arch =~ /\d$/) {
2632 # some subarchs have numbers, truncate them
2634 $kconfig = "$builddir/arch/$arch/Kconfig";
2635 if (! -f $kconfig) {
2636 doprint "No idea what arch dir $orig is for\n";
2637 doprint "no dependencies used\n";
2642 read_kconfig($kconfig);
2645 sub read_config_list {
2649 or dodie "Failed to read $config";
2652 if (/^((CONFIG\S*)=.*)/) {
2653 if (!defined($config_ignore{$2})) {
2654 $config_list{$2} = $1;
2662 sub read_output_config {
2665 assign_configs \%config_ignore, $config;
2668 sub make_new_config {
2671 open (OUT, ">$output_config")
2672 or dodie "Failed to write $output_config";
2674 foreach my $config (@configs) {
2675 print OUT "$config\n";
2683 $config =~ s/CONFIG_//;
2691 my $kconfig = chomp_config $dep;
2693 $dep = $depends{"$kconfig"};
2695 # the dep string we have saves the dependencies as they
2696 # were found, including expressions like ! && ||. We
2697 # want to split this out into just an array of configs.
2699 my $valid = "A-Za-z_0-9";
2703 while ($dep =~ /[$valid]/) {
2705 if ($dep =~ /^[^$valid]*([$valid]+)/) {
2706 my $conf = "CONFIG_" . $1;
2708 $configs[$#configs + 1] = $conf;
2710 $dep =~ s/^[^$valid]*[$valid]+//;
2712 die "this should never happen";
2722 my %processed_configs;
2723 my %nochange_config;
2725 sub test_this_config {
2730 # if we already processed this config, skip it
2731 if (defined($processed_configs{$config})) {
2734 $processed_configs{$config} = 1;
2736 # if this config failed during this round, skip it
2737 if (defined($nochange_config{$config})) {
2741 my $kconfig = chomp_config $config;
2743 # Test dependencies first
2744 if (defined($depends{"$kconfig"})) {
2745 my @parents = get_depends $config;
2746 foreach my $parent (@parents) {
2747 # if the parent is in the min config, check it first
2748 next if (!defined($min_configs{$parent}));
2749 $found = test_this_config($parent);
2750 if (defined($found)) {
2756 # Remove this config from the list of configs
2757 # do a make oldnoconfig and then read the resulting
2758 # .config to make sure it is missing the config that
2760 my %configs = %min_configs;
2761 delete $configs{$config};
2762 make_new_config ((values %configs), (values %keep_configs));
2765 assign_configs \%configs, $output_config;
2767 return $config if (!defined($configs{$config}));
2769 doprint "disabling config $config did not change .config\n";
2771 $nochange_config{$config} = 1;
2776 sub make_min_config {
2779 if (!defined($output_minconfig)) {
2780 fail "OUTPUT_MIN_CONFIG not defined" and return;
2783 # If output_minconfig exists, and the start_minconfig
2784 # came from min_config, than ask if we should use
2786 if (-f $output_minconfig && !$start_minconfig_defined) {
2787 print "$output_minconfig exists\n";
2788 if (read_yn " Use it as minconfig?") {
2789 $start_minconfig = $output_minconfig;
2793 if (!defined($start_minconfig)) {
2794 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
2797 my $temp_config = "$tmpdir/temp_config";
2799 # First things first. We build an allnoconfig to find
2800 # out what the defaults are that we can't touch.
2801 # Some are selections, but we really can't handle selections.
2803 my $save_minconfig = $minconfig;
2806 run_command "$make allnoconfig" or return 0;
2810 process_config_ignore $output_config;
2812 undef %save_configs;
2815 if (defined($ignore_config)) {
2816 # make sure the file exists
2817 `touch $ignore_config`;
2818 assign_configs \%save_configs, $ignore_config;
2821 %keep_configs = %save_configs;
2823 doprint "Load initial configs from $start_minconfig\n";
2825 # Look at the current min configs, and save off all the
2826 # ones that were set via the allnoconfig
2827 assign_configs \%min_configs, $start_minconfig;
2829 my @config_keys = keys %min_configs;
2831 # All configs need a depcount
2832 foreach my $config (@config_keys) {
2833 my $kconfig = chomp_config $config;
2834 if (!defined $depcount{$kconfig}) {
2835 $depcount{$kconfig} = 0;
2839 # Remove anything that was set by the make allnoconfig
2840 # we shouldn't need them as they get set for us anyway.
2841 foreach my $config (@config_keys) {
2842 # Remove anything in the ignore_config
2843 if (defined($keep_configs{$config})) {
2844 my $file = $ignore_config;
2845 $file =~ s,.*/(.*?)$,$1,;
2846 doprint "$config set by $file ... ignored\n";
2847 delete $min_configs{$config};
2850 # But make sure the settings are the same. If a min config
2851 # sets a selection, we do not want to get rid of it if
2852 # it is not the same as what we have. Just move it into
2854 if (defined($config_ignore{$config})) {
2855 if ($config_ignore{$config} ne $min_configs{$config}) {
2856 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
2857 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
2858 $keep_configs{$config} = $min_configs{$config};
2860 doprint "$config set by allnoconfig ... ignored\n";
2862 delete $min_configs{$config};
2874 # Now disable each config one by one and do a make oldconfig
2875 # till we find a config that changes our list.
2877 my @test_configs = keys %min_configs;
2879 # Sort keys by who is most dependent on
2880 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
2883 # Put configs that did not modify the config at the end.
2885 for (my $i = 0; $i < $#test_configs; $i++) {
2886 if (!defined($nochange_config{$test_configs[0]})) {
2890 # This config didn't change the .config last time.
2891 # Place it at the end
2892 my $config = shift @test_configs;
2893 push @test_configs, $config;
2896 # if every test config has failed to modify the .config file
2897 # in the past, then reset and start over.
2899 undef %nochange_config;
2902 undef %processed_configs;
2904 foreach my $config (@test_configs) {
2906 $found = test_this_config $config;
2908 last if (defined($found));
2910 # oh well, try another config
2913 if (!defined($found)) {
2914 # we could have failed due to the nochange_config hash
2915 # reset and try again
2917 undef %nochange_config;
2921 doprint "No more configs found that we can disable\n";
2929 doprint "Test with $config disabled\n";
2931 # set in_bisect to keep build and monitor from dieing
2936 start_monitor_and_boot or $failed = 1;
2942 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
2943 # this config is needed, add it to the ignore list.
2944 $keep_configs{$config} = $min_configs{$config};
2945 $save_configs{$config} = $min_configs{$config};
2946 delete $min_configs{$config};
2948 # update new ignore configs
2949 if (defined($ignore_config)) {
2950 open (OUT, ">$temp_config")
2951 or die "Can't write to $temp_config";
2952 foreach my $config (keys %save_configs) {
2953 print OUT "$save_configs{$config}\n";
2956 run_command "mv $temp_config $ignore_config" or
2957 dodie "failed to copy update to $ignore_config";
2961 # We booted without this config, remove it from the minconfigs.
2962 doprint "$config is not needed, disabling\n";
2964 delete $min_configs{$config};
2966 # Also disable anything that is not enabled in this config
2968 assign_configs \%configs, $output_config;
2969 my @config_keys = keys %min_configs;
2970 foreach my $config (@config_keys) {
2971 if (!defined($configs{$config})) {
2972 doprint "$config is not set, disabling\n";
2973 delete $min_configs{$config};
2977 # Save off all the current mandidory configs
2978 open (OUT, ">$temp_config")
2979 or die "Can't write to $temp_config";
2980 foreach my $config (keys %keep_configs) {
2981 print OUT "$keep_configs{$config}\n";
2983 foreach my $config (keys %min_configs) {
2984 print OUT "$min_configs{$config}\n";
2988 run_command "mv $temp_config $output_minconfig" or
2989 dodie "failed to copy update to $output_minconfig";
2992 doprint "Reboot and wait $sleep_time seconds\n";
3000 $#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
3003 $ktest_config = $ARGV[0];
3004 if (! -f $ktest_config) {
3005 print "$ktest_config does not exist.\n";
3006 if (!read_yn "Create it?") {
3011 $ktest_config = "ktest.conf";
3014 if (! -f $ktest_config) {
3015 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3017 # Generated by ktest.pl
3020 # PWD is a ktest.pl variable that will result in the process working
3021 # directory that ktest.pl is executed in.
3023 # THIS_DIR is automatically assigned the PWD of the path that generated
3024 # the config file. It is best to use this variable when assigning other
3025 # directory paths within this directory. This allows you to easily
3026 # move the test cases to other locations or to other machines.
3028 THIS_DIR := $variable{"PWD"}
3030 # Define each test with TEST_START
3031 # The config options below it will override the defaults
3039 read_config $ktest_config;
3041 if (defined($opt{"LOG_FILE"})) {
3042 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3045 # Append any configs entered in manually to the config file.
3046 my @new_configs = keys %entered_configs;
3047 if ($#new_configs >= 0) {
3048 print "\nAppending entered in configs to $ktest_config\n";
3049 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3050 foreach my $config (@new_configs) {
3051 print OUT "$config = $entered_configs{$config}\n";
3052 $opt{$config} = process_variables($entered_configs{$config});
3056 if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3057 unlink $opt{"LOG_FILE"};
3060 doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3062 for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3065 doprint "DEFAULT OPTIONS:\n";
3067 doprint "\nTEST $i OPTIONS";
3068 if (defined($repeat_tests{$i})) {
3069 $repeat = $repeat_tests{$i};
3070 doprint " ITERATE $repeat";
3075 foreach my $option (sort keys %opt) {
3077 if ($option =~ /\[(\d+)\]$/) {
3083 doprint "$option = $opt{$option}\n";
3087 sub __set_test_option {
3088 my ($name, $i) = @_;
3090 my $option = "$name\[$i\]";
3092 if (defined($opt{$option})) {
3093 return $opt{$option};
3096 foreach my $test (keys %repeat_tests) {
3098 $i < $test + $repeat_tests{$test}) {
3099 $option = "$name\[$test\]";
3100 if (defined($opt{$option})) {
3101 return $opt{$option};
3106 if (defined($opt{$name})) {
3113 sub set_test_option {
3114 my ($name, $i) = @_;
3116 my $option = __set_test_option($name, $i);
3117 return $option if (!defined($option));
3119 return eval_option($option, $i);
3122 # First we need to do is the builds
3123 for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
3125 # Do not reboot on failing test options
3130 my $makecmd = set_test_option("MAKE_CMD", $i);
3132 $machine = set_test_option("MACHINE", $i);
3133 $ssh_user = set_test_option("SSH_USER", $i);
3134 $tmpdir = set_test_option("TMP_DIR", $i);
3135 $outputdir = set_test_option("OUTPUT_DIR", $i);
3136 $builddir = set_test_option("BUILD_DIR", $i);
3137 $test_type = set_test_option("TEST_TYPE", $i);
3138 $build_type = set_test_option("BUILD_TYPE", $i);
3139 $build_options = set_test_option("BUILD_OPTIONS", $i);
3140 $pre_build = set_test_option("PRE_BUILD", $i);
3141 $post_build = set_test_option("POST_BUILD", $i);
3142 $pre_build_die = set_test_option("PRE_BUILD_DIE", $i);
3143 $post_build_die = set_test_option("POST_BUILD_DIE", $i);
3144 $power_cycle = set_test_option("POWER_CYCLE", $i);
3145 $reboot = set_test_option("REBOOT", $i);
3146 $noclean = set_test_option("BUILD_NOCLEAN", $i);
3147 $minconfig = set_test_option("MIN_CONFIG", $i);
3148 $output_minconfig = set_test_option("OUTPUT_MIN_CONFIG", $i);
3149 $start_minconfig = set_test_option("START_MIN_CONFIG", $i);
3150 $ignore_config = set_test_option("IGNORE_CONFIG", $i);
3151 $run_test = set_test_option("TEST", $i);
3152 $addconfig = set_test_option("ADD_CONFIG", $i);
3153 $reboot_type = set_test_option("REBOOT_TYPE", $i);
3154 $grub_menu = set_test_option("GRUB_MENU", $i);
3155 $post_install = set_test_option("POST_INSTALL", $i);
3156 $no_install = set_test_option("NO_INSTALL", $i);
3157 $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
3158 $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
3159 $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
3160 $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
3161 $power_off = set_test_option("POWER_OFF", $i);
3162 $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
3163 $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
3164 $sleep_time = set_test_option("SLEEP_TIME", $i);
3165 $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
3166 $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
3167 $ignore_warnings = set_test_option("IGNORE_WARNINGS", $i);
3168 $bisect_manual = set_test_option("BISECT_MANUAL", $i);
3169 $bisect_skip = set_test_option("BISECT_SKIP", $i);
3170 $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
3171 $store_failures = set_test_option("STORE_FAILURES", $i);
3172 $store_successes = set_test_option("STORE_SUCCESSES", $i);
3173 $test_name = set_test_option("TEST_NAME", $i);
3174 $timeout = set_test_option("TIMEOUT", $i);
3175 $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
3176 $console = set_test_option("CONSOLE", $i);
3177 $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
3178 $success_line = set_test_option("SUCCESS_LINE", $i);
3179 $reboot_success_line = set_test_option("REBOOT_SUCCESS_LINE", $i);
3180 $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
3181 $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
3182 $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
3183 $build_target = set_test_option("BUILD_TARGET", $i);
3184 $ssh_exec = set_test_option("SSH_EXEC", $i);
3185 $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
3186 $target_image = set_test_option("TARGET_IMAGE", $i);
3187 $localversion = set_test_option("LOCALVERSION", $i);
3189 $start_minconfig_defined = 1;
3191 if (!defined($start_minconfig)) {
3192 $start_minconfig_defined = 0;
3193 $start_minconfig = $minconfig;
3196 chdir $builddir || die "can't change directory to $builddir";
3198 foreach my $dir ($tmpdir, $outputdir) {
3201 die "can't create $dir";
3205 $ENV{"SSH_USER"} = $ssh_user;
3206 $ENV{"MACHINE"} = $machine;
3208 $target = "$ssh_user\@$machine";
3210 $buildlog = "$tmpdir/buildlog-$machine";
3211 $testlog = "$tmpdir/testlog-$machine";
3212 $dmesg = "$tmpdir/dmesg-$machine";
3213 $make = "$makecmd O=$outputdir";
3214 $output_config = "$outputdir/.config";
3216 if ($reboot_type eq "grub") {
3217 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3218 } elsif (!defined($reboot_script)) {
3219 dodie "REBOOT_SCRIPT not defined"
3222 my $run_type = $build_type;
3223 if ($test_type eq "patchcheck") {
3224 $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
3225 } elsif ($test_type eq "bisect") {
3226 $run_type = $opt{"BISECT_TYPE[$i]"};
3227 } elsif ($test_type eq "config_bisect") {
3228 $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
3231 if ($test_type eq "make_min_config") {
3235 # mistake in config file?
3236 if (!defined($run_type)) {
3237 $run_type = "ERROR";
3241 $installme = " no_install" if ($no_install);
3244 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
3250 if (defined($addconfig)) {
3251 my $min = $minconfig;
3252 if (!defined($minconfig)) {
3255 run_command "cat $addconfig $min > $tmpdir/add_config" or
3256 dodie "Failed to create temp config";
3257 $minconfig = "$tmpdir/add_config";
3260 my $checkout = $opt{"CHECKOUT[$i]"};
3261 if (defined($checkout)) {
3262 run_command "git checkout $checkout" or
3263 die "failed to checkout $checkout";
3269 if ($test_type eq "bisect") {
3272 } elsif ($test_type eq "config_bisect") {
3275 } elsif ($test_type eq "patchcheck") {
3278 } elsif ($test_type eq "make_min_config") {
3283 if ($build_type ne "nobuild") {
3284 build $build_type or next;
3287 if ($test_type eq "install") {
3294 if ($test_type ne "build") {
3296 start_monitor_and_boot or $failed = 1;
3298 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3299 do_run_test or $failed = 1;
3308 if ($opt{"POWEROFF_ON_SUCCESS"}) {
3310 } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
3314 doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";