blob: f360cbdbf00f0fcf2dc5448606648f0ffe93c470 [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");
488 } else {
489 get_ktest_config("REBOOT_SCRIPT");
490 }
491}
492
Steven Rostedt77d942c2011-05-20 13:36:58 -0400493sub process_variables {
Steven Rostedt8d735212011-10-17 11:36:44 -0400494 my ($value, $remove_undef) = @_;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400495 my $retval = "";
496
497 # We want to check for '\', and it is just easier
498 # to check the previous characet of '$' and not need
499 # to worry if '$' is the first character. By adding
500 # a space to $value, we can just check [^\\]\$ and
501 # it will still work.
502 $value = " $value";
503
504 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
505 my $begin = $1;
506 my $var = $2;
507 my $end = $3;
508 # append beginning of value to retval
509 $retval = "$retval$begin";
510 if (defined($variable{$var})) {
511 $retval = "$retval$variable{$var}";
Steven Rostedt8d735212011-10-17 11:36:44 -0400512 } elsif (defined($remove_undef) && $remove_undef) {
513 # for if statements, any variable that is not defined,
514 # we simple convert to 0
515 $retval = "${retval}0";
Steven Rostedt77d942c2011-05-20 13:36:58 -0400516 } else {
517 # put back the origin piece.
518 $retval = "$retval\$\{$var\}";
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500519 # This could be an option that is used later, save
520 # it so we don't warn if this option is not one of
521 # ktests options.
522 $used_options{$var} = 1;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400523 }
524 $value = $end;
525 }
526 $retval = "$retval$value";
527
528 # remove the space added in the beginning
529 $retval =~ s/ //;
530
531 return "$retval"
532}
533
Steven Rostedta57419b2010-11-02 15:13:54 -0400534sub set_value {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400535 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
Steven Rostedta57419b2010-11-02 15:13:54 -0400536
Steven Rostedtcad96662011-12-22 11:32:52 -0500537 my $prvalue = process_variables($rvalue);
538
539 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500540 # Note if a test is something other than build, then we
541 # will need other manditory options.
Steven Rostedtcad96662011-12-22 11:32:52 -0500542 if ($prvalue ne "install") {
Steven Rostedt165708b2011-11-26 20:56:52 -0500543 $buildonly = 0;
544 } else {
545 # install still limits some manditory options.
546 $buildonly = 2;
547 }
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500548 }
549
Steven Rostedta57419b2010-11-02 15:13:54 -0400550 if (defined($opt{$lvalue})) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400551 if (!$override || defined(${$overrides}{$lvalue})) {
552 my $extra = "";
553 if ($override) {
554 $extra = "In the same override section!\n";
555 }
556 die "$name: $.: Option $lvalue defined more than once!\n$extra";
557 }
Steven Rostedtcad96662011-12-22 11:32:52 -0500558 ${$overrides}{$lvalue} = $prvalue;
Steven Rostedta57419b2010-11-02 15:13:54 -0400559 }
Steven Rostedt21a96792010-11-08 16:45:50 -0500560 if ($rvalue =~ /^\s*$/) {
561 delete $opt{$lvalue};
562 } else {
Steven Rostedtcad96662011-12-22 11:32:52 -0500563 $opt{$lvalue} = $prvalue;
Steven Rostedt21a96792010-11-08 16:45:50 -0500564 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400565}
566
Steven Rostedt77d942c2011-05-20 13:36:58 -0400567sub set_variable {
568 my ($lvalue, $rvalue) = @_;
569
570 if ($rvalue =~ /^\s*$/) {
571 delete $variable{$lvalue};
572 } else {
573 $rvalue = process_variables($rvalue);
574 $variable{$lvalue} = $rvalue;
575 }
576}
577
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400578sub process_compare {
579 my ($lval, $cmp, $rval) = @_;
580
581 # remove whitespace
582
583 $lval =~ s/^\s*//;
584 $lval =~ s/\s*$//;
585
586 $rval =~ s/^\s*//;
587 $rval =~ s/\s*$//;
588
589 if ($cmp eq "==") {
590 return $lval eq $rval;
591 } elsif ($cmp eq "!=") {
592 return $lval ne $rval;
593 }
594
595 my $statement = "$lval $cmp $rval";
596 my $ret = eval $statement;
597
598 # $@ stores error of eval
599 if ($@) {
600 return -1;
601 }
602
603 return $ret;
604}
605
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400606sub value_defined {
607 my ($val) = @_;
608
609 return defined($variable{$2}) ||
610 defined($opt{$2});
611}
612
Steven Rostedt8d735212011-10-17 11:36:44 -0400613my $d = 0;
614sub process_expression {
615 my ($name, $val) = @_;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400616
Steven Rostedt8d735212011-10-17 11:36:44 -0400617 my $c = $d++;
618
619 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
620 my $express = $1;
621
622 if (process_expression($name, $express)) {
623 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
624 } else {
625 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
626 }
627 }
628
629 $d--;
630 my $OR = "\\|\\|";
631 my $AND = "\\&\\&";
632
633 while ($val =~ s/^(.*?)($OR|$AND)//) {
634 my $express = $1;
635 my $op = $2;
636
637 if (process_expression($name, $express)) {
638 if ($op eq "||") {
639 return 1;
640 }
641 } else {
642 if ($op eq "&&") {
643 return 0;
644 }
645 }
646 }
Steven Rostedt45d73a52011-09-30 19:44:53 -0400647
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400648 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
649 my $ret = process_compare($1, $2, $3);
650 if ($ret < 0) {
651 die "$name: $.: Unable to process comparison\n";
652 }
653 return $ret;
654 }
655
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400656 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
657 if (defined $1) {
658 return !value_defined($2);
659 } else {
660 return value_defined($2);
661 }
662 }
663
Steven Rostedt45d73a52011-09-30 19:44:53 -0400664 if ($val =~ /^\s*0\s*$/) {
665 return 0;
666 } elsif ($val =~ /^\s*\d+\s*$/) {
667 return 1;
668 }
669
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400670 die ("$name: $.: Undefined content $val in if statement\n");
Steven Rostedt8d735212011-10-17 11:36:44 -0400671}
672
673sub process_if {
674 my ($name, $value) = @_;
675
676 # Convert variables and replace undefined ones with 0
677 my $val = process_variables($value, 1);
678 my $ret = process_expression $name, $val;
679
680 return $ret;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400681}
682
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400683sub __read_config {
684 my ($config, $current_test_num) = @_;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400685
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400686 my $in;
687 open($in, $config) || die "can't read file $config";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400688
Steven Rostedta57419b2010-11-02 15:13:54 -0400689 my $name = $config;
690 $name =~ s,.*/(.*),$1,;
691
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400692 my $test_num = $$current_test_num;
Steven Rostedta57419b2010-11-02 15:13:54 -0400693 my $default = 1;
694 my $repeat = 1;
695 my $num_tests_set = 0;
696 my $skip = 0;
697 my $rest;
Steven Rostedta9f84422011-10-17 11:06:29 -0400698 my $line;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400699 my $test_case = 0;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400700 my $if = 0;
701 my $if_set = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400702 my $override = 0;
703
704 my %overrides;
Steven Rostedta57419b2010-11-02 15:13:54 -0400705
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400706 while (<$in>) {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400707
708 # ignore blank lines and comments
709 next if (/^\s*$/ || /\s*\#/);
710
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400711 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400712
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400713 my $type = $1;
714 $rest = $2;
Steven Rostedta9f84422011-10-17 11:06:29 -0400715 $line = $2;
Steven Rostedta57419b2010-11-02 15:13:54 -0400716
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400717 my $old_test_num;
718 my $old_repeat;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400719 $override = 0;
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400720
721 if ($type eq "TEST_START") {
722
723 if ($num_tests_set) {
724 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
725 }
726
727 $old_test_num = $test_num;
728 $old_repeat = $repeat;
729
730 $test_num += $repeat;
731 $default = 0;
732 $repeat = 1;
733 } else {
734 $default = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400735 }
736
Steven Rostedta9f84422011-10-17 11:06:29 -0400737 # If SKIP is anywhere in the line, the command will be skipped
738 if ($rest =~ s/\s+SKIP\b//) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400739 $skip = 1;
740 } else {
Steven Rostedt0df213c2011-06-14 20:51:37 -0400741 $test_case = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400742 $skip = 0;
743 }
744
Steven Rostedta9f84422011-10-17 11:06:29 -0400745 if ($rest =~ s/\sELSE\b//) {
746 if (!$if) {
747 die "$name: $.: ELSE found with out matching IF section\n$_";
748 }
749 $if = 0;
750
751 if ($if_set) {
752 $skip = 1;
753 } else {
754 $skip = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400755 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400756 }
757
Steven Rostedta9f84422011-10-17 11:06:29 -0400758 if ($rest =~ s/\sIF\s+(.*)//) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400759 if (process_if($name, $1)) {
760 $if_set = 1;
761 } else {
762 $skip = 1;
763 }
764 $if = 1;
765 } else {
766 $if = 0;
Steven Rostedta9f84422011-10-17 11:06:29 -0400767 $if_set = 0;
Steven Rostedta57419b2010-11-02 15:13:54 -0400768 }
769
Steven Rostedta9f84422011-10-17 11:06:29 -0400770 if (!$skip) {
771 if ($type eq "TEST_START") {
772 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
773 $repeat = $1;
774 $repeat_tests{"$test_num"} = $repeat;
775 }
776 } elsif ($rest =~ s/\sOVERRIDE\b//) {
777 # DEFAULT only
778 $override = 1;
779 # Clear previous overrides
780 %overrides = ();
781 }
782 }
783
784 if (!$skip && $rest !~ /^\s*$/) {
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400785 die "$name: $.: Gargbage found after $type\n$_";
Steven Rostedta57419b2010-11-02 15:13:54 -0400786 }
787
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400788 if ($skip && $type eq "TEST_START") {
Steven Rostedta57419b2010-11-02 15:13:54 -0400789 $test_num = $old_test_num;
Steven Rostedte48c5292010-11-02 14:35:37 -0400790 $repeat = $old_repeat;
Steven Rostedta57419b2010-11-02 15:13:54 -0400791 }
792
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400793 } elsif (/^\s*ELSE\b(.*)$/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400794 if (!$if) {
795 die "$name: $.: ELSE found with out matching IF section\n$_";
796 }
797 $rest = $1;
798 if ($if_set) {
799 $skip = 1;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400800 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400801 } else {
802 $skip = 0;
803
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400804 if ($rest =~ /\sIF\s+(.*)/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400805 # May be a ELSE IF section.
806 if (!process_if($name, $1)) {
807 $skip = 1;
808 }
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400809 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400810 } else {
811 $if = 0;
812 }
813 }
814
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400815 if ($rest !~ /^\s*$/) {
816 die "$name: $.: Gargbage found after DEFAULTS\n$_";
817 }
818
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400819 } elsif (/^\s*INCLUDE\s+(\S+)/) {
820
821 next if ($skip);
822
823 if (!$default) {
824 die "$name: $.: INCLUDE can only be done in default sections\n$_";
825 }
826
827 my $file = process_variables($1);
828
829 if ($file !~ m,^/,) {
830 # check the path of the config file first
831 if ($config =~ m,(.*)/,) {
832 if (-f "$1/$file") {
833 $file = "$1/$file";
834 }
835 }
836 }
837
838 if ( ! -r $file ) {
839 die "$name: $.: Can't read file $file\n$_";
840 }
841
842 if (__read_config($file, \$test_num)) {
843 $test_case = 1;
844 }
845
Steven Rostedta57419b2010-11-02 15:13:54 -0400846 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
847
848 next if ($skip);
849
Steven Rostedt2545eb62010-11-02 15:01:32 -0400850 my $lvalue = $1;
851 my $rvalue = $2;
852
Steven Rostedta57419b2010-11-02 15:13:54 -0400853 if (!$default &&
854 ($lvalue eq "NUM_TESTS" ||
855 $lvalue eq "LOG_FILE" ||
856 $lvalue eq "CLEAR_LOG")) {
857 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400858 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400859
860 if ($lvalue eq "NUM_TESTS") {
861 if ($test_num) {
862 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
863 }
864 if (!$default) {
865 die "$name: $.: NUM_TESTS must be set in default section\n";
866 }
867 $num_tests_set = 1;
868 }
869
870 if ($default || $lvalue =~ /\[\d+\]$/) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400871 set_value($lvalue, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400872 } else {
873 my $val = "$lvalue\[$test_num\]";
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400874 set_value($val, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400875
876 if ($repeat > 1) {
877 $repeats{$val} = $repeat;
878 }
879 }
Steven Rostedt77d942c2011-05-20 13:36:58 -0400880 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
881 next if ($skip);
882
883 my $lvalue = $1;
884 my $rvalue = $2;
885
886 # process config variables.
887 # Config variables are only active while reading the
888 # config and can be defined anywhere. They also ignore
889 # TEST_START and DEFAULTS, but are skipped if they are in
890 # on of these sections that have SKIP defined.
891 # The save variable can be
892 # defined multiple times and the new one simply overrides
893 # the prevous one.
894 set_variable($lvalue, $rvalue);
895
Steven Rostedta57419b2010-11-02 15:13:54 -0400896 } else {
897 die "$name: $.: Garbage found in config\n$_";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400898 }
899 }
900
Steven Rostedta57419b2010-11-02 15:13:54 -0400901 if ($test_num) {
902 $test_num += $repeat - 1;
903 $opt{"NUM_TESTS"} = $test_num;
904 }
905
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400906 close($in);
907
908 $$current_test_num = $test_num;
909
910 return $test_case;
911}
912
Steven Rostedtc4261d02011-11-23 13:41:18 -0500913sub get_test_case {
914 print "What test case would you like to run?\n";
915 print " (build, install or boot)\n";
916 print " Other tests are available but require editing the config file\n";
917 my $ans = <STDIN>;
918 chomp $ans;
919 $default{"TEST_TYPE"} = $ans;
920}
921
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400922sub read_config {
923 my ($config) = @_;
924
925 my $test_case;
926 my $test_num = 0;
927
928 $test_case = __read_config $config, \$test_num;
929
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500930 # make sure we have all mandatory configs
931 get_ktest_configs;
932
Steven Rostedt0df213c2011-06-14 20:51:37 -0400933 # was a test specified?
934 if (!$test_case) {
935 print "No test case specified.\n";
Steven Rostedtc4261d02011-11-23 13:41:18 -0500936 get_test_case;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400937 }
938
Steven Rostedta75fece2010-11-02 14:58:27 -0400939 # set any defaults
940
941 foreach my $default (keys %default) {
942 if (!defined($opt{$default})) {
943 $opt{$default} = $default{$default};
944 }
945 }
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500946
947 if ($opt{"IGNORE_UNUSED"} == 1) {
948 return;
949 }
950
951 my %not_used;
952
953 # check if there are any stragglers (typos?)
954 foreach my $option (keys %opt) {
955 my $op = $option;
956 # remove per test labels.
957 $op =~ s/\[.*\]//;
958 if (!exists($option_map{$op}) &&
959 !exists($default{$op}) &&
960 !exists($used_options{$op})) {
961 $not_used{$op} = 1;
962 }
963 }
964
965 if (%not_used) {
966 my $s = "s are";
967 $s = " is" if (keys %not_used == 1);
968 print "The following option$s not used; could be a typo:\n";
969 foreach my $option (keys %not_used) {
970 print "$option\n";
971 }
972 print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
973 if (!read_yn "Do you want to continue?") {
974 exit -1;
975 }
976 }
Steven Rostedt2545eb62010-11-02 15:01:32 -0400977}
978
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400979sub __eval_option {
980 my ($option, $i) = @_;
981
982 # Add space to evaluate the character before $
983 $option = " $option";
984 my $retval = "";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530985 my $repeated = 0;
986 my $parent = 0;
987
988 foreach my $test (keys %repeat_tests) {
989 if ($i >= $test &&
990 $i < $test + $repeat_tests{$test}) {
991
992 $repeated = 1;
993 $parent = $test;
994 last;
995 }
996 }
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400997
998 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
999 my $start = $1;
1000 my $var = $2;
1001 my $end = $3;
1002
1003 # Append beginning of line
1004 $retval = "$retval$start";
1005
1006 # If the iteration option OPT[$i] exists, then use that.
1007 # otherwise see if the default OPT (without [$i]) exists.
1008
1009 my $o = "$var\[$i\]";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301010 my $parento = "$var\[$parent\]";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001011
1012 if (defined($opt{$o})) {
1013 $o = $opt{$o};
1014 $retval = "$retval$o";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301015 } elsif ($repeated && defined($opt{$parento})) {
1016 $o = $opt{$parento};
1017 $retval = "$retval$o";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001018 } elsif (defined($opt{$var})) {
1019 $o = $opt{$var};
1020 $retval = "$retval$o";
1021 } else {
1022 $retval = "$retval\$\{$var\}";
1023 }
1024
1025 $option = $end;
1026 }
1027
1028 $retval = "$retval$option";
1029
1030 $retval =~ s/^ //;
1031
1032 return $retval;
1033}
1034
1035sub eval_option {
1036 my ($option, $i) = @_;
1037
1038 my $prev = "";
1039
1040 # Since an option can evaluate to another option,
1041 # keep iterating until we do not evaluate any more
1042 # options.
1043 my $r = 0;
1044 while ($prev ne $option) {
1045 # Check for recursive evaluations.
1046 # 100 deep should be more than enough.
1047 if ($r++ > 100) {
1048 die "Over 100 evaluations accurred with $option\n" .
1049 "Check for recursive variables\n";
1050 }
1051 $prev = $option;
1052 $option = __eval_option($option, $i);
1053 }
1054
1055 return $option;
1056}
1057
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001058sub _logit {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001059 if (defined($opt{"LOG_FILE"})) {
1060 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
1061 print OUT @_;
1062 close(OUT);
1063 }
1064}
1065
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001066sub logit {
1067 if (defined($opt{"LOG_FILE"})) {
1068 _logit @_;
1069 } else {
1070 print @_;
1071 }
1072}
1073
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001074sub doprint {
1075 print @_;
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001076 _logit @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001077}
1078
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001079sub run_command;
Andrew Jones2728be42011-08-12 15:32:05 +02001080sub start_monitor;
1081sub end_monitor;
1082sub wait_for_monitor;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001083
1084sub reboot {
Andrew Jones2728be42011-08-12 15:32:05 +02001085 my ($time) = @_;
1086
Steven Rostedt2b803362011-09-30 18:00:23 -04001087 if (defined($time)) {
1088 start_monitor;
1089 # flush out current monitor
1090 # May contain the reboot success line
1091 wait_for_monitor 1;
1092 }
1093
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001094 # try to reboot normally
Steven Rostedte48c5292010-11-02 14:35:37 -04001095 if (run_command $reboot) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001096 if (defined($powercycle_after_reboot)) {
1097 sleep $powercycle_after_reboot;
1098 run_command "$power_cycle";
1099 }
1100 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001101 # nope? power cycle it.
Steven Rostedta75fece2010-11-02 14:58:27 -04001102 run_command "$power_cycle";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001103 }
Andrew Jones2728be42011-08-12 15:32:05 +02001104
1105 if (defined($time)) {
Steven Rostedt2b803362011-09-30 18:00:23 -04001106 wait_for_monitor($time, $reboot_success_line);
Andrew Jones2728be42011-08-12 15:32:05 +02001107 end_monitor;
1108 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001109}
1110
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001111sub reboot_to_good {
1112 my ($time) = @_;
1113
1114 if (defined($switch_to_good)) {
1115 run_command $switch_to_good;
1116 return;
1117 }
1118
1119 reboot $time;
1120}
1121
Steven Rostedt576f6272010-11-02 14:58:38 -04001122sub do_not_reboot {
1123 my $i = $iteration;
1124
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001125 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -04001126 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1127 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1128}
1129
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001130sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001131 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001132
Steven Rostedt576f6272010-11-02 14:58:38 -04001133 my $i = $iteration;
1134
1135 if ($reboot_on_error && !do_not_reboot) {
1136
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001137 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001138 reboot_to_good;
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001139
Steven Rostedta75fece2010-11-02 14:58:27 -04001140 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001141 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001142 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001143 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001144
Steven Rostedtf80802c2011-03-07 13:18:47 -05001145 if (defined($opt{"LOG_FILE"})) {
1146 print " See $opt{LOG_FILE} for more info.\n";
1147 }
1148
Steven Rostedt576f6272010-11-02 14:58:38 -04001149 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001150}
1151
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001152sub open_console {
1153 my ($fp) = @_;
1154
1155 my $flags;
1156
Steven Rostedta75fece2010-11-02 14:58:27 -04001157 my $pid = open($fp, "$console|") or
1158 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001159
1160 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001161 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001162 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001163 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001164
1165 return $pid;
1166}
1167
1168sub close_console {
1169 my ($fp, $pid) = @_;
1170
1171 doprint "kill child process $pid\n";
1172 kill 2, $pid;
1173
1174 print "closing!\n";
1175 close($fp);
1176}
1177
1178sub start_monitor {
1179 if ($monitor_cnt++) {
1180 return;
1181 }
1182 $monitor_fp = \*MONFD;
1183 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001184
1185 return;
1186
1187 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001188}
1189
1190sub end_monitor {
1191 if (--$monitor_cnt) {
1192 return;
1193 }
1194 close_console($monitor_fp, $monitor_pid);
1195}
1196
1197sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001198 my ($time, $stop) = @_;
1199 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001200 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001201 my $booted = 0;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001202
Steven Rostedta75fece2010-11-02 14:58:27 -04001203 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001204
1205 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001206 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001207 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001208 last if (!defined($line));
1209 print "$line";
1210 $full_line .= $line;
1211
1212 if (defined($stop) && $full_line =~ /$stop/) {
1213 doprint "wait for monitor detected $stop\n";
1214 $booted = 1;
1215 }
1216
1217 if ($line =~ /\n/) {
1218 $full_line = "";
1219 }
1220 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001221 print "** Monitor flushed **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001222}
1223
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301224sub save_logs {
1225 my ($result, $basedir) = @_;
1226 my @t = localtime;
1227 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1228 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1229
1230 my $type = $build_type;
1231 if ($type =~ /useconfig/) {
1232 $type = "useconfig";
1233 }
1234
1235 my $dir = "$machine-$test_type-$type-$result-$date";
1236
1237 $dir = "$basedir/$dir";
1238
1239 if (!-d $dir) {
1240 mkpath($dir) or
1241 die "can't create $dir";
1242 }
1243
1244 my %files = (
1245 "config" => $output_config,
1246 "buildlog" => $buildlog,
1247 "dmesg" => $dmesg,
1248 "testlog" => $testlog,
1249 );
1250
1251 while (my ($name, $source) = each(%files)) {
1252 if (-f "$source") {
1253 cp "$source", "$dir/$name" or
1254 die "failed to copy $source";
1255 }
1256 }
1257
1258 doprint "*** Saved info to $dir ***\n";
1259}
1260
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001261sub fail {
1262
Steven Rostedta75fece2010-11-02 14:58:27 -04001263 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001264 dodie @_;
1265 }
1266
Steven Rostedta75fece2010-11-02 14:58:27 -04001267 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001268
Steven Rostedt576f6272010-11-02 14:58:38 -04001269 my $i = $iteration;
1270
Steven Rostedta75fece2010-11-02 14:58:27 -04001271 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001272 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001273 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001274 reboot_to_good $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001275 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001276
Steven Rostedt9064af52011-06-13 10:38:48 -04001277 my $name = "";
1278
1279 if (defined($test_name)) {
1280 $name = " ($test_name)";
1281 }
1282
Steven Rostedt576f6272010-11-02 14:58:38 -04001283 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1284 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001285 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001286 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1287 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001288
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301289 if (defined($store_failures)) {
1290 save_logs "fail", $store_failures;
1291 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001292
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001293 return 1;
1294}
1295
Steven Rostedt2545eb62010-11-02 15:01:32 -04001296sub run_command {
1297 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001298 my $dolog = 0;
1299 my $dord = 0;
1300 my $pid;
1301
Steven Rostedte48c5292010-11-02 14:35:37 -04001302 $command =~ s/\$SSH_USER/$ssh_user/g;
1303 $command =~ s/\$MACHINE/$machine/g;
1304
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001305 doprint("$command ... ");
1306
1307 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001308 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001309
1310 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001311 open(LOG, ">>$opt{LOG_FILE}") or
1312 dodie "failed to write to log";
1313 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001314 }
1315
1316 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001317 open (RD, ">$redirect") or
1318 dodie "failed to write to redirect $redirect";
1319 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001320 }
1321
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001322 while (<CMD>) {
1323 print LOG if ($dolog);
1324 print RD if ($dord);
1325 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001326
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001327 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001328 my $failed = $?;
1329
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001330 close(CMD);
1331 close(LOG) if ($dolog);
1332 close(RD) if ($dord);
1333
Steven Rostedt2545eb62010-11-02 15:01:32 -04001334 if ($failed) {
1335 doprint "FAILED!\n";
1336 } else {
1337 doprint "SUCCESS\n";
1338 }
1339
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001340 return !$failed;
1341}
1342
Steven Rostedte48c5292010-11-02 14:35:37 -04001343sub run_ssh {
1344 my ($cmd) = @_;
1345 my $cp_exec = $ssh_exec;
1346
1347 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1348 return run_command "$cp_exec";
1349}
1350
1351sub run_scp {
1352 my ($src, $dst) = @_;
1353 my $cp_scp = $scp_to_target;
1354
1355 $cp_scp =~ s/\$SRC_FILE/$src/g;
1356 $cp_scp =~ s/\$DST_FILE/$dst/g;
1357
1358 return run_command "$cp_scp";
1359}
1360
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001361sub get_grub_index {
1362
Steven Rostedta75fece2010-11-02 14:58:27 -04001363 if ($reboot_type ne "grub") {
1364 return;
1365 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001366 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001367
1368 doprint "Find grub menu ... ";
1369 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001370
1371 my $ssh_grub = $ssh_exec;
1372 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1373
1374 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001375 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001376
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001377 my $found = 0;
1378
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001379 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001380 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001381 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001382 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001383 last;
1384 } elsif (/^\s*title\s/) {
1385 $grub_number++;
1386 }
1387 }
1388 close(IN);
1389
Steven Rostedta75fece2010-11-02 14:58:27 -04001390 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001391 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001392 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001393}
1394
Steven Rostedt2545eb62010-11-02 15:01:32 -04001395sub wait_for_input
1396{
1397 my ($fp, $time) = @_;
1398 my $rin;
1399 my $ready;
1400 my $line;
1401 my $ch;
1402
1403 if (!defined($time)) {
1404 $time = $timeout;
1405 }
1406
1407 $rin = '';
1408 vec($rin, fileno($fp), 1) = 1;
1409 $ready = select($rin, undef, undef, $time);
1410
1411 $line = "";
1412
1413 # try to read one char at a time
1414 while (sysread $fp, $ch, 1) {
1415 $line .= $ch;
1416 last if ($ch eq "\n");
1417 }
1418
1419 if (!length($line)) {
1420 return undef;
1421 }
1422
1423 return $line;
1424}
1425
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001426sub reboot_to {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001427 if (defined($switch_to_test)) {
1428 run_command $switch_to_test;
1429 }
1430
Steven Rostedta75fece2010-11-02 14:58:27 -04001431 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001432 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
1433 reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -04001434 return;
1435 }
1436
1437 run_command "$reboot_script";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001438}
1439
Steven Rostedta57419b2010-11-02 15:13:54 -04001440sub get_sha1 {
1441 my ($commit) = @_;
1442
1443 doprint "git rev-list --max-count=1 $commit ... ";
1444 my $sha1 = `git rev-list --max-count=1 $commit`;
1445 my $ret = $?;
1446
1447 logit $sha1;
1448
1449 if ($ret) {
1450 doprint "FAILED\n";
1451 dodie "Failed to get git $commit";
1452 }
1453
1454 print "SUCCESS\n";
1455
1456 chomp $sha1;
1457
1458 return $sha1;
1459}
1460
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001461sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001462 my $booted = 0;
1463 my $bug = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001464 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001465 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001466
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001467 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001468
1469 my $line;
1470 my $full_line = "";
1471
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001472 open(DMESG, "> $dmesg") or
1473 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001474
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001475 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001476
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001477 my $success_start;
1478 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001479 my $monitor_start = time;
1480 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001481 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001482
Steven Rostedt2d01b262011-03-08 09:47:54 -05001483 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001484
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001485 if ($bug && defined($stop_after_failure) &&
1486 $stop_after_failure >= 0) {
1487 my $time = $stop_after_failure - (time - $failure_start);
1488 $line = wait_for_input($monitor_fp, $time);
1489 if (!defined($line)) {
1490 doprint "bug timed out after $booted_timeout seconds\n";
1491 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1492 last;
1493 }
1494 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001495 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001496 if (!defined($line)) {
1497 my $s = $booted_timeout == 1 ? "" : "s";
1498 doprint "Successful boot found: break after $booted_timeout second$s\n";
1499 last;
1500 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001501 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001502 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001503 if (!defined($line)) {
1504 my $s = $timeout == 1 ? "" : "s";
1505 doprint "Timed out after $timeout second$s\n";
1506 last;
1507 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001508 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001509
Steven Rostedt2545eb62010-11-02 15:01:32 -04001510 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001511 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001512
1513 # we are not guaranteed to get a full line
1514 $full_line .= $line;
1515
Steven Rostedta75fece2010-11-02 14:58:27 -04001516 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001517 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001518 $success_start = time;
1519 }
1520
1521 if ($booted && defined($stop_after_success) &&
1522 $stop_after_success >= 0) {
1523 my $now = time;
1524 if ($now - $success_start >= $stop_after_success) {
1525 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1526 last;
1527 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001528 }
1529
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001530 if ($full_line =~ /\[ backtrace testing \]/) {
1531 $skip_call_trace = 1;
1532 }
1533
Steven Rostedt2545eb62010-11-02 15:01:32 -04001534 if ($full_line =~ /call trace:/i) {
Steven Rostedt46519202011-03-08 09:40:31 -05001535 if (!$bug && !$skip_call_trace) {
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001536 $bug = 1;
1537 $failure_start = time;
1538 }
1539 }
1540
1541 if ($bug && defined($stop_after_failure) &&
1542 $stop_after_failure >= 0) {
1543 my $now = time;
1544 if ($now - $failure_start >= $stop_after_failure) {
1545 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1546 last;
1547 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001548 }
1549
1550 if ($full_line =~ /\[ end of backtrace testing \]/) {
1551 $skip_call_trace = 0;
1552 }
1553
1554 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001555 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001556 $bug = 1;
1557 }
1558
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001559 # Detect triple faults by testing the banner
1560 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1561 if ($1 eq $version) {
1562 $version_found = 1;
1563 } elsif ($version_found && $detect_triplefault) {
1564 # We already booted into the kernel we are testing,
1565 # but now we booted into another kernel?
1566 # Consider this a triple fault.
1567 doprint "Aleady booted in Linux kernel $version, but now\n";
1568 doprint "we booted into Linux kernel $1.\n";
1569 doprint "Assuming that this is a triple fault.\n";
1570 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1571 last;
1572 }
1573 }
1574
Steven Rostedt2545eb62010-11-02 15:01:32 -04001575 if ($line =~ /\n/) {
1576 $full_line = "";
1577 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001578
1579 if ($stop_test_after > 0 && !$booted && !$bug) {
1580 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001581 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001582 $done = 1;
1583 }
1584 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001585 }
1586
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001587 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001588
Steven Rostedt2545eb62010-11-02 15:01:32 -04001589 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001590 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001591 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001592 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001593
Steven Rostedta75fece2010-11-02 14:58:27 -04001594 if (!$booted) {
1595 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001596 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001597 }
1598
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001599 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001600}
1601
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001602sub eval_kernel_version {
1603 my ($option) = @_;
1604
1605 $option =~ s/\$KERNEL_VERSION/$version/g;
1606
1607 return $option;
1608}
1609
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001610sub do_post_install {
1611
1612 return if (!defined($post_install));
1613
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001614 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001615 run_command "$cp_post_install" or
1616 dodie "Failed to run post install";
1617}
1618
Steven Rostedt2545eb62010-11-02 15:01:32 -04001619sub install {
1620
Steven Rostedte0a87422011-09-30 17:50:48 -04001621 return if ($no_install);
1622
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001623 my $cp_target = eval_kernel_version $target_image;
1624
1625 run_scp "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001626 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001627
1628 my $install_mods = 0;
1629
1630 # should we process modules?
1631 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001632 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001633 while (<IN>) {
1634 if (/CONFIG_MODULES(=y)?/) {
1635 $install_mods = 1 if (defined($1));
1636 last;
1637 }
1638 }
1639 close(IN);
1640
1641 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001642 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001643 doprint "No modules needed\n";
1644 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001645 }
1646
Steven Rostedta75fece2010-11-02 14:58:27 -04001647 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001648 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001649
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001650 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001651 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001652
Steven Rostedte48c5292010-11-02 14:35:37 -04001653 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001654 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001655
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001656 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001657 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001658 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001659
Steven Rostedte48c5292010-11-02 14:35:37 -04001660 run_scp "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001661 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001662
Steven Rostedta75fece2010-11-02 14:58:27 -04001663 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001664
Steven Rostedte7b13442011-06-14 20:44:36 -04001665 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001666 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001667
Steven Rostedte48c5292010-11-02 14:35:37 -04001668 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001669
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001670 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001671}
1672
Steven Rostedtddf607e2011-06-14 20:49:13 -04001673sub get_version {
1674 # get the release name
1675 doprint "$make kernelrelease ... ";
1676 $version = `$make kernelrelease | tail -1`;
1677 chomp($version);
1678 doprint "$version\n";
1679}
1680
1681sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001682 # Make sure the stable kernel has finished booting
1683 start_monitor;
1684 wait_for_monitor 5;
1685 end_monitor;
1686
Steven Rostedtddf607e2011-06-14 20:49:13 -04001687 get_grub_index;
1688 get_version;
1689 install;
1690
1691 start_monitor;
1692 return monitor;
1693}
1694
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001695sub check_buildlog {
1696 my ($patch) = @_;
1697
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001698 my @files = `git show $patch | diffstat -l`;
1699
1700 open(IN, "git show $patch |") or
1701 dodie "failed to show $patch";
1702 while (<IN>) {
1703 if (m,^--- a/(.*),) {
1704 chomp $1;
1705 $files[$#files] = $1;
1706 }
1707 }
1708 close(IN);
1709
1710 open(IN, $buildlog) or dodie "Can't open $buildlog";
1711 while (<IN>) {
1712 if (/^\s*(.*?):.*(warning|error)/) {
1713 my $err = $1;
1714 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001715 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001716 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001717 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001718 }
1719 }
1720 }
1721 }
1722 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001723
1724 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001725}
1726
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001727sub apply_min_config {
1728 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05001729
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001730 # Read the config file and remove anything that
1731 # is in the force_config hash (from minconfig and others)
1732 # then add the force config back.
1733
1734 doprint "Applying minimum configurations into $output_config.new\n";
1735
1736 open (OUT, ">$outconfig") or
1737 dodie "Can't create $outconfig";
1738
1739 if (-f $output_config) {
1740 open (IN, $output_config) or
1741 dodie "Failed to open $output_config";
1742 while (<IN>) {
1743 if (/^(# )?(CONFIG_[^\s=]*)/) {
1744 next if (defined($force_config{$2}));
1745 }
1746 print OUT;
1747 }
1748 close IN;
1749 }
1750 foreach my $config (keys %force_config) {
1751 print OUT "$force_config{$config}\n";
1752 }
1753 close OUT;
1754
1755 run_command "mv $outconfig $output_config";
1756}
1757
1758sub make_oldconfig {
1759
Steven Rostedt4c4ab122011-07-15 21:16:17 -04001760 my @force_list = keys %force_config;
1761
1762 if ($#force_list >= 0) {
1763 apply_min_config;
1764 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001765
1766 if (!run_command "$make oldnoconfig") {
Steven Rostedt612b9e92011-03-07 13:27:43 -05001767 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1768 # try a yes '' | oldconfig
1769 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001770 run_command "yes '' | $make oldconfig" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001771 dodie "failed make config oldconfig";
1772 }
1773}
1774
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001775# read a config file and use this to force new configs.
1776sub load_force_config {
1777 my ($config) = @_;
1778
1779 open(IN, $config) or
1780 dodie "failed to read $config";
1781 while (<IN>) {
1782 chomp;
1783 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1784 $force_config{$1} = $_;
1785 } elsif (/^# (CONFIG_\S*) is not set/) {
1786 $force_config{$1} = $_;
1787 }
1788 }
1789 close IN;
1790}
1791
Steven Rostedt2545eb62010-11-02 15:01:32 -04001792sub build {
1793 my ($type) = @_;
1794
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001795 unlink $buildlog;
1796
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001797 # Failed builds should not reboot the target
1798 my $save_no_reboot = $no_reboot;
1799 $no_reboot = 1;
1800
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001801 if (defined($pre_build)) {
1802 my $ret = run_command $pre_build;
1803 if (!$ret && defined($pre_build_die) &&
1804 $pre_build_die) {
1805 dodie "failed to pre_build\n";
1806 }
1807 }
1808
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001809 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001810 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001811 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001812
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001813 $type = "oldconfig";
1814 }
1815
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001816 # old config can ask questions
1817 if ($type eq "oldconfig") {
Steven Rostedt9386c6a2010-11-08 16:35:48 -05001818 $type = "oldnoconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001819
1820 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001821 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001822
Andrew Jones13488232011-08-12 15:32:04 +02001823 if (!$noclean) {
1824 run_command "mv $output_config $outputdir/config_temp" or
1825 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001826
Andrew Jones13488232011-08-12 15:32:04 +02001827 run_command "$make mrproper" or dodie "make mrproper";
1828
1829 run_command "mv $outputdir/config_temp $output_config" or
1830 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001831 }
1832
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001833 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001834 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001835 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001836 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001837 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001838
1839 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04001840 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1841 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001842 close(OUT);
1843
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001844 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001845 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001846 }
1847
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001848 if ($type ne "oldnoconfig") {
1849 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001850 dodie "failed make config";
1851 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001852 # Run old config regardless, to enforce min configurations
1853 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001854
Steven Rostedta75fece2010-11-02 14:58:27 -04001855 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001856 my $build_ret = run_command "$make $build_options";
1857 undef $redirect;
1858
1859 if (defined($post_build)) {
1860 my $ret = run_command $post_build;
1861 if (!$ret && defined($post_build_die) &&
1862 $post_build_die) {
1863 dodie "failed to post_build\n";
1864 }
1865 }
1866
1867 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001868 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001869 if ($in_bisect) {
1870 $no_reboot = $save_no_reboot;
1871 return 0;
1872 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001873 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001874 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001875
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001876 $no_reboot = $save_no_reboot;
1877
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001878 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001879}
1880
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001881sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04001882 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001883 if (defined($poweroff_after_halt)) {
1884 sleep $poweroff_after_halt;
1885 run_command "$power_off";
1886 }
1887 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001888 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04001889 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001890 }
1891}
1892
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001893sub success {
1894 my ($i) = @_;
1895
Steven Rostedte48c5292010-11-02 14:35:37 -04001896 $successes++;
1897
Steven Rostedt9064af52011-06-13 10:38:48 -04001898 my $name = "";
1899
1900 if (defined($test_name)) {
1901 $name = " ($test_name)";
1902 }
1903
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001904 doprint "\n\n*******************************************\n";
1905 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001906 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001907 doprint "*******************************************\n";
1908 doprint "*******************************************\n";
1909
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301910 if (defined($store_successes)) {
1911 save_logs "success", $store_successes;
1912 }
1913
Steven Rostedt576f6272010-11-02 14:58:38 -04001914 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001915 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001916 reboot_to_good $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001917 }
1918}
1919
Steven Rostedtc960bb92011-03-08 09:22:39 -05001920sub answer_bisect {
1921 for (;;) {
1922 doprint "Pass or fail? [p/f]";
1923 my $ans = <STDIN>;
1924 chomp $ans;
1925 if ($ans eq "p" || $ans eq "P") {
1926 return 1;
1927 } elsif ($ans eq "f" || $ans eq "F") {
1928 return 0;
1929 } else {
1930 print "Please answer 'P' or 'F'\n";
1931 }
1932 }
1933}
1934
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001935sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001936 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001937
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001938 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04001939 $reboot_on_error = 0;
1940 $poweroff_on_error = 0;
1941 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001942
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301943 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001944 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301945 undef $redirect;
1946
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001947 exit $failed;
1948}
1949
1950my $child_done;
1951
1952sub child_finished {
1953 $child_done = 1;
1954}
1955
1956sub do_run_test {
1957 my $child_pid;
1958 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001959 my $line;
1960 my $full_line;
1961 my $bug = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001962
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001963 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001964
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001965 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001966
1967 $child_done = 0;
1968
1969 $SIG{CHLD} = qw(child_finished);
1970
1971 $child_pid = fork;
1972
1973 child_run_test if (!$child_pid);
1974
1975 $full_line = "";
1976
1977 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001978 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001979 if (defined($line)) {
1980
1981 # we are not guaranteed to get a full line
1982 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05001983 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001984
1985 if ($full_line =~ /call trace:/i) {
1986 $bug = 1;
1987 }
1988
1989 if ($full_line =~ /Kernel panic -/) {
1990 $bug = 1;
1991 }
1992
1993 if ($line =~ /\n/) {
1994 $full_line = "";
1995 }
1996 }
1997 } while (!$child_done && !$bug);
1998
1999 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002000 my $failure_start = time;
2001 my $now;
2002 do {
2003 $line = wait_for_input($monitor_fp, 1);
2004 if (defined($line)) {
2005 doprint $line;
2006 }
2007 $now = time;
2008 if ($now - $failure_start >= $stop_after_failure) {
2009 last;
2010 }
2011 } while (defined($line));
2012
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002013 doprint "Detected kernel crash!\n";
2014 # kill the child with extreme prejudice
2015 kill 9, $child_pid;
2016 }
2017
2018 waitpid $child_pid, 0;
2019 $child_exit = $?;
2020
Steven Rostedtc5dacb82011-12-22 12:43:57 -05002021 if (!$bug && $in_bisect) {
2022 if (defined($bisect_ret_good)) {
2023 if ($child_exit == $bisect_ret_good) {
2024 return 1;
2025 }
2026 }
2027 if (defined($bisect_ret_skip)) {
2028 if ($child_exit == $bisect_ret_skip) {
2029 return -1;
2030 }
2031 }
2032 if (defined($bisect_ret_abort)) {
2033 if ($child_exit == $bisect_ret_abort) {
2034 fail "test abort" and return -2;
2035 }
2036 }
2037 if (defined($bisect_ret_bad)) {
2038 if ($child_exit == $bisect_ret_skip) {
2039 return 0;
2040 }
2041 }
2042 if (defined($bisect_ret_default)) {
2043 if ($bisect_ret_default eq "good") {
2044 return 1;
2045 } elsif ($bisect_ret_default eq "bad") {
2046 return 0;
2047 } elsif ($bisect_ret_default eq "skip") {
2048 return -1;
2049 } elsif ($bisect_ret_default eq "abort") {
2050 return -2;
2051 } else {
2052 fail "unknown default action: $bisect_ret_default"
2053 and return -2;
2054 }
2055 }
2056 }
2057
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002058 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002059 return 0 if $in_bisect;
2060 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002061 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002062 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002063}
2064
Steven Rostedta75fece2010-11-02 14:58:27 -04002065sub run_git_bisect {
2066 my ($command) = @_;
2067
2068 doprint "$command ... ";
2069
2070 my $output = `$command 2>&1`;
2071 my $ret = $?;
2072
2073 logit $output;
2074
2075 if ($ret) {
2076 doprint "FAILED\n";
2077 dodie "Failed to git bisect";
2078 }
2079
2080 doprint "SUCCESS\n";
2081 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2082 doprint "$1 [$2]\n";
2083 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002084 $bisect_bad_commit = $1;
Steven Rostedta75fece2010-11-02 14:58:27 -04002085 doprint "Found bad commit... $1\n";
2086 return 0;
2087 } else {
2088 # we already logged it, just print it now.
2089 print $output;
2090 }
2091
2092 return 1;
2093}
2094
Steven Rostedtc23dca72011-03-08 09:26:31 -05002095sub bisect_reboot {
2096 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002097 reboot_to_good $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05002098}
2099
2100# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05002101sub run_bisect_test {
2102 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002103
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002104 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002105 my $result;
2106 my $output;
2107 my $ret;
2108
Steven Rostedt0a05c762010-11-08 11:14:10 -05002109 $in_bisect = 1;
2110
2111 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002112
2113 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002114 if ($failed && $bisect_skip) {
2115 $in_bisect = 0;
2116 return -1;
2117 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002118 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002119
2120 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04002121 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002122
2123 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002124 if ($failed && $bisect_skip) {
2125 end_monitor;
2126 bisect_reboot;
2127 $in_bisect = 0;
2128 return -1;
2129 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002130 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002131
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002132 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002133 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002134 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002135 }
2136
2137 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002138 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002139 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002140 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002141 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04002142
2143 # reboot the box to a kernel we can ssh to
2144 if ($type ne "build") {
2145 bisect_reboot;
2146 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002147 $in_bisect = 0;
2148
2149 return $result;
2150}
2151
2152sub run_bisect {
2153 my ($type) = @_;
2154 my $buildtype = "oldconfig";
2155
2156 # We should have a minconfig to use?
2157 if (defined($minconfig)) {
2158 $buildtype = "useconfig:$minconfig";
2159 }
2160
2161 my $ret = run_bisect_test $type, $buildtype;
2162
Steven Rostedtc960bb92011-03-08 09:22:39 -05002163 if ($bisect_manual) {
2164 $ret = answer_bisect;
2165 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002166
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002167 # Are we looking for where it worked, not failed?
2168 if ($reverse_bisect) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002169 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002170 }
2171
Steven Rostedtc23dca72011-03-08 09:26:31 -05002172 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002173 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002174 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002175 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002176 } elsif ($bisect_skip) {
2177 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2178 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002179 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002180}
2181
Steven Rostedtdad98752011-11-22 20:48:57 -05002182sub update_bisect_replay {
2183 my $tmp_log = "$tmpdir/ktest_bisect_log";
2184 run_command "git bisect log > $tmp_log" or
2185 die "can't create bisect log";
2186 return $tmp_log;
2187}
2188
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002189sub bisect {
2190 my ($i) = @_;
2191
2192 my $result;
2193
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002194 die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2195 die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
2196 die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002197
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002198 my $good = $bisect_good;
2199 my $bad = $bisect_bad;
2200 my $type = $bisect_type;
2201 my $start = $bisect_start;
2202 my $replay = $bisect_replay;
2203 my $start_files = $bisect_files;
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002204
2205 if (defined($start_files)) {
2206 $start_files = " -- " . $start_files;
2207 } else {
2208 $start_files = "";
2209 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002210
Steven Rostedta57419b2010-11-02 15:13:54 -04002211 # convert to true sha1's
2212 $good = get_sha1($good);
2213 $bad = get_sha1($bad);
2214
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002215 if (defined($bisect_reverse) && $bisect_reverse == 1) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002216 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2217 $reverse_bisect = 1;
2218 } else {
2219 $reverse_bisect = 0;
2220 }
2221
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002222 # Can't have a test without having a test to run
2223 if ($type eq "test" && !defined($run_test)) {
2224 $type = "boot";
2225 }
2226
Steven Rostedtdad98752011-11-22 20:48:57 -05002227 # Check if a bisect was running
2228 my $bisect_start_file = "$builddir/.git/BISECT_START";
2229
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002230 my $check = $bisect_check;
Steven Rostedtdad98752011-11-22 20:48:57 -05002231 my $do_check = defined($check) && $check ne "0";
2232
2233 if ( -f $bisect_start_file ) {
2234 print "Bisect in progress found\n";
2235 if ($do_check) {
2236 print " If you say yes, then no checks of good or bad will be done\n";
2237 }
2238 if (defined($replay)) {
2239 print "** BISECT_REPLAY is defined in config file **";
2240 print " Ignore config option and perform new git bisect log?\n";
2241 if (read_ync " (yes, no, or cancel) ") {
2242 $replay = update_bisect_replay;
2243 $do_check = 0;
2244 }
2245 } elsif (read_yn "read git log and continue?") {
2246 $replay = update_bisect_replay;
2247 $do_check = 0;
2248 }
2249 }
2250
2251 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002252
2253 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002254 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002255
2256 if ($check ne "good") {
2257 doprint "TESTING BISECT BAD [$bad]\n";
2258 run_command "git checkout $bad" or
2259 die "Failed to checkout $bad";
2260
2261 $result = run_bisect $type;
2262
2263 if ($result ne "bad") {
2264 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2265 }
2266 }
2267
2268 if ($check ne "bad") {
2269 doprint "TESTING BISECT GOOD [$good]\n";
2270 run_command "git checkout $good" or
2271 die "Failed to checkout $good";
2272
2273 $result = run_bisect $type;
2274
2275 if ($result ne "good") {
2276 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2277 }
2278 }
2279
2280 # checkout where we started
2281 run_command "git checkout $head" or
2282 die "Failed to checkout $head";
2283 }
2284
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002285 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002286 dodie "could not start bisect";
2287
2288 run_command "git bisect good $good" or
2289 dodie "could not set bisect good to $good";
2290
2291 run_git_bisect "git bisect bad $bad" or
2292 dodie "could not set bisect bad to $bad";
2293
2294 if (defined($replay)) {
2295 run_command "git bisect replay $replay" or
2296 dodie "failed to run replay";
2297 }
2298
2299 if (defined($start)) {
2300 run_command "git checkout $start" or
2301 dodie "failed to checkout $start";
2302 }
2303
2304 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002305 do {
2306 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002307 $test = run_git_bisect "git bisect $result";
2308 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002309
2310 run_command "git bisect log" or
2311 dodie "could not capture git bisect log";
2312
2313 run_command "git bisect reset" or
2314 dodie "could not reset git bisect";
2315
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002316 doprint "Bad commit was [$bisect_bad_commit]\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002317
Steven Rostedt0a05c762010-11-08 11:14:10 -05002318 success $i;
2319}
2320
2321my %config_ignore;
2322my %config_set;
2323
2324my %config_list;
2325my %null_config;
2326
2327my %dependency;
2328
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002329sub assign_configs {
2330 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002331
2332 open (IN, $config)
2333 or dodie "Failed to read $config";
2334
2335 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002336 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002337 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002338 }
2339 }
2340
2341 close(IN);
2342}
2343
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002344sub process_config_ignore {
2345 my ($config) = @_;
2346
2347 assign_configs \%config_ignore, $config;
2348}
2349
Steven Rostedt0a05c762010-11-08 11:14:10 -05002350sub read_current_config {
2351 my ($config_ref) = @_;
2352
2353 %{$config_ref} = ();
2354 undef %{$config_ref};
2355
2356 my @key = keys %{$config_ref};
2357 if ($#key >= 0) {
2358 print "did not delete!\n";
2359 exit;
2360 }
2361 open (IN, "$output_config");
2362
2363 while (<IN>) {
2364 if (/^(CONFIG\S+)=(.*)/) {
2365 ${$config_ref}{$1} = $2;
2366 }
2367 }
2368 close(IN);
2369}
2370
2371sub get_dependencies {
2372 my ($config) = @_;
2373
2374 my $arr = $dependency{$config};
2375 if (!defined($arr)) {
2376 return ();
2377 }
2378
2379 my @deps = @{$arr};
2380
2381 foreach my $dep (@{$arr}) {
2382 print "ADD DEP $dep\n";
2383 @deps = (@deps, get_dependencies $dep);
2384 }
2385
2386 return @deps;
2387}
2388
2389sub create_config {
2390 my @configs = @_;
2391
2392 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2393
2394 foreach my $config (@configs) {
2395 print OUT "$config_set{$config}\n";
2396 my @deps = get_dependencies $config;
2397 foreach my $dep (@deps) {
2398 print OUT "$config_set{$dep}\n";
2399 }
2400 }
2401
2402 foreach my $config (keys %config_ignore) {
2403 print OUT "$config_ignore{$config}\n";
2404 }
2405 close(OUT);
2406
2407# exit;
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002408 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002409}
2410
2411sub compare_configs {
2412 my (%a, %b) = @_;
2413
2414 foreach my $item (keys %a) {
2415 if (!defined($b{$item})) {
2416 print "diff $item\n";
2417 return 1;
2418 }
2419 delete $b{$item};
2420 }
2421
2422 my @keys = keys %b;
2423 if ($#keys) {
2424 print "diff2 $keys[0]\n";
2425 }
2426 return -1 if ($#keys >= 0);
2427
2428 return 0;
2429}
2430
2431sub run_config_bisect_test {
2432 my ($type) = @_;
2433
2434 return run_bisect_test $type, "oldconfig";
2435}
2436
2437sub process_passed {
2438 my (%configs) = @_;
2439
2440 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2441 # Passed! All these configs are part of a good compile.
2442 # Add them to the min options.
2443 foreach my $config (keys %configs) {
2444 if (defined($config_list{$config})) {
2445 doprint " removing $config\n";
2446 $config_ignore{$config} = $config_list{$config};
2447 delete $config_list{$config};
2448 }
2449 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002450 doprint "config copied to $outputdir/config_good\n";
2451 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002452}
2453
2454sub process_failed {
2455 my ($config) = @_;
2456
2457 doprint "\n\n***************************************\n";
2458 doprint "Found bad config: $config\n";
2459 doprint "***************************************\n\n";
2460}
2461
2462sub run_config_bisect {
2463
2464 my @start_list = keys %config_list;
2465
2466 if ($#start_list < 0) {
2467 doprint "No more configs to test!!!\n";
2468 return -1;
2469 }
2470
2471 doprint "***** RUN TEST ***\n";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002472 my $type = $config_bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002473 my $ret;
2474 my %current_config;
2475
2476 my $count = $#start_list + 1;
2477 doprint " $count configs to test\n";
2478
2479 my $half = int($#start_list / 2);
2480
2481 do {
2482 my @tophalf = @start_list[0 .. $half];
2483
2484 create_config @tophalf;
2485 read_current_config \%current_config;
2486
2487 $count = $#tophalf + 1;
2488 doprint "Testing $count configs\n";
2489 my $found = 0;
2490 # make sure we test something
2491 foreach my $config (@tophalf) {
2492 if (defined($current_config{$config})) {
2493 logit " $config\n";
2494 $found = 1;
2495 }
2496 }
2497 if (!$found) {
2498 # try the other half
2499 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002500 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedt0a05c762010-11-08 11:14:10 -05002501 create_config @tophalf;
2502 read_current_config \%current_config;
2503 foreach my $config (@tophalf) {
2504 if (defined($current_config{$config})) {
2505 logit " $config\n";
2506 $found = 1;
2507 }
2508 }
2509 if (!$found) {
2510 doprint "Failed: Can't make new config with current configs\n";
2511 foreach my $config (@start_list) {
2512 doprint " CONFIG: $config\n";
2513 }
2514 return -1;
2515 }
2516 $count = $#tophalf + 1;
2517 doprint "Testing $count configs\n";
2518 }
2519
2520 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002521 if ($bisect_manual) {
2522 $ret = answer_bisect;
2523 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002524 if ($ret) {
2525 process_passed %current_config;
2526 return 0;
2527 }
2528
2529 doprint "This config had a failure.\n";
2530 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002531 doprint "config copied to $outputdir/config_bad\n";
2532 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002533
2534 # A config exists in this group that was bad.
2535 foreach my $config (keys %config_list) {
2536 if (!defined($current_config{$config})) {
2537 doprint " removing $config\n";
2538 delete $config_list{$config};
2539 }
2540 }
2541
2542 @start_list = @tophalf;
2543
2544 if ($#start_list == 0) {
2545 process_failed $start_list[0];
2546 return 1;
2547 }
2548
2549 # remove half the configs we are looking at and see if
2550 # they are good.
2551 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002552 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002553
Steven Rostedtc960bb92011-03-08 09:22:39 -05002554 # we found a single config, try it again unless we are running manually
2555
2556 if ($bisect_manual) {
2557 process_failed $start_list[0];
2558 return 1;
2559 }
2560
Steven Rostedt0a05c762010-11-08 11:14:10 -05002561 my @tophalf = @start_list[0 .. 0];
2562
2563 $ret = run_config_bisect_test $type;
2564 if ($ret) {
2565 process_passed %current_config;
2566 return 0;
2567 }
2568
2569 process_failed $start_list[0];
2570 return 1;
2571}
2572
2573sub config_bisect {
2574 my ($i) = @_;
2575
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002576 my $start_config = $config_bisect;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002577
2578 my $tmpconfig = "$tmpdir/use_config";
2579
Steven Rostedt30f75da2011-06-13 10:35:35 -04002580 if (defined($config_bisect_good)) {
2581 process_config_ignore $config_bisect_good;
2582 }
2583
Steven Rostedt0a05c762010-11-08 11:14:10 -05002584 # Make the file with the bad config and the min config
2585 if (defined($minconfig)) {
2586 # read the min config for things to ignore
2587 run_command "cp $minconfig $tmpconfig" or
2588 dodie "failed to copy $minconfig to $tmpconfig";
2589 } else {
2590 unlink $tmpconfig;
2591 }
2592
Steven Rostedt0a05c762010-11-08 11:14:10 -05002593 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002594 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002595 process_config_ignore $tmpconfig;
2596 }
2597
2598 # now process the start config
2599 run_command "cp $start_config $output_config" or
2600 dodie "failed to copy $start_config to $output_config";
2601
2602 # read directly what we want to check
2603 my %config_check;
2604 open (IN, $output_config)
2605 or dodie "faied to open $output_config";
2606
2607 while (<IN>) {
2608 if (/^((CONFIG\S*)=.*)/) {
2609 $config_check{$2} = $1;
2610 }
2611 }
2612 close(IN);
2613
Steven Rostedt250bae82011-07-15 22:05:59 -04002614 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002615 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002616
2617 # check to see what we lost (or gained)
2618 open (IN, $output_config)
2619 or dodie "Failed to read $start_config";
2620
2621 my %removed_configs;
2622 my %added_configs;
2623
2624 while (<IN>) {
2625 if (/^((CONFIG\S*)=.*)/) {
2626 # save off all options
2627 $config_set{$2} = $1;
2628 if (defined($config_check{$2})) {
2629 if (defined($config_ignore{$2})) {
2630 $removed_configs{$2} = $1;
2631 } else {
2632 $config_list{$2} = $1;
2633 }
2634 } elsif (!defined($config_ignore{$2})) {
2635 $added_configs{$2} = $1;
2636 $config_list{$2} = $1;
2637 }
2638 }
2639 }
2640 close(IN);
2641
2642 my @confs = keys %removed_configs;
2643 if ($#confs >= 0) {
2644 doprint "Configs overridden by default configs and removed from check:\n";
2645 foreach my $config (@confs) {
2646 doprint " $config\n";
2647 }
2648 }
2649 @confs = keys %added_configs;
2650 if ($#confs >= 0) {
2651 doprint "Configs appearing in make oldconfig and added:\n";
2652 foreach my $config (@confs) {
2653 doprint " $config\n";
2654 }
2655 }
2656
2657 my %config_test;
2658 my $once = 0;
2659
2660 # Sometimes kconfig does weird things. We must make sure
2661 # that the config we autocreate has everything we need
2662 # to test, otherwise we may miss testing configs, or
2663 # may not be able to create a new config.
2664 # Here we create a config with everything set.
2665 create_config (keys %config_list);
2666 read_current_config \%config_test;
2667 foreach my $config (keys %config_list) {
2668 if (!defined($config_test{$config})) {
2669 if (!$once) {
2670 $once = 1;
2671 doprint "Configs not produced by kconfig (will not be checked):\n";
2672 }
2673 doprint " $config\n";
2674 delete $config_list{$config};
2675 }
2676 }
2677 my $ret;
2678 do {
2679 $ret = run_config_bisect;
2680 } while (!$ret);
2681
2682 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002683
2684 success $i;
2685}
2686
Steven Rostedt27d934b2011-05-20 09:18:18 -04002687sub patchcheck_reboot {
2688 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002689 reboot_to_good $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04002690}
2691
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002692sub patchcheck {
2693 my ($i) = @_;
2694
2695 die "PATCHCHECK_START[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002696 if (!defined($patchcheck_start));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002697 die "PATCHCHECK_TYPE[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002698 if (!defined($patchcheck_type));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002699
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002700 my $start = $patchcheck_start;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002701
2702 my $end = "HEAD";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002703 if (defined($patchcheck_end)) {
2704 $end = $patchcheck_end;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002705 }
2706
Steven Rostedta57419b2010-11-02 15:13:54 -04002707 # Get the true sha1's since we can use things like HEAD~3
2708 $start = get_sha1($start);
2709 $end = get_sha1($end);
2710
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002711 my $type = $patchcheck_type;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002712
2713 # Can't have a test without having a test to run
2714 if ($type eq "test" && !defined($run_test)) {
2715 $type = "boot";
2716 }
2717
2718 open (IN, "git log --pretty=oneline $end|") or
2719 dodie "could not get git list";
2720
2721 my @list;
2722
2723 while (<IN>) {
2724 chomp;
2725 $list[$#list+1] = $_;
2726 last if (/^$start/);
2727 }
2728 close(IN);
2729
2730 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002731 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002732 }
2733
2734 # go backwards in the list
2735 @list = reverse @list;
2736
2737 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04002738 my %ignored_warnings;
2739
2740 if (defined($ignore_warnings)) {
2741 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2742 $ignored_warnings{$sha1} = 1;
2743 }
2744 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002745
2746 $in_patchcheck = 1;
2747 foreach my $item (@list) {
2748 my $sha1 = $item;
2749 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2750
2751 doprint "\nProcessing commit $item\n\n";
2752
2753 run_command "git checkout $sha1" or
2754 die "Failed to checkout $sha1";
2755
2756 # only clean on the first and last patch
2757 if ($item eq $list[0] ||
2758 $item eq $list[$#list]) {
2759 $noclean = $save_clean;
2760 } else {
2761 $noclean = 1;
2762 }
2763
2764 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002765 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002766 } else {
2767 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002768 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002769 }
2770
Steven Rostedt19902072011-06-14 20:46:25 -04002771
2772 if (!defined($ignored_warnings{$sha1})) {
2773 check_buildlog $sha1 or return 0;
2774 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002775
2776 next if ($type eq "build");
2777
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002778 my $failed = 0;
2779
Steven Rostedtddf607e2011-06-14 20:49:13 -04002780 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002781
2782 if (!$failed && $type ne "boot"){
2783 do_run_test or $failed = 1;
2784 }
2785 end_monitor;
2786 return 0 if ($failed);
2787
Steven Rostedt27d934b2011-05-20 09:18:18 -04002788 patchcheck_reboot;
2789
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002790 }
2791 $in_patchcheck = 0;
2792 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002793
2794 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002795}
2796
Steven Rostedtb9066f62011-07-15 21:25:24 -04002797my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04002798my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002799my $iflevel = 0;
2800my @ifdeps;
2801
2802# prevent recursion
2803my %read_kconfigs;
2804
Steven Rostedtac6974c2011-10-04 09:40:17 -04002805sub add_dep {
2806 # $config depends on $dep
2807 my ($config, $dep) = @_;
2808
2809 if (defined($depends{$config})) {
2810 $depends{$config} .= " " . $dep;
2811 } else {
2812 $depends{$config} = $dep;
2813 }
2814
2815 # record the number of configs depending on $dep
2816 if (defined $depcount{$dep}) {
2817 $depcount{$dep}++;
2818 } else {
2819 $depcount{$dep} = 1;
2820 }
2821}
2822
Steven Rostedtb9066f62011-07-15 21:25:24 -04002823# taken from streamline_config.pl
2824sub read_kconfig {
2825 my ($kconfig) = @_;
2826
2827 my $state = "NONE";
2828 my $config;
2829 my @kconfigs;
2830
2831 my $cont = 0;
2832 my $line;
2833
2834
2835 if (! -f $kconfig) {
2836 doprint "file $kconfig does not exist, skipping\n";
2837 return;
2838 }
2839
2840 open(KIN, "$kconfig")
2841 or die "Can't open $kconfig";
2842 while (<KIN>) {
2843 chomp;
2844
2845 # Make sure that lines ending with \ continue
2846 if ($cont) {
2847 $_ = $line . " " . $_;
2848 }
2849
2850 if (s/\\$//) {
2851 $cont = 1;
2852 $line = $_;
2853 next;
2854 }
2855
2856 $cont = 0;
2857
2858 # collect any Kconfig sources
2859 if (/^source\s*"(.*)"/) {
2860 $kconfigs[$#kconfigs+1] = $1;
2861 }
2862
2863 # configs found
2864 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2865 $state = "NEW";
2866 $config = $2;
2867
2868 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04002869 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04002870 }
2871
2872 # collect the depends for the config
2873 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2874
Steven Rostedtac6974c2011-10-04 09:40:17 -04002875 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002876
2877 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04002878 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2879
2880 # selected by depends on config
2881 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002882
2883 # Check for if statements
2884 } elsif (/^if\s+(.*\S)\s*$/) {
2885 my $deps = $1;
2886 # remove beginning and ending non text
2887 $deps =~ s/^[^a-zA-Z0-9_]*//;
2888 $deps =~ s/[^a-zA-Z0-9_]*$//;
2889
2890 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2891
2892 $ifdeps[$iflevel++] = join ':', @deps;
2893
2894 } elsif (/^endif/) {
2895
2896 $iflevel-- if ($iflevel);
2897
2898 # stop on "help"
2899 } elsif (/^\s*help\s*$/) {
2900 $state = "NONE";
2901 }
2902 }
2903 close(KIN);
2904
2905 # read in any configs that were found.
2906 foreach $kconfig (@kconfigs) {
2907 if (!defined($read_kconfigs{$kconfig})) {
2908 $read_kconfigs{$kconfig} = 1;
2909 read_kconfig("$builddir/$kconfig");
2910 }
2911 }
2912}
2913
2914sub read_depends {
2915 # find out which arch this is by the kconfig file
2916 open (IN, $output_config)
2917 or dodie "Failed to read $output_config";
2918 my $arch;
2919 while (<IN>) {
2920 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2921 $arch = $1;
2922 last;
2923 }
2924 }
2925 close IN;
2926
2927 if (!defined($arch)) {
2928 doprint "Could not find arch from config file\n";
2929 doprint "no dependencies used\n";
2930 return;
2931 }
2932
2933 # arch is really the subarch, we need to know
2934 # what directory to look at.
2935 if ($arch eq "i386" || $arch eq "x86_64") {
2936 $arch = "x86";
2937 } elsif ($arch =~ /^tile/) {
2938 $arch = "tile";
2939 }
2940
2941 my $kconfig = "$builddir/arch/$arch/Kconfig";
2942
2943 if (! -f $kconfig && $arch =~ /\d$/) {
2944 my $orig = $arch;
2945 # some subarchs have numbers, truncate them
2946 $arch =~ s/\d*$//;
2947 $kconfig = "$builddir/arch/$arch/Kconfig";
2948 if (! -f $kconfig) {
2949 doprint "No idea what arch dir $orig is for\n";
2950 doprint "no dependencies used\n";
2951 return;
2952 }
2953 }
2954
2955 read_kconfig($kconfig);
2956}
2957
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002958sub read_config_list {
2959 my ($config) = @_;
2960
2961 open (IN, $config)
2962 or dodie "Failed to read $config";
2963
2964 while (<IN>) {
2965 if (/^((CONFIG\S*)=.*)/) {
2966 if (!defined($config_ignore{$2})) {
2967 $config_list{$2} = $1;
2968 }
2969 }
2970 }
2971
2972 close(IN);
2973}
2974
2975sub read_output_config {
2976 my ($config) = @_;
2977
2978 assign_configs \%config_ignore, $config;
2979}
2980
2981sub make_new_config {
2982 my @configs = @_;
2983
2984 open (OUT, ">$output_config")
2985 or dodie "Failed to write $output_config";
2986
2987 foreach my $config (@configs) {
2988 print OUT "$config\n";
2989 }
2990 close OUT;
2991}
2992
Steven Rostedtac6974c2011-10-04 09:40:17 -04002993sub chomp_config {
2994 my ($config) = @_;
2995
2996 $config =~ s/CONFIG_//;
2997
2998 return $config;
2999}
3000
Steven Rostedtb9066f62011-07-15 21:25:24 -04003001sub get_depends {
3002 my ($dep) = @_;
3003
Steven Rostedtac6974c2011-10-04 09:40:17 -04003004 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003005
3006 $dep = $depends{"$kconfig"};
3007
3008 # the dep string we have saves the dependencies as they
3009 # were found, including expressions like ! && ||. We
3010 # want to split this out into just an array of configs.
3011
3012 my $valid = "A-Za-z_0-9";
3013
3014 my @configs;
3015
3016 while ($dep =~ /[$valid]/) {
3017
3018 if ($dep =~ /^[^$valid]*([$valid]+)/) {
3019 my $conf = "CONFIG_" . $1;
3020
3021 $configs[$#configs + 1] = $conf;
3022
3023 $dep =~ s/^[^$valid]*[$valid]+//;
3024 } else {
3025 die "this should never happen";
3026 }
3027 }
3028
3029 return @configs;
3030}
3031
3032my %min_configs;
3033my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003034my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003035my %processed_configs;
3036my %nochange_config;
3037
3038sub test_this_config {
3039 my ($config) = @_;
3040
3041 my $found;
3042
3043 # if we already processed this config, skip it
3044 if (defined($processed_configs{$config})) {
3045 return undef;
3046 }
3047 $processed_configs{$config} = 1;
3048
3049 # if this config failed during this round, skip it
3050 if (defined($nochange_config{$config})) {
3051 return undef;
3052 }
3053
Steven Rostedtac6974c2011-10-04 09:40:17 -04003054 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003055
3056 # Test dependencies first
3057 if (defined($depends{"$kconfig"})) {
3058 my @parents = get_depends $config;
3059 foreach my $parent (@parents) {
3060 # if the parent is in the min config, check it first
3061 next if (!defined($min_configs{$parent}));
3062 $found = test_this_config($parent);
3063 if (defined($found)) {
3064 return $found;
3065 }
3066 }
3067 }
3068
3069 # Remove this config from the list of configs
3070 # do a make oldnoconfig and then read the resulting
3071 # .config to make sure it is missing the config that
3072 # we had before
3073 my %configs = %min_configs;
3074 delete $configs{$config};
3075 make_new_config ((values %configs), (values %keep_configs));
3076 make_oldconfig;
3077 undef %configs;
3078 assign_configs \%configs, $output_config;
3079
3080 return $config if (!defined($configs{$config}));
3081
3082 doprint "disabling config $config did not change .config\n";
3083
3084 $nochange_config{$config} = 1;
3085
3086 return undef;
3087}
3088
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003089sub make_min_config {
3090 my ($i) = @_;
3091
3092 if (!defined($output_minconfig)) {
3093 fail "OUTPUT_MIN_CONFIG not defined" and return;
3094 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04003095
3096 # If output_minconfig exists, and the start_minconfig
3097 # came from min_config, than ask if we should use
3098 # that instead.
3099 if (-f $output_minconfig && !$start_minconfig_defined) {
3100 print "$output_minconfig exists\n";
3101 if (read_yn " Use it as minconfig?") {
3102 $start_minconfig = $output_minconfig;
3103 }
3104 }
3105
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003106 if (!defined($start_minconfig)) {
3107 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
3108 }
3109
Steven Rostedt35ce5952011-07-15 21:57:25 -04003110 my $temp_config = "$tmpdir/temp_config";
3111
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003112 # First things first. We build an allnoconfig to find
3113 # out what the defaults are that we can't touch.
3114 # Some are selections, but we really can't handle selections.
3115
3116 my $save_minconfig = $minconfig;
3117 undef $minconfig;
3118
3119 run_command "$make allnoconfig" or return 0;
3120
Steven Rostedtb9066f62011-07-15 21:25:24 -04003121 read_depends;
3122
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003123 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003124
Steven Rostedt43d1b652011-07-15 22:01:56 -04003125 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003126 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003127
3128 if (defined($ignore_config)) {
3129 # make sure the file exists
3130 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003131 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003132 }
3133
Steven Rostedt43d1b652011-07-15 22:01:56 -04003134 %keep_configs = %save_configs;
3135
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003136 doprint "Load initial configs from $start_minconfig\n";
3137
3138 # Look at the current min configs, and save off all the
3139 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003140 assign_configs \%min_configs, $start_minconfig;
3141
3142 my @config_keys = keys %min_configs;
3143
Steven Rostedtac6974c2011-10-04 09:40:17 -04003144 # All configs need a depcount
3145 foreach my $config (@config_keys) {
3146 my $kconfig = chomp_config $config;
3147 if (!defined $depcount{$kconfig}) {
3148 $depcount{$kconfig} = 0;
3149 }
3150 }
3151
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003152 # Remove anything that was set by the make allnoconfig
3153 # we shouldn't need them as they get set for us anyway.
3154 foreach my $config (@config_keys) {
3155 # Remove anything in the ignore_config
3156 if (defined($keep_configs{$config})) {
3157 my $file = $ignore_config;
3158 $file =~ s,.*/(.*?)$,$1,;
3159 doprint "$config set by $file ... ignored\n";
3160 delete $min_configs{$config};
3161 next;
3162 }
3163 # But make sure the settings are the same. If a min config
3164 # sets a selection, we do not want to get rid of it if
3165 # it is not the same as what we have. Just move it into
3166 # the keep configs.
3167 if (defined($config_ignore{$config})) {
3168 if ($config_ignore{$config} ne $min_configs{$config}) {
3169 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3170 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3171 $keep_configs{$config} = $min_configs{$config};
3172 } else {
3173 doprint "$config set by allnoconfig ... ignored\n";
3174 }
3175 delete $min_configs{$config};
3176 }
3177 }
3178
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003179 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003180 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003181
3182 while (!$done) {
3183
3184 my $config;
3185 my $found;
3186
3187 # Now disable each config one by one and do a make oldconfig
3188 # till we find a config that changes our list.
3189
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003190 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003191
3192 # Sort keys by who is most dependent on
3193 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3194 @test_configs ;
3195
3196 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003197 my $reset = 1;
3198 for (my $i = 0; $i < $#test_configs; $i++) {
3199 if (!defined($nochange_config{$test_configs[0]})) {
3200 $reset = 0;
3201 last;
3202 }
3203 # This config didn't change the .config last time.
3204 # Place it at the end
3205 my $config = shift @test_configs;
3206 push @test_configs, $config;
3207 }
3208
3209 # if every test config has failed to modify the .config file
3210 # in the past, then reset and start over.
3211 if ($reset) {
3212 undef %nochange_config;
3213 }
3214
Steven Rostedtb9066f62011-07-15 21:25:24 -04003215 undef %processed_configs;
3216
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003217 foreach my $config (@test_configs) {
3218
Steven Rostedtb9066f62011-07-15 21:25:24 -04003219 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003220
Steven Rostedtb9066f62011-07-15 21:25:24 -04003221 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003222
3223 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003224 }
3225
3226 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003227 # we could have failed due to the nochange_config hash
3228 # reset and try again
3229 if (!$take_two) {
3230 undef %nochange_config;
3231 $take_two = 1;
3232 next;
3233 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003234 doprint "No more configs found that we can disable\n";
3235 $done = 1;
3236 last;
3237 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003238 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003239
3240 $config = $found;
3241
3242 doprint "Test with $config disabled\n";
3243
3244 # set in_bisect to keep build and monitor from dieing
3245 $in_bisect = 1;
3246
3247 my $failed = 0;
3248 build "oldconfig";
3249 start_monitor_and_boot or $failed = 1;
3250 end_monitor;
3251
3252 $in_bisect = 0;
3253
3254 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003255 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003256 # this config is needed, add it to the ignore list.
3257 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003258 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003259 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003260
3261 # update new ignore configs
3262 if (defined($ignore_config)) {
3263 open (OUT, ">$temp_config")
3264 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003265 foreach my $config (keys %save_configs) {
3266 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003267 }
3268 close OUT;
3269 run_command "mv $temp_config $ignore_config" or
3270 dodie "failed to copy update to $ignore_config";
3271 }
3272
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003273 } else {
3274 # We booted without this config, remove it from the minconfigs.
3275 doprint "$config is not needed, disabling\n";
3276
3277 delete $min_configs{$config};
3278
3279 # Also disable anything that is not enabled in this config
3280 my %configs;
3281 assign_configs \%configs, $output_config;
3282 my @config_keys = keys %min_configs;
3283 foreach my $config (@config_keys) {
3284 if (!defined($configs{$config})) {
3285 doprint "$config is not set, disabling\n";
3286 delete $min_configs{$config};
3287 }
3288 }
3289
3290 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003291 open (OUT, ">$temp_config")
3292 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003293 foreach my $config (keys %keep_configs) {
3294 print OUT "$keep_configs{$config}\n";
3295 }
3296 foreach my $config (keys %min_configs) {
3297 print OUT "$min_configs{$config}\n";
3298 }
3299 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003300
3301 run_command "mv $temp_config $output_minconfig" or
3302 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003303 }
3304
3305 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003306 reboot_to_good $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003307 }
3308
3309 success $i;
3310 return 1;
3311}
3312
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003313$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003314
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003315if ($#ARGV == 0) {
3316 $ktest_config = $ARGV[0];
3317 if (! -f $ktest_config) {
3318 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003319 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003320 exit 0;
3321 }
3322 }
3323} else {
3324 $ktest_config = "ktest.conf";
3325}
3326
3327if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003328 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003329 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003330 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3331 print OUT << "EOF"
3332# Generated by ktest.pl
3333#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003334
3335# PWD is a ktest.pl variable that will result in the process working
3336# directory that ktest.pl is executed in.
3337
3338# THIS_DIR is automatically assigned the PWD of the path that generated
3339# the config file. It is best to use this variable when assigning other
3340# directory paths within this directory. This allows you to easily
3341# move the test cases to other locations or to other machines.
3342#
3343THIS_DIR := $variable{"PWD"}
3344
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003345# Define each test with TEST_START
3346# The config options below it will override the defaults
3347TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003348TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003349
3350DEFAULTS
3351EOF
3352;
3353 close(OUT);
3354}
3355read_config $ktest_config;
3356
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003357if (defined($opt{"LOG_FILE"})) {
3358 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3359}
3360
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003361# Append any configs entered in manually to the config file.
3362my @new_configs = keys %entered_configs;
3363if ($#new_configs >= 0) {
3364 print "\nAppending entered in configs to $ktest_config\n";
3365 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3366 foreach my $config (@new_configs) {
3367 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003368 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003369 }
3370}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003371
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003372if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3373 unlink $opt{"LOG_FILE"};
3374}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003375
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003376doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3377
Steven Rostedta57419b2010-11-02 15:13:54 -04003378for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3379
3380 if (!$i) {
3381 doprint "DEFAULT OPTIONS:\n";
3382 } else {
3383 doprint "\nTEST $i OPTIONS";
3384 if (defined($repeat_tests{$i})) {
3385 $repeat = $repeat_tests{$i};
3386 doprint " ITERATE $repeat";
3387 }
3388 doprint "\n";
3389 }
3390
3391 foreach my $option (sort keys %opt) {
3392
3393 if ($option =~ /\[(\d+)\]$/) {
3394 next if ($i != $1);
3395 } else {
3396 next if ($i);
3397 }
3398
3399 doprint "$option = $opt{$option}\n";
3400 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003401}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003402
Steven Rostedt2a625122011-05-20 15:48:59 -04003403sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003404 my ($name, $i) = @_;
3405
3406 my $option = "$name\[$i\]";
3407
3408 if (defined($opt{$option})) {
3409 return $opt{$option};
3410 }
3411
Steven Rostedta57419b2010-11-02 15:13:54 -04003412 foreach my $test (keys %repeat_tests) {
3413 if ($i >= $test &&
3414 $i < $test + $repeat_tests{$test}) {
3415 $option = "$name\[$test\]";
3416 if (defined($opt{$option})) {
3417 return $opt{$option};
3418 }
3419 }
3420 }
3421
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003422 if (defined($opt{$name})) {
3423 return $opt{$name};
3424 }
3425
3426 return undef;
3427}
3428
Steven Rostedt2a625122011-05-20 15:48:59 -04003429sub set_test_option {
3430 my ($name, $i) = @_;
3431
3432 my $option = __set_test_option($name, $i);
3433 return $option if (!defined($option));
3434
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003435 return eval_option($option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003436}
3437
Steven Rostedt2545eb62010-11-02 15:01:32 -04003438# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003439for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003440
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003441 # Do not reboot on failing test options
3442 $no_reboot = 1;
3443
Steven Rostedt576f6272010-11-02 14:58:38 -04003444 $iteration = $i;
3445
Steven Rostedta75fece2010-11-02 14:58:27 -04003446 my $makecmd = set_test_option("MAKE_CMD", $i);
3447
Steven Rostedt9cc9e092011-12-22 21:37:22 -05003448 # Load all the options into their mapped variable names
3449 foreach my $opt (keys %option_map) {
3450 ${$option_map{$opt}} = set_test_option($opt, $i);
3451 }
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003452
Steven Rostedt35ce5952011-07-15 21:57:25 -04003453 $start_minconfig_defined = 1;
3454
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003455 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003456 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003457 $start_minconfig = $minconfig;
3458 }
3459
Steven Rostedta75fece2010-11-02 14:58:27 -04003460 chdir $builddir || die "can't change directory to $builddir";
3461
Andrew Jonesa908a662011-08-12 15:32:03 +02003462 foreach my $dir ($tmpdir, $outputdir) {
3463 if (!-d $dir) {
3464 mkpath($dir) or
3465 die "can't create $dir";
3466 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003467 }
3468
Steven Rostedte48c5292010-11-02 14:35:37 -04003469 $ENV{"SSH_USER"} = $ssh_user;
3470 $ENV{"MACHINE"} = $machine;
3471
Steven Rostedta75fece2010-11-02 14:58:27 -04003472 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303473 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003474 $dmesg = "$tmpdir/dmesg-$machine";
3475 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003476 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003477
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003478 if (!$buildonly) {
3479 $target = "$ssh_user\@$machine";
3480 if ($reboot_type eq "grub") {
3481 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3482 } elsif (!defined($reboot_script)) {
3483 dodie "REBOOT_SCRIPT not defined"
3484 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003485 }
3486
3487 my $run_type = $build_type;
3488 if ($test_type eq "patchcheck") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003489 $run_type = $patchcheck_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003490 } elsif ($test_type eq "bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003491 $run_type = $bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003492 } elsif ($test_type eq "config_bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003493 $run_type = $config_bisect_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003494 }
3495
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003496 if ($test_type eq "make_min_config") {
3497 $run_type = "";
3498 }
3499
Steven Rostedta75fece2010-11-02 14:58:27 -04003500 # mistake in config file?
3501 if (!defined($run_type)) {
3502 $run_type = "ERROR";
3503 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003504
Steven Rostedte0a87422011-09-30 17:50:48 -04003505 my $installme = "";
3506 $installme = " no_install" if ($no_install);
3507
Steven Rostedt2545eb62010-11-02 15:01:32 -04003508 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003509 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003510
3511 unlink $dmesg;
3512 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303513 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003514
Steven Rostedt250bae82011-07-15 22:05:59 -04003515 if (defined($addconfig)) {
3516 my $min = $minconfig;
3517 if (!defined($minconfig)) {
3518 $min = "";
3519 }
3520 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003521 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003522 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003523 }
3524
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003525 if (defined($checkout)) {
3526 run_command "git checkout $checkout" or
3527 die "failed to checkout $checkout";
3528 }
3529
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003530 $no_reboot = 0;
3531
3532
Steven Rostedta75fece2010-11-02 14:58:27 -04003533 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003534 bisect $i;
3535 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003536 } elsif ($test_type eq "config_bisect") {
3537 config_bisect $i;
3538 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003539 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003540 patchcheck $i;
3541 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003542 } elsif ($test_type eq "make_min_config") {
3543 make_min_config $i;
3544 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003545 }
3546
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003547 if ($build_type ne "nobuild") {
3548 build $build_type or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003549 }
3550
Steven Rostedtcd8e3682011-08-18 16:35:44 -04003551 if ($test_type eq "install") {
3552 get_version;
3553 install;
3554 success $i;
3555 next;
3556 }
3557
Steven Rostedta75fece2010-11-02 14:58:27 -04003558 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04003559 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04003560 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04003561
3562 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3563 do_run_test or $failed = 1;
3564 }
3565 end_monitor;
3566 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003567 }
3568
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003569 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003570}
3571
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003572if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003573 halt;
Steven Rostedt576f6272010-11-02 14:58:38 -04003574} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003575 reboot_to_good;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003576}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003577
Steven Rostedte48c5292010-11-02 14:35:37 -04003578doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3579
Steven Rostedt2545eb62010-11-02 15:01:32 -04003580exit 0;