blob: 95d6a6f7c33aa7971c50f9b107f0c8474a16d70a [file] [log] [blame]
Steven Rostedt2545eb62010-11-02 15:01:32 -04001#!/usr/bin/perl -w
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002#
Uwe Kleine-Königcce1dac2011-01-24 21:12:01 +01003# Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04004# Licensed under the terms of the GNU GPL License version 2
5#
Steven Rostedt2545eb62010-11-02 15:01:32 -04006
7use strict;
8use IPC::Open2;
9use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Steven Rostedt7faafbd2010-11-02 14:58:22 -040010use File::Path qw(mkpath);
11use File::Copy qw(cp);
Steven Rostedt2545eb62010-11-02 15:01:32 -040012use FileHandle;
13
Steven Rostedte48c5292010-11-02 14:35:37 -040014my $VERSION = "0.2";
15
Steven Rostedt2545eb62010-11-02 15:01:32 -040016$| = 1;
17
18my %opt;
Steven Rostedta57419b2010-11-02 15:13:54 -040019my %repeat_tests;
20my %repeats;
Steven Rostedt2545eb62010-11-02 15:01:32 -040021
22#default opts
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050023my %default = (
24 "NUM_TESTS" => 1,
25 "TEST_TYPE" => "build",
26 "BUILD_TYPE" => "randconfig",
27 "MAKE_CMD" => "make",
28 "TIMEOUT" => 120,
29 "TMP_DIR" => "/tmp/ktest/\${MACHINE}",
30 "SLEEP_TIME" => 60, # sleep time between tests
31 "BUILD_NOCLEAN" => 0,
32 "REBOOT_ON_ERROR" => 0,
33 "POWEROFF_ON_ERROR" => 0,
34 "REBOOT_ON_SUCCESS" => 1,
35 "POWEROFF_ON_SUCCESS" => 0,
36 "BUILD_OPTIONS" => "",
37 "BISECT_SLEEP_TIME" => 60, # sleep time between bisects
38 "PATCHCHECK_SLEEP_TIME" => 60, # sleep time between patch checks
39 "CLEAR_LOG" => 0,
40 "BISECT_MANUAL" => 0,
41 "BISECT_SKIP" => 1,
42 "SUCCESS_LINE" => "login:",
43 "DETECT_TRIPLE_FAULT" => 1,
44 "NO_INSTALL" => 0,
45 "BOOTED_TIMEOUT" => 1,
46 "DIE_ON_FAILURE" => 1,
47 "SSH_EXEC" => "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND",
48 "SCP_TO_TARGET" => "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE",
Steven Rostedt02ad2612012-03-21 08:21:24 -040049 "SCP_TO_TARGET_INSTALL" => "\${SCP_TO_TARGET}",
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050050 "REBOOT" => "ssh \$SSH_USER\@\$MACHINE reboot",
51 "STOP_AFTER_SUCCESS" => 10,
52 "STOP_AFTER_FAILURE" => 60,
53 "STOP_TEST_AFTER" => 600,
Steven Rostedt600bbf02011-11-21 20:12:04 -050054
55# required, and we will ask users if they don't have them but we keep the default
56# value something that is common.
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050057 "REBOOT_TYPE" => "grub",
58 "LOCALVERSION" => "-test",
59 "SSH_USER" => "root",
60 "BUILD_TARGET" => "arch/x86/boot/bzImage",
61 "TARGET_IMAGE" => "/boot/vmlinuz-test",
Steven Rostedt9cc9e092011-12-22 21:37:22 -050062
63 "LOG_FILE" => undef,
64 "IGNORE_UNUSED" => 0,
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050065);
Steven Rostedt2545eb62010-11-02 15:01:32 -040066
Steven Rostedt8d1491b2010-11-18 15:39:48 -050067my $ktest_config;
Steven Rostedt2545eb62010-11-02 15:01:32 -040068my $version;
Steven Rostedta75fece2010-11-02 14:58:27 -040069my $machine;
Steven Rostedte48c5292010-11-02 14:35:37 -040070my $ssh_user;
Steven Rostedta75fece2010-11-02 14:58:27 -040071my $tmpdir;
72my $builddir;
73my $outputdir;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -050074my $output_config;
Steven Rostedta75fece2010-11-02 14:58:27 -040075my $test_type;
Steven Rostedt7faafbd2010-11-02 14:58:22 -040076my $build_type;
Steven Rostedta75fece2010-11-02 14:58:27 -040077my $build_options;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -040078my $pre_build;
79my $post_build;
80my $pre_build_die;
81my $post_build_die;
Steven Rostedta75fece2010-11-02 14:58:27 -040082my $reboot_type;
83my $reboot_script;
84my $power_cycle;
Steven Rostedte48c5292010-11-02 14:35:37 -040085my $reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -040086my $reboot_on_error;
Steven Rostedtbc7c5802011-12-22 16:29:10 -050087my $switch_to_good;
88my $switch_to_test;
Steven Rostedta75fece2010-11-02 14:58:27 -040089my $poweroff_on_error;
Steven Rostedt648a1822012-03-21 11:18:27 -040090my $reboot_on_success;
Steven Rostedta75fece2010-11-02 14:58:27 -040091my $die_on_failure;
Steven Rostedt576f6272010-11-02 14:58:38 -040092my $powercycle_after_reboot;
93my $poweroff_after_halt;
Steven Rostedte48c5292010-11-02 14:35:37 -040094my $ssh_exec;
95my $scp_to_target;
Steven Rostedt02ad2612012-03-21 08:21:24 -040096my $scp_to_target_install;
Steven Rostedta75fece2010-11-02 14:58:27 -040097my $power_off;
98my $grub_menu;
Steven Rostedt2545eb62010-11-02 15:01:32 -040099my $grub_number;
100my $target;
101my $make;
Steven Rostedt8b37ca82010-11-02 14:58:33 -0400102my $post_install;
Steven Rostedte0a87422011-09-30 17:50:48 -0400103my $no_install;
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400104my $noclean;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400105my $minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400106my $start_minconfig;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400107my $start_minconfig_defined;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400108my $output_minconfig;
109my $ignore_config;
Steven Rostedtbe405f92012-01-04 21:51:59 -0500110my $ignore_errors;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -0400111my $addconfig;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400112my $in_bisect = 0;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500113my $bisect_bad_commit = "";
Steven Rostedtd6ce2a02010-11-02 14:58:05 -0400114my $reverse_bisect;
Steven Rostedtc960bb92011-03-08 09:22:39 -0500115my $bisect_manual;
Steven Rostedtc23dca72011-03-08 09:26:31 -0500116my $bisect_skip;
Steven Rostedt30f75da2011-06-13 10:35:35 -0400117my $config_bisect_good;
Steven Rostedtc5dacb82011-12-22 12:43:57 -0500118my $bisect_ret_good;
119my $bisect_ret_bad;
120my $bisect_ret_skip;
121my $bisect_ret_abort;
122my $bisect_ret_default;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400123my $in_patchcheck = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400124my $run_test;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400125my $redirect;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400126my $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +0530127my $testlog;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400128my $dmesg;
129my $monitor_fp;
130my $monitor_pid;
131my $monitor_cnt = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -0400132my $sleep_time;
133my $bisect_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -0400134my $patchcheck_sleep_time;
Steven Rostedt19902072011-06-14 20:46:25 -0400135my $ignore_warnings;
Steven Rostedta75fece2010-11-02 14:58:27 -0400136my $store_failures;
Rabin Vincentde5b6e32011-11-18 17:05:31 +0530137my $store_successes;
Steven Rostedt9064af52011-06-13 10:38:48 -0400138my $test_name;
Steven Rostedta75fece2010-11-02 14:58:27 -0400139my $timeout;
140my $booted_timeout;
Steven Rostedtf1a5b962011-06-13 10:30:00 -0400141my $detect_triplefault;
Steven Rostedta75fece2010-11-02 14:58:27 -0400142my $console;
Steven Rostedt2b803362011-09-30 18:00:23 -0400143my $reboot_success_line;
Steven Rostedta75fece2010-11-02 14:58:27 -0400144my $success_line;
Steven Rostedt1c8a6172010-11-09 12:55:40 -0500145my $stop_after_success;
146my $stop_after_failure;
Steven Rostedt2d01b262011-03-08 09:47:54 -0500147my $stop_test_after;
Steven Rostedta75fece2010-11-02 14:58:27 -0400148my $build_target;
149my $target_image;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500150my $checkout;
Steven Rostedta75fece2010-11-02 14:58:27 -0400151my $localversion;
Steven Rostedt576f6272010-11-02 14:58:38 -0400152my $iteration = 0;
Steven Rostedte48c5292010-11-02 14:35:37 -0400153my $successes = 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400154
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500155my $bisect_good;
156my $bisect_bad;
157my $bisect_type;
158my $bisect_start;
159my $bisect_replay;
160my $bisect_files;
161my $bisect_reverse;
162my $bisect_check;
163
164my $config_bisect;
165my $config_bisect_type;
166
167my $patchcheck_type;
168my $patchcheck_start;
169my $patchcheck_end;
170
Steven Rostedt165708b2011-11-26 20:56:52 -0500171# set when a test is something other that just building or install
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500172# which would require more options.
173my $buildonly = 1;
174
Steven Rostedtdbd37832011-11-23 16:00:48 -0500175# set when creating a new config
176my $newconfig = 0;
177
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500178my %entered_configs;
179my %config_help;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400180my %variable;
Steven Rostedtfcb3f162011-06-13 10:40:58 -0400181my %force_config;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500182
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400183# do not force reboots on config problems
184my $no_reboot = 1;
185
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500186my %option_map = (
187 "MACHINE" => \$machine,
188 "SSH_USER" => \$ssh_user,
189 "TMP_DIR" => \$tmpdir,
190 "OUTPUT_DIR" => \$outputdir,
191 "BUILD_DIR" => \$builddir,
192 "TEST_TYPE" => \$test_type,
193 "BUILD_TYPE" => \$build_type,
194 "BUILD_OPTIONS" => \$build_options,
195 "PRE_BUILD" => \$pre_build,
196 "POST_BUILD" => \$post_build,
197 "PRE_BUILD_DIE" => \$pre_build_die,
198 "POST_BUILD_DIE" => \$post_build_die,
199 "POWER_CYCLE" => \$power_cycle,
200 "REBOOT" => \$reboot,
201 "BUILD_NOCLEAN" => \$noclean,
202 "MIN_CONFIG" => \$minconfig,
203 "OUTPUT_MIN_CONFIG" => \$output_minconfig,
204 "START_MIN_CONFIG" => \$start_minconfig,
205 "IGNORE_CONFIG" => \$ignore_config,
206 "TEST" => \$run_test,
207 "ADD_CONFIG" => \$addconfig,
208 "REBOOT_TYPE" => \$reboot_type,
209 "GRUB_MENU" => \$grub_menu,
210 "POST_INSTALL" => \$post_install,
211 "NO_INSTALL" => \$no_install,
212 "REBOOT_SCRIPT" => \$reboot_script,
213 "REBOOT_ON_ERROR" => \$reboot_on_error,
214 "SWITCH_TO_GOOD" => \$switch_to_good,
215 "SWITCH_TO_TEST" => \$switch_to_test,
216 "POWEROFF_ON_ERROR" => \$poweroff_on_error,
Steven Rostedt648a1822012-03-21 11:18:27 -0400217 "REBOOT_ON_SUCCESS" => \$reboot_on_success,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500218 "DIE_ON_FAILURE" => \$die_on_failure,
219 "POWER_OFF" => \$power_off,
220 "POWERCYCLE_AFTER_REBOOT" => \$powercycle_after_reboot,
221 "POWEROFF_AFTER_HALT" => \$poweroff_after_halt,
222 "SLEEP_TIME" => \$sleep_time,
223 "BISECT_SLEEP_TIME" => \$bisect_sleep_time,
224 "PATCHCHECK_SLEEP_TIME" => \$patchcheck_sleep_time,
225 "IGNORE_WARNINGS" => \$ignore_warnings,
Steven Rostedtbe405f92012-01-04 21:51:59 -0500226 "IGNORE_ERRORS" => \$ignore_errors,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500227 "BISECT_MANUAL" => \$bisect_manual,
228 "BISECT_SKIP" => \$bisect_skip,
229 "CONFIG_BISECT_GOOD" => \$config_bisect_good,
230 "BISECT_RET_GOOD" => \$bisect_ret_good,
231 "BISECT_RET_BAD" => \$bisect_ret_bad,
232 "BISECT_RET_SKIP" => \$bisect_ret_skip,
233 "BISECT_RET_ABORT" => \$bisect_ret_abort,
234 "BISECT_RET_DEFAULT" => \$bisect_ret_default,
235 "STORE_FAILURES" => \$store_failures,
236 "STORE_SUCCESSES" => \$store_successes,
237 "TEST_NAME" => \$test_name,
238 "TIMEOUT" => \$timeout,
239 "BOOTED_TIMEOUT" => \$booted_timeout,
240 "CONSOLE" => \$console,
241 "DETECT_TRIPLE_FAULT" => \$detect_triplefault,
242 "SUCCESS_LINE" => \$success_line,
243 "REBOOT_SUCCESS_LINE" => \$reboot_success_line,
244 "STOP_AFTER_SUCCESS" => \$stop_after_success,
245 "STOP_AFTER_FAILURE" => \$stop_after_failure,
246 "STOP_TEST_AFTER" => \$stop_test_after,
247 "BUILD_TARGET" => \$build_target,
248 "SSH_EXEC" => \$ssh_exec,
249 "SCP_TO_TARGET" => \$scp_to_target,
Steven Rostedt02ad2612012-03-21 08:21:24 -0400250 "SCP_TO_TARGET_INSTALL" => \$scp_to_target_install,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500251 "CHECKOUT" => \$checkout,
252 "TARGET_IMAGE" => \$target_image,
253 "LOCALVERSION" => \$localversion,
254
255 "BISECT_GOOD" => \$bisect_good,
256 "BISECT_BAD" => \$bisect_bad,
257 "BISECT_TYPE" => \$bisect_type,
258 "BISECT_START" => \$bisect_start,
259 "BISECT_REPLAY" => \$bisect_replay,
260 "BISECT_FILES" => \$bisect_files,
261 "BISECT_REVERSE" => \$bisect_reverse,
262 "BISECT_CHECK" => \$bisect_check,
263
264 "CONFIG_BISECT" => \$config_bisect,
265 "CONFIG_BISECT_TYPE" => \$config_bisect_type,
266
267 "PATCHCHECK_TYPE" => \$patchcheck_type,
268 "PATCHCHECK_START" => \$patchcheck_start,
269 "PATCHCHECK_END" => \$patchcheck_end,
270);
271
272# Options may be used by other options, record them.
273my %used_options;
274
Steven Rostedt7bf51072011-10-22 09:07:03 -0400275# default variables that can be used
276chomp ($variable{"PWD"} = `pwd`);
277
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500278$config_help{"MACHINE"} = << "EOF"
279 The machine hostname that you will test.
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500280 For build only tests, it is still needed to differentiate log files.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500281EOF
282 ;
283$config_help{"SSH_USER"} = << "EOF"
284 The box is expected to have ssh on normal bootup, provide the user
285 (most likely root, since you need privileged operations)
286EOF
287 ;
288$config_help{"BUILD_DIR"} = << "EOF"
289 The directory that contains the Linux source code (full path).
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{"OUTPUT_DIR"} = << "EOF"
295 The directory that the objects will be built (full path).
296 (can not be same as BUILD_DIR)
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500297 You can use \${PWD} that will be the path where ktest.pl is run, or use
298 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500299EOF
300 ;
301$config_help{"BUILD_TARGET"} = << "EOF"
302 The location of the compiled file to copy to the target.
303 (relative to OUTPUT_DIR)
304EOF
305 ;
Steven Rostedtdbd37832011-11-23 16:00:48 -0500306$config_help{"BUILD_OPTIONS"} = << "EOF"
307 Options to add to \"make\" when building.
308 i.e. -j20
309EOF
310 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500311$config_help{"TARGET_IMAGE"} = << "EOF"
312 The place to put your image on the test machine.
313EOF
314 ;
315$config_help{"POWER_CYCLE"} = << "EOF"
316 A script or command to reboot the box.
317
318 Here is a digital loggers power switch example
319 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
320
321 Here is an example to reboot a virtual box on the current host
322 with the name "Guest".
323 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
324EOF
325 ;
326$config_help{"CONSOLE"} = << "EOF"
327 The script or command that reads the console
328
329 If you use ttywatch server, something like the following would work.
330CONSOLE = nc -d localhost 3001
331
332 For a virtual machine with guest name "Guest".
333CONSOLE = virsh console Guest
334EOF
335 ;
336$config_help{"LOCALVERSION"} = << "EOF"
337 Required version ending to differentiate the test
338 from other linux builds on the system.
339EOF
340 ;
341$config_help{"REBOOT_TYPE"} = << "EOF"
342 Way to reboot the box to the test kernel.
343 Only valid options so far are "grub" and "script".
344
345 If you specify grub, it will assume grub version 1
346 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
347 and select that target to reboot to the kernel. If this is not
348 your setup, then specify "script" and have a command or script
349 specified in REBOOT_SCRIPT to boot to the target.
350
351 The entry in /boot/grub/menu.lst must be entered in manually.
352 The test will not modify that file.
353EOF
354 ;
355$config_help{"GRUB_MENU"} = << "EOF"
356 The grub title name for the test kernel to boot
357 (Only mandatory if REBOOT_TYPE = grub)
358
359 Note, ktest.pl will not update the grub menu.lst, you need to
360 manually add an option for the test. ktest.pl will search
361 the grub menu.lst for this option to find what kernel to
362 reboot into.
363
364 For example, if in the /boot/grub/menu.lst the test kernel title has:
365 title Test Kernel
366 kernel vmlinuz-test
367 GRUB_MENU = Test Kernel
368EOF
369 ;
370$config_help{"REBOOT_SCRIPT"} = << "EOF"
371 A script to reboot the target into the test kernel
372 (Only mandatory if REBOOT_TYPE = script)
373EOF
374 ;
375
Steven Rostedtdad98752011-11-22 20:48:57 -0500376sub read_prompt {
377 my ($cancel, $prompt) = @_;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400378
379 my $ans;
380
381 for (;;) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500382 if ($cancel) {
383 print "$prompt [y/n/C] ";
384 } else {
385 print "$prompt [Y/n] ";
386 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400387 $ans = <STDIN>;
388 chomp $ans;
389 if ($ans =~ /^\s*$/) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500390 if ($cancel) {
391 $ans = "c";
392 } else {
393 $ans = "y";
394 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400395 }
396 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
Steven Rostedtdad98752011-11-22 20:48:57 -0500397 if ($cancel) {
398 last if ($ans =~ /^c$/i);
399 print "Please answer either 'y', 'n' or 'c'.\n";
400 } else {
401 print "Please answer either 'y' or 'n'.\n";
402 }
403 }
404 if ($ans =~ /^c/i) {
405 exit;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400406 }
407 if ($ans !~ /^y$/i) {
408 return 0;
409 }
410 return 1;
411}
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500412
Steven Rostedtdad98752011-11-22 20:48:57 -0500413sub read_yn {
414 my ($prompt) = @_;
415
416 return read_prompt 0, $prompt;
417}
418
419sub read_ync {
420 my ($prompt) = @_;
421
422 return read_prompt 1, $prompt;
423}
424
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500425sub get_ktest_config {
426 my ($config) = @_;
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400427 my $ans;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500428
429 return if (defined($opt{$config}));
430
431 if (defined($config_help{$config})) {
432 print "\n";
433 print $config_help{$config};
434 }
435
436 for (;;) {
437 print "$config = ";
Steven Rostedtdbd37832011-11-23 16:00:48 -0500438 if (defined($default{$config}) && length($default{$config})) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500439 print "\[$default{$config}\] ";
440 }
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400441 $ans = <STDIN>;
442 $ans =~ s/^\s*(.*\S)\s*$/$1/;
443 if ($ans =~ /^\s*$/) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500444 if ($default{$config}) {
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400445 $ans = $default{$config};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500446 } else {
447 print "Your answer can not be blank\n";
448 next;
449 }
450 }
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500451 $entered_configs{$config} = ${ans};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500452 last;
453 }
454}
455
456sub get_ktest_configs {
457 get_ktest_config("MACHINE");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500458 get_ktest_config("BUILD_DIR");
459 get_ktest_config("OUTPUT_DIR");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500460
Steven Rostedtdbd37832011-11-23 16:00:48 -0500461 if ($newconfig) {
462 get_ktest_config("BUILD_OPTIONS");
463 }
464
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500465 # options required for other than just building a kernel
466 if (!$buildonly) {
Steven Rostedt165708b2011-11-26 20:56:52 -0500467 get_ktest_config("POWER_CYCLE");
468 get_ktest_config("CONSOLE");
469 }
470
471 # options required for install and more
472 if ($buildonly != 1) {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500473 get_ktest_config("SSH_USER");
474 get_ktest_config("BUILD_TARGET");
475 get_ktest_config("TARGET_IMAGE");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500476 }
477
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500478 get_ktest_config("LOCALVERSION");
479
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500480 return if ($buildonly);
481
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500482 my $rtype = $opt{"REBOOT_TYPE"};
483
484 if (!defined($rtype)) {
485 if (!defined($opt{"GRUB_MENU"})) {
486 get_ktest_config("REBOOT_TYPE");
487 $rtype = $entered_configs{"REBOOT_TYPE"};
488 } else {
489 $rtype = "grub";
490 }
491 }
492
493 if ($rtype eq "grub") {
494 get_ktest_config("GRUB_MENU");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500495 }
496}
497
Steven Rostedt77d942c2011-05-20 13:36:58 -0400498sub process_variables {
Steven Rostedt8d735212011-10-17 11:36:44 -0400499 my ($value, $remove_undef) = @_;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400500 my $retval = "";
501
502 # We want to check for '\', and it is just easier
503 # to check the previous characet of '$' and not need
504 # to worry if '$' is the first character. By adding
505 # a space to $value, we can just check [^\\]\$ and
506 # it will still work.
507 $value = " $value";
508
509 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
510 my $begin = $1;
511 my $var = $2;
512 my $end = $3;
513 # append beginning of value to retval
514 $retval = "$retval$begin";
515 if (defined($variable{$var})) {
516 $retval = "$retval$variable{$var}";
Steven Rostedt8d735212011-10-17 11:36:44 -0400517 } elsif (defined($remove_undef) && $remove_undef) {
518 # for if statements, any variable that is not defined,
519 # we simple convert to 0
520 $retval = "${retval}0";
Steven Rostedt77d942c2011-05-20 13:36:58 -0400521 } else {
522 # put back the origin piece.
523 $retval = "$retval\$\{$var\}";
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500524 # This could be an option that is used later, save
525 # it so we don't warn if this option is not one of
526 # ktests options.
527 $used_options{$var} = 1;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400528 }
529 $value = $end;
530 }
531 $retval = "$retval$value";
532
533 # remove the space added in the beginning
534 $retval =~ s/ //;
535
536 return "$retval"
537}
538
Steven Rostedta57419b2010-11-02 15:13:54 -0400539sub set_value {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400540 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
Steven Rostedta57419b2010-11-02 15:13:54 -0400541
Steven Rostedtcad96662011-12-22 11:32:52 -0500542 my $prvalue = process_variables($rvalue);
543
544 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500545 # Note if a test is something other than build, then we
546 # will need other manditory options.
Steven Rostedtcad96662011-12-22 11:32:52 -0500547 if ($prvalue ne "install") {
Steven Rostedt165708b2011-11-26 20:56:52 -0500548 $buildonly = 0;
549 } else {
550 # install still limits some manditory options.
551 $buildonly = 2;
552 }
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500553 }
554
Steven Rostedta57419b2010-11-02 15:13:54 -0400555 if (defined($opt{$lvalue})) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400556 if (!$override || defined(${$overrides}{$lvalue})) {
557 my $extra = "";
558 if ($override) {
559 $extra = "In the same override section!\n";
560 }
561 die "$name: $.: Option $lvalue defined more than once!\n$extra";
562 }
Steven Rostedtcad96662011-12-22 11:32:52 -0500563 ${$overrides}{$lvalue} = $prvalue;
Steven Rostedta57419b2010-11-02 15:13:54 -0400564 }
Steven Rostedt21a96792010-11-08 16:45:50 -0500565 if ($rvalue =~ /^\s*$/) {
566 delete $opt{$lvalue};
567 } else {
Steven Rostedtcad96662011-12-22 11:32:52 -0500568 $opt{$lvalue} = $prvalue;
Steven Rostedt21a96792010-11-08 16:45:50 -0500569 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400570}
571
Steven Rostedt77d942c2011-05-20 13:36:58 -0400572sub set_variable {
573 my ($lvalue, $rvalue) = @_;
574
575 if ($rvalue =~ /^\s*$/) {
576 delete $variable{$lvalue};
577 } else {
578 $rvalue = process_variables($rvalue);
579 $variable{$lvalue} = $rvalue;
580 }
581}
582
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400583sub process_compare {
584 my ($lval, $cmp, $rval) = @_;
585
586 # remove whitespace
587
588 $lval =~ s/^\s*//;
589 $lval =~ s/\s*$//;
590
591 $rval =~ s/^\s*//;
592 $rval =~ s/\s*$//;
593
594 if ($cmp eq "==") {
595 return $lval eq $rval;
596 } elsif ($cmp eq "!=") {
597 return $lval ne $rval;
598 }
599
600 my $statement = "$lval $cmp $rval";
601 my $ret = eval $statement;
602
603 # $@ stores error of eval
604 if ($@) {
605 return -1;
606 }
607
608 return $ret;
609}
610
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400611sub value_defined {
612 my ($val) = @_;
613
614 return defined($variable{$2}) ||
615 defined($opt{$2});
616}
617
Steven Rostedt8d735212011-10-17 11:36:44 -0400618my $d = 0;
619sub process_expression {
620 my ($name, $val) = @_;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400621
Steven Rostedt8d735212011-10-17 11:36:44 -0400622 my $c = $d++;
623
624 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
625 my $express = $1;
626
627 if (process_expression($name, $express)) {
628 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
629 } else {
630 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
631 }
632 }
633
634 $d--;
635 my $OR = "\\|\\|";
636 my $AND = "\\&\\&";
637
638 while ($val =~ s/^(.*?)($OR|$AND)//) {
639 my $express = $1;
640 my $op = $2;
641
642 if (process_expression($name, $express)) {
643 if ($op eq "||") {
644 return 1;
645 }
646 } else {
647 if ($op eq "&&") {
648 return 0;
649 }
650 }
651 }
Steven Rostedt45d73a52011-09-30 19:44:53 -0400652
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400653 if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
654 my $ret = process_compare($1, $2, $3);
655 if ($ret < 0) {
656 die "$name: $.: Unable to process comparison\n";
657 }
658 return $ret;
659 }
660
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400661 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
662 if (defined $1) {
663 return !value_defined($2);
664 } else {
665 return value_defined($2);
666 }
667 }
668
Steven Rostedt45d73a52011-09-30 19:44:53 -0400669 if ($val =~ /^\s*0\s*$/) {
670 return 0;
671 } elsif ($val =~ /^\s*\d+\s*$/) {
672 return 1;
673 }
674
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400675 die ("$name: $.: Undefined content $val in if statement\n");
Steven Rostedt8d735212011-10-17 11:36:44 -0400676}
677
678sub process_if {
679 my ($name, $value) = @_;
680
681 # Convert variables and replace undefined ones with 0
682 my $val = process_variables($value, 1);
683 my $ret = process_expression $name, $val;
684
685 return $ret;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400686}
687
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400688sub __read_config {
689 my ($config, $current_test_num) = @_;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400690
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400691 my $in;
692 open($in, $config) || die "can't read file $config";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400693
Steven Rostedta57419b2010-11-02 15:13:54 -0400694 my $name = $config;
695 $name =~ s,.*/(.*),$1,;
696
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400697 my $test_num = $$current_test_num;
Steven Rostedta57419b2010-11-02 15:13:54 -0400698 my $default = 1;
699 my $repeat = 1;
700 my $num_tests_set = 0;
701 my $skip = 0;
702 my $rest;
Steven Rostedta9f84422011-10-17 11:06:29 -0400703 my $line;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400704 my $test_case = 0;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400705 my $if = 0;
706 my $if_set = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400707 my $override = 0;
708
709 my %overrides;
Steven Rostedta57419b2010-11-02 15:13:54 -0400710
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400711 while (<$in>) {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400712
713 # ignore blank lines and comments
714 next if (/^\s*$/ || /\s*\#/);
715
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400716 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400717
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400718 my $type = $1;
719 $rest = $2;
Steven Rostedta9f84422011-10-17 11:06:29 -0400720 $line = $2;
Steven Rostedta57419b2010-11-02 15:13:54 -0400721
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400722 my $old_test_num;
723 my $old_repeat;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400724 $override = 0;
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400725
726 if ($type eq "TEST_START") {
727
728 if ($num_tests_set) {
729 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
730 }
731
732 $old_test_num = $test_num;
733 $old_repeat = $repeat;
734
735 $test_num += $repeat;
736 $default = 0;
737 $repeat = 1;
738 } else {
739 $default = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400740 }
741
Steven Rostedta9f84422011-10-17 11:06:29 -0400742 # If SKIP is anywhere in the line, the command will be skipped
743 if ($rest =~ s/\s+SKIP\b//) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400744 $skip = 1;
745 } else {
Steven Rostedt0df213c2011-06-14 20:51:37 -0400746 $test_case = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400747 $skip = 0;
748 }
749
Steven Rostedta9f84422011-10-17 11:06:29 -0400750 if ($rest =~ s/\sELSE\b//) {
751 if (!$if) {
752 die "$name: $.: ELSE found with out matching IF section\n$_";
753 }
754 $if = 0;
755
756 if ($if_set) {
757 $skip = 1;
758 } else {
759 $skip = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400760 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400761 }
762
Steven Rostedta9f84422011-10-17 11:06:29 -0400763 if ($rest =~ s/\sIF\s+(.*)//) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400764 if (process_if($name, $1)) {
765 $if_set = 1;
766 } else {
767 $skip = 1;
768 }
769 $if = 1;
770 } else {
771 $if = 0;
Steven Rostedta9f84422011-10-17 11:06:29 -0400772 $if_set = 0;
Steven Rostedta57419b2010-11-02 15:13:54 -0400773 }
774
Steven Rostedta9f84422011-10-17 11:06:29 -0400775 if (!$skip) {
776 if ($type eq "TEST_START") {
777 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
778 $repeat = $1;
779 $repeat_tests{"$test_num"} = $repeat;
780 }
781 } elsif ($rest =~ s/\sOVERRIDE\b//) {
782 # DEFAULT only
783 $override = 1;
784 # Clear previous overrides
785 %overrides = ();
786 }
787 }
788
789 if (!$skip && $rest !~ /^\s*$/) {
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400790 die "$name: $.: Gargbage found after $type\n$_";
Steven Rostedta57419b2010-11-02 15:13:54 -0400791 }
792
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400793 if ($skip && $type eq "TEST_START") {
Steven Rostedta57419b2010-11-02 15:13:54 -0400794 $test_num = $old_test_num;
Steven Rostedte48c5292010-11-02 14:35:37 -0400795 $repeat = $old_repeat;
Steven Rostedta57419b2010-11-02 15:13:54 -0400796 }
797
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400798 } elsif (/^\s*ELSE\b(.*)$/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400799 if (!$if) {
800 die "$name: $.: ELSE found with out matching IF section\n$_";
801 }
802 $rest = $1;
803 if ($if_set) {
804 $skip = 1;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400805 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400806 } else {
807 $skip = 0;
808
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400809 if ($rest =~ /\sIF\s+(.*)/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400810 # May be a ELSE IF section.
811 if (!process_if($name, $1)) {
812 $skip = 1;
813 }
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400814 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400815 } else {
816 $if = 0;
817 }
818 }
819
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400820 if ($rest !~ /^\s*$/) {
821 die "$name: $.: Gargbage found after DEFAULTS\n$_";
822 }
823
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400824 } elsif (/^\s*INCLUDE\s+(\S+)/) {
825
826 next if ($skip);
827
828 if (!$default) {
829 die "$name: $.: INCLUDE can only be done in default sections\n$_";
830 }
831
832 my $file = process_variables($1);
833
834 if ($file !~ m,^/,) {
835 # check the path of the config file first
836 if ($config =~ m,(.*)/,) {
837 if (-f "$1/$file") {
838 $file = "$1/$file";
839 }
840 }
841 }
842
843 if ( ! -r $file ) {
844 die "$name: $.: Can't read file $file\n$_";
845 }
846
847 if (__read_config($file, \$test_num)) {
848 $test_case = 1;
849 }
850
Steven Rostedta57419b2010-11-02 15:13:54 -0400851 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
852
853 next if ($skip);
854
Steven Rostedt2545eb62010-11-02 15:01:32 -0400855 my $lvalue = $1;
856 my $rvalue = $2;
857
Steven Rostedta57419b2010-11-02 15:13:54 -0400858 if (!$default &&
859 ($lvalue eq "NUM_TESTS" ||
860 $lvalue eq "LOG_FILE" ||
861 $lvalue eq "CLEAR_LOG")) {
862 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400863 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400864
865 if ($lvalue eq "NUM_TESTS") {
866 if ($test_num) {
867 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
868 }
869 if (!$default) {
870 die "$name: $.: NUM_TESTS must be set in default section\n";
871 }
872 $num_tests_set = 1;
873 }
874
875 if ($default || $lvalue =~ /\[\d+\]$/) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400876 set_value($lvalue, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400877 } else {
878 my $val = "$lvalue\[$test_num\]";
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400879 set_value($val, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400880
881 if ($repeat > 1) {
882 $repeats{$val} = $repeat;
883 }
884 }
Steven Rostedt77d942c2011-05-20 13:36:58 -0400885 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
886 next if ($skip);
887
888 my $lvalue = $1;
889 my $rvalue = $2;
890
891 # process config variables.
892 # Config variables are only active while reading the
893 # config and can be defined anywhere. They also ignore
894 # TEST_START and DEFAULTS, but are skipped if they are in
895 # on of these sections that have SKIP defined.
896 # The save variable can be
897 # defined multiple times and the new one simply overrides
898 # the prevous one.
899 set_variable($lvalue, $rvalue);
900
Steven Rostedta57419b2010-11-02 15:13:54 -0400901 } else {
902 die "$name: $.: Garbage found in config\n$_";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400903 }
904 }
905
Steven Rostedta57419b2010-11-02 15:13:54 -0400906 if ($test_num) {
907 $test_num += $repeat - 1;
908 $opt{"NUM_TESTS"} = $test_num;
909 }
910
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400911 close($in);
912
913 $$current_test_num = $test_num;
914
915 return $test_case;
916}
917
Steven Rostedtc4261d02011-11-23 13:41:18 -0500918sub get_test_case {
919 print "What test case would you like to run?\n";
920 print " (build, install or boot)\n";
921 print " Other tests are available but require editing the config file\n";
922 my $ans = <STDIN>;
923 chomp $ans;
924 $default{"TEST_TYPE"} = $ans;
925}
926
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400927sub read_config {
928 my ($config) = @_;
929
930 my $test_case;
931 my $test_num = 0;
932
933 $test_case = __read_config $config, \$test_num;
934
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500935 # make sure we have all mandatory configs
936 get_ktest_configs;
937
Steven Rostedt0df213c2011-06-14 20:51:37 -0400938 # was a test specified?
939 if (!$test_case) {
940 print "No test case specified.\n";
Steven Rostedtc4261d02011-11-23 13:41:18 -0500941 get_test_case;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400942 }
943
Steven Rostedta75fece2010-11-02 14:58:27 -0400944 # set any defaults
945
946 foreach my $default (keys %default) {
947 if (!defined($opt{$default})) {
948 $opt{$default} = $default{$default};
949 }
950 }
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500951
952 if ($opt{"IGNORE_UNUSED"} == 1) {
953 return;
954 }
955
956 my %not_used;
957
958 # check if there are any stragglers (typos?)
959 foreach my $option (keys %opt) {
960 my $op = $option;
961 # remove per test labels.
962 $op =~ s/\[.*\]//;
963 if (!exists($option_map{$op}) &&
964 !exists($default{$op}) &&
965 !exists($used_options{$op})) {
966 $not_used{$op} = 1;
967 }
968 }
969
970 if (%not_used) {
971 my $s = "s are";
972 $s = " is" if (keys %not_used == 1);
973 print "The following option$s not used; could be a typo:\n";
974 foreach my $option (keys %not_used) {
975 print "$option\n";
976 }
977 print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
978 if (!read_yn "Do you want to continue?") {
979 exit -1;
980 }
981 }
Steven Rostedt2545eb62010-11-02 15:01:32 -0400982}
983
Steven Rostedt23715c3c2011-06-13 11:03:34 -0400984sub __eval_option {
985 my ($option, $i) = @_;
986
987 # Add space to evaluate the character before $
988 $option = " $option";
989 my $retval = "";
Rabin Vincentf9dfb652011-11-18 17:05:30 +0530990 my $repeated = 0;
991 my $parent = 0;
992
993 foreach my $test (keys %repeat_tests) {
994 if ($i >= $test &&
995 $i < $test + $repeat_tests{$test}) {
996
997 $repeated = 1;
998 $parent = $test;
999 last;
1000 }
1001 }
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001002
1003 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
1004 my $start = $1;
1005 my $var = $2;
1006 my $end = $3;
1007
1008 # Append beginning of line
1009 $retval = "$retval$start";
1010
1011 # If the iteration option OPT[$i] exists, then use that.
1012 # otherwise see if the default OPT (without [$i]) exists.
1013
1014 my $o = "$var\[$i\]";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301015 my $parento = "$var\[$parent\]";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001016
1017 if (defined($opt{$o})) {
1018 $o = $opt{$o};
1019 $retval = "$retval$o";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301020 } elsif ($repeated && defined($opt{$parento})) {
1021 $o = $opt{$parento};
1022 $retval = "$retval$o";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001023 } elsif (defined($opt{$var})) {
1024 $o = $opt{$var};
1025 $retval = "$retval$o";
1026 } else {
1027 $retval = "$retval\$\{$var\}";
1028 }
1029
1030 $option = $end;
1031 }
1032
1033 $retval = "$retval$option";
1034
1035 $retval =~ s/^ //;
1036
1037 return $retval;
1038}
1039
1040sub eval_option {
1041 my ($option, $i) = @_;
1042
1043 my $prev = "";
1044
1045 # Since an option can evaluate to another option,
1046 # keep iterating until we do not evaluate any more
1047 # options.
1048 my $r = 0;
1049 while ($prev ne $option) {
1050 # Check for recursive evaluations.
1051 # 100 deep should be more than enough.
1052 if ($r++ > 100) {
1053 die "Over 100 evaluations accurred with $option\n" .
1054 "Check for recursive variables\n";
1055 }
1056 $prev = $option;
1057 $option = __eval_option($option, $i);
1058 }
1059
1060 return $option;
1061}
1062
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001063sub _logit {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001064 if (defined($opt{"LOG_FILE"})) {
1065 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
1066 print OUT @_;
1067 close(OUT);
1068 }
1069}
1070
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001071sub logit {
1072 if (defined($opt{"LOG_FILE"})) {
1073 _logit @_;
1074 } else {
1075 print @_;
1076 }
1077}
1078
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001079sub doprint {
1080 print @_;
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001081 _logit @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001082}
1083
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001084sub run_command;
Andrew Jones2728be42011-08-12 15:32:05 +02001085sub start_monitor;
1086sub end_monitor;
1087sub wait_for_monitor;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001088
1089sub reboot {
Andrew Jones2728be42011-08-12 15:32:05 +02001090 my ($time) = @_;
1091
Steven Rostedt2b803362011-09-30 18:00:23 -04001092 if (defined($time)) {
1093 start_monitor;
1094 # flush out current monitor
1095 # May contain the reboot success line
1096 wait_for_monitor 1;
1097 }
1098
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001099 # try to reboot normally
Steven Rostedte48c5292010-11-02 14:35:37 -04001100 if (run_command $reboot) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001101 if (defined($powercycle_after_reboot)) {
1102 sleep $powercycle_after_reboot;
1103 run_command "$power_cycle";
1104 }
1105 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001106 # nope? power cycle it.
Steven Rostedta75fece2010-11-02 14:58:27 -04001107 run_command "$power_cycle";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001108 }
Andrew Jones2728be42011-08-12 15:32:05 +02001109
1110 if (defined($time)) {
Steven Rostedt2b803362011-09-30 18:00:23 -04001111 wait_for_monitor($time, $reboot_success_line);
Andrew Jones2728be42011-08-12 15:32:05 +02001112 end_monitor;
1113 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001114}
1115
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001116sub reboot_to_good {
1117 my ($time) = @_;
1118
1119 if (defined($switch_to_good)) {
1120 run_command $switch_to_good;
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001121 }
1122
1123 reboot $time;
1124}
1125
Steven Rostedt576f6272010-11-02 14:58:38 -04001126sub do_not_reboot {
1127 my $i = $iteration;
1128
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001129 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -04001130 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1131 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1132}
1133
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001134sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001135 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001136
Steven Rostedt576f6272010-11-02 14:58:38 -04001137 my $i = $iteration;
1138
1139 if ($reboot_on_error && !do_not_reboot) {
1140
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001141 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001142 reboot_to_good;
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001143
Steven Rostedta75fece2010-11-02 14:58:27 -04001144 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001145 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001146 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001147 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001148
Steven Rostedtf80802c2011-03-07 13:18:47 -05001149 if (defined($opt{"LOG_FILE"})) {
1150 print " See $opt{LOG_FILE} for more info.\n";
1151 }
1152
Steven Rostedt576f6272010-11-02 14:58:38 -04001153 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001154}
1155
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001156sub open_console {
1157 my ($fp) = @_;
1158
1159 my $flags;
1160
Steven Rostedta75fece2010-11-02 14:58:27 -04001161 my $pid = open($fp, "$console|") or
1162 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001163
1164 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001165 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001166 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001167 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001168
1169 return $pid;
1170}
1171
1172sub close_console {
1173 my ($fp, $pid) = @_;
1174
1175 doprint "kill child process $pid\n";
1176 kill 2, $pid;
1177
1178 print "closing!\n";
1179 close($fp);
1180}
1181
1182sub start_monitor {
1183 if ($monitor_cnt++) {
1184 return;
1185 }
1186 $monitor_fp = \*MONFD;
1187 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001188
1189 return;
1190
1191 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001192}
1193
1194sub end_monitor {
1195 if (--$monitor_cnt) {
1196 return;
1197 }
1198 close_console($monitor_fp, $monitor_pid);
1199}
1200
1201sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001202 my ($time, $stop) = @_;
1203 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001204 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001205 my $booted = 0;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001206
Steven Rostedta75fece2010-11-02 14:58:27 -04001207 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001208
1209 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001210 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001211 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001212 last if (!defined($line));
1213 print "$line";
1214 $full_line .= $line;
1215
1216 if (defined($stop) && $full_line =~ /$stop/) {
1217 doprint "wait for monitor detected $stop\n";
1218 $booted = 1;
1219 }
1220
1221 if ($line =~ /\n/) {
1222 $full_line = "";
1223 }
1224 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001225 print "** Monitor flushed **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001226}
1227
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301228sub save_logs {
1229 my ($result, $basedir) = @_;
1230 my @t = localtime;
1231 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1232 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1233
1234 my $type = $build_type;
1235 if ($type =~ /useconfig/) {
1236 $type = "useconfig";
1237 }
1238
1239 my $dir = "$machine-$test_type-$type-$result-$date";
1240
1241 $dir = "$basedir/$dir";
1242
1243 if (!-d $dir) {
1244 mkpath($dir) or
1245 die "can't create $dir";
1246 }
1247
1248 my %files = (
1249 "config" => $output_config,
1250 "buildlog" => $buildlog,
1251 "dmesg" => $dmesg,
1252 "testlog" => $testlog,
1253 );
1254
1255 while (my ($name, $source) = each(%files)) {
1256 if (-f "$source") {
1257 cp "$source", "$dir/$name" or
1258 die "failed to copy $source";
1259 }
1260 }
1261
1262 doprint "*** Saved info to $dir ***\n";
1263}
1264
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001265sub fail {
1266
Steven Rostedta75fece2010-11-02 14:58:27 -04001267 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001268 dodie @_;
1269 }
1270
Steven Rostedta75fece2010-11-02 14:58:27 -04001271 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001272
Steven Rostedt576f6272010-11-02 14:58:38 -04001273 my $i = $iteration;
1274
Steven Rostedta75fece2010-11-02 14:58:27 -04001275 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001276 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001277 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001278 reboot_to_good $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001279 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001280
Steven Rostedt9064af52011-06-13 10:38:48 -04001281 my $name = "";
1282
1283 if (defined($test_name)) {
1284 $name = " ($test_name)";
1285 }
1286
Steven Rostedt576f6272010-11-02 14:58:38 -04001287 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1288 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001289 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001290 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1291 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001292
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301293 if (defined($store_failures)) {
1294 save_logs "fail", $store_failures;
1295 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001296
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001297 return 1;
1298}
1299
Steven Rostedt2545eb62010-11-02 15:01:32 -04001300sub run_command {
1301 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001302 my $dolog = 0;
1303 my $dord = 0;
1304 my $pid;
1305
Steven Rostedte48c5292010-11-02 14:35:37 -04001306 $command =~ s/\$SSH_USER/$ssh_user/g;
1307 $command =~ s/\$MACHINE/$machine/g;
1308
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001309 doprint("$command ... ");
1310
1311 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001312 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001313
1314 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001315 open(LOG, ">>$opt{LOG_FILE}") or
1316 dodie "failed to write to log";
1317 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001318 }
1319
1320 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001321 open (RD, ">$redirect") or
1322 dodie "failed to write to redirect $redirect";
1323 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001324 }
1325
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001326 while (<CMD>) {
1327 print LOG if ($dolog);
1328 print RD if ($dord);
1329 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001330
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001331 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001332 my $failed = $?;
1333
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001334 close(CMD);
1335 close(LOG) if ($dolog);
1336 close(RD) if ($dord);
1337
Steven Rostedt2545eb62010-11-02 15:01:32 -04001338 if ($failed) {
1339 doprint "FAILED!\n";
1340 } else {
1341 doprint "SUCCESS\n";
1342 }
1343
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001344 return !$failed;
1345}
1346
Steven Rostedte48c5292010-11-02 14:35:37 -04001347sub run_ssh {
1348 my ($cmd) = @_;
1349 my $cp_exec = $ssh_exec;
1350
1351 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1352 return run_command "$cp_exec";
1353}
1354
1355sub run_scp {
Steven Rostedt02ad2612012-03-21 08:21:24 -04001356 my ($src, $dst, $cp_scp) = @_;
Steven Rostedte48c5292010-11-02 14:35:37 -04001357
1358 $cp_scp =~ s/\$SRC_FILE/$src/g;
1359 $cp_scp =~ s/\$DST_FILE/$dst/g;
1360
1361 return run_command "$cp_scp";
1362}
1363
Steven Rostedt02ad2612012-03-21 08:21:24 -04001364sub run_scp_install {
1365 my ($src, $dst) = @_;
1366
1367 my $cp_scp = $scp_to_target_install;
1368
1369 return run_scp($src, $dst, $cp_scp);
1370}
1371
1372sub run_scp_mod {
1373 my ($src, $dst) = @_;
1374
1375 my $cp_scp = $scp_to_target;
1376
1377 return run_scp($src, $dst, $cp_scp);
1378}
1379
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001380sub get_grub_index {
1381
Steven Rostedta75fece2010-11-02 14:58:27 -04001382 if ($reboot_type ne "grub") {
1383 return;
1384 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001385 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001386
1387 doprint "Find grub menu ... ";
1388 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001389
1390 my $ssh_grub = $ssh_exec;
1391 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1392
1393 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001394 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001395
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001396 my $found = 0;
1397
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001398 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001399 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001400 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001401 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001402 last;
1403 } elsif (/^\s*title\s/) {
1404 $grub_number++;
1405 }
1406 }
1407 close(IN);
1408
Steven Rostedta75fece2010-11-02 14:58:27 -04001409 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001410 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001411 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001412}
1413
Steven Rostedt2545eb62010-11-02 15:01:32 -04001414sub wait_for_input
1415{
1416 my ($fp, $time) = @_;
1417 my $rin;
1418 my $ready;
1419 my $line;
1420 my $ch;
1421
1422 if (!defined($time)) {
1423 $time = $timeout;
1424 }
1425
1426 $rin = '';
1427 vec($rin, fileno($fp), 1) = 1;
1428 $ready = select($rin, undef, undef, $time);
1429
1430 $line = "";
1431
1432 # try to read one char at a time
1433 while (sysread $fp, $ch, 1) {
1434 $line .= $ch;
1435 last if ($ch eq "\n");
1436 }
1437
1438 if (!length($line)) {
1439 return undef;
1440 }
1441
1442 return $line;
1443}
1444
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001445sub reboot_to {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001446 if (defined($switch_to_test)) {
1447 run_command $switch_to_test;
1448 }
1449
Steven Rostedta75fece2010-11-02 14:58:27 -04001450 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001451 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001452 } elsif (defined $reboot_script) {
1453 run_command "$reboot_script";
Steven Rostedta75fece2010-11-02 14:58:27 -04001454 }
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001455 reboot;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001456}
1457
Steven Rostedta57419b2010-11-02 15:13:54 -04001458sub get_sha1 {
1459 my ($commit) = @_;
1460
1461 doprint "git rev-list --max-count=1 $commit ... ";
1462 my $sha1 = `git rev-list --max-count=1 $commit`;
1463 my $ret = $?;
1464
1465 logit $sha1;
1466
1467 if ($ret) {
1468 doprint "FAILED\n";
1469 dodie "Failed to get git $commit";
1470 }
1471
1472 print "SUCCESS\n";
1473
1474 chomp $sha1;
1475
1476 return $sha1;
1477}
1478
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001479sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001480 my $booted = 0;
1481 my $bug = 0;
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001482 my $bug_ignored = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001483 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001484 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001485
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001486 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001487
1488 my $line;
1489 my $full_line = "";
1490
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001491 open(DMESG, "> $dmesg") or
1492 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001493
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001494 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001495
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001496 my $success_start;
1497 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001498 my $monitor_start = time;
1499 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001500 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001501
Steven Rostedt2d01b262011-03-08 09:47:54 -05001502 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001503
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001504 if ($bug && defined($stop_after_failure) &&
1505 $stop_after_failure >= 0) {
1506 my $time = $stop_after_failure - (time - $failure_start);
1507 $line = wait_for_input($monitor_fp, $time);
1508 if (!defined($line)) {
1509 doprint "bug timed out after $booted_timeout seconds\n";
1510 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1511 last;
1512 }
1513 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001514 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001515 if (!defined($line)) {
1516 my $s = $booted_timeout == 1 ? "" : "s";
1517 doprint "Successful boot found: break after $booted_timeout second$s\n";
1518 last;
1519 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001520 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001521 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001522 if (!defined($line)) {
1523 my $s = $timeout == 1 ? "" : "s";
1524 doprint "Timed out after $timeout second$s\n";
1525 last;
1526 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001527 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001528
Steven Rostedt2545eb62010-11-02 15:01:32 -04001529 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001530 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001531
1532 # we are not guaranteed to get a full line
1533 $full_line .= $line;
1534
Steven Rostedta75fece2010-11-02 14:58:27 -04001535 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001536 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001537 $success_start = time;
1538 }
1539
1540 if ($booted && defined($stop_after_success) &&
1541 $stop_after_success >= 0) {
1542 my $now = time;
1543 if ($now - $success_start >= $stop_after_success) {
1544 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1545 last;
1546 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001547 }
1548
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001549 if ($full_line =~ /\[ backtrace testing \]/) {
1550 $skip_call_trace = 1;
1551 }
1552
Steven Rostedt2545eb62010-11-02 15:01:32 -04001553 if ($full_line =~ /call trace:/i) {
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001554 if (!$bug && !$skip_call_trace) {
1555 if ($ignore_errors) {
1556 $bug_ignored = 1;
1557 } else {
1558 $bug = 1;
1559 $failure_start = time;
1560 }
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001561 }
1562 }
1563
1564 if ($bug && defined($stop_after_failure) &&
1565 $stop_after_failure >= 0) {
1566 my $now = time;
1567 if ($now - $failure_start >= $stop_after_failure) {
1568 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1569 last;
1570 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001571 }
1572
1573 if ($full_line =~ /\[ end of backtrace testing \]/) {
1574 $skip_call_trace = 0;
1575 }
1576
1577 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001578 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001579 $bug = 1;
1580 }
1581
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001582 # Detect triple faults by testing the banner
1583 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1584 if ($1 eq $version) {
1585 $version_found = 1;
1586 } elsif ($version_found && $detect_triplefault) {
1587 # We already booted into the kernel we are testing,
1588 # but now we booted into another kernel?
1589 # Consider this a triple fault.
1590 doprint "Aleady booted in Linux kernel $version, but now\n";
1591 doprint "we booted into Linux kernel $1.\n";
1592 doprint "Assuming that this is a triple fault.\n";
1593 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1594 last;
1595 }
1596 }
1597
Steven Rostedt2545eb62010-11-02 15:01:32 -04001598 if ($line =~ /\n/) {
1599 $full_line = "";
1600 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001601
1602 if ($stop_test_after > 0 && !$booted && !$bug) {
1603 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001604 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001605 $done = 1;
1606 }
1607 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001608 }
1609
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001610 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001611
Steven Rostedt2545eb62010-11-02 15:01:32 -04001612 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001613 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001614 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001615 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001616
Steven Rostedta75fece2010-11-02 14:58:27 -04001617 if (!$booted) {
1618 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001619 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001620 }
1621
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001622 if ($bug_ignored) {
1623 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
1624 }
1625
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001626 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001627}
1628
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001629sub eval_kernel_version {
1630 my ($option) = @_;
1631
1632 $option =~ s/\$KERNEL_VERSION/$version/g;
1633
1634 return $option;
1635}
1636
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001637sub do_post_install {
1638
1639 return if (!defined($post_install));
1640
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001641 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001642 run_command "$cp_post_install" or
1643 dodie "Failed to run post install";
1644}
1645
Steven Rostedt2545eb62010-11-02 15:01:32 -04001646sub install {
1647
Steven Rostedte0a87422011-09-30 17:50:48 -04001648 return if ($no_install);
1649
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001650 my $cp_target = eval_kernel_version $target_image;
1651
Steven Rostedt02ad2612012-03-21 08:21:24 -04001652 run_scp_install "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001653 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001654
1655 my $install_mods = 0;
1656
1657 # should we process modules?
1658 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001659 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001660 while (<IN>) {
1661 if (/CONFIG_MODULES(=y)?/) {
1662 $install_mods = 1 if (defined($1));
1663 last;
1664 }
1665 }
1666 close(IN);
1667
1668 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001669 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001670 doprint "No modules needed\n";
1671 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001672 }
1673
Steven Rostedt627977d2012-03-21 08:16:15 -04001674 run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001675 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001676
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001677 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001678 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001679
Steven Rostedte48c5292010-11-02 14:35:37 -04001680 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001681 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001682
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001683 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001684 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001685 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001686
Steven Rostedt02ad2612012-03-21 08:21:24 -04001687 run_scp_mod "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001688 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001689
Steven Rostedta75fece2010-11-02 14:58:27 -04001690 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001691
Steven Rostedte7b13442011-06-14 20:44:36 -04001692 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001693 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001694
Steven Rostedte48c5292010-11-02 14:35:37 -04001695 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001696
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001697 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001698}
1699
Steven Rostedtddf607e2011-06-14 20:49:13 -04001700sub get_version {
1701 # get the release name
1702 doprint "$make kernelrelease ... ";
1703 $version = `$make kernelrelease | tail -1`;
1704 chomp($version);
1705 doprint "$version\n";
1706}
1707
1708sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001709 # Make sure the stable kernel has finished booting
1710 start_monitor;
1711 wait_for_monitor 5;
1712 end_monitor;
1713
Steven Rostedtddf607e2011-06-14 20:49:13 -04001714 get_grub_index;
1715 get_version;
1716 install;
1717
1718 start_monitor;
1719 return monitor;
1720}
1721
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001722sub check_buildlog {
1723 my ($patch) = @_;
1724
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001725 my @files = `git show $patch | diffstat -l`;
1726
1727 open(IN, "git show $patch |") or
1728 dodie "failed to show $patch";
1729 while (<IN>) {
1730 if (m,^--- a/(.*),) {
1731 chomp $1;
1732 $files[$#files] = $1;
1733 }
1734 }
1735 close(IN);
1736
1737 open(IN, $buildlog) or dodie "Can't open $buildlog";
1738 while (<IN>) {
1739 if (/^\s*(.*?):.*(warning|error)/) {
1740 my $err = $1;
1741 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001742 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001743 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001744 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001745 }
1746 }
1747 }
1748 }
1749 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001750
1751 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001752}
1753
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001754sub apply_min_config {
1755 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05001756
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001757 # Read the config file and remove anything that
1758 # is in the force_config hash (from minconfig and others)
1759 # then add the force config back.
1760
1761 doprint "Applying minimum configurations into $output_config.new\n";
1762
1763 open (OUT, ">$outconfig") or
1764 dodie "Can't create $outconfig";
1765
1766 if (-f $output_config) {
1767 open (IN, $output_config) or
1768 dodie "Failed to open $output_config";
1769 while (<IN>) {
1770 if (/^(# )?(CONFIG_[^\s=]*)/) {
1771 next if (defined($force_config{$2}));
1772 }
1773 print OUT;
1774 }
1775 close IN;
1776 }
1777 foreach my $config (keys %force_config) {
1778 print OUT "$force_config{$config}\n";
1779 }
1780 close OUT;
1781
1782 run_command "mv $outconfig $output_config";
1783}
1784
1785sub make_oldconfig {
1786
Steven Rostedt4c4ab122011-07-15 21:16:17 -04001787 my @force_list = keys %force_config;
1788
1789 if ($#force_list >= 0) {
1790 apply_min_config;
1791 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001792
1793 if (!run_command "$make oldnoconfig") {
Steven Rostedt612b9e92011-03-07 13:27:43 -05001794 # Perhaps oldnoconfig doesn't exist in this version of the kernel
1795 # try a yes '' | oldconfig
1796 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001797 run_command "yes '' | $make oldconfig" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001798 dodie "failed make config oldconfig";
1799 }
1800}
1801
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001802# read a config file and use this to force new configs.
1803sub load_force_config {
1804 my ($config) = @_;
1805
1806 open(IN, $config) or
1807 dodie "failed to read $config";
1808 while (<IN>) {
1809 chomp;
1810 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1811 $force_config{$1} = $_;
1812 } elsif (/^# (CONFIG_\S*) is not set/) {
1813 $force_config{$1} = $_;
1814 }
1815 }
1816 close IN;
1817}
1818
Steven Rostedt2545eb62010-11-02 15:01:32 -04001819sub build {
1820 my ($type) = @_;
1821
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001822 unlink $buildlog;
1823
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001824 # Failed builds should not reboot the target
1825 my $save_no_reboot = $no_reboot;
1826 $no_reboot = 1;
1827
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001828 if (defined($pre_build)) {
1829 my $ret = run_command $pre_build;
1830 if (!$ret && defined($pre_build_die) &&
1831 $pre_build_die) {
1832 dodie "failed to pre_build\n";
1833 }
1834 }
1835
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001836 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001837 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001838 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001839
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001840 $type = "oldconfig";
1841 }
1842
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001843 # old config can ask questions
1844 if ($type eq "oldconfig") {
Steven Rostedt9386c6a2010-11-08 16:35:48 -05001845 $type = "oldnoconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001846
1847 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001848 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001849
Andrew Jones13488232011-08-12 15:32:04 +02001850 if (!$noclean) {
1851 run_command "mv $output_config $outputdir/config_temp" or
1852 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001853
Andrew Jones13488232011-08-12 15:32:04 +02001854 run_command "$make mrproper" or dodie "make mrproper";
1855
1856 run_command "mv $outputdir/config_temp $output_config" or
1857 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001858 }
1859
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001860 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001861 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001862 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001863 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001864 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001865
1866 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04001867 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
1868 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001869 close(OUT);
1870
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001871 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001872 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001873 }
1874
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001875 if ($type ne "oldnoconfig") {
1876 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001877 dodie "failed make config";
1878 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001879 # Run old config regardless, to enforce min configurations
1880 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001881
Steven Rostedta75fece2010-11-02 14:58:27 -04001882 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001883 my $build_ret = run_command "$make $build_options";
1884 undef $redirect;
1885
1886 if (defined($post_build)) {
1887 my $ret = run_command $post_build;
1888 if (!$ret && defined($post_build_die) &&
1889 $post_build_die) {
1890 dodie "failed to post_build\n";
1891 }
1892 }
1893
1894 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001895 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001896 if ($in_bisect) {
1897 $no_reboot = $save_no_reboot;
1898 return 0;
1899 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001900 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001901 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001902
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001903 $no_reboot = $save_no_reboot;
1904
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001905 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001906}
1907
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001908sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04001909 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001910 if (defined($poweroff_after_halt)) {
1911 sleep $poweroff_after_halt;
1912 run_command "$power_off";
1913 }
1914 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001915 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04001916 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001917 }
1918}
1919
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001920sub success {
1921 my ($i) = @_;
1922
Steven Rostedte48c5292010-11-02 14:35:37 -04001923 $successes++;
1924
Steven Rostedt9064af52011-06-13 10:38:48 -04001925 my $name = "";
1926
1927 if (defined($test_name)) {
1928 $name = " ($test_name)";
1929 }
1930
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001931 doprint "\n\n*******************************************\n";
1932 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001933 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001934 doprint "*******************************************\n";
1935 doprint "*******************************************\n";
1936
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301937 if (defined($store_successes)) {
1938 save_logs "success", $store_successes;
1939 }
1940
Steven Rostedt576f6272010-11-02 14:58:38 -04001941 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001942 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001943 reboot_to_good $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001944 }
1945}
1946
Steven Rostedtc960bb92011-03-08 09:22:39 -05001947sub answer_bisect {
1948 for (;;) {
1949 doprint "Pass or fail? [p/f]";
1950 my $ans = <STDIN>;
1951 chomp $ans;
1952 if ($ans eq "p" || $ans eq "P") {
1953 return 1;
1954 } elsif ($ans eq "f" || $ans eq "F") {
1955 return 0;
1956 } else {
1957 print "Please answer 'P' or 'F'\n";
1958 }
1959 }
1960}
1961
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001962sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001963 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001964
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001965 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04001966 $reboot_on_error = 0;
1967 $poweroff_on_error = 0;
1968 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001969
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301970 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001971 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05301972 undef $redirect;
1973
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001974 exit $failed;
1975}
1976
1977my $child_done;
1978
1979sub child_finished {
1980 $child_done = 1;
1981}
1982
1983sub do_run_test {
1984 my $child_pid;
1985 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001986 my $line;
1987 my $full_line;
1988 my $bug = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001989
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001990 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001991
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001992 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001993
1994 $child_done = 0;
1995
1996 $SIG{CHLD} = qw(child_finished);
1997
1998 $child_pid = fork;
1999
2000 child_run_test if (!$child_pid);
2001
2002 $full_line = "";
2003
2004 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002005 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002006 if (defined($line)) {
2007
2008 # we are not guaranteed to get a full line
2009 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002010 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002011
2012 if ($full_line =~ /call trace:/i) {
2013 $bug = 1;
2014 }
2015
2016 if ($full_line =~ /Kernel panic -/) {
2017 $bug = 1;
2018 }
2019
2020 if ($line =~ /\n/) {
2021 $full_line = "";
2022 }
2023 }
2024 } while (!$child_done && !$bug);
2025
2026 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002027 my $failure_start = time;
2028 my $now;
2029 do {
2030 $line = wait_for_input($monitor_fp, 1);
2031 if (defined($line)) {
2032 doprint $line;
2033 }
2034 $now = time;
2035 if ($now - $failure_start >= $stop_after_failure) {
2036 last;
2037 }
2038 } while (defined($line));
2039
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002040 doprint "Detected kernel crash!\n";
2041 # kill the child with extreme prejudice
2042 kill 9, $child_pid;
2043 }
2044
2045 waitpid $child_pid, 0;
2046 $child_exit = $?;
2047
Steven Rostedtc5dacb82011-12-22 12:43:57 -05002048 if (!$bug && $in_bisect) {
2049 if (defined($bisect_ret_good)) {
2050 if ($child_exit == $bisect_ret_good) {
2051 return 1;
2052 }
2053 }
2054 if (defined($bisect_ret_skip)) {
2055 if ($child_exit == $bisect_ret_skip) {
2056 return -1;
2057 }
2058 }
2059 if (defined($bisect_ret_abort)) {
2060 if ($child_exit == $bisect_ret_abort) {
2061 fail "test abort" and return -2;
2062 }
2063 }
2064 if (defined($bisect_ret_bad)) {
2065 if ($child_exit == $bisect_ret_skip) {
2066 return 0;
2067 }
2068 }
2069 if (defined($bisect_ret_default)) {
2070 if ($bisect_ret_default eq "good") {
2071 return 1;
2072 } elsif ($bisect_ret_default eq "bad") {
2073 return 0;
2074 } elsif ($bisect_ret_default eq "skip") {
2075 return -1;
2076 } elsif ($bisect_ret_default eq "abort") {
2077 return -2;
2078 } else {
2079 fail "unknown default action: $bisect_ret_default"
2080 and return -2;
2081 }
2082 }
2083 }
2084
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002085 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002086 return 0 if $in_bisect;
2087 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002088 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002089 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002090}
2091
Steven Rostedta75fece2010-11-02 14:58:27 -04002092sub run_git_bisect {
2093 my ($command) = @_;
2094
2095 doprint "$command ... ";
2096
2097 my $output = `$command 2>&1`;
2098 my $ret = $?;
2099
2100 logit $output;
2101
2102 if ($ret) {
2103 doprint "FAILED\n";
2104 dodie "Failed to git bisect";
2105 }
2106
2107 doprint "SUCCESS\n";
2108 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2109 doprint "$1 [$2]\n";
2110 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002111 $bisect_bad_commit = $1;
Steven Rostedta75fece2010-11-02 14:58:27 -04002112 doprint "Found bad commit... $1\n";
2113 return 0;
2114 } else {
2115 # we already logged it, just print it now.
2116 print $output;
2117 }
2118
2119 return 1;
2120}
2121
Steven Rostedtc23dca72011-03-08 09:26:31 -05002122sub bisect_reboot {
2123 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002124 reboot_to_good $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05002125}
2126
2127# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05002128sub run_bisect_test {
2129 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002130
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002131 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002132 my $result;
2133 my $output;
2134 my $ret;
2135
Steven Rostedt0a05c762010-11-08 11:14:10 -05002136 $in_bisect = 1;
2137
2138 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002139
2140 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002141 if ($failed && $bisect_skip) {
2142 $in_bisect = 0;
2143 return -1;
2144 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002145 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002146
2147 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04002148 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002149
2150 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002151 if ($failed && $bisect_skip) {
2152 end_monitor;
2153 bisect_reboot;
2154 $in_bisect = 0;
2155 return -1;
2156 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002157 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002158
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002159 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002160 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002161 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002162 }
2163
2164 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002165 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002166 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002167 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002168 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04002169
2170 # reboot the box to a kernel we can ssh to
2171 if ($type ne "build") {
2172 bisect_reboot;
2173 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002174 $in_bisect = 0;
2175
2176 return $result;
2177}
2178
2179sub run_bisect {
2180 my ($type) = @_;
2181 my $buildtype = "oldconfig";
2182
2183 # We should have a minconfig to use?
2184 if (defined($minconfig)) {
2185 $buildtype = "useconfig:$minconfig";
2186 }
2187
2188 my $ret = run_bisect_test $type, $buildtype;
2189
Steven Rostedtc960bb92011-03-08 09:22:39 -05002190 if ($bisect_manual) {
2191 $ret = answer_bisect;
2192 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002193
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002194 # Are we looking for where it worked, not failed?
2195 if ($reverse_bisect) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002196 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002197 }
2198
Steven Rostedtc23dca72011-03-08 09:26:31 -05002199 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002200 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002201 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002202 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002203 } elsif ($bisect_skip) {
2204 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2205 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002206 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002207}
2208
Steven Rostedtdad98752011-11-22 20:48:57 -05002209sub update_bisect_replay {
2210 my $tmp_log = "$tmpdir/ktest_bisect_log";
2211 run_command "git bisect log > $tmp_log" or
2212 die "can't create bisect log";
2213 return $tmp_log;
2214}
2215
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002216sub bisect {
2217 my ($i) = @_;
2218
2219 my $result;
2220
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002221 die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2222 die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
2223 die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002224
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002225 my $good = $bisect_good;
2226 my $bad = $bisect_bad;
2227 my $type = $bisect_type;
2228 my $start = $bisect_start;
2229 my $replay = $bisect_replay;
2230 my $start_files = $bisect_files;
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002231
2232 if (defined($start_files)) {
2233 $start_files = " -- " . $start_files;
2234 } else {
2235 $start_files = "";
2236 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002237
Steven Rostedta57419b2010-11-02 15:13:54 -04002238 # convert to true sha1's
2239 $good = get_sha1($good);
2240 $bad = get_sha1($bad);
2241
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002242 if (defined($bisect_reverse) && $bisect_reverse == 1) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002243 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2244 $reverse_bisect = 1;
2245 } else {
2246 $reverse_bisect = 0;
2247 }
2248
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002249 # Can't have a test without having a test to run
2250 if ($type eq "test" && !defined($run_test)) {
2251 $type = "boot";
2252 }
2253
Steven Rostedtdad98752011-11-22 20:48:57 -05002254 # Check if a bisect was running
2255 my $bisect_start_file = "$builddir/.git/BISECT_START";
2256
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002257 my $check = $bisect_check;
Steven Rostedtdad98752011-11-22 20:48:57 -05002258 my $do_check = defined($check) && $check ne "0";
2259
2260 if ( -f $bisect_start_file ) {
2261 print "Bisect in progress found\n";
2262 if ($do_check) {
2263 print " If you say yes, then no checks of good or bad will be done\n";
2264 }
2265 if (defined($replay)) {
2266 print "** BISECT_REPLAY is defined in config file **";
2267 print " Ignore config option and perform new git bisect log?\n";
2268 if (read_ync " (yes, no, or cancel) ") {
2269 $replay = update_bisect_replay;
2270 $do_check = 0;
2271 }
2272 } elsif (read_yn "read git log and continue?") {
2273 $replay = update_bisect_replay;
2274 $do_check = 0;
2275 }
2276 }
2277
2278 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002279
2280 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002281 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002282
2283 if ($check ne "good") {
2284 doprint "TESTING BISECT BAD [$bad]\n";
2285 run_command "git checkout $bad" or
2286 die "Failed to checkout $bad";
2287
2288 $result = run_bisect $type;
2289
2290 if ($result ne "bad") {
2291 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2292 }
2293 }
2294
2295 if ($check ne "bad") {
2296 doprint "TESTING BISECT GOOD [$good]\n";
2297 run_command "git checkout $good" or
2298 die "Failed to checkout $good";
2299
2300 $result = run_bisect $type;
2301
2302 if ($result ne "good") {
2303 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2304 }
2305 }
2306
2307 # checkout where we started
2308 run_command "git checkout $head" or
2309 die "Failed to checkout $head";
2310 }
2311
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002312 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002313 dodie "could not start bisect";
2314
2315 run_command "git bisect good $good" or
2316 dodie "could not set bisect good to $good";
2317
2318 run_git_bisect "git bisect bad $bad" or
2319 dodie "could not set bisect bad to $bad";
2320
2321 if (defined($replay)) {
2322 run_command "git bisect replay $replay" or
2323 dodie "failed to run replay";
2324 }
2325
2326 if (defined($start)) {
2327 run_command "git checkout $start" or
2328 dodie "failed to checkout $start";
2329 }
2330
2331 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002332 do {
2333 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002334 $test = run_git_bisect "git bisect $result";
2335 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002336
2337 run_command "git bisect log" or
2338 dodie "could not capture git bisect log";
2339
2340 run_command "git bisect reset" or
2341 dodie "could not reset git bisect";
2342
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002343 doprint "Bad commit was [$bisect_bad_commit]\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002344
Steven Rostedt0a05c762010-11-08 11:14:10 -05002345 success $i;
2346}
2347
2348my %config_ignore;
2349my %config_set;
2350
2351my %config_list;
2352my %null_config;
2353
2354my %dependency;
2355
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002356sub assign_configs {
2357 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002358
2359 open (IN, $config)
2360 or dodie "Failed to read $config";
2361
2362 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002363 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002364 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002365 }
2366 }
2367
2368 close(IN);
2369}
2370
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002371sub process_config_ignore {
2372 my ($config) = @_;
2373
2374 assign_configs \%config_ignore, $config;
2375}
2376
Steven Rostedt0a05c762010-11-08 11:14:10 -05002377sub read_current_config {
2378 my ($config_ref) = @_;
2379
2380 %{$config_ref} = ();
2381 undef %{$config_ref};
2382
2383 my @key = keys %{$config_ref};
2384 if ($#key >= 0) {
2385 print "did not delete!\n";
2386 exit;
2387 }
2388 open (IN, "$output_config");
2389
2390 while (<IN>) {
2391 if (/^(CONFIG\S+)=(.*)/) {
2392 ${$config_ref}{$1} = $2;
2393 }
2394 }
2395 close(IN);
2396}
2397
2398sub get_dependencies {
2399 my ($config) = @_;
2400
2401 my $arr = $dependency{$config};
2402 if (!defined($arr)) {
2403 return ();
2404 }
2405
2406 my @deps = @{$arr};
2407
2408 foreach my $dep (@{$arr}) {
2409 print "ADD DEP $dep\n";
2410 @deps = (@deps, get_dependencies $dep);
2411 }
2412
2413 return @deps;
2414}
2415
2416sub create_config {
2417 my @configs = @_;
2418
2419 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2420
2421 foreach my $config (@configs) {
2422 print OUT "$config_set{$config}\n";
2423 my @deps = get_dependencies $config;
2424 foreach my $dep (@deps) {
2425 print OUT "$config_set{$dep}\n";
2426 }
2427 }
2428
2429 foreach my $config (keys %config_ignore) {
2430 print OUT "$config_ignore{$config}\n";
2431 }
2432 close(OUT);
2433
2434# exit;
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002435 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002436}
2437
2438sub compare_configs {
2439 my (%a, %b) = @_;
2440
2441 foreach my $item (keys %a) {
2442 if (!defined($b{$item})) {
2443 print "diff $item\n";
2444 return 1;
2445 }
2446 delete $b{$item};
2447 }
2448
2449 my @keys = keys %b;
2450 if ($#keys) {
2451 print "diff2 $keys[0]\n";
2452 }
2453 return -1 if ($#keys >= 0);
2454
2455 return 0;
2456}
2457
2458sub run_config_bisect_test {
2459 my ($type) = @_;
2460
2461 return run_bisect_test $type, "oldconfig";
2462}
2463
2464sub process_passed {
2465 my (%configs) = @_;
2466
2467 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2468 # Passed! All these configs are part of a good compile.
2469 # Add them to the min options.
2470 foreach my $config (keys %configs) {
2471 if (defined($config_list{$config})) {
2472 doprint " removing $config\n";
2473 $config_ignore{$config} = $config_list{$config};
2474 delete $config_list{$config};
2475 }
2476 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002477 doprint "config copied to $outputdir/config_good\n";
2478 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002479}
2480
2481sub process_failed {
2482 my ($config) = @_;
2483
2484 doprint "\n\n***************************************\n";
2485 doprint "Found bad config: $config\n";
2486 doprint "***************************************\n\n";
2487}
2488
2489sub run_config_bisect {
2490
2491 my @start_list = keys %config_list;
2492
2493 if ($#start_list < 0) {
2494 doprint "No more configs to test!!!\n";
2495 return -1;
2496 }
2497
2498 doprint "***** RUN TEST ***\n";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002499 my $type = $config_bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002500 my $ret;
2501 my %current_config;
2502
2503 my $count = $#start_list + 1;
2504 doprint " $count configs to test\n";
2505
2506 my $half = int($#start_list / 2);
2507
2508 do {
2509 my @tophalf = @start_list[0 .. $half];
2510
2511 create_config @tophalf;
2512 read_current_config \%current_config;
2513
2514 $count = $#tophalf + 1;
2515 doprint "Testing $count configs\n";
2516 my $found = 0;
2517 # make sure we test something
2518 foreach my $config (@tophalf) {
2519 if (defined($current_config{$config})) {
2520 logit " $config\n";
2521 $found = 1;
2522 }
2523 }
2524 if (!$found) {
2525 # try the other half
2526 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002527 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedt0a05c762010-11-08 11:14:10 -05002528 create_config @tophalf;
2529 read_current_config \%current_config;
2530 foreach my $config (@tophalf) {
2531 if (defined($current_config{$config})) {
2532 logit " $config\n";
2533 $found = 1;
2534 }
2535 }
2536 if (!$found) {
2537 doprint "Failed: Can't make new config with current configs\n";
2538 foreach my $config (@start_list) {
2539 doprint " CONFIG: $config\n";
2540 }
2541 return -1;
2542 }
2543 $count = $#tophalf + 1;
2544 doprint "Testing $count configs\n";
2545 }
2546
2547 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002548 if ($bisect_manual) {
2549 $ret = answer_bisect;
2550 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002551 if ($ret) {
2552 process_passed %current_config;
2553 return 0;
2554 }
2555
2556 doprint "This config had a failure.\n";
2557 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002558 doprint "config copied to $outputdir/config_bad\n";
2559 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002560
2561 # A config exists in this group that was bad.
2562 foreach my $config (keys %config_list) {
2563 if (!defined($current_config{$config})) {
2564 doprint " removing $config\n";
2565 delete $config_list{$config};
2566 }
2567 }
2568
2569 @start_list = @tophalf;
2570
2571 if ($#start_list == 0) {
2572 process_failed $start_list[0];
2573 return 1;
2574 }
2575
2576 # remove half the configs we are looking at and see if
2577 # they are good.
2578 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002579 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002580
Steven Rostedtc960bb92011-03-08 09:22:39 -05002581 # we found a single config, try it again unless we are running manually
2582
2583 if ($bisect_manual) {
2584 process_failed $start_list[0];
2585 return 1;
2586 }
2587
Steven Rostedt0a05c762010-11-08 11:14:10 -05002588 my @tophalf = @start_list[0 .. 0];
2589
2590 $ret = run_config_bisect_test $type;
2591 if ($ret) {
2592 process_passed %current_config;
2593 return 0;
2594 }
2595
2596 process_failed $start_list[0];
2597 return 1;
2598}
2599
2600sub config_bisect {
2601 my ($i) = @_;
2602
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002603 my $start_config = $config_bisect;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002604
2605 my $tmpconfig = "$tmpdir/use_config";
2606
Steven Rostedt30f75da2011-06-13 10:35:35 -04002607 if (defined($config_bisect_good)) {
2608 process_config_ignore $config_bisect_good;
2609 }
2610
Steven Rostedt0a05c762010-11-08 11:14:10 -05002611 # Make the file with the bad config and the min config
2612 if (defined($minconfig)) {
2613 # read the min config for things to ignore
2614 run_command "cp $minconfig $tmpconfig" or
2615 dodie "failed to copy $minconfig to $tmpconfig";
2616 } else {
2617 unlink $tmpconfig;
2618 }
2619
Steven Rostedt0a05c762010-11-08 11:14:10 -05002620 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002621 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002622 process_config_ignore $tmpconfig;
2623 }
2624
2625 # now process the start config
2626 run_command "cp $start_config $output_config" or
2627 dodie "failed to copy $start_config to $output_config";
2628
2629 # read directly what we want to check
2630 my %config_check;
2631 open (IN, $output_config)
Masanari Iidaf9dee312012-02-11 21:46:56 +09002632 or dodie "failed to open $output_config";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002633
2634 while (<IN>) {
2635 if (/^((CONFIG\S*)=.*)/) {
2636 $config_check{$2} = $1;
2637 }
2638 }
2639 close(IN);
2640
Steven Rostedt250bae82011-07-15 22:05:59 -04002641 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002642 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002643
2644 # check to see what we lost (or gained)
2645 open (IN, $output_config)
2646 or dodie "Failed to read $start_config";
2647
2648 my %removed_configs;
2649 my %added_configs;
2650
2651 while (<IN>) {
2652 if (/^((CONFIG\S*)=.*)/) {
2653 # save off all options
2654 $config_set{$2} = $1;
2655 if (defined($config_check{$2})) {
2656 if (defined($config_ignore{$2})) {
2657 $removed_configs{$2} = $1;
2658 } else {
2659 $config_list{$2} = $1;
2660 }
2661 } elsif (!defined($config_ignore{$2})) {
2662 $added_configs{$2} = $1;
2663 $config_list{$2} = $1;
2664 }
2665 }
2666 }
2667 close(IN);
2668
2669 my @confs = keys %removed_configs;
2670 if ($#confs >= 0) {
2671 doprint "Configs overridden by default configs and removed from check:\n";
2672 foreach my $config (@confs) {
2673 doprint " $config\n";
2674 }
2675 }
2676 @confs = keys %added_configs;
2677 if ($#confs >= 0) {
2678 doprint "Configs appearing in make oldconfig and added:\n";
2679 foreach my $config (@confs) {
2680 doprint " $config\n";
2681 }
2682 }
2683
2684 my %config_test;
2685 my $once = 0;
2686
2687 # Sometimes kconfig does weird things. We must make sure
2688 # that the config we autocreate has everything we need
2689 # to test, otherwise we may miss testing configs, or
2690 # may not be able to create a new config.
2691 # Here we create a config with everything set.
2692 create_config (keys %config_list);
2693 read_current_config \%config_test;
2694 foreach my $config (keys %config_list) {
2695 if (!defined($config_test{$config})) {
2696 if (!$once) {
2697 $once = 1;
2698 doprint "Configs not produced by kconfig (will not be checked):\n";
2699 }
2700 doprint " $config\n";
2701 delete $config_list{$config};
2702 }
2703 }
2704 my $ret;
2705 do {
2706 $ret = run_config_bisect;
2707 } while (!$ret);
2708
2709 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002710
2711 success $i;
2712}
2713
Steven Rostedt27d934b2011-05-20 09:18:18 -04002714sub patchcheck_reboot {
2715 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002716 reboot_to_good $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04002717}
2718
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002719sub patchcheck {
2720 my ($i) = @_;
2721
2722 die "PATCHCHECK_START[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002723 if (!defined($patchcheck_start));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002724 die "PATCHCHECK_TYPE[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002725 if (!defined($patchcheck_type));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002726
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002727 my $start = $patchcheck_start;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002728
2729 my $end = "HEAD";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002730 if (defined($patchcheck_end)) {
2731 $end = $patchcheck_end;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002732 }
2733
Steven Rostedta57419b2010-11-02 15:13:54 -04002734 # Get the true sha1's since we can use things like HEAD~3
2735 $start = get_sha1($start);
2736 $end = get_sha1($end);
2737
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002738 my $type = $patchcheck_type;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002739
2740 # Can't have a test without having a test to run
2741 if ($type eq "test" && !defined($run_test)) {
2742 $type = "boot";
2743 }
2744
2745 open (IN, "git log --pretty=oneline $end|") or
2746 dodie "could not get git list";
2747
2748 my @list;
2749
2750 while (<IN>) {
2751 chomp;
2752 $list[$#list+1] = $_;
2753 last if (/^$start/);
2754 }
2755 close(IN);
2756
2757 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002758 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002759 }
2760
2761 # go backwards in the list
2762 @list = reverse @list;
2763
2764 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04002765 my %ignored_warnings;
2766
2767 if (defined($ignore_warnings)) {
2768 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2769 $ignored_warnings{$sha1} = 1;
2770 }
2771 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002772
2773 $in_patchcheck = 1;
2774 foreach my $item (@list) {
2775 my $sha1 = $item;
2776 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2777
2778 doprint "\nProcessing commit $item\n\n";
2779
2780 run_command "git checkout $sha1" or
2781 die "Failed to checkout $sha1";
2782
2783 # only clean on the first and last patch
2784 if ($item eq $list[0] ||
2785 $item eq $list[$#list]) {
2786 $noclean = $save_clean;
2787 } else {
2788 $noclean = 1;
2789 }
2790
2791 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002792 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002793 } else {
2794 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002795 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002796 }
2797
Steven Rostedt19902072011-06-14 20:46:25 -04002798
2799 if (!defined($ignored_warnings{$sha1})) {
2800 check_buildlog $sha1 or return 0;
2801 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002802
2803 next if ($type eq "build");
2804
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002805 my $failed = 0;
2806
Steven Rostedtddf607e2011-06-14 20:49:13 -04002807 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002808
2809 if (!$failed && $type ne "boot"){
2810 do_run_test or $failed = 1;
2811 }
2812 end_monitor;
2813 return 0 if ($failed);
2814
Steven Rostedt27d934b2011-05-20 09:18:18 -04002815 patchcheck_reboot;
2816
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002817 }
2818 $in_patchcheck = 0;
2819 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002820
2821 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002822}
2823
Steven Rostedtb9066f62011-07-15 21:25:24 -04002824my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04002825my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002826my $iflevel = 0;
2827my @ifdeps;
2828
2829# prevent recursion
2830my %read_kconfigs;
2831
Steven Rostedtac6974c2011-10-04 09:40:17 -04002832sub add_dep {
2833 # $config depends on $dep
2834 my ($config, $dep) = @_;
2835
2836 if (defined($depends{$config})) {
2837 $depends{$config} .= " " . $dep;
2838 } else {
2839 $depends{$config} = $dep;
2840 }
2841
2842 # record the number of configs depending on $dep
2843 if (defined $depcount{$dep}) {
2844 $depcount{$dep}++;
2845 } else {
2846 $depcount{$dep} = 1;
2847 }
2848}
2849
Steven Rostedtb9066f62011-07-15 21:25:24 -04002850# taken from streamline_config.pl
2851sub read_kconfig {
2852 my ($kconfig) = @_;
2853
2854 my $state = "NONE";
2855 my $config;
2856 my @kconfigs;
2857
2858 my $cont = 0;
2859 my $line;
2860
2861
2862 if (! -f $kconfig) {
2863 doprint "file $kconfig does not exist, skipping\n";
2864 return;
2865 }
2866
2867 open(KIN, "$kconfig")
2868 or die "Can't open $kconfig";
2869 while (<KIN>) {
2870 chomp;
2871
2872 # Make sure that lines ending with \ continue
2873 if ($cont) {
2874 $_ = $line . " " . $_;
2875 }
2876
2877 if (s/\\$//) {
2878 $cont = 1;
2879 $line = $_;
2880 next;
2881 }
2882
2883 $cont = 0;
2884
2885 # collect any Kconfig sources
2886 if (/^source\s*"(.*)"/) {
2887 $kconfigs[$#kconfigs+1] = $1;
2888 }
2889
2890 # configs found
2891 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
2892 $state = "NEW";
2893 $config = $2;
2894
2895 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04002896 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04002897 }
2898
2899 # collect the depends for the config
2900 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
2901
Steven Rostedtac6974c2011-10-04 09:40:17 -04002902 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002903
2904 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04002905 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
2906
2907 # selected by depends on config
2908 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04002909
2910 # Check for if statements
2911 } elsif (/^if\s+(.*\S)\s*$/) {
2912 my $deps = $1;
2913 # remove beginning and ending non text
2914 $deps =~ s/^[^a-zA-Z0-9_]*//;
2915 $deps =~ s/[^a-zA-Z0-9_]*$//;
2916
2917 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
2918
2919 $ifdeps[$iflevel++] = join ':', @deps;
2920
2921 } elsif (/^endif/) {
2922
2923 $iflevel-- if ($iflevel);
2924
2925 # stop on "help"
2926 } elsif (/^\s*help\s*$/) {
2927 $state = "NONE";
2928 }
2929 }
2930 close(KIN);
2931
2932 # read in any configs that were found.
2933 foreach $kconfig (@kconfigs) {
2934 if (!defined($read_kconfigs{$kconfig})) {
2935 $read_kconfigs{$kconfig} = 1;
2936 read_kconfig("$builddir/$kconfig");
2937 }
2938 }
2939}
2940
2941sub read_depends {
2942 # find out which arch this is by the kconfig file
2943 open (IN, $output_config)
2944 or dodie "Failed to read $output_config";
2945 my $arch;
2946 while (<IN>) {
2947 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
2948 $arch = $1;
2949 last;
2950 }
2951 }
2952 close IN;
2953
2954 if (!defined($arch)) {
2955 doprint "Could not find arch from config file\n";
2956 doprint "no dependencies used\n";
2957 return;
2958 }
2959
2960 # arch is really the subarch, we need to know
2961 # what directory to look at.
2962 if ($arch eq "i386" || $arch eq "x86_64") {
2963 $arch = "x86";
2964 } elsif ($arch =~ /^tile/) {
2965 $arch = "tile";
2966 }
2967
2968 my $kconfig = "$builddir/arch/$arch/Kconfig";
2969
2970 if (! -f $kconfig && $arch =~ /\d$/) {
2971 my $orig = $arch;
2972 # some subarchs have numbers, truncate them
2973 $arch =~ s/\d*$//;
2974 $kconfig = "$builddir/arch/$arch/Kconfig";
2975 if (! -f $kconfig) {
2976 doprint "No idea what arch dir $orig is for\n";
2977 doprint "no dependencies used\n";
2978 return;
2979 }
2980 }
2981
2982 read_kconfig($kconfig);
2983}
2984
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002985sub read_config_list {
2986 my ($config) = @_;
2987
2988 open (IN, $config)
2989 or dodie "Failed to read $config";
2990
2991 while (<IN>) {
2992 if (/^((CONFIG\S*)=.*)/) {
2993 if (!defined($config_ignore{$2})) {
2994 $config_list{$2} = $1;
2995 }
2996 }
2997 }
2998
2999 close(IN);
3000}
3001
3002sub read_output_config {
3003 my ($config) = @_;
3004
3005 assign_configs \%config_ignore, $config;
3006}
3007
3008sub make_new_config {
3009 my @configs = @_;
3010
3011 open (OUT, ">$output_config")
3012 or dodie "Failed to write $output_config";
3013
3014 foreach my $config (@configs) {
3015 print OUT "$config\n";
3016 }
3017 close OUT;
3018}
3019
Steven Rostedtac6974c2011-10-04 09:40:17 -04003020sub chomp_config {
3021 my ($config) = @_;
3022
3023 $config =~ s/CONFIG_//;
3024
3025 return $config;
3026}
3027
Steven Rostedtb9066f62011-07-15 21:25:24 -04003028sub get_depends {
3029 my ($dep) = @_;
3030
Steven Rostedtac6974c2011-10-04 09:40:17 -04003031 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003032
3033 $dep = $depends{"$kconfig"};
3034
3035 # the dep string we have saves the dependencies as they
3036 # were found, including expressions like ! && ||. We
3037 # want to split this out into just an array of configs.
3038
3039 my $valid = "A-Za-z_0-9";
3040
3041 my @configs;
3042
3043 while ($dep =~ /[$valid]/) {
3044
3045 if ($dep =~ /^[^$valid]*([$valid]+)/) {
3046 my $conf = "CONFIG_" . $1;
3047
3048 $configs[$#configs + 1] = $conf;
3049
3050 $dep =~ s/^[^$valid]*[$valid]+//;
3051 } else {
3052 die "this should never happen";
3053 }
3054 }
3055
3056 return @configs;
3057}
3058
3059my %min_configs;
3060my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003061my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003062my %processed_configs;
3063my %nochange_config;
3064
3065sub test_this_config {
3066 my ($config) = @_;
3067
3068 my $found;
3069
3070 # if we already processed this config, skip it
3071 if (defined($processed_configs{$config})) {
3072 return undef;
3073 }
3074 $processed_configs{$config} = 1;
3075
3076 # if this config failed during this round, skip it
3077 if (defined($nochange_config{$config})) {
3078 return undef;
3079 }
3080
Steven Rostedtac6974c2011-10-04 09:40:17 -04003081 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003082
3083 # Test dependencies first
3084 if (defined($depends{"$kconfig"})) {
3085 my @parents = get_depends $config;
3086 foreach my $parent (@parents) {
3087 # if the parent is in the min config, check it first
3088 next if (!defined($min_configs{$parent}));
3089 $found = test_this_config($parent);
3090 if (defined($found)) {
3091 return $found;
3092 }
3093 }
3094 }
3095
3096 # Remove this config from the list of configs
3097 # do a make oldnoconfig and then read the resulting
3098 # .config to make sure it is missing the config that
3099 # we had before
3100 my %configs = %min_configs;
3101 delete $configs{$config};
3102 make_new_config ((values %configs), (values %keep_configs));
3103 make_oldconfig;
3104 undef %configs;
3105 assign_configs \%configs, $output_config;
3106
3107 return $config if (!defined($configs{$config}));
3108
3109 doprint "disabling config $config did not change .config\n";
3110
3111 $nochange_config{$config} = 1;
3112
3113 return undef;
3114}
3115
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003116sub make_min_config {
3117 my ($i) = @_;
3118
3119 if (!defined($output_minconfig)) {
3120 fail "OUTPUT_MIN_CONFIG not defined" and return;
3121 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04003122
3123 # If output_minconfig exists, and the start_minconfig
3124 # came from min_config, than ask if we should use
3125 # that instead.
3126 if (-f $output_minconfig && !$start_minconfig_defined) {
3127 print "$output_minconfig exists\n";
3128 if (read_yn " Use it as minconfig?") {
3129 $start_minconfig = $output_minconfig;
3130 }
3131 }
3132
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003133 if (!defined($start_minconfig)) {
3134 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
3135 }
3136
Steven Rostedt35ce5952011-07-15 21:57:25 -04003137 my $temp_config = "$tmpdir/temp_config";
3138
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003139 # First things first. We build an allnoconfig to find
3140 # out what the defaults are that we can't touch.
3141 # Some are selections, but we really can't handle selections.
3142
3143 my $save_minconfig = $minconfig;
3144 undef $minconfig;
3145
3146 run_command "$make allnoconfig" or return 0;
3147
Steven Rostedtb9066f62011-07-15 21:25:24 -04003148 read_depends;
3149
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003150 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003151
Steven Rostedt43d1b652011-07-15 22:01:56 -04003152 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003153 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003154
3155 if (defined($ignore_config)) {
3156 # make sure the file exists
3157 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003158 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003159 }
3160
Steven Rostedt43d1b652011-07-15 22:01:56 -04003161 %keep_configs = %save_configs;
3162
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003163 doprint "Load initial configs from $start_minconfig\n";
3164
3165 # Look at the current min configs, and save off all the
3166 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003167 assign_configs \%min_configs, $start_minconfig;
3168
3169 my @config_keys = keys %min_configs;
3170
Steven Rostedtac6974c2011-10-04 09:40:17 -04003171 # All configs need a depcount
3172 foreach my $config (@config_keys) {
3173 my $kconfig = chomp_config $config;
3174 if (!defined $depcount{$kconfig}) {
3175 $depcount{$kconfig} = 0;
3176 }
3177 }
3178
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003179 # Remove anything that was set by the make allnoconfig
3180 # we shouldn't need them as they get set for us anyway.
3181 foreach my $config (@config_keys) {
3182 # Remove anything in the ignore_config
3183 if (defined($keep_configs{$config})) {
3184 my $file = $ignore_config;
3185 $file =~ s,.*/(.*?)$,$1,;
3186 doprint "$config set by $file ... ignored\n";
3187 delete $min_configs{$config};
3188 next;
3189 }
3190 # But make sure the settings are the same. If a min config
3191 # sets a selection, we do not want to get rid of it if
3192 # it is not the same as what we have. Just move it into
3193 # the keep configs.
3194 if (defined($config_ignore{$config})) {
3195 if ($config_ignore{$config} ne $min_configs{$config}) {
3196 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3197 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3198 $keep_configs{$config} = $min_configs{$config};
3199 } else {
3200 doprint "$config set by allnoconfig ... ignored\n";
3201 }
3202 delete $min_configs{$config};
3203 }
3204 }
3205
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003206 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003207 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003208
3209 while (!$done) {
3210
3211 my $config;
3212 my $found;
3213
3214 # Now disable each config one by one and do a make oldconfig
3215 # till we find a config that changes our list.
3216
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003217 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003218
3219 # Sort keys by who is most dependent on
3220 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3221 @test_configs ;
3222
3223 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003224 my $reset = 1;
3225 for (my $i = 0; $i < $#test_configs; $i++) {
3226 if (!defined($nochange_config{$test_configs[0]})) {
3227 $reset = 0;
3228 last;
3229 }
3230 # This config didn't change the .config last time.
3231 # Place it at the end
3232 my $config = shift @test_configs;
3233 push @test_configs, $config;
3234 }
3235
3236 # if every test config has failed to modify the .config file
3237 # in the past, then reset and start over.
3238 if ($reset) {
3239 undef %nochange_config;
3240 }
3241
Steven Rostedtb9066f62011-07-15 21:25:24 -04003242 undef %processed_configs;
3243
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003244 foreach my $config (@test_configs) {
3245
Steven Rostedtb9066f62011-07-15 21:25:24 -04003246 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003247
Steven Rostedtb9066f62011-07-15 21:25:24 -04003248 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003249
3250 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003251 }
3252
3253 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003254 # we could have failed due to the nochange_config hash
3255 # reset and try again
3256 if (!$take_two) {
3257 undef %nochange_config;
3258 $take_two = 1;
3259 next;
3260 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003261 doprint "No more configs found that we can disable\n";
3262 $done = 1;
3263 last;
3264 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003265 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003266
3267 $config = $found;
3268
3269 doprint "Test with $config disabled\n";
3270
3271 # set in_bisect to keep build and monitor from dieing
3272 $in_bisect = 1;
3273
3274 my $failed = 0;
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003275 build "oldconfig" or $failed = 1;
3276 if (!$failed) {
3277 start_monitor_and_boot or $failed = 1;
3278 end_monitor;
3279 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003280
3281 $in_bisect = 0;
3282
3283 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003284 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003285 # this config is needed, add it to the ignore list.
3286 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003287 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003288 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003289
3290 # update new ignore configs
3291 if (defined($ignore_config)) {
3292 open (OUT, ">$temp_config")
3293 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003294 foreach my $config (keys %save_configs) {
3295 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003296 }
3297 close OUT;
3298 run_command "mv $temp_config $ignore_config" or
3299 dodie "failed to copy update to $ignore_config";
3300 }
3301
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003302 } else {
3303 # We booted without this config, remove it from the minconfigs.
3304 doprint "$config is not needed, disabling\n";
3305
3306 delete $min_configs{$config};
3307
3308 # Also disable anything that is not enabled in this config
3309 my %configs;
3310 assign_configs \%configs, $output_config;
3311 my @config_keys = keys %min_configs;
3312 foreach my $config (@config_keys) {
3313 if (!defined($configs{$config})) {
3314 doprint "$config is not set, disabling\n";
3315 delete $min_configs{$config};
3316 }
3317 }
3318
3319 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003320 open (OUT, ">$temp_config")
3321 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003322 foreach my $config (keys %keep_configs) {
3323 print OUT "$keep_configs{$config}\n";
3324 }
3325 foreach my $config (keys %min_configs) {
3326 print OUT "$min_configs{$config}\n";
3327 }
3328 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003329
3330 run_command "mv $temp_config $output_minconfig" or
3331 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003332 }
3333
3334 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003335 reboot_to_good $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003336 }
3337
3338 success $i;
3339 return 1;
3340}
3341
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003342$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003343
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003344if ($#ARGV == 0) {
3345 $ktest_config = $ARGV[0];
3346 if (! -f $ktest_config) {
3347 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003348 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003349 exit 0;
3350 }
3351 }
3352} else {
3353 $ktest_config = "ktest.conf";
3354}
3355
3356if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003357 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003358 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003359 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3360 print OUT << "EOF"
3361# Generated by ktest.pl
3362#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003363
3364# PWD is a ktest.pl variable that will result in the process working
3365# directory that ktest.pl is executed in.
3366
3367# THIS_DIR is automatically assigned the PWD of the path that generated
3368# the config file. It is best to use this variable when assigning other
3369# directory paths within this directory. This allows you to easily
3370# move the test cases to other locations or to other machines.
3371#
3372THIS_DIR := $variable{"PWD"}
3373
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003374# Define each test with TEST_START
3375# The config options below it will override the defaults
3376TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003377TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003378
3379DEFAULTS
3380EOF
3381;
3382 close(OUT);
3383}
3384read_config $ktest_config;
3385
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003386if (defined($opt{"LOG_FILE"})) {
3387 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3388}
3389
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003390# Append any configs entered in manually to the config file.
3391my @new_configs = keys %entered_configs;
3392if ($#new_configs >= 0) {
3393 print "\nAppending entered in configs to $ktest_config\n";
3394 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3395 foreach my $config (@new_configs) {
3396 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003397 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003398 }
3399}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003400
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003401if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3402 unlink $opt{"LOG_FILE"};
3403}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003404
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003405doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3406
Steven Rostedta57419b2010-11-02 15:13:54 -04003407for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3408
3409 if (!$i) {
3410 doprint "DEFAULT OPTIONS:\n";
3411 } else {
3412 doprint "\nTEST $i OPTIONS";
3413 if (defined($repeat_tests{$i})) {
3414 $repeat = $repeat_tests{$i};
3415 doprint " ITERATE $repeat";
3416 }
3417 doprint "\n";
3418 }
3419
3420 foreach my $option (sort keys %opt) {
3421
3422 if ($option =~ /\[(\d+)\]$/) {
3423 next if ($i != $1);
3424 } else {
3425 next if ($i);
3426 }
3427
3428 doprint "$option = $opt{$option}\n";
3429 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003430}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003431
Steven Rostedt2a625122011-05-20 15:48:59 -04003432sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003433 my ($name, $i) = @_;
3434
3435 my $option = "$name\[$i\]";
3436
3437 if (defined($opt{$option})) {
3438 return $opt{$option};
3439 }
3440
Steven Rostedta57419b2010-11-02 15:13:54 -04003441 foreach my $test (keys %repeat_tests) {
3442 if ($i >= $test &&
3443 $i < $test + $repeat_tests{$test}) {
3444 $option = "$name\[$test\]";
3445 if (defined($opt{$option})) {
3446 return $opt{$option};
3447 }
3448 }
3449 }
3450
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003451 if (defined($opt{$name})) {
3452 return $opt{$name};
3453 }
3454
3455 return undef;
3456}
3457
Steven Rostedt2a625122011-05-20 15:48:59 -04003458sub set_test_option {
3459 my ($name, $i) = @_;
3460
3461 my $option = __set_test_option($name, $i);
3462 return $option if (!defined($option));
3463
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003464 return eval_option($option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003465}
3466
Steven Rostedt2545eb62010-11-02 15:01:32 -04003467# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003468for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003469
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003470 # Do not reboot on failing test options
3471 $no_reboot = 1;
3472
Steven Rostedt576f6272010-11-02 14:58:38 -04003473 $iteration = $i;
3474
Steven Rostedta75fece2010-11-02 14:58:27 -04003475 my $makecmd = set_test_option("MAKE_CMD", $i);
3476
Steven Rostedt9cc9e092011-12-22 21:37:22 -05003477 # Load all the options into their mapped variable names
3478 foreach my $opt (keys %option_map) {
3479 ${$option_map{$opt}} = set_test_option($opt, $i);
3480 }
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003481
Steven Rostedt35ce5952011-07-15 21:57:25 -04003482 $start_minconfig_defined = 1;
3483
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003484 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003485 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003486 $start_minconfig = $minconfig;
3487 }
3488
Steven Rostedta75fece2010-11-02 14:58:27 -04003489 chdir $builddir || die "can't change directory to $builddir";
3490
Andrew Jonesa908a662011-08-12 15:32:03 +02003491 foreach my $dir ($tmpdir, $outputdir) {
3492 if (!-d $dir) {
3493 mkpath($dir) or
3494 die "can't create $dir";
3495 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003496 }
3497
Steven Rostedte48c5292010-11-02 14:35:37 -04003498 $ENV{"SSH_USER"} = $ssh_user;
3499 $ENV{"MACHINE"} = $machine;
3500
Steven Rostedta75fece2010-11-02 14:58:27 -04003501 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303502 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003503 $dmesg = "$tmpdir/dmesg-$machine";
3504 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003505 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003506
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003507 if (!$buildonly) {
3508 $target = "$ssh_user\@$machine";
3509 if ($reboot_type eq "grub") {
3510 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003511 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003512 }
3513
3514 my $run_type = $build_type;
3515 if ($test_type eq "patchcheck") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003516 $run_type = $patchcheck_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003517 } elsif ($test_type eq "bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003518 $run_type = $bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003519 } elsif ($test_type eq "config_bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003520 $run_type = $config_bisect_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003521 }
3522
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003523 if ($test_type eq "make_min_config") {
3524 $run_type = "";
3525 }
3526
Steven Rostedta75fece2010-11-02 14:58:27 -04003527 # mistake in config file?
3528 if (!defined($run_type)) {
3529 $run_type = "ERROR";
3530 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003531
Steven Rostedte0a87422011-09-30 17:50:48 -04003532 my $installme = "";
3533 $installme = " no_install" if ($no_install);
3534
Steven Rostedt2545eb62010-11-02 15:01:32 -04003535 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003536 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003537
3538 unlink $dmesg;
3539 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303540 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003541
Steven Rostedt250bae82011-07-15 22:05:59 -04003542 if (defined($addconfig)) {
3543 my $min = $minconfig;
3544 if (!defined($minconfig)) {
3545 $min = "";
3546 }
3547 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003548 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003549 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003550 }
3551
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003552 if (defined($checkout)) {
3553 run_command "git checkout $checkout" or
3554 die "failed to checkout $checkout";
3555 }
3556
Steven Rostedt648a1822012-03-21 11:18:27 -04003557 # A test may opt to not reboot the box
3558 if ($reboot_on_success) {
3559 $no_reboot = 0;
3560 }
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003561
Steven Rostedta75fece2010-11-02 14:58:27 -04003562 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003563 bisect $i;
3564 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003565 } elsif ($test_type eq "config_bisect") {
3566 config_bisect $i;
3567 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003568 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003569 patchcheck $i;
3570 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003571 } elsif ($test_type eq "make_min_config") {
3572 make_min_config $i;
3573 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003574 }
3575
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003576 if ($build_type ne "nobuild") {
3577 build $build_type or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003578 }
3579
Steven Rostedtcd8e3682011-08-18 16:35:44 -04003580 if ($test_type eq "install") {
3581 get_version;
3582 install;
3583 success $i;
3584 next;
3585 }
3586
Steven Rostedta75fece2010-11-02 14:58:27 -04003587 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04003588 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04003589 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04003590
3591 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3592 do_run_test or $failed = 1;
3593 }
3594 end_monitor;
3595 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003596 }
3597
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003598 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003599}
3600
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003601if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003602 halt;
Steven Rostedt576f6272010-11-02 14:58:38 -04003603} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003604 reboot_to_good;
Steven Rostedt648a1822012-03-21 11:18:27 -04003605} elsif (defined($switch_to_good)) {
3606 # still need to get to the good kernel
3607 run_command $switch_to_good;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003608}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003609
Steven Rostedt648a1822012-03-21 11:18:27 -04003610
Steven Rostedte48c5292010-11-02 14:35:37 -04003611doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3612
Steven Rostedt2545eb62010-11-02 15:01:32 -04003613exit 0;