blob: e7fed79039393fcf365b1e37796a194ed35e9410 [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;
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001119 }
1120
1121 reboot $time;
1122}
1123
Steven Rostedt576f6272010-11-02 14:58:38 -04001124sub do_not_reboot {
1125 my $i = $iteration;
1126
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001127 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -04001128 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1129 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1130}
1131
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001132sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001133 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001134
Steven Rostedt576f6272010-11-02 14:58:38 -04001135 my $i = $iteration;
1136
1137 if ($reboot_on_error && !do_not_reboot) {
1138
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001139 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001140 reboot_to_good;
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001141
Steven Rostedta75fece2010-11-02 14:58:27 -04001142 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001143 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001144 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001145 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001146
Steven Rostedtf80802c2011-03-07 13:18:47 -05001147 if (defined($opt{"LOG_FILE"})) {
1148 print " See $opt{LOG_FILE} for more info.\n";
1149 }
1150
Steven Rostedt576f6272010-11-02 14:58:38 -04001151 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001152}
1153
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001154sub open_console {
1155 my ($fp) = @_;
1156
1157 my $flags;
1158
Steven Rostedta75fece2010-11-02 14:58:27 -04001159 my $pid = open($fp, "$console|") or
1160 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001161
1162 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001163 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001164 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001165 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001166
1167 return $pid;
1168}
1169
1170sub close_console {
1171 my ($fp, $pid) = @_;
1172
1173 doprint "kill child process $pid\n";
1174 kill 2, $pid;
1175
1176 print "closing!\n";
1177 close($fp);
1178}
1179
1180sub start_monitor {
1181 if ($monitor_cnt++) {
1182 return;
1183 }
1184 $monitor_fp = \*MONFD;
1185 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001186
1187 return;
1188
1189 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001190}
1191
1192sub end_monitor {
1193 if (--$monitor_cnt) {
1194 return;
1195 }
1196 close_console($monitor_fp, $monitor_pid);
1197}
1198
1199sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001200 my ($time, $stop) = @_;
1201 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001202 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001203 my $booted = 0;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001204
Steven Rostedta75fece2010-11-02 14:58:27 -04001205 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001206
1207 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001208 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001209 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001210 last if (!defined($line));
1211 print "$line";
1212 $full_line .= $line;
1213
1214 if (defined($stop) && $full_line =~ /$stop/) {
1215 doprint "wait for monitor detected $stop\n";
1216 $booted = 1;
1217 }
1218
1219 if ($line =~ /\n/) {
1220 $full_line = "";
1221 }
1222 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001223 print "** Monitor flushed **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001224}
1225
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301226sub save_logs {
1227 my ($result, $basedir) = @_;
1228 my @t = localtime;
1229 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1230 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1231
1232 my $type = $build_type;
1233 if ($type =~ /useconfig/) {
1234 $type = "useconfig";
1235 }
1236
1237 my $dir = "$machine-$test_type-$type-$result-$date";
1238
1239 $dir = "$basedir/$dir";
1240
1241 if (!-d $dir) {
1242 mkpath($dir) or
1243 die "can't create $dir";
1244 }
1245
1246 my %files = (
1247 "config" => $output_config,
1248 "buildlog" => $buildlog,
1249 "dmesg" => $dmesg,
1250 "testlog" => $testlog,
1251 );
1252
1253 while (my ($name, $source) = each(%files)) {
1254 if (-f "$source") {
1255 cp "$source", "$dir/$name" or
1256 die "failed to copy $source";
1257 }
1258 }
1259
1260 doprint "*** Saved info to $dir ***\n";
1261}
1262
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001263sub fail {
1264
Steven Rostedta75fece2010-11-02 14:58:27 -04001265 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001266 dodie @_;
1267 }
1268
Steven Rostedta75fece2010-11-02 14:58:27 -04001269 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001270
Steven Rostedt576f6272010-11-02 14:58:38 -04001271 my $i = $iteration;
1272
Steven Rostedta75fece2010-11-02 14:58:27 -04001273 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001274 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001275 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001276 reboot_to_good $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001277 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001278
Steven Rostedt9064af52011-06-13 10:38:48 -04001279 my $name = "";
1280
1281 if (defined($test_name)) {
1282 $name = " ($test_name)";
1283 }
1284
Steven Rostedt576f6272010-11-02 14:58:38 -04001285 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1286 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001287 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001288 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1289 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001290
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301291 if (defined($store_failures)) {
1292 save_logs "fail", $store_failures;
1293 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001294
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001295 return 1;
1296}
1297
Steven Rostedt2545eb62010-11-02 15:01:32 -04001298sub run_command {
1299 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001300 my $dolog = 0;
1301 my $dord = 0;
1302 my $pid;
1303
Steven Rostedte48c5292010-11-02 14:35:37 -04001304 $command =~ s/\$SSH_USER/$ssh_user/g;
1305 $command =~ s/\$MACHINE/$machine/g;
1306
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001307 doprint("$command ... ");
1308
1309 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001310 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001311
1312 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001313 open(LOG, ">>$opt{LOG_FILE}") or
1314 dodie "failed to write to log";
1315 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001316 }
1317
1318 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001319 open (RD, ">$redirect") or
1320 dodie "failed to write to redirect $redirect";
1321 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001322 }
1323
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001324 while (<CMD>) {
1325 print LOG if ($dolog);
1326 print RD if ($dord);
1327 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001328
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001329 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001330 my $failed = $?;
1331
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001332 close(CMD);
1333 close(LOG) if ($dolog);
1334 close(RD) if ($dord);
1335
Steven Rostedt2545eb62010-11-02 15:01:32 -04001336 if ($failed) {
1337 doprint "FAILED!\n";
1338 } else {
1339 doprint "SUCCESS\n";
1340 }
1341
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001342 return !$failed;
1343}
1344
Steven Rostedte48c5292010-11-02 14:35:37 -04001345sub run_ssh {
1346 my ($cmd) = @_;
1347 my $cp_exec = $ssh_exec;
1348
1349 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1350 return run_command "$cp_exec";
1351}
1352
1353sub run_scp {
Steven Rostedt02ad2612012-03-21 08:21:24 -04001354 my ($src, $dst, $cp_scp) = @_;
Steven Rostedte48c5292010-11-02 14:35:37 -04001355
1356 $cp_scp =~ s/\$SRC_FILE/$src/g;
1357 $cp_scp =~ s/\$DST_FILE/$dst/g;
1358
1359 return run_command "$cp_scp";
1360}
1361
Steven Rostedt02ad2612012-03-21 08:21:24 -04001362sub run_scp_install {
1363 my ($src, $dst) = @_;
1364
1365 my $cp_scp = $scp_to_target_install;
1366
1367 return run_scp($src, $dst, $cp_scp);
1368}
1369
1370sub run_scp_mod {
1371 my ($src, $dst) = @_;
1372
1373 my $cp_scp = $scp_to_target;
1374
1375 return run_scp($src, $dst, $cp_scp);
1376}
1377
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001378sub get_grub_index {
1379
Steven Rostedta75fece2010-11-02 14:58:27 -04001380 if ($reboot_type ne "grub") {
1381 return;
1382 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001383 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001384
1385 doprint "Find grub menu ... ";
1386 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001387
1388 my $ssh_grub = $ssh_exec;
1389 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1390
1391 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001392 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001393
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001394 my $found = 0;
1395
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001396 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001397 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001398 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001399 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001400 last;
1401 } elsif (/^\s*title\s/) {
1402 $grub_number++;
1403 }
1404 }
1405 close(IN);
1406
Steven Rostedta75fece2010-11-02 14:58:27 -04001407 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001408 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001409 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001410}
1411
Steven Rostedt2545eb62010-11-02 15:01:32 -04001412sub wait_for_input
1413{
1414 my ($fp, $time) = @_;
1415 my $rin;
1416 my $ready;
1417 my $line;
1418 my $ch;
1419
1420 if (!defined($time)) {
1421 $time = $timeout;
1422 }
1423
1424 $rin = '';
1425 vec($rin, fileno($fp), 1) = 1;
1426 $ready = select($rin, undef, undef, $time);
1427
1428 $line = "";
1429
1430 # try to read one char at a time
1431 while (sysread $fp, $ch, 1) {
1432 $line .= $ch;
1433 last if ($ch eq "\n");
1434 }
1435
1436 if (!length($line)) {
1437 return undef;
1438 }
1439
1440 return $line;
1441}
1442
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001443sub reboot_to {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001444 if (defined($switch_to_test)) {
1445 run_command $switch_to_test;
1446 }
1447
Steven Rostedta75fece2010-11-02 14:58:27 -04001448 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001449 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001450 } elsif (defined $reboot_script) {
1451 run_command "$reboot_script";
Steven Rostedta75fece2010-11-02 14:58:27 -04001452 }
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001453 reboot;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001454}
1455
Steven Rostedta57419b2010-11-02 15:13:54 -04001456sub get_sha1 {
1457 my ($commit) = @_;
1458
1459 doprint "git rev-list --max-count=1 $commit ... ";
1460 my $sha1 = `git rev-list --max-count=1 $commit`;
1461 my $ret = $?;
1462
1463 logit $sha1;
1464
1465 if ($ret) {
1466 doprint "FAILED\n";
1467 dodie "Failed to get git $commit";
1468 }
1469
1470 print "SUCCESS\n";
1471
1472 chomp $sha1;
1473
1474 return $sha1;
1475}
1476
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001477sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001478 my $booted = 0;
1479 my $bug = 0;
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001480 my $bug_ignored = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001481 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001482 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001483
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001484 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001485
1486 my $line;
1487 my $full_line = "";
1488
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001489 open(DMESG, "> $dmesg") or
1490 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001491
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001492 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001493
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001494 my $success_start;
1495 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001496 my $monitor_start = time;
1497 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001498 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001499
Steven Rostedt2d01b262011-03-08 09:47:54 -05001500 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001501
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001502 if ($bug && defined($stop_after_failure) &&
1503 $stop_after_failure >= 0) {
1504 my $time = $stop_after_failure - (time - $failure_start);
1505 $line = wait_for_input($monitor_fp, $time);
1506 if (!defined($line)) {
1507 doprint "bug timed out after $booted_timeout seconds\n";
1508 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1509 last;
1510 }
1511 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001512 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001513 if (!defined($line)) {
1514 my $s = $booted_timeout == 1 ? "" : "s";
1515 doprint "Successful boot found: break after $booted_timeout second$s\n";
1516 last;
1517 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001518 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001519 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001520 if (!defined($line)) {
1521 my $s = $timeout == 1 ? "" : "s";
1522 doprint "Timed out after $timeout second$s\n";
1523 last;
1524 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001525 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001526
Steven Rostedt2545eb62010-11-02 15:01:32 -04001527 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001528 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001529
1530 # we are not guaranteed to get a full line
1531 $full_line .= $line;
1532
Steven Rostedta75fece2010-11-02 14:58:27 -04001533 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001534 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001535 $success_start = time;
1536 }
1537
1538 if ($booted && defined($stop_after_success) &&
1539 $stop_after_success >= 0) {
1540 my $now = time;
1541 if ($now - $success_start >= $stop_after_success) {
1542 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1543 last;
1544 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001545 }
1546
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001547 if ($full_line =~ /\[ backtrace testing \]/) {
1548 $skip_call_trace = 1;
1549 }
1550
Steven Rostedt2545eb62010-11-02 15:01:32 -04001551 if ($full_line =~ /call trace:/i) {
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001552 if (!$bug && !$skip_call_trace) {
1553 if ($ignore_errors) {
1554 $bug_ignored = 1;
1555 } else {
1556 $bug = 1;
1557 $failure_start = time;
1558 }
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001559 }
1560 }
1561
1562 if ($bug && defined($stop_after_failure) &&
1563 $stop_after_failure >= 0) {
1564 my $now = time;
1565 if ($now - $failure_start >= $stop_after_failure) {
1566 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1567 last;
1568 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001569 }
1570
1571 if ($full_line =~ /\[ end of backtrace testing \]/) {
1572 $skip_call_trace = 0;
1573 }
1574
1575 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001576 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001577 $bug = 1;
1578 }
1579
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001580 # Detect triple faults by testing the banner
1581 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1582 if ($1 eq $version) {
1583 $version_found = 1;
1584 } elsif ($version_found && $detect_triplefault) {
1585 # We already booted into the kernel we are testing,
1586 # but now we booted into another kernel?
1587 # Consider this a triple fault.
1588 doprint "Aleady booted in Linux kernel $version, but now\n";
1589 doprint "we booted into Linux kernel $1.\n";
1590 doprint "Assuming that this is a triple fault.\n";
1591 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1592 last;
1593 }
1594 }
1595
Steven Rostedt2545eb62010-11-02 15:01:32 -04001596 if ($line =~ /\n/) {
1597 $full_line = "";
1598 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001599
1600 if ($stop_test_after > 0 && !$booted && !$bug) {
1601 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001602 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001603 $done = 1;
1604 }
1605 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001606 }
1607
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001608 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001609
Steven Rostedt2545eb62010-11-02 15:01:32 -04001610 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001611 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001612 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001613 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001614
Steven Rostedta75fece2010-11-02 14:58:27 -04001615 if (!$booted) {
1616 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001617 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001618 }
1619
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001620 if ($bug_ignored) {
1621 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
1622 }
1623
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001624 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001625}
1626
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001627sub eval_kernel_version {
1628 my ($option) = @_;
1629
1630 $option =~ s/\$KERNEL_VERSION/$version/g;
1631
1632 return $option;
1633}
1634
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001635sub do_post_install {
1636
1637 return if (!defined($post_install));
1638
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001639 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001640 run_command "$cp_post_install" or
1641 dodie "Failed to run post install";
1642}
1643
Steven Rostedt2545eb62010-11-02 15:01:32 -04001644sub install {
1645
Steven Rostedte0a87422011-09-30 17:50:48 -04001646 return if ($no_install);
1647
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001648 my $cp_target = eval_kernel_version $target_image;
1649
Steven Rostedt02ad2612012-03-21 08:21:24 -04001650 run_scp_install "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001651 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001652
1653 my $install_mods = 0;
1654
1655 # should we process modules?
1656 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001657 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001658 while (<IN>) {
1659 if (/CONFIG_MODULES(=y)?/) {
1660 $install_mods = 1 if (defined($1));
1661 last;
1662 }
1663 }
1664 close(IN);
1665
1666 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001667 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001668 doprint "No modules needed\n";
1669 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001670 }
1671
Steven Rostedt627977d2012-03-21 08:16:15 -04001672 run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001673 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001674
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001675 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001676 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001677
Steven Rostedte48c5292010-11-02 14:35:37 -04001678 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001679 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001680
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001681 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001682 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001683 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001684
Steven Rostedt02ad2612012-03-21 08:21:24 -04001685 run_scp_mod "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001686 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001687
Steven Rostedta75fece2010-11-02 14:58:27 -04001688 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001689
Steven Rostedte7b13442011-06-14 20:44:36 -04001690 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001691 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001692
Steven Rostedte48c5292010-11-02 14:35:37 -04001693 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001694
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001695 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001696}
1697
Steven Rostedtddf607e2011-06-14 20:49:13 -04001698sub get_version {
1699 # get the release name
1700 doprint "$make kernelrelease ... ";
1701 $version = `$make kernelrelease | tail -1`;
1702 chomp($version);
1703 doprint "$version\n";
1704}
1705
1706sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001707 # Make sure the stable kernel has finished booting
1708 start_monitor;
1709 wait_for_monitor 5;
1710 end_monitor;
1711
Steven Rostedtddf607e2011-06-14 20:49:13 -04001712 get_grub_index;
1713 get_version;
1714 install;
1715
1716 start_monitor;
1717 return monitor;
1718}
1719
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001720sub check_buildlog {
1721 my ($patch) = @_;
1722
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001723 my @files = `git show $patch | diffstat -l`;
1724
1725 open(IN, "git show $patch |") or
1726 dodie "failed to show $patch";
1727 while (<IN>) {
1728 if (m,^--- a/(.*),) {
1729 chomp $1;
1730 $files[$#files] = $1;
1731 }
1732 }
1733 close(IN);
1734
1735 open(IN, $buildlog) or dodie "Can't open $buildlog";
1736 while (<IN>) {
1737 if (/^\s*(.*?):.*(warning|error)/) {
1738 my $err = $1;
1739 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001740 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001741 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001742 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001743 }
1744 }
1745 }
1746 }
1747 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001748
1749 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001750}
1751
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001752sub apply_min_config {
1753 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05001754
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001755 # Read the config file and remove anything that
1756 # is in the force_config hash (from minconfig and others)
1757 # then add the force config back.
1758
1759 doprint "Applying minimum configurations into $output_config.new\n";
1760
1761 open (OUT, ">$outconfig") or
1762 dodie "Can't create $outconfig";
1763
1764 if (-f $output_config) {
1765 open (IN, $output_config) or
1766 dodie "Failed to open $output_config";
1767 while (<IN>) {
1768 if (/^(# )?(CONFIG_[^\s=]*)/) {
1769 next if (defined($force_config{$2}));
1770 }
1771 print OUT;
1772 }
1773 close IN;
1774 }
1775 foreach my $config (keys %force_config) {
1776 print OUT "$force_config{$config}\n";
1777 }
1778 close OUT;
1779
1780 run_command "mv $outconfig $output_config";
1781}
1782
1783sub make_oldconfig {
1784
Steven Rostedt4c4ab122011-07-15 21:16:17 -04001785 my @force_list = keys %force_config;
1786
1787 if ($#force_list >= 0) {
1788 apply_min_config;
1789 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001790
1791 if (!run_command "$make oldnoconfig") {
Steven Rostedt612b9e92011-03-07 13:27:43 -05001792 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1793 # try a yes '' | oldconfig
1794 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001795 run_command "yes '' | $make oldconfig" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001796 dodie "failed make config oldconfig";
1797 }
1798}
1799
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001800# read a config file and use this to force new configs.
1801sub load_force_config {
1802 my ($config) = @_;
1803
1804 open(IN, $config) or
1805 dodie "failed to read $config";
1806 while (<IN>) {
1807 chomp;
1808 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1809 $force_config{$1} = $_;
1810 } elsif (/^# (CONFIG_\S*) is not set/) {
1811 $force_config{$1} = $_;
1812 }
1813 }
1814 close IN;
1815}
1816
Steven Rostedt2545eb62010-11-02 15:01:32 -04001817sub build {
1818 my ($type) = @_;
1819
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001820 unlink $buildlog;
1821
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001822 # Failed builds should not reboot the target
1823 my $save_no_reboot = $no_reboot;
1824 $no_reboot = 1;
1825
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001826 if (defined($pre_build)) {
1827 my $ret = run_command $pre_build;
1828 if (!$ret && defined($pre_build_die) &&
1829 $pre_build_die) {
1830 dodie "failed to pre_build\n";
1831 }
1832 }
1833
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001834 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001835 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001836 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001837
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001838 $type = "oldconfig";
1839 }
1840
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001841 # old config can ask questions
1842 if ($type eq "oldconfig") {
Steven Rostedt9386c6a2010-11-08 16:35:48 -05001843 $type = "oldnoconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001844
1845 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001846 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001847
Andrew Jones13488232011-08-12 15:32:04 +02001848 if (!$noclean) {
1849 run_command "mv $output_config $outputdir/config_temp" or
1850 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001851
Andrew Jones13488232011-08-12 15:32:04 +02001852 run_command "$make mrproper" or dodie "make mrproper";
1853
1854 run_command "mv $outputdir/config_temp $output_config" or
1855 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001856 }
1857
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001858 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001859 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001860 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001861 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001862 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001863
1864 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04001865 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1866 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001867 close(OUT);
1868
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001869 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001870 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001871 }
1872
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001873 if ($type ne "oldnoconfig") {
1874 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001875 dodie "failed make config";
1876 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001877 # Run old config regardless, to enforce min configurations
1878 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001879
Steven Rostedta75fece2010-11-02 14:58:27 -04001880 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001881 my $build_ret = run_command "$make $build_options";
1882 undef $redirect;
1883
1884 if (defined($post_build)) {
1885 my $ret = run_command $post_build;
1886 if (!$ret && defined($post_build_die) &&
1887 $post_build_die) {
1888 dodie "failed to post_build\n";
1889 }
1890 }
1891
1892 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001893 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001894 if ($in_bisect) {
1895 $no_reboot = $save_no_reboot;
1896 return 0;
1897 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001898 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001899 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001900
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001901 $no_reboot = $save_no_reboot;
1902
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001903 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001904}
1905
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001906sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04001907 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001908 if (defined($poweroff_after_halt)) {
1909 sleep $poweroff_after_halt;
1910 run_command "$power_off";
1911 }
1912 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001913 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04001914 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001915 }
1916}
1917
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001918sub success {
1919 my ($i) = @_;
1920
Steven Rostedte48c5292010-11-02 14:35:37 -04001921 $successes++;
1922
Steven Rostedt9064af52011-06-13 10:38:48 -04001923 my $name = "";
1924
1925 if (defined($test_name)) {
1926 $name = " ($test_name)";
1927 }
1928
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001929 doprint "\n\n*******************************************\n";
1930 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001931 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001932 doprint "*******************************************\n";
1933 doprint "*******************************************\n";
1934
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301935 if (defined($store_successes)) {
1936 save_logs "success", $store_successes;
1937 }
1938
Steven Rostedt576f6272010-11-02 14:58:38 -04001939 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001940 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001941 reboot_to_good $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001942 }
1943}
1944
Steven Rostedtc960bb92011-03-08 09:22:39 -05001945sub answer_bisect {
1946 for (;;) {
1947 doprint "Pass or fail? [p/f]";
1948 my $ans = <STDIN>;
1949 chomp $ans;
1950 if ($ans eq "p" || $ans eq "P") {
1951 return 1;
1952 } elsif ($ans eq "f" || $ans eq "F") {
1953 return 0;
1954 } else {
1955 print "Please answer 'P' or 'F'\n";
1956 }
1957 }
1958}
1959
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001960sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001961 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001962
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001963 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04001964 $reboot_on_error = 0;
1965 $poweroff_on_error = 0;
1966 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001967
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301968 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001969 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301970 undef $redirect;
1971
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001972 exit $failed;
1973}
1974
1975my $child_done;
1976
1977sub child_finished {
1978 $child_done = 1;
1979}
1980
1981sub do_run_test {
1982 my $child_pid;
1983 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001984 my $line;
1985 my $full_line;
1986 my $bug = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001987
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001988 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001989
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001990 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001991
1992 $child_done = 0;
1993
1994 $SIG{CHLD} = qw(child_finished);
1995
1996 $child_pid = fork;
1997
1998 child_run_test if (!$child_pid);
1999
2000 $full_line = "";
2001
2002 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002003 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002004 if (defined($line)) {
2005
2006 # we are not guaranteed to get a full line
2007 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002008 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002009
2010 if ($full_line =~ /call trace:/i) {
2011 $bug = 1;
2012 }
2013
2014 if ($full_line =~ /Kernel panic -/) {
2015 $bug = 1;
2016 }
2017
2018 if ($line =~ /\n/) {
2019 $full_line = "";
2020 }
2021 }
2022 } while (!$child_done && !$bug);
2023
2024 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002025 my $failure_start = time;
2026 my $now;
2027 do {
2028 $line = wait_for_input($monitor_fp, 1);
2029 if (defined($line)) {
2030 doprint $line;
2031 }
2032 $now = time;
2033 if ($now - $failure_start >= $stop_after_failure) {
2034 last;
2035 }
2036 } while (defined($line));
2037
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002038 doprint "Detected kernel crash!\n";
2039 # kill the child with extreme prejudice
2040 kill 9, $child_pid;
2041 }
2042
2043 waitpid $child_pid, 0;
2044 $child_exit = $?;
2045
Steven Rostedtc5dacb82011-12-22 12:43:57 -05002046 if (!$bug && $in_bisect) {
2047 if (defined($bisect_ret_good)) {
2048 if ($child_exit == $bisect_ret_good) {
2049 return 1;
2050 }
2051 }
2052 if (defined($bisect_ret_skip)) {
2053 if ($child_exit == $bisect_ret_skip) {
2054 return -1;
2055 }
2056 }
2057 if (defined($bisect_ret_abort)) {
2058 if ($child_exit == $bisect_ret_abort) {
2059 fail "test abort" and return -2;
2060 }
2061 }
2062 if (defined($bisect_ret_bad)) {
2063 if ($child_exit == $bisect_ret_skip) {
2064 return 0;
2065 }
2066 }
2067 if (defined($bisect_ret_default)) {
2068 if ($bisect_ret_default eq "good") {
2069 return 1;
2070 } elsif ($bisect_ret_default eq "bad") {
2071 return 0;
2072 } elsif ($bisect_ret_default eq "skip") {
2073 return -1;
2074 } elsif ($bisect_ret_default eq "abort") {
2075 return -2;
2076 } else {
2077 fail "unknown default action: $bisect_ret_default"
2078 and return -2;
2079 }
2080 }
2081 }
2082
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002083 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002084 return 0 if $in_bisect;
2085 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002086 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002087 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002088}
2089
Steven Rostedta75fece2010-11-02 14:58:27 -04002090sub run_git_bisect {
2091 my ($command) = @_;
2092
2093 doprint "$command ... ";
2094
2095 my $output = `$command 2>&1`;
2096 my $ret = $?;
2097
2098 logit $output;
2099
2100 if ($ret) {
2101 doprint "FAILED\n";
2102 dodie "Failed to git bisect";
2103 }
2104
2105 doprint "SUCCESS\n";
2106 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2107 doprint "$1 [$2]\n";
2108 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002109 $bisect_bad_commit = $1;
Steven Rostedta75fece2010-11-02 14:58:27 -04002110 doprint "Found bad commit... $1\n";
2111 return 0;
2112 } else {
2113 # we already logged it, just print it now.
2114 print $output;
2115 }
2116
2117 return 1;
2118}
2119
Steven Rostedtc23dca72011-03-08 09:26:31 -05002120sub bisect_reboot {
2121 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002122 reboot_to_good $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05002123}
2124
2125# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05002126sub run_bisect_test {
2127 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002128
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002129 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002130 my $result;
2131 my $output;
2132 my $ret;
2133
Steven Rostedt0a05c762010-11-08 11:14:10 -05002134 $in_bisect = 1;
2135
2136 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002137
2138 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002139 if ($failed && $bisect_skip) {
2140 $in_bisect = 0;
2141 return -1;
2142 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002143 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002144
2145 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04002146 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002147
2148 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002149 if ($failed && $bisect_skip) {
2150 end_monitor;
2151 bisect_reboot;
2152 $in_bisect = 0;
2153 return -1;
2154 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002155 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002156
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002157 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002158 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002159 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002160 }
2161
2162 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002163 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002164 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002165 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002166 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04002167
2168 # reboot the box to a kernel we can ssh to
2169 if ($type ne "build") {
2170 bisect_reboot;
2171 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002172 $in_bisect = 0;
2173
2174 return $result;
2175}
2176
2177sub run_bisect {
2178 my ($type) = @_;
2179 my $buildtype = "oldconfig";
2180
2181 # We should have a minconfig to use?
2182 if (defined($minconfig)) {
2183 $buildtype = "useconfig:$minconfig";
2184 }
2185
2186 my $ret = run_bisect_test $type, $buildtype;
2187
Steven Rostedtc960bb92011-03-08 09:22:39 -05002188 if ($bisect_manual) {
2189 $ret = answer_bisect;
2190 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002191
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002192 # Are we looking for where it worked, not failed?
2193 if ($reverse_bisect) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002194 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002195 }
2196
Steven Rostedtc23dca72011-03-08 09:26:31 -05002197 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002198 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002199 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002200 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002201 } elsif ($bisect_skip) {
2202 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2203 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002204 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002205}
2206
Steven Rostedtdad98752011-11-22 20:48:57 -05002207sub update_bisect_replay {
2208 my $tmp_log = "$tmpdir/ktest_bisect_log";
2209 run_command "git bisect log > $tmp_log" or
2210 die "can't create bisect log";
2211 return $tmp_log;
2212}
2213
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002214sub bisect {
2215 my ($i) = @_;
2216
2217 my $result;
2218
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002219 die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2220 die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
2221 die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002222
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002223 my $good = $bisect_good;
2224 my $bad = $bisect_bad;
2225 my $type = $bisect_type;
2226 my $start = $bisect_start;
2227 my $replay = $bisect_replay;
2228 my $start_files = $bisect_files;
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002229
2230 if (defined($start_files)) {
2231 $start_files = " -- " . $start_files;
2232 } else {
2233 $start_files = "";
2234 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002235
Steven Rostedta57419b2010-11-02 15:13:54 -04002236 # convert to true sha1's
2237 $good = get_sha1($good);
2238 $bad = get_sha1($bad);
2239
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002240 if (defined($bisect_reverse) && $bisect_reverse == 1) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002241 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2242 $reverse_bisect = 1;
2243 } else {
2244 $reverse_bisect = 0;
2245 }
2246
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002247 # Can't have a test without having a test to run
2248 if ($type eq "test" && !defined($run_test)) {
2249 $type = "boot";
2250 }
2251
Steven Rostedtdad98752011-11-22 20:48:57 -05002252 # Check if a bisect was running
2253 my $bisect_start_file = "$builddir/.git/BISECT_START";
2254
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002255 my $check = $bisect_check;
Steven Rostedtdad98752011-11-22 20:48:57 -05002256 my $do_check = defined($check) && $check ne "0";
2257
2258 if ( -f $bisect_start_file ) {
2259 print "Bisect in progress found\n";
2260 if ($do_check) {
2261 print " If you say yes, then no checks of good or bad will be done\n";
2262 }
2263 if (defined($replay)) {
2264 print "** BISECT_REPLAY is defined in config file **";
2265 print " Ignore config option and perform new git bisect log?\n";
2266 if (read_ync " (yes, no, or cancel) ") {
2267 $replay = update_bisect_replay;
2268 $do_check = 0;
2269 }
2270 } elsif (read_yn "read git log and continue?") {
2271 $replay = update_bisect_replay;
2272 $do_check = 0;
2273 }
2274 }
2275
2276 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002277
2278 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002279 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002280
2281 if ($check ne "good") {
2282 doprint "TESTING BISECT BAD [$bad]\n";
2283 run_command "git checkout $bad" or
2284 die "Failed to checkout $bad";
2285
2286 $result = run_bisect $type;
2287
2288 if ($result ne "bad") {
2289 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2290 }
2291 }
2292
2293 if ($check ne "bad") {
2294 doprint "TESTING BISECT GOOD [$good]\n";
2295 run_command "git checkout $good" or
2296 die "Failed to checkout $good";
2297
2298 $result = run_bisect $type;
2299
2300 if ($result ne "good") {
2301 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2302 }
2303 }
2304
2305 # checkout where we started
2306 run_command "git checkout $head" or
2307 die "Failed to checkout $head";
2308 }
2309
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002310 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002311 dodie "could not start bisect";
2312
2313 run_command "git bisect good $good" or
2314 dodie "could not set bisect good to $good";
2315
2316 run_git_bisect "git bisect bad $bad" or
2317 dodie "could not set bisect bad to $bad";
2318
2319 if (defined($replay)) {
2320 run_command "git bisect replay $replay" or
2321 dodie "failed to run replay";
2322 }
2323
2324 if (defined($start)) {
2325 run_command "git checkout $start" or
2326 dodie "failed to checkout $start";
2327 }
2328
2329 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002330 do {
2331 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002332 $test = run_git_bisect "git bisect $result";
2333 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002334
2335 run_command "git bisect log" or
2336 dodie "could not capture git bisect log";
2337
2338 run_command "git bisect reset" or
2339 dodie "could not reset git bisect";
2340
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002341 doprint "Bad commit was [$bisect_bad_commit]\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002342
Steven Rostedt0a05c762010-11-08 11:14:10 -05002343 success $i;
2344}
2345
2346my %config_ignore;
2347my %config_set;
2348
2349my %config_list;
2350my %null_config;
2351
2352my %dependency;
2353
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002354sub assign_configs {
2355 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002356
2357 open (IN, $config)
2358 or dodie "Failed to read $config";
2359
2360 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002361 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002362 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002363 }
2364 }
2365
2366 close(IN);
2367}
2368
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002369sub process_config_ignore {
2370 my ($config) = @_;
2371
2372 assign_configs \%config_ignore, $config;
2373}
2374
Steven Rostedt0a05c762010-11-08 11:14:10 -05002375sub read_current_config {
2376 my ($config_ref) = @_;
2377
2378 %{$config_ref} = ();
2379 undef %{$config_ref};
2380
2381 my @key = keys %{$config_ref};
2382 if ($#key >= 0) {
2383 print "did not delete!\n";
2384 exit;
2385 }
2386 open (IN, "$output_config");
2387
2388 while (<IN>) {
2389 if (/^(CONFIG\S+)=(.*)/) {
2390 ${$config_ref}{$1} = $2;
2391 }
2392 }
2393 close(IN);
2394}
2395
2396sub get_dependencies {
2397 my ($config) = @_;
2398
2399 my $arr = $dependency{$config};
2400 if (!defined($arr)) {
2401 return ();
2402 }
2403
2404 my @deps = @{$arr};
2405
2406 foreach my $dep (@{$arr}) {
2407 print "ADD DEP $dep\n";
2408 @deps = (@deps, get_dependencies $dep);
2409 }
2410
2411 return @deps;
2412}
2413
2414sub create_config {
2415 my @configs = @_;
2416
2417 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2418
2419 foreach my $config (@configs) {
2420 print OUT "$config_set{$config}\n";
2421 my @deps = get_dependencies $config;
2422 foreach my $dep (@deps) {
2423 print OUT "$config_set{$dep}\n";
2424 }
2425 }
2426
2427 foreach my $config (keys %config_ignore) {
2428 print OUT "$config_ignore{$config}\n";
2429 }
2430 close(OUT);
2431
2432# exit;
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002433 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002434}
2435
2436sub compare_configs {
2437 my (%a, %b) = @_;
2438
2439 foreach my $item (keys %a) {
2440 if (!defined($b{$item})) {
2441 print "diff $item\n";
2442 return 1;
2443 }
2444 delete $b{$item};
2445 }
2446
2447 my @keys = keys %b;
2448 if ($#keys) {
2449 print "diff2 $keys[0]\n";
2450 }
2451 return -1 if ($#keys >= 0);
2452
2453 return 0;
2454}
2455
2456sub run_config_bisect_test {
2457 my ($type) = @_;
2458
2459 return run_bisect_test $type, "oldconfig";
2460}
2461
2462sub process_passed {
2463 my (%configs) = @_;
2464
2465 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2466 # Passed! All these configs are part of a good compile.
2467 # Add them to the min options.
2468 foreach my $config (keys %configs) {
2469 if (defined($config_list{$config})) {
2470 doprint " removing $config\n";
2471 $config_ignore{$config} = $config_list{$config};
2472 delete $config_list{$config};
2473 }
2474 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002475 doprint "config copied to $outputdir/config_good\n";
2476 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002477}
2478
2479sub process_failed {
2480 my ($config) = @_;
2481
2482 doprint "\n\n***************************************\n";
2483 doprint "Found bad config: $config\n";
2484 doprint "***************************************\n\n";
2485}
2486
2487sub run_config_bisect {
2488
2489 my @start_list = keys %config_list;
2490
2491 if ($#start_list < 0) {
2492 doprint "No more configs to test!!!\n";
2493 return -1;
2494 }
2495
2496 doprint "***** RUN TEST ***\n";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002497 my $type = $config_bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002498 my $ret;
2499 my %current_config;
2500
2501 my $count = $#start_list + 1;
2502 doprint " $count configs to test\n";
2503
2504 my $half = int($#start_list / 2);
2505
2506 do {
2507 my @tophalf = @start_list[0 .. $half];
2508
2509 create_config @tophalf;
2510 read_current_config \%current_config;
2511
2512 $count = $#tophalf + 1;
2513 doprint "Testing $count configs\n";
2514 my $found = 0;
2515 # make sure we test something
2516 foreach my $config (@tophalf) {
2517 if (defined($current_config{$config})) {
2518 logit " $config\n";
2519 $found = 1;
2520 }
2521 }
2522 if (!$found) {
2523 # try the other half
2524 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002525 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedt0a05c762010-11-08 11:14:10 -05002526 create_config @tophalf;
2527 read_current_config \%current_config;
2528 foreach my $config (@tophalf) {
2529 if (defined($current_config{$config})) {
2530 logit " $config\n";
2531 $found = 1;
2532 }
2533 }
2534 if (!$found) {
2535 doprint "Failed: Can't make new config with current configs\n";
2536 foreach my $config (@start_list) {
2537 doprint " CONFIG: $config\n";
2538 }
2539 return -1;
2540 }
2541 $count = $#tophalf + 1;
2542 doprint "Testing $count configs\n";
2543 }
2544
2545 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002546 if ($bisect_manual) {
2547 $ret = answer_bisect;
2548 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002549 if ($ret) {
2550 process_passed %current_config;
2551 return 0;
2552 }
2553
2554 doprint "This config had a failure.\n";
2555 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002556 doprint "config copied to $outputdir/config_bad\n";
2557 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002558
2559 # A config exists in this group that was bad.
2560 foreach my $config (keys %config_list) {
2561 if (!defined($current_config{$config})) {
2562 doprint " removing $config\n";
2563 delete $config_list{$config};
2564 }
2565 }
2566
2567 @start_list = @tophalf;
2568
2569 if ($#start_list == 0) {
2570 process_failed $start_list[0];
2571 return 1;
2572 }
2573
2574 # remove half the configs we are looking at and see if
2575 # they are good.
2576 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002577 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002578
Steven Rostedtc960bb92011-03-08 09:22:39 -05002579 # we found a single config, try it again unless we are running manually
2580
2581 if ($bisect_manual) {
2582 process_failed $start_list[0];
2583 return 1;
2584 }
2585
Steven Rostedt0a05c762010-11-08 11:14:10 -05002586 my @tophalf = @start_list[0 .. 0];
2587
2588 $ret = run_config_bisect_test $type;
2589 if ($ret) {
2590 process_passed %current_config;
2591 return 0;
2592 }
2593
2594 process_failed $start_list[0];
2595 return 1;
2596}
2597
2598sub config_bisect {
2599 my ($i) = @_;
2600
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002601 my $start_config = $config_bisect;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002602
2603 my $tmpconfig = "$tmpdir/use_config";
2604
Steven Rostedt30f75da2011-06-13 10:35:35 -04002605 if (defined($config_bisect_good)) {
2606 process_config_ignore $config_bisect_good;
2607 }
2608
Steven Rostedt0a05c762010-11-08 11:14:10 -05002609 # Make the file with the bad config and the min config
2610 if (defined($minconfig)) {
2611 # read the min config for things to ignore
2612 run_command "cp $minconfig $tmpconfig" or
2613 dodie "failed to copy $minconfig to $tmpconfig";
2614 } else {
2615 unlink $tmpconfig;
2616 }
2617
Steven Rostedt0a05c762010-11-08 11:14:10 -05002618 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002619 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002620 process_config_ignore $tmpconfig;
2621 }
2622
2623 # now process the start config
2624 run_command "cp $start_config $output_config" or
2625 dodie "failed to copy $start_config to $output_config";
2626
2627 # read directly what we want to check
2628 my %config_check;
2629 open (IN, $output_config)
2630 or dodie "faied to open $output_config";
2631
2632 while (<IN>) {
2633 if (/^((CONFIG\S*)=.*)/) {
2634 $config_check{$2} = $1;
2635 }
2636 }
2637 close(IN);
2638
Steven Rostedt250bae82011-07-15 22:05:59 -04002639 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002640 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002641
2642 # check to see what we lost (or gained)
2643 open (IN, $output_config)
2644 or dodie "Failed to read $start_config";
2645
2646 my %removed_configs;
2647 my %added_configs;
2648
2649 while (<IN>) {
2650 if (/^((CONFIG\S*)=.*)/) {
2651 # save off all options
2652 $config_set{$2} = $1;
2653 if (defined($config_check{$2})) {
2654 if (defined($config_ignore{$2})) {
2655 $removed_configs{$2} = $1;
2656 } else {
2657 $config_list{$2} = $1;
2658 }
2659 } elsif (!defined($config_ignore{$2})) {
2660 $added_configs{$2} = $1;
2661 $config_list{$2} = $1;
2662 }
2663 }
2664 }
2665 close(IN);
2666
2667 my @confs = keys %removed_configs;
2668 if ($#confs >= 0) {
2669 doprint "Configs overridden by default configs and removed from check:\n";
2670 foreach my $config (@confs) {
2671 doprint " $config\n";
2672 }
2673 }
2674 @confs = keys %added_configs;
2675 if ($#confs >= 0) {
2676 doprint "Configs appearing in make oldconfig and added:\n";
2677 foreach my $config (@confs) {
2678 doprint " $config\n";
2679 }
2680 }
2681
2682 my %config_test;
2683 my $once = 0;
2684
2685 # Sometimes kconfig does weird things. We must make sure
2686 # that the config we autocreate has everything we need
2687 # to test, otherwise we may miss testing configs, or
2688 # may not be able to create a new config.
2689 # Here we create a config with everything set.
2690 create_config (keys %config_list);
2691 read_current_config \%config_test;
2692 foreach my $config (keys %config_list) {
2693 if (!defined($config_test{$config})) {
2694 if (!$once) {
2695 $once = 1;
2696 doprint "Configs not produced by kconfig (will not be checked):\n";
2697 }
2698 doprint " $config\n";
2699 delete $config_list{$config};
2700 }
2701 }
2702 my $ret;
2703 do {
2704 $ret = run_config_bisect;
2705 } while (!$ret);
2706
2707 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002708
2709 success $i;
2710}
2711
Steven Rostedt27d934b2011-05-20 09:18:18 -04002712sub patchcheck_reboot {
2713 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002714 reboot_to_good $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04002715}
2716
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002717sub patchcheck {
2718 my ($i) = @_;
2719
2720 die "PATCHCHECK_START[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002721 if (!defined($patchcheck_start));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002722 die "PATCHCHECK_TYPE[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002723 if (!defined($patchcheck_type));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002724
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002725 my $start = $patchcheck_start;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002726
2727 my $end = "HEAD";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002728 if (defined($patchcheck_end)) {
2729 $end = $patchcheck_end;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002730 }
2731
Steven Rostedta57419b2010-11-02 15:13:54 -04002732 # Get the true sha1's since we can use things like HEAD~3
2733 $start = get_sha1($start);
2734 $end = get_sha1($end);
2735
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002736 my $type = $patchcheck_type;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002737
2738 # Can't have a test without having a test to run
2739 if ($type eq "test" && !defined($run_test)) {
2740 $type = "boot";
2741 }
2742
2743 open (IN, "git log --pretty=oneline $end|") or
2744 dodie "could not get git list";
2745
2746 my @list;
2747
2748 while (<IN>) {
2749 chomp;
2750 $list[$#list+1] = $_;
2751 last if (/^$start/);
2752 }
2753 close(IN);
2754
2755 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002756 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002757 }
2758
2759 # go backwards in the list
2760 @list = reverse @list;
2761
2762 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04002763 my %ignored_warnings;
2764
2765 if (defined($ignore_warnings)) {
2766 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2767 $ignored_warnings{$sha1} = 1;
2768 }
2769 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002770
2771 $in_patchcheck = 1;
2772 foreach my $item (@list) {
2773 my $sha1 = $item;
2774 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2775
2776 doprint "\nProcessing commit $item\n\n";
2777
2778 run_command "git checkout $sha1" or
2779 die "Failed to checkout $sha1";
2780
2781 # only clean on the first and last patch
2782 if ($item eq $list[0] ||
2783 $item eq $list[$#list]) {
2784 $noclean = $save_clean;
2785 } else {
2786 $noclean = 1;
2787 }
2788
2789 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002790 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002791 } else {
2792 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002793 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002794 }
2795
Steven Rostedt19902072011-06-14 20:46:25 -04002796
2797 if (!defined($ignored_warnings{$sha1})) {
2798 check_buildlog $sha1 or return 0;
2799 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002800
2801 next if ($type eq "build");
2802
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002803 my $failed = 0;
2804
Steven Rostedtddf607e2011-06-14 20:49:13 -04002805 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002806
2807 if (!$failed && $type ne "boot"){
2808 do_run_test or $failed = 1;
2809 }
2810 end_monitor;
2811 return 0 if ($failed);
2812
Steven Rostedt27d934b2011-05-20 09:18:18 -04002813 patchcheck_reboot;
2814
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002815 }
2816 $in_patchcheck = 0;
2817 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002818
2819 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002820}
2821
Steven Rostedtb9066f62011-07-15 21:25:24 -04002822my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04002823my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002824my $iflevel = 0;
2825my @ifdeps;
2826
2827# prevent recursion
2828my %read_kconfigs;
2829
Steven Rostedtac6974c2011-10-04 09:40:17 -04002830sub add_dep {
2831 # $config depends on $dep
2832 my ($config, $dep) = @_;
2833
2834 if (defined($depends{$config})) {
2835 $depends{$config} .= " " . $dep;
2836 } else {
2837 $depends{$config} = $dep;
2838 }
2839
2840 # record the number of configs depending on $dep
2841 if (defined $depcount{$dep}) {
2842 $depcount{$dep}++;
2843 } else {
2844 $depcount{$dep} = 1;
2845 }
2846}
2847
Steven Rostedtb9066f62011-07-15 21:25:24 -04002848# taken from streamline_config.pl
2849sub read_kconfig {
2850 my ($kconfig) = @_;
2851
2852 my $state = "NONE";
2853 my $config;
2854 my @kconfigs;
2855
2856 my $cont = 0;
2857 my $line;
2858
2859
2860 if (! -f $kconfig) {
2861 doprint "file $kconfig does not exist, skipping\n";
2862 return;
2863 }
2864
2865 open(KIN, "$kconfig")
2866 or die "Can't open $kconfig";
2867 while (<KIN>) {
2868 chomp;
2869
2870 # Make sure that lines ending with \ continue
2871 if ($cont) {
2872 $_ = $line . " " . $_;
2873 }
2874
2875 if (s/\\$//) {
2876 $cont = 1;
2877 $line = $_;
2878 next;
2879 }
2880
2881 $cont = 0;
2882
2883 # collect any Kconfig sources
2884 if (/^source\s*"(.*)"/) {
2885 $kconfigs[$#kconfigs+1] = $1;
2886 }
2887
2888 # configs found
2889 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2890 $state = "NEW";
2891 $config = $2;
2892
2893 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04002894 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04002895 }
2896
2897 # collect the depends for the config
2898 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2899
Steven Rostedtac6974c2011-10-04 09:40:17 -04002900 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002901
2902 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04002903 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2904
2905 # selected by depends on config
2906 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002907
2908 # Check for if statements
2909 } elsif (/^if\s+(.*\S)\s*$/) {
2910 my $deps = $1;
2911 # remove beginning and ending non text
2912 $deps =~ s/^[^a-zA-Z0-9_]*//;
2913 $deps =~ s/[^a-zA-Z0-9_]*$//;
2914
2915 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2916
2917 $ifdeps[$iflevel++] = join ':', @deps;
2918
2919 } elsif (/^endif/) {
2920
2921 $iflevel-- if ($iflevel);
2922
2923 # stop on "help"
2924 } elsif (/^\s*help\s*$/) {
2925 $state = "NONE";
2926 }
2927 }
2928 close(KIN);
2929
2930 # read in any configs that were found.
2931 foreach $kconfig (@kconfigs) {
2932 if (!defined($read_kconfigs{$kconfig})) {
2933 $read_kconfigs{$kconfig} = 1;
2934 read_kconfig("$builddir/$kconfig");
2935 }
2936 }
2937}
2938
2939sub read_depends {
2940 # find out which arch this is by the kconfig file
2941 open (IN, $output_config)
2942 or dodie "Failed to read $output_config";
2943 my $arch;
2944 while (<IN>) {
2945 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2946 $arch = $1;
2947 last;
2948 }
2949 }
2950 close IN;
2951
2952 if (!defined($arch)) {
2953 doprint "Could not find arch from config file\n";
2954 doprint "no dependencies used\n";
2955 return;
2956 }
2957
2958 # arch is really the subarch, we need to know
2959 # what directory to look at.
2960 if ($arch eq "i386" || $arch eq "x86_64") {
2961 $arch = "x86";
2962 } elsif ($arch =~ /^tile/) {
2963 $arch = "tile";
2964 }
2965
2966 my $kconfig = "$builddir/arch/$arch/Kconfig";
2967
2968 if (! -f $kconfig && $arch =~ /\d$/) {
2969 my $orig = $arch;
2970 # some subarchs have numbers, truncate them
2971 $arch =~ s/\d*$//;
2972 $kconfig = "$builddir/arch/$arch/Kconfig";
2973 if (! -f $kconfig) {
2974 doprint "No idea what arch dir $orig is for\n";
2975 doprint "no dependencies used\n";
2976 return;
2977 }
2978 }
2979
2980 read_kconfig($kconfig);
2981}
2982
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002983sub read_config_list {
2984 my ($config) = @_;
2985
2986 open (IN, $config)
2987 or dodie "Failed to read $config";
2988
2989 while (<IN>) {
2990 if (/^((CONFIG\S*)=.*)/) {
2991 if (!defined($config_ignore{$2})) {
2992 $config_list{$2} = $1;
2993 }
2994 }
2995 }
2996
2997 close(IN);
2998}
2999
3000sub read_output_config {
3001 my ($config) = @_;
3002
3003 assign_configs \%config_ignore, $config;
3004}
3005
3006sub make_new_config {
3007 my @configs = @_;
3008
3009 open (OUT, ">$output_config")
3010 or dodie "Failed to write $output_config";
3011
3012 foreach my $config (@configs) {
3013 print OUT "$config\n";
3014 }
3015 close OUT;
3016}
3017
Steven Rostedtac6974c2011-10-04 09:40:17 -04003018sub chomp_config {
3019 my ($config) = @_;
3020
3021 $config =~ s/CONFIG_//;
3022
3023 return $config;
3024}
3025
Steven Rostedtb9066f62011-07-15 21:25:24 -04003026sub get_depends {
3027 my ($dep) = @_;
3028
Steven Rostedtac6974c2011-10-04 09:40:17 -04003029 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003030
3031 $dep = $depends{"$kconfig"};
3032
3033 # the dep string we have saves the dependencies as they
3034 # were found, including expressions like ! && ||. We
3035 # want to split this out into just an array of configs.
3036
3037 my $valid = "A-Za-z_0-9";
3038
3039 my @configs;
3040
3041 while ($dep =~ /[$valid]/) {
3042
3043 if ($dep =~ /^[^$valid]*([$valid]+)/) {
3044 my $conf = "CONFIG_" . $1;
3045
3046 $configs[$#configs + 1] = $conf;
3047
3048 $dep =~ s/^[^$valid]*[$valid]+//;
3049 } else {
3050 die "this should never happen";
3051 }
3052 }
3053
3054 return @configs;
3055}
3056
3057my %min_configs;
3058my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003059my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003060my %processed_configs;
3061my %nochange_config;
3062
3063sub test_this_config {
3064 my ($config) = @_;
3065
3066 my $found;
3067
3068 # if we already processed this config, skip it
3069 if (defined($processed_configs{$config})) {
3070 return undef;
3071 }
3072 $processed_configs{$config} = 1;
3073
3074 # if this config failed during this round, skip it
3075 if (defined($nochange_config{$config})) {
3076 return undef;
3077 }
3078
Steven Rostedtac6974c2011-10-04 09:40:17 -04003079 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003080
3081 # Test dependencies first
3082 if (defined($depends{"$kconfig"})) {
3083 my @parents = get_depends $config;
3084 foreach my $parent (@parents) {
3085 # if the parent is in the min config, check it first
3086 next if (!defined($min_configs{$parent}));
3087 $found = test_this_config($parent);
3088 if (defined($found)) {
3089 return $found;
3090 }
3091 }
3092 }
3093
3094 # Remove this config from the list of configs
3095 # do a make oldnoconfig and then read the resulting
3096 # .config to make sure it is missing the config that
3097 # we had before
3098 my %configs = %min_configs;
3099 delete $configs{$config};
3100 make_new_config ((values %configs), (values %keep_configs));
3101 make_oldconfig;
3102 undef %configs;
3103 assign_configs \%configs, $output_config;
3104
3105 return $config if (!defined($configs{$config}));
3106
3107 doprint "disabling config $config did not change .config\n";
3108
3109 $nochange_config{$config} = 1;
3110
3111 return undef;
3112}
3113
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003114sub make_min_config {
3115 my ($i) = @_;
3116
3117 if (!defined($output_minconfig)) {
3118 fail "OUTPUT_MIN_CONFIG not defined" and return;
3119 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04003120
3121 # If output_minconfig exists, and the start_minconfig
3122 # came from min_config, than ask if we should use
3123 # that instead.
3124 if (-f $output_minconfig && !$start_minconfig_defined) {
3125 print "$output_minconfig exists\n";
3126 if (read_yn " Use it as minconfig?") {
3127 $start_minconfig = $output_minconfig;
3128 }
3129 }
3130
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003131 if (!defined($start_minconfig)) {
3132 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
3133 }
3134
Steven Rostedt35ce5952011-07-15 21:57:25 -04003135 my $temp_config = "$tmpdir/temp_config";
3136
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003137 # First things first. We build an allnoconfig to find
3138 # out what the defaults are that we can't touch.
3139 # Some are selections, but we really can't handle selections.
3140
3141 my $save_minconfig = $minconfig;
3142 undef $minconfig;
3143
3144 run_command "$make allnoconfig" or return 0;
3145
Steven Rostedtb9066f62011-07-15 21:25:24 -04003146 read_depends;
3147
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003148 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003149
Steven Rostedt43d1b652011-07-15 22:01:56 -04003150 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003151 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003152
3153 if (defined($ignore_config)) {
3154 # make sure the file exists
3155 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003156 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003157 }
3158
Steven Rostedt43d1b652011-07-15 22:01:56 -04003159 %keep_configs = %save_configs;
3160
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003161 doprint "Load initial configs from $start_minconfig\n";
3162
3163 # Look at the current min configs, and save off all the
3164 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003165 assign_configs \%min_configs, $start_minconfig;
3166
3167 my @config_keys = keys %min_configs;
3168
Steven Rostedtac6974c2011-10-04 09:40:17 -04003169 # All configs need a depcount
3170 foreach my $config (@config_keys) {
3171 my $kconfig = chomp_config $config;
3172 if (!defined $depcount{$kconfig}) {
3173 $depcount{$kconfig} = 0;
3174 }
3175 }
3176
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003177 # Remove anything that was set by the make allnoconfig
3178 # we shouldn't need them as they get set for us anyway.
3179 foreach my $config (@config_keys) {
3180 # Remove anything in the ignore_config
3181 if (defined($keep_configs{$config})) {
3182 my $file = $ignore_config;
3183 $file =~ s,.*/(.*?)$,$1,;
3184 doprint "$config set by $file ... ignored\n";
3185 delete $min_configs{$config};
3186 next;
3187 }
3188 # But make sure the settings are the same. If a min config
3189 # sets a selection, we do not want to get rid of it if
3190 # it is not the same as what we have. Just move it into
3191 # the keep configs.
3192 if (defined($config_ignore{$config})) {
3193 if ($config_ignore{$config} ne $min_configs{$config}) {
3194 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3195 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3196 $keep_configs{$config} = $min_configs{$config};
3197 } else {
3198 doprint "$config set by allnoconfig ... ignored\n";
3199 }
3200 delete $min_configs{$config};
3201 }
3202 }
3203
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003204 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003205 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003206
3207 while (!$done) {
3208
3209 my $config;
3210 my $found;
3211
3212 # Now disable each config one by one and do a make oldconfig
3213 # till we find a config that changes our list.
3214
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003215 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003216
3217 # Sort keys by who is most dependent on
3218 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3219 @test_configs ;
3220
3221 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003222 my $reset = 1;
3223 for (my $i = 0; $i < $#test_configs; $i++) {
3224 if (!defined($nochange_config{$test_configs[0]})) {
3225 $reset = 0;
3226 last;
3227 }
3228 # This config didn't change the .config last time.
3229 # Place it at the end
3230 my $config = shift @test_configs;
3231 push @test_configs, $config;
3232 }
3233
3234 # if every test config has failed to modify the .config file
3235 # in the past, then reset and start over.
3236 if ($reset) {
3237 undef %nochange_config;
3238 }
3239
Steven Rostedtb9066f62011-07-15 21:25:24 -04003240 undef %processed_configs;
3241
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003242 foreach my $config (@test_configs) {
3243
Steven Rostedtb9066f62011-07-15 21:25:24 -04003244 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003245
Steven Rostedtb9066f62011-07-15 21:25:24 -04003246 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003247
3248 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003249 }
3250
3251 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003252 # we could have failed due to the nochange_config hash
3253 # reset and try again
3254 if (!$take_two) {
3255 undef %nochange_config;
3256 $take_two = 1;
3257 next;
3258 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003259 doprint "No more configs found that we can disable\n";
3260 $done = 1;
3261 last;
3262 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003263 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003264
3265 $config = $found;
3266
3267 doprint "Test with $config disabled\n";
3268
3269 # set in_bisect to keep build and monitor from dieing
3270 $in_bisect = 1;
3271
3272 my $failed = 0;
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003273 build "oldconfig" or $failed = 1;
3274 if (!$failed) {
3275 start_monitor_and_boot or $failed = 1;
3276 end_monitor;
3277 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003278
3279 $in_bisect = 0;
3280
3281 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003282 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003283 # this config is needed, add it to the ignore list.
3284 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003285 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003286 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003287
3288 # update new ignore configs
3289 if (defined($ignore_config)) {
3290 open (OUT, ">$temp_config")
3291 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003292 foreach my $config (keys %save_configs) {
3293 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003294 }
3295 close OUT;
3296 run_command "mv $temp_config $ignore_config" or
3297 dodie "failed to copy update to $ignore_config";
3298 }
3299
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003300 } else {
3301 # We booted without this config, remove it from the minconfigs.
3302 doprint "$config is not needed, disabling\n";
3303
3304 delete $min_configs{$config};
3305
3306 # Also disable anything that is not enabled in this config
3307 my %configs;
3308 assign_configs \%configs, $output_config;
3309 my @config_keys = keys %min_configs;
3310 foreach my $config (@config_keys) {
3311 if (!defined($configs{$config})) {
3312 doprint "$config is not set, disabling\n";
3313 delete $min_configs{$config};
3314 }
3315 }
3316
3317 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003318 open (OUT, ">$temp_config")
3319 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003320 foreach my $config (keys %keep_configs) {
3321 print OUT "$keep_configs{$config}\n";
3322 }
3323 foreach my $config (keys %min_configs) {
3324 print OUT "$min_configs{$config}\n";
3325 }
3326 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003327
3328 run_command "mv $temp_config $output_minconfig" or
3329 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003330 }
3331
3332 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003333 reboot_to_good $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003334 }
3335
3336 success $i;
3337 return 1;
3338}
3339
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003340$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003341
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003342if ($#ARGV == 0) {
3343 $ktest_config = $ARGV[0];
3344 if (! -f $ktest_config) {
3345 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003346 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003347 exit 0;
3348 }
3349 }
3350} else {
3351 $ktest_config = "ktest.conf";
3352}
3353
3354if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003355 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003356 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003357 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3358 print OUT << "EOF"
3359# Generated by ktest.pl
3360#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003361
3362# PWD is a ktest.pl variable that will result in the process working
3363# directory that ktest.pl is executed in.
3364
3365# THIS_DIR is automatically assigned the PWD of the path that generated
3366# the config file. It is best to use this variable when assigning other
3367# directory paths within this directory. This allows you to easily
3368# move the test cases to other locations or to other machines.
3369#
3370THIS_DIR := $variable{"PWD"}
3371
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003372# Define each test with TEST_START
3373# The config options below it will override the defaults
3374TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003375TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003376
3377DEFAULTS
3378EOF
3379;
3380 close(OUT);
3381}
3382read_config $ktest_config;
3383
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003384if (defined($opt{"LOG_FILE"})) {
3385 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3386}
3387
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003388# Append any configs entered in manually to the config file.
3389my @new_configs = keys %entered_configs;
3390if ($#new_configs >= 0) {
3391 print "\nAppending entered in configs to $ktest_config\n";
3392 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3393 foreach my $config (@new_configs) {
3394 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003395 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003396 }
3397}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003398
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003399if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3400 unlink $opt{"LOG_FILE"};
3401}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003402
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003403doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3404
Steven Rostedta57419b2010-11-02 15:13:54 -04003405for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3406
3407 if (!$i) {
3408 doprint "DEFAULT OPTIONS:\n";
3409 } else {
3410 doprint "\nTEST $i OPTIONS";
3411 if (defined($repeat_tests{$i})) {
3412 $repeat = $repeat_tests{$i};
3413 doprint " ITERATE $repeat";
3414 }
3415 doprint "\n";
3416 }
3417
3418 foreach my $option (sort keys %opt) {
3419
3420 if ($option =~ /\[(\d+)\]$/) {
3421 next if ($i != $1);
3422 } else {
3423 next if ($i);
3424 }
3425
3426 doprint "$option = $opt{$option}\n";
3427 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003428}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003429
Steven Rostedt2a625122011-05-20 15:48:59 -04003430sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003431 my ($name, $i) = @_;
3432
3433 my $option = "$name\[$i\]";
3434
3435 if (defined($opt{$option})) {
3436 return $opt{$option};
3437 }
3438
Steven Rostedta57419b2010-11-02 15:13:54 -04003439 foreach my $test (keys %repeat_tests) {
3440 if ($i >= $test &&
3441 $i < $test + $repeat_tests{$test}) {
3442 $option = "$name\[$test\]";
3443 if (defined($opt{$option})) {
3444 return $opt{$option};
3445 }
3446 }
3447 }
3448
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003449 if (defined($opt{$name})) {
3450 return $opt{$name};
3451 }
3452
3453 return undef;
3454}
3455
Steven Rostedt2a625122011-05-20 15:48:59 -04003456sub set_test_option {
3457 my ($name, $i) = @_;
3458
3459 my $option = __set_test_option($name, $i);
3460 return $option if (!defined($option));
3461
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003462 return eval_option($option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003463}
3464
Steven Rostedt2545eb62010-11-02 15:01:32 -04003465# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003466for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003467
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003468 # Do not reboot on failing test options
3469 $no_reboot = 1;
3470
Steven Rostedt576f6272010-11-02 14:58:38 -04003471 $iteration = $i;
3472
Steven Rostedta75fece2010-11-02 14:58:27 -04003473 my $makecmd = set_test_option("MAKE_CMD", $i);
3474
Steven Rostedt9cc9e092011-12-22 21:37:22 -05003475 # Load all the options into their mapped variable names
3476 foreach my $opt (keys %option_map) {
3477 ${$option_map{$opt}} = set_test_option($opt, $i);
3478 }
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003479
Steven Rostedt35ce5952011-07-15 21:57:25 -04003480 $start_minconfig_defined = 1;
3481
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003482 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003483 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003484 $start_minconfig = $minconfig;
3485 }
3486
Steven Rostedta75fece2010-11-02 14:58:27 -04003487 chdir $builddir || die "can't change directory to $builddir";
3488
Andrew Jonesa908a662011-08-12 15:32:03 +02003489 foreach my $dir ($tmpdir, $outputdir) {
3490 if (!-d $dir) {
3491 mkpath($dir) or
3492 die "can't create $dir";
3493 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003494 }
3495
Steven Rostedte48c5292010-11-02 14:35:37 -04003496 $ENV{"SSH_USER"} = $ssh_user;
3497 $ENV{"MACHINE"} = $machine;
3498
Steven Rostedta75fece2010-11-02 14:58:27 -04003499 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303500 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003501 $dmesg = "$tmpdir/dmesg-$machine";
3502 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003503 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003504
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003505 if (!$buildonly) {
3506 $target = "$ssh_user\@$machine";
3507 if ($reboot_type eq "grub") {
3508 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003509 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003510 }
3511
3512 my $run_type = $build_type;
3513 if ($test_type eq "patchcheck") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003514 $run_type = $patchcheck_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003515 } elsif ($test_type eq "bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003516 $run_type = $bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003517 } elsif ($test_type eq "config_bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003518 $run_type = $config_bisect_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003519 }
3520
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003521 if ($test_type eq "make_min_config") {
3522 $run_type = "";
3523 }
3524
Steven Rostedta75fece2010-11-02 14:58:27 -04003525 # mistake in config file?
3526 if (!defined($run_type)) {
3527 $run_type = "ERROR";
3528 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003529
Steven Rostedte0a87422011-09-30 17:50:48 -04003530 my $installme = "";
3531 $installme = " no_install" if ($no_install);
3532
Steven Rostedt2545eb62010-11-02 15:01:32 -04003533 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003534 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003535
3536 unlink $dmesg;
3537 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303538 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003539
Steven Rostedt250bae82011-07-15 22:05:59 -04003540 if (defined($addconfig)) {
3541 my $min = $minconfig;
3542 if (!defined($minconfig)) {
3543 $min = "";
3544 }
3545 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003546 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003547 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003548 }
3549
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003550 if (defined($checkout)) {
3551 run_command "git checkout $checkout" or
3552 die "failed to checkout $checkout";
3553 }
3554
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003555 $no_reboot = 0;
3556
3557
Steven Rostedta75fece2010-11-02 14:58:27 -04003558 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003559 bisect $i;
3560 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003561 } elsif ($test_type eq "config_bisect") {
3562 config_bisect $i;
3563 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003564 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003565 patchcheck $i;
3566 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003567 } elsif ($test_type eq "make_min_config") {
3568 make_min_config $i;
3569 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003570 }
3571
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003572 if ($build_type ne "nobuild") {
3573 build $build_type or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003574 }
3575
Steven Rostedtcd8e3682011-08-18 16:35:44 -04003576 if ($test_type eq "install") {
3577 get_version;
3578 install;
3579 success $i;
3580 next;
3581 }
3582
Steven Rostedta75fece2010-11-02 14:58:27 -04003583 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04003584 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04003585 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04003586
3587 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3588 do_run_test or $failed = 1;
3589 }
3590 end_monitor;
3591 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003592 }
3593
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003594 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003595}
3596
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003597if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003598 halt;
Steven Rostedt576f6272010-11-02 14:58:38 -04003599} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003600 reboot_to_good;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003601}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003602
Steven Rostedte48c5292010-11-02 14:35:37 -04003603doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3604
Steven Rostedt2545eb62010-11-02 15:01:32 -04003605exit 0;