blob: 3d19ee4452497c736be0c0c53951039c99a8532c [file] [log] [blame]
Steven Rostedt2545eb62010-11-02 15:01:32 -04001#!/usr/bin/perl -w
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002#
Uwe Kleine-Königcce1dac2011-01-24 21:12:01 +01003# Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04004# Licensed under the terms of the GNU GPL License version 2
5#
Steven Rostedt2545eb62010-11-02 15:01:32 -04006
7use strict;
8use IPC::Open2;
9use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Steven Rostedt7faafbd2010-11-02 14:58:22 -040010use File::Path qw(mkpath);
11use File::Copy qw(cp);
Steven Rostedt2545eb62010-11-02 15:01:32 -040012use FileHandle;
13
Steven Rostedte48c5292010-11-02 14:35:37 -040014my $VERSION = "0.2";
15
Steven Rostedt2545eb62010-11-02 15:01:32 -040016$| = 1;
17
18my %opt;
Steven Rostedta57419b2010-11-02 15:13:54 -040019my %repeat_tests;
20my %repeats;
Steven Rostedt2545eb62010-11-02 15:01:32 -040021
22#default opts
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050023my %default = (
24 "NUM_TESTS" => 1,
25 "TEST_TYPE" => "build",
26 "BUILD_TYPE" => "randconfig",
27 "MAKE_CMD" => "make",
28 "TIMEOUT" => 120,
29 "TMP_DIR" => "/tmp/ktest/\${MACHINE}",
30 "SLEEP_TIME" => 60, # sleep time between tests
31 "BUILD_NOCLEAN" => 0,
32 "REBOOT_ON_ERROR" => 0,
33 "POWEROFF_ON_ERROR" => 0,
34 "REBOOT_ON_SUCCESS" => 1,
35 "POWEROFF_ON_SUCCESS" => 0,
36 "BUILD_OPTIONS" => "",
37 "BISECT_SLEEP_TIME" => 60, # sleep time between bisects
38 "PATCHCHECK_SLEEP_TIME" => 60, # sleep time between patch checks
39 "CLEAR_LOG" => 0,
40 "BISECT_MANUAL" => 0,
41 "BISECT_SKIP" => 1,
Steven Rostedtccc513b2012-05-21 17:13:40 -040042 "MIN_CONFIG_TYPE" => "boot",
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050043 "SUCCESS_LINE" => "login:",
44 "DETECT_TRIPLE_FAULT" => 1,
45 "NO_INSTALL" => 0,
46 "BOOTED_TIMEOUT" => 1,
47 "DIE_ON_FAILURE" => 1,
48 "SSH_EXEC" => "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND",
49 "SCP_TO_TARGET" => "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE",
Steven Rostedt02ad2612012-03-21 08:21:24 -040050 "SCP_TO_TARGET_INSTALL" => "\${SCP_TO_TARGET}",
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050051 "REBOOT" => "ssh \$SSH_USER\@\$MACHINE reboot",
52 "STOP_AFTER_SUCCESS" => 10,
53 "STOP_AFTER_FAILURE" => 60,
54 "STOP_TEST_AFTER" => 600,
Steven Rostedt407b95b2012-07-19 16:05:42 -040055 "MAX_MONITOR_WAIT" => 1800,
Steven Rostedta15ba912012-11-13 14:30:37 -050056 "GRUB_REBOOT" => "grub2-reboot",
Steven Rostedt77869542012-12-11 17:37:41 -050057 "SYSLINUX" => "extlinux",
58 "SYSLINUX_PATH" => "/boot/extlinux",
Steven Rostedt600bbf02011-11-21 20:12:04 -050059
60# required, and we will ask users if they don't have them but we keep the default
61# value something that is common.
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050062 "REBOOT_TYPE" => "grub",
63 "LOCALVERSION" => "-test",
64 "SSH_USER" => "root",
65 "BUILD_TARGET" => "arch/x86/boot/bzImage",
66 "TARGET_IMAGE" => "/boot/vmlinuz-test",
Steven Rostedt9cc9e092011-12-22 21:37:22 -050067
68 "LOG_FILE" => undef,
69 "IGNORE_UNUSED" => 0,
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050070);
Steven Rostedt2545eb62010-11-02 15:01:32 -040071
Steven Rostedt8d1491b2010-11-18 15:39:48 -050072my $ktest_config;
Steven Rostedt2545eb62010-11-02 15:01:32 -040073my $version;
Steven Rostedt683a3e62012-05-18 13:34:35 -040074my $have_version = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -040075my $machine;
Steven Rostedte48c5292010-11-02 14:35:37 -040076my $ssh_user;
Steven Rostedta75fece2010-11-02 14:58:27 -040077my $tmpdir;
78my $builddir;
79my $outputdir;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -050080my $output_config;
Steven Rostedta75fece2010-11-02 14:58:27 -040081my $test_type;
Steven Rostedt7faafbd2010-11-02 14:58:22 -040082my $build_type;
Steven Rostedta75fece2010-11-02 14:58:27 -040083my $build_options;
Steven Rostedt921ed4c2012-07-19 15:18:27 -040084my $final_post_ktest;
85my $pre_ktest;
86my $post_ktest;
87my $pre_test;
88my $post_test;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -040089my $pre_build;
90my $post_build;
91my $pre_build_die;
92my $post_build_die;
Steven Rostedta75fece2010-11-02 14:58:27 -040093my $reboot_type;
94my $reboot_script;
95my $power_cycle;
Steven Rostedte48c5292010-11-02 14:35:37 -040096my $reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -040097my $reboot_on_error;
Steven Rostedtbc7c5802011-12-22 16:29:10 -050098my $switch_to_good;
99my $switch_to_test;
Steven Rostedta75fece2010-11-02 14:58:27 -0400100my $poweroff_on_error;
Steven Rostedt648a1822012-03-21 11:18:27 -0400101my $reboot_on_success;
Steven Rostedta75fece2010-11-02 14:58:27 -0400102my $die_on_failure;
Steven Rostedt576f6272010-11-02 14:58:38 -0400103my $powercycle_after_reboot;
104my $poweroff_after_halt;
Steven Rostedt407b95b2012-07-19 16:05:42 -0400105my $max_monitor_wait;
Steven Rostedte48c5292010-11-02 14:35:37 -0400106my $ssh_exec;
107my $scp_to_target;
Steven Rostedt02ad2612012-03-21 08:21:24 -0400108my $scp_to_target_install;
Steven Rostedta75fece2010-11-02 14:58:27 -0400109my $power_off;
110my $grub_menu;
Steven Rostedta15ba912012-11-13 14:30:37 -0500111my $grub_file;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400112my $grub_number;
Steven Rostedta15ba912012-11-13 14:30:37 -0500113my $grub_reboot;
Steven Rostedt77869542012-12-11 17:37:41 -0500114my $syslinux;
115my $syslinux_path;
116my $syslinux_label;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400117my $target;
118my $make;
Steven Rostedte5c2ec12012-07-19 15:22:05 -0400119my $pre_install;
Steven Rostedt8b37ca82010-11-02 14:58:33 -0400120my $post_install;
Steven Rostedte0a87422011-09-30 17:50:48 -0400121my $no_install;
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400122my $noclean;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400123my $minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400124my $start_minconfig;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400125my $start_minconfig_defined;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400126my $output_minconfig;
Steven Rostedtccc513b2012-05-21 17:13:40 -0400127my $minconfig_type;
Steven Rostedt43de3312012-05-21 23:35:12 -0400128my $use_output_minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400129my $ignore_config;
Steven Rostedtbe405f92012-01-04 21:51:59 -0500130my $ignore_errors;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -0400131my $addconfig;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400132my $in_bisect = 0;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500133my $bisect_bad_commit = "";
Steven Rostedtd6ce2a02010-11-02 14:58:05 -0400134my $reverse_bisect;
Steven Rostedtc960bb92011-03-08 09:22:39 -0500135my $bisect_manual;
Steven Rostedtc23dca72011-03-08 09:26:31 -0500136my $bisect_skip;
Steven Rostedt30f75da2011-06-13 10:35:35 -0400137my $config_bisect_good;
Steven Rostedtc5dacb82011-12-22 12:43:57 -0500138my $bisect_ret_good;
139my $bisect_ret_bad;
140my $bisect_ret_skip;
141my $bisect_ret_abort;
142my $bisect_ret_default;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400143my $in_patchcheck = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400144my $run_test;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400145my $redirect;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400146my $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +0530147my $testlog;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400148my $dmesg;
149my $monitor_fp;
150my $monitor_pid;
151my $monitor_cnt = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -0400152my $sleep_time;
153my $bisect_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -0400154my $patchcheck_sleep_time;
Steven Rostedt19902072011-06-14 20:46:25 -0400155my $ignore_warnings;
Steven Rostedta75fece2010-11-02 14:58:27 -0400156my $store_failures;
Rabin Vincentde5b6e32011-11-18 17:05:31 +0530157my $store_successes;
Steven Rostedt9064af52011-06-13 10:38:48 -0400158my $test_name;
Steven Rostedta75fece2010-11-02 14:58:27 -0400159my $timeout;
160my $booted_timeout;
Steven Rostedtf1a5b962011-06-13 10:30:00 -0400161my $detect_triplefault;
Steven Rostedta75fece2010-11-02 14:58:27 -0400162my $console;
Steven Rostedt2b803362011-09-30 18:00:23 -0400163my $reboot_success_line;
Steven Rostedta75fece2010-11-02 14:58:27 -0400164my $success_line;
Steven Rostedt1c8a6172010-11-09 12:55:40 -0500165my $stop_after_success;
166my $stop_after_failure;
Steven Rostedt2d01b262011-03-08 09:47:54 -0500167my $stop_test_after;
Steven Rostedta75fece2010-11-02 14:58:27 -0400168my $build_target;
169my $target_image;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500170my $checkout;
Steven Rostedta75fece2010-11-02 14:58:27 -0400171my $localversion;
Steven Rostedt576f6272010-11-02 14:58:38 -0400172my $iteration = 0;
Steven Rostedte48c5292010-11-02 14:35:37 -0400173my $successes = 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400174
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500175my $bisect_good;
176my $bisect_bad;
177my $bisect_type;
178my $bisect_start;
179my $bisect_replay;
180my $bisect_files;
181my $bisect_reverse;
182my $bisect_check;
183
184my $config_bisect;
185my $config_bisect_type;
Steven Rostedtb0918612012-07-19 15:26:00 -0400186my $config_bisect_check;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500187
188my $patchcheck_type;
189my $patchcheck_start;
190my $patchcheck_end;
191
Steven Rostedt165708b2011-11-26 20:56:52 -0500192# set when a test is something other that just building or install
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500193# which would require more options.
194my $buildonly = 1;
195
Steven Rostedtdbd37832011-11-23 16:00:48 -0500196# set when creating a new config
197my $newconfig = 0;
198
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500199my %entered_configs;
200my %config_help;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400201my %variable;
Steven Rostedtcf79fab2012-07-19 15:29:43 -0400202
203# force_config is the list of configs that we force enabled (or disabled)
204# in a .config file. The MIN_CONFIG and ADD_CONFIG configs.
Steven Rostedtfcb3f162011-06-13 10:40:58 -0400205my %force_config;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500206
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400207# do not force reboots on config problems
208my $no_reboot = 1;
209
Steven Rostedt759a3cc2012-05-01 08:20:12 -0400210# reboot on success
211my $reboot_success = 0;
212
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500213my %option_map = (
214 "MACHINE" => \$machine,
215 "SSH_USER" => \$ssh_user,
216 "TMP_DIR" => \$tmpdir,
217 "OUTPUT_DIR" => \$outputdir,
218 "BUILD_DIR" => \$builddir,
219 "TEST_TYPE" => \$test_type,
Steven Rostedt921ed4c2012-07-19 15:18:27 -0400220 "PRE_KTEST" => \$pre_ktest,
221 "POST_KTEST" => \$post_ktest,
222 "PRE_TEST" => \$pre_test,
223 "POST_TEST" => \$post_test,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500224 "BUILD_TYPE" => \$build_type,
225 "BUILD_OPTIONS" => \$build_options,
226 "PRE_BUILD" => \$pre_build,
227 "POST_BUILD" => \$post_build,
228 "PRE_BUILD_DIE" => \$pre_build_die,
229 "POST_BUILD_DIE" => \$post_build_die,
230 "POWER_CYCLE" => \$power_cycle,
231 "REBOOT" => \$reboot,
232 "BUILD_NOCLEAN" => \$noclean,
233 "MIN_CONFIG" => \$minconfig,
234 "OUTPUT_MIN_CONFIG" => \$output_minconfig,
235 "START_MIN_CONFIG" => \$start_minconfig,
Steven Rostedtccc513b2012-05-21 17:13:40 -0400236 "MIN_CONFIG_TYPE" => \$minconfig_type,
Steven Rostedt43de3312012-05-21 23:35:12 -0400237 "USE_OUTPUT_MIN_CONFIG" => \$use_output_minconfig,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500238 "IGNORE_CONFIG" => \$ignore_config,
239 "TEST" => \$run_test,
240 "ADD_CONFIG" => \$addconfig,
241 "REBOOT_TYPE" => \$reboot_type,
242 "GRUB_MENU" => \$grub_menu,
Steven Rostedta15ba912012-11-13 14:30:37 -0500243 "GRUB_FILE" => \$grub_file,
244 "GRUB_REBOOT" => \$grub_reboot,
Steven Rostedt77869542012-12-11 17:37:41 -0500245 "SYSLINUX" => \$syslinux,
246 "SYSLINUX_PATH" => \$syslinux_path,
247 "SYSLINUX_LABEL" => \$syslinux_label,
Steven Rostedte5c2ec12012-07-19 15:22:05 -0400248 "PRE_INSTALL" => \$pre_install,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500249 "POST_INSTALL" => \$post_install,
250 "NO_INSTALL" => \$no_install,
251 "REBOOT_SCRIPT" => \$reboot_script,
252 "REBOOT_ON_ERROR" => \$reboot_on_error,
253 "SWITCH_TO_GOOD" => \$switch_to_good,
254 "SWITCH_TO_TEST" => \$switch_to_test,
255 "POWEROFF_ON_ERROR" => \$poweroff_on_error,
Steven Rostedt648a1822012-03-21 11:18:27 -0400256 "REBOOT_ON_SUCCESS" => \$reboot_on_success,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500257 "DIE_ON_FAILURE" => \$die_on_failure,
258 "POWER_OFF" => \$power_off,
259 "POWERCYCLE_AFTER_REBOOT" => \$powercycle_after_reboot,
260 "POWEROFF_AFTER_HALT" => \$poweroff_after_halt,
Steven Rostedt407b95b2012-07-19 16:05:42 -0400261 "MAX_MONITOR_WAIT" => \$max_monitor_wait,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500262 "SLEEP_TIME" => \$sleep_time,
263 "BISECT_SLEEP_TIME" => \$bisect_sleep_time,
264 "PATCHCHECK_SLEEP_TIME" => \$patchcheck_sleep_time,
265 "IGNORE_WARNINGS" => \$ignore_warnings,
Steven Rostedtbe405f92012-01-04 21:51:59 -0500266 "IGNORE_ERRORS" => \$ignore_errors,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500267 "BISECT_MANUAL" => \$bisect_manual,
268 "BISECT_SKIP" => \$bisect_skip,
269 "CONFIG_BISECT_GOOD" => \$config_bisect_good,
270 "BISECT_RET_GOOD" => \$bisect_ret_good,
271 "BISECT_RET_BAD" => \$bisect_ret_bad,
272 "BISECT_RET_SKIP" => \$bisect_ret_skip,
273 "BISECT_RET_ABORT" => \$bisect_ret_abort,
274 "BISECT_RET_DEFAULT" => \$bisect_ret_default,
275 "STORE_FAILURES" => \$store_failures,
276 "STORE_SUCCESSES" => \$store_successes,
277 "TEST_NAME" => \$test_name,
278 "TIMEOUT" => \$timeout,
279 "BOOTED_TIMEOUT" => \$booted_timeout,
280 "CONSOLE" => \$console,
281 "DETECT_TRIPLE_FAULT" => \$detect_triplefault,
282 "SUCCESS_LINE" => \$success_line,
283 "REBOOT_SUCCESS_LINE" => \$reboot_success_line,
284 "STOP_AFTER_SUCCESS" => \$stop_after_success,
285 "STOP_AFTER_FAILURE" => \$stop_after_failure,
286 "STOP_TEST_AFTER" => \$stop_test_after,
287 "BUILD_TARGET" => \$build_target,
288 "SSH_EXEC" => \$ssh_exec,
289 "SCP_TO_TARGET" => \$scp_to_target,
Steven Rostedt02ad2612012-03-21 08:21:24 -0400290 "SCP_TO_TARGET_INSTALL" => \$scp_to_target_install,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500291 "CHECKOUT" => \$checkout,
292 "TARGET_IMAGE" => \$target_image,
293 "LOCALVERSION" => \$localversion,
294
295 "BISECT_GOOD" => \$bisect_good,
296 "BISECT_BAD" => \$bisect_bad,
297 "BISECT_TYPE" => \$bisect_type,
298 "BISECT_START" => \$bisect_start,
299 "BISECT_REPLAY" => \$bisect_replay,
300 "BISECT_FILES" => \$bisect_files,
301 "BISECT_REVERSE" => \$bisect_reverse,
302 "BISECT_CHECK" => \$bisect_check,
303
304 "CONFIG_BISECT" => \$config_bisect,
305 "CONFIG_BISECT_TYPE" => \$config_bisect_type,
Steven Rostedtb0918612012-07-19 15:26:00 -0400306 "CONFIG_BISECT_CHECK" => \$config_bisect_check,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500307
308 "PATCHCHECK_TYPE" => \$patchcheck_type,
309 "PATCHCHECK_START" => \$patchcheck_start,
310 "PATCHCHECK_END" => \$patchcheck_end,
311);
312
313# Options may be used by other options, record them.
314my %used_options;
315
Steven Rostedt7bf51072011-10-22 09:07:03 -0400316# default variables that can be used
317chomp ($variable{"PWD"} = `pwd`);
318
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500319$config_help{"MACHINE"} = << "EOF"
320 The machine hostname that you will test.
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500321 For build only tests, it is still needed to differentiate log files.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500322EOF
323 ;
324$config_help{"SSH_USER"} = << "EOF"
325 The box is expected to have ssh on normal bootup, provide the user
326 (most likely root, since you need privileged operations)
327EOF
328 ;
329$config_help{"BUILD_DIR"} = << "EOF"
330 The directory that contains the Linux source code (full path).
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500331 You can use \${PWD} that will be the path where ktest.pl is run, or use
332 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500333EOF
334 ;
335$config_help{"OUTPUT_DIR"} = << "EOF"
336 The directory that the objects will be built (full path).
337 (can not be same as BUILD_DIR)
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500338 You can use \${PWD} that will be the path where ktest.pl is run, or use
339 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500340EOF
341 ;
342$config_help{"BUILD_TARGET"} = << "EOF"
343 The location of the compiled file to copy to the target.
344 (relative to OUTPUT_DIR)
345EOF
346 ;
Steven Rostedtdbd37832011-11-23 16:00:48 -0500347$config_help{"BUILD_OPTIONS"} = << "EOF"
348 Options to add to \"make\" when building.
349 i.e. -j20
350EOF
351 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500352$config_help{"TARGET_IMAGE"} = << "EOF"
353 The place to put your image on the test machine.
354EOF
355 ;
356$config_help{"POWER_CYCLE"} = << "EOF"
357 A script or command to reboot the box.
358
359 Here is a digital loggers power switch example
360 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
361
362 Here is an example to reboot a virtual box on the current host
363 with the name "Guest".
364 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
365EOF
366 ;
367$config_help{"CONSOLE"} = << "EOF"
368 The script or command that reads the console
369
370 If you use ttywatch server, something like the following would work.
371CONSOLE = nc -d localhost 3001
372
373 For a virtual machine with guest name "Guest".
374CONSOLE = virsh console Guest
375EOF
376 ;
377$config_help{"LOCALVERSION"} = << "EOF"
378 Required version ending to differentiate the test
379 from other linux builds on the system.
380EOF
381 ;
382$config_help{"REBOOT_TYPE"} = << "EOF"
383 Way to reboot the box to the test kernel.
Steven Rostedt77869542012-12-11 17:37:41 -0500384 Only valid options so far are "grub", "grub2", "syslinux", and "script".
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500385
386 If you specify grub, it will assume grub version 1
387 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
388 and select that target to reboot to the kernel. If this is not
389 your setup, then specify "script" and have a command or script
390 specified in REBOOT_SCRIPT to boot to the target.
391
392 The entry in /boot/grub/menu.lst must be entered in manually.
393 The test will not modify that file.
Steven Rostedta15ba912012-11-13 14:30:37 -0500394
395 If you specify grub2, then you also need to specify both \$GRUB_MENU
396 and \$GRUB_FILE.
Steven Rostedt77869542012-12-11 17:37:41 -0500397
398 If you specify syslinux, then you may use SYSLINUX to define the syslinux
399 command (defaults to extlinux), and SYSLINUX_PATH to specify the path to
400 the syslinux install (defaults to /boot/extlinux). But you have to specify
401 SYSLINUX_LABEL to define the label to boot to for the test kernel.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500402EOF
403 ;
404$config_help{"GRUB_MENU"} = << "EOF"
405 The grub title name for the test kernel to boot
Steven Rostedta15ba912012-11-13 14:30:37 -0500406 (Only mandatory if REBOOT_TYPE = grub or grub2)
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500407
408 Note, ktest.pl will not update the grub menu.lst, you need to
409 manually add an option for the test. ktest.pl will search
410 the grub menu.lst for this option to find what kernel to
411 reboot into.
412
413 For example, if in the /boot/grub/menu.lst the test kernel title has:
414 title Test Kernel
415 kernel vmlinuz-test
416 GRUB_MENU = Test Kernel
Steven Rostedta15ba912012-11-13 14:30:37 -0500417
418 For grub2, a search of \$GRUB_FILE is performed for the lines
419 that begin with "menuentry". It will not detect submenus. The
420 menu must be a non-nested menu. Add the quotes used in the menu
421 to guarantee your selection, as the first menuentry with the content
422 of \$GRUB_MENU that is found will be used.
423EOF
424 ;
425$config_help{"GRUB_FILE"} = << "EOF"
426 If grub2 is used, the full path for the grub.cfg file is placed
427 here. Use something like /boot/grub2/grub.cfg to search.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500428EOF
429 ;
Steven Rostedt77869542012-12-11 17:37:41 -0500430$config_help{"SYSLINUX_LABEL"} = << "EOF"
431 If syslinux is used, the label that boots the target kernel must
432 be specified with SYSLINUX_LABEL.
433EOF
434 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500435$config_help{"REBOOT_SCRIPT"} = << "EOF"
436 A script to reboot the target into the test kernel
437 (Only mandatory if REBOOT_TYPE = script)
438EOF
439 ;
440
Steven Rostedtdad98752011-11-22 20:48:57 -0500441sub read_prompt {
442 my ($cancel, $prompt) = @_;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400443
444 my $ans;
445
446 for (;;) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500447 if ($cancel) {
448 print "$prompt [y/n/C] ";
449 } else {
450 print "$prompt [Y/n] ";
451 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400452 $ans = <STDIN>;
453 chomp $ans;
454 if ($ans =~ /^\s*$/) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500455 if ($cancel) {
456 $ans = "c";
457 } else {
458 $ans = "y";
459 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400460 }
461 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
Steven Rostedtdad98752011-11-22 20:48:57 -0500462 if ($cancel) {
463 last if ($ans =~ /^c$/i);
464 print "Please answer either 'y', 'n' or 'c'.\n";
465 } else {
466 print "Please answer either 'y' or 'n'.\n";
467 }
468 }
469 if ($ans =~ /^c/i) {
470 exit;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400471 }
472 if ($ans !~ /^y$/i) {
473 return 0;
474 }
475 return 1;
476}
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500477
Steven Rostedtdad98752011-11-22 20:48:57 -0500478sub read_yn {
479 my ($prompt) = @_;
480
481 return read_prompt 0, $prompt;
482}
483
484sub read_ync {
485 my ($prompt) = @_;
486
487 return read_prompt 1, $prompt;
488}
489
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500490sub get_ktest_config {
491 my ($config) = @_;
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400492 my $ans;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500493
494 return if (defined($opt{$config}));
495
496 if (defined($config_help{$config})) {
497 print "\n";
498 print $config_help{$config};
499 }
500
501 for (;;) {
502 print "$config = ";
Steven Rostedtdbd37832011-11-23 16:00:48 -0500503 if (defined($default{$config}) && length($default{$config})) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500504 print "\[$default{$config}\] ";
505 }
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400506 $ans = <STDIN>;
507 $ans =~ s/^\s*(.*\S)\s*$/$1/;
508 if ($ans =~ /^\s*$/) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500509 if ($default{$config}) {
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400510 $ans = $default{$config};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500511 } else {
512 print "Your answer can not be blank\n";
513 next;
514 }
515 }
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500516 $entered_configs{$config} = ${ans};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500517 last;
518 }
519}
520
521sub get_ktest_configs {
522 get_ktest_config("MACHINE");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500523 get_ktest_config("BUILD_DIR");
524 get_ktest_config("OUTPUT_DIR");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500525
Steven Rostedtdbd37832011-11-23 16:00:48 -0500526 if ($newconfig) {
527 get_ktest_config("BUILD_OPTIONS");
528 }
529
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500530 # options required for other than just building a kernel
531 if (!$buildonly) {
Steven Rostedt165708b2011-11-26 20:56:52 -0500532 get_ktest_config("POWER_CYCLE");
533 get_ktest_config("CONSOLE");
534 }
535
536 # options required for install and more
537 if ($buildonly != 1) {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500538 get_ktest_config("SSH_USER");
539 get_ktest_config("BUILD_TARGET");
540 get_ktest_config("TARGET_IMAGE");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500541 }
542
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500543 get_ktest_config("LOCALVERSION");
544
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500545 return if ($buildonly);
546
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500547 my $rtype = $opt{"REBOOT_TYPE"};
548
549 if (!defined($rtype)) {
550 if (!defined($opt{"GRUB_MENU"})) {
551 get_ktest_config("REBOOT_TYPE");
552 $rtype = $entered_configs{"REBOOT_TYPE"};
553 } else {
554 $rtype = "grub";
555 }
556 }
557
558 if ($rtype eq "grub") {
559 get_ktest_config("GRUB_MENU");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500560 }
Steven Rostedta15ba912012-11-13 14:30:37 -0500561
562 if ($rtype eq "grub2") {
563 get_ktest_config("GRUB_MENU");
564 get_ktest_config("GRUB_FILE");
565 }
Steven Rostedt77869542012-12-11 17:37:41 -0500566
567 if ($rtype eq "syslinux") {
568 get_ktest_config("SYSLINUX_LABEL");
569 }
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500570}
571
Steven Rostedt77d942c2011-05-20 13:36:58 -0400572sub process_variables {
Steven Rostedt8d735212011-10-17 11:36:44 -0400573 my ($value, $remove_undef) = @_;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400574 my $retval = "";
575
576 # We want to check for '\', and it is just easier
577 # to check the previous characet of '$' and not need
578 # to worry if '$' is the first character. By adding
579 # a space to $value, we can just check [^\\]\$ and
580 # it will still work.
581 $value = " $value";
582
583 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
584 my $begin = $1;
585 my $var = $2;
586 my $end = $3;
587 # append beginning of value to retval
588 $retval = "$retval$begin";
589 if (defined($variable{$var})) {
590 $retval = "$retval$variable{$var}";
Steven Rostedt8d735212011-10-17 11:36:44 -0400591 } elsif (defined($remove_undef) && $remove_undef) {
592 # for if statements, any variable that is not defined,
593 # we simple convert to 0
594 $retval = "${retval}0";
Steven Rostedt77d942c2011-05-20 13:36:58 -0400595 } else {
596 # put back the origin piece.
597 $retval = "$retval\$\{$var\}";
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500598 # This could be an option that is used later, save
599 # it so we don't warn if this option is not one of
600 # ktests options.
601 $used_options{$var} = 1;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400602 }
603 $value = $end;
604 }
605 $retval = "$retval$value";
606
607 # remove the space added in the beginning
608 $retval =~ s/ //;
609
610 return "$retval"
611}
612
Steven Rostedta57419b2010-11-02 15:13:54 -0400613sub set_value {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400614 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
Steven Rostedta57419b2010-11-02 15:13:54 -0400615
Steven Rostedtcad96662011-12-22 11:32:52 -0500616 my $prvalue = process_variables($rvalue);
617
618 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500619 # Note if a test is something other than build, then we
620 # will need other manditory options.
Steven Rostedtcad96662011-12-22 11:32:52 -0500621 if ($prvalue ne "install") {
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -0500622 # for bisect, we need to check BISECT_TYPE
623 if ($prvalue ne "bisect") {
624 $buildonly = 0;
625 }
626 } else {
627 # install still limits some manditory options.
628 $buildonly = 2;
629 }
630 }
631
632 if ($buildonly && $lvalue =~ /^BISECT_TYPE(\[.*\])?$/ && $prvalue ne "build") {
633 if ($prvalue ne "install") {
Steven Rostedt165708b2011-11-26 20:56:52 -0500634 $buildonly = 0;
635 } else {
636 # install still limits some manditory options.
637 $buildonly = 2;
638 }
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500639 }
640
Steven Rostedta57419b2010-11-02 15:13:54 -0400641 if (defined($opt{$lvalue})) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400642 if (!$override || defined(${$overrides}{$lvalue})) {
643 my $extra = "";
644 if ($override) {
645 $extra = "In the same override section!\n";
646 }
647 die "$name: $.: Option $lvalue defined more than once!\n$extra";
648 }
Steven Rostedtcad96662011-12-22 11:32:52 -0500649 ${$overrides}{$lvalue} = $prvalue;
Steven Rostedta57419b2010-11-02 15:13:54 -0400650 }
Steven Rostedt21a96792010-11-08 16:45:50 -0500651 if ($rvalue =~ /^\s*$/) {
652 delete $opt{$lvalue};
653 } else {
Steven Rostedtcad96662011-12-22 11:32:52 -0500654 $opt{$lvalue} = $prvalue;
Steven Rostedt21a96792010-11-08 16:45:50 -0500655 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400656}
657
Steven Rostedt77d942c2011-05-20 13:36:58 -0400658sub set_variable {
659 my ($lvalue, $rvalue) = @_;
660
661 if ($rvalue =~ /^\s*$/) {
662 delete $variable{$lvalue};
663 } else {
664 $rvalue = process_variables($rvalue);
665 $variable{$lvalue} = $rvalue;
666 }
667}
668
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400669sub process_compare {
670 my ($lval, $cmp, $rval) = @_;
671
672 # remove whitespace
673
674 $lval =~ s/^\s*//;
675 $lval =~ s/\s*$//;
676
677 $rval =~ s/^\s*//;
678 $rval =~ s/\s*$//;
679
680 if ($cmp eq "==") {
681 return $lval eq $rval;
682 } elsif ($cmp eq "!=") {
683 return $lval ne $rval;
Steven Rostedt8fddbe92012-07-30 14:37:01 -0400684 } elsif ($cmp eq "=~") {
685 return $lval =~ m/$rval/;
686 } elsif ($cmp eq "!~") {
687 return $lval !~ m/$rval/;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400688 }
689
690 my $statement = "$lval $cmp $rval";
691 my $ret = eval $statement;
692
693 # $@ stores error of eval
694 if ($@) {
695 return -1;
696 }
697
698 return $ret;
699}
700
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400701sub value_defined {
702 my ($val) = @_;
703
704 return defined($variable{$2}) ||
705 defined($opt{$2});
706}
707
Steven Rostedt8d735212011-10-17 11:36:44 -0400708my $d = 0;
709sub process_expression {
710 my ($name, $val) = @_;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400711
Steven Rostedt8d735212011-10-17 11:36:44 -0400712 my $c = $d++;
713
714 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
715 my $express = $1;
716
717 if (process_expression($name, $express)) {
718 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
719 } else {
720 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
721 }
722 }
723
724 $d--;
725 my $OR = "\\|\\|";
726 my $AND = "\\&\\&";
727
728 while ($val =~ s/^(.*?)($OR|$AND)//) {
729 my $express = $1;
730 my $op = $2;
731
732 if (process_expression($name, $express)) {
733 if ($op eq "||") {
734 return 1;
735 }
736 } else {
737 if ($op eq "&&") {
738 return 0;
739 }
740 }
741 }
Steven Rostedt45d73a52011-09-30 19:44:53 -0400742
Steven Rostedt8fddbe92012-07-30 14:37:01 -0400743 if ($val =~ /(.*)(==|\!=|>=|<=|>|<|=~|\!~)(.*)/) {
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400744 my $ret = process_compare($1, $2, $3);
745 if ($ret < 0) {
746 die "$name: $.: Unable to process comparison\n";
747 }
748 return $ret;
749 }
750
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400751 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
752 if (defined $1) {
753 return !value_defined($2);
754 } else {
755 return value_defined($2);
756 }
757 }
758
Steven Rostedt45d73a52011-09-30 19:44:53 -0400759 if ($val =~ /^\s*0\s*$/) {
760 return 0;
761 } elsif ($val =~ /^\s*\d+\s*$/) {
762 return 1;
763 }
764
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400765 die ("$name: $.: Undefined content $val in if statement\n");
Steven Rostedt8d735212011-10-17 11:36:44 -0400766}
767
768sub process_if {
769 my ($name, $value) = @_;
770
771 # Convert variables and replace undefined ones with 0
772 my $val = process_variables($value, 1);
773 my $ret = process_expression $name, $val;
774
775 return $ret;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400776}
777
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400778sub __read_config {
779 my ($config, $current_test_num) = @_;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400780
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400781 my $in;
782 open($in, $config) || die "can't read file $config";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400783
Steven Rostedta57419b2010-11-02 15:13:54 -0400784 my $name = $config;
785 $name =~ s,.*/(.*),$1,;
786
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400787 my $test_num = $$current_test_num;
Steven Rostedta57419b2010-11-02 15:13:54 -0400788 my $default = 1;
789 my $repeat = 1;
790 my $num_tests_set = 0;
791 my $skip = 0;
792 my $rest;
Steven Rostedta9f84422011-10-17 11:06:29 -0400793 my $line;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400794 my $test_case = 0;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400795 my $if = 0;
796 my $if_set = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400797 my $override = 0;
798
799 my %overrides;
Steven Rostedta57419b2010-11-02 15:13:54 -0400800
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400801 while (<$in>) {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400802
803 # ignore blank lines and comments
804 next if (/^\s*$/ || /\s*\#/);
805
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400806 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400807
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400808 my $type = $1;
809 $rest = $2;
Steven Rostedta9f84422011-10-17 11:06:29 -0400810 $line = $2;
Steven Rostedta57419b2010-11-02 15:13:54 -0400811
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400812 my $old_test_num;
813 my $old_repeat;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400814 $override = 0;
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400815
816 if ($type eq "TEST_START") {
817
818 if ($num_tests_set) {
819 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
820 }
821
822 $old_test_num = $test_num;
823 $old_repeat = $repeat;
824
825 $test_num += $repeat;
826 $default = 0;
827 $repeat = 1;
828 } else {
829 $default = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400830 }
831
Steven Rostedta9f84422011-10-17 11:06:29 -0400832 # If SKIP is anywhere in the line, the command will be skipped
833 if ($rest =~ s/\s+SKIP\b//) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400834 $skip = 1;
835 } else {
Steven Rostedt0df213c2011-06-14 20:51:37 -0400836 $test_case = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400837 $skip = 0;
838 }
839
Steven Rostedta9f84422011-10-17 11:06:29 -0400840 if ($rest =~ s/\sELSE\b//) {
841 if (!$if) {
842 die "$name: $.: ELSE found with out matching IF section\n$_";
843 }
844 $if = 0;
845
846 if ($if_set) {
847 $skip = 1;
848 } else {
849 $skip = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400850 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400851 }
852
Steven Rostedta9f84422011-10-17 11:06:29 -0400853 if ($rest =~ s/\sIF\s+(.*)//) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400854 if (process_if($name, $1)) {
855 $if_set = 1;
856 } else {
857 $skip = 1;
858 }
859 $if = 1;
860 } else {
861 $if = 0;
Steven Rostedta9f84422011-10-17 11:06:29 -0400862 $if_set = 0;
Steven Rostedta57419b2010-11-02 15:13:54 -0400863 }
864
Steven Rostedta9f84422011-10-17 11:06:29 -0400865 if (!$skip) {
866 if ($type eq "TEST_START") {
867 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
868 $repeat = $1;
869 $repeat_tests{"$test_num"} = $repeat;
870 }
871 } elsif ($rest =~ s/\sOVERRIDE\b//) {
872 # DEFAULT only
873 $override = 1;
874 # Clear previous overrides
875 %overrides = ();
876 }
877 }
878
879 if (!$skip && $rest !~ /^\s*$/) {
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400880 die "$name: $.: Gargbage found after $type\n$_";
Steven Rostedta57419b2010-11-02 15:13:54 -0400881 }
882
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400883 if ($skip && $type eq "TEST_START") {
Steven Rostedta57419b2010-11-02 15:13:54 -0400884 $test_num = $old_test_num;
Steven Rostedte48c5292010-11-02 14:35:37 -0400885 $repeat = $old_repeat;
Steven Rostedta57419b2010-11-02 15:13:54 -0400886 }
887
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400888 } elsif (/^\s*ELSE\b(.*)$/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400889 if (!$if) {
890 die "$name: $.: ELSE found with out matching IF section\n$_";
891 }
892 $rest = $1;
893 if ($if_set) {
894 $skip = 1;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400895 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400896 } else {
897 $skip = 0;
898
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400899 if ($rest =~ /\sIF\s+(.*)/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400900 # May be a ELSE IF section.
Steven Rostedt95f57832012-09-26 14:48:17 -0400901 if (process_if($name, $1)) {
902 $if_set = 1;
903 } else {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400904 $skip = 1;
905 }
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400906 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400907 } else {
908 $if = 0;
909 }
910 }
911
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400912 if ($rest !~ /^\s*$/) {
913 die "$name: $.: Gargbage found after DEFAULTS\n$_";
914 }
915
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400916 } elsif (/^\s*INCLUDE\s+(\S+)/) {
917
918 next if ($skip);
919
920 if (!$default) {
921 die "$name: $.: INCLUDE can only be done in default sections\n$_";
922 }
923
924 my $file = process_variables($1);
925
926 if ($file !~ m,^/,) {
927 # check the path of the config file first
928 if ($config =~ m,(.*)/,) {
929 if (-f "$1/$file") {
930 $file = "$1/$file";
931 }
932 }
933 }
934
935 if ( ! -r $file ) {
936 die "$name: $.: Can't read file $file\n$_";
937 }
938
939 if (__read_config($file, \$test_num)) {
940 $test_case = 1;
941 }
942
Steven Rostedta57419b2010-11-02 15:13:54 -0400943 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
944
945 next if ($skip);
946
Steven Rostedt2545eb62010-11-02 15:01:32 -0400947 my $lvalue = $1;
948 my $rvalue = $2;
949
Steven Rostedta57419b2010-11-02 15:13:54 -0400950 if (!$default &&
951 ($lvalue eq "NUM_TESTS" ||
952 $lvalue eq "LOG_FILE" ||
953 $lvalue eq "CLEAR_LOG")) {
954 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400955 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400956
957 if ($lvalue eq "NUM_TESTS") {
958 if ($test_num) {
959 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
960 }
961 if (!$default) {
962 die "$name: $.: NUM_TESTS must be set in default section\n";
963 }
964 $num_tests_set = 1;
965 }
966
967 if ($default || $lvalue =~ /\[\d+\]$/) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400968 set_value($lvalue, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400969 } else {
970 my $val = "$lvalue\[$test_num\]";
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400971 set_value($val, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400972
973 if ($repeat > 1) {
974 $repeats{$val} = $repeat;
975 }
976 }
Steven Rostedt77d942c2011-05-20 13:36:58 -0400977 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
978 next if ($skip);
979
980 my $lvalue = $1;
981 my $rvalue = $2;
982
983 # process config variables.
984 # Config variables are only active while reading the
985 # config and can be defined anywhere. They also ignore
986 # TEST_START and DEFAULTS, but are skipped if they are in
987 # on of these sections that have SKIP defined.
988 # The save variable can be
989 # defined multiple times and the new one simply overrides
990 # the prevous one.
991 set_variable($lvalue, $rvalue);
992
Steven Rostedta57419b2010-11-02 15:13:54 -0400993 } else {
994 die "$name: $.: Garbage found in config\n$_";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400995 }
996 }
997
Steven Rostedta57419b2010-11-02 15:13:54 -0400998 if ($test_num) {
999 $test_num += $repeat - 1;
1000 $opt{"NUM_TESTS"} = $test_num;
1001 }
1002
Steven Rostedt2ed3b162011-09-30 21:00:00 -04001003 close($in);
1004
1005 $$current_test_num = $test_num;
1006
1007 return $test_case;
1008}
1009
Steven Rostedtc4261d02011-11-23 13:41:18 -05001010sub get_test_case {
1011 print "What test case would you like to run?\n";
1012 print " (build, install or boot)\n";
1013 print " Other tests are available but require editing the config file\n";
1014 my $ans = <STDIN>;
1015 chomp $ans;
1016 $default{"TEST_TYPE"} = $ans;
1017}
1018
Steven Rostedt2ed3b162011-09-30 21:00:00 -04001019sub read_config {
1020 my ($config) = @_;
1021
1022 my $test_case;
1023 my $test_num = 0;
1024
1025 $test_case = __read_config $config, \$test_num;
1026
Steven Rostedt8d1491b2010-11-18 15:39:48 -05001027 # make sure we have all mandatory configs
1028 get_ktest_configs;
1029
Steven Rostedt0df213c2011-06-14 20:51:37 -04001030 # was a test specified?
1031 if (!$test_case) {
1032 print "No test case specified.\n";
Steven Rostedtc4261d02011-11-23 13:41:18 -05001033 get_test_case;
Steven Rostedt0df213c2011-06-14 20:51:37 -04001034 }
1035
Steven Rostedta75fece2010-11-02 14:58:27 -04001036 # set any defaults
1037
1038 foreach my $default (keys %default) {
1039 if (!defined($opt{$default})) {
1040 $opt{$default} = $default{$default};
1041 }
1042 }
Steven Rostedt9cc9e092011-12-22 21:37:22 -05001043
1044 if ($opt{"IGNORE_UNUSED"} == 1) {
1045 return;
1046 }
1047
1048 my %not_used;
1049
1050 # check if there are any stragglers (typos?)
1051 foreach my $option (keys %opt) {
1052 my $op = $option;
1053 # remove per test labels.
1054 $op =~ s/\[.*\]//;
1055 if (!exists($option_map{$op}) &&
1056 !exists($default{$op}) &&
1057 !exists($used_options{$op})) {
1058 $not_used{$op} = 1;
1059 }
1060 }
1061
1062 if (%not_used) {
1063 my $s = "s are";
1064 $s = " is" if (keys %not_used == 1);
1065 print "The following option$s not used; could be a typo:\n";
1066 foreach my $option (keys %not_used) {
1067 print "$option\n";
1068 }
1069 print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
1070 if (!read_yn "Do you want to continue?") {
1071 exit -1;
1072 }
1073 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001074}
1075
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001076sub __eval_option {
Steven Rostedt (Red Hat)04262be2013-01-31 10:12:20 -05001077 my ($name, $option, $i) = @_;
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001078
1079 # Add space to evaluate the character before $
1080 $option = " $option";
1081 my $retval = "";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301082 my $repeated = 0;
1083 my $parent = 0;
1084
1085 foreach my $test (keys %repeat_tests) {
1086 if ($i >= $test &&
1087 $i < $test + $repeat_tests{$test}) {
1088
1089 $repeated = 1;
1090 $parent = $test;
1091 last;
1092 }
1093 }
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001094
1095 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
1096 my $start = $1;
1097 my $var = $2;
1098 my $end = $3;
1099
1100 # Append beginning of line
1101 $retval = "$retval$start";
1102
1103 # If the iteration option OPT[$i] exists, then use that.
1104 # otherwise see if the default OPT (without [$i]) exists.
1105
1106 my $o = "$var\[$i\]";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301107 my $parento = "$var\[$parent\]";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001108
Steven Rostedt (Red Hat)04262be2013-01-31 10:12:20 -05001109 # If a variable contains itself, use the default var
1110 if (($var eq $name) && defined($opt{$var})) {
1111 $o = $opt{$var};
1112 $retval = "$retval$o";
1113 } elsif (defined($opt{$o})) {
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001114 $o = $opt{$o};
1115 $retval = "$retval$o";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301116 } elsif ($repeated && defined($opt{$parento})) {
1117 $o = $opt{$parento};
1118 $retval = "$retval$o";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001119 } elsif (defined($opt{$var})) {
1120 $o = $opt{$var};
1121 $retval = "$retval$o";
1122 } else {
1123 $retval = "$retval\$\{$var\}";
1124 }
1125
1126 $option = $end;
1127 }
1128
1129 $retval = "$retval$option";
1130
1131 $retval =~ s/^ //;
1132
1133 return $retval;
1134}
1135
1136sub eval_option {
Steven Rostedt (Red Hat)04262be2013-01-31 10:12:20 -05001137 my ($name, $option, $i) = @_;
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001138
1139 my $prev = "";
1140
1141 # Since an option can evaluate to another option,
1142 # keep iterating until we do not evaluate any more
1143 # options.
1144 my $r = 0;
1145 while ($prev ne $option) {
1146 # Check for recursive evaluations.
1147 # 100 deep should be more than enough.
1148 if ($r++ > 100) {
1149 die "Over 100 evaluations accurred with $option\n" .
1150 "Check for recursive variables\n";
1151 }
1152 $prev = $option;
Steven Rostedt (Red Hat)04262be2013-01-31 10:12:20 -05001153 $option = __eval_option($name, $option, $i);
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001154 }
1155
1156 return $option;
1157}
1158
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001159sub _logit {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001160 if (defined($opt{"LOG_FILE"})) {
1161 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
1162 print OUT @_;
1163 close(OUT);
1164 }
1165}
1166
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001167sub logit {
1168 if (defined($opt{"LOG_FILE"})) {
1169 _logit @_;
1170 } else {
1171 print @_;
1172 }
1173}
1174
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001175sub doprint {
1176 print @_;
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001177 _logit @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001178}
1179
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001180sub run_command;
Andrew Jones2728be42011-08-12 15:32:05 +02001181sub start_monitor;
1182sub end_monitor;
1183sub wait_for_monitor;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001184
1185sub reboot {
Andrew Jones2728be42011-08-12 15:32:05 +02001186 my ($time) = @_;
1187
Steven Rostedta4968722012-12-11 14:59:05 -05001188 # Make sure everything has been written to disk
1189 run_ssh("sync");
1190
Steven Rostedt2b803362011-09-30 18:00:23 -04001191 if (defined($time)) {
1192 start_monitor;
1193 # flush out current monitor
1194 # May contain the reboot success line
1195 wait_for_monitor 1;
1196 }
1197
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001198 # try to reboot normally
Steven Rostedte48c5292010-11-02 14:35:37 -04001199 if (run_command $reboot) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001200 if (defined($powercycle_after_reboot)) {
1201 sleep $powercycle_after_reboot;
1202 run_command "$power_cycle";
1203 }
1204 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001205 # nope? power cycle it.
Steven Rostedta75fece2010-11-02 14:58:27 -04001206 run_command "$power_cycle";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001207 }
Andrew Jones2728be42011-08-12 15:32:05 +02001208
1209 if (defined($time)) {
Steven Rostedt407b95b2012-07-19 16:05:42 -04001210 if (wait_for_monitor($time, $reboot_success_line)) {
1211 # reboot got stuck?
Steven Rostedt8a80c722012-07-19 16:08:33 -04001212 doprint "Reboot did not finish. Forcing power cycle\n";
Steven Rostedt407b95b2012-07-19 16:05:42 -04001213 run_command "$power_cycle";
1214 }
Andrew Jones2728be42011-08-12 15:32:05 +02001215 end_monitor;
1216 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001217}
1218
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001219sub reboot_to_good {
1220 my ($time) = @_;
1221
1222 if (defined($switch_to_good)) {
1223 run_command $switch_to_good;
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001224 }
1225
1226 reboot $time;
1227}
1228
Steven Rostedt576f6272010-11-02 14:58:38 -04001229sub do_not_reboot {
1230 my $i = $iteration;
1231
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001232 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -04001233 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1234 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1235}
1236
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001237sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001238 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001239
Steven Rostedt576f6272010-11-02 14:58:38 -04001240 my $i = $iteration;
1241
1242 if ($reboot_on_error && !do_not_reboot) {
1243
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001244 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001245 reboot_to_good;
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001246
Steven Rostedta75fece2010-11-02 14:58:27 -04001247 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001248 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001249 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001250 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001251
Steven Rostedtf80802c2011-03-07 13:18:47 -05001252 if (defined($opt{"LOG_FILE"})) {
1253 print " See $opt{LOG_FILE} for more info.\n";
1254 }
1255
Steven Rostedt576f6272010-11-02 14:58:38 -04001256 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001257}
1258
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001259sub open_console {
1260 my ($fp) = @_;
1261
1262 my $flags;
1263
Steven Rostedta75fece2010-11-02 14:58:27 -04001264 my $pid = open($fp, "$console|") or
1265 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001266
1267 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001268 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001269 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001270 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001271
1272 return $pid;
1273}
1274
1275sub close_console {
1276 my ($fp, $pid) = @_;
1277
1278 doprint "kill child process $pid\n";
1279 kill 2, $pid;
1280
1281 print "closing!\n";
1282 close($fp);
1283}
1284
1285sub start_monitor {
1286 if ($monitor_cnt++) {
1287 return;
1288 }
1289 $monitor_fp = \*MONFD;
1290 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001291
1292 return;
1293
1294 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001295}
1296
1297sub end_monitor {
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001298 return if (!defined $console);
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001299 if (--$monitor_cnt) {
1300 return;
1301 }
1302 close_console($monitor_fp, $monitor_pid);
1303}
1304
1305sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001306 my ($time, $stop) = @_;
1307 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001308 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001309 my $booted = 0;
Steven Rostedt407b95b2012-07-19 16:05:42 -04001310 my $start_time = time;
Steven Rostedt8a80c722012-07-19 16:08:33 -04001311 my $skip_call_trace = 0;
1312 my $bug = 0;
1313 my $bug_ignored = 0;
Steven Rostedt407b95b2012-07-19 16:05:42 -04001314 my $now;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001315
Steven Rostedta75fece2010-11-02 14:58:27 -04001316 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001317
1318 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001319 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001320 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001321 last if (!defined($line));
1322 print "$line";
1323 $full_line .= $line;
1324
1325 if (defined($stop) && $full_line =~ /$stop/) {
1326 doprint "wait for monitor detected $stop\n";
1327 $booted = 1;
1328 }
1329
Steven Rostedt8a80c722012-07-19 16:08:33 -04001330 if ($full_line =~ /\[ backtrace testing \]/) {
1331 $skip_call_trace = 1;
1332 }
1333
1334 if ($full_line =~ /call trace:/i) {
1335 if (!$bug && !$skip_call_trace) {
1336 if ($ignore_errors) {
1337 $bug_ignored = 1;
1338 } else {
1339 $bug = 1;
1340 }
1341 }
1342 }
1343
1344 if ($full_line =~ /\[ end of backtrace testing \]/) {
1345 $skip_call_trace = 0;
1346 }
1347
1348 if ($full_line =~ /Kernel panic -/) {
1349 $bug = 1;
1350 }
1351
Steven Rostedt2b803362011-09-30 18:00:23 -04001352 if ($line =~ /\n/) {
1353 $full_line = "";
1354 }
Steven Rostedt407b95b2012-07-19 16:05:42 -04001355 $now = time;
1356 if ($now - $start_time >= $max_monitor_wait) {
1357 doprint "Exiting monitor flush due to hitting MAX_MONITOR_WAIT\n";
1358 return 1;
1359 }
Steven Rostedt2b803362011-09-30 18:00:23 -04001360 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001361 print "** Monitor flushed **\n";
Steven Rostedt8a80c722012-07-19 16:08:33 -04001362 return $bug;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001363}
1364
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301365sub save_logs {
1366 my ($result, $basedir) = @_;
1367 my @t = localtime;
1368 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1369 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1370
1371 my $type = $build_type;
1372 if ($type =~ /useconfig/) {
1373 $type = "useconfig";
1374 }
1375
1376 my $dir = "$machine-$test_type-$type-$result-$date";
1377
1378 $dir = "$basedir/$dir";
1379
1380 if (!-d $dir) {
1381 mkpath($dir) or
1382 die "can't create $dir";
1383 }
1384
1385 my %files = (
1386 "config" => $output_config,
1387 "buildlog" => $buildlog,
1388 "dmesg" => $dmesg,
1389 "testlog" => $testlog,
1390 );
1391
1392 while (my ($name, $source) = each(%files)) {
1393 if (-f "$source") {
1394 cp "$source", "$dir/$name" or
1395 die "failed to copy $source";
1396 }
1397 }
1398
1399 doprint "*** Saved info to $dir ***\n";
1400}
1401
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001402sub fail {
1403
Steven Rostedt921ed4c2012-07-19 15:18:27 -04001404 if (defined($post_test)) {
1405 run_command $post_test;
1406 }
1407
Steven Rostedta75fece2010-11-02 14:58:27 -04001408 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001409 dodie @_;
1410 }
1411
Steven Rostedta75fece2010-11-02 14:58:27 -04001412 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001413
Steven Rostedt576f6272010-11-02 14:58:38 -04001414 my $i = $iteration;
1415
Steven Rostedta75fece2010-11-02 14:58:27 -04001416 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001417 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001418 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001419 reboot_to_good $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001420 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001421
Steven Rostedt9064af52011-06-13 10:38:48 -04001422 my $name = "";
1423
1424 if (defined($test_name)) {
1425 $name = " ($test_name)";
1426 }
1427
Steven Rostedt576f6272010-11-02 14:58:38 -04001428 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1429 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001430 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001431 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1432 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001433
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301434 if (defined($store_failures)) {
1435 save_logs "fail", $store_failures;
1436 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001437
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001438 return 1;
1439}
1440
Steven Rostedt2545eb62010-11-02 15:01:32 -04001441sub run_command {
1442 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001443 my $dolog = 0;
1444 my $dord = 0;
1445 my $pid;
1446
Steven Rostedte48c5292010-11-02 14:35:37 -04001447 $command =~ s/\$SSH_USER/$ssh_user/g;
1448 $command =~ s/\$MACHINE/$machine/g;
1449
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001450 doprint("$command ... ");
1451
1452 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001453 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001454
1455 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001456 open(LOG, ">>$opt{LOG_FILE}") or
1457 dodie "failed to write to log";
1458 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001459 }
1460
1461 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001462 open (RD, ">$redirect") or
1463 dodie "failed to write to redirect $redirect";
1464 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001465 }
1466
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001467 while (<CMD>) {
1468 print LOG if ($dolog);
1469 print RD if ($dord);
1470 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001471
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001472 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001473 my $failed = $?;
1474
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001475 close(CMD);
1476 close(LOG) if ($dolog);
1477 close(RD) if ($dord);
1478
Steven Rostedt2545eb62010-11-02 15:01:32 -04001479 if ($failed) {
1480 doprint "FAILED!\n";
1481 } else {
1482 doprint "SUCCESS\n";
1483 }
1484
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001485 return !$failed;
1486}
1487
Steven Rostedte48c5292010-11-02 14:35:37 -04001488sub run_ssh {
1489 my ($cmd) = @_;
1490 my $cp_exec = $ssh_exec;
1491
1492 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1493 return run_command "$cp_exec";
1494}
1495
1496sub run_scp {
Steven Rostedt02ad2612012-03-21 08:21:24 -04001497 my ($src, $dst, $cp_scp) = @_;
Steven Rostedte48c5292010-11-02 14:35:37 -04001498
1499 $cp_scp =~ s/\$SRC_FILE/$src/g;
1500 $cp_scp =~ s/\$DST_FILE/$dst/g;
1501
1502 return run_command "$cp_scp";
1503}
1504
Steven Rostedt02ad2612012-03-21 08:21:24 -04001505sub run_scp_install {
1506 my ($src, $dst) = @_;
1507
1508 my $cp_scp = $scp_to_target_install;
1509
1510 return run_scp($src, $dst, $cp_scp);
1511}
1512
1513sub run_scp_mod {
1514 my ($src, $dst) = @_;
1515
1516 my $cp_scp = $scp_to_target;
1517
1518 return run_scp($src, $dst, $cp_scp);
1519}
1520
Steven Rostedta15ba912012-11-13 14:30:37 -05001521sub get_grub2_index {
1522
1523 return if (defined($grub_number));
1524
1525 doprint "Find grub2 menu ... ";
1526 $grub_number = -1;
1527
1528 my $ssh_grub = $ssh_exec;
1529 $ssh_grub =~ s,\$SSH_COMMAND,cat $grub_file,g;
1530
1531 open(IN, "$ssh_grub |")
1532 or die "unable to get $grub_file";
1533
1534 my $found = 0;
1535
1536 while (<IN>) {
1537 if (/^menuentry.*$grub_menu/) {
1538 $grub_number++;
1539 $found = 1;
1540 last;
1541 } elsif (/^menuentry\s/) {
1542 $grub_number++;
1543 }
1544 }
1545 close(IN);
1546
1547 die "Could not find '$grub_menu' in $grub_file on $machine"
1548 if (!$found);
1549 doprint "$grub_number\n";
1550}
1551
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001552sub get_grub_index {
1553
Steven Rostedta15ba912012-11-13 14:30:37 -05001554 if ($reboot_type eq "grub2") {
1555 get_grub2_index;
1556 return;
1557 }
1558
Steven Rostedta75fece2010-11-02 14:58:27 -04001559 if ($reboot_type ne "grub") {
1560 return;
1561 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001562 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001563
1564 doprint "Find grub menu ... ";
1565 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001566
1567 my $ssh_grub = $ssh_exec;
1568 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1569
1570 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001571 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001572
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001573 my $found = 0;
1574
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001575 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001576 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001577 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001578 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001579 last;
1580 } elsif (/^\s*title\s/) {
1581 $grub_number++;
1582 }
1583 }
1584 close(IN);
1585
Steven Rostedta75fece2010-11-02 14:58:27 -04001586 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001587 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001588 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001589}
1590
Steven Rostedt2545eb62010-11-02 15:01:32 -04001591sub wait_for_input
1592{
1593 my ($fp, $time) = @_;
1594 my $rin;
1595 my $ready;
1596 my $line;
1597 my $ch;
1598
1599 if (!defined($time)) {
1600 $time = $timeout;
1601 }
1602
1603 $rin = '';
1604 vec($rin, fileno($fp), 1) = 1;
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001605 ($ready, $time) = select($rin, undef, undef, $time);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001606
1607 $line = "";
1608
1609 # try to read one char at a time
1610 while (sysread $fp, $ch, 1) {
1611 $line .= $ch;
1612 last if ($ch eq "\n");
1613 }
1614
1615 if (!length($line)) {
1616 return undef;
1617 }
1618
1619 return $line;
1620}
1621
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001622sub reboot_to {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001623 if (defined($switch_to_test)) {
1624 run_command $switch_to_test;
1625 }
1626
Steven Rostedta75fece2010-11-02 14:58:27 -04001627 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001628 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
Steven Rostedta15ba912012-11-13 14:30:37 -05001629 } elsif ($reboot_type eq "grub2") {
1630 run_ssh "$grub_reboot $grub_number";
Steven Rostedt77869542012-12-11 17:37:41 -05001631 } elsif ($reboot_type eq "syslinux") {
1632 run_ssh "$syslinux --once \\\"$syslinux_label\\\" $syslinux_path";
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001633 } elsif (defined $reboot_script) {
1634 run_command "$reboot_script";
Steven Rostedta75fece2010-11-02 14:58:27 -04001635 }
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001636 reboot;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001637}
1638
Steven Rostedta57419b2010-11-02 15:13:54 -04001639sub get_sha1 {
1640 my ($commit) = @_;
1641
1642 doprint "git rev-list --max-count=1 $commit ... ";
1643 my $sha1 = `git rev-list --max-count=1 $commit`;
1644 my $ret = $?;
1645
1646 logit $sha1;
1647
1648 if ($ret) {
1649 doprint "FAILED\n";
1650 dodie "Failed to get git $commit";
1651 }
1652
1653 print "SUCCESS\n";
1654
1655 chomp $sha1;
1656
1657 return $sha1;
1658}
1659
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001660sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001661 my $booted = 0;
1662 my $bug = 0;
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001663 my $bug_ignored = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001664 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001665 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001666
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001667 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001668
1669 my $line;
1670 my $full_line = "";
1671
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001672 open(DMESG, "> $dmesg") or
1673 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001674
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001675 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001676
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001677 my $success_start;
1678 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001679 my $monitor_start = time;
1680 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001681 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001682
Steven Rostedt2d01b262011-03-08 09:47:54 -05001683 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001684
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001685 if ($bug && defined($stop_after_failure) &&
1686 $stop_after_failure >= 0) {
1687 my $time = $stop_after_failure - (time - $failure_start);
1688 $line = wait_for_input($monitor_fp, $time);
1689 if (!defined($line)) {
1690 doprint "bug timed out after $booted_timeout seconds\n";
1691 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1692 last;
1693 }
1694 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001695 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001696 if (!defined($line)) {
1697 my $s = $booted_timeout == 1 ? "" : "s";
1698 doprint "Successful boot found: break after $booted_timeout second$s\n";
1699 last;
1700 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001701 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001702 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001703 if (!defined($line)) {
1704 my $s = $timeout == 1 ? "" : "s";
1705 doprint "Timed out after $timeout second$s\n";
1706 last;
1707 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001708 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001709
Steven Rostedt2545eb62010-11-02 15:01:32 -04001710 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001711 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001712
1713 # we are not guaranteed to get a full line
1714 $full_line .= $line;
1715
Steven Rostedta75fece2010-11-02 14:58:27 -04001716 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001717 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001718 $success_start = time;
1719 }
1720
1721 if ($booted && defined($stop_after_success) &&
1722 $stop_after_success >= 0) {
1723 my $now = time;
1724 if ($now - $success_start >= $stop_after_success) {
1725 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1726 last;
1727 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001728 }
1729
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001730 if ($full_line =~ /\[ backtrace testing \]/) {
1731 $skip_call_trace = 1;
1732 }
1733
Steven Rostedt2545eb62010-11-02 15:01:32 -04001734 if ($full_line =~ /call trace:/i) {
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001735 if (!$bug && !$skip_call_trace) {
1736 if ($ignore_errors) {
1737 $bug_ignored = 1;
1738 } else {
1739 $bug = 1;
1740 $failure_start = time;
1741 }
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001742 }
1743 }
1744
1745 if ($bug && defined($stop_after_failure) &&
1746 $stop_after_failure >= 0) {
1747 my $now = time;
1748 if ($now - $failure_start >= $stop_after_failure) {
1749 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1750 last;
1751 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001752 }
1753
1754 if ($full_line =~ /\[ end of backtrace testing \]/) {
1755 $skip_call_trace = 0;
1756 }
1757
1758 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001759 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001760 $bug = 1;
1761 }
1762
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001763 # Detect triple faults by testing the banner
1764 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1765 if ($1 eq $version) {
1766 $version_found = 1;
1767 } elsif ($version_found && $detect_triplefault) {
1768 # We already booted into the kernel we are testing,
1769 # but now we booted into another kernel?
1770 # Consider this a triple fault.
1771 doprint "Aleady booted in Linux kernel $version, but now\n";
1772 doprint "we booted into Linux kernel $1.\n";
1773 doprint "Assuming that this is a triple fault.\n";
1774 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1775 last;
1776 }
1777 }
1778
Steven Rostedt2545eb62010-11-02 15:01:32 -04001779 if ($line =~ /\n/) {
1780 $full_line = "";
1781 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001782
1783 if ($stop_test_after > 0 && !$booted && !$bug) {
1784 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001785 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001786 $done = 1;
1787 }
1788 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001789 }
1790
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001791 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001792
Steven Rostedt2545eb62010-11-02 15:01:32 -04001793 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001794 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001795 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001796 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001797
Steven Rostedta75fece2010-11-02 14:58:27 -04001798 if (!$booted) {
1799 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001800 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001801 }
1802
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001803 if ($bug_ignored) {
1804 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
1805 }
1806
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001807 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001808}
1809
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001810sub eval_kernel_version {
1811 my ($option) = @_;
1812
1813 $option =~ s/\$KERNEL_VERSION/$version/g;
1814
1815 return $option;
1816}
1817
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001818sub do_post_install {
1819
1820 return if (!defined($post_install));
1821
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001822 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001823 run_command "$cp_post_install" or
1824 dodie "Failed to run post install";
1825}
1826
Steven Rostedte1a6c3d2012-12-11 21:19:41 -05001827# Sometimes the reboot fails, and will hang. We try to ssh to the box
1828# and if we fail, we force another reboot, that should powercycle it.
1829sub test_booted {
1830 if (!run_ssh "echo testing connection") {
1831 reboot $sleep_time;
1832 }
1833}
1834
Steven Rostedt2545eb62010-11-02 15:01:32 -04001835sub install {
1836
Steven Rostedte0a87422011-09-30 17:50:48 -04001837 return if ($no_install);
1838
Steven Rostedte5c2ec12012-07-19 15:22:05 -04001839 if (defined($pre_install)) {
1840 my $cp_pre_install = eval_kernel_version $pre_install;
1841 run_command "$cp_pre_install" or
1842 dodie "Failed to run pre install";
1843 }
1844
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001845 my $cp_target = eval_kernel_version $target_image;
1846
Steven Rostedte1a6c3d2012-12-11 21:19:41 -05001847 test_booted;
1848
Steven Rostedt02ad2612012-03-21 08:21:24 -04001849 run_scp_install "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001850 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001851
1852 my $install_mods = 0;
1853
1854 # should we process modules?
1855 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001856 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001857 while (<IN>) {
1858 if (/CONFIG_MODULES(=y)?/) {
Steven Rostedt8bc5e4e2012-10-26 00:10:32 -04001859 if (defined($1)) {
1860 $install_mods = 1;
1861 last;
1862 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001863 }
1864 }
1865 close(IN);
1866
1867 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001868 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001869 doprint "No modules needed\n";
1870 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001871 }
1872
Steven Rostedt627977d2012-03-21 08:16:15 -04001873 run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001874 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001875
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001876 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001877 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001878
Steven Rostedte48c5292010-11-02 14:35:37 -04001879 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001880 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001881
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001882 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001883 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001884 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001885
Steven Rostedt02ad2612012-03-21 08:21:24 -04001886 run_scp_mod "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001887 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001888
Steven Rostedta75fece2010-11-02 14:58:27 -04001889 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001890
Steven Rostedte7b13442011-06-14 20:44:36 -04001891 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001892 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001893
Steven Rostedte48c5292010-11-02 14:35:37 -04001894 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001895
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001896 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001897}
1898
Steven Rostedtddf607e2011-06-14 20:49:13 -04001899sub get_version {
1900 # get the release name
Steven Rostedt683a3e62012-05-18 13:34:35 -04001901 return if ($have_version);
Steven Rostedtddf607e2011-06-14 20:49:13 -04001902 doprint "$make kernelrelease ... ";
1903 $version = `$make kernelrelease | tail -1`;
1904 chomp($version);
1905 doprint "$version\n";
Steven Rostedt683a3e62012-05-18 13:34:35 -04001906 $have_version = 1;
Steven Rostedtddf607e2011-06-14 20:49:13 -04001907}
1908
1909sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001910 # Make sure the stable kernel has finished booting
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001911
1912 # Install bisects, don't need console
1913 if (defined $console) {
1914 start_monitor;
1915 wait_for_monitor 5;
1916 end_monitor;
1917 }
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001918
Steven Rostedtddf607e2011-06-14 20:49:13 -04001919 get_grub_index;
1920 get_version;
1921 install;
1922
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001923 start_monitor if (defined $console);
Steven Rostedtddf607e2011-06-14 20:49:13 -04001924 return monitor;
1925}
1926
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001927sub check_buildlog {
1928 my ($patch) = @_;
1929
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001930 my @files = `git show $patch | diffstat -l`;
1931
Steven Rostedt (Red Hat)35275682013-01-30 12:28:15 -05001932 foreach my $file (@files) {
1933 chomp $file;
1934 }
1935
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001936 open(IN, "git show $patch |") or
1937 dodie "failed to show $patch";
1938 while (<IN>) {
1939 if (m,^--- a/(.*),) {
1940 chomp $1;
1941 $files[$#files] = $1;
1942 }
1943 }
1944 close(IN);
1945
1946 open(IN, $buildlog) or dodie "Can't open $buildlog";
1947 while (<IN>) {
1948 if (/^\s*(.*?):.*(warning|error)/) {
1949 my $err = $1;
1950 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001951 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001952 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001953 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001954 }
1955 }
1956 }
1957 }
1958 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001959
1960 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001961}
1962
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001963sub apply_min_config {
1964 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05001965
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001966 # Read the config file and remove anything that
1967 # is in the force_config hash (from minconfig and others)
1968 # then add the force config back.
1969
1970 doprint "Applying minimum configurations into $output_config.new\n";
1971
1972 open (OUT, ">$outconfig") or
1973 dodie "Can't create $outconfig";
1974
1975 if (-f $output_config) {
1976 open (IN, $output_config) or
1977 dodie "Failed to open $output_config";
1978 while (<IN>) {
1979 if (/^(# )?(CONFIG_[^\s=]*)/) {
1980 next if (defined($force_config{$2}));
1981 }
1982 print OUT;
1983 }
1984 close IN;
1985 }
1986 foreach my $config (keys %force_config) {
1987 print OUT "$force_config{$config}\n";
1988 }
1989 close OUT;
1990
1991 run_command "mv $outconfig $output_config";
1992}
1993
1994sub make_oldconfig {
1995
Steven Rostedt4c4ab122011-07-15 21:16:17 -04001996 my @force_list = keys %force_config;
1997
1998 if ($#force_list >= 0) {
1999 apply_min_config;
2000 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002001
Adam Leefb16d892012-09-01 01:05:17 +08002002 if (!run_command "$make olddefconfig") {
2003 # Perhaps olddefconfig doesn't exist in this version of the kernel
Steven Rostedt18925172012-12-11 20:16:03 -05002004 # try oldnoconfig
2005 doprint "olddefconfig failed, trying make oldnoconfig\n";
2006 if (!run_command "$make oldnoconfig") {
2007 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
2008 # try a yes '' | oldconfig
2009 run_command "yes '' | $make oldconfig" or
2010 dodie "failed make config oldconfig";
2011 }
Steven Rostedt612b9e92011-03-07 13:27:43 -05002012 }
2013}
2014
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002015# read a config file and use this to force new configs.
2016sub load_force_config {
2017 my ($config) = @_;
2018
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002019 doprint "Loading force configs from $config\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002020 open(IN, $config) or
2021 dodie "failed to read $config";
2022 while (<IN>) {
2023 chomp;
2024 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
2025 $force_config{$1} = $_;
2026 } elsif (/^# (CONFIG_\S*) is not set/) {
2027 $force_config{$1} = $_;
2028 }
2029 }
2030 close IN;
2031}
2032
Steven Rostedt2545eb62010-11-02 15:01:32 -04002033sub build {
2034 my ($type) = @_;
2035
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002036 unlink $buildlog;
2037
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04002038 # Failed builds should not reboot the target
2039 my $save_no_reboot = $no_reboot;
2040 $no_reboot = 1;
2041
Steven Rostedt683a3e62012-05-18 13:34:35 -04002042 # Calculate a new version from here.
2043 $have_version = 0;
2044
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04002045 if (defined($pre_build)) {
2046 my $ret = run_command $pre_build;
2047 if (!$ret && defined($pre_build_die) &&
2048 $pre_build_die) {
2049 dodie "failed to pre_build\n";
2050 }
2051 }
2052
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002053 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05002054 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002055 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002056
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002057 $type = "oldconfig";
2058 }
2059
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002060 # old config can ask questions
2061 if ($type eq "oldconfig") {
Adam Leefb16d892012-09-01 01:05:17 +08002062 $type = "olddefconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002063
2064 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05002065 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002066
Andrew Jones13488232011-08-12 15:32:04 +02002067 if (!$noclean) {
2068 run_command "mv $output_config $outputdir/config_temp" or
2069 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04002070
Andrew Jones13488232011-08-12 15:32:04 +02002071 run_command "$make mrproper" or dodie "make mrproper";
2072
2073 run_command "mv $outputdir/config_temp $output_config" or
2074 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002075 }
2076
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002077 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05002078 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002079 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002080 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002081 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04002082
2083 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04002084 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
2085 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04002086 close(OUT);
2087
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002088 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002089 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04002090 }
2091
Adam Leefb16d892012-09-01 01:05:17 +08002092 if ($type ne "olddefconfig") {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002093 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05002094 dodie "failed make config";
2095 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002096 # Run old config regardless, to enforce min configurations
2097 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002098
Steven Rostedta75fece2010-11-02 14:58:27 -04002099 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04002100 my $build_ret = run_command "$make $build_options";
2101 undef $redirect;
2102
2103 if (defined($post_build)) {
Steven Rostedt683a3e62012-05-18 13:34:35 -04002104 # Because a post build may change the kernel version
2105 # do it now.
2106 get_version;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04002107 my $ret = run_command $post_build;
2108 if (!$ret && defined($post_build_die) &&
2109 $post_build_die) {
2110 dodie "failed to post_build\n";
2111 }
2112 }
2113
2114 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002115 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04002116 if ($in_bisect) {
2117 $no_reboot = $save_no_reboot;
2118 return 0;
2119 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002120 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002121 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002122
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04002123 $no_reboot = $save_no_reboot;
2124
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002125 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002126}
2127
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002128sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04002129 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04002130 if (defined($poweroff_after_halt)) {
2131 sleep $poweroff_after_halt;
2132 run_command "$power_off";
2133 }
2134 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002135 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04002136 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002137 }
2138}
2139
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002140sub success {
2141 my ($i) = @_;
2142
Steven Rostedt921ed4c2012-07-19 15:18:27 -04002143 if (defined($post_test)) {
2144 run_command $post_test;
2145 }
2146
Steven Rostedte48c5292010-11-02 14:35:37 -04002147 $successes++;
2148
Steven Rostedt9064af52011-06-13 10:38:48 -04002149 my $name = "";
2150
2151 if (defined($test_name)) {
2152 $name = " ($test_name)";
2153 }
2154
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002155 doprint "\n\n*******************************************\n";
2156 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04002157 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002158 doprint "*******************************************\n";
2159 doprint "*******************************************\n";
2160
Rabin Vincentde5b6e32011-11-18 17:05:31 +05302161 if (defined($store_successes)) {
2162 save_logs "success", $store_successes;
2163 }
2164
Steven Rostedt576f6272010-11-02 14:58:38 -04002165 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002166 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002167 reboot_to_good $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002168 }
2169}
2170
Steven Rostedtc960bb92011-03-08 09:22:39 -05002171sub answer_bisect {
2172 for (;;) {
2173 doprint "Pass or fail? [p/f]";
2174 my $ans = <STDIN>;
2175 chomp $ans;
2176 if ($ans eq "p" || $ans eq "P") {
2177 return 1;
2178 } elsif ($ans eq "f" || $ans eq "F") {
2179 return 0;
2180 } else {
2181 print "Please answer 'P' or 'F'\n";
2182 }
2183 }
2184}
2185
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002186sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002187 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002188
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002189 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04002190 $reboot_on_error = 0;
2191 $poweroff_on_error = 0;
2192 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002193
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05302194 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002195 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05302196 undef $redirect;
2197
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002198 exit $failed;
2199}
2200
2201my $child_done;
2202
2203sub child_finished {
2204 $child_done = 1;
2205}
2206
2207sub do_run_test {
2208 my $child_pid;
2209 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002210 my $line;
2211 my $full_line;
2212 my $bug = 0;
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002213 my $bug_ignored = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002214
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002215 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002216
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002217 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002218
2219 $child_done = 0;
2220
2221 $SIG{CHLD} = qw(child_finished);
2222
2223 $child_pid = fork;
2224
2225 child_run_test if (!$child_pid);
2226
2227 $full_line = "";
2228
2229 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002230 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002231 if (defined($line)) {
2232
2233 # we are not guaranteed to get a full line
2234 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002235 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002236
2237 if ($full_line =~ /call trace:/i) {
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002238 if ($ignore_errors) {
2239 $bug_ignored = 1;
2240 } else {
2241 $bug = 1;
2242 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002243 }
2244
2245 if ($full_line =~ /Kernel panic -/) {
2246 $bug = 1;
2247 }
2248
2249 if ($line =~ /\n/) {
2250 $full_line = "";
2251 }
2252 }
2253 } while (!$child_done && !$bug);
2254
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002255 if (!$bug && $bug_ignored) {
2256 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
2257 }
2258
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002259 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002260 my $failure_start = time;
2261 my $now;
2262 do {
2263 $line = wait_for_input($monitor_fp, 1);
2264 if (defined($line)) {
2265 doprint $line;
2266 }
2267 $now = time;
2268 if ($now - $failure_start >= $stop_after_failure) {
2269 last;
2270 }
2271 } while (defined($line));
2272
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002273 doprint "Detected kernel crash!\n";
2274 # kill the child with extreme prejudice
2275 kill 9, $child_pid;
2276 }
2277
2278 waitpid $child_pid, 0;
2279 $child_exit = $?;
2280
Steven Rostedtc5dacb82011-12-22 12:43:57 -05002281 if (!$bug && $in_bisect) {
2282 if (defined($bisect_ret_good)) {
2283 if ($child_exit == $bisect_ret_good) {
2284 return 1;
2285 }
2286 }
2287 if (defined($bisect_ret_skip)) {
2288 if ($child_exit == $bisect_ret_skip) {
2289 return -1;
2290 }
2291 }
2292 if (defined($bisect_ret_abort)) {
2293 if ($child_exit == $bisect_ret_abort) {
2294 fail "test abort" and return -2;
2295 }
2296 }
2297 if (defined($bisect_ret_bad)) {
2298 if ($child_exit == $bisect_ret_skip) {
2299 return 0;
2300 }
2301 }
2302 if (defined($bisect_ret_default)) {
2303 if ($bisect_ret_default eq "good") {
2304 return 1;
2305 } elsif ($bisect_ret_default eq "bad") {
2306 return 0;
2307 } elsif ($bisect_ret_default eq "skip") {
2308 return -1;
2309 } elsif ($bisect_ret_default eq "abort") {
2310 return -2;
2311 } else {
2312 fail "unknown default action: $bisect_ret_default"
2313 and return -2;
2314 }
2315 }
2316 }
2317
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002318 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002319 return 0 if $in_bisect;
2320 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002321 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002322 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002323}
2324
Steven Rostedta75fece2010-11-02 14:58:27 -04002325sub run_git_bisect {
2326 my ($command) = @_;
2327
2328 doprint "$command ... ";
2329
2330 my $output = `$command 2>&1`;
2331 my $ret = $?;
2332
2333 logit $output;
2334
2335 if ($ret) {
2336 doprint "FAILED\n";
2337 dodie "Failed to git bisect";
2338 }
2339
2340 doprint "SUCCESS\n";
2341 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2342 doprint "$1 [$2]\n";
2343 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002344 $bisect_bad_commit = $1;
Steven Rostedta75fece2010-11-02 14:58:27 -04002345 doprint "Found bad commit... $1\n";
2346 return 0;
2347 } else {
2348 # we already logged it, just print it now.
2349 print $output;
2350 }
2351
2352 return 1;
2353}
2354
Steven Rostedtc23dca72011-03-08 09:26:31 -05002355sub bisect_reboot {
2356 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002357 reboot_to_good $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05002358}
2359
2360# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05002361sub run_bisect_test {
2362 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002363
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002364 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002365 my $result;
2366 my $output;
2367 my $ret;
2368
Steven Rostedt0a05c762010-11-08 11:14:10 -05002369 $in_bisect = 1;
2370
2371 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002372
2373 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002374 if ($failed && $bisect_skip) {
2375 $in_bisect = 0;
2376 return -1;
2377 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002378 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002379
2380 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04002381 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002382
2383 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002384 if ($failed && $bisect_skip) {
2385 end_monitor;
2386 bisect_reboot;
2387 $in_bisect = 0;
2388 return -1;
2389 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002390 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002391
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002392 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002393 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002394 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002395 }
2396
2397 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002398 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002399 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002400 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002401 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04002402
2403 # reboot the box to a kernel we can ssh to
2404 if ($type ne "build") {
2405 bisect_reboot;
2406 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002407 $in_bisect = 0;
2408
2409 return $result;
2410}
2411
2412sub run_bisect {
2413 my ($type) = @_;
2414 my $buildtype = "oldconfig";
2415
2416 # We should have a minconfig to use?
2417 if (defined($minconfig)) {
2418 $buildtype = "useconfig:$minconfig";
2419 }
2420
2421 my $ret = run_bisect_test $type, $buildtype;
2422
Steven Rostedtc960bb92011-03-08 09:22:39 -05002423 if ($bisect_manual) {
2424 $ret = answer_bisect;
2425 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002426
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002427 # Are we looking for where it worked, not failed?
Russ Dill5158ba3e2012-04-23 19:43:00 -07002428 if ($reverse_bisect && $ret >= 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002429 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002430 }
2431
Steven Rostedtc23dca72011-03-08 09:26:31 -05002432 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002433 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002434 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002435 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002436 } elsif ($bisect_skip) {
2437 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2438 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002439 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002440}
2441
Steven Rostedtdad98752011-11-22 20:48:57 -05002442sub update_bisect_replay {
2443 my $tmp_log = "$tmpdir/ktest_bisect_log";
2444 run_command "git bisect log > $tmp_log" or
2445 die "can't create bisect log";
2446 return $tmp_log;
2447}
2448
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002449sub bisect {
2450 my ($i) = @_;
2451
2452 my $result;
2453
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002454 die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2455 die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
2456 die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002457
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002458 my $good = $bisect_good;
2459 my $bad = $bisect_bad;
2460 my $type = $bisect_type;
2461 my $start = $bisect_start;
2462 my $replay = $bisect_replay;
2463 my $start_files = $bisect_files;
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002464
2465 if (defined($start_files)) {
2466 $start_files = " -- " . $start_files;
2467 } else {
2468 $start_files = "";
2469 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002470
Steven Rostedta57419b2010-11-02 15:13:54 -04002471 # convert to true sha1's
2472 $good = get_sha1($good);
2473 $bad = get_sha1($bad);
2474
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002475 if (defined($bisect_reverse) && $bisect_reverse == 1) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002476 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2477 $reverse_bisect = 1;
2478 } else {
2479 $reverse_bisect = 0;
2480 }
2481
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002482 # Can't have a test without having a test to run
2483 if ($type eq "test" && !defined($run_test)) {
2484 $type = "boot";
2485 }
2486
Steven Rostedtdad98752011-11-22 20:48:57 -05002487 # Check if a bisect was running
2488 my $bisect_start_file = "$builddir/.git/BISECT_START";
2489
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002490 my $check = $bisect_check;
Steven Rostedtdad98752011-11-22 20:48:57 -05002491 my $do_check = defined($check) && $check ne "0";
2492
2493 if ( -f $bisect_start_file ) {
2494 print "Bisect in progress found\n";
2495 if ($do_check) {
2496 print " If you say yes, then no checks of good or bad will be done\n";
2497 }
2498 if (defined($replay)) {
2499 print "** BISECT_REPLAY is defined in config file **";
2500 print " Ignore config option and perform new git bisect log?\n";
2501 if (read_ync " (yes, no, or cancel) ") {
2502 $replay = update_bisect_replay;
2503 $do_check = 0;
2504 }
2505 } elsif (read_yn "read git log and continue?") {
2506 $replay = update_bisect_replay;
2507 $do_check = 0;
2508 }
2509 }
2510
2511 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002512
2513 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002514 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002515
2516 if ($check ne "good") {
2517 doprint "TESTING BISECT BAD [$bad]\n";
2518 run_command "git checkout $bad" or
2519 die "Failed to checkout $bad";
2520
2521 $result = run_bisect $type;
2522
2523 if ($result ne "bad") {
2524 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2525 }
2526 }
2527
2528 if ($check ne "bad") {
2529 doprint "TESTING BISECT GOOD [$good]\n";
2530 run_command "git checkout $good" or
2531 die "Failed to checkout $good";
2532
2533 $result = run_bisect $type;
2534
2535 if ($result ne "good") {
2536 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2537 }
2538 }
2539
2540 # checkout where we started
2541 run_command "git checkout $head" or
2542 die "Failed to checkout $head";
2543 }
2544
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002545 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002546 dodie "could not start bisect";
2547
2548 run_command "git bisect good $good" or
2549 dodie "could not set bisect good to $good";
2550
2551 run_git_bisect "git bisect bad $bad" or
2552 dodie "could not set bisect bad to $bad";
2553
2554 if (defined($replay)) {
2555 run_command "git bisect replay $replay" or
2556 dodie "failed to run replay";
2557 }
2558
2559 if (defined($start)) {
2560 run_command "git checkout $start" or
2561 dodie "failed to checkout $start";
2562 }
2563
2564 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002565 do {
2566 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002567 $test = run_git_bisect "git bisect $result";
2568 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002569
2570 run_command "git bisect log" or
2571 dodie "could not capture git bisect log";
2572
2573 run_command "git bisect reset" or
2574 dodie "could not reset git bisect";
2575
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002576 doprint "Bad commit was [$bisect_bad_commit]\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002577
Steven Rostedt0a05c762010-11-08 11:14:10 -05002578 success $i;
2579}
2580
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002581# config_ignore holds the configs that were set (or unset) for
2582# a good config and we will ignore these configs for the rest
2583# of a config bisect. These configs stay as they were.
Steven Rostedt0a05c762010-11-08 11:14:10 -05002584my %config_ignore;
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002585
2586# config_set holds what all configs were set as.
Steven Rostedt0a05c762010-11-08 11:14:10 -05002587my %config_set;
2588
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002589# config_off holds the set of configs that the bad config had disabled.
2590# We need to record them and set them in the .config when running
Adam Leefb16d892012-09-01 01:05:17 +08002591# olddefconfig, because olddefconfig keeps the defaults.
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002592my %config_off;
2593
2594# config_off_tmp holds a set of configs to turn off for now
2595my @config_off_tmp;
2596
2597# config_list is the set of configs that are being tested
Steven Rostedt0a05c762010-11-08 11:14:10 -05002598my %config_list;
2599my %null_config;
2600
2601my %dependency;
2602
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002603sub assign_configs {
2604 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002605
2606 open (IN, $config)
2607 or dodie "Failed to read $config";
2608
2609 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002610 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002611 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002612 }
2613 }
2614
2615 close(IN);
2616}
2617
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002618sub process_config_ignore {
2619 my ($config) = @_;
2620
2621 assign_configs \%config_ignore, $config;
2622}
2623
Steven Rostedt0a05c762010-11-08 11:14:10 -05002624sub read_current_config {
2625 my ($config_ref) = @_;
2626
2627 %{$config_ref} = ();
2628 undef %{$config_ref};
2629
2630 my @key = keys %{$config_ref};
2631 if ($#key >= 0) {
2632 print "did not delete!\n";
2633 exit;
2634 }
2635 open (IN, "$output_config");
2636
2637 while (<IN>) {
2638 if (/^(CONFIG\S+)=(.*)/) {
2639 ${$config_ref}{$1} = $2;
2640 }
2641 }
2642 close(IN);
2643}
2644
2645sub get_dependencies {
2646 my ($config) = @_;
2647
2648 my $arr = $dependency{$config};
2649 if (!defined($arr)) {
2650 return ();
2651 }
2652
2653 my @deps = @{$arr};
2654
2655 foreach my $dep (@{$arr}) {
2656 print "ADD DEP $dep\n";
2657 @deps = (@deps, get_dependencies $dep);
2658 }
2659
2660 return @deps;
2661}
2662
2663sub create_config {
2664 my @configs = @_;
2665
2666 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2667
2668 foreach my $config (@configs) {
2669 print OUT "$config_set{$config}\n";
2670 my @deps = get_dependencies $config;
2671 foreach my $dep (@deps) {
2672 print OUT "$config_set{$dep}\n";
2673 }
2674 }
2675
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002676 # turn off configs to keep off
2677 foreach my $config (keys %config_off) {
2678 print OUT "# $config is not set\n";
2679 }
2680
2681 # turn off configs that should be off for now
2682 foreach my $config (@config_off_tmp) {
2683 print OUT "# $config is not set\n";
2684 }
2685
Steven Rostedt0a05c762010-11-08 11:14:10 -05002686 foreach my $config (keys %config_ignore) {
2687 print OUT "$config_ignore{$config}\n";
2688 }
2689 close(OUT);
2690
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002691 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002692}
2693
2694sub compare_configs {
2695 my (%a, %b) = @_;
2696
2697 foreach my $item (keys %a) {
2698 if (!defined($b{$item})) {
2699 print "diff $item\n";
2700 return 1;
2701 }
2702 delete $b{$item};
2703 }
2704
2705 my @keys = keys %b;
2706 if ($#keys) {
2707 print "diff2 $keys[0]\n";
2708 }
2709 return -1 if ($#keys >= 0);
2710
2711 return 0;
2712}
2713
2714sub run_config_bisect_test {
2715 my ($type) = @_;
2716
2717 return run_bisect_test $type, "oldconfig";
2718}
2719
2720sub process_passed {
2721 my (%configs) = @_;
2722
2723 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2724 # Passed! All these configs are part of a good compile.
2725 # Add them to the min options.
2726 foreach my $config (keys %configs) {
2727 if (defined($config_list{$config})) {
2728 doprint " removing $config\n";
2729 $config_ignore{$config} = $config_list{$config};
2730 delete $config_list{$config};
2731 }
2732 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002733 doprint "config copied to $outputdir/config_good\n";
2734 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002735}
2736
2737sub process_failed {
2738 my ($config) = @_;
2739
2740 doprint "\n\n***************************************\n";
2741 doprint "Found bad config: $config\n";
2742 doprint "***************************************\n\n";
2743}
2744
2745sub run_config_bisect {
2746
2747 my @start_list = keys %config_list;
2748
2749 if ($#start_list < 0) {
2750 doprint "No more configs to test!!!\n";
2751 return -1;
2752 }
2753
2754 doprint "***** RUN TEST ***\n";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002755 my $type = $config_bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002756 my $ret;
2757 my %current_config;
2758
2759 my $count = $#start_list + 1;
2760 doprint " $count configs to test\n";
2761
2762 my $half = int($#start_list / 2);
2763
2764 do {
2765 my @tophalf = @start_list[0 .. $half];
2766
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002767 # keep the bottom half off
2768 if ($half < $#start_list) {
2769 @config_off_tmp = @start_list[$half + 1 .. $#start_list];
2770 } else {
2771 @config_off_tmp = ();
2772 }
2773
Steven Rostedt0a05c762010-11-08 11:14:10 -05002774 create_config @tophalf;
2775 read_current_config \%current_config;
2776
2777 $count = $#tophalf + 1;
2778 doprint "Testing $count configs\n";
2779 my $found = 0;
2780 # make sure we test something
2781 foreach my $config (@tophalf) {
2782 if (defined($current_config{$config})) {
2783 logit " $config\n";
2784 $found = 1;
2785 }
2786 }
2787 if (!$found) {
2788 # try the other half
2789 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002790
2791 # keep the top half off
2792 @config_off_tmp = @tophalf;
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002793 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002794
Steven Rostedt0a05c762010-11-08 11:14:10 -05002795 create_config @tophalf;
2796 read_current_config \%current_config;
2797 foreach my $config (@tophalf) {
2798 if (defined($current_config{$config})) {
2799 logit " $config\n";
2800 $found = 1;
2801 }
2802 }
2803 if (!$found) {
2804 doprint "Failed: Can't make new config with current configs\n";
2805 foreach my $config (@start_list) {
2806 doprint " CONFIG: $config\n";
2807 }
2808 return -1;
2809 }
2810 $count = $#tophalf + 1;
2811 doprint "Testing $count configs\n";
2812 }
2813
2814 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002815 if ($bisect_manual) {
2816 $ret = answer_bisect;
2817 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002818 if ($ret) {
2819 process_passed %current_config;
2820 return 0;
2821 }
2822
2823 doprint "This config had a failure.\n";
2824 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002825 doprint "config copied to $outputdir/config_bad\n";
2826 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002827
2828 # A config exists in this group that was bad.
2829 foreach my $config (keys %config_list) {
2830 if (!defined($current_config{$config})) {
2831 doprint " removing $config\n";
2832 delete $config_list{$config};
2833 }
2834 }
2835
2836 @start_list = @tophalf;
2837
2838 if ($#start_list == 0) {
2839 process_failed $start_list[0];
2840 return 1;
2841 }
2842
2843 # remove half the configs we are looking at and see if
2844 # they are good.
2845 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002846 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002847
Steven Rostedtc960bb92011-03-08 09:22:39 -05002848 # we found a single config, try it again unless we are running manually
2849
2850 if ($bisect_manual) {
2851 process_failed $start_list[0];
2852 return 1;
2853 }
2854
Steven Rostedt0a05c762010-11-08 11:14:10 -05002855 my @tophalf = @start_list[0 .. 0];
2856
2857 $ret = run_config_bisect_test $type;
2858 if ($ret) {
2859 process_passed %current_config;
2860 return 0;
2861 }
2862
2863 process_failed $start_list[0];
2864 return 1;
2865}
2866
2867sub config_bisect {
2868 my ($i) = @_;
2869
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002870 my $start_config = $config_bisect;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002871
2872 my $tmpconfig = "$tmpdir/use_config";
2873
Steven Rostedt30f75da2011-06-13 10:35:35 -04002874 if (defined($config_bisect_good)) {
2875 process_config_ignore $config_bisect_good;
2876 }
2877
Steven Rostedt0a05c762010-11-08 11:14:10 -05002878 # Make the file with the bad config and the min config
2879 if (defined($minconfig)) {
2880 # read the min config for things to ignore
2881 run_command "cp $minconfig $tmpconfig" or
2882 dodie "failed to copy $minconfig to $tmpconfig";
2883 } else {
2884 unlink $tmpconfig;
2885 }
2886
Steven Rostedt0a05c762010-11-08 11:14:10 -05002887 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002888 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002889 process_config_ignore $tmpconfig;
2890 }
2891
2892 # now process the start config
2893 run_command "cp $start_config $output_config" or
2894 dodie "failed to copy $start_config to $output_config";
2895
2896 # read directly what we want to check
2897 my %config_check;
2898 open (IN, $output_config)
Masanari Iidaf9dee312012-02-11 21:46:56 +09002899 or dodie "failed to open $output_config";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002900
2901 while (<IN>) {
2902 if (/^((CONFIG\S*)=.*)/) {
2903 $config_check{$2} = $1;
2904 }
2905 }
2906 close(IN);
2907
Steven Rostedt250bae82011-07-15 22:05:59 -04002908 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002909 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002910
2911 # check to see what we lost (or gained)
2912 open (IN, $output_config)
2913 or dodie "Failed to read $start_config";
2914
2915 my %removed_configs;
2916 my %added_configs;
2917
2918 while (<IN>) {
2919 if (/^((CONFIG\S*)=.*)/) {
2920 # save off all options
2921 $config_set{$2} = $1;
2922 if (defined($config_check{$2})) {
2923 if (defined($config_ignore{$2})) {
2924 $removed_configs{$2} = $1;
2925 } else {
2926 $config_list{$2} = $1;
2927 }
2928 } elsif (!defined($config_ignore{$2})) {
2929 $added_configs{$2} = $1;
2930 $config_list{$2} = $1;
2931 }
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002932 } elsif (/^# ((CONFIG\S*).*)/) {
2933 # Keep these configs disabled
2934 $config_set{$2} = $1;
2935 $config_off{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002936 }
2937 }
2938 close(IN);
2939
2940 my @confs = keys %removed_configs;
2941 if ($#confs >= 0) {
2942 doprint "Configs overridden by default configs and removed from check:\n";
2943 foreach my $config (@confs) {
2944 doprint " $config\n";
2945 }
2946 }
2947 @confs = keys %added_configs;
2948 if ($#confs >= 0) {
2949 doprint "Configs appearing in make oldconfig and added:\n";
2950 foreach my $config (@confs) {
2951 doprint " $config\n";
2952 }
2953 }
2954
2955 my %config_test;
2956 my $once = 0;
2957
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002958 @config_off_tmp = ();
2959
Steven Rostedt0a05c762010-11-08 11:14:10 -05002960 # Sometimes kconfig does weird things. We must make sure
2961 # that the config we autocreate has everything we need
2962 # to test, otherwise we may miss testing configs, or
2963 # may not be able to create a new config.
2964 # Here we create a config with everything set.
2965 create_config (keys %config_list);
2966 read_current_config \%config_test;
2967 foreach my $config (keys %config_list) {
2968 if (!defined($config_test{$config})) {
2969 if (!$once) {
2970 $once = 1;
2971 doprint "Configs not produced by kconfig (will not be checked):\n";
2972 }
2973 doprint " $config\n";
2974 delete $config_list{$config};
2975 }
2976 }
2977 my $ret;
Steven Rostedtb0918612012-07-19 15:26:00 -04002978
2979 if (defined($config_bisect_check) && $config_bisect_check) {
2980 doprint " Checking to make sure bad config with min config fails\n";
2981 create_config keys %config_list;
2982 $ret = run_config_bisect_test $config_bisect_type;
2983 if ($ret) {
2984 doprint " FAILED! Bad config with min config boots fine\n";
2985 return -1;
2986 }
2987 doprint " Bad config with min config fails as expected\n";
2988 }
2989
Steven Rostedt0a05c762010-11-08 11:14:10 -05002990 do {
2991 $ret = run_config_bisect;
2992 } while (!$ret);
2993
2994 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002995
2996 success $i;
2997}
2998
Steven Rostedt27d934b2011-05-20 09:18:18 -04002999sub patchcheck_reboot {
3000 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003001 reboot_to_good $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04003002}
3003
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003004sub patchcheck {
3005 my ($i) = @_;
3006
3007 die "PATCHCHECK_START[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003008 if (!defined($patchcheck_start));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003009 die "PATCHCHECK_TYPE[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003010 if (!defined($patchcheck_type));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003011
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003012 my $start = $patchcheck_start;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003013
3014 my $end = "HEAD";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003015 if (defined($patchcheck_end)) {
3016 $end = $patchcheck_end;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003017 }
3018
Steven Rostedta57419b2010-11-02 15:13:54 -04003019 # Get the true sha1's since we can use things like HEAD~3
3020 $start = get_sha1($start);
3021 $end = get_sha1($end);
3022
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003023 my $type = $patchcheck_type;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003024
3025 # Can't have a test without having a test to run
3026 if ($type eq "test" && !defined($run_test)) {
3027 $type = "boot";
3028 }
3029
3030 open (IN, "git log --pretty=oneline $end|") or
3031 dodie "could not get git list";
3032
3033 my @list;
3034
3035 while (<IN>) {
3036 chomp;
3037 $list[$#list+1] = $_;
3038 last if (/^$start/);
3039 }
3040 close(IN);
3041
3042 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003043 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003044 }
3045
3046 # go backwards in the list
3047 @list = reverse @list;
3048
3049 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04003050 my %ignored_warnings;
3051
3052 if (defined($ignore_warnings)) {
3053 foreach my $sha1 (split /\s+/, $ignore_warnings) {
3054 $ignored_warnings{$sha1} = 1;
3055 }
3056 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003057
3058 $in_patchcheck = 1;
3059 foreach my $item (@list) {
3060 my $sha1 = $item;
3061 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
3062
3063 doprint "\nProcessing commit $item\n\n";
3064
3065 run_command "git checkout $sha1" or
3066 die "Failed to checkout $sha1";
3067
3068 # only clean on the first and last patch
3069 if ($item eq $list[0] ||
3070 $item eq $list[$#list]) {
3071 $noclean = $save_clean;
3072 } else {
3073 $noclean = 1;
3074 }
3075
3076 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003077 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003078 } else {
3079 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003080 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003081 }
3082
Steven Rostedt19902072011-06-14 20:46:25 -04003083
3084 if (!defined($ignored_warnings{$sha1})) {
3085 check_buildlog $sha1 or return 0;
3086 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003087
3088 next if ($type eq "build");
3089
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003090 my $failed = 0;
3091
Steven Rostedtddf607e2011-06-14 20:49:13 -04003092 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003093
3094 if (!$failed && $type ne "boot"){
3095 do_run_test or $failed = 1;
3096 }
3097 end_monitor;
3098 return 0 if ($failed);
3099
Steven Rostedt27d934b2011-05-20 09:18:18 -04003100 patchcheck_reboot;
3101
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003102 }
3103 $in_patchcheck = 0;
3104 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003105
3106 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003107}
3108
Steven Rostedtb9066f62011-07-15 21:25:24 -04003109my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003110my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003111my $iflevel = 0;
3112my @ifdeps;
3113
3114# prevent recursion
3115my %read_kconfigs;
3116
Steven Rostedtac6974c2011-10-04 09:40:17 -04003117sub add_dep {
3118 # $config depends on $dep
3119 my ($config, $dep) = @_;
3120
3121 if (defined($depends{$config})) {
3122 $depends{$config} .= " " . $dep;
3123 } else {
3124 $depends{$config} = $dep;
3125 }
3126
3127 # record the number of configs depending on $dep
3128 if (defined $depcount{$dep}) {
3129 $depcount{$dep}++;
3130 } else {
3131 $depcount{$dep} = 1;
3132 }
3133}
3134
Steven Rostedtb9066f62011-07-15 21:25:24 -04003135# taken from streamline_config.pl
3136sub read_kconfig {
3137 my ($kconfig) = @_;
3138
3139 my $state = "NONE";
3140 my $config;
3141 my @kconfigs;
3142
3143 my $cont = 0;
3144 my $line;
3145
3146
3147 if (! -f $kconfig) {
3148 doprint "file $kconfig does not exist, skipping\n";
3149 return;
3150 }
3151
3152 open(KIN, "$kconfig")
3153 or die "Can't open $kconfig";
3154 while (<KIN>) {
3155 chomp;
3156
3157 # Make sure that lines ending with \ continue
3158 if ($cont) {
3159 $_ = $line . " " . $_;
3160 }
3161
3162 if (s/\\$//) {
3163 $cont = 1;
3164 $line = $_;
3165 next;
3166 }
3167
3168 $cont = 0;
3169
3170 # collect any Kconfig sources
3171 if (/^source\s*"(.*)"/) {
3172 $kconfigs[$#kconfigs+1] = $1;
3173 }
3174
3175 # configs found
3176 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
3177 $state = "NEW";
3178 $config = $2;
3179
3180 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04003181 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04003182 }
3183
3184 # collect the depends for the config
3185 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
3186
Steven Rostedtac6974c2011-10-04 09:40:17 -04003187 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003188
3189 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04003190 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
3191
3192 # selected by depends on config
3193 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003194
3195 # Check for if statements
3196 } elsif (/^if\s+(.*\S)\s*$/) {
3197 my $deps = $1;
3198 # remove beginning and ending non text
3199 $deps =~ s/^[^a-zA-Z0-9_]*//;
3200 $deps =~ s/[^a-zA-Z0-9_]*$//;
3201
3202 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
3203
3204 $ifdeps[$iflevel++] = join ':', @deps;
3205
3206 } elsif (/^endif/) {
3207
3208 $iflevel-- if ($iflevel);
3209
3210 # stop on "help"
3211 } elsif (/^\s*help\s*$/) {
3212 $state = "NONE";
3213 }
3214 }
3215 close(KIN);
3216
3217 # read in any configs that were found.
3218 foreach $kconfig (@kconfigs) {
3219 if (!defined($read_kconfigs{$kconfig})) {
3220 $read_kconfigs{$kconfig} = 1;
3221 read_kconfig("$builddir/$kconfig");
3222 }
3223 }
3224}
3225
3226sub read_depends {
3227 # find out which arch this is by the kconfig file
3228 open (IN, $output_config)
3229 or dodie "Failed to read $output_config";
3230 my $arch;
3231 while (<IN>) {
3232 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
3233 $arch = $1;
3234 last;
3235 }
3236 }
3237 close IN;
3238
3239 if (!defined($arch)) {
3240 doprint "Could not find arch from config file\n";
3241 doprint "no dependencies used\n";
3242 return;
3243 }
3244
3245 # arch is really the subarch, we need to know
3246 # what directory to look at.
3247 if ($arch eq "i386" || $arch eq "x86_64") {
3248 $arch = "x86";
3249 } elsif ($arch =~ /^tile/) {
3250 $arch = "tile";
3251 }
3252
3253 my $kconfig = "$builddir/arch/$arch/Kconfig";
3254
3255 if (! -f $kconfig && $arch =~ /\d$/) {
3256 my $orig = $arch;
3257 # some subarchs have numbers, truncate them
3258 $arch =~ s/\d*$//;
3259 $kconfig = "$builddir/arch/$arch/Kconfig";
3260 if (! -f $kconfig) {
3261 doprint "No idea what arch dir $orig is for\n";
3262 doprint "no dependencies used\n";
3263 return;
3264 }
3265 }
3266
3267 read_kconfig($kconfig);
3268}
3269
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003270sub read_config_list {
3271 my ($config) = @_;
3272
3273 open (IN, $config)
3274 or dodie "Failed to read $config";
3275
3276 while (<IN>) {
3277 if (/^((CONFIG\S*)=.*)/) {
3278 if (!defined($config_ignore{$2})) {
3279 $config_list{$2} = $1;
3280 }
3281 }
3282 }
3283
3284 close(IN);
3285}
3286
3287sub read_output_config {
3288 my ($config) = @_;
3289
3290 assign_configs \%config_ignore, $config;
3291}
3292
3293sub make_new_config {
3294 my @configs = @_;
3295
3296 open (OUT, ">$output_config")
3297 or dodie "Failed to write $output_config";
3298
3299 foreach my $config (@configs) {
3300 print OUT "$config\n";
3301 }
3302 close OUT;
3303}
3304
Steven Rostedtac6974c2011-10-04 09:40:17 -04003305sub chomp_config {
3306 my ($config) = @_;
3307
3308 $config =~ s/CONFIG_//;
3309
3310 return $config;
3311}
3312
Steven Rostedtb9066f62011-07-15 21:25:24 -04003313sub get_depends {
3314 my ($dep) = @_;
3315
Steven Rostedtac6974c2011-10-04 09:40:17 -04003316 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003317
3318 $dep = $depends{"$kconfig"};
3319
3320 # the dep string we have saves the dependencies as they
3321 # were found, including expressions like ! && ||. We
3322 # want to split this out into just an array of configs.
3323
3324 my $valid = "A-Za-z_0-9";
3325
3326 my @configs;
3327
3328 while ($dep =~ /[$valid]/) {
3329
3330 if ($dep =~ /^[^$valid]*([$valid]+)/) {
3331 my $conf = "CONFIG_" . $1;
3332
3333 $configs[$#configs + 1] = $conf;
3334
3335 $dep =~ s/^[^$valid]*[$valid]+//;
3336 } else {
3337 die "this should never happen";
3338 }
3339 }
3340
3341 return @configs;
3342}
3343
3344my %min_configs;
3345my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003346my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003347my %processed_configs;
3348my %nochange_config;
3349
3350sub test_this_config {
3351 my ($config) = @_;
3352
3353 my $found;
3354
3355 # if we already processed this config, skip it
3356 if (defined($processed_configs{$config})) {
3357 return undef;
3358 }
3359 $processed_configs{$config} = 1;
3360
3361 # if this config failed during this round, skip it
3362 if (defined($nochange_config{$config})) {
3363 return undef;
3364 }
3365
Steven Rostedtac6974c2011-10-04 09:40:17 -04003366 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003367
3368 # Test dependencies first
3369 if (defined($depends{"$kconfig"})) {
3370 my @parents = get_depends $config;
3371 foreach my $parent (@parents) {
3372 # if the parent is in the min config, check it first
3373 next if (!defined($min_configs{$parent}));
3374 $found = test_this_config($parent);
3375 if (defined($found)) {
3376 return $found;
3377 }
3378 }
3379 }
3380
3381 # Remove this config from the list of configs
Adam Leefb16d892012-09-01 01:05:17 +08003382 # do a make olddefconfig and then read the resulting
Steven Rostedtb9066f62011-07-15 21:25:24 -04003383 # .config to make sure it is missing the config that
3384 # we had before
3385 my %configs = %min_configs;
3386 delete $configs{$config};
3387 make_new_config ((values %configs), (values %keep_configs));
3388 make_oldconfig;
3389 undef %configs;
3390 assign_configs \%configs, $output_config;
3391
3392 return $config if (!defined($configs{$config}));
3393
3394 doprint "disabling config $config did not change .config\n";
3395
3396 $nochange_config{$config} = 1;
3397
3398 return undef;
3399}
3400
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003401sub make_min_config {
3402 my ($i) = @_;
3403
Steven Rostedtccc513b2012-05-21 17:13:40 -04003404 my $type = $minconfig_type;
3405 if ($type ne "boot" && $type ne "test") {
3406 fail "Invalid MIN_CONFIG_TYPE '$minconfig_type'\n" .
3407 " make_min_config works only with 'boot' and 'test'\n" and return;
3408 }
3409
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003410 if (!defined($output_minconfig)) {
3411 fail "OUTPUT_MIN_CONFIG not defined" and return;
3412 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04003413
3414 # If output_minconfig exists, and the start_minconfig
3415 # came from min_config, than ask if we should use
3416 # that instead.
3417 if (-f $output_minconfig && !$start_minconfig_defined) {
3418 print "$output_minconfig exists\n";
Steven Rostedt43de3312012-05-21 23:35:12 -04003419 if (!defined($use_output_minconfig)) {
3420 if (read_yn " Use it as minconfig?") {
3421 $start_minconfig = $output_minconfig;
3422 }
3423 } elsif ($use_output_minconfig > 0) {
3424 doprint "Using $output_minconfig as MIN_CONFIG\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003425 $start_minconfig = $output_minconfig;
Steven Rostedt43de3312012-05-21 23:35:12 -04003426 } else {
3427 doprint "Set to still use MIN_CONFIG as starting point\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003428 }
3429 }
3430
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003431 if (!defined($start_minconfig)) {
3432 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
3433 }
3434
Steven Rostedt35ce5952011-07-15 21:57:25 -04003435 my $temp_config = "$tmpdir/temp_config";
3436
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003437 # First things first. We build an allnoconfig to find
3438 # out what the defaults are that we can't touch.
3439 # Some are selections, but we really can't handle selections.
3440
3441 my $save_minconfig = $minconfig;
3442 undef $minconfig;
3443
3444 run_command "$make allnoconfig" or return 0;
3445
Steven Rostedtb9066f62011-07-15 21:25:24 -04003446 read_depends;
3447
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003448 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003449
Steven Rostedt43d1b652011-07-15 22:01:56 -04003450 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003451 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003452
3453 if (defined($ignore_config)) {
3454 # make sure the file exists
3455 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003456 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003457 }
3458
Steven Rostedt43d1b652011-07-15 22:01:56 -04003459 %keep_configs = %save_configs;
3460
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003461 doprint "Load initial configs from $start_minconfig\n";
3462
3463 # Look at the current min configs, and save off all the
3464 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003465 assign_configs \%min_configs, $start_minconfig;
3466
3467 my @config_keys = keys %min_configs;
3468
Steven Rostedtac6974c2011-10-04 09:40:17 -04003469 # All configs need a depcount
3470 foreach my $config (@config_keys) {
3471 my $kconfig = chomp_config $config;
3472 if (!defined $depcount{$kconfig}) {
3473 $depcount{$kconfig} = 0;
3474 }
3475 }
3476
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003477 # Remove anything that was set by the make allnoconfig
3478 # we shouldn't need them as they get set for us anyway.
3479 foreach my $config (@config_keys) {
3480 # Remove anything in the ignore_config
3481 if (defined($keep_configs{$config})) {
3482 my $file = $ignore_config;
3483 $file =~ s,.*/(.*?)$,$1,;
3484 doprint "$config set by $file ... ignored\n";
3485 delete $min_configs{$config};
3486 next;
3487 }
3488 # But make sure the settings are the same. If a min config
3489 # sets a selection, we do not want to get rid of it if
3490 # it is not the same as what we have. Just move it into
3491 # the keep configs.
3492 if (defined($config_ignore{$config})) {
3493 if ($config_ignore{$config} ne $min_configs{$config}) {
3494 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3495 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3496 $keep_configs{$config} = $min_configs{$config};
3497 } else {
3498 doprint "$config set by allnoconfig ... ignored\n";
3499 }
3500 delete $min_configs{$config};
3501 }
3502 }
3503
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003504 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003505 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003506
3507 while (!$done) {
3508
3509 my $config;
3510 my $found;
3511
3512 # Now disable each config one by one and do a make oldconfig
3513 # till we find a config that changes our list.
3514
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003515 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003516
3517 # Sort keys by who is most dependent on
3518 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3519 @test_configs ;
3520
3521 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003522 my $reset = 1;
3523 for (my $i = 0; $i < $#test_configs; $i++) {
3524 if (!defined($nochange_config{$test_configs[0]})) {
3525 $reset = 0;
3526 last;
3527 }
3528 # This config didn't change the .config last time.
3529 # Place it at the end
3530 my $config = shift @test_configs;
3531 push @test_configs, $config;
3532 }
3533
3534 # if every test config has failed to modify the .config file
3535 # in the past, then reset and start over.
3536 if ($reset) {
3537 undef %nochange_config;
3538 }
3539
Steven Rostedtb9066f62011-07-15 21:25:24 -04003540 undef %processed_configs;
3541
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003542 foreach my $config (@test_configs) {
3543
Steven Rostedtb9066f62011-07-15 21:25:24 -04003544 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003545
Steven Rostedtb9066f62011-07-15 21:25:24 -04003546 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003547
3548 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003549 }
3550
3551 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003552 # we could have failed due to the nochange_config hash
3553 # reset and try again
3554 if (!$take_two) {
3555 undef %nochange_config;
3556 $take_two = 1;
3557 next;
3558 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003559 doprint "No more configs found that we can disable\n";
3560 $done = 1;
3561 last;
3562 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003563 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003564
3565 $config = $found;
3566
3567 doprint "Test with $config disabled\n";
3568
3569 # set in_bisect to keep build and monitor from dieing
3570 $in_bisect = 1;
3571
3572 my $failed = 0;
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003573 build "oldconfig" or $failed = 1;
3574 if (!$failed) {
3575 start_monitor_and_boot or $failed = 1;
Steven Rostedtccc513b2012-05-21 17:13:40 -04003576
3577 if ($type eq "test" && !$failed) {
3578 do_run_test or $failed = 1;
3579 }
3580
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003581 end_monitor;
3582 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003583
3584 $in_bisect = 0;
3585
3586 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003587 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003588 # this config is needed, add it to the ignore list.
3589 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003590 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003591 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003592
3593 # update new ignore configs
3594 if (defined($ignore_config)) {
3595 open (OUT, ">$temp_config")
3596 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003597 foreach my $config (keys %save_configs) {
3598 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003599 }
3600 close OUT;
3601 run_command "mv $temp_config $ignore_config" or
3602 dodie "failed to copy update to $ignore_config";
3603 }
3604
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003605 } else {
3606 # We booted without this config, remove it from the minconfigs.
3607 doprint "$config is not needed, disabling\n";
3608
3609 delete $min_configs{$config};
3610
3611 # Also disable anything that is not enabled in this config
3612 my %configs;
3613 assign_configs \%configs, $output_config;
3614 my @config_keys = keys %min_configs;
3615 foreach my $config (@config_keys) {
3616 if (!defined($configs{$config})) {
3617 doprint "$config is not set, disabling\n";
3618 delete $min_configs{$config};
3619 }
3620 }
3621
3622 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003623 open (OUT, ">$temp_config")
3624 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003625 foreach my $config (keys %keep_configs) {
3626 print OUT "$keep_configs{$config}\n";
3627 }
3628 foreach my $config (keys %min_configs) {
3629 print OUT "$min_configs{$config}\n";
3630 }
3631 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003632
3633 run_command "mv $temp_config $output_minconfig" or
3634 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003635 }
3636
3637 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003638 reboot_to_good $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003639 }
3640
3641 success $i;
3642 return 1;
3643}
3644
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003645$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003646
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003647if ($#ARGV == 0) {
3648 $ktest_config = $ARGV[0];
3649 if (! -f $ktest_config) {
3650 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003651 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003652 exit 0;
3653 }
3654 }
3655} else {
3656 $ktest_config = "ktest.conf";
3657}
3658
3659if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003660 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003661 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003662 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3663 print OUT << "EOF"
3664# Generated by ktest.pl
3665#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003666
3667# PWD is a ktest.pl variable that will result in the process working
3668# directory that ktest.pl is executed in.
3669
3670# THIS_DIR is automatically assigned the PWD of the path that generated
3671# the config file. It is best to use this variable when assigning other
3672# directory paths within this directory. This allows you to easily
3673# move the test cases to other locations or to other machines.
3674#
3675THIS_DIR := $variable{"PWD"}
3676
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003677# Define each test with TEST_START
3678# The config options below it will override the defaults
3679TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003680TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003681
3682DEFAULTS
3683EOF
3684;
3685 close(OUT);
3686}
3687read_config $ktest_config;
3688
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003689if (defined($opt{"LOG_FILE"})) {
Steven Rostedt (Red Hat)04262be2013-01-31 10:12:20 -05003690 $opt{"LOG_FILE"} = eval_option("LOG_FILE", $opt{"LOG_FILE"}, -1);
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003691}
3692
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003693# Append any configs entered in manually to the config file.
3694my @new_configs = keys %entered_configs;
3695if ($#new_configs >= 0) {
3696 print "\nAppending entered in configs to $ktest_config\n";
3697 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3698 foreach my $config (@new_configs) {
3699 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003700 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003701 }
3702}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003703
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003704if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3705 unlink $opt{"LOG_FILE"};
3706}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003707
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003708doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3709
Steven Rostedta57419b2010-11-02 15:13:54 -04003710for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3711
3712 if (!$i) {
3713 doprint "DEFAULT OPTIONS:\n";
3714 } else {
3715 doprint "\nTEST $i OPTIONS";
3716 if (defined($repeat_tests{$i})) {
3717 $repeat = $repeat_tests{$i};
3718 doprint " ITERATE $repeat";
3719 }
3720 doprint "\n";
3721 }
3722
3723 foreach my $option (sort keys %opt) {
3724
3725 if ($option =~ /\[(\d+)\]$/) {
3726 next if ($i != $1);
3727 } else {
3728 next if ($i);
3729 }
3730
3731 doprint "$option = $opt{$option}\n";
3732 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003733}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003734
Steven Rostedt2a625122011-05-20 15:48:59 -04003735sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003736 my ($name, $i) = @_;
3737
3738 my $option = "$name\[$i\]";
3739
3740 if (defined($opt{$option})) {
3741 return $opt{$option};
3742 }
3743
Steven Rostedta57419b2010-11-02 15:13:54 -04003744 foreach my $test (keys %repeat_tests) {
3745 if ($i >= $test &&
3746 $i < $test + $repeat_tests{$test}) {
3747 $option = "$name\[$test\]";
3748 if (defined($opt{$option})) {
3749 return $opt{$option};
3750 }
3751 }
3752 }
3753
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003754 if (defined($opt{$name})) {
3755 return $opt{$name};
3756 }
3757
3758 return undef;
3759}
3760
Steven Rostedt2a625122011-05-20 15:48:59 -04003761sub set_test_option {
3762 my ($name, $i) = @_;
3763
3764 my $option = __set_test_option($name, $i);
3765 return $option if (!defined($option));
3766
Steven Rostedt (Red Hat)04262be2013-01-31 10:12:20 -05003767 return eval_option($name, $option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003768}
3769
Steven Rostedt2545eb62010-11-02 15:01:32 -04003770# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003771for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003772
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003773 # Do not reboot on failing test options
3774 $no_reboot = 1;
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003775 $reboot_success = 0;
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003776
Steven Rostedt683a3e62012-05-18 13:34:35 -04003777 $have_version = 0;
3778
Steven Rostedt576f6272010-11-02 14:58:38 -04003779 $iteration = $i;
3780
Steven Rostedtc1434dc2012-07-20 22:39:16 -04003781 undef %force_config;
3782
Steven Rostedta75fece2010-11-02 14:58:27 -04003783 my $makecmd = set_test_option("MAKE_CMD", $i);
3784
Steven Rostedt9cc9e092011-12-22 21:37:22 -05003785 # Load all the options into their mapped variable names
3786 foreach my $opt (keys %option_map) {
3787 ${$option_map{$opt}} = set_test_option($opt, $i);
3788 }
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003789
Steven Rostedt35ce5952011-07-15 21:57:25 -04003790 $start_minconfig_defined = 1;
3791
Steven Rostedt921ed4c2012-07-19 15:18:27 -04003792 # The first test may override the PRE_KTEST option
3793 if (defined($pre_ktest) && $i == 1) {
3794 doprint "\n";
3795 run_command $pre_ktest;
3796 }
3797
3798 # Any test can override the POST_KTEST option
3799 # The last test takes precedence.
3800 if (defined($post_ktest)) {
3801 $final_post_ktest = $post_ktest;
3802 }
3803
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003804 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003805 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003806 $start_minconfig = $minconfig;
3807 }
3808
Steven Rostedta75fece2010-11-02 14:58:27 -04003809 chdir $builddir || die "can't change directory to $builddir";
3810
Andrew Jonesa908a662011-08-12 15:32:03 +02003811 foreach my $dir ($tmpdir, $outputdir) {
3812 if (!-d $dir) {
3813 mkpath($dir) or
3814 die "can't create $dir";
3815 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003816 }
3817
Steven Rostedte48c5292010-11-02 14:35:37 -04003818 $ENV{"SSH_USER"} = $ssh_user;
3819 $ENV{"MACHINE"} = $machine;
3820
Steven Rostedta75fece2010-11-02 14:58:27 -04003821 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303822 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003823 $dmesg = "$tmpdir/dmesg-$machine";
3824 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003825 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003826
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003827 if (!$buildonly) {
3828 $target = "$ssh_user\@$machine";
3829 if ($reboot_type eq "grub") {
3830 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
Steven Rostedta15ba912012-11-13 14:30:37 -05003831 } elsif ($reboot_type eq "grub2") {
3832 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3833 dodie "GRUB_FILE not defined" if (!defined($grub_file));
Steven Rostedt77869542012-12-11 17:37:41 -05003834 } elsif ($reboot_type eq "syslinux") {
3835 dodie "SYSLINUX_LABEL not defined" if (!defined($syslinux_label));
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003836 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003837 }
3838
3839 my $run_type = $build_type;
3840 if ($test_type eq "patchcheck") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003841 $run_type = $patchcheck_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003842 } elsif ($test_type eq "bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003843 $run_type = $bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003844 } elsif ($test_type eq "config_bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003845 $run_type = $config_bisect_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003846 }
3847
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003848 if ($test_type eq "make_min_config") {
3849 $run_type = "";
3850 }
3851
Steven Rostedta75fece2010-11-02 14:58:27 -04003852 # mistake in config file?
3853 if (!defined($run_type)) {
3854 $run_type = "ERROR";
3855 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003856
Steven Rostedte0a87422011-09-30 17:50:48 -04003857 my $installme = "";
3858 $installme = " no_install" if ($no_install);
3859
Steven Rostedt2545eb62010-11-02 15:01:32 -04003860 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003861 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003862
Steven Rostedt921ed4c2012-07-19 15:18:27 -04003863 if (defined($pre_test)) {
3864 run_command $pre_test;
3865 }
3866
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003867 unlink $dmesg;
3868 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303869 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003870
Steven Rostedt250bae82011-07-15 22:05:59 -04003871 if (defined($addconfig)) {
3872 my $min = $minconfig;
3873 if (!defined($minconfig)) {
3874 $min = "";
3875 }
3876 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003877 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003878 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003879 }
3880
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003881 if (defined($checkout)) {
3882 run_command "git checkout $checkout" or
3883 die "failed to checkout $checkout";
3884 }
3885
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003886 $no_reboot = 0;
3887
Steven Rostedt648a1822012-03-21 11:18:27 -04003888 # A test may opt to not reboot the box
3889 if ($reboot_on_success) {
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003890 $reboot_success = 1;
Steven Rostedt648a1822012-03-21 11:18:27 -04003891 }
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003892
Steven Rostedta75fece2010-11-02 14:58:27 -04003893 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003894 bisect $i;
3895 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003896 } elsif ($test_type eq "config_bisect") {
3897 config_bisect $i;
3898 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003899 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003900 patchcheck $i;
3901 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003902 } elsif ($test_type eq "make_min_config") {
3903 make_min_config $i;
3904 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003905 }
3906
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003907 if ($build_type ne "nobuild") {
3908 build $build_type or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003909 }
3910
Steven Rostedtcd8e3682011-08-18 16:35:44 -04003911 if ($test_type eq "install") {
3912 get_version;
3913 install;
3914 success $i;
3915 next;
3916 }
3917
Steven Rostedta75fece2010-11-02 14:58:27 -04003918 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04003919 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04003920 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04003921
3922 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3923 do_run_test or $failed = 1;
3924 }
3925 end_monitor;
3926 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003927 }
3928
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003929 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003930}
3931
Steven Rostedt921ed4c2012-07-19 15:18:27 -04003932if (defined($final_post_ktest)) {
3933 run_command $final_post_ktest;
3934}
3935
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003936if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003937 halt;
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003938} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot && $reboot_success) {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003939 reboot_to_good;
Steven Rostedt648a1822012-03-21 11:18:27 -04003940} elsif (defined($switch_to_good)) {
3941 # still need to get to the good kernel
3942 run_command $switch_to_good;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003943}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003944
Steven Rostedt648a1822012-03-21 11:18:27 -04003945
Steven Rostedte48c5292010-11-02 14:35:37 -04003946doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3947
Steven Rostedt2545eb62010-11-02 15:01:32 -04003948exit 0;