blob: f81c8b3a39a884e4097156eb50c559a8abc460b6 [file] [log] [blame]
Steven Rostedt2545eb62010-11-02 15:01:32 -04001#!/usr/bin/perl -w
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002#
Uwe Kleine-Königcce1dac2011-01-24 21:12:01 +01003# Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04004# Licensed under the terms of the GNU GPL License version 2
5#
Steven Rostedt2545eb62010-11-02 15:01:32 -04006
7use strict;
8use IPC::Open2;
9use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Steven Rostedt7faafbd2010-11-02 14:58:22 -040010use File::Path qw(mkpath);
11use File::Copy qw(cp);
Steven Rostedt2545eb62010-11-02 15:01:32 -040012use FileHandle;
13
Steven Rostedte48c5292010-11-02 14:35:37 -040014my $VERSION = "0.2";
15
Steven Rostedt2545eb62010-11-02 15:01:32 -040016$| = 1;
17
18my %opt;
Steven Rostedta57419b2010-11-02 15:13:54 -040019my %repeat_tests;
20my %repeats;
Steven Rostedt2545eb62010-11-02 15:01:32 -040021
22#default opts
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050023my %default = (
24 "NUM_TESTS" => 1,
25 "TEST_TYPE" => "build",
26 "BUILD_TYPE" => "randconfig",
27 "MAKE_CMD" => "make",
28 "TIMEOUT" => 120,
29 "TMP_DIR" => "/tmp/ktest/\${MACHINE}",
30 "SLEEP_TIME" => 60, # sleep time between tests
31 "BUILD_NOCLEAN" => 0,
32 "REBOOT_ON_ERROR" => 0,
33 "POWEROFF_ON_ERROR" => 0,
34 "REBOOT_ON_SUCCESS" => 1,
35 "POWEROFF_ON_SUCCESS" => 0,
36 "BUILD_OPTIONS" => "",
37 "BISECT_SLEEP_TIME" => 60, # sleep time between bisects
38 "PATCHCHECK_SLEEP_TIME" => 60, # sleep time between patch checks
39 "CLEAR_LOG" => 0,
40 "BISECT_MANUAL" => 0,
41 "BISECT_SKIP" => 1,
42 "SUCCESS_LINE" => "login:",
43 "DETECT_TRIPLE_FAULT" => 1,
44 "NO_INSTALL" => 0,
45 "BOOTED_TIMEOUT" => 1,
46 "DIE_ON_FAILURE" => 1,
47 "SSH_EXEC" => "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND",
48 "SCP_TO_TARGET" => "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE",
49 "REBOOT" => "ssh \$SSH_USER\@\$MACHINE reboot",
50 "STOP_AFTER_SUCCESS" => 10,
51 "STOP_AFTER_FAILURE" => 60,
52 "STOP_TEST_AFTER" => 600,
Steven Rostedt600bbf02011-11-21 20:12:04 -050053
54# required, and we will ask users if they don't have them but we keep the default
55# value something that is common.
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050056 "REBOOT_TYPE" => "grub",
57 "LOCALVERSION" => "-test",
58 "SSH_USER" => "root",
59 "BUILD_TARGET" => "arch/x86/boot/bzImage",
60 "TARGET_IMAGE" => "/boot/vmlinuz-test",
Steven Rostedt9cc9e092011-12-22 21:37:22 -050061
62 "LOG_FILE" => undef,
63 "IGNORE_UNUSED" => 0,
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050064);
Steven Rostedt2545eb62010-11-02 15:01:32 -040065
Steven Rostedt8d1491b2010-11-18 15:39:48 -050066my $ktest_config;
Steven Rostedt2545eb62010-11-02 15:01:32 -040067my $version;
Steven Rostedta75fece2010-11-02 14:58:27 -040068my $machine;
Steven Rostedte48c5292010-11-02 14:35:37 -040069my $ssh_user;
Steven Rostedta75fece2010-11-02 14:58:27 -040070my $tmpdir;
71my $builddir;
72my $outputdir;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -050073my $output_config;
Steven Rostedta75fece2010-11-02 14:58:27 -040074my $test_type;
Steven Rostedt7faafbd2010-11-02 14:58:22 -040075my $build_type;
Steven Rostedta75fece2010-11-02 14:58:27 -040076my $build_options;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -040077my $pre_build;
78my $post_build;
79my $pre_build_die;
80my $post_build_die;
Steven Rostedta75fece2010-11-02 14:58:27 -040081my $reboot_type;
82my $reboot_script;
83my $power_cycle;
Steven Rostedte48c5292010-11-02 14:35:37 -040084my $reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -040085my $reboot_on_error;
Steven Rostedtbc7c5802011-12-22 16:29:10 -050086my $switch_to_good;
87my $switch_to_test;
Steven Rostedta75fece2010-11-02 14:58:27 -040088my $poweroff_on_error;
89my $die_on_failure;
Steven Rostedt576f6272010-11-02 14:58:38 -040090my $powercycle_after_reboot;
91my $poweroff_after_halt;
Steven Rostedte48c5292010-11-02 14:35:37 -040092my $ssh_exec;
93my $scp_to_target;
Steven Rostedta75fece2010-11-02 14:58:27 -040094my $power_off;
95my $grub_menu;
Steven Rostedt2545eb62010-11-02 15:01:32 -040096my $grub_number;
97my $target;
98my $make;
Steven Rostedt8b37ca82010-11-02 14:58:33 -040099my $post_install;
Steven Rostedte0a87422011-09-30 17:50:48 -0400100my $no_install;
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400101my $noclean;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400102my $minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400103my $start_minconfig;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400104my $start_minconfig_defined;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400105my $output_minconfig;
106my $ignore_config;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -0400107my $addconfig;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400108my $in_bisect = 0;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500109my $bisect_bad_commit = "";
Steven Rostedtd6ce2a02010-11-02 14:58:05 -0400110my $reverse_bisect;
Steven Rostedtc960bb92011-03-08 09:22:39 -0500111my $bisect_manual;
Steven Rostedtc23dca72011-03-08 09:26:31 -0500112my $bisect_skip;
Steven Rostedt30f75da2011-06-13 10:35:35 -0400113my $config_bisect_good;
Steven Rostedtc5dacb82011-12-22 12:43:57 -0500114my $bisect_ret_good;
115my $bisect_ret_bad;
116my $bisect_ret_skip;
117my $bisect_ret_abort;
118my $bisect_ret_default;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400119my $in_patchcheck = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400120my $run_test;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400121my $redirect;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400122my $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +0530123my $testlog;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400124my $dmesg;
125my $monitor_fp;
126my $monitor_pid;
127my $monitor_cnt = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -0400128my $sleep_time;
129my $bisect_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -0400130my $patchcheck_sleep_time;
Steven Rostedt19902072011-06-14 20:46:25 -0400131my $ignore_warnings;
Steven Rostedta75fece2010-11-02 14:58:27 -0400132my $store_failures;
Rabin Vincentde5b6e32011-11-18 17:05:31 +0530133my $store_successes;
Steven Rostedt9064af52011-06-13 10:38:48 -0400134my $test_name;
Steven Rostedta75fece2010-11-02 14:58:27 -0400135my $timeout;
136my $booted_timeout;
Steven Rostedtf1a5b962011-06-13 10:30:00 -0400137my $detect_triplefault;
Steven Rostedta75fece2010-11-02 14:58:27 -0400138my $console;
Steven Rostedt2b803362011-09-30 18:00:23 -0400139my $reboot_success_line;
Steven Rostedta75fece2010-11-02 14:58:27 -0400140my $success_line;
Steven Rostedt1c8a6172010-11-09 12:55:40 -0500141my $stop_after_success;
142my $stop_after_failure;
Steven Rostedt2d01b262011-03-08 09:47:54 -0500143my $stop_test_after;
Steven Rostedta75fece2010-11-02 14:58:27 -0400144my $build_target;
145my $target_image;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500146my $checkout;
Steven Rostedta75fece2010-11-02 14:58:27 -0400147my $localversion;
Steven Rostedt576f6272010-11-02 14:58:38 -0400148my $iteration = 0;
Steven Rostedte48c5292010-11-02 14:35:37 -0400149my $successes = 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400150
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500151my $bisect_good;
152my $bisect_bad;
153my $bisect_type;
154my $bisect_start;
155my $bisect_replay;
156my $bisect_files;
157my $bisect_reverse;
158my $bisect_check;
159
160my $config_bisect;
161my $config_bisect_type;
162
163my $patchcheck_type;
164my $patchcheck_start;
165my $patchcheck_end;
166
Steven Rostedt165708b2011-11-26 20:56:52 -0500167# set when a test is something other that just building or install
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500168# which would require more options.
169my $buildonly = 1;
170
Steven Rostedtdbd37832011-11-23 16:00:48 -0500171# set when creating a new config
172my $newconfig = 0;
173
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500174my %entered_configs;
175my %config_help;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400176my %variable;
Steven Rostedtfcb3f162011-06-13 10:40:58 -0400177my %force_config;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500178
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400179# do not force reboots on config problems
180my $no_reboot = 1;
181
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500182my %option_map = (
183 "MACHINE" => \$machine,
184 "SSH_USER" => \$ssh_user,
185 "TMP_DIR" => \$tmpdir,
186 "OUTPUT_DIR" => \$outputdir,
187 "BUILD_DIR" => \$builddir,
188 "TEST_TYPE" => \$test_type,
189 "BUILD_TYPE" => \$build_type,
190 "BUILD_OPTIONS" => \$build_options,
191 "PRE_BUILD" => \$pre_build,
192 "POST_BUILD" => \$post_build,
193 "PRE_BUILD_DIE" => \$pre_build_die,
194 "POST_BUILD_DIE" => \$post_build_die,
195 "POWER_CYCLE" => \$power_cycle,
196 "REBOOT" => \$reboot,
197 "BUILD_NOCLEAN" => \$noclean,
198 "MIN_CONFIG" => \$minconfig,
199 "OUTPUT_MIN_CONFIG" => \$output_minconfig,
200 "START_MIN_CONFIG" => \$start_minconfig,
201 "IGNORE_CONFIG" => \$ignore_config,
202 "TEST" => \$run_test,
203 "ADD_CONFIG" => \$addconfig,
204 "REBOOT_TYPE" => \$reboot_type,
205 "GRUB_MENU" => \$grub_menu,
206 "POST_INSTALL" => \$post_install,
207 "NO_INSTALL" => \$no_install,
208 "REBOOT_SCRIPT" => \$reboot_script,
209 "REBOOT_ON_ERROR" => \$reboot_on_error,
210 "SWITCH_TO_GOOD" => \$switch_to_good,
211 "SWITCH_TO_TEST" => \$switch_to_test,
212 "POWEROFF_ON_ERROR" => \$poweroff_on_error,
213 "DIE_ON_FAILURE" => \$die_on_failure,
214 "POWER_OFF" => \$power_off,
215 "POWERCYCLE_AFTER_REBOOT" => \$powercycle_after_reboot,
216 "POWEROFF_AFTER_HALT" => \$poweroff_after_halt,
217 "SLEEP_TIME" => \$sleep_time,
218 "BISECT_SLEEP_TIME" => \$bisect_sleep_time,
219 "PATCHCHECK_SLEEP_TIME" => \$patchcheck_sleep_time,
220 "IGNORE_WARNINGS" => \$ignore_warnings,
221 "BISECT_MANUAL" => \$bisect_manual,
222 "BISECT_SKIP" => \$bisect_skip,
223 "CONFIG_BISECT_GOOD" => \$config_bisect_good,
224 "BISECT_RET_GOOD" => \$bisect_ret_good,
225 "BISECT_RET_BAD" => \$bisect_ret_bad,
226 "BISECT_RET_SKIP" => \$bisect_ret_skip,
227 "BISECT_RET_ABORT" => \$bisect_ret_abort,
228 "BISECT_RET_DEFAULT" => \$bisect_ret_default,
229 "STORE_FAILURES" => \$store_failures,
230 "STORE_SUCCESSES" => \$store_successes,
231 "TEST_NAME" => \$test_name,
232 "TIMEOUT" => \$timeout,
233 "BOOTED_TIMEOUT" => \$booted_timeout,
234 "CONSOLE" => \$console,
235 "DETECT_TRIPLE_FAULT" => \$detect_triplefault,
236 "SUCCESS_LINE" => \$success_line,
237 "REBOOT_SUCCESS_LINE" => \$reboot_success_line,
238 "STOP_AFTER_SUCCESS" => \$stop_after_success,
239 "STOP_AFTER_FAILURE" => \$stop_after_failure,
240 "STOP_TEST_AFTER" => \$stop_test_after,
241 "BUILD_TARGET" => \$build_target,
242 "SSH_EXEC" => \$ssh_exec,
243 "SCP_TO_TARGET" => \$scp_to_target,
244 "CHECKOUT" => \$checkout,
245 "TARGET_IMAGE" => \$target_image,
246 "LOCALVERSION" => \$localversion,
247
248 "BISECT_GOOD" => \$bisect_good,
249 "BISECT_BAD" => \$bisect_bad,
250 "BISECT_TYPE" => \$bisect_type,
251 "BISECT_START" => \$bisect_start,
252 "BISECT_REPLAY" => \$bisect_replay,
253 "BISECT_FILES" => \$bisect_files,
254 "BISECT_REVERSE" => \$bisect_reverse,
255 "BISECT_CHECK" => \$bisect_check,
256
257 "CONFIG_BISECT" => \$config_bisect,
258 "CONFIG_BISECT_TYPE" => \$config_bisect_type,
259
260 "PATCHCHECK_TYPE" => \$patchcheck_type,
261 "PATCHCHECK_START" => \$patchcheck_start,
262 "PATCHCHECK_END" => \$patchcheck_end,
263);
264
265# Options may be used by other options, record them.
266my %used_options;
267
Steven Rostedt7bf51072011-10-22 09:07:03 -0400268# default variables that can be used
269chomp ($variable{"PWD"} = `pwd`);
270
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500271$config_help{"MACHINE"} = << "EOF"
272 The machine hostname that you will test.
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500273 For build only tests, it is still needed to differentiate log files.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500274EOF
275 ;
276$config_help{"SSH_USER"} = << "EOF"
277 The box is expected to have ssh on normal bootup, provide the user
278 (most likely root, since you need privileged operations)
279EOF
280 ;
281$config_help{"BUILD_DIR"} = << "EOF"
282 The directory that contains the Linux source code (full path).
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500283 You can use \${PWD} that will be the path where ktest.pl is run, or use
284 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500285EOF
286 ;
287$config_help{"OUTPUT_DIR"} = << "EOF"
288 The directory that the objects will be built (full path).
289 (can not be same as BUILD_DIR)
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500290 You can use \${PWD} that will be the path where ktest.pl is run, or use
291 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500292EOF
293 ;
294$config_help{"BUILD_TARGET"} = << "EOF"
295 The location of the compiled file to copy to the target.
296 (relative to OUTPUT_DIR)
297EOF
298 ;
Steven Rostedtdbd37832011-11-23 16:00:48 -0500299$config_help{"BUILD_OPTIONS"} = << "EOF"
300 Options to add to \"make\" when building.
301 i.e. -j20
302EOF
303 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500304$config_help{"TARGET_IMAGE"} = << "EOF"
305 The place to put your image on the test machine.
306EOF
307 ;
308$config_help{"POWER_CYCLE"} = << "EOF"
309 A script or command to reboot the box.
310
311 Here is a digital loggers power switch example
312 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
313
314 Here is an example to reboot a virtual box on the current host
315 with the name "Guest".
316 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
317EOF
318 ;
319$config_help{"CONSOLE"} = << "EOF"
320 The script or command that reads the console
321
322 If you use ttywatch server, something like the following would work.
323CONSOLE = nc -d localhost 3001
324
325 For a virtual machine with guest name "Guest".
326CONSOLE = virsh console Guest
327EOF
328 ;
329$config_help{"LOCALVERSION"} = << "EOF"
330 Required version ending to differentiate the test
331 from other linux builds on the system.
332EOF
333 ;
334$config_help{"REBOOT_TYPE"} = << "EOF"
335 Way to reboot the box to the test kernel.
336 Only valid options so far are "grub" and "script".
337
338 If you specify grub, it will assume grub version 1
339 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
340 and select that target to reboot to the kernel. If this is not
341 your setup, then specify "script" and have a command or script
342 specified in REBOOT_SCRIPT to boot to the target.
343
344 The entry in /boot/grub/menu.lst must be entered in manually.
345 The test will not modify that file.
346EOF
347 ;
348$config_help{"GRUB_MENU"} = << "EOF"
349 The grub title name for the test kernel to boot
350 (Only mandatory if REBOOT_TYPE = grub)
351
352 Note, ktest.pl will not update the grub menu.lst, you need to
353 manually add an option for the test. ktest.pl will search
354 the grub menu.lst for this option to find what kernel to
355 reboot into.
356
357 For example, if in the /boot/grub/menu.lst the test kernel title has:
358 title Test Kernel
359 kernel vmlinuz-test
360 GRUB_MENU = Test Kernel
361EOF
362 ;
363$config_help{"REBOOT_SCRIPT"} = << "EOF"
364 A script to reboot the target into the test kernel
365 (Only mandatory if REBOOT_TYPE = script)
366EOF
367 ;
368
Steven Rostedtdad98752011-11-22 20:48:57 -0500369sub read_prompt {
370 my ($cancel, $prompt) = @_;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400371
372 my $ans;
373
374 for (;;) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500375 if ($cancel) {
376 print "$prompt [y/n/C] ";
377 } else {
378 print "$prompt [Y/n] ";
379 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400380 $ans = <STDIN>;
381 chomp $ans;
382 if ($ans =~ /^\s*$/) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500383 if ($cancel) {
384 $ans = "c";
385 } else {
386 $ans = "y";
387 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400388 }
389 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
Steven Rostedtdad98752011-11-22 20:48:57 -0500390 if ($cancel) {
391 last if ($ans =~ /^c$/i);
392 print "Please answer either 'y', 'n' or 'c'.\n";
393 } else {
394 print "Please answer either 'y' or 'n'.\n";
395 }
396 }
397 if ($ans =~ /^c/i) {
398 exit;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400399 }
400 if ($ans !~ /^y$/i) {
401 return 0;
402 }
403 return 1;
404}
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500405
Steven Rostedtdad98752011-11-22 20:48:57 -0500406sub read_yn {
407 my ($prompt) = @_;
408
409 return read_prompt 0, $prompt;
410}
411
412sub read_ync {
413 my ($prompt) = @_;
414
415 return read_prompt 1, $prompt;
416}
417
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500418sub get_ktest_config {
419 my ($config) = @_;
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400420 my $ans;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500421
422 return if (defined($opt{$config}));
423
424 if (defined($config_help{$config})) {
425 print "\n";
426 print $config_help{$config};
427 }
428
429 for (;;) {
430 print "$config = ";
Steven Rostedtdbd37832011-11-23 16:00:48 -0500431 if (defined($default{$config}) && length($default{$config})) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500432 print "\[$default{$config}\] ";
433 }
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400434 $ans = <STDIN>;
435 $ans =~ s/^\s*(.*\S)\s*$/$1/;
436 if ($ans =~ /^\s*$/) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500437 if ($default{$config}) {
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400438 $ans = $default{$config};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500439 } else {
440 print "Your answer can not be blank\n";
441 next;
442 }
443 }
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500444 $entered_configs{$config} = ${ans};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500445 last;
446 }
447}
448
449sub get_ktest_configs {
450 get_ktest_config("MACHINE");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500451 get_ktest_config("BUILD_DIR");
452 get_ktest_config("OUTPUT_DIR");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500453
Steven Rostedtdbd37832011-11-23 16:00:48 -0500454 if ($newconfig) {
455 get_ktest_config("BUILD_OPTIONS");
456 }
457
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500458 # options required for other than just building a kernel
459 if (!$buildonly) {
Steven Rostedt165708b2011-11-26 20:56:52 -0500460 get_ktest_config("POWER_CYCLE");
461 get_ktest_config("CONSOLE");
462 }
463
464 # options required for install and more
465 if ($buildonly != 1) {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500466 get_ktest_config("SSH_USER");
467 get_ktest_config("BUILD_TARGET");
468 get_ktest_config("TARGET_IMAGE");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500469 }
470
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500471 get_ktest_config("LOCALVERSION");
472
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500473 return if ($buildonly);
474
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500475 my $rtype = $opt{"REBOOT_TYPE"};
476
477 if (!defined($rtype)) {
478 if (!defined($opt{"GRUB_MENU"})) {
479 get_ktest_config("REBOOT_TYPE");
480 $rtype = $entered_configs{"REBOOT_TYPE"};
481 } else {
482 $rtype = "grub";
483 }
484 }
485
486 if ($rtype eq "grub") {
487 get_ktest_config("GRUB_MENU");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500488 }
489}
490
Steven Rostedt77d942c2011-05-20 13:36:58 -0400491sub process_variables {
Steven Rostedt8d735212011-10-17 11:36:44 -0400492 my ($value, $remove_undef) = @_;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400493 my $retval = "";
494
495 # We want to check for '\', and it is just easier
496 # to check the previous characet of '$' and not need
497 # to worry if '$' is the first character. By adding
498 # a space to $value, we can just check [^\\]\$ and
499 # it will still work.
500 $value = " $value";
501
502 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
503 my $begin = $1;
504 my $var = $2;
505 my $end = $3;
506 # append beginning of value to retval
507 $retval = "$retval$begin";
508 if (defined($variable{$var})) {
509 $retval = "$retval$variable{$var}";
Steven Rostedt8d735212011-10-17 11:36:44 -0400510 } elsif (defined($remove_undef) && $remove_undef) {
511 # for if statements, any variable that is not defined,
512 # we simple convert to 0
513 $retval = "${retval}0";
Steven Rostedt77d942c2011-05-20 13:36:58 -0400514 } else {
515 # put back the origin piece.
516 $retval = "$retval\$\{$var\}";
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500517 # This could be an option that is used later, save
518 # it so we don't warn if this option is not one of
519 # ktests options.
520 $used_options{$var} = 1;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400521 }
522 $value = $end;
523 }
524 $retval = "$retval$value";
525
526 # remove the space added in the beginning
527 $retval =~ s/ //;
528
529 return "$retval"
530}
531
Steven Rostedta57419b2010-11-02 15:13:54 -0400532sub set_value {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400533 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
Steven Rostedta57419b2010-11-02 15:13:54 -0400534
Steven Rostedtcad96662011-12-22 11:32:52 -0500535 my $prvalue = process_variables($rvalue);
536
537 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500538 # Note if a test is something other than build, then we
539 # will need other manditory options.
Steven Rostedtcad96662011-12-22 11:32:52 -0500540 if ($prvalue ne "install") {
Steven Rostedt165708b2011-11-26 20:56:52 -0500541 $buildonly = 0;
542 } else {
543 # install still limits some manditory options.
544 $buildonly = 2;
545 }
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500546 }
547
Steven Rostedta57419b2010-11-02 15:13:54 -0400548 if (defined($opt{$lvalue})) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400549 if (!$override || defined(${$overrides}{$lvalue})) {
550 my $extra = "";
551 if ($override) {
552 $extra = "In the same override section!\n";
553 }
554 die "$name: $.: Option $lvalue defined more than once!\n$extra";
555 }
Steven Rostedtcad96662011-12-22 11:32:52 -0500556 ${$overrides}{$lvalue} = $prvalue;
Steven Rostedta57419b2010-11-02 15:13:54 -0400557 }
Steven Rostedt21a96792010-11-08 16:45:50 -0500558 if ($rvalue =~ /^\s*$/) {
559 delete $opt{$lvalue};
560 } else {
Steven Rostedtcad96662011-12-22 11:32:52 -0500561 $opt{$lvalue} = $prvalue;
Steven Rostedt21a96792010-11-08 16:45:50 -0500562 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400563}
564
Steven Rostedt77d942c2011-05-20 13:36:58 -0400565sub set_variable {
566 my ($lvalue, $rvalue) = @_;
567
568 if ($rvalue =~ /^\s*$/) {
569 delete $variable{$lvalue};
570 } else {
571 $rvalue = process_variables($rvalue);
572 $variable{$lvalue} = $rvalue;
573 }
574}
575
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400576sub process_compare {
577 my ($lval, $cmp, $rval) = @_;
578
579 # remove whitespace
580
581 $lval =~ s/^\s*//;
582 $lval =~ s/\s*$//;
583
584 $rval =~ s/^\s*//;
585 $rval =~ s/\s*$//;
586
587 if ($cmp eq "==") {
588 return $lval eq $rval;
589 } elsif ($cmp eq "!=") {
590 return $lval ne $rval;
591 }
592
593 my $statement = "$lval $cmp $rval";
594 my $ret = eval $statement;
595
596 # $@ stores error of eval
597 if ($@) {
598 return -1;
599 }
600
601 return $ret;
602}
603
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400604sub value_defined {
605 my ($val) = @_;
606
607 return defined($variable{$2}) ||
608 defined($opt{$2});
609}
610
Steven Rostedt8d735212011-10-17 11:36:44 -0400611my $d = 0;
612sub process_expression {
613 my ($name, $val) = @_;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400614
Steven Rostedt8d735212011-10-17 11:36:44 -0400615 my $c = $d++;
616
617 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
618 my $express = $1;
619
620 if (process_expression($name, $express)) {
621 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
622 } else {
623 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
624 }
625 }
626
627 $d--;
628 my $OR = "\\|\\|";
629 my $AND = "\\&\\&";
630
631 while ($val =~ s/^(.*?)($OR|$AND)//) {
632 my $express = $1;
633 my $op = $2;
634
635 if (process_expression($name, $express)) {
636 if ($op eq "||") {
637 return 1;
638 }
639 } else {
640 if ($op eq "&&") {
641 return 0;
642 }
643 }
644 }
Steven Rostedt45d73a52011-09-30 19:44:53 -0400645
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400646 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
647 my $ret = process_compare($1, $2, $3);
648 if ($ret < 0) {
649 die "$name: $.: Unable to process comparison\n";
650 }
651 return $ret;
652 }
653
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400654 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
655 if (defined $1) {
656 return !value_defined($2);
657 } else {
658 return value_defined($2);
659 }
660 }
661
Steven Rostedt45d73a52011-09-30 19:44:53 -0400662 if ($val =~ /^\s*0\s*$/) {
663 return 0;
664 } elsif ($val =~ /^\s*\d+\s*$/) {
665 return 1;
666 }
667
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400668 die ("$name: $.: Undefined content $val in if statement\n");
Steven Rostedt8d735212011-10-17 11:36:44 -0400669}
670
671sub process_if {
672 my ($name, $value) = @_;
673
674 # Convert variables and replace undefined ones with 0
675 my $val = process_variables($value, 1);
676 my $ret = process_expression $name, $val;
677
678 return $ret;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400679}
680
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400681sub __read_config {
682 my ($config, $current_test_num) = @_;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400683
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400684 my $in;
685 open($in, $config) || die "can't read file $config";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400686
Steven Rostedta57419b2010-11-02 15:13:54 -0400687 my $name = $config;
688 $name =~ s,.*/(.*),$1,;
689
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400690 my $test_num = $$current_test_num;
Steven Rostedta57419b2010-11-02 15:13:54 -0400691 my $default = 1;
692 my $repeat = 1;
693 my $num_tests_set = 0;
694 my $skip = 0;
695 my $rest;
Steven Rostedta9f84422011-10-17 11:06:29 -0400696 my $line;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400697 my $test_case = 0;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400698 my $if = 0;
699 my $if_set = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400700 my $override = 0;
701
702 my %overrides;
Steven Rostedta57419b2010-11-02 15:13:54 -0400703
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400704 while (<$in>) {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400705
706 # ignore blank lines and comments
707 next if (/^\s*$/ || /\s*\#/);
708
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400709 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400710
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400711 my $type = $1;
712 $rest = $2;
Steven Rostedta9f84422011-10-17 11:06:29 -0400713 $line = $2;
Steven Rostedta57419b2010-11-02 15:13:54 -0400714
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400715 my $old_test_num;
716 my $old_repeat;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400717 $override = 0;
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400718
719 if ($type eq "TEST_START") {
720
721 if ($num_tests_set) {
722 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
723 }
724
725 $old_test_num = $test_num;
726 $old_repeat = $repeat;
727
728 $test_num += $repeat;
729 $default = 0;
730 $repeat = 1;
731 } else {
732 $default = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400733 }
734
Steven Rostedta9f84422011-10-17 11:06:29 -0400735 # If SKIP is anywhere in the line, the command will be skipped
736 if ($rest =~ s/\s+SKIP\b//) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400737 $skip = 1;
738 } else {
Steven Rostedt0df213c2011-06-14 20:51:37 -0400739 $test_case = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400740 $skip = 0;
741 }
742
Steven Rostedta9f84422011-10-17 11:06:29 -0400743 if ($rest =~ s/\sELSE\b//) {
744 if (!$if) {
745 die "$name: $.: ELSE found with out matching IF section\n$_";
746 }
747 $if = 0;
748
749 if ($if_set) {
750 $skip = 1;
751 } else {
752 $skip = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400753 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400754 }
755
Steven Rostedta9f84422011-10-17 11:06:29 -0400756 if ($rest =~ s/\sIF\s+(.*)//) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400757 if (process_if($name, $1)) {
758 $if_set = 1;
759 } else {
760 $skip = 1;
761 }
762 $if = 1;
763 } else {
764 $if = 0;
Steven Rostedta9f84422011-10-17 11:06:29 -0400765 $if_set = 0;
Steven Rostedta57419b2010-11-02 15:13:54 -0400766 }
767
Steven Rostedta9f84422011-10-17 11:06:29 -0400768 if (!$skip) {
769 if ($type eq "TEST_START") {
770 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
771 $repeat = $1;
772 $repeat_tests{"$test_num"} = $repeat;
773 }
774 } elsif ($rest =~ s/\sOVERRIDE\b//) {
775 # DEFAULT only
776 $override = 1;
777 # Clear previous overrides
778 %overrides = ();
779 }
780 }
781
782 if (!$skip && $rest !~ /^\s*$/) {
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400783 die "$name: $.: Gargbage found after $type\n$_";
Steven Rostedta57419b2010-11-02 15:13:54 -0400784 }
785
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400786 if ($skip && $type eq "TEST_START") {
Steven Rostedta57419b2010-11-02 15:13:54 -0400787 $test_num = $old_test_num;
Steven Rostedte48c5292010-11-02 14:35:37 -0400788 $repeat = $old_repeat;
Steven Rostedta57419b2010-11-02 15:13:54 -0400789 }
790
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400791 } elsif (/^\s*ELSE\b(.*)$/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400792 if (!$if) {
793 die "$name: $.: ELSE found with out matching IF section\n$_";
794 }
795 $rest = $1;
796 if ($if_set) {
797 $skip = 1;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400798 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400799 } else {
800 $skip = 0;
801
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400802 if ($rest =~ /\sIF\s+(.*)/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400803 # May be a ELSE IF section.
804 if (!process_if($name, $1)) {
805 $skip = 1;
806 }
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400807 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400808 } else {
809 $if = 0;
810 }
811 }
812
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400813 if ($rest !~ /^\s*$/) {
814 die "$name: $.: Gargbage found after DEFAULTS\n$_";
815 }
816
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400817 } elsif (/^\s*INCLUDE\s+(\S+)/) {
818
819 next if ($skip);
820
821 if (!$default) {
822 die "$name: $.: INCLUDE can only be done in default sections\n$_";
823 }
824
825 my $file = process_variables($1);
826
827 if ($file !~ m,^/,) {
828 # check the path of the config file first
829 if ($config =~ m,(.*)/,) {
830 if (-f "$1/$file") {
831 $file = "$1/$file";
832 }
833 }
834 }
835
836 if ( ! -r $file ) {
837 die "$name: $.: Can't read file $file\n$_";
838 }
839
840 if (__read_config($file, \$test_num)) {
841 $test_case = 1;
842 }
843
Steven Rostedta57419b2010-11-02 15:13:54 -0400844 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
845
846 next if ($skip);
847
Steven Rostedt2545eb62010-11-02 15:01:32 -0400848 my $lvalue = $1;
849 my $rvalue = $2;
850
Steven Rostedta57419b2010-11-02 15:13:54 -0400851 if (!$default &&
852 ($lvalue eq "NUM_TESTS" ||
853 $lvalue eq "LOG_FILE" ||
854 $lvalue eq "CLEAR_LOG")) {
855 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400856 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400857
858 if ($lvalue eq "NUM_TESTS") {
859 if ($test_num) {
860 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
861 }
862 if (!$default) {
863 die "$name: $.: NUM_TESTS must be set in default section\n";
864 }
865 $num_tests_set = 1;
866 }
867
868 if ($default || $lvalue =~ /\[\d+\]$/) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400869 set_value($lvalue, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400870 } else {
871 my $val = "$lvalue\[$test_num\]";
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400872 set_value($val, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400873
874 if ($repeat > 1) {
875 $repeats{$val} = $repeat;
876 }
877 }
Steven Rostedt77d942c2011-05-20 13:36:58 -0400878 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
879 next if ($skip);
880
881 my $lvalue = $1;
882 my $rvalue = $2;
883
884 # process config variables.
885 # Config variables are only active while reading the
886 # config and can be defined anywhere. They also ignore
887 # TEST_START and DEFAULTS, but are skipped if they are in
888 # on of these sections that have SKIP defined.
889 # The save variable can be
890 # defined multiple times and the new one simply overrides
891 # the prevous one.
892 set_variable($lvalue, $rvalue);
893
Steven Rostedta57419b2010-11-02 15:13:54 -0400894 } else {
895 die "$name: $.: Garbage found in config\n$_";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400896 }
897 }
898
Steven Rostedta57419b2010-11-02 15:13:54 -0400899 if ($test_num) {
900 $test_num += $repeat - 1;
901 $opt{"NUM_TESTS"} = $test_num;
902 }
903
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400904 close($in);
905
906 $$current_test_num = $test_num;
907
908 return $test_case;
909}
910
Steven Rostedtc4261d02011-11-23 13:41:18 -0500911sub get_test_case {
912 print "What test case would you like to run?\n";
913 print " (build, install or boot)\n";
914 print " Other tests are available but require editing the config file\n";
915 my $ans = <STDIN>;
916 chomp $ans;
917 $default{"TEST_TYPE"} = $ans;
918}
919
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400920sub read_config {
921 my ($config) = @_;
922
923 my $test_case;
924 my $test_num = 0;
925
926 $test_case = __read_config $config, \$test_num;
927
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500928 # make sure we have all mandatory configs
929 get_ktest_configs;
930
Steven Rostedt0df213c2011-06-14 20:51:37 -0400931 # was a test specified?
932 if (!$test_case) {
933 print "No test case specified.\n";
Steven Rostedtc4261d02011-11-23 13:41:18 -0500934 get_test_case;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400935 }
936
Steven Rostedta75fece2010-11-02 14:58:27 -0400937 # set any defaults
938
939 foreach my $default (keys %default) {
940 if (!defined($opt{$default})) {
941 $opt{$default} = $default{$default};
942 }
943 }
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500944
945 if ($opt{"IGNORE_UNUSED"} == 1) {
946 return;
947 }
948
949 my %not_used;
950
951 # check if there are any stragglers (typos?)
952 foreach my $option (keys %opt) {
953 my $op = $option;
954 # remove per test labels.
955 $op =~ s/\[.*\]//;
956 if (!exists($option_map{$op}) &&
957 !exists($default{$op}) &&
958 !exists($used_options{$op})) {
959 $not_used{$op} = 1;
960 }
961 }
962
963 if (%not_used) {
964 my $s = "s are";
965 $s = " is" if (keys %not_used == 1);
966 print "The following option$s not used; could be a typo:\n";
967 foreach my $option (keys %not_used) {
968 print "$option\n";
969 }
970 print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
971 if (!read_yn "Do you want to continue?") {
972 exit -1;
973 }
974 }
Steven Rostedt2545eb62010-11-02 15:01:32 -0400975}
976
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400977sub __eval_option {
978 my ($option, $i) = @_;
979
980 # Add space to evaluate the character before $
981 $option = " $option";
982 my $retval = "";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530983 my $repeated = 0;
984 my $parent = 0;
985
986 foreach my $test (keys %repeat_tests) {
987 if ($i >= $test &&
988 $i < $test + $repeat_tests{$test}) {
989
990 $repeated = 1;
991 $parent = $test;
992 last;
993 }
994 }
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400995
996 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
997 my $start = $1;
998 my $var = $2;
999 my $end = $3;
1000
1001 # Append beginning of line
1002 $retval = "$retval$start";
1003
1004 # If the iteration option OPT[$i] exists, then use that.
1005 # otherwise see if the default OPT (without [$i]) exists.
1006
1007 my $o = "$var\[$i\]";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301008 my $parento = "$var\[$parent\]";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001009
1010 if (defined($opt{$o})) {
1011 $o = $opt{$o};
1012 $retval = "$retval$o";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301013 } elsif ($repeated && defined($opt{$parento})) {
1014 $o = $opt{$parento};
1015 $retval = "$retval$o";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001016 } elsif (defined($opt{$var})) {
1017 $o = $opt{$var};
1018 $retval = "$retval$o";
1019 } else {
1020 $retval = "$retval\$\{$var\}";
1021 }
1022
1023 $option = $end;
1024 }
1025
1026 $retval = "$retval$option";
1027
1028 $retval =~ s/^ //;
1029
1030 return $retval;
1031}
1032
1033sub eval_option {
1034 my ($option, $i) = @_;
1035
1036 my $prev = "";
1037
1038 # Since an option can evaluate to another option,
1039 # keep iterating until we do not evaluate any more
1040 # options.
1041 my $r = 0;
1042 while ($prev ne $option) {
1043 # Check for recursive evaluations.
1044 # 100 deep should be more than enough.
1045 if ($r++ > 100) {
1046 die "Over 100 evaluations accurred with $option\n" .
1047 "Check for recursive variables\n";
1048 }
1049 $prev = $option;
1050 $option = __eval_option($option, $i);
1051 }
1052
1053 return $option;
1054}
1055
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001056sub _logit {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001057 if (defined($opt{"LOG_FILE"})) {
1058 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
1059 print OUT @_;
1060 close(OUT);
1061 }
1062}
1063
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001064sub logit {
1065 if (defined($opt{"LOG_FILE"})) {
1066 _logit @_;
1067 } else {
1068 print @_;
1069 }
1070}
1071
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001072sub doprint {
1073 print @_;
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001074 _logit @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001075}
1076
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001077sub run_command;
Andrew Jones2728be42011-08-12 15:32:05 +02001078sub start_monitor;
1079sub end_monitor;
1080sub wait_for_monitor;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001081
1082sub reboot {
Andrew Jones2728be42011-08-12 15:32:05 +02001083 my ($time) = @_;
1084
Steven Rostedt2b803362011-09-30 18:00:23 -04001085 if (defined($time)) {
1086 start_monitor;
1087 # flush out current monitor
1088 # May contain the reboot success line
1089 wait_for_monitor 1;
1090 }
1091
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001092 # try to reboot normally
Steven Rostedte48c5292010-11-02 14:35:37 -04001093 if (run_command $reboot) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001094 if (defined($powercycle_after_reboot)) {
1095 sleep $powercycle_after_reboot;
1096 run_command "$power_cycle";
1097 }
1098 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001099 # nope? power cycle it.
Steven Rostedta75fece2010-11-02 14:58:27 -04001100 run_command "$power_cycle";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001101 }
Andrew Jones2728be42011-08-12 15:32:05 +02001102
1103 if (defined($time)) {
Steven Rostedt2b803362011-09-30 18:00:23 -04001104 wait_for_monitor($time, $reboot_success_line);
Andrew Jones2728be42011-08-12 15:32:05 +02001105 end_monitor;
1106 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001107}
1108
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001109sub reboot_to_good {
1110 my ($time) = @_;
1111
1112 if (defined($switch_to_good)) {
1113 run_command $switch_to_good;
1114 return;
1115 }
1116
1117 reboot $time;
1118}
1119
Steven Rostedt576f6272010-11-02 14:58:38 -04001120sub do_not_reboot {
1121 my $i = $iteration;
1122
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001123 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -04001124 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1125 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1126}
1127
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001128sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001129 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001130
Steven Rostedt576f6272010-11-02 14:58:38 -04001131 my $i = $iteration;
1132
1133 if ($reboot_on_error && !do_not_reboot) {
1134
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001135 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001136 reboot_to_good;
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001137
Steven Rostedta75fece2010-11-02 14:58:27 -04001138 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001139 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001140 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001141 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001142
Steven Rostedtf80802c2011-03-07 13:18:47 -05001143 if (defined($opt{"LOG_FILE"})) {
1144 print " See $opt{LOG_FILE} for more info.\n";
1145 }
1146
Steven Rostedt576f6272010-11-02 14:58:38 -04001147 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001148}
1149
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001150sub open_console {
1151 my ($fp) = @_;
1152
1153 my $flags;
1154
Steven Rostedta75fece2010-11-02 14:58:27 -04001155 my $pid = open($fp, "$console|") or
1156 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001157
1158 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001159 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001160 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001161 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001162
1163 return $pid;
1164}
1165
1166sub close_console {
1167 my ($fp, $pid) = @_;
1168
1169 doprint "kill child process $pid\n";
1170 kill 2, $pid;
1171
1172 print "closing!\n";
1173 close($fp);
1174}
1175
1176sub start_monitor {
1177 if ($monitor_cnt++) {
1178 return;
1179 }
1180 $monitor_fp = \*MONFD;
1181 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001182
1183 return;
1184
1185 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001186}
1187
1188sub end_monitor {
1189 if (--$monitor_cnt) {
1190 return;
1191 }
1192 close_console($monitor_fp, $monitor_pid);
1193}
1194
1195sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001196 my ($time, $stop) = @_;
1197 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001198 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001199 my $booted = 0;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001200
Steven Rostedta75fece2010-11-02 14:58:27 -04001201 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001202
1203 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001204 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001205 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001206 last if (!defined($line));
1207 print "$line";
1208 $full_line .= $line;
1209
1210 if (defined($stop) && $full_line =~ /$stop/) {
1211 doprint "wait for monitor detected $stop\n";
1212 $booted = 1;
1213 }
1214
1215 if ($line =~ /\n/) {
1216 $full_line = "";
1217 }
1218 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001219 print "** Monitor flushed **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001220}
1221
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301222sub save_logs {
1223 my ($result, $basedir) = @_;
1224 my @t = localtime;
1225 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1226 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1227
1228 my $type = $build_type;
1229 if ($type =~ /useconfig/) {
1230 $type = "useconfig";
1231 }
1232
1233 my $dir = "$machine-$test_type-$type-$result-$date";
1234
1235 $dir = "$basedir/$dir";
1236
1237 if (!-d $dir) {
1238 mkpath($dir) or
1239 die "can't create $dir";
1240 }
1241
1242 my %files = (
1243 "config" => $output_config,
1244 "buildlog" => $buildlog,
1245 "dmesg" => $dmesg,
1246 "testlog" => $testlog,
1247 );
1248
1249 while (my ($name, $source) = each(%files)) {
1250 if (-f "$source") {
1251 cp "$source", "$dir/$name" or
1252 die "failed to copy $source";
1253 }
1254 }
1255
1256 doprint "*** Saved info to $dir ***\n";
1257}
1258
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001259sub fail {
1260
Steven Rostedta75fece2010-11-02 14:58:27 -04001261 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001262 dodie @_;
1263 }
1264
Steven Rostedta75fece2010-11-02 14:58:27 -04001265 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001266
Steven Rostedt576f6272010-11-02 14:58:38 -04001267 my $i = $iteration;
1268
Steven Rostedta75fece2010-11-02 14:58:27 -04001269 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001270 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001271 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001272 reboot_to_good $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001273 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001274
Steven Rostedt9064af52011-06-13 10:38:48 -04001275 my $name = "";
1276
1277 if (defined($test_name)) {
1278 $name = " ($test_name)";
1279 }
1280
Steven Rostedt576f6272010-11-02 14:58:38 -04001281 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1282 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001283 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001284 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1285 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001286
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301287 if (defined($store_failures)) {
1288 save_logs "fail", $store_failures;
1289 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001290
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001291 return 1;
1292}
1293
Steven Rostedt2545eb62010-11-02 15:01:32 -04001294sub run_command {
1295 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001296 my $dolog = 0;
1297 my $dord = 0;
1298 my $pid;
1299
Steven Rostedte48c5292010-11-02 14:35:37 -04001300 $command =~ s/\$SSH_USER/$ssh_user/g;
1301 $command =~ s/\$MACHINE/$machine/g;
1302
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001303 doprint("$command ... ");
1304
1305 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001306 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001307
1308 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001309 open(LOG, ">>$opt{LOG_FILE}") or
1310 dodie "failed to write to log";
1311 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001312 }
1313
1314 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001315 open (RD, ">$redirect") or
1316 dodie "failed to write to redirect $redirect";
1317 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001318 }
1319
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001320 while (<CMD>) {
1321 print LOG if ($dolog);
1322 print RD if ($dord);
1323 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001324
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001325 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001326 my $failed = $?;
1327
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001328 close(CMD);
1329 close(LOG) if ($dolog);
1330 close(RD) if ($dord);
1331
Steven Rostedt2545eb62010-11-02 15:01:32 -04001332 if ($failed) {
1333 doprint "FAILED!\n";
1334 } else {
1335 doprint "SUCCESS\n";
1336 }
1337
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001338 return !$failed;
1339}
1340
Steven Rostedte48c5292010-11-02 14:35:37 -04001341sub run_ssh {
1342 my ($cmd) = @_;
1343 my $cp_exec = $ssh_exec;
1344
1345 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1346 return run_command "$cp_exec";
1347}
1348
1349sub run_scp {
1350 my ($src, $dst) = @_;
1351 my $cp_scp = $scp_to_target;
1352
1353 $cp_scp =~ s/\$SRC_FILE/$src/g;
1354 $cp_scp =~ s/\$DST_FILE/$dst/g;
1355
1356 return run_command "$cp_scp";
1357}
1358
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001359sub get_grub_index {
1360
Steven Rostedta75fece2010-11-02 14:58:27 -04001361 if ($reboot_type ne "grub") {
1362 return;
1363 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001364 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001365
1366 doprint "Find grub menu ... ";
1367 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001368
1369 my $ssh_grub = $ssh_exec;
1370 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1371
1372 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001373 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001374
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001375 my $found = 0;
1376
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001377 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001378 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001379 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001380 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001381 last;
1382 } elsif (/^\s*title\s/) {
1383 $grub_number++;
1384 }
1385 }
1386 close(IN);
1387
Steven Rostedta75fece2010-11-02 14:58:27 -04001388 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001389 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001390 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001391}
1392
Steven Rostedt2545eb62010-11-02 15:01:32 -04001393sub wait_for_input
1394{
1395 my ($fp, $time) = @_;
1396 my $rin;
1397 my $ready;
1398 my $line;
1399 my $ch;
1400
1401 if (!defined($time)) {
1402 $time = $timeout;
1403 }
1404
1405 $rin = '';
1406 vec($rin, fileno($fp), 1) = 1;
1407 $ready = select($rin, undef, undef, $time);
1408
1409 $line = "";
1410
1411 # try to read one char at a time
1412 while (sysread $fp, $ch, 1) {
1413 $line .= $ch;
1414 last if ($ch eq "\n");
1415 }
1416
1417 if (!length($line)) {
1418 return undef;
1419 }
1420
1421 return $line;
1422}
1423
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001424sub reboot_to {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001425 if (defined($switch_to_test)) {
1426 run_command $switch_to_test;
1427 }
1428
Steven Rostedta75fece2010-11-02 14:58:27 -04001429 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001430 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001431 } elsif (defined $reboot_script) {
1432 run_command "$reboot_script";
Steven Rostedta75fece2010-11-02 14:58:27 -04001433 }
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001434 reboot;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001435}
1436
Steven Rostedta57419b2010-11-02 15:13:54 -04001437sub get_sha1 {
1438 my ($commit) = @_;
1439
1440 doprint "git rev-list --max-count=1 $commit ... ";
1441 my $sha1 = `git rev-list --max-count=1 $commit`;
1442 my $ret = $?;
1443
1444 logit $sha1;
1445
1446 if ($ret) {
1447 doprint "FAILED\n";
1448 dodie "Failed to get git $commit";
1449 }
1450
1451 print "SUCCESS\n";
1452
1453 chomp $sha1;
1454
1455 return $sha1;
1456}
1457
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001458sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001459 my $booted = 0;
1460 my $bug = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001461 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001462 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001463
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001464 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001465
1466 my $line;
1467 my $full_line = "";
1468
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001469 open(DMESG, "> $dmesg") or
1470 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001471
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001472 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001473
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001474 my $success_start;
1475 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001476 my $monitor_start = time;
1477 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001478 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001479
Steven Rostedt2d01b262011-03-08 09:47:54 -05001480 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001481
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001482 if ($bug && defined($stop_after_failure) &&
1483 $stop_after_failure >= 0) {
1484 my $time = $stop_after_failure - (time - $failure_start);
1485 $line = wait_for_input($monitor_fp, $time);
1486 if (!defined($line)) {
1487 doprint "bug timed out after $booted_timeout seconds\n";
1488 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1489 last;
1490 }
1491 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001492 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001493 if (!defined($line)) {
1494 my $s = $booted_timeout == 1 ? "" : "s";
1495 doprint "Successful boot found: break after $booted_timeout second$s\n";
1496 last;
1497 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001498 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001499 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001500 if (!defined($line)) {
1501 my $s = $timeout == 1 ? "" : "s";
1502 doprint "Timed out after $timeout second$s\n";
1503 last;
1504 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001505 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001506
Steven Rostedt2545eb62010-11-02 15:01:32 -04001507 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001508 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001509
1510 # we are not guaranteed to get a full line
1511 $full_line .= $line;
1512
Steven Rostedta75fece2010-11-02 14:58:27 -04001513 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001514 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001515 $success_start = time;
1516 }
1517
1518 if ($booted && defined($stop_after_success) &&
1519 $stop_after_success >= 0) {
1520 my $now = time;
1521 if ($now - $success_start >= $stop_after_success) {
1522 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1523 last;
1524 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001525 }
1526
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001527 if ($full_line =~ /\[ backtrace testing \]/) {
1528 $skip_call_trace = 1;
1529 }
1530
Steven Rostedt2545eb62010-11-02 15:01:32 -04001531 if ($full_line =~ /call trace:/i) {
Steven Rostedt46519202011-03-08 09:40:31 -05001532 if (!$bug && !$skip_call_trace) {
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001533 $bug = 1;
1534 $failure_start = time;
1535 }
1536 }
1537
1538 if ($bug && defined($stop_after_failure) &&
1539 $stop_after_failure >= 0) {
1540 my $now = time;
1541 if ($now - $failure_start >= $stop_after_failure) {
1542 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1543 last;
1544 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001545 }
1546
1547 if ($full_line =~ /\[ end of backtrace testing \]/) {
1548 $skip_call_trace = 0;
1549 }
1550
1551 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001552 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001553 $bug = 1;
1554 }
1555
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001556 # Detect triple faults by testing the banner
1557 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1558 if ($1 eq $version) {
1559 $version_found = 1;
1560 } elsif ($version_found && $detect_triplefault) {
1561 # We already booted into the kernel we are testing,
1562 # but now we booted into another kernel?
1563 # Consider this a triple fault.
1564 doprint "Aleady booted in Linux kernel $version, but now\n";
1565 doprint "we booted into Linux kernel $1.\n";
1566 doprint "Assuming that this is a triple fault.\n";
1567 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1568 last;
1569 }
1570 }
1571
Steven Rostedt2545eb62010-11-02 15:01:32 -04001572 if ($line =~ /\n/) {
1573 $full_line = "";
1574 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001575
1576 if ($stop_test_after > 0 && !$booted && !$bug) {
1577 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001578 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001579 $done = 1;
1580 }
1581 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001582 }
1583
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001584 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001585
Steven Rostedt2545eb62010-11-02 15:01:32 -04001586 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001587 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001588 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001589 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001590
Steven Rostedta75fece2010-11-02 14:58:27 -04001591 if (!$booted) {
1592 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001593 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001594 }
1595
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001596 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001597}
1598
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001599sub eval_kernel_version {
1600 my ($option) = @_;
1601
1602 $option =~ s/\$KERNEL_VERSION/$version/g;
1603
1604 return $option;
1605}
1606
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001607sub do_post_install {
1608
1609 return if (!defined($post_install));
1610
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001611 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001612 run_command "$cp_post_install" or
1613 dodie "Failed to run post install";
1614}
1615
Steven Rostedt2545eb62010-11-02 15:01:32 -04001616sub install {
1617
Steven Rostedte0a87422011-09-30 17:50:48 -04001618 return if ($no_install);
1619
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001620 my $cp_target = eval_kernel_version $target_image;
1621
1622 run_scp "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001623 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001624
1625 my $install_mods = 0;
1626
1627 # should we process modules?
1628 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001629 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001630 while (<IN>) {
1631 if (/CONFIG_MODULES(=y)?/) {
1632 $install_mods = 1 if (defined($1));
1633 last;
1634 }
1635 }
1636 close(IN);
1637
1638 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001639 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001640 doprint "No modules needed\n";
1641 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001642 }
1643
Steven Rostedta75fece2010-11-02 14:58:27 -04001644 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001645 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001646
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001647 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001648 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001649
Steven Rostedte48c5292010-11-02 14:35:37 -04001650 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001651 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001652
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001653 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001654 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001655 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001656
Steven Rostedte48c5292010-11-02 14:35:37 -04001657 run_scp "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001658 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001659
Steven Rostedta75fece2010-11-02 14:58:27 -04001660 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001661
Steven Rostedte7b13442011-06-14 20:44:36 -04001662 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001663 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001664
Steven Rostedte48c5292010-11-02 14:35:37 -04001665 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001666
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001667 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001668}
1669
Steven Rostedtddf607e2011-06-14 20:49:13 -04001670sub get_version {
1671 # get the release name
1672 doprint "$make kernelrelease ... ";
1673 $version = `$make kernelrelease | tail -1`;
1674 chomp($version);
1675 doprint "$version\n";
1676}
1677
1678sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001679 # Make sure the stable kernel has finished booting
1680 start_monitor;
1681 wait_for_monitor 5;
1682 end_monitor;
1683
Steven Rostedtddf607e2011-06-14 20:49:13 -04001684 get_grub_index;
1685 get_version;
1686 install;
1687
1688 start_monitor;
1689 return monitor;
1690}
1691
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001692sub check_buildlog {
1693 my ($patch) = @_;
1694
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001695 my @files = `git show $patch | diffstat -l`;
1696
1697 open(IN, "git show $patch |") or
1698 dodie "failed to show $patch";
1699 while (<IN>) {
1700 if (m,^--- a/(.*),) {
1701 chomp $1;
1702 $files[$#files] = $1;
1703 }
1704 }
1705 close(IN);
1706
1707 open(IN, $buildlog) or dodie "Can't open $buildlog";
1708 while (<IN>) {
1709 if (/^\s*(.*?):.*(warning|error)/) {
1710 my $err = $1;
1711 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001712 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001713 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001714 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001715 }
1716 }
1717 }
1718 }
1719 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001720
1721 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001722}
1723
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001724sub apply_min_config {
1725 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05001726
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001727 # Read the config file and remove anything that
1728 # is in the force_config hash (from minconfig and others)
1729 # then add the force config back.
1730
1731 doprint "Applying minimum configurations into $output_config.new\n";
1732
1733 open (OUT, ">$outconfig") or
1734 dodie "Can't create $outconfig";
1735
1736 if (-f $output_config) {
1737 open (IN, $output_config) or
1738 dodie "Failed to open $output_config";
1739 while (<IN>) {
1740 if (/^(# )?(CONFIG_[^\s=]*)/) {
1741 next if (defined($force_config{$2}));
1742 }
1743 print OUT;
1744 }
1745 close IN;
1746 }
1747 foreach my $config (keys %force_config) {
1748 print OUT "$force_config{$config}\n";
1749 }
1750 close OUT;
1751
1752 run_command "mv $outconfig $output_config";
1753}
1754
1755sub make_oldconfig {
1756
Steven Rostedt4c4ab122011-07-15 21:16:17 -04001757 my @force_list = keys %force_config;
1758
1759 if ($#force_list >= 0) {
1760 apply_min_config;
1761 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001762
1763 if (!run_command "$make oldnoconfig") {
Steven Rostedt612b9e92011-03-07 13:27:43 -05001764 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1765 # try a yes '' | oldconfig
1766 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001767 run_command "yes '' | $make oldconfig" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001768 dodie "failed make config oldconfig";
1769 }
1770}
1771
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001772# read a config file and use this to force new configs.
1773sub load_force_config {
1774 my ($config) = @_;
1775
1776 open(IN, $config) or
1777 dodie "failed to read $config";
1778 while (<IN>) {
1779 chomp;
1780 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1781 $force_config{$1} = $_;
1782 } elsif (/^# (CONFIG_\S*) is not set/) {
1783 $force_config{$1} = $_;
1784 }
1785 }
1786 close IN;
1787}
1788
Steven Rostedt2545eb62010-11-02 15:01:32 -04001789sub build {
1790 my ($type) = @_;
1791
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001792 unlink $buildlog;
1793
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001794 # Failed builds should not reboot the target
1795 my $save_no_reboot = $no_reboot;
1796 $no_reboot = 1;
1797
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001798 if (defined($pre_build)) {
1799 my $ret = run_command $pre_build;
1800 if (!$ret && defined($pre_build_die) &&
1801 $pre_build_die) {
1802 dodie "failed to pre_build\n";
1803 }
1804 }
1805
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001806 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001807 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001808 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001809
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001810 $type = "oldconfig";
1811 }
1812
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001813 # old config can ask questions
1814 if ($type eq "oldconfig") {
Steven Rostedt9386c6a2010-11-08 16:35:48 -05001815 $type = "oldnoconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001816
1817 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001818 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001819
Andrew Jones13488232011-08-12 15:32:04 +02001820 if (!$noclean) {
1821 run_command "mv $output_config $outputdir/config_temp" or
1822 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001823
Andrew Jones13488232011-08-12 15:32:04 +02001824 run_command "$make mrproper" or dodie "make mrproper";
1825
1826 run_command "mv $outputdir/config_temp $output_config" or
1827 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001828 }
1829
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001830 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001831 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001832 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001833 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001834 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001835
1836 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04001837 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1838 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001839 close(OUT);
1840
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001841 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001842 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001843 }
1844
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001845 if ($type ne "oldnoconfig") {
1846 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001847 dodie "failed make config";
1848 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001849 # Run old config regardless, to enforce min configurations
1850 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001851
Steven Rostedta75fece2010-11-02 14:58:27 -04001852 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001853 my $build_ret = run_command "$make $build_options";
1854 undef $redirect;
1855
1856 if (defined($post_build)) {
1857 my $ret = run_command $post_build;
1858 if (!$ret && defined($post_build_die) &&
1859 $post_build_die) {
1860 dodie "failed to post_build\n";
1861 }
1862 }
1863
1864 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001865 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001866 if ($in_bisect) {
1867 $no_reboot = $save_no_reboot;
1868 return 0;
1869 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001870 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001871 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001872
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001873 $no_reboot = $save_no_reboot;
1874
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001875 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001876}
1877
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001878sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04001879 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001880 if (defined($poweroff_after_halt)) {
1881 sleep $poweroff_after_halt;
1882 run_command "$power_off";
1883 }
1884 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001885 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04001886 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001887 }
1888}
1889
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001890sub success {
1891 my ($i) = @_;
1892
Steven Rostedte48c5292010-11-02 14:35:37 -04001893 $successes++;
1894
Steven Rostedt9064af52011-06-13 10:38:48 -04001895 my $name = "";
1896
1897 if (defined($test_name)) {
1898 $name = " ($test_name)";
1899 }
1900
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001901 doprint "\n\n*******************************************\n";
1902 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001903 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001904 doprint "*******************************************\n";
1905 doprint "*******************************************\n";
1906
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301907 if (defined($store_successes)) {
1908 save_logs "success", $store_successes;
1909 }
1910
Steven Rostedt576f6272010-11-02 14:58:38 -04001911 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001912 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001913 reboot_to_good $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001914 }
1915}
1916
Steven Rostedtc960bb92011-03-08 09:22:39 -05001917sub answer_bisect {
1918 for (;;) {
1919 doprint "Pass or fail? [p/f]";
1920 my $ans = <STDIN>;
1921 chomp $ans;
1922 if ($ans eq "p" || $ans eq "P") {
1923 return 1;
1924 } elsif ($ans eq "f" || $ans eq "F") {
1925 return 0;
1926 } else {
1927 print "Please answer 'P' or 'F'\n";
1928 }
1929 }
1930}
1931
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001932sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001933 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001934
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001935 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04001936 $reboot_on_error = 0;
1937 $poweroff_on_error = 0;
1938 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001939
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301940 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001941 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301942 undef $redirect;
1943
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001944 exit $failed;
1945}
1946
1947my $child_done;
1948
1949sub child_finished {
1950 $child_done = 1;
1951}
1952
1953sub do_run_test {
1954 my $child_pid;
1955 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001956 my $line;
1957 my $full_line;
1958 my $bug = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001959
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001960 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001961
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001962 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001963
1964 $child_done = 0;
1965
1966 $SIG{CHLD} = qw(child_finished);
1967
1968 $child_pid = fork;
1969
1970 child_run_test if (!$child_pid);
1971
1972 $full_line = "";
1973
1974 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001975 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001976 if (defined($line)) {
1977
1978 # we are not guaranteed to get a full line
1979 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05001980 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001981
1982 if ($full_line =~ /call trace:/i) {
1983 $bug = 1;
1984 }
1985
1986 if ($full_line =~ /Kernel panic -/) {
1987 $bug = 1;
1988 }
1989
1990 if ($line =~ /\n/) {
1991 $full_line = "";
1992 }
1993 }
1994 } while (!$child_done && !$bug);
1995
1996 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05001997 my $failure_start = time;
1998 my $now;
1999 do {
2000 $line = wait_for_input($monitor_fp, 1);
2001 if (defined($line)) {
2002 doprint $line;
2003 }
2004 $now = time;
2005 if ($now - $failure_start >= $stop_after_failure) {
2006 last;
2007 }
2008 } while (defined($line));
2009
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002010 doprint "Detected kernel crash!\n";
2011 # kill the child with extreme prejudice
2012 kill 9, $child_pid;
2013 }
2014
2015 waitpid $child_pid, 0;
2016 $child_exit = $?;
2017
Steven Rostedtc5dacb82011-12-22 12:43:57 -05002018 if (!$bug && $in_bisect) {
2019 if (defined($bisect_ret_good)) {
2020 if ($child_exit == $bisect_ret_good) {
2021 return 1;
2022 }
2023 }
2024 if (defined($bisect_ret_skip)) {
2025 if ($child_exit == $bisect_ret_skip) {
2026 return -1;
2027 }
2028 }
2029 if (defined($bisect_ret_abort)) {
2030 if ($child_exit == $bisect_ret_abort) {
2031 fail "test abort" and return -2;
2032 }
2033 }
2034 if (defined($bisect_ret_bad)) {
2035 if ($child_exit == $bisect_ret_skip) {
2036 return 0;
2037 }
2038 }
2039 if (defined($bisect_ret_default)) {
2040 if ($bisect_ret_default eq "good") {
2041 return 1;
2042 } elsif ($bisect_ret_default eq "bad") {
2043 return 0;
2044 } elsif ($bisect_ret_default eq "skip") {
2045 return -1;
2046 } elsif ($bisect_ret_default eq "abort") {
2047 return -2;
2048 } else {
2049 fail "unknown default action: $bisect_ret_default"
2050 and return -2;
2051 }
2052 }
2053 }
2054
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002055 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002056 return 0 if $in_bisect;
2057 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002058 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002059 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002060}
2061
Steven Rostedta75fece2010-11-02 14:58:27 -04002062sub run_git_bisect {
2063 my ($command) = @_;
2064
2065 doprint "$command ... ";
2066
2067 my $output = `$command 2>&1`;
2068 my $ret = $?;
2069
2070 logit $output;
2071
2072 if ($ret) {
2073 doprint "FAILED\n";
2074 dodie "Failed to git bisect";
2075 }
2076
2077 doprint "SUCCESS\n";
2078 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2079 doprint "$1 [$2]\n";
2080 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002081 $bisect_bad_commit = $1;
Steven Rostedta75fece2010-11-02 14:58:27 -04002082 doprint "Found bad commit... $1\n";
2083 return 0;
2084 } else {
2085 # we already logged it, just print it now.
2086 print $output;
2087 }
2088
2089 return 1;
2090}
2091
Steven Rostedtc23dca72011-03-08 09:26:31 -05002092sub bisect_reboot {
2093 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002094 reboot_to_good $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05002095}
2096
2097# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05002098sub run_bisect_test {
2099 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002100
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002101 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002102 my $result;
2103 my $output;
2104 my $ret;
2105
Steven Rostedt0a05c762010-11-08 11:14:10 -05002106 $in_bisect = 1;
2107
2108 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002109
2110 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002111 if ($failed && $bisect_skip) {
2112 $in_bisect = 0;
2113 return -1;
2114 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002115 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002116
2117 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04002118 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002119
2120 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002121 if ($failed && $bisect_skip) {
2122 end_monitor;
2123 bisect_reboot;
2124 $in_bisect = 0;
2125 return -1;
2126 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002127 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002128
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002129 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002130 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002131 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002132 }
2133
2134 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002135 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002136 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002137 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002138 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04002139
2140 # reboot the box to a kernel we can ssh to
2141 if ($type ne "build") {
2142 bisect_reboot;
2143 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002144 $in_bisect = 0;
2145
2146 return $result;
2147}
2148
2149sub run_bisect {
2150 my ($type) = @_;
2151 my $buildtype = "oldconfig";
2152
2153 # We should have a minconfig to use?
2154 if (defined($minconfig)) {
2155 $buildtype = "useconfig:$minconfig";
2156 }
2157
2158 my $ret = run_bisect_test $type, $buildtype;
2159
Steven Rostedtc960bb92011-03-08 09:22:39 -05002160 if ($bisect_manual) {
2161 $ret = answer_bisect;
2162 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002163
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002164 # Are we looking for where it worked, not failed?
2165 if ($reverse_bisect) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002166 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002167 }
2168
Steven Rostedtc23dca72011-03-08 09:26:31 -05002169 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002170 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002171 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002172 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002173 } elsif ($bisect_skip) {
2174 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2175 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002176 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002177}
2178
Steven Rostedtdad98752011-11-22 20:48:57 -05002179sub update_bisect_replay {
2180 my $tmp_log = "$tmpdir/ktest_bisect_log";
2181 run_command "git bisect log > $tmp_log" or
2182 die "can't create bisect log";
2183 return $tmp_log;
2184}
2185
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002186sub bisect {
2187 my ($i) = @_;
2188
2189 my $result;
2190
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002191 die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2192 die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
2193 die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002194
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002195 my $good = $bisect_good;
2196 my $bad = $bisect_bad;
2197 my $type = $bisect_type;
2198 my $start = $bisect_start;
2199 my $replay = $bisect_replay;
2200 my $start_files = $bisect_files;
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002201
2202 if (defined($start_files)) {
2203 $start_files = " -- " . $start_files;
2204 } else {
2205 $start_files = "";
2206 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002207
Steven Rostedta57419b2010-11-02 15:13:54 -04002208 # convert to true sha1's
2209 $good = get_sha1($good);
2210 $bad = get_sha1($bad);
2211
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002212 if (defined($bisect_reverse) && $bisect_reverse == 1) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002213 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2214 $reverse_bisect = 1;
2215 } else {
2216 $reverse_bisect = 0;
2217 }
2218
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002219 # Can't have a test without having a test to run
2220 if ($type eq "test" && !defined($run_test)) {
2221 $type = "boot";
2222 }
2223
Steven Rostedtdad98752011-11-22 20:48:57 -05002224 # Check if a bisect was running
2225 my $bisect_start_file = "$builddir/.git/BISECT_START";
2226
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002227 my $check = $bisect_check;
Steven Rostedtdad98752011-11-22 20:48:57 -05002228 my $do_check = defined($check) && $check ne "0";
2229
2230 if ( -f $bisect_start_file ) {
2231 print "Bisect in progress found\n";
2232 if ($do_check) {
2233 print " If you say yes, then no checks of good or bad will be done\n";
2234 }
2235 if (defined($replay)) {
2236 print "** BISECT_REPLAY is defined in config file **";
2237 print " Ignore config option and perform new git bisect log?\n";
2238 if (read_ync " (yes, no, or cancel) ") {
2239 $replay = update_bisect_replay;
2240 $do_check = 0;
2241 }
2242 } elsif (read_yn "read git log and continue?") {
2243 $replay = update_bisect_replay;
2244 $do_check = 0;
2245 }
2246 }
2247
2248 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002249
2250 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002251 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002252
2253 if ($check ne "good") {
2254 doprint "TESTING BISECT BAD [$bad]\n";
2255 run_command "git checkout $bad" or
2256 die "Failed to checkout $bad";
2257
2258 $result = run_bisect $type;
2259
2260 if ($result ne "bad") {
2261 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2262 }
2263 }
2264
2265 if ($check ne "bad") {
2266 doprint "TESTING BISECT GOOD [$good]\n";
2267 run_command "git checkout $good" or
2268 die "Failed to checkout $good";
2269
2270 $result = run_bisect $type;
2271
2272 if ($result ne "good") {
2273 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2274 }
2275 }
2276
2277 # checkout where we started
2278 run_command "git checkout $head" or
2279 die "Failed to checkout $head";
2280 }
2281
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002282 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002283 dodie "could not start bisect";
2284
2285 run_command "git bisect good $good" or
2286 dodie "could not set bisect good to $good";
2287
2288 run_git_bisect "git bisect bad $bad" or
2289 dodie "could not set bisect bad to $bad";
2290
2291 if (defined($replay)) {
2292 run_command "git bisect replay $replay" or
2293 dodie "failed to run replay";
2294 }
2295
2296 if (defined($start)) {
2297 run_command "git checkout $start" or
2298 dodie "failed to checkout $start";
2299 }
2300
2301 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002302 do {
2303 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002304 $test = run_git_bisect "git bisect $result";
2305 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002306
2307 run_command "git bisect log" or
2308 dodie "could not capture git bisect log";
2309
2310 run_command "git bisect reset" or
2311 dodie "could not reset git bisect";
2312
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002313 doprint "Bad commit was [$bisect_bad_commit]\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002314
Steven Rostedt0a05c762010-11-08 11:14:10 -05002315 success $i;
2316}
2317
2318my %config_ignore;
2319my %config_set;
2320
2321my %config_list;
2322my %null_config;
2323
2324my %dependency;
2325
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002326sub assign_configs {
2327 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002328
2329 open (IN, $config)
2330 or dodie "Failed to read $config";
2331
2332 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002333 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002334 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002335 }
2336 }
2337
2338 close(IN);
2339}
2340
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002341sub process_config_ignore {
2342 my ($config) = @_;
2343
2344 assign_configs \%config_ignore, $config;
2345}
2346
Steven Rostedt0a05c762010-11-08 11:14:10 -05002347sub read_current_config {
2348 my ($config_ref) = @_;
2349
2350 %{$config_ref} = ();
2351 undef %{$config_ref};
2352
2353 my @key = keys %{$config_ref};
2354 if ($#key >= 0) {
2355 print "did not delete!\n";
2356 exit;
2357 }
2358 open (IN, "$output_config");
2359
2360 while (<IN>) {
2361 if (/^(CONFIG\S+)=(.*)/) {
2362 ${$config_ref}{$1} = $2;
2363 }
2364 }
2365 close(IN);
2366}
2367
2368sub get_dependencies {
2369 my ($config) = @_;
2370
2371 my $arr = $dependency{$config};
2372 if (!defined($arr)) {
2373 return ();
2374 }
2375
2376 my @deps = @{$arr};
2377
2378 foreach my $dep (@{$arr}) {
2379 print "ADD DEP $dep\n";
2380 @deps = (@deps, get_dependencies $dep);
2381 }
2382
2383 return @deps;
2384}
2385
2386sub create_config {
2387 my @configs = @_;
2388
2389 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2390
2391 foreach my $config (@configs) {
2392 print OUT "$config_set{$config}\n";
2393 my @deps = get_dependencies $config;
2394 foreach my $dep (@deps) {
2395 print OUT "$config_set{$dep}\n";
2396 }
2397 }
2398
2399 foreach my $config (keys %config_ignore) {
2400 print OUT "$config_ignore{$config}\n";
2401 }
2402 close(OUT);
2403
2404# exit;
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002405 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002406}
2407
2408sub compare_configs {
2409 my (%a, %b) = @_;
2410
2411 foreach my $item (keys %a) {
2412 if (!defined($b{$item})) {
2413 print "diff $item\n";
2414 return 1;
2415 }
2416 delete $b{$item};
2417 }
2418
2419 my @keys = keys %b;
2420 if ($#keys) {
2421 print "diff2 $keys[0]\n";
2422 }
2423 return -1 if ($#keys >= 0);
2424
2425 return 0;
2426}
2427
2428sub run_config_bisect_test {
2429 my ($type) = @_;
2430
2431 return run_bisect_test $type, "oldconfig";
2432}
2433
2434sub process_passed {
2435 my (%configs) = @_;
2436
2437 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2438 # Passed! All these configs are part of a good compile.
2439 # Add them to the min options.
2440 foreach my $config (keys %configs) {
2441 if (defined($config_list{$config})) {
2442 doprint " removing $config\n";
2443 $config_ignore{$config} = $config_list{$config};
2444 delete $config_list{$config};
2445 }
2446 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002447 doprint "config copied to $outputdir/config_good\n";
2448 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002449}
2450
2451sub process_failed {
2452 my ($config) = @_;
2453
2454 doprint "\n\n***************************************\n";
2455 doprint "Found bad config: $config\n";
2456 doprint "***************************************\n\n";
2457}
2458
2459sub run_config_bisect {
2460
2461 my @start_list = keys %config_list;
2462
2463 if ($#start_list < 0) {
2464 doprint "No more configs to test!!!\n";
2465 return -1;
2466 }
2467
2468 doprint "***** RUN TEST ***\n";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002469 my $type = $config_bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002470 my $ret;
2471 my %current_config;
2472
2473 my $count = $#start_list + 1;
2474 doprint " $count configs to test\n";
2475
2476 my $half = int($#start_list / 2);
2477
2478 do {
2479 my @tophalf = @start_list[0 .. $half];
2480
2481 create_config @tophalf;
2482 read_current_config \%current_config;
2483
2484 $count = $#tophalf + 1;
2485 doprint "Testing $count configs\n";
2486 my $found = 0;
2487 # make sure we test something
2488 foreach my $config (@tophalf) {
2489 if (defined($current_config{$config})) {
2490 logit " $config\n";
2491 $found = 1;
2492 }
2493 }
2494 if (!$found) {
2495 # try the other half
2496 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002497 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedt0a05c762010-11-08 11:14:10 -05002498 create_config @tophalf;
2499 read_current_config \%current_config;
2500 foreach my $config (@tophalf) {
2501 if (defined($current_config{$config})) {
2502 logit " $config\n";
2503 $found = 1;
2504 }
2505 }
2506 if (!$found) {
2507 doprint "Failed: Can't make new config with current configs\n";
2508 foreach my $config (@start_list) {
2509 doprint " CONFIG: $config\n";
2510 }
2511 return -1;
2512 }
2513 $count = $#tophalf + 1;
2514 doprint "Testing $count configs\n";
2515 }
2516
2517 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002518 if ($bisect_manual) {
2519 $ret = answer_bisect;
2520 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002521 if ($ret) {
2522 process_passed %current_config;
2523 return 0;
2524 }
2525
2526 doprint "This config had a failure.\n";
2527 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002528 doprint "config copied to $outputdir/config_bad\n";
2529 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002530
2531 # A config exists in this group that was bad.
2532 foreach my $config (keys %config_list) {
2533 if (!defined($current_config{$config})) {
2534 doprint " removing $config\n";
2535 delete $config_list{$config};
2536 }
2537 }
2538
2539 @start_list = @tophalf;
2540
2541 if ($#start_list == 0) {
2542 process_failed $start_list[0];
2543 return 1;
2544 }
2545
2546 # remove half the configs we are looking at and see if
2547 # they are good.
2548 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002549 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002550
Steven Rostedtc960bb92011-03-08 09:22:39 -05002551 # we found a single config, try it again unless we are running manually
2552
2553 if ($bisect_manual) {
2554 process_failed $start_list[0];
2555 return 1;
2556 }
2557
Steven Rostedt0a05c762010-11-08 11:14:10 -05002558 my @tophalf = @start_list[0 .. 0];
2559
2560 $ret = run_config_bisect_test $type;
2561 if ($ret) {
2562 process_passed %current_config;
2563 return 0;
2564 }
2565
2566 process_failed $start_list[0];
2567 return 1;
2568}
2569
2570sub config_bisect {
2571 my ($i) = @_;
2572
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002573 my $start_config = $config_bisect;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002574
2575 my $tmpconfig = "$tmpdir/use_config";
2576
Steven Rostedt30f75da2011-06-13 10:35:35 -04002577 if (defined($config_bisect_good)) {
2578 process_config_ignore $config_bisect_good;
2579 }
2580
Steven Rostedt0a05c762010-11-08 11:14:10 -05002581 # Make the file with the bad config and the min config
2582 if (defined($minconfig)) {
2583 # read the min config for things to ignore
2584 run_command "cp $minconfig $tmpconfig" or
2585 dodie "failed to copy $minconfig to $tmpconfig";
2586 } else {
2587 unlink $tmpconfig;
2588 }
2589
Steven Rostedt0a05c762010-11-08 11:14:10 -05002590 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002591 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002592 process_config_ignore $tmpconfig;
2593 }
2594
2595 # now process the start config
2596 run_command "cp $start_config $output_config" or
2597 dodie "failed to copy $start_config to $output_config";
2598
2599 # read directly what we want to check
2600 my %config_check;
2601 open (IN, $output_config)
2602 or dodie "faied to open $output_config";
2603
2604 while (<IN>) {
2605 if (/^((CONFIG\S*)=.*)/) {
2606 $config_check{$2} = $1;
2607 }
2608 }
2609 close(IN);
2610
Steven Rostedt250bae82011-07-15 22:05:59 -04002611 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002612 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002613
2614 # check to see what we lost (or gained)
2615 open (IN, $output_config)
2616 or dodie "Failed to read $start_config";
2617
2618 my %removed_configs;
2619 my %added_configs;
2620
2621 while (<IN>) {
2622 if (/^((CONFIG\S*)=.*)/) {
2623 # save off all options
2624 $config_set{$2} = $1;
2625 if (defined($config_check{$2})) {
2626 if (defined($config_ignore{$2})) {
2627 $removed_configs{$2} = $1;
2628 } else {
2629 $config_list{$2} = $1;
2630 }
2631 } elsif (!defined($config_ignore{$2})) {
2632 $added_configs{$2} = $1;
2633 $config_list{$2} = $1;
2634 }
2635 }
2636 }
2637 close(IN);
2638
2639 my @confs = keys %removed_configs;
2640 if ($#confs >= 0) {
2641 doprint "Configs overridden by default configs and removed from check:\n";
2642 foreach my $config (@confs) {
2643 doprint " $config\n";
2644 }
2645 }
2646 @confs = keys %added_configs;
2647 if ($#confs >= 0) {
2648 doprint "Configs appearing in make oldconfig and added:\n";
2649 foreach my $config (@confs) {
2650 doprint " $config\n";
2651 }
2652 }
2653
2654 my %config_test;
2655 my $once = 0;
2656
2657 # Sometimes kconfig does weird things. We must make sure
2658 # that the config we autocreate has everything we need
2659 # to test, otherwise we may miss testing configs, or
2660 # may not be able to create a new config.
2661 # Here we create a config with everything set.
2662 create_config (keys %config_list);
2663 read_current_config \%config_test;
2664 foreach my $config (keys %config_list) {
2665 if (!defined($config_test{$config})) {
2666 if (!$once) {
2667 $once = 1;
2668 doprint "Configs not produced by kconfig (will not be checked):\n";
2669 }
2670 doprint " $config\n";
2671 delete $config_list{$config};
2672 }
2673 }
2674 my $ret;
2675 do {
2676 $ret = run_config_bisect;
2677 } while (!$ret);
2678
2679 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002680
2681 success $i;
2682}
2683
Steven Rostedt27d934b2011-05-20 09:18:18 -04002684sub patchcheck_reboot {
2685 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002686 reboot_to_good $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04002687}
2688
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002689sub patchcheck {
2690 my ($i) = @_;
2691
2692 die "PATCHCHECK_START[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002693 if (!defined($patchcheck_start));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002694 die "PATCHCHECK_TYPE[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002695 if (!defined($patchcheck_type));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002696
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002697 my $start = $patchcheck_start;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002698
2699 my $end = "HEAD";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002700 if (defined($patchcheck_end)) {
2701 $end = $patchcheck_end;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002702 }
2703
Steven Rostedta57419b2010-11-02 15:13:54 -04002704 # Get the true sha1's since we can use things like HEAD~3
2705 $start = get_sha1($start);
2706 $end = get_sha1($end);
2707
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002708 my $type = $patchcheck_type;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002709
2710 # Can't have a test without having a test to run
2711 if ($type eq "test" && !defined($run_test)) {
2712 $type = "boot";
2713 }
2714
2715 open (IN, "git log --pretty=oneline $end|") or
2716 dodie "could not get git list";
2717
2718 my @list;
2719
2720 while (<IN>) {
2721 chomp;
2722 $list[$#list+1] = $_;
2723 last if (/^$start/);
2724 }
2725 close(IN);
2726
2727 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002728 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002729 }
2730
2731 # go backwards in the list
2732 @list = reverse @list;
2733
2734 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04002735 my %ignored_warnings;
2736
2737 if (defined($ignore_warnings)) {
2738 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2739 $ignored_warnings{$sha1} = 1;
2740 }
2741 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002742
2743 $in_patchcheck = 1;
2744 foreach my $item (@list) {
2745 my $sha1 = $item;
2746 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2747
2748 doprint "\nProcessing commit $item\n\n";
2749
2750 run_command "git checkout $sha1" or
2751 die "Failed to checkout $sha1";
2752
2753 # only clean on the first and last patch
2754 if ($item eq $list[0] ||
2755 $item eq $list[$#list]) {
2756 $noclean = $save_clean;
2757 } else {
2758 $noclean = 1;
2759 }
2760
2761 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002762 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002763 } else {
2764 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002765 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002766 }
2767
Steven Rostedt19902072011-06-14 20:46:25 -04002768
2769 if (!defined($ignored_warnings{$sha1})) {
2770 check_buildlog $sha1 or return 0;
2771 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002772
2773 next if ($type eq "build");
2774
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002775 my $failed = 0;
2776
Steven Rostedtddf607e2011-06-14 20:49:13 -04002777 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002778
2779 if (!$failed && $type ne "boot"){
2780 do_run_test or $failed = 1;
2781 }
2782 end_monitor;
2783 return 0 if ($failed);
2784
Steven Rostedt27d934b2011-05-20 09:18:18 -04002785 patchcheck_reboot;
2786
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002787 }
2788 $in_patchcheck = 0;
2789 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002790
2791 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002792}
2793
Steven Rostedtb9066f62011-07-15 21:25:24 -04002794my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04002795my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002796my $iflevel = 0;
2797my @ifdeps;
2798
2799# prevent recursion
2800my %read_kconfigs;
2801
Steven Rostedtac6974c2011-10-04 09:40:17 -04002802sub add_dep {
2803 # $config depends on $dep
2804 my ($config, $dep) = @_;
2805
2806 if (defined($depends{$config})) {
2807 $depends{$config} .= " " . $dep;
2808 } else {
2809 $depends{$config} = $dep;
2810 }
2811
2812 # record the number of configs depending on $dep
2813 if (defined $depcount{$dep}) {
2814 $depcount{$dep}++;
2815 } else {
2816 $depcount{$dep} = 1;
2817 }
2818}
2819
Steven Rostedtb9066f62011-07-15 21:25:24 -04002820# taken from streamline_config.pl
2821sub read_kconfig {
2822 my ($kconfig) = @_;
2823
2824 my $state = "NONE";
2825 my $config;
2826 my @kconfigs;
2827
2828 my $cont = 0;
2829 my $line;
2830
2831
2832 if (! -f $kconfig) {
2833 doprint "file $kconfig does not exist, skipping\n";
2834 return;
2835 }
2836
2837 open(KIN, "$kconfig")
2838 or die "Can't open $kconfig";
2839 while (<KIN>) {
2840 chomp;
2841
2842 # Make sure that lines ending with \ continue
2843 if ($cont) {
2844 $_ = $line . " " . $_;
2845 }
2846
2847 if (s/\\$//) {
2848 $cont = 1;
2849 $line = $_;
2850 next;
2851 }
2852
2853 $cont = 0;
2854
2855 # collect any Kconfig sources
2856 if (/^source\s*"(.*)"/) {
2857 $kconfigs[$#kconfigs+1] = $1;
2858 }
2859
2860 # configs found
2861 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2862 $state = "NEW";
2863 $config = $2;
2864
2865 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04002866 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04002867 }
2868
2869 # collect the depends for the config
2870 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2871
Steven Rostedtac6974c2011-10-04 09:40:17 -04002872 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002873
2874 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04002875 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2876
2877 # selected by depends on config
2878 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002879
2880 # Check for if statements
2881 } elsif (/^if\s+(.*\S)\s*$/) {
2882 my $deps = $1;
2883 # remove beginning and ending non text
2884 $deps =~ s/^[^a-zA-Z0-9_]*//;
2885 $deps =~ s/[^a-zA-Z0-9_]*$//;
2886
2887 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2888
2889 $ifdeps[$iflevel++] = join ':', @deps;
2890
2891 } elsif (/^endif/) {
2892
2893 $iflevel-- if ($iflevel);
2894
2895 # stop on "help"
2896 } elsif (/^\s*help\s*$/) {
2897 $state = "NONE";
2898 }
2899 }
2900 close(KIN);
2901
2902 # read in any configs that were found.
2903 foreach $kconfig (@kconfigs) {
2904 if (!defined($read_kconfigs{$kconfig})) {
2905 $read_kconfigs{$kconfig} = 1;
2906 read_kconfig("$builddir/$kconfig");
2907 }
2908 }
2909}
2910
2911sub read_depends {
2912 # find out which arch this is by the kconfig file
2913 open (IN, $output_config)
2914 or dodie "Failed to read $output_config";
2915 my $arch;
2916 while (<IN>) {
2917 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2918 $arch = $1;
2919 last;
2920 }
2921 }
2922 close IN;
2923
2924 if (!defined($arch)) {
2925 doprint "Could not find arch from config file\n";
2926 doprint "no dependencies used\n";
2927 return;
2928 }
2929
2930 # arch is really the subarch, we need to know
2931 # what directory to look at.
2932 if ($arch eq "i386" || $arch eq "x86_64") {
2933 $arch = "x86";
2934 } elsif ($arch =~ /^tile/) {
2935 $arch = "tile";
2936 }
2937
2938 my $kconfig = "$builddir/arch/$arch/Kconfig";
2939
2940 if (! -f $kconfig && $arch =~ /\d$/) {
2941 my $orig = $arch;
2942 # some subarchs have numbers, truncate them
2943 $arch =~ s/\d*$//;
2944 $kconfig = "$builddir/arch/$arch/Kconfig";
2945 if (! -f $kconfig) {
2946 doprint "No idea what arch dir $orig is for\n";
2947 doprint "no dependencies used\n";
2948 return;
2949 }
2950 }
2951
2952 read_kconfig($kconfig);
2953}
2954
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002955sub read_config_list {
2956 my ($config) = @_;
2957
2958 open (IN, $config)
2959 or dodie "Failed to read $config";
2960
2961 while (<IN>) {
2962 if (/^((CONFIG\S*)=.*)/) {
2963 if (!defined($config_ignore{$2})) {
2964 $config_list{$2} = $1;
2965 }
2966 }
2967 }
2968
2969 close(IN);
2970}
2971
2972sub read_output_config {
2973 my ($config) = @_;
2974
2975 assign_configs \%config_ignore, $config;
2976}
2977
2978sub make_new_config {
2979 my @configs = @_;
2980
2981 open (OUT, ">$output_config")
2982 or dodie "Failed to write $output_config";
2983
2984 foreach my $config (@configs) {
2985 print OUT "$config\n";
2986 }
2987 close OUT;
2988}
2989
Steven Rostedtac6974c2011-10-04 09:40:17 -04002990sub chomp_config {
2991 my ($config) = @_;
2992
2993 $config =~ s/CONFIG_//;
2994
2995 return $config;
2996}
2997
Steven Rostedtb9066f62011-07-15 21:25:24 -04002998sub get_depends {
2999 my ($dep) = @_;
3000
Steven Rostedtac6974c2011-10-04 09:40:17 -04003001 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003002
3003 $dep = $depends{"$kconfig"};
3004
3005 # the dep string we have saves the dependencies as they
3006 # were found, including expressions like ! && ||. We
3007 # want to split this out into just an array of configs.
3008
3009 my $valid = "A-Za-z_0-9";
3010
3011 my @configs;
3012
3013 while ($dep =~ /[$valid]/) {
3014
3015 if ($dep =~ /^[^$valid]*([$valid]+)/) {
3016 my $conf = "CONFIG_" . $1;
3017
3018 $configs[$#configs + 1] = $conf;
3019
3020 $dep =~ s/^[^$valid]*[$valid]+//;
3021 } else {
3022 die "this should never happen";
3023 }
3024 }
3025
3026 return @configs;
3027}
3028
3029my %min_configs;
3030my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003031my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003032my %processed_configs;
3033my %nochange_config;
3034
3035sub test_this_config {
3036 my ($config) = @_;
3037
3038 my $found;
3039
3040 # if we already processed this config, skip it
3041 if (defined($processed_configs{$config})) {
3042 return undef;
3043 }
3044 $processed_configs{$config} = 1;
3045
3046 # if this config failed during this round, skip it
3047 if (defined($nochange_config{$config})) {
3048 return undef;
3049 }
3050
Steven Rostedtac6974c2011-10-04 09:40:17 -04003051 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003052
3053 # Test dependencies first
3054 if (defined($depends{"$kconfig"})) {
3055 my @parents = get_depends $config;
3056 foreach my $parent (@parents) {
3057 # if the parent is in the min config, check it first
3058 next if (!defined($min_configs{$parent}));
3059 $found = test_this_config($parent);
3060 if (defined($found)) {
3061 return $found;
3062 }
3063 }
3064 }
3065
3066 # Remove this config from the list of configs
3067 # do a make oldnoconfig and then read the resulting
3068 # .config to make sure it is missing the config that
3069 # we had before
3070 my %configs = %min_configs;
3071 delete $configs{$config};
3072 make_new_config ((values %configs), (values %keep_configs));
3073 make_oldconfig;
3074 undef %configs;
3075 assign_configs \%configs, $output_config;
3076
3077 return $config if (!defined($configs{$config}));
3078
3079 doprint "disabling config $config did not change .config\n";
3080
3081 $nochange_config{$config} = 1;
3082
3083 return undef;
3084}
3085
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003086sub make_min_config {
3087 my ($i) = @_;
3088
3089 if (!defined($output_minconfig)) {
3090 fail "OUTPUT_MIN_CONFIG not defined" and return;
3091 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04003092
3093 # If output_minconfig exists, and the start_minconfig
3094 # came from min_config, than ask if we should use
3095 # that instead.
3096 if (-f $output_minconfig && !$start_minconfig_defined) {
3097 print "$output_minconfig exists\n";
3098 if (read_yn " Use it as minconfig?") {
3099 $start_minconfig = $output_minconfig;
3100 }
3101 }
3102
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003103 if (!defined($start_minconfig)) {
3104 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
3105 }
3106
Steven Rostedt35ce5952011-07-15 21:57:25 -04003107 my $temp_config = "$tmpdir/temp_config";
3108
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003109 # First things first. We build an allnoconfig to find
3110 # out what the defaults are that we can't touch.
3111 # Some are selections, but we really can't handle selections.
3112
3113 my $save_minconfig = $minconfig;
3114 undef $minconfig;
3115
3116 run_command "$make allnoconfig" or return 0;
3117
Steven Rostedtb9066f62011-07-15 21:25:24 -04003118 read_depends;
3119
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003120 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003121
Steven Rostedt43d1b652011-07-15 22:01:56 -04003122 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003123 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003124
3125 if (defined($ignore_config)) {
3126 # make sure the file exists
3127 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003128 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003129 }
3130
Steven Rostedt43d1b652011-07-15 22:01:56 -04003131 %keep_configs = %save_configs;
3132
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003133 doprint "Load initial configs from $start_minconfig\n";
3134
3135 # Look at the current min configs, and save off all the
3136 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003137 assign_configs \%min_configs, $start_minconfig;
3138
3139 my @config_keys = keys %min_configs;
3140
Steven Rostedtac6974c2011-10-04 09:40:17 -04003141 # All configs need a depcount
3142 foreach my $config (@config_keys) {
3143 my $kconfig = chomp_config $config;
3144 if (!defined $depcount{$kconfig}) {
3145 $depcount{$kconfig} = 0;
3146 }
3147 }
3148
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003149 # Remove anything that was set by the make allnoconfig
3150 # we shouldn't need them as they get set for us anyway.
3151 foreach my $config (@config_keys) {
3152 # Remove anything in the ignore_config
3153 if (defined($keep_configs{$config})) {
3154 my $file = $ignore_config;
3155 $file =~ s,.*/(.*?)$,$1,;
3156 doprint "$config set by $file ... ignored\n";
3157 delete $min_configs{$config};
3158 next;
3159 }
3160 # But make sure the settings are the same. If a min config
3161 # sets a selection, we do not want to get rid of it if
3162 # it is not the same as what we have. Just move it into
3163 # the keep configs.
3164 if (defined($config_ignore{$config})) {
3165 if ($config_ignore{$config} ne $min_configs{$config}) {
3166 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3167 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3168 $keep_configs{$config} = $min_configs{$config};
3169 } else {
3170 doprint "$config set by allnoconfig ... ignored\n";
3171 }
3172 delete $min_configs{$config};
3173 }
3174 }
3175
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003176 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003177 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003178
3179 while (!$done) {
3180
3181 my $config;
3182 my $found;
3183
3184 # Now disable each config one by one and do a make oldconfig
3185 # till we find a config that changes our list.
3186
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003187 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003188
3189 # Sort keys by who is most dependent on
3190 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3191 @test_configs ;
3192
3193 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003194 my $reset = 1;
3195 for (my $i = 0; $i < $#test_configs; $i++) {
3196 if (!defined($nochange_config{$test_configs[0]})) {
3197 $reset = 0;
3198 last;
3199 }
3200 # This config didn't change the .config last time.
3201 # Place it at the end
3202 my $config = shift @test_configs;
3203 push @test_configs, $config;
3204 }
3205
3206 # if every test config has failed to modify the .config file
3207 # in the past, then reset and start over.
3208 if ($reset) {
3209 undef %nochange_config;
3210 }
3211
Steven Rostedtb9066f62011-07-15 21:25:24 -04003212 undef %processed_configs;
3213
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003214 foreach my $config (@test_configs) {
3215
Steven Rostedtb9066f62011-07-15 21:25:24 -04003216 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003217
Steven Rostedtb9066f62011-07-15 21:25:24 -04003218 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003219
3220 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003221 }
3222
3223 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003224 # we could have failed due to the nochange_config hash
3225 # reset and try again
3226 if (!$take_two) {
3227 undef %nochange_config;
3228 $take_two = 1;
3229 next;
3230 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003231 doprint "No more configs found that we can disable\n";
3232 $done = 1;
3233 last;
3234 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003235 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003236
3237 $config = $found;
3238
3239 doprint "Test with $config disabled\n";
3240
3241 # set in_bisect to keep build and monitor from dieing
3242 $in_bisect = 1;
3243
3244 my $failed = 0;
3245 build "oldconfig";
3246 start_monitor_and_boot or $failed = 1;
3247 end_monitor;
3248
3249 $in_bisect = 0;
3250
3251 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003252 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003253 # this config is needed, add it to the ignore list.
3254 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003255 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003256 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003257
3258 # update new ignore configs
3259 if (defined($ignore_config)) {
3260 open (OUT, ">$temp_config")
3261 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003262 foreach my $config (keys %save_configs) {
3263 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003264 }
3265 close OUT;
3266 run_command "mv $temp_config $ignore_config" or
3267 dodie "failed to copy update to $ignore_config";
3268 }
3269
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003270 } else {
3271 # We booted without this config, remove it from the minconfigs.
3272 doprint "$config is not needed, disabling\n";
3273
3274 delete $min_configs{$config};
3275
3276 # Also disable anything that is not enabled in this config
3277 my %configs;
3278 assign_configs \%configs, $output_config;
3279 my @config_keys = keys %min_configs;
3280 foreach my $config (@config_keys) {
3281 if (!defined($configs{$config})) {
3282 doprint "$config is not set, disabling\n";
3283 delete $min_configs{$config};
3284 }
3285 }
3286
3287 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003288 open (OUT, ">$temp_config")
3289 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003290 foreach my $config (keys %keep_configs) {
3291 print OUT "$keep_configs{$config}\n";
3292 }
3293 foreach my $config (keys %min_configs) {
3294 print OUT "$min_configs{$config}\n";
3295 }
3296 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003297
3298 run_command "mv $temp_config $output_minconfig" or
3299 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003300 }
3301
3302 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003303 reboot_to_good $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003304 }
3305
3306 success $i;
3307 return 1;
3308}
3309
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003310$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003311
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003312if ($#ARGV == 0) {
3313 $ktest_config = $ARGV[0];
3314 if (! -f $ktest_config) {
3315 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003316 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003317 exit 0;
3318 }
3319 }
3320} else {
3321 $ktest_config = "ktest.conf";
3322}
3323
3324if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003325 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003326 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003327 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3328 print OUT << "EOF"
3329# Generated by ktest.pl
3330#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003331
3332# PWD is a ktest.pl variable that will result in the process working
3333# directory that ktest.pl is executed in.
3334
3335# THIS_DIR is automatically assigned the PWD of the path that generated
3336# the config file. It is best to use this variable when assigning other
3337# directory paths within this directory. This allows you to easily
3338# move the test cases to other locations or to other machines.
3339#
3340THIS_DIR := $variable{"PWD"}
3341
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003342# Define each test with TEST_START
3343# The config options below it will override the defaults
3344TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003345TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003346
3347DEFAULTS
3348EOF
3349;
3350 close(OUT);
3351}
3352read_config $ktest_config;
3353
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003354if (defined($opt{"LOG_FILE"})) {
3355 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3356}
3357
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003358# Append any configs entered in manually to the config file.
3359my @new_configs = keys %entered_configs;
3360if ($#new_configs >= 0) {
3361 print "\nAppending entered in configs to $ktest_config\n";
3362 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3363 foreach my $config (@new_configs) {
3364 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003365 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003366 }
3367}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003368
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003369if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3370 unlink $opt{"LOG_FILE"};
3371}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003372
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003373doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3374
Steven Rostedta57419b2010-11-02 15:13:54 -04003375for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3376
3377 if (!$i) {
3378 doprint "DEFAULT OPTIONS:\n";
3379 } else {
3380 doprint "\nTEST $i OPTIONS";
3381 if (defined($repeat_tests{$i})) {
3382 $repeat = $repeat_tests{$i};
3383 doprint " ITERATE $repeat";
3384 }
3385 doprint "\n";
3386 }
3387
3388 foreach my $option (sort keys %opt) {
3389
3390 if ($option =~ /\[(\d+)\]$/) {
3391 next if ($i != $1);
3392 } else {
3393 next if ($i);
3394 }
3395
3396 doprint "$option = $opt{$option}\n";
3397 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003398}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003399
Steven Rostedt2a625122011-05-20 15:48:59 -04003400sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003401 my ($name, $i) = @_;
3402
3403 my $option = "$name\[$i\]";
3404
3405 if (defined($opt{$option})) {
3406 return $opt{$option};
3407 }
3408
Steven Rostedta57419b2010-11-02 15:13:54 -04003409 foreach my $test (keys %repeat_tests) {
3410 if ($i >= $test &&
3411 $i < $test + $repeat_tests{$test}) {
3412 $option = "$name\[$test\]";
3413 if (defined($opt{$option})) {
3414 return $opt{$option};
3415 }
3416 }
3417 }
3418
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003419 if (defined($opt{$name})) {
3420 return $opt{$name};
3421 }
3422
3423 return undef;
3424}
3425
Steven Rostedt2a625122011-05-20 15:48:59 -04003426sub set_test_option {
3427 my ($name, $i) = @_;
3428
3429 my $option = __set_test_option($name, $i);
3430 return $option if (!defined($option));
3431
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003432 return eval_option($option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003433}
3434
Steven Rostedt2545eb62010-11-02 15:01:32 -04003435# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003436for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003437
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003438 # Do not reboot on failing test options
3439 $no_reboot = 1;
3440
Steven Rostedt576f6272010-11-02 14:58:38 -04003441 $iteration = $i;
3442
Steven Rostedta75fece2010-11-02 14:58:27 -04003443 my $makecmd = set_test_option("MAKE_CMD", $i);
3444
Steven Rostedt9cc9e092011-12-22 21:37:22 -05003445 # Load all the options into their mapped variable names
3446 foreach my $opt (keys %option_map) {
3447 ${$option_map{$opt}} = set_test_option($opt, $i);
3448 }
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003449
Steven Rostedt35ce5952011-07-15 21:57:25 -04003450 $start_minconfig_defined = 1;
3451
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003452 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003453 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003454 $start_minconfig = $minconfig;
3455 }
3456
Steven Rostedta75fece2010-11-02 14:58:27 -04003457 chdir $builddir || die "can't change directory to $builddir";
3458
Andrew Jonesa908a662011-08-12 15:32:03 +02003459 foreach my $dir ($tmpdir, $outputdir) {
3460 if (!-d $dir) {
3461 mkpath($dir) or
3462 die "can't create $dir";
3463 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003464 }
3465
Steven Rostedte48c5292010-11-02 14:35:37 -04003466 $ENV{"SSH_USER"} = $ssh_user;
3467 $ENV{"MACHINE"} = $machine;
3468
Steven Rostedta75fece2010-11-02 14:58:27 -04003469 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303470 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003471 $dmesg = "$tmpdir/dmesg-$machine";
3472 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003473 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003474
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003475 if (!$buildonly) {
3476 $target = "$ssh_user\@$machine";
3477 if ($reboot_type eq "grub") {
3478 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003479 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003480 }
3481
3482 my $run_type = $build_type;
3483 if ($test_type eq "patchcheck") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003484 $run_type = $patchcheck_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003485 } elsif ($test_type eq "bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003486 $run_type = $bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003487 } elsif ($test_type eq "config_bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003488 $run_type = $config_bisect_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003489 }
3490
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003491 if ($test_type eq "make_min_config") {
3492 $run_type = "";
3493 }
3494
Steven Rostedta75fece2010-11-02 14:58:27 -04003495 # mistake in config file?
3496 if (!defined($run_type)) {
3497 $run_type = "ERROR";
3498 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003499
Steven Rostedte0a87422011-09-30 17:50:48 -04003500 my $installme = "";
3501 $installme = " no_install" if ($no_install);
3502
Steven Rostedt2545eb62010-11-02 15:01:32 -04003503 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003504 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003505
3506 unlink $dmesg;
3507 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303508 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003509
Steven Rostedt250bae82011-07-15 22:05:59 -04003510 if (defined($addconfig)) {
3511 my $min = $minconfig;
3512 if (!defined($minconfig)) {
3513 $min = "";
3514 }
3515 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003516 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003517 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003518 }
3519
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003520 if (defined($checkout)) {
3521 run_command "git checkout $checkout" or
3522 die "failed to checkout $checkout";
3523 }
3524
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003525 $no_reboot = 0;
3526
3527
Steven Rostedta75fece2010-11-02 14:58:27 -04003528 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003529 bisect $i;
3530 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003531 } elsif ($test_type eq "config_bisect") {
3532 config_bisect $i;
3533 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003534 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003535 patchcheck $i;
3536 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003537 } elsif ($test_type eq "make_min_config") {
3538 make_min_config $i;
3539 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003540 }
3541
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003542 if ($build_type ne "nobuild") {
3543 build $build_type or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003544 }
3545
Steven Rostedtcd8e3682011-08-18 16:35:44 -04003546 if ($test_type eq "install") {
3547 get_version;
3548 install;
3549 success $i;
3550 next;
3551 }
3552
Steven Rostedta75fece2010-11-02 14:58:27 -04003553 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04003554 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04003555 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04003556
3557 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3558 do_run_test or $failed = 1;
3559 }
3560 end_monitor;
3561 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003562 }
3563
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003564 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003565}
3566
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003567if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003568 halt;
Steven Rostedt576f6272010-11-02 14:58:38 -04003569} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003570 reboot_to_good;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003571}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003572
Steven Rostedte48c5292010-11-02 14:35:37 -04003573doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3574
Steven Rostedt2545eb62010-11-02 15:01:32 -04003575exit 0;