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);
26 "MAILER" => "sendmail", # default mailer
27 "EMAIL_ON_ERROR" => 1,
28 "EMAIL_WHEN_FINISHED" => 1,
29 "EMAIL_WHEN_CANCELED" => 0,
30 "EMAIL_WHEN_STARTED" => 0,
32 "TEST_TYPE" => "build",
33 "BUILD_TYPE" => "randconfig",
35 "CLOSE_CONSOLE_SIGNAL" => "INT",
37 "TMP_DIR" => "/tmp/ktest/\${MACHINE}",
38 "SLEEP_TIME" => 60, # sleep time between tests
40 "REBOOT_ON_ERROR" => 0,
41 "POWEROFF_ON_ERROR" => 0,
42 "REBOOT_ON_SUCCESS" => 1,
43 "POWEROFF_ON_SUCCESS" => 0,
44 "BUILD_OPTIONS" => "",
45 "BISECT_SLEEP_TIME" => 60, # sleep time between bisects
46 "PATCHCHECK_SLEEP_TIME" => 60, # sleep time between patch checks
51 "MIN_CONFIG_TYPE" => "boot",
52 "SUCCESS_LINE" => "login:",
53 "DETECT_TRIPLE_FAULT" => 1,
55 "BOOTED_TIMEOUT" => 1,
56 "DIE_ON_FAILURE" => 1,
57 "SSH_EXEC" => "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND",
58 "SCP_TO_TARGET" => "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE",
59 "SCP_TO_TARGET_INSTALL" => "\${SCP_TO_TARGET}",
60 "REBOOT" => "ssh \$SSH_USER\@\$MACHINE reboot",
61 "STOP_AFTER_SUCCESS" => 10,
62 "STOP_AFTER_FAILURE" => 60,
63 "STOP_TEST_AFTER" => 600,
64 "MAX_MONITOR_WAIT" => 1800,
65 "GRUB_REBOOT" => "grub2-reboot",
66 "SYSLINUX" => "extlinux",
67 "SYSLINUX_PATH" => "/boot/extlinux",
68 "CONNECT_TIMEOUT" => 25,
70 # required, and we will ask users if they don't have them but we keep the default
71 # value something that is common.
72 "REBOOT_TYPE" => "grub",
73 "LOCALVERSION" => "-test",
75 "BUILD_TARGET" => "arch/x86/boot/bzImage",
76 "TARGET_IMAGE" => "/boot/vmlinuz-test",
82 my $ktest_config = "ktest.conf";
111 my $poweroff_on_error;
112 my $reboot_on_success;
114 my $powercycle_after_reboot;
115 my $poweroff_after_halt;
116 my $max_monitor_wait;
119 my $scp_to_target_install;
137 my $start_minconfig_defined;
138 my $output_minconfig;
140 my $use_output_minconfig;
146 my $bisect_bad_commit = "";
151 my $config_bisect_good;
155 my $bisect_ret_abort;
156 my $bisect_ret_default;
157 my $in_patchcheck = 0;
166 my $bisect_sleep_time;
167 my $patchcheck_sleep_time;
174 my $config_bisect_exec;
176 my $detect_triplefault;
178 my $close_console_signal;
179 my $reboot_success_line;
181 my $stop_after_success;
182 my $stop_after_failure;
191 my $run_command_status = 0;
203 my $config_bisect_type;
204 my $config_bisect_check;
207 my $patchcheck_start;
208 my $patchcheck_cherry;
217 my $dirname = $FindBin::Bin;
222 my $email_when_finished;
223 my $email_when_started;
224 my $email_when_canceled;
226 my $script_start_time = localtime();
228 # set when a test is something other that just building or install
229 # which would require more options.
232 # tell build not to worry about warnings, even when WARNINGS_FILE is set
235 # set when creating a new config
242 # force_config is the list of configs that we force enabled (or disabled)
243 # in a .config file. The MIN_CONFIG and ADD_CONFIG configs.
246 # do not force reboots on config problems
250 my $reboot_success = 0;
253 "MAILTO" => \$mailto,
254 "MAILER" => \$mailer,
255 "EMAIL_ON_ERROR" => \$email_on_error,
256 "EMAIL_WHEN_FINISHED" => \$email_when_finished,
257 "EMAIL_WHEN_STARTED" => \$email_when_started,
258 "EMAIL_WHEN_CANCELED" => \$email_when_canceled,
259 "MACHINE" => \$machine,
260 "SSH_USER" => \$ssh_user,
261 "TMP_DIR" => \$tmpdir,
262 "OUTPUT_DIR" => \$outputdir,
263 "BUILD_DIR" => \$builddir,
264 "TEST_TYPE" => \$test_type,
265 "PRE_KTEST" => \$pre_ktest,
266 "POST_KTEST" => \$post_ktest,
267 "PRE_TEST" => \$pre_test,
268 "POST_TEST" => \$post_test,
269 "BUILD_TYPE" => \$build_type,
270 "BUILD_OPTIONS" => \$build_options,
271 "PRE_BUILD" => \$pre_build,
272 "POST_BUILD" => \$post_build,
273 "PRE_BUILD_DIE" => \$pre_build_die,
274 "POST_BUILD_DIE" => \$post_build_die,
275 "POWER_CYCLE" => \$power_cycle,
276 "REBOOT" => \$reboot,
277 "BUILD_NOCLEAN" => \$noclean,
278 "MIN_CONFIG" => \$minconfig,
279 "OUTPUT_MIN_CONFIG" => \$output_minconfig,
280 "START_MIN_CONFIG" => \$start_minconfig,
281 "MIN_CONFIG_TYPE" => \$minconfig_type,
282 "USE_OUTPUT_MIN_CONFIG" => \$use_output_minconfig,
283 "WARNINGS_FILE" => \$warnings_file,
284 "IGNORE_CONFIG" => \$ignore_config,
285 "TEST" => \$run_test,
286 "ADD_CONFIG" => \$addconfig,
287 "REBOOT_TYPE" => \$reboot_type,
288 "GRUB_MENU" => \$grub_menu,
289 "GRUB_FILE" => \$grub_file,
290 "GRUB_REBOOT" => \$grub_reboot,
291 "SYSLINUX" => \$syslinux,
292 "SYSLINUX_PATH" => \$syslinux_path,
293 "SYSLINUX_LABEL" => \$syslinux_label,
294 "PRE_INSTALL" => \$pre_install,
295 "POST_INSTALL" => \$post_install,
296 "NO_INSTALL" => \$no_install,
297 "REBOOT_SCRIPT" => \$reboot_script,
298 "REBOOT_ON_ERROR" => \$reboot_on_error,
299 "SWITCH_TO_GOOD" => \$switch_to_good,
300 "SWITCH_TO_TEST" => \$switch_to_test,
301 "POWEROFF_ON_ERROR" => \$poweroff_on_error,
302 "REBOOT_ON_SUCCESS" => \$reboot_on_success,
303 "DIE_ON_FAILURE" => \$die_on_failure,
304 "POWER_OFF" => \$power_off,
305 "POWERCYCLE_AFTER_REBOOT" => \$powercycle_after_reboot,
306 "POWEROFF_AFTER_HALT" => \$poweroff_after_halt,
307 "MAX_MONITOR_WAIT" => \$max_monitor_wait,
308 "SLEEP_TIME" => \$sleep_time,
309 "BISECT_SLEEP_TIME" => \$bisect_sleep_time,
310 "PATCHCHECK_SLEEP_TIME" => \$patchcheck_sleep_time,
311 "IGNORE_WARNINGS" => \$ignore_warnings,
312 "IGNORE_ERRORS" => \$ignore_errors,
313 "BISECT_MANUAL" => \$bisect_manual,
314 "BISECT_SKIP" => \$bisect_skip,
315 "BISECT_TRIES" => \$bisect_tries,
316 "CONFIG_BISECT_GOOD" => \$config_bisect_good,
317 "BISECT_RET_GOOD" => \$bisect_ret_good,
318 "BISECT_RET_BAD" => \$bisect_ret_bad,
319 "BISECT_RET_SKIP" => \$bisect_ret_skip,
320 "BISECT_RET_ABORT" => \$bisect_ret_abort,
321 "BISECT_RET_DEFAULT" => \$bisect_ret_default,
322 "STORE_FAILURES" => \$store_failures,
323 "STORE_SUCCESSES" => \$store_successes,
324 "TEST_NAME" => \$test_name,
325 "TIMEOUT" => \$timeout,
326 "CONNECT_TIMEOUT" => \$connect_timeout,
327 "CONFIG_BISECT_EXEC" => \$config_bisect_exec,
328 "BOOTED_TIMEOUT" => \$booted_timeout,
329 "CONSOLE" => \$console,
330 "CLOSE_CONSOLE_SIGNAL" => \$close_console_signal,
331 "DETECT_TRIPLE_FAULT" => \$detect_triplefault,
332 "SUCCESS_LINE" => \$success_line,
333 "REBOOT_SUCCESS_LINE" => \$reboot_success_line,
334 "STOP_AFTER_SUCCESS" => \$stop_after_success,
335 "STOP_AFTER_FAILURE" => \$stop_after_failure,
336 "STOP_TEST_AFTER" => \$stop_test_after,
337 "BUILD_TARGET" => \$build_target,
338 "SSH_EXEC" => \$ssh_exec,
339 "SCP_TO_TARGET" => \$scp_to_target,
340 "SCP_TO_TARGET_INSTALL" => \$scp_to_target_install,
341 "CHECKOUT" => \$checkout,
342 "TARGET_IMAGE" => \$target_image,
343 "LOCALVERSION" => \$localversion,
345 "BISECT_GOOD" => \$bisect_good,
346 "BISECT_BAD" => \$bisect_bad,
347 "BISECT_TYPE" => \$bisect_type,
348 "BISECT_START" => \$bisect_start,
349 "BISECT_REPLAY" => \$bisect_replay,
350 "BISECT_FILES" => \$bisect_files,
351 "BISECT_REVERSE" => \$bisect_reverse,
352 "BISECT_CHECK" => \$bisect_check,
354 "CONFIG_BISECT" => \$config_bisect,
355 "CONFIG_BISECT_TYPE" => \$config_bisect_type,
356 "CONFIG_BISECT_CHECK" => \$config_bisect_check,
358 "PATCHCHECK_TYPE" => \$patchcheck_type,
359 "PATCHCHECK_START" => \$patchcheck_start,
360 "PATCHCHECK_CHERRY" => \$patchcheck_cherry,
361 "PATCHCHECK_END" => \$patchcheck_end,
364 # Options may be used by other options, record them.
367 # default variables that can be used
368 chomp ($variable{"PWD"} = `pwd`);
369 $pwd = $variable{"PWD"};
371 $config_help{"MACHINE"} = << "EOF"
372 The machine hostname that you will test.
373 For build only tests, it is still needed to differentiate log files.
376 $config_help{"SSH_USER"} = << "EOF"
377 The box is expected to have ssh on normal bootup, provide the user
378 (most likely root, since you need privileged operations)
381 $config_help{"BUILD_DIR"} = << "EOF"
382 The directory that contains the Linux source code (full path).
383 You can use \${PWD} that will be the path where ktest.pl is run, or use
384 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
387 $config_help{"OUTPUT_DIR"} = << "EOF"
388 The directory that the objects will be built (full path).
389 (can not be same as BUILD_DIR)
390 You can use \${PWD} that will be the path where ktest.pl is run, or use
391 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
394 $config_help{"BUILD_TARGET"} = << "EOF"
395 The location of the compiled file to copy to the target.
396 (relative to OUTPUT_DIR)
399 $config_help{"BUILD_OPTIONS"} = << "EOF"
400 Options to add to \"make\" when building.
404 $config_help{"TARGET_IMAGE"} = << "EOF"
405 The place to put your image on the test machine.
408 $config_help{"POWER_CYCLE"} = << "EOF"
409 A script or command to reboot the box.
411 Here is a digital loggers power switch example
412 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
414 Here is an example to reboot a virtual box on the current host
415 with the name "Guest".
416 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
419 $config_help{"CONSOLE"} = << "EOF"
420 The script or command that reads the console
422 If you use ttywatch server, something like the following would work.
423 CONSOLE = nc -d localhost 3001
425 For a virtual machine with guest name "Guest".
426 CONSOLE = virsh console Guest
429 $config_help{"LOCALVERSION"} = << "EOF"
430 Required version ending to differentiate the test
431 from other linux builds on the system.
434 $config_help{"REBOOT_TYPE"} = << "EOF"
435 Way to reboot the box to the test kernel.
436 Only valid options so far are "grub", "grub2", "syslinux", and "script".
438 If you specify grub, it will assume grub version 1
439 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
440 and select that target to reboot to the kernel. If this is not
441 your setup, then specify "script" and have a command or script
442 specified in REBOOT_SCRIPT to boot to the target.
444 The entry in /boot/grub/menu.lst must be entered in manually.
445 The test will not modify that file.
447 If you specify grub2, then you also need to specify both \$GRUB_MENU
450 If you specify syslinux, then you may use SYSLINUX to define the syslinux
451 command (defaults to extlinux), and SYSLINUX_PATH to specify the path to
452 the syslinux install (defaults to /boot/extlinux). But you have to specify
453 SYSLINUX_LABEL to define the label to boot to for the test kernel.
456 $config_help{"GRUB_MENU"} = << "EOF"
457 The grub title name for the test kernel to boot
458 (Only mandatory if REBOOT_TYPE = grub or grub2)
460 Note, ktest.pl will not update the grub menu.lst, you need to
461 manually add an option for the test. ktest.pl will search
462 the grub menu.lst for this option to find what kernel to
465 For example, if in the /boot/grub/menu.lst the test kernel title has:
468 GRUB_MENU = Test Kernel
470 For grub2, a search of \$GRUB_FILE is performed for the lines
471 that begin with "menuentry". It will not detect submenus. The
472 menu must be a non-nested menu. Add the quotes used in the menu
473 to guarantee your selection, as the first menuentry with the content
474 of \$GRUB_MENU that is found will be used.
477 $config_help{"GRUB_FILE"} = << "EOF"
478 If grub2 is used, the full path for the grub.cfg file is placed
479 here. Use something like /boot/grub2/grub.cfg to search.
482 $config_help{"SYSLINUX_LABEL"} = << "EOF"
483 If syslinux is used, the label that boots the target kernel must
484 be specified with SYSLINUX_LABEL.
487 $config_help{"REBOOT_SCRIPT"} = << "EOF"
488 A script to reboot the target into the test kernel
489 (Only mandatory if REBOOT_TYPE = script)
494 if (defined($opt{"LOG_FILE"})) {
495 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
502 if (defined($opt{"LOG_FILE"})) {
515 my ($cancel, $prompt) = @_;
521 print "$prompt [y/n/C] ";
523 print "$prompt [Y/n] ";
527 if ($ans =~ /^\s*$/) {
534 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
536 last if ($ans =~ /^c$/i);
537 print "Please answer either 'y', 'n' or 'c'.\n";
539 print "Please answer either 'y' or 'n'.\n";
545 if ($ans !~ /^y$/i) {
554 return read_prompt 0, $prompt;
560 return read_prompt 1, $prompt;
563 sub get_mandatory_config {
567 return if (defined($opt{$config}));
569 if (defined($config_help{$config})) {
571 print $config_help{$config};
576 if (defined($default{$config}) && length($default{$config})) {
577 print "\[$default{$config}\] ";
580 $ans =~ s/^\s*(.*\S)\s*$/$1/;
581 if ($ans =~ /^\s*$/) {
582 if ($default{$config}) {
583 $ans = $default{$config};
585 print "Your answer can not be blank\n";
589 $entered_configs{$config} = ${ans};
601 $hours = int($time / 3600);
602 $time -= $hours * 3600;
605 $minutes = int($time / 60);
606 $time -= $minutes * 60;
610 doprint "$hours hour";
611 doprint "s" if ($hours > 1);
616 doprint "$minutes minute";
617 doprint "s" if ($minutes > 1);
621 doprint "$time second";
622 doprint "s" if ($time != 1);
628 doprint "Build time: ";
629 show_time($build_time);
633 doprint "Install time: ";
634 show_time($install_time);
638 doprint "Reboot time: ";
639 show_time($reboot_time);
643 doprint "Test time: ";
644 show_time($test_time);
647 # reset for iterations like bisect
654 sub get_mandatory_configs {
655 get_mandatory_config("MACHINE");
656 get_mandatory_config("BUILD_DIR");
657 get_mandatory_config("OUTPUT_DIR");
660 get_mandatory_config("BUILD_OPTIONS");
663 # options required for other than just building a kernel
665 get_mandatory_config("POWER_CYCLE");
666 get_mandatory_config("CONSOLE");
669 # options required for install and more
670 if ($buildonly != 1) {
671 get_mandatory_config("SSH_USER");
672 get_mandatory_config("BUILD_TARGET");
673 get_mandatory_config("TARGET_IMAGE");
676 get_mandatory_config("LOCALVERSION");
678 return if ($buildonly);
680 my $rtype = $opt{"REBOOT_TYPE"};
682 if (!defined($rtype)) {
683 if (!defined($opt{"GRUB_MENU"})) {
684 get_mandatory_config("REBOOT_TYPE");
685 $rtype = $entered_configs{"REBOOT_TYPE"};
691 if ($rtype eq "grub") {
692 get_mandatory_config("GRUB_MENU");
695 if ($rtype eq "grub2") {
696 get_mandatory_config("GRUB_MENU");
697 get_mandatory_config("GRUB_FILE");
700 if ($rtype eq "syslinux") {
701 get_mandatory_config("SYSLINUX_LABEL");
705 sub process_variables {
706 my ($value, $remove_undef) = @_;
709 # We want to check for '\', and it is just easier
710 # to check the previous characet of '$' and not need
711 # to worry if '$' is the first character. By adding
712 # a space to $value, we can just check [^\\]\$ and
713 # it will still work.
716 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
720 # append beginning of value to retval
721 $retval = "$retval$begin";
722 if (defined($variable{$var})) {
723 $retval = "$retval$variable{$var}";
724 } elsif (defined($remove_undef) && $remove_undef) {
725 # for if statements, any variable that is not defined,
726 # we simple convert to 0
727 $retval = "${retval}0";
729 # put back the origin piece.
730 $retval = "$retval\$\{$var\}";
731 # This could be an option that is used later, save
732 # it so we don't warn if this option is not one of
734 $used_options{$var} = 1;
738 $retval = "$retval$value";
740 # remove the space added in the beginning
747 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
749 my $prvalue = process_variables($rvalue);
751 if ($lvalue =~ /^(TEST|BISECT|CONFIG_BISECT)_TYPE(\[.*\])?$/ &&
752 $prvalue !~ /^(config_|)bisect$/ &&
753 $prvalue !~ /^build$/ &&
756 # Note if a test is something other than build, then we
757 # will need other mandatory options.
758 if ($prvalue ne "install") {
761 # install still limits some mandatory options.
766 if (defined($opt{$lvalue})) {
767 if (!$override || defined(${$overrides}{$lvalue})) {
770 $extra = "In the same override section!\n";
772 die "$name: $.: Option $lvalue defined more than once!\n$extra";
774 ${$overrides}{$lvalue} = $prvalue;
777 $opt{$lvalue} = $prvalue;
781 my ($lvalue, $rvalue, $name) = @_;
783 my $prvalue = process_variables($rvalue);
786 if (defined($evals{$lvalue})) {
787 $arr = $evals{$lvalue};
790 $evals{$lvalue} = $arr;
793 push @{$arr}, $rvalue;
797 my ($lvalue, $rvalue) = @_;
799 if ($rvalue =~ /^\s*$/) {
800 delete $variable{$lvalue};
802 $rvalue = process_variables($rvalue);
803 $variable{$lvalue} = $rvalue;
807 sub process_compare {
808 my ($lval, $cmp, $rval) = @_;
819 return $lval eq $rval;
820 } elsif ($cmp eq "!=") {
821 return $lval ne $rval;
822 } elsif ($cmp eq "=~") {
823 return $lval =~ m/$rval/;
824 } elsif ($cmp eq "!~") {
825 return $lval !~ m/$rval/;
828 my $statement = "$lval $cmp $rval";
829 my $ret = eval $statement;
831 # $@ stores error of eval
842 return defined($variable{$2}) ||
847 sub process_expression {
848 my ($name, $val) = @_;
852 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
855 if (process_expression($name, $express)) {
856 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
858 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
866 while ($val =~ s/^(.*?)($OR|$AND)//) {
870 if (process_expression($name, $express)) {
881 if ($val =~ /(.*)(==|\!=|>=|<=|>|<|=~|\!~)(.*)/) {
882 my $ret = process_compare($1, $2, $3);
884 die "$name: $.: Unable to process comparison\n";
889 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
891 return !value_defined($2);
893 return value_defined($2);
897 if ($val =~ /^\s*0\s*$/) {
899 } elsif ($val =~ /^\s*\d+\s*$/) {
903 die ("$name: $.: Undefined content $val in if statement\n");
907 my ($name, $value) = @_;
909 # Convert variables and replace undefined ones with 0
910 my $val = process_variables($value, 1);
911 my $ret = process_expression $name, $val;
917 my ($config, $current_test_num) = @_;
920 open($in, $config) || die "can't read file $config";
923 $name =~ s,.*/(.*),$1,;
925 my $test_num = $$current_test_num;
928 my $num_tests_set = 0;
941 # ignore blank lines and comments
942 next if (/^\s*$/ || /\s*\#/);
944 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
954 if ($type eq "TEST_START") {
956 if ($num_tests_set) {
957 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
960 $old_test_num = $test_num;
961 $old_repeat = $repeat;
963 $test_num += $repeat;
970 # If SKIP is anywhere in the line, the command will be skipped
971 if ($rest =~ s/\s+SKIP\b//) {
978 if ($rest =~ s/\sELSE\b//) {
980 die "$name: $.: ELSE found with out matching IF section\n$_";
991 if ($rest =~ s/\sIF\s+(.*)//) {
992 if (process_if($name, $1)) {
1004 if ($type eq "TEST_START") {
1005 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
1007 $repeat_tests{"$test_num"} = $repeat;
1009 } elsif ($rest =~ s/\sOVERRIDE\b//) {
1012 # Clear previous overrides
1017 if (!$skip && $rest !~ /^\s*$/) {
1018 die "$name: $.: Gargbage found after $type\n$_";
1021 if ($skip && $type eq "TEST_START") {
1022 $test_num = $old_test_num;
1023 $repeat = $old_repeat;
1026 } elsif (/^\s*ELSE\b(.*)$/) {
1028 die "$name: $.: ELSE found with out matching IF section\n$_";
1037 if ($rest =~ /\sIF\s+(.*)/) {
1038 # May be a ELSE IF section.
1039 if (process_if($name, $1)) {
1050 if ($rest !~ /^\s*$/) {
1051 die "$name: $.: Gargbage found after DEFAULTS\n$_";
1054 } elsif (/^\s*INCLUDE\s+(\S+)/) {
1059 die "$name: $.: INCLUDE can only be done in default sections\n$_";
1062 my $file = process_variables($1);
1064 if ($file !~ m,^/,) {
1065 # check the path of the config file first
1066 if ($config =~ m,(.*)/,) {
1067 if (-f "$1/$file") {
1074 die "$name: $.: Can't read file $file\n$_";
1077 if (__read_config($file, \$test_num)) {
1081 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=~\s*(.*?)\s*$/) {
1088 if ($default || $lvalue =~ /\[\d+\]$/) {
1089 set_eval($lvalue, $rvalue, $name);
1091 my $val = "$lvalue\[$test_num\]";
1092 set_eval($val, $rvalue, $name);
1095 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
1103 ($lvalue eq "NUM_TESTS" ||
1104 $lvalue eq "LOG_FILE" ||
1105 $lvalue eq "CLEAR_LOG")) {
1106 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
1109 if ($lvalue eq "NUM_TESTS") {
1111 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
1114 die "$name: $.: NUM_TESTS must be set in default section\n";
1119 if ($default || $lvalue =~ /\[\d+\]$/) {
1120 set_value($lvalue, $rvalue, $override, \%overrides, $name);
1122 my $val = "$lvalue\[$test_num\]";
1123 set_value($val, $rvalue, $override, \%overrides, $name);
1126 $repeats{$val} = $repeat;
1129 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
1135 # process config variables.
1136 # Config variables are only active while reading the
1137 # config and can be defined anywhere. They also ignore
1138 # TEST_START and DEFAULTS, but are skipped if they are in
1139 # on of these sections that have SKIP defined.
1140 # The save variable can be
1141 # defined multiple times and the new one simply overrides
1143 set_variable($lvalue, $rvalue);
1146 die "$name: $.: Garbage found in config\n$_";
1151 $test_num += $repeat - 1;
1152 $opt{"NUM_TESTS"} = $test_num;
1157 $$current_test_num = $test_num;
1163 print "What test case would you like to run?\n";
1164 print " (build, install or boot)\n";
1165 print " Other tests are available but require editing ktest.conf\n";
1166 print " (see tools/testing/ktest/sample.conf)\n";
1169 $default{"TEST_TYPE"} = $ans;
1178 $test_case = __read_config $config, \$test_num;
1180 # make sure we have all mandatory configs
1181 get_mandatory_configs;
1183 # was a test specified?
1185 print "No test case specified.\n";
1191 foreach my $default (keys %default) {
1192 if (!defined($opt{$default})) {
1193 $opt{$default} = $default{$default};
1197 if ($opt{"IGNORE_UNUSED"} == 1) {
1203 # check if there are any stragglers (typos?)
1204 foreach my $option (keys %opt) {
1206 # remove per test labels.
1208 if (!exists($option_map{$op}) &&
1209 !exists($default{$op}) &&
1210 !exists($used_options{$op})) {
1217 $s = " is" if (keys %not_used == 1);
1218 print "The following option$s not used; could be a typo:\n";
1219 foreach my $option (keys %not_used) {
1222 print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
1223 if (!read_yn "Do you want to continue?") {
1230 my ($name, $option, $i) = @_;
1232 # Add space to evaluate the character before $
1233 $option = " $option";
1238 foreach my $test (keys %repeat_tests) {
1240 $i < $test + $repeat_tests{$test}) {
1248 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
1253 # Append beginning of line
1254 $retval = "$retval$start";
1256 # If the iteration option OPT[$i] exists, then use that.
1257 # otherwise see if the default OPT (without [$i]) exists.
1259 my $o = "$var\[$i\]";
1260 my $parento = "$var\[$parent\]";
1262 # If a variable contains itself, use the default var
1263 if (($var eq $name) && defined($opt{$var})) {
1265 $retval = "$retval$o";
1266 } elsif (defined($opt{$o})) {
1268 $retval = "$retval$o";
1269 } elsif ($repeated && defined($opt{$parento})) {
1270 $o = $opt{$parento};
1271 $retval = "$retval$o";
1272 } elsif (defined($opt{$var})) {
1274 $retval = "$retval$o";
1275 } elsif ($var eq "KERNEL_VERSION" && defined($make)) {
1276 # special option KERNEL_VERSION uses kernel version
1278 $retval = "$retval$version";
1280 $retval = "$retval\$\{$var\}";
1286 $retval = "$retval$option";
1294 my ($name, $option, $i) = @_;
1296 my $option_name = "$name\[$i\]";
1299 my $old_option = $option;
1301 if (defined($evals{$option_name})) {
1302 $ev = $evals{$option_name};
1303 } elsif (defined($evals{$name})) {
1304 $ev = $evals{$name};
1309 for my $e (@{$ev}) {
1310 eval "\$option =~ $e";
1313 if ($option ne $old_option) {
1314 doprint("$name changed from '$old_option' to '$option'\n");
1321 my ($name, $option, $i) = @_;
1325 # Since an option can evaluate to another option,
1326 # keep iterating until we do not evaluate any more
1329 while ($prev ne $option) {
1330 # Check for recursive evaluations.
1331 # 100 deep should be more than enough.
1333 die "Over 100 evaluations accurred with $option\n" .
1334 "Check for recursive variables\n";
1337 $option = __eval_option($name, $option, $i);
1340 $option = process_evals($name, $option, $i);
1348 sub wait_for_monitor;
1354 # test if the machine can be connected to within a few seconds
1355 my $stat = run_ssh("echo check machine status", $connect_timeout);
1357 doprint("power cycle\n");
1362 run_command "$power_cycle";
1365 # flush out current monitor
1366 # May contain the reboot success line
1370 # Make sure everything has been written to disk
1373 if (defined($time)) {
1375 # flush out current monitor
1376 # May contain the reboot success line
1380 # try to reboot normally
1381 if (run_command $reboot) {
1382 if (defined($powercycle_after_reboot)) {
1383 sleep $powercycle_after_reboot;
1384 run_command "$power_cycle";
1387 # nope? power cycle it.
1388 run_command "$power_cycle";
1392 if (defined($time)) {
1394 # We only want to get to the new kernel, don't fail
1395 # if we stumble over a call trace.
1396 my $save_ignore_errors = $ignore_errors;
1399 # Look for the good kernel to boot
1400 if (wait_for_monitor($time, "Linux version")) {
1402 doprint "Reboot did not finish. Forcing power cycle\n";
1403 run_command "$power_cycle";
1406 $ignore_errors = $save_ignore_errors;
1408 # Still need to wait for the reboot to finish
1409 wait_for_monitor($time, $reboot_success_line);
1415 sub reboot_to_good {
1418 if (defined($switch_to_good)) {
1419 run_command $switch_to_good;
1428 return $test_type eq "build" || $no_reboot ||
1429 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1430 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build") ||
1431 ($test_type eq "config_bisect" && $opt{"CONFIG_BISECT_TYPE[$i]"} eq "build");
1435 doprint "CRITICAL FAILURE... ", @_, "\n";
1439 if ($reboot_on_error && !do_not_reboot) {
1441 doprint "REBOOTING\n";
1444 } elsif ($poweroff_on_error && defined($power_off)) {
1445 doprint "POWERING OFF\n";
1449 if (defined($opt{"LOG_FILE"})) {
1450 print " See $opt{LOG_FILE} for more info.\n";
1453 if ($email_on_error) {
1454 send_email("KTEST: critical failure for your [$test_type] test",
1455 "Your test started at $script_start_time has failed with:\n@_\n");
1459 # restore terminal settings
1460 system("stty $stty_orig");
1463 if (defined($post_test)) {
1464 run_command $post_test;
1471 my ($ptm, $pts) = @_;
1473 my $TIOCSPTLCK = 0x40045431;
1474 my $TIOCGPTN = 0x80045430;
1476 sysopen($ptm, "/dev/ptmx", O_RDWR | O_NONBLOCK) or
1477 dodie "Cant open /dev/ptmx";
1480 $tmp = pack("i", 0);
1481 ioctl($ptm, $TIOCSPTLCK, $tmp) or
1482 dodie "ioctl TIOCSPTLCK for /dev/ptmx failed";
1485 ioctl($ptm, $TIOCGPTN, $tmp) or
1486 dodie "ioctl TIOCGPTN for /dev/ptmx failed";
1487 $tmp = unpack("i", $tmp);
1489 sysopen($pts, "/dev/pts/$tmp", O_RDWR | O_NONBLOCK) or
1490 dodie "Can't open /dev/pts/$tmp";
1494 my ($ptm, $pts) = @_;
1502 open(\*STDIN, '<&', $pts);
1503 open(\*STDOUT, '>&', $pts);
1504 open(\*STDERR, '>&', $pts);
1509 die "Can't open console $console";
1517 # save terminal settings
1518 $stty_orig = `stty -g`;
1520 # place terminal in cbreak mode so that stdin can be read one character at
1521 # a time without having to wait for a newline
1522 system("stty -icanon -echo -icrnl");
1524 create_pty($ptm, $pts);
1530 exec_console($ptm, $pts)
1538 open(PTSFD, "Stop perl from warning about single use of PTSFD");
1542 my ($fp, $pid) = @_;
1544 doprint "kill child process $pid\n";
1545 kill $close_console_signal, $pid;
1547 doprint "wait for child process $pid to exit\n";
1553 # restore terminal settings
1554 system("stty $stty_orig");
1558 if ($monitor_cnt++) {
1561 $monitor_fp = \*MONFD;
1562 $monitor_pid = open_console $monitor_fp;
1566 open(MONFD, "Stop perl from warning about single use of MONFD");
1570 return if (!defined $console);
1571 if (--$monitor_cnt) {
1574 close_console($monitor_fp, $monitor_pid);
1577 sub wait_for_monitor {
1578 my ($time, $stop) = @_;
1582 my $start_time = time;
1583 my $skip_call_trace = 0;
1585 my $bug_ignored = 0;
1588 doprint "** Wait for monitor to settle down **\n";
1590 # read the monitor and wait for the system to calm down
1592 $line = wait_for_input($monitor_fp, $time);
1593 last if (!defined($line));
1595 $full_line .= $line;
1597 if (defined($stop) && $full_line =~ /$stop/) {
1598 doprint "wait for monitor detected $stop\n";
1602 if ($full_line =~ /\[ backtrace testing \]/) {
1603 $skip_call_trace = 1;
1606 if ($full_line =~ /call trace:/i) {
1607 if (!$bug && !$skip_call_trace) {
1608 if ($ignore_errors) {
1616 if ($full_line =~ /\[ end of backtrace testing \]/) {
1617 $skip_call_trace = 0;
1620 if ($full_line =~ /Kernel panic -/) {
1624 if ($line =~ /\n/) {
1628 if ($now - $start_time >= $max_monitor_wait) {
1629 doprint "Exiting monitor flush due to hitting MAX_MONITOR_WAIT\n";
1633 print "** Monitor flushed **\n";
1635 # if stop is defined but wasn't hit, return error
1636 # used by reboot (which wants to see a reboot)
1637 if (defined($stop) && !$booted) {
1644 my ($result, $basedir) = @_;
1646 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1647 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1649 my $type = $build_type;
1650 if ($type =~ /useconfig/) {
1651 $type = "useconfig";
1654 my $dir = "$machine-$test_type-$type-$result-$date";
1656 $dir = "$basedir/$dir";
1660 die "can't create $dir";
1664 "config" => $output_config,
1665 "buildlog" => $buildlog,
1667 "testlog" => $testlog,
1670 while (my ($name, $source) = each(%files)) {
1672 cp "$source", "$dir/$name" or
1673 die "failed to copy $source";
1677 doprint "*** Saved info to $dir ***\n";
1682 if ($die_on_failure) {
1690 # no need to reboot for just building.
1691 if (!do_not_reboot) {
1692 doprint "REBOOTING\n";
1693 reboot_to_good $sleep_time;
1698 if (defined($test_name)) {
1699 $name = " ($test_name)";
1704 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1705 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1706 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
1707 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1708 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1710 if (defined($store_failures)) {
1711 save_logs "fail", $store_failures;
1714 if (defined($post_test)) {
1715 run_command $post_test;
1722 my ($command, $redirect, $timeout) = @_;
1730 $command =~ s/\$SSH_USER/$ssh_user/g;
1731 $command =~ s/\$MACHINE/$machine/g;
1733 doprint("$command ... ");
1736 $pid = open(CMD, "$command 2>&1 |") or
1737 (fail "unable to exec $command" and return 0);
1739 if (defined($opt{"LOG_FILE"})) {
1740 open(LOG, ">>$opt{LOG_FILE}") or
1741 dodie "failed to write to log";
1745 if (defined($redirect)) {
1746 if ($redirect eq 1) {
1748 # Have the output of the command on its own line
1751 open (RD, ">$redirect") or
1752 dodie "failed to write to redirect $redirect";
1757 my $hit_timeout = 0;
1761 if (defined($timeout)) {
1762 doprint "timeout = $timeout\n";
1764 my $line = wait_for_input($fp, $timeout);
1765 if (!defined($line)) {
1767 if (defined($timeout) && (($now - $start_time) >= $timeout)) {
1768 doprint "Hit timeout of $timeout, killing process\n";
1774 print LOG $line if ($dolog);
1775 print RD $line if ($dord);
1776 print $line if ($dostdout);
1780 # shift 8 for real exit status
1781 $run_command_status = $? >> 8;
1784 close(LOG) if ($dolog);
1785 close(RD) if ($dord);
1788 my $delta = $end_time - $start_time;
1791 doprint "[1 second] ";
1793 doprint "[$delta seconds] ";
1797 $run_command_status = 1;
1800 if ($run_command_status) {
1801 doprint "FAILED!\n";
1803 doprint "SUCCESS\n";
1806 return !$run_command_status;
1810 my ($cmd, $timeout) = @_;
1811 my $cp_exec = $ssh_exec;
1813 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1814 return run_command "$cp_exec", undef , $timeout;
1818 my ($src, $dst, $cp_scp) = @_;
1820 $cp_scp =~ s/\$SRC_FILE/$src/g;
1821 $cp_scp =~ s/\$DST_FILE/$dst/g;
1823 return run_command "$cp_scp";
1826 sub run_scp_install {
1827 my ($src, $dst) = @_;
1829 my $cp_scp = $scp_to_target_install;
1831 return run_scp($src, $dst, $cp_scp);
1835 my ($src, $dst) = @_;
1837 my $cp_scp = $scp_to_target;
1839 return run_scp($src, $dst, $cp_scp);
1842 sub get_grub2_index {
1844 return if (defined($grub_number) && defined($last_grub_menu) &&
1845 $last_grub_menu eq $grub_menu && defined($last_machine) &&
1846 $last_machine eq $machine);
1848 doprint "Find grub2 menu ... ";
1851 my $ssh_grub = $ssh_exec;
1852 $ssh_grub =~ s,\$SSH_COMMAND,cat $grub_file,g;
1854 open(IN, "$ssh_grub |")
1855 or die "unable to get $grub_file";
1860 if (/^menuentry.*$grub_menu/) {
1864 } elsif (/^menuentry\s/) {
1870 die "Could not find '$grub_menu' in $grub_file on $machine"
1872 doprint "$grub_number\n";
1873 $last_grub_menu = $grub_menu;
1874 $last_machine = $machine;
1877 sub get_grub_index {
1879 if ($reboot_type eq "grub2") {
1884 if ($reboot_type ne "grub") {
1887 return if (defined($grub_number) && defined($last_grub_menu) &&
1888 $last_grub_menu eq $grub_menu && defined($last_machine) &&
1889 $last_machine eq $machine);
1891 doprint "Find grub menu ... ";
1894 my $ssh_grub = $ssh_exec;
1895 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1897 open(IN, "$ssh_grub |")
1898 or die "unable to get menu.lst";
1903 if (/^\s*title\s+$grub_menu\s*$/) {
1907 } elsif (/^\s*title\s/) {
1913 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
1915 doprint "$grub_number\n";
1916 $last_grub_menu = $grub_menu;
1917 $last_machine = $machine;
1922 my ($fp, $time) = @_;
1931 if (!defined($time)) {
1936 vec($rin, fileno($fp), 1) = 1;
1937 vec($rin, fileno(\*STDIN), 1) = 1;
1942 $nr = select($rout=$rin, undef, undef, $time);
1946 # copy data from stdin to the console
1947 if (vec($rout, fileno(\*STDIN), 1) == 1) {
1948 $nr = sysread(\*STDIN, $buf, 1000);
1949 syswrite($fp, $buf, $nr) if ($nr > 0);
1952 # The timeout is based on time waiting for the fp data
1953 if (vec($rout, fileno($fp), 1) != 1) {
1954 last if (defined($time) && (time - $start_time > $time));
1960 # try to read one char at a time
1961 while (sysread $fp, $ch, 1) {
1963 last if ($ch eq "\n");
1966 last if (!length($line));
1974 if (defined($switch_to_test)) {
1975 run_command $switch_to_test;
1978 if ($reboot_type eq "grub") {
1979 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1980 } elsif ($reboot_type eq "grub2") {
1981 run_ssh "$grub_reboot $grub_number";
1982 } elsif ($reboot_type eq "syslinux") {
1983 run_ssh "$syslinux --once \\\"$syslinux_label\\\" $syslinux_path";
1984 } elsif (defined $reboot_script) {
1985 run_command "$reboot_script";
1993 doprint "git rev-list --max-count=1 $commit ... ";
1994 my $sha1 = `git rev-list --max-count=1 $commit`;
2001 dodie "Failed to get git $commit";
2014 my $bug_ignored = 0;
2015 my $skip_call_trace = 0;
2018 my $start_time = time;
2025 open(DMESG, "> $dmesg") or
2026 die "unable to write to $dmesg";
2032 my $monitor_start = time;
2034 my $version_found = 0;
2038 if ($bug && defined($stop_after_failure) &&
2039 $stop_after_failure >= 0) {
2040 my $time = $stop_after_failure - (time - $failure_start);
2041 $line = wait_for_input($monitor_fp, $time);
2042 if (!defined($line)) {
2043 doprint "bug timed out after $booted_timeout seconds\n";
2044 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
2048 $line = wait_for_input($monitor_fp, $booted_timeout);
2049 if (!defined($line)) {
2050 my $s = $booted_timeout == 1 ? "" : "s";
2051 doprint "Successful boot found: break after $booted_timeout second$s\n";
2055 $line = wait_for_input($monitor_fp);
2056 if (!defined($line)) {
2057 my $s = $timeout == 1 ? "" : "s";
2058 doprint "Timed out after $timeout second$s\n";
2066 # we are not guaranteed to get a full line
2067 $full_line .= $line;
2069 if ($full_line =~ /$success_line/) {
2071 $success_start = time;
2074 if ($booted && defined($stop_after_success) &&
2075 $stop_after_success >= 0) {
2077 if ($now - $success_start >= $stop_after_success) {
2078 doprint "Test forced to stop after $stop_after_success seconds after success\n";
2083 if ($full_line =~ /\[ backtrace testing \]/) {
2084 $skip_call_trace = 1;
2087 if ($full_line =~ /call trace:/i) {
2088 if (!$bug && !$skip_call_trace) {
2089 if ($ignore_errors) {
2093 $failure_start = time;
2098 if ($bug && defined($stop_after_failure) &&
2099 $stop_after_failure >= 0) {
2101 if ($now - $failure_start >= $stop_after_failure) {
2102 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
2107 if ($full_line =~ /\[ end of backtrace testing \]/) {
2108 $skip_call_trace = 0;
2111 if ($full_line =~ /Kernel panic -/) {
2112 $failure_start = time;
2116 # Detect triple faults by testing the banner
2117 if ($full_line =~ /\bLinux version (\S+).*\n/) {
2118 if ($1 eq $version) {
2120 } elsif ($version_found && $detect_triplefault) {
2121 # We already booted into the kernel we are testing,
2122 # but now we booted into another kernel?
2123 # Consider this a triple fault.
2124 doprint "Already booted in Linux kernel $version, but now\n";
2125 doprint "we booted into Linux kernel $1.\n";
2126 doprint "Assuming that this is a triple fault.\n";
2127 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
2132 if ($line =~ /\n/) {
2136 if ($stop_test_after > 0 && !$booted && !$bug) {
2137 if (time - $monitor_start > $stop_test_after) {
2138 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
2144 my $end_time = time;
2145 $reboot_time = $end_time - $start_time;
2150 return 0 if ($in_bisect);
2151 fail "failed - got a bug report" and return 0;
2155 return 0 if ($in_bisect);
2156 fail "failed - never got a boot prompt." and return 0;
2160 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
2166 sub eval_kernel_version {
2169 $option =~ s/\$KERNEL_VERSION/$version/g;
2174 sub do_post_install {
2176 return if (!defined($post_install));
2178 my $cp_post_install = eval_kernel_version $post_install;
2179 run_command "$cp_post_install" or
2180 dodie "Failed to run post install";
2183 # Sometimes the reboot fails, and will hang. We try to ssh to the box
2184 # and if we fail, we force another reboot, that should powercycle it.
2186 if (!run_ssh "echo testing connection") {
2193 return if ($no_install);
2195 my $start_time = time;
2197 if (defined($pre_install)) {
2198 my $cp_pre_install = eval_kernel_version $pre_install;
2199 run_command "$cp_pre_install" or
2200 dodie "Failed to run pre install";
2203 my $cp_target = eval_kernel_version $target_image;
2207 run_scp_install "$outputdir/$build_target", "$cp_target" or
2208 dodie "failed to copy image";
2210 my $install_mods = 0;
2212 # should we process modules?
2214 open(IN, "$output_config") or dodie("Can't read config file");
2216 if (/CONFIG_MODULES(=y)?/) {
2225 if (!$install_mods) {
2227 doprint "No modules needed\n";
2228 my $end_time = time;
2229 $install_time = $end_time - $start_time;
2233 run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
2234 dodie "Failed to install modules";
2236 my $modlib = "/lib/modules/$version";
2237 my $modtar = "ktest-mods.tar.bz2";
2239 run_ssh "rm -rf $modlib" or
2240 dodie "failed to remove old mods: $modlib";
2242 # would be nice if scp -r did not follow symbolic links
2243 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
2244 dodie "making tarball";
2246 run_scp_mod "$tmpdir/$modtar", "/tmp" or
2247 dodie "failed to copy modules";
2249 unlink "$tmpdir/$modtar";
2251 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
2252 dodie "failed to tar modules";
2254 run_ssh "rm -f /tmp/$modtar";
2258 my $end_time = time;
2259 $install_time = $end_time - $start_time;
2263 # get the release name
2264 return if ($have_version);
2265 doprint "$make kernelrelease ... ";
2266 $version = `$make -s kernelrelease | tail -1`;
2268 doprint "$version\n";
2272 sub start_monitor_and_install {
2273 # Make sure the stable kernel has finished booting
2275 # Install bisects, don't need console
2276 if (defined $console) {
2286 start_monitor if (defined $console);
2290 my $check_build_re = ".*:.*(warning|error|Error):.*";
2291 my $utf8_quote = "\\x{e2}\\x{80}(\\x{98}|\\x{99})";
2293 sub process_warning_line {
2298 # for distcc heterogeneous systems, some compilers
2299 # do things differently causing warning lines
2300 # to be slightly different. This makes an attempt
2301 # to fixe those issues.
2303 # chop off the index into the line
2304 # using distcc, some compilers give different indexes
2305 # depending on white space
2306 $line =~ s/^(\s*\S+:\d+:)\d+/$1/;
2308 # Some compilers use UTF-8 extended for quotes and some don't.
2309 $line =~ s/$utf8_quote/'/g;
2314 # Read buildlog and check against warnings file for any
2319 sub check_buildlog {
2320 return 1 if (!defined $warnings_file);
2324 # Failed builds should not reboot the target
2325 my $save_no_reboot = $no_reboot;
2328 if (-f $warnings_file) {
2329 open(IN, $warnings_file) or
2330 dodie "Error opening $warnings_file";
2333 if (/$check_build_re/) {
2334 my $warning = process_warning_line $_;
2336 $warnings_list{$warning} = 1;
2342 # If warnings file didn't exist, and WARNINGS_FILE exist,
2343 # then we fail on any warning!
2345 open(IN, $buildlog) or dodie "Can't open $buildlog";
2347 if (/$check_build_re/) {
2348 my $warning = process_warning_line $_;
2350 if (!defined $warnings_list{$warning}) {
2351 fail "New warning found (not in $warnings_file)\n$_\n";
2352 $no_reboot = $save_no_reboot;
2357 $no_reboot = $save_no_reboot;
2361 sub check_patch_buildlog {
2364 my @files = `git show $patch | diffstat -l`;
2366 foreach my $file (@files) {
2370 open(IN, "git show $patch |") or
2371 dodie "failed to show $patch";
2373 if (m,^--- a/(.*),) {
2375 $files[$#files] = $1;
2380 open(IN, $buildlog) or dodie "Can't open $buildlog";
2382 if (/^\s*(.*?):.*(warning|error)/) {
2384 foreach my $file (@files) {
2385 my $fullpath = "$builddir/$file";
2386 if ($file eq $err || $fullpath eq $err) {
2387 fail "$file built with warnings" and return 0;
2397 sub apply_min_config {
2398 my $outconfig = "$output_config.new";
2400 # Read the config file and remove anything that
2401 # is in the force_config hash (from minconfig and others)
2402 # then add the force config back.
2404 doprint "Applying minimum configurations into $output_config.new\n";
2406 open (OUT, ">$outconfig") or
2407 dodie "Can't create $outconfig";
2409 if (-f $output_config) {
2410 open (IN, $output_config) or
2411 dodie "Failed to open $output_config";
2413 if (/^(# )?(CONFIG_[^\s=]*)/) {
2414 next if (defined($force_config{$2}));
2420 foreach my $config (keys %force_config) {
2421 print OUT "$force_config{$config}\n";
2425 run_command "mv $outconfig $output_config";
2428 sub make_oldconfig {
2430 my @force_list = keys %force_config;
2432 if ($#force_list >= 0) {
2436 if (!run_command "$make olddefconfig") {
2437 # Perhaps olddefconfig doesn't exist in this version of the kernel
2439 doprint "olddefconfig failed, trying make oldnoconfig\n";
2440 if (!run_command "$make oldnoconfig") {
2441 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
2442 # try a yes '' | oldconfig
2443 run_command "yes '' | $make oldconfig" or
2444 dodie "failed make config oldconfig";
2449 # read a config file and use this to force new configs.
2450 sub load_force_config {
2453 doprint "Loading force configs from $config\n";
2454 open(IN, $config) or
2455 dodie "failed to read $config";
2458 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
2459 $force_config{$1} = $_;
2460 } elsif (/^# (CONFIG_\S*) is not set/) {
2461 $force_config{$1} = $_;
2472 my $start_time = time;
2474 # Failed builds should not reboot the target
2475 my $save_no_reboot = $no_reboot;
2478 # Calculate a new version from here.
2481 if (defined($pre_build)) {
2482 my $ret = run_command $pre_build;
2483 if (!$ret && defined($pre_build_die) &&
2485 dodie "failed to pre_build\n";
2489 if ($type =~ /^useconfig:(.*)/) {
2490 run_command "cp $1 $output_config" or
2491 dodie "could not copy $1 to .config";
2493 $type = "oldconfig";
2496 # old config can ask questions
2497 if ($type eq "oldconfig") {
2498 $type = "olddefconfig";
2500 # allow for empty configs
2501 run_command "touch $output_config";
2504 run_command "mv $output_config $outputdir/config_temp" or
2505 dodie "moving .config";
2507 run_command "$make mrproper" or dodie "make mrproper";
2509 run_command "mv $outputdir/config_temp $output_config" or
2510 dodie "moving config_temp";
2513 } elsif (!$noclean) {
2514 unlink "$output_config";
2515 run_command "$make mrproper" or
2516 dodie "make mrproper";
2519 # add something to distinguish this build
2520 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
2521 print OUT "$localversion\n";
2524 if (defined($minconfig)) {
2525 load_force_config($minconfig);
2528 if ($type ne "olddefconfig") {
2529 run_command "$make $type" or
2530 dodie "failed make config";
2532 # Run old config regardless, to enforce min configurations
2535 my $build_ret = run_command "$make $build_options", $buildlog;
2537 if (defined($post_build)) {
2538 # Because a post build may change the kernel version
2541 my $ret = run_command $post_build;
2542 if (!$ret && defined($post_build_die) &&
2544 dodie "failed to post_build\n";
2549 # bisect may need this to pass
2551 $no_reboot = $save_no_reboot;
2554 fail "failed build" and return 0;
2557 $no_reboot = $save_no_reboot;
2559 my $end_time = time;
2560 $build_time = $end_time - $start_time;
2566 if (!run_ssh "halt" or defined($power_off)) {
2567 if (defined($poweroff_after_halt)) {
2568 sleep $poweroff_after_halt;
2569 run_command "$power_off";
2573 run_command "$power_off";
2584 if (defined($test_name)) {
2585 $name = " ($test_name)";
2590 doprint "\n\n*******************************************\n";
2591 doprint "*******************************************\n";
2592 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
2593 doprint "*******************************************\n";
2594 doprint "*******************************************\n";
2596 if (defined($store_successes)) {
2597 save_logs "success", $store_successes;
2600 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
2601 doprint "Reboot and wait $sleep_time seconds\n";
2602 reboot_to_good $sleep_time;
2605 if (defined($post_test)) {
2606 run_command $post_test;
2612 doprint "Pass, fail, or skip? [p/f/s]";
2615 if ($ans eq "p" || $ans eq "P") {
2617 } elsif ($ans eq "f" || $ans eq "F") {
2619 } elsif ($ans eq "s" || $ans eq "S") {
2622 print "Please answer 'p', 'f', or 's'\n";
2627 sub child_run_test {
2629 # child should have no power
2630 $reboot_on_error = 0;
2631 $poweroff_on_error = 0;
2632 $die_on_failure = 1;
2634 run_command $run_test, $testlog;
2636 exit $run_command_status;
2641 sub child_finished {
2651 my $bug_ignored = 0;
2653 my $start_time = time;
2657 doprint "run test $run_test\n";
2661 $SIG{CHLD} = qw(child_finished);
2665 child_run_test if (!$child_pid);
2670 $line = wait_for_input($monitor_fp, 1);
2671 if (defined($line)) {
2673 # we are not guaranteed to get a full line
2674 $full_line .= $line;
2677 if ($full_line =~ /call trace:/i) {
2678 if ($ignore_errors) {
2685 if ($full_line =~ /Kernel panic -/) {
2689 if ($line =~ /\n/) {
2693 } while (!$child_done && !$bug);
2695 if (!$bug && $bug_ignored) {
2696 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
2700 my $failure_start = time;
2703 $line = wait_for_input($monitor_fp, 1);
2704 if (defined($line)) {
2708 if ($now - $failure_start >= $stop_after_failure) {
2711 } while (defined($line));
2713 doprint "Detected kernel crash!\n";
2714 # kill the child with extreme prejudice
2718 waitpid $child_pid, 0;
2719 $child_exit = $? >> 8;
2721 my $end_time = time;
2722 $test_time = $end_time - $start_time;
2724 if (!$bug && $in_bisect) {
2725 if (defined($bisect_ret_good)) {
2726 if ($child_exit == $bisect_ret_good) {
2730 if (defined($bisect_ret_skip)) {
2731 if ($child_exit == $bisect_ret_skip) {
2735 if (defined($bisect_ret_abort)) {
2736 if ($child_exit == $bisect_ret_abort) {
2737 fail "test abort" and return -2;
2740 if (defined($bisect_ret_bad)) {
2741 if ($child_exit == $bisect_ret_skip) {
2745 if (defined($bisect_ret_default)) {
2746 if ($bisect_ret_default eq "good") {
2748 } elsif ($bisect_ret_default eq "bad") {
2750 } elsif ($bisect_ret_default eq "skip") {
2752 } elsif ($bisect_ret_default eq "abort") {
2755 fail "unknown default action: $bisect_ret_default"
2761 if ($bug || $child_exit) {
2762 return 0 if $in_bisect;
2763 fail "test failed" and return 0;
2768 sub run_git_bisect {
2771 doprint "$command ... ";
2773 my $output = `$command 2>&1`;
2780 dodie "Failed to git bisect";
2783 doprint "SUCCESS\n";
2784 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2785 doprint "$1 [$2]\n";
2786 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
2787 $bisect_bad_commit = $1;
2788 doprint "Found bad commit... $1\n";
2791 # we already logged it, just print it now.
2799 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
2800 reboot_to_good $bisect_sleep_time;
2803 # returns 1 on success, 0 on failure, -1 on skip
2804 sub run_bisect_test {
2805 my ($type, $buildtype) = @_;
2814 build $buildtype or $failed = 1;
2816 if ($type ne "build") {
2817 if ($failed && $bisect_skip) {
2821 dodie "Failed on build" if $failed;
2824 start_monitor_and_install or $failed = 1;
2826 if ($type ne "boot") {
2827 if ($failed && $bisect_skip) {
2833 dodie "Failed on boot" if $failed;
2835 do_run_test or $failed = 1;
2846 # reboot the box to a kernel we can ssh to
2847 if ($type ne "build") {
2857 my $buildtype = "oldconfig";
2859 # We should have a minconfig to use?
2860 if (defined($minconfig)) {
2861 $buildtype = "useconfig:$minconfig";
2864 # If the user sets bisect_tries to less than 1, then no tries
2868 # Still let the user manually decide that though.
2869 if ($bisect_tries < 1 && $bisect_manual) {
2870 $ret = answer_bisect;
2873 for (my $i = 0; $i < $bisect_tries; $i++) {
2874 if ($bisect_tries > 1) {
2876 doprint("Running bisect trial $t of $bisect_tries:\n");
2878 $ret = run_bisect_test $type, $buildtype;
2880 if ($bisect_manual) {
2881 $ret = answer_bisect;
2887 # Are we looking for where it worked, not failed?
2888 if ($reverse_bisect && $ret >= 0) {
2894 } elsif ($ret == 0) {
2896 } elsif ($bisect_skip) {
2897 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2902 sub update_bisect_replay {
2903 my $tmp_log = "$tmpdir/ktest_bisect_log";
2904 run_command "git bisect log > $tmp_log" or
2905 die "can't create bisect log";
2914 die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2915 die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
2916 die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
2918 my $good = $bisect_good;
2919 my $bad = $bisect_bad;
2920 my $type = $bisect_type;
2921 my $start = $bisect_start;
2922 my $replay = $bisect_replay;
2923 my $start_files = $bisect_files;
2925 if (defined($start_files)) {
2926 $start_files = " -- " . $start_files;
2931 # convert to true sha1's
2932 $good = get_sha1($good);
2933 $bad = get_sha1($bad);
2935 if (defined($bisect_reverse) && $bisect_reverse == 1) {
2936 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2937 $reverse_bisect = 1;
2939 $reverse_bisect = 0;
2942 # Can't have a test without having a test to run
2943 if ($type eq "test" && !defined($run_test)) {
2947 # Check if a bisect was running
2948 my $bisect_start_file = "$builddir/.git/BISECT_START";
2950 my $check = $bisect_check;
2951 my $do_check = defined($check) && $check ne "0";
2953 if ( -f $bisect_start_file ) {
2954 print "Bisect in progress found\n";
2956 print " If you say yes, then no checks of good or bad will be done\n";
2958 if (defined($replay)) {
2959 print "** BISECT_REPLAY is defined in config file **";
2960 print " Ignore config option and perform new git bisect log?\n";
2961 if (read_ync " (yes, no, or cancel) ") {
2962 $replay = update_bisect_replay;
2965 } elsif (read_yn "read git log and continue?") {
2966 $replay = update_bisect_replay;
2974 my $head = get_sha1("HEAD");
2976 if ($check ne "good") {
2977 doprint "TESTING BISECT BAD [$bad]\n";
2978 run_command "git checkout $bad" or
2979 die "Failed to checkout $bad";
2981 $result = run_bisect $type;
2983 if ($result ne "bad") {
2984 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2988 if ($check ne "bad") {
2989 doprint "TESTING BISECT GOOD [$good]\n";
2990 run_command "git checkout $good" or
2991 die "Failed to checkout $good";
2993 $result = run_bisect $type;
2995 if ($result ne "good") {
2996 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
3000 # checkout where we started
3001 run_command "git checkout $head" or
3002 die "Failed to checkout $head";
3005 run_command "git bisect start$start_files" or
3006 dodie "could not start bisect";
3008 if (defined($replay)) {
3009 run_command "git bisect replay $replay" or
3010 dodie "failed to run replay";
3013 run_command "git bisect good $good" or
3014 dodie "could not set bisect good to $good";
3016 run_git_bisect "git bisect bad $bad" or
3017 dodie "could not set bisect bad to $bad";
3021 if (defined($start)) {
3022 run_command "git checkout $start" or
3023 dodie "failed to checkout $start";
3028 $result = run_bisect $type;
3029 $test = run_git_bisect "git bisect $result";
3033 run_command "git bisect log" or
3034 dodie "could not capture git bisect log";
3036 run_command "git bisect reset" or
3037 dodie "could not reset git bisect";
3039 doprint "Bad commit was [$bisect_bad_commit]\n";
3044 # config_ignore holds the configs that were set (or unset) for
3045 # a good config and we will ignore these configs for the rest
3046 # of a config bisect. These configs stay as they were.
3049 # config_set holds what all configs were set as.
3052 # config_off holds the set of configs that the bad config had disabled.
3053 # We need to record them and set them in the .config when running
3054 # olddefconfig, because olddefconfig keeps the defaults.
3057 # config_off_tmp holds a set of configs to turn off for now
3060 # config_list is the set of configs that are being tested
3066 sub assign_configs {
3067 my ($hash, $config) = @_;
3069 doprint "Reading configs from $config\n";
3072 or dodie "Failed to read $config";
3076 if (/^((CONFIG\S*)=.*)/) {
3078 } elsif (/^(# (CONFIG\S*) is not set)/) {
3086 sub process_config_ignore {
3089 assign_configs \%config_ignore, $config;
3092 sub get_dependencies {
3095 my $arr = $dependency{$config};
3096 if (!defined($arr)) {
3102 foreach my $dep (@{$arr}) {
3103 print "ADD DEP $dep\n";
3104 @deps = (@deps, get_dependencies $dep);
3111 my ($pc, $file) = @_;
3113 my %configs = %{$pc};
3115 doprint "Saving configs into $file\n";
3117 open(OUT, ">$file") or dodie "Can not write to $file";
3119 foreach my $config (keys %configs) {
3120 print OUT "$configs{$config}\n";
3126 my ($name, $pc) = @_;
3128 doprint "Creating old config from $name configs\n";
3130 save_config $pc, $output_config;
3135 sub run_config_bisect_test {
3138 my $ret = run_bisect_test $type, "oldconfig";
3140 if ($bisect_manual) {
3141 $ret = answer_bisect;
3147 sub config_bisect_end {
3148 my ($good, $bad) = @_;
3149 my $diffexec = "diff -u";
3151 if (-f "$builddir/scripts/diffconfig") {
3152 $diffexec = "$builddir/scripts/diffconfig";
3154 doprint "\n\n***************************************\n";
3155 doprint "No more config bisecting possible.\n";
3156 run_command "$diffexec $good $bad", 1;
3157 doprint "***************************************\n\n";
3160 sub run_config_bisect {
3161 my ($good, $bad, $last_result) = @_;
3166 if (!length($last_result)) {
3169 run_command "$config_bisect_exec $reset -b $outputdir $good $bad $last_result", 1;
3171 # config-bisect returns:
3172 # 0 if there is more to bisect
3173 # 1 for finding a good config
3174 # 2 if it can not find any more configs
3176 if ($run_command_status) {
3177 return $run_command_status;
3180 $ret = run_config_bisect_test $config_bisect_type;
3182 doprint "NEW GOOD CONFIG\n";
3183 # Return 3 for good config
3186 doprint "NEW BAD CONFIG\n";
3187 # Return 4 for bad config
3198 my $type = $config_bisect_type;
3201 $bad_config = $config_bisect;
3203 if (defined($config_bisect_good)) {
3204 $good_config = $config_bisect_good;
3205 } elsif (defined($minconfig)) {
3206 $good_config = $minconfig;
3208 doprint "No config specified, checking if defconfig works";
3209 $ret = run_bisect_test $type, "defconfig";
3211 fail "Have no good config to compare with, please set CONFIG_BISECT_GOOD";
3214 $good_config = $output_config;
3217 if (!defined($config_bisect_exec)) {
3218 # First check the location that ktest.pl ran
3219 my @locations = ( "$pwd/config-bisect.pl",
3220 "$dirname/config-bisect.pl",
3221 "$builddir/tools/testing/ktest/config-bisect.pl",
3223 foreach my $loc (@locations) {
3224 doprint "loc = $loc\n";
3225 $config_bisect_exec = $loc;
3226 last if (defined($config_bisect_exec && -x $config_bisect_exec));
3228 if (!defined($config_bisect_exec)) {
3229 fail "Could not find an executable config-bisect.pl\n",
3230 " Set CONFIG_BISECT_EXEC to point to config-bisect.pl";
3235 # we don't want min configs to cause issues here.
3236 doprint "Disabling 'MIN_CONFIG' for this test\n";
3243 if (-f "$tmpdir/good_config.tmp" || -f "$tmpdir/bad_config.tmp") {
3244 if (read_yn "Interrupted config-bisect. Continue (n - will start new)?") {
3245 if (-f "$tmpdir/good_config.tmp") {
3246 $good_config = "$tmpdir/good_config.tmp";
3248 $good_config = "$tmpdir/good_config";
3250 if (-f "$tmpdir/bad_config.tmp") {
3251 $bad_config = "$tmpdir/bad_config.tmp";
3253 $bad_config = "$tmpdir/bad_config";
3257 doprint "Run good configs through make oldconfig\n";
3258 assign_configs \%tmp_configs, $good_config;
3259 create_config "$good_config", \%tmp_configs;
3260 $good_config = "$tmpdir/good_config";
3261 system("cp $output_config $good_config") == 0 or dodie "cp good config";
3263 doprint "Run bad configs through make oldconfig\n";
3264 assign_configs \%tmp_configs, $bad_config;
3265 create_config "$bad_config", \%tmp_configs;
3266 $bad_config = "$tmpdir/bad_config";
3267 system("cp $output_config $bad_config") == 0 or dodie "cp bad config";
3269 if (defined($config_bisect_check) && $config_bisect_check ne "0") {
3270 if ($config_bisect_check ne "good") {
3271 doprint "Testing bad config\n";
3273 $ret = run_bisect_test $type, "useconfig:$bad_config";
3275 fail "Bad config succeeded when expected to fail!";
3279 if ($config_bisect_check ne "bad") {
3280 doprint "Testing good config\n";
3282 $ret = run_bisect_test $type, "useconfig:$good_config";
3284 fail "Good config failed when expected to succeed!";
3293 $ret = run_config_bisect $good_config, $bad_config, $last_run;
3296 } elsif ($ret == 4) {
3300 } while ($ret == 3 || $ret == 4);
3303 config_bisect_end "$good_config.tmp", "$bad_config.tmp";
3306 return $ret if ($ret < 0);
3311 sub patchcheck_reboot {
3312 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
3313 reboot_to_good $patchcheck_sleep_time;
3319 die "PATCHCHECK_START[$i] not defined\n"
3320 if (!defined($patchcheck_start));
3321 die "PATCHCHECK_TYPE[$i] not defined\n"
3322 if (!defined($patchcheck_type));
3324 my $start = $patchcheck_start;
3326 my $cherry = $patchcheck_cherry;
3327 if (!defined($cherry)) {
3332 if (defined($patchcheck_end)) {
3333 $end = $patchcheck_end;
3335 die "PATCHCHECK_END must be defined with PATCHCHECK_CHERRY\n";
3338 # Get the true sha1's since we can use things like HEAD~3
3339 $start = get_sha1($start);
3340 $end = get_sha1($end);
3342 my $type = $patchcheck_type;
3344 # Can't have a test without having a test to run
3345 if ($type eq "test" && !defined($run_test)) {
3350 open (IN, "git cherry -v $start $end|") or
3351 dodie "could not get git list";
3353 open (IN, "git log --pretty=oneline $end|") or
3354 dodie "could not get git list";
3361 # git cherry adds a '+' we want to remove
3363 $list[$#list+1] = $_;
3364 last if (/^$start/);
3369 if ($list[$#list] !~ /^$start/) {
3370 fail "SHA1 $start not found";
3373 # go backwards in the list
3374 @list = reverse @list;
3377 doprint("Going to test the following commits:\n");
3378 foreach my $l (@list) {
3382 my $save_clean = $noclean;
3383 my %ignored_warnings;
3385 if (defined($ignore_warnings)) {
3386 foreach my $sha1 (split /\s+/, $ignore_warnings) {
3387 $ignored_warnings{$sha1} = 1;
3392 foreach my $item (@list) {
3394 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
3396 doprint "\nProcessing commit \"$item\"\n\n";
3398 run_command "git checkout $sha1" or
3399 die "Failed to checkout $sha1";
3401 # only clean on the first and last patch
3402 if ($item eq $list[0] ||
3403 $item eq $list[$#list]) {
3404 $noclean = $save_clean;
3409 if (defined($minconfig)) {
3410 build "useconfig:$minconfig" or return 0;
3412 # ?? no config to use?
3413 build "oldconfig" or return 0;
3416 # No need to do per patch checking if warnings file exists
3417 if (!defined($warnings_file) && !defined($ignored_warnings{$sha1})) {
3418 check_patch_buildlog $sha1 or return 0;
3421 check_buildlog or return 0;
3423 next if ($type eq "build");
3427 start_monitor_and_install or $failed = 1;
3429 if (!$failed && $type ne "boot"){
3430 do_run_test or $failed = 1;
3455 # $config depends on $dep
3456 my ($config, $dep) = @_;
3458 if (defined($depends{$config})) {
3459 $depends{$config} .= " " . $dep;
3461 $depends{$config} = $dep;
3464 # record the number of configs depending on $dep
3465 if (defined $depcount{$dep}) {
3468 $depcount{$dep} = 1;
3472 # taken from streamline_config.pl
3484 if (! -f $kconfig) {
3485 doprint "file $kconfig does not exist, skipping\n";
3489 open(KIN, "$kconfig")
3490 or die "Can't open $kconfig";
3494 # Make sure that lines ending with \ continue
3496 $_ = $line . " " . $_;
3507 # collect any Kconfig sources
3508 if (/^source\s*"(.*)"/) {
3509 $kconfigs[$#kconfigs+1] = $1;
3513 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
3517 for (my $i = 0; $i < $iflevel; $i++) {
3518 add_dep $config, $ifdeps[$i];
3521 # collect the depends for the config
3522 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
3524 add_dep $config, $1;
3526 # Get the configs that select this config
3527 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
3529 # selected by depends on config
3530 add_dep $1, $config;
3532 # Check for if statements
3533 } elsif (/^if\s+(.*\S)\s*$/) {
3535 # remove beginning and ending non text
3536 $deps =~ s/^[^a-zA-Z0-9_]*//;
3537 $deps =~ s/[^a-zA-Z0-9_]*$//;
3539 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
3541 $ifdeps[$iflevel++] = join ':', @deps;
3543 } elsif (/^endif/) {
3545 $iflevel-- if ($iflevel);
3548 } elsif (/^\s*help\s*$/) {
3554 # read in any configs that were found.
3555 foreach $kconfig (@kconfigs) {
3556 if (!defined($read_kconfigs{$kconfig})) {
3557 $read_kconfigs{$kconfig} = 1;
3558 read_kconfig("$builddir/$kconfig");
3564 # find out which arch this is by the kconfig file
3565 open (IN, $output_config)
3566 or dodie "Failed to read $output_config";
3569 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
3576 if (!defined($arch)) {
3577 doprint "Could not find arch from config file\n";
3578 doprint "no dependencies used\n";
3582 # arch is really the subarch, we need to know
3583 # what directory to look at.
3584 if ($arch eq "i386" || $arch eq "x86_64") {
3586 } elsif ($arch =~ /^tile/) {
3590 my $kconfig = "$builddir/arch/$arch/Kconfig";
3592 if (! -f $kconfig && $arch =~ /\d$/) {
3594 # some subarchs have numbers, truncate them
3596 $kconfig = "$builddir/arch/$arch/Kconfig";
3597 if (! -f $kconfig) {
3598 doprint "No idea what arch dir $orig is for\n";
3599 doprint "no dependencies used\n";
3604 read_kconfig($kconfig);
3607 sub make_new_config {
3610 open (OUT, ">$output_config")
3611 or dodie "Failed to write $output_config";
3613 foreach my $config (@configs) {
3614 print OUT "$config\n";
3622 $config =~ s/CONFIG_//;
3630 my $kconfig = chomp_config $dep;
3632 $dep = $depends{"$kconfig"};
3634 # the dep string we have saves the dependencies as they
3635 # were found, including expressions like ! && ||. We
3636 # want to split this out into just an array of configs.
3638 my $valid = "A-Za-z_0-9";
3642 while ($dep =~ /[$valid]/) {
3644 if ($dep =~ /^[^$valid]*([$valid]+)/) {
3645 my $conf = "CONFIG_" . $1;
3647 $configs[$#configs + 1] = $conf;
3649 $dep =~ s/^[^$valid]*[$valid]+//;
3651 die "this should never happen";
3661 my %processed_configs;
3662 my %nochange_config;
3664 sub test_this_config {
3669 # if we already processed this config, skip it
3670 if (defined($processed_configs{$config})) {
3673 $processed_configs{$config} = 1;
3675 # if this config failed during this round, skip it
3676 if (defined($nochange_config{$config})) {
3680 my $kconfig = chomp_config $config;
3682 # Test dependencies first
3683 if (defined($depends{"$kconfig"})) {
3684 my @parents = get_depends $config;
3685 foreach my $parent (@parents) {
3686 # if the parent is in the min config, check it first
3687 next if (!defined($min_configs{$parent}));
3688 $found = test_this_config($parent);
3689 if (defined($found)) {
3695 # Remove this config from the list of configs
3696 # do a make olddefconfig and then read the resulting
3697 # .config to make sure it is missing the config that
3699 my %configs = %min_configs;
3700 delete $configs{$config};
3701 make_new_config ((values %configs), (values %keep_configs));
3704 assign_configs \%configs, $output_config;
3706 if (!defined($configs{$config}) || $configs{$config} =~ /^#/) {
3710 doprint "disabling config $config did not change .config\n";
3712 $nochange_config{$config} = 1;
3717 sub make_min_config {
3720 my $type = $minconfig_type;
3721 if ($type ne "boot" && $type ne "test") {
3722 fail "Invalid MIN_CONFIG_TYPE '$minconfig_type'\n" .
3723 " make_min_config works only with 'boot' and 'test'\n" and return;
3726 if (!defined($output_minconfig)) {
3727 fail "OUTPUT_MIN_CONFIG not defined" and return;
3730 # If output_minconfig exists, and the start_minconfig
3731 # came from min_config, than ask if we should use
3733 if (-f $output_minconfig && !$start_minconfig_defined) {
3734 print "$output_minconfig exists\n";
3735 if (!defined($use_output_minconfig)) {
3736 if (read_yn " Use it as minconfig?") {
3737 $start_minconfig = $output_minconfig;
3739 } elsif ($use_output_minconfig > 0) {
3740 doprint "Using $output_minconfig as MIN_CONFIG\n";
3741 $start_minconfig = $output_minconfig;
3743 doprint "Set to still use MIN_CONFIG as starting point\n";
3747 if (!defined($start_minconfig)) {
3748 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
3751 my $temp_config = "$tmpdir/temp_config";
3753 # First things first. We build an allnoconfig to find
3754 # out what the defaults are that we can't touch.
3755 # Some are selections, but we really can't handle selections.
3757 my $save_minconfig = $minconfig;
3760 run_command "$make allnoconfig" or return 0;
3764 process_config_ignore $output_config;
3766 undef %save_configs;
3769 if (defined($ignore_config)) {
3770 # make sure the file exists
3771 `touch $ignore_config`;
3772 assign_configs \%save_configs, $ignore_config;
3775 %keep_configs = %save_configs;
3777 doprint "Load initial configs from $start_minconfig\n";
3779 # Look at the current min configs, and save off all the
3780 # ones that were set via the allnoconfig
3781 assign_configs \%min_configs, $start_minconfig;
3783 my @config_keys = keys %min_configs;
3785 # All configs need a depcount
3786 foreach my $config (@config_keys) {
3787 my $kconfig = chomp_config $config;
3788 if (!defined $depcount{$kconfig}) {
3789 $depcount{$kconfig} = 0;
3793 # Remove anything that was set by the make allnoconfig
3794 # we shouldn't need them as they get set for us anyway.
3795 foreach my $config (@config_keys) {
3796 # Remove anything in the ignore_config
3797 if (defined($keep_configs{$config})) {
3798 my $file = $ignore_config;
3799 $file =~ s,.*/(.*?)$,$1,;
3800 doprint "$config set by $file ... ignored\n";
3801 delete $min_configs{$config};
3804 # But make sure the settings are the same. If a min config
3805 # sets a selection, we do not want to get rid of it if
3806 # it is not the same as what we have. Just move it into
3808 if (defined($config_ignore{$config})) {
3809 if ($config_ignore{$config} ne $min_configs{$config}) {
3810 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3811 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3812 $keep_configs{$config} = $min_configs{$config};
3814 doprint "$config set by allnoconfig ... ignored\n";
3816 delete $min_configs{$config};
3828 # Now disable each config one by one and do a make oldconfig
3829 # till we find a config that changes our list.
3831 my @test_configs = keys %min_configs;
3833 # Sort keys by who is most dependent on
3834 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3837 # Put configs that did not modify the config at the end.
3839 for (my $i = 0; $i < $#test_configs; $i++) {
3840 if (!defined($nochange_config{$test_configs[0]})) {
3844 # This config didn't change the .config last time.
3845 # Place it at the end
3846 my $config = shift @test_configs;
3847 push @test_configs, $config;
3850 # if every test config has failed to modify the .config file
3851 # in the past, then reset and start over.
3853 undef %nochange_config;
3856 undef %processed_configs;
3858 foreach my $config (@test_configs) {
3860 $found = test_this_config $config;
3862 last if (defined($found));
3864 # oh well, try another config
3867 if (!defined($found)) {
3868 # we could have failed due to the nochange_config hash
3869 # reset and try again
3871 undef %nochange_config;
3875 doprint "No more configs found that we can disable\n";
3883 doprint "Test with $config disabled\n";
3885 # set in_bisect to keep build and monitor from dieing
3889 build "oldconfig" or $failed = 1;
3891 start_monitor_and_install or $failed = 1;
3893 if ($type eq "test" && !$failed) {
3894 do_run_test or $failed = 1;
3903 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
3904 # this config is needed, add it to the ignore list.
3905 $keep_configs{$config} = $min_configs{$config};
3906 $save_configs{$config} = $min_configs{$config};
3907 delete $min_configs{$config};
3909 # update new ignore configs
3910 if (defined($ignore_config)) {
3911 open (OUT, ">$temp_config")
3912 or die "Can't write to $temp_config";
3913 foreach my $config (keys %save_configs) {
3914 print OUT "$save_configs{$config}\n";
3917 run_command "mv $temp_config $ignore_config" or
3918 dodie "failed to copy update to $ignore_config";
3922 # We booted without this config, remove it from the minconfigs.
3923 doprint "$config is not needed, disabling\n";
3925 delete $min_configs{$config};
3927 # Also disable anything that is not enabled in this config
3929 assign_configs \%configs, $output_config;
3930 my @config_keys = keys %min_configs;
3931 foreach my $config (@config_keys) {
3932 if (!defined($configs{$config})) {
3933 doprint "$config is not set, disabling\n";
3934 delete $min_configs{$config};
3938 # Save off all the current mandatory configs
3939 open (OUT, ">$temp_config")
3940 or die "Can't write to $temp_config";
3941 foreach my $config (keys %keep_configs) {
3942 print OUT "$keep_configs{$config}\n";
3944 foreach my $config (keys %min_configs) {
3945 print OUT "$min_configs{$config}\n";
3949 run_command "mv $temp_config $output_minconfig" or
3950 dodie "failed to copy update to $output_minconfig";
3953 doprint "Reboot and wait $sleep_time seconds\n";
3954 reboot_to_good $sleep_time;
3961 sub make_warnings_file {
3964 if (!defined($warnings_file)) {
3965 dodie "Must define WARNINGS_FILE for make_warnings_file test";
3968 if ($build_type eq "nobuild") {
3969 dodie "BUILD_TYPE can not be 'nobuild' for make_warnings_file test";
3972 build $build_type or dodie "Failed to build";
3974 open(OUT, ">$warnings_file") or dodie "Can't create $warnings_file";
3976 open(IN, $buildlog) or dodie "Can't open $buildlog";
3979 # Some compilers use UTF-8 extended for quotes
3980 # for distcc heterogeneous systems, this causes issues
3983 if (/$check_build_re/) {
3994 $#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl [config-file]\n";
3997 $ktest_config = $ARGV[0];
3998 if (! -f $ktest_config) {
3999 print "$ktest_config does not exist.\n";
4000 if (!read_yn "Create it?") {
4006 if (! -f $ktest_config) {
4009 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
4011 # Generated by ktest.pl
4014 # PWD is a ktest.pl variable that will result in the process working
4015 # directory that ktest.pl is executed in.
4017 # THIS_DIR is automatically assigned the PWD of the path that generated
4018 # the config file. It is best to use this variable when assigning other
4019 # directory paths within this directory. This allows you to easily
4020 # move the test cases to other locations or to other machines.
4022 THIS_DIR := $variable{"PWD"}
4024 # Define each test with TEST_START
4025 # The config options below it will override the defaults
4027 TEST_TYPE = $default{"TEST_TYPE"}
4034 read_config $ktest_config;
4036 if (defined($opt{"LOG_FILE"})) {
4037 $opt{"LOG_FILE"} = eval_option("LOG_FILE", $opt{"LOG_FILE"}, -1);
4040 # Append any configs entered in manually to the config file.
4041 my @new_configs = keys %entered_configs;
4042 if ($#new_configs >= 0) {
4043 print "\nAppending entered in configs to $ktest_config\n";
4044 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
4045 foreach my $config (@new_configs) {
4046 print OUT "$config = $entered_configs{$config}\n";
4047 $opt{$config} = process_variables($entered_configs{$config});
4051 if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
4052 unlink $opt{"LOG_FILE"};
4055 doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
4057 for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
4060 doprint "DEFAULT OPTIONS:\n";
4062 doprint "\nTEST $i OPTIONS";
4063 if (defined($repeat_tests{$i})) {
4064 $repeat = $repeat_tests{$i};
4065 doprint " ITERATE $repeat";
4070 foreach my $option (sort keys %opt) {
4072 if ($option =~ /\[(\d+)\]$/) {
4078 doprint "$option = $opt{$option}\n";
4082 sub option_defined {
4085 if (defined($opt{$option}) && $opt{$option} !~ /^\s*$/) {
4092 sub __set_test_option {
4093 my ($name, $i) = @_;
4095 my $option = "$name\[$i\]";
4097 if (option_defined($option)) {
4098 return $opt{$option};
4101 foreach my $test (keys %repeat_tests) {
4103 $i < $test + $repeat_tests{$test}) {
4104 $option = "$name\[$test\]";
4105 if (option_defined($option)) {
4106 return $opt{$option};
4111 if (option_defined($name)) {
4118 sub set_test_option {
4119 my ($name, $i) = @_;
4121 my $option = __set_test_option($name, $i);
4122 return $option if (!defined($option));
4124 return eval_option($name, $option, $i);
4128 my ($subject, $message) = @_;
4129 system("$mailer -s \'$subject\' $mailto <<< \'$message\'");
4132 sub _sendmail_send {
4133 my ($subject, $message) = @_;
4134 system("echo -e \"Subject: $subject\n\n$message\" | sendmail -t $mailto");
4138 if (defined($mailto) && defined($mailer)) {
4139 if ($mailer eq "mail" || $mailer eq "mailx"){ _mailx_send(@_);}
4140 elsif ($mailer eq "sendmail" ) { _sendmail_send(@_);}
4141 else { doprint "\nYour mailer: $mailer is not supported.\n" }
4143 print "No email sent: email or mailer not specified in config.\n"
4148 if ($email_when_canceled) {
4149 send_email("KTEST: Your [$test_type] test was cancelled",
4150 "Your test started at $script_start_time was cancelled: sig int");
4152 die "\nCaught Sig Int, test interrupted: $!\n"
4155 $SIG{INT} = qw(cancel_test);
4157 # First we need to do is the builds
4158 for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
4160 # Do not reboot on failing test options
4162 $reboot_success = 0;
4173 undef %force_config;
4175 my $makecmd = set_test_option("MAKE_CMD", $i);
4177 $outputdir = set_test_option("OUTPUT_DIR", $i);
4178 $builddir = set_test_option("BUILD_DIR", $i);
4180 chdir $builddir || die "can't change directory to $builddir";
4182 if (!-d $outputdir) {
4183 mkpath($outputdir) or
4184 die "can't create $outputdir";
4187 $make = "$makecmd O=$outputdir";
4189 # Load all the options into their mapped variable names
4190 foreach my $opt (keys %option_map) {
4191 ${$option_map{$opt}} = set_test_option($opt, $i);
4194 $start_minconfig_defined = 1;
4196 # The first test may override the PRE_KTEST option
4198 if (defined($pre_ktest)) {
4200 run_command $pre_ktest;
4202 if ($email_when_started) {
4203 send_email("KTEST: Your [$test_type] test was started",
4204 "Your test was started on $script_start_time");
4208 # Any test can override the POST_KTEST option
4209 # The last test takes precedence.
4210 if (defined($post_ktest)) {
4211 $final_post_ktest = $post_ktest;
4214 if (!defined($start_minconfig)) {
4215 $start_minconfig_defined = 0;
4216 $start_minconfig = $minconfig;
4221 die "can't create $tmpdir";
4224 $ENV{"SSH_USER"} = $ssh_user;
4225 $ENV{"MACHINE"} = $machine;
4227 $buildlog = "$tmpdir/buildlog-$machine";
4228 $testlog = "$tmpdir/testlog-$machine";
4229 $dmesg = "$tmpdir/dmesg-$machine";
4230 $output_config = "$outputdir/.config";
4233 $target = "$ssh_user\@$machine";
4234 if ($reboot_type eq "grub") {
4235 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
4236 } elsif ($reboot_type eq "grub2") {
4237 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
4238 dodie "GRUB_FILE not defined" if (!defined($grub_file));
4239 } elsif ($reboot_type eq "syslinux") {
4240 dodie "SYSLINUX_LABEL not defined" if (!defined($syslinux_label));
4244 my $run_type = $build_type;
4245 if ($test_type eq "patchcheck") {
4246 $run_type = $patchcheck_type;
4247 } elsif ($test_type eq "bisect") {
4248 $run_type = $bisect_type;
4249 } elsif ($test_type eq "config_bisect") {
4250 $run_type = $config_bisect_type;
4251 } elsif ($test_type eq "make_min_config") {
4253 } elsif ($test_type eq "make_warnings_file") {
4257 # mistake in config file?
4258 if (!defined($run_type)) {
4259 $run_type = "ERROR";
4263 $installme = " no_install" if ($no_install);
4267 if (defined($test_name)) {
4268 $name = " ($test_name)";
4272 doprint "RUNNING TEST $i of $opt{NUM_TESTS}$name with option $test_type $run_type$installme\n\n";
4274 if (defined($pre_test)) {
4275 run_command $pre_test;
4282 if (defined($addconfig)) {
4283 my $min = $minconfig;
4284 if (!defined($minconfig)) {
4287 run_command "cat $addconfig $min > $tmpdir/add_config" or
4288 dodie "Failed to create temp config";
4289 $minconfig = "$tmpdir/add_config";
4292 if (defined($checkout)) {
4293 run_command "git checkout $checkout" or
4294 die "failed to checkout $checkout";
4299 # A test may opt to not reboot the box
4300 if ($reboot_on_success) {
4301 $reboot_success = 1;
4304 if ($test_type eq "bisect") {
4307 } elsif ($test_type eq "config_bisect") {
4310 } elsif ($test_type eq "patchcheck") {
4313 } elsif ($test_type eq "make_min_config") {
4316 } elsif ($test_type eq "make_warnings_file") {
4318 make_warnings_file $i;
4322 if ($build_type ne "nobuild") {
4323 build $build_type or next;
4324 check_buildlog or next;
4327 if ($test_type eq "install") {
4334 if ($test_type ne "build") {
4336 start_monitor_and_install or $failed = 1;
4338 if (!$failed && $test_type ne "boot" && defined($run_test)) {
4339 do_run_test or $failed = 1;
4353 if (defined($final_post_ktest)) {
4354 run_command $final_post_ktest;
4357 if ($opt{"POWEROFF_ON_SUCCESS"}) {
4359 } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot && $reboot_success) {
4361 } elsif (defined($switch_to_good)) {
4362 # still need to get to the good kernel
4363 run_command $switch_to_good;
4367 doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
4369 if ($email_when_finished) {
4370 send_email("KTEST: Your [$test_type] test has finished!",
4371 "$successes of $opt{NUM_TESTS} tests started at $script_start_time were successful!");