blob: ff21e921be25a183dbf21ffe8cf38f3c1992cdda [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;
Steven Rostedtbc7c5802011-12-22 16:29:10 -050082my $switch_to_good;
83my $switch_to_test;
Steven Rostedta75fece2010-11-02 14:58:27 -040084my $poweroff_on_error;
85my $die_on_failure;
Steven Rostedt576f6272010-11-02 14:58:38 -040086my $powercycle_after_reboot;
87my $poweroff_after_halt;
Steven Rostedte48c5292010-11-02 14:35:37 -040088my $ssh_exec;
89my $scp_to_target;
Steven Rostedta75fece2010-11-02 14:58:27 -040090my $power_off;
91my $grub_menu;
Steven Rostedt2545eb62010-11-02 15:01:32 -040092my $grub_number;
93my $target;
94my $make;
Steven Rostedt8b37ca82010-11-02 14:58:33 -040095my $post_install;
Steven Rostedte0a87422011-09-30 17:50:48 -040096my $no_install;
Steven Rostedt5c42fc52010-11-02 14:57:01 -040097my $noclean;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -040098my $minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -040099my $start_minconfig;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400100my $start_minconfig_defined;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400101my $output_minconfig;
102my $ignore_config;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -0400103my $addconfig;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400104my $in_bisect = 0;
105my $bisect_bad = "";
Steven Rostedtd6ce2a02010-11-02 14:58:05 -0400106my $reverse_bisect;
Steven Rostedtc960bb92011-03-08 09:22:39 -0500107my $bisect_manual;
Steven Rostedtc23dca72011-03-08 09:26:31 -0500108my $bisect_skip;
Steven Rostedt30f75da2011-06-13 10:35:35 -0400109my $config_bisect_good;
Steven Rostedtc5dacb82011-12-22 12:43:57 -0500110my $bisect_ret_good;
111my $bisect_ret_bad;
112my $bisect_ret_skip;
113my $bisect_ret_abort;
114my $bisect_ret_default;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400115my $in_patchcheck = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400116my $run_test;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400117my $redirect;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400118my $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +0530119my $testlog;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400120my $dmesg;
121my $monitor_fp;
122my $monitor_pid;
123my $monitor_cnt = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -0400124my $sleep_time;
125my $bisect_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -0400126my $patchcheck_sleep_time;
Steven Rostedt19902072011-06-14 20:46:25 -0400127my $ignore_warnings;
Steven Rostedta75fece2010-11-02 14:58:27 -0400128my $store_failures;
Rabin Vincentde5b6e32011-11-18 17:05:31 +0530129my $store_successes;
Steven Rostedt9064af52011-06-13 10:38:48 -0400130my $test_name;
Steven Rostedta75fece2010-11-02 14:58:27 -0400131my $timeout;
132my $booted_timeout;
Steven Rostedtf1a5b962011-06-13 10:30:00 -0400133my $detect_triplefault;
Steven Rostedta75fece2010-11-02 14:58:27 -0400134my $console;
Steven Rostedt2b803362011-09-30 18:00:23 -0400135my $reboot_success_line;
Steven Rostedta75fece2010-11-02 14:58:27 -0400136my $success_line;
Steven Rostedt1c8a6172010-11-09 12:55:40 -0500137my $stop_after_success;
138my $stop_after_failure;
Steven Rostedt2d01b262011-03-08 09:47:54 -0500139my $stop_test_after;
Steven Rostedta75fece2010-11-02 14:58:27 -0400140my $build_target;
141my $target_image;
142my $localversion;
Steven Rostedt576f6272010-11-02 14:58:38 -0400143my $iteration = 0;
Steven Rostedte48c5292010-11-02 14:35:37 -0400144my $successes = 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400145
Steven Rostedt165708b2011-11-26 20:56:52 -0500146# set when a test is something other that just building or install
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500147# which would require more options.
148my $buildonly = 1;
149
Steven Rostedtdbd37832011-11-23 16:00:48 -0500150# set when creating a new config
151my $newconfig = 0;
152
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500153my %entered_configs;
154my %config_help;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400155my %variable;
Steven Rostedtfcb3f162011-06-13 10:40:58 -0400156my %force_config;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500157
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400158# do not force reboots on config problems
159my $no_reboot = 1;
160
Steven Rostedt7bf51072011-10-22 09:07:03 -0400161# default variables that can be used
162chomp ($variable{"PWD"} = `pwd`);
163
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500164$config_help{"MACHINE"} = << "EOF"
165 The machine hostname that you will test.
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500166 For build only tests, it is still needed to differentiate log files.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500167EOF
168 ;
169$config_help{"SSH_USER"} = << "EOF"
170 The box is expected to have ssh on normal bootup, provide the user
171 (most likely root, since you need privileged operations)
172EOF
173 ;
174$config_help{"BUILD_DIR"} = << "EOF"
175 The directory that contains the Linux source code (full path).
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500176 You can use \${PWD} that will be the path where ktest.pl is run, or use
177 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500178EOF
179 ;
180$config_help{"OUTPUT_DIR"} = << "EOF"
181 The directory that the objects will be built (full path).
182 (can not be same as BUILD_DIR)
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500183 You can use \${PWD} that will be the path where ktest.pl is run, or use
184 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500185EOF
186 ;
187$config_help{"BUILD_TARGET"} = << "EOF"
188 The location of the compiled file to copy to the target.
189 (relative to OUTPUT_DIR)
190EOF
191 ;
Steven Rostedtdbd37832011-11-23 16:00:48 -0500192$config_help{"BUILD_OPTIONS"} = << "EOF"
193 Options to add to \"make\" when building.
194 i.e. -j20
195EOF
196 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500197$config_help{"TARGET_IMAGE"} = << "EOF"
198 The place to put your image on the test machine.
199EOF
200 ;
201$config_help{"POWER_CYCLE"} = << "EOF"
202 A script or command to reboot the box.
203
204 Here is a digital loggers power switch example
205 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
206
207 Here is an example to reboot a virtual box on the current host
208 with the name "Guest".
209 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
210EOF
211 ;
212$config_help{"CONSOLE"} = << "EOF"
213 The script or command that reads the console
214
215 If you use ttywatch server, something like the following would work.
216CONSOLE = nc -d localhost 3001
217
218 For a virtual machine with guest name "Guest".
219CONSOLE = virsh console Guest
220EOF
221 ;
222$config_help{"LOCALVERSION"} = << "EOF"
223 Required version ending to differentiate the test
224 from other linux builds on the system.
225EOF
226 ;
227$config_help{"REBOOT_TYPE"} = << "EOF"
228 Way to reboot the box to the test kernel.
229 Only valid options so far are "grub" and "script".
230
231 If you specify grub, it will assume grub version 1
232 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
233 and select that target to reboot to the kernel. If this is not
234 your setup, then specify "script" and have a command or script
235 specified in REBOOT_SCRIPT to boot to the target.
236
237 The entry in /boot/grub/menu.lst must be entered in manually.
238 The test will not modify that file.
239EOF
240 ;
241$config_help{"GRUB_MENU"} = << "EOF"
242 The grub title name for the test kernel to boot
243 (Only mandatory if REBOOT_TYPE = grub)
244
245 Note, ktest.pl will not update the grub menu.lst, you need to
246 manually add an option for the test. ktest.pl will search
247 the grub menu.lst for this option to find what kernel to
248 reboot into.
249
250 For example, if in the /boot/grub/menu.lst the test kernel title has:
251 title Test Kernel
252 kernel vmlinuz-test
253 GRUB_MENU = Test Kernel
254EOF
255 ;
256$config_help{"REBOOT_SCRIPT"} = << "EOF"
257 A script to reboot the target into the test kernel
258 (Only mandatory if REBOOT_TYPE = script)
259EOF
260 ;
261
Steven Rostedtdad98752011-11-22 20:48:57 -0500262sub read_prompt {
263 my ($cancel, $prompt) = @_;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400264
265 my $ans;
266
267 for (;;) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500268 if ($cancel) {
269 print "$prompt [y/n/C] ";
270 } else {
271 print "$prompt [Y/n] ";
272 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400273 $ans = <STDIN>;
274 chomp $ans;
275 if ($ans =~ /^\s*$/) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500276 if ($cancel) {
277 $ans = "c";
278 } else {
279 $ans = "y";
280 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400281 }
282 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
Steven Rostedtdad98752011-11-22 20:48:57 -0500283 if ($cancel) {
284 last if ($ans =~ /^c$/i);
285 print "Please answer either 'y', 'n' or 'c'.\n";
286 } else {
287 print "Please answer either 'y' or 'n'.\n";
288 }
289 }
290 if ($ans =~ /^c/i) {
291 exit;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400292 }
293 if ($ans !~ /^y$/i) {
294 return 0;
295 }
296 return 1;
297}
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500298
Steven Rostedtdad98752011-11-22 20:48:57 -0500299sub read_yn {
300 my ($prompt) = @_;
301
302 return read_prompt 0, $prompt;
303}
304
305sub read_ync {
306 my ($prompt) = @_;
307
308 return read_prompt 1, $prompt;
309}
310
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500311sub get_ktest_config {
312 my ($config) = @_;
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400313 my $ans;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500314
315 return if (defined($opt{$config}));
316
317 if (defined($config_help{$config})) {
318 print "\n";
319 print $config_help{$config};
320 }
321
322 for (;;) {
323 print "$config = ";
Steven Rostedtdbd37832011-11-23 16:00:48 -0500324 if (defined($default{$config}) && length($default{$config})) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500325 print "\[$default{$config}\] ";
326 }
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400327 $ans = <STDIN>;
328 $ans =~ s/^\s*(.*\S)\s*$/$1/;
329 if ($ans =~ /^\s*$/) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500330 if ($default{$config}) {
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400331 $ans = $default{$config};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500332 } else {
333 print "Your answer can not be blank\n";
334 next;
335 }
336 }
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500337 $entered_configs{$config} = ${ans};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500338 last;
339 }
340}
341
342sub get_ktest_configs {
343 get_ktest_config("MACHINE");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500344 get_ktest_config("BUILD_DIR");
345 get_ktest_config("OUTPUT_DIR");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500346
Steven Rostedtdbd37832011-11-23 16:00:48 -0500347 if ($newconfig) {
348 get_ktest_config("BUILD_OPTIONS");
349 }
350
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500351 # options required for other than just building a kernel
352 if (!$buildonly) {
Steven Rostedt165708b2011-11-26 20:56:52 -0500353 get_ktest_config("POWER_CYCLE");
354 get_ktest_config("CONSOLE");
355 }
356
357 # options required for install and more
358 if ($buildonly != 1) {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500359 get_ktest_config("SSH_USER");
360 get_ktest_config("BUILD_TARGET");
361 get_ktest_config("TARGET_IMAGE");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500362 }
363
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500364 get_ktest_config("LOCALVERSION");
365
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500366 return if ($buildonly);
367
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500368 my $rtype = $opt{"REBOOT_TYPE"};
369
370 if (!defined($rtype)) {
371 if (!defined($opt{"GRUB_MENU"})) {
372 get_ktest_config("REBOOT_TYPE");
373 $rtype = $entered_configs{"REBOOT_TYPE"};
374 } else {
375 $rtype = "grub";
376 }
377 }
378
379 if ($rtype eq "grub") {
380 get_ktest_config("GRUB_MENU");
381 } else {
382 get_ktest_config("REBOOT_SCRIPT");
383 }
384}
385
Steven Rostedt77d942c2011-05-20 13:36:58 -0400386sub process_variables {
Steven Rostedt8d735212011-10-17 11:36:44 -0400387 my ($value, $remove_undef) = @_;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400388 my $retval = "";
389
390 # We want to check for '\', and it is just easier
391 # to check the previous characet of '$' and not need
392 # to worry if '$' is the first character. By adding
393 # a space to $value, we can just check [^\\]\$ and
394 # it will still work.
395 $value = " $value";
396
397 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
398 my $begin = $1;
399 my $var = $2;
400 my $end = $3;
401 # append beginning of value to retval
402 $retval = "$retval$begin";
403 if (defined($variable{$var})) {
404 $retval = "$retval$variable{$var}";
Steven Rostedt8d735212011-10-17 11:36:44 -0400405 } elsif (defined($remove_undef) && $remove_undef) {
406 # for if statements, any variable that is not defined,
407 # we simple convert to 0
408 $retval = "${retval}0";
Steven Rostedt77d942c2011-05-20 13:36:58 -0400409 } else {
410 # put back the origin piece.
411 $retval = "$retval\$\{$var\}";
412 }
413 $value = $end;
414 }
415 $retval = "$retval$value";
416
417 # remove the space added in the beginning
418 $retval =~ s/ //;
419
420 return "$retval"
421}
422
Steven Rostedta57419b2010-11-02 15:13:54 -0400423sub set_value {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400424 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
Steven Rostedta57419b2010-11-02 15:13:54 -0400425
Steven Rostedtcad96662011-12-22 11:32:52 -0500426 my $prvalue = process_variables($rvalue);
427
428 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500429 # Note if a test is something other than build, then we
430 # will need other manditory options.
Steven Rostedtcad96662011-12-22 11:32:52 -0500431 if ($prvalue ne "install") {
Steven Rostedt165708b2011-11-26 20:56:52 -0500432 $buildonly = 0;
433 } else {
434 # install still limits some manditory options.
435 $buildonly = 2;
436 }
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500437 }
438
Steven Rostedta57419b2010-11-02 15:13:54 -0400439 if (defined($opt{$lvalue})) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400440 if (!$override || defined(${$overrides}{$lvalue})) {
441 my $extra = "";
442 if ($override) {
443 $extra = "In the same override section!\n";
444 }
445 die "$name: $.: Option $lvalue defined more than once!\n$extra";
446 }
Steven Rostedtcad96662011-12-22 11:32:52 -0500447 ${$overrides}{$lvalue} = $prvalue;
Steven Rostedta57419b2010-11-02 15:13:54 -0400448 }
Steven Rostedt21a96792010-11-08 16:45:50 -0500449 if ($rvalue =~ /^\s*$/) {
450 delete $opt{$lvalue};
451 } else {
Steven Rostedtcad96662011-12-22 11:32:52 -0500452 $opt{$lvalue} = $prvalue;
Steven Rostedt21a96792010-11-08 16:45:50 -0500453 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400454}
455
Steven Rostedt77d942c2011-05-20 13:36:58 -0400456sub set_variable {
457 my ($lvalue, $rvalue) = @_;
458
459 if ($rvalue =~ /^\s*$/) {
460 delete $variable{$lvalue};
461 } else {
462 $rvalue = process_variables($rvalue);
463 $variable{$lvalue} = $rvalue;
464 }
465}
466
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400467sub process_compare {
468 my ($lval, $cmp, $rval) = @_;
469
470 # remove whitespace
471
472 $lval =~ s/^\s*//;
473 $lval =~ s/\s*$//;
474
475 $rval =~ s/^\s*//;
476 $rval =~ s/\s*$//;
477
478 if ($cmp eq "==") {
479 return $lval eq $rval;
480 } elsif ($cmp eq "!=") {
481 return $lval ne $rval;
482 }
483
484 my $statement = "$lval $cmp $rval";
485 my $ret = eval $statement;
486
487 # $@ stores error of eval
488 if ($@) {
489 return -1;
490 }
491
492 return $ret;
493}
494
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400495sub value_defined {
496 my ($val) = @_;
497
498 return defined($variable{$2}) ||
499 defined($opt{$2});
500}
501
Steven Rostedt8d735212011-10-17 11:36:44 -0400502my $d = 0;
503sub process_expression {
504 my ($name, $val) = @_;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400505
Steven Rostedt8d735212011-10-17 11:36:44 -0400506 my $c = $d++;
507
508 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
509 my $express = $1;
510
511 if (process_expression($name, $express)) {
512 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
513 } else {
514 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
515 }
516 }
517
518 $d--;
519 my $OR = "\\|\\|";
520 my $AND = "\\&\\&";
521
522 while ($val =~ s/^(.*?)($OR|$AND)//) {
523 my $express = $1;
524 my $op = $2;
525
526 if (process_expression($name, $express)) {
527 if ($op eq "||") {
528 return 1;
529 }
530 } else {
531 if ($op eq "&&") {
532 return 0;
533 }
534 }
535 }
Steven Rostedt45d73a52011-09-30 19:44:53 -0400536
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400537 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
538 my $ret = process_compare($1, $2, $3);
539 if ($ret < 0) {
540 die "$name: $.: Unable to process comparison\n";
541 }
542 return $ret;
543 }
544
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400545 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
546 if (defined $1) {
547 return !value_defined($2);
548 } else {
549 return value_defined($2);
550 }
551 }
552
Steven Rostedt45d73a52011-09-30 19:44:53 -0400553 if ($val =~ /^\s*0\s*$/) {
554 return 0;
555 } elsif ($val =~ /^\s*\d+\s*$/) {
556 return 1;
557 }
558
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400559 die ("$name: $.: Undefined content $val in if statement\n");
Steven Rostedt8d735212011-10-17 11:36:44 -0400560}
561
562sub process_if {
563 my ($name, $value) = @_;
564
565 # Convert variables and replace undefined ones with 0
566 my $val = process_variables($value, 1);
567 my $ret = process_expression $name, $val;
568
569 return $ret;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400570}
571
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400572sub __read_config {
573 my ($config, $current_test_num) = @_;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400574
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400575 my $in;
576 open($in, $config) || die "can't read file $config";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400577
Steven Rostedta57419b2010-11-02 15:13:54 -0400578 my $name = $config;
579 $name =~ s,.*/(.*),$1,;
580
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400581 my $test_num = $$current_test_num;
Steven Rostedta57419b2010-11-02 15:13:54 -0400582 my $default = 1;
583 my $repeat = 1;
584 my $num_tests_set = 0;
585 my $skip = 0;
586 my $rest;
Steven Rostedta9f84422011-10-17 11:06:29 -0400587 my $line;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400588 my $test_case = 0;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400589 my $if = 0;
590 my $if_set = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400591 my $override = 0;
592
593 my %overrides;
Steven Rostedta57419b2010-11-02 15:13:54 -0400594
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400595 while (<$in>) {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400596
597 # ignore blank lines and comments
598 next if (/^\s*$/ || /\s*\#/);
599
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400600 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400601
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400602 my $type = $1;
603 $rest = $2;
Steven Rostedta9f84422011-10-17 11:06:29 -0400604 $line = $2;
Steven Rostedta57419b2010-11-02 15:13:54 -0400605
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400606 my $old_test_num;
607 my $old_repeat;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400608 $override = 0;
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400609
610 if ($type eq "TEST_START") {
611
612 if ($num_tests_set) {
613 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
614 }
615
616 $old_test_num = $test_num;
617 $old_repeat = $repeat;
618
619 $test_num += $repeat;
620 $default = 0;
621 $repeat = 1;
622 } else {
623 $default = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400624 }
625
Steven Rostedta9f84422011-10-17 11:06:29 -0400626 # If SKIP is anywhere in the line, the command will be skipped
627 if ($rest =~ s/\s+SKIP\b//) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400628 $skip = 1;
629 } else {
Steven Rostedt0df213c2011-06-14 20:51:37 -0400630 $test_case = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400631 $skip = 0;
632 }
633
Steven Rostedta9f84422011-10-17 11:06:29 -0400634 if ($rest =~ s/\sELSE\b//) {
635 if (!$if) {
636 die "$name: $.: ELSE found with out matching IF section\n$_";
637 }
638 $if = 0;
639
640 if ($if_set) {
641 $skip = 1;
642 } else {
643 $skip = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400644 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400645 }
646
Steven Rostedta9f84422011-10-17 11:06:29 -0400647 if ($rest =~ s/\sIF\s+(.*)//) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400648 if (process_if($name, $1)) {
649 $if_set = 1;
650 } else {
651 $skip = 1;
652 }
653 $if = 1;
654 } else {
655 $if = 0;
Steven Rostedta9f84422011-10-17 11:06:29 -0400656 $if_set = 0;
Steven Rostedta57419b2010-11-02 15:13:54 -0400657 }
658
Steven Rostedta9f84422011-10-17 11:06:29 -0400659 if (!$skip) {
660 if ($type eq "TEST_START") {
661 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
662 $repeat = $1;
663 $repeat_tests{"$test_num"} = $repeat;
664 }
665 } elsif ($rest =~ s/\sOVERRIDE\b//) {
666 # DEFAULT only
667 $override = 1;
668 # Clear previous overrides
669 %overrides = ();
670 }
671 }
672
673 if (!$skip && $rest !~ /^\s*$/) {
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400674 die "$name: $.: Gargbage found after $type\n$_";
Steven Rostedta57419b2010-11-02 15:13:54 -0400675 }
676
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400677 if ($skip && $type eq "TEST_START") {
Steven Rostedta57419b2010-11-02 15:13:54 -0400678 $test_num = $old_test_num;
Steven Rostedte48c5292010-11-02 14:35:37 -0400679 $repeat = $old_repeat;
Steven Rostedta57419b2010-11-02 15:13:54 -0400680 }
681
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400682 } elsif (/^\s*ELSE\b(.*)$/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400683 if (!$if) {
684 die "$name: $.: ELSE found with out matching IF section\n$_";
685 }
686 $rest = $1;
687 if ($if_set) {
688 $skip = 1;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400689 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400690 } else {
691 $skip = 0;
692
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400693 if ($rest =~ /\sIF\s+(.*)/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400694 # May be a ELSE IF section.
695 if (!process_if($name, $1)) {
696 $skip = 1;
697 }
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400698 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400699 } else {
700 $if = 0;
701 }
702 }
703
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400704 if ($rest !~ /^\s*$/) {
705 die "$name: $.: Gargbage found after DEFAULTS\n$_";
706 }
707
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400708 } elsif (/^\s*INCLUDE\s+(\S+)/) {
709
710 next if ($skip);
711
712 if (!$default) {
713 die "$name: $.: INCLUDE can only be done in default sections\n$_";
714 }
715
716 my $file = process_variables($1);
717
718 if ($file !~ m,^/,) {
719 # check the path of the config file first
720 if ($config =~ m,(.*)/,) {
721 if (-f "$1/$file") {
722 $file = "$1/$file";
723 }
724 }
725 }
726
727 if ( ! -r $file ) {
728 die "$name: $.: Can't read file $file\n$_";
729 }
730
731 if (__read_config($file, \$test_num)) {
732 $test_case = 1;
733 }
734
Steven Rostedta57419b2010-11-02 15:13:54 -0400735 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
736
737 next if ($skip);
738
Steven Rostedt2545eb62010-11-02 15:01:32 -0400739 my $lvalue = $1;
740 my $rvalue = $2;
741
Steven Rostedta57419b2010-11-02 15:13:54 -0400742 if (!$default &&
743 ($lvalue eq "NUM_TESTS" ||
744 $lvalue eq "LOG_FILE" ||
745 $lvalue eq "CLEAR_LOG")) {
746 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400747 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400748
749 if ($lvalue eq "NUM_TESTS") {
750 if ($test_num) {
751 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
752 }
753 if (!$default) {
754 die "$name: $.: NUM_TESTS must be set in default section\n";
755 }
756 $num_tests_set = 1;
757 }
758
759 if ($default || $lvalue =~ /\[\d+\]$/) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400760 set_value($lvalue, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400761 } else {
762 my $val = "$lvalue\[$test_num\]";
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400763 set_value($val, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400764
765 if ($repeat > 1) {
766 $repeats{$val} = $repeat;
767 }
768 }
Steven Rostedt77d942c2011-05-20 13:36:58 -0400769 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
770 next if ($skip);
771
772 my $lvalue = $1;
773 my $rvalue = $2;
774
775 # process config variables.
776 # Config variables are only active while reading the
777 # config and can be defined anywhere. They also ignore
778 # TEST_START and DEFAULTS, but are skipped if they are in
779 # on of these sections that have SKIP defined.
780 # The save variable can be
781 # defined multiple times and the new one simply overrides
782 # the prevous one.
783 set_variable($lvalue, $rvalue);
784
Steven Rostedta57419b2010-11-02 15:13:54 -0400785 } else {
786 die "$name: $.: Garbage found in config\n$_";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400787 }
788 }
789
Steven Rostedta57419b2010-11-02 15:13:54 -0400790 if ($test_num) {
791 $test_num += $repeat - 1;
792 $opt{"NUM_TESTS"} = $test_num;
793 }
794
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400795 close($in);
796
797 $$current_test_num = $test_num;
798
799 return $test_case;
800}
801
Steven Rostedtc4261d02011-11-23 13:41:18 -0500802sub get_test_case {
803 print "What test case would you like to run?\n";
804 print " (build, install or boot)\n";
805 print " Other tests are available but require editing the config file\n";
806 my $ans = <STDIN>;
807 chomp $ans;
808 $default{"TEST_TYPE"} = $ans;
809}
810
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400811sub read_config {
812 my ($config) = @_;
813
814 my $test_case;
815 my $test_num = 0;
816
817 $test_case = __read_config $config, \$test_num;
818
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500819 # make sure we have all mandatory configs
820 get_ktest_configs;
821
Steven Rostedt0df213c2011-06-14 20:51:37 -0400822 # was a test specified?
823 if (!$test_case) {
824 print "No test case specified.\n";
Steven Rostedtc4261d02011-11-23 13:41:18 -0500825 get_test_case;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400826 }
827
Steven Rostedta75fece2010-11-02 14:58:27 -0400828 # set any defaults
829
830 foreach my $default (keys %default) {
831 if (!defined($opt{$default})) {
832 $opt{$default} = $default{$default};
833 }
834 }
Steven Rostedt2545eb62010-11-02 15:01:32 -0400835}
836
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400837sub __eval_option {
838 my ($option, $i) = @_;
839
840 # Add space to evaluate the character before $
841 $option = " $option";
842 my $retval = "";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530843 my $repeated = 0;
844 my $parent = 0;
845
846 foreach my $test (keys %repeat_tests) {
847 if ($i >= $test &&
848 $i < $test + $repeat_tests{$test}) {
849
850 $repeated = 1;
851 $parent = $test;
852 last;
853 }
854 }
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400855
856 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
857 my $start = $1;
858 my $var = $2;
859 my $end = $3;
860
861 # Append beginning of line
862 $retval = "$retval$start";
863
864 # If the iteration option OPT[$i] exists, then use that.
865 # otherwise see if the default OPT (without [$i]) exists.
866
867 my $o = "$var\[$i\]";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530868 my $parento = "$var\[$parent\]";
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400869
870 if (defined($opt{$o})) {
871 $o = $opt{$o};
872 $retval = "$retval$o";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530873 } elsif ($repeated && defined($opt{$parento})) {
874 $o = $opt{$parento};
875 $retval = "$retval$o";
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400876 } elsif (defined($opt{$var})) {
877 $o = $opt{$var};
878 $retval = "$retval$o";
879 } else {
880 $retval = "$retval\$\{$var\}";
881 }
882
883 $option = $end;
884 }
885
886 $retval = "$retval$option";
887
888 $retval =~ s/^ //;
889
890 return $retval;
891}
892
893sub eval_option {
894 my ($option, $i) = @_;
895
896 my $prev = "";
897
898 # Since an option can evaluate to another option,
899 # keep iterating until we do not evaluate any more
900 # options.
901 my $r = 0;
902 while ($prev ne $option) {
903 # Check for recursive evaluations.
904 # 100 deep should be more than enough.
905 if ($r++ > 100) {
906 die "Over 100 evaluations accurred with $option\n" .
907 "Check for recursive variables\n";
908 }
909 $prev = $option;
910 $option = __eval_option($option, $i);
911 }
912
913 return $option;
914}
915
Steven Rostedtd1e2f222010-11-08 16:39:57 -0500916sub _logit {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400917 if (defined($opt{"LOG_FILE"})) {
918 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
919 print OUT @_;
920 close(OUT);
921 }
922}
923
Steven Rostedtd1e2f222010-11-08 16:39:57 -0500924sub logit {
925 if (defined($opt{"LOG_FILE"})) {
926 _logit @_;
927 } else {
928 print @_;
929 }
930}
931
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400932sub doprint {
933 print @_;
Steven Rostedtd1e2f222010-11-08 16:39:57 -0500934 _logit @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400935}
936
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400937sub run_command;
Andrew Jones2728be42011-08-12 15:32:05 +0200938sub start_monitor;
939sub end_monitor;
940sub wait_for_monitor;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400941
942sub reboot {
Andrew Jones2728be42011-08-12 15:32:05 +0200943 my ($time) = @_;
944
Steven Rostedt2b803362011-09-30 18:00:23 -0400945 if (defined($time)) {
946 start_monitor;
947 # flush out current monitor
948 # May contain the reboot success line
949 wait_for_monitor 1;
950 }
951
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400952 # try to reboot normally
Steven Rostedte48c5292010-11-02 14:35:37 -0400953 if (run_command $reboot) {
Steven Rostedt576f6272010-11-02 14:58:38 -0400954 if (defined($powercycle_after_reboot)) {
955 sleep $powercycle_after_reboot;
956 run_command "$power_cycle";
957 }
958 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400959 # nope? power cycle it.
Steven Rostedta75fece2010-11-02 14:58:27 -0400960 run_command "$power_cycle";
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400961 }
Andrew Jones2728be42011-08-12 15:32:05 +0200962
963 if (defined($time)) {
Steven Rostedt2b803362011-09-30 18:00:23 -0400964 wait_for_monitor($time, $reboot_success_line);
Andrew Jones2728be42011-08-12 15:32:05 +0200965 end_monitor;
966 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400967}
968
Steven Rostedtbc7c5802011-12-22 16:29:10 -0500969sub reboot_to_good {
970 my ($time) = @_;
971
972 if (defined($switch_to_good)) {
973 run_command $switch_to_good;
974 return;
975 }
976
977 reboot $time;
978}
979
Steven Rostedt576f6272010-11-02 14:58:38 -0400980sub do_not_reboot {
981 my $i = $iteration;
982
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400983 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -0400984 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
985 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
986}
987
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400988sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400989 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400990
Steven Rostedt576f6272010-11-02 14:58:38 -0400991 my $i = $iteration;
992
993 if ($reboot_on_error && !do_not_reboot) {
994
Steven Rostedt75c3fda72010-11-02 14:57:21 -0400995 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -0500996 reboot_to_good;
Steven Rostedt75c3fda72010-11-02 14:57:21 -0400997
Steven Rostedta75fece2010-11-02 14:58:27 -0400998 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400999 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001000 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001001 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001002
Steven Rostedtf80802c2011-03-07 13:18:47 -05001003 if (defined($opt{"LOG_FILE"})) {
1004 print " See $opt{LOG_FILE} for more info.\n";
1005 }
1006
Steven Rostedt576f6272010-11-02 14:58:38 -04001007 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001008}
1009
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001010sub open_console {
1011 my ($fp) = @_;
1012
1013 my $flags;
1014
Steven Rostedta75fece2010-11-02 14:58:27 -04001015 my $pid = open($fp, "$console|") or
1016 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001017
1018 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001019 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001020 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001021 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001022
1023 return $pid;
1024}
1025
1026sub close_console {
1027 my ($fp, $pid) = @_;
1028
1029 doprint "kill child process $pid\n";
1030 kill 2, $pid;
1031
1032 print "closing!\n";
1033 close($fp);
1034}
1035
1036sub start_monitor {
1037 if ($monitor_cnt++) {
1038 return;
1039 }
1040 $monitor_fp = \*MONFD;
1041 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001042
1043 return;
1044
1045 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001046}
1047
1048sub end_monitor {
1049 if (--$monitor_cnt) {
1050 return;
1051 }
1052 close_console($monitor_fp, $monitor_pid);
1053}
1054
1055sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001056 my ($time, $stop) = @_;
1057 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001058 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001059 my $booted = 0;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001060
Steven Rostedta75fece2010-11-02 14:58:27 -04001061 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001062
1063 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001064 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001065 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001066 last if (!defined($line));
1067 print "$line";
1068 $full_line .= $line;
1069
1070 if (defined($stop) && $full_line =~ /$stop/) {
1071 doprint "wait for monitor detected $stop\n";
1072 $booted = 1;
1073 }
1074
1075 if ($line =~ /\n/) {
1076 $full_line = "";
1077 }
1078 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001079 print "** Monitor flushed **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001080}
1081
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301082sub save_logs {
1083 my ($result, $basedir) = @_;
1084 my @t = localtime;
1085 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1086 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1087
1088 my $type = $build_type;
1089 if ($type =~ /useconfig/) {
1090 $type = "useconfig";
1091 }
1092
1093 my $dir = "$machine-$test_type-$type-$result-$date";
1094
1095 $dir = "$basedir/$dir";
1096
1097 if (!-d $dir) {
1098 mkpath($dir) or
1099 die "can't create $dir";
1100 }
1101
1102 my %files = (
1103 "config" => $output_config,
1104 "buildlog" => $buildlog,
1105 "dmesg" => $dmesg,
1106 "testlog" => $testlog,
1107 );
1108
1109 while (my ($name, $source) = each(%files)) {
1110 if (-f "$source") {
1111 cp "$source", "$dir/$name" or
1112 die "failed to copy $source";
1113 }
1114 }
1115
1116 doprint "*** Saved info to $dir ***\n";
1117}
1118
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001119sub fail {
1120
Steven Rostedta75fece2010-11-02 14:58:27 -04001121 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001122 dodie @_;
1123 }
1124
Steven Rostedta75fece2010-11-02 14:58:27 -04001125 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001126
Steven Rostedt576f6272010-11-02 14:58:38 -04001127 my $i = $iteration;
1128
Steven Rostedta75fece2010-11-02 14:58:27 -04001129 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001130 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001131 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001132 reboot_to_good $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001133 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001134
Steven Rostedt9064af52011-06-13 10:38:48 -04001135 my $name = "";
1136
1137 if (defined($test_name)) {
1138 $name = " ($test_name)";
1139 }
1140
Steven Rostedt576f6272010-11-02 14:58:38 -04001141 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1142 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001143 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001144 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1145 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001146
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301147 if (defined($store_failures)) {
1148 save_logs "fail", $store_failures;
1149 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001150
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001151 return 1;
1152}
1153
Steven Rostedt2545eb62010-11-02 15:01:32 -04001154sub run_command {
1155 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001156 my $dolog = 0;
1157 my $dord = 0;
1158 my $pid;
1159
Steven Rostedte48c5292010-11-02 14:35:37 -04001160 $command =~ s/\$SSH_USER/$ssh_user/g;
1161 $command =~ s/\$MACHINE/$machine/g;
1162
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001163 doprint("$command ... ");
1164
1165 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001166 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001167
1168 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001169 open(LOG, ">>$opt{LOG_FILE}") or
1170 dodie "failed to write to log";
1171 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001172 }
1173
1174 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001175 open (RD, ">$redirect") or
1176 dodie "failed to write to redirect $redirect";
1177 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001178 }
1179
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001180 while (<CMD>) {
1181 print LOG if ($dolog);
1182 print RD if ($dord);
1183 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001184
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001185 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001186 my $failed = $?;
1187
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001188 close(CMD);
1189 close(LOG) if ($dolog);
1190 close(RD) if ($dord);
1191
Steven Rostedt2545eb62010-11-02 15:01:32 -04001192 if ($failed) {
1193 doprint "FAILED!\n";
1194 } else {
1195 doprint "SUCCESS\n";
1196 }
1197
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001198 return !$failed;
1199}
1200
Steven Rostedte48c5292010-11-02 14:35:37 -04001201sub run_ssh {
1202 my ($cmd) = @_;
1203 my $cp_exec = $ssh_exec;
1204
1205 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1206 return run_command "$cp_exec";
1207}
1208
1209sub run_scp {
1210 my ($src, $dst) = @_;
1211 my $cp_scp = $scp_to_target;
1212
1213 $cp_scp =~ s/\$SRC_FILE/$src/g;
1214 $cp_scp =~ s/\$DST_FILE/$dst/g;
1215
1216 return run_command "$cp_scp";
1217}
1218
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001219sub get_grub_index {
1220
Steven Rostedta75fece2010-11-02 14:58:27 -04001221 if ($reboot_type ne "grub") {
1222 return;
1223 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001224 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001225
1226 doprint "Find grub menu ... ";
1227 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001228
1229 my $ssh_grub = $ssh_exec;
1230 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1231
1232 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001233 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001234
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001235 my $found = 0;
1236
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001237 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001238 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001239 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001240 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001241 last;
1242 } elsif (/^\s*title\s/) {
1243 $grub_number++;
1244 }
1245 }
1246 close(IN);
1247
Steven Rostedta75fece2010-11-02 14:58:27 -04001248 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001249 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001250 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001251}
1252
Steven Rostedt2545eb62010-11-02 15:01:32 -04001253sub wait_for_input
1254{
1255 my ($fp, $time) = @_;
1256 my $rin;
1257 my $ready;
1258 my $line;
1259 my $ch;
1260
1261 if (!defined($time)) {
1262 $time = $timeout;
1263 }
1264
1265 $rin = '';
1266 vec($rin, fileno($fp), 1) = 1;
1267 $ready = select($rin, undef, undef, $time);
1268
1269 $line = "";
1270
1271 # try to read one char at a time
1272 while (sysread $fp, $ch, 1) {
1273 $line .= $ch;
1274 last if ($ch eq "\n");
1275 }
1276
1277 if (!length($line)) {
1278 return undef;
1279 }
1280
1281 return $line;
1282}
1283
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001284sub reboot_to {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001285 if (defined($switch_to_test)) {
1286 run_command $switch_to_test;
1287 }
1288
Steven Rostedta75fece2010-11-02 14:58:27 -04001289 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001290 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1291 reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -04001292 return;
1293 }
1294
1295 run_command "$reboot_script";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001296}
1297
Steven Rostedta57419b2010-11-02 15:13:54 -04001298sub get_sha1 {
1299 my ($commit) = @_;
1300
1301 doprint "git rev-list --max-count=1 $commit ... ";
1302 my $sha1 = `git rev-list --max-count=1 $commit`;
1303 my $ret = $?;
1304
1305 logit $sha1;
1306
1307 if ($ret) {
1308 doprint "FAILED\n";
1309 dodie "Failed to get git $commit";
1310 }
1311
1312 print "SUCCESS\n";
1313
1314 chomp $sha1;
1315
1316 return $sha1;
1317}
1318
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001319sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001320 my $booted = 0;
1321 my $bug = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001322 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001323 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001324
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001325 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001326
1327 my $line;
1328 my $full_line = "";
1329
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001330 open(DMESG, "> $dmesg") or
1331 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001332
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001333 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001334
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001335 my $success_start;
1336 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001337 my $monitor_start = time;
1338 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001339 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001340
Steven Rostedt2d01b262011-03-08 09:47:54 -05001341 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001342
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001343 if ($bug && defined($stop_after_failure) &&
1344 $stop_after_failure >= 0) {
1345 my $time = $stop_after_failure - (time - $failure_start);
1346 $line = wait_for_input($monitor_fp, $time);
1347 if (!defined($line)) {
1348 doprint "bug timed out after $booted_timeout seconds\n";
1349 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1350 last;
1351 }
1352 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001353 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001354 if (!defined($line)) {
1355 my $s = $booted_timeout == 1 ? "" : "s";
1356 doprint "Successful boot found: break after $booted_timeout second$s\n";
1357 last;
1358 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001359 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001360 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001361 if (!defined($line)) {
1362 my $s = $timeout == 1 ? "" : "s";
1363 doprint "Timed out after $timeout second$s\n";
1364 last;
1365 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001366 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001367
Steven Rostedt2545eb62010-11-02 15:01:32 -04001368 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001369 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001370
1371 # we are not guaranteed to get a full line
1372 $full_line .= $line;
1373
Steven Rostedta75fece2010-11-02 14:58:27 -04001374 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001375 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001376 $success_start = time;
1377 }
1378
1379 if ($booted && defined($stop_after_success) &&
1380 $stop_after_success >= 0) {
1381 my $now = time;
1382 if ($now - $success_start >= $stop_after_success) {
1383 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1384 last;
1385 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001386 }
1387
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001388 if ($full_line =~ /\[ backtrace testing \]/) {
1389 $skip_call_trace = 1;
1390 }
1391
Steven Rostedt2545eb62010-11-02 15:01:32 -04001392 if ($full_line =~ /call trace:/i) {
Steven Rostedt46519202011-03-08 09:40:31 -05001393 if (!$bug && !$skip_call_trace) {
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001394 $bug = 1;
1395 $failure_start = time;
1396 }
1397 }
1398
1399 if ($bug && defined($stop_after_failure) &&
1400 $stop_after_failure >= 0) {
1401 my $now = time;
1402 if ($now - $failure_start >= $stop_after_failure) {
1403 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1404 last;
1405 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001406 }
1407
1408 if ($full_line =~ /\[ end of backtrace testing \]/) {
1409 $skip_call_trace = 0;
1410 }
1411
1412 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001413 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001414 $bug = 1;
1415 }
1416
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001417 # Detect triple faults by testing the banner
1418 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1419 if ($1 eq $version) {
1420 $version_found = 1;
1421 } elsif ($version_found && $detect_triplefault) {
1422 # We already booted into the kernel we are testing,
1423 # but now we booted into another kernel?
1424 # Consider this a triple fault.
1425 doprint "Aleady booted in Linux kernel $version, but now\n";
1426 doprint "we booted into Linux kernel $1.\n";
1427 doprint "Assuming that this is a triple fault.\n";
1428 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1429 last;
1430 }
1431 }
1432
Steven Rostedt2545eb62010-11-02 15:01:32 -04001433 if ($line =~ /\n/) {
1434 $full_line = "";
1435 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001436
1437 if ($stop_test_after > 0 && !$booted && !$bug) {
1438 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001439 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001440 $done = 1;
1441 }
1442 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001443 }
1444
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001445 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001446
Steven Rostedt2545eb62010-11-02 15:01:32 -04001447 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001448 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001449 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001450 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001451
Steven Rostedta75fece2010-11-02 14:58:27 -04001452 if (!$booted) {
1453 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001454 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001455 }
1456
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001457 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001458}
1459
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001460sub eval_kernel_version {
1461 my ($option) = @_;
1462
1463 $option =~ s/\$KERNEL_VERSION/$version/g;
1464
1465 return $option;
1466}
1467
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001468sub do_post_install {
1469
1470 return if (!defined($post_install));
1471
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001472 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001473 run_command "$cp_post_install" or
1474 dodie "Failed to run post install";
1475}
1476
Steven Rostedt2545eb62010-11-02 15:01:32 -04001477sub install {
1478
Steven Rostedte0a87422011-09-30 17:50:48 -04001479 return if ($no_install);
1480
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001481 my $cp_target = eval_kernel_version $target_image;
1482
1483 run_scp "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001484 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001485
1486 my $install_mods = 0;
1487
1488 # should we process modules?
1489 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001490 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001491 while (<IN>) {
1492 if (/CONFIG_MODULES(=y)?/) {
1493 $install_mods = 1 if (defined($1));
1494 last;
1495 }
1496 }
1497 close(IN);
1498
1499 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001500 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001501 doprint "No modules needed\n";
1502 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001503 }
1504
Steven Rostedta75fece2010-11-02 14:58:27 -04001505 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001506 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001507
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001508 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001509 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001510
Steven Rostedte48c5292010-11-02 14:35:37 -04001511 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001512 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001513
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001514 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001515 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001516 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001517
Steven Rostedte48c5292010-11-02 14:35:37 -04001518 run_scp "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001519 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001520
Steven Rostedta75fece2010-11-02 14:58:27 -04001521 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001522
Steven Rostedte7b13442011-06-14 20:44:36 -04001523 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001524 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001525
Steven Rostedte48c5292010-11-02 14:35:37 -04001526 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001527
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001528 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001529}
1530
Steven Rostedtddf607e2011-06-14 20:49:13 -04001531sub get_version {
1532 # get the release name
1533 doprint "$make kernelrelease ... ";
1534 $version = `$make kernelrelease | tail -1`;
1535 chomp($version);
1536 doprint "$version\n";
1537}
1538
1539sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001540 # Make sure the stable kernel has finished booting
1541 start_monitor;
1542 wait_for_monitor 5;
1543 end_monitor;
1544
Steven Rostedtddf607e2011-06-14 20:49:13 -04001545 get_grub_index;
1546 get_version;
1547 install;
1548
1549 start_monitor;
1550 return monitor;
1551}
1552
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001553sub check_buildlog {
1554 my ($patch) = @_;
1555
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001556 my @files = `git show $patch | diffstat -l`;
1557
1558 open(IN, "git show $patch |") or
1559 dodie "failed to show $patch";
1560 while (<IN>) {
1561 if (m,^--- a/(.*),) {
1562 chomp $1;
1563 $files[$#files] = $1;
1564 }
1565 }
1566 close(IN);
1567
1568 open(IN, $buildlog) or dodie "Can't open $buildlog";
1569 while (<IN>) {
1570 if (/^\s*(.*?):.*(warning|error)/) {
1571 my $err = $1;
1572 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001573 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001574 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001575 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001576 }
1577 }
1578 }
1579 }
1580 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001581
1582 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001583}
1584
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001585sub apply_min_config {
1586 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05001587
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001588 # Read the config file and remove anything that
1589 # is in the force_config hash (from minconfig and others)
1590 # then add the force config back.
1591
1592 doprint "Applying minimum configurations into $output_config.new\n";
1593
1594 open (OUT, ">$outconfig") or
1595 dodie "Can't create $outconfig";
1596
1597 if (-f $output_config) {
1598 open (IN, $output_config) or
1599 dodie "Failed to open $output_config";
1600 while (<IN>) {
1601 if (/^(# )?(CONFIG_[^\s=]*)/) {
1602 next if (defined($force_config{$2}));
1603 }
1604 print OUT;
1605 }
1606 close IN;
1607 }
1608 foreach my $config (keys %force_config) {
1609 print OUT "$force_config{$config}\n";
1610 }
1611 close OUT;
1612
1613 run_command "mv $outconfig $output_config";
1614}
1615
1616sub make_oldconfig {
1617
Steven Rostedt4c4ab122011-07-15 21:16:17 -04001618 my @force_list = keys %force_config;
1619
1620 if ($#force_list >= 0) {
1621 apply_min_config;
1622 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001623
1624 if (!run_command "$make oldnoconfig") {
Steven Rostedt612b9e92011-03-07 13:27:43 -05001625 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1626 # try a yes '' | oldconfig
1627 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001628 run_command "yes '' | $make oldconfig" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001629 dodie "failed make config oldconfig";
1630 }
1631}
1632
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001633# read a config file and use this to force new configs.
1634sub load_force_config {
1635 my ($config) = @_;
1636
1637 open(IN, $config) or
1638 dodie "failed to read $config";
1639 while (<IN>) {
1640 chomp;
1641 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1642 $force_config{$1} = $_;
1643 } elsif (/^# (CONFIG_\S*) is not set/) {
1644 $force_config{$1} = $_;
1645 }
1646 }
1647 close IN;
1648}
1649
Steven Rostedt2545eb62010-11-02 15:01:32 -04001650sub build {
1651 my ($type) = @_;
1652
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001653 unlink $buildlog;
1654
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001655 # Failed builds should not reboot the target
1656 my $save_no_reboot = $no_reboot;
1657 $no_reboot = 1;
1658
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001659 if (defined($pre_build)) {
1660 my $ret = run_command $pre_build;
1661 if (!$ret && defined($pre_build_die) &&
1662 $pre_build_die) {
1663 dodie "failed to pre_build\n";
1664 }
1665 }
1666
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001667 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001668 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001669 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001670
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001671 $type = "oldconfig";
1672 }
1673
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001674 # old config can ask questions
1675 if ($type eq "oldconfig") {
Steven Rostedt9386c6a2010-11-08 16:35:48 -05001676 $type = "oldnoconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001677
1678 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001679 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001680
Andrew Jones13488232011-08-12 15:32:04 +02001681 if (!$noclean) {
1682 run_command "mv $output_config $outputdir/config_temp" or
1683 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001684
Andrew Jones13488232011-08-12 15:32:04 +02001685 run_command "$make mrproper" or dodie "make mrproper";
1686
1687 run_command "mv $outputdir/config_temp $output_config" or
1688 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001689 }
1690
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001691 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001692 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001693 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001694 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001695 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001696
1697 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04001698 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1699 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001700 close(OUT);
1701
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001702 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001703 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001704 }
1705
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001706 if ($type ne "oldnoconfig") {
1707 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001708 dodie "failed make config";
1709 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001710 # Run old config regardless, to enforce min configurations
1711 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001712
Steven Rostedta75fece2010-11-02 14:58:27 -04001713 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001714 my $build_ret = run_command "$make $build_options";
1715 undef $redirect;
1716
1717 if (defined($post_build)) {
1718 my $ret = run_command $post_build;
1719 if (!$ret && defined($post_build_die) &&
1720 $post_build_die) {
1721 dodie "failed to post_build\n";
1722 }
1723 }
1724
1725 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001726 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001727 if ($in_bisect) {
1728 $no_reboot = $save_no_reboot;
1729 return 0;
1730 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001731 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001732 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001733
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001734 $no_reboot = $save_no_reboot;
1735
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001736 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001737}
1738
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001739sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04001740 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001741 if (defined($poweroff_after_halt)) {
1742 sleep $poweroff_after_halt;
1743 run_command "$power_off";
1744 }
1745 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001746 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04001747 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001748 }
1749}
1750
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001751sub success {
1752 my ($i) = @_;
1753
Steven Rostedte48c5292010-11-02 14:35:37 -04001754 $successes++;
1755
Steven Rostedt9064af52011-06-13 10:38:48 -04001756 my $name = "";
1757
1758 if (defined($test_name)) {
1759 $name = " ($test_name)";
1760 }
1761
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001762 doprint "\n\n*******************************************\n";
1763 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001764 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001765 doprint "*******************************************\n";
1766 doprint "*******************************************\n";
1767
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301768 if (defined($store_successes)) {
1769 save_logs "success", $store_successes;
1770 }
1771
Steven Rostedt576f6272010-11-02 14:58:38 -04001772 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001773 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001774 reboot_to_good $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001775 }
1776}
1777
Steven Rostedtc960bb92011-03-08 09:22:39 -05001778sub answer_bisect {
1779 for (;;) {
1780 doprint "Pass or fail? [p/f]";
1781 my $ans = <STDIN>;
1782 chomp $ans;
1783 if ($ans eq "p" || $ans eq "P") {
1784 return 1;
1785 } elsif ($ans eq "f" || $ans eq "F") {
1786 return 0;
1787 } else {
1788 print "Please answer 'P' or 'F'\n";
1789 }
1790 }
1791}
1792
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001793sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001794 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001795
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001796 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04001797 $reboot_on_error = 0;
1798 $poweroff_on_error = 0;
1799 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001800
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301801 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001802 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301803 undef $redirect;
1804
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001805 exit $failed;
1806}
1807
1808my $child_done;
1809
1810sub child_finished {
1811 $child_done = 1;
1812}
1813
1814sub do_run_test {
1815 my $child_pid;
1816 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001817 my $line;
1818 my $full_line;
1819 my $bug = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001820
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001821 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001822
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001823 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001824
1825 $child_done = 0;
1826
1827 $SIG{CHLD} = qw(child_finished);
1828
1829 $child_pid = fork;
1830
1831 child_run_test if (!$child_pid);
1832
1833 $full_line = "";
1834
1835 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001836 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001837 if (defined($line)) {
1838
1839 # we are not guaranteed to get a full line
1840 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05001841 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001842
1843 if ($full_line =~ /call trace:/i) {
1844 $bug = 1;
1845 }
1846
1847 if ($full_line =~ /Kernel panic -/) {
1848 $bug = 1;
1849 }
1850
1851 if ($line =~ /\n/) {
1852 $full_line = "";
1853 }
1854 }
1855 } while (!$child_done && !$bug);
1856
1857 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05001858 my $failure_start = time;
1859 my $now;
1860 do {
1861 $line = wait_for_input($monitor_fp, 1);
1862 if (defined($line)) {
1863 doprint $line;
1864 }
1865 $now = time;
1866 if ($now - $failure_start >= $stop_after_failure) {
1867 last;
1868 }
1869 } while (defined($line));
1870
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001871 doprint "Detected kernel crash!\n";
1872 # kill the child with extreme prejudice
1873 kill 9, $child_pid;
1874 }
1875
1876 waitpid $child_pid, 0;
1877 $child_exit = $?;
1878
Steven Rostedtc5dacb82011-12-22 12:43:57 -05001879 if (!$bug && $in_bisect) {
1880 if (defined($bisect_ret_good)) {
1881 if ($child_exit == $bisect_ret_good) {
1882 return 1;
1883 }
1884 }
1885 if (defined($bisect_ret_skip)) {
1886 if ($child_exit == $bisect_ret_skip) {
1887 return -1;
1888 }
1889 }
1890 if (defined($bisect_ret_abort)) {
1891 if ($child_exit == $bisect_ret_abort) {
1892 fail "test abort" and return -2;
1893 }
1894 }
1895 if (defined($bisect_ret_bad)) {
1896 if ($child_exit == $bisect_ret_skip) {
1897 return 0;
1898 }
1899 }
1900 if (defined($bisect_ret_default)) {
1901 if ($bisect_ret_default eq "good") {
1902 return 1;
1903 } elsif ($bisect_ret_default eq "bad") {
1904 return 0;
1905 } elsif ($bisect_ret_default eq "skip") {
1906 return -1;
1907 } elsif ($bisect_ret_default eq "abort") {
1908 return -2;
1909 } else {
1910 fail "unknown default action: $bisect_ret_default"
1911 and return -2;
1912 }
1913 }
1914 }
1915
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001916 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001917 return 0 if $in_bisect;
1918 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001919 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001920 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001921}
1922
Steven Rostedta75fece2010-11-02 14:58:27 -04001923sub run_git_bisect {
1924 my ($command) = @_;
1925
1926 doprint "$command ... ";
1927
1928 my $output = `$command 2>&1`;
1929 my $ret = $?;
1930
1931 logit $output;
1932
1933 if ($ret) {
1934 doprint "FAILED\n";
1935 dodie "Failed to git bisect";
1936 }
1937
1938 doprint "SUCCESS\n";
1939 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1940 doprint "$1 [$2]\n";
1941 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
1942 $bisect_bad = $1;
1943 doprint "Found bad commit... $1\n";
1944 return 0;
1945 } else {
1946 # we already logged it, just print it now.
1947 print $output;
1948 }
1949
1950 return 1;
1951}
1952
Steven Rostedtc23dca72011-03-08 09:26:31 -05001953sub bisect_reboot {
1954 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001955 reboot_to_good $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05001956}
1957
1958# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05001959sub run_bisect_test {
1960 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001961
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001962 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001963 my $result;
1964 my $output;
1965 my $ret;
1966
Steven Rostedt0a05c762010-11-08 11:14:10 -05001967 $in_bisect = 1;
1968
1969 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001970
1971 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05001972 if ($failed && $bisect_skip) {
1973 $in_bisect = 0;
1974 return -1;
1975 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001976 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001977
1978 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04001979 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001980
1981 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05001982 if ($failed && $bisect_skip) {
1983 end_monitor;
1984 bisect_reboot;
1985 $in_bisect = 0;
1986 return -1;
1987 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001988 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001989
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001990 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001991 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001992 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001993 }
1994
1995 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05001996 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001997 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05001998 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001999 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04002000
2001 # reboot the box to a kernel we can ssh to
2002 if ($type ne "build") {
2003 bisect_reboot;
2004 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002005 $in_bisect = 0;
2006
2007 return $result;
2008}
2009
2010sub run_bisect {
2011 my ($type) = @_;
2012 my $buildtype = "oldconfig";
2013
2014 # We should have a minconfig to use?
2015 if (defined($minconfig)) {
2016 $buildtype = "useconfig:$minconfig";
2017 }
2018
2019 my $ret = run_bisect_test $type, $buildtype;
2020
Steven Rostedtc960bb92011-03-08 09:22:39 -05002021 if ($bisect_manual) {
2022 $ret = answer_bisect;
2023 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002024
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002025 # Are we looking for where it worked, not failed?
2026 if ($reverse_bisect) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002027 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002028 }
2029
Steven Rostedtc23dca72011-03-08 09:26:31 -05002030 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002031 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002032 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002033 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002034 } elsif ($bisect_skip) {
2035 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2036 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002037 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002038}
2039
Steven Rostedtdad98752011-11-22 20:48:57 -05002040sub update_bisect_replay {
2041 my $tmp_log = "$tmpdir/ktest_bisect_log";
2042 run_command "git bisect log > $tmp_log" or
2043 die "can't create bisect log";
2044 return $tmp_log;
2045}
2046
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002047sub bisect {
2048 my ($i) = @_;
2049
2050 my $result;
2051
2052 die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
2053 die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
2054 die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
2055
2056 my $good = $opt{"BISECT_GOOD[$i]"};
2057 my $bad = $opt{"BISECT_BAD[$i]"};
2058 my $type = $opt{"BISECT_TYPE[$i]"};
Steven Rostedta75fece2010-11-02 14:58:27 -04002059 my $start = $opt{"BISECT_START[$i]"};
2060 my $replay = $opt{"BISECT_REPLAY[$i]"};
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002061 my $start_files = $opt{"BISECT_FILES[$i]"};
2062
2063 if (defined($start_files)) {
2064 $start_files = " -- " . $start_files;
2065 } else {
2066 $start_files = "";
2067 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002068
Steven Rostedta57419b2010-11-02 15:13:54 -04002069 # convert to true sha1's
2070 $good = get_sha1($good);
2071 $bad = get_sha1($bad);
2072
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002073 if (defined($opt{"BISECT_REVERSE[$i]"}) &&
2074 $opt{"BISECT_REVERSE[$i]"} == 1) {
2075 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2076 $reverse_bisect = 1;
2077 } else {
2078 $reverse_bisect = 0;
2079 }
2080
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002081 # Can't have a test without having a test to run
2082 if ($type eq "test" && !defined($run_test)) {
2083 $type = "boot";
2084 }
2085
Steven Rostedtdad98752011-11-22 20:48:57 -05002086 # Check if a bisect was running
2087 my $bisect_start_file = "$builddir/.git/BISECT_START";
2088
Steven Rostedta75fece2010-11-02 14:58:27 -04002089 my $check = $opt{"BISECT_CHECK[$i]"};
Steven Rostedtdad98752011-11-22 20:48:57 -05002090 my $do_check = defined($check) && $check ne "0";
2091
2092 if ( -f $bisect_start_file ) {
2093 print "Bisect in progress found\n";
2094 if ($do_check) {
2095 print " If you say yes, then no checks of good or bad will be done\n";
2096 }
2097 if (defined($replay)) {
2098 print "** BISECT_REPLAY is defined in config file **";
2099 print " Ignore config option and perform new git bisect log?\n";
2100 if (read_ync " (yes, no, or cancel) ") {
2101 $replay = update_bisect_replay;
2102 $do_check = 0;
2103 }
2104 } elsif (read_yn "read git log and continue?") {
2105 $replay = update_bisect_replay;
2106 $do_check = 0;
2107 }
2108 }
2109
2110 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002111
2112 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002113 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002114
2115 if ($check ne "good") {
2116 doprint "TESTING BISECT BAD [$bad]\n";
2117 run_command "git checkout $bad" or
2118 die "Failed to checkout $bad";
2119
2120 $result = run_bisect $type;
2121
2122 if ($result ne "bad") {
2123 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2124 }
2125 }
2126
2127 if ($check ne "bad") {
2128 doprint "TESTING BISECT GOOD [$good]\n";
2129 run_command "git checkout $good" or
2130 die "Failed to checkout $good";
2131
2132 $result = run_bisect $type;
2133
2134 if ($result ne "good") {
2135 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2136 }
2137 }
2138
2139 # checkout where we started
2140 run_command "git checkout $head" or
2141 die "Failed to checkout $head";
2142 }
2143
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002144 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002145 dodie "could not start bisect";
2146
2147 run_command "git bisect good $good" or
2148 dodie "could not set bisect good to $good";
2149
2150 run_git_bisect "git bisect bad $bad" or
2151 dodie "could not set bisect bad to $bad";
2152
2153 if (defined($replay)) {
2154 run_command "git bisect replay $replay" or
2155 dodie "failed to run replay";
2156 }
2157
2158 if (defined($start)) {
2159 run_command "git checkout $start" or
2160 dodie "failed to checkout $start";
2161 }
2162
2163 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002164 do {
2165 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002166 $test = run_git_bisect "git bisect $result";
2167 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002168
2169 run_command "git bisect log" or
2170 dodie "could not capture git bisect log";
2171
2172 run_command "git bisect reset" or
2173 dodie "could not reset git bisect";
2174
2175 doprint "Bad commit was [$bisect_bad]\n";
2176
Steven Rostedt0a05c762010-11-08 11:14:10 -05002177 success $i;
2178}
2179
2180my %config_ignore;
2181my %config_set;
2182
2183my %config_list;
2184my %null_config;
2185
2186my %dependency;
2187
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002188sub assign_configs {
2189 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002190
2191 open (IN, $config)
2192 or dodie "Failed to read $config";
2193
2194 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002195 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002196 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002197 }
2198 }
2199
2200 close(IN);
2201}
2202
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002203sub process_config_ignore {
2204 my ($config) = @_;
2205
2206 assign_configs \%config_ignore, $config;
2207}
2208
Steven Rostedt0a05c762010-11-08 11:14:10 -05002209sub read_current_config {
2210 my ($config_ref) = @_;
2211
2212 %{$config_ref} = ();
2213 undef %{$config_ref};
2214
2215 my @key = keys %{$config_ref};
2216 if ($#key >= 0) {
2217 print "did not delete!\n";
2218 exit;
2219 }
2220 open (IN, "$output_config");
2221
2222 while (<IN>) {
2223 if (/^(CONFIG\S+)=(.*)/) {
2224 ${$config_ref}{$1} = $2;
2225 }
2226 }
2227 close(IN);
2228}
2229
2230sub get_dependencies {
2231 my ($config) = @_;
2232
2233 my $arr = $dependency{$config};
2234 if (!defined($arr)) {
2235 return ();
2236 }
2237
2238 my @deps = @{$arr};
2239
2240 foreach my $dep (@{$arr}) {
2241 print "ADD DEP $dep\n";
2242 @deps = (@deps, get_dependencies $dep);
2243 }
2244
2245 return @deps;
2246}
2247
2248sub create_config {
2249 my @configs = @_;
2250
2251 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2252
2253 foreach my $config (@configs) {
2254 print OUT "$config_set{$config}\n";
2255 my @deps = get_dependencies $config;
2256 foreach my $dep (@deps) {
2257 print OUT "$config_set{$dep}\n";
2258 }
2259 }
2260
2261 foreach my $config (keys %config_ignore) {
2262 print OUT "$config_ignore{$config}\n";
2263 }
2264 close(OUT);
2265
2266# exit;
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002267 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002268}
2269
2270sub compare_configs {
2271 my (%a, %b) = @_;
2272
2273 foreach my $item (keys %a) {
2274 if (!defined($b{$item})) {
2275 print "diff $item\n";
2276 return 1;
2277 }
2278 delete $b{$item};
2279 }
2280
2281 my @keys = keys %b;
2282 if ($#keys) {
2283 print "diff2 $keys[0]\n";
2284 }
2285 return -1 if ($#keys >= 0);
2286
2287 return 0;
2288}
2289
2290sub run_config_bisect_test {
2291 my ($type) = @_;
2292
2293 return run_bisect_test $type, "oldconfig";
2294}
2295
2296sub process_passed {
2297 my (%configs) = @_;
2298
2299 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2300 # Passed! All these configs are part of a good compile.
2301 # Add them to the min options.
2302 foreach my $config (keys %configs) {
2303 if (defined($config_list{$config})) {
2304 doprint " removing $config\n";
2305 $config_ignore{$config} = $config_list{$config};
2306 delete $config_list{$config};
2307 }
2308 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002309 doprint "config copied to $outputdir/config_good\n";
2310 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002311}
2312
2313sub process_failed {
2314 my ($config) = @_;
2315
2316 doprint "\n\n***************************************\n";
2317 doprint "Found bad config: $config\n";
2318 doprint "***************************************\n\n";
2319}
2320
2321sub run_config_bisect {
2322
2323 my @start_list = keys %config_list;
2324
2325 if ($#start_list < 0) {
2326 doprint "No more configs to test!!!\n";
2327 return -1;
2328 }
2329
2330 doprint "***** RUN TEST ***\n";
2331 my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
2332 my $ret;
2333 my %current_config;
2334
2335 my $count = $#start_list + 1;
2336 doprint " $count configs to test\n";
2337
2338 my $half = int($#start_list / 2);
2339
2340 do {
2341 my @tophalf = @start_list[0 .. $half];
2342
2343 create_config @tophalf;
2344 read_current_config \%current_config;
2345
2346 $count = $#tophalf + 1;
2347 doprint "Testing $count configs\n";
2348 my $found = 0;
2349 # make sure we test something
2350 foreach my $config (@tophalf) {
2351 if (defined($current_config{$config})) {
2352 logit " $config\n";
2353 $found = 1;
2354 }
2355 }
2356 if (!$found) {
2357 # try the other half
2358 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002359 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedt0a05c762010-11-08 11:14:10 -05002360 create_config @tophalf;
2361 read_current_config \%current_config;
2362 foreach my $config (@tophalf) {
2363 if (defined($current_config{$config})) {
2364 logit " $config\n";
2365 $found = 1;
2366 }
2367 }
2368 if (!$found) {
2369 doprint "Failed: Can't make new config with current configs\n";
2370 foreach my $config (@start_list) {
2371 doprint " CONFIG: $config\n";
2372 }
2373 return -1;
2374 }
2375 $count = $#tophalf + 1;
2376 doprint "Testing $count configs\n";
2377 }
2378
2379 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002380 if ($bisect_manual) {
2381 $ret = answer_bisect;
2382 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002383 if ($ret) {
2384 process_passed %current_config;
2385 return 0;
2386 }
2387
2388 doprint "This config had a failure.\n";
2389 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002390 doprint "config copied to $outputdir/config_bad\n";
2391 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002392
2393 # A config exists in this group that was bad.
2394 foreach my $config (keys %config_list) {
2395 if (!defined($current_config{$config})) {
2396 doprint " removing $config\n";
2397 delete $config_list{$config};
2398 }
2399 }
2400
2401 @start_list = @tophalf;
2402
2403 if ($#start_list == 0) {
2404 process_failed $start_list[0];
2405 return 1;
2406 }
2407
2408 # remove half the configs we are looking at and see if
2409 # they are good.
2410 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002411 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002412
Steven Rostedtc960bb92011-03-08 09:22:39 -05002413 # we found a single config, try it again unless we are running manually
2414
2415 if ($bisect_manual) {
2416 process_failed $start_list[0];
2417 return 1;
2418 }
2419
Steven Rostedt0a05c762010-11-08 11:14:10 -05002420 my @tophalf = @start_list[0 .. 0];
2421
2422 $ret = run_config_bisect_test $type;
2423 if ($ret) {
2424 process_passed %current_config;
2425 return 0;
2426 }
2427
2428 process_failed $start_list[0];
2429 return 1;
2430}
2431
2432sub config_bisect {
2433 my ($i) = @_;
2434
2435 my $start_config = $opt{"CONFIG_BISECT[$i]"};
2436
2437 my $tmpconfig = "$tmpdir/use_config";
2438
Steven Rostedt30f75da2011-06-13 10:35:35 -04002439 if (defined($config_bisect_good)) {
2440 process_config_ignore $config_bisect_good;
2441 }
2442
Steven Rostedt0a05c762010-11-08 11:14:10 -05002443 # Make the file with the bad config and the min config
2444 if (defined($minconfig)) {
2445 # read the min config for things to ignore
2446 run_command "cp $minconfig $tmpconfig" or
2447 dodie "failed to copy $minconfig to $tmpconfig";
2448 } else {
2449 unlink $tmpconfig;
2450 }
2451
Steven Rostedt0a05c762010-11-08 11:14:10 -05002452 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002453 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002454 process_config_ignore $tmpconfig;
2455 }
2456
2457 # now process the start config
2458 run_command "cp $start_config $output_config" or
2459 dodie "failed to copy $start_config to $output_config";
2460
2461 # read directly what we want to check
2462 my %config_check;
2463 open (IN, $output_config)
2464 or dodie "faied to open $output_config";
2465
2466 while (<IN>) {
2467 if (/^((CONFIG\S*)=.*)/) {
2468 $config_check{$2} = $1;
2469 }
2470 }
2471 close(IN);
2472
Steven Rostedt250bae82011-07-15 22:05:59 -04002473 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002474 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002475
2476 # check to see what we lost (or gained)
2477 open (IN, $output_config)
2478 or dodie "Failed to read $start_config";
2479
2480 my %removed_configs;
2481 my %added_configs;
2482
2483 while (<IN>) {
2484 if (/^((CONFIG\S*)=.*)/) {
2485 # save off all options
2486 $config_set{$2} = $1;
2487 if (defined($config_check{$2})) {
2488 if (defined($config_ignore{$2})) {
2489 $removed_configs{$2} = $1;
2490 } else {
2491 $config_list{$2} = $1;
2492 }
2493 } elsif (!defined($config_ignore{$2})) {
2494 $added_configs{$2} = $1;
2495 $config_list{$2} = $1;
2496 }
2497 }
2498 }
2499 close(IN);
2500
2501 my @confs = keys %removed_configs;
2502 if ($#confs >= 0) {
2503 doprint "Configs overridden by default configs and removed from check:\n";
2504 foreach my $config (@confs) {
2505 doprint " $config\n";
2506 }
2507 }
2508 @confs = keys %added_configs;
2509 if ($#confs >= 0) {
2510 doprint "Configs appearing in make oldconfig and added:\n";
2511 foreach my $config (@confs) {
2512 doprint " $config\n";
2513 }
2514 }
2515
2516 my %config_test;
2517 my $once = 0;
2518
2519 # Sometimes kconfig does weird things. We must make sure
2520 # that the config we autocreate has everything we need
2521 # to test, otherwise we may miss testing configs, or
2522 # may not be able to create a new config.
2523 # Here we create a config with everything set.
2524 create_config (keys %config_list);
2525 read_current_config \%config_test;
2526 foreach my $config (keys %config_list) {
2527 if (!defined($config_test{$config})) {
2528 if (!$once) {
2529 $once = 1;
2530 doprint "Configs not produced by kconfig (will not be checked):\n";
2531 }
2532 doprint " $config\n";
2533 delete $config_list{$config};
2534 }
2535 }
2536 my $ret;
2537 do {
2538 $ret = run_config_bisect;
2539 } while (!$ret);
2540
2541 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002542
2543 success $i;
2544}
2545
Steven Rostedt27d934b2011-05-20 09:18:18 -04002546sub patchcheck_reboot {
2547 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002548 reboot_to_good $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04002549}
2550
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002551sub patchcheck {
2552 my ($i) = @_;
2553
2554 die "PATCHCHECK_START[$i] not defined\n"
2555 if (!defined($opt{"PATCHCHECK_START[$i]"}));
2556 die "PATCHCHECK_TYPE[$i] not defined\n"
2557 if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
2558
2559 my $start = $opt{"PATCHCHECK_START[$i]"};
2560
2561 my $end = "HEAD";
2562 if (defined($opt{"PATCHCHECK_END[$i]"})) {
2563 $end = $opt{"PATCHCHECK_END[$i]"};
2564 }
2565
Steven Rostedta57419b2010-11-02 15:13:54 -04002566 # Get the true sha1's since we can use things like HEAD~3
2567 $start = get_sha1($start);
2568 $end = get_sha1($end);
2569
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002570 my $type = $opt{"PATCHCHECK_TYPE[$i]"};
2571
2572 # Can't have a test without having a test to run
2573 if ($type eq "test" && !defined($run_test)) {
2574 $type = "boot";
2575 }
2576
2577 open (IN, "git log --pretty=oneline $end|") or
2578 dodie "could not get git list";
2579
2580 my @list;
2581
2582 while (<IN>) {
2583 chomp;
2584 $list[$#list+1] = $_;
2585 last if (/^$start/);
2586 }
2587 close(IN);
2588
2589 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002590 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002591 }
2592
2593 # go backwards in the list
2594 @list = reverse @list;
2595
2596 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04002597 my %ignored_warnings;
2598
2599 if (defined($ignore_warnings)) {
2600 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2601 $ignored_warnings{$sha1} = 1;
2602 }
2603 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002604
2605 $in_patchcheck = 1;
2606 foreach my $item (@list) {
2607 my $sha1 = $item;
2608 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2609
2610 doprint "\nProcessing commit $item\n\n";
2611
2612 run_command "git checkout $sha1" or
2613 die "Failed to checkout $sha1";
2614
2615 # only clean on the first and last patch
2616 if ($item eq $list[0] ||
2617 $item eq $list[$#list]) {
2618 $noclean = $save_clean;
2619 } else {
2620 $noclean = 1;
2621 }
2622
2623 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002624 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002625 } else {
2626 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002627 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002628 }
2629
Steven Rostedt19902072011-06-14 20:46:25 -04002630
2631 if (!defined($ignored_warnings{$sha1})) {
2632 check_buildlog $sha1 or return 0;
2633 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002634
2635 next if ($type eq "build");
2636
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002637 my $failed = 0;
2638
Steven Rostedtddf607e2011-06-14 20:49:13 -04002639 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002640
2641 if (!$failed && $type ne "boot"){
2642 do_run_test or $failed = 1;
2643 }
2644 end_monitor;
2645 return 0 if ($failed);
2646
Steven Rostedt27d934b2011-05-20 09:18:18 -04002647 patchcheck_reboot;
2648
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002649 }
2650 $in_patchcheck = 0;
2651 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002652
2653 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002654}
2655
Steven Rostedtb9066f62011-07-15 21:25:24 -04002656my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04002657my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002658my $iflevel = 0;
2659my @ifdeps;
2660
2661# prevent recursion
2662my %read_kconfigs;
2663
Steven Rostedtac6974c2011-10-04 09:40:17 -04002664sub add_dep {
2665 # $config depends on $dep
2666 my ($config, $dep) = @_;
2667
2668 if (defined($depends{$config})) {
2669 $depends{$config} .= " " . $dep;
2670 } else {
2671 $depends{$config} = $dep;
2672 }
2673
2674 # record the number of configs depending on $dep
2675 if (defined $depcount{$dep}) {
2676 $depcount{$dep}++;
2677 } else {
2678 $depcount{$dep} = 1;
2679 }
2680}
2681
Steven Rostedtb9066f62011-07-15 21:25:24 -04002682# taken from streamline_config.pl
2683sub read_kconfig {
2684 my ($kconfig) = @_;
2685
2686 my $state = "NONE";
2687 my $config;
2688 my @kconfigs;
2689
2690 my $cont = 0;
2691 my $line;
2692
2693
2694 if (! -f $kconfig) {
2695 doprint "file $kconfig does not exist, skipping\n";
2696 return;
2697 }
2698
2699 open(KIN, "$kconfig")
2700 or die "Can't open $kconfig";
2701 while (<KIN>) {
2702 chomp;
2703
2704 # Make sure that lines ending with \ continue
2705 if ($cont) {
2706 $_ = $line . " " . $_;
2707 }
2708
2709 if (s/\\$//) {
2710 $cont = 1;
2711 $line = $_;
2712 next;
2713 }
2714
2715 $cont = 0;
2716
2717 # collect any Kconfig sources
2718 if (/^source\s*"(.*)"/) {
2719 $kconfigs[$#kconfigs+1] = $1;
2720 }
2721
2722 # configs found
2723 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2724 $state = "NEW";
2725 $config = $2;
2726
2727 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04002728 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04002729 }
2730
2731 # collect the depends for the config
2732 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2733
Steven Rostedtac6974c2011-10-04 09:40:17 -04002734 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002735
2736 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04002737 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2738
2739 # selected by depends on config
2740 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002741
2742 # Check for if statements
2743 } elsif (/^if\s+(.*\S)\s*$/) {
2744 my $deps = $1;
2745 # remove beginning and ending non text
2746 $deps =~ s/^[^a-zA-Z0-9_]*//;
2747 $deps =~ s/[^a-zA-Z0-9_]*$//;
2748
2749 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2750
2751 $ifdeps[$iflevel++] = join ':', @deps;
2752
2753 } elsif (/^endif/) {
2754
2755 $iflevel-- if ($iflevel);
2756
2757 # stop on "help"
2758 } elsif (/^\s*help\s*$/) {
2759 $state = "NONE";
2760 }
2761 }
2762 close(KIN);
2763
2764 # read in any configs that were found.
2765 foreach $kconfig (@kconfigs) {
2766 if (!defined($read_kconfigs{$kconfig})) {
2767 $read_kconfigs{$kconfig} = 1;
2768 read_kconfig("$builddir/$kconfig");
2769 }
2770 }
2771}
2772
2773sub read_depends {
2774 # find out which arch this is by the kconfig file
2775 open (IN, $output_config)
2776 or dodie "Failed to read $output_config";
2777 my $arch;
2778 while (<IN>) {
2779 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2780 $arch = $1;
2781 last;
2782 }
2783 }
2784 close IN;
2785
2786 if (!defined($arch)) {
2787 doprint "Could not find arch from config file\n";
2788 doprint "no dependencies used\n";
2789 return;
2790 }
2791
2792 # arch is really the subarch, we need to know
2793 # what directory to look at.
2794 if ($arch eq "i386" || $arch eq "x86_64") {
2795 $arch = "x86";
2796 } elsif ($arch =~ /^tile/) {
2797 $arch = "tile";
2798 }
2799
2800 my $kconfig = "$builddir/arch/$arch/Kconfig";
2801
2802 if (! -f $kconfig && $arch =~ /\d$/) {
2803 my $orig = $arch;
2804 # some subarchs have numbers, truncate them
2805 $arch =~ s/\d*$//;
2806 $kconfig = "$builddir/arch/$arch/Kconfig";
2807 if (! -f $kconfig) {
2808 doprint "No idea what arch dir $orig is for\n";
2809 doprint "no dependencies used\n";
2810 return;
2811 }
2812 }
2813
2814 read_kconfig($kconfig);
2815}
2816
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002817sub read_config_list {
2818 my ($config) = @_;
2819
2820 open (IN, $config)
2821 or dodie "Failed to read $config";
2822
2823 while (<IN>) {
2824 if (/^((CONFIG\S*)=.*)/) {
2825 if (!defined($config_ignore{$2})) {
2826 $config_list{$2} = $1;
2827 }
2828 }
2829 }
2830
2831 close(IN);
2832}
2833
2834sub read_output_config {
2835 my ($config) = @_;
2836
2837 assign_configs \%config_ignore, $config;
2838}
2839
2840sub make_new_config {
2841 my @configs = @_;
2842
2843 open (OUT, ">$output_config")
2844 or dodie "Failed to write $output_config";
2845
2846 foreach my $config (@configs) {
2847 print OUT "$config\n";
2848 }
2849 close OUT;
2850}
2851
Steven Rostedtac6974c2011-10-04 09:40:17 -04002852sub chomp_config {
2853 my ($config) = @_;
2854
2855 $config =~ s/CONFIG_//;
2856
2857 return $config;
2858}
2859
Steven Rostedtb9066f62011-07-15 21:25:24 -04002860sub get_depends {
2861 my ($dep) = @_;
2862
Steven Rostedtac6974c2011-10-04 09:40:17 -04002863 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002864
2865 $dep = $depends{"$kconfig"};
2866
2867 # the dep string we have saves the dependencies as they
2868 # were found, including expressions like ! && ||. We
2869 # want to split this out into just an array of configs.
2870
2871 my $valid = "A-Za-z_0-9";
2872
2873 my @configs;
2874
2875 while ($dep =~ /[$valid]/) {
2876
2877 if ($dep =~ /^[^$valid]*([$valid]+)/) {
2878 my $conf = "CONFIG_" . $1;
2879
2880 $configs[$#configs + 1] = $conf;
2881
2882 $dep =~ s/^[^$valid]*[$valid]+//;
2883 } else {
2884 die "this should never happen";
2885 }
2886 }
2887
2888 return @configs;
2889}
2890
2891my %min_configs;
2892my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04002893my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002894my %processed_configs;
2895my %nochange_config;
2896
2897sub test_this_config {
2898 my ($config) = @_;
2899
2900 my $found;
2901
2902 # if we already processed this config, skip it
2903 if (defined($processed_configs{$config})) {
2904 return undef;
2905 }
2906 $processed_configs{$config} = 1;
2907
2908 # if this config failed during this round, skip it
2909 if (defined($nochange_config{$config})) {
2910 return undef;
2911 }
2912
Steven Rostedtac6974c2011-10-04 09:40:17 -04002913 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002914
2915 # Test dependencies first
2916 if (defined($depends{"$kconfig"})) {
2917 my @parents = get_depends $config;
2918 foreach my $parent (@parents) {
2919 # if the parent is in the min config, check it first
2920 next if (!defined($min_configs{$parent}));
2921 $found = test_this_config($parent);
2922 if (defined($found)) {
2923 return $found;
2924 }
2925 }
2926 }
2927
2928 # Remove this config from the list of configs
2929 # do a make oldnoconfig and then read the resulting
2930 # .config to make sure it is missing the config that
2931 # we had before
2932 my %configs = %min_configs;
2933 delete $configs{$config};
2934 make_new_config ((values %configs), (values %keep_configs));
2935 make_oldconfig;
2936 undef %configs;
2937 assign_configs \%configs, $output_config;
2938
2939 return $config if (!defined($configs{$config}));
2940
2941 doprint "disabling config $config did not change .config\n";
2942
2943 $nochange_config{$config} = 1;
2944
2945 return undef;
2946}
2947
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002948sub make_min_config {
2949 my ($i) = @_;
2950
2951 if (!defined($output_minconfig)) {
2952 fail "OUTPUT_MIN_CONFIG not defined" and return;
2953 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04002954
2955 # If output_minconfig exists, and the start_minconfig
2956 # came from min_config, than ask if we should use
2957 # that instead.
2958 if (-f $output_minconfig && !$start_minconfig_defined) {
2959 print "$output_minconfig exists\n";
2960 if (read_yn " Use it as minconfig?") {
2961 $start_minconfig = $output_minconfig;
2962 }
2963 }
2964
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002965 if (!defined($start_minconfig)) {
2966 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
2967 }
2968
Steven Rostedt35ce5952011-07-15 21:57:25 -04002969 my $temp_config = "$tmpdir/temp_config";
2970
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002971 # First things first. We build an allnoconfig to find
2972 # out what the defaults are that we can't touch.
2973 # Some are selections, but we really can't handle selections.
2974
2975 my $save_minconfig = $minconfig;
2976 undef $minconfig;
2977
2978 run_command "$make allnoconfig" or return 0;
2979
Steven Rostedtb9066f62011-07-15 21:25:24 -04002980 read_depends;
2981
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002982 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002983
Steven Rostedt43d1b652011-07-15 22:01:56 -04002984 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002985 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002986
2987 if (defined($ignore_config)) {
2988 # make sure the file exists
2989 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04002990 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002991 }
2992
Steven Rostedt43d1b652011-07-15 22:01:56 -04002993 %keep_configs = %save_configs;
2994
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002995 doprint "Load initial configs from $start_minconfig\n";
2996
2997 # Look at the current min configs, and save off all the
2998 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002999 assign_configs \%min_configs, $start_minconfig;
3000
3001 my @config_keys = keys %min_configs;
3002
Steven Rostedtac6974c2011-10-04 09:40:17 -04003003 # All configs need a depcount
3004 foreach my $config (@config_keys) {
3005 my $kconfig = chomp_config $config;
3006 if (!defined $depcount{$kconfig}) {
3007 $depcount{$kconfig} = 0;
3008 }
3009 }
3010
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003011 # Remove anything that was set by the make allnoconfig
3012 # we shouldn't need them as they get set for us anyway.
3013 foreach my $config (@config_keys) {
3014 # Remove anything in the ignore_config
3015 if (defined($keep_configs{$config})) {
3016 my $file = $ignore_config;
3017 $file =~ s,.*/(.*?)$,$1,;
3018 doprint "$config set by $file ... ignored\n";
3019 delete $min_configs{$config};
3020 next;
3021 }
3022 # But make sure the settings are the same. If a min config
3023 # sets a selection, we do not want to get rid of it if
3024 # it is not the same as what we have. Just move it into
3025 # the keep configs.
3026 if (defined($config_ignore{$config})) {
3027 if ($config_ignore{$config} ne $min_configs{$config}) {
3028 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3029 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3030 $keep_configs{$config} = $min_configs{$config};
3031 } else {
3032 doprint "$config set by allnoconfig ... ignored\n";
3033 }
3034 delete $min_configs{$config};
3035 }
3036 }
3037
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003038 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003039 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003040
3041 while (!$done) {
3042
3043 my $config;
3044 my $found;
3045
3046 # Now disable each config one by one and do a make oldconfig
3047 # till we find a config that changes our list.
3048
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003049 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003050
3051 # Sort keys by who is most dependent on
3052 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3053 @test_configs ;
3054
3055 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003056 my $reset = 1;
3057 for (my $i = 0; $i < $#test_configs; $i++) {
3058 if (!defined($nochange_config{$test_configs[0]})) {
3059 $reset = 0;
3060 last;
3061 }
3062 # This config didn't change the .config last time.
3063 # Place it at the end
3064 my $config = shift @test_configs;
3065 push @test_configs, $config;
3066 }
3067
3068 # if every test config has failed to modify the .config file
3069 # in the past, then reset and start over.
3070 if ($reset) {
3071 undef %nochange_config;
3072 }
3073
Steven Rostedtb9066f62011-07-15 21:25:24 -04003074 undef %processed_configs;
3075
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003076 foreach my $config (@test_configs) {
3077
Steven Rostedtb9066f62011-07-15 21:25:24 -04003078 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003079
Steven Rostedtb9066f62011-07-15 21:25:24 -04003080 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003081
3082 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003083 }
3084
3085 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003086 # we could have failed due to the nochange_config hash
3087 # reset and try again
3088 if (!$take_two) {
3089 undef %nochange_config;
3090 $take_two = 1;
3091 next;
3092 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003093 doprint "No more configs found that we can disable\n";
3094 $done = 1;
3095 last;
3096 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003097 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003098
3099 $config = $found;
3100
3101 doprint "Test with $config disabled\n";
3102
3103 # set in_bisect to keep build and monitor from dieing
3104 $in_bisect = 1;
3105
3106 my $failed = 0;
3107 build "oldconfig";
3108 start_monitor_and_boot or $failed = 1;
3109 end_monitor;
3110
3111 $in_bisect = 0;
3112
3113 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003114 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003115 # this config is needed, add it to the ignore list.
3116 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003117 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003118 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003119
3120 # update new ignore configs
3121 if (defined($ignore_config)) {
3122 open (OUT, ">$temp_config")
3123 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003124 foreach my $config (keys %save_configs) {
3125 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003126 }
3127 close OUT;
3128 run_command "mv $temp_config $ignore_config" or
3129 dodie "failed to copy update to $ignore_config";
3130 }
3131
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003132 } else {
3133 # We booted without this config, remove it from the minconfigs.
3134 doprint "$config is not needed, disabling\n";
3135
3136 delete $min_configs{$config};
3137
3138 # Also disable anything that is not enabled in this config
3139 my %configs;
3140 assign_configs \%configs, $output_config;
3141 my @config_keys = keys %min_configs;
3142 foreach my $config (@config_keys) {
3143 if (!defined($configs{$config})) {
3144 doprint "$config is not set, disabling\n";
3145 delete $min_configs{$config};
3146 }
3147 }
3148
3149 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003150 open (OUT, ">$temp_config")
3151 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003152 foreach my $config (keys %keep_configs) {
3153 print OUT "$keep_configs{$config}\n";
3154 }
3155 foreach my $config (keys %min_configs) {
3156 print OUT "$min_configs{$config}\n";
3157 }
3158 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003159
3160 run_command "mv $temp_config $output_minconfig" or
3161 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003162 }
3163
3164 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003165 reboot_to_good $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003166 }
3167
3168 success $i;
3169 return 1;
3170}
3171
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003172$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003173
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003174if ($#ARGV == 0) {
3175 $ktest_config = $ARGV[0];
3176 if (! -f $ktest_config) {
3177 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003178 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003179 exit 0;
3180 }
3181 }
3182} else {
3183 $ktest_config = "ktest.conf";
3184}
3185
3186if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003187 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003188 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003189 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3190 print OUT << "EOF"
3191# Generated by ktest.pl
3192#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003193
3194# PWD is a ktest.pl variable that will result in the process working
3195# directory that ktest.pl is executed in.
3196
3197# THIS_DIR is automatically assigned the PWD of the path that generated
3198# the config file. It is best to use this variable when assigning other
3199# directory paths within this directory. This allows you to easily
3200# move the test cases to other locations or to other machines.
3201#
3202THIS_DIR := $variable{"PWD"}
3203
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003204# Define each test with TEST_START
3205# The config options below it will override the defaults
3206TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003207TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003208
3209DEFAULTS
3210EOF
3211;
3212 close(OUT);
3213}
3214read_config $ktest_config;
3215
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003216if (defined($opt{"LOG_FILE"})) {
3217 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3218}
3219
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003220# Append any configs entered in manually to the config file.
3221my @new_configs = keys %entered_configs;
3222if ($#new_configs >= 0) {
3223 print "\nAppending entered in configs to $ktest_config\n";
3224 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3225 foreach my $config (@new_configs) {
3226 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003227 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003228 }
3229}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003230
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003231if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3232 unlink $opt{"LOG_FILE"};
3233}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003234
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003235doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3236
Steven Rostedta57419b2010-11-02 15:13:54 -04003237for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3238
3239 if (!$i) {
3240 doprint "DEFAULT OPTIONS:\n";
3241 } else {
3242 doprint "\nTEST $i OPTIONS";
3243 if (defined($repeat_tests{$i})) {
3244 $repeat = $repeat_tests{$i};
3245 doprint " ITERATE $repeat";
3246 }
3247 doprint "\n";
3248 }
3249
3250 foreach my $option (sort keys %opt) {
3251
3252 if ($option =~ /\[(\d+)\]$/) {
3253 next if ($i != $1);
3254 } else {
3255 next if ($i);
3256 }
3257
3258 doprint "$option = $opt{$option}\n";
3259 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003260}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003261
Steven Rostedt2a625122011-05-20 15:48:59 -04003262sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003263 my ($name, $i) = @_;
3264
3265 my $option = "$name\[$i\]";
3266
3267 if (defined($opt{$option})) {
3268 return $opt{$option};
3269 }
3270
Steven Rostedta57419b2010-11-02 15:13:54 -04003271 foreach my $test (keys %repeat_tests) {
3272 if ($i >= $test &&
3273 $i < $test + $repeat_tests{$test}) {
3274 $option = "$name\[$test\]";
3275 if (defined($opt{$option})) {
3276 return $opt{$option};
3277 }
3278 }
3279 }
3280
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003281 if (defined($opt{$name})) {
3282 return $opt{$name};
3283 }
3284
3285 return undef;
3286}
3287
Steven Rostedt2a625122011-05-20 15:48:59 -04003288sub set_test_option {
3289 my ($name, $i) = @_;
3290
3291 my $option = __set_test_option($name, $i);
3292 return $option if (!defined($option));
3293
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003294 return eval_option($option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003295}
3296
Steven Rostedt2545eb62010-11-02 15:01:32 -04003297# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003298for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003299
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003300 # Do not reboot on failing test options
3301 $no_reboot = 1;
3302
Steven Rostedt576f6272010-11-02 14:58:38 -04003303 $iteration = $i;
3304
Steven Rostedta75fece2010-11-02 14:58:27 -04003305 my $makecmd = set_test_option("MAKE_CMD", $i);
3306
3307 $machine = set_test_option("MACHINE", $i);
Steven Rostedte48c5292010-11-02 14:35:37 -04003308 $ssh_user = set_test_option("SSH_USER", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003309 $tmpdir = set_test_option("TMP_DIR", $i);
3310 $outputdir = set_test_option("OUTPUT_DIR", $i);
3311 $builddir = set_test_option("BUILD_DIR", $i);
3312 $test_type = set_test_option("TEST_TYPE", $i);
3313 $build_type = set_test_option("BUILD_TYPE", $i);
3314 $build_options = set_test_option("BUILD_OPTIONS", $i);
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04003315 $pre_build = set_test_option("PRE_BUILD", $i);
3316 $post_build = set_test_option("POST_BUILD", $i);
3317 $pre_build_die = set_test_option("PRE_BUILD_DIE", $i);
3318 $post_build_die = set_test_option("POST_BUILD_DIE", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003319 $power_cycle = set_test_option("POWER_CYCLE", $i);
Steven Rostedte48c5292010-11-02 14:35:37 -04003320 $reboot = set_test_option("REBOOT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003321 $noclean = set_test_option("BUILD_NOCLEAN", $i);
3322 $minconfig = set_test_option("MIN_CONFIG", $i);
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003323 $output_minconfig = set_test_option("OUTPUT_MIN_CONFIG", $i);
3324 $start_minconfig = set_test_option("START_MIN_CONFIG", $i);
3325 $ignore_config = set_test_option("IGNORE_CONFIG", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003326 $run_test = set_test_option("TEST", $i);
3327 $addconfig = set_test_option("ADD_CONFIG", $i);
3328 $reboot_type = set_test_option("REBOOT_TYPE", $i);
3329 $grub_menu = set_test_option("GRUB_MENU", $i);
Steven Rostedt8b37ca82010-11-02 14:58:33 -04003330 $post_install = set_test_option("POST_INSTALL", $i);
Steven Rostedte0a87422011-09-30 17:50:48 -04003331 $no_install = set_test_option("NO_INSTALL", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003332 $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
3333 $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003334 $switch_to_good = set_test_option("SWITCH_TO_GOOD", $i);
3335 $switch_to_test = set_test_option("SWITCH_TO_TEST", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003336 $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
3337 $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
3338 $power_off = set_test_option("POWER_OFF", $i);
Steven Rostedt576f6272010-11-02 14:58:38 -04003339 $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
3340 $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003341 $sleep_time = set_test_option("SLEEP_TIME", $i);
3342 $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
Steven Rostedt27d934b2011-05-20 09:18:18 -04003343 $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
Steven Rostedt19902072011-06-14 20:46:25 -04003344 $ignore_warnings = set_test_option("IGNORE_WARNINGS", $i);
Steven Rostedtc960bb92011-03-08 09:22:39 -05003345 $bisect_manual = set_test_option("BISECT_MANUAL", $i);
Steven Rostedtc23dca72011-03-08 09:26:31 -05003346 $bisect_skip = set_test_option("BISECT_SKIP", $i);
Steven Rostedt30f75da2011-06-13 10:35:35 -04003347 $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
Steven Rostedtc5dacb82011-12-22 12:43:57 -05003348 $bisect_ret_good = set_test_option("BISECT_RET_GOOD", $i);
3349 $bisect_ret_bad = set_test_option("BISECT_RET_BAD", $i);
3350 $bisect_ret_skip = set_test_option("BISECT_RET_SKIP", $i);
3351 $bisect_ret_abort = set_test_option("BISECT_RET_ABORT", $i);
3352 $bisect_ret_default = set_test_option("BISECT_RET_DEFAULT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003353 $store_failures = set_test_option("STORE_FAILURES", $i);
Rabin Vincentde5b6e32011-11-18 17:05:31 +05303354 $store_successes = set_test_option("STORE_SUCCESSES", $i);
Steven Rostedt9064af52011-06-13 10:38:48 -04003355 $test_name = set_test_option("TEST_NAME", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003356 $timeout = set_test_option("TIMEOUT", $i);
3357 $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
3358 $console = set_test_option("CONSOLE", $i);
Steven Rostedtf1a5b962011-06-13 10:30:00 -04003359 $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003360 $success_line = set_test_option("SUCCESS_LINE", $i);
Steven Rostedt2b803362011-09-30 18:00:23 -04003361 $reboot_success_line = set_test_option("REBOOT_SUCCESS_LINE", $i);
Steven Rostedt1c8a6172010-11-09 12:55:40 -05003362 $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
3363 $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
Steven Rostedt2d01b262011-03-08 09:47:54 -05003364 $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003365 $build_target = set_test_option("BUILD_TARGET", $i);
Steven Rostedte48c5292010-11-02 14:35:37 -04003366 $ssh_exec = set_test_option("SSH_EXEC", $i);
3367 $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
Steven Rostedta75fece2010-11-02 14:58:27 -04003368 $target_image = set_test_option("TARGET_IMAGE", $i);
3369 $localversion = set_test_option("LOCALVERSION", $i);
3370
Steven Rostedt35ce5952011-07-15 21:57:25 -04003371 $start_minconfig_defined = 1;
3372
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003373 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003374 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003375 $start_minconfig = $minconfig;
3376 }
3377
Steven Rostedta75fece2010-11-02 14:58:27 -04003378 chdir $builddir || die "can't change directory to $builddir";
3379
Andrew Jonesa908a662011-08-12 15:32:03 +02003380 foreach my $dir ($tmpdir, $outputdir) {
3381 if (!-d $dir) {
3382 mkpath($dir) or
3383 die "can't create $dir";
3384 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003385 }
3386
Steven Rostedte48c5292010-11-02 14:35:37 -04003387 $ENV{"SSH_USER"} = $ssh_user;
3388 $ENV{"MACHINE"} = $machine;
3389
Steven Rostedta75fece2010-11-02 14:58:27 -04003390 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303391 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003392 $dmesg = "$tmpdir/dmesg-$machine";
3393 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003394 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003395
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003396 if (!$buildonly) {
3397 $target = "$ssh_user\@$machine";
3398 if ($reboot_type eq "grub") {
3399 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3400 } elsif (!defined($reboot_script)) {
3401 dodie "REBOOT_SCRIPT not defined"
3402 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003403 }
3404
3405 my $run_type = $build_type;
3406 if ($test_type eq "patchcheck") {
3407 $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
3408 } elsif ($test_type eq "bisect") {
3409 $run_type = $opt{"BISECT_TYPE[$i]"};
Steven Rostedt0a05c762010-11-08 11:14:10 -05003410 } elsif ($test_type eq "config_bisect") {
3411 $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
Steven Rostedta75fece2010-11-02 14:58:27 -04003412 }
3413
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003414 if ($test_type eq "make_min_config") {
3415 $run_type = "";
3416 }
3417
Steven Rostedta75fece2010-11-02 14:58:27 -04003418 # mistake in config file?
3419 if (!defined($run_type)) {
3420 $run_type = "ERROR";
3421 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003422
Steven Rostedte0a87422011-09-30 17:50:48 -04003423 my $installme = "";
3424 $installme = " no_install" if ($no_install);
3425
Steven Rostedt2545eb62010-11-02 15:01:32 -04003426 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003427 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003428
3429 unlink $dmesg;
3430 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303431 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003432
Steven Rostedt250bae82011-07-15 22:05:59 -04003433 if (defined($addconfig)) {
3434 my $min = $minconfig;
3435 if (!defined($minconfig)) {
3436 $min = "";
3437 }
3438 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003439 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003440 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003441 }
3442
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003443 my $checkout = $opt{"CHECKOUT[$i]"};
3444 if (defined($checkout)) {
3445 run_command "git checkout $checkout" or
3446 die "failed to checkout $checkout";
3447 }
3448
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003449 $no_reboot = 0;
3450
3451
Steven Rostedta75fece2010-11-02 14:58:27 -04003452 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003453 bisect $i;
3454 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003455 } elsif ($test_type eq "config_bisect") {
3456 config_bisect $i;
3457 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003458 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003459 patchcheck $i;
3460 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003461 } elsif ($test_type eq "make_min_config") {
3462 make_min_config $i;
3463 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003464 }
3465
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003466 if ($build_type ne "nobuild") {
3467 build $build_type or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003468 }
3469
Steven Rostedtcd8e3682011-08-18 16:35:44 -04003470 if ($test_type eq "install") {
3471 get_version;
3472 install;
3473 success $i;
3474 next;
3475 }
3476
Steven Rostedta75fece2010-11-02 14:58:27 -04003477 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04003478 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04003479 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04003480
3481 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3482 do_run_test or $failed = 1;
3483 }
3484 end_monitor;
3485 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003486 }
3487
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003488 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003489}
3490
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003491if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003492 halt;
Steven Rostedt576f6272010-11-02 14:58:38 -04003493} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003494 reboot_to_good;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003495}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003496
Steven Rostedte48c5292010-11-02 14:35:37 -04003497doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3498
Steven Rostedt2545eb62010-11-02 15:01:32 -04003499exit 0;