blob: 47c28146dfc2dc8bc841ff036c80883b5d2f32d2 [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 Rostedta75fece2010-11-02 14:58:27 -040021my %default;
Steven Rostedt2545eb62010-11-02 15:01:32 -040022
23#default opts
Steven Rostedta57419b2010-11-02 15:13:54 -040024$default{"NUM_TESTS"} = 1;
Steven Rostedtbb8474b2011-11-23 15:58:00 -050025$default{"TEST_TYPE"} = "build";
Steven Rostedta75fece2010-11-02 14:58:27 -040026$default{"BUILD_TYPE"} = "randconfig";
27$default{"MAKE_CMD"} = "make";
28$default{"TIMEOUT"} = 120;
Steven Rostedt48920632011-06-14 20:42:19 -040029$default{"TMP_DIR"} = "/tmp/ktest/\${MACHINE}";
Steven Rostedta75fece2010-11-02 14:58:27 -040030$default{"SLEEP_TIME"} = 60; # sleep time between tests
31$default{"BUILD_NOCLEAN"} = 0;
32$default{"REBOOT_ON_ERROR"} = 0;
33$default{"POWEROFF_ON_ERROR"} = 0;
34$default{"REBOOT_ON_SUCCESS"} = 1;
35$default{"POWEROFF_ON_SUCCESS"} = 0;
36$default{"BUILD_OPTIONS"} = "";
37$default{"BISECT_SLEEP_TIME"} = 60; # sleep time between bisects
Steven Rostedt27d934b2011-05-20 09:18:18 -040038$default{"PATCHCHECK_SLEEP_TIME"} = 60; # sleep time between patch checks
Steven Rostedta75fece2010-11-02 14:58:27 -040039$default{"CLEAR_LOG"} = 0;
Steven Rostedtc960bb92011-03-08 09:22:39 -050040$default{"BISECT_MANUAL"} = 0;
Steven Rostedtc23dca72011-03-08 09:26:31 -050041$default{"BISECT_SKIP"} = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -040042$default{"SUCCESS_LINE"} = "login:";
Steven Rostedtf1a5b962011-06-13 10:30:00 -040043$default{"DETECT_TRIPLE_FAULT"} = 1;
Steven Rostedte0a87422011-09-30 17:50:48 -040044$default{"NO_INSTALL"} = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -040045$default{"BOOTED_TIMEOUT"} = 1;
46$default{"DIE_ON_FAILURE"} = 1;
Steven Rostedte48c5292010-11-02 14:35:37 -040047$default{"SSH_EXEC"} = "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND";
48$default{"SCP_TO_TARGET"} = "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE";
49$default{"REBOOT"} = "ssh \$SSH_USER\@\$MACHINE reboot";
Steven Rostedt1c8a6172010-11-09 12:55:40 -050050$default{"STOP_AFTER_SUCCESS"} = 10;
51$default{"STOP_AFTER_FAILURE"} = 60;
Steven Rostedt2d01b262011-03-08 09:47:54 -050052$default{"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.
56$default{"REBOOT_TYPE"} = "grub";
Steven Rostedt8d1491b2010-11-18 15:39:48 -050057$default{"LOCALVERSION"} = "-test";
Steven Rostedt600bbf02011-11-21 20:12:04 -050058$default{"SSH_USER"} = "root";
59$default{"BUILD_TARGET"} = "arch/x86/boot/bzImage";
60$default{"TARGET_IMAGE"} = "/boot/vmlinuz-test";
Steven Rostedt2545eb62010-11-02 15:01:32 -040061
Steven Rostedt8d1491b2010-11-18 15:39:48 -050062my $ktest_config;
Steven Rostedt2545eb62010-11-02 15:01:32 -040063my $version;
Steven Rostedta75fece2010-11-02 14:58:27 -040064my $machine;
Steven Rostedte48c5292010-11-02 14:35:37 -040065my $ssh_user;
Steven Rostedta75fece2010-11-02 14:58:27 -040066my $tmpdir;
67my $builddir;
68my $outputdir;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -050069my $output_config;
Steven Rostedta75fece2010-11-02 14:58:27 -040070my $test_type;
Steven Rostedt7faafbd2010-11-02 14:58:22 -040071my $build_type;
Steven Rostedta75fece2010-11-02 14:58:27 -040072my $build_options;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -040073my $pre_build;
74my $post_build;
75my $pre_build_die;
76my $post_build_die;
Steven Rostedta75fece2010-11-02 14:58:27 -040077my $reboot_type;
78my $reboot_script;
79my $power_cycle;
Steven Rostedte48c5292010-11-02 14:35:37 -040080my $reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -040081my $reboot_on_error;
82my $poweroff_on_error;
83my $die_on_failure;
Steven Rostedt576f6272010-11-02 14:58:38 -040084my $powercycle_after_reboot;
85my $poweroff_after_halt;
Steven Rostedte48c5292010-11-02 14:35:37 -040086my $ssh_exec;
87my $scp_to_target;
Steven Rostedta75fece2010-11-02 14:58:27 -040088my $power_off;
89my $grub_menu;
Steven Rostedt2545eb62010-11-02 15:01:32 -040090my $grub_number;
91my $target;
92my $make;
Steven Rostedt8b37ca82010-11-02 14:58:33 -040093my $post_install;
Steven Rostedte0a87422011-09-30 17:50:48 -040094my $no_install;
Steven Rostedt5c42fc52010-11-02 14:57:01 -040095my $noclean;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -040096my $minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -040097my $start_minconfig;
Steven Rostedt35ce5952011-07-15 21:57:25 -040098my $start_minconfig_defined;
Steven Rostedt4c4ab122011-07-15 21:16:17 -040099my $output_minconfig;
100my $ignore_config;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -0400101my $addconfig;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400102my $in_bisect = 0;
103my $bisect_bad = "";
Steven Rostedtd6ce2a02010-11-02 14:58:05 -0400104my $reverse_bisect;
Steven Rostedtc960bb92011-03-08 09:22:39 -0500105my $bisect_manual;
Steven Rostedtc23dca72011-03-08 09:26:31 -0500106my $bisect_skip;
Steven Rostedt30f75da2011-06-13 10:35:35 -0400107my $config_bisect_good;
Steven Rostedtc5dacb82011-12-22 12:43:57 -0500108my $bisect_ret_good;
109my $bisect_ret_bad;
110my $bisect_ret_skip;
111my $bisect_ret_abort;
112my $bisect_ret_default;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400113my $in_patchcheck = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400114my $run_test;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400115my $redirect;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400116my $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +0530117my $testlog;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400118my $dmesg;
119my $monitor_fp;
120my $monitor_pid;
121my $monitor_cnt = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -0400122my $sleep_time;
123my $bisect_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -0400124my $patchcheck_sleep_time;
Steven Rostedt19902072011-06-14 20:46:25 -0400125my $ignore_warnings;
Steven Rostedta75fece2010-11-02 14:58:27 -0400126my $store_failures;
Rabin Vincentde5b6e32011-11-18 17:05:31 +0530127my $store_successes;
Steven Rostedt9064af52011-06-13 10:38:48 -0400128my $test_name;
Steven Rostedta75fece2010-11-02 14:58:27 -0400129my $timeout;
130my $booted_timeout;
Steven Rostedtf1a5b962011-06-13 10:30:00 -0400131my $detect_triplefault;
Steven Rostedta75fece2010-11-02 14:58:27 -0400132my $console;
Steven Rostedt2b803362011-09-30 18:00:23 -0400133my $reboot_success_line;
Steven Rostedta75fece2010-11-02 14:58:27 -0400134my $success_line;
Steven Rostedt1c8a6172010-11-09 12:55:40 -0500135my $stop_after_success;
136my $stop_after_failure;
Steven Rostedt2d01b262011-03-08 09:47:54 -0500137my $stop_test_after;
Steven Rostedta75fece2010-11-02 14:58:27 -0400138my $build_target;
139my $target_image;
140my $localversion;
Steven Rostedt576f6272010-11-02 14:58:38 -0400141my $iteration = 0;
Steven Rostedte48c5292010-11-02 14:35:37 -0400142my $successes = 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400143
Steven Rostedt165708b2011-11-26 20:56:52 -0500144# set when a test is something other that just building or install
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500145# which would require more options.
146my $buildonly = 1;
147
Steven Rostedtdbd37832011-11-23 16:00:48 -0500148# set when creating a new config
149my $newconfig = 0;
150
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500151my %entered_configs;
152my %config_help;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400153my %variable;
Steven Rostedtfcb3f162011-06-13 10:40:58 -0400154my %force_config;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500155
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400156# do not force reboots on config problems
157my $no_reboot = 1;
158
Steven Rostedt7bf51072011-10-22 09:07:03 -0400159# default variables that can be used
160chomp ($variable{"PWD"} = `pwd`);
161
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500162$config_help{"MACHINE"} = << "EOF"
163 The machine hostname that you will test.
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500164 For build only tests, it is still needed to differentiate log files.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500165EOF
166 ;
167$config_help{"SSH_USER"} = << "EOF"
168 The box is expected to have ssh on normal bootup, provide the user
169 (most likely root, since you need privileged operations)
170EOF
171 ;
172$config_help{"BUILD_DIR"} = << "EOF"
173 The directory that contains the Linux source code (full path).
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500174 You can use \${PWD} that will be the path where ktest.pl is run, or use
175 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500176EOF
177 ;
178$config_help{"OUTPUT_DIR"} = << "EOF"
179 The directory that the objects will be built (full path).
180 (can not be same as BUILD_DIR)
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500181 You can use \${PWD} that will be the path where ktest.pl is run, or use
182 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500183EOF
184 ;
185$config_help{"BUILD_TARGET"} = << "EOF"
186 The location of the compiled file to copy to the target.
187 (relative to OUTPUT_DIR)
188EOF
189 ;
Steven Rostedtdbd37832011-11-23 16:00:48 -0500190$config_help{"BUILD_OPTIONS"} = << "EOF"
191 Options to add to \"make\" when building.
192 i.e. -j20
193EOF
194 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500195$config_help{"TARGET_IMAGE"} = << "EOF"
196 The place to put your image on the test machine.
197EOF
198 ;
199$config_help{"POWER_CYCLE"} = << "EOF"
200 A script or command to reboot the box.
201
202 Here is a digital loggers power switch example
203 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
204
205 Here is an example to reboot a virtual box on the current host
206 with the name "Guest".
207 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
208EOF
209 ;
210$config_help{"CONSOLE"} = << "EOF"
211 The script or command that reads the console
212
213 If you use ttywatch server, something like the following would work.
214CONSOLE = nc -d localhost 3001
215
216 For a virtual machine with guest name "Guest".
217CONSOLE = virsh console Guest
218EOF
219 ;
220$config_help{"LOCALVERSION"} = << "EOF"
221 Required version ending to differentiate the test
222 from other linux builds on the system.
223EOF
224 ;
225$config_help{"REBOOT_TYPE"} = << "EOF"
226 Way to reboot the box to the test kernel.
227 Only valid options so far are "grub" and "script".
228
229 If you specify grub, it will assume grub version 1
230 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
231 and select that target to reboot to the kernel. If this is not
232 your setup, then specify "script" and have a command or script
233 specified in REBOOT_SCRIPT to boot to the target.
234
235 The entry in /boot/grub/menu.lst must be entered in manually.
236 The test will not modify that file.
237EOF
238 ;
239$config_help{"GRUB_MENU"} = << "EOF"
240 The grub title name for the test kernel to boot
241 (Only mandatory if REBOOT_TYPE = grub)
242
243 Note, ktest.pl will not update the grub menu.lst, you need to
244 manually add an option for the test. ktest.pl will search
245 the grub menu.lst for this option to find what kernel to
246 reboot into.
247
248 For example, if in the /boot/grub/menu.lst the test kernel title has:
249 title Test Kernel
250 kernel vmlinuz-test
251 GRUB_MENU = Test Kernel
252EOF
253 ;
254$config_help{"REBOOT_SCRIPT"} = << "EOF"
255 A script to reboot the target into the test kernel
256 (Only mandatory if REBOOT_TYPE = script)
257EOF
258 ;
259
Steven Rostedtdad98752011-11-22 20:48:57 -0500260sub read_prompt {
261 my ($cancel, $prompt) = @_;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400262
263 my $ans;
264
265 for (;;) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500266 if ($cancel) {
267 print "$prompt [y/n/C] ";
268 } else {
269 print "$prompt [Y/n] ";
270 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400271 $ans = <STDIN>;
272 chomp $ans;
273 if ($ans =~ /^\s*$/) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500274 if ($cancel) {
275 $ans = "c";
276 } else {
277 $ans = "y";
278 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400279 }
280 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
Steven Rostedtdad98752011-11-22 20:48:57 -0500281 if ($cancel) {
282 last if ($ans =~ /^c$/i);
283 print "Please answer either 'y', 'n' or 'c'.\n";
284 } else {
285 print "Please answer either 'y' or 'n'.\n";
286 }
287 }
288 if ($ans =~ /^c/i) {
289 exit;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400290 }
291 if ($ans !~ /^y$/i) {
292 return 0;
293 }
294 return 1;
295}
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500296
Steven Rostedtdad98752011-11-22 20:48:57 -0500297sub read_yn {
298 my ($prompt) = @_;
299
300 return read_prompt 0, $prompt;
301}
302
303sub read_ync {
304 my ($prompt) = @_;
305
306 return read_prompt 1, $prompt;
307}
308
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500309sub get_ktest_config {
310 my ($config) = @_;
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400311 my $ans;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500312
313 return if (defined($opt{$config}));
314
315 if (defined($config_help{$config})) {
316 print "\n";
317 print $config_help{$config};
318 }
319
320 for (;;) {
321 print "$config = ";
Steven Rostedtdbd37832011-11-23 16:00:48 -0500322 if (defined($default{$config}) && length($default{$config})) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500323 print "\[$default{$config}\] ";
324 }
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400325 $ans = <STDIN>;
326 $ans =~ s/^\s*(.*\S)\s*$/$1/;
327 if ($ans =~ /^\s*$/) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500328 if ($default{$config}) {
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400329 $ans = $default{$config};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500330 } else {
331 print "Your answer can not be blank\n";
332 next;
333 }
334 }
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500335 $entered_configs{$config} = ${ans};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500336 last;
337 }
338}
339
340sub get_ktest_configs {
341 get_ktest_config("MACHINE");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500342 get_ktest_config("BUILD_DIR");
343 get_ktest_config("OUTPUT_DIR");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500344
Steven Rostedtdbd37832011-11-23 16:00:48 -0500345 if ($newconfig) {
346 get_ktest_config("BUILD_OPTIONS");
347 }
348
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500349 # options required for other than just building a kernel
350 if (!$buildonly) {
Steven Rostedt165708b2011-11-26 20:56:52 -0500351 get_ktest_config("POWER_CYCLE");
352 get_ktest_config("CONSOLE");
353 }
354
355 # options required for install and more
356 if ($buildonly != 1) {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500357 get_ktest_config("SSH_USER");
358 get_ktest_config("BUILD_TARGET");
359 get_ktest_config("TARGET_IMAGE");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500360 }
361
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500362 get_ktest_config("LOCALVERSION");
363
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500364 return if ($buildonly);
365
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500366 my $rtype = $opt{"REBOOT_TYPE"};
367
368 if (!defined($rtype)) {
369 if (!defined($opt{"GRUB_MENU"})) {
370 get_ktest_config("REBOOT_TYPE");
371 $rtype = $entered_configs{"REBOOT_TYPE"};
372 } else {
373 $rtype = "grub";
374 }
375 }
376
377 if ($rtype eq "grub") {
378 get_ktest_config("GRUB_MENU");
379 } else {
380 get_ktest_config("REBOOT_SCRIPT");
381 }
382}
383
Steven Rostedt77d942c2011-05-20 13:36:58 -0400384sub process_variables {
Steven Rostedt8d735212011-10-17 11:36:44 -0400385 my ($value, $remove_undef) = @_;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400386 my $retval = "";
387
388 # We want to check for '\', and it is just easier
389 # to check the previous characet of '$' and not need
390 # to worry if '$' is the first character. By adding
391 # a space to $value, we can just check [^\\]\$ and
392 # it will still work.
393 $value = " $value";
394
395 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
396 my $begin = $1;
397 my $var = $2;
398 my $end = $3;
399 # append beginning of value to retval
400 $retval = "$retval$begin";
401 if (defined($variable{$var})) {
402 $retval = "$retval$variable{$var}";
Steven Rostedt8d735212011-10-17 11:36:44 -0400403 } elsif (defined($remove_undef) && $remove_undef) {
404 # for if statements, any variable that is not defined,
405 # we simple convert to 0
406 $retval = "${retval}0";
Steven Rostedt77d942c2011-05-20 13:36:58 -0400407 } else {
408 # put back the origin piece.
409 $retval = "$retval\$\{$var\}";
410 }
411 $value = $end;
412 }
413 $retval = "$retval$value";
414
415 # remove the space added in the beginning
416 $retval =~ s/ //;
417
418 return "$retval"
419}
420
Steven Rostedta57419b2010-11-02 15:13:54 -0400421sub set_value {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400422 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
Steven Rostedta57419b2010-11-02 15:13:54 -0400423
Steven Rostedtcad96662011-12-22 11:32:52 -0500424 my $prvalue = process_variables($rvalue);
425
426 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500427 # Note if a test is something other than build, then we
428 # will need other manditory options.
Steven Rostedtcad96662011-12-22 11:32:52 -0500429 if ($prvalue ne "install") {
Steven Rostedt165708b2011-11-26 20:56:52 -0500430 $buildonly = 0;
431 } else {
432 # install still limits some manditory options.
433 $buildonly = 2;
434 }
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500435 }
436
Steven Rostedta57419b2010-11-02 15:13:54 -0400437 if (defined($opt{$lvalue})) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400438 if (!$override || defined(${$overrides}{$lvalue})) {
439 my $extra = "";
440 if ($override) {
441 $extra = "In the same override section!\n";
442 }
443 die "$name: $.: Option $lvalue defined more than once!\n$extra";
444 }
Steven Rostedtcad96662011-12-22 11:32:52 -0500445 ${$overrides}{$lvalue} = $prvalue;
Steven Rostedta57419b2010-11-02 15:13:54 -0400446 }
Steven Rostedt21a96792010-11-08 16:45:50 -0500447 if ($rvalue =~ /^\s*$/) {
448 delete $opt{$lvalue};
449 } else {
Steven Rostedtcad96662011-12-22 11:32:52 -0500450 $opt{$lvalue} = $prvalue;
Steven Rostedt21a96792010-11-08 16:45:50 -0500451 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400452}
453
Steven Rostedt77d942c2011-05-20 13:36:58 -0400454sub set_variable {
455 my ($lvalue, $rvalue) = @_;
456
457 if ($rvalue =~ /^\s*$/) {
458 delete $variable{$lvalue};
459 } else {
460 $rvalue = process_variables($rvalue);
461 $variable{$lvalue} = $rvalue;
462 }
463}
464
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400465sub process_compare {
466 my ($lval, $cmp, $rval) = @_;
467
468 # remove whitespace
469
470 $lval =~ s/^\s*//;
471 $lval =~ s/\s*$//;
472
473 $rval =~ s/^\s*//;
474 $rval =~ s/\s*$//;
475
476 if ($cmp eq "==") {
477 return $lval eq $rval;
478 } elsif ($cmp eq "!=") {
479 return $lval ne $rval;
480 }
481
482 my $statement = "$lval $cmp $rval";
483 my $ret = eval $statement;
484
485 # $@ stores error of eval
486 if ($@) {
487 return -1;
488 }
489
490 return $ret;
491}
492
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400493sub value_defined {
494 my ($val) = @_;
495
496 return defined($variable{$2}) ||
497 defined($opt{$2});
498}
499
Steven Rostedt8d735212011-10-17 11:36:44 -0400500my $d = 0;
501sub process_expression {
502 my ($name, $val) = @_;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400503
Steven Rostedt8d735212011-10-17 11:36:44 -0400504 my $c = $d++;
505
506 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
507 my $express = $1;
508
509 if (process_expression($name, $express)) {
510 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
511 } else {
512 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
513 }
514 }
515
516 $d--;
517 my $OR = "\\|\\|";
518 my $AND = "\\&\\&";
519
520 while ($val =~ s/^(.*?)($OR|$AND)//) {
521 my $express = $1;
522 my $op = $2;
523
524 if (process_expression($name, $express)) {
525 if ($op eq "||") {
526 return 1;
527 }
528 } else {
529 if ($op eq "&&") {
530 return 0;
531 }
532 }
533 }
Steven Rostedt45d73a52011-09-30 19:44:53 -0400534
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400535 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
536 my $ret = process_compare($1, $2, $3);
537 if ($ret < 0) {
538 die "$name: $.: Unable to process comparison\n";
539 }
540 return $ret;
541 }
542
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400543 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
544 if (defined $1) {
545 return !value_defined($2);
546 } else {
547 return value_defined($2);
548 }
549 }
550
Steven Rostedt45d73a52011-09-30 19:44:53 -0400551 if ($val =~ /^\s*0\s*$/) {
552 return 0;
553 } elsif ($val =~ /^\s*\d+\s*$/) {
554 return 1;
555 }
556
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400557 die ("$name: $.: Undefined content $val in if statement\n");
Steven Rostedt8d735212011-10-17 11:36:44 -0400558}
559
560sub process_if {
561 my ($name, $value) = @_;
562
563 # Convert variables and replace undefined ones with 0
564 my $val = process_variables($value, 1);
565 my $ret = process_expression $name, $val;
566
567 return $ret;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400568}
569
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400570sub __read_config {
571 my ($config, $current_test_num) = @_;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400572
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400573 my $in;
574 open($in, $config) || die "can't read file $config";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400575
Steven Rostedta57419b2010-11-02 15:13:54 -0400576 my $name = $config;
577 $name =~ s,.*/(.*),$1,;
578
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400579 my $test_num = $$current_test_num;
Steven Rostedta57419b2010-11-02 15:13:54 -0400580 my $default = 1;
581 my $repeat = 1;
582 my $num_tests_set = 0;
583 my $skip = 0;
584 my $rest;
Steven Rostedta9f84422011-10-17 11:06:29 -0400585 my $line;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400586 my $test_case = 0;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400587 my $if = 0;
588 my $if_set = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400589 my $override = 0;
590
591 my %overrides;
Steven Rostedta57419b2010-11-02 15:13:54 -0400592
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400593 while (<$in>) {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400594
595 # ignore blank lines and comments
596 next if (/^\s*$/ || /\s*\#/);
597
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400598 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400599
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400600 my $type = $1;
601 $rest = $2;
Steven Rostedta9f84422011-10-17 11:06:29 -0400602 $line = $2;
Steven Rostedta57419b2010-11-02 15:13:54 -0400603
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400604 my $old_test_num;
605 my $old_repeat;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400606 $override = 0;
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400607
608 if ($type eq "TEST_START") {
609
610 if ($num_tests_set) {
611 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
612 }
613
614 $old_test_num = $test_num;
615 $old_repeat = $repeat;
616
617 $test_num += $repeat;
618 $default = 0;
619 $repeat = 1;
620 } else {
621 $default = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400622 }
623
Steven Rostedta9f84422011-10-17 11:06:29 -0400624 # If SKIP is anywhere in the line, the command will be skipped
625 if ($rest =~ s/\s+SKIP\b//) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400626 $skip = 1;
627 } else {
Steven Rostedt0df213c2011-06-14 20:51:37 -0400628 $test_case = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400629 $skip = 0;
630 }
631
Steven Rostedta9f84422011-10-17 11:06:29 -0400632 if ($rest =~ s/\sELSE\b//) {
633 if (!$if) {
634 die "$name: $.: ELSE found with out matching IF section\n$_";
635 }
636 $if = 0;
637
638 if ($if_set) {
639 $skip = 1;
640 } else {
641 $skip = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400642 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400643 }
644
Steven Rostedta9f84422011-10-17 11:06:29 -0400645 if ($rest =~ s/\sIF\s+(.*)//) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400646 if (process_if($name, $1)) {
647 $if_set = 1;
648 } else {
649 $skip = 1;
650 }
651 $if = 1;
652 } else {
653 $if = 0;
Steven Rostedta9f84422011-10-17 11:06:29 -0400654 $if_set = 0;
Steven Rostedta57419b2010-11-02 15:13:54 -0400655 }
656
Steven Rostedta9f84422011-10-17 11:06:29 -0400657 if (!$skip) {
658 if ($type eq "TEST_START") {
659 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
660 $repeat = $1;
661 $repeat_tests{"$test_num"} = $repeat;
662 }
663 } elsif ($rest =~ s/\sOVERRIDE\b//) {
664 # DEFAULT only
665 $override = 1;
666 # Clear previous overrides
667 %overrides = ();
668 }
669 }
670
671 if (!$skip && $rest !~ /^\s*$/) {
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400672 die "$name: $.: Gargbage found after $type\n$_";
Steven Rostedta57419b2010-11-02 15:13:54 -0400673 }
674
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400675 if ($skip && $type eq "TEST_START") {
Steven Rostedta57419b2010-11-02 15:13:54 -0400676 $test_num = $old_test_num;
Steven Rostedte48c5292010-11-02 14:35:37 -0400677 $repeat = $old_repeat;
Steven Rostedta57419b2010-11-02 15:13:54 -0400678 }
679
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400680 } elsif (/^\s*ELSE\b(.*)$/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400681 if (!$if) {
682 die "$name: $.: ELSE found with out matching IF section\n$_";
683 }
684 $rest = $1;
685 if ($if_set) {
686 $skip = 1;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400687 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400688 } else {
689 $skip = 0;
690
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400691 if ($rest =~ /\sIF\s+(.*)/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400692 # May be a ELSE IF section.
693 if (!process_if($name, $1)) {
694 $skip = 1;
695 }
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400696 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400697 } else {
698 $if = 0;
699 }
700 }
701
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400702 if ($rest !~ /^\s*$/) {
703 die "$name: $.: Gargbage found after DEFAULTS\n$_";
704 }
705
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400706 } elsif (/^\s*INCLUDE\s+(\S+)/) {
707
708 next if ($skip);
709
710 if (!$default) {
711 die "$name: $.: INCLUDE can only be done in default sections\n$_";
712 }
713
714 my $file = process_variables($1);
715
716 if ($file !~ m,^/,) {
717 # check the path of the config file first
718 if ($config =~ m,(.*)/,) {
719 if (-f "$1/$file") {
720 $file = "$1/$file";
721 }
722 }
723 }
724
725 if ( ! -r $file ) {
726 die "$name: $.: Can't read file $file\n$_";
727 }
728
729 if (__read_config($file, \$test_num)) {
730 $test_case = 1;
731 }
732
Steven Rostedta57419b2010-11-02 15:13:54 -0400733 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
734
735 next if ($skip);
736
Steven Rostedt2545eb62010-11-02 15:01:32 -0400737 my $lvalue = $1;
738 my $rvalue = $2;
739
Steven Rostedta57419b2010-11-02 15:13:54 -0400740 if (!$default &&
741 ($lvalue eq "NUM_TESTS" ||
742 $lvalue eq "LOG_FILE" ||
743 $lvalue eq "CLEAR_LOG")) {
744 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400745 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400746
747 if ($lvalue eq "NUM_TESTS") {
748 if ($test_num) {
749 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
750 }
751 if (!$default) {
752 die "$name: $.: NUM_TESTS must be set in default section\n";
753 }
754 $num_tests_set = 1;
755 }
756
757 if ($default || $lvalue =~ /\[\d+\]$/) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400758 set_value($lvalue, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400759 } else {
760 my $val = "$lvalue\[$test_num\]";
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400761 set_value($val, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400762
763 if ($repeat > 1) {
764 $repeats{$val} = $repeat;
765 }
766 }
Steven Rostedt77d942c2011-05-20 13:36:58 -0400767 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
768 next if ($skip);
769
770 my $lvalue = $1;
771 my $rvalue = $2;
772
773 # process config variables.
774 # Config variables are only active while reading the
775 # config and can be defined anywhere. They also ignore
776 # TEST_START and DEFAULTS, but are skipped if they are in
777 # on of these sections that have SKIP defined.
778 # The save variable can be
779 # defined multiple times and the new one simply overrides
780 # the prevous one.
781 set_variable($lvalue, $rvalue);
782
Steven Rostedta57419b2010-11-02 15:13:54 -0400783 } else {
784 die "$name: $.: Garbage found in config\n$_";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400785 }
786 }
787
Steven Rostedta57419b2010-11-02 15:13:54 -0400788 if ($test_num) {
789 $test_num += $repeat - 1;
790 $opt{"NUM_TESTS"} = $test_num;
791 }
792
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400793 close($in);
794
795 $$current_test_num = $test_num;
796
797 return $test_case;
798}
799
Steven Rostedtc4261d02011-11-23 13:41:18 -0500800sub get_test_case {
801 print "What test case would you like to run?\n";
802 print " (build, install or boot)\n";
803 print " Other tests are available but require editing the config file\n";
804 my $ans = <STDIN>;
805 chomp $ans;
806 $default{"TEST_TYPE"} = $ans;
807}
808
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400809sub read_config {
810 my ($config) = @_;
811
812 my $test_case;
813 my $test_num = 0;
814
815 $test_case = __read_config $config, \$test_num;
816
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500817 # make sure we have all mandatory configs
818 get_ktest_configs;
819
Steven Rostedt0df213c2011-06-14 20:51:37 -0400820 # was a test specified?
821 if (!$test_case) {
822 print "No test case specified.\n";
Steven Rostedtc4261d02011-11-23 13:41:18 -0500823 get_test_case;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400824 }
825
Steven Rostedta75fece2010-11-02 14:58:27 -0400826 # set any defaults
827
828 foreach my $default (keys %default) {
829 if (!defined($opt{$default})) {
830 $opt{$default} = $default{$default};
831 }
832 }
Steven Rostedt2545eb62010-11-02 15:01:32 -0400833}
834
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400835sub __eval_option {
836 my ($option, $i) = @_;
837
838 # Add space to evaluate the character before $
839 $option = " $option";
840 my $retval = "";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530841 my $repeated = 0;
842 my $parent = 0;
843
844 foreach my $test (keys %repeat_tests) {
845 if ($i >= $test &&
846 $i < $test + $repeat_tests{$test}) {
847
848 $repeated = 1;
849 $parent = $test;
850 last;
851 }
852 }
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400853
854 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
855 my $start = $1;
856 my $var = $2;
857 my $end = $3;
858
859 # Append beginning of line
860 $retval = "$retval$start";
861
862 # If the iteration option OPT[$i] exists, then use that.
863 # otherwise see if the default OPT (without [$i]) exists.
864
865 my $o = "$var\[$i\]";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530866 my $parento = "$var\[$parent\]";
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400867
868 if (defined($opt{$o})) {
869 $o = $opt{$o};
870 $retval = "$retval$o";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530871 } elsif ($repeated && defined($opt{$parento})) {
872 $o = $opt{$parento};
873 $retval = "$retval$o";
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400874 } elsif (defined($opt{$var})) {
875 $o = $opt{$var};
876 $retval = "$retval$o";
877 } else {
878 $retval = "$retval\$\{$var\}";
879 }
880
881 $option = $end;
882 }
883
884 $retval = "$retval$option";
885
886 $retval =~ s/^ //;
887
888 return $retval;
889}
890
891sub eval_option {
892 my ($option, $i) = @_;
893
894 my $prev = "";
895
896 # Since an option can evaluate to another option,
897 # keep iterating until we do not evaluate any more
898 # options.
899 my $r = 0;
900 while ($prev ne $option) {
901 # Check for recursive evaluations.
902 # 100 deep should be more than enough.
903 if ($r++ > 100) {
904 die "Over 100 evaluations accurred with $option\n" .
905 "Check for recursive variables\n";
906 }
907 $prev = $option;
908 $option = __eval_option($option, $i);
909 }
910
911 return $option;
912}
913
Steven Rostedtd1e2f222010-11-08 16:39:57 -0500914sub _logit {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400915 if (defined($opt{"LOG_FILE"})) {
916 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
917 print OUT @_;
918 close(OUT);
919 }
920}
921
Steven Rostedtd1e2f222010-11-08 16:39:57 -0500922sub logit {
923 if (defined($opt{"LOG_FILE"})) {
924 _logit @_;
925 } else {
926 print @_;
927 }
928}
929
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400930sub doprint {
931 print @_;
Steven Rostedtd1e2f222010-11-08 16:39:57 -0500932 _logit @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400933}
934
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400935sub run_command;
Andrew Jones2728be42011-08-12 15:32:05 +0200936sub start_monitor;
937sub end_monitor;
938sub wait_for_monitor;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400939
940sub reboot {
Andrew Jones2728be42011-08-12 15:32:05 +0200941 my ($time) = @_;
942
Steven Rostedt2b803362011-09-30 18:00:23 -0400943 if (defined($time)) {
944 start_monitor;
945 # flush out current monitor
946 # May contain the reboot success line
947 wait_for_monitor 1;
948 }
949
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400950 # try to reboot normally
Steven Rostedte48c5292010-11-02 14:35:37 -0400951 if (run_command $reboot) {
Steven Rostedt576f6272010-11-02 14:58:38 -0400952 if (defined($powercycle_after_reboot)) {
953 sleep $powercycle_after_reboot;
954 run_command "$power_cycle";
955 }
956 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400957 # nope? power cycle it.
Steven Rostedta75fece2010-11-02 14:58:27 -0400958 run_command "$power_cycle";
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400959 }
Andrew Jones2728be42011-08-12 15:32:05 +0200960
961 if (defined($time)) {
Steven Rostedt2b803362011-09-30 18:00:23 -0400962 wait_for_monitor($time, $reboot_success_line);
Andrew Jones2728be42011-08-12 15:32:05 +0200963 end_monitor;
964 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400965}
966
Steven Rostedt576f6272010-11-02 14:58:38 -0400967sub do_not_reboot {
968 my $i = $iteration;
969
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400970 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -0400971 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
972 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
973}
974
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400975sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400976 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400977
Steven Rostedt576f6272010-11-02 14:58:38 -0400978 my $i = $iteration;
979
980 if ($reboot_on_error && !do_not_reboot) {
981
Steven Rostedt75c3fda72010-11-02 14:57:21 -0400982 doprint "REBOOTING\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400983 reboot;
Steven Rostedt75c3fda72010-11-02 14:57:21 -0400984
Steven Rostedta75fece2010-11-02 14:58:27 -0400985 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400986 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400987 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400988 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -0400989
Steven Rostedtf80802c2011-03-07 13:18:47 -0500990 if (defined($opt{"LOG_FILE"})) {
991 print " See $opt{LOG_FILE} for more info.\n";
992 }
993
Steven Rostedt576f6272010-11-02 14:58:38 -0400994 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400995}
996
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400997sub open_console {
998 my ($fp) = @_;
999
1000 my $flags;
1001
Steven Rostedta75fece2010-11-02 14:58:27 -04001002 my $pid = open($fp, "$console|") or
1003 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001004
1005 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001006 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001007 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001008 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001009
1010 return $pid;
1011}
1012
1013sub close_console {
1014 my ($fp, $pid) = @_;
1015
1016 doprint "kill child process $pid\n";
1017 kill 2, $pid;
1018
1019 print "closing!\n";
1020 close($fp);
1021}
1022
1023sub start_monitor {
1024 if ($monitor_cnt++) {
1025 return;
1026 }
1027 $monitor_fp = \*MONFD;
1028 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001029
1030 return;
1031
1032 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001033}
1034
1035sub end_monitor {
1036 if (--$monitor_cnt) {
1037 return;
1038 }
1039 close_console($monitor_fp, $monitor_pid);
1040}
1041
1042sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001043 my ($time, $stop) = @_;
1044 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001045 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001046 my $booted = 0;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001047
Steven Rostedta75fece2010-11-02 14:58:27 -04001048 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001049
1050 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001051 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001052 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001053 last if (!defined($line));
1054 print "$line";
1055 $full_line .= $line;
1056
1057 if (defined($stop) && $full_line =~ /$stop/) {
1058 doprint "wait for monitor detected $stop\n";
1059 $booted = 1;
1060 }
1061
1062 if ($line =~ /\n/) {
1063 $full_line = "";
1064 }
1065 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001066 print "** Monitor flushed **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001067}
1068
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301069sub save_logs {
1070 my ($result, $basedir) = @_;
1071 my @t = localtime;
1072 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1073 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1074
1075 my $type = $build_type;
1076 if ($type =~ /useconfig/) {
1077 $type = "useconfig";
1078 }
1079
1080 my $dir = "$machine-$test_type-$type-$result-$date";
1081
1082 $dir = "$basedir/$dir";
1083
1084 if (!-d $dir) {
1085 mkpath($dir) or
1086 die "can't create $dir";
1087 }
1088
1089 my %files = (
1090 "config" => $output_config,
1091 "buildlog" => $buildlog,
1092 "dmesg" => $dmesg,
1093 "testlog" => $testlog,
1094 );
1095
1096 while (my ($name, $source) = each(%files)) {
1097 if (-f "$source") {
1098 cp "$source", "$dir/$name" or
1099 die "failed to copy $source";
1100 }
1101 }
1102
1103 doprint "*** Saved info to $dir ***\n";
1104}
1105
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001106sub fail {
1107
Steven Rostedta75fece2010-11-02 14:58:27 -04001108 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001109 dodie @_;
1110 }
1111
Steven Rostedta75fece2010-11-02 14:58:27 -04001112 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001113
Steven Rostedt576f6272010-11-02 14:58:38 -04001114 my $i = $iteration;
1115
Steven Rostedta75fece2010-11-02 14:58:27 -04001116 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001117 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001118 doprint "REBOOTING\n";
Andrew Jones2728be42011-08-12 15:32:05 +02001119 reboot $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001120 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001121
Steven Rostedt9064af52011-06-13 10:38:48 -04001122 my $name = "";
1123
1124 if (defined($test_name)) {
1125 $name = " ($test_name)";
1126 }
1127
Steven Rostedt576f6272010-11-02 14:58:38 -04001128 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1129 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001130 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001131 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1132 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001133
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301134 if (defined($store_failures)) {
1135 save_logs "fail", $store_failures;
1136 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001137
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001138 return 1;
1139}
1140
Steven Rostedt2545eb62010-11-02 15:01:32 -04001141sub run_command {
1142 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001143 my $dolog = 0;
1144 my $dord = 0;
1145 my $pid;
1146
Steven Rostedte48c5292010-11-02 14:35:37 -04001147 $command =~ s/\$SSH_USER/$ssh_user/g;
1148 $command =~ s/\$MACHINE/$machine/g;
1149
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001150 doprint("$command ... ");
1151
1152 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001153 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001154
1155 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001156 open(LOG, ">>$opt{LOG_FILE}") or
1157 dodie "failed to write to log";
1158 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001159 }
1160
1161 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001162 open (RD, ">$redirect") or
1163 dodie "failed to write to redirect $redirect";
1164 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001165 }
1166
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001167 while (<CMD>) {
1168 print LOG if ($dolog);
1169 print RD if ($dord);
1170 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001171
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001172 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001173 my $failed = $?;
1174
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001175 close(CMD);
1176 close(LOG) if ($dolog);
1177 close(RD) if ($dord);
1178
Steven Rostedt2545eb62010-11-02 15:01:32 -04001179 if ($failed) {
1180 doprint "FAILED!\n";
1181 } else {
1182 doprint "SUCCESS\n";
1183 }
1184
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001185 return !$failed;
1186}
1187
Steven Rostedte48c5292010-11-02 14:35:37 -04001188sub run_ssh {
1189 my ($cmd) = @_;
1190 my $cp_exec = $ssh_exec;
1191
1192 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1193 return run_command "$cp_exec";
1194}
1195
1196sub run_scp {
1197 my ($src, $dst) = @_;
1198 my $cp_scp = $scp_to_target;
1199
1200 $cp_scp =~ s/\$SRC_FILE/$src/g;
1201 $cp_scp =~ s/\$DST_FILE/$dst/g;
1202
1203 return run_command "$cp_scp";
1204}
1205
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001206sub get_grub_index {
1207
Steven Rostedta75fece2010-11-02 14:58:27 -04001208 if ($reboot_type ne "grub") {
1209 return;
1210 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001211 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001212
1213 doprint "Find grub menu ... ";
1214 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001215
1216 my $ssh_grub = $ssh_exec;
1217 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1218
1219 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001220 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001221
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001222 my $found = 0;
1223
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001224 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001225 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001226 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001227 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001228 last;
1229 } elsif (/^\s*title\s/) {
1230 $grub_number++;
1231 }
1232 }
1233 close(IN);
1234
Steven Rostedta75fece2010-11-02 14:58:27 -04001235 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001236 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001237 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001238}
1239
Steven Rostedt2545eb62010-11-02 15:01:32 -04001240sub wait_for_input
1241{
1242 my ($fp, $time) = @_;
1243 my $rin;
1244 my $ready;
1245 my $line;
1246 my $ch;
1247
1248 if (!defined($time)) {
1249 $time = $timeout;
1250 }
1251
1252 $rin = '';
1253 vec($rin, fileno($fp), 1) = 1;
1254 $ready = select($rin, undef, undef, $time);
1255
1256 $line = "";
1257
1258 # try to read one char at a time
1259 while (sysread $fp, $ch, 1) {
1260 $line .= $ch;
1261 last if ($ch eq "\n");
1262 }
1263
1264 if (!length($line)) {
1265 return undef;
1266 }
1267
1268 return $line;
1269}
1270
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001271sub reboot_to {
Steven Rostedta75fece2010-11-02 14:58:27 -04001272 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001273 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1274 reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -04001275 return;
1276 }
1277
1278 run_command "$reboot_script";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001279}
1280
Steven Rostedta57419b2010-11-02 15:13:54 -04001281sub get_sha1 {
1282 my ($commit) = @_;
1283
1284 doprint "git rev-list --max-count=1 $commit ... ";
1285 my $sha1 = `git rev-list --max-count=1 $commit`;
1286 my $ret = $?;
1287
1288 logit $sha1;
1289
1290 if ($ret) {
1291 doprint "FAILED\n";
1292 dodie "Failed to get git $commit";
1293 }
1294
1295 print "SUCCESS\n";
1296
1297 chomp $sha1;
1298
1299 return $sha1;
1300}
1301
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001302sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001303 my $booted = 0;
1304 my $bug = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001305 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001306 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001307
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001308 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001309
1310 my $line;
1311 my $full_line = "";
1312
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001313 open(DMESG, "> $dmesg") or
1314 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001315
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001316 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001317
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001318 my $success_start;
1319 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001320 my $monitor_start = time;
1321 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001322 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001323
Steven Rostedt2d01b262011-03-08 09:47:54 -05001324 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001325
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001326 if ($bug && defined($stop_after_failure) &&
1327 $stop_after_failure >= 0) {
1328 my $time = $stop_after_failure - (time - $failure_start);
1329 $line = wait_for_input($monitor_fp, $time);
1330 if (!defined($line)) {
1331 doprint "bug timed out after $booted_timeout seconds\n";
1332 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1333 last;
1334 }
1335 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001336 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001337 if (!defined($line)) {
1338 my $s = $booted_timeout == 1 ? "" : "s";
1339 doprint "Successful boot found: break after $booted_timeout second$s\n";
1340 last;
1341 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001342 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001343 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001344 if (!defined($line)) {
1345 my $s = $timeout == 1 ? "" : "s";
1346 doprint "Timed out after $timeout second$s\n";
1347 last;
1348 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001349 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001350
Steven Rostedt2545eb62010-11-02 15:01:32 -04001351 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001352 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001353
1354 # we are not guaranteed to get a full line
1355 $full_line .= $line;
1356
Steven Rostedta75fece2010-11-02 14:58:27 -04001357 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001358 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001359 $success_start = time;
1360 }
1361
1362 if ($booted && defined($stop_after_success) &&
1363 $stop_after_success >= 0) {
1364 my $now = time;
1365 if ($now - $success_start >= $stop_after_success) {
1366 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1367 last;
1368 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001369 }
1370
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001371 if ($full_line =~ /\[ backtrace testing \]/) {
1372 $skip_call_trace = 1;
1373 }
1374
Steven Rostedt2545eb62010-11-02 15:01:32 -04001375 if ($full_line =~ /call trace:/i) {
Steven Rostedt46519202011-03-08 09:40:31 -05001376 if (!$bug && !$skip_call_trace) {
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001377 $bug = 1;
1378 $failure_start = time;
1379 }
1380 }
1381
1382 if ($bug && defined($stop_after_failure) &&
1383 $stop_after_failure >= 0) {
1384 my $now = time;
1385 if ($now - $failure_start >= $stop_after_failure) {
1386 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1387 last;
1388 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001389 }
1390
1391 if ($full_line =~ /\[ end of backtrace testing \]/) {
1392 $skip_call_trace = 0;
1393 }
1394
1395 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001396 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001397 $bug = 1;
1398 }
1399
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001400 # Detect triple faults by testing the banner
1401 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1402 if ($1 eq $version) {
1403 $version_found = 1;
1404 } elsif ($version_found && $detect_triplefault) {
1405 # We already booted into the kernel we are testing,
1406 # but now we booted into another kernel?
1407 # Consider this a triple fault.
1408 doprint "Aleady booted in Linux kernel $version, but now\n";
1409 doprint "we booted into Linux kernel $1.\n";
1410 doprint "Assuming that this is a triple fault.\n";
1411 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1412 last;
1413 }
1414 }
1415
Steven Rostedt2545eb62010-11-02 15:01:32 -04001416 if ($line =~ /\n/) {
1417 $full_line = "";
1418 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001419
1420 if ($stop_test_after > 0 && !$booted && !$bug) {
1421 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001422 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001423 $done = 1;
1424 }
1425 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001426 }
1427
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001428 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001429
Steven Rostedt2545eb62010-11-02 15:01:32 -04001430 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001431 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001432 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001433 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001434
Steven Rostedta75fece2010-11-02 14:58:27 -04001435 if (!$booted) {
1436 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001437 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001438 }
1439
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001440 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001441}
1442
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001443sub eval_kernel_version {
1444 my ($option) = @_;
1445
1446 $option =~ s/\$KERNEL_VERSION/$version/g;
1447
1448 return $option;
1449}
1450
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001451sub do_post_install {
1452
1453 return if (!defined($post_install));
1454
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001455 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001456 run_command "$cp_post_install" or
1457 dodie "Failed to run post install";
1458}
1459
Steven Rostedt2545eb62010-11-02 15:01:32 -04001460sub install {
1461
Steven Rostedte0a87422011-09-30 17:50:48 -04001462 return if ($no_install);
1463
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001464 my $cp_target = eval_kernel_version $target_image;
1465
1466 run_scp "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001467 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001468
1469 my $install_mods = 0;
1470
1471 # should we process modules?
1472 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001473 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001474 while (<IN>) {
1475 if (/CONFIG_MODULES(=y)?/) {
1476 $install_mods = 1 if (defined($1));
1477 last;
1478 }
1479 }
1480 close(IN);
1481
1482 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001483 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001484 doprint "No modules needed\n";
1485 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001486 }
1487
Steven Rostedta75fece2010-11-02 14:58:27 -04001488 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001489 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001490
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001491 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001492 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001493
Steven Rostedte48c5292010-11-02 14:35:37 -04001494 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001495 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001496
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001497 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001498 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001499 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001500
Steven Rostedte48c5292010-11-02 14:35:37 -04001501 run_scp "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001502 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001503
Steven Rostedta75fece2010-11-02 14:58:27 -04001504 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001505
Steven Rostedte7b13442011-06-14 20:44:36 -04001506 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001507 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001508
Steven Rostedte48c5292010-11-02 14:35:37 -04001509 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001510
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001511 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001512}
1513
Steven Rostedtddf607e2011-06-14 20:49:13 -04001514sub get_version {
1515 # get the release name
1516 doprint "$make kernelrelease ... ";
1517 $version = `$make kernelrelease | tail -1`;
1518 chomp($version);
1519 doprint "$version\n";
1520}
1521
1522sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001523 # Make sure the stable kernel has finished booting
1524 start_monitor;
1525 wait_for_monitor 5;
1526 end_monitor;
1527
Steven Rostedtddf607e2011-06-14 20:49:13 -04001528 get_grub_index;
1529 get_version;
1530 install;
1531
1532 start_monitor;
1533 return monitor;
1534}
1535
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001536sub check_buildlog {
1537 my ($patch) = @_;
1538
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001539 my @files = `git show $patch | diffstat -l`;
1540
1541 open(IN, "git show $patch |") or
1542 dodie "failed to show $patch";
1543 while (<IN>) {
1544 if (m,^--- a/(.*),) {
1545 chomp $1;
1546 $files[$#files] = $1;
1547 }
1548 }
1549 close(IN);
1550
1551 open(IN, $buildlog) or dodie "Can't open $buildlog";
1552 while (<IN>) {
1553 if (/^\s*(.*?):.*(warning|error)/) {
1554 my $err = $1;
1555 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001556 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001557 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001558 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001559 }
1560 }
1561 }
1562 }
1563 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001564
1565 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001566}
1567
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001568sub apply_min_config {
1569 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05001570
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001571 # Read the config file and remove anything that
1572 # is in the force_config hash (from minconfig and others)
1573 # then add the force config back.
1574
1575 doprint "Applying minimum configurations into $output_config.new\n";
1576
1577 open (OUT, ">$outconfig") or
1578 dodie "Can't create $outconfig";
1579
1580 if (-f $output_config) {
1581 open (IN, $output_config) or
1582 dodie "Failed to open $output_config";
1583 while (<IN>) {
1584 if (/^(# )?(CONFIG_[^\s=]*)/) {
1585 next if (defined($force_config{$2}));
1586 }
1587 print OUT;
1588 }
1589 close IN;
1590 }
1591 foreach my $config (keys %force_config) {
1592 print OUT "$force_config{$config}\n";
1593 }
1594 close OUT;
1595
1596 run_command "mv $outconfig $output_config";
1597}
1598
1599sub make_oldconfig {
1600
Steven Rostedt4c4ab122011-07-15 21:16:17 -04001601 my @force_list = keys %force_config;
1602
1603 if ($#force_list >= 0) {
1604 apply_min_config;
1605 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001606
1607 if (!run_command "$make oldnoconfig") {
Steven Rostedt612b9e92011-03-07 13:27:43 -05001608 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1609 # try a yes '' | oldconfig
1610 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001611 run_command "yes '' | $make oldconfig" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001612 dodie "failed make config oldconfig";
1613 }
1614}
1615
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001616# read a config file and use this to force new configs.
1617sub load_force_config {
1618 my ($config) = @_;
1619
1620 open(IN, $config) or
1621 dodie "failed to read $config";
1622 while (<IN>) {
1623 chomp;
1624 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1625 $force_config{$1} = $_;
1626 } elsif (/^# (CONFIG_\S*) is not set/) {
1627 $force_config{$1} = $_;
1628 }
1629 }
1630 close IN;
1631}
1632
Steven Rostedt2545eb62010-11-02 15:01:32 -04001633sub build {
1634 my ($type) = @_;
1635
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001636 unlink $buildlog;
1637
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001638 # Failed builds should not reboot the target
1639 my $save_no_reboot = $no_reboot;
1640 $no_reboot = 1;
1641
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001642 if (defined($pre_build)) {
1643 my $ret = run_command $pre_build;
1644 if (!$ret && defined($pre_build_die) &&
1645 $pre_build_die) {
1646 dodie "failed to pre_build\n";
1647 }
1648 }
1649
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001650 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001651 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001652 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001653
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001654 $type = "oldconfig";
1655 }
1656
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001657 # old config can ask questions
1658 if ($type eq "oldconfig") {
Steven Rostedt9386c6a2010-11-08 16:35:48 -05001659 $type = "oldnoconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001660
1661 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001662 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001663
Andrew Jones13488232011-08-12 15:32:04 +02001664 if (!$noclean) {
1665 run_command "mv $output_config $outputdir/config_temp" or
1666 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001667
Andrew Jones13488232011-08-12 15:32:04 +02001668 run_command "$make mrproper" or dodie "make mrproper";
1669
1670 run_command "mv $outputdir/config_temp $output_config" or
1671 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001672 }
1673
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001674 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001675 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001676 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001677 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001678 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001679
1680 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04001681 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1682 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001683 close(OUT);
1684
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001685 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001686 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001687 }
1688
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001689 if ($type ne "oldnoconfig") {
1690 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001691 dodie "failed make config";
1692 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001693 # Run old config regardless, to enforce min configurations
1694 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001695
Steven Rostedta75fece2010-11-02 14:58:27 -04001696 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001697 my $build_ret = run_command "$make $build_options";
1698 undef $redirect;
1699
1700 if (defined($post_build)) {
1701 my $ret = run_command $post_build;
1702 if (!$ret && defined($post_build_die) &&
1703 $post_build_die) {
1704 dodie "failed to post_build\n";
1705 }
1706 }
1707
1708 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001709 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001710 if ($in_bisect) {
1711 $no_reboot = $save_no_reboot;
1712 return 0;
1713 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001714 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001715 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001716
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001717 $no_reboot = $save_no_reboot;
1718
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001719 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001720}
1721
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001722sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04001723 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001724 if (defined($poweroff_after_halt)) {
1725 sleep $poweroff_after_halt;
1726 run_command "$power_off";
1727 }
1728 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001729 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04001730 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001731 }
1732}
1733
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001734sub success {
1735 my ($i) = @_;
1736
Steven Rostedte48c5292010-11-02 14:35:37 -04001737 $successes++;
1738
Steven Rostedt9064af52011-06-13 10:38:48 -04001739 my $name = "";
1740
1741 if (defined($test_name)) {
1742 $name = " ($test_name)";
1743 }
1744
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001745 doprint "\n\n*******************************************\n";
1746 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001747 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001748 doprint "*******************************************\n";
1749 doprint "*******************************************\n";
1750
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301751 if (defined($store_successes)) {
1752 save_logs "success", $store_successes;
1753 }
1754
Steven Rostedt576f6272010-11-02 14:58:38 -04001755 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001756 doprint "Reboot and wait $sleep_time seconds\n";
Andrew Jones2728be42011-08-12 15:32:05 +02001757 reboot $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001758 }
1759}
1760
Steven Rostedtc960bb92011-03-08 09:22:39 -05001761sub answer_bisect {
1762 for (;;) {
1763 doprint "Pass or fail? [p/f]";
1764 my $ans = <STDIN>;
1765 chomp $ans;
1766 if ($ans eq "p" || $ans eq "P") {
1767 return 1;
1768 } elsif ($ans eq "f" || $ans eq "F") {
1769 return 0;
1770 } else {
1771 print "Please answer 'P' or 'F'\n";
1772 }
1773 }
1774}
1775
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001776sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001777 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001778
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001779 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04001780 $reboot_on_error = 0;
1781 $poweroff_on_error = 0;
1782 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001783
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301784 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001785 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301786 undef $redirect;
1787
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001788 exit $failed;
1789}
1790
1791my $child_done;
1792
1793sub child_finished {
1794 $child_done = 1;
1795}
1796
1797sub do_run_test {
1798 my $child_pid;
1799 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001800 my $line;
1801 my $full_line;
1802 my $bug = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001803
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001804 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001805
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001806 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001807
1808 $child_done = 0;
1809
1810 $SIG{CHLD} = qw(child_finished);
1811
1812 $child_pid = fork;
1813
1814 child_run_test if (!$child_pid);
1815
1816 $full_line = "";
1817
1818 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001819 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001820 if (defined($line)) {
1821
1822 # we are not guaranteed to get a full line
1823 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05001824 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001825
1826 if ($full_line =~ /call trace:/i) {
1827 $bug = 1;
1828 }
1829
1830 if ($full_line =~ /Kernel panic -/) {
1831 $bug = 1;
1832 }
1833
1834 if ($line =~ /\n/) {
1835 $full_line = "";
1836 }
1837 }
1838 } while (!$child_done && !$bug);
1839
1840 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05001841 my $failure_start = time;
1842 my $now;
1843 do {
1844 $line = wait_for_input($monitor_fp, 1);
1845 if (defined($line)) {
1846 doprint $line;
1847 }
1848 $now = time;
1849 if ($now - $failure_start >= $stop_after_failure) {
1850 last;
1851 }
1852 } while (defined($line));
1853
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001854 doprint "Detected kernel crash!\n";
1855 # kill the child with extreme prejudice
1856 kill 9, $child_pid;
1857 }
1858
1859 waitpid $child_pid, 0;
1860 $child_exit = $?;
1861
Steven Rostedtc5dacb82011-12-22 12:43:57 -05001862 if (!$bug && $in_bisect) {
1863 if (defined($bisect_ret_good)) {
1864 if ($child_exit == $bisect_ret_good) {
1865 return 1;
1866 }
1867 }
1868 if (defined($bisect_ret_skip)) {
1869 if ($child_exit == $bisect_ret_skip) {
1870 return -1;
1871 }
1872 }
1873 if (defined($bisect_ret_abort)) {
1874 if ($child_exit == $bisect_ret_abort) {
1875 fail "test abort" and return -2;
1876 }
1877 }
1878 if (defined($bisect_ret_bad)) {
1879 if ($child_exit == $bisect_ret_skip) {
1880 return 0;
1881 }
1882 }
1883 if (defined($bisect_ret_default)) {
1884 if ($bisect_ret_default eq "good") {
1885 return 1;
1886 } elsif ($bisect_ret_default eq "bad") {
1887 return 0;
1888 } elsif ($bisect_ret_default eq "skip") {
1889 return -1;
1890 } elsif ($bisect_ret_default eq "abort") {
1891 return -2;
1892 } else {
1893 fail "unknown default action: $bisect_ret_default"
1894 and return -2;
1895 }
1896 }
1897 }
1898
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001899 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001900 return 0 if $in_bisect;
1901 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001902 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001903 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001904}
1905
Steven Rostedta75fece2010-11-02 14:58:27 -04001906sub run_git_bisect {
1907 my ($command) = @_;
1908
1909 doprint "$command ... ";
1910
1911 my $output = `$command 2>&1`;
1912 my $ret = $?;
1913
1914 logit $output;
1915
1916 if ($ret) {
1917 doprint "FAILED\n";
1918 dodie "Failed to git bisect";
1919 }
1920
1921 doprint "SUCCESS\n";
1922 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1923 doprint "$1 [$2]\n";
1924 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
1925 $bisect_bad = $1;
1926 doprint "Found bad commit... $1\n";
1927 return 0;
1928 } else {
1929 # we already logged it, just print it now.
1930 print $output;
1931 }
1932
1933 return 1;
1934}
1935
Steven Rostedtc23dca72011-03-08 09:26:31 -05001936sub bisect_reboot {
1937 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Andrew Jones2728be42011-08-12 15:32:05 +02001938 reboot $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05001939}
1940
1941# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05001942sub run_bisect_test {
1943 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001944
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001945 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001946 my $result;
1947 my $output;
1948 my $ret;
1949
Steven Rostedt0a05c762010-11-08 11:14:10 -05001950 $in_bisect = 1;
1951
1952 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001953
1954 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05001955 if ($failed && $bisect_skip) {
1956 $in_bisect = 0;
1957 return -1;
1958 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001959 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001960
1961 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04001962 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001963
1964 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05001965 if ($failed && $bisect_skip) {
1966 end_monitor;
1967 bisect_reboot;
1968 $in_bisect = 0;
1969 return -1;
1970 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001971 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001972
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001973 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001974 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001975 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001976 }
1977
1978 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05001979 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001980 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05001981 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001982 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04001983
1984 # reboot the box to a kernel we can ssh to
1985 if ($type ne "build") {
1986 bisect_reboot;
1987 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05001988 $in_bisect = 0;
1989
1990 return $result;
1991}
1992
1993sub run_bisect {
1994 my ($type) = @_;
1995 my $buildtype = "oldconfig";
1996
1997 # We should have a minconfig to use?
1998 if (defined($minconfig)) {
1999 $buildtype = "useconfig:$minconfig";
2000 }
2001
2002 my $ret = run_bisect_test $type, $buildtype;
2003
Steven Rostedtc960bb92011-03-08 09:22:39 -05002004 if ($bisect_manual) {
2005 $ret = answer_bisect;
2006 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002007
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002008 # Are we looking for where it worked, not failed?
2009 if ($reverse_bisect) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002010 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002011 }
2012
Steven Rostedtc23dca72011-03-08 09:26:31 -05002013 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002014 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002015 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002016 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002017 } elsif ($bisect_skip) {
2018 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2019 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002020 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002021}
2022
Steven Rostedtdad98752011-11-22 20:48:57 -05002023sub update_bisect_replay {
2024 my $tmp_log = "$tmpdir/ktest_bisect_log";
2025 run_command "git bisect log > $tmp_log" or
2026 die "can't create bisect log";
2027 return $tmp_log;
2028}
2029
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002030sub bisect {
2031 my ($i) = @_;
2032
2033 my $result;
2034
2035 die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
2036 die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
2037 die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
2038
2039 my $good = $opt{"BISECT_GOOD[$i]"};
2040 my $bad = $opt{"BISECT_BAD[$i]"};
2041 my $type = $opt{"BISECT_TYPE[$i]"};
Steven Rostedta75fece2010-11-02 14:58:27 -04002042 my $start = $opt{"BISECT_START[$i]"};
2043 my $replay = $opt{"BISECT_REPLAY[$i]"};
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002044 my $start_files = $opt{"BISECT_FILES[$i]"};
2045
2046 if (defined($start_files)) {
2047 $start_files = " -- " . $start_files;
2048 } else {
2049 $start_files = "";
2050 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002051
Steven Rostedta57419b2010-11-02 15:13:54 -04002052 # convert to true sha1's
2053 $good = get_sha1($good);
2054 $bad = get_sha1($bad);
2055
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002056 if (defined($opt{"BISECT_REVERSE[$i]"}) &&
2057 $opt{"BISECT_REVERSE[$i]"} == 1) {
2058 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2059 $reverse_bisect = 1;
2060 } else {
2061 $reverse_bisect = 0;
2062 }
2063
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002064 # Can't have a test without having a test to run
2065 if ($type eq "test" && !defined($run_test)) {
2066 $type = "boot";
2067 }
2068
Steven Rostedtdad98752011-11-22 20:48:57 -05002069 # Check if a bisect was running
2070 my $bisect_start_file = "$builddir/.git/BISECT_START";
2071
Steven Rostedta75fece2010-11-02 14:58:27 -04002072 my $check = $opt{"BISECT_CHECK[$i]"};
Steven Rostedtdad98752011-11-22 20:48:57 -05002073 my $do_check = defined($check) && $check ne "0";
2074
2075 if ( -f $bisect_start_file ) {
2076 print "Bisect in progress found\n";
2077 if ($do_check) {
2078 print " If you say yes, then no checks of good or bad will be done\n";
2079 }
2080 if (defined($replay)) {
2081 print "** BISECT_REPLAY is defined in config file **";
2082 print " Ignore config option and perform new git bisect log?\n";
2083 if (read_ync " (yes, no, or cancel) ") {
2084 $replay = update_bisect_replay;
2085 $do_check = 0;
2086 }
2087 } elsif (read_yn "read git log and continue?") {
2088 $replay = update_bisect_replay;
2089 $do_check = 0;
2090 }
2091 }
2092
2093 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002094
2095 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002096 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002097
2098 if ($check ne "good") {
2099 doprint "TESTING BISECT BAD [$bad]\n";
2100 run_command "git checkout $bad" or
2101 die "Failed to checkout $bad";
2102
2103 $result = run_bisect $type;
2104
2105 if ($result ne "bad") {
2106 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2107 }
2108 }
2109
2110 if ($check ne "bad") {
2111 doprint "TESTING BISECT GOOD [$good]\n";
2112 run_command "git checkout $good" or
2113 die "Failed to checkout $good";
2114
2115 $result = run_bisect $type;
2116
2117 if ($result ne "good") {
2118 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2119 }
2120 }
2121
2122 # checkout where we started
2123 run_command "git checkout $head" or
2124 die "Failed to checkout $head";
2125 }
2126
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002127 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002128 dodie "could not start bisect";
2129
2130 run_command "git bisect good $good" or
2131 dodie "could not set bisect good to $good";
2132
2133 run_git_bisect "git bisect bad $bad" or
2134 dodie "could not set bisect bad to $bad";
2135
2136 if (defined($replay)) {
2137 run_command "git bisect replay $replay" or
2138 dodie "failed to run replay";
2139 }
2140
2141 if (defined($start)) {
2142 run_command "git checkout $start" or
2143 dodie "failed to checkout $start";
2144 }
2145
2146 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002147 do {
2148 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002149 $test = run_git_bisect "git bisect $result";
2150 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002151
2152 run_command "git bisect log" or
2153 dodie "could not capture git bisect log";
2154
2155 run_command "git bisect reset" or
2156 dodie "could not reset git bisect";
2157
2158 doprint "Bad commit was [$bisect_bad]\n";
2159
Steven Rostedt0a05c762010-11-08 11:14:10 -05002160 success $i;
2161}
2162
2163my %config_ignore;
2164my %config_set;
2165
2166my %config_list;
2167my %null_config;
2168
2169my %dependency;
2170
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002171sub assign_configs {
2172 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002173
2174 open (IN, $config)
2175 or dodie "Failed to read $config";
2176
2177 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002178 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002179 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002180 }
2181 }
2182
2183 close(IN);
2184}
2185
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002186sub process_config_ignore {
2187 my ($config) = @_;
2188
2189 assign_configs \%config_ignore, $config;
2190}
2191
Steven Rostedt0a05c762010-11-08 11:14:10 -05002192sub read_current_config {
2193 my ($config_ref) = @_;
2194
2195 %{$config_ref} = ();
2196 undef %{$config_ref};
2197
2198 my @key = keys %{$config_ref};
2199 if ($#key >= 0) {
2200 print "did not delete!\n";
2201 exit;
2202 }
2203 open (IN, "$output_config");
2204
2205 while (<IN>) {
2206 if (/^(CONFIG\S+)=(.*)/) {
2207 ${$config_ref}{$1} = $2;
2208 }
2209 }
2210 close(IN);
2211}
2212
2213sub get_dependencies {
2214 my ($config) = @_;
2215
2216 my $arr = $dependency{$config};
2217 if (!defined($arr)) {
2218 return ();
2219 }
2220
2221 my @deps = @{$arr};
2222
2223 foreach my $dep (@{$arr}) {
2224 print "ADD DEP $dep\n";
2225 @deps = (@deps, get_dependencies $dep);
2226 }
2227
2228 return @deps;
2229}
2230
2231sub create_config {
2232 my @configs = @_;
2233
2234 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2235
2236 foreach my $config (@configs) {
2237 print OUT "$config_set{$config}\n";
2238 my @deps = get_dependencies $config;
2239 foreach my $dep (@deps) {
2240 print OUT "$config_set{$dep}\n";
2241 }
2242 }
2243
2244 foreach my $config (keys %config_ignore) {
2245 print OUT "$config_ignore{$config}\n";
2246 }
2247 close(OUT);
2248
2249# exit;
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002250 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002251}
2252
2253sub compare_configs {
2254 my (%a, %b) = @_;
2255
2256 foreach my $item (keys %a) {
2257 if (!defined($b{$item})) {
2258 print "diff $item\n";
2259 return 1;
2260 }
2261 delete $b{$item};
2262 }
2263
2264 my @keys = keys %b;
2265 if ($#keys) {
2266 print "diff2 $keys[0]\n";
2267 }
2268 return -1 if ($#keys >= 0);
2269
2270 return 0;
2271}
2272
2273sub run_config_bisect_test {
2274 my ($type) = @_;
2275
2276 return run_bisect_test $type, "oldconfig";
2277}
2278
2279sub process_passed {
2280 my (%configs) = @_;
2281
2282 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2283 # Passed! All these configs are part of a good compile.
2284 # Add them to the min options.
2285 foreach my $config (keys %configs) {
2286 if (defined($config_list{$config})) {
2287 doprint " removing $config\n";
2288 $config_ignore{$config} = $config_list{$config};
2289 delete $config_list{$config};
2290 }
2291 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002292 doprint "config copied to $outputdir/config_good\n";
2293 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002294}
2295
2296sub process_failed {
2297 my ($config) = @_;
2298
2299 doprint "\n\n***************************************\n";
2300 doprint "Found bad config: $config\n";
2301 doprint "***************************************\n\n";
2302}
2303
2304sub run_config_bisect {
2305
2306 my @start_list = keys %config_list;
2307
2308 if ($#start_list < 0) {
2309 doprint "No more configs to test!!!\n";
2310 return -1;
2311 }
2312
2313 doprint "***** RUN TEST ***\n";
2314 my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
2315 my $ret;
2316 my %current_config;
2317
2318 my $count = $#start_list + 1;
2319 doprint " $count configs to test\n";
2320
2321 my $half = int($#start_list / 2);
2322
2323 do {
2324 my @tophalf = @start_list[0 .. $half];
2325
2326 create_config @tophalf;
2327 read_current_config \%current_config;
2328
2329 $count = $#tophalf + 1;
2330 doprint "Testing $count configs\n";
2331 my $found = 0;
2332 # make sure we test something
2333 foreach my $config (@tophalf) {
2334 if (defined($current_config{$config})) {
2335 logit " $config\n";
2336 $found = 1;
2337 }
2338 }
2339 if (!$found) {
2340 # try the other half
2341 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002342 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedt0a05c762010-11-08 11:14:10 -05002343 create_config @tophalf;
2344 read_current_config \%current_config;
2345 foreach my $config (@tophalf) {
2346 if (defined($current_config{$config})) {
2347 logit " $config\n";
2348 $found = 1;
2349 }
2350 }
2351 if (!$found) {
2352 doprint "Failed: Can't make new config with current configs\n";
2353 foreach my $config (@start_list) {
2354 doprint " CONFIG: $config\n";
2355 }
2356 return -1;
2357 }
2358 $count = $#tophalf + 1;
2359 doprint "Testing $count configs\n";
2360 }
2361
2362 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002363 if ($bisect_manual) {
2364 $ret = answer_bisect;
2365 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002366 if ($ret) {
2367 process_passed %current_config;
2368 return 0;
2369 }
2370
2371 doprint "This config had a failure.\n";
2372 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002373 doprint "config copied to $outputdir/config_bad\n";
2374 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002375
2376 # A config exists in this group that was bad.
2377 foreach my $config (keys %config_list) {
2378 if (!defined($current_config{$config})) {
2379 doprint " removing $config\n";
2380 delete $config_list{$config};
2381 }
2382 }
2383
2384 @start_list = @tophalf;
2385
2386 if ($#start_list == 0) {
2387 process_failed $start_list[0];
2388 return 1;
2389 }
2390
2391 # remove half the configs we are looking at and see if
2392 # they are good.
2393 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002394 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002395
Steven Rostedtc960bb92011-03-08 09:22:39 -05002396 # we found a single config, try it again unless we are running manually
2397
2398 if ($bisect_manual) {
2399 process_failed $start_list[0];
2400 return 1;
2401 }
2402
Steven Rostedt0a05c762010-11-08 11:14:10 -05002403 my @tophalf = @start_list[0 .. 0];
2404
2405 $ret = run_config_bisect_test $type;
2406 if ($ret) {
2407 process_passed %current_config;
2408 return 0;
2409 }
2410
2411 process_failed $start_list[0];
2412 return 1;
2413}
2414
2415sub config_bisect {
2416 my ($i) = @_;
2417
2418 my $start_config = $opt{"CONFIG_BISECT[$i]"};
2419
2420 my $tmpconfig = "$tmpdir/use_config";
2421
Steven Rostedt30f75da2011-06-13 10:35:35 -04002422 if (defined($config_bisect_good)) {
2423 process_config_ignore $config_bisect_good;
2424 }
2425
Steven Rostedt0a05c762010-11-08 11:14:10 -05002426 # Make the file with the bad config and the min config
2427 if (defined($minconfig)) {
2428 # read the min config for things to ignore
2429 run_command "cp $minconfig $tmpconfig" or
2430 dodie "failed to copy $minconfig to $tmpconfig";
2431 } else {
2432 unlink $tmpconfig;
2433 }
2434
Steven Rostedt0a05c762010-11-08 11:14:10 -05002435 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002436 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002437 process_config_ignore $tmpconfig;
2438 }
2439
2440 # now process the start config
2441 run_command "cp $start_config $output_config" or
2442 dodie "failed to copy $start_config to $output_config";
2443
2444 # read directly what we want to check
2445 my %config_check;
2446 open (IN, $output_config)
2447 or dodie "faied to open $output_config";
2448
2449 while (<IN>) {
2450 if (/^((CONFIG\S*)=.*)/) {
2451 $config_check{$2} = $1;
2452 }
2453 }
2454 close(IN);
2455
Steven Rostedt250bae82011-07-15 22:05:59 -04002456 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002457 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002458
2459 # check to see what we lost (or gained)
2460 open (IN, $output_config)
2461 or dodie "Failed to read $start_config";
2462
2463 my %removed_configs;
2464 my %added_configs;
2465
2466 while (<IN>) {
2467 if (/^((CONFIG\S*)=.*)/) {
2468 # save off all options
2469 $config_set{$2} = $1;
2470 if (defined($config_check{$2})) {
2471 if (defined($config_ignore{$2})) {
2472 $removed_configs{$2} = $1;
2473 } else {
2474 $config_list{$2} = $1;
2475 }
2476 } elsif (!defined($config_ignore{$2})) {
2477 $added_configs{$2} = $1;
2478 $config_list{$2} = $1;
2479 }
2480 }
2481 }
2482 close(IN);
2483
2484 my @confs = keys %removed_configs;
2485 if ($#confs >= 0) {
2486 doprint "Configs overridden by default configs and removed from check:\n";
2487 foreach my $config (@confs) {
2488 doprint " $config\n";
2489 }
2490 }
2491 @confs = keys %added_configs;
2492 if ($#confs >= 0) {
2493 doprint "Configs appearing in make oldconfig and added:\n";
2494 foreach my $config (@confs) {
2495 doprint " $config\n";
2496 }
2497 }
2498
2499 my %config_test;
2500 my $once = 0;
2501
2502 # Sometimes kconfig does weird things. We must make sure
2503 # that the config we autocreate has everything we need
2504 # to test, otherwise we may miss testing configs, or
2505 # may not be able to create a new config.
2506 # Here we create a config with everything set.
2507 create_config (keys %config_list);
2508 read_current_config \%config_test;
2509 foreach my $config (keys %config_list) {
2510 if (!defined($config_test{$config})) {
2511 if (!$once) {
2512 $once = 1;
2513 doprint "Configs not produced by kconfig (will not be checked):\n";
2514 }
2515 doprint " $config\n";
2516 delete $config_list{$config};
2517 }
2518 }
2519 my $ret;
2520 do {
2521 $ret = run_config_bisect;
2522 } while (!$ret);
2523
2524 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002525
2526 success $i;
2527}
2528
Steven Rostedt27d934b2011-05-20 09:18:18 -04002529sub patchcheck_reboot {
2530 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Andrew Jones2728be42011-08-12 15:32:05 +02002531 reboot $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04002532}
2533
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002534sub patchcheck {
2535 my ($i) = @_;
2536
2537 die "PATCHCHECK_START[$i] not defined\n"
2538 if (!defined($opt{"PATCHCHECK_START[$i]"}));
2539 die "PATCHCHECK_TYPE[$i] not defined\n"
2540 if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
2541
2542 my $start = $opt{"PATCHCHECK_START[$i]"};
2543
2544 my $end = "HEAD";
2545 if (defined($opt{"PATCHCHECK_END[$i]"})) {
2546 $end = $opt{"PATCHCHECK_END[$i]"};
2547 }
2548
Steven Rostedta57419b2010-11-02 15:13:54 -04002549 # Get the true sha1's since we can use things like HEAD~3
2550 $start = get_sha1($start);
2551 $end = get_sha1($end);
2552
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002553 my $type = $opt{"PATCHCHECK_TYPE[$i]"};
2554
2555 # Can't have a test without having a test to run
2556 if ($type eq "test" && !defined($run_test)) {
2557 $type = "boot";
2558 }
2559
2560 open (IN, "git log --pretty=oneline $end|") or
2561 dodie "could not get git list";
2562
2563 my @list;
2564
2565 while (<IN>) {
2566 chomp;
2567 $list[$#list+1] = $_;
2568 last if (/^$start/);
2569 }
2570 close(IN);
2571
2572 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002573 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002574 }
2575
2576 # go backwards in the list
2577 @list = reverse @list;
2578
2579 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04002580 my %ignored_warnings;
2581
2582 if (defined($ignore_warnings)) {
2583 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2584 $ignored_warnings{$sha1} = 1;
2585 }
2586 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002587
2588 $in_patchcheck = 1;
2589 foreach my $item (@list) {
2590 my $sha1 = $item;
2591 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2592
2593 doprint "\nProcessing commit $item\n\n";
2594
2595 run_command "git checkout $sha1" or
2596 die "Failed to checkout $sha1";
2597
2598 # only clean on the first and last patch
2599 if ($item eq $list[0] ||
2600 $item eq $list[$#list]) {
2601 $noclean = $save_clean;
2602 } else {
2603 $noclean = 1;
2604 }
2605
2606 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002607 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002608 } else {
2609 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002610 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002611 }
2612
Steven Rostedt19902072011-06-14 20:46:25 -04002613
2614 if (!defined($ignored_warnings{$sha1})) {
2615 check_buildlog $sha1 or return 0;
2616 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002617
2618 next if ($type eq "build");
2619
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002620 my $failed = 0;
2621
Steven Rostedtddf607e2011-06-14 20:49:13 -04002622 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002623
2624 if (!$failed && $type ne "boot"){
2625 do_run_test or $failed = 1;
2626 }
2627 end_monitor;
2628 return 0 if ($failed);
2629
Steven Rostedt27d934b2011-05-20 09:18:18 -04002630 patchcheck_reboot;
2631
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002632 }
2633 $in_patchcheck = 0;
2634 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002635
2636 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002637}
2638
Steven Rostedtb9066f62011-07-15 21:25:24 -04002639my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04002640my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002641my $iflevel = 0;
2642my @ifdeps;
2643
2644# prevent recursion
2645my %read_kconfigs;
2646
Steven Rostedtac6974c2011-10-04 09:40:17 -04002647sub add_dep {
2648 # $config depends on $dep
2649 my ($config, $dep) = @_;
2650
2651 if (defined($depends{$config})) {
2652 $depends{$config} .= " " . $dep;
2653 } else {
2654 $depends{$config} = $dep;
2655 }
2656
2657 # record the number of configs depending on $dep
2658 if (defined $depcount{$dep}) {
2659 $depcount{$dep}++;
2660 } else {
2661 $depcount{$dep} = 1;
2662 }
2663}
2664
Steven Rostedtb9066f62011-07-15 21:25:24 -04002665# taken from streamline_config.pl
2666sub read_kconfig {
2667 my ($kconfig) = @_;
2668
2669 my $state = "NONE";
2670 my $config;
2671 my @kconfigs;
2672
2673 my $cont = 0;
2674 my $line;
2675
2676
2677 if (! -f $kconfig) {
2678 doprint "file $kconfig does not exist, skipping\n";
2679 return;
2680 }
2681
2682 open(KIN, "$kconfig")
2683 or die "Can't open $kconfig";
2684 while (<KIN>) {
2685 chomp;
2686
2687 # Make sure that lines ending with \ continue
2688 if ($cont) {
2689 $_ = $line . " " . $_;
2690 }
2691
2692 if (s/\\$//) {
2693 $cont = 1;
2694 $line = $_;
2695 next;
2696 }
2697
2698 $cont = 0;
2699
2700 # collect any Kconfig sources
2701 if (/^source\s*"(.*)"/) {
2702 $kconfigs[$#kconfigs+1] = $1;
2703 }
2704
2705 # configs found
2706 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2707 $state = "NEW";
2708 $config = $2;
2709
2710 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04002711 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04002712 }
2713
2714 # collect the depends for the config
2715 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2716
Steven Rostedtac6974c2011-10-04 09:40:17 -04002717 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002718
2719 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04002720 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2721
2722 # selected by depends on config
2723 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002724
2725 # Check for if statements
2726 } elsif (/^if\s+(.*\S)\s*$/) {
2727 my $deps = $1;
2728 # remove beginning and ending non text
2729 $deps =~ s/^[^a-zA-Z0-9_]*//;
2730 $deps =~ s/[^a-zA-Z0-9_]*$//;
2731
2732 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2733
2734 $ifdeps[$iflevel++] = join ':', @deps;
2735
2736 } elsif (/^endif/) {
2737
2738 $iflevel-- if ($iflevel);
2739
2740 # stop on "help"
2741 } elsif (/^\s*help\s*$/) {
2742 $state = "NONE";
2743 }
2744 }
2745 close(KIN);
2746
2747 # read in any configs that were found.
2748 foreach $kconfig (@kconfigs) {
2749 if (!defined($read_kconfigs{$kconfig})) {
2750 $read_kconfigs{$kconfig} = 1;
2751 read_kconfig("$builddir/$kconfig");
2752 }
2753 }
2754}
2755
2756sub read_depends {
2757 # find out which arch this is by the kconfig file
2758 open (IN, $output_config)
2759 or dodie "Failed to read $output_config";
2760 my $arch;
2761 while (<IN>) {
2762 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2763 $arch = $1;
2764 last;
2765 }
2766 }
2767 close IN;
2768
2769 if (!defined($arch)) {
2770 doprint "Could not find arch from config file\n";
2771 doprint "no dependencies used\n";
2772 return;
2773 }
2774
2775 # arch is really the subarch, we need to know
2776 # what directory to look at.
2777 if ($arch eq "i386" || $arch eq "x86_64") {
2778 $arch = "x86";
2779 } elsif ($arch =~ /^tile/) {
2780 $arch = "tile";
2781 }
2782
2783 my $kconfig = "$builddir/arch/$arch/Kconfig";
2784
2785 if (! -f $kconfig && $arch =~ /\d$/) {
2786 my $orig = $arch;
2787 # some subarchs have numbers, truncate them
2788 $arch =~ s/\d*$//;
2789 $kconfig = "$builddir/arch/$arch/Kconfig";
2790 if (! -f $kconfig) {
2791 doprint "No idea what arch dir $orig is for\n";
2792 doprint "no dependencies used\n";
2793 return;
2794 }
2795 }
2796
2797 read_kconfig($kconfig);
2798}
2799
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002800sub read_config_list {
2801 my ($config) = @_;
2802
2803 open (IN, $config)
2804 or dodie "Failed to read $config";
2805
2806 while (<IN>) {
2807 if (/^((CONFIG\S*)=.*)/) {
2808 if (!defined($config_ignore{$2})) {
2809 $config_list{$2} = $1;
2810 }
2811 }
2812 }
2813
2814 close(IN);
2815}
2816
2817sub read_output_config {
2818 my ($config) = @_;
2819
2820 assign_configs \%config_ignore, $config;
2821}
2822
2823sub make_new_config {
2824 my @configs = @_;
2825
2826 open (OUT, ">$output_config")
2827 or dodie "Failed to write $output_config";
2828
2829 foreach my $config (@configs) {
2830 print OUT "$config\n";
2831 }
2832 close OUT;
2833}
2834
Steven Rostedtac6974c2011-10-04 09:40:17 -04002835sub chomp_config {
2836 my ($config) = @_;
2837
2838 $config =~ s/CONFIG_//;
2839
2840 return $config;
2841}
2842
Steven Rostedtb9066f62011-07-15 21:25:24 -04002843sub get_depends {
2844 my ($dep) = @_;
2845
Steven Rostedtac6974c2011-10-04 09:40:17 -04002846 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002847
2848 $dep = $depends{"$kconfig"};
2849
2850 # the dep string we have saves the dependencies as they
2851 # were found, including expressions like ! && ||. We
2852 # want to split this out into just an array of configs.
2853
2854 my $valid = "A-Za-z_0-9";
2855
2856 my @configs;
2857
2858 while ($dep =~ /[$valid]/) {
2859
2860 if ($dep =~ /^[^$valid]*([$valid]+)/) {
2861 my $conf = "CONFIG_" . $1;
2862
2863 $configs[$#configs + 1] = $conf;
2864
2865 $dep =~ s/^[^$valid]*[$valid]+//;
2866 } else {
2867 die "this should never happen";
2868 }
2869 }
2870
2871 return @configs;
2872}
2873
2874my %min_configs;
2875my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04002876my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002877my %processed_configs;
2878my %nochange_config;
2879
2880sub test_this_config {
2881 my ($config) = @_;
2882
2883 my $found;
2884
2885 # if we already processed this config, skip it
2886 if (defined($processed_configs{$config})) {
2887 return undef;
2888 }
2889 $processed_configs{$config} = 1;
2890
2891 # if this config failed during this round, skip it
2892 if (defined($nochange_config{$config})) {
2893 return undef;
2894 }
2895
Steven Rostedtac6974c2011-10-04 09:40:17 -04002896 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002897
2898 # Test dependencies first
2899 if (defined($depends{"$kconfig"})) {
2900 my @parents = get_depends $config;
2901 foreach my $parent (@parents) {
2902 # if the parent is in the min config, check it first
2903 next if (!defined($min_configs{$parent}));
2904 $found = test_this_config($parent);
2905 if (defined($found)) {
2906 return $found;
2907 }
2908 }
2909 }
2910
2911 # Remove this config from the list of configs
2912 # do a make oldnoconfig and then read the resulting
2913 # .config to make sure it is missing the config that
2914 # we had before
2915 my %configs = %min_configs;
2916 delete $configs{$config};
2917 make_new_config ((values %configs), (values %keep_configs));
2918 make_oldconfig;
2919 undef %configs;
2920 assign_configs \%configs, $output_config;
2921
2922 return $config if (!defined($configs{$config}));
2923
2924 doprint "disabling config $config did not change .config\n";
2925
2926 $nochange_config{$config} = 1;
2927
2928 return undef;
2929}
2930
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002931sub make_min_config {
2932 my ($i) = @_;
2933
2934 if (!defined($output_minconfig)) {
2935 fail "OUTPUT_MIN_CONFIG not defined" and return;
2936 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04002937
2938 # If output_minconfig exists, and the start_minconfig
2939 # came from min_config, than ask if we should use
2940 # that instead.
2941 if (-f $output_minconfig && !$start_minconfig_defined) {
2942 print "$output_minconfig exists\n";
2943 if (read_yn " Use it as minconfig?") {
2944 $start_minconfig = $output_minconfig;
2945 }
2946 }
2947
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002948 if (!defined($start_minconfig)) {
2949 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
2950 }
2951
Steven Rostedt35ce5952011-07-15 21:57:25 -04002952 my $temp_config = "$tmpdir/temp_config";
2953
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002954 # First things first. We build an allnoconfig to find
2955 # out what the defaults are that we can't touch.
2956 # Some are selections, but we really can't handle selections.
2957
2958 my $save_minconfig = $minconfig;
2959 undef $minconfig;
2960
2961 run_command "$make allnoconfig" or return 0;
2962
Steven Rostedtb9066f62011-07-15 21:25:24 -04002963 read_depends;
2964
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002965 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002966
Steven Rostedt43d1b652011-07-15 22:01:56 -04002967 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002968 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002969
2970 if (defined($ignore_config)) {
2971 # make sure the file exists
2972 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04002973 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002974 }
2975
Steven Rostedt43d1b652011-07-15 22:01:56 -04002976 %keep_configs = %save_configs;
2977
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002978 doprint "Load initial configs from $start_minconfig\n";
2979
2980 # Look at the current min configs, and save off all the
2981 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002982 assign_configs \%min_configs, $start_minconfig;
2983
2984 my @config_keys = keys %min_configs;
2985
Steven Rostedtac6974c2011-10-04 09:40:17 -04002986 # All configs need a depcount
2987 foreach my $config (@config_keys) {
2988 my $kconfig = chomp_config $config;
2989 if (!defined $depcount{$kconfig}) {
2990 $depcount{$kconfig} = 0;
2991 }
2992 }
2993
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002994 # Remove anything that was set by the make allnoconfig
2995 # we shouldn't need them as they get set for us anyway.
2996 foreach my $config (@config_keys) {
2997 # Remove anything in the ignore_config
2998 if (defined($keep_configs{$config})) {
2999 my $file = $ignore_config;
3000 $file =~ s,.*/(.*?)$,$1,;
3001 doprint "$config set by $file ... ignored\n";
3002 delete $min_configs{$config};
3003 next;
3004 }
3005 # But make sure the settings are the same. If a min config
3006 # sets a selection, we do not want to get rid of it if
3007 # it is not the same as what we have. Just move it into
3008 # the keep configs.
3009 if (defined($config_ignore{$config})) {
3010 if ($config_ignore{$config} ne $min_configs{$config}) {
3011 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3012 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3013 $keep_configs{$config} = $min_configs{$config};
3014 } else {
3015 doprint "$config set by allnoconfig ... ignored\n";
3016 }
3017 delete $min_configs{$config};
3018 }
3019 }
3020
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003021 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003022 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003023
3024 while (!$done) {
3025
3026 my $config;
3027 my $found;
3028
3029 # Now disable each config one by one and do a make oldconfig
3030 # till we find a config that changes our list.
3031
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003032 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003033
3034 # Sort keys by who is most dependent on
3035 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3036 @test_configs ;
3037
3038 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003039 my $reset = 1;
3040 for (my $i = 0; $i < $#test_configs; $i++) {
3041 if (!defined($nochange_config{$test_configs[0]})) {
3042 $reset = 0;
3043 last;
3044 }
3045 # This config didn't change the .config last time.
3046 # Place it at the end
3047 my $config = shift @test_configs;
3048 push @test_configs, $config;
3049 }
3050
3051 # if every test config has failed to modify the .config file
3052 # in the past, then reset and start over.
3053 if ($reset) {
3054 undef %nochange_config;
3055 }
3056
Steven Rostedtb9066f62011-07-15 21:25:24 -04003057 undef %processed_configs;
3058
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003059 foreach my $config (@test_configs) {
3060
Steven Rostedtb9066f62011-07-15 21:25:24 -04003061 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003062
Steven Rostedtb9066f62011-07-15 21:25:24 -04003063 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003064
3065 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003066 }
3067
3068 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003069 # we could have failed due to the nochange_config hash
3070 # reset and try again
3071 if (!$take_two) {
3072 undef %nochange_config;
3073 $take_two = 1;
3074 next;
3075 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003076 doprint "No more configs found that we can disable\n";
3077 $done = 1;
3078 last;
3079 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003080 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003081
3082 $config = $found;
3083
3084 doprint "Test with $config disabled\n";
3085
3086 # set in_bisect to keep build and monitor from dieing
3087 $in_bisect = 1;
3088
3089 my $failed = 0;
3090 build "oldconfig";
3091 start_monitor_and_boot or $failed = 1;
3092 end_monitor;
3093
3094 $in_bisect = 0;
3095
3096 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003097 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003098 # this config is needed, add it to the ignore list.
3099 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003100 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003101 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003102
3103 # update new ignore configs
3104 if (defined($ignore_config)) {
3105 open (OUT, ">$temp_config")
3106 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003107 foreach my $config (keys %save_configs) {
3108 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003109 }
3110 close OUT;
3111 run_command "mv $temp_config $ignore_config" or
3112 dodie "failed to copy update to $ignore_config";
3113 }
3114
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003115 } else {
3116 # We booted without this config, remove it from the minconfigs.
3117 doprint "$config is not needed, disabling\n";
3118
3119 delete $min_configs{$config};
3120
3121 # Also disable anything that is not enabled in this config
3122 my %configs;
3123 assign_configs \%configs, $output_config;
3124 my @config_keys = keys %min_configs;
3125 foreach my $config (@config_keys) {
3126 if (!defined($configs{$config})) {
3127 doprint "$config is not set, disabling\n";
3128 delete $min_configs{$config};
3129 }
3130 }
3131
3132 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003133 open (OUT, ">$temp_config")
3134 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003135 foreach my $config (keys %keep_configs) {
3136 print OUT "$keep_configs{$config}\n";
3137 }
3138 foreach my $config (keys %min_configs) {
3139 print OUT "$min_configs{$config}\n";
3140 }
3141 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003142
3143 run_command "mv $temp_config $output_minconfig" or
3144 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003145 }
3146
3147 doprint "Reboot and wait $sleep_time seconds\n";
Andrew Jones2728be42011-08-12 15:32:05 +02003148 reboot $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003149 }
3150
3151 success $i;
3152 return 1;
3153}
3154
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003155$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003156
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003157if ($#ARGV == 0) {
3158 $ktest_config = $ARGV[0];
3159 if (! -f $ktest_config) {
3160 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003161 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003162 exit 0;
3163 }
3164 }
3165} else {
3166 $ktest_config = "ktest.conf";
3167}
3168
3169if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003170 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003171 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003172 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3173 print OUT << "EOF"
3174# Generated by ktest.pl
3175#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003176
3177# PWD is a ktest.pl variable that will result in the process working
3178# directory that ktest.pl is executed in.
3179
3180# THIS_DIR is automatically assigned the PWD of the path that generated
3181# the config file. It is best to use this variable when assigning other
3182# directory paths within this directory. This allows you to easily
3183# move the test cases to other locations or to other machines.
3184#
3185THIS_DIR := $variable{"PWD"}
3186
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003187# Define each test with TEST_START
3188# The config options below it will override the defaults
3189TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003190TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003191
3192DEFAULTS
3193EOF
3194;
3195 close(OUT);
3196}
3197read_config $ktest_config;
3198
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003199if (defined($opt{"LOG_FILE"})) {
3200 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3201}
3202
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003203# Append any configs entered in manually to the config file.
3204my @new_configs = keys %entered_configs;
3205if ($#new_configs >= 0) {
3206 print "\nAppending entered in configs to $ktest_config\n";
3207 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3208 foreach my $config (@new_configs) {
3209 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003210 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003211 }
3212}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003213
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003214if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3215 unlink $opt{"LOG_FILE"};
3216}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003217
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003218doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3219
Steven Rostedta57419b2010-11-02 15:13:54 -04003220for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3221
3222 if (!$i) {
3223 doprint "DEFAULT OPTIONS:\n";
3224 } else {
3225 doprint "\nTEST $i OPTIONS";
3226 if (defined($repeat_tests{$i})) {
3227 $repeat = $repeat_tests{$i};
3228 doprint " ITERATE $repeat";
3229 }
3230 doprint "\n";
3231 }
3232
3233 foreach my $option (sort keys %opt) {
3234
3235 if ($option =~ /\[(\d+)\]$/) {
3236 next if ($i != $1);
3237 } else {
3238 next if ($i);
3239 }
3240
3241 doprint "$option = $opt{$option}\n";
3242 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003243}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003244
Steven Rostedt2a625122011-05-20 15:48:59 -04003245sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003246 my ($name, $i) = @_;
3247
3248 my $option = "$name\[$i\]";
3249
3250 if (defined($opt{$option})) {
3251 return $opt{$option};
3252 }
3253
Steven Rostedta57419b2010-11-02 15:13:54 -04003254 foreach my $test (keys %repeat_tests) {
3255 if ($i >= $test &&
3256 $i < $test + $repeat_tests{$test}) {
3257 $option = "$name\[$test\]";
3258 if (defined($opt{$option})) {
3259 return $opt{$option};
3260 }
3261 }
3262 }
3263
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003264 if (defined($opt{$name})) {
3265 return $opt{$name};
3266 }
3267
3268 return undef;
3269}
3270
Steven Rostedt2a625122011-05-20 15:48:59 -04003271sub set_test_option {
3272 my ($name, $i) = @_;
3273
3274 my $option = __set_test_option($name, $i);
3275 return $option if (!defined($option));
3276
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003277 return eval_option($option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003278}
3279
Steven Rostedt2545eb62010-11-02 15:01:32 -04003280# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003281for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003282
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003283 # Do not reboot on failing test options
3284 $no_reboot = 1;
3285
Steven Rostedt576f6272010-11-02 14:58:38 -04003286 $iteration = $i;
3287
Steven Rostedta75fece2010-11-02 14:58:27 -04003288 my $makecmd = set_test_option("MAKE_CMD", $i);
3289
3290 $machine = set_test_option("MACHINE", $i);
Steven Rostedte48c5292010-11-02 14:35:37 -04003291 $ssh_user = set_test_option("SSH_USER", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003292 $tmpdir = set_test_option("TMP_DIR", $i);
3293 $outputdir = set_test_option("OUTPUT_DIR", $i);
3294 $builddir = set_test_option("BUILD_DIR", $i);
3295 $test_type = set_test_option("TEST_TYPE", $i);
3296 $build_type = set_test_option("BUILD_TYPE", $i);
3297 $build_options = set_test_option("BUILD_OPTIONS", $i);
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04003298 $pre_build = set_test_option("PRE_BUILD", $i);
3299 $post_build = set_test_option("POST_BUILD", $i);
3300 $pre_build_die = set_test_option("PRE_BUILD_DIE", $i);
3301 $post_build_die = set_test_option("POST_BUILD_DIE", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003302 $power_cycle = set_test_option("POWER_CYCLE", $i);
Steven Rostedte48c5292010-11-02 14:35:37 -04003303 $reboot = set_test_option("REBOOT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003304 $noclean = set_test_option("BUILD_NOCLEAN", $i);
3305 $minconfig = set_test_option("MIN_CONFIG", $i);
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003306 $output_minconfig = set_test_option("OUTPUT_MIN_CONFIG", $i);
3307 $start_minconfig = set_test_option("START_MIN_CONFIG", $i);
3308 $ignore_config = set_test_option("IGNORE_CONFIG", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003309 $run_test = set_test_option("TEST", $i);
3310 $addconfig = set_test_option("ADD_CONFIG", $i);
3311 $reboot_type = set_test_option("REBOOT_TYPE", $i);
3312 $grub_menu = set_test_option("GRUB_MENU", $i);
Steven Rostedt8b37ca82010-11-02 14:58:33 -04003313 $post_install = set_test_option("POST_INSTALL", $i);
Steven Rostedte0a87422011-09-30 17:50:48 -04003314 $no_install = set_test_option("NO_INSTALL", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003315 $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
3316 $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
3317 $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
3318 $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
3319 $power_off = set_test_option("POWER_OFF", $i);
Steven Rostedt576f6272010-11-02 14:58:38 -04003320 $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
3321 $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003322 $sleep_time = set_test_option("SLEEP_TIME", $i);
3323 $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
Steven Rostedt27d934b2011-05-20 09:18:18 -04003324 $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
Steven Rostedt19902072011-06-14 20:46:25 -04003325 $ignore_warnings = set_test_option("IGNORE_WARNINGS", $i);
Steven Rostedtc960bb92011-03-08 09:22:39 -05003326 $bisect_manual = set_test_option("BISECT_MANUAL", $i);
Steven Rostedtc23dca72011-03-08 09:26:31 -05003327 $bisect_skip = set_test_option("BISECT_SKIP", $i);
Steven Rostedt30f75da2011-06-13 10:35:35 -04003328 $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
Steven Rostedtc5dacb82011-12-22 12:43:57 -05003329 $bisect_ret_good = set_test_option("BISECT_RET_GOOD", $i);
3330 $bisect_ret_bad = set_test_option("BISECT_RET_BAD", $i);
3331 $bisect_ret_skip = set_test_option("BISECT_RET_SKIP", $i);
3332 $bisect_ret_abort = set_test_option("BISECT_RET_ABORT", $i);
3333 $bisect_ret_default = set_test_option("BISECT_RET_DEFAULT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003334 $store_failures = set_test_option("STORE_FAILURES", $i);
Rabin Vincentde5b6e32011-11-18 17:05:31 +05303335 $store_successes = set_test_option("STORE_SUCCESSES", $i);
Steven Rostedt9064af52011-06-13 10:38:48 -04003336 $test_name = set_test_option("TEST_NAME", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003337 $timeout = set_test_option("TIMEOUT", $i);
3338 $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
3339 $console = set_test_option("CONSOLE", $i);
Steven Rostedtf1a5b962011-06-13 10:30:00 -04003340 $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003341 $success_line = set_test_option("SUCCESS_LINE", $i);
Steven Rostedt2b803362011-09-30 18:00:23 -04003342 $reboot_success_line = set_test_option("REBOOT_SUCCESS_LINE", $i);
Steven Rostedt1c8a6172010-11-09 12:55:40 -05003343 $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
3344 $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
Steven Rostedt2d01b262011-03-08 09:47:54 -05003345 $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003346 $build_target = set_test_option("BUILD_TARGET", $i);
Steven Rostedte48c5292010-11-02 14:35:37 -04003347 $ssh_exec = set_test_option("SSH_EXEC", $i);
3348 $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003349 $target_image = set_test_option("TARGET_IMAGE", $i);
3350 $localversion = set_test_option("LOCALVERSION", $i);
3351
Steven Rostedt35ce5952011-07-15 21:57:25 -04003352 $start_minconfig_defined = 1;
3353
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003354 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003355 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003356 $start_minconfig = $minconfig;
3357 }
3358
Steven Rostedta75fece2010-11-02 14:58:27 -04003359 chdir $builddir || die "can't change directory to $builddir";
3360
Andrew Jonesa908a662011-08-12 15:32:03 +02003361 foreach my $dir ($tmpdir, $outputdir) {
3362 if (!-d $dir) {
3363 mkpath($dir) or
3364 die "can't create $dir";
3365 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003366 }
3367
Steven Rostedte48c5292010-11-02 14:35:37 -04003368 $ENV{"SSH_USER"} = $ssh_user;
3369 $ENV{"MACHINE"} = $machine;
3370
Steven Rostedta75fece2010-11-02 14:58:27 -04003371 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303372 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003373 $dmesg = "$tmpdir/dmesg-$machine";
3374 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003375 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003376
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003377 if (!$buildonly) {
3378 $target = "$ssh_user\@$machine";
3379 if ($reboot_type eq "grub") {
3380 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3381 } elsif (!defined($reboot_script)) {
3382 dodie "REBOOT_SCRIPT not defined"
3383 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003384 }
3385
3386 my $run_type = $build_type;
3387 if ($test_type eq "patchcheck") {
3388 $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
3389 } elsif ($test_type eq "bisect") {
3390 $run_type = $opt{"BISECT_TYPE[$i]"};
Steven Rostedt0a05c762010-11-08 11:14:10 -05003391 } elsif ($test_type eq "config_bisect") {
3392 $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
Steven Rostedta75fece2010-11-02 14:58:27 -04003393 }
3394
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003395 if ($test_type eq "make_min_config") {
3396 $run_type = "";
3397 }
3398
Steven Rostedta75fece2010-11-02 14:58:27 -04003399 # mistake in config file?
3400 if (!defined($run_type)) {
3401 $run_type = "ERROR";
3402 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003403
Steven Rostedte0a87422011-09-30 17:50:48 -04003404 my $installme = "";
3405 $installme = " no_install" if ($no_install);
3406
Steven Rostedt2545eb62010-11-02 15:01:32 -04003407 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003408 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003409
3410 unlink $dmesg;
3411 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303412 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003413
Steven Rostedt250bae82011-07-15 22:05:59 -04003414 if (defined($addconfig)) {
3415 my $min = $minconfig;
3416 if (!defined($minconfig)) {
3417 $min = "";
3418 }
3419 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003420 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003421 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003422 }
3423
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003424 my $checkout = $opt{"CHECKOUT[$i]"};
3425 if (defined($checkout)) {
3426 run_command "git checkout $checkout" or
3427 die "failed to checkout $checkout";
3428 }
3429
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003430 $no_reboot = 0;
3431
3432
Steven Rostedta75fece2010-11-02 14:58:27 -04003433 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003434 bisect $i;
3435 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003436 } elsif ($test_type eq "config_bisect") {
3437 config_bisect $i;
3438 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003439 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003440 patchcheck $i;
3441 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003442 } elsif ($test_type eq "make_min_config") {
3443 make_min_config $i;
3444 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003445 }
3446
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003447 if ($build_type ne "nobuild") {
3448 build $build_type or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003449 }
3450
Steven Rostedtcd8e3682011-08-18 16:35:44 -04003451 if ($test_type eq "install") {
3452 get_version;
3453 install;
3454 success $i;
3455 next;
3456 }
3457
Steven Rostedta75fece2010-11-02 14:58:27 -04003458 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04003459 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04003460 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04003461
3462 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3463 do_run_test or $failed = 1;
3464 }
3465 end_monitor;
3466 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003467 }
3468
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003469 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003470}
3471
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003472if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003473 halt;
Steven Rostedt576f6272010-11-02 14:58:38 -04003474} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003475 reboot;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003476}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003477
Steven Rostedte48c5292010-11-02 14:35:37 -04003478doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3479
Steven Rostedt2545eb62010-11-02 15:01:32 -04003480exit 0;