blob: d6690df5e7f1417b98d5f6a03830eff39ed3a80a [file] [log] [blame]
Steven Rostedt2545eb62010-11-02 15:01:32 -04001#!/usr/bin/perl -w
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002#
Uwe Kleine-Königcce1dac2011-01-24 21:12:01 +01003# Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04004# Licensed under the terms of the GNU GPL License version 2
5#
Steven Rostedt2545eb62010-11-02 15:01:32 -04006
7use strict;
8use IPC::Open2;
9use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Steven Rostedt7faafbd2010-11-02 14:58:22 -040010use File::Path qw(mkpath);
11use File::Copy qw(cp);
Steven Rostedt2545eb62010-11-02 15:01:32 -040012use FileHandle;
13
Steven Rostedte48c5292010-11-02 14:35:37 -040014my $VERSION = "0.2";
15
Steven Rostedt2545eb62010-11-02 15:01:32 -040016$| = 1;
17
18my %opt;
Steven Rostedta57419b2010-11-02 15:13:54 -040019my %repeat_tests;
20my %repeats;
Steven Rostedt2545eb62010-11-02 15:01:32 -040021
22#default opts
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050023my %default = (
24 "NUM_TESTS" => 1,
25 "TEST_TYPE" => "build",
26 "BUILD_TYPE" => "randconfig",
27 "MAKE_CMD" => "make",
28 "TIMEOUT" => 120,
29 "TMP_DIR" => "/tmp/ktest/\${MACHINE}",
30 "SLEEP_TIME" => 60, # sleep time between tests
31 "BUILD_NOCLEAN" => 0,
32 "REBOOT_ON_ERROR" => 0,
33 "POWEROFF_ON_ERROR" => 0,
34 "REBOOT_ON_SUCCESS" => 1,
35 "POWEROFF_ON_SUCCESS" => 0,
36 "BUILD_OPTIONS" => "",
37 "BISECT_SLEEP_TIME" => 60, # sleep time between bisects
38 "PATCHCHECK_SLEEP_TIME" => 60, # sleep time between patch checks
39 "CLEAR_LOG" => 0,
40 "BISECT_MANUAL" => 0,
41 "BISECT_SKIP" => 1,
Steven Rostedtccc513b2012-05-21 17:13:40 -040042 "MIN_CONFIG_TYPE" => "boot",
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050043 "SUCCESS_LINE" => "login:",
44 "DETECT_TRIPLE_FAULT" => 1,
45 "NO_INSTALL" => 0,
46 "BOOTED_TIMEOUT" => 1,
47 "DIE_ON_FAILURE" => 1,
48 "SSH_EXEC" => "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND",
49 "SCP_TO_TARGET" => "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE",
Steven Rostedt02ad2612012-03-21 08:21:24 -040050 "SCP_TO_TARGET_INSTALL" => "\${SCP_TO_TARGET}",
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050051 "REBOOT" => "ssh \$SSH_USER\@\$MACHINE reboot",
52 "STOP_AFTER_SUCCESS" => 10,
53 "STOP_AFTER_FAILURE" => 60,
54 "STOP_TEST_AFTER" => 600,
Steven Rostedt407b95b2012-07-19 16:05:42 -040055 "MAX_MONITOR_WAIT" => 1800,
Steven Rostedta15ba912012-11-13 14:30:37 -050056 "GRUB_REBOOT" => "grub2-reboot",
Steven Rostedt77869542012-12-11 17:37:41 -050057 "SYSLINUX" => "extlinux",
58 "SYSLINUX_PATH" => "/boot/extlinux",
Steven Rostedt600bbf02011-11-21 20:12:04 -050059
60# required, and we will ask users if they don't have them but we keep the default
61# value something that is common.
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050062 "REBOOT_TYPE" => "grub",
63 "LOCALVERSION" => "-test",
64 "SSH_USER" => "root",
65 "BUILD_TARGET" => "arch/x86/boot/bzImage",
66 "TARGET_IMAGE" => "/boot/vmlinuz-test",
Steven Rostedt9cc9e092011-12-22 21:37:22 -050067
68 "LOG_FILE" => undef,
69 "IGNORE_UNUSED" => 0,
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050070);
Steven Rostedt2545eb62010-11-02 15:01:32 -040071
Steven Rostedt8d1491b2010-11-18 15:39:48 -050072my $ktest_config;
Steven Rostedt2545eb62010-11-02 15:01:32 -040073my $version;
Steven Rostedt683a3e62012-05-18 13:34:35 -040074my $have_version = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -040075my $machine;
Steven Rostedte48c5292010-11-02 14:35:37 -040076my $ssh_user;
Steven Rostedta75fece2010-11-02 14:58:27 -040077my $tmpdir;
78my $builddir;
79my $outputdir;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -050080my $output_config;
Steven Rostedta75fece2010-11-02 14:58:27 -040081my $test_type;
Steven Rostedt7faafbd2010-11-02 14:58:22 -040082my $build_type;
Steven Rostedta75fece2010-11-02 14:58:27 -040083my $build_options;
Steven Rostedt921ed4c2012-07-19 15:18:27 -040084my $final_post_ktest;
85my $pre_ktest;
86my $post_ktest;
87my $pre_test;
88my $post_test;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -040089my $pre_build;
90my $post_build;
91my $pre_build_die;
92my $post_build_die;
Steven Rostedta75fece2010-11-02 14:58:27 -040093my $reboot_type;
94my $reboot_script;
95my $power_cycle;
Steven Rostedte48c5292010-11-02 14:35:37 -040096my $reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -040097my $reboot_on_error;
Steven Rostedtbc7c5802011-12-22 16:29:10 -050098my $switch_to_good;
99my $switch_to_test;
Steven Rostedta75fece2010-11-02 14:58:27 -0400100my $poweroff_on_error;
Steven Rostedt648a1822012-03-21 11:18:27 -0400101my $reboot_on_success;
Steven Rostedta75fece2010-11-02 14:58:27 -0400102my $die_on_failure;
Steven Rostedt576f6272010-11-02 14:58:38 -0400103my $powercycle_after_reboot;
104my $poweroff_after_halt;
Steven Rostedt407b95b2012-07-19 16:05:42 -0400105my $max_monitor_wait;
Steven Rostedte48c5292010-11-02 14:35:37 -0400106my $ssh_exec;
107my $scp_to_target;
Steven Rostedt02ad2612012-03-21 08:21:24 -0400108my $scp_to_target_install;
Steven Rostedta75fece2010-11-02 14:58:27 -0400109my $power_off;
110my $grub_menu;
Steven Rostedta15ba912012-11-13 14:30:37 -0500111my $grub_file;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400112my $grub_number;
Steven Rostedta15ba912012-11-13 14:30:37 -0500113my $grub_reboot;
Steven Rostedt77869542012-12-11 17:37:41 -0500114my $syslinux;
115my $syslinux_path;
116my $syslinux_label;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400117my $target;
118my $make;
Steven Rostedte5c2ec12012-07-19 15:22:05 -0400119my $pre_install;
Steven Rostedt8b37ca82010-11-02 14:58:33 -0400120my $post_install;
Steven Rostedte0a87422011-09-30 17:50:48 -0400121my $no_install;
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400122my $noclean;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400123my $minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400124my $start_minconfig;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400125my $start_minconfig_defined;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400126my $output_minconfig;
Steven Rostedtccc513b2012-05-21 17:13:40 -0400127my $minconfig_type;
Steven Rostedt43de3312012-05-21 23:35:12 -0400128my $use_output_minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400129my $ignore_config;
Steven Rostedtbe405f92012-01-04 21:51:59 -0500130my $ignore_errors;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -0400131my $addconfig;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400132my $in_bisect = 0;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500133my $bisect_bad_commit = "";
Steven Rostedtd6ce2a02010-11-02 14:58:05 -0400134my $reverse_bisect;
Steven Rostedtc960bb92011-03-08 09:22:39 -0500135my $bisect_manual;
Steven Rostedtc23dca72011-03-08 09:26:31 -0500136my $bisect_skip;
Steven Rostedt30f75da2011-06-13 10:35:35 -0400137my $config_bisect_good;
Steven Rostedtc5dacb82011-12-22 12:43:57 -0500138my $bisect_ret_good;
139my $bisect_ret_bad;
140my $bisect_ret_skip;
141my $bisect_ret_abort;
142my $bisect_ret_default;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400143my $in_patchcheck = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400144my $run_test;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400145my $redirect;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400146my $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +0530147my $testlog;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400148my $dmesg;
149my $monitor_fp;
150my $monitor_pid;
151my $monitor_cnt = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -0400152my $sleep_time;
153my $bisect_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -0400154my $patchcheck_sleep_time;
Steven Rostedt19902072011-06-14 20:46:25 -0400155my $ignore_warnings;
Steven Rostedta75fece2010-11-02 14:58:27 -0400156my $store_failures;
Rabin Vincentde5b6e32011-11-18 17:05:31 +0530157my $store_successes;
Steven Rostedt9064af52011-06-13 10:38:48 -0400158my $test_name;
Steven Rostedta75fece2010-11-02 14:58:27 -0400159my $timeout;
160my $booted_timeout;
Steven Rostedtf1a5b962011-06-13 10:30:00 -0400161my $detect_triplefault;
Steven Rostedta75fece2010-11-02 14:58:27 -0400162my $console;
Steven Rostedt2b803362011-09-30 18:00:23 -0400163my $reboot_success_line;
Steven Rostedta75fece2010-11-02 14:58:27 -0400164my $success_line;
Steven Rostedt1c8a6172010-11-09 12:55:40 -0500165my $stop_after_success;
166my $stop_after_failure;
Steven Rostedt2d01b262011-03-08 09:47:54 -0500167my $stop_test_after;
Steven Rostedta75fece2010-11-02 14:58:27 -0400168my $build_target;
169my $target_image;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500170my $checkout;
Steven Rostedta75fece2010-11-02 14:58:27 -0400171my $localversion;
Steven Rostedt576f6272010-11-02 14:58:38 -0400172my $iteration = 0;
Steven Rostedte48c5292010-11-02 14:35:37 -0400173my $successes = 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400174
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500175my $bisect_good;
176my $bisect_bad;
177my $bisect_type;
178my $bisect_start;
179my $bisect_replay;
180my $bisect_files;
181my $bisect_reverse;
182my $bisect_check;
183
184my $config_bisect;
185my $config_bisect_type;
Steven Rostedtb0918612012-07-19 15:26:00 -0400186my $config_bisect_check;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500187
188my $patchcheck_type;
189my $patchcheck_start;
190my $patchcheck_end;
191
Steven Rostedt165708b2011-11-26 20:56:52 -0500192# set when a test is something other that just building or install
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500193# which would require more options.
194my $buildonly = 1;
195
Steven Rostedtdbd37832011-11-23 16:00:48 -0500196# set when creating a new config
197my $newconfig = 0;
198
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500199my %entered_configs;
200my %config_help;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400201my %variable;
Steven Rostedtcf79fab2012-07-19 15:29:43 -0400202
203# force_config is the list of configs that we force enabled (or disabled)
204# in a .config file. The MIN_CONFIG and ADD_CONFIG configs.
Steven Rostedtfcb3f162011-06-13 10:40:58 -0400205my %force_config;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500206
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400207# do not force reboots on config problems
208my $no_reboot = 1;
209
Steven Rostedt759a3cc2012-05-01 08:20:12 -0400210# reboot on success
211my $reboot_success = 0;
212
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500213my %option_map = (
214 "MACHINE" => \$machine,
215 "SSH_USER" => \$ssh_user,
216 "TMP_DIR" => \$tmpdir,
217 "OUTPUT_DIR" => \$outputdir,
218 "BUILD_DIR" => \$builddir,
219 "TEST_TYPE" => \$test_type,
Steven Rostedt921ed4c2012-07-19 15:18:27 -0400220 "PRE_KTEST" => \$pre_ktest,
221 "POST_KTEST" => \$post_ktest,
222 "PRE_TEST" => \$pre_test,
223 "POST_TEST" => \$post_test,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500224 "BUILD_TYPE" => \$build_type,
225 "BUILD_OPTIONS" => \$build_options,
226 "PRE_BUILD" => \$pre_build,
227 "POST_BUILD" => \$post_build,
228 "PRE_BUILD_DIE" => \$pre_build_die,
229 "POST_BUILD_DIE" => \$post_build_die,
230 "POWER_CYCLE" => \$power_cycle,
231 "REBOOT" => \$reboot,
232 "BUILD_NOCLEAN" => \$noclean,
233 "MIN_CONFIG" => \$minconfig,
234 "OUTPUT_MIN_CONFIG" => \$output_minconfig,
235 "START_MIN_CONFIG" => \$start_minconfig,
Steven Rostedtccc513b2012-05-21 17:13:40 -0400236 "MIN_CONFIG_TYPE" => \$minconfig_type,
Steven Rostedt43de3312012-05-21 23:35:12 -0400237 "USE_OUTPUT_MIN_CONFIG" => \$use_output_minconfig,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500238 "IGNORE_CONFIG" => \$ignore_config,
239 "TEST" => \$run_test,
240 "ADD_CONFIG" => \$addconfig,
241 "REBOOT_TYPE" => \$reboot_type,
242 "GRUB_MENU" => \$grub_menu,
Steven Rostedta15ba912012-11-13 14:30:37 -0500243 "GRUB_FILE" => \$grub_file,
244 "GRUB_REBOOT" => \$grub_reboot,
Steven Rostedt77869542012-12-11 17:37:41 -0500245 "SYSLINUX" => \$syslinux,
246 "SYSLINUX_PATH" => \$syslinux_path,
247 "SYSLINUX_LABEL" => \$syslinux_label,
Steven Rostedte5c2ec12012-07-19 15:22:05 -0400248 "PRE_INSTALL" => \$pre_install,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500249 "POST_INSTALL" => \$post_install,
250 "NO_INSTALL" => \$no_install,
251 "REBOOT_SCRIPT" => \$reboot_script,
252 "REBOOT_ON_ERROR" => \$reboot_on_error,
253 "SWITCH_TO_GOOD" => \$switch_to_good,
254 "SWITCH_TO_TEST" => \$switch_to_test,
255 "POWEROFF_ON_ERROR" => \$poweroff_on_error,
Steven Rostedt648a1822012-03-21 11:18:27 -0400256 "REBOOT_ON_SUCCESS" => \$reboot_on_success,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500257 "DIE_ON_FAILURE" => \$die_on_failure,
258 "POWER_OFF" => \$power_off,
259 "POWERCYCLE_AFTER_REBOOT" => \$powercycle_after_reboot,
260 "POWEROFF_AFTER_HALT" => \$poweroff_after_halt,
Steven Rostedt407b95b2012-07-19 16:05:42 -0400261 "MAX_MONITOR_WAIT" => \$max_monitor_wait,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500262 "SLEEP_TIME" => \$sleep_time,
263 "BISECT_SLEEP_TIME" => \$bisect_sleep_time,
264 "PATCHCHECK_SLEEP_TIME" => \$patchcheck_sleep_time,
265 "IGNORE_WARNINGS" => \$ignore_warnings,
Steven Rostedtbe405f92012-01-04 21:51:59 -0500266 "IGNORE_ERRORS" => \$ignore_errors,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500267 "BISECT_MANUAL" => \$bisect_manual,
268 "BISECT_SKIP" => \$bisect_skip,
269 "CONFIG_BISECT_GOOD" => \$config_bisect_good,
270 "BISECT_RET_GOOD" => \$bisect_ret_good,
271 "BISECT_RET_BAD" => \$bisect_ret_bad,
272 "BISECT_RET_SKIP" => \$bisect_ret_skip,
273 "BISECT_RET_ABORT" => \$bisect_ret_abort,
274 "BISECT_RET_DEFAULT" => \$bisect_ret_default,
275 "STORE_FAILURES" => \$store_failures,
276 "STORE_SUCCESSES" => \$store_successes,
277 "TEST_NAME" => \$test_name,
278 "TIMEOUT" => \$timeout,
279 "BOOTED_TIMEOUT" => \$booted_timeout,
280 "CONSOLE" => \$console,
281 "DETECT_TRIPLE_FAULT" => \$detect_triplefault,
282 "SUCCESS_LINE" => \$success_line,
283 "REBOOT_SUCCESS_LINE" => \$reboot_success_line,
284 "STOP_AFTER_SUCCESS" => \$stop_after_success,
285 "STOP_AFTER_FAILURE" => \$stop_after_failure,
286 "STOP_TEST_AFTER" => \$stop_test_after,
287 "BUILD_TARGET" => \$build_target,
288 "SSH_EXEC" => \$ssh_exec,
289 "SCP_TO_TARGET" => \$scp_to_target,
Steven Rostedt02ad2612012-03-21 08:21:24 -0400290 "SCP_TO_TARGET_INSTALL" => \$scp_to_target_install,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500291 "CHECKOUT" => \$checkout,
292 "TARGET_IMAGE" => \$target_image,
293 "LOCALVERSION" => \$localversion,
294
295 "BISECT_GOOD" => \$bisect_good,
296 "BISECT_BAD" => \$bisect_bad,
297 "BISECT_TYPE" => \$bisect_type,
298 "BISECT_START" => \$bisect_start,
299 "BISECT_REPLAY" => \$bisect_replay,
300 "BISECT_FILES" => \$bisect_files,
301 "BISECT_REVERSE" => \$bisect_reverse,
302 "BISECT_CHECK" => \$bisect_check,
303
304 "CONFIG_BISECT" => \$config_bisect,
305 "CONFIG_BISECT_TYPE" => \$config_bisect_type,
Steven Rostedtb0918612012-07-19 15:26:00 -0400306 "CONFIG_BISECT_CHECK" => \$config_bisect_check,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500307
308 "PATCHCHECK_TYPE" => \$patchcheck_type,
309 "PATCHCHECK_START" => \$patchcheck_start,
310 "PATCHCHECK_END" => \$patchcheck_end,
311);
312
313# Options may be used by other options, record them.
314my %used_options;
315
Steven Rostedt7bf51072011-10-22 09:07:03 -0400316# default variables that can be used
317chomp ($variable{"PWD"} = `pwd`);
318
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500319$config_help{"MACHINE"} = << "EOF"
320 The machine hostname that you will test.
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500321 For build only tests, it is still needed to differentiate log files.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500322EOF
323 ;
324$config_help{"SSH_USER"} = << "EOF"
325 The box is expected to have ssh on normal bootup, provide the user
326 (most likely root, since you need privileged operations)
327EOF
328 ;
329$config_help{"BUILD_DIR"} = << "EOF"
330 The directory that contains the Linux source code (full path).
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500331 You can use \${PWD} that will be the path where ktest.pl is run, or use
332 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500333EOF
334 ;
335$config_help{"OUTPUT_DIR"} = << "EOF"
336 The directory that the objects will be built (full path).
337 (can not be same as BUILD_DIR)
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500338 You can use \${PWD} that will be the path where ktest.pl is run, or use
339 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500340EOF
341 ;
342$config_help{"BUILD_TARGET"} = << "EOF"
343 The location of the compiled file to copy to the target.
344 (relative to OUTPUT_DIR)
345EOF
346 ;
Steven Rostedtdbd37832011-11-23 16:00:48 -0500347$config_help{"BUILD_OPTIONS"} = << "EOF"
348 Options to add to \"make\" when building.
349 i.e. -j20
350EOF
351 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500352$config_help{"TARGET_IMAGE"} = << "EOF"
353 The place to put your image on the test machine.
354EOF
355 ;
356$config_help{"POWER_CYCLE"} = << "EOF"
357 A script or command to reboot the box.
358
359 Here is a digital loggers power switch example
360 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
361
362 Here is an example to reboot a virtual box on the current host
363 with the name "Guest".
364 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
365EOF
366 ;
367$config_help{"CONSOLE"} = << "EOF"
368 The script or command that reads the console
369
370 If you use ttywatch server, something like the following would work.
371CONSOLE = nc -d localhost 3001
372
373 For a virtual machine with guest name "Guest".
374CONSOLE = virsh console Guest
375EOF
376 ;
377$config_help{"LOCALVERSION"} = << "EOF"
378 Required version ending to differentiate the test
379 from other linux builds on the system.
380EOF
381 ;
382$config_help{"REBOOT_TYPE"} = << "EOF"
383 Way to reboot the box to the test kernel.
Steven Rostedt77869542012-12-11 17:37:41 -0500384 Only valid options so far are "grub", "grub2", "syslinux", and "script".
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500385
386 If you specify grub, it will assume grub version 1
387 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
388 and select that target to reboot to the kernel. If this is not
389 your setup, then specify "script" and have a command or script
390 specified in REBOOT_SCRIPT to boot to the target.
391
392 The entry in /boot/grub/menu.lst must be entered in manually.
393 The test will not modify that file.
Steven Rostedta15ba912012-11-13 14:30:37 -0500394
395 If you specify grub2, then you also need to specify both \$GRUB_MENU
396 and \$GRUB_FILE.
Steven Rostedt77869542012-12-11 17:37:41 -0500397
398 If you specify syslinux, then you may use SYSLINUX to define the syslinux
399 command (defaults to extlinux), and SYSLINUX_PATH to specify the path to
400 the syslinux install (defaults to /boot/extlinux). But you have to specify
401 SYSLINUX_LABEL to define the label to boot to for the test kernel.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500402EOF
403 ;
404$config_help{"GRUB_MENU"} = << "EOF"
405 The grub title name for the test kernel to boot
Steven Rostedta15ba912012-11-13 14:30:37 -0500406 (Only mandatory if REBOOT_TYPE = grub or grub2)
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500407
408 Note, ktest.pl will not update the grub menu.lst, you need to
409 manually add an option for the test. ktest.pl will search
410 the grub menu.lst for this option to find what kernel to
411 reboot into.
412
413 For example, if in the /boot/grub/menu.lst the test kernel title has:
414 title Test Kernel
415 kernel vmlinuz-test
416 GRUB_MENU = Test Kernel
Steven Rostedta15ba912012-11-13 14:30:37 -0500417
418 For grub2, a search of \$GRUB_FILE is performed for the lines
419 that begin with "menuentry". It will not detect submenus. The
420 menu must be a non-nested menu. Add the quotes used in the menu
421 to guarantee your selection, as the first menuentry with the content
422 of \$GRUB_MENU that is found will be used.
423EOF
424 ;
425$config_help{"GRUB_FILE"} = << "EOF"
426 If grub2 is used, the full path for the grub.cfg file is placed
427 here. Use something like /boot/grub2/grub.cfg to search.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500428EOF
429 ;
Steven Rostedt77869542012-12-11 17:37:41 -0500430$config_help{"SYSLINUX_LABEL"} = << "EOF"
431 If syslinux is used, the label that boots the target kernel must
432 be specified with SYSLINUX_LABEL.
433EOF
434 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500435$config_help{"REBOOT_SCRIPT"} = << "EOF"
436 A script to reboot the target into the test kernel
437 (Only mandatory if REBOOT_TYPE = script)
438EOF
439 ;
440
Steven Rostedtdad98752011-11-22 20:48:57 -0500441sub read_prompt {
442 my ($cancel, $prompt) = @_;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400443
444 my $ans;
445
446 for (;;) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500447 if ($cancel) {
448 print "$prompt [y/n/C] ";
449 } else {
450 print "$prompt [Y/n] ";
451 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400452 $ans = <STDIN>;
453 chomp $ans;
454 if ($ans =~ /^\s*$/) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500455 if ($cancel) {
456 $ans = "c";
457 } else {
458 $ans = "y";
459 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400460 }
461 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
Steven Rostedtdad98752011-11-22 20:48:57 -0500462 if ($cancel) {
463 last if ($ans =~ /^c$/i);
464 print "Please answer either 'y', 'n' or 'c'.\n";
465 } else {
466 print "Please answer either 'y' or 'n'.\n";
467 }
468 }
469 if ($ans =~ /^c/i) {
470 exit;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400471 }
472 if ($ans !~ /^y$/i) {
473 return 0;
474 }
475 return 1;
476}
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500477
Steven Rostedtdad98752011-11-22 20:48:57 -0500478sub read_yn {
479 my ($prompt) = @_;
480
481 return read_prompt 0, $prompt;
482}
483
484sub read_ync {
485 my ($prompt) = @_;
486
487 return read_prompt 1, $prompt;
488}
489
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500490sub get_ktest_config {
491 my ($config) = @_;
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400492 my $ans;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500493
494 return if (defined($opt{$config}));
495
496 if (defined($config_help{$config})) {
497 print "\n";
498 print $config_help{$config};
499 }
500
501 for (;;) {
502 print "$config = ";
Steven Rostedtdbd37832011-11-23 16:00:48 -0500503 if (defined($default{$config}) && length($default{$config})) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500504 print "\[$default{$config}\] ";
505 }
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400506 $ans = <STDIN>;
507 $ans =~ s/^\s*(.*\S)\s*$/$1/;
508 if ($ans =~ /^\s*$/) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500509 if ($default{$config}) {
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400510 $ans = $default{$config};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500511 } else {
512 print "Your answer can not be blank\n";
513 next;
514 }
515 }
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500516 $entered_configs{$config} = ${ans};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500517 last;
518 }
519}
520
521sub get_ktest_configs {
522 get_ktest_config("MACHINE");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500523 get_ktest_config("BUILD_DIR");
524 get_ktest_config("OUTPUT_DIR");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500525
Steven Rostedtdbd37832011-11-23 16:00:48 -0500526 if ($newconfig) {
527 get_ktest_config("BUILD_OPTIONS");
528 }
529
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500530 # options required for other than just building a kernel
531 if (!$buildonly) {
Steven Rostedt165708b2011-11-26 20:56:52 -0500532 get_ktest_config("POWER_CYCLE");
533 get_ktest_config("CONSOLE");
534 }
535
536 # options required for install and more
537 if ($buildonly != 1) {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500538 get_ktest_config("SSH_USER");
539 get_ktest_config("BUILD_TARGET");
540 get_ktest_config("TARGET_IMAGE");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500541 }
542
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500543 get_ktest_config("LOCALVERSION");
544
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500545 return if ($buildonly);
546
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500547 my $rtype = $opt{"REBOOT_TYPE"};
548
549 if (!defined($rtype)) {
550 if (!defined($opt{"GRUB_MENU"})) {
551 get_ktest_config("REBOOT_TYPE");
552 $rtype = $entered_configs{"REBOOT_TYPE"};
553 } else {
554 $rtype = "grub";
555 }
556 }
557
558 if ($rtype eq "grub") {
559 get_ktest_config("GRUB_MENU");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500560 }
Steven Rostedta15ba912012-11-13 14:30:37 -0500561
562 if ($rtype eq "grub2") {
563 get_ktest_config("GRUB_MENU");
564 get_ktest_config("GRUB_FILE");
565 }
Steven Rostedt77869542012-12-11 17:37:41 -0500566
567 if ($rtype eq "syslinux") {
568 get_ktest_config("SYSLINUX_LABEL");
569 }
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500570}
571
Steven Rostedt77d942c2011-05-20 13:36:58 -0400572sub process_variables {
Steven Rostedt8d735212011-10-17 11:36:44 -0400573 my ($value, $remove_undef) = @_;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400574 my $retval = "";
575
576 # We want to check for '\', and it is just easier
577 # to check the previous characet of '$' and not need
578 # to worry if '$' is the first character. By adding
579 # a space to $value, we can just check [^\\]\$ and
580 # it will still work.
581 $value = " $value";
582
583 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
584 my $begin = $1;
585 my $var = $2;
586 my $end = $3;
587 # append beginning of value to retval
588 $retval = "$retval$begin";
589 if (defined($variable{$var})) {
590 $retval = "$retval$variable{$var}";
Steven Rostedt8d735212011-10-17 11:36:44 -0400591 } elsif (defined($remove_undef) && $remove_undef) {
592 # for if statements, any variable that is not defined,
593 # we simple convert to 0
594 $retval = "${retval}0";
Steven Rostedt77d942c2011-05-20 13:36:58 -0400595 } else {
596 # put back the origin piece.
597 $retval = "$retval\$\{$var\}";
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500598 # This could be an option that is used later, save
599 # it so we don't warn if this option is not one of
600 # ktests options.
601 $used_options{$var} = 1;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400602 }
603 $value = $end;
604 }
605 $retval = "$retval$value";
606
607 # remove the space added in the beginning
608 $retval =~ s/ //;
609
610 return "$retval"
611}
612
Steven Rostedta57419b2010-11-02 15:13:54 -0400613sub set_value {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400614 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
Steven Rostedta57419b2010-11-02 15:13:54 -0400615
Steven Rostedtcad96662011-12-22 11:32:52 -0500616 my $prvalue = process_variables($rvalue);
617
618 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500619 # Note if a test is something other than build, then we
620 # will need other manditory options.
Steven Rostedtcad96662011-12-22 11:32:52 -0500621 if ($prvalue ne "install") {
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -0500622 # for bisect, we need to check BISECT_TYPE
623 if ($prvalue ne "bisect") {
624 $buildonly = 0;
625 }
626 } else {
627 # install still limits some manditory options.
628 $buildonly = 2;
629 }
630 }
631
632 if ($buildonly && $lvalue =~ /^BISECT_TYPE(\[.*\])?$/ && $prvalue ne "build") {
633 if ($prvalue ne "install") {
Steven Rostedt165708b2011-11-26 20:56:52 -0500634 $buildonly = 0;
635 } else {
636 # install still limits some manditory options.
637 $buildonly = 2;
638 }
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500639 }
640
Steven Rostedta57419b2010-11-02 15:13:54 -0400641 if (defined($opt{$lvalue})) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400642 if (!$override || defined(${$overrides}{$lvalue})) {
643 my $extra = "";
644 if ($override) {
645 $extra = "In the same override section!\n";
646 }
647 die "$name: $.: Option $lvalue defined more than once!\n$extra";
648 }
Steven Rostedtcad96662011-12-22 11:32:52 -0500649 ${$overrides}{$lvalue} = $prvalue;
Steven Rostedta57419b2010-11-02 15:13:54 -0400650 }
Steven Rostedt21a96792010-11-08 16:45:50 -0500651 if ($rvalue =~ /^\s*$/) {
652 delete $opt{$lvalue};
653 } else {
Steven Rostedtcad96662011-12-22 11:32:52 -0500654 $opt{$lvalue} = $prvalue;
Steven Rostedt21a96792010-11-08 16:45:50 -0500655 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400656}
657
Steven Rostedt77d942c2011-05-20 13:36:58 -0400658sub set_variable {
659 my ($lvalue, $rvalue) = @_;
660
661 if ($rvalue =~ /^\s*$/) {
662 delete $variable{$lvalue};
663 } else {
664 $rvalue = process_variables($rvalue);
665 $variable{$lvalue} = $rvalue;
666 }
667}
668
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400669sub process_compare {
670 my ($lval, $cmp, $rval) = @_;
671
672 # remove whitespace
673
674 $lval =~ s/^\s*//;
675 $lval =~ s/\s*$//;
676
677 $rval =~ s/^\s*//;
678 $rval =~ s/\s*$//;
679
680 if ($cmp eq "==") {
681 return $lval eq $rval;
682 } elsif ($cmp eq "!=") {
683 return $lval ne $rval;
Steven Rostedt8fddbe92012-07-30 14:37:01 -0400684 } elsif ($cmp eq "=~") {
685 return $lval =~ m/$rval/;
686 } elsif ($cmp eq "!~") {
687 return $lval !~ m/$rval/;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400688 }
689
690 my $statement = "$lval $cmp $rval";
691 my $ret = eval $statement;
692
693 # $@ stores error of eval
694 if ($@) {
695 return -1;
696 }
697
698 return $ret;
699}
700
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400701sub value_defined {
702 my ($val) = @_;
703
704 return defined($variable{$2}) ||
705 defined($opt{$2});
706}
707
Steven Rostedt8d735212011-10-17 11:36:44 -0400708my $d = 0;
709sub process_expression {
710 my ($name, $val) = @_;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400711
Steven Rostedt8d735212011-10-17 11:36:44 -0400712 my $c = $d++;
713
714 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
715 my $express = $1;
716
717 if (process_expression($name, $express)) {
718 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
719 } else {
720 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
721 }
722 }
723
724 $d--;
725 my $OR = "\\|\\|";
726 my $AND = "\\&\\&";
727
728 while ($val =~ s/^(.*?)($OR|$AND)//) {
729 my $express = $1;
730 my $op = $2;
731
732 if (process_expression($name, $express)) {
733 if ($op eq "||") {
734 return 1;
735 }
736 } else {
737 if ($op eq "&&") {
738 return 0;
739 }
740 }
741 }
Steven Rostedt45d73a52011-09-30 19:44:53 -0400742
Steven Rostedt8fddbe92012-07-30 14:37:01 -0400743 if ($val =~ /(.*)(==|\!=|>=|<=|>|<|=~|\!~)(.*)/) {
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400744 my $ret = process_compare($1, $2, $3);
745 if ($ret < 0) {
746 die "$name: $.: Unable to process comparison\n";
747 }
748 return $ret;
749 }
750
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400751 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
752 if (defined $1) {
753 return !value_defined($2);
754 } else {
755 return value_defined($2);
756 }
757 }
758
Steven Rostedt45d73a52011-09-30 19:44:53 -0400759 if ($val =~ /^\s*0\s*$/) {
760 return 0;
761 } elsif ($val =~ /^\s*\d+\s*$/) {
762 return 1;
763 }
764
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400765 die ("$name: $.: Undefined content $val in if statement\n");
Steven Rostedt8d735212011-10-17 11:36:44 -0400766}
767
768sub process_if {
769 my ($name, $value) = @_;
770
771 # Convert variables and replace undefined ones with 0
772 my $val = process_variables($value, 1);
773 my $ret = process_expression $name, $val;
774
775 return $ret;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400776}
777
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400778sub __read_config {
779 my ($config, $current_test_num) = @_;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400780
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400781 my $in;
782 open($in, $config) || die "can't read file $config";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400783
Steven Rostedta57419b2010-11-02 15:13:54 -0400784 my $name = $config;
785 $name =~ s,.*/(.*),$1,;
786
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400787 my $test_num = $$current_test_num;
Steven Rostedta57419b2010-11-02 15:13:54 -0400788 my $default = 1;
789 my $repeat = 1;
790 my $num_tests_set = 0;
791 my $skip = 0;
792 my $rest;
Steven Rostedta9f84422011-10-17 11:06:29 -0400793 my $line;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400794 my $test_case = 0;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400795 my $if = 0;
796 my $if_set = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400797 my $override = 0;
798
799 my %overrides;
Steven Rostedta57419b2010-11-02 15:13:54 -0400800
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400801 while (<$in>) {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400802
803 # ignore blank lines and comments
804 next if (/^\s*$/ || /\s*\#/);
805
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400806 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400807
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400808 my $type = $1;
809 $rest = $2;
Steven Rostedta9f84422011-10-17 11:06:29 -0400810 $line = $2;
Steven Rostedta57419b2010-11-02 15:13:54 -0400811
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400812 my $old_test_num;
813 my $old_repeat;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400814 $override = 0;
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400815
816 if ($type eq "TEST_START") {
817
818 if ($num_tests_set) {
819 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
820 }
821
822 $old_test_num = $test_num;
823 $old_repeat = $repeat;
824
825 $test_num += $repeat;
826 $default = 0;
827 $repeat = 1;
828 } else {
829 $default = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400830 }
831
Steven Rostedta9f84422011-10-17 11:06:29 -0400832 # If SKIP is anywhere in the line, the command will be skipped
833 if ($rest =~ s/\s+SKIP\b//) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400834 $skip = 1;
835 } else {
Steven Rostedt0df213c2011-06-14 20:51:37 -0400836 $test_case = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400837 $skip = 0;
838 }
839
Steven Rostedta9f84422011-10-17 11:06:29 -0400840 if ($rest =~ s/\sELSE\b//) {
841 if (!$if) {
842 die "$name: $.: ELSE found with out matching IF section\n$_";
843 }
844 $if = 0;
845
846 if ($if_set) {
847 $skip = 1;
848 } else {
849 $skip = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400850 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400851 }
852
Steven Rostedta9f84422011-10-17 11:06:29 -0400853 if ($rest =~ s/\sIF\s+(.*)//) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400854 if (process_if($name, $1)) {
855 $if_set = 1;
856 } else {
857 $skip = 1;
858 }
859 $if = 1;
860 } else {
861 $if = 0;
Steven Rostedta9f84422011-10-17 11:06:29 -0400862 $if_set = 0;
Steven Rostedta57419b2010-11-02 15:13:54 -0400863 }
864
Steven Rostedta9f84422011-10-17 11:06:29 -0400865 if (!$skip) {
866 if ($type eq "TEST_START") {
867 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
868 $repeat = $1;
869 $repeat_tests{"$test_num"} = $repeat;
870 }
871 } elsif ($rest =~ s/\sOVERRIDE\b//) {
872 # DEFAULT only
873 $override = 1;
874 # Clear previous overrides
875 %overrides = ();
876 }
877 }
878
879 if (!$skip && $rest !~ /^\s*$/) {
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400880 die "$name: $.: Gargbage found after $type\n$_";
Steven Rostedta57419b2010-11-02 15:13:54 -0400881 }
882
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400883 if ($skip && $type eq "TEST_START") {
Steven Rostedta57419b2010-11-02 15:13:54 -0400884 $test_num = $old_test_num;
Steven Rostedte48c5292010-11-02 14:35:37 -0400885 $repeat = $old_repeat;
Steven Rostedta57419b2010-11-02 15:13:54 -0400886 }
887
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400888 } elsif (/^\s*ELSE\b(.*)$/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400889 if (!$if) {
890 die "$name: $.: ELSE found with out matching IF section\n$_";
891 }
892 $rest = $1;
893 if ($if_set) {
894 $skip = 1;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400895 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400896 } else {
897 $skip = 0;
898
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400899 if ($rest =~ /\sIF\s+(.*)/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400900 # May be a ELSE IF section.
Steven Rostedt95f57832012-09-26 14:48:17 -0400901 if (process_if($name, $1)) {
902 $if_set = 1;
903 } else {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400904 $skip = 1;
905 }
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400906 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400907 } else {
908 $if = 0;
909 }
910 }
911
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400912 if ($rest !~ /^\s*$/) {
913 die "$name: $.: Gargbage found after DEFAULTS\n$_";
914 }
915
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400916 } elsif (/^\s*INCLUDE\s+(\S+)/) {
917
918 next if ($skip);
919
920 if (!$default) {
921 die "$name: $.: INCLUDE can only be done in default sections\n$_";
922 }
923
924 my $file = process_variables($1);
925
926 if ($file !~ m,^/,) {
927 # check the path of the config file first
928 if ($config =~ m,(.*)/,) {
929 if (-f "$1/$file") {
930 $file = "$1/$file";
931 }
932 }
933 }
934
935 if ( ! -r $file ) {
936 die "$name: $.: Can't read file $file\n$_";
937 }
938
939 if (__read_config($file, \$test_num)) {
940 $test_case = 1;
941 }
942
Steven Rostedta57419b2010-11-02 15:13:54 -0400943 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
944
945 next if ($skip);
946
Steven Rostedt2545eb62010-11-02 15:01:32 -0400947 my $lvalue = $1;
948 my $rvalue = $2;
949
Steven Rostedta57419b2010-11-02 15:13:54 -0400950 if (!$default &&
951 ($lvalue eq "NUM_TESTS" ||
952 $lvalue eq "LOG_FILE" ||
953 $lvalue eq "CLEAR_LOG")) {
954 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400955 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400956
957 if ($lvalue eq "NUM_TESTS") {
958 if ($test_num) {
959 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
960 }
961 if (!$default) {
962 die "$name: $.: NUM_TESTS must be set in default section\n";
963 }
964 $num_tests_set = 1;
965 }
966
967 if ($default || $lvalue =~ /\[\d+\]$/) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400968 set_value($lvalue, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400969 } else {
970 my $val = "$lvalue\[$test_num\]";
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400971 set_value($val, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400972
973 if ($repeat > 1) {
974 $repeats{$val} = $repeat;
975 }
976 }
Steven Rostedt77d942c2011-05-20 13:36:58 -0400977 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
978 next if ($skip);
979
980 my $lvalue = $1;
981 my $rvalue = $2;
982
983 # process config variables.
984 # Config variables are only active while reading the
985 # config and can be defined anywhere. They also ignore
986 # TEST_START and DEFAULTS, but are skipped if they are in
987 # on of these sections that have SKIP defined.
988 # The save variable can be
989 # defined multiple times and the new one simply overrides
990 # the prevous one.
991 set_variable($lvalue, $rvalue);
992
Steven Rostedta57419b2010-11-02 15:13:54 -0400993 } else {
994 die "$name: $.: Garbage found in config\n$_";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400995 }
996 }
997
Steven Rostedta57419b2010-11-02 15:13:54 -0400998 if ($test_num) {
999 $test_num += $repeat - 1;
1000 $opt{"NUM_TESTS"} = $test_num;
1001 }
1002
Steven Rostedt2ed3b162011-09-30 21:00:00 -04001003 close($in);
1004
1005 $$current_test_num = $test_num;
1006
1007 return $test_case;
1008}
1009
Steven Rostedtc4261d02011-11-23 13:41:18 -05001010sub get_test_case {
1011 print "What test case would you like to run?\n";
1012 print " (build, install or boot)\n";
1013 print " Other tests are available but require editing the config file\n";
1014 my $ans = <STDIN>;
1015 chomp $ans;
1016 $default{"TEST_TYPE"} = $ans;
1017}
1018
Steven Rostedt2ed3b162011-09-30 21:00:00 -04001019sub read_config {
1020 my ($config) = @_;
1021
1022 my $test_case;
1023 my $test_num = 0;
1024
1025 $test_case = __read_config $config, \$test_num;
1026
Steven Rostedt8d1491b2010-11-18 15:39:48 -05001027 # make sure we have all mandatory configs
1028 get_ktest_configs;
1029
Steven Rostedt0df213c2011-06-14 20:51:37 -04001030 # was a test specified?
1031 if (!$test_case) {
1032 print "No test case specified.\n";
Steven Rostedtc4261d02011-11-23 13:41:18 -05001033 get_test_case;
Steven Rostedt0df213c2011-06-14 20:51:37 -04001034 }
1035
Steven Rostedta75fece2010-11-02 14:58:27 -04001036 # set any defaults
1037
1038 foreach my $default (keys %default) {
1039 if (!defined($opt{$default})) {
1040 $opt{$default} = $default{$default};
1041 }
1042 }
Steven Rostedt9cc9e092011-12-22 21:37:22 -05001043
1044 if ($opt{"IGNORE_UNUSED"} == 1) {
1045 return;
1046 }
1047
1048 my %not_used;
1049
1050 # check if there are any stragglers (typos?)
1051 foreach my $option (keys %opt) {
1052 my $op = $option;
1053 # remove per test labels.
1054 $op =~ s/\[.*\]//;
1055 if (!exists($option_map{$op}) &&
1056 !exists($default{$op}) &&
1057 !exists($used_options{$op})) {
1058 $not_used{$op} = 1;
1059 }
1060 }
1061
1062 if (%not_used) {
1063 my $s = "s are";
1064 $s = " is" if (keys %not_used == 1);
1065 print "The following option$s not used; could be a typo:\n";
1066 foreach my $option (keys %not_used) {
1067 print "$option\n";
1068 }
1069 print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
1070 if (!read_yn "Do you want to continue?") {
1071 exit -1;
1072 }
1073 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001074}
1075
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001076sub __eval_option {
1077 my ($option, $i) = @_;
1078
1079 # Add space to evaluate the character before $
1080 $option = " $option";
1081 my $retval = "";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301082 my $repeated = 0;
1083 my $parent = 0;
1084
1085 foreach my $test (keys %repeat_tests) {
1086 if ($i >= $test &&
1087 $i < $test + $repeat_tests{$test}) {
1088
1089 $repeated = 1;
1090 $parent = $test;
1091 last;
1092 }
1093 }
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001094
1095 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
1096 my $start = $1;
1097 my $var = $2;
1098 my $end = $3;
1099
1100 # Append beginning of line
1101 $retval = "$retval$start";
1102
1103 # If the iteration option OPT[$i] exists, then use that.
1104 # otherwise see if the default OPT (without [$i]) exists.
1105
1106 my $o = "$var\[$i\]";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301107 my $parento = "$var\[$parent\]";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001108
1109 if (defined($opt{$o})) {
1110 $o = $opt{$o};
1111 $retval = "$retval$o";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301112 } elsif ($repeated && defined($opt{$parento})) {
1113 $o = $opt{$parento};
1114 $retval = "$retval$o";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001115 } elsif (defined($opt{$var})) {
1116 $o = $opt{$var};
1117 $retval = "$retval$o";
1118 } else {
1119 $retval = "$retval\$\{$var\}";
1120 }
1121
1122 $option = $end;
1123 }
1124
1125 $retval = "$retval$option";
1126
1127 $retval =~ s/^ //;
1128
1129 return $retval;
1130}
1131
1132sub eval_option {
1133 my ($option, $i) = @_;
1134
1135 my $prev = "";
1136
1137 # Since an option can evaluate to another option,
1138 # keep iterating until we do not evaluate any more
1139 # options.
1140 my $r = 0;
1141 while ($prev ne $option) {
1142 # Check for recursive evaluations.
1143 # 100 deep should be more than enough.
1144 if ($r++ > 100) {
1145 die "Over 100 evaluations accurred with $option\n" .
1146 "Check for recursive variables\n";
1147 }
1148 $prev = $option;
1149 $option = __eval_option($option, $i);
1150 }
1151
1152 return $option;
1153}
1154
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001155sub _logit {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001156 if (defined($opt{"LOG_FILE"})) {
1157 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
1158 print OUT @_;
1159 close(OUT);
1160 }
1161}
1162
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001163sub logit {
1164 if (defined($opt{"LOG_FILE"})) {
1165 _logit @_;
1166 } else {
1167 print @_;
1168 }
1169}
1170
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001171sub doprint {
1172 print @_;
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001173 _logit @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001174}
1175
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001176sub run_command;
Andrew Jones2728be42011-08-12 15:32:05 +02001177sub start_monitor;
1178sub end_monitor;
1179sub wait_for_monitor;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001180
1181sub reboot {
Andrew Jones2728be42011-08-12 15:32:05 +02001182 my ($time) = @_;
1183
Steven Rostedta4968722012-12-11 14:59:05 -05001184 # Make sure everything has been written to disk
1185 run_ssh("sync");
1186
Steven Rostedt2b803362011-09-30 18:00:23 -04001187 if (defined($time)) {
1188 start_monitor;
1189 # flush out current monitor
1190 # May contain the reboot success line
1191 wait_for_monitor 1;
1192 }
1193
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001194 # try to reboot normally
Steven Rostedte48c5292010-11-02 14:35:37 -04001195 if (run_command $reboot) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001196 if (defined($powercycle_after_reboot)) {
1197 sleep $powercycle_after_reboot;
1198 run_command "$power_cycle";
1199 }
1200 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001201 # nope? power cycle it.
Steven Rostedta75fece2010-11-02 14:58:27 -04001202 run_command "$power_cycle";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001203 }
Andrew Jones2728be42011-08-12 15:32:05 +02001204
1205 if (defined($time)) {
Steven Rostedt407b95b2012-07-19 16:05:42 -04001206 if (wait_for_monitor($time, $reboot_success_line)) {
1207 # reboot got stuck?
Steven Rostedt8a80c722012-07-19 16:08:33 -04001208 doprint "Reboot did not finish. Forcing power cycle\n";
Steven Rostedt407b95b2012-07-19 16:05:42 -04001209 run_command "$power_cycle";
1210 }
Andrew Jones2728be42011-08-12 15:32:05 +02001211 end_monitor;
1212 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001213}
1214
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001215sub reboot_to_good {
1216 my ($time) = @_;
1217
1218 if (defined($switch_to_good)) {
1219 run_command $switch_to_good;
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001220 }
1221
1222 reboot $time;
1223}
1224
Steven Rostedt576f6272010-11-02 14:58:38 -04001225sub do_not_reboot {
1226 my $i = $iteration;
1227
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001228 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -04001229 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1230 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1231}
1232
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001233sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001234 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001235
Steven Rostedt576f6272010-11-02 14:58:38 -04001236 my $i = $iteration;
1237
1238 if ($reboot_on_error && !do_not_reboot) {
1239
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001240 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001241 reboot_to_good;
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001242
Steven Rostedta75fece2010-11-02 14:58:27 -04001243 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001244 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001245 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001246 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001247
Steven Rostedtf80802c2011-03-07 13:18:47 -05001248 if (defined($opt{"LOG_FILE"})) {
1249 print " See $opt{LOG_FILE} for more info.\n";
1250 }
1251
Steven Rostedt576f6272010-11-02 14:58:38 -04001252 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001253}
1254
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001255sub open_console {
1256 my ($fp) = @_;
1257
1258 my $flags;
1259
Steven Rostedta75fece2010-11-02 14:58:27 -04001260 my $pid = open($fp, "$console|") or
1261 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001262
1263 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001264 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001265 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001266 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001267
1268 return $pid;
1269}
1270
1271sub close_console {
1272 my ($fp, $pid) = @_;
1273
1274 doprint "kill child process $pid\n";
1275 kill 2, $pid;
1276
1277 print "closing!\n";
1278 close($fp);
1279}
1280
1281sub start_monitor {
1282 if ($monitor_cnt++) {
1283 return;
1284 }
1285 $monitor_fp = \*MONFD;
1286 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001287
1288 return;
1289
1290 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001291}
1292
1293sub end_monitor {
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001294 return if (!defined $console);
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001295 if (--$monitor_cnt) {
1296 return;
1297 }
1298 close_console($monitor_fp, $monitor_pid);
1299}
1300
1301sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001302 my ($time, $stop) = @_;
1303 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001304 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001305 my $booted = 0;
Steven Rostedt407b95b2012-07-19 16:05:42 -04001306 my $start_time = time;
Steven Rostedt8a80c722012-07-19 16:08:33 -04001307 my $skip_call_trace = 0;
1308 my $bug = 0;
1309 my $bug_ignored = 0;
Steven Rostedt407b95b2012-07-19 16:05:42 -04001310 my $now;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001311
Steven Rostedta75fece2010-11-02 14:58:27 -04001312 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001313
1314 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001315 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001316 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001317 last if (!defined($line));
1318 print "$line";
1319 $full_line .= $line;
1320
1321 if (defined($stop) && $full_line =~ /$stop/) {
1322 doprint "wait for monitor detected $stop\n";
1323 $booted = 1;
1324 }
1325
Steven Rostedt8a80c722012-07-19 16:08:33 -04001326 if ($full_line =~ /\[ backtrace testing \]/) {
1327 $skip_call_trace = 1;
1328 }
1329
1330 if ($full_line =~ /call trace:/i) {
1331 if (!$bug && !$skip_call_trace) {
1332 if ($ignore_errors) {
1333 $bug_ignored = 1;
1334 } else {
1335 $bug = 1;
1336 }
1337 }
1338 }
1339
1340 if ($full_line =~ /\[ end of backtrace testing \]/) {
1341 $skip_call_trace = 0;
1342 }
1343
1344 if ($full_line =~ /Kernel panic -/) {
1345 $bug = 1;
1346 }
1347
Steven Rostedt2b803362011-09-30 18:00:23 -04001348 if ($line =~ /\n/) {
1349 $full_line = "";
1350 }
Steven Rostedt407b95b2012-07-19 16:05:42 -04001351 $now = time;
1352 if ($now - $start_time >= $max_monitor_wait) {
1353 doprint "Exiting monitor flush due to hitting MAX_MONITOR_WAIT\n";
1354 return 1;
1355 }
Steven Rostedt2b803362011-09-30 18:00:23 -04001356 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001357 print "** Monitor flushed **\n";
Steven Rostedt8a80c722012-07-19 16:08:33 -04001358 return $bug;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001359}
1360
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301361sub save_logs {
1362 my ($result, $basedir) = @_;
1363 my @t = localtime;
1364 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1365 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1366
1367 my $type = $build_type;
1368 if ($type =~ /useconfig/) {
1369 $type = "useconfig";
1370 }
1371
1372 my $dir = "$machine-$test_type-$type-$result-$date";
1373
1374 $dir = "$basedir/$dir";
1375
1376 if (!-d $dir) {
1377 mkpath($dir) or
1378 die "can't create $dir";
1379 }
1380
1381 my %files = (
1382 "config" => $output_config,
1383 "buildlog" => $buildlog,
1384 "dmesg" => $dmesg,
1385 "testlog" => $testlog,
1386 );
1387
1388 while (my ($name, $source) = each(%files)) {
1389 if (-f "$source") {
1390 cp "$source", "$dir/$name" or
1391 die "failed to copy $source";
1392 }
1393 }
1394
1395 doprint "*** Saved info to $dir ***\n";
1396}
1397
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001398sub fail {
1399
Steven Rostedt921ed4c2012-07-19 15:18:27 -04001400 if (defined($post_test)) {
1401 run_command $post_test;
1402 }
1403
Steven Rostedta75fece2010-11-02 14:58:27 -04001404 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001405 dodie @_;
1406 }
1407
Steven Rostedta75fece2010-11-02 14:58:27 -04001408 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001409
Steven Rostedt576f6272010-11-02 14:58:38 -04001410 my $i = $iteration;
1411
Steven Rostedta75fece2010-11-02 14:58:27 -04001412 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001413 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001414 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001415 reboot_to_good $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001416 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001417
Steven Rostedt9064af52011-06-13 10:38:48 -04001418 my $name = "";
1419
1420 if (defined($test_name)) {
1421 $name = " ($test_name)";
1422 }
1423
Steven Rostedt576f6272010-11-02 14:58:38 -04001424 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1425 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001426 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001427 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1428 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001429
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301430 if (defined($store_failures)) {
1431 save_logs "fail", $store_failures;
1432 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001433
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001434 return 1;
1435}
1436
Steven Rostedt2545eb62010-11-02 15:01:32 -04001437sub run_command {
1438 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001439 my $dolog = 0;
1440 my $dord = 0;
1441 my $pid;
1442
Steven Rostedte48c5292010-11-02 14:35:37 -04001443 $command =~ s/\$SSH_USER/$ssh_user/g;
1444 $command =~ s/\$MACHINE/$machine/g;
1445
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001446 doprint("$command ... ");
1447
1448 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001449 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001450
1451 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001452 open(LOG, ">>$opt{LOG_FILE}") or
1453 dodie "failed to write to log";
1454 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001455 }
1456
1457 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001458 open (RD, ">$redirect") or
1459 dodie "failed to write to redirect $redirect";
1460 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001461 }
1462
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001463 while (<CMD>) {
1464 print LOG if ($dolog);
1465 print RD if ($dord);
1466 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001467
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001468 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001469 my $failed = $?;
1470
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001471 close(CMD);
1472 close(LOG) if ($dolog);
1473 close(RD) if ($dord);
1474
Steven Rostedt2545eb62010-11-02 15:01:32 -04001475 if ($failed) {
1476 doprint "FAILED!\n";
1477 } else {
1478 doprint "SUCCESS\n";
1479 }
1480
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001481 return !$failed;
1482}
1483
Steven Rostedte48c5292010-11-02 14:35:37 -04001484sub run_ssh {
1485 my ($cmd) = @_;
1486 my $cp_exec = $ssh_exec;
1487
1488 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1489 return run_command "$cp_exec";
1490}
1491
1492sub run_scp {
Steven Rostedt02ad2612012-03-21 08:21:24 -04001493 my ($src, $dst, $cp_scp) = @_;
Steven Rostedte48c5292010-11-02 14:35:37 -04001494
1495 $cp_scp =~ s/\$SRC_FILE/$src/g;
1496 $cp_scp =~ s/\$DST_FILE/$dst/g;
1497
1498 return run_command "$cp_scp";
1499}
1500
Steven Rostedt02ad2612012-03-21 08:21:24 -04001501sub run_scp_install {
1502 my ($src, $dst) = @_;
1503
1504 my $cp_scp = $scp_to_target_install;
1505
1506 return run_scp($src, $dst, $cp_scp);
1507}
1508
1509sub run_scp_mod {
1510 my ($src, $dst) = @_;
1511
1512 my $cp_scp = $scp_to_target;
1513
1514 return run_scp($src, $dst, $cp_scp);
1515}
1516
Steven Rostedta15ba912012-11-13 14:30:37 -05001517sub get_grub2_index {
1518
1519 return if (defined($grub_number));
1520
1521 doprint "Find grub2 menu ... ";
1522 $grub_number = -1;
1523
1524 my $ssh_grub = $ssh_exec;
1525 $ssh_grub =~ s,\$SSH_COMMAND,cat $grub_file,g;
1526
1527 open(IN, "$ssh_grub |")
1528 or die "unable to get $grub_file";
1529
1530 my $found = 0;
1531
1532 while (<IN>) {
1533 if (/^menuentry.*$grub_menu/) {
1534 $grub_number++;
1535 $found = 1;
1536 last;
1537 } elsif (/^menuentry\s/) {
1538 $grub_number++;
1539 }
1540 }
1541 close(IN);
1542
1543 die "Could not find '$grub_menu' in $grub_file on $machine"
1544 if (!$found);
1545 doprint "$grub_number\n";
1546}
1547
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001548sub get_grub_index {
1549
Steven Rostedta15ba912012-11-13 14:30:37 -05001550 if ($reboot_type eq "grub2") {
1551 get_grub2_index;
1552 return;
1553 }
1554
Steven Rostedta75fece2010-11-02 14:58:27 -04001555 if ($reboot_type ne "grub") {
1556 return;
1557 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001558 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001559
1560 doprint "Find grub menu ... ";
1561 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001562
1563 my $ssh_grub = $ssh_exec;
1564 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1565
1566 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001567 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001568
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001569 my $found = 0;
1570
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001571 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001572 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001573 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001574 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001575 last;
1576 } elsif (/^\s*title\s/) {
1577 $grub_number++;
1578 }
1579 }
1580 close(IN);
1581
Steven Rostedta75fece2010-11-02 14:58:27 -04001582 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001583 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001584 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001585}
1586
Steven Rostedt2545eb62010-11-02 15:01:32 -04001587sub wait_for_input
1588{
1589 my ($fp, $time) = @_;
1590 my $rin;
1591 my $ready;
1592 my $line;
1593 my $ch;
1594
1595 if (!defined($time)) {
1596 $time = $timeout;
1597 }
1598
1599 $rin = '';
1600 vec($rin, fileno($fp), 1) = 1;
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001601 ($ready, $time) = select($rin, undef, undef, $time);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001602
1603 $line = "";
1604
1605 # try to read one char at a time
1606 while (sysread $fp, $ch, 1) {
1607 $line .= $ch;
1608 last if ($ch eq "\n");
1609 }
1610
1611 if (!length($line)) {
1612 return undef;
1613 }
1614
1615 return $line;
1616}
1617
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001618sub reboot_to {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001619 if (defined($switch_to_test)) {
1620 run_command $switch_to_test;
1621 }
1622
Steven Rostedta75fece2010-11-02 14:58:27 -04001623 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001624 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
Steven Rostedta15ba912012-11-13 14:30:37 -05001625 } elsif ($reboot_type eq "grub2") {
1626 run_ssh "$grub_reboot $grub_number";
Steven Rostedt77869542012-12-11 17:37:41 -05001627 } elsif ($reboot_type eq "syslinux") {
1628 run_ssh "$syslinux --once \\\"$syslinux_label\\\" $syslinux_path";
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001629 } elsif (defined $reboot_script) {
1630 run_command "$reboot_script";
Steven Rostedta75fece2010-11-02 14:58:27 -04001631 }
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001632 reboot;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001633}
1634
Steven Rostedta57419b2010-11-02 15:13:54 -04001635sub get_sha1 {
1636 my ($commit) = @_;
1637
1638 doprint "git rev-list --max-count=1 $commit ... ";
1639 my $sha1 = `git rev-list --max-count=1 $commit`;
1640 my $ret = $?;
1641
1642 logit $sha1;
1643
1644 if ($ret) {
1645 doprint "FAILED\n";
1646 dodie "Failed to get git $commit";
1647 }
1648
1649 print "SUCCESS\n";
1650
1651 chomp $sha1;
1652
1653 return $sha1;
1654}
1655
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001656sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001657 my $booted = 0;
1658 my $bug = 0;
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001659 my $bug_ignored = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001660 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001661 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001662
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001663 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001664
1665 my $line;
1666 my $full_line = "";
1667
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001668 open(DMESG, "> $dmesg") or
1669 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001670
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001671 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001672
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001673 my $success_start;
1674 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001675 my $monitor_start = time;
1676 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001677 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001678
Steven Rostedt2d01b262011-03-08 09:47:54 -05001679 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001680
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001681 if ($bug && defined($stop_after_failure) &&
1682 $stop_after_failure >= 0) {
1683 my $time = $stop_after_failure - (time - $failure_start);
1684 $line = wait_for_input($monitor_fp, $time);
1685 if (!defined($line)) {
1686 doprint "bug timed out after $booted_timeout seconds\n";
1687 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1688 last;
1689 }
1690 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001691 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001692 if (!defined($line)) {
1693 my $s = $booted_timeout == 1 ? "" : "s";
1694 doprint "Successful boot found: break after $booted_timeout second$s\n";
1695 last;
1696 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001697 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001698 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001699 if (!defined($line)) {
1700 my $s = $timeout == 1 ? "" : "s";
1701 doprint "Timed out after $timeout second$s\n";
1702 last;
1703 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001704 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001705
Steven Rostedt2545eb62010-11-02 15:01:32 -04001706 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001707 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001708
1709 # we are not guaranteed to get a full line
1710 $full_line .= $line;
1711
Steven Rostedta75fece2010-11-02 14:58:27 -04001712 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001713 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001714 $success_start = time;
1715 }
1716
1717 if ($booted && defined($stop_after_success) &&
1718 $stop_after_success >= 0) {
1719 my $now = time;
1720 if ($now - $success_start >= $stop_after_success) {
1721 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1722 last;
1723 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001724 }
1725
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001726 if ($full_line =~ /\[ backtrace testing \]/) {
1727 $skip_call_trace = 1;
1728 }
1729
Steven Rostedt2545eb62010-11-02 15:01:32 -04001730 if ($full_line =~ /call trace:/i) {
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001731 if (!$bug && !$skip_call_trace) {
1732 if ($ignore_errors) {
1733 $bug_ignored = 1;
1734 } else {
1735 $bug = 1;
1736 $failure_start = time;
1737 }
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001738 }
1739 }
1740
1741 if ($bug && defined($stop_after_failure) &&
1742 $stop_after_failure >= 0) {
1743 my $now = time;
1744 if ($now - $failure_start >= $stop_after_failure) {
1745 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1746 last;
1747 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001748 }
1749
1750 if ($full_line =~ /\[ end of backtrace testing \]/) {
1751 $skip_call_trace = 0;
1752 }
1753
1754 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001755 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001756 $bug = 1;
1757 }
1758
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001759 # Detect triple faults by testing the banner
1760 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1761 if ($1 eq $version) {
1762 $version_found = 1;
1763 } elsif ($version_found && $detect_triplefault) {
1764 # We already booted into the kernel we are testing,
1765 # but now we booted into another kernel?
1766 # Consider this a triple fault.
1767 doprint "Aleady booted in Linux kernel $version, but now\n";
1768 doprint "we booted into Linux kernel $1.\n";
1769 doprint "Assuming that this is a triple fault.\n";
1770 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1771 last;
1772 }
1773 }
1774
Steven Rostedt2545eb62010-11-02 15:01:32 -04001775 if ($line =~ /\n/) {
1776 $full_line = "";
1777 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001778
1779 if ($stop_test_after > 0 && !$booted && !$bug) {
1780 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001781 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001782 $done = 1;
1783 }
1784 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001785 }
1786
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001787 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001788
Steven Rostedt2545eb62010-11-02 15:01:32 -04001789 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001790 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001791 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001792 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001793
Steven Rostedta75fece2010-11-02 14:58:27 -04001794 if (!$booted) {
1795 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001796 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001797 }
1798
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001799 if ($bug_ignored) {
1800 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
1801 }
1802
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001803 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001804}
1805
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001806sub eval_kernel_version {
1807 my ($option) = @_;
1808
1809 $option =~ s/\$KERNEL_VERSION/$version/g;
1810
1811 return $option;
1812}
1813
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001814sub do_post_install {
1815
1816 return if (!defined($post_install));
1817
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001818 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001819 run_command "$cp_post_install" or
1820 dodie "Failed to run post install";
1821}
1822
Steven Rostedte1a6c3d2012-12-11 21:19:41 -05001823# Sometimes the reboot fails, and will hang. We try to ssh to the box
1824# and if we fail, we force another reboot, that should powercycle it.
1825sub test_booted {
1826 if (!run_ssh "echo testing connection") {
1827 reboot $sleep_time;
1828 }
1829}
1830
Steven Rostedt2545eb62010-11-02 15:01:32 -04001831sub install {
1832
Steven Rostedte0a87422011-09-30 17:50:48 -04001833 return if ($no_install);
1834
Steven Rostedte5c2ec12012-07-19 15:22:05 -04001835 if (defined($pre_install)) {
1836 my $cp_pre_install = eval_kernel_version $pre_install;
1837 run_command "$cp_pre_install" or
1838 dodie "Failed to run pre install";
1839 }
1840
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001841 my $cp_target = eval_kernel_version $target_image;
1842
Steven Rostedte1a6c3d2012-12-11 21:19:41 -05001843 test_booted;
1844
Steven Rostedt02ad2612012-03-21 08:21:24 -04001845 run_scp_install "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001846 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001847
1848 my $install_mods = 0;
1849
1850 # should we process modules?
1851 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001852 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001853 while (<IN>) {
1854 if (/CONFIG_MODULES(=y)?/) {
Steven Rostedt8bc5e4e2012-10-26 00:10:32 -04001855 if (defined($1)) {
1856 $install_mods = 1;
1857 last;
1858 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001859 }
1860 }
1861 close(IN);
1862
1863 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001864 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001865 doprint "No modules needed\n";
1866 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001867 }
1868
Steven Rostedt627977d2012-03-21 08:16:15 -04001869 run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001870 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001871
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001872 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001873 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001874
Steven Rostedte48c5292010-11-02 14:35:37 -04001875 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001876 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001877
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001878 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001879 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001880 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001881
Steven Rostedt02ad2612012-03-21 08:21:24 -04001882 run_scp_mod "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001883 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001884
Steven Rostedta75fece2010-11-02 14:58:27 -04001885 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001886
Steven Rostedte7b13442011-06-14 20:44:36 -04001887 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001888 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001889
Steven Rostedte48c5292010-11-02 14:35:37 -04001890 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001891
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001892 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001893}
1894
Steven Rostedtddf607e2011-06-14 20:49:13 -04001895sub get_version {
1896 # get the release name
Steven Rostedt683a3e62012-05-18 13:34:35 -04001897 return if ($have_version);
Steven Rostedtddf607e2011-06-14 20:49:13 -04001898 doprint "$make kernelrelease ... ";
1899 $version = `$make kernelrelease | tail -1`;
1900 chomp($version);
1901 doprint "$version\n";
Steven Rostedt683a3e62012-05-18 13:34:35 -04001902 $have_version = 1;
Steven Rostedtddf607e2011-06-14 20:49:13 -04001903}
1904
1905sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001906 # Make sure the stable kernel has finished booting
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001907
1908 # Install bisects, don't need console
1909 if (defined $console) {
1910 start_monitor;
1911 wait_for_monitor 5;
1912 end_monitor;
1913 }
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001914
Steven Rostedtddf607e2011-06-14 20:49:13 -04001915 get_grub_index;
1916 get_version;
1917 install;
1918
Steven Rostedt (Red Hat)319ab142013-01-30 12:25:38 -05001919 start_monitor if (defined $console);
Steven Rostedtddf607e2011-06-14 20:49:13 -04001920 return monitor;
1921}
1922
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001923sub check_buildlog {
1924 my ($patch) = @_;
1925
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001926 my @files = `git show $patch | diffstat -l`;
1927
1928 open(IN, "git show $patch |") or
1929 dodie "failed to show $patch";
1930 while (<IN>) {
1931 if (m,^--- a/(.*),) {
1932 chomp $1;
1933 $files[$#files] = $1;
1934 }
1935 }
1936 close(IN);
1937
1938 open(IN, $buildlog) or dodie "Can't open $buildlog";
1939 while (<IN>) {
1940 if (/^\s*(.*?):.*(warning|error)/) {
1941 my $err = $1;
1942 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001943 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001944 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001945 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001946 }
1947 }
1948 }
1949 }
1950 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001951
1952 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001953}
1954
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001955sub apply_min_config {
1956 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05001957
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001958 # Read the config file and remove anything that
1959 # is in the force_config hash (from minconfig and others)
1960 # then add the force config back.
1961
1962 doprint "Applying minimum configurations into $output_config.new\n";
1963
1964 open (OUT, ">$outconfig") or
1965 dodie "Can't create $outconfig";
1966
1967 if (-f $output_config) {
1968 open (IN, $output_config) or
1969 dodie "Failed to open $output_config";
1970 while (<IN>) {
1971 if (/^(# )?(CONFIG_[^\s=]*)/) {
1972 next if (defined($force_config{$2}));
1973 }
1974 print OUT;
1975 }
1976 close IN;
1977 }
1978 foreach my $config (keys %force_config) {
1979 print OUT "$force_config{$config}\n";
1980 }
1981 close OUT;
1982
1983 run_command "mv $outconfig $output_config";
1984}
1985
1986sub make_oldconfig {
1987
Steven Rostedt4c4ab122011-07-15 21:16:17 -04001988 my @force_list = keys %force_config;
1989
1990 if ($#force_list >= 0) {
1991 apply_min_config;
1992 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001993
Adam Leefb16d892012-09-01 01:05:17 +08001994 if (!run_command "$make olddefconfig") {
1995 # Perhaps olddefconfig doesn't exist in this version of the kernel
Steven Rostedt18925172012-12-11 20:16:03 -05001996 # try oldnoconfig
1997 doprint "olddefconfig failed, trying make oldnoconfig\n";
1998 if (!run_command "$make oldnoconfig") {
1999 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
2000 # try a yes '' | oldconfig
2001 run_command "yes '' | $make oldconfig" or
2002 dodie "failed make config oldconfig";
2003 }
Steven Rostedt612b9e92011-03-07 13:27:43 -05002004 }
2005}
2006
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002007# read a config file and use this to force new configs.
2008sub load_force_config {
2009 my ($config) = @_;
2010
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002011 doprint "Loading force configs from $config\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002012 open(IN, $config) or
2013 dodie "failed to read $config";
2014 while (<IN>) {
2015 chomp;
2016 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
2017 $force_config{$1} = $_;
2018 } elsif (/^# (CONFIG_\S*) is not set/) {
2019 $force_config{$1} = $_;
2020 }
2021 }
2022 close IN;
2023}
2024
Steven Rostedt2545eb62010-11-02 15:01:32 -04002025sub build {
2026 my ($type) = @_;
2027
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002028 unlink $buildlog;
2029
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04002030 # Failed builds should not reboot the target
2031 my $save_no_reboot = $no_reboot;
2032 $no_reboot = 1;
2033
Steven Rostedt683a3e62012-05-18 13:34:35 -04002034 # Calculate a new version from here.
2035 $have_version = 0;
2036
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04002037 if (defined($pre_build)) {
2038 my $ret = run_command $pre_build;
2039 if (!$ret && defined($pre_build_die) &&
2040 $pre_build_die) {
2041 dodie "failed to pre_build\n";
2042 }
2043 }
2044
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002045 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05002046 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002047 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002048
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002049 $type = "oldconfig";
2050 }
2051
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002052 # old config can ask questions
2053 if ($type eq "oldconfig") {
Adam Leefb16d892012-09-01 01:05:17 +08002054 $type = "olddefconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002055
2056 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05002057 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002058
Andrew Jones13488232011-08-12 15:32:04 +02002059 if (!$noclean) {
2060 run_command "mv $output_config $outputdir/config_temp" or
2061 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04002062
Andrew Jones13488232011-08-12 15:32:04 +02002063 run_command "$make mrproper" or dodie "make mrproper";
2064
2065 run_command "mv $outputdir/config_temp $output_config" or
2066 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002067 }
2068
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002069 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05002070 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002071 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002072 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002073 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04002074
2075 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04002076 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
2077 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04002078 close(OUT);
2079
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002080 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002081 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04002082 }
2083
Adam Leefb16d892012-09-01 01:05:17 +08002084 if ($type ne "olddefconfig") {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002085 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05002086 dodie "failed make config";
2087 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002088 # Run old config regardless, to enforce min configurations
2089 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002090
Steven Rostedta75fece2010-11-02 14:58:27 -04002091 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04002092 my $build_ret = run_command "$make $build_options";
2093 undef $redirect;
2094
2095 if (defined($post_build)) {
Steven Rostedt683a3e62012-05-18 13:34:35 -04002096 # Because a post build may change the kernel version
2097 # do it now.
2098 get_version;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04002099 my $ret = run_command $post_build;
2100 if (!$ret && defined($post_build_die) &&
2101 $post_build_die) {
2102 dodie "failed to post_build\n";
2103 }
2104 }
2105
2106 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002107 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04002108 if ($in_bisect) {
2109 $no_reboot = $save_no_reboot;
2110 return 0;
2111 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002112 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002113 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002114
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04002115 $no_reboot = $save_no_reboot;
2116
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002117 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002118}
2119
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002120sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04002121 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04002122 if (defined($poweroff_after_halt)) {
2123 sleep $poweroff_after_halt;
2124 run_command "$power_off";
2125 }
2126 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002127 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04002128 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002129 }
2130}
2131
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002132sub success {
2133 my ($i) = @_;
2134
Steven Rostedt921ed4c2012-07-19 15:18:27 -04002135 if (defined($post_test)) {
2136 run_command $post_test;
2137 }
2138
Steven Rostedte48c5292010-11-02 14:35:37 -04002139 $successes++;
2140
Steven Rostedt9064af52011-06-13 10:38:48 -04002141 my $name = "";
2142
2143 if (defined($test_name)) {
2144 $name = " ($test_name)";
2145 }
2146
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002147 doprint "\n\n*******************************************\n";
2148 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04002149 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002150 doprint "*******************************************\n";
2151 doprint "*******************************************\n";
2152
Rabin Vincentde5b6e32011-11-18 17:05:31 +05302153 if (defined($store_successes)) {
2154 save_logs "success", $store_successes;
2155 }
2156
Steven Rostedt576f6272010-11-02 14:58:38 -04002157 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002158 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002159 reboot_to_good $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002160 }
2161}
2162
Steven Rostedtc960bb92011-03-08 09:22:39 -05002163sub answer_bisect {
2164 for (;;) {
2165 doprint "Pass or fail? [p/f]";
2166 my $ans = <STDIN>;
2167 chomp $ans;
2168 if ($ans eq "p" || $ans eq "P") {
2169 return 1;
2170 } elsif ($ans eq "f" || $ans eq "F") {
2171 return 0;
2172 } else {
2173 print "Please answer 'P' or 'F'\n";
2174 }
2175 }
2176}
2177
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002178sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002179 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002180
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002181 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04002182 $reboot_on_error = 0;
2183 $poweroff_on_error = 0;
2184 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002185
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05302186 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002187 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05302188 undef $redirect;
2189
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002190 exit $failed;
2191}
2192
2193my $child_done;
2194
2195sub child_finished {
2196 $child_done = 1;
2197}
2198
2199sub do_run_test {
2200 my $child_pid;
2201 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002202 my $line;
2203 my $full_line;
2204 my $bug = 0;
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002205 my $bug_ignored = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002206
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002207 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002208
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002209 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002210
2211 $child_done = 0;
2212
2213 $SIG{CHLD} = qw(child_finished);
2214
2215 $child_pid = fork;
2216
2217 child_run_test if (!$child_pid);
2218
2219 $full_line = "";
2220
2221 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002222 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002223 if (defined($line)) {
2224
2225 # we are not guaranteed to get a full line
2226 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002227 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002228
2229 if ($full_line =~ /call trace:/i) {
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002230 if ($ignore_errors) {
2231 $bug_ignored = 1;
2232 } else {
2233 $bug = 1;
2234 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002235 }
2236
2237 if ($full_line =~ /Kernel panic -/) {
2238 $bug = 1;
2239 }
2240
2241 if ($line =~ /\n/) {
2242 $full_line = "";
2243 }
2244 }
2245 } while (!$child_done && !$bug);
2246
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002247 if (!$bug && $bug_ignored) {
2248 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
2249 }
2250
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002251 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002252 my $failure_start = time;
2253 my $now;
2254 do {
2255 $line = wait_for_input($monitor_fp, 1);
2256 if (defined($line)) {
2257 doprint $line;
2258 }
2259 $now = time;
2260 if ($now - $failure_start >= $stop_after_failure) {
2261 last;
2262 }
2263 } while (defined($line));
2264
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002265 doprint "Detected kernel crash!\n";
2266 # kill the child with extreme prejudice
2267 kill 9, $child_pid;
2268 }
2269
2270 waitpid $child_pid, 0;
2271 $child_exit = $?;
2272
Steven Rostedtc5dacb82011-12-22 12:43:57 -05002273 if (!$bug && $in_bisect) {
2274 if (defined($bisect_ret_good)) {
2275 if ($child_exit == $bisect_ret_good) {
2276 return 1;
2277 }
2278 }
2279 if (defined($bisect_ret_skip)) {
2280 if ($child_exit == $bisect_ret_skip) {
2281 return -1;
2282 }
2283 }
2284 if (defined($bisect_ret_abort)) {
2285 if ($child_exit == $bisect_ret_abort) {
2286 fail "test abort" and return -2;
2287 }
2288 }
2289 if (defined($bisect_ret_bad)) {
2290 if ($child_exit == $bisect_ret_skip) {
2291 return 0;
2292 }
2293 }
2294 if (defined($bisect_ret_default)) {
2295 if ($bisect_ret_default eq "good") {
2296 return 1;
2297 } elsif ($bisect_ret_default eq "bad") {
2298 return 0;
2299 } elsif ($bisect_ret_default eq "skip") {
2300 return -1;
2301 } elsif ($bisect_ret_default eq "abort") {
2302 return -2;
2303 } else {
2304 fail "unknown default action: $bisect_ret_default"
2305 and return -2;
2306 }
2307 }
2308 }
2309
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002310 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002311 return 0 if $in_bisect;
2312 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002313 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002314 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002315}
2316
Steven Rostedta75fece2010-11-02 14:58:27 -04002317sub run_git_bisect {
2318 my ($command) = @_;
2319
2320 doprint "$command ... ";
2321
2322 my $output = `$command 2>&1`;
2323 my $ret = $?;
2324
2325 logit $output;
2326
2327 if ($ret) {
2328 doprint "FAILED\n";
2329 dodie "Failed to git bisect";
2330 }
2331
2332 doprint "SUCCESS\n";
2333 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2334 doprint "$1 [$2]\n";
2335 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002336 $bisect_bad_commit = $1;
Steven Rostedta75fece2010-11-02 14:58:27 -04002337 doprint "Found bad commit... $1\n";
2338 return 0;
2339 } else {
2340 # we already logged it, just print it now.
2341 print $output;
2342 }
2343
2344 return 1;
2345}
2346
Steven Rostedtc23dca72011-03-08 09:26:31 -05002347sub bisect_reboot {
2348 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002349 reboot_to_good $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05002350}
2351
2352# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05002353sub run_bisect_test {
2354 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002355
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002356 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002357 my $result;
2358 my $output;
2359 my $ret;
2360
Steven Rostedt0a05c762010-11-08 11:14:10 -05002361 $in_bisect = 1;
2362
2363 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002364
2365 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002366 if ($failed && $bisect_skip) {
2367 $in_bisect = 0;
2368 return -1;
2369 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002370 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002371
2372 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04002373 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002374
2375 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002376 if ($failed && $bisect_skip) {
2377 end_monitor;
2378 bisect_reboot;
2379 $in_bisect = 0;
2380 return -1;
2381 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002382 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002383
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002384 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002385 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002386 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002387 }
2388
2389 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002390 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002391 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002392 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002393 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04002394
2395 # reboot the box to a kernel we can ssh to
2396 if ($type ne "build") {
2397 bisect_reboot;
2398 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002399 $in_bisect = 0;
2400
2401 return $result;
2402}
2403
2404sub run_bisect {
2405 my ($type) = @_;
2406 my $buildtype = "oldconfig";
2407
2408 # We should have a minconfig to use?
2409 if (defined($minconfig)) {
2410 $buildtype = "useconfig:$minconfig";
2411 }
2412
2413 my $ret = run_bisect_test $type, $buildtype;
2414
Steven Rostedtc960bb92011-03-08 09:22:39 -05002415 if ($bisect_manual) {
2416 $ret = answer_bisect;
2417 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002418
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002419 # Are we looking for where it worked, not failed?
Russ Dill5158ba3e2012-04-23 19:43:00 -07002420 if ($reverse_bisect && $ret >= 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002421 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002422 }
2423
Steven Rostedtc23dca72011-03-08 09:26:31 -05002424 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002425 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002426 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002427 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002428 } elsif ($bisect_skip) {
2429 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2430 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002431 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002432}
2433
Steven Rostedtdad98752011-11-22 20:48:57 -05002434sub update_bisect_replay {
2435 my $tmp_log = "$tmpdir/ktest_bisect_log";
2436 run_command "git bisect log > $tmp_log" or
2437 die "can't create bisect log";
2438 return $tmp_log;
2439}
2440
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002441sub bisect {
2442 my ($i) = @_;
2443
2444 my $result;
2445
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002446 die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2447 die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
2448 die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002449
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002450 my $good = $bisect_good;
2451 my $bad = $bisect_bad;
2452 my $type = $bisect_type;
2453 my $start = $bisect_start;
2454 my $replay = $bisect_replay;
2455 my $start_files = $bisect_files;
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002456
2457 if (defined($start_files)) {
2458 $start_files = " -- " . $start_files;
2459 } else {
2460 $start_files = "";
2461 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002462
Steven Rostedta57419b2010-11-02 15:13:54 -04002463 # convert to true sha1's
2464 $good = get_sha1($good);
2465 $bad = get_sha1($bad);
2466
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002467 if (defined($bisect_reverse) && $bisect_reverse == 1) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002468 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2469 $reverse_bisect = 1;
2470 } else {
2471 $reverse_bisect = 0;
2472 }
2473
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002474 # Can't have a test without having a test to run
2475 if ($type eq "test" && !defined($run_test)) {
2476 $type = "boot";
2477 }
2478
Steven Rostedtdad98752011-11-22 20:48:57 -05002479 # Check if a bisect was running
2480 my $bisect_start_file = "$builddir/.git/BISECT_START";
2481
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002482 my $check = $bisect_check;
Steven Rostedtdad98752011-11-22 20:48:57 -05002483 my $do_check = defined($check) && $check ne "0";
2484
2485 if ( -f $bisect_start_file ) {
2486 print "Bisect in progress found\n";
2487 if ($do_check) {
2488 print " If you say yes, then no checks of good or bad will be done\n";
2489 }
2490 if (defined($replay)) {
2491 print "** BISECT_REPLAY is defined in config file **";
2492 print " Ignore config option and perform new git bisect log?\n";
2493 if (read_ync " (yes, no, or cancel) ") {
2494 $replay = update_bisect_replay;
2495 $do_check = 0;
2496 }
2497 } elsif (read_yn "read git log and continue?") {
2498 $replay = update_bisect_replay;
2499 $do_check = 0;
2500 }
2501 }
2502
2503 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002504
2505 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002506 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002507
2508 if ($check ne "good") {
2509 doprint "TESTING BISECT BAD [$bad]\n";
2510 run_command "git checkout $bad" or
2511 die "Failed to checkout $bad";
2512
2513 $result = run_bisect $type;
2514
2515 if ($result ne "bad") {
2516 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2517 }
2518 }
2519
2520 if ($check ne "bad") {
2521 doprint "TESTING BISECT GOOD [$good]\n";
2522 run_command "git checkout $good" or
2523 die "Failed to checkout $good";
2524
2525 $result = run_bisect $type;
2526
2527 if ($result ne "good") {
2528 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2529 }
2530 }
2531
2532 # checkout where we started
2533 run_command "git checkout $head" or
2534 die "Failed to checkout $head";
2535 }
2536
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002537 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002538 dodie "could not start bisect";
2539
2540 run_command "git bisect good $good" or
2541 dodie "could not set bisect good to $good";
2542
2543 run_git_bisect "git bisect bad $bad" or
2544 dodie "could not set bisect bad to $bad";
2545
2546 if (defined($replay)) {
2547 run_command "git bisect replay $replay" or
2548 dodie "failed to run replay";
2549 }
2550
2551 if (defined($start)) {
2552 run_command "git checkout $start" or
2553 dodie "failed to checkout $start";
2554 }
2555
2556 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002557 do {
2558 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002559 $test = run_git_bisect "git bisect $result";
2560 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002561
2562 run_command "git bisect log" or
2563 dodie "could not capture git bisect log";
2564
2565 run_command "git bisect reset" or
2566 dodie "could not reset git bisect";
2567
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002568 doprint "Bad commit was [$bisect_bad_commit]\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002569
Steven Rostedt0a05c762010-11-08 11:14:10 -05002570 success $i;
2571}
2572
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002573# config_ignore holds the configs that were set (or unset) for
2574# a good config and we will ignore these configs for the rest
2575# of a config bisect. These configs stay as they were.
Steven Rostedt0a05c762010-11-08 11:14:10 -05002576my %config_ignore;
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002577
2578# config_set holds what all configs were set as.
Steven Rostedt0a05c762010-11-08 11:14:10 -05002579my %config_set;
2580
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002581# config_off holds the set of configs that the bad config had disabled.
2582# We need to record them and set them in the .config when running
Adam Leefb16d892012-09-01 01:05:17 +08002583# olddefconfig, because olddefconfig keeps the defaults.
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002584my %config_off;
2585
2586# config_off_tmp holds a set of configs to turn off for now
2587my @config_off_tmp;
2588
2589# config_list is the set of configs that are being tested
Steven Rostedt0a05c762010-11-08 11:14:10 -05002590my %config_list;
2591my %null_config;
2592
2593my %dependency;
2594
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002595sub assign_configs {
2596 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002597
2598 open (IN, $config)
2599 or dodie "Failed to read $config";
2600
2601 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002602 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002603 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002604 }
2605 }
2606
2607 close(IN);
2608}
2609
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002610sub process_config_ignore {
2611 my ($config) = @_;
2612
2613 assign_configs \%config_ignore, $config;
2614}
2615
Steven Rostedt0a05c762010-11-08 11:14:10 -05002616sub read_current_config {
2617 my ($config_ref) = @_;
2618
2619 %{$config_ref} = ();
2620 undef %{$config_ref};
2621
2622 my @key = keys %{$config_ref};
2623 if ($#key >= 0) {
2624 print "did not delete!\n";
2625 exit;
2626 }
2627 open (IN, "$output_config");
2628
2629 while (<IN>) {
2630 if (/^(CONFIG\S+)=(.*)/) {
2631 ${$config_ref}{$1} = $2;
2632 }
2633 }
2634 close(IN);
2635}
2636
2637sub get_dependencies {
2638 my ($config) = @_;
2639
2640 my $arr = $dependency{$config};
2641 if (!defined($arr)) {
2642 return ();
2643 }
2644
2645 my @deps = @{$arr};
2646
2647 foreach my $dep (@{$arr}) {
2648 print "ADD DEP $dep\n";
2649 @deps = (@deps, get_dependencies $dep);
2650 }
2651
2652 return @deps;
2653}
2654
2655sub create_config {
2656 my @configs = @_;
2657
2658 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2659
2660 foreach my $config (@configs) {
2661 print OUT "$config_set{$config}\n";
2662 my @deps = get_dependencies $config;
2663 foreach my $dep (@deps) {
2664 print OUT "$config_set{$dep}\n";
2665 }
2666 }
2667
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002668 # turn off configs to keep off
2669 foreach my $config (keys %config_off) {
2670 print OUT "# $config is not set\n";
2671 }
2672
2673 # turn off configs that should be off for now
2674 foreach my $config (@config_off_tmp) {
2675 print OUT "# $config is not set\n";
2676 }
2677
Steven Rostedt0a05c762010-11-08 11:14:10 -05002678 foreach my $config (keys %config_ignore) {
2679 print OUT "$config_ignore{$config}\n";
2680 }
2681 close(OUT);
2682
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002683 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002684}
2685
2686sub compare_configs {
2687 my (%a, %b) = @_;
2688
2689 foreach my $item (keys %a) {
2690 if (!defined($b{$item})) {
2691 print "diff $item\n";
2692 return 1;
2693 }
2694 delete $b{$item};
2695 }
2696
2697 my @keys = keys %b;
2698 if ($#keys) {
2699 print "diff2 $keys[0]\n";
2700 }
2701 return -1 if ($#keys >= 0);
2702
2703 return 0;
2704}
2705
2706sub run_config_bisect_test {
2707 my ($type) = @_;
2708
2709 return run_bisect_test $type, "oldconfig";
2710}
2711
2712sub process_passed {
2713 my (%configs) = @_;
2714
2715 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2716 # Passed! All these configs are part of a good compile.
2717 # Add them to the min options.
2718 foreach my $config (keys %configs) {
2719 if (defined($config_list{$config})) {
2720 doprint " removing $config\n";
2721 $config_ignore{$config} = $config_list{$config};
2722 delete $config_list{$config};
2723 }
2724 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002725 doprint "config copied to $outputdir/config_good\n";
2726 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002727}
2728
2729sub process_failed {
2730 my ($config) = @_;
2731
2732 doprint "\n\n***************************************\n";
2733 doprint "Found bad config: $config\n";
2734 doprint "***************************************\n\n";
2735}
2736
2737sub run_config_bisect {
2738
2739 my @start_list = keys %config_list;
2740
2741 if ($#start_list < 0) {
2742 doprint "No more configs to test!!!\n";
2743 return -1;
2744 }
2745
2746 doprint "***** RUN TEST ***\n";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002747 my $type = $config_bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002748 my $ret;
2749 my %current_config;
2750
2751 my $count = $#start_list + 1;
2752 doprint " $count configs to test\n";
2753
2754 my $half = int($#start_list / 2);
2755
2756 do {
2757 my @tophalf = @start_list[0 .. $half];
2758
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002759 # keep the bottom half off
2760 if ($half < $#start_list) {
2761 @config_off_tmp = @start_list[$half + 1 .. $#start_list];
2762 } else {
2763 @config_off_tmp = ();
2764 }
2765
Steven Rostedt0a05c762010-11-08 11:14:10 -05002766 create_config @tophalf;
2767 read_current_config \%current_config;
2768
2769 $count = $#tophalf + 1;
2770 doprint "Testing $count configs\n";
2771 my $found = 0;
2772 # make sure we test something
2773 foreach my $config (@tophalf) {
2774 if (defined($current_config{$config})) {
2775 logit " $config\n";
2776 $found = 1;
2777 }
2778 }
2779 if (!$found) {
2780 # try the other half
2781 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002782
2783 # keep the top half off
2784 @config_off_tmp = @tophalf;
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002785 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002786
Steven Rostedt0a05c762010-11-08 11:14:10 -05002787 create_config @tophalf;
2788 read_current_config \%current_config;
2789 foreach my $config (@tophalf) {
2790 if (defined($current_config{$config})) {
2791 logit " $config\n";
2792 $found = 1;
2793 }
2794 }
2795 if (!$found) {
2796 doprint "Failed: Can't make new config with current configs\n";
2797 foreach my $config (@start_list) {
2798 doprint " CONFIG: $config\n";
2799 }
2800 return -1;
2801 }
2802 $count = $#tophalf + 1;
2803 doprint "Testing $count configs\n";
2804 }
2805
2806 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002807 if ($bisect_manual) {
2808 $ret = answer_bisect;
2809 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002810 if ($ret) {
2811 process_passed %current_config;
2812 return 0;
2813 }
2814
2815 doprint "This config had a failure.\n";
2816 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002817 doprint "config copied to $outputdir/config_bad\n";
2818 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002819
2820 # A config exists in this group that was bad.
2821 foreach my $config (keys %config_list) {
2822 if (!defined($current_config{$config})) {
2823 doprint " removing $config\n";
2824 delete $config_list{$config};
2825 }
2826 }
2827
2828 @start_list = @tophalf;
2829
2830 if ($#start_list == 0) {
2831 process_failed $start_list[0];
2832 return 1;
2833 }
2834
2835 # remove half the configs we are looking at and see if
2836 # they are good.
2837 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002838 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002839
Steven Rostedtc960bb92011-03-08 09:22:39 -05002840 # we found a single config, try it again unless we are running manually
2841
2842 if ($bisect_manual) {
2843 process_failed $start_list[0];
2844 return 1;
2845 }
2846
Steven Rostedt0a05c762010-11-08 11:14:10 -05002847 my @tophalf = @start_list[0 .. 0];
2848
2849 $ret = run_config_bisect_test $type;
2850 if ($ret) {
2851 process_passed %current_config;
2852 return 0;
2853 }
2854
2855 process_failed $start_list[0];
2856 return 1;
2857}
2858
2859sub config_bisect {
2860 my ($i) = @_;
2861
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002862 my $start_config = $config_bisect;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002863
2864 my $tmpconfig = "$tmpdir/use_config";
2865
Steven Rostedt30f75da2011-06-13 10:35:35 -04002866 if (defined($config_bisect_good)) {
2867 process_config_ignore $config_bisect_good;
2868 }
2869
Steven Rostedt0a05c762010-11-08 11:14:10 -05002870 # Make the file with the bad config and the min config
2871 if (defined($minconfig)) {
2872 # read the min config for things to ignore
2873 run_command "cp $minconfig $tmpconfig" or
2874 dodie "failed to copy $minconfig to $tmpconfig";
2875 } else {
2876 unlink $tmpconfig;
2877 }
2878
Steven Rostedt0a05c762010-11-08 11:14:10 -05002879 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002880 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002881 process_config_ignore $tmpconfig;
2882 }
2883
2884 # now process the start config
2885 run_command "cp $start_config $output_config" or
2886 dodie "failed to copy $start_config to $output_config";
2887
2888 # read directly what we want to check
2889 my %config_check;
2890 open (IN, $output_config)
Masanari Iidaf9dee312012-02-11 21:46:56 +09002891 or dodie "failed to open $output_config";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002892
2893 while (<IN>) {
2894 if (/^((CONFIG\S*)=.*)/) {
2895 $config_check{$2} = $1;
2896 }
2897 }
2898 close(IN);
2899
Steven Rostedt250bae82011-07-15 22:05:59 -04002900 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002901 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002902
2903 # check to see what we lost (or gained)
2904 open (IN, $output_config)
2905 or dodie "Failed to read $start_config";
2906
2907 my %removed_configs;
2908 my %added_configs;
2909
2910 while (<IN>) {
2911 if (/^((CONFIG\S*)=.*)/) {
2912 # save off all options
2913 $config_set{$2} = $1;
2914 if (defined($config_check{$2})) {
2915 if (defined($config_ignore{$2})) {
2916 $removed_configs{$2} = $1;
2917 } else {
2918 $config_list{$2} = $1;
2919 }
2920 } elsif (!defined($config_ignore{$2})) {
2921 $added_configs{$2} = $1;
2922 $config_list{$2} = $1;
2923 }
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002924 } elsif (/^# ((CONFIG\S*).*)/) {
2925 # Keep these configs disabled
2926 $config_set{$2} = $1;
2927 $config_off{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002928 }
2929 }
2930 close(IN);
2931
2932 my @confs = keys %removed_configs;
2933 if ($#confs >= 0) {
2934 doprint "Configs overridden by default configs and removed from check:\n";
2935 foreach my $config (@confs) {
2936 doprint " $config\n";
2937 }
2938 }
2939 @confs = keys %added_configs;
2940 if ($#confs >= 0) {
2941 doprint "Configs appearing in make oldconfig and added:\n";
2942 foreach my $config (@confs) {
2943 doprint " $config\n";
2944 }
2945 }
2946
2947 my %config_test;
2948 my $once = 0;
2949
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002950 @config_off_tmp = ();
2951
Steven Rostedt0a05c762010-11-08 11:14:10 -05002952 # Sometimes kconfig does weird things. We must make sure
2953 # that the config we autocreate has everything we need
2954 # to test, otherwise we may miss testing configs, or
2955 # may not be able to create a new config.
2956 # Here we create a config with everything set.
2957 create_config (keys %config_list);
2958 read_current_config \%config_test;
2959 foreach my $config (keys %config_list) {
2960 if (!defined($config_test{$config})) {
2961 if (!$once) {
2962 $once = 1;
2963 doprint "Configs not produced by kconfig (will not be checked):\n";
2964 }
2965 doprint " $config\n";
2966 delete $config_list{$config};
2967 }
2968 }
2969 my $ret;
Steven Rostedtb0918612012-07-19 15:26:00 -04002970
2971 if (defined($config_bisect_check) && $config_bisect_check) {
2972 doprint " Checking to make sure bad config with min config fails\n";
2973 create_config keys %config_list;
2974 $ret = run_config_bisect_test $config_bisect_type;
2975 if ($ret) {
2976 doprint " FAILED! Bad config with min config boots fine\n";
2977 return -1;
2978 }
2979 doprint " Bad config with min config fails as expected\n";
2980 }
2981
Steven Rostedt0a05c762010-11-08 11:14:10 -05002982 do {
2983 $ret = run_config_bisect;
2984 } while (!$ret);
2985
2986 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002987
2988 success $i;
2989}
2990
Steven Rostedt27d934b2011-05-20 09:18:18 -04002991sub patchcheck_reboot {
2992 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002993 reboot_to_good $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04002994}
2995
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002996sub patchcheck {
2997 my ($i) = @_;
2998
2999 die "PATCHCHECK_START[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003000 if (!defined($patchcheck_start));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003001 die "PATCHCHECK_TYPE[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003002 if (!defined($patchcheck_type));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003003
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003004 my $start = $patchcheck_start;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003005
3006 my $end = "HEAD";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003007 if (defined($patchcheck_end)) {
3008 $end = $patchcheck_end;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003009 }
3010
Steven Rostedta57419b2010-11-02 15:13:54 -04003011 # Get the true sha1's since we can use things like HEAD~3
3012 $start = get_sha1($start);
3013 $end = get_sha1($end);
3014
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003015 my $type = $patchcheck_type;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003016
3017 # Can't have a test without having a test to run
3018 if ($type eq "test" && !defined($run_test)) {
3019 $type = "boot";
3020 }
3021
3022 open (IN, "git log --pretty=oneline $end|") or
3023 dodie "could not get git list";
3024
3025 my @list;
3026
3027 while (<IN>) {
3028 chomp;
3029 $list[$#list+1] = $_;
3030 last if (/^$start/);
3031 }
3032 close(IN);
3033
3034 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003035 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003036 }
3037
3038 # go backwards in the list
3039 @list = reverse @list;
3040
3041 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04003042 my %ignored_warnings;
3043
3044 if (defined($ignore_warnings)) {
3045 foreach my $sha1 (split /\s+/, $ignore_warnings) {
3046 $ignored_warnings{$sha1} = 1;
3047 }
3048 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003049
3050 $in_patchcheck = 1;
3051 foreach my $item (@list) {
3052 my $sha1 = $item;
3053 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
3054
3055 doprint "\nProcessing commit $item\n\n";
3056
3057 run_command "git checkout $sha1" or
3058 die "Failed to checkout $sha1";
3059
3060 # only clean on the first and last patch
3061 if ($item eq $list[0] ||
3062 $item eq $list[$#list]) {
3063 $noclean = $save_clean;
3064 } else {
3065 $noclean = 1;
3066 }
3067
3068 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003069 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003070 } else {
3071 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003072 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003073 }
3074
Steven Rostedt19902072011-06-14 20:46:25 -04003075
3076 if (!defined($ignored_warnings{$sha1})) {
3077 check_buildlog $sha1 or return 0;
3078 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003079
3080 next if ($type eq "build");
3081
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003082 my $failed = 0;
3083
Steven Rostedtddf607e2011-06-14 20:49:13 -04003084 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003085
3086 if (!$failed && $type ne "boot"){
3087 do_run_test or $failed = 1;
3088 }
3089 end_monitor;
3090 return 0 if ($failed);
3091
Steven Rostedt27d934b2011-05-20 09:18:18 -04003092 patchcheck_reboot;
3093
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003094 }
3095 $in_patchcheck = 0;
3096 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003097
3098 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003099}
3100
Steven Rostedtb9066f62011-07-15 21:25:24 -04003101my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003102my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003103my $iflevel = 0;
3104my @ifdeps;
3105
3106# prevent recursion
3107my %read_kconfigs;
3108
Steven Rostedtac6974c2011-10-04 09:40:17 -04003109sub add_dep {
3110 # $config depends on $dep
3111 my ($config, $dep) = @_;
3112
3113 if (defined($depends{$config})) {
3114 $depends{$config} .= " " . $dep;
3115 } else {
3116 $depends{$config} = $dep;
3117 }
3118
3119 # record the number of configs depending on $dep
3120 if (defined $depcount{$dep}) {
3121 $depcount{$dep}++;
3122 } else {
3123 $depcount{$dep} = 1;
3124 }
3125}
3126
Steven Rostedtb9066f62011-07-15 21:25:24 -04003127# taken from streamline_config.pl
3128sub read_kconfig {
3129 my ($kconfig) = @_;
3130
3131 my $state = "NONE";
3132 my $config;
3133 my @kconfigs;
3134
3135 my $cont = 0;
3136 my $line;
3137
3138
3139 if (! -f $kconfig) {
3140 doprint "file $kconfig does not exist, skipping\n";
3141 return;
3142 }
3143
3144 open(KIN, "$kconfig")
3145 or die "Can't open $kconfig";
3146 while (<KIN>) {
3147 chomp;
3148
3149 # Make sure that lines ending with \ continue
3150 if ($cont) {
3151 $_ = $line . " " . $_;
3152 }
3153
3154 if (s/\\$//) {
3155 $cont = 1;
3156 $line = $_;
3157 next;
3158 }
3159
3160 $cont = 0;
3161
3162 # collect any Kconfig sources
3163 if (/^source\s*"(.*)"/) {
3164 $kconfigs[$#kconfigs+1] = $1;
3165 }
3166
3167 # configs found
3168 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
3169 $state = "NEW";
3170 $config = $2;
3171
3172 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04003173 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04003174 }
3175
3176 # collect the depends for the config
3177 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
3178
Steven Rostedtac6974c2011-10-04 09:40:17 -04003179 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003180
3181 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04003182 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
3183
3184 # selected by depends on config
3185 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003186
3187 # Check for if statements
3188 } elsif (/^if\s+(.*\S)\s*$/) {
3189 my $deps = $1;
3190 # remove beginning and ending non text
3191 $deps =~ s/^[^a-zA-Z0-9_]*//;
3192 $deps =~ s/[^a-zA-Z0-9_]*$//;
3193
3194 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
3195
3196 $ifdeps[$iflevel++] = join ':', @deps;
3197
3198 } elsif (/^endif/) {
3199
3200 $iflevel-- if ($iflevel);
3201
3202 # stop on "help"
3203 } elsif (/^\s*help\s*$/) {
3204 $state = "NONE";
3205 }
3206 }
3207 close(KIN);
3208
3209 # read in any configs that were found.
3210 foreach $kconfig (@kconfigs) {
3211 if (!defined($read_kconfigs{$kconfig})) {
3212 $read_kconfigs{$kconfig} = 1;
3213 read_kconfig("$builddir/$kconfig");
3214 }
3215 }
3216}
3217
3218sub read_depends {
3219 # find out which arch this is by the kconfig file
3220 open (IN, $output_config)
3221 or dodie "Failed to read $output_config";
3222 my $arch;
3223 while (<IN>) {
3224 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
3225 $arch = $1;
3226 last;
3227 }
3228 }
3229 close IN;
3230
3231 if (!defined($arch)) {
3232 doprint "Could not find arch from config file\n";
3233 doprint "no dependencies used\n";
3234 return;
3235 }
3236
3237 # arch is really the subarch, we need to know
3238 # what directory to look at.
3239 if ($arch eq "i386" || $arch eq "x86_64") {
3240 $arch = "x86";
3241 } elsif ($arch =~ /^tile/) {
3242 $arch = "tile";
3243 }
3244
3245 my $kconfig = "$builddir/arch/$arch/Kconfig";
3246
3247 if (! -f $kconfig && $arch =~ /\d$/) {
3248 my $orig = $arch;
3249 # some subarchs have numbers, truncate them
3250 $arch =~ s/\d*$//;
3251 $kconfig = "$builddir/arch/$arch/Kconfig";
3252 if (! -f $kconfig) {
3253 doprint "No idea what arch dir $orig is for\n";
3254 doprint "no dependencies used\n";
3255 return;
3256 }
3257 }
3258
3259 read_kconfig($kconfig);
3260}
3261
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003262sub read_config_list {
3263 my ($config) = @_;
3264
3265 open (IN, $config)
3266 or dodie "Failed to read $config";
3267
3268 while (<IN>) {
3269 if (/^((CONFIG\S*)=.*)/) {
3270 if (!defined($config_ignore{$2})) {
3271 $config_list{$2} = $1;
3272 }
3273 }
3274 }
3275
3276 close(IN);
3277}
3278
3279sub read_output_config {
3280 my ($config) = @_;
3281
3282 assign_configs \%config_ignore, $config;
3283}
3284
3285sub make_new_config {
3286 my @configs = @_;
3287
3288 open (OUT, ">$output_config")
3289 or dodie "Failed to write $output_config";
3290
3291 foreach my $config (@configs) {
3292 print OUT "$config\n";
3293 }
3294 close OUT;
3295}
3296
Steven Rostedtac6974c2011-10-04 09:40:17 -04003297sub chomp_config {
3298 my ($config) = @_;
3299
3300 $config =~ s/CONFIG_//;
3301
3302 return $config;
3303}
3304
Steven Rostedtb9066f62011-07-15 21:25:24 -04003305sub get_depends {
3306 my ($dep) = @_;
3307
Steven Rostedtac6974c2011-10-04 09:40:17 -04003308 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003309
3310 $dep = $depends{"$kconfig"};
3311
3312 # the dep string we have saves the dependencies as they
3313 # were found, including expressions like ! && ||. We
3314 # want to split this out into just an array of configs.
3315
3316 my $valid = "A-Za-z_0-9";
3317
3318 my @configs;
3319
3320 while ($dep =~ /[$valid]/) {
3321
3322 if ($dep =~ /^[^$valid]*([$valid]+)/) {
3323 my $conf = "CONFIG_" . $1;
3324
3325 $configs[$#configs + 1] = $conf;
3326
3327 $dep =~ s/^[^$valid]*[$valid]+//;
3328 } else {
3329 die "this should never happen";
3330 }
3331 }
3332
3333 return @configs;
3334}
3335
3336my %min_configs;
3337my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003338my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003339my %processed_configs;
3340my %nochange_config;
3341
3342sub test_this_config {
3343 my ($config) = @_;
3344
3345 my $found;
3346
3347 # if we already processed this config, skip it
3348 if (defined($processed_configs{$config})) {
3349 return undef;
3350 }
3351 $processed_configs{$config} = 1;
3352
3353 # if this config failed during this round, skip it
3354 if (defined($nochange_config{$config})) {
3355 return undef;
3356 }
3357
Steven Rostedtac6974c2011-10-04 09:40:17 -04003358 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003359
3360 # Test dependencies first
3361 if (defined($depends{"$kconfig"})) {
3362 my @parents = get_depends $config;
3363 foreach my $parent (@parents) {
3364 # if the parent is in the min config, check it first
3365 next if (!defined($min_configs{$parent}));
3366 $found = test_this_config($parent);
3367 if (defined($found)) {
3368 return $found;
3369 }
3370 }
3371 }
3372
3373 # Remove this config from the list of configs
Adam Leefb16d892012-09-01 01:05:17 +08003374 # do a make olddefconfig and then read the resulting
Steven Rostedtb9066f62011-07-15 21:25:24 -04003375 # .config to make sure it is missing the config that
3376 # we had before
3377 my %configs = %min_configs;
3378 delete $configs{$config};
3379 make_new_config ((values %configs), (values %keep_configs));
3380 make_oldconfig;
3381 undef %configs;
3382 assign_configs \%configs, $output_config;
3383
3384 return $config if (!defined($configs{$config}));
3385
3386 doprint "disabling config $config did not change .config\n";
3387
3388 $nochange_config{$config} = 1;
3389
3390 return undef;
3391}
3392
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003393sub make_min_config {
3394 my ($i) = @_;
3395
Steven Rostedtccc513b2012-05-21 17:13:40 -04003396 my $type = $minconfig_type;
3397 if ($type ne "boot" && $type ne "test") {
3398 fail "Invalid MIN_CONFIG_TYPE '$minconfig_type'\n" .
3399 " make_min_config works only with 'boot' and 'test'\n" and return;
3400 }
3401
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003402 if (!defined($output_minconfig)) {
3403 fail "OUTPUT_MIN_CONFIG not defined" and return;
3404 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04003405
3406 # If output_minconfig exists, and the start_minconfig
3407 # came from min_config, than ask if we should use
3408 # that instead.
3409 if (-f $output_minconfig && !$start_minconfig_defined) {
3410 print "$output_minconfig exists\n";
Steven Rostedt43de3312012-05-21 23:35:12 -04003411 if (!defined($use_output_minconfig)) {
3412 if (read_yn " Use it as minconfig?") {
3413 $start_minconfig = $output_minconfig;
3414 }
3415 } elsif ($use_output_minconfig > 0) {
3416 doprint "Using $output_minconfig as MIN_CONFIG\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003417 $start_minconfig = $output_minconfig;
Steven Rostedt43de3312012-05-21 23:35:12 -04003418 } else {
3419 doprint "Set to still use MIN_CONFIG as starting point\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003420 }
3421 }
3422
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003423 if (!defined($start_minconfig)) {
3424 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
3425 }
3426
Steven Rostedt35ce5952011-07-15 21:57:25 -04003427 my $temp_config = "$tmpdir/temp_config";
3428
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003429 # First things first. We build an allnoconfig to find
3430 # out what the defaults are that we can't touch.
3431 # Some are selections, but we really can't handle selections.
3432
3433 my $save_minconfig = $minconfig;
3434 undef $minconfig;
3435
3436 run_command "$make allnoconfig" or return 0;
3437
Steven Rostedtb9066f62011-07-15 21:25:24 -04003438 read_depends;
3439
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003440 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003441
Steven Rostedt43d1b652011-07-15 22:01:56 -04003442 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003443 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003444
3445 if (defined($ignore_config)) {
3446 # make sure the file exists
3447 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003448 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003449 }
3450
Steven Rostedt43d1b652011-07-15 22:01:56 -04003451 %keep_configs = %save_configs;
3452
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003453 doprint "Load initial configs from $start_minconfig\n";
3454
3455 # Look at the current min configs, and save off all the
3456 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003457 assign_configs \%min_configs, $start_minconfig;
3458
3459 my @config_keys = keys %min_configs;
3460
Steven Rostedtac6974c2011-10-04 09:40:17 -04003461 # All configs need a depcount
3462 foreach my $config (@config_keys) {
3463 my $kconfig = chomp_config $config;
3464 if (!defined $depcount{$kconfig}) {
3465 $depcount{$kconfig} = 0;
3466 }
3467 }
3468
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003469 # Remove anything that was set by the make allnoconfig
3470 # we shouldn't need them as they get set for us anyway.
3471 foreach my $config (@config_keys) {
3472 # Remove anything in the ignore_config
3473 if (defined($keep_configs{$config})) {
3474 my $file = $ignore_config;
3475 $file =~ s,.*/(.*?)$,$1,;
3476 doprint "$config set by $file ... ignored\n";
3477 delete $min_configs{$config};
3478 next;
3479 }
3480 # But make sure the settings are the same. If a min config
3481 # sets a selection, we do not want to get rid of it if
3482 # it is not the same as what we have. Just move it into
3483 # the keep configs.
3484 if (defined($config_ignore{$config})) {
3485 if ($config_ignore{$config} ne $min_configs{$config}) {
3486 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3487 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3488 $keep_configs{$config} = $min_configs{$config};
3489 } else {
3490 doprint "$config set by allnoconfig ... ignored\n";
3491 }
3492 delete $min_configs{$config};
3493 }
3494 }
3495
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003496 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003497 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003498
3499 while (!$done) {
3500
3501 my $config;
3502 my $found;
3503
3504 # Now disable each config one by one and do a make oldconfig
3505 # till we find a config that changes our list.
3506
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003507 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003508
3509 # Sort keys by who is most dependent on
3510 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3511 @test_configs ;
3512
3513 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003514 my $reset = 1;
3515 for (my $i = 0; $i < $#test_configs; $i++) {
3516 if (!defined($nochange_config{$test_configs[0]})) {
3517 $reset = 0;
3518 last;
3519 }
3520 # This config didn't change the .config last time.
3521 # Place it at the end
3522 my $config = shift @test_configs;
3523 push @test_configs, $config;
3524 }
3525
3526 # if every test config has failed to modify the .config file
3527 # in the past, then reset and start over.
3528 if ($reset) {
3529 undef %nochange_config;
3530 }
3531
Steven Rostedtb9066f62011-07-15 21:25:24 -04003532 undef %processed_configs;
3533
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003534 foreach my $config (@test_configs) {
3535
Steven Rostedtb9066f62011-07-15 21:25:24 -04003536 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003537
Steven Rostedtb9066f62011-07-15 21:25:24 -04003538 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003539
3540 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003541 }
3542
3543 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003544 # we could have failed due to the nochange_config hash
3545 # reset and try again
3546 if (!$take_two) {
3547 undef %nochange_config;
3548 $take_two = 1;
3549 next;
3550 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003551 doprint "No more configs found that we can disable\n";
3552 $done = 1;
3553 last;
3554 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003555 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003556
3557 $config = $found;
3558
3559 doprint "Test with $config disabled\n";
3560
3561 # set in_bisect to keep build and monitor from dieing
3562 $in_bisect = 1;
3563
3564 my $failed = 0;
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003565 build "oldconfig" or $failed = 1;
3566 if (!$failed) {
3567 start_monitor_and_boot or $failed = 1;
Steven Rostedtccc513b2012-05-21 17:13:40 -04003568
3569 if ($type eq "test" && !$failed) {
3570 do_run_test or $failed = 1;
3571 }
3572
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003573 end_monitor;
3574 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003575
3576 $in_bisect = 0;
3577
3578 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003579 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003580 # this config is needed, add it to the ignore list.
3581 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003582 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003583 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003584
3585 # update new ignore configs
3586 if (defined($ignore_config)) {
3587 open (OUT, ">$temp_config")
3588 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003589 foreach my $config (keys %save_configs) {
3590 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003591 }
3592 close OUT;
3593 run_command "mv $temp_config $ignore_config" or
3594 dodie "failed to copy update to $ignore_config";
3595 }
3596
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003597 } else {
3598 # We booted without this config, remove it from the minconfigs.
3599 doprint "$config is not needed, disabling\n";
3600
3601 delete $min_configs{$config};
3602
3603 # Also disable anything that is not enabled in this config
3604 my %configs;
3605 assign_configs \%configs, $output_config;
3606 my @config_keys = keys %min_configs;
3607 foreach my $config (@config_keys) {
3608 if (!defined($configs{$config})) {
3609 doprint "$config is not set, disabling\n";
3610 delete $min_configs{$config};
3611 }
3612 }
3613
3614 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003615 open (OUT, ">$temp_config")
3616 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003617 foreach my $config (keys %keep_configs) {
3618 print OUT "$keep_configs{$config}\n";
3619 }
3620 foreach my $config (keys %min_configs) {
3621 print OUT "$min_configs{$config}\n";
3622 }
3623 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003624
3625 run_command "mv $temp_config $output_minconfig" or
3626 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003627 }
3628
3629 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003630 reboot_to_good $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003631 }
3632
3633 success $i;
3634 return 1;
3635}
3636
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003637$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003638
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003639if ($#ARGV == 0) {
3640 $ktest_config = $ARGV[0];
3641 if (! -f $ktest_config) {
3642 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003643 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003644 exit 0;
3645 }
3646 }
3647} else {
3648 $ktest_config = "ktest.conf";
3649}
3650
3651if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003652 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003653 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003654 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3655 print OUT << "EOF"
3656# Generated by ktest.pl
3657#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003658
3659# PWD is a ktest.pl variable that will result in the process working
3660# directory that ktest.pl is executed in.
3661
3662# THIS_DIR is automatically assigned the PWD of the path that generated
3663# the config file. It is best to use this variable when assigning other
3664# directory paths within this directory. This allows you to easily
3665# move the test cases to other locations or to other machines.
3666#
3667THIS_DIR := $variable{"PWD"}
3668
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003669# Define each test with TEST_START
3670# The config options below it will override the defaults
3671TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003672TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003673
3674DEFAULTS
3675EOF
3676;
3677 close(OUT);
3678}
3679read_config $ktest_config;
3680
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003681if (defined($opt{"LOG_FILE"})) {
3682 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3683}
3684
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003685# Append any configs entered in manually to the config file.
3686my @new_configs = keys %entered_configs;
3687if ($#new_configs >= 0) {
3688 print "\nAppending entered in configs to $ktest_config\n";
3689 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3690 foreach my $config (@new_configs) {
3691 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003692 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003693 }
3694}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003695
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003696if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3697 unlink $opt{"LOG_FILE"};
3698}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003699
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003700doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3701
Steven Rostedta57419b2010-11-02 15:13:54 -04003702for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3703
3704 if (!$i) {
3705 doprint "DEFAULT OPTIONS:\n";
3706 } else {
3707 doprint "\nTEST $i OPTIONS";
3708 if (defined($repeat_tests{$i})) {
3709 $repeat = $repeat_tests{$i};
3710 doprint " ITERATE $repeat";
3711 }
3712 doprint "\n";
3713 }
3714
3715 foreach my $option (sort keys %opt) {
3716
3717 if ($option =~ /\[(\d+)\]$/) {
3718 next if ($i != $1);
3719 } else {
3720 next if ($i);
3721 }
3722
3723 doprint "$option = $opt{$option}\n";
3724 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003725}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003726
Steven Rostedt2a625122011-05-20 15:48:59 -04003727sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003728 my ($name, $i) = @_;
3729
3730 my $option = "$name\[$i\]";
3731
3732 if (defined($opt{$option})) {
3733 return $opt{$option};
3734 }
3735
Steven Rostedta57419b2010-11-02 15:13:54 -04003736 foreach my $test (keys %repeat_tests) {
3737 if ($i >= $test &&
3738 $i < $test + $repeat_tests{$test}) {
3739 $option = "$name\[$test\]";
3740 if (defined($opt{$option})) {
3741 return $opt{$option};
3742 }
3743 }
3744 }
3745
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003746 if (defined($opt{$name})) {
3747 return $opt{$name};
3748 }
3749
3750 return undef;
3751}
3752
Steven Rostedt2a625122011-05-20 15:48:59 -04003753sub set_test_option {
3754 my ($name, $i) = @_;
3755
3756 my $option = __set_test_option($name, $i);
3757 return $option if (!defined($option));
3758
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003759 return eval_option($option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003760}
3761
Steven Rostedt2545eb62010-11-02 15:01:32 -04003762# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003763for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003764
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003765 # Do not reboot on failing test options
3766 $no_reboot = 1;
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003767 $reboot_success = 0;
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003768
Steven Rostedt683a3e62012-05-18 13:34:35 -04003769 $have_version = 0;
3770
Steven Rostedt576f6272010-11-02 14:58:38 -04003771 $iteration = $i;
3772
Steven Rostedtc1434dc2012-07-20 22:39:16 -04003773 undef %force_config;
3774
Steven Rostedta75fece2010-11-02 14:58:27 -04003775 my $makecmd = set_test_option("MAKE_CMD", $i);
3776
Steven Rostedt9cc9e092011-12-22 21:37:22 -05003777 # Load all the options into their mapped variable names
3778 foreach my $opt (keys %option_map) {
3779 ${$option_map{$opt}} = set_test_option($opt, $i);
3780 }
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003781
Steven Rostedt35ce5952011-07-15 21:57:25 -04003782 $start_minconfig_defined = 1;
3783
Steven Rostedt921ed4c2012-07-19 15:18:27 -04003784 # The first test may override the PRE_KTEST option
3785 if (defined($pre_ktest) && $i == 1) {
3786 doprint "\n";
3787 run_command $pre_ktest;
3788 }
3789
3790 # Any test can override the POST_KTEST option
3791 # The last test takes precedence.
3792 if (defined($post_ktest)) {
3793 $final_post_ktest = $post_ktest;
3794 }
3795
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003796 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003797 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003798 $start_minconfig = $minconfig;
3799 }
3800
Steven Rostedta75fece2010-11-02 14:58:27 -04003801 chdir $builddir || die "can't change directory to $builddir";
3802
Andrew Jonesa908a662011-08-12 15:32:03 +02003803 foreach my $dir ($tmpdir, $outputdir) {
3804 if (!-d $dir) {
3805 mkpath($dir) or
3806 die "can't create $dir";
3807 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003808 }
3809
Steven Rostedte48c5292010-11-02 14:35:37 -04003810 $ENV{"SSH_USER"} = $ssh_user;
3811 $ENV{"MACHINE"} = $machine;
3812
Steven Rostedta75fece2010-11-02 14:58:27 -04003813 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303814 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003815 $dmesg = "$tmpdir/dmesg-$machine";
3816 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003817 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003818
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003819 if (!$buildonly) {
3820 $target = "$ssh_user\@$machine";
3821 if ($reboot_type eq "grub") {
3822 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
Steven Rostedta15ba912012-11-13 14:30:37 -05003823 } elsif ($reboot_type eq "grub2") {
3824 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3825 dodie "GRUB_FILE not defined" if (!defined($grub_file));
Steven Rostedt77869542012-12-11 17:37:41 -05003826 } elsif ($reboot_type eq "syslinux") {
3827 dodie "SYSLINUX_LABEL not defined" if (!defined($syslinux_label));
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003828 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003829 }
3830
3831 my $run_type = $build_type;
3832 if ($test_type eq "patchcheck") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003833 $run_type = $patchcheck_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003834 } elsif ($test_type eq "bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003835 $run_type = $bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003836 } elsif ($test_type eq "config_bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003837 $run_type = $config_bisect_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003838 }
3839
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003840 if ($test_type eq "make_min_config") {
3841 $run_type = "";
3842 }
3843
Steven Rostedta75fece2010-11-02 14:58:27 -04003844 # mistake in config file?
3845 if (!defined($run_type)) {
3846 $run_type = "ERROR";
3847 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003848
Steven Rostedte0a87422011-09-30 17:50:48 -04003849 my $installme = "";
3850 $installme = " no_install" if ($no_install);
3851
Steven Rostedt2545eb62010-11-02 15:01:32 -04003852 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003853 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003854
Steven Rostedt921ed4c2012-07-19 15:18:27 -04003855 if (defined($pre_test)) {
3856 run_command $pre_test;
3857 }
3858
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003859 unlink $dmesg;
3860 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303861 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003862
Steven Rostedt250bae82011-07-15 22:05:59 -04003863 if (defined($addconfig)) {
3864 my $min = $minconfig;
3865 if (!defined($minconfig)) {
3866 $min = "";
3867 }
3868 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003869 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003870 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003871 }
3872
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003873 if (defined($checkout)) {
3874 run_command "git checkout $checkout" or
3875 die "failed to checkout $checkout";
3876 }
3877
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003878 $no_reboot = 0;
3879
Steven Rostedt648a1822012-03-21 11:18:27 -04003880 # A test may opt to not reboot the box
3881 if ($reboot_on_success) {
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003882 $reboot_success = 1;
Steven Rostedt648a1822012-03-21 11:18:27 -04003883 }
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003884
Steven Rostedta75fece2010-11-02 14:58:27 -04003885 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003886 bisect $i;
3887 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003888 } elsif ($test_type eq "config_bisect") {
3889 config_bisect $i;
3890 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003891 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003892 patchcheck $i;
3893 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003894 } elsif ($test_type eq "make_min_config") {
3895 make_min_config $i;
3896 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003897 }
3898
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003899 if ($build_type ne "nobuild") {
3900 build $build_type or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003901 }
3902
Steven Rostedtcd8e3682011-08-18 16:35:44 -04003903 if ($test_type eq "install") {
3904 get_version;
3905 install;
3906 success $i;
3907 next;
3908 }
3909
Steven Rostedta75fece2010-11-02 14:58:27 -04003910 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04003911 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04003912 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04003913
3914 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3915 do_run_test or $failed = 1;
3916 }
3917 end_monitor;
3918 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003919 }
3920
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003921 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003922}
3923
Steven Rostedt921ed4c2012-07-19 15:18:27 -04003924if (defined($final_post_ktest)) {
3925 run_command $final_post_ktest;
3926}
3927
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003928if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003929 halt;
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003930} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot && $reboot_success) {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003931 reboot_to_good;
Steven Rostedt648a1822012-03-21 11:18:27 -04003932} elsif (defined($switch_to_good)) {
3933 # still need to get to the good kernel
3934 run_command $switch_to_good;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003935}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003936
Steven Rostedt648a1822012-03-21 11:18:27 -04003937
Steven Rostedte48c5292010-11-02 14:35:37 -04003938doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3939
Steven Rostedt2545eb62010-11-02 15:01:32 -04003940exit 0;