blob: b6de81927cc3e279403056adb5668a0a174df0a7 [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,
Steven Rostedtccc513b2012-05-21 17:13:40 -040042 "MIN_CONFIG_TYPE" => "boot",
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050043 "SUCCESS_LINE" => "login:",
44 "DETECT_TRIPLE_FAULT" => 1,
45 "NO_INSTALL" => 0,
46 "BOOTED_TIMEOUT" => 1,
47 "DIE_ON_FAILURE" => 1,
48 "SSH_EXEC" => "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND",
49 "SCP_TO_TARGET" => "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE",
Steven Rostedt02ad2612012-03-21 08:21:24 -040050 "SCP_TO_TARGET_INSTALL" => "\${SCP_TO_TARGET}",
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050051 "REBOOT" => "ssh \$SSH_USER\@\$MACHINE reboot",
52 "STOP_AFTER_SUCCESS" => 10,
53 "STOP_AFTER_FAILURE" => 60,
54 "STOP_TEST_AFTER" => 600,
Steven Rostedt600bbf02011-11-21 20:12:04 -050055
56# required, and we will ask users if they don't have them but we keep the default
57# value something that is common.
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050058 "REBOOT_TYPE" => "grub",
59 "LOCALVERSION" => "-test",
60 "SSH_USER" => "root",
61 "BUILD_TARGET" => "arch/x86/boot/bzImage",
62 "TARGET_IMAGE" => "/boot/vmlinuz-test",
Steven Rostedt9cc9e092011-12-22 21:37:22 -050063
64 "LOG_FILE" => undef,
65 "IGNORE_UNUSED" => 0,
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050066);
Steven Rostedt2545eb62010-11-02 15:01:32 -040067
Steven Rostedt8d1491b2010-11-18 15:39:48 -050068my $ktest_config;
Steven Rostedt2545eb62010-11-02 15:01:32 -040069my $version;
Steven Rostedt683a3e62012-05-18 13:34:35 -040070my $have_version = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -040071my $machine;
Steven Rostedte48c5292010-11-02 14:35:37 -040072my $ssh_user;
Steven Rostedta75fece2010-11-02 14:58:27 -040073my $tmpdir;
74my $builddir;
75my $outputdir;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -050076my $output_config;
Steven Rostedta75fece2010-11-02 14:58:27 -040077my $test_type;
Steven Rostedt7faafbd2010-11-02 14:58:22 -040078my $build_type;
Steven Rostedta75fece2010-11-02 14:58:27 -040079my $build_options;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -040080my $pre_build;
81my $post_build;
82my $pre_build_die;
83my $post_build_die;
Steven Rostedta75fece2010-11-02 14:58:27 -040084my $reboot_type;
85my $reboot_script;
86my $power_cycle;
Steven Rostedte48c5292010-11-02 14:35:37 -040087my $reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -040088my $reboot_on_error;
Steven Rostedtbc7c5802011-12-22 16:29:10 -050089my $switch_to_good;
90my $switch_to_test;
Steven Rostedta75fece2010-11-02 14:58:27 -040091my $poweroff_on_error;
Steven Rostedt648a1822012-03-21 11:18:27 -040092my $reboot_on_success;
Steven Rostedta75fece2010-11-02 14:58:27 -040093my $die_on_failure;
Steven Rostedt576f6272010-11-02 14:58:38 -040094my $powercycle_after_reboot;
95my $poweroff_after_halt;
Steven Rostedte48c5292010-11-02 14:35:37 -040096my $ssh_exec;
97my $scp_to_target;
Steven Rostedt02ad2612012-03-21 08:21:24 -040098my $scp_to_target_install;
Steven Rostedta75fece2010-11-02 14:58:27 -040099my $power_off;
100my $grub_menu;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400101my $grub_number;
102my $target;
103my $make;
Steven Rostedt8b37ca82010-11-02 14:58:33 -0400104my $post_install;
Steven Rostedte0a87422011-09-30 17:50:48 -0400105my $no_install;
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400106my $noclean;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400107my $minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400108my $start_minconfig;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400109my $start_minconfig_defined;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400110my $output_minconfig;
Steven Rostedtccc513b2012-05-21 17:13:40 -0400111my $minconfig_type;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400112my $ignore_config;
Steven Rostedtbe405f92012-01-04 21:51:59 -0500113my $ignore_errors;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -0400114my $addconfig;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400115my $in_bisect = 0;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500116my $bisect_bad_commit = "";
Steven Rostedtd6ce2a02010-11-02 14:58:05 -0400117my $reverse_bisect;
Steven Rostedtc960bb92011-03-08 09:22:39 -0500118my $bisect_manual;
Steven Rostedtc23dca72011-03-08 09:26:31 -0500119my $bisect_skip;
Steven Rostedt30f75da2011-06-13 10:35:35 -0400120my $config_bisect_good;
Steven Rostedtc5dacb82011-12-22 12:43:57 -0500121my $bisect_ret_good;
122my $bisect_ret_bad;
123my $bisect_ret_skip;
124my $bisect_ret_abort;
125my $bisect_ret_default;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400126my $in_patchcheck = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400127my $run_test;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400128my $redirect;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400129my $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +0530130my $testlog;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400131my $dmesg;
132my $monitor_fp;
133my $monitor_pid;
134my $monitor_cnt = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -0400135my $sleep_time;
136my $bisect_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -0400137my $patchcheck_sleep_time;
Steven Rostedt19902072011-06-14 20:46:25 -0400138my $ignore_warnings;
Steven Rostedta75fece2010-11-02 14:58:27 -0400139my $store_failures;
Rabin Vincentde5b6e32011-11-18 17:05:31 +0530140my $store_successes;
Steven Rostedt9064af52011-06-13 10:38:48 -0400141my $test_name;
Steven Rostedta75fece2010-11-02 14:58:27 -0400142my $timeout;
143my $booted_timeout;
Steven Rostedtf1a5b962011-06-13 10:30:00 -0400144my $detect_triplefault;
Steven Rostedta75fece2010-11-02 14:58:27 -0400145my $console;
Steven Rostedt2b803362011-09-30 18:00:23 -0400146my $reboot_success_line;
Steven Rostedta75fece2010-11-02 14:58:27 -0400147my $success_line;
Steven Rostedt1c8a6172010-11-09 12:55:40 -0500148my $stop_after_success;
149my $stop_after_failure;
Steven Rostedt2d01b262011-03-08 09:47:54 -0500150my $stop_test_after;
Steven Rostedta75fece2010-11-02 14:58:27 -0400151my $build_target;
152my $target_image;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500153my $checkout;
Steven Rostedta75fece2010-11-02 14:58:27 -0400154my $localversion;
Steven Rostedt576f6272010-11-02 14:58:38 -0400155my $iteration = 0;
Steven Rostedte48c5292010-11-02 14:35:37 -0400156my $successes = 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400157
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500158my $bisect_good;
159my $bisect_bad;
160my $bisect_type;
161my $bisect_start;
162my $bisect_replay;
163my $bisect_files;
164my $bisect_reverse;
165my $bisect_check;
166
167my $config_bisect;
168my $config_bisect_type;
169
170my $patchcheck_type;
171my $patchcheck_start;
172my $patchcheck_end;
173
Steven Rostedt165708b2011-11-26 20:56:52 -0500174# set when a test is something other that just building or install
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500175# which would require more options.
176my $buildonly = 1;
177
Steven Rostedtdbd37832011-11-23 16:00:48 -0500178# set when creating a new config
179my $newconfig = 0;
180
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500181my %entered_configs;
182my %config_help;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400183my %variable;
Steven Rostedtfcb3f162011-06-13 10:40:58 -0400184my %force_config;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500185
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400186# do not force reboots on config problems
187my $no_reboot = 1;
188
Steven Rostedt759a3cc2012-05-01 08:20:12 -0400189# reboot on success
190my $reboot_success = 0;
191
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500192my %option_map = (
193 "MACHINE" => \$machine,
194 "SSH_USER" => \$ssh_user,
195 "TMP_DIR" => \$tmpdir,
196 "OUTPUT_DIR" => \$outputdir,
197 "BUILD_DIR" => \$builddir,
198 "TEST_TYPE" => \$test_type,
199 "BUILD_TYPE" => \$build_type,
200 "BUILD_OPTIONS" => \$build_options,
201 "PRE_BUILD" => \$pre_build,
202 "POST_BUILD" => \$post_build,
203 "PRE_BUILD_DIE" => \$pre_build_die,
204 "POST_BUILD_DIE" => \$post_build_die,
205 "POWER_CYCLE" => \$power_cycle,
206 "REBOOT" => \$reboot,
207 "BUILD_NOCLEAN" => \$noclean,
208 "MIN_CONFIG" => \$minconfig,
209 "OUTPUT_MIN_CONFIG" => \$output_minconfig,
210 "START_MIN_CONFIG" => \$start_minconfig,
Steven Rostedtccc513b2012-05-21 17:13:40 -0400211 "MIN_CONFIG_TYPE" => \$minconfig_type,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500212 "IGNORE_CONFIG" => \$ignore_config,
213 "TEST" => \$run_test,
214 "ADD_CONFIG" => \$addconfig,
215 "REBOOT_TYPE" => \$reboot_type,
216 "GRUB_MENU" => \$grub_menu,
217 "POST_INSTALL" => \$post_install,
218 "NO_INSTALL" => \$no_install,
219 "REBOOT_SCRIPT" => \$reboot_script,
220 "REBOOT_ON_ERROR" => \$reboot_on_error,
221 "SWITCH_TO_GOOD" => \$switch_to_good,
222 "SWITCH_TO_TEST" => \$switch_to_test,
223 "POWEROFF_ON_ERROR" => \$poweroff_on_error,
Steven Rostedt648a1822012-03-21 11:18:27 -0400224 "REBOOT_ON_SUCCESS" => \$reboot_on_success,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500225 "DIE_ON_FAILURE" => \$die_on_failure,
226 "POWER_OFF" => \$power_off,
227 "POWERCYCLE_AFTER_REBOOT" => \$powercycle_after_reboot,
228 "POWEROFF_AFTER_HALT" => \$poweroff_after_halt,
229 "SLEEP_TIME" => \$sleep_time,
230 "BISECT_SLEEP_TIME" => \$bisect_sleep_time,
231 "PATCHCHECK_SLEEP_TIME" => \$patchcheck_sleep_time,
232 "IGNORE_WARNINGS" => \$ignore_warnings,
Steven Rostedtbe405f92012-01-04 21:51:59 -0500233 "IGNORE_ERRORS" => \$ignore_errors,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500234 "BISECT_MANUAL" => \$bisect_manual,
235 "BISECT_SKIP" => \$bisect_skip,
236 "CONFIG_BISECT_GOOD" => \$config_bisect_good,
237 "BISECT_RET_GOOD" => \$bisect_ret_good,
238 "BISECT_RET_BAD" => \$bisect_ret_bad,
239 "BISECT_RET_SKIP" => \$bisect_ret_skip,
240 "BISECT_RET_ABORT" => \$bisect_ret_abort,
241 "BISECT_RET_DEFAULT" => \$bisect_ret_default,
242 "STORE_FAILURES" => \$store_failures,
243 "STORE_SUCCESSES" => \$store_successes,
244 "TEST_NAME" => \$test_name,
245 "TIMEOUT" => \$timeout,
246 "BOOTED_TIMEOUT" => \$booted_timeout,
247 "CONSOLE" => \$console,
248 "DETECT_TRIPLE_FAULT" => \$detect_triplefault,
249 "SUCCESS_LINE" => \$success_line,
250 "REBOOT_SUCCESS_LINE" => \$reboot_success_line,
251 "STOP_AFTER_SUCCESS" => \$stop_after_success,
252 "STOP_AFTER_FAILURE" => \$stop_after_failure,
253 "STOP_TEST_AFTER" => \$stop_test_after,
254 "BUILD_TARGET" => \$build_target,
255 "SSH_EXEC" => \$ssh_exec,
256 "SCP_TO_TARGET" => \$scp_to_target,
Steven Rostedt02ad2612012-03-21 08:21:24 -0400257 "SCP_TO_TARGET_INSTALL" => \$scp_to_target_install,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500258 "CHECKOUT" => \$checkout,
259 "TARGET_IMAGE" => \$target_image,
260 "LOCALVERSION" => \$localversion,
261
262 "BISECT_GOOD" => \$bisect_good,
263 "BISECT_BAD" => \$bisect_bad,
264 "BISECT_TYPE" => \$bisect_type,
265 "BISECT_START" => \$bisect_start,
266 "BISECT_REPLAY" => \$bisect_replay,
267 "BISECT_FILES" => \$bisect_files,
268 "BISECT_REVERSE" => \$bisect_reverse,
269 "BISECT_CHECK" => \$bisect_check,
270
271 "CONFIG_BISECT" => \$config_bisect,
272 "CONFIG_BISECT_TYPE" => \$config_bisect_type,
273
274 "PATCHCHECK_TYPE" => \$patchcheck_type,
275 "PATCHCHECK_START" => \$patchcheck_start,
276 "PATCHCHECK_END" => \$patchcheck_end,
277);
278
279# Options may be used by other options, record them.
280my %used_options;
281
Steven Rostedt7bf51072011-10-22 09:07:03 -0400282# default variables that can be used
283chomp ($variable{"PWD"} = `pwd`);
284
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500285$config_help{"MACHINE"} = << "EOF"
286 The machine hostname that you will test.
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500287 For build only tests, it is still needed to differentiate log files.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500288EOF
289 ;
290$config_help{"SSH_USER"} = << "EOF"
291 The box is expected to have ssh on normal bootup, provide the user
292 (most likely root, since you need privileged operations)
293EOF
294 ;
295$config_help{"BUILD_DIR"} = << "EOF"
296 The directory that contains the Linux source code (full path).
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500297 You can use \${PWD} that will be the path where ktest.pl is run, or use
298 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500299EOF
300 ;
301$config_help{"OUTPUT_DIR"} = << "EOF"
302 The directory that the objects will be built (full path).
303 (can not be same as BUILD_DIR)
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500304 You can use \${PWD} that will be the path where ktest.pl is run, or use
305 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500306EOF
307 ;
308$config_help{"BUILD_TARGET"} = << "EOF"
309 The location of the compiled file to copy to the target.
310 (relative to OUTPUT_DIR)
311EOF
312 ;
Steven Rostedtdbd37832011-11-23 16:00:48 -0500313$config_help{"BUILD_OPTIONS"} = << "EOF"
314 Options to add to \"make\" when building.
315 i.e. -j20
316EOF
317 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500318$config_help{"TARGET_IMAGE"} = << "EOF"
319 The place to put your image on the test machine.
320EOF
321 ;
322$config_help{"POWER_CYCLE"} = << "EOF"
323 A script or command to reboot the box.
324
325 Here is a digital loggers power switch example
326 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
327
328 Here is an example to reboot a virtual box on the current host
329 with the name "Guest".
330 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
331EOF
332 ;
333$config_help{"CONSOLE"} = << "EOF"
334 The script or command that reads the console
335
336 If you use ttywatch server, something like the following would work.
337CONSOLE = nc -d localhost 3001
338
339 For a virtual machine with guest name "Guest".
340CONSOLE = virsh console Guest
341EOF
342 ;
343$config_help{"LOCALVERSION"} = << "EOF"
344 Required version ending to differentiate the test
345 from other linux builds on the system.
346EOF
347 ;
348$config_help{"REBOOT_TYPE"} = << "EOF"
349 Way to reboot the box to the test kernel.
350 Only valid options so far are "grub" and "script".
351
352 If you specify grub, it will assume grub version 1
353 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
354 and select that target to reboot to the kernel. If this is not
355 your setup, then specify "script" and have a command or script
356 specified in REBOOT_SCRIPT to boot to the target.
357
358 The entry in /boot/grub/menu.lst must be entered in manually.
359 The test will not modify that file.
360EOF
361 ;
362$config_help{"GRUB_MENU"} = << "EOF"
363 The grub title name for the test kernel to boot
364 (Only mandatory if REBOOT_TYPE = grub)
365
366 Note, ktest.pl will not update the grub menu.lst, you need to
367 manually add an option for the test. ktest.pl will search
368 the grub menu.lst for this option to find what kernel to
369 reboot into.
370
371 For example, if in the /boot/grub/menu.lst the test kernel title has:
372 title Test Kernel
373 kernel vmlinuz-test
374 GRUB_MENU = Test Kernel
375EOF
376 ;
377$config_help{"REBOOT_SCRIPT"} = << "EOF"
378 A script to reboot the target into the test kernel
379 (Only mandatory if REBOOT_TYPE = script)
380EOF
381 ;
382
Steven Rostedtdad98752011-11-22 20:48:57 -0500383sub read_prompt {
384 my ($cancel, $prompt) = @_;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400385
386 my $ans;
387
388 for (;;) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500389 if ($cancel) {
390 print "$prompt [y/n/C] ";
391 } else {
392 print "$prompt [Y/n] ";
393 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400394 $ans = <STDIN>;
395 chomp $ans;
396 if ($ans =~ /^\s*$/) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500397 if ($cancel) {
398 $ans = "c";
399 } else {
400 $ans = "y";
401 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400402 }
403 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
Steven Rostedtdad98752011-11-22 20:48:57 -0500404 if ($cancel) {
405 last if ($ans =~ /^c$/i);
406 print "Please answer either 'y', 'n' or 'c'.\n";
407 } else {
408 print "Please answer either 'y' or 'n'.\n";
409 }
410 }
411 if ($ans =~ /^c/i) {
412 exit;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400413 }
414 if ($ans !~ /^y$/i) {
415 return 0;
416 }
417 return 1;
418}
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500419
Steven Rostedtdad98752011-11-22 20:48:57 -0500420sub read_yn {
421 my ($prompt) = @_;
422
423 return read_prompt 0, $prompt;
424}
425
426sub read_ync {
427 my ($prompt) = @_;
428
429 return read_prompt 1, $prompt;
430}
431
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500432sub get_ktest_config {
433 my ($config) = @_;
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400434 my $ans;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500435
436 return if (defined($opt{$config}));
437
438 if (defined($config_help{$config})) {
439 print "\n";
440 print $config_help{$config};
441 }
442
443 for (;;) {
444 print "$config = ";
Steven Rostedtdbd37832011-11-23 16:00:48 -0500445 if (defined($default{$config}) && length($default{$config})) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500446 print "\[$default{$config}\] ";
447 }
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400448 $ans = <STDIN>;
449 $ans =~ s/^\s*(.*\S)\s*$/$1/;
450 if ($ans =~ /^\s*$/) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500451 if ($default{$config}) {
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400452 $ans = $default{$config};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500453 } else {
454 print "Your answer can not be blank\n";
455 next;
456 }
457 }
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500458 $entered_configs{$config} = ${ans};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500459 last;
460 }
461}
462
463sub get_ktest_configs {
464 get_ktest_config("MACHINE");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500465 get_ktest_config("BUILD_DIR");
466 get_ktest_config("OUTPUT_DIR");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500467
Steven Rostedtdbd37832011-11-23 16:00:48 -0500468 if ($newconfig) {
469 get_ktest_config("BUILD_OPTIONS");
470 }
471
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500472 # options required for other than just building a kernel
473 if (!$buildonly) {
Steven Rostedt165708b2011-11-26 20:56:52 -0500474 get_ktest_config("POWER_CYCLE");
475 get_ktest_config("CONSOLE");
476 }
477
478 # options required for install and more
479 if ($buildonly != 1) {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500480 get_ktest_config("SSH_USER");
481 get_ktest_config("BUILD_TARGET");
482 get_ktest_config("TARGET_IMAGE");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500483 }
484
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500485 get_ktest_config("LOCALVERSION");
486
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500487 return if ($buildonly);
488
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500489 my $rtype = $opt{"REBOOT_TYPE"};
490
491 if (!defined($rtype)) {
492 if (!defined($opt{"GRUB_MENU"})) {
493 get_ktest_config("REBOOT_TYPE");
494 $rtype = $entered_configs{"REBOOT_TYPE"};
495 } else {
496 $rtype = "grub";
497 }
498 }
499
500 if ($rtype eq "grub") {
501 get_ktest_config("GRUB_MENU");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500502 }
503}
504
Steven Rostedt77d942c2011-05-20 13:36:58 -0400505sub process_variables {
Steven Rostedt8d735212011-10-17 11:36:44 -0400506 my ($value, $remove_undef) = @_;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400507 my $retval = "";
508
509 # We want to check for '\', and it is just easier
510 # to check the previous characet of '$' and not need
511 # to worry if '$' is the first character. By adding
512 # a space to $value, we can just check [^\\]\$ and
513 # it will still work.
514 $value = " $value";
515
516 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
517 my $begin = $1;
518 my $var = $2;
519 my $end = $3;
520 # append beginning of value to retval
521 $retval = "$retval$begin";
522 if (defined($variable{$var})) {
523 $retval = "$retval$variable{$var}";
Steven Rostedt8d735212011-10-17 11:36:44 -0400524 } elsif (defined($remove_undef) && $remove_undef) {
525 # for if statements, any variable that is not defined,
526 # we simple convert to 0
527 $retval = "${retval}0";
Steven Rostedt77d942c2011-05-20 13:36:58 -0400528 } else {
529 # put back the origin piece.
530 $retval = "$retval\$\{$var\}";
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500531 # This could be an option that is used later, save
532 # it so we don't warn if this option is not one of
533 # ktests options.
534 $used_options{$var} = 1;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400535 }
536 $value = $end;
537 }
538 $retval = "$retval$value";
539
540 # remove the space added in the beginning
541 $retval =~ s/ //;
542
543 return "$retval"
544}
545
Steven Rostedta57419b2010-11-02 15:13:54 -0400546sub set_value {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400547 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
Steven Rostedta57419b2010-11-02 15:13:54 -0400548
Steven Rostedtcad96662011-12-22 11:32:52 -0500549 my $prvalue = process_variables($rvalue);
550
551 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500552 # Note if a test is something other than build, then we
553 # will need other manditory options.
Steven Rostedtcad96662011-12-22 11:32:52 -0500554 if ($prvalue ne "install") {
Steven Rostedt165708b2011-11-26 20:56:52 -0500555 $buildonly = 0;
556 } else {
557 # install still limits some manditory options.
558 $buildonly = 2;
559 }
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500560 }
561
Steven Rostedta57419b2010-11-02 15:13:54 -0400562 if (defined($opt{$lvalue})) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400563 if (!$override || defined(${$overrides}{$lvalue})) {
564 my $extra = "";
565 if ($override) {
566 $extra = "In the same override section!\n";
567 }
568 die "$name: $.: Option $lvalue defined more than once!\n$extra";
569 }
Steven Rostedtcad96662011-12-22 11:32:52 -0500570 ${$overrides}{$lvalue} = $prvalue;
Steven Rostedta57419b2010-11-02 15:13:54 -0400571 }
Steven Rostedt21a96792010-11-08 16:45:50 -0500572 if ($rvalue =~ /^\s*$/) {
573 delete $opt{$lvalue};
574 } else {
Steven Rostedtcad96662011-12-22 11:32:52 -0500575 $opt{$lvalue} = $prvalue;
Steven Rostedt21a96792010-11-08 16:45:50 -0500576 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400577}
578
Steven Rostedt77d942c2011-05-20 13:36:58 -0400579sub set_variable {
580 my ($lvalue, $rvalue) = @_;
581
582 if ($rvalue =~ /^\s*$/) {
583 delete $variable{$lvalue};
584 } else {
585 $rvalue = process_variables($rvalue);
586 $variable{$lvalue} = $rvalue;
587 }
588}
589
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400590sub process_compare {
591 my ($lval, $cmp, $rval) = @_;
592
593 # remove whitespace
594
595 $lval =~ s/^\s*//;
596 $lval =~ s/\s*$//;
597
598 $rval =~ s/^\s*//;
599 $rval =~ s/\s*$//;
600
601 if ($cmp eq "==") {
602 return $lval eq $rval;
603 } elsif ($cmp eq "!=") {
604 return $lval ne $rval;
605 }
606
607 my $statement = "$lval $cmp $rval";
608 my $ret = eval $statement;
609
610 # $@ stores error of eval
611 if ($@) {
612 return -1;
613 }
614
615 return $ret;
616}
617
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400618sub value_defined {
619 my ($val) = @_;
620
621 return defined($variable{$2}) ||
622 defined($opt{$2});
623}
624
Steven Rostedt8d735212011-10-17 11:36:44 -0400625my $d = 0;
626sub process_expression {
627 my ($name, $val) = @_;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400628
Steven Rostedt8d735212011-10-17 11:36:44 -0400629 my $c = $d++;
630
631 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
632 my $express = $1;
633
634 if (process_expression($name, $express)) {
635 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
636 } else {
637 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
638 }
639 }
640
641 $d--;
642 my $OR = "\\|\\|";
643 my $AND = "\\&\\&";
644
645 while ($val =~ s/^(.*?)($OR|$AND)//) {
646 my $express = $1;
647 my $op = $2;
648
649 if (process_expression($name, $express)) {
650 if ($op eq "||") {
651 return 1;
652 }
653 } else {
654 if ($op eq "&&") {
655 return 0;
656 }
657 }
658 }
Steven Rostedt45d73a52011-09-30 19:44:53 -0400659
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400660 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
661 my $ret = process_compare($1, $2, $3);
662 if ($ret < 0) {
663 die "$name: $.: Unable to process comparison\n";
664 }
665 return $ret;
666 }
667
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400668 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
669 if (defined $1) {
670 return !value_defined($2);
671 } else {
672 return value_defined($2);
673 }
674 }
675
Steven Rostedt45d73a52011-09-30 19:44:53 -0400676 if ($val =~ /^\s*0\s*$/) {
677 return 0;
678 } elsif ($val =~ /^\s*\d+\s*$/) {
679 return 1;
680 }
681
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400682 die ("$name: $.: Undefined content $val in if statement\n");
Steven Rostedt8d735212011-10-17 11:36:44 -0400683}
684
685sub process_if {
686 my ($name, $value) = @_;
687
688 # Convert variables and replace undefined ones with 0
689 my $val = process_variables($value, 1);
690 my $ret = process_expression $name, $val;
691
692 return $ret;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400693}
694
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400695sub __read_config {
696 my ($config, $current_test_num) = @_;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400697
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400698 my $in;
699 open($in, $config) || die "can't read file $config";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400700
Steven Rostedta57419b2010-11-02 15:13:54 -0400701 my $name = $config;
702 $name =~ s,.*/(.*),$1,;
703
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400704 my $test_num = $$current_test_num;
Steven Rostedta57419b2010-11-02 15:13:54 -0400705 my $default = 1;
706 my $repeat = 1;
707 my $num_tests_set = 0;
708 my $skip = 0;
709 my $rest;
Steven Rostedta9f84422011-10-17 11:06:29 -0400710 my $line;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400711 my $test_case = 0;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400712 my $if = 0;
713 my $if_set = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400714 my $override = 0;
715
716 my %overrides;
Steven Rostedta57419b2010-11-02 15:13:54 -0400717
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400718 while (<$in>) {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400719
720 # ignore blank lines and comments
721 next if (/^\s*$/ || /\s*\#/);
722
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400723 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400724
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400725 my $type = $1;
726 $rest = $2;
Steven Rostedta9f84422011-10-17 11:06:29 -0400727 $line = $2;
Steven Rostedta57419b2010-11-02 15:13:54 -0400728
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400729 my $old_test_num;
730 my $old_repeat;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400731 $override = 0;
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400732
733 if ($type eq "TEST_START") {
734
735 if ($num_tests_set) {
736 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
737 }
738
739 $old_test_num = $test_num;
740 $old_repeat = $repeat;
741
742 $test_num += $repeat;
743 $default = 0;
744 $repeat = 1;
745 } else {
746 $default = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400747 }
748
Steven Rostedta9f84422011-10-17 11:06:29 -0400749 # If SKIP is anywhere in the line, the command will be skipped
750 if ($rest =~ s/\s+SKIP\b//) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400751 $skip = 1;
752 } else {
Steven Rostedt0df213c2011-06-14 20:51:37 -0400753 $test_case = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400754 $skip = 0;
755 }
756
Steven Rostedta9f84422011-10-17 11:06:29 -0400757 if ($rest =~ s/\sELSE\b//) {
758 if (!$if) {
759 die "$name: $.: ELSE found with out matching IF section\n$_";
760 }
761 $if = 0;
762
763 if ($if_set) {
764 $skip = 1;
765 } else {
766 $skip = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400767 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400768 }
769
Steven Rostedta9f84422011-10-17 11:06:29 -0400770 if ($rest =~ s/\sIF\s+(.*)//) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400771 if (process_if($name, $1)) {
772 $if_set = 1;
773 } else {
774 $skip = 1;
775 }
776 $if = 1;
777 } else {
778 $if = 0;
Steven Rostedta9f84422011-10-17 11:06:29 -0400779 $if_set = 0;
Steven Rostedta57419b2010-11-02 15:13:54 -0400780 }
781
Steven Rostedta9f84422011-10-17 11:06:29 -0400782 if (!$skip) {
783 if ($type eq "TEST_START") {
784 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
785 $repeat = $1;
786 $repeat_tests{"$test_num"} = $repeat;
787 }
788 } elsif ($rest =~ s/\sOVERRIDE\b//) {
789 # DEFAULT only
790 $override = 1;
791 # Clear previous overrides
792 %overrides = ();
793 }
794 }
795
796 if (!$skip && $rest !~ /^\s*$/) {
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400797 die "$name: $.: Gargbage found after $type\n$_";
Steven Rostedta57419b2010-11-02 15:13:54 -0400798 }
799
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400800 if ($skip && $type eq "TEST_START") {
Steven Rostedta57419b2010-11-02 15:13:54 -0400801 $test_num = $old_test_num;
Steven Rostedte48c5292010-11-02 14:35:37 -0400802 $repeat = $old_repeat;
Steven Rostedta57419b2010-11-02 15:13:54 -0400803 }
804
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400805 } elsif (/^\s*ELSE\b(.*)$/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400806 if (!$if) {
807 die "$name: $.: ELSE found with out matching IF section\n$_";
808 }
809 $rest = $1;
810 if ($if_set) {
811 $skip = 1;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400812 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400813 } else {
814 $skip = 0;
815
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400816 if ($rest =~ /\sIF\s+(.*)/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400817 # May be a ELSE IF section.
818 if (!process_if($name, $1)) {
819 $skip = 1;
820 }
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400821 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400822 } else {
823 $if = 0;
824 }
825 }
826
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400827 if ($rest !~ /^\s*$/) {
828 die "$name: $.: Gargbage found after DEFAULTS\n$_";
829 }
830
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400831 } elsif (/^\s*INCLUDE\s+(\S+)/) {
832
833 next if ($skip);
834
835 if (!$default) {
836 die "$name: $.: INCLUDE can only be done in default sections\n$_";
837 }
838
839 my $file = process_variables($1);
840
841 if ($file !~ m,^/,) {
842 # check the path of the config file first
843 if ($config =~ m,(.*)/,) {
844 if (-f "$1/$file") {
845 $file = "$1/$file";
846 }
847 }
848 }
849
850 if ( ! -r $file ) {
851 die "$name: $.: Can't read file $file\n$_";
852 }
853
854 if (__read_config($file, \$test_num)) {
855 $test_case = 1;
856 }
857
Steven Rostedta57419b2010-11-02 15:13:54 -0400858 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
859
860 next if ($skip);
861
Steven Rostedt2545eb62010-11-02 15:01:32 -0400862 my $lvalue = $1;
863 my $rvalue = $2;
864
Steven Rostedta57419b2010-11-02 15:13:54 -0400865 if (!$default &&
866 ($lvalue eq "NUM_TESTS" ||
867 $lvalue eq "LOG_FILE" ||
868 $lvalue eq "CLEAR_LOG")) {
869 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400870 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400871
872 if ($lvalue eq "NUM_TESTS") {
873 if ($test_num) {
874 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
875 }
876 if (!$default) {
877 die "$name: $.: NUM_TESTS must be set in default section\n";
878 }
879 $num_tests_set = 1;
880 }
881
882 if ($default || $lvalue =~ /\[\d+\]$/) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400883 set_value($lvalue, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400884 } else {
885 my $val = "$lvalue\[$test_num\]";
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400886 set_value($val, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400887
888 if ($repeat > 1) {
889 $repeats{$val} = $repeat;
890 }
891 }
Steven Rostedt77d942c2011-05-20 13:36:58 -0400892 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
893 next if ($skip);
894
895 my $lvalue = $1;
896 my $rvalue = $2;
897
898 # process config variables.
899 # Config variables are only active while reading the
900 # config and can be defined anywhere. They also ignore
901 # TEST_START and DEFAULTS, but are skipped if they are in
902 # on of these sections that have SKIP defined.
903 # The save variable can be
904 # defined multiple times and the new one simply overrides
905 # the prevous one.
906 set_variable($lvalue, $rvalue);
907
Steven Rostedta57419b2010-11-02 15:13:54 -0400908 } else {
909 die "$name: $.: Garbage found in config\n$_";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400910 }
911 }
912
Steven Rostedta57419b2010-11-02 15:13:54 -0400913 if ($test_num) {
914 $test_num += $repeat - 1;
915 $opt{"NUM_TESTS"} = $test_num;
916 }
917
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400918 close($in);
919
920 $$current_test_num = $test_num;
921
922 return $test_case;
923}
924
Steven Rostedtc4261d02011-11-23 13:41:18 -0500925sub get_test_case {
926 print "What test case would you like to run?\n";
927 print " (build, install or boot)\n";
928 print " Other tests are available but require editing the config file\n";
929 my $ans = <STDIN>;
930 chomp $ans;
931 $default{"TEST_TYPE"} = $ans;
932}
933
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400934sub read_config {
935 my ($config) = @_;
936
937 my $test_case;
938 my $test_num = 0;
939
940 $test_case = __read_config $config, \$test_num;
941
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500942 # make sure we have all mandatory configs
943 get_ktest_configs;
944
Steven Rostedt0df213c2011-06-14 20:51:37 -0400945 # was a test specified?
946 if (!$test_case) {
947 print "No test case specified.\n";
Steven Rostedtc4261d02011-11-23 13:41:18 -0500948 get_test_case;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400949 }
950
Steven Rostedta75fece2010-11-02 14:58:27 -0400951 # set any defaults
952
953 foreach my $default (keys %default) {
954 if (!defined($opt{$default})) {
955 $opt{$default} = $default{$default};
956 }
957 }
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500958
959 if ($opt{"IGNORE_UNUSED"} == 1) {
960 return;
961 }
962
963 my %not_used;
964
965 # check if there are any stragglers (typos?)
966 foreach my $option (keys %opt) {
967 my $op = $option;
968 # remove per test labels.
969 $op =~ s/\[.*\]//;
970 if (!exists($option_map{$op}) &&
971 !exists($default{$op}) &&
972 !exists($used_options{$op})) {
973 $not_used{$op} = 1;
974 }
975 }
976
977 if (%not_used) {
978 my $s = "s are";
979 $s = " is" if (keys %not_used == 1);
980 print "The following option$s not used; could be a typo:\n";
981 foreach my $option (keys %not_used) {
982 print "$option\n";
983 }
984 print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
985 if (!read_yn "Do you want to continue?") {
986 exit -1;
987 }
988 }
Steven Rostedt2545eb62010-11-02 15:01:32 -0400989}
990
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400991sub __eval_option {
992 my ($option, $i) = @_;
993
994 # Add space to evaluate the character before $
995 $option = " $option";
996 my $retval = "";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530997 my $repeated = 0;
998 my $parent = 0;
999
1000 foreach my $test (keys %repeat_tests) {
1001 if ($i >= $test &&
1002 $i < $test + $repeat_tests{$test}) {
1003
1004 $repeated = 1;
1005 $parent = $test;
1006 last;
1007 }
1008 }
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001009
1010 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
1011 my $start = $1;
1012 my $var = $2;
1013 my $end = $3;
1014
1015 # Append beginning of line
1016 $retval = "$retval$start";
1017
1018 # If the iteration option OPT[$i] exists, then use that.
1019 # otherwise see if the default OPT (without [$i]) exists.
1020
1021 my $o = "$var\[$i\]";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301022 my $parento = "$var\[$parent\]";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001023
1024 if (defined($opt{$o})) {
1025 $o = $opt{$o};
1026 $retval = "$retval$o";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301027 } elsif ($repeated && defined($opt{$parento})) {
1028 $o = $opt{$parento};
1029 $retval = "$retval$o";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001030 } elsif (defined($opt{$var})) {
1031 $o = $opt{$var};
1032 $retval = "$retval$o";
1033 } else {
1034 $retval = "$retval\$\{$var\}";
1035 }
1036
1037 $option = $end;
1038 }
1039
1040 $retval = "$retval$option";
1041
1042 $retval =~ s/^ //;
1043
1044 return $retval;
1045}
1046
1047sub eval_option {
1048 my ($option, $i) = @_;
1049
1050 my $prev = "";
1051
1052 # Since an option can evaluate to another option,
1053 # keep iterating until we do not evaluate any more
1054 # options.
1055 my $r = 0;
1056 while ($prev ne $option) {
1057 # Check for recursive evaluations.
1058 # 100 deep should be more than enough.
1059 if ($r++ > 100) {
1060 die "Over 100 evaluations accurred with $option\n" .
1061 "Check for recursive variables\n";
1062 }
1063 $prev = $option;
1064 $option = __eval_option($option, $i);
1065 }
1066
1067 return $option;
1068}
1069
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001070sub _logit {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001071 if (defined($opt{"LOG_FILE"})) {
1072 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
1073 print OUT @_;
1074 close(OUT);
1075 }
1076}
1077
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001078sub logit {
1079 if (defined($opt{"LOG_FILE"})) {
1080 _logit @_;
1081 } else {
1082 print @_;
1083 }
1084}
1085
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001086sub doprint {
1087 print @_;
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001088 _logit @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001089}
1090
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001091sub run_command;
Andrew Jones2728be42011-08-12 15:32:05 +02001092sub start_monitor;
1093sub end_monitor;
1094sub wait_for_monitor;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001095
1096sub reboot {
Andrew Jones2728be42011-08-12 15:32:05 +02001097 my ($time) = @_;
1098
Steven Rostedt2b803362011-09-30 18:00:23 -04001099 if (defined($time)) {
1100 start_monitor;
1101 # flush out current monitor
1102 # May contain the reboot success line
1103 wait_for_monitor 1;
1104 }
1105
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001106 # try to reboot normally
Steven Rostedte48c5292010-11-02 14:35:37 -04001107 if (run_command $reboot) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001108 if (defined($powercycle_after_reboot)) {
1109 sleep $powercycle_after_reboot;
1110 run_command "$power_cycle";
1111 }
1112 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001113 # nope? power cycle it.
Steven Rostedta75fece2010-11-02 14:58:27 -04001114 run_command "$power_cycle";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001115 }
Andrew Jones2728be42011-08-12 15:32:05 +02001116
1117 if (defined($time)) {
Steven Rostedt2b803362011-09-30 18:00:23 -04001118 wait_for_monitor($time, $reboot_success_line);
Andrew Jones2728be42011-08-12 15:32:05 +02001119 end_monitor;
1120 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001121}
1122
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001123sub reboot_to_good {
1124 my ($time) = @_;
1125
1126 if (defined($switch_to_good)) {
1127 run_command $switch_to_good;
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001128 }
1129
1130 reboot $time;
1131}
1132
Steven Rostedt576f6272010-11-02 14:58:38 -04001133sub do_not_reboot {
1134 my $i = $iteration;
1135
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001136 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -04001137 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1138 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1139}
1140
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001141sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001142 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001143
Steven Rostedt576f6272010-11-02 14:58:38 -04001144 my $i = $iteration;
1145
1146 if ($reboot_on_error && !do_not_reboot) {
1147
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001148 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001149 reboot_to_good;
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001150
Steven Rostedta75fece2010-11-02 14:58:27 -04001151 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001152 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001153 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001154 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001155
Steven Rostedtf80802c2011-03-07 13:18:47 -05001156 if (defined($opt{"LOG_FILE"})) {
1157 print " See $opt{LOG_FILE} for more info.\n";
1158 }
1159
Steven Rostedt576f6272010-11-02 14:58:38 -04001160 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001161}
1162
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001163sub open_console {
1164 my ($fp) = @_;
1165
1166 my $flags;
1167
Steven Rostedta75fece2010-11-02 14:58:27 -04001168 my $pid = open($fp, "$console|") or
1169 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001170
1171 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001172 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001173 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001174 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001175
1176 return $pid;
1177}
1178
1179sub close_console {
1180 my ($fp, $pid) = @_;
1181
1182 doprint "kill child process $pid\n";
1183 kill 2, $pid;
1184
1185 print "closing!\n";
1186 close($fp);
1187}
1188
1189sub start_monitor {
1190 if ($monitor_cnt++) {
1191 return;
1192 }
1193 $monitor_fp = \*MONFD;
1194 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001195
1196 return;
1197
1198 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001199}
1200
1201sub end_monitor {
1202 if (--$monitor_cnt) {
1203 return;
1204 }
1205 close_console($monitor_fp, $monitor_pid);
1206}
1207
1208sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001209 my ($time, $stop) = @_;
1210 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001211 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001212 my $booted = 0;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001213
Steven Rostedta75fece2010-11-02 14:58:27 -04001214 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001215
1216 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001217 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001218 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001219 last if (!defined($line));
1220 print "$line";
1221 $full_line .= $line;
1222
1223 if (defined($stop) && $full_line =~ /$stop/) {
1224 doprint "wait for monitor detected $stop\n";
1225 $booted = 1;
1226 }
1227
1228 if ($line =~ /\n/) {
1229 $full_line = "";
1230 }
1231 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001232 print "** Monitor flushed **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001233}
1234
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301235sub save_logs {
1236 my ($result, $basedir) = @_;
1237 my @t = localtime;
1238 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1239 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1240
1241 my $type = $build_type;
1242 if ($type =~ /useconfig/) {
1243 $type = "useconfig";
1244 }
1245
1246 my $dir = "$machine-$test_type-$type-$result-$date";
1247
1248 $dir = "$basedir/$dir";
1249
1250 if (!-d $dir) {
1251 mkpath($dir) or
1252 die "can't create $dir";
1253 }
1254
1255 my %files = (
1256 "config" => $output_config,
1257 "buildlog" => $buildlog,
1258 "dmesg" => $dmesg,
1259 "testlog" => $testlog,
1260 );
1261
1262 while (my ($name, $source) = each(%files)) {
1263 if (-f "$source") {
1264 cp "$source", "$dir/$name" or
1265 die "failed to copy $source";
1266 }
1267 }
1268
1269 doprint "*** Saved info to $dir ***\n";
1270}
1271
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001272sub fail {
1273
Steven Rostedta75fece2010-11-02 14:58:27 -04001274 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001275 dodie @_;
1276 }
1277
Steven Rostedta75fece2010-11-02 14:58:27 -04001278 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001279
Steven Rostedt576f6272010-11-02 14:58:38 -04001280 my $i = $iteration;
1281
Steven Rostedta75fece2010-11-02 14:58:27 -04001282 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001283 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001284 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001285 reboot_to_good $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001286 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001287
Steven Rostedt9064af52011-06-13 10:38:48 -04001288 my $name = "";
1289
1290 if (defined($test_name)) {
1291 $name = " ($test_name)";
1292 }
1293
Steven Rostedt576f6272010-11-02 14:58:38 -04001294 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1295 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001296 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001297 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1298 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001299
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301300 if (defined($store_failures)) {
1301 save_logs "fail", $store_failures;
1302 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001303
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001304 return 1;
1305}
1306
Steven Rostedt2545eb62010-11-02 15:01:32 -04001307sub run_command {
1308 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001309 my $dolog = 0;
1310 my $dord = 0;
1311 my $pid;
1312
Steven Rostedte48c5292010-11-02 14:35:37 -04001313 $command =~ s/\$SSH_USER/$ssh_user/g;
1314 $command =~ s/\$MACHINE/$machine/g;
1315
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001316 doprint("$command ... ");
1317
1318 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001319 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001320
1321 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001322 open(LOG, ">>$opt{LOG_FILE}") or
1323 dodie "failed to write to log";
1324 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001325 }
1326
1327 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001328 open (RD, ">$redirect") or
1329 dodie "failed to write to redirect $redirect";
1330 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001331 }
1332
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001333 while (<CMD>) {
1334 print LOG if ($dolog);
1335 print RD if ($dord);
1336 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001337
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001338 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001339 my $failed = $?;
1340
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001341 close(CMD);
1342 close(LOG) if ($dolog);
1343 close(RD) if ($dord);
1344
Steven Rostedt2545eb62010-11-02 15:01:32 -04001345 if ($failed) {
1346 doprint "FAILED!\n";
1347 } else {
1348 doprint "SUCCESS\n";
1349 }
1350
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001351 return !$failed;
1352}
1353
Steven Rostedte48c5292010-11-02 14:35:37 -04001354sub run_ssh {
1355 my ($cmd) = @_;
1356 my $cp_exec = $ssh_exec;
1357
1358 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1359 return run_command "$cp_exec";
1360}
1361
1362sub run_scp {
Steven Rostedt02ad2612012-03-21 08:21:24 -04001363 my ($src, $dst, $cp_scp) = @_;
Steven Rostedte48c5292010-11-02 14:35:37 -04001364
1365 $cp_scp =~ s/\$SRC_FILE/$src/g;
1366 $cp_scp =~ s/\$DST_FILE/$dst/g;
1367
1368 return run_command "$cp_scp";
1369}
1370
Steven Rostedt02ad2612012-03-21 08:21:24 -04001371sub run_scp_install {
1372 my ($src, $dst) = @_;
1373
1374 my $cp_scp = $scp_to_target_install;
1375
1376 return run_scp($src, $dst, $cp_scp);
1377}
1378
1379sub run_scp_mod {
1380 my ($src, $dst) = @_;
1381
1382 my $cp_scp = $scp_to_target;
1383
1384 return run_scp($src, $dst, $cp_scp);
1385}
1386
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001387sub get_grub_index {
1388
Steven Rostedta75fece2010-11-02 14:58:27 -04001389 if ($reboot_type ne "grub") {
1390 return;
1391 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001392 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001393
1394 doprint "Find grub menu ... ";
1395 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001396
1397 my $ssh_grub = $ssh_exec;
1398 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1399
1400 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001401 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001402
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001403 my $found = 0;
1404
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001405 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001406 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001407 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001408 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001409 last;
1410 } elsif (/^\s*title\s/) {
1411 $grub_number++;
1412 }
1413 }
1414 close(IN);
1415
Steven Rostedta75fece2010-11-02 14:58:27 -04001416 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001417 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001418 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001419}
1420
Steven Rostedt2545eb62010-11-02 15:01:32 -04001421sub wait_for_input
1422{
1423 my ($fp, $time) = @_;
1424 my $rin;
1425 my $ready;
1426 my $line;
1427 my $ch;
1428
1429 if (!defined($time)) {
1430 $time = $timeout;
1431 }
1432
1433 $rin = '';
1434 vec($rin, fileno($fp), 1) = 1;
1435 $ready = select($rin, undef, undef, $time);
1436
1437 $line = "";
1438
1439 # try to read one char at a time
1440 while (sysread $fp, $ch, 1) {
1441 $line .= $ch;
1442 last if ($ch eq "\n");
1443 }
1444
1445 if (!length($line)) {
1446 return undef;
1447 }
1448
1449 return $line;
1450}
1451
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001452sub reboot_to {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001453 if (defined($switch_to_test)) {
1454 run_command $switch_to_test;
1455 }
1456
Steven Rostedta75fece2010-11-02 14:58:27 -04001457 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001458 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001459 } elsif (defined $reboot_script) {
1460 run_command "$reboot_script";
Steven Rostedta75fece2010-11-02 14:58:27 -04001461 }
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001462 reboot;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001463}
1464
Steven Rostedta57419b2010-11-02 15:13:54 -04001465sub get_sha1 {
1466 my ($commit) = @_;
1467
1468 doprint "git rev-list --max-count=1 $commit ... ";
1469 my $sha1 = `git rev-list --max-count=1 $commit`;
1470 my $ret = $?;
1471
1472 logit $sha1;
1473
1474 if ($ret) {
1475 doprint "FAILED\n";
1476 dodie "Failed to get git $commit";
1477 }
1478
1479 print "SUCCESS\n";
1480
1481 chomp $sha1;
1482
1483 return $sha1;
1484}
1485
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001486sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001487 my $booted = 0;
1488 my $bug = 0;
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001489 my $bug_ignored = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001490 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001491 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001492
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001493 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001494
1495 my $line;
1496 my $full_line = "";
1497
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001498 open(DMESG, "> $dmesg") or
1499 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001500
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001501 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001502
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001503 my $success_start;
1504 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001505 my $monitor_start = time;
1506 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001507 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001508
Steven Rostedt2d01b262011-03-08 09:47:54 -05001509 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001510
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001511 if ($bug && defined($stop_after_failure) &&
1512 $stop_after_failure >= 0) {
1513 my $time = $stop_after_failure - (time - $failure_start);
1514 $line = wait_for_input($monitor_fp, $time);
1515 if (!defined($line)) {
1516 doprint "bug timed out after $booted_timeout seconds\n";
1517 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1518 last;
1519 }
1520 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001521 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001522 if (!defined($line)) {
1523 my $s = $booted_timeout == 1 ? "" : "s";
1524 doprint "Successful boot found: break after $booted_timeout second$s\n";
1525 last;
1526 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001527 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001528 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001529 if (!defined($line)) {
1530 my $s = $timeout == 1 ? "" : "s";
1531 doprint "Timed out after $timeout second$s\n";
1532 last;
1533 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001534 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001535
Steven Rostedt2545eb62010-11-02 15:01:32 -04001536 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001537 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001538
1539 # we are not guaranteed to get a full line
1540 $full_line .= $line;
1541
Steven Rostedta75fece2010-11-02 14:58:27 -04001542 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001543 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001544 $success_start = time;
1545 }
1546
1547 if ($booted && defined($stop_after_success) &&
1548 $stop_after_success >= 0) {
1549 my $now = time;
1550 if ($now - $success_start >= $stop_after_success) {
1551 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1552 last;
1553 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001554 }
1555
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001556 if ($full_line =~ /\[ backtrace testing \]/) {
1557 $skip_call_trace = 1;
1558 }
1559
Steven Rostedt2545eb62010-11-02 15:01:32 -04001560 if ($full_line =~ /call trace:/i) {
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001561 if (!$bug && !$skip_call_trace) {
1562 if ($ignore_errors) {
1563 $bug_ignored = 1;
1564 } else {
1565 $bug = 1;
1566 $failure_start = time;
1567 }
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001568 }
1569 }
1570
1571 if ($bug && defined($stop_after_failure) &&
1572 $stop_after_failure >= 0) {
1573 my $now = time;
1574 if ($now - $failure_start >= $stop_after_failure) {
1575 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1576 last;
1577 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001578 }
1579
1580 if ($full_line =~ /\[ end of backtrace testing \]/) {
1581 $skip_call_trace = 0;
1582 }
1583
1584 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001585 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001586 $bug = 1;
1587 }
1588
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001589 # Detect triple faults by testing the banner
1590 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1591 if ($1 eq $version) {
1592 $version_found = 1;
1593 } elsif ($version_found && $detect_triplefault) {
1594 # We already booted into the kernel we are testing,
1595 # but now we booted into another kernel?
1596 # Consider this a triple fault.
1597 doprint "Aleady booted in Linux kernel $version, but now\n";
1598 doprint "we booted into Linux kernel $1.\n";
1599 doprint "Assuming that this is a triple fault.\n";
1600 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1601 last;
1602 }
1603 }
1604
Steven Rostedt2545eb62010-11-02 15:01:32 -04001605 if ($line =~ /\n/) {
1606 $full_line = "";
1607 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001608
1609 if ($stop_test_after > 0 && !$booted && !$bug) {
1610 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001611 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001612 $done = 1;
1613 }
1614 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001615 }
1616
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001617 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001618
Steven Rostedt2545eb62010-11-02 15:01:32 -04001619 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001620 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001621 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001622 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001623
Steven Rostedta75fece2010-11-02 14:58:27 -04001624 if (!$booted) {
1625 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001626 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001627 }
1628
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001629 if ($bug_ignored) {
1630 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
1631 }
1632
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001633 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001634}
1635
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001636sub eval_kernel_version {
1637 my ($option) = @_;
1638
1639 $option =~ s/\$KERNEL_VERSION/$version/g;
1640
1641 return $option;
1642}
1643
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001644sub do_post_install {
1645
1646 return if (!defined($post_install));
1647
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001648 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001649 run_command "$cp_post_install" or
1650 dodie "Failed to run post install";
1651}
1652
Steven Rostedt2545eb62010-11-02 15:01:32 -04001653sub install {
1654
Steven Rostedte0a87422011-09-30 17:50:48 -04001655 return if ($no_install);
1656
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001657 my $cp_target = eval_kernel_version $target_image;
1658
Steven Rostedt02ad2612012-03-21 08:21:24 -04001659 run_scp_install "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001660 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001661
1662 my $install_mods = 0;
1663
1664 # should we process modules?
1665 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001666 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001667 while (<IN>) {
1668 if (/CONFIG_MODULES(=y)?/) {
1669 $install_mods = 1 if (defined($1));
1670 last;
1671 }
1672 }
1673 close(IN);
1674
1675 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001676 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001677 doprint "No modules needed\n";
1678 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001679 }
1680
Steven Rostedt627977d2012-03-21 08:16:15 -04001681 run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001682 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001683
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001684 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001685 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001686
Steven Rostedte48c5292010-11-02 14:35:37 -04001687 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001688 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001689
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001690 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001691 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001692 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001693
Steven Rostedt02ad2612012-03-21 08:21:24 -04001694 run_scp_mod "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001695 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001696
Steven Rostedta75fece2010-11-02 14:58:27 -04001697 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001698
Steven Rostedte7b13442011-06-14 20:44:36 -04001699 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001700 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001701
Steven Rostedte48c5292010-11-02 14:35:37 -04001702 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001703
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001704 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001705}
1706
Steven Rostedtddf607e2011-06-14 20:49:13 -04001707sub get_version {
1708 # get the release name
Steven Rostedt683a3e62012-05-18 13:34:35 -04001709 return if ($have_version);
Steven Rostedtddf607e2011-06-14 20:49:13 -04001710 doprint "$make kernelrelease ... ";
1711 $version = `$make kernelrelease | tail -1`;
1712 chomp($version);
1713 doprint "$version\n";
Steven Rostedt683a3e62012-05-18 13:34:35 -04001714 $have_version = 1;
Steven Rostedtddf607e2011-06-14 20:49:13 -04001715}
1716
1717sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001718 # Make sure the stable kernel has finished booting
1719 start_monitor;
1720 wait_for_monitor 5;
1721 end_monitor;
1722
Steven Rostedtddf607e2011-06-14 20:49:13 -04001723 get_grub_index;
1724 get_version;
1725 install;
1726
1727 start_monitor;
1728 return monitor;
1729}
1730
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001731sub check_buildlog {
1732 my ($patch) = @_;
1733
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001734 my @files = `git show $patch | diffstat -l`;
1735
1736 open(IN, "git show $patch |") or
1737 dodie "failed to show $patch";
1738 while (<IN>) {
1739 if (m,^--- a/(.*),) {
1740 chomp $1;
1741 $files[$#files] = $1;
1742 }
1743 }
1744 close(IN);
1745
1746 open(IN, $buildlog) or dodie "Can't open $buildlog";
1747 while (<IN>) {
1748 if (/^\s*(.*?):.*(warning|error)/) {
1749 my $err = $1;
1750 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001751 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001752 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001753 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001754 }
1755 }
1756 }
1757 }
1758 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001759
1760 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001761}
1762
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001763sub apply_min_config {
1764 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05001765
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001766 # Read the config file and remove anything that
1767 # is in the force_config hash (from minconfig and others)
1768 # then add the force config back.
1769
1770 doprint "Applying minimum configurations into $output_config.new\n";
1771
1772 open (OUT, ">$outconfig") or
1773 dodie "Can't create $outconfig";
1774
1775 if (-f $output_config) {
1776 open (IN, $output_config) or
1777 dodie "Failed to open $output_config";
1778 while (<IN>) {
1779 if (/^(# )?(CONFIG_[^\s=]*)/) {
1780 next if (defined($force_config{$2}));
1781 }
1782 print OUT;
1783 }
1784 close IN;
1785 }
1786 foreach my $config (keys %force_config) {
1787 print OUT "$force_config{$config}\n";
1788 }
1789 close OUT;
1790
1791 run_command "mv $outconfig $output_config";
1792}
1793
1794sub make_oldconfig {
1795
Steven Rostedt4c4ab122011-07-15 21:16:17 -04001796 my @force_list = keys %force_config;
1797
1798 if ($#force_list >= 0) {
1799 apply_min_config;
1800 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001801
1802 if (!run_command "$make oldnoconfig") {
Steven Rostedt612b9e92011-03-07 13:27:43 -05001803 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1804 # try a yes '' | oldconfig
1805 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001806 run_command "yes '' | $make oldconfig" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001807 dodie "failed make config oldconfig";
1808 }
1809}
1810
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001811# read a config file and use this to force new configs.
1812sub load_force_config {
1813 my ($config) = @_;
1814
1815 open(IN, $config) or
1816 dodie "failed to read $config";
1817 while (<IN>) {
1818 chomp;
1819 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1820 $force_config{$1} = $_;
1821 } elsif (/^# (CONFIG_\S*) is not set/) {
1822 $force_config{$1} = $_;
1823 }
1824 }
1825 close IN;
1826}
1827
Steven Rostedt2545eb62010-11-02 15:01:32 -04001828sub build {
1829 my ($type) = @_;
1830
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001831 unlink $buildlog;
1832
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001833 # Failed builds should not reboot the target
1834 my $save_no_reboot = $no_reboot;
1835 $no_reboot = 1;
1836
Steven Rostedt683a3e62012-05-18 13:34:35 -04001837 # Calculate a new version from here.
1838 $have_version = 0;
1839
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001840 if (defined($pre_build)) {
1841 my $ret = run_command $pre_build;
1842 if (!$ret && defined($pre_build_die) &&
1843 $pre_build_die) {
1844 dodie "failed to pre_build\n";
1845 }
1846 }
1847
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001848 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001849 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001850 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001851
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001852 $type = "oldconfig";
1853 }
1854
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001855 # old config can ask questions
1856 if ($type eq "oldconfig") {
Steven Rostedt9386c6a2010-11-08 16:35:48 -05001857 $type = "oldnoconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001858
1859 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001860 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001861
Andrew Jones13488232011-08-12 15:32:04 +02001862 if (!$noclean) {
1863 run_command "mv $output_config $outputdir/config_temp" or
1864 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001865
Andrew Jones13488232011-08-12 15:32:04 +02001866 run_command "$make mrproper" or dodie "make mrproper";
1867
1868 run_command "mv $outputdir/config_temp $output_config" or
1869 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001870 }
1871
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001872 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001873 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001874 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001875 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001876 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001877
1878 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04001879 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1880 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001881 close(OUT);
1882
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001883 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001884 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001885 }
1886
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001887 if ($type ne "oldnoconfig") {
1888 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001889 dodie "failed make config";
1890 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001891 # Run old config regardless, to enforce min configurations
1892 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001893
Steven Rostedta75fece2010-11-02 14:58:27 -04001894 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001895 my $build_ret = run_command "$make $build_options";
1896 undef $redirect;
1897
1898 if (defined($post_build)) {
Steven Rostedt683a3e62012-05-18 13:34:35 -04001899 # Because a post build may change the kernel version
1900 # do it now.
1901 get_version;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001902 my $ret = run_command $post_build;
1903 if (!$ret && defined($post_build_die) &&
1904 $post_build_die) {
1905 dodie "failed to post_build\n";
1906 }
1907 }
1908
1909 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001910 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001911 if ($in_bisect) {
1912 $no_reboot = $save_no_reboot;
1913 return 0;
1914 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001915 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001916 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001917
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001918 $no_reboot = $save_no_reboot;
1919
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001920 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001921}
1922
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001923sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04001924 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001925 if (defined($poweroff_after_halt)) {
1926 sleep $poweroff_after_halt;
1927 run_command "$power_off";
1928 }
1929 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001930 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04001931 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001932 }
1933}
1934
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001935sub success {
1936 my ($i) = @_;
1937
Steven Rostedte48c5292010-11-02 14:35:37 -04001938 $successes++;
1939
Steven Rostedt9064af52011-06-13 10:38:48 -04001940 my $name = "";
1941
1942 if (defined($test_name)) {
1943 $name = " ($test_name)";
1944 }
1945
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001946 doprint "\n\n*******************************************\n";
1947 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001948 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001949 doprint "*******************************************\n";
1950 doprint "*******************************************\n";
1951
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301952 if (defined($store_successes)) {
1953 save_logs "success", $store_successes;
1954 }
1955
Steven Rostedt576f6272010-11-02 14:58:38 -04001956 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001957 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001958 reboot_to_good $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001959 }
1960}
1961
Steven Rostedtc960bb92011-03-08 09:22:39 -05001962sub answer_bisect {
1963 for (;;) {
1964 doprint "Pass or fail? [p/f]";
1965 my $ans = <STDIN>;
1966 chomp $ans;
1967 if ($ans eq "p" || $ans eq "P") {
1968 return 1;
1969 } elsif ($ans eq "f" || $ans eq "F") {
1970 return 0;
1971 } else {
1972 print "Please answer 'P' or 'F'\n";
1973 }
1974 }
1975}
1976
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001977sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001978 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001979
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001980 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04001981 $reboot_on_error = 0;
1982 $poweroff_on_error = 0;
1983 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001984
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301985 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001986 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301987 undef $redirect;
1988
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001989 exit $failed;
1990}
1991
1992my $child_done;
1993
1994sub child_finished {
1995 $child_done = 1;
1996}
1997
1998sub do_run_test {
1999 my $child_pid;
2000 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002001 my $line;
2002 my $full_line;
2003 my $bug = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002004
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002005 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002006
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002007 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002008
2009 $child_done = 0;
2010
2011 $SIG{CHLD} = qw(child_finished);
2012
2013 $child_pid = fork;
2014
2015 child_run_test if (!$child_pid);
2016
2017 $full_line = "";
2018
2019 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002020 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002021 if (defined($line)) {
2022
2023 # we are not guaranteed to get a full line
2024 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002025 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002026
2027 if ($full_line =~ /call trace:/i) {
2028 $bug = 1;
2029 }
2030
2031 if ($full_line =~ /Kernel panic -/) {
2032 $bug = 1;
2033 }
2034
2035 if ($line =~ /\n/) {
2036 $full_line = "";
2037 }
2038 }
2039 } while (!$child_done && !$bug);
2040
2041 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002042 my $failure_start = time;
2043 my $now;
2044 do {
2045 $line = wait_for_input($monitor_fp, 1);
2046 if (defined($line)) {
2047 doprint $line;
2048 }
2049 $now = time;
2050 if ($now - $failure_start >= $stop_after_failure) {
2051 last;
2052 }
2053 } while (defined($line));
2054
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002055 doprint "Detected kernel crash!\n";
2056 # kill the child with extreme prejudice
2057 kill 9, $child_pid;
2058 }
2059
2060 waitpid $child_pid, 0;
2061 $child_exit = $?;
2062
Steven Rostedtc5dacb82011-12-22 12:43:57 -05002063 if (!$bug && $in_bisect) {
2064 if (defined($bisect_ret_good)) {
2065 if ($child_exit == $bisect_ret_good) {
2066 return 1;
2067 }
2068 }
2069 if (defined($bisect_ret_skip)) {
2070 if ($child_exit == $bisect_ret_skip) {
2071 return -1;
2072 }
2073 }
2074 if (defined($bisect_ret_abort)) {
2075 if ($child_exit == $bisect_ret_abort) {
2076 fail "test abort" and return -2;
2077 }
2078 }
2079 if (defined($bisect_ret_bad)) {
2080 if ($child_exit == $bisect_ret_skip) {
2081 return 0;
2082 }
2083 }
2084 if (defined($bisect_ret_default)) {
2085 if ($bisect_ret_default eq "good") {
2086 return 1;
2087 } elsif ($bisect_ret_default eq "bad") {
2088 return 0;
2089 } elsif ($bisect_ret_default eq "skip") {
2090 return -1;
2091 } elsif ($bisect_ret_default eq "abort") {
2092 return -2;
2093 } else {
2094 fail "unknown default action: $bisect_ret_default"
2095 and return -2;
2096 }
2097 }
2098 }
2099
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002100 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002101 return 0 if $in_bisect;
2102 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002103 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002104 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002105}
2106
Steven Rostedta75fece2010-11-02 14:58:27 -04002107sub run_git_bisect {
2108 my ($command) = @_;
2109
2110 doprint "$command ... ";
2111
2112 my $output = `$command 2>&1`;
2113 my $ret = $?;
2114
2115 logit $output;
2116
2117 if ($ret) {
2118 doprint "FAILED\n";
2119 dodie "Failed to git bisect";
2120 }
2121
2122 doprint "SUCCESS\n";
2123 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2124 doprint "$1 [$2]\n";
2125 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002126 $bisect_bad_commit = $1;
Steven Rostedta75fece2010-11-02 14:58:27 -04002127 doprint "Found bad commit... $1\n";
2128 return 0;
2129 } else {
2130 # we already logged it, just print it now.
2131 print $output;
2132 }
2133
2134 return 1;
2135}
2136
Steven Rostedtc23dca72011-03-08 09:26:31 -05002137sub bisect_reboot {
2138 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002139 reboot_to_good $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05002140}
2141
2142# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05002143sub run_bisect_test {
2144 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002145
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002146 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002147 my $result;
2148 my $output;
2149 my $ret;
2150
Steven Rostedt0a05c762010-11-08 11:14:10 -05002151 $in_bisect = 1;
2152
2153 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002154
2155 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002156 if ($failed && $bisect_skip) {
2157 $in_bisect = 0;
2158 return -1;
2159 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002160 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002161
2162 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04002163 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002164
2165 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002166 if ($failed && $bisect_skip) {
2167 end_monitor;
2168 bisect_reboot;
2169 $in_bisect = 0;
2170 return -1;
2171 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002172 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002173
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002174 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002175 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002176 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002177 }
2178
2179 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002180 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002181 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002182 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002183 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04002184
2185 # reboot the box to a kernel we can ssh to
2186 if ($type ne "build") {
2187 bisect_reboot;
2188 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002189 $in_bisect = 0;
2190
2191 return $result;
2192}
2193
2194sub run_bisect {
2195 my ($type) = @_;
2196 my $buildtype = "oldconfig";
2197
2198 # We should have a minconfig to use?
2199 if (defined($minconfig)) {
2200 $buildtype = "useconfig:$minconfig";
2201 }
2202
2203 my $ret = run_bisect_test $type, $buildtype;
2204
Steven Rostedtc960bb92011-03-08 09:22:39 -05002205 if ($bisect_manual) {
2206 $ret = answer_bisect;
2207 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002208
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002209 # Are we looking for where it worked, not failed?
Russ Dill5158ba3e2012-04-23 19:43:00 -07002210 if ($reverse_bisect && $ret >= 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002211 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002212 }
2213
Steven Rostedtc23dca72011-03-08 09:26:31 -05002214 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002215 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002216 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002217 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002218 } elsif ($bisect_skip) {
2219 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2220 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002221 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002222}
2223
Steven Rostedtdad98752011-11-22 20:48:57 -05002224sub update_bisect_replay {
2225 my $tmp_log = "$tmpdir/ktest_bisect_log";
2226 run_command "git bisect log > $tmp_log" or
2227 die "can't create bisect log";
2228 return $tmp_log;
2229}
2230
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002231sub bisect {
2232 my ($i) = @_;
2233
2234 my $result;
2235
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002236 die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2237 die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
2238 die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002239
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002240 my $good = $bisect_good;
2241 my $bad = $bisect_bad;
2242 my $type = $bisect_type;
2243 my $start = $bisect_start;
2244 my $replay = $bisect_replay;
2245 my $start_files = $bisect_files;
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002246
2247 if (defined($start_files)) {
2248 $start_files = " -- " . $start_files;
2249 } else {
2250 $start_files = "";
2251 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002252
Steven Rostedta57419b2010-11-02 15:13:54 -04002253 # convert to true sha1's
2254 $good = get_sha1($good);
2255 $bad = get_sha1($bad);
2256
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002257 if (defined($bisect_reverse) && $bisect_reverse == 1) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002258 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2259 $reverse_bisect = 1;
2260 } else {
2261 $reverse_bisect = 0;
2262 }
2263
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002264 # Can't have a test without having a test to run
2265 if ($type eq "test" && !defined($run_test)) {
2266 $type = "boot";
2267 }
2268
Steven Rostedtdad98752011-11-22 20:48:57 -05002269 # Check if a bisect was running
2270 my $bisect_start_file = "$builddir/.git/BISECT_START";
2271
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002272 my $check = $bisect_check;
Steven Rostedtdad98752011-11-22 20:48:57 -05002273 my $do_check = defined($check) && $check ne "0";
2274
2275 if ( -f $bisect_start_file ) {
2276 print "Bisect in progress found\n";
2277 if ($do_check) {
2278 print " If you say yes, then no checks of good or bad will be done\n";
2279 }
2280 if (defined($replay)) {
2281 print "** BISECT_REPLAY is defined in config file **";
2282 print " Ignore config option and perform new git bisect log?\n";
2283 if (read_ync " (yes, no, or cancel) ") {
2284 $replay = update_bisect_replay;
2285 $do_check = 0;
2286 }
2287 } elsif (read_yn "read git log and continue?") {
2288 $replay = update_bisect_replay;
2289 $do_check = 0;
2290 }
2291 }
2292
2293 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002294
2295 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002296 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002297
2298 if ($check ne "good") {
2299 doprint "TESTING BISECT BAD [$bad]\n";
2300 run_command "git checkout $bad" or
2301 die "Failed to checkout $bad";
2302
2303 $result = run_bisect $type;
2304
2305 if ($result ne "bad") {
2306 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2307 }
2308 }
2309
2310 if ($check ne "bad") {
2311 doprint "TESTING BISECT GOOD [$good]\n";
2312 run_command "git checkout $good" or
2313 die "Failed to checkout $good";
2314
2315 $result = run_bisect $type;
2316
2317 if ($result ne "good") {
2318 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2319 }
2320 }
2321
2322 # checkout where we started
2323 run_command "git checkout $head" or
2324 die "Failed to checkout $head";
2325 }
2326
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002327 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002328 dodie "could not start bisect";
2329
2330 run_command "git bisect good $good" or
2331 dodie "could not set bisect good to $good";
2332
2333 run_git_bisect "git bisect bad $bad" or
2334 dodie "could not set bisect bad to $bad";
2335
2336 if (defined($replay)) {
2337 run_command "git bisect replay $replay" or
2338 dodie "failed to run replay";
2339 }
2340
2341 if (defined($start)) {
2342 run_command "git checkout $start" or
2343 dodie "failed to checkout $start";
2344 }
2345
2346 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002347 do {
2348 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002349 $test = run_git_bisect "git bisect $result";
2350 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002351
2352 run_command "git bisect log" or
2353 dodie "could not capture git bisect log";
2354
2355 run_command "git bisect reset" or
2356 dodie "could not reset git bisect";
2357
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002358 doprint "Bad commit was [$bisect_bad_commit]\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002359
Steven Rostedt0a05c762010-11-08 11:14:10 -05002360 success $i;
2361}
2362
2363my %config_ignore;
2364my %config_set;
2365
2366my %config_list;
2367my %null_config;
2368
2369my %dependency;
2370
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002371sub assign_configs {
2372 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002373
2374 open (IN, $config)
2375 or dodie "Failed to read $config";
2376
2377 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002378 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002379 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002380 }
2381 }
2382
2383 close(IN);
2384}
2385
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002386sub process_config_ignore {
2387 my ($config) = @_;
2388
2389 assign_configs \%config_ignore, $config;
2390}
2391
Steven Rostedt0a05c762010-11-08 11:14:10 -05002392sub read_current_config {
2393 my ($config_ref) = @_;
2394
2395 %{$config_ref} = ();
2396 undef %{$config_ref};
2397
2398 my @key = keys %{$config_ref};
2399 if ($#key >= 0) {
2400 print "did not delete!\n";
2401 exit;
2402 }
2403 open (IN, "$output_config");
2404
2405 while (<IN>) {
2406 if (/^(CONFIG\S+)=(.*)/) {
2407 ${$config_ref}{$1} = $2;
2408 }
2409 }
2410 close(IN);
2411}
2412
2413sub get_dependencies {
2414 my ($config) = @_;
2415
2416 my $arr = $dependency{$config};
2417 if (!defined($arr)) {
2418 return ();
2419 }
2420
2421 my @deps = @{$arr};
2422
2423 foreach my $dep (@{$arr}) {
2424 print "ADD DEP $dep\n";
2425 @deps = (@deps, get_dependencies $dep);
2426 }
2427
2428 return @deps;
2429}
2430
2431sub create_config {
2432 my @configs = @_;
2433
2434 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2435
2436 foreach my $config (@configs) {
2437 print OUT "$config_set{$config}\n";
2438 my @deps = get_dependencies $config;
2439 foreach my $dep (@deps) {
2440 print OUT "$config_set{$dep}\n";
2441 }
2442 }
2443
2444 foreach my $config (keys %config_ignore) {
2445 print OUT "$config_ignore{$config}\n";
2446 }
2447 close(OUT);
2448
2449# exit;
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002450 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002451}
2452
2453sub compare_configs {
2454 my (%a, %b) = @_;
2455
2456 foreach my $item (keys %a) {
2457 if (!defined($b{$item})) {
2458 print "diff $item\n";
2459 return 1;
2460 }
2461 delete $b{$item};
2462 }
2463
2464 my @keys = keys %b;
2465 if ($#keys) {
2466 print "diff2 $keys[0]\n";
2467 }
2468 return -1 if ($#keys >= 0);
2469
2470 return 0;
2471}
2472
2473sub run_config_bisect_test {
2474 my ($type) = @_;
2475
2476 return run_bisect_test $type, "oldconfig";
2477}
2478
2479sub process_passed {
2480 my (%configs) = @_;
2481
2482 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2483 # Passed! All these configs are part of a good compile.
2484 # Add them to the min options.
2485 foreach my $config (keys %configs) {
2486 if (defined($config_list{$config})) {
2487 doprint " removing $config\n";
2488 $config_ignore{$config} = $config_list{$config};
2489 delete $config_list{$config};
2490 }
2491 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002492 doprint "config copied to $outputdir/config_good\n";
2493 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002494}
2495
2496sub process_failed {
2497 my ($config) = @_;
2498
2499 doprint "\n\n***************************************\n";
2500 doprint "Found bad config: $config\n";
2501 doprint "***************************************\n\n";
2502}
2503
2504sub run_config_bisect {
2505
2506 my @start_list = keys %config_list;
2507
2508 if ($#start_list < 0) {
2509 doprint "No more configs to test!!!\n";
2510 return -1;
2511 }
2512
2513 doprint "***** RUN TEST ***\n";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002514 my $type = $config_bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002515 my $ret;
2516 my %current_config;
2517
2518 my $count = $#start_list + 1;
2519 doprint " $count configs to test\n";
2520
2521 my $half = int($#start_list / 2);
2522
2523 do {
2524 my @tophalf = @start_list[0 .. $half];
2525
2526 create_config @tophalf;
2527 read_current_config \%current_config;
2528
2529 $count = $#tophalf + 1;
2530 doprint "Testing $count configs\n";
2531 my $found = 0;
2532 # make sure we test something
2533 foreach my $config (@tophalf) {
2534 if (defined($current_config{$config})) {
2535 logit " $config\n";
2536 $found = 1;
2537 }
2538 }
2539 if (!$found) {
2540 # try the other half
2541 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002542 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedt0a05c762010-11-08 11:14:10 -05002543 create_config @tophalf;
2544 read_current_config \%current_config;
2545 foreach my $config (@tophalf) {
2546 if (defined($current_config{$config})) {
2547 logit " $config\n";
2548 $found = 1;
2549 }
2550 }
2551 if (!$found) {
2552 doprint "Failed: Can't make new config with current configs\n";
2553 foreach my $config (@start_list) {
2554 doprint " CONFIG: $config\n";
2555 }
2556 return -1;
2557 }
2558 $count = $#tophalf + 1;
2559 doprint "Testing $count configs\n";
2560 }
2561
2562 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002563 if ($bisect_manual) {
2564 $ret = answer_bisect;
2565 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002566 if ($ret) {
2567 process_passed %current_config;
2568 return 0;
2569 }
2570
2571 doprint "This config had a failure.\n";
2572 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002573 doprint "config copied to $outputdir/config_bad\n";
2574 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002575
2576 # A config exists in this group that was bad.
2577 foreach my $config (keys %config_list) {
2578 if (!defined($current_config{$config})) {
2579 doprint " removing $config\n";
2580 delete $config_list{$config};
2581 }
2582 }
2583
2584 @start_list = @tophalf;
2585
2586 if ($#start_list == 0) {
2587 process_failed $start_list[0];
2588 return 1;
2589 }
2590
2591 # remove half the configs we are looking at and see if
2592 # they are good.
2593 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002594 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002595
Steven Rostedtc960bb92011-03-08 09:22:39 -05002596 # we found a single config, try it again unless we are running manually
2597
2598 if ($bisect_manual) {
2599 process_failed $start_list[0];
2600 return 1;
2601 }
2602
Steven Rostedt0a05c762010-11-08 11:14:10 -05002603 my @tophalf = @start_list[0 .. 0];
2604
2605 $ret = run_config_bisect_test $type;
2606 if ($ret) {
2607 process_passed %current_config;
2608 return 0;
2609 }
2610
2611 process_failed $start_list[0];
2612 return 1;
2613}
2614
2615sub config_bisect {
2616 my ($i) = @_;
2617
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002618 my $start_config = $config_bisect;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002619
2620 my $tmpconfig = "$tmpdir/use_config";
2621
Steven Rostedt30f75da2011-06-13 10:35:35 -04002622 if (defined($config_bisect_good)) {
2623 process_config_ignore $config_bisect_good;
2624 }
2625
Steven Rostedt0a05c762010-11-08 11:14:10 -05002626 # Make the file with the bad config and the min config
2627 if (defined($minconfig)) {
2628 # read the min config for things to ignore
2629 run_command "cp $minconfig $tmpconfig" or
2630 dodie "failed to copy $minconfig to $tmpconfig";
2631 } else {
2632 unlink $tmpconfig;
2633 }
2634
Steven Rostedt0a05c762010-11-08 11:14:10 -05002635 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002636 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002637 process_config_ignore $tmpconfig;
2638 }
2639
2640 # now process the start config
2641 run_command "cp $start_config $output_config" or
2642 dodie "failed to copy $start_config to $output_config";
2643
2644 # read directly what we want to check
2645 my %config_check;
2646 open (IN, $output_config)
Masanari Iidaf9dee312012-02-11 21:46:56 +09002647 or dodie "failed to open $output_config";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002648
2649 while (<IN>) {
2650 if (/^((CONFIG\S*)=.*)/) {
2651 $config_check{$2} = $1;
2652 }
2653 }
2654 close(IN);
2655
Steven Rostedt250bae82011-07-15 22:05:59 -04002656 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002657 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002658
2659 # check to see what we lost (or gained)
2660 open (IN, $output_config)
2661 or dodie "Failed to read $start_config";
2662
2663 my %removed_configs;
2664 my %added_configs;
2665
2666 while (<IN>) {
2667 if (/^((CONFIG\S*)=.*)/) {
2668 # save off all options
2669 $config_set{$2} = $1;
2670 if (defined($config_check{$2})) {
2671 if (defined($config_ignore{$2})) {
2672 $removed_configs{$2} = $1;
2673 } else {
2674 $config_list{$2} = $1;
2675 }
2676 } elsif (!defined($config_ignore{$2})) {
2677 $added_configs{$2} = $1;
2678 $config_list{$2} = $1;
2679 }
2680 }
2681 }
2682 close(IN);
2683
2684 my @confs = keys %removed_configs;
2685 if ($#confs >= 0) {
2686 doprint "Configs overridden by default configs and removed from check:\n";
2687 foreach my $config (@confs) {
2688 doprint " $config\n";
2689 }
2690 }
2691 @confs = keys %added_configs;
2692 if ($#confs >= 0) {
2693 doprint "Configs appearing in make oldconfig and added:\n";
2694 foreach my $config (@confs) {
2695 doprint " $config\n";
2696 }
2697 }
2698
2699 my %config_test;
2700 my $once = 0;
2701
2702 # Sometimes kconfig does weird things. We must make sure
2703 # that the config we autocreate has everything we need
2704 # to test, otherwise we may miss testing configs, or
2705 # may not be able to create a new config.
2706 # Here we create a config with everything set.
2707 create_config (keys %config_list);
2708 read_current_config \%config_test;
2709 foreach my $config (keys %config_list) {
2710 if (!defined($config_test{$config})) {
2711 if (!$once) {
2712 $once = 1;
2713 doprint "Configs not produced by kconfig (will not be checked):\n";
2714 }
2715 doprint " $config\n";
2716 delete $config_list{$config};
2717 }
2718 }
2719 my $ret;
2720 do {
2721 $ret = run_config_bisect;
2722 } while (!$ret);
2723
2724 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002725
2726 success $i;
2727}
2728
Steven Rostedt27d934b2011-05-20 09:18:18 -04002729sub patchcheck_reboot {
2730 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002731 reboot_to_good $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04002732}
2733
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002734sub patchcheck {
2735 my ($i) = @_;
2736
2737 die "PATCHCHECK_START[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002738 if (!defined($patchcheck_start));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002739 die "PATCHCHECK_TYPE[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002740 if (!defined($patchcheck_type));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002741
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002742 my $start = $patchcheck_start;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002743
2744 my $end = "HEAD";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002745 if (defined($patchcheck_end)) {
2746 $end = $patchcheck_end;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002747 }
2748
Steven Rostedta57419b2010-11-02 15:13:54 -04002749 # Get the true sha1's since we can use things like HEAD~3
2750 $start = get_sha1($start);
2751 $end = get_sha1($end);
2752
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002753 my $type = $patchcheck_type;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002754
2755 # Can't have a test without having a test to run
2756 if ($type eq "test" && !defined($run_test)) {
2757 $type = "boot";
2758 }
2759
2760 open (IN, "git log --pretty=oneline $end|") or
2761 dodie "could not get git list";
2762
2763 my @list;
2764
2765 while (<IN>) {
2766 chomp;
2767 $list[$#list+1] = $_;
2768 last if (/^$start/);
2769 }
2770 close(IN);
2771
2772 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002773 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002774 }
2775
2776 # go backwards in the list
2777 @list = reverse @list;
2778
2779 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04002780 my %ignored_warnings;
2781
2782 if (defined($ignore_warnings)) {
2783 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2784 $ignored_warnings{$sha1} = 1;
2785 }
2786 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002787
2788 $in_patchcheck = 1;
2789 foreach my $item (@list) {
2790 my $sha1 = $item;
2791 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2792
2793 doprint "\nProcessing commit $item\n\n";
2794
2795 run_command "git checkout $sha1" or
2796 die "Failed to checkout $sha1";
2797
2798 # only clean on the first and last patch
2799 if ($item eq $list[0] ||
2800 $item eq $list[$#list]) {
2801 $noclean = $save_clean;
2802 } else {
2803 $noclean = 1;
2804 }
2805
2806 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002807 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002808 } else {
2809 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002810 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002811 }
2812
Steven Rostedt19902072011-06-14 20:46:25 -04002813
2814 if (!defined($ignored_warnings{$sha1})) {
2815 check_buildlog $sha1 or return 0;
2816 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002817
2818 next if ($type eq "build");
2819
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002820 my $failed = 0;
2821
Steven Rostedtddf607e2011-06-14 20:49:13 -04002822 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002823
2824 if (!$failed && $type ne "boot"){
2825 do_run_test or $failed = 1;
2826 }
2827 end_monitor;
2828 return 0 if ($failed);
2829
Steven Rostedt27d934b2011-05-20 09:18:18 -04002830 patchcheck_reboot;
2831
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002832 }
2833 $in_patchcheck = 0;
2834 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002835
2836 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002837}
2838
Steven Rostedtb9066f62011-07-15 21:25:24 -04002839my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04002840my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002841my $iflevel = 0;
2842my @ifdeps;
2843
2844# prevent recursion
2845my %read_kconfigs;
2846
Steven Rostedtac6974c2011-10-04 09:40:17 -04002847sub add_dep {
2848 # $config depends on $dep
2849 my ($config, $dep) = @_;
2850
2851 if (defined($depends{$config})) {
2852 $depends{$config} .= " " . $dep;
2853 } else {
2854 $depends{$config} = $dep;
2855 }
2856
2857 # record the number of configs depending on $dep
2858 if (defined $depcount{$dep}) {
2859 $depcount{$dep}++;
2860 } else {
2861 $depcount{$dep} = 1;
2862 }
2863}
2864
Steven Rostedtb9066f62011-07-15 21:25:24 -04002865# taken from streamline_config.pl
2866sub read_kconfig {
2867 my ($kconfig) = @_;
2868
2869 my $state = "NONE";
2870 my $config;
2871 my @kconfigs;
2872
2873 my $cont = 0;
2874 my $line;
2875
2876
2877 if (! -f $kconfig) {
2878 doprint "file $kconfig does not exist, skipping\n";
2879 return;
2880 }
2881
2882 open(KIN, "$kconfig")
2883 or die "Can't open $kconfig";
2884 while (<KIN>) {
2885 chomp;
2886
2887 # Make sure that lines ending with \ continue
2888 if ($cont) {
2889 $_ = $line . " " . $_;
2890 }
2891
2892 if (s/\\$//) {
2893 $cont = 1;
2894 $line = $_;
2895 next;
2896 }
2897
2898 $cont = 0;
2899
2900 # collect any Kconfig sources
2901 if (/^source\s*"(.*)"/) {
2902 $kconfigs[$#kconfigs+1] = $1;
2903 }
2904
2905 # configs found
2906 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2907 $state = "NEW";
2908 $config = $2;
2909
2910 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04002911 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04002912 }
2913
2914 # collect the depends for the config
2915 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2916
Steven Rostedtac6974c2011-10-04 09:40:17 -04002917 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002918
2919 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04002920 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2921
2922 # selected by depends on config
2923 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002924
2925 # Check for if statements
2926 } elsif (/^if\s+(.*\S)\s*$/) {
2927 my $deps = $1;
2928 # remove beginning and ending non text
2929 $deps =~ s/^[^a-zA-Z0-9_]*//;
2930 $deps =~ s/[^a-zA-Z0-9_]*$//;
2931
2932 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2933
2934 $ifdeps[$iflevel++] = join ':', @deps;
2935
2936 } elsif (/^endif/) {
2937
2938 $iflevel-- if ($iflevel);
2939
2940 # stop on "help"
2941 } elsif (/^\s*help\s*$/) {
2942 $state = "NONE";
2943 }
2944 }
2945 close(KIN);
2946
2947 # read in any configs that were found.
2948 foreach $kconfig (@kconfigs) {
2949 if (!defined($read_kconfigs{$kconfig})) {
2950 $read_kconfigs{$kconfig} = 1;
2951 read_kconfig("$builddir/$kconfig");
2952 }
2953 }
2954}
2955
2956sub read_depends {
2957 # find out which arch this is by the kconfig file
2958 open (IN, $output_config)
2959 or dodie "Failed to read $output_config";
2960 my $arch;
2961 while (<IN>) {
2962 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2963 $arch = $1;
2964 last;
2965 }
2966 }
2967 close IN;
2968
2969 if (!defined($arch)) {
2970 doprint "Could not find arch from config file\n";
2971 doprint "no dependencies used\n";
2972 return;
2973 }
2974
2975 # arch is really the subarch, we need to know
2976 # what directory to look at.
2977 if ($arch eq "i386" || $arch eq "x86_64") {
2978 $arch = "x86";
2979 } elsif ($arch =~ /^tile/) {
2980 $arch = "tile";
2981 }
2982
2983 my $kconfig = "$builddir/arch/$arch/Kconfig";
2984
2985 if (! -f $kconfig && $arch =~ /\d$/) {
2986 my $orig = $arch;
2987 # some subarchs have numbers, truncate them
2988 $arch =~ s/\d*$//;
2989 $kconfig = "$builddir/arch/$arch/Kconfig";
2990 if (! -f $kconfig) {
2991 doprint "No idea what arch dir $orig is for\n";
2992 doprint "no dependencies used\n";
2993 return;
2994 }
2995 }
2996
2997 read_kconfig($kconfig);
2998}
2999
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003000sub read_config_list {
3001 my ($config) = @_;
3002
3003 open (IN, $config)
3004 or dodie "Failed to read $config";
3005
3006 while (<IN>) {
3007 if (/^((CONFIG\S*)=.*)/) {
3008 if (!defined($config_ignore{$2})) {
3009 $config_list{$2} = $1;
3010 }
3011 }
3012 }
3013
3014 close(IN);
3015}
3016
3017sub read_output_config {
3018 my ($config) = @_;
3019
3020 assign_configs \%config_ignore, $config;
3021}
3022
3023sub make_new_config {
3024 my @configs = @_;
3025
3026 open (OUT, ">$output_config")
3027 or dodie "Failed to write $output_config";
3028
3029 foreach my $config (@configs) {
3030 print OUT "$config\n";
3031 }
3032 close OUT;
3033}
3034
Steven Rostedtac6974c2011-10-04 09:40:17 -04003035sub chomp_config {
3036 my ($config) = @_;
3037
3038 $config =~ s/CONFIG_//;
3039
3040 return $config;
3041}
3042
Steven Rostedtb9066f62011-07-15 21:25:24 -04003043sub get_depends {
3044 my ($dep) = @_;
3045
Steven Rostedtac6974c2011-10-04 09:40:17 -04003046 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003047
3048 $dep = $depends{"$kconfig"};
3049
3050 # the dep string we have saves the dependencies as they
3051 # were found, including expressions like ! && ||. We
3052 # want to split this out into just an array of configs.
3053
3054 my $valid = "A-Za-z_0-9";
3055
3056 my @configs;
3057
3058 while ($dep =~ /[$valid]/) {
3059
3060 if ($dep =~ /^[^$valid]*([$valid]+)/) {
3061 my $conf = "CONFIG_" . $1;
3062
3063 $configs[$#configs + 1] = $conf;
3064
3065 $dep =~ s/^[^$valid]*[$valid]+//;
3066 } else {
3067 die "this should never happen";
3068 }
3069 }
3070
3071 return @configs;
3072}
3073
3074my %min_configs;
3075my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003076my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003077my %processed_configs;
3078my %nochange_config;
3079
3080sub test_this_config {
3081 my ($config) = @_;
3082
3083 my $found;
3084
3085 # if we already processed this config, skip it
3086 if (defined($processed_configs{$config})) {
3087 return undef;
3088 }
3089 $processed_configs{$config} = 1;
3090
3091 # if this config failed during this round, skip it
3092 if (defined($nochange_config{$config})) {
3093 return undef;
3094 }
3095
Steven Rostedtac6974c2011-10-04 09:40:17 -04003096 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003097
3098 # Test dependencies first
3099 if (defined($depends{"$kconfig"})) {
3100 my @parents = get_depends $config;
3101 foreach my $parent (@parents) {
3102 # if the parent is in the min config, check it first
3103 next if (!defined($min_configs{$parent}));
3104 $found = test_this_config($parent);
3105 if (defined($found)) {
3106 return $found;
3107 }
3108 }
3109 }
3110
3111 # Remove this config from the list of configs
3112 # do a make oldnoconfig and then read the resulting
3113 # .config to make sure it is missing the config that
3114 # we had before
3115 my %configs = %min_configs;
3116 delete $configs{$config};
3117 make_new_config ((values %configs), (values %keep_configs));
3118 make_oldconfig;
3119 undef %configs;
3120 assign_configs \%configs, $output_config;
3121
3122 return $config if (!defined($configs{$config}));
3123
3124 doprint "disabling config $config did not change .config\n";
3125
3126 $nochange_config{$config} = 1;
3127
3128 return undef;
3129}
3130
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003131sub make_min_config {
3132 my ($i) = @_;
3133
Steven Rostedtccc513b2012-05-21 17:13:40 -04003134 my $type = $minconfig_type;
3135 if ($type ne "boot" && $type ne "test") {
3136 fail "Invalid MIN_CONFIG_TYPE '$minconfig_type'\n" .
3137 " make_min_config works only with 'boot' and 'test'\n" and return;
3138 }
3139
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003140 if (!defined($output_minconfig)) {
3141 fail "OUTPUT_MIN_CONFIG not defined" and return;
3142 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04003143
3144 # If output_minconfig exists, and the start_minconfig
3145 # came from min_config, than ask if we should use
3146 # that instead.
3147 if (-f $output_minconfig && !$start_minconfig_defined) {
3148 print "$output_minconfig exists\n";
3149 if (read_yn " Use it as minconfig?") {
3150 $start_minconfig = $output_minconfig;
3151 }
3152 }
3153
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003154 if (!defined($start_minconfig)) {
3155 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
3156 }
3157
Steven Rostedt35ce5952011-07-15 21:57:25 -04003158 my $temp_config = "$tmpdir/temp_config";
3159
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003160 # First things first. We build an allnoconfig to find
3161 # out what the defaults are that we can't touch.
3162 # Some are selections, but we really can't handle selections.
3163
3164 my $save_minconfig = $minconfig;
3165 undef $minconfig;
3166
3167 run_command "$make allnoconfig" or return 0;
3168
Steven Rostedtb9066f62011-07-15 21:25:24 -04003169 read_depends;
3170
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003171 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003172
Steven Rostedt43d1b652011-07-15 22:01:56 -04003173 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003174 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003175
3176 if (defined($ignore_config)) {
3177 # make sure the file exists
3178 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003179 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003180 }
3181
Steven Rostedt43d1b652011-07-15 22:01:56 -04003182 %keep_configs = %save_configs;
3183
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003184 doprint "Load initial configs from $start_minconfig\n";
3185
3186 # Look at the current min configs, and save off all the
3187 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003188 assign_configs \%min_configs, $start_minconfig;
3189
3190 my @config_keys = keys %min_configs;
3191
Steven Rostedtac6974c2011-10-04 09:40:17 -04003192 # All configs need a depcount
3193 foreach my $config (@config_keys) {
3194 my $kconfig = chomp_config $config;
3195 if (!defined $depcount{$kconfig}) {
3196 $depcount{$kconfig} = 0;
3197 }
3198 }
3199
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003200 # Remove anything that was set by the make allnoconfig
3201 # we shouldn't need them as they get set for us anyway.
3202 foreach my $config (@config_keys) {
3203 # Remove anything in the ignore_config
3204 if (defined($keep_configs{$config})) {
3205 my $file = $ignore_config;
3206 $file =~ s,.*/(.*?)$,$1,;
3207 doprint "$config set by $file ... ignored\n";
3208 delete $min_configs{$config};
3209 next;
3210 }
3211 # But make sure the settings are the same. If a min config
3212 # sets a selection, we do not want to get rid of it if
3213 # it is not the same as what we have. Just move it into
3214 # the keep configs.
3215 if (defined($config_ignore{$config})) {
3216 if ($config_ignore{$config} ne $min_configs{$config}) {
3217 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3218 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3219 $keep_configs{$config} = $min_configs{$config};
3220 } else {
3221 doprint "$config set by allnoconfig ... ignored\n";
3222 }
3223 delete $min_configs{$config};
3224 }
3225 }
3226
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003227 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003228 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003229
3230 while (!$done) {
3231
3232 my $config;
3233 my $found;
3234
3235 # Now disable each config one by one and do a make oldconfig
3236 # till we find a config that changes our list.
3237
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003238 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003239
3240 # Sort keys by who is most dependent on
3241 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3242 @test_configs ;
3243
3244 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003245 my $reset = 1;
3246 for (my $i = 0; $i < $#test_configs; $i++) {
3247 if (!defined($nochange_config{$test_configs[0]})) {
3248 $reset = 0;
3249 last;
3250 }
3251 # This config didn't change the .config last time.
3252 # Place it at the end
3253 my $config = shift @test_configs;
3254 push @test_configs, $config;
3255 }
3256
3257 # if every test config has failed to modify the .config file
3258 # in the past, then reset and start over.
3259 if ($reset) {
3260 undef %nochange_config;
3261 }
3262
Steven Rostedtb9066f62011-07-15 21:25:24 -04003263 undef %processed_configs;
3264
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003265 foreach my $config (@test_configs) {
3266
Steven Rostedtb9066f62011-07-15 21:25:24 -04003267 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003268
Steven Rostedtb9066f62011-07-15 21:25:24 -04003269 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003270
3271 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003272 }
3273
3274 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003275 # we could have failed due to the nochange_config hash
3276 # reset and try again
3277 if (!$take_two) {
3278 undef %nochange_config;
3279 $take_two = 1;
3280 next;
3281 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003282 doprint "No more configs found that we can disable\n";
3283 $done = 1;
3284 last;
3285 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003286 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003287
3288 $config = $found;
3289
3290 doprint "Test with $config disabled\n";
3291
3292 # set in_bisect to keep build and monitor from dieing
3293 $in_bisect = 1;
3294
3295 my $failed = 0;
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003296 build "oldconfig" or $failed = 1;
3297 if (!$failed) {
3298 start_monitor_and_boot or $failed = 1;
Steven Rostedtccc513b2012-05-21 17:13:40 -04003299
3300 if ($type eq "test" && !$failed) {
3301 do_run_test or $failed = 1;
3302 }
3303
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003304 end_monitor;
3305 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003306
3307 $in_bisect = 0;
3308
3309 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003310 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003311 # this config is needed, add it to the ignore list.
3312 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003313 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003314 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003315
3316 # update new ignore configs
3317 if (defined($ignore_config)) {
3318 open (OUT, ">$temp_config")
3319 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003320 foreach my $config (keys %save_configs) {
3321 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003322 }
3323 close OUT;
3324 run_command "mv $temp_config $ignore_config" or
3325 dodie "failed to copy update to $ignore_config";
3326 }
3327
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003328 } else {
3329 # We booted without this config, remove it from the minconfigs.
3330 doprint "$config is not needed, disabling\n";
3331
3332 delete $min_configs{$config};
3333
3334 # Also disable anything that is not enabled in this config
3335 my %configs;
3336 assign_configs \%configs, $output_config;
3337 my @config_keys = keys %min_configs;
3338 foreach my $config (@config_keys) {
3339 if (!defined($configs{$config})) {
3340 doprint "$config is not set, disabling\n";
3341 delete $min_configs{$config};
3342 }
3343 }
3344
3345 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003346 open (OUT, ">$temp_config")
3347 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003348 foreach my $config (keys %keep_configs) {
3349 print OUT "$keep_configs{$config}\n";
3350 }
3351 foreach my $config (keys %min_configs) {
3352 print OUT "$min_configs{$config}\n";
3353 }
3354 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003355
3356 run_command "mv $temp_config $output_minconfig" or
3357 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003358 }
3359
3360 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003361 reboot_to_good $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003362 }
3363
3364 success $i;
3365 return 1;
3366}
3367
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003368$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003369
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003370if ($#ARGV == 0) {
3371 $ktest_config = $ARGV[0];
3372 if (! -f $ktest_config) {
3373 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003374 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003375 exit 0;
3376 }
3377 }
3378} else {
3379 $ktest_config = "ktest.conf";
3380}
3381
3382if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003383 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003384 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003385 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3386 print OUT << "EOF"
3387# Generated by ktest.pl
3388#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003389
3390# PWD is a ktest.pl variable that will result in the process working
3391# directory that ktest.pl is executed in.
3392
3393# THIS_DIR is automatically assigned the PWD of the path that generated
3394# the config file. It is best to use this variable when assigning other
3395# directory paths within this directory. This allows you to easily
3396# move the test cases to other locations or to other machines.
3397#
3398THIS_DIR := $variable{"PWD"}
3399
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003400# Define each test with TEST_START
3401# The config options below it will override the defaults
3402TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003403TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003404
3405DEFAULTS
3406EOF
3407;
3408 close(OUT);
3409}
3410read_config $ktest_config;
3411
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003412if (defined($opt{"LOG_FILE"})) {
3413 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3414}
3415
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003416# Append any configs entered in manually to the config file.
3417my @new_configs = keys %entered_configs;
3418if ($#new_configs >= 0) {
3419 print "\nAppending entered in configs to $ktest_config\n";
3420 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3421 foreach my $config (@new_configs) {
3422 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003423 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003424 }
3425}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003426
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003427if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3428 unlink $opt{"LOG_FILE"};
3429}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003430
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003431doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3432
Steven Rostedta57419b2010-11-02 15:13:54 -04003433for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3434
3435 if (!$i) {
3436 doprint "DEFAULT OPTIONS:\n";
3437 } else {
3438 doprint "\nTEST $i OPTIONS";
3439 if (defined($repeat_tests{$i})) {
3440 $repeat = $repeat_tests{$i};
3441 doprint " ITERATE $repeat";
3442 }
3443 doprint "\n";
3444 }
3445
3446 foreach my $option (sort keys %opt) {
3447
3448 if ($option =~ /\[(\d+)\]$/) {
3449 next if ($i != $1);
3450 } else {
3451 next if ($i);
3452 }
3453
3454 doprint "$option = $opt{$option}\n";
3455 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003456}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003457
Steven Rostedt2a625122011-05-20 15:48:59 -04003458sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003459 my ($name, $i) = @_;
3460
3461 my $option = "$name\[$i\]";
3462
3463 if (defined($opt{$option})) {
3464 return $opt{$option};
3465 }
3466
Steven Rostedta57419b2010-11-02 15:13:54 -04003467 foreach my $test (keys %repeat_tests) {
3468 if ($i >= $test &&
3469 $i < $test + $repeat_tests{$test}) {
3470 $option = "$name\[$test\]";
3471 if (defined($opt{$option})) {
3472 return $opt{$option};
3473 }
3474 }
3475 }
3476
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003477 if (defined($opt{$name})) {
3478 return $opt{$name};
3479 }
3480
3481 return undef;
3482}
3483
Steven Rostedt2a625122011-05-20 15:48:59 -04003484sub set_test_option {
3485 my ($name, $i) = @_;
3486
3487 my $option = __set_test_option($name, $i);
3488 return $option if (!defined($option));
3489
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003490 return eval_option($option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003491}
3492
Steven Rostedt2545eb62010-11-02 15:01:32 -04003493# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003494for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003495
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003496 # Do not reboot on failing test options
3497 $no_reboot = 1;
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003498 $reboot_success = 0;
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003499
Steven Rostedt683a3e62012-05-18 13:34:35 -04003500 $have_version = 0;
3501
Steven Rostedt576f6272010-11-02 14:58:38 -04003502 $iteration = $i;
3503
Steven Rostedta75fece2010-11-02 14:58:27 -04003504 my $makecmd = set_test_option("MAKE_CMD", $i);
3505
Steven Rostedt9cc9e092011-12-22 21:37:22 -05003506 # Load all the options into their mapped variable names
3507 foreach my $opt (keys %option_map) {
3508 ${$option_map{$opt}} = set_test_option($opt, $i);
3509 }
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003510
Steven Rostedt35ce5952011-07-15 21:57:25 -04003511 $start_minconfig_defined = 1;
3512
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003513 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003514 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003515 $start_minconfig = $minconfig;
3516 }
3517
Steven Rostedta75fece2010-11-02 14:58:27 -04003518 chdir $builddir || die "can't change directory to $builddir";
3519
Andrew Jonesa908a662011-08-12 15:32:03 +02003520 foreach my $dir ($tmpdir, $outputdir) {
3521 if (!-d $dir) {
3522 mkpath($dir) or
3523 die "can't create $dir";
3524 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003525 }
3526
Steven Rostedte48c5292010-11-02 14:35:37 -04003527 $ENV{"SSH_USER"} = $ssh_user;
3528 $ENV{"MACHINE"} = $machine;
3529
Steven Rostedta75fece2010-11-02 14:58:27 -04003530 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303531 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003532 $dmesg = "$tmpdir/dmesg-$machine";
3533 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003534 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003535
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003536 if (!$buildonly) {
3537 $target = "$ssh_user\@$machine";
3538 if ($reboot_type eq "grub") {
3539 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003540 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003541 }
3542
3543 my $run_type = $build_type;
3544 if ($test_type eq "patchcheck") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003545 $run_type = $patchcheck_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003546 } elsif ($test_type eq "bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003547 $run_type = $bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003548 } elsif ($test_type eq "config_bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003549 $run_type = $config_bisect_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003550 }
3551
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003552 if ($test_type eq "make_min_config") {
3553 $run_type = "";
3554 }
3555
Steven Rostedta75fece2010-11-02 14:58:27 -04003556 # mistake in config file?
3557 if (!defined($run_type)) {
3558 $run_type = "ERROR";
3559 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003560
Steven Rostedte0a87422011-09-30 17:50:48 -04003561 my $installme = "";
3562 $installme = " no_install" if ($no_install);
3563
Steven Rostedt2545eb62010-11-02 15:01:32 -04003564 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003565 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003566
3567 unlink $dmesg;
3568 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303569 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003570
Steven Rostedt250bae82011-07-15 22:05:59 -04003571 if (defined($addconfig)) {
3572 my $min = $minconfig;
3573 if (!defined($minconfig)) {
3574 $min = "";
3575 }
3576 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003577 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003578 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003579 }
3580
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003581 if (defined($checkout)) {
3582 run_command "git checkout $checkout" or
3583 die "failed to checkout $checkout";
3584 }
3585
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003586 $no_reboot = 0;
3587
Steven Rostedt648a1822012-03-21 11:18:27 -04003588 # A test may opt to not reboot the box
3589 if ($reboot_on_success) {
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003590 $reboot_success = 1;
Steven Rostedt648a1822012-03-21 11:18:27 -04003591 }
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003592
Steven Rostedta75fece2010-11-02 14:58:27 -04003593 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003594 bisect $i;
3595 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003596 } elsif ($test_type eq "config_bisect") {
3597 config_bisect $i;
3598 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003599 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003600 patchcheck $i;
3601 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003602 } elsif ($test_type eq "make_min_config") {
3603 make_min_config $i;
3604 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003605 }
3606
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003607 if ($build_type ne "nobuild") {
3608 build $build_type or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003609 }
3610
Steven Rostedtcd8e3682011-08-18 16:35:44 -04003611 if ($test_type eq "install") {
3612 get_version;
3613 install;
3614 success $i;
3615 next;
3616 }
3617
Steven Rostedta75fece2010-11-02 14:58:27 -04003618 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04003619 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04003620 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04003621
3622 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3623 do_run_test or $failed = 1;
3624 }
3625 end_monitor;
3626 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003627 }
3628
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003629 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003630}
3631
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003632if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003633 halt;
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003634} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot && $reboot_success) {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003635 reboot_to_good;
Steven Rostedt648a1822012-03-21 11:18:27 -04003636} elsif (defined($switch_to_good)) {
3637 # still need to get to the good kernel
3638 run_command $switch_to_good;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003639}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003640
Steven Rostedt648a1822012-03-21 11:18:27 -04003641
Steven Rostedte48c5292010-11-02 14:35:37 -04003642doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3643
Steven Rostedt2545eb62010-11-02 15:01:32 -04003644exit 0;