ktest: When creating new config, allow the use of ${THIS_DIR}
[deliverable/linux.git] / tools / testing / ktest / ktest.pl
CommitLineData
2545eb61 1#!/usr/bin/perl -w
d6ce2a0b 2#
cce1dac8 3# Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
d6ce2a0b
SR
4# Licensed under the terms of the GNU GPL License version 2
5#
2545eb61
SR
6
7use strict;
8use IPC::Open2;
9use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
7faafbd6
SR
10use File::Path qw(mkpath);
11use File::Copy qw(cp);
2545eb61
SR
12use FileHandle;
13
e48c5293
SR
14my $VERSION = "0.2";
15
2545eb61
SR
16$| = 1;
17
18my %opt;
a57419b3
SR
19my %repeat_tests;
20my %repeats;
a75fecec 21my %default;
2545eb61
SR
22
23#default opts
a57419b3 24$default{"NUM_TESTS"} = 1;
a75fecec
SR
25$default{"TEST_TYPE"} = "test";
26$default{"BUILD_TYPE"} = "randconfig";
27$default{"MAKE_CMD"} = "make";
28$default{"TIMEOUT"} = 120;
48920630 29$default{"TMP_DIR"} = "/tmp/ktest/\${MACHINE}";
a75fecec
SR
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
27d934b2 38$default{"PATCHCHECK_SLEEP_TIME"} = 60; # sleep time between patch checks
a75fecec 39$default{"CLEAR_LOG"} = 0;
c960bb9f 40$default{"BISECT_MANUAL"} = 0;
c23dca7c 41$default{"BISECT_SKIP"} = 1;
a75fecec 42$default{"SUCCESS_LINE"} = "login:";
f1a5b962 43$default{"DETECT_TRIPLE_FAULT"} = 1;
e0a8742e 44$default{"NO_INSTALL"} = 0;
a75fecec
SR
45$default{"BOOTED_TIMEOUT"} = 1;
46$default{"DIE_ON_FAILURE"} = 1;
e48c5293
SR
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";
1c8a617a
SR
50$default{"STOP_AFTER_SUCCESS"} = 10;
51$default{"STOP_AFTER_FAILURE"} = 60;
2d01b26a 52$default{"STOP_TEST_AFTER"} = 600;
600bbf0a
SR
53
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";
8d1491ba 57$default{"LOCALVERSION"} = "-test";
600bbf0a
SR
58$default{"SSH_USER"} = "root";
59$default{"BUILD_TARGET"} = "arch/x86/boot/bzImage";
60$default{"TARGET_IMAGE"} = "/boot/vmlinuz-test";
2545eb61 61
8d1491ba 62my $ktest_config;
2545eb61 63my $version;
a75fecec 64my $machine;
e48c5293 65my $ssh_user;
a75fecec
SR
66my $tmpdir;
67my $builddir;
68my $outputdir;
51ad1dd1 69my $output_config;
a75fecec 70my $test_type;
7faafbd6 71my $build_type;
a75fecec 72my $build_options;
0bd6c1a3
SR
73my $pre_build;
74my $post_build;
75my $pre_build_die;
76my $post_build_die;
a75fecec
SR
77my $reboot_type;
78my $reboot_script;
79my $power_cycle;
e48c5293 80my $reboot;
a75fecec
SR
81my $reboot_on_error;
82my $poweroff_on_error;
83my $die_on_failure;
576f627c
SR
84my $powercycle_after_reboot;
85my $poweroff_after_halt;
e48c5293
SR
86my $ssh_exec;
87my $scp_to_target;
a75fecec
SR
88my $power_off;
89my $grub_menu;
2545eb61
SR
90my $grub_number;
91my $target;
92my $make;
8b37ca8c 93my $post_install;
e0a8742e 94my $no_install;
5c42fc5b 95my $noclean;
5f9b6ced 96my $minconfig;
4c4ab120 97my $start_minconfig;
35ce5952 98my $start_minconfig_defined;
4c4ab120
SR
99my $output_minconfig;
100my $ignore_config;
2b7d9b21 101my $addconfig;
5f9b6ced
SR
102my $in_bisect = 0;
103my $bisect_bad = "";
d6ce2a0b 104my $reverse_bisect;
c960bb9f 105my $bisect_manual;
c23dca7c 106my $bisect_skip;
30f75da5 107my $config_bisect_good;
6c5ee0be 108my $in_patchcheck = 0;
5a391fbf 109my $run_test;
6c5ee0be 110my $redirect;
7faafbd6 111my $buildlog;
a9dd5d63 112my $testlog;
7faafbd6
SR
113my $dmesg;
114my $monitor_fp;
115my $monitor_pid;
116my $monitor_cnt = 0;
a75fecec
SR
117my $sleep_time;
118my $bisect_sleep_time;
27d934b2 119my $patchcheck_sleep_time;
1990207d 120my $ignore_warnings;
a75fecec 121my $store_failures;
de5b6e3b 122my $store_successes;
9064af52 123my $test_name;
a75fecec
SR
124my $timeout;
125my $booted_timeout;
f1a5b962 126my $detect_triplefault;
a75fecec 127my $console;
2b803365 128my $reboot_success_line;
a75fecec 129my $success_line;
1c8a617a
SR
130my $stop_after_success;
131my $stop_after_failure;
2d01b26a 132my $stop_test_after;
a75fecec
SR
133my $build_target;
134my $target_image;
135my $localversion;
576f627c 136my $iteration = 0;
e48c5293 137my $successes = 0;
2545eb61 138
8d1491ba
SR
139my %entered_configs;
140my %config_help;
77d942ce 141my %variable;
fcb3f16a 142my %force_config;
8d1491ba 143
4ab1cce5
SR
144# do not force reboots on config problems
145my $no_reboot = 1;
146
7bf51073
SR
147# default variables that can be used
148chomp ($variable{"PWD"} = `pwd`);
149
8d1491ba
SR
150$config_help{"MACHINE"} = << "EOF"
151 The machine hostname that you will test.
152EOF
153 ;
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)
157EOF
158 ;
159$config_help{"BUILD_DIR"} = << "EOF"
160 The directory that contains the Linux source code (full path).
0e7a22de
SR
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.
8d1491ba
SR
163EOF
164 ;
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)
0e7a22de
SR
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.
8d1491ba
SR
170EOF
171 ;
172$config_help{"BUILD_TARGET"} = << "EOF"
173 The location of the compiled file to copy to the target.
174 (relative to OUTPUT_DIR)
175EOF
176 ;
177$config_help{"TARGET_IMAGE"} = << "EOF"
178 The place to put your image on the test machine.
179EOF
180 ;
181$config_help{"POWER_CYCLE"} = << "EOF"
182 A script or command to reboot the box.
183
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'
186
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
190EOF
191 ;
192$config_help{"CONSOLE"} = << "EOF"
193 The script or command that reads the console
194
195 If you use ttywatch server, something like the following would work.
196CONSOLE = nc -d localhost 3001
197
198 For a virtual machine with guest name "Guest".
199CONSOLE = virsh console Guest
200EOF
201 ;
202$config_help{"LOCALVERSION"} = << "EOF"
203 Required version ending to differentiate the test
204 from other linux builds on the system.
205EOF
206 ;
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".
210
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.
216
217 The entry in /boot/grub/menu.lst must be entered in manually.
218 The test will not modify that file.
219EOF
220 ;
221$config_help{"GRUB_MENU"} = << "EOF"
222 The grub title name for the test kernel to boot
223 (Only mandatory if REBOOT_TYPE = grub)
224
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
228 reboot into.
229
230 For example, if in the /boot/grub/menu.lst the test kernel title has:
231 title Test Kernel
232 kernel vmlinuz-test
233 GRUB_MENU = Test Kernel
234EOF
235 ;
236$config_help{"REBOOT_SCRIPT"} = << "EOF"
237 A script to reboot the target into the test kernel
238 (Only mandatory if REBOOT_TYPE = script)
239EOF
240 ;
241
35ce5952
SR
242sub read_yn {
243 my ($prompt) = @_;
244
245 my $ans;
246
247 for (;;) {
248 print "$prompt [Y/n] ";
249 $ans = <STDIN>;
250 chomp $ans;
251 if ($ans =~ /^\s*$/) {
252 $ans = "y";
253 }
254 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
255 print "Please answer either 'y' or 'n'.\n";
256 }
257 if ($ans !~ /^y$/i) {
258 return 0;
259 }
260 return 1;
261}
8d1491ba
SR
262
263sub get_ktest_config {
264 my ($config) = @_;
815e2bd7 265 my $ans;
8d1491ba
SR
266
267 return if (defined($opt{$config}));
268
269 if (defined($config_help{$config})) {
270 print "\n";
271 print $config_help{$config};
272 }
273
274 for (;;) {
275 print "$config = ";
276 if (defined($default{$config})) {
277 print "\[$default{$config}\] ";
278 }
815e2bd7
SR
279 $ans = <STDIN>;
280 $ans =~ s/^\s*(.*\S)\s*$/$1/;
281 if ($ans =~ /^\s*$/) {
8d1491ba 282 if ($default{$config}) {
815e2bd7 283 $ans = $default{$config};
8d1491ba
SR
284 } else {
285 print "Your answer can not be blank\n";
286 next;
287 }
288 }
0e7a22de 289 $entered_configs{$config} = ${ans};
8d1491ba
SR
290 last;
291 }
292}
293
294sub 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");
304
305 my $rtype = $opt{"REBOOT_TYPE"};
306
307 if (!defined($rtype)) {
308 if (!defined($opt{"GRUB_MENU"})) {
309 get_ktest_config("REBOOT_TYPE");
310 $rtype = $entered_configs{"REBOOT_TYPE"};
311 } else {
312 $rtype = "grub";
313 }
314 }
315
316 if ($rtype eq "grub") {
317 get_ktest_config("GRUB_MENU");
318 } else {
319 get_ktest_config("REBOOT_SCRIPT");
320 }
321}
322
77d942ce 323sub process_variables {
8d735212 324 my ($value, $remove_undef) = @_;
77d942ce
SR
325 my $retval = "";
326
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.
332 $value = " $value";
333
334 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
335 my $begin = $1;
336 my $var = $2;
337 my $end = $3;
338 # append beginning of value to retval
339 $retval = "$retval$begin";
340 if (defined($variable{$var})) {
341 $retval = "$retval$variable{$var}";
8d735212
SR
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";
77d942ce
SR
346 } else {
347 # put back the origin piece.
348 $retval = "$retval\$\{$var\}";
349 }
350 $value = $end;
351 }
352 $retval = "$retval$value";
353
354 # remove the space added in the beginning
355 $retval =~ s/ //;
356
357 return "$retval"
358}
359
a57419b3 360sub set_value {
3d1cc414 361 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
a57419b3
SR
362
363 if (defined($opt{$lvalue})) {
3d1cc414
SR
364 if (!$override || defined(${$overrides}{$lvalue})) {
365 my $extra = "";
366 if ($override) {
367 $extra = "In the same override section!\n";
368 }
369 die "$name: $.: Option $lvalue defined more than once!\n$extra";
370 }
371 ${$overrides}{$lvalue} = $rvalue;
a57419b3 372 }
21a9679f
SR
373 if ($rvalue =~ /^\s*$/) {
374 delete $opt{$lvalue};
375 } else {
77d942ce 376 $rvalue = process_variables($rvalue);
21a9679f
SR
377 $opt{$lvalue} = $rvalue;
378 }
a57419b3
SR
379}
380
77d942ce
SR
381sub set_variable {
382 my ($lvalue, $rvalue) = @_;
383
384 if ($rvalue =~ /^\s*$/) {
385 delete $variable{$lvalue};
386 } else {
387 $rvalue = process_variables($rvalue);
388 $variable{$lvalue} = $rvalue;
389 }
390}
391
ab7a3f52
SR
392sub process_compare {
393 my ($lval, $cmp, $rval) = @_;
394
395 # remove whitespace
396
397 $lval =~ s/^\s*//;
398 $lval =~ s/\s*$//;
399
400 $rval =~ s/^\s*//;
401 $rval =~ s/\s*$//;
402
403 if ($cmp eq "==") {
404 return $lval eq $rval;
405 } elsif ($cmp eq "!=") {
406 return $lval ne $rval;
407 }
408
409 my $statement = "$lval $cmp $rval";
410 my $ret = eval $statement;
411
412 # $@ stores error of eval
413 if ($@) {
414 return -1;
415 }
416
417 return $ret;
418}
419
9900b5dc
SR
420sub value_defined {
421 my ($val) = @_;
422
423 return defined($variable{$2}) ||
424 defined($opt{$2});
425}
426
8d735212
SR
427my $d = 0;
428sub process_expression {
429 my ($name, $val) = @_;
430
431 my $c = $d++;
432
433 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
434 my $express = $1;
435
436 if (process_expression($name, $express)) {
437 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
438 } else {
439 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
440 }
441 }
442
443 $d--;
444 my $OR = "\\|\\|";
445 my $AND = "\\&\\&";
45d73a5d 446
8d735212
SR
447 while ($val =~ s/^(.*?)($OR|$AND)//) {
448 my $express = $1;
449 my $op = $2;
450
451 if (process_expression($name, $express)) {
452 if ($op eq "||") {
453 return 1;
454 }
455 } else {
456 if ($op eq "&&") {
457 return 0;
458 }
459 }
460 }
45d73a5d 461
ab7a3f52
SR
462 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
463 my $ret = process_compare($1, $2, $3);
464 if ($ret < 0) {
465 die "$name: $.: Unable to process comparison\n";
466 }
467 return $ret;
468 }
469
9900b5dc
SR
470 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
471 if (defined $1) {
472 return !value_defined($2);
473 } else {
474 return value_defined($2);
475 }
476 }
477
45d73a5d
SR
478 if ($val =~ /^\s*0\s*$/) {
479 return 0;
480 } elsif ($val =~ /^\s*\d+\s*$/) {
481 return 1;
482 }
483
9900b5dc 484 die ("$name: $.: Undefined content $val in if statement\n");
8d735212
SR
485}
486
487sub process_if {
488 my ($name, $value) = @_;
489
490 # Convert variables and replace undefined ones with 0
491 my $val = process_variables($value, 1);
492 my $ret = process_expression $name, $val;
493
494 return $ret;
45d73a5d
SR
495}
496
2ed3b161
SR
497sub __read_config {
498 my ($config, $current_test_num) = @_;
2545eb61 499
2ed3b161
SR
500 my $in;
501 open($in, $config) || die "can't read file $config";
2545eb61 502
a57419b3
SR
503 my $name = $config;
504 $name =~ s,.*/(.*),$1,;
505
2ed3b161 506 my $test_num = $$current_test_num;
a57419b3
SR
507 my $default = 1;
508 my $repeat = 1;
509 my $num_tests_set = 0;
510 my $skip = 0;
511 my $rest;
a9f84424 512 my $line;
0df213ca 513 my $test_case = 0;
45d73a5d
SR
514 my $if = 0;
515 my $if_set = 0;
3d1cc414
SR
516 my $override = 0;
517
518 my %overrides;
a57419b3 519
2ed3b161 520 while (<$in>) {
2545eb61
SR
521
522 # ignore blank lines and comments
523 next if (/^\s*$/ || /\s*\#/);
524
0050b6bb 525 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
a57419b3 526
0050b6bb
SR
527 my $type = $1;
528 $rest = $2;
a9f84424 529 $line = $2;
a57419b3 530
0050b6bb
SR
531 my $old_test_num;
532 my $old_repeat;
3d1cc414 533 $override = 0;
0050b6bb
SR
534
535 if ($type eq "TEST_START") {
536
537 if ($num_tests_set) {
538 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
539 }
a57419b3 540
0050b6bb
SR
541 $old_test_num = $test_num;
542 $old_repeat = $repeat;
a57419b3 543
0050b6bb
SR
544 $test_num += $repeat;
545 $default = 0;
546 $repeat = 1;
547 } else {
548 $default = 1;
549 }
a57419b3 550
a9f84424
SR
551 # If SKIP is anywhere in the line, the command will be skipped
552 if ($rest =~ s/\s+SKIP\b//) {
a57419b3
SR
553 $skip = 1;
554 } else {
0df213ca 555 $test_case = 1;
a57419b3
SR
556 $skip = 0;
557 }
558
a9f84424
SR
559 if ($rest =~ s/\sELSE\b//) {
560 if (!$if) {
561 die "$name: $.: ELSE found with out matching IF section\n$_";
562 }
563 $if = 0;
564
565 if ($if_set) {
566 $skip = 1;
567 } else {
568 $skip = 0;
3d1cc414 569 }
a57419b3
SR
570 }
571
a9f84424 572 if ($rest =~ s/\sIF\s+(.*)//) {
45d73a5d
SR
573 if (process_if($name, $1)) {
574 $if_set = 1;
575 } else {
576 $skip = 1;
577 }
578 $if = 1;
579 } else {
580 $if = 0;
a9f84424
SR
581 $if_set = 0;
582 }
583
584 if (!$skip) {
585 if ($type eq "TEST_START") {
586 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
587 $repeat = $1;
588 $repeat_tests{"$test_num"} = $repeat;
589 }
590 } elsif ($rest =~ s/\sOVERRIDE\b//) {
591 # DEFAULT only
592 $override = 1;
593 # Clear previous overrides
594 %overrides = ();
595 }
a57419b3
SR
596 }
597
a9f84424 598 if (!$skip && $rest !~ /^\s*$/) {
0050b6bb 599 die "$name: $.: Gargbage found after $type\n$_";
a57419b3
SR
600 }
601
0050b6bb 602 if ($skip && $type eq "TEST_START") {
a57419b3 603 $test_num = $old_test_num;
e48c5293 604 $repeat = $old_repeat;
a57419b3
SR
605 }
606
ab7a3f52 607 } elsif (/^\s*ELSE\b(.*)$/) {
45d73a5d
SR
608 if (!$if) {
609 die "$name: $.: ELSE found with out matching IF section\n$_";
610 }
611 $rest = $1;
612 if ($if_set) {
613 $skip = 1;
ab7a3f52 614 $rest = "";
45d73a5d
SR
615 } else {
616 $skip = 0;
617
ab7a3f52 618 if ($rest =~ /\sIF\s+(.*)/) {
45d73a5d
SR
619 # May be a ELSE IF section.
620 if (!process_if($name, $1)) {
621 $skip = 1;
622 }
ab7a3f52 623 $rest = "";
45d73a5d
SR
624 } else {
625 $if = 0;
626 }
627 }
628
ab7a3f52
SR
629 if ($rest !~ /^\s*$/) {
630 die "$name: $.: Gargbage found after DEFAULTS\n$_";
631 }
632
2ed3b161
SR
633 } elsif (/^\s*INCLUDE\s+(\S+)/) {
634
635 next if ($skip);
636
637 if (!$default) {
638 die "$name: $.: INCLUDE can only be done in default sections\n$_";
639 }
640
641 my $file = process_variables($1);
642
643 if ($file !~ m,^/,) {
644 # check the path of the config file first
645 if ($config =~ m,(.*)/,) {
646 if (-f "$1/$file") {
647 $file = "$1/$file";
648 }
649 }
650 }
651
652 if ( ! -r $file ) {
653 die "$name: $.: Can't read file $file\n$_";
654 }
655
656 if (__read_config($file, \$test_num)) {
657 $test_case = 1;
658 }
659
a57419b3
SR
660 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
661
662 next if ($skip);
663
2545eb61
SR
664 my $lvalue = $1;
665 my $rvalue = $2;
666
a57419b3
SR
667 if (!$default &&
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";
672 }
673
674 if ($lvalue eq "NUM_TESTS") {
675 if ($test_num) {
676 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
677 }
678 if (!$default) {
679 die "$name: $.: NUM_TESTS must be set in default section\n";
680 }
681 $num_tests_set = 1;
682 }
683
684 if ($default || $lvalue =~ /\[\d+\]$/) {
3d1cc414 685 set_value($lvalue, $rvalue, $override, \%overrides, $name);
a57419b3
SR
686 } else {
687 my $val = "$lvalue\[$test_num\]";
3d1cc414 688 set_value($val, $rvalue, $override, \%overrides, $name);
a57419b3
SR
689
690 if ($repeat > 1) {
691 $repeats{$val} = $repeat;
692 }
a75fecec 693 }
77d942ce
SR
694 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
695 next if ($skip);
696
697 my $lvalue = $1;
698 my $rvalue = $2;
699
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
707 # the prevous one.
708 set_variable($lvalue, $rvalue);
709
a57419b3
SR
710 } else {
711 die "$name: $.: Garbage found in config\n$_";
2545eb61
SR
712 }
713 }
714
a57419b3
SR
715 if ($test_num) {
716 $test_num += $repeat - 1;
717 $opt{"NUM_TESTS"} = $test_num;
718 }
719
2ed3b161
SR
720 close($in);
721
722 $$current_test_num = $test_num;
723
724 return $test_case;
725}
726
727sub read_config {
728 my ($config) = @_;
729
730 my $test_case;
731 my $test_num = 0;
732
733 $test_case = __read_config $config, \$test_num;
734
8d1491ba
SR
735 # make sure we have all mandatory configs
736 get_ktest_configs;
737
0df213ca
SR
738 # was a test specified?
739 if (!$test_case) {
740 print "No test case specified.\n";
741 print "What test case would you like to run?\n";
742 my $ans = <STDIN>;
743 chomp $ans;
744 $default{"TEST_TYPE"} = $ans;
745 }
746
a75fecec
SR
747 # set any defaults
748
749 foreach my $default (keys %default) {
750 if (!defined($opt{$default})) {
751 $opt{$default} = $default{$default};
752 }
753 }
2545eb61
SR
754}
755
23715c3c
SR
756sub __eval_option {
757 my ($option, $i) = @_;
758
759 # Add space to evaluate the character before $
760 $option = " $option";
761 my $retval = "";
f9dfb65b
RV
762 my $repeated = 0;
763 my $parent = 0;
764
765 foreach my $test (keys %repeat_tests) {
766 if ($i >= $test &&
767 $i < $test + $repeat_tests{$test}) {
768
769 $repeated = 1;
770 $parent = $test;
771 last;
772 }
773 }
23715c3c
SR
774
775 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
776 my $start = $1;
777 my $var = $2;
778 my $end = $3;
779
780 # Append beginning of line
781 $retval = "$retval$start";
782
783 # If the iteration option OPT[$i] exists, then use that.
784 # otherwise see if the default OPT (without [$i]) exists.
785
786 my $o = "$var\[$i\]";
f9dfb65b 787 my $parento = "$var\[$parent\]";
23715c3c
SR
788
789 if (defined($opt{$o})) {
790 $o = $opt{$o};
791 $retval = "$retval$o";
f9dfb65b
RV
792 } elsif ($repeated && defined($opt{$parento})) {
793 $o = $opt{$parento};
794 $retval = "$retval$o";
23715c3c
SR
795 } elsif (defined($opt{$var})) {
796 $o = $opt{$var};
797 $retval = "$retval$o";
798 } else {
799 $retval = "$retval\$\{$var\}";
800 }
801
802 $option = $end;
803 }
804
805 $retval = "$retval$option";
806
807 $retval =~ s/^ //;
808
809 return $retval;
810}
811
812sub eval_option {
813 my ($option, $i) = @_;
814
815 my $prev = "";
816
817 # Since an option can evaluate to another option,
818 # keep iterating until we do not evaluate any more
819 # options.
820 my $r = 0;
821 while ($prev ne $option) {
822 # Check for recursive evaluations.
823 # 100 deep should be more than enough.
824 if ($r++ > 100) {
825 die "Over 100 evaluations accurred with $option\n" .
826 "Check for recursive variables\n";
827 }
828 $prev = $option;
829 $option = __eval_option($option, $i);
830 }
831
832 return $option;
833}
834
d1e2f22a 835sub _logit {
2545eb61
SR
836 if (defined($opt{"LOG_FILE"})) {
837 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
838 print OUT @_;
839 close(OUT);
840 }
841}
842
d1e2f22a
SR
843sub logit {
844 if (defined($opt{"LOG_FILE"})) {
845 _logit @_;
846 } else {
847 print @_;
848 }
849}
850
5f9b6ced
SR
851sub doprint {
852 print @_;
d1e2f22a 853 _logit @_;
5f9b6ced
SR
854}
855
7faafbd6 856sub run_command;
2728be41
AJ
857sub start_monitor;
858sub end_monitor;
859sub wait_for_monitor;
7faafbd6
SR
860
861sub reboot {
2728be41
AJ
862 my ($time) = @_;
863
2b803365
SR
864 if (defined($time)) {
865 start_monitor;
866 # flush out current monitor
867 # May contain the reboot success line
868 wait_for_monitor 1;
869 }
870
7faafbd6 871 # try to reboot normally
e48c5293 872 if (run_command $reboot) {
576f627c
SR
873 if (defined($powercycle_after_reboot)) {
874 sleep $powercycle_after_reboot;
875 run_command "$power_cycle";
876 }
877 } else {
7faafbd6 878 # nope? power cycle it.
a75fecec 879 run_command "$power_cycle";
7faafbd6 880 }
2728be41
AJ
881
882 if (defined($time)) {
2b803365 883 wait_for_monitor($time, $reboot_success_line);
2728be41
AJ
884 end_monitor;
885 }
7faafbd6
SR
886}
887
576f627c
SR
888sub do_not_reboot {
889 my $i = $iteration;
890
4ab1cce5 891 return $test_type eq "build" || $no_reboot ||
576f627c
SR
892 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
893 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
894}
895
5c42fc5b 896sub dodie {
5a391fbf 897 doprint "CRITICAL FAILURE... ", @_, "\n";
5c42fc5b 898
576f627c
SR
899 my $i = $iteration;
900
901 if ($reboot_on_error && !do_not_reboot) {
902
75c3fda7 903 doprint "REBOOTING\n";
7faafbd6 904 reboot;
75c3fda7 905
a75fecec 906 } elsif ($poweroff_on_error && defined($power_off)) {
5c42fc5b 907 doprint "POWERING OFF\n";
a75fecec 908 `$power_off`;
5c42fc5b 909 }
75c3fda7 910
f80802cb
SR
911 if (defined($opt{"LOG_FILE"})) {
912 print " See $opt{LOG_FILE} for more info.\n";
913 }
914
576f627c 915 die @_, "\n";
5c42fc5b
SR
916}
917
7faafbd6
SR
918sub open_console {
919 my ($fp) = @_;
920
921 my $flags;
922
a75fecec
SR
923 my $pid = open($fp, "$console|") or
924 dodie "Can't open console $console";
7faafbd6
SR
925
926 $flags = fcntl($fp, F_GETFL, 0) or
576f627c 927 dodie "Can't get flags for the socket: $!";
7faafbd6 928 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
576f627c 929 dodie "Can't set flags for the socket: $!";
7faafbd6
SR
930
931 return $pid;
932}
933
934sub close_console {
935 my ($fp, $pid) = @_;
936
937 doprint "kill child process $pid\n";
938 kill 2, $pid;
939
940 print "closing!\n";
941 close($fp);
942}
943
944sub start_monitor {
945 if ($monitor_cnt++) {
946 return;
947 }
948 $monitor_fp = \*MONFD;
949 $monitor_pid = open_console $monitor_fp;
a75fecec
SR
950
951 return;
952
953 open(MONFD, "Stop perl from warning about single use of MONFD");
7faafbd6
SR
954}
955
956sub end_monitor {
957 if (--$monitor_cnt) {
958 return;
959 }
960 close_console($monitor_fp, $monitor_pid);
961}
962
963sub wait_for_monitor {
2b803365
SR
964 my ($time, $stop) = @_;
965 my $full_line = "";
7faafbd6 966 my $line;
2b803365 967 my $booted = 0;
7faafbd6 968
a75fecec 969 doprint "** Wait for monitor to settle down **\n";
7faafbd6
SR
970
971 # read the monitor and wait for the system to calm down
2b803365 972 while (!$booted) {
7faafbd6 973 $line = wait_for_input($monitor_fp, $time);
2b803365
SR
974 last if (!defined($line));
975 print "$line";
976 $full_line .= $line;
977
978 if (defined($stop) && $full_line =~ /$stop/) {
979 doprint "wait for monitor detected $stop\n";
980 $booted = 1;
981 }
982
983 if ($line =~ /\n/) {
984 $full_line = "";
985 }
986 }
a75fecec 987 print "** Monitor flushed **\n";
7faafbd6
SR
988}
989
de5b6e3b
RV
990sub save_logs {
991 my ($result, $basedir) = @_;
992 my @t = localtime;
993 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
994 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
995
996 my $type = $build_type;
997 if ($type =~ /useconfig/) {
998 $type = "useconfig";
999 }
1000
1001 my $dir = "$machine-$test_type-$type-$result-$date";
1002
1003 $dir = "$basedir/$dir";
1004
1005 if (!-d $dir) {
1006 mkpath($dir) or
1007 die "can't create $dir";
1008 }
1009
1010 my %files = (
1011 "config" => $output_config,
1012 "buildlog" => $buildlog,
1013 "dmesg" => $dmesg,
1014 "testlog" => $testlog,
1015 );
1016
1017 while (my ($name, $source) = each(%files)) {
1018 if (-f "$source") {
1019 cp "$source", "$dir/$name" or
1020 die "failed to copy $source";
1021 }
1022 }
1023
1024 doprint "*** Saved info to $dir ***\n";
1025}
1026
2b7d9b21
SR
1027sub fail {
1028
a75fecec 1029 if ($die_on_failure) {
2b7d9b21
SR
1030 dodie @_;
1031 }
1032
a75fecec 1033 doprint "FAILED\n";
7faafbd6 1034
576f627c
SR
1035 my $i = $iteration;
1036
a75fecec 1037 # no need to reboot for just building.
576f627c 1038 if (!do_not_reboot) {
a75fecec 1039 doprint "REBOOTING\n";
2728be41 1040 reboot $sleep_time;
a75fecec 1041 }
7faafbd6 1042
9064af52
SR
1043 my $name = "";
1044
1045 if (defined($test_name)) {
1046 $name = " ($test_name)";
1047 }
1048
576f627c
SR
1049 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1050 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
9064af52 1051 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
576f627c
SR
1052 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1053 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
a75fecec 1054
de5b6e3b
RV
1055 if (defined($store_failures)) {
1056 save_logs "fail", $store_failures;
1057 }
7faafbd6 1058
2b7d9b21
SR
1059 return 1;
1060}
1061
2545eb61
SR
1062sub run_command {
1063 my ($command) = @_;
d6ce2a0b
SR
1064 my $dolog = 0;
1065 my $dord = 0;
1066 my $pid;
1067
e48c5293
SR
1068 $command =~ s/\$SSH_USER/$ssh_user/g;
1069 $command =~ s/\$MACHINE/$machine/g;
1070
d6ce2a0b
SR
1071 doprint("$command ... ");
1072
1073 $pid = open(CMD, "$command 2>&1 |") or
2b7d9b21 1074 (fail "unable to exec $command" and return 0);
2545eb61
SR
1075
1076 if (defined($opt{"LOG_FILE"})) {
d6ce2a0b
SR
1077 open(LOG, ">>$opt{LOG_FILE}") or
1078 dodie "failed to write to log";
1079 $dolog = 1;
6c5ee0be
SR
1080 }
1081
1082 if (defined($redirect)) {
d6ce2a0b
SR
1083 open (RD, ">$redirect") or
1084 dodie "failed to write to redirect $redirect";
1085 $dord = 1;
2545eb61
SR
1086 }
1087
d6ce2a0b
SR
1088 while (<CMD>) {
1089 print LOG if ($dolog);
1090 print RD if ($dord);
1091 }
2545eb61 1092
d6ce2a0b 1093 waitpid($pid, 0);
2545eb61
SR
1094 my $failed = $?;
1095
d6ce2a0b
SR
1096 close(CMD);
1097 close(LOG) if ($dolog);
1098 close(RD) if ($dord);
1099
2545eb61
SR
1100 if ($failed) {
1101 doprint "FAILED!\n";
1102 } else {
1103 doprint "SUCCESS\n";
1104 }
1105
5f9b6ced
SR
1106 return !$failed;
1107}
1108
e48c5293
SR
1109sub run_ssh {
1110 my ($cmd) = @_;
1111 my $cp_exec = $ssh_exec;
1112
1113 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1114 return run_command "$cp_exec";
1115}
1116
1117sub run_scp {
1118 my ($src, $dst) = @_;
1119 my $cp_scp = $scp_to_target;
1120
1121 $cp_scp =~ s/\$SRC_FILE/$src/g;
1122 $cp_scp =~ s/\$DST_FILE/$dst/g;
1123
1124 return run_command "$cp_scp";
1125}
1126
5f9b6ced
SR
1127sub get_grub_index {
1128
a75fecec
SR
1129 if ($reboot_type ne "grub") {
1130 return;
1131 }
5a391fbf 1132 return if (defined($grub_number));
5f9b6ced
SR
1133
1134 doprint "Find grub menu ... ";
1135 $grub_number = -1;
e48c5293
SR
1136
1137 my $ssh_grub = $ssh_exec;
1138 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1139
1140 open(IN, "$ssh_grub |")
5f9b6ced 1141 or die "unable to get menu.lst";
e48c5293 1142
eaa1fe25
SR
1143 my $found = 0;
1144
5f9b6ced 1145 while (<IN>) {
a75fecec 1146 if (/^\s*title\s+$grub_menu\s*$/) {
5f9b6ced 1147 $grub_number++;
eaa1fe25 1148 $found = 1;
5f9b6ced
SR
1149 last;
1150 } elsif (/^\s*title\s/) {
1151 $grub_number++;
1152 }
1153 }
1154 close(IN);
1155
a75fecec 1156 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
eaa1fe25 1157 if (!$found);
5f9b6ced 1158 doprint "$grub_number\n";
2545eb61
SR
1159}
1160
2545eb61
SR
1161sub wait_for_input
1162{
1163 my ($fp, $time) = @_;
1164 my $rin;
1165 my $ready;
1166 my $line;
1167 my $ch;
1168
1169 if (!defined($time)) {
1170 $time = $timeout;
1171 }
1172
1173 $rin = '';
1174 vec($rin, fileno($fp), 1) = 1;
1175 $ready = select($rin, undef, undef, $time);
1176
1177 $line = "";
1178
1179 # try to read one char at a time
1180 while (sysread $fp, $ch, 1) {
1181 $line .= $ch;
1182 last if ($ch eq "\n");
1183 }
1184
1185 if (!length($line)) {
1186 return undef;
1187 }
1188
1189 return $line;
1190}
1191
75c3fda7 1192sub reboot_to {
a75fecec 1193 if ($reboot_type eq "grub") {
c54367f9
SR
1194 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1195 reboot;
a75fecec
SR
1196 return;
1197 }
1198
1199 run_command "$reboot_script";
2545eb61
SR
1200}
1201
a57419b3
SR
1202sub get_sha1 {
1203 my ($commit) = @_;
1204
1205 doprint "git rev-list --max-count=1 $commit ... ";
1206 my $sha1 = `git rev-list --max-count=1 $commit`;
1207 my $ret = $?;
1208
1209 logit $sha1;
1210
1211 if ($ret) {
1212 doprint "FAILED\n";
1213 dodie "Failed to get git $commit";
1214 }
1215
1216 print "SUCCESS\n";
1217
1218 chomp $sha1;
1219
1220 return $sha1;
1221}
1222
5a391fbf 1223sub monitor {
2545eb61
SR
1224 my $booted = 0;
1225 my $bug = 0;
5c42fc5b 1226 my $skip_call_trace = 0;
2b7d9b21 1227 my $loops;
2545eb61 1228
7faafbd6 1229 wait_for_monitor 5;
2545eb61
SR
1230
1231 my $line;
1232 my $full_line = "";
1233
7faafbd6
SR
1234 open(DMESG, "> $dmesg") or
1235 die "unable to write to $dmesg";
2545eb61 1236
75c3fda7 1237 reboot_to;
2545eb61 1238
1c8a617a
SR
1239 my $success_start;
1240 my $failure_start;
2d01b26a
SR
1241 my $monitor_start = time;
1242 my $done = 0;
f1a5b962 1243 my $version_found = 0;
1c8a617a 1244
2d01b26a 1245 while (!$done) {
2545eb61 1246
ecaf8e52
SR
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";
1254 last;
1255 }
1256 } elsif ($booted) {
a75fecec 1257 $line = wait_for_input($monitor_fp, $booted_timeout);
cd4f1d53
SR
1258 if (!defined($line)) {
1259 my $s = $booted_timeout == 1 ? "" : "s";
1260 doprint "Successful boot found: break after $booted_timeout second$s\n";
1261 last;
1262 }
2b7d9b21 1263 } else {
7faafbd6 1264 $line = wait_for_input($monitor_fp);
cd4f1d53
SR
1265 if (!defined($line)) {
1266 my $s = $timeout == 1 ? "" : "s";
1267 doprint "Timed out after $timeout second$s\n";
1268 last;
1269 }
2b7d9b21 1270 }
2545eb61 1271
2545eb61 1272 doprint $line;
7faafbd6 1273 print DMESG $line;
2545eb61
SR
1274
1275 # we are not guaranteed to get a full line
1276 $full_line .= $line;
1277
a75fecec 1278 if ($full_line =~ /$success_line/) {
2545eb61 1279 $booted = 1;
1c8a617a
SR
1280 $success_start = time;
1281 }
1282
1283 if ($booted && defined($stop_after_success) &&
1284 $stop_after_success >= 0) {
1285 my $now = time;
1286 if ($now - $success_start >= $stop_after_success) {
1287 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1288 last;
1289 }
2545eb61
SR
1290 }
1291
5c42fc5b
SR
1292 if ($full_line =~ /\[ backtrace testing \]/) {
1293 $skip_call_trace = 1;
1294 }
1295
2545eb61 1296 if ($full_line =~ /call trace:/i) {
4651920e 1297 if (!$bug && !$skip_call_trace) {
1c8a617a
SR
1298 $bug = 1;
1299 $failure_start = time;
1300 }
1301 }
1302
1303 if ($bug && defined($stop_after_failure) &&
1304 $stop_after_failure >= 0) {
1305 my $now = time;
1306 if ($now - $failure_start >= $stop_after_failure) {
1307 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1308 last;
1309 }
5c42fc5b
SR
1310 }
1311
1312 if ($full_line =~ /\[ end of backtrace testing \]/) {
1313 $skip_call_trace = 0;
1314 }
1315
1316 if ($full_line =~ /Kernel panic -/) {
10abf118 1317 $failure_start = time;
2545eb61
SR
1318 $bug = 1;
1319 }
1320
f1a5b962
SR
1321 # Detect triple faults by testing the banner
1322 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1323 if ($1 eq $version) {
1324 $version_found = 1;
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";
1333 last;
1334 }
1335 }
1336
2545eb61
SR
1337 if ($line =~ /\n/) {
1338 $full_line = "";
1339 }
2d01b26a
SR
1340
1341 if ($stop_test_after > 0 && !$booted && !$bug) {
1342 if (time - $monitor_start > $stop_test_after) {
4d62bf51 1343 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
2d01b26a
SR
1344 $done = 1;
1345 }
1346 }
2545eb61
SR
1347 }
1348
7faafbd6 1349 close(DMESG);
2545eb61 1350
a75fecec 1351 if ($bug) {
2b7d9b21 1352 return 0 if ($in_bisect);
576f627c 1353 fail "failed - got a bug report" and return 0;
2545eb61
SR
1354 }
1355
a75fecec 1356 if (!$booted) {
2b7d9b21 1357 return 0 if ($in_bisect);
576f627c 1358 fail "failed - never got a boot prompt." and return 0;
2545eb61 1359 }
5f9b6ced 1360
2b7d9b21 1361 return 1;
2545eb61
SR
1362}
1363
db05cfef
SR
1364sub do_post_install {
1365
1366 return if (!defined($post_install));
1367
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";
1372}
1373
2545eb61
SR
1374sub install {
1375
e0a8742e
SR
1376 return if ($no_install);
1377
e48c5293 1378 run_scp "$outputdir/$build_target", "$target_image" or
5c42fc5b 1379 dodie "failed to copy image";
2545eb61 1380
5f9b6ced 1381 my $install_mods = 0;
2545eb61 1382
5f9b6ced
SR
1383 # should we process modules?
1384 $install_mods = 0;
51ad1dd1 1385 open(IN, "$output_config") or dodie("Can't read config file");
5f9b6ced
SR
1386 while (<IN>) {
1387 if (/CONFIG_MODULES(=y)?/) {
1388 $install_mods = 1 if (defined($1));
1389 last;
5c42fc5b 1390 }
5f9b6ced
SR
1391 }
1392 close(IN);
5c42fc5b 1393
5f9b6ced 1394 if (!$install_mods) {
db05cfef 1395 do_post_install;
5f9b6ced
SR
1396 doprint "No modules needed\n";
1397 return;
1398 }
2545eb61 1399
a75fecec 1400 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
5f9b6ced 1401 dodie "Failed to install modules";
5c42fc5b 1402
5f9b6ced 1403 my $modlib = "/lib/modules/$version";
a57419b3 1404 my $modtar = "ktest-mods.tar.bz2";
5c42fc5b 1405
e48c5293 1406 run_ssh "rm -rf $modlib" or
5f9b6ced 1407 dodie "failed to remove old mods: $modlib";
5c42fc5b 1408
5f9b6ced 1409 # would be nice if scp -r did not follow symbolic links
a75fecec 1410 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
5f9b6ced
SR
1411 dodie "making tarball";
1412
e48c5293 1413 run_scp "$tmpdir/$modtar", "/tmp" or
5f9b6ced
SR
1414 dodie "failed to copy modules";
1415
a75fecec 1416 unlink "$tmpdir/$modtar";
5f9b6ced 1417
e7b13441 1418 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
5f9b6ced 1419 dodie "failed to tar modules";
2545eb61 1420
e48c5293 1421 run_ssh "rm -f /tmp/$modtar";
8b37ca8c 1422
db05cfef 1423 do_post_install;
2545eb61
SR
1424}
1425
ddf607e5
SR
1426sub get_version {
1427 # get the release name
1428 doprint "$make kernelrelease ... ";
1429 $version = `$make kernelrelease | tail -1`;
1430 chomp($version);
1431 doprint "$version\n";
1432}
1433
1434sub start_monitor_and_boot {
9f7424cc
SR
1435 # Make sure the stable kernel has finished booting
1436 start_monitor;
1437 wait_for_monitor 5;
1438 end_monitor;
1439
ddf607e5
SR
1440 get_grub_index;
1441 get_version;
1442 install;
1443
1444 start_monitor;
1445 return monitor;
1446}
1447
6c5ee0be
SR
1448sub check_buildlog {
1449 my ($patch) = @_;
1450
6c5ee0be
SR
1451 my @files = `git show $patch | diffstat -l`;
1452
1453 open(IN, "git show $patch |") or
1454 dodie "failed to show $patch";
1455 while (<IN>) {
1456 if (m,^--- a/(.*),) {
1457 chomp $1;
1458 $files[$#files] = $1;
1459 }
1460 }
1461 close(IN);
1462
1463 open(IN, $buildlog) or dodie "Can't open $buildlog";
1464 while (<IN>) {
1465 if (/^\s*(.*?):.*(warning|error)/) {
1466 my $err = $1;
1467 foreach my $file (@files) {
a75fecec 1468 my $fullpath = "$builddir/$file";
6c5ee0be 1469 if ($file eq $err || $fullpath eq $err) {
2b7d9b21 1470 fail "$file built with warnings" and return 0;
6c5ee0be
SR
1471 }
1472 }
1473 }
1474 }
1475 close(IN);
2b7d9b21
SR
1476
1477 return 1;
6c5ee0be
SR
1478}
1479
fcb3f16a
SR
1480sub apply_min_config {
1481 my $outconfig = "$output_config.new";
1482
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.
1486
1487 doprint "Applying minimum configurations into $output_config.new\n";
1488
1489 open (OUT, ">$outconfig") or
1490 dodie "Can't create $outconfig";
1491
1492 if (-f $output_config) {
1493 open (IN, $output_config) or
1494 dodie "Failed to open $output_config";
1495 while (<IN>) {
1496 if (/^(# )?(CONFIG_[^\s=]*)/) {
1497 next if (defined($force_config{$2}));
1498 }
1499 print OUT;
1500 }
1501 close IN;
1502 }
1503 foreach my $config (keys %force_config) {
1504 print OUT "$force_config{$config}\n";
1505 }
1506 close OUT;
1507
1508 run_command "mv $outconfig $output_config";
1509}
1510
612b9e9b 1511sub make_oldconfig {
612b9e9b 1512
4c4ab120
SR
1513 my @force_list = keys %force_config;
1514
1515 if ($#force_list >= 0) {
1516 apply_min_config;
1517 }
fcb3f16a
SR
1518
1519 if (!run_command "$make oldnoconfig") {
612b9e9b
SR
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";
fcb3f16a 1523 run_command "yes '' | $make oldconfig" or
612b9e9b
SR
1524 dodie "failed make config oldconfig";
1525 }
1526}
1527
fcb3f16a
SR
1528# read a config file and use this to force new configs.
1529sub load_force_config {
1530 my ($config) = @_;
1531
1532 open(IN, $config) or
1533 dodie "failed to read $config";
1534 while (<IN>) {
1535 chomp;
1536 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1537 $force_config{$1} = $_;
1538 } elsif (/^# (CONFIG_\S*) is not set/) {
1539 $force_config{$1} = $_;
1540 }
1541 }
1542 close IN;
1543}
1544
2545eb61
SR
1545sub build {
1546 my ($type) = @_;
5c42fc5b 1547
7faafbd6
SR
1548 unlink $buildlog;
1549
4ab1cce5
SR
1550 # Failed builds should not reboot the target
1551 my $save_no_reboot = $no_reboot;
1552 $no_reboot = 1;
1553
0bd6c1a3
SR
1554 if (defined($pre_build)) {
1555 my $ret = run_command $pre_build;
1556 if (!$ret && defined($pre_build_die) &&
1557 $pre_build_die) {
1558 dodie "failed to pre_build\n";
1559 }
1560 }
1561
75c3fda7 1562 if ($type =~ /^useconfig:(.*)/) {
51ad1dd1 1563 run_command "cp $1 $output_config" or
75c3fda7 1564 dodie "could not copy $1 to .config";
5f9b6ced 1565
75c3fda7
SR
1566 $type = "oldconfig";
1567 }
1568
5c42fc5b
SR
1569 # old config can ask questions
1570 if ($type eq "oldconfig") {
9386c6ab 1571 $type = "oldnoconfig";
75c3fda7
SR
1572
1573 # allow for empty configs
51ad1dd1 1574 run_command "touch $output_config";
75c3fda7 1575
13488231
AJ
1576 if (!$noclean) {
1577 run_command "mv $output_config $outputdir/config_temp" or
1578 dodie "moving .config";
2545eb61 1579
13488231 1580 run_command "$make mrproper" or dodie "make mrproper";
2545eb61 1581
13488231
AJ
1582 run_command "mv $outputdir/config_temp $output_config" or
1583 dodie "moving config_temp";
1584 }
5c42fc5b
SR
1585
1586 } elsif (!$noclean) {
51ad1dd1 1587 unlink "$output_config";
5f9b6ced 1588 run_command "$make mrproper" or
5c42fc5b 1589 dodie "make mrproper";
5c42fc5b 1590 }
2545eb61
SR
1591
1592 # add something to distinguish this build
a75fecec
SR
1593 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1594 print OUT "$localversion\n";
2545eb61
SR
1595 close(OUT);
1596
5f9b6ced 1597 if (defined($minconfig)) {
fcb3f16a 1598 load_force_config($minconfig);
2545eb61
SR
1599 }
1600
fcb3f16a
SR
1601 if ($type ne "oldnoconfig") {
1602 run_command "$make $type" or
612b9e9b
SR
1603 dodie "failed make config";
1604 }
fcb3f16a
SR
1605 # Run old config regardless, to enforce min configurations
1606 make_oldconfig;
2545eb61 1607
a75fecec 1608 $redirect = "$buildlog";
0bd6c1a3
SR
1609 my $build_ret = run_command "$make $build_options";
1610 undef $redirect;
1611
1612 if (defined($post_build)) {
1613 my $ret = run_command $post_build;
1614 if (!$ret && defined($post_build_die) &&
1615 $post_build_die) {
1616 dodie "failed to post_build\n";
1617 }
1618 }
1619
1620 if (!$build_ret) {
5f9b6ced 1621 # bisect may need this to pass
4ab1cce5
SR
1622 if ($in_bisect) {
1623 $no_reboot = $save_no_reboot;
1624 return 0;
1625 }
2b7d9b21 1626 fail "failed build" and return 0;
2545eb61 1627 }
5f9b6ced 1628
4ab1cce5
SR
1629 $no_reboot = $save_no_reboot;
1630
2b7d9b21 1631 return 1;
2545eb61
SR
1632}
1633
75c3fda7 1634sub halt {
e48c5293 1635 if (!run_ssh "halt" or defined($power_off)) {
576f627c
SR
1636 if (defined($poweroff_after_halt)) {
1637 sleep $poweroff_after_halt;
1638 run_command "$power_off";
1639 }
1640 } else {
75c3fda7 1641 # nope? the zap it!
a75fecec 1642 run_command "$power_off";
75c3fda7
SR
1643 }
1644}
1645
5f9b6ced
SR
1646sub success {
1647 my ($i) = @_;
1648
e48c5293
SR
1649 $successes++;
1650
9064af52
SR
1651 my $name = "";
1652
1653 if (defined($test_name)) {
1654 $name = " ($test_name)";
1655 }
1656
5f9b6ced
SR
1657 doprint "\n\n*******************************************\n";
1658 doprint "*******************************************\n";
9064af52 1659 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
5f9b6ced
SR
1660 doprint "*******************************************\n";
1661 doprint "*******************************************\n";
1662
de5b6e3b
RV
1663 if (defined($store_successes)) {
1664 save_logs "success", $store_successes;
1665 }
1666
576f627c 1667 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
a75fecec 1668 doprint "Reboot and wait $sleep_time seconds\n";
2728be41 1669 reboot $sleep_time;
5f9b6ced
SR
1670 }
1671}
1672
c960bb9f
SR
1673sub answer_bisect {
1674 for (;;) {
1675 doprint "Pass or fail? [p/f]";
1676 my $ans = <STDIN>;
1677 chomp $ans;
1678 if ($ans eq "p" || $ans eq "P") {
1679 return 1;
1680 } elsif ($ans eq "f" || $ans eq "F") {
1681 return 0;
1682 } else {
1683 print "Please answer 'P' or 'F'\n";
1684 }
1685 }
1686}
1687
5a391fbf 1688sub child_run_test {
7faafbd6 1689 my $failed = 0;
5a391fbf 1690
7faafbd6 1691 # child should have no power
a75fecec
SR
1692 $reboot_on_error = 0;
1693 $poweroff_on_error = 0;
1694 $die_on_failure = 1;
7faafbd6 1695
a9dd5d63 1696 $redirect = "$testlog";
7faafbd6 1697 run_command $run_test or $failed = 1;
a9dd5d63
RV
1698 undef $redirect;
1699
5a391fbf
SR
1700 exit $failed;
1701}
1702
1703my $child_done;
1704
1705sub child_finished {
1706 $child_done = 1;
1707}
1708
1709sub do_run_test {
1710 my $child_pid;
1711 my $child_exit;
5a391fbf
SR
1712 my $line;
1713 my $full_line;
1714 my $bug = 0;
5a391fbf 1715
7faafbd6 1716 wait_for_monitor 1;
5a391fbf 1717
7faafbd6 1718 doprint "run test $run_test\n";
5a391fbf
SR
1719
1720 $child_done = 0;
1721
1722 $SIG{CHLD} = qw(child_finished);
1723
1724 $child_pid = fork;
1725
1726 child_run_test if (!$child_pid);
1727
1728 $full_line = "";
1729
1730 do {
7faafbd6 1731 $line = wait_for_input($monitor_fp, 1);
5a391fbf
SR
1732 if (defined($line)) {
1733
1734 # we are not guaranteed to get a full line
1735 $full_line .= $line;
8ea0e063 1736 doprint $line;
5a391fbf
SR
1737
1738 if ($full_line =~ /call trace:/i) {
1739 $bug = 1;
1740 }
1741
1742 if ($full_line =~ /Kernel panic -/) {
1743 $bug = 1;
1744 }
1745
1746 if ($line =~ /\n/) {
1747 $full_line = "";
1748 }
1749 }
1750 } while (!$child_done && !$bug);
1751
1752 if ($bug) {
8ea0e063
SR
1753 my $failure_start = time;
1754 my $now;
1755 do {
1756 $line = wait_for_input($monitor_fp, 1);
1757 if (defined($line)) {
1758 doprint $line;
1759 }
1760 $now = time;
1761 if ($now - $failure_start >= $stop_after_failure) {
1762 last;
1763 }
1764 } while (defined($line));
1765
5a391fbf
SR
1766 doprint "Detected kernel crash!\n";
1767 # kill the child with extreme prejudice
1768 kill 9, $child_pid;
1769 }
1770
1771 waitpid $child_pid, 0;
1772 $child_exit = $?;
1773
5a391fbf 1774 if ($bug || $child_exit) {
2b7d9b21
SR
1775 return 0 if $in_bisect;
1776 fail "test failed" and return 0;
5a391fbf 1777 }
2b7d9b21 1778 return 1;
5a391fbf
SR
1779}
1780
a75fecec
SR
1781sub run_git_bisect {
1782 my ($command) = @_;
1783
1784 doprint "$command ... ";
1785
1786 my $output = `$command 2>&1`;
1787 my $ret = $?;
1788
1789 logit $output;
1790
1791 if ($ret) {
1792 doprint "FAILED\n";
1793 dodie "Failed to git bisect";
1794 }
1795
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/) {
1800 $bisect_bad = $1;
1801 doprint "Found bad commit... $1\n";
1802 return 0;
1803 } else {
1804 # we already logged it, just print it now.
1805 print $output;
1806 }
1807
1808 return 1;
1809}
1810
c23dca7c
SR
1811sub bisect_reboot {
1812 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
2728be41 1813 reboot $bisect_sleep_time;
c23dca7c
SR
1814}
1815
1816# returns 1 on success, 0 on failure, -1 on skip
0a05c769
SR
1817sub run_bisect_test {
1818 my ($type, $buildtype) = @_;
5f9b6ced 1819
2b7d9b21 1820 my $failed = 0;
5f9b6ced
SR
1821 my $result;
1822 my $output;
1823 my $ret;
1824
0a05c769
SR
1825 $in_bisect = 1;
1826
1827 build $buildtype or $failed = 1;
5f9b6ced
SR
1828
1829 if ($type ne "build") {
c23dca7c
SR
1830 if ($failed && $bisect_skip) {
1831 $in_bisect = 0;
1832 return -1;
1833 }
7faafbd6 1834 dodie "Failed on build" if $failed;
5f9b6ced
SR
1835
1836 # Now boot the box
ddf607e5 1837 start_monitor_and_boot or $failed = 1;
5f9b6ced
SR
1838
1839 if ($type ne "boot") {
c23dca7c
SR
1840 if ($failed && $bisect_skip) {
1841 end_monitor;
1842 bisect_reboot;
1843 $in_bisect = 0;
1844 return -1;
1845 }
7faafbd6 1846 dodie "Failed on boot" if $failed;
5a391fbf 1847
2b7d9b21 1848 do_run_test or $failed = 1;
5f9b6ced 1849 }
7faafbd6 1850 end_monitor;
5f9b6ced
SR
1851 }
1852
1853 if ($failed) {
0a05c769 1854 $result = 0;
5f9b6ced 1855 } else {
0a05c769
SR
1856 $result = 1;
1857 }
4025bc62
SR
1858
1859 # reboot the box to a kernel we can ssh to
1860 if ($type ne "build") {
1861 bisect_reboot;
1862 }
0a05c769
SR
1863 $in_bisect = 0;
1864
1865 return $result;
1866}
1867
1868sub run_bisect {
1869 my ($type) = @_;
1870 my $buildtype = "oldconfig";
1871
1872 # We should have a minconfig to use?
1873 if (defined($minconfig)) {
1874 $buildtype = "useconfig:$minconfig";
5f9b6ced
SR
1875 }
1876
0a05c769
SR
1877 my $ret = run_bisect_test $type, $buildtype;
1878
c960bb9f
SR
1879 if ($bisect_manual) {
1880 $ret = answer_bisect;
1881 }
0a05c769 1882
d6ce2a0b
SR
1883 # Are we looking for where it worked, not failed?
1884 if ($reverse_bisect) {
0a05c769 1885 $ret = !$ret;
d6ce2a0b
SR
1886 }
1887
c23dca7c 1888 if ($ret > 0) {
0a05c769 1889 return "good";
c23dca7c 1890 } elsif ($ret == 0) {
0a05c769 1891 return "bad";
c23dca7c
SR
1892 } elsif ($bisect_skip) {
1893 doprint "HIT A BAD COMMIT ... SKIPPING\n";
1894 return "skip";
0a05c769 1895 }
5f9b6ced
SR
1896}
1897
1898sub bisect {
1899 my ($i) = @_;
1900
1901 my $result;
1902
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]"}));
1906
1907 my $good = $opt{"BISECT_GOOD[$i]"};
1908 my $bad = $opt{"BISECT_BAD[$i]"};
1909 my $type = $opt{"BISECT_TYPE[$i]"};
a75fecec
SR
1910 my $start = $opt{"BISECT_START[$i]"};
1911 my $replay = $opt{"BISECT_REPLAY[$i]"};
3410f6fd
SR
1912 my $start_files = $opt{"BISECT_FILES[$i]"};
1913
1914 if (defined($start_files)) {
1915 $start_files = " -- " . $start_files;
1916 } else {
1917 $start_files = "";
1918 }
5f9b6ced 1919
a57419b3
SR
1920 # convert to true sha1's
1921 $good = get_sha1($good);
1922 $bad = get_sha1($bad);
1923
d6ce2a0b
SR
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;
1928 } else {
1929 $reverse_bisect = 0;
1930 }
1931
a75fecec
SR
1932 # Can't have a test without having a test to run
1933 if ($type eq "test" && !defined($run_test)) {
1934 $type = "boot";
1935 }
1936
1937 my $check = $opt{"BISECT_CHECK[$i]"};
1938 if (defined($check) && $check ne "0") {
1939
1940 # get current HEAD
a57419b3 1941 my $head = get_sha1("HEAD");
a75fecec
SR
1942
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";
1947
1948 $result = run_bisect $type;
1949
1950 if ($result ne "bad") {
1951 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
1952 }
1953 }
1954
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";
1959
1960 $result = run_bisect $type;
1961
1962 if ($result ne "good") {
1963 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
1964 }
1965 }
1966
1967 # checkout where we started
1968 run_command "git checkout $head" or
1969 die "Failed to checkout $head";
1970 }
1971
3410f6fd 1972 run_command "git bisect start$start_files" or
a75fecec 1973 dodie "could not start bisect";
5f9b6ced
SR
1974
1975 run_command "git bisect good $good" or
a75fecec 1976 dodie "could not set bisect good to $good";
5f9b6ced 1977
a75fecec
SR
1978 run_git_bisect "git bisect bad $bad" or
1979 dodie "could not set bisect bad to $bad";
5f9b6ced 1980
a75fecec
SR
1981 if (defined($replay)) {
1982 run_command "git bisect replay $replay" or
1983 dodie "failed to run replay";
5a391fbf
SR
1984 }
1985
a75fecec
SR
1986 if (defined($start)) {
1987 run_command "git checkout $start" or
1988 dodie "failed to checkout $start";
1989 }
1990
1991 my $test;
5f9b6ced
SR
1992 do {
1993 $result = run_bisect $type;
a75fecec
SR
1994 $test = run_git_bisect "git bisect $result";
1995 } while ($test);
5f9b6ced
SR
1996
1997 run_command "git bisect log" or
1998 dodie "could not capture git bisect log";
1999
2000 run_command "git bisect reset" or
2001 dodie "could not reset git bisect";
2002
2003 doprint "Bad commit was [$bisect_bad]\n";
2004
0a05c769
SR
2005 success $i;
2006}
2007
2008my %config_ignore;
2009my %config_set;
2010
2011my %config_list;
2012my %null_config;
2013
2014my %dependency;
2015
4c4ab120
SR
2016sub assign_configs {
2017 my ($hash, $config) = @_;
0a05c769
SR
2018
2019 open (IN, $config)
2020 or dodie "Failed to read $config";
2021
2022 while (<IN>) {
9bf71749 2023 if (/^((CONFIG\S*)=.*)/) {
4c4ab120 2024 ${$hash}{$2} = $1;
0a05c769
SR
2025 }
2026 }
2027
2028 close(IN);
2029}
2030
4c4ab120
SR
2031sub process_config_ignore {
2032 my ($config) = @_;
2033
2034 assign_configs \%config_ignore, $config;
2035}
2036
0a05c769
SR
2037sub read_current_config {
2038 my ($config_ref) = @_;
2039
2040 %{$config_ref} = ();
2041 undef %{$config_ref};
2042
2043 my @key = keys %{$config_ref};
2044 if ($#key >= 0) {
2045 print "did not delete!\n";
2046 exit;
2047 }
2048 open (IN, "$output_config");
2049
2050 while (<IN>) {
2051 if (/^(CONFIG\S+)=(.*)/) {
2052 ${$config_ref}{$1} = $2;
2053 }
2054 }
2055 close(IN);
2056}
2057
2058sub get_dependencies {
2059 my ($config) = @_;
2060
2061 my $arr = $dependency{$config};
2062 if (!defined($arr)) {
2063 return ();
2064 }
2065
2066 my @deps = @{$arr};
2067
2068 foreach my $dep (@{$arr}) {
2069 print "ADD DEP $dep\n";
2070 @deps = (@deps, get_dependencies $dep);
2071 }
2072
2073 return @deps;
2074}
2075
2076sub create_config {
2077 my @configs = @_;
2078
2079 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2080
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";
2086 }
2087 }
2088
2089 foreach my $config (keys %config_ignore) {
2090 print OUT "$config_ignore{$config}\n";
2091 }
2092 close(OUT);
2093
2094# exit;
fcb3f16a 2095 make_oldconfig;
0a05c769
SR
2096}
2097
2098sub compare_configs {
2099 my (%a, %b) = @_;
2100
2101 foreach my $item (keys %a) {
2102 if (!defined($b{$item})) {
2103 print "diff $item\n";
2104 return 1;
2105 }
2106 delete $b{$item};
2107 }
2108
2109 my @keys = keys %b;
2110 if ($#keys) {
2111 print "diff2 $keys[0]\n";
2112 }
2113 return -1 if ($#keys >= 0);
2114
2115 return 0;
2116}
2117
2118sub run_config_bisect_test {
2119 my ($type) = @_;
2120
2121 return run_bisect_test $type, "oldconfig";
2122}
2123
2124sub process_passed {
2125 my (%configs) = @_;
2126
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};
2135 }
2136 }
f1a27850
SR
2137 doprint "config copied to $outputdir/config_good\n";
2138 run_command "cp -f $output_config $outputdir/config_good";
0a05c769
SR
2139}
2140
2141sub process_failed {
2142 my ($config) = @_;
2143
2144 doprint "\n\n***************************************\n";
2145 doprint "Found bad config: $config\n";
2146 doprint "***************************************\n\n";
2147}
2148
2149sub run_config_bisect {
2150
2151 my @start_list = keys %config_list;
2152
2153 if ($#start_list < 0) {
2154 doprint "No more configs to test!!!\n";
2155 return -1;
2156 }
2157
2158 doprint "***** RUN TEST ***\n";
2159 my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
2160 my $ret;
2161 my %current_config;
2162
2163 my $count = $#start_list + 1;
2164 doprint " $count configs to test\n";
2165
2166 my $half = int($#start_list / 2);
2167
2168 do {
2169 my @tophalf = @start_list[0 .. $half];
2170
2171 create_config @tophalf;
2172 read_current_config \%current_config;
2173
2174 $count = $#tophalf + 1;
2175 doprint "Testing $count configs\n";
2176 my $found = 0;
2177 # make sure we test something
2178 foreach my $config (@tophalf) {
2179 if (defined($current_config{$config})) {
2180 logit " $config\n";
2181 $found = 1;
2182 }
2183 }
2184 if (!$found) {
2185 # try the other half
2186 doprint "Top half produced no set configs, trying bottom half\n";
4c8cc55b 2187 @tophalf = @start_list[$half + 1 .. $#start_list];
0a05c769
SR
2188 create_config @tophalf;
2189 read_current_config \%current_config;
2190 foreach my $config (@tophalf) {
2191 if (defined($current_config{$config})) {
2192 logit " $config\n";
2193 $found = 1;
2194 }
2195 }
2196 if (!$found) {
2197 doprint "Failed: Can't make new config with current configs\n";
2198 foreach my $config (@start_list) {
2199 doprint " CONFIG: $config\n";
2200 }
2201 return -1;
2202 }
2203 $count = $#tophalf + 1;
2204 doprint "Testing $count configs\n";
2205 }
2206
2207 $ret = run_config_bisect_test $type;
c960bb9f
SR
2208 if ($bisect_manual) {
2209 $ret = answer_bisect;
2210 }
0a05c769
SR
2211 if ($ret) {
2212 process_passed %current_config;
2213 return 0;
2214 }
2215
2216 doprint "This config had a failure.\n";
2217 doprint "Removing these configs that were not set in this config:\n";
f1a27850
SR
2218 doprint "config copied to $outputdir/config_bad\n";
2219 run_command "cp -f $output_config $outputdir/config_bad";
0a05c769
SR
2220
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};
2226 }
2227 }
2228
2229 @start_list = @tophalf;
2230
2231 if ($#start_list == 0) {
2232 process_failed $start_list[0];
2233 return 1;
2234 }
2235
2236 # remove half the configs we are looking at and see if
2237 # they are good.
2238 $half = int($#start_list / 2);
4c8cc55b 2239 } while ($#start_list > 0);
0a05c769 2240
c960bb9f
SR
2241 # we found a single config, try it again unless we are running manually
2242
2243 if ($bisect_manual) {
2244 process_failed $start_list[0];
2245 return 1;
2246 }
2247
0a05c769
SR
2248 my @tophalf = @start_list[0 .. 0];
2249
2250 $ret = run_config_bisect_test $type;
2251 if ($ret) {
2252 process_passed %current_config;
2253 return 0;
2254 }
2255
2256 process_failed $start_list[0];
2257 return 1;
2258}
2259
2260sub config_bisect {
2261 my ($i) = @_;
2262
2263 my $start_config = $opt{"CONFIG_BISECT[$i]"};
2264
2265 my $tmpconfig = "$tmpdir/use_config";
2266
30f75da5
SR
2267 if (defined($config_bisect_good)) {
2268 process_config_ignore $config_bisect_good;
2269 }
2270
0a05c769
SR
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";
2276 } else {
2277 unlink $tmpconfig;
2278 }
2279
0a05c769 2280 if (-f $tmpconfig) {
fcb3f16a 2281 load_force_config($tmpconfig);
0a05c769
SR
2282 process_config_ignore $tmpconfig;
2283 }
2284
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";
2288
2289 # read directly what we want to check
2290 my %config_check;
2291 open (IN, $output_config)
2292 or dodie "faied to open $output_config";
2293
2294 while (<IN>) {
2295 if (/^((CONFIG\S*)=.*)/) {
2296 $config_check{$2} = $1;
2297 }
2298 }
2299 close(IN);
2300
250bae8b 2301 # Now run oldconfig with the minconfig
fcb3f16a 2302 make_oldconfig;
0a05c769
SR
2303
2304 # check to see what we lost (or gained)
2305 open (IN, $output_config)
2306 or dodie "Failed to read $start_config";
2307
2308 my %removed_configs;
2309 my %added_configs;
2310
2311 while (<IN>) {
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;
2318 } else {
2319 $config_list{$2} = $1;
2320 }
2321 } elsif (!defined($config_ignore{$2})) {
2322 $added_configs{$2} = $1;
2323 $config_list{$2} = $1;
2324 }
2325 }
2326 }
2327 close(IN);
2328
2329 my @confs = keys %removed_configs;
2330 if ($#confs >= 0) {
2331 doprint "Configs overridden by default configs and removed from check:\n";
2332 foreach my $config (@confs) {
2333 doprint " $config\n";
2334 }
2335 }
2336 @confs = keys %added_configs;
2337 if ($#confs >= 0) {
2338 doprint "Configs appearing in make oldconfig and added:\n";
2339 foreach my $config (@confs) {
2340 doprint " $config\n";
2341 }
2342 }
2343
2344 my %config_test;
2345 my $once = 0;
2346
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})) {
2356 if (!$once) {
2357 $once = 1;
2358 doprint "Configs not produced by kconfig (will not be checked):\n";
2359 }
2360 doprint " $config\n";
2361 delete $config_list{$config};
2362 }
2363 }
2364 my $ret;
2365 do {
2366 $ret = run_config_bisect;
2367 } while (!$ret);
2368
2369 return $ret if ($ret < 0);
5f9b6ced
SR
2370
2371 success $i;
2372}
2373
27d934b2
SR
2374sub patchcheck_reboot {
2375 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
2728be41 2376 reboot $patchcheck_sleep_time;
27d934b2
SR
2377}
2378
6c5ee0be
SR
2379sub patchcheck {
2380 my ($i) = @_;
2381
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]"}));
2386
2387 my $start = $opt{"PATCHCHECK_START[$i]"};
2388
2389 my $end = "HEAD";
2390 if (defined($opt{"PATCHCHECK_END[$i]"})) {
2391 $end = $opt{"PATCHCHECK_END[$i]"};
2392 }
2393
a57419b3
SR
2394 # Get the true sha1's since we can use things like HEAD~3
2395 $start = get_sha1($start);
2396 $end = get_sha1($end);
2397
6c5ee0be
SR
2398 my $type = $opt{"PATCHCHECK_TYPE[$i]"};
2399
2400 # Can't have a test without having a test to run
2401 if ($type eq "test" && !defined($run_test)) {
2402 $type = "boot";
2403 }
2404
2405 open (IN, "git log --pretty=oneline $end|") or
2406 dodie "could not get git list";
2407
2408 my @list;
2409
2410 while (<IN>) {
2411 chomp;
2412 $list[$#list+1] = $_;
2413 last if (/^$start/);
2414 }
2415 close(IN);
2416
2417 if ($list[$#list] !~ /^$start/) {
2b7d9b21 2418 fail "SHA1 $start not found";
6c5ee0be
SR
2419 }
2420
2421 # go backwards in the list
2422 @list = reverse @list;
2423
2424 my $save_clean = $noclean;
1990207d
SR
2425 my %ignored_warnings;
2426
2427 if (defined($ignore_warnings)) {
2428 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2429 $ignored_warnings{$sha1} = 1;
2430 }
2431 }
6c5ee0be
SR
2432
2433 $in_patchcheck = 1;
2434 foreach my $item (@list) {
2435 my $sha1 = $item;
2436 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2437
2438 doprint "\nProcessing commit $item\n\n";
2439
2440 run_command "git checkout $sha1" or
2441 die "Failed to checkout $sha1";
2442
2443 # only clean on the first and last patch
2444 if ($item eq $list[0] ||
2445 $item eq $list[$#list]) {
2446 $noclean = $save_clean;
2447 } else {
2448 $noclean = 1;
2449 }
2450
2451 if (defined($minconfig)) {
2b7d9b21 2452 build "useconfig:$minconfig" or return 0;
6c5ee0be
SR
2453 } else {
2454 # ?? no config to use?
2b7d9b21 2455 build "oldconfig" or return 0;
6c5ee0be
SR
2456 }
2457
1990207d
SR
2458
2459 if (!defined($ignored_warnings{$sha1})) {
2460 check_buildlog $sha1 or return 0;
2461 }
6c5ee0be
SR
2462
2463 next if ($type eq "build");
2464
7faafbd6
SR
2465 my $failed = 0;
2466
ddf607e5 2467 start_monitor_and_boot or $failed = 1;
7faafbd6
SR
2468
2469 if (!$failed && $type ne "boot"){
2470 do_run_test or $failed = 1;
2471 }
2472 end_monitor;
2473 return 0 if ($failed);
2474
27d934b2
SR
2475 patchcheck_reboot;
2476
6c5ee0be
SR
2477 }
2478 $in_patchcheck = 0;
2479 success $i;
2b7d9b21
SR
2480
2481 return 1;
6c5ee0be
SR
2482}
2483
b9066f6c 2484my %depends;
ac6974c7 2485my %depcount;
b9066f6c
SR
2486my $iflevel = 0;
2487my @ifdeps;
2488
2489# prevent recursion
2490my %read_kconfigs;
2491
ac6974c7
SR
2492sub add_dep {
2493 # $config depends on $dep
2494 my ($config, $dep) = @_;
2495
2496 if (defined($depends{$config})) {
2497 $depends{$config} .= " " . $dep;
2498 } else {
2499 $depends{$config} = $dep;
2500 }
2501
2502 # record the number of configs depending on $dep
2503 if (defined $depcount{$dep}) {
2504 $depcount{$dep}++;
2505 } else {
2506 $depcount{$dep} = 1;
2507 }
2508}
2509
b9066f6c
SR
2510# taken from streamline_config.pl
2511sub read_kconfig {
2512 my ($kconfig) = @_;
2513
2514 my $state = "NONE";
2515 my $config;
2516 my @kconfigs;
2517
2518 my $cont = 0;
2519 my $line;
2520
2521
2522 if (! -f $kconfig) {
2523 doprint "file $kconfig does not exist, skipping\n";
2524 return;
2525 }
2526
2527 open(KIN, "$kconfig")
2528 or die "Can't open $kconfig";
2529 while (<KIN>) {
2530 chomp;
2531
2532 # Make sure that lines ending with \ continue
2533 if ($cont) {
2534 $_ = $line . " " . $_;
2535 }
2536
2537 if (s/\\$//) {
2538 $cont = 1;
2539 $line = $_;
2540 next;
2541 }
2542
2543 $cont = 0;
2544
2545 # collect any Kconfig sources
2546 if (/^source\s*"(.*)"/) {
2547 $kconfigs[$#kconfigs+1] = $1;
2548 }
2549
2550 # configs found
2551 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2552 $state = "NEW";
2553 $config = $2;
2554
2555 for (my $i = 0; $i < $iflevel; $i++) {
ac6974c7 2556 add_dep $config, $ifdeps[$i];
b9066f6c
SR
2557 }
2558
2559 # collect the depends for the config
2560 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2561
ac6974c7 2562 add_dep $config, $1;
b9066f6c
SR
2563
2564 # Get the configs that select this config
ac6974c7
SR
2565 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2566
2567 # selected by depends on config
2568 add_dep $1, $config;
b9066f6c
SR
2569
2570 # Check for if statements
2571 } elsif (/^if\s+(.*\S)\s*$/) {
2572 my $deps = $1;
2573 # remove beginning and ending non text
2574 $deps =~ s/^[^a-zA-Z0-9_]*//;
2575 $deps =~ s/[^a-zA-Z0-9_]*$//;
2576
2577 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2578
2579 $ifdeps[$iflevel++] = join ':', @deps;
2580
2581 } elsif (/^endif/) {
2582
2583 $iflevel-- if ($iflevel);
2584
2585 # stop on "help"
2586 } elsif (/^\s*help\s*$/) {
2587 $state = "NONE";
2588 }
2589 }
2590 close(KIN);
2591
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");
2597 }
2598 }
2599}
2600
2601sub read_depends {
2602 # find out which arch this is by the kconfig file
2603 open (IN, $output_config)
2604 or dodie "Failed to read $output_config";
2605 my $arch;
2606 while (<IN>) {
2607 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2608 $arch = $1;
2609 last;
2610 }
2611 }
2612 close IN;
2613
2614 if (!defined($arch)) {
2615 doprint "Could not find arch from config file\n";
2616 doprint "no dependencies used\n";
2617 return;
2618 }
2619
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") {
2623 $arch = "x86";
2624 } elsif ($arch =~ /^tile/) {
2625 $arch = "tile";
2626 }
2627
2628 my $kconfig = "$builddir/arch/$arch/Kconfig";
2629
2630 if (! -f $kconfig && $arch =~ /\d$/) {
2631 my $orig = $arch;
2632 # some subarchs have numbers, truncate them
2633 $arch =~ s/\d*$//;
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";
2638 return;
2639 }
2640 }
2641
2642 read_kconfig($kconfig);
2643}
2644
4c4ab120
SR
2645sub read_config_list {
2646 my ($config) = @_;
2647
2648 open (IN, $config)
2649 or dodie "Failed to read $config";
2650
2651 while (<IN>) {
2652 if (/^((CONFIG\S*)=.*)/) {
2653 if (!defined($config_ignore{$2})) {
2654 $config_list{$2} = $1;
2655 }
2656 }
2657 }
2658
2659 close(IN);
2660}
2661
2662sub read_output_config {
2663 my ($config) = @_;
2664
2665 assign_configs \%config_ignore, $config;
2666}
2667
2668sub make_new_config {
2669 my @configs = @_;
2670
2671 open (OUT, ">$output_config")
2672 or dodie "Failed to write $output_config";
2673
2674 foreach my $config (@configs) {
2675 print OUT "$config\n";
2676 }
2677 close OUT;
2678}
2679
ac6974c7
SR
2680sub chomp_config {
2681 my ($config) = @_;
2682
2683 $config =~ s/CONFIG_//;
2684
2685 return $config;
2686}
2687
b9066f6c
SR
2688sub get_depends {
2689 my ($dep) = @_;
2690
ac6974c7 2691 my $kconfig = chomp_config $dep;
b9066f6c
SR
2692
2693 $dep = $depends{"$kconfig"};
2694
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.
2698
2699 my $valid = "A-Za-z_0-9";
2700
2701 my @configs;
2702
2703 while ($dep =~ /[$valid]/) {
2704
2705 if ($dep =~ /^[^$valid]*([$valid]+)/) {
2706 my $conf = "CONFIG_" . $1;
2707
2708 $configs[$#configs + 1] = $conf;
2709
2710 $dep =~ s/^[^$valid]*[$valid]+//;
2711 } else {
2712 die "this should never happen";
2713 }
2714 }
2715
2716 return @configs;
2717}
2718
2719my %min_configs;
2720my %keep_configs;
43d1b651 2721my %save_configs;
b9066f6c
SR
2722my %processed_configs;
2723my %nochange_config;
2724
2725sub test_this_config {
2726 my ($config) = @_;
2727
2728 my $found;
2729
2730 # if we already processed this config, skip it
2731 if (defined($processed_configs{$config})) {
2732 return undef;
2733 }
2734 $processed_configs{$config} = 1;
2735
2736 # if this config failed during this round, skip it
2737 if (defined($nochange_config{$config})) {
2738 return undef;
2739 }
2740
ac6974c7 2741 my $kconfig = chomp_config $config;
b9066f6c
SR
2742
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)) {
2751 return $found;
2752 }
2753 }
2754 }
2755
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
2759 # we had before
2760 my %configs = %min_configs;
2761 delete $configs{$config};
2762 make_new_config ((values %configs), (values %keep_configs));
2763 make_oldconfig;
2764 undef %configs;
2765 assign_configs \%configs, $output_config;
2766
2767 return $config if (!defined($configs{$config}));
2768
2769 doprint "disabling config $config did not change .config\n";
2770
2771 $nochange_config{$config} = 1;
2772
2773 return undef;
2774}
2775
4c4ab120
SR
2776sub make_min_config {
2777 my ($i) = @_;
2778
2779 if (!defined($output_minconfig)) {
2780 fail "OUTPUT_MIN_CONFIG not defined" and return;
2781 }
35ce5952
SR
2782
2783 # If output_minconfig exists, and the start_minconfig
2784 # came from min_config, than ask if we should use
2785 # that instead.
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;
2790 }
2791 }
2792
4c4ab120
SR
2793 if (!defined($start_minconfig)) {
2794 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
2795 }
2796
35ce5952
SR
2797 my $temp_config = "$tmpdir/temp_config";
2798
4c4ab120
SR
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.
2802
2803 my $save_minconfig = $minconfig;
2804 undef $minconfig;
2805
2806 run_command "$make allnoconfig" or return 0;
2807
b9066f6c
SR
2808 read_depends;
2809
4c4ab120 2810 process_config_ignore $output_config;
b9066f6c 2811
43d1b651 2812 undef %save_configs;
b9066f6c 2813 undef %min_configs;
4c4ab120
SR
2814
2815 if (defined($ignore_config)) {
2816 # make sure the file exists
2817 `touch $ignore_config`;
43d1b651 2818 assign_configs \%save_configs, $ignore_config;
4c4ab120
SR
2819 }
2820
43d1b651
SR
2821 %keep_configs = %save_configs;
2822
4c4ab120
SR
2823 doprint "Load initial configs from $start_minconfig\n";
2824
2825 # Look at the current min configs, and save off all the
2826 # ones that were set via the allnoconfig
4c4ab120
SR
2827 assign_configs \%min_configs, $start_minconfig;
2828
2829 my @config_keys = keys %min_configs;
2830
ac6974c7
SR
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;
2836 }
2837 }
2838
4c4ab120
SR
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};
2848 next;
2849 }
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
2853 # the keep configs.
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};
2859 } else {
2860 doprint "$config set by allnoconfig ... ignored\n";
2861 }
2862 delete $min_configs{$config};
2863 }
2864 }
2865
4c4ab120 2866 my $done = 0;
b9066f6c 2867 my $take_two = 0;
4c4ab120
SR
2868
2869 while (!$done) {
2870
2871 my $config;
2872 my $found;
2873
2874 # Now disable each config one by one and do a make oldconfig
2875 # till we find a config that changes our list.
2876
4c4ab120 2877 my @test_configs = keys %min_configs;
ac6974c7
SR
2878
2879 # Sort keys by who is most dependent on
2880 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
2881 @test_configs ;
2882
2883 # Put configs that did not modify the config at the end.
4c4ab120
SR
2884 my $reset = 1;
2885 for (my $i = 0; $i < $#test_configs; $i++) {
2886 if (!defined($nochange_config{$test_configs[0]})) {
2887 $reset = 0;
2888 last;
2889 }
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;
2894 }
2895
2896 # if every test config has failed to modify the .config file
2897 # in the past, then reset and start over.
2898 if ($reset) {
2899 undef %nochange_config;
2900 }
2901
b9066f6c
SR
2902 undef %processed_configs;
2903
4c4ab120
SR
2904 foreach my $config (@test_configs) {
2905
b9066f6c 2906 $found = test_this_config $config;
4c4ab120 2907
b9066f6c 2908 last if (defined($found));
4c4ab120
SR
2909
2910 # oh well, try another config
4c4ab120
SR
2911 }
2912
2913 if (!defined($found)) {
b9066f6c
SR
2914 # we could have failed due to the nochange_config hash
2915 # reset and try again
2916 if (!$take_two) {
2917 undef %nochange_config;
2918 $take_two = 1;
2919 next;
2920 }
4c4ab120
SR
2921 doprint "No more configs found that we can disable\n";
2922 $done = 1;
2923 last;
2924 }
b9066f6c 2925 $take_two = 0;
4c4ab120
SR
2926
2927 $config = $found;
2928
2929 doprint "Test with $config disabled\n";
2930
2931 # set in_bisect to keep build and monitor from dieing
2932 $in_bisect = 1;
2933
2934 my $failed = 0;
2935 build "oldconfig";
2936 start_monitor_and_boot or $failed = 1;
2937 end_monitor;
2938
2939 $in_bisect = 0;
2940
2941 if ($failed) {
b9066f6c 2942 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
4c4ab120
SR
2943 # this config is needed, add it to the ignore list.
2944 $keep_configs{$config} = $min_configs{$config};
43d1b651 2945 $save_configs{$config} = $min_configs{$config};
4c4ab120 2946 delete $min_configs{$config};
35ce5952
SR
2947
2948 # update new ignore configs
2949 if (defined($ignore_config)) {
2950 open (OUT, ">$temp_config")
2951 or die "Can't write to $temp_config";
43d1b651
SR
2952 foreach my $config (keys %save_configs) {
2953 print OUT "$save_configs{$config}\n";
35ce5952
SR
2954 }
2955 close OUT;
2956 run_command "mv $temp_config $ignore_config" or
2957 dodie "failed to copy update to $ignore_config";
2958 }
2959
4c4ab120
SR
2960 } else {
2961 # We booted without this config, remove it from the minconfigs.
2962 doprint "$config is not needed, disabling\n";
2963
2964 delete $min_configs{$config};
2965
2966 # Also disable anything that is not enabled in this config
2967 my %configs;
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};
2974 }
2975 }
2976
2977 # Save off all the current mandidory configs
35ce5952
SR
2978 open (OUT, ">$temp_config")
2979 or die "Can't write to $temp_config";
4c4ab120
SR
2980 foreach my $config (keys %keep_configs) {
2981 print OUT "$keep_configs{$config}\n";
2982 }
2983 foreach my $config (keys %min_configs) {
2984 print OUT "$min_configs{$config}\n";
2985 }
2986 close OUT;
35ce5952
SR
2987
2988 run_command "mv $temp_config $output_minconfig" or
2989 dodie "failed to copy update to $output_minconfig";
4c4ab120
SR
2990 }
2991
2992 doprint "Reboot and wait $sleep_time seconds\n";
2728be41 2993 reboot $sleep_time;
4c4ab120
SR
2994 }
2995
2996 success $i;
2997 return 1;
2998}
2999
8d1491ba
SR
3000$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
3001
3002if ($#ARGV == 0) {
3003 $ktest_config = $ARGV[0];
3004 if (! -f $ktest_config) {
3005 print "$ktest_config does not exist.\n";
35ce5952 3006 if (!read_yn "Create it?") {
8d1491ba
SR
3007 exit 0;
3008 }
3009 }
3010} else {
3011 $ktest_config = "ktest.conf";
3012}
3013
3014if (! -f $ktest_config) {
3015 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3016 print OUT << "EOF"
3017# Generated by ktest.pl
3018#
0e7a22de
SR
3019
3020# PWD is a ktest.pl variable that will result in the process working
3021# directory that ktest.pl is executed in.
3022
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.
3027#
3028THIS_DIR := $variable{"PWD"}
3029
8d1491ba
SR
3030# Define each test with TEST_START
3031# The config options below it will override the defaults
3032TEST_START
3033
3034DEFAULTS
3035EOF
3036;
3037 close(OUT);
3038}
3039read_config $ktest_config;
3040
23715c3c
SR
3041if (defined($opt{"LOG_FILE"})) {
3042 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3043}
3044
8d1491ba
SR
3045# Append any configs entered in manually to the config file.
3046my @new_configs = keys %entered_configs;
3047if ($#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";
0e7a22de 3052 $opt{$config} = process_variables($entered_configs{$config});
8d1491ba
SR
3053 }
3054}
2545eb61 3055
2b7d9b21
SR
3056if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3057 unlink $opt{"LOG_FILE"};
3058}
2545eb61 3059
2b7d9b21
SR
3060doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3061
a57419b3
SR
3062for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3063
3064 if (!$i) {
3065 doprint "DEFAULT OPTIONS:\n";
3066 } else {
3067 doprint "\nTEST $i OPTIONS";
3068 if (defined($repeat_tests{$i})) {
3069 $repeat = $repeat_tests{$i};
3070 doprint " ITERATE $repeat";
3071 }
3072 doprint "\n";
3073 }
3074
3075 foreach my $option (sort keys %opt) {
3076
3077 if ($option =~ /\[(\d+)\]$/) {
3078 next if ($i != $1);
3079 } else {
3080 next if ($i);
3081 }
3082
3083 doprint "$option = $opt{$option}\n";
3084 }
2b7d9b21 3085}
2545eb61 3086
2a62512b 3087sub __set_test_option {
5a391fbf 3088 my ($name, $i) = @_;
2545eb61 3089
5a391fbf 3090 my $option = "$name\[$i\]";
5c42fc5b 3091
5a391fbf
SR
3092 if (defined($opt{$option})) {
3093 return $opt{$option};
5f9b6ced
SR
3094 }
3095
a57419b3
SR
3096 foreach my $test (keys %repeat_tests) {
3097 if ($i >= $test &&
3098 $i < $test + $repeat_tests{$test}) {
3099 $option = "$name\[$test\]";
3100 if (defined($opt{$option})) {
3101 return $opt{$option};
3102 }
3103 }
3104 }
3105
5a391fbf
SR
3106 if (defined($opt{$name})) {
3107 return $opt{$name};
2545eb61
SR
3108 }
3109
5a391fbf
SR
3110 return undef;
3111}
3112
2a62512b
SR
3113sub set_test_option {
3114 my ($name, $i) = @_;
3115
3116 my $option = __set_test_option($name, $i);
3117 return $option if (!defined($option));
3118
23715c3c 3119 return eval_option($option, $i);
2a62512b
SR
3120}
3121
5a391fbf 3122# First we need to do is the builds
a75fecec
SR
3123for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
3124
4ab1cce5
SR
3125 # Do not reboot on failing test options
3126 $no_reboot = 1;
3127
576f627c
SR
3128 $iteration = $i;
3129
a75fecec
SR
3130 my $makecmd = set_test_option("MAKE_CMD", $i);
3131
3132 $machine = set_test_option("MACHINE", $i);
e48c5293 3133 $ssh_user = set_test_option("SSH_USER", $i);
a75fecec
SR
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);
0bd6c1a3
SR
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);
a75fecec 3144 $power_cycle = set_test_option("POWER_CYCLE", $i);
e48c5293 3145 $reboot = set_test_option("REBOOT", $i);
a75fecec
SR
3146 $noclean = set_test_option("BUILD_NOCLEAN", $i);
3147 $minconfig = set_test_option("MIN_CONFIG", $i);
4c4ab120
SR
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);
a75fecec
SR
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);
8b37ca8c 3155 $post_install = set_test_option("POST_INSTALL", $i);
e0a8742e 3156 $no_install = set_test_option("NO_INSTALL", $i);
a75fecec
SR
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);
576f627c
SR
3162 $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
3163 $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
a75fecec
SR
3164 $sleep_time = set_test_option("SLEEP_TIME", $i);
3165 $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
27d934b2 3166 $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
1990207d 3167 $ignore_warnings = set_test_option("IGNORE_WARNINGS", $i);
c960bb9f 3168 $bisect_manual = set_test_option("BISECT_MANUAL", $i);
c23dca7c 3169 $bisect_skip = set_test_option("BISECT_SKIP", $i);
30f75da5 3170 $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
a75fecec 3171 $store_failures = set_test_option("STORE_FAILURES", $i);
de5b6e3b 3172 $store_successes = set_test_option("STORE_SUCCESSES", $i);
9064af52 3173 $test_name = set_test_option("TEST_NAME", $i);
a75fecec
SR
3174 $timeout = set_test_option("TIMEOUT", $i);
3175 $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
3176 $console = set_test_option("CONSOLE", $i);
f1a5b962 3177 $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
a75fecec 3178 $success_line = set_test_option("SUCCESS_LINE", $i);
2b803365 3179 $reboot_success_line = set_test_option("REBOOT_SUCCESS_LINE", $i);
1c8a617a
SR
3180 $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
3181 $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
2d01b26a 3182 $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
a75fecec 3183 $build_target = set_test_option("BUILD_TARGET", $i);
e48c5293
SR
3184 $ssh_exec = set_test_option("SSH_EXEC", $i);
3185 $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
a75fecec
SR
3186 $target_image = set_test_option("TARGET_IMAGE", $i);
3187 $localversion = set_test_option("LOCALVERSION", $i);
3188
35ce5952
SR
3189 $start_minconfig_defined = 1;
3190
4c4ab120 3191 if (!defined($start_minconfig)) {
35ce5952 3192 $start_minconfig_defined = 0;
4c4ab120
SR
3193 $start_minconfig = $minconfig;
3194 }
3195
a75fecec
SR
3196 chdir $builddir || die "can't change directory to $builddir";
3197
a908a665
AJ
3198 foreach my $dir ($tmpdir, $outputdir) {
3199 if (!-d $dir) {
3200 mkpath($dir) or
3201 die "can't create $dir";
3202 }
a75fecec 3203 }
1a5cfce3 3204
e48c5293
SR
3205 $ENV{"SSH_USER"} = $ssh_user;
3206 $ENV{"MACHINE"} = $machine;
3207
a75fecec
SR
3208 $target = "$ssh_user\@$machine";
3209
3210 $buildlog = "$tmpdir/buildlog-$machine";
a9dd5d63 3211 $testlog = "$tmpdir/testlog-$machine";
a75fecec
SR
3212 $dmesg = "$tmpdir/dmesg-$machine";
3213 $make = "$makecmd O=$outputdir";
51ad1dd1 3214 $output_config = "$outputdir/.config";
a75fecec
SR
3215
3216 if ($reboot_type eq "grub") {
576f627c 3217 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
a75fecec 3218 } elsif (!defined($reboot_script)) {
576f627c 3219 dodie "REBOOT_SCRIPT not defined"
a75fecec
SR
3220 }
3221
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]"};
0a05c769
SR
3227 } elsif ($test_type eq "config_bisect") {
3228 $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
a75fecec
SR
3229 }
3230
4c4ab120
SR
3231 if ($test_type eq "make_min_config") {
3232 $run_type = "";
3233 }
3234
a75fecec
SR
3235 # mistake in config file?
3236 if (!defined($run_type)) {
3237 $run_type = "ERROR";
3238 }
5a391fbf 3239
e0a8742e
SR
3240 my $installme = "";
3241 $installme = " no_install" if ($no_install);
3242
2545eb61 3243 doprint "\n\n";
e0a8742e 3244 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
7faafbd6
SR
3245
3246 unlink $dmesg;
3247 unlink $buildlog;
a9dd5d63 3248 unlink $testlog;
2545eb61 3249
250bae8b
SR
3250 if (defined($addconfig)) {
3251 my $min = $minconfig;
3252 if (!defined($minconfig)) {
3253 $min = "";
3254 }
3255 run_command "cat $addconfig $min > $tmpdir/add_config" or
2b7d9b21 3256 dodie "Failed to create temp config";
9be2e6b5 3257 $minconfig = "$tmpdir/add_config";
2b7d9b21
SR
3258 }
3259
6c5ee0be
SR
3260 my $checkout = $opt{"CHECKOUT[$i]"};
3261 if (defined($checkout)) {
3262 run_command "git checkout $checkout" or
3263 die "failed to checkout $checkout";
3264 }
3265
4ab1cce5
SR
3266 $no_reboot = 0;
3267
3268
a75fecec 3269 if ($test_type eq "bisect") {
5f9b6ced
SR
3270 bisect $i;
3271 next;
0a05c769
SR
3272 } elsif ($test_type eq "config_bisect") {
3273 config_bisect $i;
3274 next;
a75fecec 3275 } elsif ($test_type eq "patchcheck") {
6c5ee0be
SR
3276 patchcheck $i;
3277 next;
4c4ab120
SR
3278 } elsif ($test_type eq "make_min_config") {
3279 make_min_config $i;
3280 next;
2545eb61 3281 }
2545eb61 3282
7faafbd6
SR
3283 if ($build_type ne "nobuild") {
3284 build $build_type or next;
2545eb61
SR
3285 }
3286
cd8e368f
SR
3287 if ($test_type eq "install") {
3288 get_version;
3289 install;
3290 success $i;
3291 next;
3292 }
3293
a75fecec 3294 if ($test_type ne "build") {
a75fecec 3295 my $failed = 0;
ddf607e5 3296 start_monitor_and_boot or $failed = 1;
a75fecec
SR
3297
3298 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3299 do_run_test or $failed = 1;
3300 }
3301 end_monitor;
3302 next if ($failed);
5a391fbf
SR
3303 }
3304
5f9b6ced 3305 success $i;
2545eb61
SR
3306}
3307
5c42fc5b 3308if ($opt{"POWEROFF_ON_SUCCESS"}) {
75c3fda7 3309 halt;
576f627c 3310} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
75c3fda7 3311 reboot;
5c42fc5b 3312}
75c3fda7 3313
e48c5293
SR
3314doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3315
2545eb61 3316exit 0;
This page took 0.261776 seconds and 5 git commands to generate.