blob: 3fd768e6599874b05dd9a84aafb7db7ab75b1ef2 [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 Rostedt407b95b2012-07-19 16:05:42 -04001215 if (wait_for_monitor($time, $reboot_success_line)) {
1216 # reboot got stuck?
Steven Rostedt8a80c722012-07-19 16:08:33 -04001217 doprint "Reboot did not finish. Forcing power cycle\n";
Steven Rostedt407b95b2012-07-19 16:05:42 -04001218 run_command "$power_cycle";
1219 }
Andrew Jones2728be42011-08-12 15:32:05 +02001220 end_monitor;
1221 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001222}
1223
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001224sub reboot_to_good {
1225 my ($time) = @_;
1226
1227 if (defined($switch_to_good)) {
1228 run_command $switch_to_good;
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001229 }
1230
1231 reboot $time;
1232}
1233
Steven Rostedt576f6272010-11-02 14:58:38 -04001234sub do_not_reboot {
1235 my $i = $iteration;
1236
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001237 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -04001238 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1239 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1240}
1241
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001242sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001243 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001244
Steven Rostedt576f6272010-11-02 14:58:38 -04001245 my $i = $iteration;
1246
1247 if ($reboot_on_error && !do_not_reboot) {
1248
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001249 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001250 reboot_to_good;
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001251
Steven Rostedta75fece2010-11-02 14:58:27 -04001252 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001253 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001254 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001255 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001256
Steven Rostedtf80802c2011-03-07 13:18:47 -05001257 if (defined($opt{"LOG_FILE"})) {
1258 print " See $opt{LOG_FILE} for more info.\n";
1259 }
1260
Steven Rostedt576f6272010-11-02 14:58:38 -04001261 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001262}
1263
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001264sub open_console {
1265 my ($fp) = @_;
1266
1267 my $flags;
1268
Steven Rostedta75fece2010-11-02 14:58:27 -04001269 my $pid = open($fp, "$console|") or
1270 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001271
1272 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001273 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001274 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001275 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001276
1277 return $pid;
1278}
1279
1280sub close_console {
1281 my ($fp, $pid) = @_;
1282
1283 doprint "kill child process $pid\n";
1284 kill 2, $pid;
1285
1286 print "closing!\n";
1287 close($fp);
1288}
1289
1290sub start_monitor {
1291 if ($monitor_cnt++) {
1292 return;
1293 }
1294 $monitor_fp = \*MONFD;
1295 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001296
1297 return;
1298
1299 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001300}
1301
1302sub end_monitor {
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001303 return if (!defined $console);
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001304 if (--$monitor_cnt) {
1305 return;
1306 }
1307 close_console($monitor_fp, $monitor_pid);
1308}
1309
1310sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001311 my ($time, $stop) = @_;
1312 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001313 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001314 my $booted = 0;
Steven Rostedt407b95b2012-07-19 16:05:42 -04001315 my $start_time = time;
Steven Rostedt8a80c722012-07-19 16:08:33 -04001316 my $skip_call_trace = 0;
1317 my $bug = 0;
1318 my $bug_ignored = 0;
Steven Rostedt407b95b2012-07-19 16:05:42 -04001319 my $now;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001320
Steven Rostedta75fece2010-11-02 14:58:27 -04001321 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001322
1323 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001324 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001325 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001326 last if (!defined($line));
1327 print "$line";
1328 $full_line .= $line;
1329
1330 if (defined($stop) && $full_line =~ /$stop/) {
1331 doprint "wait for monitor detected $stop\n";
1332 $booted = 1;
1333 }
1334
Steven Rostedt8a80c722012-07-19 16:08:33 -04001335 if ($full_line =~ /\[ backtrace testing \]/) {
1336 $skip_call_trace = 1;
1337 }
1338
1339 if ($full_line =~ /call trace:/i) {
1340 if (!$bug && !$skip_call_trace) {
1341 if ($ignore_errors) {
1342 $bug_ignored = 1;
1343 } else {
1344 $bug = 1;
1345 }
1346 }
1347 }
1348
1349 if ($full_line =~ /\[ end of backtrace testing \]/) {
1350 $skip_call_trace = 0;
1351 }
1352
1353 if ($full_line =~ /Kernel panic -/) {
1354 $bug = 1;
1355 }
1356
Steven Rostedt2b803362011-09-30 18:00:23 -04001357 if ($line =~ /\n/) {
1358 $full_line = "";
1359 }
Steven Rostedt407b95b2012-07-19 16:05:42 -04001360 $now = time;
1361 if ($now - $start_time >= $max_monitor_wait) {
1362 doprint "Exiting monitor flush due to hitting MAX_MONITOR_WAIT\n";
1363 return 1;
1364 }
Steven Rostedt2b803362011-09-30 18:00:23 -04001365 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001366 print "** Monitor flushed **\n";
Steven Rostedt8a80c722012-07-19 16:08:33 -04001367 return $bug;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001368}
1369
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301370sub save_logs {
1371 my ($result, $basedir) = @_;
1372 my @t = localtime;
1373 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1374 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1375
1376 my $type = $build_type;
1377 if ($type =~ /useconfig/) {
1378 $type = "useconfig";
1379 }
1380
1381 my $dir = "$machine-$test_type-$type-$result-$date";
1382
1383 $dir = "$basedir/$dir";
1384
1385 if (!-d $dir) {
1386 mkpath($dir) or
1387 die "can't create $dir";
1388 }
1389
1390 my %files = (
1391 "config" => $output_config,
1392 "buildlog" => $buildlog,
1393 "dmesg" => $dmesg,
1394 "testlog" => $testlog,
1395 );
1396
1397 while (my ($name, $source) = each(%files)) {
1398 if (-f "$source") {
1399 cp "$source", "$dir/$name" or
1400 die "failed to copy $source";
1401 }
1402 }
1403
1404 doprint "*** Saved info to $dir ***\n";
1405}
1406
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001407sub fail {
1408
Steven Rostedt921ed4c2012-07-19 15:18:27 -04001409 if (defined($post_test)) {
1410 run_command $post_test;
1411 }
1412
Steven Rostedta75fece2010-11-02 14:58:27 -04001413 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001414 dodie @_;
1415 }
1416
Steven Rostedta75fece2010-11-02 14:58:27 -04001417 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001418
Steven Rostedt576f6272010-11-02 14:58:38 -04001419 my $i = $iteration;
1420
Steven Rostedta75fece2010-11-02 14:58:27 -04001421 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001422 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001423 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001424 reboot_to_good $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001425 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001426
Steven Rostedt9064af52011-06-13 10:38:48 -04001427 my $name = "";
1428
1429 if (defined($test_name)) {
1430 $name = " ($test_name)";
1431 }
1432
Steven Rostedt576f6272010-11-02 14:58:38 -04001433 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1434 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001435 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001436 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1437 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001438
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301439 if (defined($store_failures)) {
1440 save_logs "fail", $store_failures;
1441 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001442
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001443 return 1;
1444}
1445
Steven Rostedt2545eb62010-11-02 15:01:32 -04001446sub run_command {
1447 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001448 my $dolog = 0;
1449 my $dord = 0;
1450 my $pid;
1451
Steven Rostedte48c5292010-11-02 14:35:37 -04001452 $command =~ s/\$SSH_USER/$ssh_user/g;
1453 $command =~ s/\$MACHINE/$machine/g;
1454
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001455 doprint("$command ... ");
1456
1457 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001458 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001459
1460 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001461 open(LOG, ">>$opt{LOG_FILE}") or
1462 dodie "failed to write to log";
1463 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001464 }
1465
1466 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001467 open (RD, ">$redirect") or
1468 dodie "failed to write to redirect $redirect";
1469 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001470 }
1471
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001472 while (<CMD>) {
1473 print LOG if ($dolog);
1474 print RD if ($dord);
1475 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001476
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001477 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001478 my $failed = $?;
1479
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001480 close(CMD);
1481 close(LOG) if ($dolog);
1482 close(RD) if ($dord);
1483
Steven Rostedt2545eb62010-11-02 15:01:32 -04001484 if ($failed) {
1485 doprint "FAILED!\n";
1486 } else {
1487 doprint "SUCCESS\n";
1488 }
1489
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001490 return !$failed;
1491}
1492
Steven Rostedte48c5292010-11-02 14:35:37 -04001493sub run_ssh {
1494 my ($cmd) = @_;
1495 my $cp_exec = $ssh_exec;
1496
1497 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1498 return run_command "$cp_exec";
1499}
1500
1501sub run_scp {
Steven Rostedt02ad2612012-03-21 08:21:24 -04001502 my ($src, $dst, $cp_scp) = @_;
Steven Rostedte48c5292010-11-02 14:35:37 -04001503
1504 $cp_scp =~ s/\$SRC_FILE/$src/g;
1505 $cp_scp =~ s/\$DST_FILE/$dst/g;
1506
1507 return run_command "$cp_scp";
1508}
1509
Steven Rostedt02ad2612012-03-21 08:21:24 -04001510sub run_scp_install {
1511 my ($src, $dst) = @_;
1512
1513 my $cp_scp = $scp_to_target_install;
1514
1515 return run_scp($src, $dst, $cp_scp);
1516}
1517
1518sub run_scp_mod {
1519 my ($src, $dst) = @_;
1520
1521 my $cp_scp = $scp_to_target;
1522
1523 return run_scp($src, $dst, $cp_scp);
1524}
1525
Steven Rostedta15ba912012-11-13 14:30:37 -05001526sub get_grub2_index {
1527
1528 return if (defined($grub_number));
1529
1530 doprint "Find grub2 menu ... ";
1531 $grub_number = -1;
1532
1533 my $ssh_grub = $ssh_exec;
1534 $ssh_grub =~ s,\$SSH_COMMAND,cat $grub_file,g;
1535
1536 open(IN, "$ssh_grub |")
1537 or die "unable to get $grub_file";
1538
1539 my $found = 0;
1540
1541 while (<IN>) {
1542 if (/^menuentry.*$grub_menu/) {
1543 $grub_number++;
1544 $found = 1;
1545 last;
1546 } elsif (/^menuentry\s/) {
1547 $grub_number++;
1548 }
1549 }
1550 close(IN);
1551
1552 die "Could not find '$grub_menu' in $grub_file on $machine"
1553 if (!$found);
1554 doprint "$grub_number\n";
1555}
1556
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001557sub get_grub_index {
1558
Steven Rostedta15ba912012-11-13 14:30:37 -05001559 if ($reboot_type eq "grub2") {
1560 get_grub2_index;
1561 return;
1562 }
1563
Steven Rostedta75fece2010-11-02 14:58:27 -04001564 if ($reboot_type ne "grub") {
1565 return;
1566 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001567 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001568
1569 doprint "Find grub menu ... ";
1570 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001571
1572 my $ssh_grub = $ssh_exec;
1573 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1574
1575 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001576 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001577
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001578 my $found = 0;
1579
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001580 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001581 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001582 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001583 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001584 last;
1585 } elsif (/^\s*title\s/) {
1586 $grub_number++;
1587 }
1588 }
1589 close(IN);
1590
Steven Rostedta75fece2010-11-02 14:58:27 -04001591 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001592 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001593 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001594}
1595
Steven Rostedt2545eb62010-11-02 15:01:32 -04001596sub wait_for_input
1597{
1598 my ($fp, $time) = @_;
1599 my $rin;
1600 my $ready;
1601 my $line;
1602 my $ch;
1603
1604 if (!defined($time)) {
1605 $time = $timeout;
1606 }
1607
1608 $rin = '';
1609 vec($rin, fileno($fp), 1) = 1;
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001610 ($ready, $time) = select($rin, undef, undef, $time);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001611
1612 $line = "";
1613
1614 # try to read one char at a time
1615 while (sysread $fp, $ch, 1) {
1616 $line .= $ch;
1617 last if ($ch eq "\n");
1618 }
1619
1620 if (!length($line)) {
1621 return undef;
1622 }
1623
1624 return $line;
1625}
1626
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001627sub reboot_to {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001628 if (defined($switch_to_test)) {
1629 run_command $switch_to_test;
1630 }
1631
Steven Rostedta75fece2010-11-02 14:58:27 -04001632 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001633 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
Steven Rostedta15ba912012-11-13 14:30:37 -05001634 } elsif ($reboot_type eq "grub2") {
1635 run_ssh "$grub_reboot $grub_number";
Steven Rostedt77869542012-12-11 17:37:41 -05001636 } elsif ($reboot_type eq "syslinux") {
1637 run_ssh "$syslinux --once \\\"$syslinux_label\\\" $syslinux_path";
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001638 } elsif (defined $reboot_script) {
1639 run_command "$reboot_script";
Steven Rostedta75fece2010-11-02 14:58:27 -04001640 }
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001641 reboot;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001642}
1643
Steven Rostedta57419b2010-11-02 15:13:54 -04001644sub get_sha1 {
1645 my ($commit) = @_;
1646
1647 doprint "git rev-list --max-count=1 $commit ... ";
1648 my $sha1 = `git rev-list --max-count=1 $commit`;
1649 my $ret = $?;
1650
1651 logit $sha1;
1652
1653 if ($ret) {
1654 doprint "FAILED\n";
1655 dodie "Failed to get git $commit";
1656 }
1657
1658 print "SUCCESS\n";
1659
1660 chomp $sha1;
1661
1662 return $sha1;
1663}
1664
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001665sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001666 my $booted = 0;
1667 my $bug = 0;
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001668 my $bug_ignored = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001669 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001670 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001671
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001672 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001673
1674 my $line;
1675 my $full_line = "";
1676
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001677 open(DMESG, "> $dmesg") or
1678 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001679
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001680 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001681
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001682 my $success_start;
1683 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001684 my $monitor_start = time;
1685 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001686 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001687
Steven Rostedt2d01b262011-03-08 09:47:54 -05001688 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001689
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001690 if ($bug && defined($stop_after_failure) &&
1691 $stop_after_failure >= 0) {
1692 my $time = $stop_after_failure - (time - $failure_start);
1693 $line = wait_for_input($monitor_fp, $time);
1694 if (!defined($line)) {
1695 doprint "bug timed out after $booted_timeout seconds\n";
1696 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1697 last;
1698 }
1699 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001700 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001701 if (!defined($line)) {
1702 my $s = $booted_timeout == 1 ? "" : "s";
1703 doprint "Successful boot found: break after $booted_timeout second$s\n";
1704 last;
1705 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001706 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001707 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001708 if (!defined($line)) {
1709 my $s = $timeout == 1 ? "" : "s";
1710 doprint "Timed out after $timeout second$s\n";
1711 last;
1712 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001713 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001714
Steven Rostedt2545eb62010-11-02 15:01:32 -04001715 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001716 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001717
1718 # we are not guaranteed to get a full line
1719 $full_line .= $line;
1720
Steven Rostedta75fece2010-11-02 14:58:27 -04001721 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001722 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001723 $success_start = time;
1724 }
1725
1726 if ($booted && defined($stop_after_success) &&
1727 $stop_after_success >= 0) {
1728 my $now = time;
1729 if ($now - $success_start >= $stop_after_success) {
1730 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1731 last;
1732 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001733 }
1734
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001735 if ($full_line =~ /\[ backtrace testing \]/) {
1736 $skip_call_trace = 1;
1737 }
1738
Steven Rostedt2545eb62010-11-02 15:01:32 -04001739 if ($full_line =~ /call trace:/i) {
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001740 if (!$bug && !$skip_call_trace) {
1741 if ($ignore_errors) {
1742 $bug_ignored = 1;
1743 } else {
1744 $bug = 1;
1745 $failure_start = time;
1746 }
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001747 }
1748 }
1749
1750 if ($bug && defined($stop_after_failure) &&
1751 $stop_after_failure >= 0) {
1752 my $now = time;
1753 if ($now - $failure_start >= $stop_after_failure) {
1754 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1755 last;
1756 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001757 }
1758
1759 if ($full_line =~ /\[ end of backtrace testing \]/) {
1760 $skip_call_trace = 0;
1761 }
1762
1763 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001764 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001765 $bug = 1;
1766 }
1767
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001768 # Detect triple faults by testing the banner
1769 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1770 if ($1 eq $version) {
1771 $version_found = 1;
1772 } elsif ($version_found && $detect_triplefault) {
1773 # We already booted into the kernel we are testing,
1774 # but now we booted into another kernel?
1775 # Consider this a triple fault.
1776 doprint "Aleady booted in Linux kernel $version, but now\n";
1777 doprint "we booted into Linux kernel $1.\n";
1778 doprint "Assuming that this is a triple fault.\n";
1779 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1780 last;
1781 }
1782 }
1783
Steven Rostedt2545eb62010-11-02 15:01:32 -04001784 if ($line =~ /\n/) {
1785 $full_line = "";
1786 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001787
1788 if ($stop_test_after > 0 && !$booted && !$bug) {
1789 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001790 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001791 $done = 1;
1792 }
1793 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001794 }
1795
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001796 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001797
Steven Rostedt2545eb62010-11-02 15:01:32 -04001798 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001799 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001800 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001801 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001802
Steven Rostedta75fece2010-11-02 14:58:27 -04001803 if (!$booted) {
1804 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001805 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001806 }
1807
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001808 if ($bug_ignored) {
1809 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
1810 }
1811
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001812 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001813}
1814
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001815sub eval_kernel_version {
1816 my ($option) = @_;
1817
1818 $option =~ s/\$KERNEL_VERSION/$version/g;
1819
1820 return $option;
1821}
1822
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001823sub do_post_install {
1824
1825 return if (!defined($post_install));
1826
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001827 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001828 run_command "$cp_post_install" or
1829 dodie "Failed to run post install";
1830}
1831
Steven Rostedte1a6c3d2012-12-11 21:19:41 -05001832# Sometimes the reboot fails, and will hang. We try to ssh to the box
1833# and if we fail, we force another reboot, that should powercycle it.
1834sub test_booted {
1835 if (!run_ssh "echo testing connection") {
1836 reboot $sleep_time;
1837 }
1838}
1839
Steven Rostedt2545eb62010-11-02 15:01:32 -04001840sub install {
1841
Steven Rostedte0a87422011-09-30 17:50:48 -04001842 return if ($no_install);
1843
Steven Rostedte5c2ec12012-07-19 15:22:05 -04001844 if (defined($pre_install)) {
1845 my $cp_pre_install = eval_kernel_version $pre_install;
1846 run_command "$cp_pre_install" or
1847 dodie "Failed to run pre install";
1848 }
1849
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001850 my $cp_target = eval_kernel_version $target_image;
1851
Steven Rostedte1a6c3d2012-12-11 21:19:41 -05001852 test_booted;
1853
Steven Rostedt02ad2612012-03-21 08:21:24 -04001854 run_scp_install "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001855 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001856
1857 my $install_mods = 0;
1858
1859 # should we process modules?
1860 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001861 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001862 while (<IN>) {
1863 if (/CONFIG_MODULES(=y)?/) {
Steven Rostedt8bc5e4e2012-10-26 00:10:32 -04001864 if (defined($1)) {
1865 $install_mods = 1;
1866 last;
1867 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001868 }
1869 }
1870 close(IN);
1871
1872 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001873 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001874 doprint "No modules needed\n";
1875 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001876 }
1877
Steven Rostedt627977d2012-03-21 08:16:15 -04001878 run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001879 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001880
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001881 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001882 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001883
Steven Rostedte48c5292010-11-02 14:35:37 -04001884 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001885 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001886
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001887 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001888 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001889 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001890
Steven Rostedt02ad2612012-03-21 08:21:24 -04001891 run_scp_mod "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001892 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001893
Steven Rostedta75fece2010-11-02 14:58:27 -04001894 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001895
Steven Rostedte7b13442011-06-14 20:44:36 -04001896 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001897 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001898
Steven Rostedte48c5292010-11-02 14:35:37 -04001899 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001900
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001901 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001902}
1903
Steven Rostedtddf607e2011-06-14 20:49:13 -04001904sub get_version {
1905 # get the release name
Steven Rostedt683a3e62012-05-18 13:34:35 -04001906 return if ($have_version);
Steven Rostedtddf607e2011-06-14 20:49:13 -04001907 doprint "$make kernelrelease ... ";
1908 $version = `$make kernelrelease | tail -1`;
1909 chomp($version);
1910 doprint "$version\n";
Steven Rostedt683a3e62012-05-18 13:34:35 -04001911 $have_version = 1;
Steven Rostedtddf607e2011-06-14 20:49:13 -04001912}
1913
1914sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001915 # Make sure the stable kernel has finished booting
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001916
1917 # Install bisects, don't need console
1918 if (defined $console) {
1919 start_monitor;
1920 wait_for_monitor 5;
1921 end_monitor;
1922 }
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001923
Steven Rostedtddf607e2011-06-14 20:49:13 -04001924 get_grub_index;
1925 get_version;
1926 install;
1927
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001928 start_monitor if (defined $console);
Steven Rostedtddf607e2011-06-14 20:49:13 -04001929 return monitor;
1930}
1931
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05001932my $check_build_re = ".*:.*(warning|error|Error):.*";
1933my $utf8_quote = "\\x{e2}\\x{80}(\\x{98}|\\x{99})";
1934
1935# Read buildlog and check against warnings file for any
1936# new warnings.
1937#
1938# Returns 1 if OK
1939# 0 otherwise
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001940sub check_buildlog {
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05001941 return 1 if (!defined $warnings_file);
1942
1943 my %warnings_list;
1944
1945 # Failed builds should not reboot the target
1946 my $save_no_reboot = $no_reboot;
1947 $no_reboot = 1;
1948
1949 if (-f $warnings_file) {
1950 open(IN, $warnings_file) or
1951 dodie "Error opening $warnings_file";
1952
1953 while (<IN>) {
1954 if (/$check_build_re/) {
1955 chomp;
1956 $warnings_list{$_} = 1;
1957 }
1958 }
1959 close(IN);
1960 }
1961
1962 # If warnings file didn't exist, and WARNINGS_FILE exist,
1963 # then we fail on any warning!
1964
1965 open(IN, $buildlog) or dodie "Can't open $buildlog";
1966 while (<IN>) {
1967 if (/$check_build_re/) {
1968
1969 # Some compilers use UTF-8 extended for quotes
1970 # for distcc heterogeneous systems, this causes issues
1971 s/$utf8_quote/'/g;
1972
1973 chomp;
1974 if (!defined $warnings_list{$_}) {
1975 fail "New warning found (not in $warnings_file)\n$_\n";
1976 $no_reboot = $save_no_reboot;
1977 return 0;
1978 }
1979 }
1980 }
1981 $no_reboot = $save_no_reboot;
1982 close(IN);
1983}
1984
1985sub check_patch_buildlog {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001986 my ($patch) = @_;
1987
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001988 my @files = `git show $patch | diffstat -l`;
1989
Steven Rostedt (Red Hat)35275682013-01-30 12:28:15 -05001990 foreach my $file (@files) {
1991 chomp $file;
1992 }
1993
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001994 open(IN, "git show $patch |") or
1995 dodie "failed to show $patch";
1996 while (<IN>) {
1997 if (m,^--- a/(.*),) {
1998 chomp $1;
1999 $files[$#files] = $1;
2000 }
2001 }
2002 close(IN);
2003
2004 open(IN, $buildlog) or dodie "Can't open $buildlog";
2005 while (<IN>) {
2006 if (/^\s*(.*?):.*(warning|error)/) {
2007 my $err = $1;
2008 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002009 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002010 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002011 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002012 }
2013 }
2014 }
2015 }
2016 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002017
2018 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002019}
2020
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002021sub apply_min_config {
2022 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05002023
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002024 # Read the config file and remove anything that
2025 # is in the force_config hash (from minconfig and others)
2026 # then add the force config back.
2027
2028 doprint "Applying minimum configurations into $output_config.new\n";
2029
2030 open (OUT, ">$outconfig") or
2031 dodie "Can't create $outconfig";
2032
2033 if (-f $output_config) {
2034 open (IN, $output_config) or
2035 dodie "Failed to open $output_config";
2036 while (<IN>) {
2037 if (/^(# )?(CONFIG_[^\s=]*)/) {
2038 next if (defined($force_config{$2}));
2039 }
2040 print OUT;
2041 }
2042 close IN;
2043 }
2044 foreach my $config (keys %force_config) {
2045 print OUT "$force_config{$config}\n";
2046 }
2047 close OUT;
2048
2049 run_command "mv $outconfig $output_config";
2050}
2051
2052sub make_oldconfig {
2053
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002054 my @force_list = keys %force_config;
2055
2056 if ($#force_list >= 0) {
2057 apply_min_config;
2058 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002059
Adam Leefb16d892012-09-01 01:05:17 +08002060 if (!run_command "$make olddefconfig") {
2061 # Perhaps olddefconfig doesn't exist in this version of the kernel
Steven Rostedt18925172012-12-11 20:16:03 -05002062 # try oldnoconfig
2063 doprint "olddefconfig failed, trying make oldnoconfig\n";
2064 if (!run_command "$make oldnoconfig") {
2065 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
2066 # try a yes '' | oldconfig
2067 run_command "yes '' | $make oldconfig" or
2068 dodie "failed make config oldconfig";
2069 }
Steven Rostedt612b9e92011-03-07 13:27:43 -05002070 }
2071}
2072
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002073# read a config file and use this to force new configs.
2074sub load_force_config {
2075 my ($config) = @_;
2076
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002077 doprint "Loading force configs from $config\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002078 open(IN, $config) or
2079 dodie "failed to read $config";
2080 while (<IN>) {
2081 chomp;
2082 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
2083 $force_config{$1} = $_;
2084 } elsif (/^# (CONFIG_\S*) is not set/) {
2085 $force_config{$1} = $_;
2086 }
2087 }
2088 close IN;
2089}
2090
Steven Rostedt2545eb62010-11-02 15:01:32 -04002091sub build {
2092 my ($type) = @_;
2093
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002094 unlink $buildlog;
2095
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04002096 # Failed builds should not reboot the target
2097 my $save_no_reboot = $no_reboot;
2098 $no_reboot = 1;
2099
Steven Rostedt683a3e62012-05-18 13:34:35 -04002100 # Calculate a new version from here.
2101 $have_version = 0;
2102
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04002103 if (defined($pre_build)) {
2104 my $ret = run_command $pre_build;
2105 if (!$ret && defined($pre_build_die) &&
2106 $pre_build_die) {
2107 dodie "failed to pre_build\n";
2108 }
2109 }
2110
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002111 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05002112 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002113 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002114
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002115 $type = "oldconfig";
2116 }
2117
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002118 # old config can ask questions
2119 if ($type eq "oldconfig") {
Adam Leefb16d892012-09-01 01:05:17 +08002120 $type = "olddefconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002121
2122 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05002123 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002124
Andrew Jones13488232011-08-12 15:32:04 +02002125 if (!$noclean) {
2126 run_command "mv $output_config $outputdir/config_temp" or
2127 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04002128
Andrew Jones13488232011-08-12 15:32:04 +02002129 run_command "$make mrproper" or dodie "make mrproper";
2130
2131 run_command "mv $outputdir/config_temp $output_config" or
2132 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002133 }
2134
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002135 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05002136 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002137 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002138 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002139 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04002140
2141 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04002142 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
2143 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04002144 close(OUT);
2145
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002146 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002147 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04002148 }
2149
Adam Leefb16d892012-09-01 01:05:17 +08002150 if ($type ne "olddefconfig") {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002151 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05002152 dodie "failed make config";
2153 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002154 # Run old config regardless, to enforce min configurations
2155 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002156
Steven Rostedta75fece2010-11-02 14:58:27 -04002157 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04002158 my $build_ret = run_command "$make $build_options";
2159 undef $redirect;
2160
2161 if (defined($post_build)) {
Steven Rostedt683a3e62012-05-18 13:34:35 -04002162 # Because a post build may change the kernel version
2163 # do it now.
2164 get_version;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04002165 my $ret = run_command $post_build;
2166 if (!$ret && defined($post_build_die) &&
2167 $post_build_die) {
2168 dodie "failed to post_build\n";
2169 }
2170 }
2171
2172 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002173 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04002174 if ($in_bisect) {
2175 $no_reboot = $save_no_reboot;
2176 return 0;
2177 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002178 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002179 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002180
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04002181 $no_reboot = $save_no_reboot;
2182
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002183 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002184}
2185
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002186sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04002187 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04002188 if (defined($poweroff_after_halt)) {
2189 sleep $poweroff_after_halt;
2190 run_command "$power_off";
2191 }
2192 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002193 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04002194 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002195 }
2196}
2197
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002198sub success {
2199 my ($i) = @_;
2200
Steven Rostedt921ed4c2012-07-19 15:18:27 -04002201 if (defined($post_test)) {
2202 run_command $post_test;
2203 }
2204
Steven Rostedte48c5292010-11-02 14:35:37 -04002205 $successes++;
2206
Steven Rostedt9064af52011-06-13 10:38:48 -04002207 my $name = "";
2208
2209 if (defined($test_name)) {
2210 $name = " ($test_name)";
2211 }
2212
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002213 doprint "\n\n*******************************************\n";
2214 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04002215 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002216 doprint "*******************************************\n";
2217 doprint "*******************************************\n";
2218
Rabin Vincentde5b6e32011-11-18 17:05:31 +05302219 if (defined($store_successes)) {
2220 save_logs "success", $store_successes;
2221 }
2222
Steven Rostedt576f6272010-11-02 14:58:38 -04002223 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002224 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002225 reboot_to_good $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002226 }
2227}
2228
Steven Rostedtc960bb92011-03-08 09:22:39 -05002229sub answer_bisect {
2230 for (;;) {
2231 doprint "Pass or fail? [p/f]";
2232 my $ans = <STDIN>;
2233 chomp $ans;
2234 if ($ans eq "p" || $ans eq "P") {
2235 return 1;
2236 } elsif ($ans eq "f" || $ans eq "F") {
2237 return 0;
2238 } else {
2239 print "Please answer 'P' or 'F'\n";
2240 }
2241 }
2242}
2243
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002244sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002245 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002246
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002247 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04002248 $reboot_on_error = 0;
2249 $poweroff_on_error = 0;
2250 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002251
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05302252 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002253 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05302254 undef $redirect;
2255
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002256 exit $failed;
2257}
2258
2259my $child_done;
2260
2261sub child_finished {
2262 $child_done = 1;
2263}
2264
2265sub do_run_test {
2266 my $child_pid;
2267 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002268 my $line;
2269 my $full_line;
2270 my $bug = 0;
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002271 my $bug_ignored = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002272
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002273 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002274
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002275 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002276
2277 $child_done = 0;
2278
2279 $SIG{CHLD} = qw(child_finished);
2280
2281 $child_pid = fork;
2282
2283 child_run_test if (!$child_pid);
2284
2285 $full_line = "";
2286
2287 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002288 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002289 if (defined($line)) {
2290
2291 # we are not guaranteed to get a full line
2292 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002293 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002294
2295 if ($full_line =~ /call trace:/i) {
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002296 if ($ignore_errors) {
2297 $bug_ignored = 1;
2298 } else {
2299 $bug = 1;
2300 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002301 }
2302
2303 if ($full_line =~ /Kernel panic -/) {
2304 $bug = 1;
2305 }
2306
2307 if ($line =~ /\n/) {
2308 $full_line = "";
2309 }
2310 }
2311 } while (!$child_done && !$bug);
2312
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002313 if (!$bug && $bug_ignored) {
2314 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
2315 }
2316
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002317 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002318 my $failure_start = time;
2319 my $now;
2320 do {
2321 $line = wait_for_input($monitor_fp, 1);
2322 if (defined($line)) {
2323 doprint $line;
2324 }
2325 $now = time;
2326 if ($now - $failure_start >= $stop_after_failure) {
2327 last;
2328 }
2329 } while (defined($line));
2330
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002331 doprint "Detected kernel crash!\n";
2332 # kill the child with extreme prejudice
2333 kill 9, $child_pid;
2334 }
2335
2336 waitpid $child_pid, 0;
2337 $child_exit = $?;
2338
Steven Rostedtc5dacb82011-12-22 12:43:57 -05002339 if (!$bug && $in_bisect) {
2340 if (defined($bisect_ret_good)) {
2341 if ($child_exit == $bisect_ret_good) {
2342 return 1;
2343 }
2344 }
2345 if (defined($bisect_ret_skip)) {
2346 if ($child_exit == $bisect_ret_skip) {
2347 return -1;
2348 }
2349 }
2350 if (defined($bisect_ret_abort)) {
2351 if ($child_exit == $bisect_ret_abort) {
2352 fail "test abort" and return -2;
2353 }
2354 }
2355 if (defined($bisect_ret_bad)) {
2356 if ($child_exit == $bisect_ret_skip) {
2357 return 0;
2358 }
2359 }
2360 if (defined($bisect_ret_default)) {
2361 if ($bisect_ret_default eq "good") {
2362 return 1;
2363 } elsif ($bisect_ret_default eq "bad") {
2364 return 0;
2365 } elsif ($bisect_ret_default eq "skip") {
2366 return -1;
2367 } elsif ($bisect_ret_default eq "abort") {
2368 return -2;
2369 } else {
2370 fail "unknown default action: $bisect_ret_default"
2371 and return -2;
2372 }
2373 }
2374 }
2375
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002376 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002377 return 0 if $in_bisect;
2378 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002379 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002380 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002381}
2382
Steven Rostedta75fece2010-11-02 14:58:27 -04002383sub run_git_bisect {
2384 my ($command) = @_;
2385
2386 doprint "$command ... ";
2387
2388 my $output = `$command 2>&1`;
2389 my $ret = $?;
2390
2391 logit $output;
2392
2393 if ($ret) {
2394 doprint "FAILED\n";
2395 dodie "Failed to git bisect";
2396 }
2397
2398 doprint "SUCCESS\n";
2399 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2400 doprint "$1 [$2]\n";
2401 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002402 $bisect_bad_commit = $1;
Steven Rostedta75fece2010-11-02 14:58:27 -04002403 doprint "Found bad commit... $1\n";
2404 return 0;
2405 } else {
2406 # we already logged it, just print it now.
2407 print $output;
2408 }
2409
2410 return 1;
2411}
2412
Steven Rostedtc23dca72011-03-08 09:26:31 -05002413sub bisect_reboot {
2414 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002415 reboot_to_good $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05002416}
2417
2418# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05002419sub run_bisect_test {
2420 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002421
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002422 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002423 my $result;
2424 my $output;
2425 my $ret;
2426
Steven Rostedt0a05c762010-11-08 11:14:10 -05002427 $in_bisect = 1;
2428
2429 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002430
2431 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002432 if ($failed && $bisect_skip) {
2433 $in_bisect = 0;
2434 return -1;
2435 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002436 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002437
2438 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04002439 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002440
2441 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002442 if ($failed && $bisect_skip) {
2443 end_monitor;
2444 bisect_reboot;
2445 $in_bisect = 0;
2446 return -1;
2447 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002448 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002449
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002450 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002451 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002452 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002453 }
2454
2455 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002456 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002457 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002458 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002459 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04002460
2461 # reboot the box to a kernel we can ssh to
2462 if ($type ne "build") {
2463 bisect_reboot;
2464 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002465 $in_bisect = 0;
2466
2467 return $result;
2468}
2469
2470sub run_bisect {
2471 my ($type) = @_;
2472 my $buildtype = "oldconfig";
2473
2474 # We should have a minconfig to use?
2475 if (defined($minconfig)) {
2476 $buildtype = "useconfig:$minconfig";
2477 }
2478
2479 my $ret = run_bisect_test $type, $buildtype;
2480
Steven Rostedtc960bb92011-03-08 09:22:39 -05002481 if ($bisect_manual) {
2482 $ret = answer_bisect;
2483 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002484
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002485 # Are we looking for where it worked, not failed?
Russ Dill5158ba3e2012-04-23 19:43:00 -07002486 if ($reverse_bisect && $ret >= 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002487 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002488 }
2489
Steven Rostedtc23dca72011-03-08 09:26:31 -05002490 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002491 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002492 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002493 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002494 } elsif ($bisect_skip) {
2495 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2496 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002497 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002498}
2499
Steven Rostedtdad98752011-11-22 20:48:57 -05002500sub update_bisect_replay {
2501 my $tmp_log = "$tmpdir/ktest_bisect_log";
2502 run_command "git bisect log > $tmp_log" or
2503 die "can't create bisect log";
2504 return $tmp_log;
2505}
2506
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002507sub bisect {
2508 my ($i) = @_;
2509
2510 my $result;
2511
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002512 die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2513 die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
2514 die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002515
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002516 my $good = $bisect_good;
2517 my $bad = $bisect_bad;
2518 my $type = $bisect_type;
2519 my $start = $bisect_start;
2520 my $replay = $bisect_replay;
2521 my $start_files = $bisect_files;
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002522
2523 if (defined($start_files)) {
2524 $start_files = " -- " . $start_files;
2525 } else {
2526 $start_files = "";
2527 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002528
Steven Rostedta57419b2010-11-02 15:13:54 -04002529 # convert to true sha1's
2530 $good = get_sha1($good);
2531 $bad = get_sha1($bad);
2532
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002533 if (defined($bisect_reverse) && $bisect_reverse == 1) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002534 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2535 $reverse_bisect = 1;
2536 } else {
2537 $reverse_bisect = 0;
2538 }
2539
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002540 # Can't have a test without having a test to run
2541 if ($type eq "test" && !defined($run_test)) {
2542 $type = "boot";
2543 }
2544
Steven Rostedtdad98752011-11-22 20:48:57 -05002545 # Check if a bisect was running
2546 my $bisect_start_file = "$builddir/.git/BISECT_START";
2547
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002548 my $check = $bisect_check;
Steven Rostedtdad98752011-11-22 20:48:57 -05002549 my $do_check = defined($check) && $check ne "0";
2550
2551 if ( -f $bisect_start_file ) {
2552 print "Bisect in progress found\n";
2553 if ($do_check) {
2554 print " If you say yes, then no checks of good or bad will be done\n";
2555 }
2556 if (defined($replay)) {
2557 print "** BISECT_REPLAY is defined in config file **";
2558 print " Ignore config option and perform new git bisect log?\n";
2559 if (read_ync " (yes, no, or cancel) ") {
2560 $replay = update_bisect_replay;
2561 $do_check = 0;
2562 }
2563 } elsif (read_yn "read git log and continue?") {
2564 $replay = update_bisect_replay;
2565 $do_check = 0;
2566 }
2567 }
2568
2569 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002570
2571 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002572 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002573
2574 if ($check ne "good") {
2575 doprint "TESTING BISECT BAD [$bad]\n";
2576 run_command "git checkout $bad" or
2577 die "Failed to checkout $bad";
2578
2579 $result = run_bisect $type;
2580
2581 if ($result ne "bad") {
2582 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2583 }
2584 }
2585
2586 if ($check ne "bad") {
2587 doprint "TESTING BISECT GOOD [$good]\n";
2588 run_command "git checkout $good" or
2589 die "Failed to checkout $good";
2590
2591 $result = run_bisect $type;
2592
2593 if ($result ne "good") {
2594 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2595 }
2596 }
2597
2598 # checkout where we started
2599 run_command "git checkout $head" or
2600 die "Failed to checkout $head";
2601 }
2602
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002603 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002604 dodie "could not start bisect";
2605
2606 run_command "git bisect good $good" or
2607 dodie "could not set bisect good to $good";
2608
2609 run_git_bisect "git bisect bad $bad" or
2610 dodie "could not set bisect bad to $bad";
2611
2612 if (defined($replay)) {
2613 run_command "git bisect replay $replay" or
2614 dodie "failed to run replay";
2615 }
2616
2617 if (defined($start)) {
2618 run_command "git checkout $start" or
2619 dodie "failed to checkout $start";
2620 }
2621
2622 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002623 do {
2624 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002625 $test = run_git_bisect "git bisect $result";
2626 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002627
2628 run_command "git bisect log" or
2629 dodie "could not capture git bisect log";
2630
2631 run_command "git bisect reset" or
2632 dodie "could not reset git bisect";
2633
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002634 doprint "Bad commit was [$bisect_bad_commit]\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002635
Steven Rostedt0a05c762010-11-08 11:14:10 -05002636 success $i;
2637}
2638
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002639# config_ignore holds the configs that were set (or unset) for
2640# a good config and we will ignore these configs for the rest
2641# of a config bisect. These configs stay as they were.
Steven Rostedt0a05c762010-11-08 11:14:10 -05002642my %config_ignore;
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002643
2644# config_set holds what all configs were set as.
Steven Rostedt0a05c762010-11-08 11:14:10 -05002645my %config_set;
2646
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002647# config_off holds the set of configs that the bad config had disabled.
2648# We need to record them and set them in the .config when running
Adam Leefb16d892012-09-01 01:05:17 +08002649# olddefconfig, because olddefconfig keeps the defaults.
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002650my %config_off;
2651
2652# config_off_tmp holds a set of configs to turn off for now
2653my @config_off_tmp;
2654
2655# config_list is the set of configs that are being tested
Steven Rostedt0a05c762010-11-08 11:14:10 -05002656my %config_list;
2657my %null_config;
2658
2659my %dependency;
2660
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002661sub assign_configs {
2662 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002663
2664 open (IN, $config)
2665 or dodie "Failed to read $config";
2666
2667 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002668 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002669 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002670 }
2671 }
2672
2673 close(IN);
2674}
2675
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002676sub process_config_ignore {
2677 my ($config) = @_;
2678
2679 assign_configs \%config_ignore, $config;
2680}
2681
Steven Rostedt0a05c762010-11-08 11:14:10 -05002682sub read_current_config {
2683 my ($config_ref) = @_;
2684
2685 %{$config_ref} = ();
2686 undef %{$config_ref};
2687
2688 my @key = keys %{$config_ref};
2689 if ($#key >= 0) {
2690 print "did not delete!\n";
2691 exit;
2692 }
2693 open (IN, "$output_config");
2694
2695 while (<IN>) {
2696 if (/^(CONFIG\S+)=(.*)/) {
2697 ${$config_ref}{$1} = $2;
2698 }
2699 }
2700 close(IN);
2701}
2702
2703sub get_dependencies {
2704 my ($config) = @_;
2705
2706 my $arr = $dependency{$config};
2707 if (!defined($arr)) {
2708 return ();
2709 }
2710
2711 my @deps = @{$arr};
2712
2713 foreach my $dep (@{$arr}) {
2714 print "ADD DEP $dep\n";
2715 @deps = (@deps, get_dependencies $dep);
2716 }
2717
2718 return @deps;
2719}
2720
2721sub create_config {
2722 my @configs = @_;
2723
2724 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2725
2726 foreach my $config (@configs) {
2727 print OUT "$config_set{$config}\n";
2728 my @deps = get_dependencies $config;
2729 foreach my $dep (@deps) {
2730 print OUT "$config_set{$dep}\n";
2731 }
2732 }
2733
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002734 # turn off configs to keep off
2735 foreach my $config (keys %config_off) {
2736 print OUT "# $config is not set\n";
2737 }
2738
2739 # turn off configs that should be off for now
2740 foreach my $config (@config_off_tmp) {
2741 print OUT "# $config is not set\n";
2742 }
2743
Steven Rostedt0a05c762010-11-08 11:14:10 -05002744 foreach my $config (keys %config_ignore) {
2745 print OUT "$config_ignore{$config}\n";
2746 }
2747 close(OUT);
2748
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002749 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002750}
2751
2752sub compare_configs {
2753 my (%a, %b) = @_;
2754
2755 foreach my $item (keys %a) {
2756 if (!defined($b{$item})) {
2757 print "diff $item\n";
2758 return 1;
2759 }
2760 delete $b{$item};
2761 }
2762
2763 my @keys = keys %b;
2764 if ($#keys) {
2765 print "diff2 $keys[0]\n";
2766 }
2767 return -1 if ($#keys >= 0);
2768
2769 return 0;
2770}
2771
2772sub run_config_bisect_test {
2773 my ($type) = @_;
2774
2775 return run_bisect_test $type, "oldconfig";
2776}
2777
2778sub process_passed {
2779 my (%configs) = @_;
2780
2781 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2782 # Passed! All these configs are part of a good compile.
2783 # Add them to the min options.
2784 foreach my $config (keys %configs) {
2785 if (defined($config_list{$config})) {
2786 doprint " removing $config\n";
2787 $config_ignore{$config} = $config_list{$config};
2788 delete $config_list{$config};
2789 }
2790 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002791 doprint "config copied to $outputdir/config_good\n";
2792 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002793}
2794
2795sub process_failed {
2796 my ($config) = @_;
2797
2798 doprint "\n\n***************************************\n";
2799 doprint "Found bad config: $config\n";
2800 doprint "***************************************\n\n";
2801}
2802
2803sub run_config_bisect {
2804
2805 my @start_list = keys %config_list;
2806
2807 if ($#start_list < 0) {
2808 doprint "No more configs to test!!!\n";
2809 return -1;
2810 }
2811
2812 doprint "***** RUN TEST ***\n";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002813 my $type = $config_bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002814 my $ret;
2815 my %current_config;
2816
2817 my $count = $#start_list + 1;
2818 doprint " $count configs to test\n";
2819
2820 my $half = int($#start_list / 2);
2821
2822 do {
2823 my @tophalf = @start_list[0 .. $half];
2824
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002825 # keep the bottom half off
2826 if ($half < $#start_list) {
2827 @config_off_tmp = @start_list[$half + 1 .. $#start_list];
2828 } else {
2829 @config_off_tmp = ();
2830 }
2831
Steven Rostedt0a05c762010-11-08 11:14:10 -05002832 create_config @tophalf;
2833 read_current_config \%current_config;
2834
2835 $count = $#tophalf + 1;
2836 doprint "Testing $count configs\n";
2837 my $found = 0;
2838 # make sure we test something
2839 foreach my $config (@tophalf) {
2840 if (defined($current_config{$config})) {
2841 logit " $config\n";
2842 $found = 1;
2843 }
2844 }
2845 if (!$found) {
2846 # try the other half
2847 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002848
2849 # keep the top half off
2850 @config_off_tmp = @tophalf;
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002851 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002852
Steven Rostedt0a05c762010-11-08 11:14:10 -05002853 create_config @tophalf;
2854 read_current_config \%current_config;
2855 foreach my $config (@tophalf) {
2856 if (defined($current_config{$config})) {
2857 logit " $config\n";
2858 $found = 1;
2859 }
2860 }
2861 if (!$found) {
2862 doprint "Failed: Can't make new config with current configs\n";
2863 foreach my $config (@start_list) {
2864 doprint " CONFIG: $config\n";
2865 }
2866 return -1;
2867 }
2868 $count = $#tophalf + 1;
2869 doprint "Testing $count configs\n";
2870 }
2871
2872 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002873 if ($bisect_manual) {
2874 $ret = answer_bisect;
2875 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002876 if ($ret) {
2877 process_passed %current_config;
2878 return 0;
2879 }
2880
2881 doprint "This config had a failure.\n";
2882 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002883 doprint "config copied to $outputdir/config_bad\n";
2884 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002885
2886 # A config exists in this group that was bad.
2887 foreach my $config (keys %config_list) {
2888 if (!defined($current_config{$config})) {
2889 doprint " removing $config\n";
2890 delete $config_list{$config};
2891 }
2892 }
2893
2894 @start_list = @tophalf;
2895
2896 if ($#start_list == 0) {
2897 process_failed $start_list[0];
2898 return 1;
2899 }
2900
2901 # remove half the configs we are looking at and see if
2902 # they are good.
2903 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002904 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002905
Steven Rostedtc960bb92011-03-08 09:22:39 -05002906 # we found a single config, try it again unless we are running manually
2907
2908 if ($bisect_manual) {
2909 process_failed $start_list[0];
2910 return 1;
2911 }
2912
Steven Rostedt0a05c762010-11-08 11:14:10 -05002913 my @tophalf = @start_list[0 .. 0];
2914
2915 $ret = run_config_bisect_test $type;
2916 if ($ret) {
2917 process_passed %current_config;
2918 return 0;
2919 }
2920
2921 process_failed $start_list[0];
2922 return 1;
2923}
2924
2925sub config_bisect {
2926 my ($i) = @_;
2927
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002928 my $start_config = $config_bisect;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002929
2930 my $tmpconfig = "$tmpdir/use_config";
2931
Steven Rostedt30f75da2011-06-13 10:35:35 -04002932 if (defined($config_bisect_good)) {
2933 process_config_ignore $config_bisect_good;
2934 }
2935
Steven Rostedt0a05c762010-11-08 11:14:10 -05002936 # Make the file with the bad config and the min config
2937 if (defined($minconfig)) {
2938 # read the min config for things to ignore
2939 run_command "cp $minconfig $tmpconfig" or
2940 dodie "failed to copy $minconfig to $tmpconfig";
2941 } else {
2942 unlink $tmpconfig;
2943 }
2944
Steven Rostedt0a05c762010-11-08 11:14:10 -05002945 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002946 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002947 process_config_ignore $tmpconfig;
2948 }
2949
2950 # now process the start config
2951 run_command "cp $start_config $output_config" or
2952 dodie "failed to copy $start_config to $output_config";
2953
2954 # read directly what we want to check
2955 my %config_check;
2956 open (IN, $output_config)
Masanari Iidaf9dee312012-02-11 21:46:56 +09002957 or dodie "failed to open $output_config";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002958
2959 while (<IN>) {
2960 if (/^((CONFIG\S*)=.*)/) {
2961 $config_check{$2} = $1;
2962 }
2963 }
2964 close(IN);
2965
Steven Rostedt250bae82011-07-15 22:05:59 -04002966 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002967 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002968
2969 # check to see what we lost (or gained)
2970 open (IN, $output_config)
2971 or dodie "Failed to read $start_config";
2972
2973 my %removed_configs;
2974 my %added_configs;
2975
2976 while (<IN>) {
2977 if (/^((CONFIG\S*)=.*)/) {
2978 # save off all options
2979 $config_set{$2} = $1;
2980 if (defined($config_check{$2})) {
2981 if (defined($config_ignore{$2})) {
2982 $removed_configs{$2} = $1;
2983 } else {
2984 $config_list{$2} = $1;
2985 }
2986 } elsif (!defined($config_ignore{$2})) {
2987 $added_configs{$2} = $1;
2988 $config_list{$2} = $1;
2989 }
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002990 } elsif (/^# ((CONFIG\S*).*)/) {
2991 # Keep these configs disabled
2992 $config_set{$2} = $1;
2993 $config_off{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002994 }
2995 }
2996 close(IN);
2997
2998 my @confs = keys %removed_configs;
2999 if ($#confs >= 0) {
3000 doprint "Configs overridden by default configs and removed from check:\n";
3001 foreach my $config (@confs) {
3002 doprint " $config\n";
3003 }
3004 }
3005 @confs = keys %added_configs;
3006 if ($#confs >= 0) {
3007 doprint "Configs appearing in make oldconfig and added:\n";
3008 foreach my $config (@confs) {
3009 doprint " $config\n";
3010 }
3011 }
3012
3013 my %config_test;
3014 my $once = 0;
3015
Steven Rostedtcf79fab2012-07-19 15:29:43 -04003016 @config_off_tmp = ();
3017
Steven Rostedt0a05c762010-11-08 11:14:10 -05003018 # Sometimes kconfig does weird things. We must make sure
3019 # that the config we autocreate has everything we need
3020 # to test, otherwise we may miss testing configs, or
3021 # may not be able to create a new config.
3022 # Here we create a config with everything set.
3023 create_config (keys %config_list);
3024 read_current_config \%config_test;
3025 foreach my $config (keys %config_list) {
3026 if (!defined($config_test{$config})) {
3027 if (!$once) {
3028 $once = 1;
3029 doprint "Configs not produced by kconfig (will not be checked):\n";
3030 }
3031 doprint " $config\n";
3032 delete $config_list{$config};
3033 }
3034 }
3035 my $ret;
Steven Rostedtb0918612012-07-19 15:26:00 -04003036
3037 if (defined($config_bisect_check) && $config_bisect_check) {
3038 doprint " Checking to make sure bad config with min config fails\n";
3039 create_config keys %config_list;
3040 $ret = run_config_bisect_test $config_bisect_type;
3041 if ($ret) {
3042 doprint " FAILED! Bad config with min config boots fine\n";
3043 return -1;
3044 }
3045 doprint " Bad config with min config fails as expected\n";
3046 }
3047
Steven Rostedt0a05c762010-11-08 11:14:10 -05003048 do {
3049 $ret = run_config_bisect;
3050 } while (!$ret);
3051
3052 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003053
3054 success $i;
3055}
3056
Steven Rostedt27d934b2011-05-20 09:18:18 -04003057sub patchcheck_reboot {
3058 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003059 reboot_to_good $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04003060}
3061
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003062sub patchcheck {
3063 my ($i) = @_;
3064
3065 die "PATCHCHECK_START[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003066 if (!defined($patchcheck_start));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003067 die "PATCHCHECK_TYPE[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003068 if (!defined($patchcheck_type));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003069
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003070 my $start = $patchcheck_start;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003071
3072 my $end = "HEAD";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003073 if (defined($patchcheck_end)) {
3074 $end = $patchcheck_end;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003075 }
3076
Steven Rostedta57419b2010-11-02 15:13:54 -04003077 # Get the true sha1's since we can use things like HEAD~3
3078 $start = get_sha1($start);
3079 $end = get_sha1($end);
3080
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003081 my $type = $patchcheck_type;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003082
3083 # Can't have a test without having a test to run
3084 if ($type eq "test" && !defined($run_test)) {
3085 $type = "boot";
3086 }
3087
3088 open (IN, "git log --pretty=oneline $end|") or
3089 dodie "could not get git list";
3090
3091 my @list;
3092
3093 while (<IN>) {
3094 chomp;
3095 $list[$#list+1] = $_;
3096 last if (/^$start/);
3097 }
3098 close(IN);
3099
3100 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003101 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003102 }
3103
3104 # go backwards in the list
3105 @list = reverse @list;
3106
3107 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04003108 my %ignored_warnings;
3109
3110 if (defined($ignore_warnings)) {
3111 foreach my $sha1 (split /\s+/, $ignore_warnings) {
3112 $ignored_warnings{$sha1} = 1;
3113 }
3114 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003115
3116 $in_patchcheck = 1;
3117 foreach my $item (@list) {
3118 my $sha1 = $item;
3119 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
3120
3121 doprint "\nProcessing commit $item\n\n";
3122
3123 run_command "git checkout $sha1" or
3124 die "Failed to checkout $sha1";
3125
3126 # only clean on the first and last patch
3127 if ($item eq $list[0] ||
3128 $item eq $list[$#list]) {
3129 $noclean = $save_clean;
3130 } else {
3131 $noclean = 1;
3132 }
3133
3134 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003135 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003136 } else {
3137 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003138 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003139 }
3140
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05003141 # No need to do per patch checking if warnings file exists
3142 if (!defined($warnings_file) && !defined($ignored_warnings{$sha1})) {
3143 check_patch_buildlog $sha1 or return 0;
Steven Rostedt19902072011-06-14 20:46:25 -04003144 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003145
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05003146 check_buildlog or return 0;
3147
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003148 next if ($type eq "build");
3149
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003150 my $failed = 0;
3151
Steven Rostedtddf607e2011-06-14 20:49:13 -04003152 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003153
3154 if (!$failed && $type ne "boot"){
3155 do_run_test or $failed = 1;
3156 }
3157 end_monitor;
3158 return 0 if ($failed);
3159
Steven Rostedt27d934b2011-05-20 09:18:18 -04003160 patchcheck_reboot;
3161
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003162 }
3163 $in_patchcheck = 0;
3164 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003165
3166 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003167}
3168
Steven Rostedtb9066f62011-07-15 21:25:24 -04003169my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003170my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003171my $iflevel = 0;
3172my @ifdeps;
3173
3174# prevent recursion
3175my %read_kconfigs;
3176
Steven Rostedtac6974c2011-10-04 09:40:17 -04003177sub add_dep {
3178 # $config depends on $dep
3179 my ($config, $dep) = @_;
3180
3181 if (defined($depends{$config})) {
3182 $depends{$config} .= " " . $dep;
3183 } else {
3184 $depends{$config} = $dep;
3185 }
3186
3187 # record the number of configs depending on $dep
3188 if (defined $depcount{$dep}) {
3189 $depcount{$dep}++;
3190 } else {
3191 $depcount{$dep} = 1;
3192 }
3193}
3194
Steven Rostedtb9066f62011-07-15 21:25:24 -04003195# taken from streamline_config.pl
3196sub read_kconfig {
3197 my ($kconfig) = @_;
3198
3199 my $state = "NONE";
3200 my $config;
3201 my @kconfigs;
3202
3203 my $cont = 0;
3204 my $line;
3205
3206
3207 if (! -f $kconfig) {
3208 doprint "file $kconfig does not exist, skipping\n";
3209 return;
3210 }
3211
3212 open(KIN, "$kconfig")
3213 or die "Can't open $kconfig";
3214 while (<KIN>) {
3215 chomp;
3216
3217 # Make sure that lines ending with \ continue
3218 if ($cont) {
3219 $_ = $line . " " . $_;
3220 }
3221
3222 if (s/\\$//) {
3223 $cont = 1;
3224 $line = $_;
3225 next;
3226 }
3227
3228 $cont = 0;
3229
3230 # collect any Kconfig sources
3231 if (/^source\s*"(.*)"/) {
3232 $kconfigs[$#kconfigs+1] = $1;
3233 }
3234
3235 # configs found
3236 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
3237 $state = "NEW";
3238 $config = $2;
3239
3240 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04003241 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04003242 }
3243
3244 # collect the depends for the config
3245 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
3246
Steven Rostedtac6974c2011-10-04 09:40:17 -04003247 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003248
3249 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04003250 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
3251
3252 # selected by depends on config
3253 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003254
3255 # Check for if statements
3256 } elsif (/^if\s+(.*\S)\s*$/) {
3257 my $deps = $1;
3258 # remove beginning and ending non text
3259 $deps =~ s/^[^a-zA-Z0-9_]*//;
3260 $deps =~ s/[^a-zA-Z0-9_]*$//;
3261
3262 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
3263
3264 $ifdeps[$iflevel++] = join ':', @deps;
3265
3266 } elsif (/^endif/) {
3267
3268 $iflevel-- if ($iflevel);
3269
3270 # stop on "help"
3271 } elsif (/^\s*help\s*$/) {
3272 $state = "NONE";
3273 }
3274 }
3275 close(KIN);
3276
3277 # read in any configs that were found.
3278 foreach $kconfig (@kconfigs) {
3279 if (!defined($read_kconfigs{$kconfig})) {
3280 $read_kconfigs{$kconfig} = 1;
3281 read_kconfig("$builddir/$kconfig");
3282 }
3283 }
3284}
3285
3286sub read_depends {
3287 # find out which arch this is by the kconfig file
3288 open (IN, $output_config)
3289 or dodie "Failed to read $output_config";
3290 my $arch;
3291 while (<IN>) {
3292 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
3293 $arch = $1;
3294 last;
3295 }
3296 }
3297 close IN;
3298
3299 if (!defined($arch)) {
3300 doprint "Could not find arch from config file\n";
3301 doprint "no dependencies used\n";
3302 return;
3303 }
3304
3305 # arch is really the subarch, we need to know
3306 # what directory to look at.
3307 if ($arch eq "i386" || $arch eq "x86_64") {
3308 $arch = "x86";
3309 } elsif ($arch =~ /^tile/) {
3310 $arch = "tile";
3311 }
3312
3313 my $kconfig = "$builddir/arch/$arch/Kconfig";
3314
3315 if (! -f $kconfig && $arch =~ /\d$/) {
3316 my $orig = $arch;
3317 # some subarchs have numbers, truncate them
3318 $arch =~ s/\d*$//;
3319 $kconfig = "$builddir/arch/$arch/Kconfig";
3320 if (! -f $kconfig) {
3321 doprint "No idea what arch dir $orig is for\n";
3322 doprint "no dependencies used\n";
3323 return;
3324 }
3325 }
3326
3327 read_kconfig($kconfig);
3328}
3329
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003330sub read_config_list {
3331 my ($config) = @_;
3332
3333 open (IN, $config)
3334 or dodie "Failed to read $config";
3335
3336 while (<IN>) {
3337 if (/^((CONFIG\S*)=.*)/) {
3338 if (!defined($config_ignore{$2})) {
3339 $config_list{$2} = $1;
3340 }
3341 }
3342 }
3343
3344 close(IN);
3345}
3346
3347sub read_output_config {
3348 my ($config) = @_;
3349
3350 assign_configs \%config_ignore, $config;
3351}
3352
3353sub make_new_config {
3354 my @configs = @_;
3355
3356 open (OUT, ">$output_config")
3357 or dodie "Failed to write $output_config";
3358
3359 foreach my $config (@configs) {
3360 print OUT "$config\n";
3361 }
3362 close OUT;
3363}
3364
Steven Rostedtac6974c2011-10-04 09:40:17 -04003365sub chomp_config {
3366 my ($config) = @_;
3367
3368 $config =~ s/CONFIG_//;
3369
3370 return $config;
3371}
3372
Steven Rostedtb9066f62011-07-15 21:25:24 -04003373sub get_depends {
3374 my ($dep) = @_;
3375
Steven Rostedtac6974c2011-10-04 09:40:17 -04003376 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003377
3378 $dep = $depends{"$kconfig"};
3379
3380 # the dep string we have saves the dependencies as they
3381 # were found, including expressions like ! && ||. We
3382 # want to split this out into just an array of configs.
3383
3384 my $valid = "A-Za-z_0-9";
3385
3386 my @configs;
3387
3388 while ($dep =~ /[$valid]/) {
3389
3390 if ($dep =~ /^[^$valid]*([$valid]+)/) {
3391 my $conf = "CONFIG_" . $1;
3392
3393 $configs[$#configs + 1] = $conf;
3394
3395 $dep =~ s/^[^$valid]*[$valid]+//;
3396 } else {
3397 die "this should never happen";
3398 }
3399 }
3400
3401 return @configs;
3402}
3403
3404my %min_configs;
3405my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003406my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003407my %processed_configs;
3408my %nochange_config;
3409
3410sub test_this_config {
3411 my ($config) = @_;
3412
3413 my $found;
3414
3415 # if we already processed this config, skip it
3416 if (defined($processed_configs{$config})) {
3417 return undef;
3418 }
3419 $processed_configs{$config} = 1;
3420
3421 # if this config failed during this round, skip it
3422 if (defined($nochange_config{$config})) {
3423 return undef;
3424 }
3425
Steven Rostedtac6974c2011-10-04 09:40:17 -04003426 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003427
3428 # Test dependencies first
3429 if (defined($depends{"$kconfig"})) {
3430 my @parents = get_depends $config;
3431 foreach my $parent (@parents) {
3432 # if the parent is in the min config, check it first
3433 next if (!defined($min_configs{$parent}));
3434 $found = test_this_config($parent);
3435 if (defined($found)) {
3436 return $found;
3437 }
3438 }
3439 }
3440
3441 # Remove this config from the list of configs
Adam Leefb16d892012-09-01 01:05:17 +08003442 # do a make olddefconfig and then read the resulting
Steven Rostedtb9066f62011-07-15 21:25:24 -04003443 # .config to make sure it is missing the config that
3444 # we had before
3445 my %configs = %min_configs;
3446 delete $configs{$config};
3447 make_new_config ((values %configs), (values %keep_configs));
3448 make_oldconfig;
3449 undef %configs;
3450 assign_configs \%configs, $output_config;
3451
3452 return $config if (!defined($configs{$config}));
3453
3454 doprint "disabling config $config did not change .config\n";
3455
3456 $nochange_config{$config} = 1;
3457
3458 return undef;
3459}
3460
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003461sub make_min_config {
3462 my ($i) = @_;
3463
Steven Rostedtccc513b2012-05-21 17:13:40 -04003464 my $type = $minconfig_type;
3465 if ($type ne "boot" && $type ne "test") {
3466 fail "Invalid MIN_CONFIG_TYPE '$minconfig_type'\n" .
3467 " make_min_config works only with 'boot' and 'test'\n" and return;
3468 }
3469
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003470 if (!defined($output_minconfig)) {
3471 fail "OUTPUT_MIN_CONFIG not defined" and return;
3472 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04003473
3474 # If output_minconfig exists, and the start_minconfig
3475 # came from min_config, than ask if we should use
3476 # that instead.
3477 if (-f $output_minconfig && !$start_minconfig_defined) {
3478 print "$output_minconfig exists\n";
Steven Rostedt43de3312012-05-21 23:35:12 -04003479 if (!defined($use_output_minconfig)) {
3480 if (read_yn " Use it as minconfig?") {
3481 $start_minconfig = $output_minconfig;
3482 }
3483 } elsif ($use_output_minconfig > 0) {
3484 doprint "Using $output_minconfig as MIN_CONFIG\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003485 $start_minconfig = $output_minconfig;
Steven Rostedt43de3312012-05-21 23:35:12 -04003486 } else {
3487 doprint "Set to still use MIN_CONFIG as starting point\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003488 }
3489 }
3490
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003491 if (!defined($start_minconfig)) {
3492 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
3493 }
3494
Steven Rostedt35ce5952011-07-15 21:57:25 -04003495 my $temp_config = "$tmpdir/temp_config";
3496
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003497 # First things first. We build an allnoconfig to find
3498 # out what the defaults are that we can't touch.
3499 # Some are selections, but we really can't handle selections.
3500
3501 my $save_minconfig = $minconfig;
3502 undef $minconfig;
3503
3504 run_command "$make allnoconfig" or return 0;
3505
Steven Rostedtb9066f62011-07-15 21:25:24 -04003506 read_depends;
3507
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003508 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003509
Steven Rostedt43d1b652011-07-15 22:01:56 -04003510 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003511 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003512
3513 if (defined($ignore_config)) {
3514 # make sure the file exists
3515 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003516 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003517 }
3518
Steven Rostedt43d1b652011-07-15 22:01:56 -04003519 %keep_configs = %save_configs;
3520
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003521 doprint "Load initial configs from $start_minconfig\n";
3522
3523 # Look at the current min configs, and save off all the
3524 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003525 assign_configs \%min_configs, $start_minconfig;
3526
3527 my @config_keys = keys %min_configs;
3528
Steven Rostedtac6974c2011-10-04 09:40:17 -04003529 # All configs need a depcount
3530 foreach my $config (@config_keys) {
3531 my $kconfig = chomp_config $config;
3532 if (!defined $depcount{$kconfig}) {
3533 $depcount{$kconfig} = 0;
3534 }
3535 }
3536
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003537 # Remove anything that was set by the make allnoconfig
3538 # we shouldn't need them as they get set for us anyway.
3539 foreach my $config (@config_keys) {
3540 # Remove anything in the ignore_config
3541 if (defined($keep_configs{$config})) {
3542 my $file = $ignore_config;
3543 $file =~ s,.*/(.*?)$,$1,;
3544 doprint "$config set by $file ... ignored\n";
3545 delete $min_configs{$config};
3546 next;
3547 }
3548 # But make sure the settings are the same. If a min config
3549 # sets a selection, we do not want to get rid of it if
3550 # it is not the same as what we have. Just move it into
3551 # the keep configs.
3552 if (defined($config_ignore{$config})) {
3553 if ($config_ignore{$config} ne $min_configs{$config}) {
3554 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3555 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3556 $keep_configs{$config} = $min_configs{$config};
3557 } else {
3558 doprint "$config set by allnoconfig ... ignored\n";
3559 }
3560 delete $min_configs{$config};
3561 }
3562 }
3563
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003564 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003565 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003566
3567 while (!$done) {
3568
3569 my $config;
3570 my $found;
3571
3572 # Now disable each config one by one and do a make oldconfig
3573 # till we find a config that changes our list.
3574
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003575 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003576
3577 # Sort keys by who is most dependent on
3578 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3579 @test_configs ;
3580
3581 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003582 my $reset = 1;
3583 for (my $i = 0; $i < $#test_configs; $i++) {
3584 if (!defined($nochange_config{$test_configs[0]})) {
3585 $reset = 0;
3586 last;
3587 }
3588 # This config didn't change the .config last time.
3589 # Place it at the end
3590 my $config = shift @test_configs;
3591 push @test_configs, $config;
3592 }
3593
3594 # if every test config has failed to modify the .config file
3595 # in the past, then reset and start over.
3596 if ($reset) {
3597 undef %nochange_config;
3598 }
3599
Steven Rostedtb9066f62011-07-15 21:25:24 -04003600 undef %processed_configs;
3601
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003602 foreach my $config (@test_configs) {
3603
Steven Rostedtb9066f62011-07-15 21:25:24 -04003604 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003605
Steven Rostedtb9066f62011-07-15 21:25:24 -04003606 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003607
3608 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003609 }
3610
3611 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003612 # we could have failed due to the nochange_config hash
3613 # reset and try again
3614 if (!$take_two) {
3615 undef %nochange_config;
3616 $take_two = 1;
3617 next;
3618 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003619 doprint "No more configs found that we can disable\n";
3620 $done = 1;
3621 last;
3622 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003623 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003624
3625 $config = $found;
3626
3627 doprint "Test with $config disabled\n";
3628
3629 # set in_bisect to keep build and monitor from dieing
3630 $in_bisect = 1;
3631
3632 my $failed = 0;
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003633 build "oldconfig" or $failed = 1;
3634 if (!$failed) {
3635 start_monitor_and_boot or $failed = 1;
Steven Rostedtccc513b2012-05-21 17:13:40 -04003636
3637 if ($type eq "test" && !$failed) {
3638 do_run_test or $failed = 1;
3639 }
3640
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003641 end_monitor;
3642 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003643
3644 $in_bisect = 0;
3645
3646 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003647 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003648 # this config is needed, add it to the ignore list.
3649 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003650 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003651 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003652
3653 # update new ignore configs
3654 if (defined($ignore_config)) {
3655 open (OUT, ">$temp_config")
3656 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003657 foreach my $config (keys %save_configs) {
3658 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003659 }
3660 close OUT;
3661 run_command "mv $temp_config $ignore_config" or
3662 dodie "failed to copy update to $ignore_config";
3663 }
3664
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003665 } else {
3666 # We booted without this config, remove it from the minconfigs.
3667 doprint "$config is not needed, disabling\n";
3668
3669 delete $min_configs{$config};
3670
3671 # Also disable anything that is not enabled in this config
3672 my %configs;
3673 assign_configs \%configs, $output_config;
3674 my @config_keys = keys %min_configs;
3675 foreach my $config (@config_keys) {
3676 if (!defined($configs{$config})) {
3677 doprint "$config is not set, disabling\n";
3678 delete $min_configs{$config};
3679 }
3680 }
3681
3682 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003683 open (OUT, ">$temp_config")
3684 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003685 foreach my $config (keys %keep_configs) {
3686 print OUT "$keep_configs{$config}\n";
3687 }
3688 foreach my $config (keys %min_configs) {
3689 print OUT "$min_configs{$config}\n";
3690 }
3691 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003692
3693 run_command "mv $temp_config $output_minconfig" or
3694 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003695 }
3696
3697 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003698 reboot_to_good $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003699 }
3700
3701 success $i;
3702 return 1;
3703}
3704
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05003705sub make_warnings_file {
3706 my ($i) = @_;
3707
3708 if (!defined($warnings_file)) {
3709 dodie "Must define WARNINGS_FILE for make_warnings_file test";
3710 }
3711
3712 if ($build_type eq "nobuild") {
3713 dodie "BUILD_TYPE can not be 'nobuild' for make_warnings_file test";
3714 }
3715
3716 build $build_type or dodie "Failed to build";
3717
3718 open(OUT, ">$warnings_file") or dodie "Can't create $warnings_file";
3719
3720 open(IN, $buildlog) or dodie "Can't open $buildlog";
3721 while (<IN>) {
3722
3723 # Some compilers use UTF-8 extended for quotes
3724 # for distcc heterogeneous systems, this causes issues
3725 s/$utf8_quote/'/g;
3726
3727 if (/$check_build_re/) {
3728 print OUT;
3729 }
3730 }
3731 close(IN);
3732
3733 close(OUT);
3734
3735 success $i;
3736}
3737
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003738$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003739
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003740if ($#ARGV == 0) {
3741 $ktest_config = $ARGV[0];
3742 if (! -f $ktest_config) {
3743 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003744 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003745 exit 0;
3746 }
3747 }
3748} else {
3749 $ktest_config = "ktest.conf";
3750}
3751
3752if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003753 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003754 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003755 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3756 print OUT << "EOF"
3757# Generated by ktest.pl
3758#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003759
3760# PWD is a ktest.pl variable that will result in the process working
3761# directory that ktest.pl is executed in.
3762
3763# THIS_DIR is automatically assigned the PWD of the path that generated
3764# the config file. It is best to use this variable when assigning other
3765# directory paths within this directory. This allows you to easily
3766# move the test cases to other locations or to other machines.
3767#
3768THIS_DIR := $variable{"PWD"}
3769
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003770# Define each test with TEST_START
3771# The config options below it will override the defaults
3772TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003773TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003774
3775DEFAULTS
3776EOF
3777;
3778 close(OUT);
3779}
3780read_config $ktest_config;
3781
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003782if (defined($opt{"LOG_FILE"})) {
Steven Rostedt (Red Hat)04262be2013-01-31 10:12:20 -05003783 $opt{"LOG_FILE"} = eval_option("LOG_FILE", $opt{"LOG_FILE"}, -1);
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003784}
3785
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003786# Append any configs entered in manually to the config file.
3787my @new_configs = keys %entered_configs;
3788if ($#new_configs >= 0) {
3789 print "\nAppending entered in configs to $ktest_config\n";
3790 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3791 foreach my $config (@new_configs) {
3792 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003793 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003794 }
3795}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003796
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003797if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3798 unlink $opt{"LOG_FILE"};
3799}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003800
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003801doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3802
Steven Rostedta57419b2010-11-02 15:13:54 -04003803for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3804
3805 if (!$i) {
3806 doprint "DEFAULT OPTIONS:\n";
3807 } else {
3808 doprint "\nTEST $i OPTIONS";
3809 if (defined($repeat_tests{$i})) {
3810 $repeat = $repeat_tests{$i};
3811 doprint " ITERATE $repeat";
3812 }
3813 doprint "\n";
3814 }
3815
3816 foreach my $option (sort keys %opt) {
3817
3818 if ($option =~ /\[(\d+)\]$/) {
3819 next if ($i != $1);
3820 } else {
3821 next if ($i);
3822 }
3823
3824 doprint "$option = $opt{$option}\n";
3825 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003826}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003827
Steven Rostedt2a625122011-05-20 15:48:59 -04003828sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003829 my ($name, $i) = @_;
3830
3831 my $option = "$name\[$i\]";
3832
3833 if (defined($opt{$option})) {
3834 return $opt{$option};
3835 }
3836
Steven Rostedta57419b2010-11-02 15:13:54 -04003837 foreach my $test (keys %repeat_tests) {
3838 if ($i >= $test &&
3839 $i < $test + $repeat_tests{$test}) {
3840 $option = "$name\[$test\]";
3841 if (defined($opt{$option})) {
3842 return $opt{$option};
3843 }
3844 }
3845 }
3846
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003847 if (defined($opt{$name})) {
3848 return $opt{$name};
3849 }
3850
3851 return undef;
3852}
3853
Steven Rostedt2a625122011-05-20 15:48:59 -04003854sub set_test_option {
3855 my ($name, $i) = @_;
3856
3857 my $option = __set_test_option($name, $i);
3858 return $option if (!defined($option));
3859
Steven Rostedt (Red Hat)04262be2013-01-31 10:12:20 -05003860 return eval_option($name, $option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003861}
3862
Steven Rostedt2545eb62010-11-02 15:01:32 -04003863# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003864for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003865
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003866 # Do not reboot on failing test options
3867 $no_reboot = 1;
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003868 $reboot_success = 0;
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003869
Steven Rostedt683a3e62012-05-18 13:34:35 -04003870 $have_version = 0;
3871
Steven Rostedt576f6272010-11-02 14:58:38 -04003872 $iteration = $i;
3873
Steven Rostedtc1434dc2012-07-20 22:39:16 -04003874 undef %force_config;
3875
Steven Rostedta75fece2010-11-02 14:58:27 -04003876 my $makecmd = set_test_option("MAKE_CMD", $i);
3877
Steven Rostedt9cc9e092011-12-22 21:37:22 -05003878 # Load all the options into their mapped variable names
3879 foreach my $opt (keys %option_map) {
3880 ${$option_map{$opt}} = set_test_option($opt, $i);
3881 }
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003882
Steven Rostedt35ce5952011-07-15 21:57:25 -04003883 $start_minconfig_defined = 1;
3884
Steven Rostedt921ed4c2012-07-19 15:18:27 -04003885 # The first test may override the PRE_KTEST option
3886 if (defined($pre_ktest) && $i == 1) {
3887 doprint "\n";
3888 run_command $pre_ktest;
3889 }
3890
3891 # Any test can override the POST_KTEST option
3892 # The last test takes precedence.
3893 if (defined($post_ktest)) {
3894 $final_post_ktest = $post_ktest;
3895 }
3896
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003897 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003898 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003899 $start_minconfig = $minconfig;
3900 }
3901
Steven Rostedta75fece2010-11-02 14:58:27 -04003902 chdir $builddir || die "can't change directory to $builddir";
3903
Andrew Jonesa908a662011-08-12 15:32:03 +02003904 foreach my $dir ($tmpdir, $outputdir) {
3905 if (!-d $dir) {
3906 mkpath($dir) or
3907 die "can't create $dir";
3908 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003909 }
3910
Steven Rostedte48c5292010-11-02 14:35:37 -04003911 $ENV{"SSH_USER"} = $ssh_user;
3912 $ENV{"MACHINE"} = $machine;
3913
Steven Rostedta75fece2010-11-02 14:58:27 -04003914 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303915 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003916 $dmesg = "$tmpdir/dmesg-$machine";
3917 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003918 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003919
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003920 if (!$buildonly) {
3921 $target = "$ssh_user\@$machine";
3922 if ($reboot_type eq "grub") {
3923 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
Steven Rostedta15ba912012-11-13 14:30:37 -05003924 } elsif ($reboot_type eq "grub2") {
3925 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3926 dodie "GRUB_FILE not defined" if (!defined($grub_file));
Steven Rostedt77869542012-12-11 17:37:41 -05003927 } elsif ($reboot_type eq "syslinux") {
3928 dodie "SYSLINUX_LABEL not defined" if (!defined($syslinux_label));
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003929 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003930 }
3931
3932 my $run_type = $build_type;
3933 if ($test_type eq "patchcheck") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003934 $run_type = $patchcheck_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003935 } elsif ($test_type eq "bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003936 $run_type = $bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003937 } elsif ($test_type eq "config_bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003938 $run_type = $config_bisect_type;
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05003939 } elsif ($test_type eq "make_min_config") {
3940 $run_type = "";
3941 } elsif ($test_type eq "make_warnings_file") {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003942 $run_type = "";
3943 }
3944
Steven Rostedta75fece2010-11-02 14:58:27 -04003945 # mistake in config file?
3946 if (!defined($run_type)) {
3947 $run_type = "ERROR";
3948 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003949
Steven Rostedte0a87422011-09-30 17:50:48 -04003950 my $installme = "";
3951 $installme = " no_install" if ($no_install);
3952
Steven Rostedt2545eb62010-11-02 15:01:32 -04003953 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003954 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003955
Steven Rostedt921ed4c2012-07-19 15:18:27 -04003956 if (defined($pre_test)) {
3957 run_command $pre_test;
3958 }
3959
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003960 unlink $dmesg;
3961 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303962 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003963
Steven Rostedt250bae82011-07-15 22:05:59 -04003964 if (defined($addconfig)) {
3965 my $min = $minconfig;
3966 if (!defined($minconfig)) {
3967 $min = "";
3968 }
3969 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003970 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003971 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003972 }
3973
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003974 if (defined($checkout)) {
3975 run_command "git checkout $checkout" or
3976 die "failed to checkout $checkout";
3977 }
3978
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003979 $no_reboot = 0;
3980
Steven Rostedt648a1822012-03-21 11:18:27 -04003981 # A test may opt to not reboot the box
3982 if ($reboot_on_success) {
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003983 $reboot_success = 1;
Steven Rostedt648a1822012-03-21 11:18:27 -04003984 }
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003985
Steven Rostedta75fece2010-11-02 14:58:27 -04003986 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003987 bisect $i;
3988 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003989 } elsif ($test_type eq "config_bisect") {
3990 config_bisect $i;
3991 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003992 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003993 patchcheck $i;
3994 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003995 } elsif ($test_type eq "make_min_config") {
3996 make_min_config $i;
3997 next;
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05003998 } elsif ($test_type eq "make_warnings_file") {
3999 $no_reboot = 1;
4000 make_warnings_file $i;
4001 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04004002 }
4003
Steven Rostedt7faafbd2010-11-02 14:58:22 -04004004 if ($build_type ne "nobuild") {
4005 build $build_type or next;
Steven Rostedt (Red Hat)4283b162013-01-30 18:37:47 -05004006 check_buildlog or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04004007 }
4008
Steven Rostedtcd8e3682011-08-18 16:35:44 -04004009 if ($test_type eq "install") {
4010 get_version;
4011 install;
4012 success $i;
4013 next;
4014 }
4015
Steven Rostedta75fece2010-11-02 14:58:27 -04004016 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04004017 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04004018 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04004019
4020 if (!$failed && $test_type ne "boot" && defined($run_test)) {
4021 do_run_test or $failed = 1;
4022 }
4023 end_monitor;
4024 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04004025 }
4026
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04004027 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04004028}
4029
Steven Rostedt921ed4c2012-07-19 15:18:27 -04004030if (defined($final_post_ktest)) {
4031 run_command $final_post_ktest;
4032}
4033
Steven Rostedt5c42fc52010-11-02 14:57:01 -04004034if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04004035 halt;
Steven Rostedt759a3cc2012-05-01 08:20:12 -04004036} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot && $reboot_success) {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05004037 reboot_to_good;
Steven Rostedt648a1822012-03-21 11:18:27 -04004038} elsif (defined($switch_to_good)) {
4039 # still need to get to the good kernel
4040 run_command $switch_to_good;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04004041}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04004042
Steven Rostedt648a1822012-03-21 11:18:27 -04004043
Steven Rostedte48c5292010-11-02 14:35:37 -04004044doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
4045
Steven Rostedt2545eb62010-11-02 15:01:32 -04004046exit 0;