blob: 5ba9ad4248d71a0e6b0c0b0d40c2ebd6e01e6aae [file] [log] [blame]
Steven Rostedt2545eb62010-11-02 15:01:32 -04001#!/usr/bin/perl -w
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002#
Uwe Kleine-Königcce1dac2011-01-24 21:12:01 +01003# Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04004# Licensed under the terms of the GNU GPL License version 2
5#
Steven Rostedt2545eb62010-11-02 15:01:32 -04006
7use strict;
8use IPC::Open2;
9use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Steven Rostedt7faafbd2010-11-02 14:58:22 -040010use File::Path qw(mkpath);
11use File::Copy qw(cp);
Steven Rostedt2545eb62010-11-02 15:01:32 -040012use FileHandle;
13
Steven Rostedte48c5292010-11-02 14:35:37 -040014my $VERSION = "0.2";
15
Steven Rostedt2545eb62010-11-02 15:01:32 -040016$| = 1;
17
18my %opt;
Steven Rostedta57419b2010-11-02 15:13:54 -040019my %repeat_tests;
20my %repeats;
Steven Rostedt2545eb62010-11-02 15:01:32 -040021
22#default opts
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050023my %default = (
24 "NUM_TESTS" => 1,
25 "TEST_TYPE" => "build",
26 "BUILD_TYPE" => "randconfig",
27 "MAKE_CMD" => "make",
28 "TIMEOUT" => 120,
29 "TMP_DIR" => "/tmp/ktest/\${MACHINE}",
30 "SLEEP_TIME" => 60, # sleep time between tests
31 "BUILD_NOCLEAN" => 0,
32 "REBOOT_ON_ERROR" => 0,
33 "POWEROFF_ON_ERROR" => 0,
34 "REBOOT_ON_SUCCESS" => 1,
35 "POWEROFF_ON_SUCCESS" => 0,
36 "BUILD_OPTIONS" => "",
37 "BISECT_SLEEP_TIME" => 60, # sleep time between bisects
38 "PATCHCHECK_SLEEP_TIME" => 60, # sleep time between patch checks
39 "CLEAR_LOG" => 0,
40 "BISECT_MANUAL" => 0,
41 "BISECT_SKIP" => 1,
42 "SUCCESS_LINE" => "login:",
43 "DETECT_TRIPLE_FAULT" => 1,
44 "NO_INSTALL" => 0,
45 "BOOTED_TIMEOUT" => 1,
46 "DIE_ON_FAILURE" => 1,
47 "SSH_EXEC" => "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND",
48 "SCP_TO_TARGET" => "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE",
49 "REBOOT" => "ssh \$SSH_USER\@\$MACHINE reboot",
50 "STOP_AFTER_SUCCESS" => 10,
51 "STOP_AFTER_FAILURE" => 60,
52 "STOP_TEST_AFTER" => 600,
Steven Rostedt600bbf02011-11-21 20:12:04 -050053
54# required, and we will ask users if they don't have them but we keep the default
55# value something that is common.
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050056 "REBOOT_TYPE" => "grub",
57 "LOCALVERSION" => "-test",
58 "SSH_USER" => "root",
59 "BUILD_TARGET" => "arch/x86/boot/bzImage",
60 "TARGET_IMAGE" => "/boot/vmlinuz-test",
61);
Steven Rostedt2545eb62010-11-02 15:01:32 -040062
Steven Rostedt8d1491b2010-11-18 15:39:48 -050063my $ktest_config;
Steven Rostedt2545eb62010-11-02 15:01:32 -040064my $version;
Steven Rostedta75fece2010-11-02 14:58:27 -040065my $machine;
Steven Rostedte48c5292010-11-02 14:35:37 -040066my $ssh_user;
Steven Rostedta75fece2010-11-02 14:58:27 -040067my $tmpdir;
68my $builddir;
69my $outputdir;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -050070my $output_config;
Steven Rostedta75fece2010-11-02 14:58:27 -040071my $test_type;
Steven Rostedt7faafbd2010-11-02 14:58:22 -040072my $build_type;
Steven Rostedta75fece2010-11-02 14:58:27 -040073my $build_options;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -040074my $pre_build;
75my $post_build;
76my $pre_build_die;
77my $post_build_die;
Steven Rostedta75fece2010-11-02 14:58:27 -040078my $reboot_type;
79my $reboot_script;
80my $power_cycle;
Steven Rostedte48c5292010-11-02 14:35:37 -040081my $reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -040082my $reboot_on_error;
Steven Rostedtbc7c5802011-12-22 16:29:10 -050083my $switch_to_good;
84my $switch_to_test;
Steven Rostedta75fece2010-11-02 14:58:27 -040085my $poweroff_on_error;
86my $die_on_failure;
Steven Rostedt576f6272010-11-02 14:58:38 -040087my $powercycle_after_reboot;
88my $poweroff_after_halt;
Steven Rostedte48c5292010-11-02 14:35:37 -040089my $ssh_exec;
90my $scp_to_target;
Steven Rostedta75fece2010-11-02 14:58:27 -040091my $power_off;
92my $grub_menu;
Steven Rostedt2545eb62010-11-02 15:01:32 -040093my $grub_number;
94my $target;
95my $make;
Steven Rostedt8b37ca82010-11-02 14:58:33 -040096my $post_install;
Steven Rostedte0a87422011-09-30 17:50:48 -040097my $no_install;
Steven Rostedt5c42fc52010-11-02 14:57:01 -040098my $noclean;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -040099my $minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400100my $start_minconfig;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400101my $start_minconfig_defined;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400102my $output_minconfig;
103my $ignore_config;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -0400104my $addconfig;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400105my $in_bisect = 0;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500106my $bisect_bad_commit = "";
Steven Rostedtd6ce2a02010-11-02 14:58:05 -0400107my $reverse_bisect;
Steven Rostedtc960bb92011-03-08 09:22:39 -0500108my $bisect_manual;
Steven Rostedtc23dca72011-03-08 09:26:31 -0500109my $bisect_skip;
Steven Rostedt30f75da2011-06-13 10:35:35 -0400110my $config_bisect_good;
Steven Rostedtc5dacb82011-12-22 12:43:57 -0500111my $bisect_ret_good;
112my $bisect_ret_bad;
113my $bisect_ret_skip;
114my $bisect_ret_abort;
115my $bisect_ret_default;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400116my $in_patchcheck = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400117my $run_test;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400118my $redirect;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400119my $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +0530120my $testlog;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400121my $dmesg;
122my $monitor_fp;
123my $monitor_pid;
124my $monitor_cnt = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -0400125my $sleep_time;
126my $bisect_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -0400127my $patchcheck_sleep_time;
Steven Rostedt19902072011-06-14 20:46:25 -0400128my $ignore_warnings;
Steven Rostedta75fece2010-11-02 14:58:27 -0400129my $store_failures;
Rabin Vincentde5b6e32011-11-18 17:05:31 +0530130my $store_successes;
Steven Rostedt9064af52011-06-13 10:38:48 -0400131my $test_name;
Steven Rostedta75fece2010-11-02 14:58:27 -0400132my $timeout;
133my $booted_timeout;
Steven Rostedtf1a5b962011-06-13 10:30:00 -0400134my $detect_triplefault;
Steven Rostedta75fece2010-11-02 14:58:27 -0400135my $console;
Steven Rostedt2b803362011-09-30 18:00:23 -0400136my $reboot_success_line;
Steven Rostedta75fece2010-11-02 14:58:27 -0400137my $success_line;
Steven Rostedt1c8a6172010-11-09 12:55:40 -0500138my $stop_after_success;
139my $stop_after_failure;
Steven Rostedt2d01b262011-03-08 09:47:54 -0500140my $stop_test_after;
Steven Rostedta75fece2010-11-02 14:58:27 -0400141my $build_target;
142my $target_image;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500143my $checkout;
Steven Rostedta75fece2010-11-02 14:58:27 -0400144my $localversion;
Steven Rostedt576f6272010-11-02 14:58:38 -0400145my $iteration = 0;
Steven Rostedte48c5292010-11-02 14:35:37 -0400146my $successes = 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400147
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500148my $bisect_good;
149my $bisect_bad;
150my $bisect_type;
151my $bisect_start;
152my $bisect_replay;
153my $bisect_files;
154my $bisect_reverse;
155my $bisect_check;
156
157my $config_bisect;
158my $config_bisect_type;
159
160my $patchcheck_type;
161my $patchcheck_start;
162my $patchcheck_end;
163
Steven Rostedt165708b2011-11-26 20:56:52 -0500164# set when a test is something other that just building or install
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500165# which would require more options.
166my $buildonly = 1;
167
Steven Rostedtdbd37832011-11-23 16:00:48 -0500168# set when creating a new config
169my $newconfig = 0;
170
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500171my %entered_configs;
172my %config_help;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400173my %variable;
Steven Rostedtfcb3f162011-06-13 10:40:58 -0400174my %force_config;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500175
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400176# do not force reboots on config problems
177my $no_reboot = 1;
178
Steven Rostedt7bf51072011-10-22 09:07:03 -0400179# default variables that can be used
180chomp ($variable{"PWD"} = `pwd`);
181
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500182$config_help{"MACHINE"} = << "EOF"
183 The machine hostname that you will test.
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500184 For build only tests, it is still needed to differentiate log files.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500185EOF
186 ;
187$config_help{"SSH_USER"} = << "EOF"
188 The box is expected to have ssh on normal bootup, provide the user
189 (most likely root, since you need privileged operations)
190EOF
191 ;
192$config_help{"BUILD_DIR"} = << "EOF"
193 The directory that contains the Linux source code (full path).
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500194 You can use \${PWD} that will be the path where ktest.pl is run, or use
195 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500196EOF
197 ;
198$config_help{"OUTPUT_DIR"} = << "EOF"
199 The directory that the objects will be built (full path).
200 (can not be same as BUILD_DIR)
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500201 You can use \${PWD} that will be the path where ktest.pl is run, or use
202 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500203EOF
204 ;
205$config_help{"BUILD_TARGET"} = << "EOF"
206 The location of the compiled file to copy to the target.
207 (relative to OUTPUT_DIR)
208EOF
209 ;
Steven Rostedtdbd37832011-11-23 16:00:48 -0500210$config_help{"BUILD_OPTIONS"} = << "EOF"
211 Options to add to \"make\" when building.
212 i.e. -j20
213EOF
214 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500215$config_help{"TARGET_IMAGE"} = << "EOF"
216 The place to put your image on the test machine.
217EOF
218 ;
219$config_help{"POWER_CYCLE"} = << "EOF"
220 A script or command to reboot the box.
221
222 Here is a digital loggers power switch example
223 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
224
225 Here is an example to reboot a virtual box on the current host
226 with the name "Guest".
227 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
228EOF
229 ;
230$config_help{"CONSOLE"} = << "EOF"
231 The script or command that reads the console
232
233 If you use ttywatch server, something like the following would work.
234CONSOLE = nc -d localhost 3001
235
236 For a virtual machine with guest name "Guest".
237CONSOLE = virsh console Guest
238EOF
239 ;
240$config_help{"LOCALVERSION"} = << "EOF"
241 Required version ending to differentiate the test
242 from other linux builds on the system.
243EOF
244 ;
245$config_help{"REBOOT_TYPE"} = << "EOF"
246 Way to reboot the box to the test kernel.
247 Only valid options so far are "grub" and "script".
248
249 If you specify grub, it will assume grub version 1
250 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
251 and select that target to reboot to the kernel. If this is not
252 your setup, then specify "script" and have a command or script
253 specified in REBOOT_SCRIPT to boot to the target.
254
255 The entry in /boot/grub/menu.lst must be entered in manually.
256 The test will not modify that file.
257EOF
258 ;
259$config_help{"GRUB_MENU"} = << "EOF"
260 The grub title name for the test kernel to boot
261 (Only mandatory if REBOOT_TYPE = grub)
262
263 Note, ktest.pl will not update the grub menu.lst, you need to
264 manually add an option for the test. ktest.pl will search
265 the grub menu.lst for this option to find what kernel to
266 reboot into.
267
268 For example, if in the /boot/grub/menu.lst the test kernel title has:
269 title Test Kernel
270 kernel vmlinuz-test
271 GRUB_MENU = Test Kernel
272EOF
273 ;
274$config_help{"REBOOT_SCRIPT"} = << "EOF"
275 A script to reboot the target into the test kernel
276 (Only mandatory if REBOOT_TYPE = script)
277EOF
278 ;
279
Steven Rostedtdad98752011-11-22 20:48:57 -0500280sub read_prompt {
281 my ($cancel, $prompt) = @_;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400282
283 my $ans;
284
285 for (;;) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500286 if ($cancel) {
287 print "$prompt [y/n/C] ";
288 } else {
289 print "$prompt [Y/n] ";
290 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400291 $ans = <STDIN>;
292 chomp $ans;
293 if ($ans =~ /^\s*$/) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500294 if ($cancel) {
295 $ans = "c";
296 } else {
297 $ans = "y";
298 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400299 }
300 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
Steven Rostedtdad98752011-11-22 20:48:57 -0500301 if ($cancel) {
302 last if ($ans =~ /^c$/i);
303 print "Please answer either 'y', 'n' or 'c'.\n";
304 } else {
305 print "Please answer either 'y' or 'n'.\n";
306 }
307 }
308 if ($ans =~ /^c/i) {
309 exit;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400310 }
311 if ($ans !~ /^y$/i) {
312 return 0;
313 }
314 return 1;
315}
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500316
Steven Rostedtdad98752011-11-22 20:48:57 -0500317sub read_yn {
318 my ($prompt) = @_;
319
320 return read_prompt 0, $prompt;
321}
322
323sub read_ync {
324 my ($prompt) = @_;
325
326 return read_prompt 1, $prompt;
327}
328
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500329sub get_ktest_config {
330 my ($config) = @_;
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400331 my $ans;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500332
333 return if (defined($opt{$config}));
334
335 if (defined($config_help{$config})) {
336 print "\n";
337 print $config_help{$config};
338 }
339
340 for (;;) {
341 print "$config = ";
Steven Rostedtdbd37832011-11-23 16:00:48 -0500342 if (defined($default{$config}) && length($default{$config})) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500343 print "\[$default{$config}\] ";
344 }
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400345 $ans = <STDIN>;
346 $ans =~ s/^\s*(.*\S)\s*$/$1/;
347 if ($ans =~ /^\s*$/) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500348 if ($default{$config}) {
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400349 $ans = $default{$config};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500350 } else {
351 print "Your answer can not be blank\n";
352 next;
353 }
354 }
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500355 $entered_configs{$config} = ${ans};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500356 last;
357 }
358}
359
360sub get_ktest_configs {
361 get_ktest_config("MACHINE");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500362 get_ktest_config("BUILD_DIR");
363 get_ktest_config("OUTPUT_DIR");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500364
Steven Rostedtdbd37832011-11-23 16:00:48 -0500365 if ($newconfig) {
366 get_ktest_config("BUILD_OPTIONS");
367 }
368
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500369 # options required for other than just building a kernel
370 if (!$buildonly) {
Steven Rostedt165708b2011-11-26 20:56:52 -0500371 get_ktest_config("POWER_CYCLE");
372 get_ktest_config("CONSOLE");
373 }
374
375 # options required for install and more
376 if ($buildonly != 1) {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500377 get_ktest_config("SSH_USER");
378 get_ktest_config("BUILD_TARGET");
379 get_ktest_config("TARGET_IMAGE");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500380 }
381
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500382 get_ktest_config("LOCALVERSION");
383
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500384 return if ($buildonly);
385
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500386 my $rtype = $opt{"REBOOT_TYPE"};
387
388 if (!defined($rtype)) {
389 if (!defined($opt{"GRUB_MENU"})) {
390 get_ktest_config("REBOOT_TYPE");
391 $rtype = $entered_configs{"REBOOT_TYPE"};
392 } else {
393 $rtype = "grub";
394 }
395 }
396
397 if ($rtype eq "grub") {
398 get_ktest_config("GRUB_MENU");
399 } else {
400 get_ktest_config("REBOOT_SCRIPT");
401 }
402}
403
Steven Rostedt77d942c2011-05-20 13:36:58 -0400404sub process_variables {
Steven Rostedt8d735212011-10-17 11:36:44 -0400405 my ($value, $remove_undef) = @_;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400406 my $retval = "";
407
408 # We want to check for '\', and it is just easier
409 # to check the previous characet of '$' and not need
410 # to worry if '$' is the first character. By adding
411 # a space to $value, we can just check [^\\]\$ and
412 # it will still work.
413 $value = " $value";
414
415 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
416 my $begin = $1;
417 my $var = $2;
418 my $end = $3;
419 # append beginning of value to retval
420 $retval = "$retval$begin";
421 if (defined($variable{$var})) {
422 $retval = "$retval$variable{$var}";
Steven Rostedt8d735212011-10-17 11:36:44 -0400423 } elsif (defined($remove_undef) && $remove_undef) {
424 # for if statements, any variable that is not defined,
425 # we simple convert to 0
426 $retval = "${retval}0";
Steven Rostedt77d942c2011-05-20 13:36:58 -0400427 } else {
428 # put back the origin piece.
429 $retval = "$retval\$\{$var\}";
430 }
431 $value = $end;
432 }
433 $retval = "$retval$value";
434
435 # remove the space added in the beginning
436 $retval =~ s/ //;
437
438 return "$retval"
439}
440
Steven Rostedta57419b2010-11-02 15:13:54 -0400441sub set_value {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400442 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
Steven Rostedta57419b2010-11-02 15:13:54 -0400443
Steven Rostedtcad96662011-12-22 11:32:52 -0500444 my $prvalue = process_variables($rvalue);
445
446 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500447 # Note if a test is something other than build, then we
448 # will need other manditory options.
Steven Rostedtcad96662011-12-22 11:32:52 -0500449 if ($prvalue ne "install") {
Steven Rostedt165708b2011-11-26 20:56:52 -0500450 $buildonly = 0;
451 } else {
452 # install still limits some manditory options.
453 $buildonly = 2;
454 }
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500455 }
456
Steven Rostedta57419b2010-11-02 15:13:54 -0400457 if (defined($opt{$lvalue})) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400458 if (!$override || defined(${$overrides}{$lvalue})) {
459 my $extra = "";
460 if ($override) {
461 $extra = "In the same override section!\n";
462 }
463 die "$name: $.: Option $lvalue defined more than once!\n$extra";
464 }
Steven Rostedtcad96662011-12-22 11:32:52 -0500465 ${$overrides}{$lvalue} = $prvalue;
Steven Rostedta57419b2010-11-02 15:13:54 -0400466 }
Steven Rostedt21a96792010-11-08 16:45:50 -0500467 if ($rvalue =~ /^\s*$/) {
468 delete $opt{$lvalue};
469 } else {
Steven Rostedtcad96662011-12-22 11:32:52 -0500470 $opt{$lvalue} = $prvalue;
Steven Rostedt21a96792010-11-08 16:45:50 -0500471 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400472}
473
Steven Rostedt77d942c2011-05-20 13:36:58 -0400474sub set_variable {
475 my ($lvalue, $rvalue) = @_;
476
477 if ($rvalue =~ /^\s*$/) {
478 delete $variable{$lvalue};
479 } else {
480 $rvalue = process_variables($rvalue);
481 $variable{$lvalue} = $rvalue;
482 }
483}
484
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400485sub process_compare {
486 my ($lval, $cmp, $rval) = @_;
487
488 # remove whitespace
489
490 $lval =~ s/^\s*//;
491 $lval =~ s/\s*$//;
492
493 $rval =~ s/^\s*//;
494 $rval =~ s/\s*$//;
495
496 if ($cmp eq "==") {
497 return $lval eq $rval;
498 } elsif ($cmp eq "!=") {
499 return $lval ne $rval;
500 }
501
502 my $statement = "$lval $cmp $rval";
503 my $ret = eval $statement;
504
505 # $@ stores error of eval
506 if ($@) {
507 return -1;
508 }
509
510 return $ret;
511}
512
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400513sub value_defined {
514 my ($val) = @_;
515
516 return defined($variable{$2}) ||
517 defined($opt{$2});
518}
519
Steven Rostedt8d735212011-10-17 11:36:44 -0400520my $d = 0;
521sub process_expression {
522 my ($name, $val) = @_;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400523
Steven Rostedt8d735212011-10-17 11:36:44 -0400524 my $c = $d++;
525
526 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
527 my $express = $1;
528
529 if (process_expression($name, $express)) {
530 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
531 } else {
532 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
533 }
534 }
535
536 $d--;
537 my $OR = "\\|\\|";
538 my $AND = "\\&\\&";
539
540 while ($val =~ s/^(.*?)($OR|$AND)//) {
541 my $express = $1;
542 my $op = $2;
543
544 if (process_expression($name, $express)) {
545 if ($op eq "||") {
546 return 1;
547 }
548 } else {
549 if ($op eq "&&") {
550 return 0;
551 }
552 }
553 }
Steven Rostedt45d73a52011-09-30 19:44:53 -0400554
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400555 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
556 my $ret = process_compare($1, $2, $3);
557 if ($ret < 0) {
558 die "$name: $.: Unable to process comparison\n";
559 }
560 return $ret;
561 }
562
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400563 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
564 if (defined $1) {
565 return !value_defined($2);
566 } else {
567 return value_defined($2);
568 }
569 }
570
Steven Rostedt45d73a52011-09-30 19:44:53 -0400571 if ($val =~ /^\s*0\s*$/) {
572 return 0;
573 } elsif ($val =~ /^\s*\d+\s*$/) {
574 return 1;
575 }
576
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400577 die ("$name: $.: Undefined content $val in if statement\n");
Steven Rostedt8d735212011-10-17 11:36:44 -0400578}
579
580sub process_if {
581 my ($name, $value) = @_;
582
583 # Convert variables and replace undefined ones with 0
584 my $val = process_variables($value, 1);
585 my $ret = process_expression $name, $val;
586
587 return $ret;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400588}
589
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400590sub __read_config {
591 my ($config, $current_test_num) = @_;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400592
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400593 my $in;
594 open($in, $config) || die "can't read file $config";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400595
Steven Rostedta57419b2010-11-02 15:13:54 -0400596 my $name = $config;
597 $name =~ s,.*/(.*),$1,;
598
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400599 my $test_num = $$current_test_num;
Steven Rostedta57419b2010-11-02 15:13:54 -0400600 my $default = 1;
601 my $repeat = 1;
602 my $num_tests_set = 0;
603 my $skip = 0;
604 my $rest;
Steven Rostedta9f84422011-10-17 11:06:29 -0400605 my $line;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400606 my $test_case = 0;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400607 my $if = 0;
608 my $if_set = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400609 my $override = 0;
610
611 my %overrides;
Steven Rostedta57419b2010-11-02 15:13:54 -0400612
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400613 while (<$in>) {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400614
615 # ignore blank lines and comments
616 next if (/^\s*$/ || /\s*\#/);
617
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400618 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400619
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400620 my $type = $1;
621 $rest = $2;
Steven Rostedta9f84422011-10-17 11:06:29 -0400622 $line = $2;
Steven Rostedta57419b2010-11-02 15:13:54 -0400623
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400624 my $old_test_num;
625 my $old_repeat;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400626 $override = 0;
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400627
628 if ($type eq "TEST_START") {
629
630 if ($num_tests_set) {
631 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
632 }
633
634 $old_test_num = $test_num;
635 $old_repeat = $repeat;
636
637 $test_num += $repeat;
638 $default = 0;
639 $repeat = 1;
640 } else {
641 $default = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400642 }
643
Steven Rostedta9f84422011-10-17 11:06:29 -0400644 # If SKIP is anywhere in the line, the command will be skipped
645 if ($rest =~ s/\s+SKIP\b//) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400646 $skip = 1;
647 } else {
Steven Rostedt0df213c2011-06-14 20:51:37 -0400648 $test_case = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400649 $skip = 0;
650 }
651
Steven Rostedta9f84422011-10-17 11:06:29 -0400652 if ($rest =~ s/\sELSE\b//) {
653 if (!$if) {
654 die "$name: $.: ELSE found with out matching IF section\n$_";
655 }
656 $if = 0;
657
658 if ($if_set) {
659 $skip = 1;
660 } else {
661 $skip = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400662 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400663 }
664
Steven Rostedta9f84422011-10-17 11:06:29 -0400665 if ($rest =~ s/\sIF\s+(.*)//) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400666 if (process_if($name, $1)) {
667 $if_set = 1;
668 } else {
669 $skip = 1;
670 }
671 $if = 1;
672 } else {
673 $if = 0;
Steven Rostedta9f84422011-10-17 11:06:29 -0400674 $if_set = 0;
Steven Rostedta57419b2010-11-02 15:13:54 -0400675 }
676
Steven Rostedta9f84422011-10-17 11:06:29 -0400677 if (!$skip) {
678 if ($type eq "TEST_START") {
679 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
680 $repeat = $1;
681 $repeat_tests{"$test_num"} = $repeat;
682 }
683 } elsif ($rest =~ s/\sOVERRIDE\b//) {
684 # DEFAULT only
685 $override = 1;
686 # Clear previous overrides
687 %overrides = ();
688 }
689 }
690
691 if (!$skip && $rest !~ /^\s*$/) {
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400692 die "$name: $.: Gargbage found after $type\n$_";
Steven Rostedta57419b2010-11-02 15:13:54 -0400693 }
694
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400695 if ($skip && $type eq "TEST_START") {
Steven Rostedta57419b2010-11-02 15:13:54 -0400696 $test_num = $old_test_num;
Steven Rostedte48c5292010-11-02 14:35:37 -0400697 $repeat = $old_repeat;
Steven Rostedta57419b2010-11-02 15:13:54 -0400698 }
699
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400700 } elsif (/^\s*ELSE\b(.*)$/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400701 if (!$if) {
702 die "$name: $.: ELSE found with out matching IF section\n$_";
703 }
704 $rest = $1;
705 if ($if_set) {
706 $skip = 1;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400707 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400708 } else {
709 $skip = 0;
710
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400711 if ($rest =~ /\sIF\s+(.*)/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400712 # May be a ELSE IF section.
713 if (!process_if($name, $1)) {
714 $skip = 1;
715 }
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400716 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400717 } else {
718 $if = 0;
719 }
720 }
721
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400722 if ($rest !~ /^\s*$/) {
723 die "$name: $.: Gargbage found after DEFAULTS\n$_";
724 }
725
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400726 } elsif (/^\s*INCLUDE\s+(\S+)/) {
727
728 next if ($skip);
729
730 if (!$default) {
731 die "$name: $.: INCLUDE can only be done in default sections\n$_";
732 }
733
734 my $file = process_variables($1);
735
736 if ($file !~ m,^/,) {
737 # check the path of the config file first
738 if ($config =~ m,(.*)/,) {
739 if (-f "$1/$file") {
740 $file = "$1/$file";
741 }
742 }
743 }
744
745 if ( ! -r $file ) {
746 die "$name: $.: Can't read file $file\n$_";
747 }
748
749 if (__read_config($file, \$test_num)) {
750 $test_case = 1;
751 }
752
Steven Rostedta57419b2010-11-02 15:13:54 -0400753 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
754
755 next if ($skip);
756
Steven Rostedt2545eb62010-11-02 15:01:32 -0400757 my $lvalue = $1;
758 my $rvalue = $2;
759
Steven Rostedta57419b2010-11-02 15:13:54 -0400760 if (!$default &&
761 ($lvalue eq "NUM_TESTS" ||
762 $lvalue eq "LOG_FILE" ||
763 $lvalue eq "CLEAR_LOG")) {
764 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400765 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400766
767 if ($lvalue eq "NUM_TESTS") {
768 if ($test_num) {
769 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
770 }
771 if (!$default) {
772 die "$name: $.: NUM_TESTS must be set in default section\n";
773 }
774 $num_tests_set = 1;
775 }
776
777 if ($default || $lvalue =~ /\[\d+\]$/) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400778 set_value($lvalue, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400779 } else {
780 my $val = "$lvalue\[$test_num\]";
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400781 set_value($val, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400782
783 if ($repeat > 1) {
784 $repeats{$val} = $repeat;
785 }
786 }
Steven Rostedt77d942c2011-05-20 13:36:58 -0400787 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
788 next if ($skip);
789
790 my $lvalue = $1;
791 my $rvalue = $2;
792
793 # process config variables.
794 # Config variables are only active while reading the
795 # config and can be defined anywhere. They also ignore
796 # TEST_START and DEFAULTS, but are skipped if they are in
797 # on of these sections that have SKIP defined.
798 # The save variable can be
799 # defined multiple times and the new one simply overrides
800 # the prevous one.
801 set_variable($lvalue, $rvalue);
802
Steven Rostedta57419b2010-11-02 15:13:54 -0400803 } else {
804 die "$name: $.: Garbage found in config\n$_";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400805 }
806 }
807
Steven Rostedta57419b2010-11-02 15:13:54 -0400808 if ($test_num) {
809 $test_num += $repeat - 1;
810 $opt{"NUM_TESTS"} = $test_num;
811 }
812
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400813 close($in);
814
815 $$current_test_num = $test_num;
816
817 return $test_case;
818}
819
Steven Rostedtc4261d02011-11-23 13:41:18 -0500820sub get_test_case {
821 print "What test case would you like to run?\n";
822 print " (build, install or boot)\n";
823 print " Other tests are available but require editing the config file\n";
824 my $ans = <STDIN>;
825 chomp $ans;
826 $default{"TEST_TYPE"} = $ans;
827}
828
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400829sub read_config {
830 my ($config) = @_;
831
832 my $test_case;
833 my $test_num = 0;
834
835 $test_case = __read_config $config, \$test_num;
836
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500837 # make sure we have all mandatory configs
838 get_ktest_configs;
839
Steven Rostedt0df213c2011-06-14 20:51:37 -0400840 # was a test specified?
841 if (!$test_case) {
842 print "No test case specified.\n";
Steven Rostedtc4261d02011-11-23 13:41:18 -0500843 get_test_case;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400844 }
845
Steven Rostedta75fece2010-11-02 14:58:27 -0400846 # set any defaults
847
848 foreach my $default (keys %default) {
849 if (!defined($opt{$default})) {
850 $opt{$default} = $default{$default};
851 }
852 }
Steven Rostedt2545eb62010-11-02 15:01:32 -0400853}
854
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400855sub __eval_option {
856 my ($option, $i) = @_;
857
858 # Add space to evaluate the character before $
859 $option = " $option";
860 my $retval = "";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530861 my $repeated = 0;
862 my $parent = 0;
863
864 foreach my $test (keys %repeat_tests) {
865 if ($i >= $test &&
866 $i < $test + $repeat_tests{$test}) {
867
868 $repeated = 1;
869 $parent = $test;
870 last;
871 }
872 }
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400873
874 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
875 my $start = $1;
876 my $var = $2;
877 my $end = $3;
878
879 # Append beginning of line
880 $retval = "$retval$start";
881
882 # If the iteration option OPT[$i] exists, then use that.
883 # otherwise see if the default OPT (without [$i]) exists.
884
885 my $o = "$var\[$i\]";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530886 my $parento = "$var\[$parent\]";
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400887
888 if (defined($opt{$o})) {
889 $o = $opt{$o};
890 $retval = "$retval$o";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530891 } elsif ($repeated && defined($opt{$parento})) {
892 $o = $opt{$parento};
893 $retval = "$retval$o";
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400894 } elsif (defined($opt{$var})) {
895 $o = $opt{$var};
896 $retval = "$retval$o";
897 } else {
898 $retval = "$retval\$\{$var\}";
899 }
900
901 $option = $end;
902 }
903
904 $retval = "$retval$option";
905
906 $retval =~ s/^ //;
907
908 return $retval;
909}
910
911sub eval_option {
912 my ($option, $i) = @_;
913
914 my $prev = "";
915
916 # Since an option can evaluate to another option,
917 # keep iterating until we do not evaluate any more
918 # options.
919 my $r = 0;
920 while ($prev ne $option) {
921 # Check for recursive evaluations.
922 # 100 deep should be more than enough.
923 if ($r++ > 100) {
924 die "Over 100 evaluations accurred with $option\n" .
925 "Check for recursive variables\n";
926 }
927 $prev = $option;
928 $option = __eval_option($option, $i);
929 }
930
931 return $option;
932}
933
Steven Rostedtd1e2f222010-11-08 16:39:57 -0500934sub _logit {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400935 if (defined($opt{"LOG_FILE"})) {
936 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
937 print OUT @_;
938 close(OUT);
939 }
940}
941
Steven Rostedtd1e2f222010-11-08 16:39:57 -0500942sub logit {
943 if (defined($opt{"LOG_FILE"})) {
944 _logit @_;
945 } else {
946 print @_;
947 }
948}
949
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400950sub doprint {
951 print @_;
Steven Rostedtd1e2f222010-11-08 16:39:57 -0500952 _logit @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400953}
954
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400955sub run_command;
Andrew Jones2728be42011-08-12 15:32:05 +0200956sub start_monitor;
957sub end_monitor;
958sub wait_for_monitor;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400959
960sub reboot {
Andrew Jones2728be42011-08-12 15:32:05 +0200961 my ($time) = @_;
962
Steven Rostedt2b803362011-09-30 18:00:23 -0400963 if (defined($time)) {
964 start_monitor;
965 # flush out current monitor
966 # May contain the reboot success line
967 wait_for_monitor 1;
968 }
969
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400970 # try to reboot normally
Steven Rostedte48c5292010-11-02 14:35:37 -0400971 if (run_command $reboot) {
Steven Rostedt576f6272010-11-02 14:58:38 -0400972 if (defined($powercycle_after_reboot)) {
973 sleep $powercycle_after_reboot;
974 run_command "$power_cycle";
975 }
976 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400977 # nope? power cycle it.
Steven Rostedta75fece2010-11-02 14:58:27 -0400978 run_command "$power_cycle";
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400979 }
Andrew Jones2728be42011-08-12 15:32:05 +0200980
981 if (defined($time)) {
Steven Rostedt2b803362011-09-30 18:00:23 -0400982 wait_for_monitor($time, $reboot_success_line);
Andrew Jones2728be42011-08-12 15:32:05 +0200983 end_monitor;
984 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400985}
986
Steven Rostedtbc7c5802011-12-22 16:29:10 -0500987sub reboot_to_good {
988 my ($time) = @_;
989
990 if (defined($switch_to_good)) {
991 run_command $switch_to_good;
992 return;
993 }
994
995 reboot $time;
996}
997
Steven Rostedt576f6272010-11-02 14:58:38 -0400998sub do_not_reboot {
999 my $i = $iteration;
1000
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001001 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -04001002 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1003 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1004}
1005
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001006sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001007 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001008
Steven Rostedt576f6272010-11-02 14:58:38 -04001009 my $i = $iteration;
1010
1011 if ($reboot_on_error && !do_not_reboot) {
1012
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001013 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001014 reboot_to_good;
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001015
Steven Rostedta75fece2010-11-02 14:58:27 -04001016 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001017 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001018 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001019 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001020
Steven Rostedtf80802c2011-03-07 13:18:47 -05001021 if (defined($opt{"LOG_FILE"})) {
1022 print " See $opt{LOG_FILE} for more info.\n";
1023 }
1024
Steven Rostedt576f6272010-11-02 14:58:38 -04001025 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001026}
1027
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001028sub open_console {
1029 my ($fp) = @_;
1030
1031 my $flags;
1032
Steven Rostedta75fece2010-11-02 14:58:27 -04001033 my $pid = open($fp, "$console|") or
1034 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001035
1036 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001037 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001038 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001039 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001040
1041 return $pid;
1042}
1043
1044sub close_console {
1045 my ($fp, $pid) = @_;
1046
1047 doprint "kill child process $pid\n";
1048 kill 2, $pid;
1049
1050 print "closing!\n";
1051 close($fp);
1052}
1053
1054sub start_monitor {
1055 if ($monitor_cnt++) {
1056 return;
1057 }
1058 $monitor_fp = \*MONFD;
1059 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001060
1061 return;
1062
1063 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001064}
1065
1066sub end_monitor {
1067 if (--$monitor_cnt) {
1068 return;
1069 }
1070 close_console($monitor_fp, $monitor_pid);
1071}
1072
1073sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001074 my ($time, $stop) = @_;
1075 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001076 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001077 my $booted = 0;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001078
Steven Rostedta75fece2010-11-02 14:58:27 -04001079 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001080
1081 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001082 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001083 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001084 last if (!defined($line));
1085 print "$line";
1086 $full_line .= $line;
1087
1088 if (defined($stop) && $full_line =~ /$stop/) {
1089 doprint "wait for monitor detected $stop\n";
1090 $booted = 1;
1091 }
1092
1093 if ($line =~ /\n/) {
1094 $full_line = "";
1095 }
1096 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001097 print "** Monitor flushed **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001098}
1099
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301100sub save_logs {
1101 my ($result, $basedir) = @_;
1102 my @t = localtime;
1103 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1104 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1105
1106 my $type = $build_type;
1107 if ($type =~ /useconfig/) {
1108 $type = "useconfig";
1109 }
1110
1111 my $dir = "$machine-$test_type-$type-$result-$date";
1112
1113 $dir = "$basedir/$dir";
1114
1115 if (!-d $dir) {
1116 mkpath($dir) or
1117 die "can't create $dir";
1118 }
1119
1120 my %files = (
1121 "config" => $output_config,
1122 "buildlog" => $buildlog,
1123 "dmesg" => $dmesg,
1124 "testlog" => $testlog,
1125 );
1126
1127 while (my ($name, $source) = each(%files)) {
1128 if (-f "$source") {
1129 cp "$source", "$dir/$name" or
1130 die "failed to copy $source";
1131 }
1132 }
1133
1134 doprint "*** Saved info to $dir ***\n";
1135}
1136
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001137sub fail {
1138
Steven Rostedta75fece2010-11-02 14:58:27 -04001139 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001140 dodie @_;
1141 }
1142
Steven Rostedta75fece2010-11-02 14:58:27 -04001143 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001144
Steven Rostedt576f6272010-11-02 14:58:38 -04001145 my $i = $iteration;
1146
Steven Rostedta75fece2010-11-02 14:58:27 -04001147 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001148 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001149 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001150 reboot_to_good $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001151 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001152
Steven Rostedt9064af52011-06-13 10:38:48 -04001153 my $name = "";
1154
1155 if (defined($test_name)) {
1156 $name = " ($test_name)";
1157 }
1158
Steven Rostedt576f6272010-11-02 14:58:38 -04001159 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1160 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001161 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001162 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1163 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001164
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301165 if (defined($store_failures)) {
1166 save_logs "fail", $store_failures;
1167 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001168
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001169 return 1;
1170}
1171
Steven Rostedt2545eb62010-11-02 15:01:32 -04001172sub run_command {
1173 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001174 my $dolog = 0;
1175 my $dord = 0;
1176 my $pid;
1177
Steven Rostedte48c5292010-11-02 14:35:37 -04001178 $command =~ s/\$SSH_USER/$ssh_user/g;
1179 $command =~ s/\$MACHINE/$machine/g;
1180
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001181 doprint("$command ... ");
1182
1183 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001184 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001185
1186 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001187 open(LOG, ">>$opt{LOG_FILE}") or
1188 dodie "failed to write to log";
1189 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001190 }
1191
1192 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001193 open (RD, ">$redirect") or
1194 dodie "failed to write to redirect $redirect";
1195 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001196 }
1197
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001198 while (<CMD>) {
1199 print LOG if ($dolog);
1200 print RD if ($dord);
1201 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001202
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001203 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001204 my $failed = $?;
1205
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001206 close(CMD);
1207 close(LOG) if ($dolog);
1208 close(RD) if ($dord);
1209
Steven Rostedt2545eb62010-11-02 15:01:32 -04001210 if ($failed) {
1211 doprint "FAILED!\n";
1212 } else {
1213 doprint "SUCCESS\n";
1214 }
1215
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001216 return !$failed;
1217}
1218
Steven Rostedte48c5292010-11-02 14:35:37 -04001219sub run_ssh {
1220 my ($cmd) = @_;
1221 my $cp_exec = $ssh_exec;
1222
1223 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1224 return run_command "$cp_exec";
1225}
1226
1227sub run_scp {
1228 my ($src, $dst) = @_;
1229 my $cp_scp = $scp_to_target;
1230
1231 $cp_scp =~ s/\$SRC_FILE/$src/g;
1232 $cp_scp =~ s/\$DST_FILE/$dst/g;
1233
1234 return run_command "$cp_scp";
1235}
1236
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001237sub get_grub_index {
1238
Steven Rostedta75fece2010-11-02 14:58:27 -04001239 if ($reboot_type ne "grub") {
1240 return;
1241 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001242 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001243
1244 doprint "Find grub menu ... ";
1245 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001246
1247 my $ssh_grub = $ssh_exec;
1248 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1249
1250 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001251 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001252
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001253 my $found = 0;
1254
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001255 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001256 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001257 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001258 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001259 last;
1260 } elsif (/^\s*title\s/) {
1261 $grub_number++;
1262 }
1263 }
1264 close(IN);
1265
Steven Rostedta75fece2010-11-02 14:58:27 -04001266 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001267 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001268 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001269}
1270
Steven Rostedt2545eb62010-11-02 15:01:32 -04001271sub wait_for_input
1272{
1273 my ($fp, $time) = @_;
1274 my $rin;
1275 my $ready;
1276 my $line;
1277 my $ch;
1278
1279 if (!defined($time)) {
1280 $time = $timeout;
1281 }
1282
1283 $rin = '';
1284 vec($rin, fileno($fp), 1) = 1;
1285 $ready = select($rin, undef, undef, $time);
1286
1287 $line = "";
1288
1289 # try to read one char at a time
1290 while (sysread $fp, $ch, 1) {
1291 $line .= $ch;
1292 last if ($ch eq "\n");
1293 }
1294
1295 if (!length($line)) {
1296 return undef;
1297 }
1298
1299 return $line;
1300}
1301
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001302sub reboot_to {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001303 if (defined($switch_to_test)) {
1304 run_command $switch_to_test;
1305 }
1306
Steven Rostedta75fece2010-11-02 14:58:27 -04001307 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001308 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1309 reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -04001310 return;
1311 }
1312
1313 run_command "$reboot_script";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001314}
1315
Steven Rostedta57419b2010-11-02 15:13:54 -04001316sub get_sha1 {
1317 my ($commit) = @_;
1318
1319 doprint "git rev-list --max-count=1 $commit ... ";
1320 my $sha1 = `git rev-list --max-count=1 $commit`;
1321 my $ret = $?;
1322
1323 logit $sha1;
1324
1325 if ($ret) {
1326 doprint "FAILED\n";
1327 dodie "Failed to get git $commit";
1328 }
1329
1330 print "SUCCESS\n";
1331
1332 chomp $sha1;
1333
1334 return $sha1;
1335}
1336
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001337sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001338 my $booted = 0;
1339 my $bug = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001340 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001341 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001342
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001343 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001344
1345 my $line;
1346 my $full_line = "";
1347
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001348 open(DMESG, "> $dmesg") or
1349 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001350
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001351 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001352
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001353 my $success_start;
1354 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001355 my $monitor_start = time;
1356 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001357 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001358
Steven Rostedt2d01b262011-03-08 09:47:54 -05001359 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001360
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001361 if ($bug && defined($stop_after_failure) &&
1362 $stop_after_failure >= 0) {
1363 my $time = $stop_after_failure - (time - $failure_start);
1364 $line = wait_for_input($monitor_fp, $time);
1365 if (!defined($line)) {
1366 doprint "bug timed out after $booted_timeout seconds\n";
1367 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1368 last;
1369 }
1370 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001371 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001372 if (!defined($line)) {
1373 my $s = $booted_timeout == 1 ? "" : "s";
1374 doprint "Successful boot found: break after $booted_timeout second$s\n";
1375 last;
1376 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001377 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001378 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001379 if (!defined($line)) {
1380 my $s = $timeout == 1 ? "" : "s";
1381 doprint "Timed out after $timeout second$s\n";
1382 last;
1383 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001384 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001385
Steven Rostedt2545eb62010-11-02 15:01:32 -04001386 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001387 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001388
1389 # we are not guaranteed to get a full line
1390 $full_line .= $line;
1391
Steven Rostedta75fece2010-11-02 14:58:27 -04001392 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001393 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001394 $success_start = time;
1395 }
1396
1397 if ($booted && defined($stop_after_success) &&
1398 $stop_after_success >= 0) {
1399 my $now = time;
1400 if ($now - $success_start >= $stop_after_success) {
1401 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1402 last;
1403 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001404 }
1405
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001406 if ($full_line =~ /\[ backtrace testing \]/) {
1407 $skip_call_trace = 1;
1408 }
1409
Steven Rostedt2545eb62010-11-02 15:01:32 -04001410 if ($full_line =~ /call trace:/i) {
Steven Rostedt46519202011-03-08 09:40:31 -05001411 if (!$bug && !$skip_call_trace) {
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001412 $bug = 1;
1413 $failure_start = time;
1414 }
1415 }
1416
1417 if ($bug && defined($stop_after_failure) &&
1418 $stop_after_failure >= 0) {
1419 my $now = time;
1420 if ($now - $failure_start >= $stop_after_failure) {
1421 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1422 last;
1423 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001424 }
1425
1426 if ($full_line =~ /\[ end of backtrace testing \]/) {
1427 $skip_call_trace = 0;
1428 }
1429
1430 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001431 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001432 $bug = 1;
1433 }
1434
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001435 # Detect triple faults by testing the banner
1436 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1437 if ($1 eq $version) {
1438 $version_found = 1;
1439 } elsif ($version_found && $detect_triplefault) {
1440 # We already booted into the kernel we are testing,
1441 # but now we booted into another kernel?
1442 # Consider this a triple fault.
1443 doprint "Aleady booted in Linux kernel $version, but now\n";
1444 doprint "we booted into Linux kernel $1.\n";
1445 doprint "Assuming that this is a triple fault.\n";
1446 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1447 last;
1448 }
1449 }
1450
Steven Rostedt2545eb62010-11-02 15:01:32 -04001451 if ($line =~ /\n/) {
1452 $full_line = "";
1453 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001454
1455 if ($stop_test_after > 0 && !$booted && !$bug) {
1456 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001457 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001458 $done = 1;
1459 }
1460 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001461 }
1462
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001463 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001464
Steven Rostedt2545eb62010-11-02 15:01:32 -04001465 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001466 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001467 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001468 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001469
Steven Rostedta75fece2010-11-02 14:58:27 -04001470 if (!$booted) {
1471 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001472 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001473 }
1474
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001475 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001476}
1477
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001478sub eval_kernel_version {
1479 my ($option) = @_;
1480
1481 $option =~ s/\$KERNEL_VERSION/$version/g;
1482
1483 return $option;
1484}
1485
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001486sub do_post_install {
1487
1488 return if (!defined($post_install));
1489
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001490 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001491 run_command "$cp_post_install" or
1492 dodie "Failed to run post install";
1493}
1494
Steven Rostedt2545eb62010-11-02 15:01:32 -04001495sub install {
1496
Steven Rostedte0a87422011-09-30 17:50:48 -04001497 return if ($no_install);
1498
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001499 my $cp_target = eval_kernel_version $target_image;
1500
1501 run_scp "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001502 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001503
1504 my $install_mods = 0;
1505
1506 # should we process modules?
1507 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001508 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001509 while (<IN>) {
1510 if (/CONFIG_MODULES(=y)?/) {
1511 $install_mods = 1 if (defined($1));
1512 last;
1513 }
1514 }
1515 close(IN);
1516
1517 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001518 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001519 doprint "No modules needed\n";
1520 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001521 }
1522
Steven Rostedta75fece2010-11-02 14:58:27 -04001523 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001524 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001525
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001526 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001527 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001528
Steven Rostedte48c5292010-11-02 14:35:37 -04001529 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001530 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001531
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001532 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001533 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001534 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001535
Steven Rostedte48c5292010-11-02 14:35:37 -04001536 run_scp "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001537 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001538
Steven Rostedta75fece2010-11-02 14:58:27 -04001539 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001540
Steven Rostedte7b13442011-06-14 20:44:36 -04001541 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001542 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001543
Steven Rostedte48c5292010-11-02 14:35:37 -04001544 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001545
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001546 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001547}
1548
Steven Rostedtddf607e2011-06-14 20:49:13 -04001549sub get_version {
1550 # get the release name
1551 doprint "$make kernelrelease ... ";
1552 $version = `$make kernelrelease | tail -1`;
1553 chomp($version);
1554 doprint "$version\n";
1555}
1556
1557sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001558 # Make sure the stable kernel has finished booting
1559 start_monitor;
1560 wait_for_monitor 5;
1561 end_monitor;
1562
Steven Rostedtddf607e2011-06-14 20:49:13 -04001563 get_grub_index;
1564 get_version;
1565 install;
1566
1567 start_monitor;
1568 return monitor;
1569}
1570
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001571sub check_buildlog {
1572 my ($patch) = @_;
1573
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001574 my @files = `git show $patch | diffstat -l`;
1575
1576 open(IN, "git show $patch |") or
1577 dodie "failed to show $patch";
1578 while (<IN>) {
1579 if (m,^--- a/(.*),) {
1580 chomp $1;
1581 $files[$#files] = $1;
1582 }
1583 }
1584 close(IN);
1585
1586 open(IN, $buildlog) or dodie "Can't open $buildlog";
1587 while (<IN>) {
1588 if (/^\s*(.*?):.*(warning|error)/) {
1589 my $err = $1;
1590 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001591 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001592 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001593 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001594 }
1595 }
1596 }
1597 }
1598 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001599
1600 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001601}
1602
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001603sub apply_min_config {
1604 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05001605
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001606 # Read the config file and remove anything that
1607 # is in the force_config hash (from minconfig and others)
1608 # then add the force config back.
1609
1610 doprint "Applying minimum configurations into $output_config.new\n";
1611
1612 open (OUT, ">$outconfig") or
1613 dodie "Can't create $outconfig";
1614
1615 if (-f $output_config) {
1616 open (IN, $output_config) or
1617 dodie "Failed to open $output_config";
1618 while (<IN>) {
1619 if (/^(# )?(CONFIG_[^\s=]*)/) {
1620 next if (defined($force_config{$2}));
1621 }
1622 print OUT;
1623 }
1624 close IN;
1625 }
1626 foreach my $config (keys %force_config) {
1627 print OUT "$force_config{$config}\n";
1628 }
1629 close OUT;
1630
1631 run_command "mv $outconfig $output_config";
1632}
1633
1634sub make_oldconfig {
1635
Steven Rostedt4c4ab122011-07-15 21:16:17 -04001636 my @force_list = keys %force_config;
1637
1638 if ($#force_list >= 0) {
1639 apply_min_config;
1640 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001641
1642 if (!run_command "$make oldnoconfig") {
Steven Rostedt612b9e92011-03-07 13:27:43 -05001643 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1644 # try a yes '' | oldconfig
1645 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001646 run_command "yes '' | $make oldconfig" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001647 dodie "failed make config oldconfig";
1648 }
1649}
1650
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001651# read a config file and use this to force new configs.
1652sub load_force_config {
1653 my ($config) = @_;
1654
1655 open(IN, $config) or
1656 dodie "failed to read $config";
1657 while (<IN>) {
1658 chomp;
1659 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1660 $force_config{$1} = $_;
1661 } elsif (/^# (CONFIG_\S*) is not set/) {
1662 $force_config{$1} = $_;
1663 }
1664 }
1665 close IN;
1666}
1667
Steven Rostedt2545eb62010-11-02 15:01:32 -04001668sub build {
1669 my ($type) = @_;
1670
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001671 unlink $buildlog;
1672
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001673 # Failed builds should not reboot the target
1674 my $save_no_reboot = $no_reboot;
1675 $no_reboot = 1;
1676
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001677 if (defined($pre_build)) {
1678 my $ret = run_command $pre_build;
1679 if (!$ret && defined($pre_build_die) &&
1680 $pre_build_die) {
1681 dodie "failed to pre_build\n";
1682 }
1683 }
1684
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001685 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001686 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001687 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001688
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001689 $type = "oldconfig";
1690 }
1691
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001692 # old config can ask questions
1693 if ($type eq "oldconfig") {
Steven Rostedt9386c6a2010-11-08 16:35:48 -05001694 $type = "oldnoconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001695
1696 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001697 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001698
Andrew Jones13488232011-08-12 15:32:04 +02001699 if (!$noclean) {
1700 run_command "mv $output_config $outputdir/config_temp" or
1701 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001702
Andrew Jones13488232011-08-12 15:32:04 +02001703 run_command "$make mrproper" or dodie "make mrproper";
1704
1705 run_command "mv $outputdir/config_temp $output_config" or
1706 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001707 }
1708
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001709 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001710 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001711 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001712 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001713 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001714
1715 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04001716 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1717 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001718 close(OUT);
1719
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001720 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001721 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001722 }
1723
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001724 if ($type ne "oldnoconfig") {
1725 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001726 dodie "failed make config";
1727 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001728 # Run old config regardless, to enforce min configurations
1729 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001730
Steven Rostedta75fece2010-11-02 14:58:27 -04001731 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001732 my $build_ret = run_command "$make $build_options";
1733 undef $redirect;
1734
1735 if (defined($post_build)) {
1736 my $ret = run_command $post_build;
1737 if (!$ret && defined($post_build_die) &&
1738 $post_build_die) {
1739 dodie "failed to post_build\n";
1740 }
1741 }
1742
1743 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001744 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001745 if ($in_bisect) {
1746 $no_reboot = $save_no_reboot;
1747 return 0;
1748 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001749 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001750 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001751
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001752 $no_reboot = $save_no_reboot;
1753
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001754 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001755}
1756
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001757sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04001758 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001759 if (defined($poweroff_after_halt)) {
1760 sleep $poweroff_after_halt;
1761 run_command "$power_off";
1762 }
1763 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001764 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04001765 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001766 }
1767}
1768
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001769sub success {
1770 my ($i) = @_;
1771
Steven Rostedte48c5292010-11-02 14:35:37 -04001772 $successes++;
1773
Steven Rostedt9064af52011-06-13 10:38:48 -04001774 my $name = "";
1775
1776 if (defined($test_name)) {
1777 $name = " ($test_name)";
1778 }
1779
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001780 doprint "\n\n*******************************************\n";
1781 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001782 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001783 doprint "*******************************************\n";
1784 doprint "*******************************************\n";
1785
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301786 if (defined($store_successes)) {
1787 save_logs "success", $store_successes;
1788 }
1789
Steven Rostedt576f6272010-11-02 14:58:38 -04001790 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001791 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001792 reboot_to_good $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001793 }
1794}
1795
Steven Rostedtc960bb92011-03-08 09:22:39 -05001796sub answer_bisect {
1797 for (;;) {
1798 doprint "Pass or fail? [p/f]";
1799 my $ans = <STDIN>;
1800 chomp $ans;
1801 if ($ans eq "p" || $ans eq "P") {
1802 return 1;
1803 } elsif ($ans eq "f" || $ans eq "F") {
1804 return 0;
1805 } else {
1806 print "Please answer 'P' or 'F'\n";
1807 }
1808 }
1809}
1810
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001811sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001812 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001813
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001814 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04001815 $reboot_on_error = 0;
1816 $poweroff_on_error = 0;
1817 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001818
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301819 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001820 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301821 undef $redirect;
1822
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001823 exit $failed;
1824}
1825
1826my $child_done;
1827
1828sub child_finished {
1829 $child_done = 1;
1830}
1831
1832sub do_run_test {
1833 my $child_pid;
1834 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001835 my $line;
1836 my $full_line;
1837 my $bug = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001838
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001839 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001840
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001841 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001842
1843 $child_done = 0;
1844
1845 $SIG{CHLD} = qw(child_finished);
1846
1847 $child_pid = fork;
1848
1849 child_run_test if (!$child_pid);
1850
1851 $full_line = "";
1852
1853 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001854 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001855 if (defined($line)) {
1856
1857 # we are not guaranteed to get a full line
1858 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05001859 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001860
1861 if ($full_line =~ /call trace:/i) {
1862 $bug = 1;
1863 }
1864
1865 if ($full_line =~ /Kernel panic -/) {
1866 $bug = 1;
1867 }
1868
1869 if ($line =~ /\n/) {
1870 $full_line = "";
1871 }
1872 }
1873 } while (!$child_done && !$bug);
1874
1875 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05001876 my $failure_start = time;
1877 my $now;
1878 do {
1879 $line = wait_for_input($monitor_fp, 1);
1880 if (defined($line)) {
1881 doprint $line;
1882 }
1883 $now = time;
1884 if ($now - $failure_start >= $stop_after_failure) {
1885 last;
1886 }
1887 } while (defined($line));
1888
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001889 doprint "Detected kernel crash!\n";
1890 # kill the child with extreme prejudice
1891 kill 9, $child_pid;
1892 }
1893
1894 waitpid $child_pid, 0;
1895 $child_exit = $?;
1896
Steven Rostedtc5dacb82011-12-22 12:43:57 -05001897 if (!$bug && $in_bisect) {
1898 if (defined($bisect_ret_good)) {
1899 if ($child_exit == $bisect_ret_good) {
1900 return 1;
1901 }
1902 }
1903 if (defined($bisect_ret_skip)) {
1904 if ($child_exit == $bisect_ret_skip) {
1905 return -1;
1906 }
1907 }
1908 if (defined($bisect_ret_abort)) {
1909 if ($child_exit == $bisect_ret_abort) {
1910 fail "test abort" and return -2;
1911 }
1912 }
1913 if (defined($bisect_ret_bad)) {
1914 if ($child_exit == $bisect_ret_skip) {
1915 return 0;
1916 }
1917 }
1918 if (defined($bisect_ret_default)) {
1919 if ($bisect_ret_default eq "good") {
1920 return 1;
1921 } elsif ($bisect_ret_default eq "bad") {
1922 return 0;
1923 } elsif ($bisect_ret_default eq "skip") {
1924 return -1;
1925 } elsif ($bisect_ret_default eq "abort") {
1926 return -2;
1927 } else {
1928 fail "unknown default action: $bisect_ret_default"
1929 and return -2;
1930 }
1931 }
1932 }
1933
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001934 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001935 return 0 if $in_bisect;
1936 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001937 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001938 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001939}
1940
Steven Rostedta75fece2010-11-02 14:58:27 -04001941sub run_git_bisect {
1942 my ($command) = @_;
1943
1944 doprint "$command ... ";
1945
1946 my $output = `$command 2>&1`;
1947 my $ret = $?;
1948
1949 logit $output;
1950
1951 if ($ret) {
1952 doprint "FAILED\n";
1953 dodie "Failed to git bisect";
1954 }
1955
1956 doprint "SUCCESS\n";
1957 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1958 doprint "$1 [$2]\n";
1959 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05001960 $bisect_bad_commit = $1;
Steven Rostedta75fece2010-11-02 14:58:27 -04001961 doprint "Found bad commit... $1\n";
1962 return 0;
1963 } else {
1964 # we already logged it, just print it now.
1965 print $output;
1966 }
1967
1968 return 1;
1969}
1970
Steven Rostedtc23dca72011-03-08 09:26:31 -05001971sub bisect_reboot {
1972 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001973 reboot_to_good $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05001974}
1975
1976# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05001977sub run_bisect_test {
1978 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001979
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001980 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001981 my $result;
1982 my $output;
1983 my $ret;
1984
Steven Rostedt0a05c762010-11-08 11:14:10 -05001985 $in_bisect = 1;
1986
1987 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001988
1989 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05001990 if ($failed && $bisect_skip) {
1991 $in_bisect = 0;
1992 return -1;
1993 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001994 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001995
1996 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04001997 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001998
1999 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002000 if ($failed && $bisect_skip) {
2001 end_monitor;
2002 bisect_reboot;
2003 $in_bisect = 0;
2004 return -1;
2005 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002006 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002007
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002008 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002009 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002010 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002011 }
2012
2013 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002014 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002015 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002016 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002017 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04002018
2019 # reboot the box to a kernel we can ssh to
2020 if ($type ne "build") {
2021 bisect_reboot;
2022 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002023 $in_bisect = 0;
2024
2025 return $result;
2026}
2027
2028sub run_bisect {
2029 my ($type) = @_;
2030 my $buildtype = "oldconfig";
2031
2032 # We should have a minconfig to use?
2033 if (defined($minconfig)) {
2034 $buildtype = "useconfig:$minconfig";
2035 }
2036
2037 my $ret = run_bisect_test $type, $buildtype;
2038
Steven Rostedtc960bb92011-03-08 09:22:39 -05002039 if ($bisect_manual) {
2040 $ret = answer_bisect;
2041 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002042
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002043 # Are we looking for where it worked, not failed?
2044 if ($reverse_bisect) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002045 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002046 }
2047
Steven Rostedtc23dca72011-03-08 09:26:31 -05002048 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002049 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002050 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002051 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002052 } elsif ($bisect_skip) {
2053 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2054 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002055 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002056}
2057
Steven Rostedtdad98752011-11-22 20:48:57 -05002058sub update_bisect_replay {
2059 my $tmp_log = "$tmpdir/ktest_bisect_log";
2060 run_command "git bisect log > $tmp_log" or
2061 die "can't create bisect log";
2062 return $tmp_log;
2063}
2064
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002065sub bisect {
2066 my ($i) = @_;
2067
2068 my $result;
2069
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002070 die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2071 die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
2072 die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002073
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002074 my $good = $bisect_good;
2075 my $bad = $bisect_bad;
2076 my $type = $bisect_type;
2077 my $start = $bisect_start;
2078 my $replay = $bisect_replay;
2079 my $start_files = $bisect_files;
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002080
2081 if (defined($start_files)) {
2082 $start_files = " -- " . $start_files;
2083 } else {
2084 $start_files = "";
2085 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002086
Steven Rostedta57419b2010-11-02 15:13:54 -04002087 # convert to true sha1's
2088 $good = get_sha1($good);
2089 $bad = get_sha1($bad);
2090
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002091 if (defined($bisect_reverse) && $bisect_reverse == 1) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002092 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2093 $reverse_bisect = 1;
2094 } else {
2095 $reverse_bisect = 0;
2096 }
2097
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002098 # Can't have a test without having a test to run
2099 if ($type eq "test" && !defined($run_test)) {
2100 $type = "boot";
2101 }
2102
Steven Rostedtdad98752011-11-22 20:48:57 -05002103 # Check if a bisect was running
2104 my $bisect_start_file = "$builddir/.git/BISECT_START";
2105
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002106 my $check = $bisect_check;
Steven Rostedtdad98752011-11-22 20:48:57 -05002107 my $do_check = defined($check) && $check ne "0";
2108
2109 if ( -f $bisect_start_file ) {
2110 print "Bisect in progress found\n";
2111 if ($do_check) {
2112 print " If you say yes, then no checks of good or bad will be done\n";
2113 }
2114 if (defined($replay)) {
2115 print "** BISECT_REPLAY is defined in config file **";
2116 print " Ignore config option and perform new git bisect log?\n";
2117 if (read_ync " (yes, no, or cancel) ") {
2118 $replay = update_bisect_replay;
2119 $do_check = 0;
2120 }
2121 } elsif (read_yn "read git log and continue?") {
2122 $replay = update_bisect_replay;
2123 $do_check = 0;
2124 }
2125 }
2126
2127 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002128
2129 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002130 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002131
2132 if ($check ne "good") {
2133 doprint "TESTING BISECT BAD [$bad]\n";
2134 run_command "git checkout $bad" or
2135 die "Failed to checkout $bad";
2136
2137 $result = run_bisect $type;
2138
2139 if ($result ne "bad") {
2140 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2141 }
2142 }
2143
2144 if ($check ne "bad") {
2145 doprint "TESTING BISECT GOOD [$good]\n";
2146 run_command "git checkout $good" or
2147 die "Failed to checkout $good";
2148
2149 $result = run_bisect $type;
2150
2151 if ($result ne "good") {
2152 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2153 }
2154 }
2155
2156 # checkout where we started
2157 run_command "git checkout $head" or
2158 die "Failed to checkout $head";
2159 }
2160
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002161 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002162 dodie "could not start bisect";
2163
2164 run_command "git bisect good $good" or
2165 dodie "could not set bisect good to $good";
2166
2167 run_git_bisect "git bisect bad $bad" or
2168 dodie "could not set bisect bad to $bad";
2169
2170 if (defined($replay)) {
2171 run_command "git bisect replay $replay" or
2172 dodie "failed to run replay";
2173 }
2174
2175 if (defined($start)) {
2176 run_command "git checkout $start" or
2177 dodie "failed to checkout $start";
2178 }
2179
2180 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002181 do {
2182 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002183 $test = run_git_bisect "git bisect $result";
2184 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002185
2186 run_command "git bisect log" or
2187 dodie "could not capture git bisect log";
2188
2189 run_command "git bisect reset" or
2190 dodie "could not reset git bisect";
2191
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002192 doprint "Bad commit was [$bisect_bad_commit]\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002193
Steven Rostedt0a05c762010-11-08 11:14:10 -05002194 success $i;
2195}
2196
2197my %config_ignore;
2198my %config_set;
2199
2200my %config_list;
2201my %null_config;
2202
2203my %dependency;
2204
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002205sub assign_configs {
2206 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002207
2208 open (IN, $config)
2209 or dodie "Failed to read $config";
2210
2211 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002212 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002213 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002214 }
2215 }
2216
2217 close(IN);
2218}
2219
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002220sub process_config_ignore {
2221 my ($config) = @_;
2222
2223 assign_configs \%config_ignore, $config;
2224}
2225
Steven Rostedt0a05c762010-11-08 11:14:10 -05002226sub read_current_config {
2227 my ($config_ref) = @_;
2228
2229 %{$config_ref} = ();
2230 undef %{$config_ref};
2231
2232 my @key = keys %{$config_ref};
2233 if ($#key >= 0) {
2234 print "did not delete!\n";
2235 exit;
2236 }
2237 open (IN, "$output_config");
2238
2239 while (<IN>) {
2240 if (/^(CONFIG\S+)=(.*)/) {
2241 ${$config_ref}{$1} = $2;
2242 }
2243 }
2244 close(IN);
2245}
2246
2247sub get_dependencies {
2248 my ($config) = @_;
2249
2250 my $arr = $dependency{$config};
2251 if (!defined($arr)) {
2252 return ();
2253 }
2254
2255 my @deps = @{$arr};
2256
2257 foreach my $dep (@{$arr}) {
2258 print "ADD DEP $dep\n";
2259 @deps = (@deps, get_dependencies $dep);
2260 }
2261
2262 return @deps;
2263}
2264
2265sub create_config {
2266 my @configs = @_;
2267
2268 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2269
2270 foreach my $config (@configs) {
2271 print OUT "$config_set{$config}\n";
2272 my @deps = get_dependencies $config;
2273 foreach my $dep (@deps) {
2274 print OUT "$config_set{$dep}\n";
2275 }
2276 }
2277
2278 foreach my $config (keys %config_ignore) {
2279 print OUT "$config_ignore{$config}\n";
2280 }
2281 close(OUT);
2282
2283# exit;
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002284 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002285}
2286
2287sub compare_configs {
2288 my (%a, %b) = @_;
2289
2290 foreach my $item (keys %a) {
2291 if (!defined($b{$item})) {
2292 print "diff $item\n";
2293 return 1;
2294 }
2295 delete $b{$item};
2296 }
2297
2298 my @keys = keys %b;
2299 if ($#keys) {
2300 print "diff2 $keys[0]\n";
2301 }
2302 return -1 if ($#keys >= 0);
2303
2304 return 0;
2305}
2306
2307sub run_config_bisect_test {
2308 my ($type) = @_;
2309
2310 return run_bisect_test $type, "oldconfig";
2311}
2312
2313sub process_passed {
2314 my (%configs) = @_;
2315
2316 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2317 # Passed! All these configs are part of a good compile.
2318 # Add them to the min options.
2319 foreach my $config (keys %configs) {
2320 if (defined($config_list{$config})) {
2321 doprint " removing $config\n";
2322 $config_ignore{$config} = $config_list{$config};
2323 delete $config_list{$config};
2324 }
2325 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002326 doprint "config copied to $outputdir/config_good\n";
2327 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002328}
2329
2330sub process_failed {
2331 my ($config) = @_;
2332
2333 doprint "\n\n***************************************\n";
2334 doprint "Found bad config: $config\n";
2335 doprint "***************************************\n\n";
2336}
2337
2338sub run_config_bisect {
2339
2340 my @start_list = keys %config_list;
2341
2342 if ($#start_list < 0) {
2343 doprint "No more configs to test!!!\n";
2344 return -1;
2345 }
2346
2347 doprint "***** RUN TEST ***\n";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002348 my $type = $config_bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002349 my $ret;
2350 my %current_config;
2351
2352 my $count = $#start_list + 1;
2353 doprint " $count configs to test\n";
2354
2355 my $half = int($#start_list / 2);
2356
2357 do {
2358 my @tophalf = @start_list[0 .. $half];
2359
2360 create_config @tophalf;
2361 read_current_config \%current_config;
2362
2363 $count = $#tophalf + 1;
2364 doprint "Testing $count configs\n";
2365 my $found = 0;
2366 # make sure we test something
2367 foreach my $config (@tophalf) {
2368 if (defined($current_config{$config})) {
2369 logit " $config\n";
2370 $found = 1;
2371 }
2372 }
2373 if (!$found) {
2374 # try the other half
2375 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002376 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedt0a05c762010-11-08 11:14:10 -05002377 create_config @tophalf;
2378 read_current_config \%current_config;
2379 foreach my $config (@tophalf) {
2380 if (defined($current_config{$config})) {
2381 logit " $config\n";
2382 $found = 1;
2383 }
2384 }
2385 if (!$found) {
2386 doprint "Failed: Can't make new config with current configs\n";
2387 foreach my $config (@start_list) {
2388 doprint " CONFIG: $config\n";
2389 }
2390 return -1;
2391 }
2392 $count = $#tophalf + 1;
2393 doprint "Testing $count configs\n";
2394 }
2395
2396 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002397 if ($bisect_manual) {
2398 $ret = answer_bisect;
2399 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002400 if ($ret) {
2401 process_passed %current_config;
2402 return 0;
2403 }
2404
2405 doprint "This config had a failure.\n";
2406 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002407 doprint "config copied to $outputdir/config_bad\n";
2408 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002409
2410 # A config exists in this group that was bad.
2411 foreach my $config (keys %config_list) {
2412 if (!defined($current_config{$config})) {
2413 doprint " removing $config\n";
2414 delete $config_list{$config};
2415 }
2416 }
2417
2418 @start_list = @tophalf;
2419
2420 if ($#start_list == 0) {
2421 process_failed $start_list[0];
2422 return 1;
2423 }
2424
2425 # remove half the configs we are looking at and see if
2426 # they are good.
2427 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002428 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002429
Steven Rostedtc960bb92011-03-08 09:22:39 -05002430 # we found a single config, try it again unless we are running manually
2431
2432 if ($bisect_manual) {
2433 process_failed $start_list[0];
2434 return 1;
2435 }
2436
Steven Rostedt0a05c762010-11-08 11:14:10 -05002437 my @tophalf = @start_list[0 .. 0];
2438
2439 $ret = run_config_bisect_test $type;
2440 if ($ret) {
2441 process_passed %current_config;
2442 return 0;
2443 }
2444
2445 process_failed $start_list[0];
2446 return 1;
2447}
2448
2449sub config_bisect {
2450 my ($i) = @_;
2451
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002452 my $start_config = $config_bisect;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002453
2454 my $tmpconfig = "$tmpdir/use_config";
2455
Steven Rostedt30f75da2011-06-13 10:35:35 -04002456 if (defined($config_bisect_good)) {
2457 process_config_ignore $config_bisect_good;
2458 }
2459
Steven Rostedt0a05c762010-11-08 11:14:10 -05002460 # Make the file with the bad config and the min config
2461 if (defined($minconfig)) {
2462 # read the min config for things to ignore
2463 run_command "cp $minconfig $tmpconfig" or
2464 dodie "failed to copy $minconfig to $tmpconfig";
2465 } else {
2466 unlink $tmpconfig;
2467 }
2468
Steven Rostedt0a05c762010-11-08 11:14:10 -05002469 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002470 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002471 process_config_ignore $tmpconfig;
2472 }
2473
2474 # now process the start config
2475 run_command "cp $start_config $output_config" or
2476 dodie "failed to copy $start_config to $output_config";
2477
2478 # read directly what we want to check
2479 my %config_check;
2480 open (IN, $output_config)
2481 or dodie "faied to open $output_config";
2482
2483 while (<IN>) {
2484 if (/^((CONFIG\S*)=.*)/) {
2485 $config_check{$2} = $1;
2486 }
2487 }
2488 close(IN);
2489
Steven Rostedt250bae82011-07-15 22:05:59 -04002490 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002491 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002492
2493 # check to see what we lost (or gained)
2494 open (IN, $output_config)
2495 or dodie "Failed to read $start_config";
2496
2497 my %removed_configs;
2498 my %added_configs;
2499
2500 while (<IN>) {
2501 if (/^((CONFIG\S*)=.*)/) {
2502 # save off all options
2503 $config_set{$2} = $1;
2504 if (defined($config_check{$2})) {
2505 if (defined($config_ignore{$2})) {
2506 $removed_configs{$2} = $1;
2507 } else {
2508 $config_list{$2} = $1;
2509 }
2510 } elsif (!defined($config_ignore{$2})) {
2511 $added_configs{$2} = $1;
2512 $config_list{$2} = $1;
2513 }
2514 }
2515 }
2516 close(IN);
2517
2518 my @confs = keys %removed_configs;
2519 if ($#confs >= 0) {
2520 doprint "Configs overridden by default configs and removed from check:\n";
2521 foreach my $config (@confs) {
2522 doprint " $config\n";
2523 }
2524 }
2525 @confs = keys %added_configs;
2526 if ($#confs >= 0) {
2527 doprint "Configs appearing in make oldconfig and added:\n";
2528 foreach my $config (@confs) {
2529 doprint " $config\n";
2530 }
2531 }
2532
2533 my %config_test;
2534 my $once = 0;
2535
2536 # Sometimes kconfig does weird things. We must make sure
2537 # that the config we autocreate has everything we need
2538 # to test, otherwise we may miss testing configs, or
2539 # may not be able to create a new config.
2540 # Here we create a config with everything set.
2541 create_config (keys %config_list);
2542 read_current_config \%config_test;
2543 foreach my $config (keys %config_list) {
2544 if (!defined($config_test{$config})) {
2545 if (!$once) {
2546 $once = 1;
2547 doprint "Configs not produced by kconfig (will not be checked):\n";
2548 }
2549 doprint " $config\n";
2550 delete $config_list{$config};
2551 }
2552 }
2553 my $ret;
2554 do {
2555 $ret = run_config_bisect;
2556 } while (!$ret);
2557
2558 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002559
2560 success $i;
2561}
2562
Steven Rostedt27d934b2011-05-20 09:18:18 -04002563sub patchcheck_reboot {
2564 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002565 reboot_to_good $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04002566}
2567
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002568sub patchcheck {
2569 my ($i) = @_;
2570
2571 die "PATCHCHECK_START[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002572 if (!defined($patchcheck_start));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002573 die "PATCHCHECK_TYPE[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002574 if (!defined($patchcheck_type));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002575
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002576 my $start = $patchcheck_start;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002577
2578 my $end = "HEAD";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002579 if (defined($patchcheck_end)) {
2580 $end = $patchcheck_end;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002581 }
2582
Steven Rostedta57419b2010-11-02 15:13:54 -04002583 # Get the true sha1's since we can use things like HEAD~3
2584 $start = get_sha1($start);
2585 $end = get_sha1($end);
2586
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002587 my $type = $patchcheck_type;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002588
2589 # Can't have a test without having a test to run
2590 if ($type eq "test" && !defined($run_test)) {
2591 $type = "boot";
2592 }
2593
2594 open (IN, "git log --pretty=oneline $end|") or
2595 dodie "could not get git list";
2596
2597 my @list;
2598
2599 while (<IN>) {
2600 chomp;
2601 $list[$#list+1] = $_;
2602 last if (/^$start/);
2603 }
2604 close(IN);
2605
2606 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002607 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002608 }
2609
2610 # go backwards in the list
2611 @list = reverse @list;
2612
2613 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04002614 my %ignored_warnings;
2615
2616 if (defined($ignore_warnings)) {
2617 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2618 $ignored_warnings{$sha1} = 1;
2619 }
2620 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002621
2622 $in_patchcheck = 1;
2623 foreach my $item (@list) {
2624 my $sha1 = $item;
2625 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2626
2627 doprint "\nProcessing commit $item\n\n";
2628
2629 run_command "git checkout $sha1" or
2630 die "Failed to checkout $sha1";
2631
2632 # only clean on the first and last patch
2633 if ($item eq $list[0] ||
2634 $item eq $list[$#list]) {
2635 $noclean = $save_clean;
2636 } else {
2637 $noclean = 1;
2638 }
2639
2640 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002641 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002642 } else {
2643 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002644 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002645 }
2646
Steven Rostedt19902072011-06-14 20:46:25 -04002647
2648 if (!defined($ignored_warnings{$sha1})) {
2649 check_buildlog $sha1 or return 0;
2650 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002651
2652 next if ($type eq "build");
2653
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002654 my $failed = 0;
2655
Steven Rostedtddf607e2011-06-14 20:49:13 -04002656 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002657
2658 if (!$failed && $type ne "boot"){
2659 do_run_test or $failed = 1;
2660 }
2661 end_monitor;
2662 return 0 if ($failed);
2663
Steven Rostedt27d934b2011-05-20 09:18:18 -04002664 patchcheck_reboot;
2665
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002666 }
2667 $in_patchcheck = 0;
2668 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002669
2670 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002671}
2672
Steven Rostedtb9066f62011-07-15 21:25:24 -04002673my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04002674my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002675my $iflevel = 0;
2676my @ifdeps;
2677
2678# prevent recursion
2679my %read_kconfigs;
2680
Steven Rostedtac6974c2011-10-04 09:40:17 -04002681sub add_dep {
2682 # $config depends on $dep
2683 my ($config, $dep) = @_;
2684
2685 if (defined($depends{$config})) {
2686 $depends{$config} .= " " . $dep;
2687 } else {
2688 $depends{$config} = $dep;
2689 }
2690
2691 # record the number of configs depending on $dep
2692 if (defined $depcount{$dep}) {
2693 $depcount{$dep}++;
2694 } else {
2695 $depcount{$dep} = 1;
2696 }
2697}
2698
Steven Rostedtb9066f62011-07-15 21:25:24 -04002699# taken from streamline_config.pl
2700sub read_kconfig {
2701 my ($kconfig) = @_;
2702
2703 my $state = "NONE";
2704 my $config;
2705 my @kconfigs;
2706
2707 my $cont = 0;
2708 my $line;
2709
2710
2711 if (! -f $kconfig) {
2712 doprint "file $kconfig does not exist, skipping\n";
2713 return;
2714 }
2715
2716 open(KIN, "$kconfig")
2717 or die "Can't open $kconfig";
2718 while (<KIN>) {
2719 chomp;
2720
2721 # Make sure that lines ending with \ continue
2722 if ($cont) {
2723 $_ = $line . " " . $_;
2724 }
2725
2726 if (s/\\$//) {
2727 $cont = 1;
2728 $line = $_;
2729 next;
2730 }
2731
2732 $cont = 0;
2733
2734 # collect any Kconfig sources
2735 if (/^source\s*"(.*)"/) {
2736 $kconfigs[$#kconfigs+1] = $1;
2737 }
2738
2739 # configs found
2740 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2741 $state = "NEW";
2742 $config = $2;
2743
2744 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04002745 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04002746 }
2747
2748 # collect the depends for the config
2749 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2750
Steven Rostedtac6974c2011-10-04 09:40:17 -04002751 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002752
2753 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04002754 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2755
2756 # selected by depends on config
2757 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002758
2759 # Check for if statements
2760 } elsif (/^if\s+(.*\S)\s*$/) {
2761 my $deps = $1;
2762 # remove beginning and ending non text
2763 $deps =~ s/^[^a-zA-Z0-9_]*//;
2764 $deps =~ s/[^a-zA-Z0-9_]*$//;
2765
2766 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2767
2768 $ifdeps[$iflevel++] = join ':', @deps;
2769
2770 } elsif (/^endif/) {
2771
2772 $iflevel-- if ($iflevel);
2773
2774 # stop on "help"
2775 } elsif (/^\s*help\s*$/) {
2776 $state = "NONE";
2777 }
2778 }
2779 close(KIN);
2780
2781 # read in any configs that were found.
2782 foreach $kconfig (@kconfigs) {
2783 if (!defined($read_kconfigs{$kconfig})) {
2784 $read_kconfigs{$kconfig} = 1;
2785 read_kconfig("$builddir/$kconfig");
2786 }
2787 }
2788}
2789
2790sub read_depends {
2791 # find out which arch this is by the kconfig file
2792 open (IN, $output_config)
2793 or dodie "Failed to read $output_config";
2794 my $arch;
2795 while (<IN>) {
2796 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2797 $arch = $1;
2798 last;
2799 }
2800 }
2801 close IN;
2802
2803 if (!defined($arch)) {
2804 doprint "Could not find arch from config file\n";
2805 doprint "no dependencies used\n";
2806 return;
2807 }
2808
2809 # arch is really the subarch, we need to know
2810 # what directory to look at.
2811 if ($arch eq "i386" || $arch eq "x86_64") {
2812 $arch = "x86";
2813 } elsif ($arch =~ /^tile/) {
2814 $arch = "tile";
2815 }
2816
2817 my $kconfig = "$builddir/arch/$arch/Kconfig";
2818
2819 if (! -f $kconfig && $arch =~ /\d$/) {
2820 my $orig = $arch;
2821 # some subarchs have numbers, truncate them
2822 $arch =~ s/\d*$//;
2823 $kconfig = "$builddir/arch/$arch/Kconfig";
2824 if (! -f $kconfig) {
2825 doprint "No idea what arch dir $orig is for\n";
2826 doprint "no dependencies used\n";
2827 return;
2828 }
2829 }
2830
2831 read_kconfig($kconfig);
2832}
2833
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002834sub read_config_list {
2835 my ($config) = @_;
2836
2837 open (IN, $config)
2838 or dodie "Failed to read $config";
2839
2840 while (<IN>) {
2841 if (/^((CONFIG\S*)=.*)/) {
2842 if (!defined($config_ignore{$2})) {
2843 $config_list{$2} = $1;
2844 }
2845 }
2846 }
2847
2848 close(IN);
2849}
2850
2851sub read_output_config {
2852 my ($config) = @_;
2853
2854 assign_configs \%config_ignore, $config;
2855}
2856
2857sub make_new_config {
2858 my @configs = @_;
2859
2860 open (OUT, ">$output_config")
2861 or dodie "Failed to write $output_config";
2862
2863 foreach my $config (@configs) {
2864 print OUT "$config\n";
2865 }
2866 close OUT;
2867}
2868
Steven Rostedtac6974c2011-10-04 09:40:17 -04002869sub chomp_config {
2870 my ($config) = @_;
2871
2872 $config =~ s/CONFIG_//;
2873
2874 return $config;
2875}
2876
Steven Rostedtb9066f62011-07-15 21:25:24 -04002877sub get_depends {
2878 my ($dep) = @_;
2879
Steven Rostedtac6974c2011-10-04 09:40:17 -04002880 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002881
2882 $dep = $depends{"$kconfig"};
2883
2884 # the dep string we have saves the dependencies as they
2885 # were found, including expressions like ! && ||. We
2886 # want to split this out into just an array of configs.
2887
2888 my $valid = "A-Za-z_0-9";
2889
2890 my @configs;
2891
2892 while ($dep =~ /[$valid]/) {
2893
2894 if ($dep =~ /^[^$valid]*([$valid]+)/) {
2895 my $conf = "CONFIG_" . $1;
2896
2897 $configs[$#configs + 1] = $conf;
2898
2899 $dep =~ s/^[^$valid]*[$valid]+//;
2900 } else {
2901 die "this should never happen";
2902 }
2903 }
2904
2905 return @configs;
2906}
2907
2908my %min_configs;
2909my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04002910my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002911my %processed_configs;
2912my %nochange_config;
2913
2914sub test_this_config {
2915 my ($config) = @_;
2916
2917 my $found;
2918
2919 # if we already processed this config, skip it
2920 if (defined($processed_configs{$config})) {
2921 return undef;
2922 }
2923 $processed_configs{$config} = 1;
2924
2925 # if this config failed during this round, skip it
2926 if (defined($nochange_config{$config})) {
2927 return undef;
2928 }
2929
Steven Rostedtac6974c2011-10-04 09:40:17 -04002930 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002931
2932 # Test dependencies first
2933 if (defined($depends{"$kconfig"})) {
2934 my @parents = get_depends $config;
2935 foreach my $parent (@parents) {
2936 # if the parent is in the min config, check it first
2937 next if (!defined($min_configs{$parent}));
2938 $found = test_this_config($parent);
2939 if (defined($found)) {
2940 return $found;
2941 }
2942 }
2943 }
2944
2945 # Remove this config from the list of configs
2946 # do a make oldnoconfig and then read the resulting
2947 # .config to make sure it is missing the config that
2948 # we had before
2949 my %configs = %min_configs;
2950 delete $configs{$config};
2951 make_new_config ((values %configs), (values %keep_configs));
2952 make_oldconfig;
2953 undef %configs;
2954 assign_configs \%configs, $output_config;
2955
2956 return $config if (!defined($configs{$config}));
2957
2958 doprint "disabling config $config did not change .config\n";
2959
2960 $nochange_config{$config} = 1;
2961
2962 return undef;
2963}
2964
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002965sub make_min_config {
2966 my ($i) = @_;
2967
2968 if (!defined($output_minconfig)) {
2969 fail "OUTPUT_MIN_CONFIG not defined" and return;
2970 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04002971
2972 # If output_minconfig exists, and the start_minconfig
2973 # came from min_config, than ask if we should use
2974 # that instead.
2975 if (-f $output_minconfig && !$start_minconfig_defined) {
2976 print "$output_minconfig exists\n";
2977 if (read_yn " Use it as minconfig?") {
2978 $start_minconfig = $output_minconfig;
2979 }
2980 }
2981
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002982 if (!defined($start_minconfig)) {
2983 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
2984 }
2985
Steven Rostedt35ce5952011-07-15 21:57:25 -04002986 my $temp_config = "$tmpdir/temp_config";
2987
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002988 # First things first. We build an allnoconfig to find
2989 # out what the defaults are that we can't touch.
2990 # Some are selections, but we really can't handle selections.
2991
2992 my $save_minconfig = $minconfig;
2993 undef $minconfig;
2994
2995 run_command "$make allnoconfig" or return 0;
2996
Steven Rostedtb9066f62011-07-15 21:25:24 -04002997 read_depends;
2998
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002999 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003000
Steven Rostedt43d1b652011-07-15 22:01:56 -04003001 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003002 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003003
3004 if (defined($ignore_config)) {
3005 # make sure the file exists
3006 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003007 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003008 }
3009
Steven Rostedt43d1b652011-07-15 22:01:56 -04003010 %keep_configs = %save_configs;
3011
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003012 doprint "Load initial configs from $start_minconfig\n";
3013
3014 # Look at the current min configs, and save off all the
3015 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003016 assign_configs \%min_configs, $start_minconfig;
3017
3018 my @config_keys = keys %min_configs;
3019
Steven Rostedtac6974c2011-10-04 09:40:17 -04003020 # All configs need a depcount
3021 foreach my $config (@config_keys) {
3022 my $kconfig = chomp_config $config;
3023 if (!defined $depcount{$kconfig}) {
3024 $depcount{$kconfig} = 0;
3025 }
3026 }
3027
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003028 # Remove anything that was set by the make allnoconfig
3029 # we shouldn't need them as they get set for us anyway.
3030 foreach my $config (@config_keys) {
3031 # Remove anything in the ignore_config
3032 if (defined($keep_configs{$config})) {
3033 my $file = $ignore_config;
3034 $file =~ s,.*/(.*?)$,$1,;
3035 doprint "$config set by $file ... ignored\n";
3036 delete $min_configs{$config};
3037 next;
3038 }
3039 # But make sure the settings are the same. If a min config
3040 # sets a selection, we do not want to get rid of it if
3041 # it is not the same as what we have. Just move it into
3042 # the keep configs.
3043 if (defined($config_ignore{$config})) {
3044 if ($config_ignore{$config} ne $min_configs{$config}) {
3045 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3046 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3047 $keep_configs{$config} = $min_configs{$config};
3048 } else {
3049 doprint "$config set by allnoconfig ... ignored\n";
3050 }
3051 delete $min_configs{$config};
3052 }
3053 }
3054
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003055 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003056 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003057
3058 while (!$done) {
3059
3060 my $config;
3061 my $found;
3062
3063 # Now disable each config one by one and do a make oldconfig
3064 # till we find a config that changes our list.
3065
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003066 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003067
3068 # Sort keys by who is most dependent on
3069 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3070 @test_configs ;
3071
3072 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003073 my $reset = 1;
3074 for (my $i = 0; $i < $#test_configs; $i++) {
3075 if (!defined($nochange_config{$test_configs[0]})) {
3076 $reset = 0;
3077 last;
3078 }
3079 # This config didn't change the .config last time.
3080 # Place it at the end
3081 my $config = shift @test_configs;
3082 push @test_configs, $config;
3083 }
3084
3085 # if every test config has failed to modify the .config file
3086 # in the past, then reset and start over.
3087 if ($reset) {
3088 undef %nochange_config;
3089 }
3090
Steven Rostedtb9066f62011-07-15 21:25:24 -04003091 undef %processed_configs;
3092
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003093 foreach my $config (@test_configs) {
3094
Steven Rostedtb9066f62011-07-15 21:25:24 -04003095 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003096
Steven Rostedtb9066f62011-07-15 21:25:24 -04003097 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003098
3099 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003100 }
3101
3102 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003103 # we could have failed due to the nochange_config hash
3104 # reset and try again
3105 if (!$take_two) {
3106 undef %nochange_config;
3107 $take_two = 1;
3108 next;
3109 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003110 doprint "No more configs found that we can disable\n";
3111 $done = 1;
3112 last;
3113 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003114 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003115
3116 $config = $found;
3117
3118 doprint "Test with $config disabled\n";
3119
3120 # set in_bisect to keep build and monitor from dieing
3121 $in_bisect = 1;
3122
3123 my $failed = 0;
3124 build "oldconfig";
3125 start_monitor_and_boot or $failed = 1;
3126 end_monitor;
3127
3128 $in_bisect = 0;
3129
3130 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003131 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003132 # this config is needed, add it to the ignore list.
3133 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003134 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003135 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003136
3137 # update new ignore configs
3138 if (defined($ignore_config)) {
3139 open (OUT, ">$temp_config")
3140 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003141 foreach my $config (keys %save_configs) {
3142 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003143 }
3144 close OUT;
3145 run_command "mv $temp_config $ignore_config" or
3146 dodie "failed to copy update to $ignore_config";
3147 }
3148
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003149 } else {
3150 # We booted without this config, remove it from the minconfigs.
3151 doprint "$config is not needed, disabling\n";
3152
3153 delete $min_configs{$config};
3154
3155 # Also disable anything that is not enabled in this config
3156 my %configs;
3157 assign_configs \%configs, $output_config;
3158 my @config_keys = keys %min_configs;
3159 foreach my $config (@config_keys) {
3160 if (!defined($configs{$config})) {
3161 doprint "$config is not set, disabling\n";
3162 delete $min_configs{$config};
3163 }
3164 }
3165
3166 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003167 open (OUT, ">$temp_config")
3168 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003169 foreach my $config (keys %keep_configs) {
3170 print OUT "$keep_configs{$config}\n";
3171 }
3172 foreach my $config (keys %min_configs) {
3173 print OUT "$min_configs{$config}\n";
3174 }
3175 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003176
3177 run_command "mv $temp_config $output_minconfig" or
3178 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003179 }
3180
3181 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003182 reboot_to_good $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003183 }
3184
3185 success $i;
3186 return 1;
3187}
3188
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003189$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003190
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003191if ($#ARGV == 0) {
3192 $ktest_config = $ARGV[0];
3193 if (! -f $ktest_config) {
3194 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003195 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003196 exit 0;
3197 }
3198 }
3199} else {
3200 $ktest_config = "ktest.conf";
3201}
3202
3203if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003204 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003205 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003206 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3207 print OUT << "EOF"
3208# Generated by ktest.pl
3209#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003210
3211# PWD is a ktest.pl variable that will result in the process working
3212# directory that ktest.pl is executed in.
3213
3214# THIS_DIR is automatically assigned the PWD of the path that generated
3215# the config file. It is best to use this variable when assigning other
3216# directory paths within this directory. This allows you to easily
3217# move the test cases to other locations or to other machines.
3218#
3219THIS_DIR := $variable{"PWD"}
3220
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003221# Define each test with TEST_START
3222# The config options below it will override the defaults
3223TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003224TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003225
3226DEFAULTS
3227EOF
3228;
3229 close(OUT);
3230}
3231read_config $ktest_config;
3232
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003233if (defined($opt{"LOG_FILE"})) {
3234 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3235}
3236
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003237# Append any configs entered in manually to the config file.
3238my @new_configs = keys %entered_configs;
3239if ($#new_configs >= 0) {
3240 print "\nAppending entered in configs to $ktest_config\n";
3241 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3242 foreach my $config (@new_configs) {
3243 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003244 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003245 }
3246}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003247
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003248if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3249 unlink $opt{"LOG_FILE"};
3250}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003251
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003252doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3253
Steven Rostedta57419b2010-11-02 15:13:54 -04003254for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3255
3256 if (!$i) {
3257 doprint "DEFAULT OPTIONS:\n";
3258 } else {
3259 doprint "\nTEST $i OPTIONS";
3260 if (defined($repeat_tests{$i})) {
3261 $repeat = $repeat_tests{$i};
3262 doprint " ITERATE $repeat";
3263 }
3264 doprint "\n";
3265 }
3266
3267 foreach my $option (sort keys %opt) {
3268
3269 if ($option =~ /\[(\d+)\]$/) {
3270 next if ($i != $1);
3271 } else {
3272 next if ($i);
3273 }
3274
3275 doprint "$option = $opt{$option}\n";
3276 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003277}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003278
Steven Rostedt2a625122011-05-20 15:48:59 -04003279sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003280 my ($name, $i) = @_;
3281
3282 my $option = "$name\[$i\]";
3283
3284 if (defined($opt{$option})) {
3285 return $opt{$option};
3286 }
3287
Steven Rostedta57419b2010-11-02 15:13:54 -04003288 foreach my $test (keys %repeat_tests) {
3289 if ($i >= $test &&
3290 $i < $test + $repeat_tests{$test}) {
3291 $option = "$name\[$test\]";
3292 if (defined($opt{$option})) {
3293 return $opt{$option};
3294 }
3295 }
3296 }
3297
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003298 if (defined($opt{$name})) {
3299 return $opt{$name};
3300 }
3301
3302 return undef;
3303}
3304
Steven Rostedt2a625122011-05-20 15:48:59 -04003305sub set_test_option {
3306 my ($name, $i) = @_;
3307
3308 my $option = __set_test_option($name, $i);
3309 return $option if (!defined($option));
3310
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003311 return eval_option($option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003312}
3313
Steven Rostedt2545eb62010-11-02 15:01:32 -04003314# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003315for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003316
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003317 # Do not reboot on failing test options
3318 $no_reboot = 1;
3319
Steven Rostedt576f6272010-11-02 14:58:38 -04003320 $iteration = $i;
3321
Steven Rostedta75fece2010-11-02 14:58:27 -04003322 my $makecmd = set_test_option("MAKE_CMD", $i);
3323
3324 $machine = set_test_option("MACHINE", $i);
Steven Rostedte48c5292010-11-02 14:35:37 -04003325 $ssh_user = set_test_option("SSH_USER", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003326 $tmpdir = set_test_option("TMP_DIR", $i);
3327 $outputdir = set_test_option("OUTPUT_DIR", $i);
3328 $builddir = set_test_option("BUILD_DIR", $i);
3329 $test_type = set_test_option("TEST_TYPE", $i);
3330 $build_type = set_test_option("BUILD_TYPE", $i);
3331 $build_options = set_test_option("BUILD_OPTIONS", $i);
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04003332 $pre_build = set_test_option("PRE_BUILD", $i);
3333 $post_build = set_test_option("POST_BUILD", $i);
3334 $pre_build_die = set_test_option("PRE_BUILD_DIE", $i);
3335 $post_build_die = set_test_option("POST_BUILD_DIE", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003336 $power_cycle = set_test_option("POWER_CYCLE", $i);
Steven Rostedte48c5292010-11-02 14:35:37 -04003337 $reboot = set_test_option("REBOOT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003338 $noclean = set_test_option("BUILD_NOCLEAN", $i);
3339 $minconfig = set_test_option("MIN_CONFIG", $i);
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003340 $output_minconfig = set_test_option("OUTPUT_MIN_CONFIG", $i);
3341 $start_minconfig = set_test_option("START_MIN_CONFIG", $i);
3342 $ignore_config = set_test_option("IGNORE_CONFIG", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003343 $run_test = set_test_option("TEST", $i);
3344 $addconfig = set_test_option("ADD_CONFIG", $i);
3345 $reboot_type = set_test_option("REBOOT_TYPE", $i);
3346 $grub_menu = set_test_option("GRUB_MENU", $i);
Steven Rostedt8b37ca82010-11-02 14:58:33 -04003347 $post_install = set_test_option("POST_INSTALL", $i);
Steven Rostedte0a87422011-09-30 17:50:48 -04003348 $no_install = set_test_option("NO_INSTALL", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003349 $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
3350 $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003351 $switch_to_good = set_test_option("SWITCH_TO_GOOD", $i);
3352 $switch_to_test = set_test_option("SWITCH_TO_TEST", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003353 $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
3354 $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
3355 $power_off = set_test_option("POWER_OFF", $i);
Steven Rostedt576f6272010-11-02 14:58:38 -04003356 $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
3357 $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003358 $sleep_time = set_test_option("SLEEP_TIME", $i);
3359 $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
Steven Rostedt27d934b2011-05-20 09:18:18 -04003360 $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
Steven Rostedt19902072011-06-14 20:46:25 -04003361 $ignore_warnings = set_test_option("IGNORE_WARNINGS", $i);
Steven Rostedtc960bb92011-03-08 09:22:39 -05003362 $bisect_manual = set_test_option("BISECT_MANUAL", $i);
Steven Rostedtc23dca72011-03-08 09:26:31 -05003363 $bisect_skip = set_test_option("BISECT_SKIP", $i);
Steven Rostedt30f75da2011-06-13 10:35:35 -04003364 $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
Steven Rostedtc5dacb82011-12-22 12:43:57 -05003365 $bisect_ret_good = set_test_option("BISECT_RET_GOOD", $i);
3366 $bisect_ret_bad = set_test_option("BISECT_RET_BAD", $i);
3367 $bisect_ret_skip = set_test_option("BISECT_RET_SKIP", $i);
3368 $bisect_ret_abort = set_test_option("BISECT_RET_ABORT", $i);
3369 $bisect_ret_default = set_test_option("BISECT_RET_DEFAULT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003370 $store_failures = set_test_option("STORE_FAILURES", $i);
Rabin Vincentde5b6e32011-11-18 17:05:31 +05303371 $store_successes = set_test_option("STORE_SUCCESSES", $i);
Steven Rostedt9064af52011-06-13 10:38:48 -04003372 $test_name = set_test_option("TEST_NAME", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003373 $timeout = set_test_option("TIMEOUT", $i);
3374 $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
3375 $console = set_test_option("CONSOLE", $i);
Steven Rostedtf1a5b962011-06-13 10:30:00 -04003376 $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003377 $success_line = set_test_option("SUCCESS_LINE", $i);
Steven Rostedt2b803362011-09-30 18:00:23 -04003378 $reboot_success_line = set_test_option("REBOOT_SUCCESS_LINE", $i);
Steven Rostedt1c8a6172010-11-09 12:55:40 -05003379 $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
3380 $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
Steven Rostedt2d01b262011-03-08 09:47:54 -05003381 $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003382 $build_target = set_test_option("BUILD_TARGET", $i);
Steven Rostedte48c5292010-11-02 14:35:37 -04003383 $ssh_exec = set_test_option("SSH_EXEC", $i);
3384 $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003385 $checkout = set_test_option("CHECKOUT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003386 $target_image = set_test_option("TARGET_IMAGE", $i);
3387 $localversion = set_test_option("LOCALVERSION", $i);
3388
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003389 $bisect_good = set_test_option("BISECT_GOOD", $i);
3390 $bisect_bad = set_test_option("BISECT_BAD", $i);
3391 $bisect_type = set_test_option("BISECT_TYPE", $i);
3392 $bisect_start = set_test_option("BISECT_START", $i);
3393 $bisect_replay = set_test_option("BISECT_REPLAY", $i);
3394 $bisect_files = set_test_option("BISECT_FILES", $i);
3395 $bisect_reverse = set_test_option("BISECT_REVERSE", $i);
3396 $bisect_check = set_test_option("BISECT_CHECK", $i);
3397
3398 $config_bisect = set_test_option("CONFIG_BISECT", $i);
3399 $config_bisect_type = set_test_option("CONFIG_BISECT_TYPE", $i);
3400
3401 $patchcheck_type = set_test_option("PATCHCHECK_TYPE", $i);
3402 $patchcheck_start = set_test_option("PATCHCHECK_START", $i);
3403 $patchcheck_end = set_test_option("PATCHCHECK_END", $i);
3404
Steven Rostedt35ce5952011-07-15 21:57:25 -04003405 $start_minconfig_defined = 1;
3406
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003407 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003408 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003409 $start_minconfig = $minconfig;
3410 }
3411
Steven Rostedta75fece2010-11-02 14:58:27 -04003412 chdir $builddir || die "can't change directory to $builddir";
3413
Andrew Jonesa908a662011-08-12 15:32:03 +02003414 foreach my $dir ($tmpdir, $outputdir) {
3415 if (!-d $dir) {
3416 mkpath($dir) or
3417 die "can't create $dir";
3418 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003419 }
3420
Steven Rostedte48c5292010-11-02 14:35:37 -04003421 $ENV{"SSH_USER"} = $ssh_user;
3422 $ENV{"MACHINE"} = $machine;
3423
Steven Rostedta75fece2010-11-02 14:58:27 -04003424 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303425 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003426 $dmesg = "$tmpdir/dmesg-$machine";
3427 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003428 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003429
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003430 if (!$buildonly) {
3431 $target = "$ssh_user\@$machine";
3432 if ($reboot_type eq "grub") {
3433 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3434 } elsif (!defined($reboot_script)) {
3435 dodie "REBOOT_SCRIPT not defined"
3436 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003437 }
3438
3439 my $run_type = $build_type;
3440 if ($test_type eq "patchcheck") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003441 $run_type = $patchcheck_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003442 } elsif ($test_type eq "bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003443 $run_type = $bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003444 } elsif ($test_type eq "config_bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003445 $run_type = $config_bisect_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003446 }
3447
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003448 if ($test_type eq "make_min_config") {
3449 $run_type = "";
3450 }
3451
Steven Rostedta75fece2010-11-02 14:58:27 -04003452 # mistake in config file?
3453 if (!defined($run_type)) {
3454 $run_type = "ERROR";
3455 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003456
Steven Rostedte0a87422011-09-30 17:50:48 -04003457 my $installme = "";
3458 $installme = " no_install" if ($no_install);
3459
Steven Rostedt2545eb62010-11-02 15:01:32 -04003460 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003461 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003462
3463 unlink $dmesg;
3464 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303465 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003466
Steven Rostedt250bae82011-07-15 22:05:59 -04003467 if (defined($addconfig)) {
3468 my $min = $minconfig;
3469 if (!defined($minconfig)) {
3470 $min = "";
3471 }
3472 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003473 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003474 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003475 }
3476
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003477 if (defined($checkout)) {
3478 run_command "git checkout $checkout" or
3479 die "failed to checkout $checkout";
3480 }
3481
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003482 $no_reboot = 0;
3483
3484
Steven Rostedta75fece2010-11-02 14:58:27 -04003485 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003486 bisect $i;
3487 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003488 } elsif ($test_type eq "config_bisect") {
3489 config_bisect $i;
3490 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003491 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003492 patchcheck $i;
3493 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003494 } elsif ($test_type eq "make_min_config") {
3495 make_min_config $i;
3496 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003497 }
3498
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003499 if ($build_type ne "nobuild") {
3500 build $build_type or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003501 }
3502
Steven Rostedtcd8e3682011-08-18 16:35:44 -04003503 if ($test_type eq "install") {
3504 get_version;
3505 install;
3506 success $i;
3507 next;
3508 }
3509
Steven Rostedta75fece2010-11-02 14:58:27 -04003510 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04003511 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04003512 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04003513
3514 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3515 do_run_test or $failed = 1;
3516 }
3517 end_monitor;
3518 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003519 }
3520
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003521 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003522}
3523
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003524if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003525 halt;
Steven Rostedt576f6272010-11-02 14:58:38 -04003526} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003527 reboot_to_good;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003528}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003529
Steven Rostedte48c5292010-11-02 14:35:37 -04003530doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3531
Steven Rostedt2545eb62010-11-02 15:01:32 -04003532exit 0;