blob: 839989d08dc24c8e778b17f140b5781b646299b2 [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 Rostedt (Red Hat)4283b162013-01-30 18:37:47 -0500129my $warnings_file;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400130my $ignore_config;
Steven Rostedtbe405f92012-01-04 21:51:59 -0500131my $ignore_errors;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -0400132my $addconfig;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400133my $in_bisect = 0;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500134my $bisect_bad_commit = "";
Steven Rostedtd6ce2a02010-11-02 14:58:05 -0400135my $reverse_bisect;
Steven Rostedtc960bb92011-03-08 09:22:39 -0500136my $bisect_manual;
Steven Rostedtc23dca72011-03-08 09:26:31 -0500137my $bisect_skip;
Steven Rostedt30f75da2011-06-13 10:35:35 -0400138my $config_bisect_good;
Steven Rostedtc5dacb82011-12-22 12:43:57 -0500139my $bisect_ret_good;
140my $bisect_ret_bad;
141my $bisect_ret_skip;
142my $bisect_ret_abort;
143my $bisect_ret_default;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400144my $in_patchcheck = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400145my $run_test;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400146my $redirect;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400147my $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +0530148my $testlog;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400149my $dmesg;
150my $monitor_fp;
151my $monitor_pid;
152my $monitor_cnt = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -0400153my $sleep_time;
154my $bisect_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -0400155my $patchcheck_sleep_time;
Steven Rostedt19902072011-06-14 20:46:25 -0400156my $ignore_warnings;
Steven Rostedta75fece2010-11-02 14:58:27 -0400157my $store_failures;
Rabin Vincentde5b6e32011-11-18 17:05:31 +0530158my $store_successes;
Steven Rostedt9064af52011-06-13 10:38:48 -0400159my $test_name;
Steven Rostedta75fece2010-11-02 14:58:27 -0400160my $timeout;
161my $booted_timeout;
Steven Rostedtf1a5b962011-06-13 10:30:00 -0400162my $detect_triplefault;
Steven Rostedta75fece2010-11-02 14:58:27 -0400163my $console;
Steven Rostedt2b803362011-09-30 18:00:23 -0400164my $reboot_success_line;
Steven Rostedta75fece2010-11-02 14:58:27 -0400165my $success_line;
Steven Rostedt1c8a6172010-11-09 12:55:40 -0500166my $stop_after_success;
167my $stop_after_failure;
Steven Rostedt2d01b262011-03-08 09:47:54 -0500168my $stop_test_after;
Steven Rostedta75fece2010-11-02 14:58:27 -0400169my $build_target;
170my $target_image;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500171my $checkout;
Steven Rostedta75fece2010-11-02 14:58:27 -0400172my $localversion;
Steven Rostedt576f6272010-11-02 14:58:38 -0400173my $iteration = 0;
Steven Rostedte48c5292010-11-02 14:35:37 -0400174my $successes = 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400175
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500176my $bisect_good;
177my $bisect_bad;
178my $bisect_type;
179my $bisect_start;
180my $bisect_replay;
181my $bisect_files;
182my $bisect_reverse;
183my $bisect_check;
184
185my $config_bisect;
186my $config_bisect_type;
Steven Rostedtb0918612012-07-19 15:26:00 -0400187my $config_bisect_check;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500188
189my $patchcheck_type;
190my $patchcheck_start;
191my $patchcheck_end;
192
Steven Rostedt165708b2011-11-26 20:56:52 -0500193# set when a test is something other that just building or install
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500194# which would require more options.
195my $buildonly = 1;
196
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -0500197# tell build not to worry about warnings, even when WARNINGS_FILE is set
198my $warnings_ok = 0;
199
Steven Rostedtdbd37832011-11-23 16:00:48 -0500200# set when creating a new config
201my $newconfig = 0;
202
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500203my %entered_configs;
204my %config_help;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400205my %variable;
Steven Rostedtcf79fab2012-07-19 15:29:43 -0400206
207# force_config is the list of configs that we force enabled (or disabled)
208# in a .config file. The MIN_CONFIG and ADD_CONFIG configs.
Steven Rostedtfcb3f162011-06-13 10:40:58 -0400209my %force_config;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500210
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400211# do not force reboots on config problems
212my $no_reboot = 1;
213
Steven Rostedt759a3cc2012-05-01 08:20:12 -0400214# reboot on success
215my $reboot_success = 0;
216
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500217my %option_map = (
218 "MACHINE" => \$machine,
219 "SSH_USER" => \$ssh_user,
220 "TMP_DIR" => \$tmpdir,
221 "OUTPUT_DIR" => \$outputdir,
222 "BUILD_DIR" => \$builddir,
223 "TEST_TYPE" => \$test_type,
Steven Rostedt921ed4c2012-07-19 15:18:27 -0400224 "PRE_KTEST" => \$pre_ktest,
225 "POST_KTEST" => \$post_ktest,
226 "PRE_TEST" => \$pre_test,
227 "POST_TEST" => \$post_test,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500228 "BUILD_TYPE" => \$build_type,
229 "BUILD_OPTIONS" => \$build_options,
230 "PRE_BUILD" => \$pre_build,
231 "POST_BUILD" => \$post_build,
232 "PRE_BUILD_DIE" => \$pre_build_die,
233 "POST_BUILD_DIE" => \$post_build_die,
234 "POWER_CYCLE" => \$power_cycle,
235 "REBOOT" => \$reboot,
236 "BUILD_NOCLEAN" => \$noclean,
237 "MIN_CONFIG" => \$minconfig,
238 "OUTPUT_MIN_CONFIG" => \$output_minconfig,
239 "START_MIN_CONFIG" => \$start_minconfig,
Steven Rostedtccc513b2012-05-21 17:13:40 -0400240 "MIN_CONFIG_TYPE" => \$minconfig_type,
Steven Rostedt43de3312012-05-21 23:35:12 -0400241 "USE_OUTPUT_MIN_CONFIG" => \$use_output_minconfig,
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -0500242 "WARNINGS_FILE" => \$warnings_file,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500243 "IGNORE_CONFIG" => \$ignore_config,
244 "TEST" => \$run_test,
245 "ADD_CONFIG" => \$addconfig,
246 "REBOOT_TYPE" => \$reboot_type,
247 "GRUB_MENU" => \$grub_menu,
Steven Rostedta15ba912012-11-13 14:30:37 -0500248 "GRUB_FILE" => \$grub_file,
249 "GRUB_REBOOT" => \$grub_reboot,
Steven Rostedt77869542012-12-11 17:37:41 -0500250 "SYSLINUX" => \$syslinux,
251 "SYSLINUX_PATH" => \$syslinux_path,
252 "SYSLINUX_LABEL" => \$syslinux_label,
Steven Rostedte5c2ec12012-07-19 15:22:05 -0400253 "PRE_INSTALL" => \$pre_install,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500254 "POST_INSTALL" => \$post_install,
255 "NO_INSTALL" => \$no_install,
256 "REBOOT_SCRIPT" => \$reboot_script,
257 "REBOOT_ON_ERROR" => \$reboot_on_error,
258 "SWITCH_TO_GOOD" => \$switch_to_good,
259 "SWITCH_TO_TEST" => \$switch_to_test,
260 "POWEROFF_ON_ERROR" => \$poweroff_on_error,
Steven Rostedt648a1822012-03-21 11:18:27 -0400261 "REBOOT_ON_SUCCESS" => \$reboot_on_success,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500262 "DIE_ON_FAILURE" => \$die_on_failure,
263 "POWER_OFF" => \$power_off,
264 "POWERCYCLE_AFTER_REBOOT" => \$powercycle_after_reboot,
265 "POWEROFF_AFTER_HALT" => \$poweroff_after_halt,
Steven Rostedt407b95b2012-07-19 16:05:42 -0400266 "MAX_MONITOR_WAIT" => \$max_monitor_wait,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500267 "SLEEP_TIME" => \$sleep_time,
268 "BISECT_SLEEP_TIME" => \$bisect_sleep_time,
269 "PATCHCHECK_SLEEP_TIME" => \$patchcheck_sleep_time,
270 "IGNORE_WARNINGS" => \$ignore_warnings,
Steven Rostedtbe405f92012-01-04 21:51:59 -0500271 "IGNORE_ERRORS" => \$ignore_errors,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500272 "BISECT_MANUAL" => \$bisect_manual,
273 "BISECT_SKIP" => \$bisect_skip,
274 "CONFIG_BISECT_GOOD" => \$config_bisect_good,
275 "BISECT_RET_GOOD" => \$bisect_ret_good,
276 "BISECT_RET_BAD" => \$bisect_ret_bad,
277 "BISECT_RET_SKIP" => \$bisect_ret_skip,
278 "BISECT_RET_ABORT" => \$bisect_ret_abort,
279 "BISECT_RET_DEFAULT" => \$bisect_ret_default,
280 "STORE_FAILURES" => \$store_failures,
281 "STORE_SUCCESSES" => \$store_successes,
282 "TEST_NAME" => \$test_name,
283 "TIMEOUT" => \$timeout,
284 "BOOTED_TIMEOUT" => \$booted_timeout,
285 "CONSOLE" => \$console,
286 "DETECT_TRIPLE_FAULT" => \$detect_triplefault,
287 "SUCCESS_LINE" => \$success_line,
288 "REBOOT_SUCCESS_LINE" => \$reboot_success_line,
289 "STOP_AFTER_SUCCESS" => \$stop_after_success,
290 "STOP_AFTER_FAILURE" => \$stop_after_failure,
291 "STOP_TEST_AFTER" => \$stop_test_after,
292 "BUILD_TARGET" => \$build_target,
293 "SSH_EXEC" => \$ssh_exec,
294 "SCP_TO_TARGET" => \$scp_to_target,
Steven Rostedt02ad2612012-03-21 08:21:24 -0400295 "SCP_TO_TARGET_INSTALL" => \$scp_to_target_install,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500296 "CHECKOUT" => \$checkout,
297 "TARGET_IMAGE" => \$target_image,
298 "LOCALVERSION" => \$localversion,
299
300 "BISECT_GOOD" => \$bisect_good,
301 "BISECT_BAD" => \$bisect_bad,
302 "BISECT_TYPE" => \$bisect_type,
303 "BISECT_START" => \$bisect_start,
304 "BISECT_REPLAY" => \$bisect_replay,
305 "BISECT_FILES" => \$bisect_files,
306 "BISECT_REVERSE" => \$bisect_reverse,
307 "BISECT_CHECK" => \$bisect_check,
308
309 "CONFIG_BISECT" => \$config_bisect,
310 "CONFIG_BISECT_TYPE" => \$config_bisect_type,
Steven Rostedtb0918612012-07-19 15:26:00 -0400311 "CONFIG_BISECT_CHECK" => \$config_bisect_check,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500312
313 "PATCHCHECK_TYPE" => \$patchcheck_type,
314 "PATCHCHECK_START" => \$patchcheck_start,
315 "PATCHCHECK_END" => \$patchcheck_end,
316);
317
318# Options may be used by other options, record them.
319my %used_options;
320
Steven Rostedt7bf51072011-10-22 09:07:03 -0400321# default variables that can be used
322chomp ($variable{"PWD"} = `pwd`);
323
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500324$config_help{"MACHINE"} = << "EOF"
325 The machine hostname that you will test.
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500326 For build only tests, it is still needed to differentiate log files.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500327EOF
328 ;
329$config_help{"SSH_USER"} = << "EOF"
330 The box is expected to have ssh on normal bootup, provide the user
331 (most likely root, since you need privileged operations)
332EOF
333 ;
334$config_help{"BUILD_DIR"} = << "EOF"
335 The directory that contains the Linux source code (full path).
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500336 You can use \${PWD} that will be the path where ktest.pl is run, or use
337 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500338EOF
339 ;
340$config_help{"OUTPUT_DIR"} = << "EOF"
341 The directory that the objects will be built (full path).
342 (can not be same as BUILD_DIR)
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500343 You can use \${PWD} that will be the path where ktest.pl is run, or use
344 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500345EOF
346 ;
347$config_help{"BUILD_TARGET"} = << "EOF"
348 The location of the compiled file to copy to the target.
349 (relative to OUTPUT_DIR)
350EOF
351 ;
Steven Rostedtdbd37832011-11-23 16:00:48 -0500352$config_help{"BUILD_OPTIONS"} = << "EOF"
353 Options to add to \"make\" when building.
354 i.e. -j20
355EOF
356 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500357$config_help{"TARGET_IMAGE"} = << "EOF"
358 The place to put your image on the test machine.
359EOF
360 ;
361$config_help{"POWER_CYCLE"} = << "EOF"
362 A script or command to reboot the box.
363
364 Here is a digital loggers power switch example
365 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
366
367 Here is an example to reboot a virtual box on the current host
368 with the name "Guest".
369 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
370EOF
371 ;
372$config_help{"CONSOLE"} = << "EOF"
373 The script or command that reads the console
374
375 If you use ttywatch server, something like the following would work.
376CONSOLE = nc -d localhost 3001
377
378 For a virtual machine with guest name "Guest".
379CONSOLE = virsh console Guest
380EOF
381 ;
382$config_help{"LOCALVERSION"} = << "EOF"
383 Required version ending to differentiate the test
384 from other linux builds on the system.
385EOF
386 ;
387$config_help{"REBOOT_TYPE"} = << "EOF"
388 Way to reboot the box to the test kernel.
Steven Rostedt77869542012-12-11 17:37:41 -0500389 Only valid options so far are "grub", "grub2", "syslinux", and "script".
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500390
391 If you specify grub, it will assume grub version 1
392 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
393 and select that target to reboot to the kernel. If this is not
394 your setup, then specify "script" and have a command or script
395 specified in REBOOT_SCRIPT to boot to the target.
396
397 The entry in /boot/grub/menu.lst must be entered in manually.
398 The test will not modify that file.
Steven Rostedta15ba912012-11-13 14:30:37 -0500399
400 If you specify grub2, then you also need to specify both \$GRUB_MENU
401 and \$GRUB_FILE.
Steven Rostedt77869542012-12-11 17:37:41 -0500402
403 If you specify syslinux, then you may use SYSLINUX to define the syslinux
404 command (defaults to extlinux), and SYSLINUX_PATH to specify the path to
405 the syslinux install (defaults to /boot/extlinux). But you have to specify
406 SYSLINUX_LABEL to define the label to boot to for the test kernel.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500407EOF
408 ;
409$config_help{"GRUB_MENU"} = << "EOF"
410 The grub title name for the test kernel to boot
Steven Rostedta15ba912012-11-13 14:30:37 -0500411 (Only mandatory if REBOOT_TYPE = grub or grub2)
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500412
413 Note, ktest.pl will not update the grub menu.lst, you need to
414 manually add an option for the test. ktest.pl will search
415 the grub menu.lst for this option to find what kernel to
416 reboot into.
417
418 For example, if in the /boot/grub/menu.lst the test kernel title has:
419 title Test Kernel
420 kernel vmlinuz-test
421 GRUB_MENU = Test Kernel
Steven Rostedta15ba912012-11-13 14:30:37 -0500422
423 For grub2, a search of \$GRUB_FILE is performed for the lines
424 that begin with "menuentry". It will not detect submenus. The
425 menu must be a non-nested menu. Add the quotes used in the menu
426 to guarantee your selection, as the first menuentry with the content
427 of \$GRUB_MENU that is found will be used.
428EOF
429 ;
430$config_help{"GRUB_FILE"} = << "EOF"
431 If grub2 is used, the full path for the grub.cfg file is placed
432 here. Use something like /boot/grub2/grub.cfg to search.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500433EOF
434 ;
Steven Rostedt77869542012-12-11 17:37:41 -0500435$config_help{"SYSLINUX_LABEL"} = << "EOF"
436 If syslinux is used, the label that boots the target kernel must
437 be specified with SYSLINUX_LABEL.
438EOF
439 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500440$config_help{"REBOOT_SCRIPT"} = << "EOF"
441 A script to reboot the target into the test kernel
442 (Only mandatory if REBOOT_TYPE = script)
443EOF
444 ;
445
Steven Rostedtdad98752011-11-22 20:48:57 -0500446sub read_prompt {
447 my ($cancel, $prompt) = @_;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400448
449 my $ans;
450
451 for (;;) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500452 if ($cancel) {
453 print "$prompt [y/n/C] ";
454 } else {
455 print "$prompt [Y/n] ";
456 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400457 $ans = <STDIN>;
458 chomp $ans;
459 if ($ans =~ /^\s*$/) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500460 if ($cancel) {
461 $ans = "c";
462 } else {
463 $ans = "y";
464 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400465 }
466 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
Steven Rostedtdad98752011-11-22 20:48:57 -0500467 if ($cancel) {
468 last if ($ans =~ /^c$/i);
469 print "Please answer either 'y', 'n' or 'c'.\n";
470 } else {
471 print "Please answer either 'y' or 'n'.\n";
472 }
473 }
474 if ($ans =~ /^c/i) {
475 exit;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400476 }
477 if ($ans !~ /^y$/i) {
478 return 0;
479 }
480 return 1;
481}
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500482
Steven Rostedtdad98752011-11-22 20:48:57 -0500483sub read_yn {
484 my ($prompt) = @_;
485
486 return read_prompt 0, $prompt;
487}
488
489sub read_ync {
490 my ($prompt) = @_;
491
492 return read_prompt 1, $prompt;
493}
494
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500495sub get_ktest_config {
496 my ($config) = @_;
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400497 my $ans;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500498
499 return if (defined($opt{$config}));
500
501 if (defined($config_help{$config})) {
502 print "\n";
503 print $config_help{$config};
504 }
505
506 for (;;) {
507 print "$config = ";
Steven Rostedtdbd37832011-11-23 16:00:48 -0500508 if (defined($default{$config}) && length($default{$config})) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500509 print "\[$default{$config}\] ";
510 }
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400511 $ans = <STDIN>;
512 $ans =~ s/^\s*(.*\S)\s*$/$1/;
513 if ($ans =~ /^\s*$/) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500514 if ($default{$config}) {
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400515 $ans = $default{$config};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500516 } else {
517 print "Your answer can not be blank\n";
518 next;
519 }
520 }
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500521 $entered_configs{$config} = ${ans};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500522 last;
523 }
524}
525
526sub get_ktest_configs {
527 get_ktest_config("MACHINE");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500528 get_ktest_config("BUILD_DIR");
529 get_ktest_config("OUTPUT_DIR");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500530
Steven Rostedtdbd37832011-11-23 16:00:48 -0500531 if ($newconfig) {
532 get_ktest_config("BUILD_OPTIONS");
533 }
534
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500535 # options required for other than just building a kernel
536 if (!$buildonly) {
Steven Rostedt165708b2011-11-26 20:56:52 -0500537 get_ktest_config("POWER_CYCLE");
538 get_ktest_config("CONSOLE");
539 }
540
541 # options required for install and more
542 if ($buildonly != 1) {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500543 get_ktest_config("SSH_USER");
544 get_ktest_config("BUILD_TARGET");
545 get_ktest_config("TARGET_IMAGE");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500546 }
547
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500548 get_ktest_config("LOCALVERSION");
549
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500550 return if ($buildonly);
551
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500552 my $rtype = $opt{"REBOOT_TYPE"};
553
554 if (!defined($rtype)) {
555 if (!defined($opt{"GRUB_MENU"})) {
556 get_ktest_config("REBOOT_TYPE");
557 $rtype = $entered_configs{"REBOOT_TYPE"};
558 } else {
559 $rtype = "grub";
560 }
561 }
562
563 if ($rtype eq "grub") {
564 get_ktest_config("GRUB_MENU");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500565 }
Steven Rostedta15ba912012-11-13 14:30:37 -0500566
567 if ($rtype eq "grub2") {
568 get_ktest_config("GRUB_MENU");
569 get_ktest_config("GRUB_FILE");
570 }
Steven Rostedt77869542012-12-11 17:37:41 -0500571
572 if ($rtype eq "syslinux") {
573 get_ktest_config("SYSLINUX_LABEL");
574 }
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500575}
576
Steven Rostedt77d942c2011-05-20 13:36:58 -0400577sub process_variables {
Steven Rostedt8d735212011-10-17 11:36:44 -0400578 my ($value, $remove_undef) = @_;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400579 my $retval = "";
580
581 # We want to check for '\', and it is just easier
582 # to check the previous characet of '$' and not need
583 # to worry if '$' is the first character. By adding
584 # a space to $value, we can just check [^\\]\$ and
585 # it will still work.
586 $value = " $value";
587
588 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
589 my $begin = $1;
590 my $var = $2;
591 my $end = $3;
592 # append beginning of value to retval
593 $retval = "$retval$begin";
594 if (defined($variable{$var})) {
595 $retval = "$retval$variable{$var}";
Steven Rostedt8d735212011-10-17 11:36:44 -0400596 } elsif (defined($remove_undef) && $remove_undef) {
597 # for if statements, any variable that is not defined,
598 # we simple convert to 0
599 $retval = "${retval}0";
Steven Rostedt77d942c2011-05-20 13:36:58 -0400600 } else {
601 # put back the origin piece.
602 $retval = "$retval\$\{$var\}";
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500603 # This could be an option that is used later, save
604 # it so we don't warn if this option is not one of
605 # ktests options.
606 $used_options{$var} = 1;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400607 }
608 $value = $end;
609 }
610 $retval = "$retval$value";
611
612 # remove the space added in the beginning
613 $retval =~ s/ //;
614
615 return "$retval"
616}
617
Steven Rostedta57419b2010-11-02 15:13:54 -0400618sub set_value {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400619 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
Steven Rostedta57419b2010-11-02 15:13:54 -0400620
Steven Rostedtcad96662011-12-22 11:32:52 -0500621 my $prvalue = process_variables($rvalue);
622
623 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500624 # Note if a test is something other than build, then we
625 # will need other manditory options.
Steven Rostedtcad96662011-12-22 11:32:52 -0500626 if ($prvalue ne "install") {
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -0500627 # for bisect, we need to check BISECT_TYPE
628 if ($prvalue ne "bisect") {
629 $buildonly = 0;
630 }
631 } else {
632 # install still limits some manditory options.
633 $buildonly = 2;
634 }
635 }
636
637 if ($buildonly && $lvalue =~ /^BISECT_TYPE(\[.*\])?$/ && $prvalue ne "build") {
638 if ($prvalue ne "install") {
Steven Rostedt165708b2011-11-26 20:56:52 -0500639 $buildonly = 0;
640 } else {
641 # install still limits some manditory options.
642 $buildonly = 2;
643 }
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500644 }
645
Steven Rostedta57419b2010-11-02 15:13:54 -0400646 if (defined($opt{$lvalue})) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400647 if (!$override || defined(${$overrides}{$lvalue})) {
648 my $extra = "";
649 if ($override) {
650 $extra = "In the same override section!\n";
651 }
652 die "$name: $.: Option $lvalue defined more than once!\n$extra";
653 }
Steven Rostedtcad96662011-12-22 11:32:52 -0500654 ${$overrides}{$lvalue} = $prvalue;
Steven Rostedta57419b2010-11-02 15:13:54 -0400655 }
Steven Rostedt21a96792010-11-08 16:45:50 -0500656 if ($rvalue =~ /^\s*$/) {
657 delete $opt{$lvalue};
658 } else {
Steven Rostedtcad96662011-12-22 11:32:52 -0500659 $opt{$lvalue} = $prvalue;
Steven Rostedt21a96792010-11-08 16:45:50 -0500660 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400661}
662
Steven Rostedt77d942c2011-05-20 13:36:58 -0400663sub set_variable {
664 my ($lvalue, $rvalue) = @_;
665
666 if ($rvalue =~ /^\s*$/) {
667 delete $variable{$lvalue};
668 } else {
669 $rvalue = process_variables($rvalue);
670 $variable{$lvalue} = $rvalue;
671 }
672}
673
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400674sub process_compare {
675 my ($lval, $cmp, $rval) = @_;
676
677 # remove whitespace
678
679 $lval =~ s/^\s*//;
680 $lval =~ s/\s*$//;
681
682 $rval =~ s/^\s*//;
683 $rval =~ s/\s*$//;
684
685 if ($cmp eq "==") {
686 return $lval eq $rval;
687 } elsif ($cmp eq "!=") {
688 return $lval ne $rval;
Steven Rostedt8fddbe92012-07-30 14:37:01 -0400689 } elsif ($cmp eq "=~") {
690 return $lval =~ m/$rval/;
691 } elsif ($cmp eq "!~") {
692 return $lval !~ m/$rval/;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400693 }
694
695 my $statement = "$lval $cmp $rval";
696 my $ret = eval $statement;
697
698 # $@ stores error of eval
699 if ($@) {
700 return -1;
701 }
702
703 return $ret;
704}
705
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400706sub value_defined {
707 my ($val) = @_;
708
709 return defined($variable{$2}) ||
710 defined($opt{$2});
711}
712
Steven Rostedt8d735212011-10-17 11:36:44 -0400713my $d = 0;
714sub process_expression {
715 my ($name, $val) = @_;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400716
Steven Rostedt8d735212011-10-17 11:36:44 -0400717 my $c = $d++;
718
719 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
720 my $express = $1;
721
722 if (process_expression($name, $express)) {
723 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
724 } else {
725 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
726 }
727 }
728
729 $d--;
730 my $OR = "\\|\\|";
731 my $AND = "\\&\\&";
732
733 while ($val =~ s/^(.*?)($OR|$AND)//) {
734 my $express = $1;
735 my $op = $2;
736
737 if (process_expression($name, $express)) {
738 if ($op eq "||") {
739 return 1;
740 }
741 } else {
742 if ($op eq "&&") {
743 return 0;
744 }
745 }
746 }
Steven Rostedt45d73a52011-09-30 19:44:53 -0400747
Steven Rostedt8fddbe92012-07-30 14:37:01 -0400748 if ($val =~ /(.*)(==|\!=|>=|<=|>|<|=~|\!~)(.*)/) {
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400749 my $ret = process_compare($1, $2, $3);
750 if ($ret < 0) {
751 die "$name: $.: Unable to process comparison\n";
752 }
753 return $ret;
754 }
755
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400756 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
757 if (defined $1) {
758 return !value_defined($2);
759 } else {
760 return value_defined($2);
761 }
762 }
763
Steven Rostedt45d73a52011-09-30 19:44:53 -0400764 if ($val =~ /^\s*0\s*$/) {
765 return 0;
766 } elsif ($val =~ /^\s*\d+\s*$/) {
767 return 1;
768 }
769
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400770 die ("$name: $.: Undefined content $val in if statement\n");
Steven Rostedt8d735212011-10-17 11:36:44 -0400771}
772
773sub process_if {
774 my ($name, $value) = @_;
775
776 # Convert variables and replace undefined ones with 0
777 my $val = process_variables($value, 1);
778 my $ret = process_expression $name, $val;
779
780 return $ret;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400781}
782
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400783sub __read_config {
784 my ($config, $current_test_num) = @_;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400785
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400786 my $in;
787 open($in, $config) || die "can't read file $config";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400788
Steven Rostedta57419b2010-11-02 15:13:54 -0400789 my $name = $config;
790 $name =~ s,.*/(.*),$1,;
791
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400792 my $test_num = $$current_test_num;
Steven Rostedta57419b2010-11-02 15:13:54 -0400793 my $default = 1;
794 my $repeat = 1;
795 my $num_tests_set = 0;
796 my $skip = 0;
797 my $rest;
Steven Rostedta9f84422011-10-17 11:06:29 -0400798 my $line;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400799 my $test_case = 0;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400800 my $if = 0;
801 my $if_set = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400802 my $override = 0;
803
804 my %overrides;
Steven Rostedta57419b2010-11-02 15:13:54 -0400805
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400806 while (<$in>) {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400807
808 # ignore blank lines and comments
809 next if (/^\s*$/ || /\s*\#/);
810
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400811 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400812
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400813 my $type = $1;
814 $rest = $2;
Steven Rostedta9f84422011-10-17 11:06:29 -0400815 $line = $2;
Steven Rostedta57419b2010-11-02 15:13:54 -0400816
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400817 my $old_test_num;
818 my $old_repeat;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400819 $override = 0;
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400820
821 if ($type eq "TEST_START") {
822
823 if ($num_tests_set) {
824 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
825 }
826
827 $old_test_num = $test_num;
828 $old_repeat = $repeat;
829
830 $test_num += $repeat;
831 $default = 0;
832 $repeat = 1;
833 } else {
834 $default = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400835 }
836
Steven Rostedta9f84422011-10-17 11:06:29 -0400837 # If SKIP is anywhere in the line, the command will be skipped
838 if ($rest =~ s/\s+SKIP\b//) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400839 $skip = 1;
840 } else {
Steven Rostedt0df213c2011-06-14 20:51:37 -0400841 $test_case = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400842 $skip = 0;
843 }
844
Steven Rostedta9f84422011-10-17 11:06:29 -0400845 if ($rest =~ s/\sELSE\b//) {
846 if (!$if) {
847 die "$name: $.: ELSE found with out matching IF section\n$_";
848 }
849 $if = 0;
850
851 if ($if_set) {
852 $skip = 1;
853 } else {
854 $skip = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400855 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400856 }
857
Steven Rostedta9f84422011-10-17 11:06:29 -0400858 if ($rest =~ s/\sIF\s+(.*)//) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400859 if (process_if($name, $1)) {
860 $if_set = 1;
861 } else {
862 $skip = 1;
863 }
864 $if = 1;
865 } else {
866 $if = 0;
Steven Rostedta9f84422011-10-17 11:06:29 -0400867 $if_set = 0;
Steven Rostedta57419b2010-11-02 15:13:54 -0400868 }
869
Steven Rostedta9f84422011-10-17 11:06:29 -0400870 if (!$skip) {
871 if ($type eq "TEST_START") {
872 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
873 $repeat = $1;
874 $repeat_tests{"$test_num"} = $repeat;
875 }
876 } elsif ($rest =~ s/\sOVERRIDE\b//) {
877 # DEFAULT only
878 $override = 1;
879 # Clear previous overrides
880 %overrides = ();
881 }
882 }
883
884 if (!$skip && $rest !~ /^\s*$/) {
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400885 die "$name: $.: Gargbage found after $type\n$_";
Steven Rostedta57419b2010-11-02 15:13:54 -0400886 }
887
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400888 if ($skip && $type eq "TEST_START") {
Steven Rostedta57419b2010-11-02 15:13:54 -0400889 $test_num = $old_test_num;
Steven Rostedte48c5292010-11-02 14:35:37 -0400890 $repeat = $old_repeat;
Steven Rostedta57419b2010-11-02 15:13:54 -0400891 }
892
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400893 } elsif (/^\s*ELSE\b(.*)$/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400894 if (!$if) {
895 die "$name: $.: ELSE found with out matching IF section\n$_";
896 }
897 $rest = $1;
898 if ($if_set) {
899 $skip = 1;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400900 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400901 } else {
902 $skip = 0;
903
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400904 if ($rest =~ /\sIF\s+(.*)/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400905 # May be a ELSE IF section.
Steven Rostedt95f57832012-09-26 14:48:17 -0400906 if (process_if($name, $1)) {
907 $if_set = 1;
908 } else {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400909 $skip = 1;
910 }
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400911 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400912 } else {
913 $if = 0;
914 }
915 }
916
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400917 if ($rest !~ /^\s*$/) {
918 die "$name: $.: Gargbage found after DEFAULTS\n$_";
919 }
920
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400921 } elsif (/^\s*INCLUDE\s+(\S+)/) {
922
923 next if ($skip);
924
925 if (!$default) {
926 die "$name: $.: INCLUDE can only be done in default sections\n$_";
927 }
928
929 my $file = process_variables($1);
930
931 if ($file !~ m,^/,) {
932 # check the path of the config file first
933 if ($config =~ m,(.*)/,) {
934 if (-f "$1/$file") {
935 $file = "$1/$file";
936 }
937 }
938 }
939
940 if ( ! -r $file ) {
941 die "$name: $.: Can't read file $file\n$_";
942 }
943
944 if (__read_config($file, \$test_num)) {
945 $test_case = 1;
946 }
947
Steven Rostedta57419b2010-11-02 15:13:54 -0400948 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
949
950 next if ($skip);
951
Steven Rostedt2545eb62010-11-02 15:01:32 -0400952 my $lvalue = $1;
953 my $rvalue = $2;
954
Steven Rostedta57419b2010-11-02 15:13:54 -0400955 if (!$default &&
956 ($lvalue eq "NUM_TESTS" ||
957 $lvalue eq "LOG_FILE" ||
958 $lvalue eq "CLEAR_LOG")) {
959 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400960 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400961
962 if ($lvalue eq "NUM_TESTS") {
963 if ($test_num) {
964 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
965 }
966 if (!$default) {
967 die "$name: $.: NUM_TESTS must be set in default section\n";
968 }
969 $num_tests_set = 1;
970 }
971
972 if ($default || $lvalue =~ /\[\d+\]$/) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400973 set_value($lvalue, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400974 } else {
975 my $val = "$lvalue\[$test_num\]";
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400976 set_value($val, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400977
978 if ($repeat > 1) {
979 $repeats{$val} = $repeat;
980 }
981 }
Steven Rostedt77d942c2011-05-20 13:36:58 -0400982 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
983 next if ($skip);
984
985 my $lvalue = $1;
986 my $rvalue = $2;
987
988 # process config variables.
989 # Config variables are only active while reading the
990 # config and can be defined anywhere. They also ignore
991 # TEST_START and DEFAULTS, but are skipped if they are in
992 # on of these sections that have SKIP defined.
993 # The save variable can be
994 # defined multiple times and the new one simply overrides
995 # the prevous one.
996 set_variable($lvalue, $rvalue);
997
Steven Rostedta57419b2010-11-02 15:13:54 -0400998 } else {
999 die "$name: $.: Garbage found in config\n$_";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001000 }
1001 }
1002
Steven Rostedta57419b2010-11-02 15:13:54 -04001003 if ($test_num) {
1004 $test_num += $repeat - 1;
1005 $opt{"NUM_TESTS"} = $test_num;
1006 }
1007
Steven Rostedt2ed3b162011-09-30 21:00:00 -04001008 close($in);
1009
1010 $$current_test_num = $test_num;
1011
1012 return $test_case;
1013}
1014
Steven Rostedtc4261d02011-11-23 13:41:18 -05001015sub get_test_case {
1016 print "What test case would you like to run?\n";
1017 print " (build, install or boot)\n";
1018 print " Other tests are available but require editing the config file\n";
1019 my $ans = <STDIN>;
1020 chomp $ans;
1021 $default{"TEST_TYPE"} = $ans;
1022}
1023
Steven Rostedt2ed3b162011-09-30 21:00:00 -04001024sub read_config {
1025 my ($config) = @_;
1026
1027 my $test_case;
1028 my $test_num = 0;
1029
1030 $test_case = __read_config $config, \$test_num;
1031
Steven Rostedt8d1491b2010-11-18 15:39:48 -05001032 # make sure we have all mandatory configs
1033 get_ktest_configs;
1034
Steven Rostedt0df213c2011-06-14 20:51:37 -04001035 # was a test specified?
1036 if (!$test_case) {
1037 print "No test case specified.\n";
Steven Rostedtc4261d02011-11-23 13:41:18 -05001038 get_test_case;
Steven Rostedt0df213c2011-06-14 20:51:37 -04001039 }
1040
Steven Rostedta75fece2010-11-02 14:58:27 -04001041 # set any defaults
1042
1043 foreach my $default (keys %default) {
1044 if (!defined($opt{$default})) {
1045 $opt{$default} = $default{$default};
1046 }
1047 }
Steven Rostedt9cc9e092011-12-22 21:37:22 -05001048
1049 if ($opt{"IGNORE_UNUSED"} == 1) {
1050 return;
1051 }
1052
1053 my %not_used;
1054
1055 # check if there are any stragglers (typos?)
1056 foreach my $option (keys %opt) {
1057 my $op = $option;
1058 # remove per test labels.
1059 $op =~ s/\[.*\]//;
1060 if (!exists($option_map{$op}) &&
1061 !exists($default{$op}) &&
1062 !exists($used_options{$op})) {
1063 $not_used{$op} = 1;
1064 }
1065 }
1066
1067 if (%not_used) {
1068 my $s = "s are";
1069 $s = " is" if (keys %not_used == 1);
1070 print "The following option$s not used; could be a typo:\n";
1071 foreach my $option (keys %not_used) {
1072 print "$option\n";
1073 }
1074 print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
1075 if (!read_yn "Do you want to continue?") {
1076 exit -1;
1077 }
1078 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001079}
1080
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001081sub __eval_option {
Steven Rostedt (Red Hat)04262be2013-01-31 10:12:20 -05001082 my ($name, $option, $i) = @_;
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001083
1084 # Add space to evaluate the character before $
1085 $option = " $option";
1086 my $retval = "";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301087 my $repeated = 0;
1088 my $parent = 0;
1089
1090 foreach my $test (keys %repeat_tests) {
1091 if ($i >= $test &&
1092 $i < $test + $repeat_tests{$test}) {
1093
1094 $repeated = 1;
1095 $parent = $test;
1096 last;
1097 }
1098 }
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001099
1100 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
1101 my $start = $1;
1102 my $var = $2;
1103 my $end = $3;
1104
1105 # Append beginning of line
1106 $retval = "$retval$start";
1107
1108 # If the iteration option OPT[$i] exists, then use that.
1109 # otherwise see if the default OPT (without [$i]) exists.
1110
1111 my $o = "$var\[$i\]";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301112 my $parento = "$var\[$parent\]";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001113
Steven Rostedt (Red Hat)04262be2013-01-31 10:12:20 -05001114 # If a variable contains itself, use the default var
1115 if (($var eq $name) && defined($opt{$var})) {
1116 $o = $opt{$var};
1117 $retval = "$retval$o";
1118 } elsif (defined($opt{$o})) {
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001119 $o = $opt{$o};
1120 $retval = "$retval$o";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301121 } elsif ($repeated && defined($opt{$parento})) {
1122 $o = $opt{$parento};
1123 $retval = "$retval$o";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001124 } elsif (defined($opt{$var})) {
1125 $o = $opt{$var};
1126 $retval = "$retval$o";
1127 } else {
1128 $retval = "$retval\$\{$var\}";
1129 }
1130
1131 $option = $end;
1132 }
1133
1134 $retval = "$retval$option";
1135
1136 $retval =~ s/^ //;
1137
1138 return $retval;
1139}
1140
1141sub eval_option {
Steven Rostedt (Red Hat)04262be2013-01-31 10:12:20 -05001142 my ($name, $option, $i) = @_;
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001143
1144 my $prev = "";
1145
1146 # Since an option can evaluate to another option,
1147 # keep iterating until we do not evaluate any more
1148 # options.
1149 my $r = 0;
1150 while ($prev ne $option) {
1151 # Check for recursive evaluations.
1152 # 100 deep should be more than enough.
1153 if ($r++ > 100) {
1154 die "Over 100 evaluations accurred with $option\n" .
1155 "Check for recursive variables\n";
1156 }
1157 $prev = $option;
Steven Rostedt (Red Hat)04262be2013-01-31 10:12:20 -05001158 $option = __eval_option($name, $option, $i);
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001159 }
1160
1161 return $option;
1162}
1163
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001164sub _logit {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001165 if (defined($opt{"LOG_FILE"})) {
1166 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
1167 print OUT @_;
1168 close(OUT);
1169 }
1170}
1171
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001172sub logit {
1173 if (defined($opt{"LOG_FILE"})) {
1174 _logit @_;
1175 } else {
1176 print @_;
1177 }
1178}
1179
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001180sub doprint {
1181 print @_;
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001182 _logit @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001183}
1184
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001185sub run_command;
Andrew Jones2728be42011-08-12 15:32:05 +02001186sub start_monitor;
1187sub end_monitor;
1188sub wait_for_monitor;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001189
1190sub reboot {
Andrew Jones2728be42011-08-12 15:32:05 +02001191 my ($time) = @_;
1192
Steven Rostedta4968722012-12-11 14:59:05 -05001193 # Make sure everything has been written to disk
1194 run_ssh("sync");
1195
Steven Rostedt2b803362011-09-30 18:00:23 -04001196 if (defined($time)) {
1197 start_monitor;
1198 # flush out current monitor
1199 # May contain the reboot success line
1200 wait_for_monitor 1;
1201 }
1202
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001203 # try to reboot normally
Steven Rostedte48c5292010-11-02 14:35:37 -04001204 if (run_command $reboot) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001205 if (defined($powercycle_after_reboot)) {
1206 sleep $powercycle_after_reboot;
1207 run_command "$power_cycle";
1208 }
1209 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001210 # nope? power cycle it.
Steven Rostedta75fece2010-11-02 14:58:27 -04001211 run_command "$power_cycle";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001212 }
Andrew Jones2728be42011-08-12 15:32:05 +02001213
1214 if (defined($time)) {
Steven Rostedt (Red Hat)d6845532013-02-04 23:08:49 -05001215 # Look for the good kernel to boot
1216 if (wait_for_monitor($time, "Linux version")) {
Steven Rostedt407b95b2012-07-19 16:05:42 -04001217 # reboot got stuck?
Steven Rostedt8a80c722012-07-19 16:08:33 -04001218 doprint "Reboot did not finish. Forcing power cycle\n";
Steven Rostedt407b95b2012-07-19 16:05:42 -04001219 run_command "$power_cycle";
1220 }
Steven Rostedt (Red Hat)d6845532013-02-04 23:08:49 -05001221
1222 # Still need to wait for the reboot to finish
1223 wait_for_monitor($time, $reboot_success_line);
1224
Andrew Jones2728be42011-08-12 15:32:05 +02001225 end_monitor;
1226 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001227}
1228
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001229sub reboot_to_good {
1230 my ($time) = @_;
1231
1232 if (defined($switch_to_good)) {
1233 run_command $switch_to_good;
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001234 }
1235
1236 reboot $time;
1237}
1238
Steven Rostedt576f6272010-11-02 14:58:38 -04001239sub do_not_reboot {
1240 my $i = $iteration;
1241
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001242 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -04001243 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1244 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1245}
1246
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001247sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001248 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001249
Steven Rostedt576f6272010-11-02 14:58:38 -04001250 my $i = $iteration;
1251
1252 if ($reboot_on_error && !do_not_reboot) {
1253
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001254 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001255 reboot_to_good;
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001256
Steven Rostedta75fece2010-11-02 14:58:27 -04001257 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001258 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001259 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001260 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001261
Steven Rostedtf80802c2011-03-07 13:18:47 -05001262 if (defined($opt{"LOG_FILE"})) {
1263 print " See $opt{LOG_FILE} for more info.\n";
1264 }
1265
Steven Rostedt576f6272010-11-02 14:58:38 -04001266 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001267}
1268
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001269sub open_console {
1270 my ($fp) = @_;
1271
1272 my $flags;
1273
Steven Rostedta75fece2010-11-02 14:58:27 -04001274 my $pid = open($fp, "$console|") or
1275 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001276
1277 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001278 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001279 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001280 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001281
1282 return $pid;
1283}
1284
1285sub close_console {
1286 my ($fp, $pid) = @_;
1287
1288 doprint "kill child process $pid\n";
1289 kill 2, $pid;
1290
1291 print "closing!\n";
1292 close($fp);
1293}
1294
1295sub start_monitor {
1296 if ($monitor_cnt++) {
1297 return;
1298 }
1299 $monitor_fp = \*MONFD;
1300 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001301
1302 return;
1303
1304 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001305}
1306
1307sub end_monitor {
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001308 return if (!defined $console);
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001309 if (--$monitor_cnt) {
1310 return;
1311 }
1312 close_console($monitor_fp, $monitor_pid);
1313}
1314
1315sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001316 my ($time, $stop) = @_;
1317 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001318 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001319 my $booted = 0;
Steven Rostedt407b95b2012-07-19 16:05:42 -04001320 my $start_time = time;
Steven Rostedt8a80c722012-07-19 16:08:33 -04001321 my $skip_call_trace = 0;
1322 my $bug = 0;
1323 my $bug_ignored = 0;
Steven Rostedt407b95b2012-07-19 16:05:42 -04001324 my $now;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001325
Steven Rostedta75fece2010-11-02 14:58:27 -04001326 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001327
1328 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001329 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001330 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001331 last if (!defined($line));
1332 print "$line";
1333 $full_line .= $line;
1334
1335 if (defined($stop) && $full_line =~ /$stop/) {
1336 doprint "wait for monitor detected $stop\n";
1337 $booted = 1;
1338 }
1339
Steven Rostedt8a80c722012-07-19 16:08:33 -04001340 if ($full_line =~ /\[ backtrace testing \]/) {
1341 $skip_call_trace = 1;
1342 }
1343
1344 if ($full_line =~ /call trace:/i) {
1345 if (!$bug && !$skip_call_trace) {
1346 if ($ignore_errors) {
1347 $bug_ignored = 1;
1348 } else {
1349 $bug = 1;
1350 }
1351 }
1352 }
1353
1354 if ($full_line =~ /\[ end of backtrace testing \]/) {
1355 $skip_call_trace = 0;
1356 }
1357
1358 if ($full_line =~ /Kernel panic -/) {
1359 $bug = 1;
1360 }
1361
Steven Rostedt2b803362011-09-30 18:00:23 -04001362 if ($line =~ /\n/) {
1363 $full_line = "";
1364 }
Steven Rostedt407b95b2012-07-19 16:05:42 -04001365 $now = time;
1366 if ($now - $start_time >= $max_monitor_wait) {
1367 doprint "Exiting monitor flush due to hitting MAX_MONITOR_WAIT\n";
1368 return 1;
1369 }
Steven Rostedt2b803362011-09-30 18:00:23 -04001370 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001371 print "** Monitor flushed **\n";
Steven Rostedt8a80c722012-07-19 16:08:33 -04001372 return $bug;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001373}
1374
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301375sub save_logs {
1376 my ($result, $basedir) = @_;
1377 my @t = localtime;
1378 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1379 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1380
1381 my $type = $build_type;
1382 if ($type =~ /useconfig/) {
1383 $type = "useconfig";
1384 }
1385
1386 my $dir = "$machine-$test_type-$type-$result-$date";
1387
1388 $dir = "$basedir/$dir";
1389
1390 if (!-d $dir) {
1391 mkpath($dir) or
1392 die "can't create $dir";
1393 }
1394
1395 my %files = (
1396 "config" => $output_config,
1397 "buildlog" => $buildlog,
1398 "dmesg" => $dmesg,
1399 "testlog" => $testlog,
1400 );
1401
1402 while (my ($name, $source) = each(%files)) {
1403 if (-f "$source") {
1404 cp "$source", "$dir/$name" or
1405 die "failed to copy $source";
1406 }
1407 }
1408
1409 doprint "*** Saved info to $dir ***\n";
1410}
1411
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001412sub fail {
1413
Steven Rostedt921ed4c2012-07-19 15:18:27 -04001414 if (defined($post_test)) {
1415 run_command $post_test;
1416 }
1417
Steven Rostedta75fece2010-11-02 14:58:27 -04001418 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001419 dodie @_;
1420 }
1421
Steven Rostedta75fece2010-11-02 14:58:27 -04001422 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001423
Steven Rostedt576f6272010-11-02 14:58:38 -04001424 my $i = $iteration;
1425
Steven Rostedta75fece2010-11-02 14:58:27 -04001426 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001427 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001428 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001429 reboot_to_good $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001430 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001431
Steven Rostedt9064af52011-06-13 10:38:48 -04001432 my $name = "";
1433
1434 if (defined($test_name)) {
1435 $name = " ($test_name)";
1436 }
1437
Steven Rostedt576f6272010-11-02 14:58:38 -04001438 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1439 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001440 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001441 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1442 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001443
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301444 if (defined($store_failures)) {
1445 save_logs "fail", $store_failures;
1446 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001447
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001448 return 1;
1449}
1450
Steven Rostedt2545eb62010-11-02 15:01:32 -04001451sub run_command {
1452 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001453 my $dolog = 0;
1454 my $dord = 0;
1455 my $pid;
1456
Steven Rostedte48c5292010-11-02 14:35:37 -04001457 $command =~ s/\$SSH_USER/$ssh_user/g;
1458 $command =~ s/\$MACHINE/$machine/g;
1459
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001460 doprint("$command ... ");
1461
1462 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001463 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001464
1465 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001466 open(LOG, ">>$opt{LOG_FILE}") or
1467 dodie "failed to write to log";
1468 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001469 }
1470
1471 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001472 open (RD, ">$redirect") or
1473 dodie "failed to write to redirect $redirect";
1474 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001475 }
1476
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001477 while (<CMD>) {
1478 print LOG if ($dolog);
1479 print RD if ($dord);
1480 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001481
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001482 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001483 my $failed = $?;
1484
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001485 close(CMD);
1486 close(LOG) if ($dolog);
1487 close(RD) if ($dord);
1488
Steven Rostedt2545eb62010-11-02 15:01:32 -04001489 if ($failed) {
1490 doprint "FAILED!\n";
1491 } else {
1492 doprint "SUCCESS\n";
1493 }
1494
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001495 return !$failed;
1496}
1497
Steven Rostedte48c5292010-11-02 14:35:37 -04001498sub run_ssh {
1499 my ($cmd) = @_;
1500 my $cp_exec = $ssh_exec;
1501
1502 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1503 return run_command "$cp_exec";
1504}
1505
1506sub run_scp {
Steven Rostedt02ad2612012-03-21 08:21:24 -04001507 my ($src, $dst, $cp_scp) = @_;
Steven Rostedte48c5292010-11-02 14:35:37 -04001508
1509 $cp_scp =~ s/\$SRC_FILE/$src/g;
1510 $cp_scp =~ s/\$DST_FILE/$dst/g;
1511
1512 return run_command "$cp_scp";
1513}
1514
Steven Rostedt02ad2612012-03-21 08:21:24 -04001515sub run_scp_install {
1516 my ($src, $dst) = @_;
1517
1518 my $cp_scp = $scp_to_target_install;
1519
1520 return run_scp($src, $dst, $cp_scp);
1521}
1522
1523sub run_scp_mod {
1524 my ($src, $dst) = @_;
1525
1526 my $cp_scp = $scp_to_target;
1527
1528 return run_scp($src, $dst, $cp_scp);
1529}
1530
Steven Rostedta15ba912012-11-13 14:30:37 -05001531sub get_grub2_index {
1532
1533 return if (defined($grub_number));
1534
1535 doprint "Find grub2 menu ... ";
1536 $grub_number = -1;
1537
1538 my $ssh_grub = $ssh_exec;
1539 $ssh_grub =~ s,\$SSH_COMMAND,cat $grub_file,g;
1540
1541 open(IN, "$ssh_grub |")
1542 or die "unable to get $grub_file";
1543
1544 my $found = 0;
1545
1546 while (<IN>) {
1547 if (/^menuentry.*$grub_menu/) {
1548 $grub_number++;
1549 $found = 1;
1550 last;
1551 } elsif (/^menuentry\s/) {
1552 $grub_number++;
1553 }
1554 }
1555 close(IN);
1556
1557 die "Could not find '$grub_menu' in $grub_file on $machine"
1558 if (!$found);
1559 doprint "$grub_number\n";
1560}
1561
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001562sub get_grub_index {
1563
Steven Rostedta15ba912012-11-13 14:30:37 -05001564 if ($reboot_type eq "grub2") {
1565 get_grub2_index;
1566 return;
1567 }
1568
Steven Rostedta75fece2010-11-02 14:58:27 -04001569 if ($reboot_type ne "grub") {
1570 return;
1571 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001572 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001573
1574 doprint "Find grub menu ... ";
1575 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001576
1577 my $ssh_grub = $ssh_exec;
1578 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1579
1580 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001581 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001582
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001583 my $found = 0;
1584
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001585 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001586 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001587 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001588 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001589 last;
1590 } elsif (/^\s*title\s/) {
1591 $grub_number++;
1592 }
1593 }
1594 close(IN);
1595
Steven Rostedta75fece2010-11-02 14:58:27 -04001596 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001597 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001598 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001599}
1600
Steven Rostedt2545eb62010-11-02 15:01:32 -04001601sub wait_for_input
1602{
1603 my ($fp, $time) = @_;
1604 my $rin;
1605 my $ready;
1606 my $line;
1607 my $ch;
1608
1609 if (!defined($time)) {
1610 $time = $timeout;
1611 }
1612
1613 $rin = '';
1614 vec($rin, fileno($fp), 1) = 1;
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001615 ($ready, $time) = select($rin, undef, undef, $time);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001616
1617 $line = "";
1618
1619 # try to read one char at a time
1620 while (sysread $fp, $ch, 1) {
1621 $line .= $ch;
1622 last if ($ch eq "\n");
1623 }
1624
1625 if (!length($line)) {
1626 return undef;
1627 }
1628
1629 return $line;
1630}
1631
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001632sub reboot_to {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001633 if (defined($switch_to_test)) {
1634 run_command $switch_to_test;
1635 }
1636
Steven Rostedta75fece2010-11-02 14:58:27 -04001637 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001638 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
Steven Rostedta15ba912012-11-13 14:30:37 -05001639 } elsif ($reboot_type eq "grub2") {
1640 run_ssh "$grub_reboot $grub_number";
Steven Rostedt77869542012-12-11 17:37:41 -05001641 } elsif ($reboot_type eq "syslinux") {
1642 run_ssh "$syslinux --once \\\"$syslinux_label\\\" $syslinux_path";
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001643 } elsif (defined $reboot_script) {
1644 run_command "$reboot_script";
Steven Rostedta75fece2010-11-02 14:58:27 -04001645 }
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001646 reboot;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001647}
1648
Steven Rostedta57419b2010-11-02 15:13:54 -04001649sub get_sha1 {
1650 my ($commit) = @_;
1651
1652 doprint "git rev-list --max-count=1 $commit ... ";
1653 my $sha1 = `git rev-list --max-count=1 $commit`;
1654 my $ret = $?;
1655
1656 logit $sha1;
1657
1658 if ($ret) {
1659 doprint "FAILED\n";
1660 dodie "Failed to get git $commit";
1661 }
1662
1663 print "SUCCESS\n";
1664
1665 chomp $sha1;
1666
1667 return $sha1;
1668}
1669
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001670sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001671 my $booted = 0;
1672 my $bug = 0;
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001673 my $bug_ignored = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001674 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001675 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001676
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001677 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001678
1679 my $line;
1680 my $full_line = "";
1681
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001682 open(DMESG, "> $dmesg") or
1683 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001684
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001685 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001686
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001687 my $success_start;
1688 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001689 my $monitor_start = time;
1690 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001691 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001692
Steven Rostedt2d01b262011-03-08 09:47:54 -05001693 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001694
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001695 if ($bug && defined($stop_after_failure) &&
1696 $stop_after_failure >= 0) {
1697 my $time = $stop_after_failure - (time - $failure_start);
1698 $line = wait_for_input($monitor_fp, $time);
1699 if (!defined($line)) {
1700 doprint "bug timed out after $booted_timeout seconds\n";
1701 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1702 last;
1703 }
1704 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001705 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001706 if (!defined($line)) {
1707 my $s = $booted_timeout == 1 ? "" : "s";
1708 doprint "Successful boot found: break after $booted_timeout second$s\n";
1709 last;
1710 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001711 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001712 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001713 if (!defined($line)) {
1714 my $s = $timeout == 1 ? "" : "s";
1715 doprint "Timed out after $timeout second$s\n";
1716 last;
1717 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001718 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001719
Steven Rostedt2545eb62010-11-02 15:01:32 -04001720 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001721 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001722
1723 # we are not guaranteed to get a full line
1724 $full_line .= $line;
1725
Steven Rostedta75fece2010-11-02 14:58:27 -04001726 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001727 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001728 $success_start = time;
1729 }
1730
1731 if ($booted && defined($stop_after_success) &&
1732 $stop_after_success >= 0) {
1733 my $now = time;
1734 if ($now - $success_start >= $stop_after_success) {
1735 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1736 last;
1737 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001738 }
1739
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001740 if ($full_line =~ /\[ backtrace testing \]/) {
1741 $skip_call_trace = 1;
1742 }
1743
Steven Rostedt2545eb62010-11-02 15:01:32 -04001744 if ($full_line =~ /call trace:/i) {
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001745 if (!$bug && !$skip_call_trace) {
1746 if ($ignore_errors) {
1747 $bug_ignored = 1;
1748 } else {
1749 $bug = 1;
1750 $failure_start = time;
1751 }
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001752 }
1753 }
1754
1755 if ($bug && defined($stop_after_failure) &&
1756 $stop_after_failure >= 0) {
1757 my $now = time;
1758 if ($now - $failure_start >= $stop_after_failure) {
1759 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1760 last;
1761 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001762 }
1763
1764 if ($full_line =~ /\[ end of backtrace testing \]/) {
1765 $skip_call_trace = 0;
1766 }
1767
1768 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001769 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001770 $bug = 1;
1771 }
1772
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001773 # Detect triple faults by testing the banner
1774 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1775 if ($1 eq $version) {
1776 $version_found = 1;
1777 } elsif ($version_found && $detect_triplefault) {
1778 # We already booted into the kernel we are testing,
1779 # but now we booted into another kernel?
1780 # Consider this a triple fault.
1781 doprint "Aleady booted in Linux kernel $version, but now\n";
1782 doprint "we booted into Linux kernel $1.\n";
1783 doprint "Assuming that this is a triple fault.\n";
1784 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1785 last;
1786 }
1787 }
1788
Steven Rostedt2545eb62010-11-02 15:01:32 -04001789 if ($line =~ /\n/) {
1790 $full_line = "";
1791 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001792
1793 if ($stop_test_after > 0 && !$booted && !$bug) {
1794 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001795 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001796 $done = 1;
1797 }
1798 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001799 }
1800
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001801 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001802
Steven Rostedt2545eb62010-11-02 15:01:32 -04001803 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001804 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001805 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001806 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001807
Steven Rostedta75fece2010-11-02 14:58:27 -04001808 if (!$booted) {
1809 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001810 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001811 }
1812
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001813 if ($bug_ignored) {
1814 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
1815 }
1816
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001817 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001818}
1819
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001820sub eval_kernel_version {
1821 my ($option) = @_;
1822
1823 $option =~ s/\$KERNEL_VERSION/$version/g;
1824
1825 return $option;
1826}
1827
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001828sub do_post_install {
1829
1830 return if (!defined($post_install));
1831
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001832 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001833 run_command "$cp_post_install" or
1834 dodie "Failed to run post install";
1835}
1836
Steven Rostedte1a6c3d2012-12-11 21:19:41 -05001837# Sometimes the reboot fails, and will hang. We try to ssh to the box
1838# and if we fail, we force another reboot, that should powercycle it.
1839sub test_booted {
1840 if (!run_ssh "echo testing connection") {
1841 reboot $sleep_time;
1842 }
1843}
1844
Steven Rostedt2545eb62010-11-02 15:01:32 -04001845sub install {
1846
Steven Rostedte0a87422011-09-30 17:50:48 -04001847 return if ($no_install);
1848
Steven Rostedte5c2ec12012-07-19 15:22:05 -04001849 if (defined($pre_install)) {
1850 my $cp_pre_install = eval_kernel_version $pre_install;
1851 run_command "$cp_pre_install" or
1852 dodie "Failed to run pre install";
1853 }
1854
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001855 my $cp_target = eval_kernel_version $target_image;
1856
Steven Rostedte1a6c3d2012-12-11 21:19:41 -05001857 test_booted;
1858
Steven Rostedt02ad2612012-03-21 08:21:24 -04001859 run_scp_install "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001860 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001861
1862 my $install_mods = 0;
1863
1864 # should we process modules?
1865 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001866 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001867 while (<IN>) {
1868 if (/CONFIG_MODULES(=y)?/) {
Steven Rostedt8bc5e4e2012-10-26 00:10:32 -04001869 if (defined($1)) {
1870 $install_mods = 1;
1871 last;
1872 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001873 }
1874 }
1875 close(IN);
1876
1877 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001878 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001879 doprint "No modules needed\n";
1880 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001881 }
1882
Steven Rostedt627977d2012-03-21 08:16:15 -04001883 run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001884 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001885
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001886 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001887 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001888
Steven Rostedte48c5292010-11-02 14:35:37 -04001889 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001890 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001891
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001892 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001893 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001894 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001895
Steven Rostedt02ad2612012-03-21 08:21:24 -04001896 run_scp_mod "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001897 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001898
Steven Rostedta75fece2010-11-02 14:58:27 -04001899 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001900
Steven Rostedte7b13442011-06-14 20:44:36 -04001901 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001902 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001903
Steven Rostedte48c5292010-11-02 14:35:37 -04001904 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001905
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001906 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001907}
1908
Steven Rostedtddf607e2011-06-14 20:49:13 -04001909sub get_version {
1910 # get the release name
Steven Rostedt683a3e62012-05-18 13:34:35 -04001911 return if ($have_version);
Steven Rostedtddf607e2011-06-14 20:49:13 -04001912 doprint "$make kernelrelease ... ";
1913 $version = `$make kernelrelease | tail -1`;
1914 chomp($version);
1915 doprint "$version\n";
Steven Rostedt683a3e62012-05-18 13:34:35 -04001916 $have_version = 1;
Steven Rostedtddf607e2011-06-14 20:49:13 -04001917}
1918
1919sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001920 # Make sure the stable kernel has finished booting
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001921
1922 # Install bisects, don't need console
1923 if (defined $console) {
1924 start_monitor;
1925 wait_for_monitor 5;
1926 end_monitor;
1927 }
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001928
Steven Rostedtddf607e2011-06-14 20:49:13 -04001929 get_grub_index;
1930 get_version;
1931 install;
1932
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001933 start_monitor if (defined $console);
Steven Rostedtddf607e2011-06-14 20:49:13 -04001934 return monitor;
1935}
1936
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05001937my $check_build_re = ".*:.*(warning|error|Error):.*";
1938my $utf8_quote = "\\x{e2}\\x{80}(\\x{98}|\\x{99})";
1939
1940# Read buildlog and check against warnings file for any
1941# new warnings.
1942#
1943# Returns 1 if OK
1944# 0 otherwise
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001945sub check_buildlog {
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05001946 return 1 if (!defined $warnings_file);
1947
1948 my %warnings_list;
1949
1950 # Failed builds should not reboot the target
1951 my $save_no_reboot = $no_reboot;
1952 $no_reboot = 1;
1953
1954 if (-f $warnings_file) {
1955 open(IN, $warnings_file) or
1956 dodie "Error opening $warnings_file";
1957
1958 while (<IN>) {
1959 if (/$check_build_re/) {
1960 chomp;
1961 $warnings_list{$_} = 1;
1962 }
1963 }
1964 close(IN);
1965 }
1966
1967 # If warnings file didn't exist, and WARNINGS_FILE exist,
1968 # then we fail on any warning!
1969
1970 open(IN, $buildlog) or dodie "Can't open $buildlog";
1971 while (<IN>) {
1972 if (/$check_build_re/) {
1973
1974 # Some compilers use UTF-8 extended for quotes
1975 # for distcc heterogeneous systems, this causes issues
1976 s/$utf8_quote/'/g;
1977
1978 chomp;
1979 if (!defined $warnings_list{$_}) {
1980 fail "New warning found (not in $warnings_file)\n$_\n";
1981 $no_reboot = $save_no_reboot;
1982 return 0;
1983 }
1984 }
1985 }
1986 $no_reboot = $save_no_reboot;
1987 close(IN);
1988}
1989
1990sub check_patch_buildlog {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001991 my ($patch) = @_;
1992
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001993 my @files = `git show $patch | diffstat -l`;
1994
Steven Rostedt (Red Hat)35275682013-01-30 12:28:15 -05001995 foreach my $file (@files) {
1996 chomp $file;
1997 }
1998
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001999 open(IN, "git show $patch |") or
2000 dodie "failed to show $patch";
2001 while (<IN>) {
2002 if (m,^--- a/(.*),) {
2003 chomp $1;
2004 $files[$#files] = $1;
2005 }
2006 }
2007 close(IN);
2008
2009 open(IN, $buildlog) or dodie "Can't open $buildlog";
2010 while (<IN>) {
2011 if (/^\s*(.*?):.*(warning|error)/) {
2012 my $err = $1;
2013 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002014 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002015 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002016 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002017 }
2018 }
2019 }
2020 }
2021 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002022
2023 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002024}
2025
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002026sub apply_min_config {
2027 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05002028
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002029 # Read the config file and remove anything that
2030 # is in the force_config hash (from minconfig and others)
2031 # then add the force config back.
2032
2033 doprint "Applying minimum configurations into $output_config.new\n";
2034
2035 open (OUT, ">$outconfig") or
2036 dodie "Can't create $outconfig";
2037
2038 if (-f $output_config) {
2039 open (IN, $output_config) or
2040 dodie "Failed to open $output_config";
2041 while (<IN>) {
2042 if (/^(# )?(CONFIG_[^\s=]*)/) {
2043 next if (defined($force_config{$2}));
2044 }
2045 print OUT;
2046 }
2047 close IN;
2048 }
2049 foreach my $config (keys %force_config) {
2050 print OUT "$force_config{$config}\n";
2051 }
2052 close OUT;
2053
2054 run_command "mv $outconfig $output_config";
2055}
2056
2057sub make_oldconfig {
2058
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002059 my @force_list = keys %force_config;
2060
2061 if ($#force_list >= 0) {
2062 apply_min_config;
2063 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002064
Adam Leefb16d892012-09-01 01:05:17 +08002065 if (!run_command "$make olddefconfig") {
2066 # Perhaps olddefconfig doesn't exist in this version of the kernel
Steven Rostedt18925172012-12-11 20:16:03 -05002067 # try oldnoconfig
2068 doprint "olddefconfig failed, trying make oldnoconfig\n";
2069 if (!run_command "$make oldnoconfig") {
2070 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
2071 # try a yes '' | oldconfig
2072 run_command "yes '' | $make oldconfig" or
2073 dodie "failed make config oldconfig";
2074 }
Steven Rostedt612b9e92011-03-07 13:27:43 -05002075 }
2076}
2077
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002078# read a config file and use this to force new configs.
2079sub load_force_config {
2080 my ($config) = @_;
2081
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002082 doprint "Loading force configs from $config\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002083 open(IN, $config) or
2084 dodie "failed to read $config";
2085 while (<IN>) {
2086 chomp;
2087 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
2088 $force_config{$1} = $_;
2089 } elsif (/^# (CONFIG_\S*) is not set/) {
2090 $force_config{$1} = $_;
2091 }
2092 }
2093 close IN;
2094}
2095
Steven Rostedt2545eb62010-11-02 15:01:32 -04002096sub build {
2097 my ($type) = @_;
2098
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002099 unlink $buildlog;
2100
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04002101 # Failed builds should not reboot the target
2102 my $save_no_reboot = $no_reboot;
2103 $no_reboot = 1;
2104
Steven Rostedt683a3e62012-05-18 13:34:35 -04002105 # Calculate a new version from here.
2106 $have_version = 0;
2107
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04002108 if (defined($pre_build)) {
2109 my $ret = run_command $pre_build;
2110 if (!$ret && defined($pre_build_die) &&
2111 $pre_build_die) {
2112 dodie "failed to pre_build\n";
2113 }
2114 }
2115
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002116 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05002117 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002118 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002119
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002120 $type = "oldconfig";
2121 }
2122
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002123 # old config can ask questions
2124 if ($type eq "oldconfig") {
Adam Leefb16d892012-09-01 01:05:17 +08002125 $type = "olddefconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002126
2127 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05002128 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002129
Andrew Jones13488232011-08-12 15:32:04 +02002130 if (!$noclean) {
2131 run_command "mv $output_config $outputdir/config_temp" or
2132 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04002133
Andrew Jones13488232011-08-12 15:32:04 +02002134 run_command "$make mrproper" or dodie "make mrproper";
2135
2136 run_command "mv $outputdir/config_temp $output_config" or
2137 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002138 }
2139
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002140 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05002141 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002142 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002143 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002144 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04002145
2146 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04002147 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
2148 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04002149 close(OUT);
2150
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002151 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002152 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04002153 }
2154
Adam Leefb16d892012-09-01 01:05:17 +08002155 if ($type ne "olddefconfig") {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002156 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05002157 dodie "failed make config";
2158 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002159 # Run old config regardless, to enforce min configurations
2160 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002161
Steven Rostedta75fece2010-11-02 14:58:27 -04002162 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04002163 my $build_ret = run_command "$make $build_options";
2164 undef $redirect;
2165
2166 if (defined($post_build)) {
Steven Rostedt683a3e62012-05-18 13:34:35 -04002167 # Because a post build may change the kernel version
2168 # do it now.
2169 get_version;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04002170 my $ret = run_command $post_build;
2171 if (!$ret && defined($post_build_die) &&
2172 $post_build_die) {
2173 dodie "failed to post_build\n";
2174 }
2175 }
2176
2177 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002178 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04002179 if ($in_bisect) {
2180 $no_reboot = $save_no_reboot;
2181 return 0;
2182 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002183 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002184 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002185
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04002186 $no_reboot = $save_no_reboot;
2187
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002188 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002189}
2190
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002191sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04002192 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04002193 if (defined($poweroff_after_halt)) {
2194 sleep $poweroff_after_halt;
2195 run_command "$power_off";
2196 }
2197 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002198 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04002199 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002200 }
2201}
2202
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002203sub success {
2204 my ($i) = @_;
2205
Steven Rostedt921ed4c2012-07-19 15:18:27 -04002206 if (defined($post_test)) {
2207 run_command $post_test;
2208 }
2209
Steven Rostedte48c5292010-11-02 14:35:37 -04002210 $successes++;
2211
Steven Rostedt9064af52011-06-13 10:38:48 -04002212 my $name = "";
2213
2214 if (defined($test_name)) {
2215 $name = " ($test_name)";
2216 }
2217
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002218 doprint "\n\n*******************************************\n";
2219 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04002220 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002221 doprint "*******************************************\n";
2222 doprint "*******************************************\n";
2223
Rabin Vincentde5b6e32011-11-18 17:05:31 +05302224 if (defined($store_successes)) {
2225 save_logs "success", $store_successes;
2226 }
2227
Steven Rostedt576f6272010-11-02 14:58:38 -04002228 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002229 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002230 reboot_to_good $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002231 }
2232}
2233
Steven Rostedtc960bb92011-03-08 09:22:39 -05002234sub answer_bisect {
2235 for (;;) {
2236 doprint "Pass or fail? [p/f]";
2237 my $ans = <STDIN>;
2238 chomp $ans;
2239 if ($ans eq "p" || $ans eq "P") {
2240 return 1;
2241 } elsif ($ans eq "f" || $ans eq "F") {
2242 return 0;
2243 } else {
2244 print "Please answer 'P' or 'F'\n";
2245 }
2246 }
2247}
2248
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002249sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002250 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002251
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002252 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04002253 $reboot_on_error = 0;
2254 $poweroff_on_error = 0;
2255 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002256
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05302257 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002258 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05302259 undef $redirect;
2260
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002261 exit $failed;
2262}
2263
2264my $child_done;
2265
2266sub child_finished {
2267 $child_done = 1;
2268}
2269
2270sub do_run_test {
2271 my $child_pid;
2272 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002273 my $line;
2274 my $full_line;
2275 my $bug = 0;
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002276 my $bug_ignored = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002277
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002278 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002279
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002280 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002281
2282 $child_done = 0;
2283
2284 $SIG{CHLD} = qw(child_finished);
2285
2286 $child_pid = fork;
2287
2288 child_run_test if (!$child_pid);
2289
2290 $full_line = "";
2291
2292 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002293 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002294 if (defined($line)) {
2295
2296 # we are not guaranteed to get a full line
2297 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002298 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002299
2300 if ($full_line =~ /call trace:/i) {
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002301 if ($ignore_errors) {
2302 $bug_ignored = 1;
2303 } else {
2304 $bug = 1;
2305 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002306 }
2307
2308 if ($full_line =~ /Kernel panic -/) {
2309 $bug = 1;
2310 }
2311
2312 if ($line =~ /\n/) {
2313 $full_line = "";
2314 }
2315 }
2316 } while (!$child_done && !$bug);
2317
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002318 if (!$bug && $bug_ignored) {
2319 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
2320 }
2321
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002322 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002323 my $failure_start = time;
2324 my $now;
2325 do {
2326 $line = wait_for_input($monitor_fp, 1);
2327 if (defined($line)) {
2328 doprint $line;
2329 }
2330 $now = time;
2331 if ($now - $failure_start >= $stop_after_failure) {
2332 last;
2333 }
2334 } while (defined($line));
2335
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002336 doprint "Detected kernel crash!\n";
2337 # kill the child with extreme prejudice
2338 kill 9, $child_pid;
2339 }
2340
2341 waitpid $child_pid, 0;
2342 $child_exit = $?;
2343
Steven Rostedtc5dacb82011-12-22 12:43:57 -05002344 if (!$bug && $in_bisect) {
2345 if (defined($bisect_ret_good)) {
2346 if ($child_exit == $bisect_ret_good) {
2347 return 1;
2348 }
2349 }
2350 if (defined($bisect_ret_skip)) {
2351 if ($child_exit == $bisect_ret_skip) {
2352 return -1;
2353 }
2354 }
2355 if (defined($bisect_ret_abort)) {
2356 if ($child_exit == $bisect_ret_abort) {
2357 fail "test abort" and return -2;
2358 }
2359 }
2360 if (defined($bisect_ret_bad)) {
2361 if ($child_exit == $bisect_ret_skip) {
2362 return 0;
2363 }
2364 }
2365 if (defined($bisect_ret_default)) {
2366 if ($bisect_ret_default eq "good") {
2367 return 1;
2368 } elsif ($bisect_ret_default eq "bad") {
2369 return 0;
2370 } elsif ($bisect_ret_default eq "skip") {
2371 return -1;
2372 } elsif ($bisect_ret_default eq "abort") {
2373 return -2;
2374 } else {
2375 fail "unknown default action: $bisect_ret_default"
2376 and return -2;
2377 }
2378 }
2379 }
2380
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002381 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002382 return 0 if $in_bisect;
2383 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002384 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002385 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002386}
2387
Steven Rostedta75fece2010-11-02 14:58:27 -04002388sub run_git_bisect {
2389 my ($command) = @_;
2390
2391 doprint "$command ... ";
2392
2393 my $output = `$command 2>&1`;
2394 my $ret = $?;
2395
2396 logit $output;
2397
2398 if ($ret) {
2399 doprint "FAILED\n";
2400 dodie "Failed to git bisect";
2401 }
2402
2403 doprint "SUCCESS\n";
2404 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2405 doprint "$1 [$2]\n";
2406 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002407 $bisect_bad_commit = $1;
Steven Rostedta75fece2010-11-02 14:58:27 -04002408 doprint "Found bad commit... $1\n";
2409 return 0;
2410 } else {
2411 # we already logged it, just print it now.
2412 print $output;
2413 }
2414
2415 return 1;
2416}
2417
Steven Rostedtc23dca72011-03-08 09:26:31 -05002418sub bisect_reboot {
2419 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002420 reboot_to_good $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05002421}
2422
2423# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05002424sub run_bisect_test {
2425 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002426
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002427 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002428 my $result;
2429 my $output;
2430 my $ret;
2431
Steven Rostedt0a05c762010-11-08 11:14:10 -05002432 $in_bisect = 1;
2433
2434 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002435
2436 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002437 if ($failed && $bisect_skip) {
2438 $in_bisect = 0;
2439 return -1;
2440 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002441 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002442
2443 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04002444 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002445
2446 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002447 if ($failed && $bisect_skip) {
2448 end_monitor;
2449 bisect_reboot;
2450 $in_bisect = 0;
2451 return -1;
2452 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002453 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002454
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002455 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002456 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002457 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002458 }
2459
2460 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002461 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002462 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002463 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002464 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04002465
2466 # reboot the box to a kernel we can ssh to
2467 if ($type ne "build") {
2468 bisect_reboot;
2469 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002470 $in_bisect = 0;
2471
2472 return $result;
2473}
2474
2475sub run_bisect {
2476 my ($type) = @_;
2477 my $buildtype = "oldconfig";
2478
2479 # We should have a minconfig to use?
2480 if (defined($minconfig)) {
2481 $buildtype = "useconfig:$minconfig";
2482 }
2483
2484 my $ret = run_bisect_test $type, $buildtype;
2485
Steven Rostedtc960bb92011-03-08 09:22:39 -05002486 if ($bisect_manual) {
2487 $ret = answer_bisect;
2488 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002489
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002490 # Are we looking for where it worked, not failed?
Russ Dill5158ba3e2012-04-23 19:43:00 -07002491 if ($reverse_bisect && $ret >= 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002492 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002493 }
2494
Steven Rostedtc23dca72011-03-08 09:26:31 -05002495 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002496 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002497 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002498 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002499 } elsif ($bisect_skip) {
2500 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2501 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002502 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002503}
2504
Steven Rostedtdad98752011-11-22 20:48:57 -05002505sub update_bisect_replay {
2506 my $tmp_log = "$tmpdir/ktest_bisect_log";
2507 run_command "git bisect log > $tmp_log" or
2508 die "can't create bisect log";
2509 return $tmp_log;
2510}
2511
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002512sub bisect {
2513 my ($i) = @_;
2514
2515 my $result;
2516
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002517 die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2518 die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
2519 die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002520
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002521 my $good = $bisect_good;
2522 my $bad = $bisect_bad;
2523 my $type = $bisect_type;
2524 my $start = $bisect_start;
2525 my $replay = $bisect_replay;
2526 my $start_files = $bisect_files;
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002527
2528 if (defined($start_files)) {
2529 $start_files = " -- " . $start_files;
2530 } else {
2531 $start_files = "";
2532 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002533
Steven Rostedta57419b2010-11-02 15:13:54 -04002534 # convert to true sha1's
2535 $good = get_sha1($good);
2536 $bad = get_sha1($bad);
2537
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002538 if (defined($bisect_reverse) && $bisect_reverse == 1) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002539 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2540 $reverse_bisect = 1;
2541 } else {
2542 $reverse_bisect = 0;
2543 }
2544
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002545 # Can't have a test without having a test to run
2546 if ($type eq "test" && !defined($run_test)) {
2547 $type = "boot";
2548 }
2549
Steven Rostedtdad98752011-11-22 20:48:57 -05002550 # Check if a bisect was running
2551 my $bisect_start_file = "$builddir/.git/BISECT_START";
2552
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002553 my $check = $bisect_check;
Steven Rostedtdad98752011-11-22 20:48:57 -05002554 my $do_check = defined($check) && $check ne "0";
2555
2556 if ( -f $bisect_start_file ) {
2557 print "Bisect in progress found\n";
2558 if ($do_check) {
2559 print " If you say yes, then no checks of good or bad will be done\n";
2560 }
2561 if (defined($replay)) {
2562 print "** BISECT_REPLAY is defined in config file **";
2563 print " Ignore config option and perform new git bisect log?\n";
2564 if (read_ync " (yes, no, or cancel) ") {
2565 $replay = update_bisect_replay;
2566 $do_check = 0;
2567 }
2568 } elsif (read_yn "read git log and continue?") {
2569 $replay = update_bisect_replay;
2570 $do_check = 0;
2571 }
2572 }
2573
2574 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002575
2576 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002577 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002578
2579 if ($check ne "good") {
2580 doprint "TESTING BISECT BAD [$bad]\n";
2581 run_command "git checkout $bad" or
2582 die "Failed to checkout $bad";
2583
2584 $result = run_bisect $type;
2585
2586 if ($result ne "bad") {
2587 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2588 }
2589 }
2590
2591 if ($check ne "bad") {
2592 doprint "TESTING BISECT GOOD [$good]\n";
2593 run_command "git checkout $good" or
2594 die "Failed to checkout $good";
2595
2596 $result = run_bisect $type;
2597
2598 if ($result ne "good") {
2599 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2600 }
2601 }
2602
2603 # checkout where we started
2604 run_command "git checkout $head" or
2605 die "Failed to checkout $head";
2606 }
2607
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002608 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002609 dodie "could not start bisect";
2610
2611 run_command "git bisect good $good" or
2612 dodie "could not set bisect good to $good";
2613
2614 run_git_bisect "git bisect bad $bad" or
2615 dodie "could not set bisect bad to $bad";
2616
2617 if (defined($replay)) {
2618 run_command "git bisect replay $replay" or
2619 dodie "failed to run replay";
2620 }
2621
2622 if (defined($start)) {
2623 run_command "git checkout $start" or
2624 dodie "failed to checkout $start";
2625 }
2626
2627 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002628 do {
2629 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002630 $test = run_git_bisect "git bisect $result";
2631 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002632
2633 run_command "git bisect log" or
2634 dodie "could not capture git bisect log";
2635
2636 run_command "git bisect reset" or
2637 dodie "could not reset git bisect";
2638
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002639 doprint "Bad commit was [$bisect_bad_commit]\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002640
Steven Rostedt0a05c762010-11-08 11:14:10 -05002641 success $i;
2642}
2643
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002644# config_ignore holds the configs that were set (or unset) for
2645# a good config and we will ignore these configs for the rest
2646# of a config bisect. These configs stay as they were.
Steven Rostedt0a05c762010-11-08 11:14:10 -05002647my %config_ignore;
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002648
2649# config_set holds what all configs were set as.
Steven Rostedt0a05c762010-11-08 11:14:10 -05002650my %config_set;
2651
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002652# config_off holds the set of configs that the bad config had disabled.
2653# We need to record them and set them in the .config when running
Adam Leefb16d892012-09-01 01:05:17 +08002654# olddefconfig, because olddefconfig keeps the defaults.
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002655my %config_off;
2656
2657# config_off_tmp holds a set of configs to turn off for now
2658my @config_off_tmp;
2659
2660# config_list is the set of configs that are being tested
Steven Rostedt0a05c762010-11-08 11:14:10 -05002661my %config_list;
2662my %null_config;
2663
2664my %dependency;
2665
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002666sub assign_configs {
2667 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002668
2669 open (IN, $config)
2670 or dodie "Failed to read $config";
2671
2672 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002673 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002674 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002675 }
2676 }
2677
2678 close(IN);
2679}
2680
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002681sub process_config_ignore {
2682 my ($config) = @_;
2683
2684 assign_configs \%config_ignore, $config;
2685}
2686
Steven Rostedt0a05c762010-11-08 11:14:10 -05002687sub read_current_config {
2688 my ($config_ref) = @_;
2689
2690 %{$config_ref} = ();
2691 undef %{$config_ref};
2692
2693 my @key = keys %{$config_ref};
2694 if ($#key >= 0) {
2695 print "did not delete!\n";
2696 exit;
2697 }
2698 open (IN, "$output_config");
2699
2700 while (<IN>) {
2701 if (/^(CONFIG\S+)=(.*)/) {
2702 ${$config_ref}{$1} = $2;
2703 }
2704 }
2705 close(IN);
2706}
2707
2708sub get_dependencies {
2709 my ($config) = @_;
2710
2711 my $arr = $dependency{$config};
2712 if (!defined($arr)) {
2713 return ();
2714 }
2715
2716 my @deps = @{$arr};
2717
2718 foreach my $dep (@{$arr}) {
2719 print "ADD DEP $dep\n";
2720 @deps = (@deps, get_dependencies $dep);
2721 }
2722
2723 return @deps;
2724}
2725
2726sub create_config {
2727 my @configs = @_;
2728
2729 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2730
2731 foreach my $config (@configs) {
2732 print OUT "$config_set{$config}\n";
2733 my @deps = get_dependencies $config;
2734 foreach my $dep (@deps) {
2735 print OUT "$config_set{$dep}\n";
2736 }
2737 }
2738
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002739 # turn off configs to keep off
2740 foreach my $config (keys %config_off) {
2741 print OUT "# $config is not set\n";
2742 }
2743
2744 # turn off configs that should be off for now
2745 foreach my $config (@config_off_tmp) {
2746 print OUT "# $config is not set\n";
2747 }
2748
Steven Rostedt0a05c762010-11-08 11:14:10 -05002749 foreach my $config (keys %config_ignore) {
2750 print OUT "$config_ignore{$config}\n";
2751 }
2752 close(OUT);
2753
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002754 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002755}
2756
2757sub compare_configs {
2758 my (%a, %b) = @_;
2759
2760 foreach my $item (keys %a) {
2761 if (!defined($b{$item})) {
2762 print "diff $item\n";
2763 return 1;
2764 }
2765 delete $b{$item};
2766 }
2767
2768 my @keys = keys %b;
2769 if ($#keys) {
2770 print "diff2 $keys[0]\n";
2771 }
2772 return -1 if ($#keys >= 0);
2773
2774 return 0;
2775}
2776
2777sub run_config_bisect_test {
2778 my ($type) = @_;
2779
2780 return run_bisect_test $type, "oldconfig";
2781}
2782
2783sub process_passed {
2784 my (%configs) = @_;
2785
2786 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2787 # Passed! All these configs are part of a good compile.
2788 # Add them to the min options.
2789 foreach my $config (keys %configs) {
2790 if (defined($config_list{$config})) {
2791 doprint " removing $config\n";
2792 $config_ignore{$config} = $config_list{$config};
2793 delete $config_list{$config};
2794 }
2795 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002796 doprint "config copied to $outputdir/config_good\n";
2797 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002798}
2799
2800sub process_failed {
2801 my ($config) = @_;
2802
2803 doprint "\n\n***************************************\n";
2804 doprint "Found bad config: $config\n";
2805 doprint "***************************************\n\n";
2806}
2807
2808sub run_config_bisect {
2809
2810 my @start_list = keys %config_list;
2811
2812 if ($#start_list < 0) {
2813 doprint "No more configs to test!!!\n";
2814 return -1;
2815 }
2816
2817 doprint "***** RUN TEST ***\n";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002818 my $type = $config_bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002819 my $ret;
2820 my %current_config;
2821
2822 my $count = $#start_list + 1;
2823 doprint " $count configs to test\n";
2824
2825 my $half = int($#start_list / 2);
2826
2827 do {
2828 my @tophalf = @start_list[0 .. $half];
2829
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002830 # keep the bottom half off
2831 if ($half < $#start_list) {
2832 @config_off_tmp = @start_list[$half + 1 .. $#start_list];
2833 } else {
2834 @config_off_tmp = ();
2835 }
2836
Steven Rostedt0a05c762010-11-08 11:14:10 -05002837 create_config @tophalf;
2838 read_current_config \%current_config;
2839
2840 $count = $#tophalf + 1;
2841 doprint "Testing $count configs\n";
2842 my $found = 0;
2843 # make sure we test something
2844 foreach my $config (@tophalf) {
2845 if (defined($current_config{$config})) {
2846 logit " $config\n";
2847 $found = 1;
2848 }
2849 }
2850 if (!$found) {
2851 # try the other half
2852 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002853
2854 # keep the top half off
2855 @config_off_tmp = @tophalf;
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002856 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002857
Steven Rostedt0a05c762010-11-08 11:14:10 -05002858 create_config @tophalf;
2859 read_current_config \%current_config;
2860 foreach my $config (@tophalf) {
2861 if (defined($current_config{$config})) {
2862 logit " $config\n";
2863 $found = 1;
2864 }
2865 }
2866 if (!$found) {
2867 doprint "Failed: Can't make new config with current configs\n";
2868 foreach my $config (@start_list) {
2869 doprint " CONFIG: $config\n";
2870 }
2871 return -1;
2872 }
2873 $count = $#tophalf + 1;
2874 doprint "Testing $count configs\n";
2875 }
2876
2877 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002878 if ($bisect_manual) {
2879 $ret = answer_bisect;
2880 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002881 if ($ret) {
2882 process_passed %current_config;
2883 return 0;
2884 }
2885
2886 doprint "This config had a failure.\n";
2887 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002888 doprint "config copied to $outputdir/config_bad\n";
2889 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002890
2891 # A config exists in this group that was bad.
2892 foreach my $config (keys %config_list) {
2893 if (!defined($current_config{$config})) {
2894 doprint " removing $config\n";
2895 delete $config_list{$config};
2896 }
2897 }
2898
2899 @start_list = @tophalf;
2900
2901 if ($#start_list == 0) {
2902 process_failed $start_list[0];
2903 return 1;
2904 }
2905
2906 # remove half the configs we are looking at and see if
2907 # they are good.
2908 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002909 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002910
Steven Rostedtc960bb92011-03-08 09:22:39 -05002911 # we found a single config, try it again unless we are running manually
2912
2913 if ($bisect_manual) {
2914 process_failed $start_list[0];
2915 return 1;
2916 }
2917
Steven Rostedt0a05c762010-11-08 11:14:10 -05002918 my @tophalf = @start_list[0 .. 0];
2919
2920 $ret = run_config_bisect_test $type;
2921 if ($ret) {
2922 process_passed %current_config;
2923 return 0;
2924 }
2925
2926 process_failed $start_list[0];
2927 return 1;
2928}
2929
2930sub config_bisect {
2931 my ($i) = @_;
2932
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002933 my $start_config = $config_bisect;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002934
2935 my $tmpconfig = "$tmpdir/use_config";
2936
Steven Rostedt30f75da2011-06-13 10:35:35 -04002937 if (defined($config_bisect_good)) {
2938 process_config_ignore $config_bisect_good;
2939 }
2940
Steven Rostedt0a05c762010-11-08 11:14:10 -05002941 # Make the file with the bad config and the min config
2942 if (defined($minconfig)) {
2943 # read the min config for things to ignore
2944 run_command "cp $minconfig $tmpconfig" or
2945 dodie "failed to copy $minconfig to $tmpconfig";
2946 } else {
2947 unlink $tmpconfig;
2948 }
2949
Steven Rostedt0a05c762010-11-08 11:14:10 -05002950 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002951 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002952 process_config_ignore $tmpconfig;
2953 }
2954
2955 # now process the start config
2956 run_command "cp $start_config $output_config" or
2957 dodie "failed to copy $start_config to $output_config";
2958
2959 # read directly what we want to check
2960 my %config_check;
2961 open (IN, $output_config)
Masanari Iidaf9dee312012-02-11 21:46:56 +09002962 or dodie "failed to open $output_config";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002963
2964 while (<IN>) {
2965 if (/^((CONFIG\S*)=.*)/) {
2966 $config_check{$2} = $1;
2967 }
2968 }
2969 close(IN);
2970
Steven Rostedt250bae82011-07-15 22:05:59 -04002971 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002972 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002973
2974 # check to see what we lost (or gained)
2975 open (IN, $output_config)
2976 or dodie "Failed to read $start_config";
2977
2978 my %removed_configs;
2979 my %added_configs;
2980
2981 while (<IN>) {
2982 if (/^((CONFIG\S*)=.*)/) {
2983 # save off all options
2984 $config_set{$2} = $1;
2985 if (defined($config_check{$2})) {
2986 if (defined($config_ignore{$2})) {
2987 $removed_configs{$2} = $1;
2988 } else {
2989 $config_list{$2} = $1;
2990 }
2991 } elsif (!defined($config_ignore{$2})) {
2992 $added_configs{$2} = $1;
2993 $config_list{$2} = $1;
2994 }
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002995 } elsif (/^# ((CONFIG\S*).*)/) {
2996 # Keep these configs disabled
2997 $config_set{$2} = $1;
2998 $config_off{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002999 }
3000 }
3001 close(IN);
3002
3003 my @confs = keys %removed_configs;
3004 if ($#confs >= 0) {
3005 doprint "Configs overridden by default configs and removed from check:\n";
3006 foreach my $config (@confs) {
3007 doprint " $config\n";
3008 }
3009 }
3010 @confs = keys %added_configs;
3011 if ($#confs >= 0) {
3012 doprint "Configs appearing in make oldconfig and added:\n";
3013 foreach my $config (@confs) {
3014 doprint " $config\n";
3015 }
3016 }
3017
3018 my %config_test;
3019 my $once = 0;
3020
Steven Rostedtcf79fab2012-07-19 15:29:43 -04003021 @config_off_tmp = ();
3022
Steven Rostedt0a05c762010-11-08 11:14:10 -05003023 # Sometimes kconfig does weird things. We must make sure
3024 # that the config we autocreate has everything we need
3025 # to test, otherwise we may miss testing configs, or
3026 # may not be able to create a new config.
3027 # Here we create a config with everything set.
3028 create_config (keys %config_list);
3029 read_current_config \%config_test;
3030 foreach my $config (keys %config_list) {
3031 if (!defined($config_test{$config})) {
3032 if (!$once) {
3033 $once = 1;
3034 doprint "Configs not produced by kconfig (will not be checked):\n";
3035 }
3036 doprint " $config\n";
3037 delete $config_list{$config};
3038 }
3039 }
3040 my $ret;
Steven Rostedtb0918612012-07-19 15:26:00 -04003041
3042 if (defined($config_bisect_check) && $config_bisect_check) {
3043 doprint " Checking to make sure bad config with min config fails\n";
3044 create_config keys %config_list;
3045 $ret = run_config_bisect_test $config_bisect_type;
3046 if ($ret) {
3047 doprint " FAILED! Bad config with min config boots fine\n";
3048 return -1;
3049 }
3050 doprint " Bad config with min config fails as expected\n";
3051 }
3052
Steven Rostedt0a05c762010-11-08 11:14:10 -05003053 do {
3054 $ret = run_config_bisect;
3055 } while (!$ret);
3056
3057 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003058
3059 success $i;
3060}
3061
Steven Rostedt27d934b2011-05-20 09:18:18 -04003062sub patchcheck_reboot {
3063 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003064 reboot_to_good $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04003065}
3066
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003067sub patchcheck {
3068 my ($i) = @_;
3069
3070 die "PATCHCHECK_START[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003071 if (!defined($patchcheck_start));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003072 die "PATCHCHECK_TYPE[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003073 if (!defined($patchcheck_type));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003074
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003075 my $start = $patchcheck_start;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003076
3077 my $end = "HEAD";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003078 if (defined($patchcheck_end)) {
3079 $end = $patchcheck_end;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003080 }
3081
Steven Rostedta57419b2010-11-02 15:13:54 -04003082 # Get the true sha1's since we can use things like HEAD~3
3083 $start = get_sha1($start);
3084 $end = get_sha1($end);
3085
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003086 my $type = $patchcheck_type;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003087
3088 # Can't have a test without having a test to run
3089 if ($type eq "test" && !defined($run_test)) {
3090 $type = "boot";
3091 }
3092
3093 open (IN, "git log --pretty=oneline $end|") or
3094 dodie "could not get git list";
3095
3096 my @list;
3097
3098 while (<IN>) {
3099 chomp;
3100 $list[$#list+1] = $_;
3101 last if (/^$start/);
3102 }
3103 close(IN);
3104
3105 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003106 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003107 }
3108
3109 # go backwards in the list
3110 @list = reverse @list;
3111
3112 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04003113 my %ignored_warnings;
3114
3115 if (defined($ignore_warnings)) {
3116 foreach my $sha1 (split /\s+/, $ignore_warnings) {
3117 $ignored_warnings{$sha1} = 1;
3118 }
3119 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003120
3121 $in_patchcheck = 1;
3122 foreach my $item (@list) {
3123 my $sha1 = $item;
3124 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
3125
3126 doprint "\nProcessing commit $item\n\n";
3127
3128 run_command "git checkout $sha1" or
3129 die "Failed to checkout $sha1";
3130
3131 # only clean on the first and last patch
3132 if ($item eq $list[0] ||
3133 $item eq $list[$#list]) {
3134 $noclean = $save_clean;
3135 } else {
3136 $noclean = 1;
3137 }
3138
3139 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003140 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003141 } else {
3142 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003143 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003144 }
3145
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05003146 # No need to do per patch checking if warnings file exists
3147 if (!defined($warnings_file) && !defined($ignored_warnings{$sha1})) {
3148 check_patch_buildlog $sha1 or return 0;
Steven Rostedt19902072011-06-14 20:46:25 -04003149 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003150
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05003151 check_buildlog or return 0;
3152
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003153 next if ($type eq "build");
3154
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003155 my $failed = 0;
3156
Steven Rostedtddf607e2011-06-14 20:49:13 -04003157 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003158
3159 if (!$failed && $type ne "boot"){
3160 do_run_test or $failed = 1;
3161 }
3162 end_monitor;
3163 return 0 if ($failed);
3164
Steven Rostedt27d934b2011-05-20 09:18:18 -04003165 patchcheck_reboot;
3166
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003167 }
3168 $in_patchcheck = 0;
3169 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003170
3171 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003172}
3173
Steven Rostedtb9066f62011-07-15 21:25:24 -04003174my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003175my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003176my $iflevel = 0;
3177my @ifdeps;
3178
3179# prevent recursion
3180my %read_kconfigs;
3181
Steven Rostedtac6974c2011-10-04 09:40:17 -04003182sub add_dep {
3183 # $config depends on $dep
3184 my ($config, $dep) = @_;
3185
3186 if (defined($depends{$config})) {
3187 $depends{$config} .= " " . $dep;
3188 } else {
3189 $depends{$config} = $dep;
3190 }
3191
3192 # record the number of configs depending on $dep
3193 if (defined $depcount{$dep}) {
3194 $depcount{$dep}++;
3195 } else {
3196 $depcount{$dep} = 1;
3197 }
3198}
3199
Steven Rostedtb9066f62011-07-15 21:25:24 -04003200# taken from streamline_config.pl
3201sub read_kconfig {
3202 my ($kconfig) = @_;
3203
3204 my $state = "NONE";
3205 my $config;
3206 my @kconfigs;
3207
3208 my $cont = 0;
3209 my $line;
3210
3211
3212 if (! -f $kconfig) {
3213 doprint "file $kconfig does not exist, skipping\n";
3214 return;
3215 }
3216
3217 open(KIN, "$kconfig")
3218 or die "Can't open $kconfig";
3219 while (<KIN>) {
3220 chomp;
3221
3222 # Make sure that lines ending with \ continue
3223 if ($cont) {
3224 $_ = $line . " " . $_;
3225 }
3226
3227 if (s/\\$//) {
3228 $cont = 1;
3229 $line = $_;
3230 next;
3231 }
3232
3233 $cont = 0;
3234
3235 # collect any Kconfig sources
3236 if (/^source\s*"(.*)"/) {
3237 $kconfigs[$#kconfigs+1] = $1;
3238 }
3239
3240 # configs found
3241 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
3242 $state = "NEW";
3243 $config = $2;
3244
3245 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04003246 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04003247 }
3248
3249 # collect the depends for the config
3250 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
3251
Steven Rostedtac6974c2011-10-04 09:40:17 -04003252 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003253
3254 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04003255 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
3256
3257 # selected by depends on config
3258 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003259
3260 # Check for if statements
3261 } elsif (/^if\s+(.*\S)\s*$/) {
3262 my $deps = $1;
3263 # remove beginning and ending non text
3264 $deps =~ s/^[^a-zA-Z0-9_]*//;
3265 $deps =~ s/[^a-zA-Z0-9_]*$//;
3266
3267 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
3268
3269 $ifdeps[$iflevel++] = join ':', @deps;
3270
3271 } elsif (/^endif/) {
3272
3273 $iflevel-- if ($iflevel);
3274
3275 # stop on "help"
3276 } elsif (/^\s*help\s*$/) {
3277 $state = "NONE";
3278 }
3279 }
3280 close(KIN);
3281
3282 # read in any configs that were found.
3283 foreach $kconfig (@kconfigs) {
3284 if (!defined($read_kconfigs{$kconfig})) {
3285 $read_kconfigs{$kconfig} = 1;
3286 read_kconfig("$builddir/$kconfig");
3287 }
3288 }
3289}
3290
3291sub read_depends {
3292 # find out which arch this is by the kconfig file
3293 open (IN, $output_config)
3294 or dodie "Failed to read $output_config";
3295 my $arch;
3296 while (<IN>) {
3297 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
3298 $arch = $1;
3299 last;
3300 }
3301 }
3302 close IN;
3303
3304 if (!defined($arch)) {
3305 doprint "Could not find arch from config file\n";
3306 doprint "no dependencies used\n";
3307 return;
3308 }
3309
3310 # arch is really the subarch, we need to know
3311 # what directory to look at.
3312 if ($arch eq "i386" || $arch eq "x86_64") {
3313 $arch = "x86";
3314 } elsif ($arch =~ /^tile/) {
3315 $arch = "tile";
3316 }
3317
3318 my $kconfig = "$builddir/arch/$arch/Kconfig";
3319
3320 if (! -f $kconfig && $arch =~ /\d$/) {
3321 my $orig = $arch;
3322 # some subarchs have numbers, truncate them
3323 $arch =~ s/\d*$//;
3324 $kconfig = "$builddir/arch/$arch/Kconfig";
3325 if (! -f $kconfig) {
3326 doprint "No idea what arch dir $orig is for\n";
3327 doprint "no dependencies used\n";
3328 return;
3329 }
3330 }
3331
3332 read_kconfig($kconfig);
3333}
3334
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003335sub read_config_list {
3336 my ($config) = @_;
3337
3338 open (IN, $config)
3339 or dodie "Failed to read $config";
3340
3341 while (<IN>) {
3342 if (/^((CONFIG\S*)=.*)/) {
3343 if (!defined($config_ignore{$2})) {
3344 $config_list{$2} = $1;
3345 }
3346 }
3347 }
3348
3349 close(IN);
3350}
3351
3352sub read_output_config {
3353 my ($config) = @_;
3354
3355 assign_configs \%config_ignore, $config;
3356}
3357
3358sub make_new_config {
3359 my @configs = @_;
3360
3361 open (OUT, ">$output_config")
3362 or dodie "Failed to write $output_config";
3363
3364 foreach my $config (@configs) {
3365 print OUT "$config\n";
3366 }
3367 close OUT;
3368}
3369
Steven Rostedtac6974c2011-10-04 09:40:17 -04003370sub chomp_config {
3371 my ($config) = @_;
3372
3373 $config =~ s/CONFIG_//;
3374
3375 return $config;
3376}
3377
Steven Rostedtb9066f62011-07-15 21:25:24 -04003378sub get_depends {
3379 my ($dep) = @_;
3380
Steven Rostedtac6974c2011-10-04 09:40:17 -04003381 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003382
3383 $dep = $depends{"$kconfig"};
3384
3385 # the dep string we have saves the dependencies as they
3386 # were found, including expressions like ! && ||. We
3387 # want to split this out into just an array of configs.
3388
3389 my $valid = "A-Za-z_0-9";
3390
3391 my @configs;
3392
3393 while ($dep =~ /[$valid]/) {
3394
3395 if ($dep =~ /^[^$valid]*([$valid]+)/) {
3396 my $conf = "CONFIG_" . $1;
3397
3398 $configs[$#configs + 1] = $conf;
3399
3400 $dep =~ s/^[^$valid]*[$valid]+//;
3401 } else {
3402 die "this should never happen";
3403 }
3404 }
3405
3406 return @configs;
3407}
3408
3409my %min_configs;
3410my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003411my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003412my %processed_configs;
3413my %nochange_config;
3414
3415sub test_this_config {
3416 my ($config) = @_;
3417
3418 my $found;
3419
3420 # if we already processed this config, skip it
3421 if (defined($processed_configs{$config})) {
3422 return undef;
3423 }
3424 $processed_configs{$config} = 1;
3425
3426 # if this config failed during this round, skip it
3427 if (defined($nochange_config{$config})) {
3428 return undef;
3429 }
3430
Steven Rostedtac6974c2011-10-04 09:40:17 -04003431 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003432
3433 # Test dependencies first
3434 if (defined($depends{"$kconfig"})) {
3435 my @parents = get_depends $config;
3436 foreach my $parent (@parents) {
3437 # if the parent is in the min config, check it first
3438 next if (!defined($min_configs{$parent}));
3439 $found = test_this_config($parent);
3440 if (defined($found)) {
3441 return $found;
3442 }
3443 }
3444 }
3445
3446 # Remove this config from the list of configs
Adam Leefb16d892012-09-01 01:05:17 +08003447 # do a make olddefconfig and then read the resulting
Steven Rostedtb9066f62011-07-15 21:25:24 -04003448 # .config to make sure it is missing the config that
3449 # we had before
3450 my %configs = %min_configs;
3451 delete $configs{$config};
3452 make_new_config ((values %configs), (values %keep_configs));
3453 make_oldconfig;
3454 undef %configs;
3455 assign_configs \%configs, $output_config;
3456
3457 return $config if (!defined($configs{$config}));
3458
3459 doprint "disabling config $config did not change .config\n";
3460
3461 $nochange_config{$config} = 1;
3462
3463 return undef;
3464}
3465
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003466sub make_min_config {
3467 my ($i) = @_;
3468
Steven Rostedtccc513b2012-05-21 17:13:40 -04003469 my $type = $minconfig_type;
3470 if ($type ne "boot" && $type ne "test") {
3471 fail "Invalid MIN_CONFIG_TYPE '$minconfig_type'\n" .
3472 " make_min_config works only with 'boot' and 'test'\n" and return;
3473 }
3474
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003475 if (!defined($output_minconfig)) {
3476 fail "OUTPUT_MIN_CONFIG not defined" and return;
3477 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04003478
3479 # If output_minconfig exists, and the start_minconfig
3480 # came from min_config, than ask if we should use
3481 # that instead.
3482 if (-f $output_minconfig && !$start_minconfig_defined) {
3483 print "$output_minconfig exists\n";
Steven Rostedt43de3312012-05-21 23:35:12 -04003484 if (!defined($use_output_minconfig)) {
3485 if (read_yn " Use it as minconfig?") {
3486 $start_minconfig = $output_minconfig;
3487 }
3488 } elsif ($use_output_minconfig > 0) {
3489 doprint "Using $output_minconfig as MIN_CONFIG\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003490 $start_minconfig = $output_minconfig;
Steven Rostedt43de3312012-05-21 23:35:12 -04003491 } else {
3492 doprint "Set to still use MIN_CONFIG as starting point\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003493 }
3494 }
3495
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003496 if (!defined($start_minconfig)) {
3497 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
3498 }
3499
Steven Rostedt35ce5952011-07-15 21:57:25 -04003500 my $temp_config = "$tmpdir/temp_config";
3501
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003502 # First things first. We build an allnoconfig to find
3503 # out what the defaults are that we can't touch.
3504 # Some are selections, but we really can't handle selections.
3505
3506 my $save_minconfig = $minconfig;
3507 undef $minconfig;
3508
3509 run_command "$make allnoconfig" or return 0;
3510
Steven Rostedtb9066f62011-07-15 21:25:24 -04003511 read_depends;
3512
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003513 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003514
Steven Rostedt43d1b652011-07-15 22:01:56 -04003515 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003516 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003517
3518 if (defined($ignore_config)) {
3519 # make sure the file exists
3520 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003521 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003522 }
3523
Steven Rostedt43d1b652011-07-15 22:01:56 -04003524 %keep_configs = %save_configs;
3525
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003526 doprint "Load initial configs from $start_minconfig\n";
3527
3528 # Look at the current min configs, and save off all the
3529 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003530 assign_configs \%min_configs, $start_minconfig;
3531
3532 my @config_keys = keys %min_configs;
3533
Steven Rostedtac6974c2011-10-04 09:40:17 -04003534 # All configs need a depcount
3535 foreach my $config (@config_keys) {
3536 my $kconfig = chomp_config $config;
3537 if (!defined $depcount{$kconfig}) {
3538 $depcount{$kconfig} = 0;
3539 }
3540 }
3541
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003542 # Remove anything that was set by the make allnoconfig
3543 # we shouldn't need them as they get set for us anyway.
3544 foreach my $config (@config_keys) {
3545 # Remove anything in the ignore_config
3546 if (defined($keep_configs{$config})) {
3547 my $file = $ignore_config;
3548 $file =~ s,.*/(.*?)$,$1,;
3549 doprint "$config set by $file ... ignored\n";
3550 delete $min_configs{$config};
3551 next;
3552 }
3553 # But make sure the settings are the same. If a min config
3554 # sets a selection, we do not want to get rid of it if
3555 # it is not the same as what we have. Just move it into
3556 # the keep configs.
3557 if (defined($config_ignore{$config})) {
3558 if ($config_ignore{$config} ne $min_configs{$config}) {
3559 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3560 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3561 $keep_configs{$config} = $min_configs{$config};
3562 } else {
3563 doprint "$config set by allnoconfig ... ignored\n";
3564 }
3565 delete $min_configs{$config};
3566 }
3567 }
3568
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003569 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003570 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003571
3572 while (!$done) {
3573
3574 my $config;
3575 my $found;
3576
3577 # Now disable each config one by one and do a make oldconfig
3578 # till we find a config that changes our list.
3579
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003580 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003581
3582 # Sort keys by who is most dependent on
3583 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3584 @test_configs ;
3585
3586 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003587 my $reset = 1;
3588 for (my $i = 0; $i < $#test_configs; $i++) {
3589 if (!defined($nochange_config{$test_configs[0]})) {
3590 $reset = 0;
3591 last;
3592 }
3593 # This config didn't change the .config last time.
3594 # Place it at the end
3595 my $config = shift @test_configs;
3596 push @test_configs, $config;
3597 }
3598
3599 # if every test config has failed to modify the .config file
3600 # in the past, then reset and start over.
3601 if ($reset) {
3602 undef %nochange_config;
3603 }
3604
Steven Rostedtb9066f62011-07-15 21:25:24 -04003605 undef %processed_configs;
3606
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003607 foreach my $config (@test_configs) {
3608
Steven Rostedtb9066f62011-07-15 21:25:24 -04003609 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003610
Steven Rostedtb9066f62011-07-15 21:25:24 -04003611 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003612
3613 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003614 }
3615
3616 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003617 # we could have failed due to the nochange_config hash
3618 # reset and try again
3619 if (!$take_two) {
3620 undef %nochange_config;
3621 $take_two = 1;
3622 next;
3623 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003624 doprint "No more configs found that we can disable\n";
3625 $done = 1;
3626 last;
3627 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003628 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003629
3630 $config = $found;
3631
3632 doprint "Test with $config disabled\n";
3633
3634 # set in_bisect to keep build and monitor from dieing
3635 $in_bisect = 1;
3636
3637 my $failed = 0;
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003638 build "oldconfig" or $failed = 1;
3639 if (!$failed) {
3640 start_monitor_and_boot or $failed = 1;
Steven Rostedtccc513b2012-05-21 17:13:40 -04003641
3642 if ($type eq "test" && !$failed) {
3643 do_run_test or $failed = 1;
3644 }
3645
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003646 end_monitor;
3647 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003648
3649 $in_bisect = 0;
3650
3651 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003652 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003653 # this config is needed, add it to the ignore list.
3654 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003655 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003656 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003657
3658 # update new ignore configs
3659 if (defined($ignore_config)) {
3660 open (OUT, ">$temp_config")
3661 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003662 foreach my $config (keys %save_configs) {
3663 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003664 }
3665 close OUT;
3666 run_command "mv $temp_config $ignore_config" or
3667 dodie "failed to copy update to $ignore_config";
3668 }
3669
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003670 } else {
3671 # We booted without this config, remove it from the minconfigs.
3672 doprint "$config is not needed, disabling\n";
3673
3674 delete $min_configs{$config};
3675
3676 # Also disable anything that is not enabled in this config
3677 my %configs;
3678 assign_configs \%configs, $output_config;
3679 my @config_keys = keys %min_configs;
3680 foreach my $config (@config_keys) {
3681 if (!defined($configs{$config})) {
3682 doprint "$config is not set, disabling\n";
3683 delete $min_configs{$config};
3684 }
3685 }
3686
3687 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003688 open (OUT, ">$temp_config")
3689 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003690 foreach my $config (keys %keep_configs) {
3691 print OUT "$keep_configs{$config}\n";
3692 }
3693 foreach my $config (keys %min_configs) {
3694 print OUT "$min_configs{$config}\n";
3695 }
3696 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003697
3698 run_command "mv $temp_config $output_minconfig" or
3699 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003700 }
3701
3702 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003703 reboot_to_good $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003704 }
3705
3706 success $i;
3707 return 1;
3708}
3709
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05003710sub make_warnings_file {
3711 my ($i) = @_;
3712
3713 if (!defined($warnings_file)) {
3714 dodie "Must define WARNINGS_FILE for make_warnings_file test";
3715 }
3716
3717 if ($build_type eq "nobuild") {
3718 dodie "BUILD_TYPE can not be 'nobuild' for make_warnings_file test";
3719 }
3720
3721 build $build_type or dodie "Failed to build";
3722
3723 open(OUT, ">$warnings_file") or dodie "Can't create $warnings_file";
3724
3725 open(IN, $buildlog) or dodie "Can't open $buildlog";
3726 while (<IN>) {
3727
3728 # Some compilers use UTF-8 extended for quotes
3729 # for distcc heterogeneous systems, this causes issues
3730 s/$utf8_quote/'/g;
3731
3732 if (/$check_build_re/) {
3733 print OUT;
3734 }
3735 }
3736 close(IN);
3737
3738 close(OUT);
3739
3740 success $i;
3741}
3742
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003743$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003744
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003745if ($#ARGV == 0) {
3746 $ktest_config = $ARGV[0];
3747 if (! -f $ktest_config) {
3748 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003749 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003750 exit 0;
3751 }
3752 }
3753} else {
3754 $ktest_config = "ktest.conf";
3755}
3756
3757if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003758 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003759 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003760 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3761 print OUT << "EOF"
3762# Generated by ktest.pl
3763#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003764
3765# PWD is a ktest.pl variable that will result in the process working
3766# directory that ktest.pl is executed in.
3767
3768# THIS_DIR is automatically assigned the PWD of the path that generated
3769# the config file. It is best to use this variable when assigning other
3770# directory paths within this directory. This allows you to easily
3771# move the test cases to other locations or to other machines.
3772#
3773THIS_DIR := $variable{"PWD"}
3774
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003775# Define each test with TEST_START
3776# The config options below it will override the defaults
3777TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003778TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003779
3780DEFAULTS
3781EOF
3782;
3783 close(OUT);
3784}
3785read_config $ktest_config;
3786
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003787if (defined($opt{"LOG_FILE"})) {
Steven Rostedt (Red Hat)04262be2013-01-31 10:12:20 -05003788 $opt{"LOG_FILE"} = eval_option("LOG_FILE", $opt{"LOG_FILE"}, -1);
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003789}
3790
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003791# Append any configs entered in manually to the config file.
3792my @new_configs = keys %entered_configs;
3793if ($#new_configs >= 0) {
3794 print "\nAppending entered in configs to $ktest_config\n";
3795 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3796 foreach my $config (@new_configs) {
3797 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003798 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003799 }
3800}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003801
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003802if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3803 unlink $opt{"LOG_FILE"};
3804}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003805
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003806doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3807
Steven Rostedta57419b2010-11-02 15:13:54 -04003808for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3809
3810 if (!$i) {
3811 doprint "DEFAULT OPTIONS:\n";
3812 } else {
3813 doprint "\nTEST $i OPTIONS";
3814 if (defined($repeat_tests{$i})) {
3815 $repeat = $repeat_tests{$i};
3816 doprint " ITERATE $repeat";
3817 }
3818 doprint "\n";
3819 }
3820
3821 foreach my $option (sort keys %opt) {
3822
3823 if ($option =~ /\[(\d+)\]$/) {
3824 next if ($i != $1);
3825 } else {
3826 next if ($i);
3827 }
3828
3829 doprint "$option = $opt{$option}\n";
3830 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003831}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003832
Steven Rostedt2a625122011-05-20 15:48:59 -04003833sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003834 my ($name, $i) = @_;
3835
3836 my $option = "$name\[$i\]";
3837
3838 if (defined($opt{$option})) {
3839 return $opt{$option};
3840 }
3841
Steven Rostedta57419b2010-11-02 15:13:54 -04003842 foreach my $test (keys %repeat_tests) {
3843 if ($i >= $test &&
3844 $i < $test + $repeat_tests{$test}) {
3845 $option = "$name\[$test\]";
3846 if (defined($opt{$option})) {
3847 return $opt{$option};
3848 }
3849 }
3850 }
3851
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003852 if (defined($opt{$name})) {
3853 return $opt{$name};
3854 }
3855
3856 return undef;
3857}
3858
Steven Rostedt2a625122011-05-20 15:48:59 -04003859sub set_test_option {
3860 my ($name, $i) = @_;
3861
3862 my $option = __set_test_option($name, $i);
3863 return $option if (!defined($option));
3864
Steven Rostedt (Red Hat)04262be2013-01-31 10:12:20 -05003865 return eval_option($name, $option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003866}
3867
Steven Rostedt2545eb62010-11-02 15:01:32 -04003868# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003869for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003870
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003871 # Do not reboot on failing test options
3872 $no_reboot = 1;
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003873 $reboot_success = 0;
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003874
Steven Rostedt683a3e62012-05-18 13:34:35 -04003875 $have_version = 0;
3876
Steven Rostedt576f6272010-11-02 14:58:38 -04003877 $iteration = $i;
3878
Steven Rostedtc1434dc2012-07-20 22:39:16 -04003879 undef %force_config;
3880
Steven Rostedta75fece2010-11-02 14:58:27 -04003881 my $makecmd = set_test_option("MAKE_CMD", $i);
3882
Steven Rostedt9cc9e092011-12-22 21:37:22 -05003883 # Load all the options into their mapped variable names
3884 foreach my $opt (keys %option_map) {
3885 ${$option_map{$opt}} = set_test_option($opt, $i);
3886 }
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003887
Steven Rostedt35ce5952011-07-15 21:57:25 -04003888 $start_minconfig_defined = 1;
3889
Steven Rostedt921ed4c2012-07-19 15:18:27 -04003890 # The first test may override the PRE_KTEST option
3891 if (defined($pre_ktest) && $i == 1) {
3892 doprint "\n";
3893 run_command $pre_ktest;
3894 }
3895
3896 # Any test can override the POST_KTEST option
3897 # The last test takes precedence.
3898 if (defined($post_ktest)) {
3899 $final_post_ktest = $post_ktest;
3900 }
3901
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003902 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003903 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003904 $start_minconfig = $minconfig;
3905 }
3906
Steven Rostedta75fece2010-11-02 14:58:27 -04003907 chdir $builddir || die "can't change directory to $builddir";
3908
Andrew Jonesa908a662011-08-12 15:32:03 +02003909 foreach my $dir ($tmpdir, $outputdir) {
3910 if (!-d $dir) {
3911 mkpath($dir) or
3912 die "can't create $dir";
3913 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003914 }
3915
Steven Rostedte48c5292010-11-02 14:35:37 -04003916 $ENV{"SSH_USER"} = $ssh_user;
3917 $ENV{"MACHINE"} = $machine;
3918
Steven Rostedta75fece2010-11-02 14:58:27 -04003919 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303920 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003921 $dmesg = "$tmpdir/dmesg-$machine";
3922 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003923 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003924
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003925 if (!$buildonly) {
3926 $target = "$ssh_user\@$machine";
3927 if ($reboot_type eq "grub") {
3928 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
Steven Rostedta15ba912012-11-13 14:30:37 -05003929 } elsif ($reboot_type eq "grub2") {
3930 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3931 dodie "GRUB_FILE not defined" if (!defined($grub_file));
Steven Rostedt77869542012-12-11 17:37:41 -05003932 } elsif ($reboot_type eq "syslinux") {
3933 dodie "SYSLINUX_LABEL not defined" if (!defined($syslinux_label));
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003934 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003935 }
3936
3937 my $run_type = $build_type;
3938 if ($test_type eq "patchcheck") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003939 $run_type = $patchcheck_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003940 } elsif ($test_type eq "bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003941 $run_type = $bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003942 } elsif ($test_type eq "config_bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003943 $run_type = $config_bisect_type;
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05003944 } elsif ($test_type eq "make_min_config") {
3945 $run_type = "";
3946 } elsif ($test_type eq "make_warnings_file") {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003947 $run_type = "";
3948 }
3949
Steven Rostedta75fece2010-11-02 14:58:27 -04003950 # mistake in config file?
3951 if (!defined($run_type)) {
3952 $run_type = "ERROR";
3953 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003954
Steven Rostedte0a87422011-09-30 17:50:48 -04003955 my $installme = "";
3956 $installme = " no_install" if ($no_install);
3957
Steven Rostedt2545eb62010-11-02 15:01:32 -04003958 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003959 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003960
Steven Rostedt921ed4c2012-07-19 15:18:27 -04003961 if (defined($pre_test)) {
3962 run_command $pre_test;
3963 }
3964
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003965 unlink $dmesg;
3966 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303967 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003968
Steven Rostedt250bae82011-07-15 22:05:59 -04003969 if (defined($addconfig)) {
3970 my $min = $minconfig;
3971 if (!defined($minconfig)) {
3972 $min = "";
3973 }
3974 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003975 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003976 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003977 }
3978
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003979 if (defined($checkout)) {
3980 run_command "git checkout $checkout" or
3981 die "failed to checkout $checkout";
3982 }
3983
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003984 $no_reboot = 0;
3985
Steven Rostedt648a1822012-03-21 11:18:27 -04003986 # A test may opt to not reboot the box
3987 if ($reboot_on_success) {
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003988 $reboot_success = 1;
Steven Rostedt648a1822012-03-21 11:18:27 -04003989 }
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003990
Steven Rostedta75fece2010-11-02 14:58:27 -04003991 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003992 bisect $i;
3993 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003994 } elsif ($test_type eq "config_bisect") {
3995 config_bisect $i;
3996 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003997 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003998 patchcheck $i;
3999 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04004000 } elsif ($test_type eq "make_min_config") {
4001 make_min_config $i;
4002 next;
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05004003 } elsif ($test_type eq "make_warnings_file") {
4004 $no_reboot = 1;
4005 make_warnings_file $i;
4006 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04004007 }
4008
Steven Rostedt7faafbd2010-11-02 14:58:22 -04004009 if ($build_type ne "nobuild") {
4010 build $build_type or next;
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05004011 check_buildlog or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04004012 }
4013
Steven Rostedtcd8e3682011-08-18 16:35:44 -04004014 if ($test_type eq "install") {
4015 get_version;
4016 install;
4017 success $i;
4018 next;
4019 }
4020
Steven Rostedta75fece2010-11-02 14:58:27 -04004021 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04004022 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04004023 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04004024
4025 if (!$failed && $test_type ne "boot" && defined($run_test)) {
4026 do_run_test or $failed = 1;
4027 }
4028 end_monitor;
4029 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04004030 }
4031
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04004032 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04004033}
4034
Steven Rostedt921ed4c2012-07-19 15:18:27 -04004035if (defined($final_post_ktest)) {
4036 run_command $final_post_ktest;
4037}
4038
Steven Rostedt5c42fc52010-11-02 14:57:01 -04004039if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04004040 halt;
Steven Rostedt759a3cc2012-05-01 08:20:12 -04004041} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot && $reboot_success) {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05004042 reboot_to_good;
Steven Rostedt648a1822012-03-21 11:18:27 -04004043} elsif (defined($switch_to_good)) {
4044 # still need to get to the good kernel
4045 run_command $switch_to_good;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04004046}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04004047
Steven Rostedt648a1822012-03-21 11:18:27 -04004048
Steven Rostedte48c5292010-11-02 14:35:37 -04004049doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
4050
Steven Rostedt2545eb62010-11-02 15:01:32 -04004051exit 0;