blob: 50b6726c38659d04bb2e04b03ba929080f23bc85 [file] [log] [blame]
Steven Rostedt2545eb62010-11-02 15:01:32 -04001#!/usr/bin/perl -w
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002#
Uwe Kleine-Königcce1dac2011-01-24 21:12:01 +01003# Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04004# Licensed under the terms of the GNU GPL License version 2
5#
Steven Rostedt2545eb62010-11-02 15:01:32 -04006
7use strict;
8use IPC::Open2;
9use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Steven Rostedt7faafbd2010-11-02 14:58:22 -040010use File::Path qw(mkpath);
11use File::Copy qw(cp);
Steven Rostedt2545eb62010-11-02 15:01:32 -040012use FileHandle;
13
Steven Rostedte48c5292010-11-02 14:35:37 -040014my $VERSION = "0.2";
15
Steven Rostedt2545eb62010-11-02 15:01:32 -040016$| = 1;
17
18my %opt;
Steven Rostedta57419b2010-11-02 15:13:54 -040019my %repeat_tests;
20my %repeats;
Steven Rostedt2545eb62010-11-02 15:01:32 -040021
22#default opts
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050023my %default = (
24 "NUM_TESTS" => 1,
25 "TEST_TYPE" => "build",
26 "BUILD_TYPE" => "randconfig",
27 "MAKE_CMD" => "make",
28 "TIMEOUT" => 120,
29 "TMP_DIR" => "/tmp/ktest/\${MACHINE}",
30 "SLEEP_TIME" => 60, # sleep time between tests
31 "BUILD_NOCLEAN" => 0,
32 "REBOOT_ON_ERROR" => 0,
33 "POWEROFF_ON_ERROR" => 0,
34 "REBOOT_ON_SUCCESS" => 1,
35 "POWEROFF_ON_SUCCESS" => 0,
36 "BUILD_OPTIONS" => "",
37 "BISECT_SLEEP_TIME" => 60, # sleep time between bisects
38 "PATCHCHECK_SLEEP_TIME" => 60, # sleep time between patch checks
39 "CLEAR_LOG" => 0,
40 "BISECT_MANUAL" => 0,
41 "BISECT_SKIP" => 1,
42 "SUCCESS_LINE" => "login:",
43 "DETECT_TRIPLE_FAULT" => 1,
44 "NO_INSTALL" => 0,
45 "BOOTED_TIMEOUT" => 1,
46 "DIE_ON_FAILURE" => 1,
47 "SSH_EXEC" => "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND",
48 "SCP_TO_TARGET" => "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE",
Steven Rostedt02ad2612012-03-21 08:21:24 -040049 "SCP_TO_TARGET_INSTALL" => "\${SCP_TO_TARGET}",
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050050 "REBOOT" => "ssh \$SSH_USER\@\$MACHINE reboot",
51 "STOP_AFTER_SUCCESS" => 10,
52 "STOP_AFTER_FAILURE" => 60,
53 "STOP_TEST_AFTER" => 600,
Steven Rostedt600bbf02011-11-21 20:12:04 -050054
55# required, and we will ask users if they don't have them but we keep the default
56# value something that is common.
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050057 "REBOOT_TYPE" => "grub",
58 "LOCALVERSION" => "-test",
59 "SSH_USER" => "root",
60 "BUILD_TARGET" => "arch/x86/boot/bzImage",
61 "TARGET_IMAGE" => "/boot/vmlinuz-test",
Steven Rostedt9cc9e092011-12-22 21:37:22 -050062
63 "LOG_FILE" => undef,
64 "IGNORE_UNUSED" => 0,
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050065);
Steven Rostedt2545eb62010-11-02 15:01:32 -040066
Steven Rostedt8d1491b2010-11-18 15:39:48 -050067my $ktest_config;
Steven Rostedt2545eb62010-11-02 15:01:32 -040068my $version;
Steven Rostedt683a3e62012-05-18 13:34:35 -040069my $have_version = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -040070my $machine;
Steven Rostedte48c5292010-11-02 14:35:37 -040071my $ssh_user;
Steven Rostedta75fece2010-11-02 14:58:27 -040072my $tmpdir;
73my $builddir;
74my $outputdir;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -050075my $output_config;
Steven Rostedta75fece2010-11-02 14:58:27 -040076my $test_type;
Steven Rostedt7faafbd2010-11-02 14:58:22 -040077my $build_type;
Steven Rostedta75fece2010-11-02 14:58:27 -040078my $build_options;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -040079my $pre_build;
80my $post_build;
81my $pre_build_die;
82my $post_build_die;
Steven Rostedta75fece2010-11-02 14:58:27 -040083my $reboot_type;
84my $reboot_script;
85my $power_cycle;
Steven Rostedte48c5292010-11-02 14:35:37 -040086my $reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -040087my $reboot_on_error;
Steven Rostedtbc7c5802011-12-22 16:29:10 -050088my $switch_to_good;
89my $switch_to_test;
Steven Rostedta75fece2010-11-02 14:58:27 -040090my $poweroff_on_error;
Steven Rostedt648a1822012-03-21 11:18:27 -040091my $reboot_on_success;
Steven Rostedta75fece2010-11-02 14:58:27 -040092my $die_on_failure;
Steven Rostedt576f6272010-11-02 14:58:38 -040093my $powercycle_after_reboot;
94my $poweroff_after_halt;
Steven Rostedte48c5292010-11-02 14:35:37 -040095my $ssh_exec;
96my $scp_to_target;
Steven Rostedt02ad2612012-03-21 08:21:24 -040097my $scp_to_target_install;
Steven Rostedta75fece2010-11-02 14:58:27 -040098my $power_off;
99my $grub_menu;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400100my $grub_number;
101my $target;
102my $make;
Steven Rostedt8b37ca82010-11-02 14:58:33 -0400103my $post_install;
Steven Rostedte0a87422011-09-30 17:50:48 -0400104my $no_install;
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400105my $noclean;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400106my $minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400107my $start_minconfig;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400108my $start_minconfig_defined;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400109my $output_minconfig;
110my $ignore_config;
Steven Rostedtbe405f92012-01-04 21:51:59 -0500111my $ignore_errors;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -0400112my $addconfig;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400113my $in_bisect = 0;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500114my $bisect_bad_commit = "";
Steven Rostedtd6ce2a02010-11-02 14:58:05 -0400115my $reverse_bisect;
Steven Rostedtc960bb92011-03-08 09:22:39 -0500116my $bisect_manual;
Steven Rostedtc23dca72011-03-08 09:26:31 -0500117my $bisect_skip;
Steven Rostedt30f75da2011-06-13 10:35:35 -0400118my $config_bisect_good;
Steven Rostedtc5dacb82011-12-22 12:43:57 -0500119my $bisect_ret_good;
120my $bisect_ret_bad;
121my $bisect_ret_skip;
122my $bisect_ret_abort;
123my $bisect_ret_default;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400124my $in_patchcheck = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400125my $run_test;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400126my $redirect;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400127my $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +0530128my $testlog;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400129my $dmesg;
130my $monitor_fp;
131my $monitor_pid;
132my $monitor_cnt = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -0400133my $sleep_time;
134my $bisect_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -0400135my $patchcheck_sleep_time;
Steven Rostedt19902072011-06-14 20:46:25 -0400136my $ignore_warnings;
Steven Rostedta75fece2010-11-02 14:58:27 -0400137my $store_failures;
Rabin Vincentde5b6e32011-11-18 17:05:31 +0530138my $store_successes;
Steven Rostedt9064af52011-06-13 10:38:48 -0400139my $test_name;
Steven Rostedta75fece2010-11-02 14:58:27 -0400140my $timeout;
141my $booted_timeout;
Steven Rostedtf1a5b962011-06-13 10:30:00 -0400142my $detect_triplefault;
Steven Rostedta75fece2010-11-02 14:58:27 -0400143my $console;
Steven Rostedt2b803362011-09-30 18:00:23 -0400144my $reboot_success_line;
Steven Rostedta75fece2010-11-02 14:58:27 -0400145my $success_line;
Steven Rostedt1c8a6172010-11-09 12:55:40 -0500146my $stop_after_success;
147my $stop_after_failure;
Steven Rostedt2d01b262011-03-08 09:47:54 -0500148my $stop_test_after;
Steven Rostedta75fece2010-11-02 14:58:27 -0400149my $build_target;
150my $target_image;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500151my $checkout;
Steven Rostedta75fece2010-11-02 14:58:27 -0400152my $localversion;
Steven Rostedt576f6272010-11-02 14:58:38 -0400153my $iteration = 0;
Steven Rostedte48c5292010-11-02 14:35:37 -0400154my $successes = 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400155
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500156my $bisect_good;
157my $bisect_bad;
158my $bisect_type;
159my $bisect_start;
160my $bisect_replay;
161my $bisect_files;
162my $bisect_reverse;
163my $bisect_check;
164
165my $config_bisect;
166my $config_bisect_type;
167
168my $patchcheck_type;
169my $patchcheck_start;
170my $patchcheck_end;
171
Steven Rostedt165708b2011-11-26 20:56:52 -0500172# set when a test is something other that just building or install
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500173# which would require more options.
174my $buildonly = 1;
175
Steven Rostedtdbd37832011-11-23 16:00:48 -0500176# set when creating a new config
177my $newconfig = 0;
178
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500179my %entered_configs;
180my %config_help;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400181my %variable;
Steven Rostedtfcb3f162011-06-13 10:40:58 -0400182my %force_config;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500183
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400184# do not force reboots on config problems
185my $no_reboot = 1;
186
Steven Rostedt759a3cc2012-05-01 08:20:12 -0400187# reboot on success
188my $reboot_success = 0;
189
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500190my %option_map = (
191 "MACHINE" => \$machine,
192 "SSH_USER" => \$ssh_user,
193 "TMP_DIR" => \$tmpdir,
194 "OUTPUT_DIR" => \$outputdir,
195 "BUILD_DIR" => \$builddir,
196 "TEST_TYPE" => \$test_type,
197 "BUILD_TYPE" => \$build_type,
198 "BUILD_OPTIONS" => \$build_options,
199 "PRE_BUILD" => \$pre_build,
200 "POST_BUILD" => \$post_build,
201 "PRE_BUILD_DIE" => \$pre_build_die,
202 "POST_BUILD_DIE" => \$post_build_die,
203 "POWER_CYCLE" => \$power_cycle,
204 "REBOOT" => \$reboot,
205 "BUILD_NOCLEAN" => \$noclean,
206 "MIN_CONFIG" => \$minconfig,
207 "OUTPUT_MIN_CONFIG" => \$output_minconfig,
208 "START_MIN_CONFIG" => \$start_minconfig,
209 "IGNORE_CONFIG" => \$ignore_config,
210 "TEST" => \$run_test,
211 "ADD_CONFIG" => \$addconfig,
212 "REBOOT_TYPE" => \$reboot_type,
213 "GRUB_MENU" => \$grub_menu,
214 "POST_INSTALL" => \$post_install,
215 "NO_INSTALL" => \$no_install,
216 "REBOOT_SCRIPT" => \$reboot_script,
217 "REBOOT_ON_ERROR" => \$reboot_on_error,
218 "SWITCH_TO_GOOD" => \$switch_to_good,
219 "SWITCH_TO_TEST" => \$switch_to_test,
220 "POWEROFF_ON_ERROR" => \$poweroff_on_error,
Steven Rostedt648a1822012-03-21 11:18:27 -0400221 "REBOOT_ON_SUCCESS" => \$reboot_on_success,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500222 "DIE_ON_FAILURE" => \$die_on_failure,
223 "POWER_OFF" => \$power_off,
224 "POWERCYCLE_AFTER_REBOOT" => \$powercycle_after_reboot,
225 "POWEROFF_AFTER_HALT" => \$poweroff_after_halt,
226 "SLEEP_TIME" => \$sleep_time,
227 "BISECT_SLEEP_TIME" => \$bisect_sleep_time,
228 "PATCHCHECK_SLEEP_TIME" => \$patchcheck_sleep_time,
229 "IGNORE_WARNINGS" => \$ignore_warnings,
Steven Rostedtbe405f92012-01-04 21:51:59 -0500230 "IGNORE_ERRORS" => \$ignore_errors,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500231 "BISECT_MANUAL" => \$bisect_manual,
232 "BISECT_SKIP" => \$bisect_skip,
233 "CONFIG_BISECT_GOOD" => \$config_bisect_good,
234 "BISECT_RET_GOOD" => \$bisect_ret_good,
235 "BISECT_RET_BAD" => \$bisect_ret_bad,
236 "BISECT_RET_SKIP" => \$bisect_ret_skip,
237 "BISECT_RET_ABORT" => \$bisect_ret_abort,
238 "BISECT_RET_DEFAULT" => \$bisect_ret_default,
239 "STORE_FAILURES" => \$store_failures,
240 "STORE_SUCCESSES" => \$store_successes,
241 "TEST_NAME" => \$test_name,
242 "TIMEOUT" => \$timeout,
243 "BOOTED_TIMEOUT" => \$booted_timeout,
244 "CONSOLE" => \$console,
245 "DETECT_TRIPLE_FAULT" => \$detect_triplefault,
246 "SUCCESS_LINE" => \$success_line,
247 "REBOOT_SUCCESS_LINE" => \$reboot_success_line,
248 "STOP_AFTER_SUCCESS" => \$stop_after_success,
249 "STOP_AFTER_FAILURE" => \$stop_after_failure,
250 "STOP_TEST_AFTER" => \$stop_test_after,
251 "BUILD_TARGET" => \$build_target,
252 "SSH_EXEC" => \$ssh_exec,
253 "SCP_TO_TARGET" => \$scp_to_target,
Steven Rostedt02ad2612012-03-21 08:21:24 -0400254 "SCP_TO_TARGET_INSTALL" => \$scp_to_target_install,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500255 "CHECKOUT" => \$checkout,
256 "TARGET_IMAGE" => \$target_image,
257 "LOCALVERSION" => \$localversion,
258
259 "BISECT_GOOD" => \$bisect_good,
260 "BISECT_BAD" => \$bisect_bad,
261 "BISECT_TYPE" => \$bisect_type,
262 "BISECT_START" => \$bisect_start,
263 "BISECT_REPLAY" => \$bisect_replay,
264 "BISECT_FILES" => \$bisect_files,
265 "BISECT_REVERSE" => \$bisect_reverse,
266 "BISECT_CHECK" => \$bisect_check,
267
268 "CONFIG_BISECT" => \$config_bisect,
269 "CONFIG_BISECT_TYPE" => \$config_bisect_type,
270
271 "PATCHCHECK_TYPE" => \$patchcheck_type,
272 "PATCHCHECK_START" => \$patchcheck_start,
273 "PATCHCHECK_END" => \$patchcheck_end,
274);
275
276# Options may be used by other options, record them.
277my %used_options;
278
Steven Rostedt7bf51072011-10-22 09:07:03 -0400279# default variables that can be used
280chomp ($variable{"PWD"} = `pwd`);
281
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500282$config_help{"MACHINE"} = << "EOF"
283 The machine hostname that you will test.
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500284 For build only tests, it is still needed to differentiate log files.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500285EOF
286 ;
287$config_help{"SSH_USER"} = << "EOF"
288 The box is expected to have ssh on normal bootup, provide the user
289 (most likely root, since you need privileged operations)
290EOF
291 ;
292$config_help{"BUILD_DIR"} = << "EOF"
293 The directory that contains the Linux source code (full path).
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500294 You can use \${PWD} that will be the path where ktest.pl is run, or use
295 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500296EOF
297 ;
298$config_help{"OUTPUT_DIR"} = << "EOF"
299 The directory that the objects will be built (full path).
300 (can not be same as BUILD_DIR)
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500301 You can use \${PWD} that will be the path where ktest.pl is run, or use
302 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500303EOF
304 ;
305$config_help{"BUILD_TARGET"} = << "EOF"
306 The location of the compiled file to copy to the target.
307 (relative to OUTPUT_DIR)
308EOF
309 ;
Steven Rostedtdbd37832011-11-23 16:00:48 -0500310$config_help{"BUILD_OPTIONS"} = << "EOF"
311 Options to add to \"make\" when building.
312 i.e. -j20
313EOF
314 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500315$config_help{"TARGET_IMAGE"} = << "EOF"
316 The place to put your image on the test machine.
317EOF
318 ;
319$config_help{"POWER_CYCLE"} = << "EOF"
320 A script or command to reboot the box.
321
322 Here is a digital loggers power switch example
323 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
324
325 Here is an example to reboot a virtual box on the current host
326 with the name "Guest".
327 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
328EOF
329 ;
330$config_help{"CONSOLE"} = << "EOF"
331 The script or command that reads the console
332
333 If you use ttywatch server, something like the following would work.
334CONSOLE = nc -d localhost 3001
335
336 For a virtual machine with guest name "Guest".
337CONSOLE = virsh console Guest
338EOF
339 ;
340$config_help{"LOCALVERSION"} = << "EOF"
341 Required version ending to differentiate the test
342 from other linux builds on the system.
343EOF
344 ;
345$config_help{"REBOOT_TYPE"} = << "EOF"
346 Way to reboot the box to the test kernel.
347 Only valid options so far are "grub" and "script".
348
349 If you specify grub, it will assume grub version 1
350 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
351 and select that target to reboot to the kernel. If this is not
352 your setup, then specify "script" and have a command or script
353 specified in REBOOT_SCRIPT to boot to the target.
354
355 The entry in /boot/grub/menu.lst must be entered in manually.
356 The test will not modify that file.
357EOF
358 ;
359$config_help{"GRUB_MENU"} = << "EOF"
360 The grub title name for the test kernel to boot
361 (Only mandatory if REBOOT_TYPE = grub)
362
363 Note, ktest.pl will not update the grub menu.lst, you need to
364 manually add an option for the test. ktest.pl will search
365 the grub menu.lst for this option to find what kernel to
366 reboot into.
367
368 For example, if in the /boot/grub/menu.lst the test kernel title has:
369 title Test Kernel
370 kernel vmlinuz-test
371 GRUB_MENU = Test Kernel
372EOF
373 ;
374$config_help{"REBOOT_SCRIPT"} = << "EOF"
375 A script to reboot the target into the test kernel
376 (Only mandatory if REBOOT_TYPE = script)
377EOF
378 ;
379
Steven Rostedtdad98752011-11-22 20:48:57 -0500380sub read_prompt {
381 my ($cancel, $prompt) = @_;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400382
383 my $ans;
384
385 for (;;) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500386 if ($cancel) {
387 print "$prompt [y/n/C] ";
388 } else {
389 print "$prompt [Y/n] ";
390 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400391 $ans = <STDIN>;
392 chomp $ans;
393 if ($ans =~ /^\s*$/) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500394 if ($cancel) {
395 $ans = "c";
396 } else {
397 $ans = "y";
398 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400399 }
400 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
Steven Rostedtdad98752011-11-22 20:48:57 -0500401 if ($cancel) {
402 last if ($ans =~ /^c$/i);
403 print "Please answer either 'y', 'n' or 'c'.\n";
404 } else {
405 print "Please answer either 'y' or 'n'.\n";
406 }
407 }
408 if ($ans =~ /^c/i) {
409 exit;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400410 }
411 if ($ans !~ /^y$/i) {
412 return 0;
413 }
414 return 1;
415}
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500416
Steven Rostedtdad98752011-11-22 20:48:57 -0500417sub read_yn {
418 my ($prompt) = @_;
419
420 return read_prompt 0, $prompt;
421}
422
423sub read_ync {
424 my ($prompt) = @_;
425
426 return read_prompt 1, $prompt;
427}
428
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500429sub get_ktest_config {
430 my ($config) = @_;
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400431 my $ans;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500432
433 return if (defined($opt{$config}));
434
435 if (defined($config_help{$config})) {
436 print "\n";
437 print $config_help{$config};
438 }
439
440 for (;;) {
441 print "$config = ";
Steven Rostedtdbd37832011-11-23 16:00:48 -0500442 if (defined($default{$config}) && length($default{$config})) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500443 print "\[$default{$config}\] ";
444 }
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400445 $ans = <STDIN>;
446 $ans =~ s/^\s*(.*\S)\s*$/$1/;
447 if ($ans =~ /^\s*$/) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500448 if ($default{$config}) {
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400449 $ans = $default{$config};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500450 } else {
451 print "Your answer can not be blank\n";
452 next;
453 }
454 }
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500455 $entered_configs{$config} = ${ans};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500456 last;
457 }
458}
459
460sub get_ktest_configs {
461 get_ktest_config("MACHINE");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500462 get_ktest_config("BUILD_DIR");
463 get_ktest_config("OUTPUT_DIR");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500464
Steven Rostedtdbd37832011-11-23 16:00:48 -0500465 if ($newconfig) {
466 get_ktest_config("BUILD_OPTIONS");
467 }
468
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500469 # options required for other than just building a kernel
470 if (!$buildonly) {
Steven Rostedt165708b2011-11-26 20:56:52 -0500471 get_ktest_config("POWER_CYCLE");
472 get_ktest_config("CONSOLE");
473 }
474
475 # options required for install and more
476 if ($buildonly != 1) {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500477 get_ktest_config("SSH_USER");
478 get_ktest_config("BUILD_TARGET");
479 get_ktest_config("TARGET_IMAGE");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500480 }
481
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500482 get_ktest_config("LOCALVERSION");
483
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500484 return if ($buildonly);
485
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500486 my $rtype = $opt{"REBOOT_TYPE"};
487
488 if (!defined($rtype)) {
489 if (!defined($opt{"GRUB_MENU"})) {
490 get_ktest_config("REBOOT_TYPE");
491 $rtype = $entered_configs{"REBOOT_TYPE"};
492 } else {
493 $rtype = "grub";
494 }
495 }
496
497 if ($rtype eq "grub") {
498 get_ktest_config("GRUB_MENU");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500499 }
500}
501
Steven Rostedt77d942c2011-05-20 13:36:58 -0400502sub process_variables {
Steven Rostedt8d735212011-10-17 11:36:44 -0400503 my ($value, $remove_undef) = @_;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400504 my $retval = "";
505
506 # We want to check for '\', and it is just easier
507 # to check the previous characet of '$' and not need
508 # to worry if '$' is the first character. By adding
509 # a space to $value, we can just check [^\\]\$ and
510 # it will still work.
511 $value = " $value";
512
513 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
514 my $begin = $1;
515 my $var = $2;
516 my $end = $3;
517 # append beginning of value to retval
518 $retval = "$retval$begin";
519 if (defined($variable{$var})) {
520 $retval = "$retval$variable{$var}";
Steven Rostedt8d735212011-10-17 11:36:44 -0400521 } elsif (defined($remove_undef) && $remove_undef) {
522 # for if statements, any variable that is not defined,
523 # we simple convert to 0
524 $retval = "${retval}0";
Steven Rostedt77d942c2011-05-20 13:36:58 -0400525 } else {
526 # put back the origin piece.
527 $retval = "$retval\$\{$var\}";
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500528 # This could be an option that is used later, save
529 # it so we don't warn if this option is not one of
530 # ktests options.
531 $used_options{$var} = 1;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400532 }
533 $value = $end;
534 }
535 $retval = "$retval$value";
536
537 # remove the space added in the beginning
538 $retval =~ s/ //;
539
540 return "$retval"
541}
542
Steven Rostedta57419b2010-11-02 15:13:54 -0400543sub set_value {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400544 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
Steven Rostedta57419b2010-11-02 15:13:54 -0400545
Steven Rostedtcad96662011-12-22 11:32:52 -0500546 my $prvalue = process_variables($rvalue);
547
548 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500549 # Note if a test is something other than build, then we
550 # will need other manditory options.
Steven Rostedtcad96662011-12-22 11:32:52 -0500551 if ($prvalue ne "install") {
Steven Rostedt165708b2011-11-26 20:56:52 -0500552 $buildonly = 0;
553 } else {
554 # install still limits some manditory options.
555 $buildonly = 2;
556 }
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500557 }
558
Steven Rostedta57419b2010-11-02 15:13:54 -0400559 if (defined($opt{$lvalue})) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400560 if (!$override || defined(${$overrides}{$lvalue})) {
561 my $extra = "";
562 if ($override) {
563 $extra = "In the same override section!\n";
564 }
565 die "$name: $.: Option $lvalue defined more than once!\n$extra";
566 }
Steven Rostedtcad96662011-12-22 11:32:52 -0500567 ${$overrides}{$lvalue} = $prvalue;
Steven Rostedta57419b2010-11-02 15:13:54 -0400568 }
Steven Rostedt21a96792010-11-08 16:45:50 -0500569 if ($rvalue =~ /^\s*$/) {
570 delete $opt{$lvalue};
571 } else {
Steven Rostedtcad96662011-12-22 11:32:52 -0500572 $opt{$lvalue} = $prvalue;
Steven Rostedt21a96792010-11-08 16:45:50 -0500573 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400574}
575
Steven Rostedt77d942c2011-05-20 13:36:58 -0400576sub set_variable {
577 my ($lvalue, $rvalue) = @_;
578
579 if ($rvalue =~ /^\s*$/) {
580 delete $variable{$lvalue};
581 } else {
582 $rvalue = process_variables($rvalue);
583 $variable{$lvalue} = $rvalue;
584 }
585}
586
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400587sub process_compare {
588 my ($lval, $cmp, $rval) = @_;
589
590 # remove whitespace
591
592 $lval =~ s/^\s*//;
593 $lval =~ s/\s*$//;
594
595 $rval =~ s/^\s*//;
596 $rval =~ s/\s*$//;
597
598 if ($cmp eq "==") {
599 return $lval eq $rval;
600 } elsif ($cmp eq "!=") {
601 return $lval ne $rval;
602 }
603
604 my $statement = "$lval $cmp $rval";
605 my $ret = eval $statement;
606
607 # $@ stores error of eval
608 if ($@) {
609 return -1;
610 }
611
612 return $ret;
613}
614
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400615sub value_defined {
616 my ($val) = @_;
617
618 return defined($variable{$2}) ||
619 defined($opt{$2});
620}
621
Steven Rostedt8d735212011-10-17 11:36:44 -0400622my $d = 0;
623sub process_expression {
624 my ($name, $val) = @_;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400625
Steven Rostedt8d735212011-10-17 11:36:44 -0400626 my $c = $d++;
627
628 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
629 my $express = $1;
630
631 if (process_expression($name, $express)) {
632 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
633 } else {
634 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
635 }
636 }
637
638 $d--;
639 my $OR = "\\|\\|";
640 my $AND = "\\&\\&";
641
642 while ($val =~ s/^(.*?)($OR|$AND)//) {
643 my $express = $1;
644 my $op = $2;
645
646 if (process_expression($name, $express)) {
647 if ($op eq "||") {
648 return 1;
649 }
650 } else {
651 if ($op eq "&&") {
652 return 0;
653 }
654 }
655 }
Steven Rostedt45d73a52011-09-30 19:44:53 -0400656
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400657 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
658 my $ret = process_compare($1, $2, $3);
659 if ($ret < 0) {
660 die "$name: $.: Unable to process comparison\n";
661 }
662 return $ret;
663 }
664
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400665 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
666 if (defined $1) {
667 return !value_defined($2);
668 } else {
669 return value_defined($2);
670 }
671 }
672
Steven Rostedt45d73a52011-09-30 19:44:53 -0400673 if ($val =~ /^\s*0\s*$/) {
674 return 0;
675 } elsif ($val =~ /^\s*\d+\s*$/) {
676 return 1;
677 }
678
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400679 die ("$name: $.: Undefined content $val in if statement\n");
Steven Rostedt8d735212011-10-17 11:36:44 -0400680}
681
682sub process_if {
683 my ($name, $value) = @_;
684
685 # Convert variables and replace undefined ones with 0
686 my $val = process_variables($value, 1);
687 my $ret = process_expression $name, $val;
688
689 return $ret;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400690}
691
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400692sub __read_config {
693 my ($config, $current_test_num) = @_;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400694
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400695 my $in;
696 open($in, $config) || die "can't read file $config";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400697
Steven Rostedta57419b2010-11-02 15:13:54 -0400698 my $name = $config;
699 $name =~ s,.*/(.*),$1,;
700
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400701 my $test_num = $$current_test_num;
Steven Rostedta57419b2010-11-02 15:13:54 -0400702 my $default = 1;
703 my $repeat = 1;
704 my $num_tests_set = 0;
705 my $skip = 0;
706 my $rest;
Steven Rostedta9f84422011-10-17 11:06:29 -0400707 my $line;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400708 my $test_case = 0;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400709 my $if = 0;
710 my $if_set = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400711 my $override = 0;
712
713 my %overrides;
Steven Rostedta57419b2010-11-02 15:13:54 -0400714
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400715 while (<$in>) {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400716
717 # ignore blank lines and comments
718 next if (/^\s*$/ || /\s*\#/);
719
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400720 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400721
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400722 my $type = $1;
723 $rest = $2;
Steven Rostedta9f84422011-10-17 11:06:29 -0400724 $line = $2;
Steven Rostedta57419b2010-11-02 15:13:54 -0400725
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400726 my $old_test_num;
727 my $old_repeat;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400728 $override = 0;
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400729
730 if ($type eq "TEST_START") {
731
732 if ($num_tests_set) {
733 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
734 }
735
736 $old_test_num = $test_num;
737 $old_repeat = $repeat;
738
739 $test_num += $repeat;
740 $default = 0;
741 $repeat = 1;
742 } else {
743 $default = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400744 }
745
Steven Rostedta9f84422011-10-17 11:06:29 -0400746 # If SKIP is anywhere in the line, the command will be skipped
747 if ($rest =~ s/\s+SKIP\b//) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400748 $skip = 1;
749 } else {
Steven Rostedt0df213c2011-06-14 20:51:37 -0400750 $test_case = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400751 $skip = 0;
752 }
753
Steven Rostedta9f84422011-10-17 11:06:29 -0400754 if ($rest =~ s/\sELSE\b//) {
755 if (!$if) {
756 die "$name: $.: ELSE found with out matching IF section\n$_";
757 }
758 $if = 0;
759
760 if ($if_set) {
761 $skip = 1;
762 } else {
763 $skip = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400764 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400765 }
766
Steven Rostedta9f84422011-10-17 11:06:29 -0400767 if ($rest =~ s/\sIF\s+(.*)//) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400768 if (process_if($name, $1)) {
769 $if_set = 1;
770 } else {
771 $skip = 1;
772 }
773 $if = 1;
774 } else {
775 $if = 0;
Steven Rostedta9f84422011-10-17 11:06:29 -0400776 $if_set = 0;
Steven Rostedta57419b2010-11-02 15:13:54 -0400777 }
778
Steven Rostedta9f84422011-10-17 11:06:29 -0400779 if (!$skip) {
780 if ($type eq "TEST_START") {
781 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
782 $repeat = $1;
783 $repeat_tests{"$test_num"} = $repeat;
784 }
785 } elsif ($rest =~ s/\sOVERRIDE\b//) {
786 # DEFAULT only
787 $override = 1;
788 # Clear previous overrides
789 %overrides = ();
790 }
791 }
792
793 if (!$skip && $rest !~ /^\s*$/) {
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400794 die "$name: $.: Gargbage found after $type\n$_";
Steven Rostedta57419b2010-11-02 15:13:54 -0400795 }
796
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400797 if ($skip && $type eq "TEST_START") {
Steven Rostedta57419b2010-11-02 15:13:54 -0400798 $test_num = $old_test_num;
Steven Rostedte48c5292010-11-02 14:35:37 -0400799 $repeat = $old_repeat;
Steven Rostedta57419b2010-11-02 15:13:54 -0400800 }
801
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400802 } elsif (/^\s*ELSE\b(.*)$/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400803 if (!$if) {
804 die "$name: $.: ELSE found with out matching IF section\n$_";
805 }
806 $rest = $1;
807 if ($if_set) {
808 $skip = 1;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400809 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400810 } else {
811 $skip = 0;
812
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400813 if ($rest =~ /\sIF\s+(.*)/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400814 # May be a ELSE IF section.
815 if (!process_if($name, $1)) {
816 $skip = 1;
817 }
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400818 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400819 } else {
820 $if = 0;
821 }
822 }
823
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400824 if ($rest !~ /^\s*$/) {
825 die "$name: $.: Gargbage found after DEFAULTS\n$_";
826 }
827
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400828 } elsif (/^\s*INCLUDE\s+(\S+)/) {
829
830 next if ($skip);
831
832 if (!$default) {
833 die "$name: $.: INCLUDE can only be done in default sections\n$_";
834 }
835
836 my $file = process_variables($1);
837
838 if ($file !~ m,^/,) {
839 # check the path of the config file first
840 if ($config =~ m,(.*)/,) {
841 if (-f "$1/$file") {
842 $file = "$1/$file";
843 }
844 }
845 }
846
847 if ( ! -r $file ) {
848 die "$name: $.: Can't read file $file\n$_";
849 }
850
851 if (__read_config($file, \$test_num)) {
852 $test_case = 1;
853 }
854
Steven Rostedta57419b2010-11-02 15:13:54 -0400855 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
856
857 next if ($skip);
858
Steven Rostedt2545eb62010-11-02 15:01:32 -0400859 my $lvalue = $1;
860 my $rvalue = $2;
861
Steven Rostedta57419b2010-11-02 15:13:54 -0400862 if (!$default &&
863 ($lvalue eq "NUM_TESTS" ||
864 $lvalue eq "LOG_FILE" ||
865 $lvalue eq "CLEAR_LOG")) {
866 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400867 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400868
869 if ($lvalue eq "NUM_TESTS") {
870 if ($test_num) {
871 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
872 }
873 if (!$default) {
874 die "$name: $.: NUM_TESTS must be set in default section\n";
875 }
876 $num_tests_set = 1;
877 }
878
879 if ($default || $lvalue =~ /\[\d+\]$/) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400880 set_value($lvalue, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400881 } else {
882 my $val = "$lvalue\[$test_num\]";
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400883 set_value($val, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400884
885 if ($repeat > 1) {
886 $repeats{$val} = $repeat;
887 }
888 }
Steven Rostedt77d942c2011-05-20 13:36:58 -0400889 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
890 next if ($skip);
891
892 my $lvalue = $1;
893 my $rvalue = $2;
894
895 # process config variables.
896 # Config variables are only active while reading the
897 # config and can be defined anywhere. They also ignore
898 # TEST_START and DEFAULTS, but are skipped if they are in
899 # on of these sections that have SKIP defined.
900 # The save variable can be
901 # defined multiple times and the new one simply overrides
902 # the prevous one.
903 set_variable($lvalue, $rvalue);
904
Steven Rostedta57419b2010-11-02 15:13:54 -0400905 } else {
906 die "$name: $.: Garbage found in config\n$_";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400907 }
908 }
909
Steven Rostedta57419b2010-11-02 15:13:54 -0400910 if ($test_num) {
911 $test_num += $repeat - 1;
912 $opt{"NUM_TESTS"} = $test_num;
913 }
914
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400915 close($in);
916
917 $$current_test_num = $test_num;
918
919 return $test_case;
920}
921
Steven Rostedtc4261d02011-11-23 13:41:18 -0500922sub get_test_case {
923 print "What test case would you like to run?\n";
924 print " (build, install or boot)\n";
925 print " Other tests are available but require editing the config file\n";
926 my $ans = <STDIN>;
927 chomp $ans;
928 $default{"TEST_TYPE"} = $ans;
929}
930
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400931sub read_config {
932 my ($config) = @_;
933
934 my $test_case;
935 my $test_num = 0;
936
937 $test_case = __read_config $config, \$test_num;
938
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500939 # make sure we have all mandatory configs
940 get_ktest_configs;
941
Steven Rostedt0df213c2011-06-14 20:51:37 -0400942 # was a test specified?
943 if (!$test_case) {
944 print "No test case specified.\n";
Steven Rostedtc4261d02011-11-23 13:41:18 -0500945 get_test_case;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400946 }
947
Steven Rostedta75fece2010-11-02 14:58:27 -0400948 # set any defaults
949
950 foreach my $default (keys %default) {
951 if (!defined($opt{$default})) {
952 $opt{$default} = $default{$default};
953 }
954 }
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500955
956 if ($opt{"IGNORE_UNUSED"} == 1) {
957 return;
958 }
959
960 my %not_used;
961
962 # check if there are any stragglers (typos?)
963 foreach my $option (keys %opt) {
964 my $op = $option;
965 # remove per test labels.
966 $op =~ s/\[.*\]//;
967 if (!exists($option_map{$op}) &&
968 !exists($default{$op}) &&
969 !exists($used_options{$op})) {
970 $not_used{$op} = 1;
971 }
972 }
973
974 if (%not_used) {
975 my $s = "s are";
976 $s = " is" if (keys %not_used == 1);
977 print "The following option$s not used; could be a typo:\n";
978 foreach my $option (keys %not_used) {
979 print "$option\n";
980 }
981 print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
982 if (!read_yn "Do you want to continue?") {
983 exit -1;
984 }
985 }
Steven Rostedt2545eb62010-11-02 15:01:32 -0400986}
987
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400988sub __eval_option {
989 my ($option, $i) = @_;
990
991 # Add space to evaluate the character before $
992 $option = " $option";
993 my $retval = "";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530994 my $repeated = 0;
995 my $parent = 0;
996
997 foreach my $test (keys %repeat_tests) {
998 if ($i >= $test &&
999 $i < $test + $repeat_tests{$test}) {
1000
1001 $repeated = 1;
1002 $parent = $test;
1003 last;
1004 }
1005 }
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001006
1007 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
1008 my $start = $1;
1009 my $var = $2;
1010 my $end = $3;
1011
1012 # Append beginning of line
1013 $retval = "$retval$start";
1014
1015 # If the iteration option OPT[$i] exists, then use that.
1016 # otherwise see if the default OPT (without [$i]) exists.
1017
1018 my $o = "$var\[$i\]";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301019 my $parento = "$var\[$parent\]";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001020
1021 if (defined($opt{$o})) {
1022 $o = $opt{$o};
1023 $retval = "$retval$o";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301024 } elsif ($repeated && defined($opt{$parento})) {
1025 $o = $opt{$parento};
1026 $retval = "$retval$o";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001027 } elsif (defined($opt{$var})) {
1028 $o = $opt{$var};
1029 $retval = "$retval$o";
1030 } else {
1031 $retval = "$retval\$\{$var\}";
1032 }
1033
1034 $option = $end;
1035 }
1036
1037 $retval = "$retval$option";
1038
1039 $retval =~ s/^ //;
1040
1041 return $retval;
1042}
1043
1044sub eval_option {
1045 my ($option, $i) = @_;
1046
1047 my $prev = "";
1048
1049 # Since an option can evaluate to another option,
1050 # keep iterating until we do not evaluate any more
1051 # options.
1052 my $r = 0;
1053 while ($prev ne $option) {
1054 # Check for recursive evaluations.
1055 # 100 deep should be more than enough.
1056 if ($r++ > 100) {
1057 die "Over 100 evaluations accurred with $option\n" .
1058 "Check for recursive variables\n";
1059 }
1060 $prev = $option;
1061 $option = __eval_option($option, $i);
1062 }
1063
1064 return $option;
1065}
1066
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001067sub _logit {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001068 if (defined($opt{"LOG_FILE"})) {
1069 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
1070 print OUT @_;
1071 close(OUT);
1072 }
1073}
1074
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001075sub logit {
1076 if (defined($opt{"LOG_FILE"})) {
1077 _logit @_;
1078 } else {
1079 print @_;
1080 }
1081}
1082
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001083sub doprint {
1084 print @_;
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001085 _logit @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001086}
1087
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001088sub run_command;
Andrew Jones2728be42011-08-12 15:32:05 +02001089sub start_monitor;
1090sub end_monitor;
1091sub wait_for_monitor;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001092
1093sub reboot {
Andrew Jones2728be42011-08-12 15:32:05 +02001094 my ($time) = @_;
1095
Steven Rostedt2b803362011-09-30 18:00:23 -04001096 if (defined($time)) {
1097 start_monitor;
1098 # flush out current monitor
1099 # May contain the reboot success line
1100 wait_for_monitor 1;
1101 }
1102
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001103 # try to reboot normally
Steven Rostedte48c5292010-11-02 14:35:37 -04001104 if (run_command $reboot) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001105 if (defined($powercycle_after_reboot)) {
1106 sleep $powercycle_after_reboot;
1107 run_command "$power_cycle";
1108 }
1109 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001110 # nope? power cycle it.
Steven Rostedta75fece2010-11-02 14:58:27 -04001111 run_command "$power_cycle";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001112 }
Andrew Jones2728be42011-08-12 15:32:05 +02001113
1114 if (defined($time)) {
Steven Rostedt2b803362011-09-30 18:00:23 -04001115 wait_for_monitor($time, $reboot_success_line);
Andrew Jones2728be42011-08-12 15:32:05 +02001116 end_monitor;
1117 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001118}
1119
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001120sub reboot_to_good {
1121 my ($time) = @_;
1122
1123 if (defined($switch_to_good)) {
1124 run_command $switch_to_good;
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001125 }
1126
1127 reboot $time;
1128}
1129
Steven Rostedt576f6272010-11-02 14:58:38 -04001130sub do_not_reboot {
1131 my $i = $iteration;
1132
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001133 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -04001134 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1135 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1136}
1137
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001138sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001139 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001140
Steven Rostedt576f6272010-11-02 14:58:38 -04001141 my $i = $iteration;
1142
1143 if ($reboot_on_error && !do_not_reboot) {
1144
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001145 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001146 reboot_to_good;
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001147
Steven Rostedta75fece2010-11-02 14:58:27 -04001148 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001149 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001150 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001151 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001152
Steven Rostedtf80802c2011-03-07 13:18:47 -05001153 if (defined($opt{"LOG_FILE"})) {
1154 print " See $opt{LOG_FILE} for more info.\n";
1155 }
1156
Steven Rostedt576f6272010-11-02 14:58:38 -04001157 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001158}
1159
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001160sub open_console {
1161 my ($fp) = @_;
1162
1163 my $flags;
1164
Steven Rostedta75fece2010-11-02 14:58:27 -04001165 my $pid = open($fp, "$console|") or
1166 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001167
1168 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001169 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001170 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001171 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001172
1173 return $pid;
1174}
1175
1176sub close_console {
1177 my ($fp, $pid) = @_;
1178
1179 doprint "kill child process $pid\n";
1180 kill 2, $pid;
1181
1182 print "closing!\n";
1183 close($fp);
1184}
1185
1186sub start_monitor {
1187 if ($monitor_cnt++) {
1188 return;
1189 }
1190 $monitor_fp = \*MONFD;
1191 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001192
1193 return;
1194
1195 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001196}
1197
1198sub end_monitor {
1199 if (--$monitor_cnt) {
1200 return;
1201 }
1202 close_console($monitor_fp, $monitor_pid);
1203}
1204
1205sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001206 my ($time, $stop) = @_;
1207 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001208 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001209 my $booted = 0;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001210
Steven Rostedta75fece2010-11-02 14:58:27 -04001211 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001212
1213 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001214 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001215 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001216 last if (!defined($line));
1217 print "$line";
1218 $full_line .= $line;
1219
1220 if (defined($stop) && $full_line =~ /$stop/) {
1221 doprint "wait for monitor detected $stop\n";
1222 $booted = 1;
1223 }
1224
1225 if ($line =~ /\n/) {
1226 $full_line = "";
1227 }
1228 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001229 print "** Monitor flushed **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001230}
1231
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301232sub save_logs {
1233 my ($result, $basedir) = @_;
1234 my @t = localtime;
1235 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1236 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1237
1238 my $type = $build_type;
1239 if ($type =~ /useconfig/) {
1240 $type = "useconfig";
1241 }
1242
1243 my $dir = "$machine-$test_type-$type-$result-$date";
1244
1245 $dir = "$basedir/$dir";
1246
1247 if (!-d $dir) {
1248 mkpath($dir) or
1249 die "can't create $dir";
1250 }
1251
1252 my %files = (
1253 "config" => $output_config,
1254 "buildlog" => $buildlog,
1255 "dmesg" => $dmesg,
1256 "testlog" => $testlog,
1257 );
1258
1259 while (my ($name, $source) = each(%files)) {
1260 if (-f "$source") {
1261 cp "$source", "$dir/$name" or
1262 die "failed to copy $source";
1263 }
1264 }
1265
1266 doprint "*** Saved info to $dir ***\n";
1267}
1268
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001269sub fail {
1270
Steven Rostedta75fece2010-11-02 14:58:27 -04001271 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001272 dodie @_;
1273 }
1274
Steven Rostedta75fece2010-11-02 14:58:27 -04001275 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001276
Steven Rostedt576f6272010-11-02 14:58:38 -04001277 my $i = $iteration;
1278
Steven Rostedta75fece2010-11-02 14:58:27 -04001279 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001280 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001281 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001282 reboot_to_good $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001283 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001284
Steven Rostedt9064af52011-06-13 10:38:48 -04001285 my $name = "";
1286
1287 if (defined($test_name)) {
1288 $name = " ($test_name)";
1289 }
1290
Steven Rostedt576f6272010-11-02 14:58:38 -04001291 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1292 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001293 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001294 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1295 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001296
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301297 if (defined($store_failures)) {
1298 save_logs "fail", $store_failures;
1299 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001300
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001301 return 1;
1302}
1303
Steven Rostedt2545eb62010-11-02 15:01:32 -04001304sub run_command {
1305 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001306 my $dolog = 0;
1307 my $dord = 0;
1308 my $pid;
1309
Steven Rostedte48c5292010-11-02 14:35:37 -04001310 $command =~ s/\$SSH_USER/$ssh_user/g;
1311 $command =~ s/\$MACHINE/$machine/g;
1312
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001313 doprint("$command ... ");
1314
1315 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001316 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001317
1318 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001319 open(LOG, ">>$opt{LOG_FILE}") or
1320 dodie "failed to write to log";
1321 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001322 }
1323
1324 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001325 open (RD, ">$redirect") or
1326 dodie "failed to write to redirect $redirect";
1327 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001328 }
1329
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001330 while (<CMD>) {
1331 print LOG if ($dolog);
1332 print RD if ($dord);
1333 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001334
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001335 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001336 my $failed = $?;
1337
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001338 close(CMD);
1339 close(LOG) if ($dolog);
1340 close(RD) if ($dord);
1341
Steven Rostedt2545eb62010-11-02 15:01:32 -04001342 if ($failed) {
1343 doprint "FAILED!\n";
1344 } else {
1345 doprint "SUCCESS\n";
1346 }
1347
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001348 return !$failed;
1349}
1350
Steven Rostedte48c5292010-11-02 14:35:37 -04001351sub run_ssh {
1352 my ($cmd) = @_;
1353 my $cp_exec = $ssh_exec;
1354
1355 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1356 return run_command "$cp_exec";
1357}
1358
1359sub run_scp {
Steven Rostedt02ad2612012-03-21 08:21:24 -04001360 my ($src, $dst, $cp_scp) = @_;
Steven Rostedte48c5292010-11-02 14:35:37 -04001361
1362 $cp_scp =~ s/\$SRC_FILE/$src/g;
1363 $cp_scp =~ s/\$DST_FILE/$dst/g;
1364
1365 return run_command "$cp_scp";
1366}
1367
Steven Rostedt02ad2612012-03-21 08:21:24 -04001368sub run_scp_install {
1369 my ($src, $dst) = @_;
1370
1371 my $cp_scp = $scp_to_target_install;
1372
1373 return run_scp($src, $dst, $cp_scp);
1374}
1375
1376sub run_scp_mod {
1377 my ($src, $dst) = @_;
1378
1379 my $cp_scp = $scp_to_target;
1380
1381 return run_scp($src, $dst, $cp_scp);
1382}
1383
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001384sub get_grub_index {
1385
Steven Rostedta75fece2010-11-02 14:58:27 -04001386 if ($reboot_type ne "grub") {
1387 return;
1388 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001389 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001390
1391 doprint "Find grub menu ... ";
1392 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001393
1394 my $ssh_grub = $ssh_exec;
1395 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1396
1397 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001398 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001399
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001400 my $found = 0;
1401
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001402 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001403 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001404 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001405 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001406 last;
1407 } elsif (/^\s*title\s/) {
1408 $grub_number++;
1409 }
1410 }
1411 close(IN);
1412
Steven Rostedta75fece2010-11-02 14:58:27 -04001413 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001414 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001415 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001416}
1417
Steven Rostedt2545eb62010-11-02 15:01:32 -04001418sub wait_for_input
1419{
1420 my ($fp, $time) = @_;
1421 my $rin;
1422 my $ready;
1423 my $line;
1424 my $ch;
1425
1426 if (!defined($time)) {
1427 $time = $timeout;
1428 }
1429
1430 $rin = '';
1431 vec($rin, fileno($fp), 1) = 1;
1432 $ready = select($rin, undef, undef, $time);
1433
1434 $line = "";
1435
1436 # try to read one char at a time
1437 while (sysread $fp, $ch, 1) {
1438 $line .= $ch;
1439 last if ($ch eq "\n");
1440 }
1441
1442 if (!length($line)) {
1443 return undef;
1444 }
1445
1446 return $line;
1447}
1448
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001449sub reboot_to {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001450 if (defined($switch_to_test)) {
1451 run_command $switch_to_test;
1452 }
1453
Steven Rostedta75fece2010-11-02 14:58:27 -04001454 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001455 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001456 } elsif (defined $reboot_script) {
1457 run_command "$reboot_script";
Steven Rostedta75fece2010-11-02 14:58:27 -04001458 }
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001459 reboot;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001460}
1461
Steven Rostedta57419b2010-11-02 15:13:54 -04001462sub get_sha1 {
1463 my ($commit) = @_;
1464
1465 doprint "git rev-list --max-count=1 $commit ... ";
1466 my $sha1 = `git rev-list --max-count=1 $commit`;
1467 my $ret = $?;
1468
1469 logit $sha1;
1470
1471 if ($ret) {
1472 doprint "FAILED\n";
1473 dodie "Failed to get git $commit";
1474 }
1475
1476 print "SUCCESS\n";
1477
1478 chomp $sha1;
1479
1480 return $sha1;
1481}
1482
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001483sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001484 my $booted = 0;
1485 my $bug = 0;
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001486 my $bug_ignored = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001487 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001488 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001489
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001490 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001491
1492 my $line;
1493 my $full_line = "";
1494
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001495 open(DMESG, "> $dmesg") or
1496 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001497
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001498 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001499
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001500 my $success_start;
1501 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001502 my $monitor_start = time;
1503 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001504 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001505
Steven Rostedt2d01b262011-03-08 09:47:54 -05001506 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001507
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001508 if ($bug && defined($stop_after_failure) &&
1509 $stop_after_failure >= 0) {
1510 my $time = $stop_after_failure - (time - $failure_start);
1511 $line = wait_for_input($monitor_fp, $time);
1512 if (!defined($line)) {
1513 doprint "bug timed out after $booted_timeout seconds\n";
1514 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1515 last;
1516 }
1517 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001518 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001519 if (!defined($line)) {
1520 my $s = $booted_timeout == 1 ? "" : "s";
1521 doprint "Successful boot found: break after $booted_timeout second$s\n";
1522 last;
1523 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001524 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001525 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001526 if (!defined($line)) {
1527 my $s = $timeout == 1 ? "" : "s";
1528 doprint "Timed out after $timeout second$s\n";
1529 last;
1530 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001531 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001532
Steven Rostedt2545eb62010-11-02 15:01:32 -04001533 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001534 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001535
1536 # we are not guaranteed to get a full line
1537 $full_line .= $line;
1538
Steven Rostedta75fece2010-11-02 14:58:27 -04001539 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001540 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001541 $success_start = time;
1542 }
1543
1544 if ($booted && defined($stop_after_success) &&
1545 $stop_after_success >= 0) {
1546 my $now = time;
1547 if ($now - $success_start >= $stop_after_success) {
1548 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1549 last;
1550 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001551 }
1552
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001553 if ($full_line =~ /\[ backtrace testing \]/) {
1554 $skip_call_trace = 1;
1555 }
1556
Steven Rostedt2545eb62010-11-02 15:01:32 -04001557 if ($full_line =~ /call trace:/i) {
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001558 if (!$bug && !$skip_call_trace) {
1559 if ($ignore_errors) {
1560 $bug_ignored = 1;
1561 } else {
1562 $bug = 1;
1563 $failure_start = time;
1564 }
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001565 }
1566 }
1567
1568 if ($bug && defined($stop_after_failure) &&
1569 $stop_after_failure >= 0) {
1570 my $now = time;
1571 if ($now - $failure_start >= $stop_after_failure) {
1572 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1573 last;
1574 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001575 }
1576
1577 if ($full_line =~ /\[ end of backtrace testing \]/) {
1578 $skip_call_trace = 0;
1579 }
1580
1581 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001582 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001583 $bug = 1;
1584 }
1585
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001586 # Detect triple faults by testing the banner
1587 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1588 if ($1 eq $version) {
1589 $version_found = 1;
1590 } elsif ($version_found && $detect_triplefault) {
1591 # We already booted into the kernel we are testing,
1592 # but now we booted into another kernel?
1593 # Consider this a triple fault.
1594 doprint "Aleady booted in Linux kernel $version, but now\n";
1595 doprint "we booted into Linux kernel $1.\n";
1596 doprint "Assuming that this is a triple fault.\n";
1597 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1598 last;
1599 }
1600 }
1601
Steven Rostedt2545eb62010-11-02 15:01:32 -04001602 if ($line =~ /\n/) {
1603 $full_line = "";
1604 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001605
1606 if ($stop_test_after > 0 && !$booted && !$bug) {
1607 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001608 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001609 $done = 1;
1610 }
1611 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001612 }
1613
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001614 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001615
Steven Rostedt2545eb62010-11-02 15:01:32 -04001616 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001617 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001618 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001619 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001620
Steven Rostedta75fece2010-11-02 14:58:27 -04001621 if (!$booted) {
1622 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001623 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001624 }
1625
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001626 if ($bug_ignored) {
1627 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
1628 }
1629
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001630 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001631}
1632
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001633sub eval_kernel_version {
1634 my ($option) = @_;
1635
1636 $option =~ s/\$KERNEL_VERSION/$version/g;
1637
1638 return $option;
1639}
1640
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001641sub do_post_install {
1642
1643 return if (!defined($post_install));
1644
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001645 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001646 run_command "$cp_post_install" or
1647 dodie "Failed to run post install";
1648}
1649
Steven Rostedt2545eb62010-11-02 15:01:32 -04001650sub install {
1651
Steven Rostedte0a87422011-09-30 17:50:48 -04001652 return if ($no_install);
1653
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001654 my $cp_target = eval_kernel_version $target_image;
1655
Steven Rostedt02ad2612012-03-21 08:21:24 -04001656 run_scp_install "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001657 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001658
1659 my $install_mods = 0;
1660
1661 # should we process modules?
1662 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001663 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001664 while (<IN>) {
1665 if (/CONFIG_MODULES(=y)?/) {
1666 $install_mods = 1 if (defined($1));
1667 last;
1668 }
1669 }
1670 close(IN);
1671
1672 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001673 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001674 doprint "No modules needed\n";
1675 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001676 }
1677
Steven Rostedt627977d2012-03-21 08:16:15 -04001678 run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001679 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001680
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001681 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001682 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001683
Steven Rostedte48c5292010-11-02 14:35:37 -04001684 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001685 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001686
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001687 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001688 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001689 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001690
Steven Rostedt02ad2612012-03-21 08:21:24 -04001691 run_scp_mod "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001692 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001693
Steven Rostedta75fece2010-11-02 14:58:27 -04001694 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001695
Steven Rostedte7b13442011-06-14 20:44:36 -04001696 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001697 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001698
Steven Rostedte48c5292010-11-02 14:35:37 -04001699 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001700
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001701 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001702}
1703
Steven Rostedtddf607e2011-06-14 20:49:13 -04001704sub get_version {
1705 # get the release name
Steven Rostedt683a3e62012-05-18 13:34:35 -04001706 return if ($have_version);
Steven Rostedtddf607e2011-06-14 20:49:13 -04001707 doprint "$make kernelrelease ... ";
1708 $version = `$make kernelrelease | tail -1`;
1709 chomp($version);
1710 doprint "$version\n";
Steven Rostedt683a3e62012-05-18 13:34:35 -04001711 $have_version = 1;
Steven Rostedtddf607e2011-06-14 20:49:13 -04001712}
1713
1714sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001715 # Make sure the stable kernel has finished booting
1716 start_monitor;
1717 wait_for_monitor 5;
1718 end_monitor;
1719
Steven Rostedtddf607e2011-06-14 20:49:13 -04001720 get_grub_index;
1721 get_version;
1722 install;
1723
1724 start_monitor;
1725 return monitor;
1726}
1727
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001728sub check_buildlog {
1729 my ($patch) = @_;
1730
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001731 my @files = `git show $patch | diffstat -l`;
1732
1733 open(IN, "git show $patch |") or
1734 dodie "failed to show $patch";
1735 while (<IN>) {
1736 if (m,^--- a/(.*),) {
1737 chomp $1;
1738 $files[$#files] = $1;
1739 }
1740 }
1741 close(IN);
1742
1743 open(IN, $buildlog) or dodie "Can't open $buildlog";
1744 while (<IN>) {
1745 if (/^\s*(.*?):.*(warning|error)/) {
1746 my $err = $1;
1747 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001748 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001749 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001750 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001751 }
1752 }
1753 }
1754 }
1755 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001756
1757 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001758}
1759
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001760sub apply_min_config {
1761 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05001762
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001763 # Read the config file and remove anything that
1764 # is in the force_config hash (from minconfig and others)
1765 # then add the force config back.
1766
1767 doprint "Applying minimum configurations into $output_config.new\n";
1768
1769 open (OUT, ">$outconfig") or
1770 dodie "Can't create $outconfig";
1771
1772 if (-f $output_config) {
1773 open (IN, $output_config) or
1774 dodie "Failed to open $output_config";
1775 while (<IN>) {
1776 if (/^(# )?(CONFIG_[^\s=]*)/) {
1777 next if (defined($force_config{$2}));
1778 }
1779 print OUT;
1780 }
1781 close IN;
1782 }
1783 foreach my $config (keys %force_config) {
1784 print OUT "$force_config{$config}\n";
1785 }
1786 close OUT;
1787
1788 run_command "mv $outconfig $output_config";
1789}
1790
1791sub make_oldconfig {
1792
Steven Rostedt4c4ab122011-07-15 21:16:17 -04001793 my @force_list = keys %force_config;
1794
1795 if ($#force_list >= 0) {
1796 apply_min_config;
1797 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001798
1799 if (!run_command "$make oldnoconfig") {
Steven Rostedt612b9e92011-03-07 13:27:43 -05001800 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1801 # try a yes '' | oldconfig
1802 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001803 run_command "yes '' | $make oldconfig" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001804 dodie "failed make config oldconfig";
1805 }
1806}
1807
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001808# read a config file and use this to force new configs.
1809sub load_force_config {
1810 my ($config) = @_;
1811
1812 open(IN, $config) or
1813 dodie "failed to read $config";
1814 while (<IN>) {
1815 chomp;
1816 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1817 $force_config{$1} = $_;
1818 } elsif (/^# (CONFIG_\S*) is not set/) {
1819 $force_config{$1} = $_;
1820 }
1821 }
1822 close IN;
1823}
1824
Steven Rostedt2545eb62010-11-02 15:01:32 -04001825sub build {
1826 my ($type) = @_;
1827
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001828 unlink $buildlog;
1829
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001830 # Failed builds should not reboot the target
1831 my $save_no_reboot = $no_reboot;
1832 $no_reboot = 1;
1833
Steven Rostedt683a3e62012-05-18 13:34:35 -04001834 # Calculate a new version from here.
1835 $have_version = 0;
1836
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001837 if (defined($pre_build)) {
1838 my $ret = run_command $pre_build;
1839 if (!$ret && defined($pre_build_die) &&
1840 $pre_build_die) {
1841 dodie "failed to pre_build\n";
1842 }
1843 }
1844
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001845 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001846 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001847 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001848
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001849 $type = "oldconfig";
1850 }
1851
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001852 # old config can ask questions
1853 if ($type eq "oldconfig") {
Steven Rostedt9386c6a2010-11-08 16:35:48 -05001854 $type = "oldnoconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001855
1856 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001857 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001858
Andrew Jones13488232011-08-12 15:32:04 +02001859 if (!$noclean) {
1860 run_command "mv $output_config $outputdir/config_temp" or
1861 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001862
Andrew Jones13488232011-08-12 15:32:04 +02001863 run_command "$make mrproper" or dodie "make mrproper";
1864
1865 run_command "mv $outputdir/config_temp $output_config" or
1866 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001867 }
1868
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001869 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001870 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001871 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001872 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001873 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001874
1875 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04001876 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1877 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001878 close(OUT);
1879
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001880 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001881 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001882 }
1883
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001884 if ($type ne "oldnoconfig") {
1885 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001886 dodie "failed make config";
1887 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001888 # Run old config regardless, to enforce min configurations
1889 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001890
Steven Rostedta75fece2010-11-02 14:58:27 -04001891 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001892 my $build_ret = run_command "$make $build_options";
1893 undef $redirect;
1894
1895 if (defined($post_build)) {
Steven Rostedt683a3e62012-05-18 13:34:35 -04001896 # Because a post build may change the kernel version
1897 # do it now.
1898 get_version;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001899 my $ret = run_command $post_build;
1900 if (!$ret && defined($post_build_die) &&
1901 $post_build_die) {
1902 dodie "failed to post_build\n";
1903 }
1904 }
1905
1906 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001907 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001908 if ($in_bisect) {
1909 $no_reboot = $save_no_reboot;
1910 return 0;
1911 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001912 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001913 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001914
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001915 $no_reboot = $save_no_reboot;
1916
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001917 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001918}
1919
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001920sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04001921 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001922 if (defined($poweroff_after_halt)) {
1923 sleep $poweroff_after_halt;
1924 run_command "$power_off";
1925 }
1926 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001927 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04001928 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001929 }
1930}
1931
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001932sub success {
1933 my ($i) = @_;
1934
Steven Rostedte48c5292010-11-02 14:35:37 -04001935 $successes++;
1936
Steven Rostedt9064af52011-06-13 10:38:48 -04001937 my $name = "";
1938
1939 if (defined($test_name)) {
1940 $name = " ($test_name)";
1941 }
1942
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001943 doprint "\n\n*******************************************\n";
1944 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001945 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001946 doprint "*******************************************\n";
1947 doprint "*******************************************\n";
1948
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301949 if (defined($store_successes)) {
1950 save_logs "success", $store_successes;
1951 }
1952
Steven Rostedt576f6272010-11-02 14:58:38 -04001953 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001954 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001955 reboot_to_good $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001956 }
1957}
1958
Steven Rostedtc960bb92011-03-08 09:22:39 -05001959sub answer_bisect {
1960 for (;;) {
1961 doprint "Pass or fail? [p/f]";
1962 my $ans = <STDIN>;
1963 chomp $ans;
1964 if ($ans eq "p" || $ans eq "P") {
1965 return 1;
1966 } elsif ($ans eq "f" || $ans eq "F") {
1967 return 0;
1968 } else {
1969 print "Please answer 'P' or 'F'\n";
1970 }
1971 }
1972}
1973
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001974sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001975 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001976
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001977 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04001978 $reboot_on_error = 0;
1979 $poweroff_on_error = 0;
1980 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001981
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301982 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001983 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301984 undef $redirect;
1985
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001986 exit $failed;
1987}
1988
1989my $child_done;
1990
1991sub child_finished {
1992 $child_done = 1;
1993}
1994
1995sub do_run_test {
1996 my $child_pid;
1997 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001998 my $line;
1999 my $full_line;
2000 my $bug = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002001
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002002 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002003
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002004 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002005
2006 $child_done = 0;
2007
2008 $SIG{CHLD} = qw(child_finished);
2009
2010 $child_pid = fork;
2011
2012 child_run_test if (!$child_pid);
2013
2014 $full_line = "";
2015
2016 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002017 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002018 if (defined($line)) {
2019
2020 # we are not guaranteed to get a full line
2021 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002022 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002023
2024 if ($full_line =~ /call trace:/i) {
2025 $bug = 1;
2026 }
2027
2028 if ($full_line =~ /Kernel panic -/) {
2029 $bug = 1;
2030 }
2031
2032 if ($line =~ /\n/) {
2033 $full_line = "";
2034 }
2035 }
2036 } while (!$child_done && !$bug);
2037
2038 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002039 my $failure_start = time;
2040 my $now;
2041 do {
2042 $line = wait_for_input($monitor_fp, 1);
2043 if (defined($line)) {
2044 doprint $line;
2045 }
2046 $now = time;
2047 if ($now - $failure_start >= $stop_after_failure) {
2048 last;
2049 }
2050 } while (defined($line));
2051
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002052 doprint "Detected kernel crash!\n";
2053 # kill the child with extreme prejudice
2054 kill 9, $child_pid;
2055 }
2056
2057 waitpid $child_pid, 0;
2058 $child_exit = $?;
2059
Steven Rostedtc5dacb82011-12-22 12:43:57 -05002060 if (!$bug && $in_bisect) {
2061 if (defined($bisect_ret_good)) {
2062 if ($child_exit == $bisect_ret_good) {
2063 return 1;
2064 }
2065 }
2066 if (defined($bisect_ret_skip)) {
2067 if ($child_exit == $bisect_ret_skip) {
2068 return -1;
2069 }
2070 }
2071 if (defined($bisect_ret_abort)) {
2072 if ($child_exit == $bisect_ret_abort) {
2073 fail "test abort" and return -2;
2074 }
2075 }
2076 if (defined($bisect_ret_bad)) {
2077 if ($child_exit == $bisect_ret_skip) {
2078 return 0;
2079 }
2080 }
2081 if (defined($bisect_ret_default)) {
2082 if ($bisect_ret_default eq "good") {
2083 return 1;
2084 } elsif ($bisect_ret_default eq "bad") {
2085 return 0;
2086 } elsif ($bisect_ret_default eq "skip") {
2087 return -1;
2088 } elsif ($bisect_ret_default eq "abort") {
2089 return -2;
2090 } else {
2091 fail "unknown default action: $bisect_ret_default"
2092 and return -2;
2093 }
2094 }
2095 }
2096
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002097 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002098 return 0 if $in_bisect;
2099 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002100 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002101 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002102}
2103
Steven Rostedta75fece2010-11-02 14:58:27 -04002104sub run_git_bisect {
2105 my ($command) = @_;
2106
2107 doprint "$command ... ";
2108
2109 my $output = `$command 2>&1`;
2110 my $ret = $?;
2111
2112 logit $output;
2113
2114 if ($ret) {
2115 doprint "FAILED\n";
2116 dodie "Failed to git bisect";
2117 }
2118
2119 doprint "SUCCESS\n";
2120 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2121 doprint "$1 [$2]\n";
2122 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002123 $bisect_bad_commit = $1;
Steven Rostedta75fece2010-11-02 14:58:27 -04002124 doprint "Found bad commit... $1\n";
2125 return 0;
2126 } else {
2127 # we already logged it, just print it now.
2128 print $output;
2129 }
2130
2131 return 1;
2132}
2133
Steven Rostedtc23dca72011-03-08 09:26:31 -05002134sub bisect_reboot {
2135 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002136 reboot_to_good $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05002137}
2138
2139# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05002140sub run_bisect_test {
2141 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002142
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002143 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002144 my $result;
2145 my $output;
2146 my $ret;
2147
Steven Rostedt0a05c762010-11-08 11:14:10 -05002148 $in_bisect = 1;
2149
2150 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002151
2152 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002153 if ($failed && $bisect_skip) {
2154 $in_bisect = 0;
2155 return -1;
2156 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002157 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002158
2159 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04002160 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002161
2162 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002163 if ($failed && $bisect_skip) {
2164 end_monitor;
2165 bisect_reboot;
2166 $in_bisect = 0;
2167 return -1;
2168 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002169 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002170
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002171 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002172 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002173 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002174 }
2175
2176 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002177 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002178 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002179 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002180 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04002181
2182 # reboot the box to a kernel we can ssh to
2183 if ($type ne "build") {
2184 bisect_reboot;
2185 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002186 $in_bisect = 0;
2187
2188 return $result;
2189}
2190
2191sub run_bisect {
2192 my ($type) = @_;
2193 my $buildtype = "oldconfig";
2194
2195 # We should have a minconfig to use?
2196 if (defined($minconfig)) {
2197 $buildtype = "useconfig:$minconfig";
2198 }
2199
2200 my $ret = run_bisect_test $type, $buildtype;
2201
Steven Rostedtc960bb92011-03-08 09:22:39 -05002202 if ($bisect_manual) {
2203 $ret = answer_bisect;
2204 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002205
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002206 # Are we looking for where it worked, not failed?
Russ Dill5158ba3e2012-04-23 19:43:00 -07002207 if ($reverse_bisect && $ret >= 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002208 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002209 }
2210
Steven Rostedtc23dca72011-03-08 09:26:31 -05002211 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002212 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002213 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002214 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002215 } elsif ($bisect_skip) {
2216 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2217 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002218 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002219}
2220
Steven Rostedtdad98752011-11-22 20:48:57 -05002221sub update_bisect_replay {
2222 my $tmp_log = "$tmpdir/ktest_bisect_log";
2223 run_command "git bisect log > $tmp_log" or
2224 die "can't create bisect log";
2225 return $tmp_log;
2226}
2227
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002228sub bisect {
2229 my ($i) = @_;
2230
2231 my $result;
2232
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002233 die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2234 die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
2235 die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002236
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002237 my $good = $bisect_good;
2238 my $bad = $bisect_bad;
2239 my $type = $bisect_type;
2240 my $start = $bisect_start;
2241 my $replay = $bisect_replay;
2242 my $start_files = $bisect_files;
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002243
2244 if (defined($start_files)) {
2245 $start_files = " -- " . $start_files;
2246 } else {
2247 $start_files = "";
2248 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002249
Steven Rostedta57419b2010-11-02 15:13:54 -04002250 # convert to true sha1's
2251 $good = get_sha1($good);
2252 $bad = get_sha1($bad);
2253
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002254 if (defined($bisect_reverse) && $bisect_reverse == 1) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002255 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2256 $reverse_bisect = 1;
2257 } else {
2258 $reverse_bisect = 0;
2259 }
2260
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002261 # Can't have a test without having a test to run
2262 if ($type eq "test" && !defined($run_test)) {
2263 $type = "boot";
2264 }
2265
Steven Rostedtdad98752011-11-22 20:48:57 -05002266 # Check if a bisect was running
2267 my $bisect_start_file = "$builddir/.git/BISECT_START";
2268
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002269 my $check = $bisect_check;
Steven Rostedtdad98752011-11-22 20:48:57 -05002270 my $do_check = defined($check) && $check ne "0";
2271
2272 if ( -f $bisect_start_file ) {
2273 print "Bisect in progress found\n";
2274 if ($do_check) {
2275 print " If you say yes, then no checks of good or bad will be done\n";
2276 }
2277 if (defined($replay)) {
2278 print "** BISECT_REPLAY is defined in config file **";
2279 print " Ignore config option and perform new git bisect log?\n";
2280 if (read_ync " (yes, no, or cancel) ") {
2281 $replay = update_bisect_replay;
2282 $do_check = 0;
2283 }
2284 } elsif (read_yn "read git log and continue?") {
2285 $replay = update_bisect_replay;
2286 $do_check = 0;
2287 }
2288 }
2289
2290 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002291
2292 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002293 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002294
2295 if ($check ne "good") {
2296 doprint "TESTING BISECT BAD [$bad]\n";
2297 run_command "git checkout $bad" or
2298 die "Failed to checkout $bad";
2299
2300 $result = run_bisect $type;
2301
2302 if ($result ne "bad") {
2303 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2304 }
2305 }
2306
2307 if ($check ne "bad") {
2308 doprint "TESTING BISECT GOOD [$good]\n";
2309 run_command "git checkout $good" or
2310 die "Failed to checkout $good";
2311
2312 $result = run_bisect $type;
2313
2314 if ($result ne "good") {
2315 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2316 }
2317 }
2318
2319 # checkout where we started
2320 run_command "git checkout $head" or
2321 die "Failed to checkout $head";
2322 }
2323
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002324 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002325 dodie "could not start bisect";
2326
2327 run_command "git bisect good $good" or
2328 dodie "could not set bisect good to $good";
2329
2330 run_git_bisect "git bisect bad $bad" or
2331 dodie "could not set bisect bad to $bad";
2332
2333 if (defined($replay)) {
2334 run_command "git bisect replay $replay" or
2335 dodie "failed to run replay";
2336 }
2337
2338 if (defined($start)) {
2339 run_command "git checkout $start" or
2340 dodie "failed to checkout $start";
2341 }
2342
2343 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002344 do {
2345 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002346 $test = run_git_bisect "git bisect $result";
2347 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002348
2349 run_command "git bisect log" or
2350 dodie "could not capture git bisect log";
2351
2352 run_command "git bisect reset" or
2353 dodie "could not reset git bisect";
2354
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002355 doprint "Bad commit was [$bisect_bad_commit]\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002356
Steven Rostedt0a05c762010-11-08 11:14:10 -05002357 success $i;
2358}
2359
2360my %config_ignore;
2361my %config_set;
2362
2363my %config_list;
2364my %null_config;
2365
2366my %dependency;
2367
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002368sub assign_configs {
2369 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002370
2371 open (IN, $config)
2372 or dodie "Failed to read $config";
2373
2374 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002375 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002376 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002377 }
2378 }
2379
2380 close(IN);
2381}
2382
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002383sub process_config_ignore {
2384 my ($config) = @_;
2385
2386 assign_configs \%config_ignore, $config;
2387}
2388
Steven Rostedt0a05c762010-11-08 11:14:10 -05002389sub read_current_config {
2390 my ($config_ref) = @_;
2391
2392 %{$config_ref} = ();
2393 undef %{$config_ref};
2394
2395 my @key = keys %{$config_ref};
2396 if ($#key >= 0) {
2397 print "did not delete!\n";
2398 exit;
2399 }
2400 open (IN, "$output_config");
2401
2402 while (<IN>) {
2403 if (/^(CONFIG\S+)=(.*)/) {
2404 ${$config_ref}{$1} = $2;
2405 }
2406 }
2407 close(IN);
2408}
2409
2410sub get_dependencies {
2411 my ($config) = @_;
2412
2413 my $arr = $dependency{$config};
2414 if (!defined($arr)) {
2415 return ();
2416 }
2417
2418 my @deps = @{$arr};
2419
2420 foreach my $dep (@{$arr}) {
2421 print "ADD DEP $dep\n";
2422 @deps = (@deps, get_dependencies $dep);
2423 }
2424
2425 return @deps;
2426}
2427
2428sub create_config {
2429 my @configs = @_;
2430
2431 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2432
2433 foreach my $config (@configs) {
2434 print OUT "$config_set{$config}\n";
2435 my @deps = get_dependencies $config;
2436 foreach my $dep (@deps) {
2437 print OUT "$config_set{$dep}\n";
2438 }
2439 }
2440
2441 foreach my $config (keys %config_ignore) {
2442 print OUT "$config_ignore{$config}\n";
2443 }
2444 close(OUT);
2445
2446# exit;
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002447 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002448}
2449
2450sub compare_configs {
2451 my (%a, %b) = @_;
2452
2453 foreach my $item (keys %a) {
2454 if (!defined($b{$item})) {
2455 print "diff $item\n";
2456 return 1;
2457 }
2458 delete $b{$item};
2459 }
2460
2461 my @keys = keys %b;
2462 if ($#keys) {
2463 print "diff2 $keys[0]\n";
2464 }
2465 return -1 if ($#keys >= 0);
2466
2467 return 0;
2468}
2469
2470sub run_config_bisect_test {
2471 my ($type) = @_;
2472
2473 return run_bisect_test $type, "oldconfig";
2474}
2475
2476sub process_passed {
2477 my (%configs) = @_;
2478
2479 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2480 # Passed! All these configs are part of a good compile.
2481 # Add them to the min options.
2482 foreach my $config (keys %configs) {
2483 if (defined($config_list{$config})) {
2484 doprint " removing $config\n";
2485 $config_ignore{$config} = $config_list{$config};
2486 delete $config_list{$config};
2487 }
2488 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002489 doprint "config copied to $outputdir/config_good\n";
2490 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002491}
2492
2493sub process_failed {
2494 my ($config) = @_;
2495
2496 doprint "\n\n***************************************\n";
2497 doprint "Found bad config: $config\n";
2498 doprint "***************************************\n\n";
2499}
2500
2501sub run_config_bisect {
2502
2503 my @start_list = keys %config_list;
2504
2505 if ($#start_list < 0) {
2506 doprint "No more configs to test!!!\n";
2507 return -1;
2508 }
2509
2510 doprint "***** RUN TEST ***\n";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002511 my $type = $config_bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002512 my $ret;
2513 my %current_config;
2514
2515 my $count = $#start_list + 1;
2516 doprint " $count configs to test\n";
2517
2518 my $half = int($#start_list / 2);
2519
2520 do {
2521 my @tophalf = @start_list[0 .. $half];
2522
2523 create_config @tophalf;
2524 read_current_config \%current_config;
2525
2526 $count = $#tophalf + 1;
2527 doprint "Testing $count configs\n";
2528 my $found = 0;
2529 # make sure we test something
2530 foreach my $config (@tophalf) {
2531 if (defined($current_config{$config})) {
2532 logit " $config\n";
2533 $found = 1;
2534 }
2535 }
2536 if (!$found) {
2537 # try the other half
2538 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002539 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedt0a05c762010-11-08 11:14:10 -05002540 create_config @tophalf;
2541 read_current_config \%current_config;
2542 foreach my $config (@tophalf) {
2543 if (defined($current_config{$config})) {
2544 logit " $config\n";
2545 $found = 1;
2546 }
2547 }
2548 if (!$found) {
2549 doprint "Failed: Can't make new config with current configs\n";
2550 foreach my $config (@start_list) {
2551 doprint " CONFIG: $config\n";
2552 }
2553 return -1;
2554 }
2555 $count = $#tophalf + 1;
2556 doprint "Testing $count configs\n";
2557 }
2558
2559 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002560 if ($bisect_manual) {
2561 $ret = answer_bisect;
2562 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002563 if ($ret) {
2564 process_passed %current_config;
2565 return 0;
2566 }
2567
2568 doprint "This config had a failure.\n";
2569 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002570 doprint "config copied to $outputdir/config_bad\n";
2571 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002572
2573 # A config exists in this group that was bad.
2574 foreach my $config (keys %config_list) {
2575 if (!defined($current_config{$config})) {
2576 doprint " removing $config\n";
2577 delete $config_list{$config};
2578 }
2579 }
2580
2581 @start_list = @tophalf;
2582
2583 if ($#start_list == 0) {
2584 process_failed $start_list[0];
2585 return 1;
2586 }
2587
2588 # remove half the configs we are looking at and see if
2589 # they are good.
2590 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002591 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002592
Steven Rostedtc960bb92011-03-08 09:22:39 -05002593 # we found a single config, try it again unless we are running manually
2594
2595 if ($bisect_manual) {
2596 process_failed $start_list[0];
2597 return 1;
2598 }
2599
Steven Rostedt0a05c762010-11-08 11:14:10 -05002600 my @tophalf = @start_list[0 .. 0];
2601
2602 $ret = run_config_bisect_test $type;
2603 if ($ret) {
2604 process_passed %current_config;
2605 return 0;
2606 }
2607
2608 process_failed $start_list[0];
2609 return 1;
2610}
2611
2612sub config_bisect {
2613 my ($i) = @_;
2614
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002615 my $start_config = $config_bisect;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002616
2617 my $tmpconfig = "$tmpdir/use_config";
2618
Steven Rostedt30f75da2011-06-13 10:35:35 -04002619 if (defined($config_bisect_good)) {
2620 process_config_ignore $config_bisect_good;
2621 }
2622
Steven Rostedt0a05c762010-11-08 11:14:10 -05002623 # Make the file with the bad config and the min config
2624 if (defined($minconfig)) {
2625 # read the min config for things to ignore
2626 run_command "cp $minconfig $tmpconfig" or
2627 dodie "failed to copy $minconfig to $tmpconfig";
2628 } else {
2629 unlink $tmpconfig;
2630 }
2631
Steven Rostedt0a05c762010-11-08 11:14:10 -05002632 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002633 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002634 process_config_ignore $tmpconfig;
2635 }
2636
2637 # now process the start config
2638 run_command "cp $start_config $output_config" or
2639 dodie "failed to copy $start_config to $output_config";
2640
2641 # read directly what we want to check
2642 my %config_check;
2643 open (IN, $output_config)
Masanari Iidaf9dee312012-02-11 21:46:56 +09002644 or dodie "failed to open $output_config";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002645
2646 while (<IN>) {
2647 if (/^((CONFIG\S*)=.*)/) {
2648 $config_check{$2} = $1;
2649 }
2650 }
2651 close(IN);
2652
Steven Rostedt250bae82011-07-15 22:05:59 -04002653 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002654 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002655
2656 # check to see what we lost (or gained)
2657 open (IN, $output_config)
2658 or dodie "Failed to read $start_config";
2659
2660 my %removed_configs;
2661 my %added_configs;
2662
2663 while (<IN>) {
2664 if (/^((CONFIG\S*)=.*)/) {
2665 # save off all options
2666 $config_set{$2} = $1;
2667 if (defined($config_check{$2})) {
2668 if (defined($config_ignore{$2})) {
2669 $removed_configs{$2} = $1;
2670 } else {
2671 $config_list{$2} = $1;
2672 }
2673 } elsif (!defined($config_ignore{$2})) {
2674 $added_configs{$2} = $1;
2675 $config_list{$2} = $1;
2676 }
2677 }
2678 }
2679 close(IN);
2680
2681 my @confs = keys %removed_configs;
2682 if ($#confs >= 0) {
2683 doprint "Configs overridden by default configs and removed from check:\n";
2684 foreach my $config (@confs) {
2685 doprint " $config\n";
2686 }
2687 }
2688 @confs = keys %added_configs;
2689 if ($#confs >= 0) {
2690 doprint "Configs appearing in make oldconfig and added:\n";
2691 foreach my $config (@confs) {
2692 doprint " $config\n";
2693 }
2694 }
2695
2696 my %config_test;
2697 my $once = 0;
2698
2699 # Sometimes kconfig does weird things. We must make sure
2700 # that the config we autocreate has everything we need
2701 # to test, otherwise we may miss testing configs, or
2702 # may not be able to create a new config.
2703 # Here we create a config with everything set.
2704 create_config (keys %config_list);
2705 read_current_config \%config_test;
2706 foreach my $config (keys %config_list) {
2707 if (!defined($config_test{$config})) {
2708 if (!$once) {
2709 $once = 1;
2710 doprint "Configs not produced by kconfig (will not be checked):\n";
2711 }
2712 doprint " $config\n";
2713 delete $config_list{$config};
2714 }
2715 }
2716 my $ret;
2717 do {
2718 $ret = run_config_bisect;
2719 } while (!$ret);
2720
2721 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002722
2723 success $i;
2724}
2725
Steven Rostedt27d934b2011-05-20 09:18:18 -04002726sub patchcheck_reboot {
2727 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002728 reboot_to_good $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04002729}
2730
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002731sub patchcheck {
2732 my ($i) = @_;
2733
2734 die "PATCHCHECK_START[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002735 if (!defined($patchcheck_start));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002736 die "PATCHCHECK_TYPE[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002737 if (!defined($patchcheck_type));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002738
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002739 my $start = $patchcheck_start;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002740
2741 my $end = "HEAD";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002742 if (defined($patchcheck_end)) {
2743 $end = $patchcheck_end;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002744 }
2745
Steven Rostedta57419b2010-11-02 15:13:54 -04002746 # Get the true sha1's since we can use things like HEAD~3
2747 $start = get_sha1($start);
2748 $end = get_sha1($end);
2749
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002750 my $type = $patchcheck_type;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002751
2752 # Can't have a test without having a test to run
2753 if ($type eq "test" && !defined($run_test)) {
2754 $type = "boot";
2755 }
2756
2757 open (IN, "git log --pretty=oneline $end|") or
2758 dodie "could not get git list";
2759
2760 my @list;
2761
2762 while (<IN>) {
2763 chomp;
2764 $list[$#list+1] = $_;
2765 last if (/^$start/);
2766 }
2767 close(IN);
2768
2769 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002770 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002771 }
2772
2773 # go backwards in the list
2774 @list = reverse @list;
2775
2776 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04002777 my %ignored_warnings;
2778
2779 if (defined($ignore_warnings)) {
2780 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2781 $ignored_warnings{$sha1} = 1;
2782 }
2783 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002784
2785 $in_patchcheck = 1;
2786 foreach my $item (@list) {
2787 my $sha1 = $item;
2788 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2789
2790 doprint "\nProcessing commit $item\n\n";
2791
2792 run_command "git checkout $sha1" or
2793 die "Failed to checkout $sha1";
2794
2795 # only clean on the first and last patch
2796 if ($item eq $list[0] ||
2797 $item eq $list[$#list]) {
2798 $noclean = $save_clean;
2799 } else {
2800 $noclean = 1;
2801 }
2802
2803 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002804 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002805 } else {
2806 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002807 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002808 }
2809
Steven Rostedt19902072011-06-14 20:46:25 -04002810
2811 if (!defined($ignored_warnings{$sha1})) {
2812 check_buildlog $sha1 or return 0;
2813 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002814
2815 next if ($type eq "build");
2816
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002817 my $failed = 0;
2818
Steven Rostedtddf607e2011-06-14 20:49:13 -04002819 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002820
2821 if (!$failed && $type ne "boot"){
2822 do_run_test or $failed = 1;
2823 }
2824 end_monitor;
2825 return 0 if ($failed);
2826
Steven Rostedt27d934b2011-05-20 09:18:18 -04002827 patchcheck_reboot;
2828
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002829 }
2830 $in_patchcheck = 0;
2831 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002832
2833 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002834}
2835
Steven Rostedtb9066f62011-07-15 21:25:24 -04002836my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04002837my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002838my $iflevel = 0;
2839my @ifdeps;
2840
2841# prevent recursion
2842my %read_kconfigs;
2843
Steven Rostedtac6974c2011-10-04 09:40:17 -04002844sub add_dep {
2845 # $config depends on $dep
2846 my ($config, $dep) = @_;
2847
2848 if (defined($depends{$config})) {
2849 $depends{$config} .= " " . $dep;
2850 } else {
2851 $depends{$config} = $dep;
2852 }
2853
2854 # record the number of configs depending on $dep
2855 if (defined $depcount{$dep}) {
2856 $depcount{$dep}++;
2857 } else {
2858 $depcount{$dep} = 1;
2859 }
2860}
2861
Steven Rostedtb9066f62011-07-15 21:25:24 -04002862# taken from streamline_config.pl
2863sub read_kconfig {
2864 my ($kconfig) = @_;
2865
2866 my $state = "NONE";
2867 my $config;
2868 my @kconfigs;
2869
2870 my $cont = 0;
2871 my $line;
2872
2873
2874 if (! -f $kconfig) {
2875 doprint "file $kconfig does not exist, skipping\n";
2876 return;
2877 }
2878
2879 open(KIN, "$kconfig")
2880 or die "Can't open $kconfig";
2881 while (<KIN>) {
2882 chomp;
2883
2884 # Make sure that lines ending with \ continue
2885 if ($cont) {
2886 $_ = $line . " " . $_;
2887 }
2888
2889 if (s/\\$//) {
2890 $cont = 1;
2891 $line = $_;
2892 next;
2893 }
2894
2895 $cont = 0;
2896
2897 # collect any Kconfig sources
2898 if (/^source\s*"(.*)"/) {
2899 $kconfigs[$#kconfigs+1] = $1;
2900 }
2901
2902 # configs found
2903 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2904 $state = "NEW";
2905 $config = $2;
2906
2907 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04002908 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04002909 }
2910
2911 # collect the depends for the config
2912 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2913
Steven Rostedtac6974c2011-10-04 09:40:17 -04002914 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002915
2916 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04002917 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2918
2919 # selected by depends on config
2920 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002921
2922 # Check for if statements
2923 } elsif (/^if\s+(.*\S)\s*$/) {
2924 my $deps = $1;
2925 # remove beginning and ending non text
2926 $deps =~ s/^[^a-zA-Z0-9_]*//;
2927 $deps =~ s/[^a-zA-Z0-9_]*$//;
2928
2929 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2930
2931 $ifdeps[$iflevel++] = join ':', @deps;
2932
2933 } elsif (/^endif/) {
2934
2935 $iflevel-- if ($iflevel);
2936
2937 # stop on "help"
2938 } elsif (/^\s*help\s*$/) {
2939 $state = "NONE";
2940 }
2941 }
2942 close(KIN);
2943
2944 # read in any configs that were found.
2945 foreach $kconfig (@kconfigs) {
2946 if (!defined($read_kconfigs{$kconfig})) {
2947 $read_kconfigs{$kconfig} = 1;
2948 read_kconfig("$builddir/$kconfig");
2949 }
2950 }
2951}
2952
2953sub read_depends {
2954 # find out which arch this is by the kconfig file
2955 open (IN, $output_config)
2956 or dodie "Failed to read $output_config";
2957 my $arch;
2958 while (<IN>) {
2959 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2960 $arch = $1;
2961 last;
2962 }
2963 }
2964 close IN;
2965
2966 if (!defined($arch)) {
2967 doprint "Could not find arch from config file\n";
2968 doprint "no dependencies used\n";
2969 return;
2970 }
2971
2972 # arch is really the subarch, we need to know
2973 # what directory to look at.
2974 if ($arch eq "i386" || $arch eq "x86_64") {
2975 $arch = "x86";
2976 } elsif ($arch =~ /^tile/) {
2977 $arch = "tile";
2978 }
2979
2980 my $kconfig = "$builddir/arch/$arch/Kconfig";
2981
2982 if (! -f $kconfig && $arch =~ /\d$/) {
2983 my $orig = $arch;
2984 # some subarchs have numbers, truncate them
2985 $arch =~ s/\d*$//;
2986 $kconfig = "$builddir/arch/$arch/Kconfig";
2987 if (! -f $kconfig) {
2988 doprint "No idea what arch dir $orig is for\n";
2989 doprint "no dependencies used\n";
2990 return;
2991 }
2992 }
2993
2994 read_kconfig($kconfig);
2995}
2996
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002997sub read_config_list {
2998 my ($config) = @_;
2999
3000 open (IN, $config)
3001 or dodie "Failed to read $config";
3002
3003 while (<IN>) {
3004 if (/^((CONFIG\S*)=.*)/) {
3005 if (!defined($config_ignore{$2})) {
3006 $config_list{$2} = $1;
3007 }
3008 }
3009 }
3010
3011 close(IN);
3012}
3013
3014sub read_output_config {
3015 my ($config) = @_;
3016
3017 assign_configs \%config_ignore, $config;
3018}
3019
3020sub make_new_config {
3021 my @configs = @_;
3022
3023 open (OUT, ">$output_config")
3024 or dodie "Failed to write $output_config";
3025
3026 foreach my $config (@configs) {
3027 print OUT "$config\n";
3028 }
3029 close OUT;
3030}
3031
Steven Rostedtac6974c2011-10-04 09:40:17 -04003032sub chomp_config {
3033 my ($config) = @_;
3034
3035 $config =~ s/CONFIG_//;
3036
3037 return $config;
3038}
3039
Steven Rostedtb9066f62011-07-15 21:25:24 -04003040sub get_depends {
3041 my ($dep) = @_;
3042
Steven Rostedtac6974c2011-10-04 09:40:17 -04003043 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003044
3045 $dep = $depends{"$kconfig"};
3046
3047 # the dep string we have saves the dependencies as they
3048 # were found, including expressions like ! && ||. We
3049 # want to split this out into just an array of configs.
3050
3051 my $valid = "A-Za-z_0-9";
3052
3053 my @configs;
3054
3055 while ($dep =~ /[$valid]/) {
3056
3057 if ($dep =~ /^[^$valid]*([$valid]+)/) {
3058 my $conf = "CONFIG_" . $1;
3059
3060 $configs[$#configs + 1] = $conf;
3061
3062 $dep =~ s/^[^$valid]*[$valid]+//;
3063 } else {
3064 die "this should never happen";
3065 }
3066 }
3067
3068 return @configs;
3069}
3070
3071my %min_configs;
3072my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003073my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003074my %processed_configs;
3075my %nochange_config;
3076
3077sub test_this_config {
3078 my ($config) = @_;
3079
3080 my $found;
3081
3082 # if we already processed this config, skip it
3083 if (defined($processed_configs{$config})) {
3084 return undef;
3085 }
3086 $processed_configs{$config} = 1;
3087
3088 # if this config failed during this round, skip it
3089 if (defined($nochange_config{$config})) {
3090 return undef;
3091 }
3092
Steven Rostedtac6974c2011-10-04 09:40:17 -04003093 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003094
3095 # Test dependencies first
3096 if (defined($depends{"$kconfig"})) {
3097 my @parents = get_depends $config;
3098 foreach my $parent (@parents) {
3099 # if the parent is in the min config, check it first
3100 next if (!defined($min_configs{$parent}));
3101 $found = test_this_config($parent);
3102 if (defined($found)) {
3103 return $found;
3104 }
3105 }
3106 }
3107
3108 # Remove this config from the list of configs
3109 # do a make oldnoconfig and then read the resulting
3110 # .config to make sure it is missing the config that
3111 # we had before
3112 my %configs = %min_configs;
3113 delete $configs{$config};
3114 make_new_config ((values %configs), (values %keep_configs));
3115 make_oldconfig;
3116 undef %configs;
3117 assign_configs \%configs, $output_config;
3118
3119 return $config if (!defined($configs{$config}));
3120
3121 doprint "disabling config $config did not change .config\n";
3122
3123 $nochange_config{$config} = 1;
3124
3125 return undef;
3126}
3127
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003128sub make_min_config {
3129 my ($i) = @_;
3130
3131 if (!defined($output_minconfig)) {
3132 fail "OUTPUT_MIN_CONFIG not defined" and return;
3133 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04003134
3135 # If output_minconfig exists, and the start_minconfig
3136 # came from min_config, than ask if we should use
3137 # that instead.
3138 if (-f $output_minconfig && !$start_minconfig_defined) {
3139 print "$output_minconfig exists\n";
3140 if (read_yn " Use it as minconfig?") {
3141 $start_minconfig = $output_minconfig;
3142 }
3143 }
3144
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003145 if (!defined($start_minconfig)) {
3146 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
3147 }
3148
Steven Rostedt35ce5952011-07-15 21:57:25 -04003149 my $temp_config = "$tmpdir/temp_config";
3150
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003151 # First things first. We build an allnoconfig to find
3152 # out what the defaults are that we can't touch.
3153 # Some are selections, but we really can't handle selections.
3154
3155 my $save_minconfig = $minconfig;
3156 undef $minconfig;
3157
3158 run_command "$make allnoconfig" or return 0;
3159
Steven Rostedtb9066f62011-07-15 21:25:24 -04003160 read_depends;
3161
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003162 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003163
Steven Rostedt43d1b652011-07-15 22:01:56 -04003164 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003165 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003166
3167 if (defined($ignore_config)) {
3168 # make sure the file exists
3169 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003170 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003171 }
3172
Steven Rostedt43d1b652011-07-15 22:01:56 -04003173 %keep_configs = %save_configs;
3174
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003175 doprint "Load initial configs from $start_minconfig\n";
3176
3177 # Look at the current min configs, and save off all the
3178 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003179 assign_configs \%min_configs, $start_minconfig;
3180
3181 my @config_keys = keys %min_configs;
3182
Steven Rostedtac6974c2011-10-04 09:40:17 -04003183 # All configs need a depcount
3184 foreach my $config (@config_keys) {
3185 my $kconfig = chomp_config $config;
3186 if (!defined $depcount{$kconfig}) {
3187 $depcount{$kconfig} = 0;
3188 }
3189 }
3190
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003191 # Remove anything that was set by the make allnoconfig
3192 # we shouldn't need them as they get set for us anyway.
3193 foreach my $config (@config_keys) {
3194 # Remove anything in the ignore_config
3195 if (defined($keep_configs{$config})) {
3196 my $file = $ignore_config;
3197 $file =~ s,.*/(.*?)$,$1,;
3198 doprint "$config set by $file ... ignored\n";
3199 delete $min_configs{$config};
3200 next;
3201 }
3202 # But make sure the settings are the same. If a min config
3203 # sets a selection, we do not want to get rid of it if
3204 # it is not the same as what we have. Just move it into
3205 # the keep configs.
3206 if (defined($config_ignore{$config})) {
3207 if ($config_ignore{$config} ne $min_configs{$config}) {
3208 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3209 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3210 $keep_configs{$config} = $min_configs{$config};
3211 } else {
3212 doprint "$config set by allnoconfig ... ignored\n";
3213 }
3214 delete $min_configs{$config};
3215 }
3216 }
3217
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003218 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003219 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003220
3221 while (!$done) {
3222
3223 my $config;
3224 my $found;
3225
3226 # Now disable each config one by one and do a make oldconfig
3227 # till we find a config that changes our list.
3228
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003229 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003230
3231 # Sort keys by who is most dependent on
3232 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3233 @test_configs ;
3234
3235 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003236 my $reset = 1;
3237 for (my $i = 0; $i < $#test_configs; $i++) {
3238 if (!defined($nochange_config{$test_configs[0]})) {
3239 $reset = 0;
3240 last;
3241 }
3242 # This config didn't change the .config last time.
3243 # Place it at the end
3244 my $config = shift @test_configs;
3245 push @test_configs, $config;
3246 }
3247
3248 # if every test config has failed to modify the .config file
3249 # in the past, then reset and start over.
3250 if ($reset) {
3251 undef %nochange_config;
3252 }
3253
Steven Rostedtb9066f62011-07-15 21:25:24 -04003254 undef %processed_configs;
3255
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003256 foreach my $config (@test_configs) {
3257
Steven Rostedtb9066f62011-07-15 21:25:24 -04003258 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003259
Steven Rostedtb9066f62011-07-15 21:25:24 -04003260 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003261
3262 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003263 }
3264
3265 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003266 # we could have failed due to the nochange_config hash
3267 # reset and try again
3268 if (!$take_two) {
3269 undef %nochange_config;
3270 $take_two = 1;
3271 next;
3272 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003273 doprint "No more configs found that we can disable\n";
3274 $done = 1;
3275 last;
3276 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003277 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003278
3279 $config = $found;
3280
3281 doprint "Test with $config disabled\n";
3282
3283 # set in_bisect to keep build and monitor from dieing
3284 $in_bisect = 1;
3285
3286 my $failed = 0;
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003287 build "oldconfig" or $failed = 1;
3288 if (!$failed) {
3289 start_monitor_and_boot or $failed = 1;
3290 end_monitor;
3291 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003292
3293 $in_bisect = 0;
3294
3295 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003296 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003297 # this config is needed, add it to the ignore list.
3298 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003299 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003300 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003301
3302 # update new ignore configs
3303 if (defined($ignore_config)) {
3304 open (OUT, ">$temp_config")
3305 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003306 foreach my $config (keys %save_configs) {
3307 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003308 }
3309 close OUT;
3310 run_command "mv $temp_config $ignore_config" or
3311 dodie "failed to copy update to $ignore_config";
3312 }
3313
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003314 } else {
3315 # We booted without this config, remove it from the minconfigs.
3316 doprint "$config is not needed, disabling\n";
3317
3318 delete $min_configs{$config};
3319
3320 # Also disable anything that is not enabled in this config
3321 my %configs;
3322 assign_configs \%configs, $output_config;
3323 my @config_keys = keys %min_configs;
3324 foreach my $config (@config_keys) {
3325 if (!defined($configs{$config})) {
3326 doprint "$config is not set, disabling\n";
3327 delete $min_configs{$config};
3328 }
3329 }
3330
3331 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003332 open (OUT, ">$temp_config")
3333 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003334 foreach my $config (keys %keep_configs) {
3335 print OUT "$keep_configs{$config}\n";
3336 }
3337 foreach my $config (keys %min_configs) {
3338 print OUT "$min_configs{$config}\n";
3339 }
3340 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003341
3342 run_command "mv $temp_config $output_minconfig" or
3343 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003344 }
3345
3346 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003347 reboot_to_good $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003348 }
3349
3350 success $i;
3351 return 1;
3352}
3353
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003354$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003355
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003356if ($#ARGV == 0) {
3357 $ktest_config = $ARGV[0];
3358 if (! -f $ktest_config) {
3359 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003360 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003361 exit 0;
3362 }
3363 }
3364} else {
3365 $ktest_config = "ktest.conf";
3366}
3367
3368if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003369 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003370 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003371 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3372 print OUT << "EOF"
3373# Generated by ktest.pl
3374#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003375
3376# PWD is a ktest.pl variable that will result in the process working
3377# directory that ktest.pl is executed in.
3378
3379# THIS_DIR is automatically assigned the PWD of the path that generated
3380# the config file. It is best to use this variable when assigning other
3381# directory paths within this directory. This allows you to easily
3382# move the test cases to other locations or to other machines.
3383#
3384THIS_DIR := $variable{"PWD"}
3385
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003386# Define each test with TEST_START
3387# The config options below it will override the defaults
3388TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003389TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003390
3391DEFAULTS
3392EOF
3393;
3394 close(OUT);
3395}
3396read_config $ktest_config;
3397
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003398if (defined($opt{"LOG_FILE"})) {
3399 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3400}
3401
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003402# Append any configs entered in manually to the config file.
3403my @new_configs = keys %entered_configs;
3404if ($#new_configs >= 0) {
3405 print "\nAppending entered in configs to $ktest_config\n";
3406 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3407 foreach my $config (@new_configs) {
3408 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003409 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003410 }
3411}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003412
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003413if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3414 unlink $opt{"LOG_FILE"};
3415}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003416
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003417doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3418
Steven Rostedta57419b2010-11-02 15:13:54 -04003419for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3420
3421 if (!$i) {
3422 doprint "DEFAULT OPTIONS:\n";
3423 } else {
3424 doprint "\nTEST $i OPTIONS";
3425 if (defined($repeat_tests{$i})) {
3426 $repeat = $repeat_tests{$i};
3427 doprint " ITERATE $repeat";
3428 }
3429 doprint "\n";
3430 }
3431
3432 foreach my $option (sort keys %opt) {
3433
3434 if ($option =~ /\[(\d+)\]$/) {
3435 next if ($i != $1);
3436 } else {
3437 next if ($i);
3438 }
3439
3440 doprint "$option = $opt{$option}\n";
3441 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003442}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003443
Steven Rostedt2a625122011-05-20 15:48:59 -04003444sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003445 my ($name, $i) = @_;
3446
3447 my $option = "$name\[$i\]";
3448
3449 if (defined($opt{$option})) {
3450 return $opt{$option};
3451 }
3452
Steven Rostedta57419b2010-11-02 15:13:54 -04003453 foreach my $test (keys %repeat_tests) {
3454 if ($i >= $test &&
3455 $i < $test + $repeat_tests{$test}) {
3456 $option = "$name\[$test\]";
3457 if (defined($opt{$option})) {
3458 return $opt{$option};
3459 }
3460 }
3461 }
3462
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003463 if (defined($opt{$name})) {
3464 return $opt{$name};
3465 }
3466
3467 return undef;
3468}
3469
Steven Rostedt2a625122011-05-20 15:48:59 -04003470sub set_test_option {
3471 my ($name, $i) = @_;
3472
3473 my $option = __set_test_option($name, $i);
3474 return $option if (!defined($option));
3475
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003476 return eval_option($option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003477}
3478
Steven Rostedt2545eb62010-11-02 15:01:32 -04003479# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003480for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003481
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003482 # Do not reboot on failing test options
3483 $no_reboot = 1;
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003484 $reboot_success = 0;
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003485
Steven Rostedt683a3e62012-05-18 13:34:35 -04003486 $have_version = 0;
3487
Steven Rostedt576f6272010-11-02 14:58:38 -04003488 $iteration = $i;
3489
Steven Rostedta75fece2010-11-02 14:58:27 -04003490 my $makecmd = set_test_option("MAKE_CMD", $i);
3491
Steven Rostedt9cc9e092011-12-22 21:37:22 -05003492 # Load all the options into their mapped variable names
3493 foreach my $opt (keys %option_map) {
3494 ${$option_map{$opt}} = set_test_option($opt, $i);
3495 }
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003496
Steven Rostedt35ce5952011-07-15 21:57:25 -04003497 $start_minconfig_defined = 1;
3498
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003499 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003500 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003501 $start_minconfig = $minconfig;
3502 }
3503
Steven Rostedta75fece2010-11-02 14:58:27 -04003504 chdir $builddir || die "can't change directory to $builddir";
3505
Andrew Jonesa908a662011-08-12 15:32:03 +02003506 foreach my $dir ($tmpdir, $outputdir) {
3507 if (!-d $dir) {
3508 mkpath($dir) or
3509 die "can't create $dir";
3510 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003511 }
3512
Steven Rostedte48c5292010-11-02 14:35:37 -04003513 $ENV{"SSH_USER"} = $ssh_user;
3514 $ENV{"MACHINE"} = $machine;
3515
Steven Rostedta75fece2010-11-02 14:58:27 -04003516 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303517 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003518 $dmesg = "$tmpdir/dmesg-$machine";
3519 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003520 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003521
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003522 if (!$buildonly) {
3523 $target = "$ssh_user\@$machine";
3524 if ($reboot_type eq "grub") {
3525 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003526 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003527 }
3528
3529 my $run_type = $build_type;
3530 if ($test_type eq "patchcheck") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003531 $run_type = $patchcheck_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003532 } elsif ($test_type eq "bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003533 $run_type = $bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003534 } elsif ($test_type eq "config_bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003535 $run_type = $config_bisect_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003536 }
3537
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003538 if ($test_type eq "make_min_config") {
3539 $run_type = "";
3540 }
3541
Steven Rostedta75fece2010-11-02 14:58:27 -04003542 # mistake in config file?
3543 if (!defined($run_type)) {
3544 $run_type = "ERROR";
3545 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003546
Steven Rostedte0a87422011-09-30 17:50:48 -04003547 my $installme = "";
3548 $installme = " no_install" if ($no_install);
3549
Steven Rostedt2545eb62010-11-02 15:01:32 -04003550 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003551 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003552
3553 unlink $dmesg;
3554 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303555 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003556
Steven Rostedt250bae82011-07-15 22:05:59 -04003557 if (defined($addconfig)) {
3558 my $min = $minconfig;
3559 if (!defined($minconfig)) {
3560 $min = "";
3561 }
3562 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003563 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003564 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003565 }
3566
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003567 if (defined($checkout)) {
3568 run_command "git checkout $checkout" or
3569 die "failed to checkout $checkout";
3570 }
3571
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003572 $no_reboot = 0;
3573
Steven Rostedt648a1822012-03-21 11:18:27 -04003574 # A test may opt to not reboot the box
3575 if ($reboot_on_success) {
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003576 $reboot_success = 1;
Steven Rostedt648a1822012-03-21 11:18:27 -04003577 }
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003578
Steven Rostedta75fece2010-11-02 14:58:27 -04003579 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003580 bisect $i;
3581 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003582 } elsif ($test_type eq "config_bisect") {
3583 config_bisect $i;
3584 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003585 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003586 patchcheck $i;
3587 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003588 } elsif ($test_type eq "make_min_config") {
3589 make_min_config $i;
3590 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003591 }
3592
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003593 if ($build_type ne "nobuild") {
3594 build $build_type or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003595 }
3596
Steven Rostedtcd8e3682011-08-18 16:35:44 -04003597 if ($test_type eq "install") {
3598 get_version;
3599 install;
3600 success $i;
3601 next;
3602 }
3603
Steven Rostedta75fece2010-11-02 14:58:27 -04003604 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04003605 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04003606 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04003607
3608 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3609 do_run_test or $failed = 1;
3610 }
3611 end_monitor;
3612 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003613 }
3614
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003615 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003616}
3617
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003618if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003619 halt;
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003620} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot && $reboot_success) {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003621 reboot_to_good;
Steven Rostedt648a1822012-03-21 11:18:27 -04003622} elsif (defined($switch_to_good)) {
3623 # still need to get to the good kernel
3624 run_command $switch_to_good;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003625}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003626
Steven Rostedt648a1822012-03-21 11:18:27 -04003627
Steven Rostedte48c5292010-11-02 14:35:37 -04003628doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3629
Steven Rostedt2545eb62010-11-02 15:01:32 -04003630exit 0;