blob: 1143e6c5f07f01278b3f1ace00ca6913012765ac [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 Rostedta75fece2010-11-02 14:58:27 -040069my $machine;
Steven Rostedte48c5292010-11-02 14:35:37 -040070my $ssh_user;
Steven Rostedta75fece2010-11-02 14:58:27 -040071my $tmpdir;
72my $builddir;
73my $outputdir;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -050074my $output_config;
Steven Rostedta75fece2010-11-02 14:58:27 -040075my $test_type;
Steven Rostedt7faafbd2010-11-02 14:58:22 -040076my $build_type;
Steven Rostedta75fece2010-11-02 14:58:27 -040077my $build_options;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -040078my $pre_build;
79my $post_build;
80my $pre_build_die;
81my $post_build_die;
Steven Rostedta75fece2010-11-02 14:58:27 -040082my $reboot_type;
83my $reboot_script;
84my $power_cycle;
Steven Rostedte48c5292010-11-02 14:35:37 -040085my $reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -040086my $reboot_on_error;
Steven Rostedtbc7c5802011-12-22 16:29:10 -050087my $switch_to_good;
88my $switch_to_test;
Steven Rostedta75fece2010-11-02 14:58:27 -040089my $poweroff_on_error;
90my $die_on_failure;
Steven Rostedt576f6272010-11-02 14:58:38 -040091my $powercycle_after_reboot;
92my $poweroff_after_halt;
Steven Rostedte48c5292010-11-02 14:35:37 -040093my $ssh_exec;
94my $scp_to_target;
Steven Rostedt02ad2612012-03-21 08:21:24 -040095my $scp_to_target_install;
Steven Rostedta75fece2010-11-02 14:58:27 -040096my $power_off;
97my $grub_menu;
Steven Rostedt2545eb62010-11-02 15:01:32 -040098my $grub_number;
99my $target;
100my $make;
Steven Rostedt8b37ca82010-11-02 14:58:33 -0400101my $post_install;
Steven Rostedte0a87422011-09-30 17:50:48 -0400102my $no_install;
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400103my $noclean;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400104my $minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400105my $start_minconfig;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400106my $start_minconfig_defined;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400107my $output_minconfig;
108my $ignore_config;
Steven Rostedtbe405f92012-01-04 21:51:59 -0500109my $ignore_errors;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -0400110my $addconfig;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400111my $in_bisect = 0;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500112my $bisect_bad_commit = "";
Steven Rostedtd6ce2a02010-11-02 14:58:05 -0400113my $reverse_bisect;
Steven Rostedtc960bb92011-03-08 09:22:39 -0500114my $bisect_manual;
Steven Rostedtc23dca72011-03-08 09:26:31 -0500115my $bisect_skip;
Steven Rostedt30f75da2011-06-13 10:35:35 -0400116my $config_bisect_good;
Steven Rostedtc5dacb82011-12-22 12:43:57 -0500117my $bisect_ret_good;
118my $bisect_ret_bad;
119my $bisect_ret_skip;
120my $bisect_ret_abort;
121my $bisect_ret_default;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400122my $in_patchcheck = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400123my $run_test;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400124my $redirect;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400125my $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +0530126my $testlog;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400127my $dmesg;
128my $monitor_fp;
129my $monitor_pid;
130my $monitor_cnt = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -0400131my $sleep_time;
132my $bisect_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -0400133my $patchcheck_sleep_time;
Steven Rostedt19902072011-06-14 20:46:25 -0400134my $ignore_warnings;
Steven Rostedta75fece2010-11-02 14:58:27 -0400135my $store_failures;
Rabin Vincentde5b6e32011-11-18 17:05:31 +0530136my $store_successes;
Steven Rostedt9064af52011-06-13 10:38:48 -0400137my $test_name;
Steven Rostedta75fece2010-11-02 14:58:27 -0400138my $timeout;
139my $booted_timeout;
Steven Rostedtf1a5b962011-06-13 10:30:00 -0400140my $detect_triplefault;
Steven Rostedta75fece2010-11-02 14:58:27 -0400141my $console;
Steven Rostedt2b803362011-09-30 18:00:23 -0400142my $reboot_success_line;
Steven Rostedta75fece2010-11-02 14:58:27 -0400143my $success_line;
Steven Rostedt1c8a6172010-11-09 12:55:40 -0500144my $stop_after_success;
145my $stop_after_failure;
Steven Rostedt2d01b262011-03-08 09:47:54 -0500146my $stop_test_after;
Steven Rostedta75fece2010-11-02 14:58:27 -0400147my $build_target;
148my $target_image;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500149my $checkout;
Steven Rostedta75fece2010-11-02 14:58:27 -0400150my $localversion;
Steven Rostedt576f6272010-11-02 14:58:38 -0400151my $iteration = 0;
Steven Rostedte48c5292010-11-02 14:35:37 -0400152my $successes = 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400153
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500154my $bisect_good;
155my $bisect_bad;
156my $bisect_type;
157my $bisect_start;
158my $bisect_replay;
159my $bisect_files;
160my $bisect_reverse;
161my $bisect_check;
162
163my $config_bisect;
164my $config_bisect_type;
165
166my $patchcheck_type;
167my $patchcheck_start;
168my $patchcheck_end;
169
Steven Rostedt165708b2011-11-26 20:56:52 -0500170# set when a test is something other that just building or install
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500171# which would require more options.
172my $buildonly = 1;
173
Steven Rostedtdbd37832011-11-23 16:00:48 -0500174# set when creating a new config
175my $newconfig = 0;
176
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500177my %entered_configs;
178my %config_help;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400179my %variable;
Steven Rostedtfcb3f162011-06-13 10:40:58 -0400180my %force_config;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500181
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400182# do not force reboots on config problems
183my $no_reboot = 1;
184
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500185my %option_map = (
186 "MACHINE" => \$machine,
187 "SSH_USER" => \$ssh_user,
188 "TMP_DIR" => \$tmpdir,
189 "OUTPUT_DIR" => \$outputdir,
190 "BUILD_DIR" => \$builddir,
191 "TEST_TYPE" => \$test_type,
192 "BUILD_TYPE" => \$build_type,
193 "BUILD_OPTIONS" => \$build_options,
194 "PRE_BUILD" => \$pre_build,
195 "POST_BUILD" => \$post_build,
196 "PRE_BUILD_DIE" => \$pre_build_die,
197 "POST_BUILD_DIE" => \$post_build_die,
198 "POWER_CYCLE" => \$power_cycle,
199 "REBOOT" => \$reboot,
200 "BUILD_NOCLEAN" => \$noclean,
201 "MIN_CONFIG" => \$minconfig,
202 "OUTPUT_MIN_CONFIG" => \$output_minconfig,
203 "START_MIN_CONFIG" => \$start_minconfig,
204 "IGNORE_CONFIG" => \$ignore_config,
205 "TEST" => \$run_test,
206 "ADD_CONFIG" => \$addconfig,
207 "REBOOT_TYPE" => \$reboot_type,
208 "GRUB_MENU" => \$grub_menu,
209 "POST_INSTALL" => \$post_install,
210 "NO_INSTALL" => \$no_install,
211 "REBOOT_SCRIPT" => \$reboot_script,
212 "REBOOT_ON_ERROR" => \$reboot_on_error,
213 "SWITCH_TO_GOOD" => \$switch_to_good,
214 "SWITCH_TO_TEST" => \$switch_to_test,
215 "POWEROFF_ON_ERROR" => \$poweroff_on_error,
216 "DIE_ON_FAILURE" => \$die_on_failure,
217 "POWER_OFF" => \$power_off,
218 "POWERCYCLE_AFTER_REBOOT" => \$powercycle_after_reboot,
219 "POWEROFF_AFTER_HALT" => \$poweroff_after_halt,
220 "SLEEP_TIME" => \$sleep_time,
221 "BISECT_SLEEP_TIME" => \$bisect_sleep_time,
222 "PATCHCHECK_SLEEP_TIME" => \$patchcheck_sleep_time,
223 "IGNORE_WARNINGS" => \$ignore_warnings,
Steven Rostedtbe405f92012-01-04 21:51:59 -0500224 "IGNORE_ERRORS" => \$ignore_errors,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500225 "BISECT_MANUAL" => \$bisect_manual,
226 "BISECT_SKIP" => \$bisect_skip,
227 "CONFIG_BISECT_GOOD" => \$config_bisect_good,
228 "BISECT_RET_GOOD" => \$bisect_ret_good,
229 "BISECT_RET_BAD" => \$bisect_ret_bad,
230 "BISECT_RET_SKIP" => \$bisect_ret_skip,
231 "BISECT_RET_ABORT" => \$bisect_ret_abort,
232 "BISECT_RET_DEFAULT" => \$bisect_ret_default,
233 "STORE_FAILURES" => \$store_failures,
234 "STORE_SUCCESSES" => \$store_successes,
235 "TEST_NAME" => \$test_name,
236 "TIMEOUT" => \$timeout,
237 "BOOTED_TIMEOUT" => \$booted_timeout,
238 "CONSOLE" => \$console,
239 "DETECT_TRIPLE_FAULT" => \$detect_triplefault,
240 "SUCCESS_LINE" => \$success_line,
241 "REBOOT_SUCCESS_LINE" => \$reboot_success_line,
242 "STOP_AFTER_SUCCESS" => \$stop_after_success,
243 "STOP_AFTER_FAILURE" => \$stop_after_failure,
244 "STOP_TEST_AFTER" => \$stop_test_after,
245 "BUILD_TARGET" => \$build_target,
246 "SSH_EXEC" => \$ssh_exec,
247 "SCP_TO_TARGET" => \$scp_to_target,
Steven Rostedt02ad2612012-03-21 08:21:24 -0400248 "SCP_TO_TARGET_INSTALL" => \$scp_to_target_install,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500249 "CHECKOUT" => \$checkout,
250 "TARGET_IMAGE" => \$target_image,
251 "LOCALVERSION" => \$localversion,
252
253 "BISECT_GOOD" => \$bisect_good,
254 "BISECT_BAD" => \$bisect_bad,
255 "BISECT_TYPE" => \$bisect_type,
256 "BISECT_START" => \$bisect_start,
257 "BISECT_REPLAY" => \$bisect_replay,
258 "BISECT_FILES" => \$bisect_files,
259 "BISECT_REVERSE" => \$bisect_reverse,
260 "BISECT_CHECK" => \$bisect_check,
261
262 "CONFIG_BISECT" => \$config_bisect,
263 "CONFIG_BISECT_TYPE" => \$config_bisect_type,
264
265 "PATCHCHECK_TYPE" => \$patchcheck_type,
266 "PATCHCHECK_START" => \$patchcheck_start,
267 "PATCHCHECK_END" => \$patchcheck_end,
268);
269
270# Options may be used by other options, record them.
271my %used_options;
272
Steven Rostedt7bf51072011-10-22 09:07:03 -0400273# default variables that can be used
274chomp ($variable{"PWD"} = `pwd`);
275
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500276$config_help{"MACHINE"} = << "EOF"
277 The machine hostname that you will test.
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500278 For build only tests, it is still needed to differentiate log files.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500279EOF
280 ;
281$config_help{"SSH_USER"} = << "EOF"
282 The box is expected to have ssh on normal bootup, provide the user
283 (most likely root, since you need privileged operations)
284EOF
285 ;
286$config_help{"BUILD_DIR"} = << "EOF"
287 The directory that contains the Linux source code (full path).
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500288 You can use \${PWD} that will be the path where ktest.pl is run, or use
289 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500290EOF
291 ;
292$config_help{"OUTPUT_DIR"} = << "EOF"
293 The directory that the objects will be built (full path).
294 (can not be same as BUILD_DIR)
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500295 You can use \${PWD} that will be the path where ktest.pl is run, or use
296 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500297EOF
298 ;
299$config_help{"BUILD_TARGET"} = << "EOF"
300 The location of the compiled file to copy to the target.
301 (relative to OUTPUT_DIR)
302EOF
303 ;
Steven Rostedtdbd37832011-11-23 16:00:48 -0500304$config_help{"BUILD_OPTIONS"} = << "EOF"
305 Options to add to \"make\" when building.
306 i.e. -j20
307EOF
308 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500309$config_help{"TARGET_IMAGE"} = << "EOF"
310 The place to put your image on the test machine.
311EOF
312 ;
313$config_help{"POWER_CYCLE"} = << "EOF"
314 A script or command to reboot the box.
315
316 Here is a digital loggers power switch example
317 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
318
319 Here is an example to reboot a virtual box on the current host
320 with the name "Guest".
321 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
322EOF
323 ;
324$config_help{"CONSOLE"} = << "EOF"
325 The script or command that reads the console
326
327 If you use ttywatch server, something like the following would work.
328CONSOLE = nc -d localhost 3001
329
330 For a virtual machine with guest name "Guest".
331CONSOLE = virsh console Guest
332EOF
333 ;
334$config_help{"LOCALVERSION"} = << "EOF"
335 Required version ending to differentiate the test
336 from other linux builds on the system.
337EOF
338 ;
339$config_help{"REBOOT_TYPE"} = << "EOF"
340 Way to reboot the box to the test kernel.
341 Only valid options so far are "grub" and "script".
342
343 If you specify grub, it will assume grub version 1
344 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
345 and select that target to reboot to the kernel. If this is not
346 your setup, then specify "script" and have a command or script
347 specified in REBOOT_SCRIPT to boot to the target.
348
349 The entry in /boot/grub/menu.lst must be entered in manually.
350 The test will not modify that file.
351EOF
352 ;
353$config_help{"GRUB_MENU"} = << "EOF"
354 The grub title name for the test kernel to boot
355 (Only mandatory if REBOOT_TYPE = grub)
356
357 Note, ktest.pl will not update the grub menu.lst, you need to
358 manually add an option for the test. ktest.pl will search
359 the grub menu.lst for this option to find what kernel to
360 reboot into.
361
362 For example, if in the /boot/grub/menu.lst the test kernel title has:
363 title Test Kernel
364 kernel vmlinuz-test
365 GRUB_MENU = Test Kernel
366EOF
367 ;
368$config_help{"REBOOT_SCRIPT"} = << "EOF"
369 A script to reboot the target into the test kernel
370 (Only mandatory if REBOOT_TYPE = script)
371EOF
372 ;
373
Steven Rostedtdad98752011-11-22 20:48:57 -0500374sub read_prompt {
375 my ($cancel, $prompt) = @_;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400376
377 my $ans;
378
379 for (;;) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500380 if ($cancel) {
381 print "$prompt [y/n/C] ";
382 } else {
383 print "$prompt [Y/n] ";
384 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400385 $ans = <STDIN>;
386 chomp $ans;
387 if ($ans =~ /^\s*$/) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500388 if ($cancel) {
389 $ans = "c";
390 } else {
391 $ans = "y";
392 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400393 }
394 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
Steven Rostedtdad98752011-11-22 20:48:57 -0500395 if ($cancel) {
396 last if ($ans =~ /^c$/i);
397 print "Please answer either 'y', 'n' or 'c'.\n";
398 } else {
399 print "Please answer either 'y' or 'n'.\n";
400 }
401 }
402 if ($ans =~ /^c/i) {
403 exit;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400404 }
405 if ($ans !~ /^y$/i) {
406 return 0;
407 }
408 return 1;
409}
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500410
Steven Rostedtdad98752011-11-22 20:48:57 -0500411sub read_yn {
412 my ($prompt) = @_;
413
414 return read_prompt 0, $prompt;
415}
416
417sub read_ync {
418 my ($prompt) = @_;
419
420 return read_prompt 1, $prompt;
421}
422
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500423sub get_ktest_config {
424 my ($config) = @_;
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400425 my $ans;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500426
427 return if (defined($opt{$config}));
428
429 if (defined($config_help{$config})) {
430 print "\n";
431 print $config_help{$config};
432 }
433
434 for (;;) {
435 print "$config = ";
Steven Rostedtdbd37832011-11-23 16:00:48 -0500436 if (defined($default{$config}) && length($default{$config})) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500437 print "\[$default{$config}\] ";
438 }
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400439 $ans = <STDIN>;
440 $ans =~ s/^\s*(.*\S)\s*$/$1/;
441 if ($ans =~ /^\s*$/) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500442 if ($default{$config}) {
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400443 $ans = $default{$config};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500444 } else {
445 print "Your answer can not be blank\n";
446 next;
447 }
448 }
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500449 $entered_configs{$config} = ${ans};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500450 last;
451 }
452}
453
454sub get_ktest_configs {
455 get_ktest_config("MACHINE");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500456 get_ktest_config("BUILD_DIR");
457 get_ktest_config("OUTPUT_DIR");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500458
Steven Rostedtdbd37832011-11-23 16:00:48 -0500459 if ($newconfig) {
460 get_ktest_config("BUILD_OPTIONS");
461 }
462
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500463 # options required for other than just building a kernel
464 if (!$buildonly) {
Steven Rostedt165708b2011-11-26 20:56:52 -0500465 get_ktest_config("POWER_CYCLE");
466 get_ktest_config("CONSOLE");
467 }
468
469 # options required for install and more
470 if ($buildonly != 1) {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500471 get_ktest_config("SSH_USER");
472 get_ktest_config("BUILD_TARGET");
473 get_ktest_config("TARGET_IMAGE");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500474 }
475
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500476 get_ktest_config("LOCALVERSION");
477
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500478 return if ($buildonly);
479
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500480 my $rtype = $opt{"REBOOT_TYPE"};
481
482 if (!defined($rtype)) {
483 if (!defined($opt{"GRUB_MENU"})) {
484 get_ktest_config("REBOOT_TYPE");
485 $rtype = $entered_configs{"REBOOT_TYPE"};
486 } else {
487 $rtype = "grub";
488 }
489 }
490
491 if ($rtype eq "grub") {
492 get_ktest_config("GRUB_MENU");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500493 }
494}
495
Steven Rostedt77d942c2011-05-20 13:36:58 -0400496sub process_variables {
Steven Rostedt8d735212011-10-17 11:36:44 -0400497 my ($value, $remove_undef) = @_;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400498 my $retval = "";
499
500 # We want to check for '\', and it is just easier
501 # to check the previous characet of '$' and not need
502 # to worry if '$' is the first character. By adding
503 # a space to $value, we can just check [^\\]\$ and
504 # it will still work.
505 $value = " $value";
506
507 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
508 my $begin = $1;
509 my $var = $2;
510 my $end = $3;
511 # append beginning of value to retval
512 $retval = "$retval$begin";
513 if (defined($variable{$var})) {
514 $retval = "$retval$variable{$var}";
Steven Rostedt8d735212011-10-17 11:36:44 -0400515 } elsif (defined($remove_undef) && $remove_undef) {
516 # for if statements, any variable that is not defined,
517 # we simple convert to 0
518 $retval = "${retval}0";
Steven Rostedt77d942c2011-05-20 13:36:58 -0400519 } else {
520 # put back the origin piece.
521 $retval = "$retval\$\{$var\}";
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500522 # This could be an option that is used later, save
523 # it so we don't warn if this option is not one of
524 # ktests options.
525 $used_options{$var} = 1;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400526 }
527 $value = $end;
528 }
529 $retval = "$retval$value";
530
531 # remove the space added in the beginning
532 $retval =~ s/ //;
533
534 return "$retval"
535}
536
Steven Rostedta57419b2010-11-02 15:13:54 -0400537sub set_value {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400538 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
Steven Rostedta57419b2010-11-02 15:13:54 -0400539
Steven Rostedtcad96662011-12-22 11:32:52 -0500540 my $prvalue = process_variables($rvalue);
541
542 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500543 # Note if a test is something other than build, then we
544 # will need other manditory options.
Steven Rostedtcad96662011-12-22 11:32:52 -0500545 if ($prvalue ne "install") {
Steven Rostedt165708b2011-11-26 20:56:52 -0500546 $buildonly = 0;
547 } else {
548 # install still limits some manditory options.
549 $buildonly = 2;
550 }
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500551 }
552
Steven Rostedta57419b2010-11-02 15:13:54 -0400553 if (defined($opt{$lvalue})) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400554 if (!$override || defined(${$overrides}{$lvalue})) {
555 my $extra = "";
556 if ($override) {
557 $extra = "In the same override section!\n";
558 }
559 die "$name: $.: Option $lvalue defined more than once!\n$extra";
560 }
Steven Rostedtcad96662011-12-22 11:32:52 -0500561 ${$overrides}{$lvalue} = $prvalue;
Steven Rostedta57419b2010-11-02 15:13:54 -0400562 }
Steven Rostedt21a96792010-11-08 16:45:50 -0500563 if ($rvalue =~ /^\s*$/) {
564 delete $opt{$lvalue};
565 } else {
Steven Rostedtcad96662011-12-22 11:32:52 -0500566 $opt{$lvalue} = $prvalue;
Steven Rostedt21a96792010-11-08 16:45:50 -0500567 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400568}
569
Steven Rostedt77d942c2011-05-20 13:36:58 -0400570sub set_variable {
571 my ($lvalue, $rvalue) = @_;
572
573 if ($rvalue =~ /^\s*$/) {
574 delete $variable{$lvalue};
575 } else {
576 $rvalue = process_variables($rvalue);
577 $variable{$lvalue} = $rvalue;
578 }
579}
580
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400581sub process_compare {
582 my ($lval, $cmp, $rval) = @_;
583
584 # remove whitespace
585
586 $lval =~ s/^\s*//;
587 $lval =~ s/\s*$//;
588
589 $rval =~ s/^\s*//;
590 $rval =~ s/\s*$//;
591
592 if ($cmp eq "==") {
593 return $lval eq $rval;
594 } elsif ($cmp eq "!=") {
595 return $lval ne $rval;
596 }
597
598 my $statement = "$lval $cmp $rval";
599 my $ret = eval $statement;
600
601 # $@ stores error of eval
602 if ($@) {
603 return -1;
604 }
605
606 return $ret;
607}
608
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400609sub value_defined {
610 my ($val) = @_;
611
612 return defined($variable{$2}) ||
613 defined($opt{$2});
614}
615
Steven Rostedt8d735212011-10-17 11:36:44 -0400616my $d = 0;
617sub process_expression {
618 my ($name, $val) = @_;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400619
Steven Rostedt8d735212011-10-17 11:36:44 -0400620 my $c = $d++;
621
622 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
623 my $express = $1;
624
625 if (process_expression($name, $express)) {
626 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
627 } else {
628 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
629 }
630 }
631
632 $d--;
633 my $OR = "\\|\\|";
634 my $AND = "\\&\\&";
635
636 while ($val =~ s/^(.*?)($OR|$AND)//) {
637 my $express = $1;
638 my $op = $2;
639
640 if (process_expression($name, $express)) {
641 if ($op eq "||") {
642 return 1;
643 }
644 } else {
645 if ($op eq "&&") {
646 return 0;
647 }
648 }
649 }
Steven Rostedt45d73a52011-09-30 19:44:53 -0400650
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400651 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
652 my $ret = process_compare($1, $2, $3);
653 if ($ret < 0) {
654 die "$name: $.: Unable to process comparison\n";
655 }
656 return $ret;
657 }
658
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400659 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
660 if (defined $1) {
661 return !value_defined($2);
662 } else {
663 return value_defined($2);
664 }
665 }
666
Steven Rostedt45d73a52011-09-30 19:44:53 -0400667 if ($val =~ /^\s*0\s*$/) {
668 return 0;
669 } elsif ($val =~ /^\s*\d+\s*$/) {
670 return 1;
671 }
672
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400673 die ("$name: $.: Undefined content $val in if statement\n");
Steven Rostedt8d735212011-10-17 11:36:44 -0400674}
675
676sub process_if {
677 my ($name, $value) = @_;
678
679 # Convert variables and replace undefined ones with 0
680 my $val = process_variables($value, 1);
681 my $ret = process_expression $name, $val;
682
683 return $ret;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400684}
685
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400686sub __read_config {
687 my ($config, $current_test_num) = @_;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400688
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400689 my $in;
690 open($in, $config) || die "can't read file $config";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400691
Steven Rostedta57419b2010-11-02 15:13:54 -0400692 my $name = $config;
693 $name =~ s,.*/(.*),$1,;
694
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400695 my $test_num = $$current_test_num;
Steven Rostedta57419b2010-11-02 15:13:54 -0400696 my $default = 1;
697 my $repeat = 1;
698 my $num_tests_set = 0;
699 my $skip = 0;
700 my $rest;
Steven Rostedta9f84422011-10-17 11:06:29 -0400701 my $line;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400702 my $test_case = 0;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400703 my $if = 0;
704 my $if_set = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400705 my $override = 0;
706
707 my %overrides;
Steven Rostedta57419b2010-11-02 15:13:54 -0400708
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400709 while (<$in>) {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400710
711 # ignore blank lines and comments
712 next if (/^\s*$/ || /\s*\#/);
713
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400714 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400715
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400716 my $type = $1;
717 $rest = $2;
Steven Rostedta9f84422011-10-17 11:06:29 -0400718 $line = $2;
Steven Rostedta57419b2010-11-02 15:13:54 -0400719
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400720 my $old_test_num;
721 my $old_repeat;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400722 $override = 0;
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400723
724 if ($type eq "TEST_START") {
725
726 if ($num_tests_set) {
727 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
728 }
729
730 $old_test_num = $test_num;
731 $old_repeat = $repeat;
732
733 $test_num += $repeat;
734 $default = 0;
735 $repeat = 1;
736 } else {
737 $default = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400738 }
739
Steven Rostedta9f84422011-10-17 11:06:29 -0400740 # If SKIP is anywhere in the line, the command will be skipped
741 if ($rest =~ s/\s+SKIP\b//) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400742 $skip = 1;
743 } else {
Steven Rostedt0df213c2011-06-14 20:51:37 -0400744 $test_case = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400745 $skip = 0;
746 }
747
Steven Rostedta9f84422011-10-17 11:06:29 -0400748 if ($rest =~ s/\sELSE\b//) {
749 if (!$if) {
750 die "$name: $.: ELSE found with out matching IF section\n$_";
751 }
752 $if = 0;
753
754 if ($if_set) {
755 $skip = 1;
756 } else {
757 $skip = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400758 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400759 }
760
Steven Rostedta9f84422011-10-17 11:06:29 -0400761 if ($rest =~ s/\sIF\s+(.*)//) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400762 if (process_if($name, $1)) {
763 $if_set = 1;
764 } else {
765 $skip = 1;
766 }
767 $if = 1;
768 } else {
769 $if = 0;
Steven Rostedta9f84422011-10-17 11:06:29 -0400770 $if_set = 0;
Steven Rostedta57419b2010-11-02 15:13:54 -0400771 }
772
Steven Rostedta9f84422011-10-17 11:06:29 -0400773 if (!$skip) {
774 if ($type eq "TEST_START") {
775 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
776 $repeat = $1;
777 $repeat_tests{"$test_num"} = $repeat;
778 }
779 } elsif ($rest =~ s/\sOVERRIDE\b//) {
780 # DEFAULT only
781 $override = 1;
782 # Clear previous overrides
783 %overrides = ();
784 }
785 }
786
787 if (!$skip && $rest !~ /^\s*$/) {
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400788 die "$name: $.: Gargbage found after $type\n$_";
Steven Rostedta57419b2010-11-02 15:13:54 -0400789 }
790
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400791 if ($skip && $type eq "TEST_START") {
Steven Rostedta57419b2010-11-02 15:13:54 -0400792 $test_num = $old_test_num;
Steven Rostedte48c5292010-11-02 14:35:37 -0400793 $repeat = $old_repeat;
Steven Rostedta57419b2010-11-02 15:13:54 -0400794 }
795
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400796 } elsif (/^\s*ELSE\b(.*)$/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400797 if (!$if) {
798 die "$name: $.: ELSE found with out matching IF section\n$_";
799 }
800 $rest = $1;
801 if ($if_set) {
802 $skip = 1;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400803 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400804 } else {
805 $skip = 0;
806
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400807 if ($rest =~ /\sIF\s+(.*)/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400808 # May be a ELSE IF section.
809 if (!process_if($name, $1)) {
810 $skip = 1;
811 }
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400812 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400813 } else {
814 $if = 0;
815 }
816 }
817
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400818 if ($rest !~ /^\s*$/) {
819 die "$name: $.: Gargbage found after DEFAULTS\n$_";
820 }
821
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400822 } elsif (/^\s*INCLUDE\s+(\S+)/) {
823
824 next if ($skip);
825
826 if (!$default) {
827 die "$name: $.: INCLUDE can only be done in default sections\n$_";
828 }
829
830 my $file = process_variables($1);
831
832 if ($file !~ m,^/,) {
833 # check the path of the config file first
834 if ($config =~ m,(.*)/,) {
835 if (-f "$1/$file") {
836 $file = "$1/$file";
837 }
838 }
839 }
840
841 if ( ! -r $file ) {
842 die "$name: $.: Can't read file $file\n$_";
843 }
844
845 if (__read_config($file, \$test_num)) {
846 $test_case = 1;
847 }
848
Steven Rostedta57419b2010-11-02 15:13:54 -0400849 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
850
851 next if ($skip);
852
Steven Rostedt2545eb62010-11-02 15:01:32 -0400853 my $lvalue = $1;
854 my $rvalue = $2;
855
Steven Rostedta57419b2010-11-02 15:13:54 -0400856 if (!$default &&
857 ($lvalue eq "NUM_TESTS" ||
858 $lvalue eq "LOG_FILE" ||
859 $lvalue eq "CLEAR_LOG")) {
860 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400861 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400862
863 if ($lvalue eq "NUM_TESTS") {
864 if ($test_num) {
865 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
866 }
867 if (!$default) {
868 die "$name: $.: NUM_TESTS must be set in default section\n";
869 }
870 $num_tests_set = 1;
871 }
872
873 if ($default || $lvalue =~ /\[\d+\]$/) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400874 set_value($lvalue, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400875 } else {
876 my $val = "$lvalue\[$test_num\]";
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400877 set_value($val, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400878
879 if ($repeat > 1) {
880 $repeats{$val} = $repeat;
881 }
882 }
Steven Rostedt77d942c2011-05-20 13:36:58 -0400883 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
884 next if ($skip);
885
886 my $lvalue = $1;
887 my $rvalue = $2;
888
889 # process config variables.
890 # Config variables are only active while reading the
891 # config and can be defined anywhere. They also ignore
892 # TEST_START and DEFAULTS, but are skipped if they are in
893 # on of these sections that have SKIP defined.
894 # The save variable can be
895 # defined multiple times and the new one simply overrides
896 # the prevous one.
897 set_variable($lvalue, $rvalue);
898
Steven Rostedta57419b2010-11-02 15:13:54 -0400899 } else {
900 die "$name: $.: Garbage found in config\n$_";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400901 }
902 }
903
Steven Rostedta57419b2010-11-02 15:13:54 -0400904 if ($test_num) {
905 $test_num += $repeat - 1;
906 $opt{"NUM_TESTS"} = $test_num;
907 }
908
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400909 close($in);
910
911 $$current_test_num = $test_num;
912
913 return $test_case;
914}
915
Steven Rostedtc4261d02011-11-23 13:41:18 -0500916sub get_test_case {
917 print "What test case would you like to run?\n";
918 print " (build, install or boot)\n";
919 print " Other tests are available but require editing the config file\n";
920 my $ans = <STDIN>;
921 chomp $ans;
922 $default{"TEST_TYPE"} = $ans;
923}
924
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400925sub read_config {
926 my ($config) = @_;
927
928 my $test_case;
929 my $test_num = 0;
930
931 $test_case = __read_config $config, \$test_num;
932
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500933 # make sure we have all mandatory configs
934 get_ktest_configs;
935
Steven Rostedt0df213c2011-06-14 20:51:37 -0400936 # was a test specified?
937 if (!$test_case) {
938 print "No test case specified.\n";
Steven Rostedtc4261d02011-11-23 13:41:18 -0500939 get_test_case;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400940 }
941
Steven Rostedta75fece2010-11-02 14:58:27 -0400942 # set any defaults
943
944 foreach my $default (keys %default) {
945 if (!defined($opt{$default})) {
946 $opt{$default} = $default{$default};
947 }
948 }
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500949
950 if ($opt{"IGNORE_UNUSED"} == 1) {
951 return;
952 }
953
954 my %not_used;
955
956 # check if there are any stragglers (typos?)
957 foreach my $option (keys %opt) {
958 my $op = $option;
959 # remove per test labels.
960 $op =~ s/\[.*\]//;
961 if (!exists($option_map{$op}) &&
962 !exists($default{$op}) &&
963 !exists($used_options{$op})) {
964 $not_used{$op} = 1;
965 }
966 }
967
968 if (%not_used) {
969 my $s = "s are";
970 $s = " is" if (keys %not_used == 1);
971 print "The following option$s not used; could be a typo:\n";
972 foreach my $option (keys %not_used) {
973 print "$option\n";
974 }
975 print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
976 if (!read_yn "Do you want to continue?") {
977 exit -1;
978 }
979 }
Steven Rostedt2545eb62010-11-02 15:01:32 -0400980}
981
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400982sub __eval_option {
983 my ($option, $i) = @_;
984
985 # Add space to evaluate the character before $
986 $option = " $option";
987 my $retval = "";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530988 my $repeated = 0;
989 my $parent = 0;
990
991 foreach my $test (keys %repeat_tests) {
992 if ($i >= $test &&
993 $i < $test + $repeat_tests{$test}) {
994
995 $repeated = 1;
996 $parent = $test;
997 last;
998 }
999 }
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001000
1001 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
1002 my $start = $1;
1003 my $var = $2;
1004 my $end = $3;
1005
1006 # Append beginning of line
1007 $retval = "$retval$start";
1008
1009 # If the iteration option OPT[$i] exists, then use that.
1010 # otherwise see if the default OPT (without [$i]) exists.
1011
1012 my $o = "$var\[$i\]";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301013 my $parento = "$var\[$parent\]";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001014
1015 if (defined($opt{$o})) {
1016 $o = $opt{$o};
1017 $retval = "$retval$o";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301018 } elsif ($repeated && defined($opt{$parento})) {
1019 $o = $opt{$parento};
1020 $retval = "$retval$o";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001021 } elsif (defined($opt{$var})) {
1022 $o = $opt{$var};
1023 $retval = "$retval$o";
1024 } else {
1025 $retval = "$retval\$\{$var\}";
1026 }
1027
1028 $option = $end;
1029 }
1030
1031 $retval = "$retval$option";
1032
1033 $retval =~ s/^ //;
1034
1035 return $retval;
1036}
1037
1038sub eval_option {
1039 my ($option, $i) = @_;
1040
1041 my $prev = "";
1042
1043 # Since an option can evaluate to another option,
1044 # keep iterating until we do not evaluate any more
1045 # options.
1046 my $r = 0;
1047 while ($prev ne $option) {
1048 # Check for recursive evaluations.
1049 # 100 deep should be more than enough.
1050 if ($r++ > 100) {
1051 die "Over 100 evaluations accurred with $option\n" .
1052 "Check for recursive variables\n";
1053 }
1054 $prev = $option;
1055 $option = __eval_option($option, $i);
1056 }
1057
1058 return $option;
1059}
1060
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001061sub _logit {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001062 if (defined($opt{"LOG_FILE"})) {
1063 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
1064 print OUT @_;
1065 close(OUT);
1066 }
1067}
1068
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001069sub logit {
1070 if (defined($opt{"LOG_FILE"})) {
1071 _logit @_;
1072 } else {
1073 print @_;
1074 }
1075}
1076
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001077sub doprint {
1078 print @_;
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001079 _logit @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001080}
1081
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001082sub run_command;
Andrew Jones2728be42011-08-12 15:32:05 +02001083sub start_monitor;
1084sub end_monitor;
1085sub wait_for_monitor;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001086
1087sub reboot {
Andrew Jones2728be42011-08-12 15:32:05 +02001088 my ($time) = @_;
1089
Steven Rostedt2b803362011-09-30 18:00:23 -04001090 if (defined($time)) {
1091 start_monitor;
1092 # flush out current monitor
1093 # May contain the reboot success line
1094 wait_for_monitor 1;
1095 }
1096
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001097 # try to reboot normally
Steven Rostedte48c5292010-11-02 14:35:37 -04001098 if (run_command $reboot) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001099 if (defined($powercycle_after_reboot)) {
1100 sleep $powercycle_after_reboot;
1101 run_command "$power_cycle";
1102 }
1103 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001104 # nope? power cycle it.
Steven Rostedta75fece2010-11-02 14:58:27 -04001105 run_command "$power_cycle";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001106 }
Andrew Jones2728be42011-08-12 15:32:05 +02001107
1108 if (defined($time)) {
Steven Rostedt2b803362011-09-30 18:00:23 -04001109 wait_for_monitor($time, $reboot_success_line);
Andrew Jones2728be42011-08-12 15:32:05 +02001110 end_monitor;
1111 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001112}
1113
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001114sub reboot_to_good {
1115 my ($time) = @_;
1116
1117 if (defined($switch_to_good)) {
1118 run_command $switch_to_good;
1119 return;
1120 }
1121
1122 reboot $time;
1123}
1124
Steven Rostedt576f6272010-11-02 14:58:38 -04001125sub do_not_reboot {
1126 my $i = $iteration;
1127
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001128 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -04001129 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1130 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1131}
1132
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001133sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001134 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001135
Steven Rostedt576f6272010-11-02 14:58:38 -04001136 my $i = $iteration;
1137
1138 if ($reboot_on_error && !do_not_reboot) {
1139
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001140 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001141 reboot_to_good;
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001142
Steven Rostedta75fece2010-11-02 14:58:27 -04001143 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001144 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001145 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001146 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001147
Steven Rostedtf80802c2011-03-07 13:18:47 -05001148 if (defined($opt{"LOG_FILE"})) {
1149 print " See $opt{LOG_FILE} for more info.\n";
1150 }
1151
Steven Rostedt576f6272010-11-02 14:58:38 -04001152 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001153}
1154
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001155sub open_console {
1156 my ($fp) = @_;
1157
1158 my $flags;
1159
Steven Rostedta75fece2010-11-02 14:58:27 -04001160 my $pid = open($fp, "$console|") or
1161 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001162
1163 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001164 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001165 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001166 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001167
1168 return $pid;
1169}
1170
1171sub close_console {
1172 my ($fp, $pid) = @_;
1173
1174 doprint "kill child process $pid\n";
1175 kill 2, $pid;
1176
1177 print "closing!\n";
1178 close($fp);
1179}
1180
1181sub start_monitor {
1182 if ($monitor_cnt++) {
1183 return;
1184 }
1185 $monitor_fp = \*MONFD;
1186 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001187
1188 return;
1189
1190 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001191}
1192
1193sub end_monitor {
1194 if (--$monitor_cnt) {
1195 return;
1196 }
1197 close_console($monitor_fp, $monitor_pid);
1198}
1199
1200sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001201 my ($time, $stop) = @_;
1202 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001203 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001204 my $booted = 0;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001205
Steven Rostedta75fece2010-11-02 14:58:27 -04001206 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001207
1208 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001209 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001210 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001211 last if (!defined($line));
1212 print "$line";
1213 $full_line .= $line;
1214
1215 if (defined($stop) && $full_line =~ /$stop/) {
1216 doprint "wait for monitor detected $stop\n";
1217 $booted = 1;
1218 }
1219
1220 if ($line =~ /\n/) {
1221 $full_line = "";
1222 }
1223 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001224 print "** Monitor flushed **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001225}
1226
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301227sub save_logs {
1228 my ($result, $basedir) = @_;
1229 my @t = localtime;
1230 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1231 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1232
1233 my $type = $build_type;
1234 if ($type =~ /useconfig/) {
1235 $type = "useconfig";
1236 }
1237
1238 my $dir = "$machine-$test_type-$type-$result-$date";
1239
1240 $dir = "$basedir/$dir";
1241
1242 if (!-d $dir) {
1243 mkpath($dir) or
1244 die "can't create $dir";
1245 }
1246
1247 my %files = (
1248 "config" => $output_config,
1249 "buildlog" => $buildlog,
1250 "dmesg" => $dmesg,
1251 "testlog" => $testlog,
1252 );
1253
1254 while (my ($name, $source) = each(%files)) {
1255 if (-f "$source") {
1256 cp "$source", "$dir/$name" or
1257 die "failed to copy $source";
1258 }
1259 }
1260
1261 doprint "*** Saved info to $dir ***\n";
1262}
1263
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001264sub fail {
1265
Steven Rostedta75fece2010-11-02 14:58:27 -04001266 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001267 dodie @_;
1268 }
1269
Steven Rostedta75fece2010-11-02 14:58:27 -04001270 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001271
Steven Rostedt576f6272010-11-02 14:58:38 -04001272 my $i = $iteration;
1273
Steven Rostedta75fece2010-11-02 14:58:27 -04001274 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001275 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001276 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001277 reboot_to_good $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001278 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001279
Steven Rostedt9064af52011-06-13 10:38:48 -04001280 my $name = "";
1281
1282 if (defined($test_name)) {
1283 $name = " ($test_name)";
1284 }
1285
Steven Rostedt576f6272010-11-02 14:58:38 -04001286 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1287 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001288 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001289 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1290 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001291
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301292 if (defined($store_failures)) {
1293 save_logs "fail", $store_failures;
1294 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001295
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001296 return 1;
1297}
1298
Steven Rostedt2545eb62010-11-02 15:01:32 -04001299sub run_command {
1300 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001301 my $dolog = 0;
1302 my $dord = 0;
1303 my $pid;
1304
Steven Rostedte48c5292010-11-02 14:35:37 -04001305 $command =~ s/\$SSH_USER/$ssh_user/g;
1306 $command =~ s/\$MACHINE/$machine/g;
1307
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001308 doprint("$command ... ");
1309
1310 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001311 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001312
1313 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001314 open(LOG, ">>$opt{LOG_FILE}") or
1315 dodie "failed to write to log";
1316 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001317 }
1318
1319 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001320 open (RD, ">$redirect") or
1321 dodie "failed to write to redirect $redirect";
1322 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001323 }
1324
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001325 while (<CMD>) {
1326 print LOG if ($dolog);
1327 print RD if ($dord);
1328 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001329
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001330 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001331 my $failed = $?;
1332
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001333 close(CMD);
1334 close(LOG) if ($dolog);
1335 close(RD) if ($dord);
1336
Steven Rostedt2545eb62010-11-02 15:01:32 -04001337 if ($failed) {
1338 doprint "FAILED!\n";
1339 } else {
1340 doprint "SUCCESS\n";
1341 }
1342
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001343 return !$failed;
1344}
1345
Steven Rostedte48c5292010-11-02 14:35:37 -04001346sub run_ssh {
1347 my ($cmd) = @_;
1348 my $cp_exec = $ssh_exec;
1349
1350 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1351 return run_command "$cp_exec";
1352}
1353
1354sub run_scp {
Steven Rostedt02ad2612012-03-21 08:21:24 -04001355 my ($src, $dst, $cp_scp) = @_;
Steven Rostedte48c5292010-11-02 14:35:37 -04001356
1357 $cp_scp =~ s/\$SRC_FILE/$src/g;
1358 $cp_scp =~ s/\$DST_FILE/$dst/g;
1359
1360 return run_command "$cp_scp";
1361}
1362
Steven Rostedt02ad2612012-03-21 08:21:24 -04001363sub run_scp_install {
1364 my ($src, $dst) = @_;
1365
1366 my $cp_scp = $scp_to_target_install;
1367
1368 return run_scp($src, $dst, $cp_scp);
1369}
1370
1371sub run_scp_mod {
1372 my ($src, $dst) = @_;
1373
1374 my $cp_scp = $scp_to_target;
1375
1376 return run_scp($src, $dst, $cp_scp);
1377}
1378
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001379sub get_grub_index {
1380
Steven Rostedta75fece2010-11-02 14:58:27 -04001381 if ($reboot_type ne "grub") {
1382 return;
1383 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001384 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001385
1386 doprint "Find grub menu ... ";
1387 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001388
1389 my $ssh_grub = $ssh_exec;
1390 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1391
1392 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001393 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001394
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001395 my $found = 0;
1396
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001397 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001398 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001399 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001400 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001401 last;
1402 } elsif (/^\s*title\s/) {
1403 $grub_number++;
1404 }
1405 }
1406 close(IN);
1407
Steven Rostedta75fece2010-11-02 14:58:27 -04001408 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001409 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001410 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001411}
1412
Steven Rostedt2545eb62010-11-02 15:01:32 -04001413sub wait_for_input
1414{
1415 my ($fp, $time) = @_;
1416 my $rin;
1417 my $ready;
1418 my $line;
1419 my $ch;
1420
1421 if (!defined($time)) {
1422 $time = $timeout;
1423 }
1424
1425 $rin = '';
1426 vec($rin, fileno($fp), 1) = 1;
1427 $ready = select($rin, undef, undef, $time);
1428
1429 $line = "";
1430
1431 # try to read one char at a time
1432 while (sysread $fp, $ch, 1) {
1433 $line .= $ch;
1434 last if ($ch eq "\n");
1435 }
1436
1437 if (!length($line)) {
1438 return undef;
1439 }
1440
1441 return $line;
1442}
1443
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001444sub reboot_to {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001445 if (defined($switch_to_test)) {
1446 run_command $switch_to_test;
1447 }
1448
Steven Rostedta75fece2010-11-02 14:58:27 -04001449 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001450 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001451 } elsif (defined $reboot_script) {
1452 run_command "$reboot_script";
Steven Rostedta75fece2010-11-02 14:58:27 -04001453 }
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001454 reboot;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001455}
1456
Steven Rostedta57419b2010-11-02 15:13:54 -04001457sub get_sha1 {
1458 my ($commit) = @_;
1459
1460 doprint "git rev-list --max-count=1 $commit ... ";
1461 my $sha1 = `git rev-list --max-count=1 $commit`;
1462 my $ret = $?;
1463
1464 logit $sha1;
1465
1466 if ($ret) {
1467 doprint "FAILED\n";
1468 dodie "Failed to get git $commit";
1469 }
1470
1471 print "SUCCESS\n";
1472
1473 chomp $sha1;
1474
1475 return $sha1;
1476}
1477
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001478sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001479 my $booted = 0;
1480 my $bug = 0;
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001481 my $bug_ignored = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001482 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001483 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001484
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001485 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001486
1487 my $line;
1488 my $full_line = "";
1489
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001490 open(DMESG, "> $dmesg") or
1491 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001492
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001493 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001494
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001495 my $success_start;
1496 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001497 my $monitor_start = time;
1498 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001499 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001500
Steven Rostedt2d01b262011-03-08 09:47:54 -05001501 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001502
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001503 if ($bug && defined($stop_after_failure) &&
1504 $stop_after_failure >= 0) {
1505 my $time = $stop_after_failure - (time - $failure_start);
1506 $line = wait_for_input($monitor_fp, $time);
1507 if (!defined($line)) {
1508 doprint "bug timed out after $booted_timeout seconds\n";
1509 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1510 last;
1511 }
1512 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001513 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001514 if (!defined($line)) {
1515 my $s = $booted_timeout == 1 ? "" : "s";
1516 doprint "Successful boot found: break after $booted_timeout second$s\n";
1517 last;
1518 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001519 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001520 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001521 if (!defined($line)) {
1522 my $s = $timeout == 1 ? "" : "s";
1523 doprint "Timed out after $timeout second$s\n";
1524 last;
1525 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001526 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001527
Steven Rostedt2545eb62010-11-02 15:01:32 -04001528 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001529 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001530
1531 # we are not guaranteed to get a full line
1532 $full_line .= $line;
1533
Steven Rostedta75fece2010-11-02 14:58:27 -04001534 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001535 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001536 $success_start = time;
1537 }
1538
1539 if ($booted && defined($stop_after_success) &&
1540 $stop_after_success >= 0) {
1541 my $now = time;
1542 if ($now - $success_start >= $stop_after_success) {
1543 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1544 last;
1545 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001546 }
1547
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001548 if ($full_line =~ /\[ backtrace testing \]/) {
1549 $skip_call_trace = 1;
1550 }
1551
Steven Rostedt2545eb62010-11-02 15:01:32 -04001552 if ($full_line =~ /call trace:/i) {
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001553 if (!$bug && !$skip_call_trace) {
1554 if ($ignore_errors) {
1555 $bug_ignored = 1;
1556 } else {
1557 $bug = 1;
1558 $failure_start = time;
1559 }
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001560 }
1561 }
1562
1563 if ($bug && defined($stop_after_failure) &&
1564 $stop_after_failure >= 0) {
1565 my $now = time;
1566 if ($now - $failure_start >= $stop_after_failure) {
1567 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1568 last;
1569 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001570 }
1571
1572 if ($full_line =~ /\[ end of backtrace testing \]/) {
1573 $skip_call_trace = 0;
1574 }
1575
1576 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001577 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001578 $bug = 1;
1579 }
1580
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001581 # Detect triple faults by testing the banner
1582 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1583 if ($1 eq $version) {
1584 $version_found = 1;
1585 } elsif ($version_found && $detect_triplefault) {
1586 # We already booted into the kernel we are testing,
1587 # but now we booted into another kernel?
1588 # Consider this a triple fault.
1589 doprint "Aleady booted in Linux kernel $version, but now\n";
1590 doprint "we booted into Linux kernel $1.\n";
1591 doprint "Assuming that this is a triple fault.\n";
1592 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1593 last;
1594 }
1595 }
1596
Steven Rostedt2545eb62010-11-02 15:01:32 -04001597 if ($line =~ /\n/) {
1598 $full_line = "";
1599 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001600
1601 if ($stop_test_after > 0 && !$booted && !$bug) {
1602 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001603 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001604 $done = 1;
1605 }
1606 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001607 }
1608
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001609 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001610
Steven Rostedt2545eb62010-11-02 15:01:32 -04001611 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001612 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001613 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001614 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001615
Steven Rostedta75fece2010-11-02 14:58:27 -04001616 if (!$booted) {
1617 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001618 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001619 }
1620
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001621 if ($bug_ignored) {
1622 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
1623 }
1624
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001625 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001626}
1627
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001628sub eval_kernel_version {
1629 my ($option) = @_;
1630
1631 $option =~ s/\$KERNEL_VERSION/$version/g;
1632
1633 return $option;
1634}
1635
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001636sub do_post_install {
1637
1638 return if (!defined($post_install));
1639
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001640 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001641 run_command "$cp_post_install" or
1642 dodie "Failed to run post install";
1643}
1644
Steven Rostedt2545eb62010-11-02 15:01:32 -04001645sub install {
1646
Steven Rostedte0a87422011-09-30 17:50:48 -04001647 return if ($no_install);
1648
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001649 my $cp_target = eval_kernel_version $target_image;
1650
Steven Rostedt02ad2612012-03-21 08:21:24 -04001651 run_scp_install "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001652 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001653
1654 my $install_mods = 0;
1655
1656 # should we process modules?
1657 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001658 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001659 while (<IN>) {
1660 if (/CONFIG_MODULES(=y)?/) {
1661 $install_mods = 1 if (defined($1));
1662 last;
1663 }
1664 }
1665 close(IN);
1666
1667 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001668 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001669 doprint "No modules needed\n";
1670 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001671 }
1672
Steven Rostedt627977d2012-03-21 08:16:15 -04001673 run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001674 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001675
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001676 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001677 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001678
Steven Rostedte48c5292010-11-02 14:35:37 -04001679 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001680 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001681
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001682 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001683 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001684 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001685
Steven Rostedt02ad2612012-03-21 08:21:24 -04001686 run_scp_mod "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001687 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001688
Steven Rostedta75fece2010-11-02 14:58:27 -04001689 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001690
Steven Rostedte7b13442011-06-14 20:44:36 -04001691 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001692 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001693
Steven Rostedte48c5292010-11-02 14:35:37 -04001694 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001695
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001696 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001697}
1698
Steven Rostedtddf607e2011-06-14 20:49:13 -04001699sub get_version {
1700 # get the release name
1701 doprint "$make kernelrelease ... ";
1702 $version = `$make kernelrelease | tail -1`;
1703 chomp($version);
1704 doprint "$version\n";
1705}
1706
1707sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001708 # Make sure the stable kernel has finished booting
1709 start_monitor;
1710 wait_for_monitor 5;
1711 end_monitor;
1712
Steven Rostedtddf607e2011-06-14 20:49:13 -04001713 get_grub_index;
1714 get_version;
1715 install;
1716
1717 start_monitor;
1718 return monitor;
1719}
1720
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001721sub check_buildlog {
1722 my ($patch) = @_;
1723
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001724 my @files = `git show $patch | diffstat -l`;
1725
1726 open(IN, "git show $patch |") or
1727 dodie "failed to show $patch";
1728 while (<IN>) {
1729 if (m,^--- a/(.*),) {
1730 chomp $1;
1731 $files[$#files] = $1;
1732 }
1733 }
1734 close(IN);
1735
1736 open(IN, $buildlog) or dodie "Can't open $buildlog";
1737 while (<IN>) {
1738 if (/^\s*(.*?):.*(warning|error)/) {
1739 my $err = $1;
1740 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001741 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001742 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001743 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001744 }
1745 }
1746 }
1747 }
1748 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001749
1750 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001751}
1752
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001753sub apply_min_config {
1754 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05001755
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001756 # Read the config file and remove anything that
1757 # is in the force_config hash (from minconfig and others)
1758 # then add the force config back.
1759
1760 doprint "Applying minimum configurations into $output_config.new\n";
1761
1762 open (OUT, ">$outconfig") or
1763 dodie "Can't create $outconfig";
1764
1765 if (-f $output_config) {
1766 open (IN, $output_config) or
1767 dodie "Failed to open $output_config";
1768 while (<IN>) {
1769 if (/^(# )?(CONFIG_[^\s=]*)/) {
1770 next if (defined($force_config{$2}));
1771 }
1772 print OUT;
1773 }
1774 close IN;
1775 }
1776 foreach my $config (keys %force_config) {
1777 print OUT "$force_config{$config}\n";
1778 }
1779 close OUT;
1780
1781 run_command "mv $outconfig $output_config";
1782}
1783
1784sub make_oldconfig {
1785
Steven Rostedt4c4ab122011-07-15 21:16:17 -04001786 my @force_list = keys %force_config;
1787
1788 if ($#force_list >= 0) {
1789 apply_min_config;
1790 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001791
1792 if (!run_command "$make oldnoconfig") {
Steven Rostedt612b9e92011-03-07 13:27:43 -05001793 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1794 # try a yes '' | oldconfig
1795 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001796 run_command "yes '' | $make oldconfig" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001797 dodie "failed make config oldconfig";
1798 }
1799}
1800
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001801# read a config file and use this to force new configs.
1802sub load_force_config {
1803 my ($config) = @_;
1804
1805 open(IN, $config) or
1806 dodie "failed to read $config";
1807 while (<IN>) {
1808 chomp;
1809 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1810 $force_config{$1} = $_;
1811 } elsif (/^# (CONFIG_\S*) is not set/) {
1812 $force_config{$1} = $_;
1813 }
1814 }
1815 close IN;
1816}
1817
Steven Rostedt2545eb62010-11-02 15:01:32 -04001818sub build {
1819 my ($type) = @_;
1820
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001821 unlink $buildlog;
1822
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001823 # Failed builds should not reboot the target
1824 my $save_no_reboot = $no_reboot;
1825 $no_reboot = 1;
1826
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001827 if (defined($pre_build)) {
1828 my $ret = run_command $pre_build;
1829 if (!$ret && defined($pre_build_die) &&
1830 $pre_build_die) {
1831 dodie "failed to pre_build\n";
1832 }
1833 }
1834
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001835 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001836 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001837 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001838
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001839 $type = "oldconfig";
1840 }
1841
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001842 # old config can ask questions
1843 if ($type eq "oldconfig") {
Steven Rostedt9386c6a2010-11-08 16:35:48 -05001844 $type = "oldnoconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001845
1846 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001847 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001848
Andrew Jones13488232011-08-12 15:32:04 +02001849 if (!$noclean) {
1850 run_command "mv $output_config $outputdir/config_temp" or
1851 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001852
Andrew Jones13488232011-08-12 15:32:04 +02001853 run_command "$make mrproper" or dodie "make mrproper";
1854
1855 run_command "mv $outputdir/config_temp $output_config" or
1856 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001857 }
1858
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001859 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001860 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001861 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001862 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001863 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001864
1865 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04001866 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1867 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001868 close(OUT);
1869
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001870 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001871 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001872 }
1873
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001874 if ($type ne "oldnoconfig") {
1875 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001876 dodie "failed make config";
1877 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001878 # Run old config regardless, to enforce min configurations
1879 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001880
Steven Rostedta75fece2010-11-02 14:58:27 -04001881 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001882 my $build_ret = run_command "$make $build_options";
1883 undef $redirect;
1884
1885 if (defined($post_build)) {
1886 my $ret = run_command $post_build;
1887 if (!$ret && defined($post_build_die) &&
1888 $post_build_die) {
1889 dodie "failed to post_build\n";
1890 }
1891 }
1892
1893 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001894 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001895 if ($in_bisect) {
1896 $no_reboot = $save_no_reboot;
1897 return 0;
1898 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001899 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001900 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001901
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001902 $no_reboot = $save_no_reboot;
1903
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001904 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001905}
1906
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001907sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04001908 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001909 if (defined($poweroff_after_halt)) {
1910 sleep $poweroff_after_halt;
1911 run_command "$power_off";
1912 }
1913 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001914 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04001915 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001916 }
1917}
1918
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001919sub success {
1920 my ($i) = @_;
1921
Steven Rostedte48c5292010-11-02 14:35:37 -04001922 $successes++;
1923
Steven Rostedt9064af52011-06-13 10:38:48 -04001924 my $name = "";
1925
1926 if (defined($test_name)) {
1927 $name = " ($test_name)";
1928 }
1929
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001930 doprint "\n\n*******************************************\n";
1931 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001932 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001933 doprint "*******************************************\n";
1934 doprint "*******************************************\n";
1935
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301936 if (defined($store_successes)) {
1937 save_logs "success", $store_successes;
1938 }
1939
Steven Rostedt576f6272010-11-02 14:58:38 -04001940 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001941 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001942 reboot_to_good $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001943 }
1944}
1945
Steven Rostedtc960bb92011-03-08 09:22:39 -05001946sub answer_bisect {
1947 for (;;) {
1948 doprint "Pass or fail? [p/f]";
1949 my $ans = <STDIN>;
1950 chomp $ans;
1951 if ($ans eq "p" || $ans eq "P") {
1952 return 1;
1953 } elsif ($ans eq "f" || $ans eq "F") {
1954 return 0;
1955 } else {
1956 print "Please answer 'P' or 'F'\n";
1957 }
1958 }
1959}
1960
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001961sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001962 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001963
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001964 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04001965 $reboot_on_error = 0;
1966 $poweroff_on_error = 0;
1967 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001968
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301969 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001970 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301971 undef $redirect;
1972
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001973 exit $failed;
1974}
1975
1976my $child_done;
1977
1978sub child_finished {
1979 $child_done = 1;
1980}
1981
1982sub do_run_test {
1983 my $child_pid;
1984 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001985 my $line;
1986 my $full_line;
1987 my $bug = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001988
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001989 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001990
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001991 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001992
1993 $child_done = 0;
1994
1995 $SIG{CHLD} = qw(child_finished);
1996
1997 $child_pid = fork;
1998
1999 child_run_test if (!$child_pid);
2000
2001 $full_line = "";
2002
2003 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002004 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002005 if (defined($line)) {
2006
2007 # we are not guaranteed to get a full line
2008 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002009 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002010
2011 if ($full_line =~ /call trace:/i) {
2012 $bug = 1;
2013 }
2014
2015 if ($full_line =~ /Kernel panic -/) {
2016 $bug = 1;
2017 }
2018
2019 if ($line =~ /\n/) {
2020 $full_line = "";
2021 }
2022 }
2023 } while (!$child_done && !$bug);
2024
2025 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002026 my $failure_start = time;
2027 my $now;
2028 do {
2029 $line = wait_for_input($monitor_fp, 1);
2030 if (defined($line)) {
2031 doprint $line;
2032 }
2033 $now = time;
2034 if ($now - $failure_start >= $stop_after_failure) {
2035 last;
2036 }
2037 } while (defined($line));
2038
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002039 doprint "Detected kernel crash!\n";
2040 # kill the child with extreme prejudice
2041 kill 9, $child_pid;
2042 }
2043
2044 waitpid $child_pid, 0;
2045 $child_exit = $?;
2046
Steven Rostedtc5dacb82011-12-22 12:43:57 -05002047 if (!$bug && $in_bisect) {
2048 if (defined($bisect_ret_good)) {
2049 if ($child_exit == $bisect_ret_good) {
2050 return 1;
2051 }
2052 }
2053 if (defined($bisect_ret_skip)) {
2054 if ($child_exit == $bisect_ret_skip) {
2055 return -1;
2056 }
2057 }
2058 if (defined($bisect_ret_abort)) {
2059 if ($child_exit == $bisect_ret_abort) {
2060 fail "test abort" and return -2;
2061 }
2062 }
2063 if (defined($bisect_ret_bad)) {
2064 if ($child_exit == $bisect_ret_skip) {
2065 return 0;
2066 }
2067 }
2068 if (defined($bisect_ret_default)) {
2069 if ($bisect_ret_default eq "good") {
2070 return 1;
2071 } elsif ($bisect_ret_default eq "bad") {
2072 return 0;
2073 } elsif ($bisect_ret_default eq "skip") {
2074 return -1;
2075 } elsif ($bisect_ret_default eq "abort") {
2076 return -2;
2077 } else {
2078 fail "unknown default action: $bisect_ret_default"
2079 and return -2;
2080 }
2081 }
2082 }
2083
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002084 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002085 return 0 if $in_bisect;
2086 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002087 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002088 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002089}
2090
Steven Rostedta75fece2010-11-02 14:58:27 -04002091sub run_git_bisect {
2092 my ($command) = @_;
2093
2094 doprint "$command ... ";
2095
2096 my $output = `$command 2>&1`;
2097 my $ret = $?;
2098
2099 logit $output;
2100
2101 if ($ret) {
2102 doprint "FAILED\n";
2103 dodie "Failed to git bisect";
2104 }
2105
2106 doprint "SUCCESS\n";
2107 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2108 doprint "$1 [$2]\n";
2109 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002110 $bisect_bad_commit = $1;
Steven Rostedta75fece2010-11-02 14:58:27 -04002111 doprint "Found bad commit... $1\n";
2112 return 0;
2113 } else {
2114 # we already logged it, just print it now.
2115 print $output;
2116 }
2117
2118 return 1;
2119}
2120
Steven Rostedtc23dca72011-03-08 09:26:31 -05002121sub bisect_reboot {
2122 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002123 reboot_to_good $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05002124}
2125
2126# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05002127sub run_bisect_test {
2128 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002129
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002130 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002131 my $result;
2132 my $output;
2133 my $ret;
2134
Steven Rostedt0a05c762010-11-08 11:14:10 -05002135 $in_bisect = 1;
2136
2137 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002138
2139 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002140 if ($failed && $bisect_skip) {
2141 $in_bisect = 0;
2142 return -1;
2143 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002144 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002145
2146 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04002147 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002148
2149 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002150 if ($failed && $bisect_skip) {
2151 end_monitor;
2152 bisect_reboot;
2153 $in_bisect = 0;
2154 return -1;
2155 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002156 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002157
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002158 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002159 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002160 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002161 }
2162
2163 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002164 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002165 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002166 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002167 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04002168
2169 # reboot the box to a kernel we can ssh to
2170 if ($type ne "build") {
2171 bisect_reboot;
2172 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002173 $in_bisect = 0;
2174
2175 return $result;
2176}
2177
2178sub run_bisect {
2179 my ($type) = @_;
2180 my $buildtype = "oldconfig";
2181
2182 # We should have a minconfig to use?
2183 if (defined($minconfig)) {
2184 $buildtype = "useconfig:$minconfig";
2185 }
2186
2187 my $ret = run_bisect_test $type, $buildtype;
2188
Steven Rostedtc960bb92011-03-08 09:22:39 -05002189 if ($bisect_manual) {
2190 $ret = answer_bisect;
2191 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002192
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002193 # Are we looking for where it worked, not failed?
2194 if ($reverse_bisect) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002195 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002196 }
2197
Steven Rostedtc23dca72011-03-08 09:26:31 -05002198 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002199 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002200 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002201 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002202 } elsif ($bisect_skip) {
2203 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2204 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002205 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002206}
2207
Steven Rostedtdad98752011-11-22 20:48:57 -05002208sub update_bisect_replay {
2209 my $tmp_log = "$tmpdir/ktest_bisect_log";
2210 run_command "git bisect log > $tmp_log" or
2211 die "can't create bisect log";
2212 return $tmp_log;
2213}
2214
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002215sub bisect {
2216 my ($i) = @_;
2217
2218 my $result;
2219
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002220 die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2221 die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
2222 die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002223
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002224 my $good = $bisect_good;
2225 my $bad = $bisect_bad;
2226 my $type = $bisect_type;
2227 my $start = $bisect_start;
2228 my $replay = $bisect_replay;
2229 my $start_files = $bisect_files;
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002230
2231 if (defined($start_files)) {
2232 $start_files = " -- " . $start_files;
2233 } else {
2234 $start_files = "";
2235 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002236
Steven Rostedta57419b2010-11-02 15:13:54 -04002237 # convert to true sha1's
2238 $good = get_sha1($good);
2239 $bad = get_sha1($bad);
2240
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002241 if (defined($bisect_reverse) && $bisect_reverse == 1) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002242 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2243 $reverse_bisect = 1;
2244 } else {
2245 $reverse_bisect = 0;
2246 }
2247
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002248 # Can't have a test without having a test to run
2249 if ($type eq "test" && !defined($run_test)) {
2250 $type = "boot";
2251 }
2252
Steven Rostedtdad98752011-11-22 20:48:57 -05002253 # Check if a bisect was running
2254 my $bisect_start_file = "$builddir/.git/BISECT_START";
2255
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002256 my $check = $bisect_check;
Steven Rostedtdad98752011-11-22 20:48:57 -05002257 my $do_check = defined($check) && $check ne "0";
2258
2259 if ( -f $bisect_start_file ) {
2260 print "Bisect in progress found\n";
2261 if ($do_check) {
2262 print " If you say yes, then no checks of good or bad will be done\n";
2263 }
2264 if (defined($replay)) {
2265 print "** BISECT_REPLAY is defined in config file **";
2266 print " Ignore config option and perform new git bisect log?\n";
2267 if (read_ync " (yes, no, or cancel) ") {
2268 $replay = update_bisect_replay;
2269 $do_check = 0;
2270 }
2271 } elsif (read_yn "read git log and continue?") {
2272 $replay = update_bisect_replay;
2273 $do_check = 0;
2274 }
2275 }
2276
2277 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002278
2279 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002280 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002281
2282 if ($check ne "good") {
2283 doprint "TESTING BISECT BAD [$bad]\n";
2284 run_command "git checkout $bad" or
2285 die "Failed to checkout $bad";
2286
2287 $result = run_bisect $type;
2288
2289 if ($result ne "bad") {
2290 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2291 }
2292 }
2293
2294 if ($check ne "bad") {
2295 doprint "TESTING BISECT GOOD [$good]\n";
2296 run_command "git checkout $good" or
2297 die "Failed to checkout $good";
2298
2299 $result = run_bisect $type;
2300
2301 if ($result ne "good") {
2302 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2303 }
2304 }
2305
2306 # checkout where we started
2307 run_command "git checkout $head" or
2308 die "Failed to checkout $head";
2309 }
2310
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002311 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002312 dodie "could not start bisect";
2313
2314 run_command "git bisect good $good" or
2315 dodie "could not set bisect good to $good";
2316
2317 run_git_bisect "git bisect bad $bad" or
2318 dodie "could not set bisect bad to $bad";
2319
2320 if (defined($replay)) {
2321 run_command "git bisect replay $replay" or
2322 dodie "failed to run replay";
2323 }
2324
2325 if (defined($start)) {
2326 run_command "git checkout $start" or
2327 dodie "failed to checkout $start";
2328 }
2329
2330 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002331 do {
2332 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002333 $test = run_git_bisect "git bisect $result";
2334 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002335
2336 run_command "git bisect log" or
2337 dodie "could not capture git bisect log";
2338
2339 run_command "git bisect reset" or
2340 dodie "could not reset git bisect";
2341
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002342 doprint "Bad commit was [$bisect_bad_commit]\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002343
Steven Rostedt0a05c762010-11-08 11:14:10 -05002344 success $i;
2345}
2346
2347my %config_ignore;
2348my %config_set;
2349
2350my %config_list;
2351my %null_config;
2352
2353my %dependency;
2354
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002355sub assign_configs {
2356 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002357
2358 open (IN, $config)
2359 or dodie "Failed to read $config";
2360
2361 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002362 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002363 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002364 }
2365 }
2366
2367 close(IN);
2368}
2369
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002370sub process_config_ignore {
2371 my ($config) = @_;
2372
2373 assign_configs \%config_ignore, $config;
2374}
2375
Steven Rostedt0a05c762010-11-08 11:14:10 -05002376sub read_current_config {
2377 my ($config_ref) = @_;
2378
2379 %{$config_ref} = ();
2380 undef %{$config_ref};
2381
2382 my @key = keys %{$config_ref};
2383 if ($#key >= 0) {
2384 print "did not delete!\n";
2385 exit;
2386 }
2387 open (IN, "$output_config");
2388
2389 while (<IN>) {
2390 if (/^(CONFIG\S+)=(.*)/) {
2391 ${$config_ref}{$1} = $2;
2392 }
2393 }
2394 close(IN);
2395}
2396
2397sub get_dependencies {
2398 my ($config) = @_;
2399
2400 my $arr = $dependency{$config};
2401 if (!defined($arr)) {
2402 return ();
2403 }
2404
2405 my @deps = @{$arr};
2406
2407 foreach my $dep (@{$arr}) {
2408 print "ADD DEP $dep\n";
2409 @deps = (@deps, get_dependencies $dep);
2410 }
2411
2412 return @deps;
2413}
2414
2415sub create_config {
2416 my @configs = @_;
2417
2418 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2419
2420 foreach my $config (@configs) {
2421 print OUT "$config_set{$config}\n";
2422 my @deps = get_dependencies $config;
2423 foreach my $dep (@deps) {
2424 print OUT "$config_set{$dep}\n";
2425 }
2426 }
2427
2428 foreach my $config (keys %config_ignore) {
2429 print OUT "$config_ignore{$config}\n";
2430 }
2431 close(OUT);
2432
2433# exit;
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002434 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002435}
2436
2437sub compare_configs {
2438 my (%a, %b) = @_;
2439
2440 foreach my $item (keys %a) {
2441 if (!defined($b{$item})) {
2442 print "diff $item\n";
2443 return 1;
2444 }
2445 delete $b{$item};
2446 }
2447
2448 my @keys = keys %b;
2449 if ($#keys) {
2450 print "diff2 $keys[0]\n";
2451 }
2452 return -1 if ($#keys >= 0);
2453
2454 return 0;
2455}
2456
2457sub run_config_bisect_test {
2458 my ($type) = @_;
2459
2460 return run_bisect_test $type, "oldconfig";
2461}
2462
2463sub process_passed {
2464 my (%configs) = @_;
2465
2466 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2467 # Passed! All these configs are part of a good compile.
2468 # Add them to the min options.
2469 foreach my $config (keys %configs) {
2470 if (defined($config_list{$config})) {
2471 doprint " removing $config\n";
2472 $config_ignore{$config} = $config_list{$config};
2473 delete $config_list{$config};
2474 }
2475 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002476 doprint "config copied to $outputdir/config_good\n";
2477 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002478}
2479
2480sub process_failed {
2481 my ($config) = @_;
2482
2483 doprint "\n\n***************************************\n";
2484 doprint "Found bad config: $config\n";
2485 doprint "***************************************\n\n";
2486}
2487
2488sub run_config_bisect {
2489
2490 my @start_list = keys %config_list;
2491
2492 if ($#start_list < 0) {
2493 doprint "No more configs to test!!!\n";
2494 return -1;
2495 }
2496
2497 doprint "***** RUN TEST ***\n";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002498 my $type = $config_bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002499 my $ret;
2500 my %current_config;
2501
2502 my $count = $#start_list + 1;
2503 doprint " $count configs to test\n";
2504
2505 my $half = int($#start_list / 2);
2506
2507 do {
2508 my @tophalf = @start_list[0 .. $half];
2509
2510 create_config @tophalf;
2511 read_current_config \%current_config;
2512
2513 $count = $#tophalf + 1;
2514 doprint "Testing $count configs\n";
2515 my $found = 0;
2516 # make sure we test something
2517 foreach my $config (@tophalf) {
2518 if (defined($current_config{$config})) {
2519 logit " $config\n";
2520 $found = 1;
2521 }
2522 }
2523 if (!$found) {
2524 # try the other half
2525 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002526 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedt0a05c762010-11-08 11:14:10 -05002527 create_config @tophalf;
2528 read_current_config \%current_config;
2529 foreach my $config (@tophalf) {
2530 if (defined($current_config{$config})) {
2531 logit " $config\n";
2532 $found = 1;
2533 }
2534 }
2535 if (!$found) {
2536 doprint "Failed: Can't make new config with current configs\n";
2537 foreach my $config (@start_list) {
2538 doprint " CONFIG: $config\n";
2539 }
2540 return -1;
2541 }
2542 $count = $#tophalf + 1;
2543 doprint "Testing $count configs\n";
2544 }
2545
2546 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002547 if ($bisect_manual) {
2548 $ret = answer_bisect;
2549 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002550 if ($ret) {
2551 process_passed %current_config;
2552 return 0;
2553 }
2554
2555 doprint "This config had a failure.\n";
2556 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002557 doprint "config copied to $outputdir/config_bad\n";
2558 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002559
2560 # A config exists in this group that was bad.
2561 foreach my $config (keys %config_list) {
2562 if (!defined($current_config{$config})) {
2563 doprint " removing $config\n";
2564 delete $config_list{$config};
2565 }
2566 }
2567
2568 @start_list = @tophalf;
2569
2570 if ($#start_list == 0) {
2571 process_failed $start_list[0];
2572 return 1;
2573 }
2574
2575 # remove half the configs we are looking at and see if
2576 # they are good.
2577 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002578 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002579
Steven Rostedtc960bb92011-03-08 09:22:39 -05002580 # we found a single config, try it again unless we are running manually
2581
2582 if ($bisect_manual) {
2583 process_failed $start_list[0];
2584 return 1;
2585 }
2586
Steven Rostedt0a05c762010-11-08 11:14:10 -05002587 my @tophalf = @start_list[0 .. 0];
2588
2589 $ret = run_config_bisect_test $type;
2590 if ($ret) {
2591 process_passed %current_config;
2592 return 0;
2593 }
2594
2595 process_failed $start_list[0];
2596 return 1;
2597}
2598
2599sub config_bisect {
2600 my ($i) = @_;
2601
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002602 my $start_config = $config_bisect;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002603
2604 my $tmpconfig = "$tmpdir/use_config";
2605
Steven Rostedt30f75da2011-06-13 10:35:35 -04002606 if (defined($config_bisect_good)) {
2607 process_config_ignore $config_bisect_good;
2608 }
2609
Steven Rostedt0a05c762010-11-08 11:14:10 -05002610 # Make the file with the bad config and the min config
2611 if (defined($minconfig)) {
2612 # read the min config for things to ignore
2613 run_command "cp $minconfig $tmpconfig" or
2614 dodie "failed to copy $minconfig to $tmpconfig";
2615 } else {
2616 unlink $tmpconfig;
2617 }
2618
Steven Rostedt0a05c762010-11-08 11:14:10 -05002619 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002620 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002621 process_config_ignore $tmpconfig;
2622 }
2623
2624 # now process the start config
2625 run_command "cp $start_config $output_config" or
2626 dodie "failed to copy $start_config to $output_config";
2627
2628 # read directly what we want to check
2629 my %config_check;
2630 open (IN, $output_config)
2631 or dodie "faied to open $output_config";
2632
2633 while (<IN>) {
2634 if (/^((CONFIG\S*)=.*)/) {
2635 $config_check{$2} = $1;
2636 }
2637 }
2638 close(IN);
2639
Steven Rostedt250bae82011-07-15 22:05:59 -04002640 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002641 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002642
2643 # check to see what we lost (or gained)
2644 open (IN, $output_config)
2645 or dodie "Failed to read $start_config";
2646
2647 my %removed_configs;
2648 my %added_configs;
2649
2650 while (<IN>) {
2651 if (/^((CONFIG\S*)=.*)/) {
2652 # save off all options
2653 $config_set{$2} = $1;
2654 if (defined($config_check{$2})) {
2655 if (defined($config_ignore{$2})) {
2656 $removed_configs{$2} = $1;
2657 } else {
2658 $config_list{$2} = $1;
2659 }
2660 } elsif (!defined($config_ignore{$2})) {
2661 $added_configs{$2} = $1;
2662 $config_list{$2} = $1;
2663 }
2664 }
2665 }
2666 close(IN);
2667
2668 my @confs = keys %removed_configs;
2669 if ($#confs >= 0) {
2670 doprint "Configs overridden by default configs and removed from check:\n";
2671 foreach my $config (@confs) {
2672 doprint " $config\n";
2673 }
2674 }
2675 @confs = keys %added_configs;
2676 if ($#confs >= 0) {
2677 doprint "Configs appearing in make oldconfig and added:\n";
2678 foreach my $config (@confs) {
2679 doprint " $config\n";
2680 }
2681 }
2682
2683 my %config_test;
2684 my $once = 0;
2685
2686 # Sometimes kconfig does weird things. We must make sure
2687 # that the config we autocreate has everything we need
2688 # to test, otherwise we may miss testing configs, or
2689 # may not be able to create a new config.
2690 # Here we create a config with everything set.
2691 create_config (keys %config_list);
2692 read_current_config \%config_test;
2693 foreach my $config (keys %config_list) {
2694 if (!defined($config_test{$config})) {
2695 if (!$once) {
2696 $once = 1;
2697 doprint "Configs not produced by kconfig (will not be checked):\n";
2698 }
2699 doprint " $config\n";
2700 delete $config_list{$config};
2701 }
2702 }
2703 my $ret;
2704 do {
2705 $ret = run_config_bisect;
2706 } while (!$ret);
2707
2708 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002709
2710 success $i;
2711}
2712
Steven Rostedt27d934b2011-05-20 09:18:18 -04002713sub patchcheck_reboot {
2714 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002715 reboot_to_good $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04002716}
2717
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002718sub patchcheck {
2719 my ($i) = @_;
2720
2721 die "PATCHCHECK_START[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002722 if (!defined($patchcheck_start));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002723 die "PATCHCHECK_TYPE[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002724 if (!defined($patchcheck_type));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002725
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002726 my $start = $patchcheck_start;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002727
2728 my $end = "HEAD";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002729 if (defined($patchcheck_end)) {
2730 $end = $patchcheck_end;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002731 }
2732
Steven Rostedta57419b2010-11-02 15:13:54 -04002733 # Get the true sha1's since we can use things like HEAD~3
2734 $start = get_sha1($start);
2735 $end = get_sha1($end);
2736
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002737 my $type = $patchcheck_type;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002738
2739 # Can't have a test without having a test to run
2740 if ($type eq "test" && !defined($run_test)) {
2741 $type = "boot";
2742 }
2743
2744 open (IN, "git log --pretty=oneline $end|") or
2745 dodie "could not get git list";
2746
2747 my @list;
2748
2749 while (<IN>) {
2750 chomp;
2751 $list[$#list+1] = $_;
2752 last if (/^$start/);
2753 }
2754 close(IN);
2755
2756 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002757 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002758 }
2759
2760 # go backwards in the list
2761 @list = reverse @list;
2762
2763 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04002764 my %ignored_warnings;
2765
2766 if (defined($ignore_warnings)) {
2767 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2768 $ignored_warnings{$sha1} = 1;
2769 }
2770 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002771
2772 $in_patchcheck = 1;
2773 foreach my $item (@list) {
2774 my $sha1 = $item;
2775 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2776
2777 doprint "\nProcessing commit $item\n\n";
2778
2779 run_command "git checkout $sha1" or
2780 die "Failed to checkout $sha1";
2781
2782 # only clean on the first and last patch
2783 if ($item eq $list[0] ||
2784 $item eq $list[$#list]) {
2785 $noclean = $save_clean;
2786 } else {
2787 $noclean = 1;
2788 }
2789
2790 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002791 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002792 } else {
2793 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002794 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002795 }
2796
Steven Rostedt19902072011-06-14 20:46:25 -04002797
2798 if (!defined($ignored_warnings{$sha1})) {
2799 check_buildlog $sha1 or return 0;
2800 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002801
2802 next if ($type eq "build");
2803
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002804 my $failed = 0;
2805
Steven Rostedtddf607e2011-06-14 20:49:13 -04002806 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002807
2808 if (!$failed && $type ne "boot"){
2809 do_run_test or $failed = 1;
2810 }
2811 end_monitor;
2812 return 0 if ($failed);
2813
Steven Rostedt27d934b2011-05-20 09:18:18 -04002814 patchcheck_reboot;
2815
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002816 }
2817 $in_patchcheck = 0;
2818 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002819
2820 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002821}
2822
Steven Rostedtb9066f62011-07-15 21:25:24 -04002823my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04002824my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002825my $iflevel = 0;
2826my @ifdeps;
2827
2828# prevent recursion
2829my %read_kconfigs;
2830
Steven Rostedtac6974c2011-10-04 09:40:17 -04002831sub add_dep {
2832 # $config depends on $dep
2833 my ($config, $dep) = @_;
2834
2835 if (defined($depends{$config})) {
2836 $depends{$config} .= " " . $dep;
2837 } else {
2838 $depends{$config} = $dep;
2839 }
2840
2841 # record the number of configs depending on $dep
2842 if (defined $depcount{$dep}) {
2843 $depcount{$dep}++;
2844 } else {
2845 $depcount{$dep} = 1;
2846 }
2847}
2848
Steven Rostedtb9066f62011-07-15 21:25:24 -04002849# taken from streamline_config.pl
2850sub read_kconfig {
2851 my ($kconfig) = @_;
2852
2853 my $state = "NONE";
2854 my $config;
2855 my @kconfigs;
2856
2857 my $cont = 0;
2858 my $line;
2859
2860
2861 if (! -f $kconfig) {
2862 doprint "file $kconfig does not exist, skipping\n";
2863 return;
2864 }
2865
2866 open(KIN, "$kconfig")
2867 or die "Can't open $kconfig";
2868 while (<KIN>) {
2869 chomp;
2870
2871 # Make sure that lines ending with \ continue
2872 if ($cont) {
2873 $_ = $line . " " . $_;
2874 }
2875
2876 if (s/\\$//) {
2877 $cont = 1;
2878 $line = $_;
2879 next;
2880 }
2881
2882 $cont = 0;
2883
2884 # collect any Kconfig sources
2885 if (/^source\s*"(.*)"/) {
2886 $kconfigs[$#kconfigs+1] = $1;
2887 }
2888
2889 # configs found
2890 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2891 $state = "NEW";
2892 $config = $2;
2893
2894 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04002895 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04002896 }
2897
2898 # collect the depends for the config
2899 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2900
Steven Rostedtac6974c2011-10-04 09:40:17 -04002901 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002902
2903 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04002904 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2905
2906 # selected by depends on config
2907 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002908
2909 # Check for if statements
2910 } elsif (/^if\s+(.*\S)\s*$/) {
2911 my $deps = $1;
2912 # remove beginning and ending non text
2913 $deps =~ s/^[^a-zA-Z0-9_]*//;
2914 $deps =~ s/[^a-zA-Z0-9_]*$//;
2915
2916 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2917
2918 $ifdeps[$iflevel++] = join ':', @deps;
2919
2920 } elsif (/^endif/) {
2921
2922 $iflevel-- if ($iflevel);
2923
2924 # stop on "help"
2925 } elsif (/^\s*help\s*$/) {
2926 $state = "NONE";
2927 }
2928 }
2929 close(KIN);
2930
2931 # read in any configs that were found.
2932 foreach $kconfig (@kconfigs) {
2933 if (!defined($read_kconfigs{$kconfig})) {
2934 $read_kconfigs{$kconfig} = 1;
2935 read_kconfig("$builddir/$kconfig");
2936 }
2937 }
2938}
2939
2940sub read_depends {
2941 # find out which arch this is by the kconfig file
2942 open (IN, $output_config)
2943 or dodie "Failed to read $output_config";
2944 my $arch;
2945 while (<IN>) {
2946 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2947 $arch = $1;
2948 last;
2949 }
2950 }
2951 close IN;
2952
2953 if (!defined($arch)) {
2954 doprint "Could not find arch from config file\n";
2955 doprint "no dependencies used\n";
2956 return;
2957 }
2958
2959 # arch is really the subarch, we need to know
2960 # what directory to look at.
2961 if ($arch eq "i386" || $arch eq "x86_64") {
2962 $arch = "x86";
2963 } elsif ($arch =~ /^tile/) {
2964 $arch = "tile";
2965 }
2966
2967 my $kconfig = "$builddir/arch/$arch/Kconfig";
2968
2969 if (! -f $kconfig && $arch =~ /\d$/) {
2970 my $orig = $arch;
2971 # some subarchs have numbers, truncate them
2972 $arch =~ s/\d*$//;
2973 $kconfig = "$builddir/arch/$arch/Kconfig";
2974 if (! -f $kconfig) {
2975 doprint "No idea what arch dir $orig is for\n";
2976 doprint "no dependencies used\n";
2977 return;
2978 }
2979 }
2980
2981 read_kconfig($kconfig);
2982}
2983
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002984sub read_config_list {
2985 my ($config) = @_;
2986
2987 open (IN, $config)
2988 or dodie "Failed to read $config";
2989
2990 while (<IN>) {
2991 if (/^((CONFIG\S*)=.*)/) {
2992 if (!defined($config_ignore{$2})) {
2993 $config_list{$2} = $1;
2994 }
2995 }
2996 }
2997
2998 close(IN);
2999}
3000
3001sub read_output_config {
3002 my ($config) = @_;
3003
3004 assign_configs \%config_ignore, $config;
3005}
3006
3007sub make_new_config {
3008 my @configs = @_;
3009
3010 open (OUT, ">$output_config")
3011 or dodie "Failed to write $output_config";
3012
3013 foreach my $config (@configs) {
3014 print OUT "$config\n";
3015 }
3016 close OUT;
3017}
3018
Steven Rostedtac6974c2011-10-04 09:40:17 -04003019sub chomp_config {
3020 my ($config) = @_;
3021
3022 $config =~ s/CONFIG_//;
3023
3024 return $config;
3025}
3026
Steven Rostedtb9066f62011-07-15 21:25:24 -04003027sub get_depends {
3028 my ($dep) = @_;
3029
Steven Rostedtac6974c2011-10-04 09:40:17 -04003030 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003031
3032 $dep = $depends{"$kconfig"};
3033
3034 # the dep string we have saves the dependencies as they
3035 # were found, including expressions like ! && ||. We
3036 # want to split this out into just an array of configs.
3037
3038 my $valid = "A-Za-z_0-9";
3039
3040 my @configs;
3041
3042 while ($dep =~ /[$valid]/) {
3043
3044 if ($dep =~ /^[^$valid]*([$valid]+)/) {
3045 my $conf = "CONFIG_" . $1;
3046
3047 $configs[$#configs + 1] = $conf;
3048
3049 $dep =~ s/^[^$valid]*[$valid]+//;
3050 } else {
3051 die "this should never happen";
3052 }
3053 }
3054
3055 return @configs;
3056}
3057
3058my %min_configs;
3059my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003060my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003061my %processed_configs;
3062my %nochange_config;
3063
3064sub test_this_config {
3065 my ($config) = @_;
3066
3067 my $found;
3068
3069 # if we already processed this config, skip it
3070 if (defined($processed_configs{$config})) {
3071 return undef;
3072 }
3073 $processed_configs{$config} = 1;
3074
3075 # if this config failed during this round, skip it
3076 if (defined($nochange_config{$config})) {
3077 return undef;
3078 }
3079
Steven Rostedtac6974c2011-10-04 09:40:17 -04003080 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003081
3082 # Test dependencies first
3083 if (defined($depends{"$kconfig"})) {
3084 my @parents = get_depends $config;
3085 foreach my $parent (@parents) {
3086 # if the parent is in the min config, check it first
3087 next if (!defined($min_configs{$parent}));
3088 $found = test_this_config($parent);
3089 if (defined($found)) {
3090 return $found;
3091 }
3092 }
3093 }
3094
3095 # Remove this config from the list of configs
3096 # do a make oldnoconfig and then read the resulting
3097 # .config to make sure it is missing the config that
3098 # we had before
3099 my %configs = %min_configs;
3100 delete $configs{$config};
3101 make_new_config ((values %configs), (values %keep_configs));
3102 make_oldconfig;
3103 undef %configs;
3104 assign_configs \%configs, $output_config;
3105
3106 return $config if (!defined($configs{$config}));
3107
3108 doprint "disabling config $config did not change .config\n";
3109
3110 $nochange_config{$config} = 1;
3111
3112 return undef;
3113}
3114
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003115sub make_min_config {
3116 my ($i) = @_;
3117
3118 if (!defined($output_minconfig)) {
3119 fail "OUTPUT_MIN_CONFIG not defined" and return;
3120 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04003121
3122 # If output_minconfig exists, and the start_minconfig
3123 # came from min_config, than ask if we should use
3124 # that instead.
3125 if (-f $output_minconfig && !$start_minconfig_defined) {
3126 print "$output_minconfig exists\n";
3127 if (read_yn " Use it as minconfig?") {
3128 $start_minconfig = $output_minconfig;
3129 }
3130 }
3131
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003132 if (!defined($start_minconfig)) {
3133 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
3134 }
3135
Steven Rostedt35ce5952011-07-15 21:57:25 -04003136 my $temp_config = "$tmpdir/temp_config";
3137
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003138 # First things first. We build an allnoconfig to find
3139 # out what the defaults are that we can't touch.
3140 # Some are selections, but we really can't handle selections.
3141
3142 my $save_minconfig = $minconfig;
3143 undef $minconfig;
3144
3145 run_command "$make allnoconfig" or return 0;
3146
Steven Rostedtb9066f62011-07-15 21:25:24 -04003147 read_depends;
3148
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003149 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003150
Steven Rostedt43d1b652011-07-15 22:01:56 -04003151 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003152 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003153
3154 if (defined($ignore_config)) {
3155 # make sure the file exists
3156 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003157 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003158 }
3159
Steven Rostedt43d1b652011-07-15 22:01:56 -04003160 %keep_configs = %save_configs;
3161
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003162 doprint "Load initial configs from $start_minconfig\n";
3163
3164 # Look at the current min configs, and save off all the
3165 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003166 assign_configs \%min_configs, $start_minconfig;
3167
3168 my @config_keys = keys %min_configs;
3169
Steven Rostedtac6974c2011-10-04 09:40:17 -04003170 # All configs need a depcount
3171 foreach my $config (@config_keys) {
3172 my $kconfig = chomp_config $config;
3173 if (!defined $depcount{$kconfig}) {
3174 $depcount{$kconfig} = 0;
3175 }
3176 }
3177
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003178 # Remove anything that was set by the make allnoconfig
3179 # we shouldn't need them as they get set for us anyway.
3180 foreach my $config (@config_keys) {
3181 # Remove anything in the ignore_config
3182 if (defined($keep_configs{$config})) {
3183 my $file = $ignore_config;
3184 $file =~ s,.*/(.*?)$,$1,;
3185 doprint "$config set by $file ... ignored\n";
3186 delete $min_configs{$config};
3187 next;
3188 }
3189 # But make sure the settings are the same. If a min config
3190 # sets a selection, we do not want to get rid of it if
3191 # it is not the same as what we have. Just move it into
3192 # the keep configs.
3193 if (defined($config_ignore{$config})) {
3194 if ($config_ignore{$config} ne $min_configs{$config}) {
3195 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3196 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3197 $keep_configs{$config} = $min_configs{$config};
3198 } else {
3199 doprint "$config set by allnoconfig ... ignored\n";
3200 }
3201 delete $min_configs{$config};
3202 }
3203 }
3204
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003205 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003206 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003207
3208 while (!$done) {
3209
3210 my $config;
3211 my $found;
3212
3213 # Now disable each config one by one and do a make oldconfig
3214 # till we find a config that changes our list.
3215
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003216 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003217
3218 # Sort keys by who is most dependent on
3219 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3220 @test_configs ;
3221
3222 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003223 my $reset = 1;
3224 for (my $i = 0; $i < $#test_configs; $i++) {
3225 if (!defined($nochange_config{$test_configs[0]})) {
3226 $reset = 0;
3227 last;
3228 }
3229 # This config didn't change the .config last time.
3230 # Place it at the end
3231 my $config = shift @test_configs;
3232 push @test_configs, $config;
3233 }
3234
3235 # if every test config has failed to modify the .config file
3236 # in the past, then reset and start over.
3237 if ($reset) {
3238 undef %nochange_config;
3239 }
3240
Steven Rostedtb9066f62011-07-15 21:25:24 -04003241 undef %processed_configs;
3242
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003243 foreach my $config (@test_configs) {
3244
Steven Rostedtb9066f62011-07-15 21:25:24 -04003245 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003246
Steven Rostedtb9066f62011-07-15 21:25:24 -04003247 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003248
3249 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003250 }
3251
3252 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003253 # we could have failed due to the nochange_config hash
3254 # reset and try again
3255 if (!$take_two) {
3256 undef %nochange_config;
3257 $take_two = 1;
3258 next;
3259 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003260 doprint "No more configs found that we can disable\n";
3261 $done = 1;
3262 last;
3263 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003264 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003265
3266 $config = $found;
3267
3268 doprint "Test with $config disabled\n";
3269
3270 # set in_bisect to keep build and monitor from dieing
3271 $in_bisect = 1;
3272
3273 my $failed = 0;
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003274 build "oldconfig" or $failed = 1;
3275 if (!$failed) {
3276 start_monitor_and_boot or $failed = 1;
3277 end_monitor;
3278 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003279
3280 $in_bisect = 0;
3281
3282 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003283 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003284 # this config is needed, add it to the ignore list.
3285 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003286 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003287 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003288
3289 # update new ignore configs
3290 if (defined($ignore_config)) {
3291 open (OUT, ">$temp_config")
3292 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003293 foreach my $config (keys %save_configs) {
3294 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003295 }
3296 close OUT;
3297 run_command "mv $temp_config $ignore_config" or
3298 dodie "failed to copy update to $ignore_config";
3299 }
3300
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003301 } else {
3302 # We booted without this config, remove it from the minconfigs.
3303 doprint "$config is not needed, disabling\n";
3304
3305 delete $min_configs{$config};
3306
3307 # Also disable anything that is not enabled in this config
3308 my %configs;
3309 assign_configs \%configs, $output_config;
3310 my @config_keys = keys %min_configs;
3311 foreach my $config (@config_keys) {
3312 if (!defined($configs{$config})) {
3313 doprint "$config is not set, disabling\n";
3314 delete $min_configs{$config};
3315 }
3316 }
3317
3318 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003319 open (OUT, ">$temp_config")
3320 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003321 foreach my $config (keys %keep_configs) {
3322 print OUT "$keep_configs{$config}\n";
3323 }
3324 foreach my $config (keys %min_configs) {
3325 print OUT "$min_configs{$config}\n";
3326 }
3327 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003328
3329 run_command "mv $temp_config $output_minconfig" or
3330 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003331 }
3332
3333 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003334 reboot_to_good $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003335 }
3336
3337 success $i;
3338 return 1;
3339}
3340
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003341$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003342
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003343if ($#ARGV == 0) {
3344 $ktest_config = $ARGV[0];
3345 if (! -f $ktest_config) {
3346 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003347 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003348 exit 0;
3349 }
3350 }
3351} else {
3352 $ktest_config = "ktest.conf";
3353}
3354
3355if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003356 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003357 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003358 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3359 print OUT << "EOF"
3360# Generated by ktest.pl
3361#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003362
3363# PWD is a ktest.pl variable that will result in the process working
3364# directory that ktest.pl is executed in.
3365
3366# THIS_DIR is automatically assigned the PWD of the path that generated
3367# the config file. It is best to use this variable when assigning other
3368# directory paths within this directory. This allows you to easily
3369# move the test cases to other locations or to other machines.
3370#
3371THIS_DIR := $variable{"PWD"}
3372
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003373# Define each test with TEST_START
3374# The config options below it will override the defaults
3375TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003376TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003377
3378DEFAULTS
3379EOF
3380;
3381 close(OUT);
3382}
3383read_config $ktest_config;
3384
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003385if (defined($opt{"LOG_FILE"})) {
3386 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3387}
3388
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003389# Append any configs entered in manually to the config file.
3390my @new_configs = keys %entered_configs;
3391if ($#new_configs >= 0) {
3392 print "\nAppending entered in configs to $ktest_config\n";
3393 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3394 foreach my $config (@new_configs) {
3395 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003396 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003397 }
3398}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003399
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003400if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3401 unlink $opt{"LOG_FILE"};
3402}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003403
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003404doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3405
Steven Rostedta57419b2010-11-02 15:13:54 -04003406for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3407
3408 if (!$i) {
3409 doprint "DEFAULT OPTIONS:\n";
3410 } else {
3411 doprint "\nTEST $i OPTIONS";
3412 if (defined($repeat_tests{$i})) {
3413 $repeat = $repeat_tests{$i};
3414 doprint " ITERATE $repeat";
3415 }
3416 doprint "\n";
3417 }
3418
3419 foreach my $option (sort keys %opt) {
3420
3421 if ($option =~ /\[(\d+)\]$/) {
3422 next if ($i != $1);
3423 } else {
3424 next if ($i);
3425 }
3426
3427 doprint "$option = $opt{$option}\n";
3428 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003429}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003430
Steven Rostedt2a625122011-05-20 15:48:59 -04003431sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003432 my ($name, $i) = @_;
3433
3434 my $option = "$name\[$i\]";
3435
3436 if (defined($opt{$option})) {
3437 return $opt{$option};
3438 }
3439
Steven Rostedta57419b2010-11-02 15:13:54 -04003440 foreach my $test (keys %repeat_tests) {
3441 if ($i >= $test &&
3442 $i < $test + $repeat_tests{$test}) {
3443 $option = "$name\[$test\]";
3444 if (defined($opt{$option})) {
3445 return $opt{$option};
3446 }
3447 }
3448 }
3449
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003450 if (defined($opt{$name})) {
3451 return $opt{$name};
3452 }
3453
3454 return undef;
3455}
3456
Steven Rostedt2a625122011-05-20 15:48:59 -04003457sub set_test_option {
3458 my ($name, $i) = @_;
3459
3460 my $option = __set_test_option($name, $i);
3461 return $option if (!defined($option));
3462
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003463 return eval_option($option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003464}
3465
Steven Rostedt2545eb62010-11-02 15:01:32 -04003466# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003467for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003468
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003469 # Do not reboot on failing test options
3470 $no_reboot = 1;
3471
Steven Rostedt576f6272010-11-02 14:58:38 -04003472 $iteration = $i;
3473
Steven Rostedta75fece2010-11-02 14:58:27 -04003474 my $makecmd = set_test_option("MAKE_CMD", $i);
3475
Steven Rostedt9cc9e092011-12-22 21:37:22 -05003476 # Load all the options into their mapped variable names
3477 foreach my $opt (keys %option_map) {
3478 ${$option_map{$opt}} = set_test_option($opt, $i);
3479 }
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003480
Steven Rostedt35ce5952011-07-15 21:57:25 -04003481 $start_minconfig_defined = 1;
3482
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003483 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003484 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003485 $start_minconfig = $minconfig;
3486 }
3487
Steven Rostedta75fece2010-11-02 14:58:27 -04003488 chdir $builddir || die "can't change directory to $builddir";
3489
Andrew Jonesa908a662011-08-12 15:32:03 +02003490 foreach my $dir ($tmpdir, $outputdir) {
3491 if (!-d $dir) {
3492 mkpath($dir) or
3493 die "can't create $dir";
3494 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003495 }
3496
Steven Rostedte48c5292010-11-02 14:35:37 -04003497 $ENV{"SSH_USER"} = $ssh_user;
3498 $ENV{"MACHINE"} = $machine;
3499
Steven Rostedta75fece2010-11-02 14:58:27 -04003500 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303501 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003502 $dmesg = "$tmpdir/dmesg-$machine";
3503 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003504 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003505
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003506 if (!$buildonly) {
3507 $target = "$ssh_user\@$machine";
3508 if ($reboot_type eq "grub") {
3509 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003510 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003511 }
3512
3513 my $run_type = $build_type;
3514 if ($test_type eq "patchcheck") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003515 $run_type = $patchcheck_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003516 } elsif ($test_type eq "bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003517 $run_type = $bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003518 } elsif ($test_type eq "config_bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003519 $run_type = $config_bisect_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003520 }
3521
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003522 if ($test_type eq "make_min_config") {
3523 $run_type = "";
3524 }
3525
Steven Rostedta75fece2010-11-02 14:58:27 -04003526 # mistake in config file?
3527 if (!defined($run_type)) {
3528 $run_type = "ERROR";
3529 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003530
Steven Rostedte0a87422011-09-30 17:50:48 -04003531 my $installme = "";
3532 $installme = " no_install" if ($no_install);
3533
Steven Rostedt2545eb62010-11-02 15:01:32 -04003534 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003535 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003536
3537 unlink $dmesg;
3538 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303539 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003540
Steven Rostedt250bae82011-07-15 22:05:59 -04003541 if (defined($addconfig)) {
3542 my $min = $minconfig;
3543 if (!defined($minconfig)) {
3544 $min = "";
3545 }
3546 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003547 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003548 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003549 }
3550
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003551 if (defined($checkout)) {
3552 run_command "git checkout $checkout" or
3553 die "failed to checkout $checkout";
3554 }
3555
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003556 $no_reboot = 0;
3557
3558
Steven Rostedta75fece2010-11-02 14:58:27 -04003559 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003560 bisect $i;
3561 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003562 } elsif ($test_type eq "config_bisect") {
3563 config_bisect $i;
3564 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003565 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003566 patchcheck $i;
3567 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003568 } elsif ($test_type eq "make_min_config") {
3569 make_min_config $i;
3570 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003571 }
3572
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003573 if ($build_type ne "nobuild") {
3574 build $build_type or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003575 }
3576
Steven Rostedtcd8e3682011-08-18 16:35:44 -04003577 if ($test_type eq "install") {
3578 get_version;
3579 install;
3580 success $i;
3581 next;
3582 }
3583
Steven Rostedta75fece2010-11-02 14:58:27 -04003584 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04003585 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04003586 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04003587
3588 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3589 do_run_test or $failed = 1;
3590 }
3591 end_monitor;
3592 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003593 }
3594
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003595 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003596}
3597
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003598if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003599 halt;
Steven Rostedt576f6272010-11-02 14:58:38 -04003600} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003601 reboot_to_good;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003602}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003603
Steven Rostedte48c5292010-11-02 14:35:37 -04003604doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3605
Steven Rostedt2545eb62010-11-02 15:01:32 -04003606exit 0;