blob: 63245a56aca37a2e7b74358cae1fe5f6b838c4f7 [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 Rostedt600bbf02011-11-21 20:12:04 -050057
58# required, and we will ask users if they don't have them but we keep the default
59# value something that is common.
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050060 "REBOOT_TYPE" => "grub",
61 "LOCALVERSION" => "-test",
62 "SSH_USER" => "root",
63 "BUILD_TARGET" => "arch/x86/boot/bzImage",
64 "TARGET_IMAGE" => "/boot/vmlinuz-test",
Steven Rostedt9cc9e092011-12-22 21:37:22 -050065
66 "LOG_FILE" => undef,
67 "IGNORE_UNUSED" => 0,
Steven Rostedt4f43e0d2011-12-22 21:32:05 -050068);
Steven Rostedt2545eb62010-11-02 15:01:32 -040069
Steven Rostedt8d1491b2010-11-18 15:39:48 -050070my $ktest_config;
Steven Rostedt2545eb62010-11-02 15:01:32 -040071my $version;
Steven Rostedt683a3e62012-05-18 13:34:35 -040072my $have_version = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -040073my $machine;
Steven Rostedte48c5292010-11-02 14:35:37 -040074my $ssh_user;
Steven Rostedta75fece2010-11-02 14:58:27 -040075my $tmpdir;
76my $builddir;
77my $outputdir;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -050078my $output_config;
Steven Rostedta75fece2010-11-02 14:58:27 -040079my $test_type;
Steven Rostedt7faafbd2010-11-02 14:58:22 -040080my $build_type;
Steven Rostedta75fece2010-11-02 14:58:27 -040081my $build_options;
Steven Rostedt921ed4c2012-07-19 15:18:27 -040082my $final_post_ktest;
83my $pre_ktest;
84my $post_ktest;
85my $pre_test;
86my $post_test;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -040087my $pre_build;
88my $post_build;
89my $pre_build_die;
90my $post_build_die;
Steven Rostedta75fece2010-11-02 14:58:27 -040091my $reboot_type;
92my $reboot_script;
93my $power_cycle;
Steven Rostedte48c5292010-11-02 14:35:37 -040094my $reboot;
Steven Rostedta75fece2010-11-02 14:58:27 -040095my $reboot_on_error;
Steven Rostedtbc7c5802011-12-22 16:29:10 -050096my $switch_to_good;
97my $switch_to_test;
Steven Rostedta75fece2010-11-02 14:58:27 -040098my $poweroff_on_error;
Steven Rostedt648a1822012-03-21 11:18:27 -040099my $reboot_on_success;
Steven Rostedta75fece2010-11-02 14:58:27 -0400100my $die_on_failure;
Steven Rostedt576f6272010-11-02 14:58:38 -0400101my $powercycle_after_reboot;
102my $poweroff_after_halt;
Steven Rostedt407b95b2012-07-19 16:05:42 -0400103my $max_monitor_wait;
Steven Rostedte48c5292010-11-02 14:35:37 -0400104my $ssh_exec;
105my $scp_to_target;
Steven Rostedt02ad2612012-03-21 08:21:24 -0400106my $scp_to_target_install;
Steven Rostedta75fece2010-11-02 14:58:27 -0400107my $power_off;
108my $grub_menu;
Steven Rostedta15ba912012-11-13 14:30:37 -0500109my $grub_file;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400110my $grub_number;
Steven Rostedta15ba912012-11-13 14:30:37 -0500111my $grub_reboot;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400112my $target;
113my $make;
Steven Rostedte5c2ec12012-07-19 15:22:05 -0400114my $pre_install;
Steven Rostedt8b37ca82010-11-02 14:58:33 -0400115my $post_install;
Steven Rostedte0a87422011-09-30 17:50:48 -0400116my $no_install;
Steven Rostedt5c42fc52010-11-02 14:57:01 -0400117my $noclean;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400118my $minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400119my $start_minconfig;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400120my $start_minconfig_defined;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400121my $output_minconfig;
Steven Rostedtccc513b2012-05-21 17:13:40 -0400122my $minconfig_type;
Steven Rostedt43de3312012-05-21 23:35:12 -0400123my $use_output_minconfig;
Steven Rostedt4c4ab122011-07-15 21:16:17 -0400124my $ignore_config;
Steven Rostedtbe405f92012-01-04 21:51:59 -0500125my $ignore_errors;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -0400126my $addconfig;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -0400127my $in_bisect = 0;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500128my $bisect_bad_commit = "";
Steven Rostedtd6ce2a02010-11-02 14:58:05 -0400129my $reverse_bisect;
Steven Rostedtc960bb92011-03-08 09:22:39 -0500130my $bisect_manual;
Steven Rostedtc23dca72011-03-08 09:26:31 -0500131my $bisect_skip;
Steven Rostedt30f75da2011-06-13 10:35:35 -0400132my $config_bisect_good;
Steven Rostedtc5dacb82011-12-22 12:43:57 -0500133my $bisect_ret_good;
134my $bisect_ret_bad;
135my $bisect_ret_skip;
136my $bisect_ret_abort;
137my $bisect_ret_default;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400138my $in_patchcheck = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -0400139my $run_test;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -0400140my $redirect;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400141my $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +0530142my $testlog;
Steven Rostedt7faafbd2010-11-02 14:58:22 -0400143my $dmesg;
144my $monitor_fp;
145my $monitor_pid;
146my $monitor_cnt = 0;
Steven Rostedta75fece2010-11-02 14:58:27 -0400147my $sleep_time;
148my $bisect_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -0400149my $patchcheck_sleep_time;
Steven Rostedt19902072011-06-14 20:46:25 -0400150my $ignore_warnings;
Steven Rostedta75fece2010-11-02 14:58:27 -0400151my $store_failures;
Rabin Vincentde5b6e32011-11-18 17:05:31 +0530152my $store_successes;
Steven Rostedt9064af52011-06-13 10:38:48 -0400153my $test_name;
Steven Rostedta75fece2010-11-02 14:58:27 -0400154my $timeout;
155my $booted_timeout;
Steven Rostedtf1a5b962011-06-13 10:30:00 -0400156my $detect_triplefault;
Steven Rostedta75fece2010-11-02 14:58:27 -0400157my $console;
Steven Rostedt2b803362011-09-30 18:00:23 -0400158my $reboot_success_line;
Steven Rostedta75fece2010-11-02 14:58:27 -0400159my $success_line;
Steven Rostedt1c8a6172010-11-09 12:55:40 -0500160my $stop_after_success;
161my $stop_after_failure;
Steven Rostedt2d01b262011-03-08 09:47:54 -0500162my $stop_test_after;
Steven Rostedta75fece2010-11-02 14:58:27 -0400163my $build_target;
164my $target_image;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500165my $checkout;
Steven Rostedta75fece2010-11-02 14:58:27 -0400166my $localversion;
Steven Rostedt576f6272010-11-02 14:58:38 -0400167my $iteration = 0;
Steven Rostedte48c5292010-11-02 14:35:37 -0400168my $successes = 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400169
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500170my $bisect_good;
171my $bisect_bad;
172my $bisect_type;
173my $bisect_start;
174my $bisect_replay;
175my $bisect_files;
176my $bisect_reverse;
177my $bisect_check;
178
179my $config_bisect;
180my $config_bisect_type;
Steven Rostedtb0918612012-07-19 15:26:00 -0400181my $config_bisect_check;
Steven Rostedtb5f4aea2011-12-22 21:33:55 -0500182
183my $patchcheck_type;
184my $patchcheck_start;
185my $patchcheck_end;
186
Steven Rostedt165708b2011-11-26 20:56:52 -0500187# set when a test is something other that just building or install
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500188# which would require more options.
189my $buildonly = 1;
190
Steven Rostedtdbd37832011-11-23 16:00:48 -0500191# set when creating a new config
192my $newconfig = 0;
193
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500194my %entered_configs;
195my %config_help;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400196my %variable;
Steven Rostedtcf79fab2012-07-19 15:29:43 -0400197
198# force_config is the list of configs that we force enabled (or disabled)
199# in a .config file. The MIN_CONFIG and ADD_CONFIG configs.
Steven Rostedtfcb3f162011-06-13 10:40:58 -0400200my %force_config;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500201
Steven Rostedt4ab1cce2011-09-30 18:12:20 -0400202# do not force reboots on config problems
203my $no_reboot = 1;
204
Steven Rostedt759a3cc2012-05-01 08:20:12 -0400205# reboot on success
206my $reboot_success = 0;
207
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500208my %option_map = (
209 "MACHINE" => \$machine,
210 "SSH_USER" => \$ssh_user,
211 "TMP_DIR" => \$tmpdir,
212 "OUTPUT_DIR" => \$outputdir,
213 "BUILD_DIR" => \$builddir,
214 "TEST_TYPE" => \$test_type,
Steven Rostedt921ed4c2012-07-19 15:18:27 -0400215 "PRE_KTEST" => \$pre_ktest,
216 "POST_KTEST" => \$post_ktest,
217 "PRE_TEST" => \$pre_test,
218 "POST_TEST" => \$post_test,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500219 "BUILD_TYPE" => \$build_type,
220 "BUILD_OPTIONS" => \$build_options,
221 "PRE_BUILD" => \$pre_build,
222 "POST_BUILD" => \$post_build,
223 "PRE_BUILD_DIE" => \$pre_build_die,
224 "POST_BUILD_DIE" => \$post_build_die,
225 "POWER_CYCLE" => \$power_cycle,
226 "REBOOT" => \$reboot,
227 "BUILD_NOCLEAN" => \$noclean,
228 "MIN_CONFIG" => \$minconfig,
229 "OUTPUT_MIN_CONFIG" => \$output_minconfig,
230 "START_MIN_CONFIG" => \$start_minconfig,
Steven Rostedtccc513b2012-05-21 17:13:40 -0400231 "MIN_CONFIG_TYPE" => \$minconfig_type,
Steven Rostedt43de3312012-05-21 23:35:12 -0400232 "USE_OUTPUT_MIN_CONFIG" => \$use_output_minconfig,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500233 "IGNORE_CONFIG" => \$ignore_config,
234 "TEST" => \$run_test,
235 "ADD_CONFIG" => \$addconfig,
236 "REBOOT_TYPE" => \$reboot_type,
237 "GRUB_MENU" => \$grub_menu,
Steven Rostedta15ba912012-11-13 14:30:37 -0500238 "GRUB_FILE" => \$grub_file,
239 "GRUB_REBOOT" => \$grub_reboot,
Steven Rostedte5c2ec12012-07-19 15:22:05 -0400240 "PRE_INSTALL" => \$pre_install,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500241 "POST_INSTALL" => \$post_install,
242 "NO_INSTALL" => \$no_install,
243 "REBOOT_SCRIPT" => \$reboot_script,
244 "REBOOT_ON_ERROR" => \$reboot_on_error,
245 "SWITCH_TO_GOOD" => \$switch_to_good,
246 "SWITCH_TO_TEST" => \$switch_to_test,
247 "POWEROFF_ON_ERROR" => \$poweroff_on_error,
Steven Rostedt648a1822012-03-21 11:18:27 -0400248 "REBOOT_ON_SUCCESS" => \$reboot_on_success,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500249 "DIE_ON_FAILURE" => \$die_on_failure,
250 "POWER_OFF" => \$power_off,
251 "POWERCYCLE_AFTER_REBOOT" => \$powercycle_after_reboot,
252 "POWEROFF_AFTER_HALT" => \$poweroff_after_halt,
Steven Rostedt407b95b2012-07-19 16:05:42 -0400253 "MAX_MONITOR_WAIT" => \$max_monitor_wait,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500254 "SLEEP_TIME" => \$sleep_time,
255 "BISECT_SLEEP_TIME" => \$bisect_sleep_time,
256 "PATCHCHECK_SLEEP_TIME" => \$patchcheck_sleep_time,
257 "IGNORE_WARNINGS" => \$ignore_warnings,
Steven Rostedtbe405f92012-01-04 21:51:59 -0500258 "IGNORE_ERRORS" => \$ignore_errors,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500259 "BISECT_MANUAL" => \$bisect_manual,
260 "BISECT_SKIP" => \$bisect_skip,
261 "CONFIG_BISECT_GOOD" => \$config_bisect_good,
262 "BISECT_RET_GOOD" => \$bisect_ret_good,
263 "BISECT_RET_BAD" => \$bisect_ret_bad,
264 "BISECT_RET_SKIP" => \$bisect_ret_skip,
265 "BISECT_RET_ABORT" => \$bisect_ret_abort,
266 "BISECT_RET_DEFAULT" => \$bisect_ret_default,
267 "STORE_FAILURES" => \$store_failures,
268 "STORE_SUCCESSES" => \$store_successes,
269 "TEST_NAME" => \$test_name,
270 "TIMEOUT" => \$timeout,
271 "BOOTED_TIMEOUT" => \$booted_timeout,
272 "CONSOLE" => \$console,
273 "DETECT_TRIPLE_FAULT" => \$detect_triplefault,
274 "SUCCESS_LINE" => \$success_line,
275 "REBOOT_SUCCESS_LINE" => \$reboot_success_line,
276 "STOP_AFTER_SUCCESS" => \$stop_after_success,
277 "STOP_AFTER_FAILURE" => \$stop_after_failure,
278 "STOP_TEST_AFTER" => \$stop_test_after,
279 "BUILD_TARGET" => \$build_target,
280 "SSH_EXEC" => \$ssh_exec,
281 "SCP_TO_TARGET" => \$scp_to_target,
Steven Rostedt02ad2612012-03-21 08:21:24 -0400282 "SCP_TO_TARGET_INSTALL" => \$scp_to_target_install,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500283 "CHECKOUT" => \$checkout,
284 "TARGET_IMAGE" => \$target_image,
285 "LOCALVERSION" => \$localversion,
286
287 "BISECT_GOOD" => \$bisect_good,
288 "BISECT_BAD" => \$bisect_bad,
289 "BISECT_TYPE" => \$bisect_type,
290 "BISECT_START" => \$bisect_start,
291 "BISECT_REPLAY" => \$bisect_replay,
292 "BISECT_FILES" => \$bisect_files,
293 "BISECT_REVERSE" => \$bisect_reverse,
294 "BISECT_CHECK" => \$bisect_check,
295
296 "CONFIG_BISECT" => \$config_bisect,
297 "CONFIG_BISECT_TYPE" => \$config_bisect_type,
Steven Rostedtb0918612012-07-19 15:26:00 -0400298 "CONFIG_BISECT_CHECK" => \$config_bisect_check,
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500299
300 "PATCHCHECK_TYPE" => \$patchcheck_type,
301 "PATCHCHECK_START" => \$patchcheck_start,
302 "PATCHCHECK_END" => \$patchcheck_end,
303);
304
305# Options may be used by other options, record them.
306my %used_options;
307
Steven Rostedt7bf51072011-10-22 09:07:03 -0400308# default variables that can be used
309chomp ($variable{"PWD"} = `pwd`);
310
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500311$config_help{"MACHINE"} = << "EOF"
312 The machine hostname that you will test.
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500313 For build only tests, it is still needed to differentiate log files.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500314EOF
315 ;
316$config_help{"SSH_USER"} = << "EOF"
317 The box is expected to have ssh on normal bootup, provide the user
318 (most likely root, since you need privileged operations)
319EOF
320 ;
321$config_help{"BUILD_DIR"} = << "EOF"
322 The directory that contains the Linux source code (full path).
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500323 You can use \${PWD} that will be the path where ktest.pl is run, or use
324 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500325EOF
326 ;
327$config_help{"OUTPUT_DIR"} = << "EOF"
328 The directory that the objects will be built (full path).
329 (can not be same as BUILD_DIR)
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500330 You can use \${PWD} that will be the path where ktest.pl is run, or use
331 \${THIS_DIR} which is assigned \${PWD} but may be changed later.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500332EOF
333 ;
334$config_help{"BUILD_TARGET"} = << "EOF"
335 The location of the compiled file to copy to the target.
336 (relative to OUTPUT_DIR)
337EOF
338 ;
Steven Rostedtdbd37832011-11-23 16:00:48 -0500339$config_help{"BUILD_OPTIONS"} = << "EOF"
340 Options to add to \"make\" when building.
341 i.e. -j20
342EOF
343 ;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500344$config_help{"TARGET_IMAGE"} = << "EOF"
345 The place to put your image on the test machine.
346EOF
347 ;
348$config_help{"POWER_CYCLE"} = << "EOF"
349 A script or command to reboot the box.
350
351 Here is a digital loggers power switch example
352 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
353
354 Here is an example to reboot a virtual box on the current host
355 with the name "Guest".
356 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
357EOF
358 ;
359$config_help{"CONSOLE"} = << "EOF"
360 The script or command that reads the console
361
362 If you use ttywatch server, something like the following would work.
363CONSOLE = nc -d localhost 3001
364
365 For a virtual machine with guest name "Guest".
366CONSOLE = virsh console Guest
367EOF
368 ;
369$config_help{"LOCALVERSION"} = << "EOF"
370 Required version ending to differentiate the test
371 from other linux builds on the system.
372EOF
373 ;
374$config_help{"REBOOT_TYPE"} = << "EOF"
375 Way to reboot the box to the test kernel.
Steven Rostedta15ba912012-11-13 14:30:37 -0500376 Only valid options so far are "grub", "grub2", and "script".
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500377
378 If you specify grub, it will assume grub version 1
379 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
380 and select that target to reboot to the kernel. If this is not
381 your setup, then specify "script" and have a command or script
382 specified in REBOOT_SCRIPT to boot to the target.
383
384 The entry in /boot/grub/menu.lst must be entered in manually.
385 The test will not modify that file.
Steven Rostedta15ba912012-11-13 14:30:37 -0500386
387 If you specify grub2, then you also need to specify both \$GRUB_MENU
388 and \$GRUB_FILE.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500389EOF
390 ;
391$config_help{"GRUB_MENU"} = << "EOF"
392 The grub title name for the test kernel to boot
Steven Rostedta15ba912012-11-13 14:30:37 -0500393 (Only mandatory if REBOOT_TYPE = grub or grub2)
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500394
395 Note, ktest.pl will not update the grub menu.lst, you need to
396 manually add an option for the test. ktest.pl will search
397 the grub menu.lst for this option to find what kernel to
398 reboot into.
399
400 For example, if in the /boot/grub/menu.lst the test kernel title has:
401 title Test Kernel
402 kernel vmlinuz-test
403 GRUB_MENU = Test Kernel
Steven Rostedta15ba912012-11-13 14:30:37 -0500404
405 For grub2, a search of \$GRUB_FILE is performed for the lines
406 that begin with "menuentry". It will not detect submenus. The
407 menu must be a non-nested menu. Add the quotes used in the menu
408 to guarantee your selection, as the first menuentry with the content
409 of \$GRUB_MENU that is found will be used.
410EOF
411 ;
412$config_help{"GRUB_FILE"} = << "EOF"
413 If grub2 is used, the full path for the grub.cfg file is placed
414 here. Use something like /boot/grub2/grub.cfg to search.
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500415EOF
416 ;
417$config_help{"REBOOT_SCRIPT"} = << "EOF"
418 A script to reboot the target into the test kernel
419 (Only mandatory if REBOOT_TYPE = script)
420EOF
421 ;
422
Steven Rostedtdad98752011-11-22 20:48:57 -0500423sub read_prompt {
424 my ($cancel, $prompt) = @_;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400425
426 my $ans;
427
428 for (;;) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500429 if ($cancel) {
430 print "$prompt [y/n/C] ";
431 } else {
432 print "$prompt [Y/n] ";
433 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400434 $ans = <STDIN>;
435 chomp $ans;
436 if ($ans =~ /^\s*$/) {
Steven Rostedtdad98752011-11-22 20:48:57 -0500437 if ($cancel) {
438 $ans = "c";
439 } else {
440 $ans = "y";
441 }
Steven Rostedt35ce5952011-07-15 21:57:25 -0400442 }
443 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
Steven Rostedtdad98752011-11-22 20:48:57 -0500444 if ($cancel) {
445 last if ($ans =~ /^c$/i);
446 print "Please answer either 'y', 'n' or 'c'.\n";
447 } else {
448 print "Please answer either 'y' or 'n'.\n";
449 }
450 }
451 if ($ans =~ /^c/i) {
452 exit;
Steven Rostedt35ce5952011-07-15 21:57:25 -0400453 }
454 if ($ans !~ /^y$/i) {
455 return 0;
456 }
457 return 1;
458}
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500459
Steven Rostedtdad98752011-11-22 20:48:57 -0500460sub read_yn {
461 my ($prompt) = @_;
462
463 return read_prompt 0, $prompt;
464}
465
466sub read_ync {
467 my ($prompt) = @_;
468
469 return read_prompt 1, $prompt;
470}
471
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500472sub get_ktest_config {
473 my ($config) = @_;
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400474 my $ans;
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500475
476 return if (defined($opt{$config}));
477
478 if (defined($config_help{$config})) {
479 print "\n";
480 print $config_help{$config};
481 }
482
483 for (;;) {
484 print "$config = ";
Steven Rostedtdbd37832011-11-23 16:00:48 -0500485 if (defined($default{$config}) && length($default{$config})) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500486 print "\[$default{$config}\] ";
487 }
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400488 $ans = <STDIN>;
489 $ans =~ s/^\s*(.*\S)\s*$/$1/;
490 if ($ans =~ /^\s*$/) {
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500491 if ($default{$config}) {
Steven Rostedt815e2bd2011-10-28 07:01:40 -0400492 $ans = $default{$config};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500493 } else {
494 print "Your answer can not be blank\n";
495 next;
496 }
497 }
Steven Rostedt0e7a22d2011-11-21 20:39:33 -0500498 $entered_configs{$config} = ${ans};
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500499 last;
500 }
501}
502
503sub get_ktest_configs {
504 get_ktest_config("MACHINE");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500505 get_ktest_config("BUILD_DIR");
506 get_ktest_config("OUTPUT_DIR");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500507
Steven Rostedtdbd37832011-11-23 16:00:48 -0500508 if ($newconfig) {
509 get_ktest_config("BUILD_OPTIONS");
510 }
511
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500512 # options required for other than just building a kernel
513 if (!$buildonly) {
Steven Rostedt165708b2011-11-26 20:56:52 -0500514 get_ktest_config("POWER_CYCLE");
515 get_ktest_config("CONSOLE");
516 }
517
518 # options required for install and more
519 if ($buildonly != 1) {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500520 get_ktest_config("SSH_USER");
521 get_ktest_config("BUILD_TARGET");
522 get_ktest_config("TARGET_IMAGE");
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500523 }
524
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500525 get_ktest_config("LOCALVERSION");
526
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500527 return if ($buildonly);
528
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500529 my $rtype = $opt{"REBOOT_TYPE"};
530
531 if (!defined($rtype)) {
532 if (!defined($opt{"GRUB_MENU"})) {
533 get_ktest_config("REBOOT_TYPE");
534 $rtype = $entered_configs{"REBOOT_TYPE"};
535 } else {
536 $rtype = "grub";
537 }
538 }
539
540 if ($rtype eq "grub") {
541 get_ktest_config("GRUB_MENU");
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500542 }
Steven Rostedta15ba912012-11-13 14:30:37 -0500543
544 if ($rtype eq "grub2") {
545 get_ktest_config("GRUB_MENU");
546 get_ktest_config("GRUB_FILE");
547 }
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500548}
549
Steven Rostedt77d942c2011-05-20 13:36:58 -0400550sub process_variables {
Steven Rostedt8d735212011-10-17 11:36:44 -0400551 my ($value, $remove_undef) = @_;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400552 my $retval = "";
553
554 # We want to check for '\', and it is just easier
555 # to check the previous characet of '$' and not need
556 # to worry if '$' is the first character. By adding
557 # a space to $value, we can just check [^\\]\$ and
558 # it will still work.
559 $value = " $value";
560
561 while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
562 my $begin = $1;
563 my $var = $2;
564 my $end = $3;
565 # append beginning of value to retval
566 $retval = "$retval$begin";
567 if (defined($variable{$var})) {
568 $retval = "$retval$variable{$var}";
Steven Rostedt8d735212011-10-17 11:36:44 -0400569 } elsif (defined($remove_undef) && $remove_undef) {
570 # for if statements, any variable that is not defined,
571 # we simple convert to 0
572 $retval = "${retval}0";
Steven Rostedt77d942c2011-05-20 13:36:58 -0400573 } else {
574 # put back the origin piece.
575 $retval = "$retval\$\{$var\}";
Steven Rostedt9cc9e092011-12-22 21:37:22 -0500576 # This could be an option that is used later, save
577 # it so we don't warn if this option is not one of
578 # ktests options.
579 $used_options{$var} = 1;
Steven Rostedt77d942c2011-05-20 13:36:58 -0400580 }
581 $value = $end;
582 }
583 $retval = "$retval$value";
584
585 # remove the space added in the beginning
586 $retval =~ s/ //;
587
588 return "$retval"
589}
590
Steven Rostedta57419b2010-11-02 15:13:54 -0400591sub set_value {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400592 my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
Steven Rostedta57419b2010-11-02 15:13:54 -0400593
Steven Rostedtcad96662011-12-22 11:32:52 -0500594 my $prvalue = process_variables($rvalue);
595
596 if ($buildonly && $lvalue =~ /^TEST_TYPE(\[.*\])?$/ && $prvalue ne "build") {
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500597 # Note if a test is something other than build, then we
598 # will need other manditory options.
Steven Rostedtcad96662011-12-22 11:32:52 -0500599 if ($prvalue ne "install") {
Steven Rostedt165708b2011-11-26 20:56:52 -0500600 $buildonly = 0;
601 } else {
602 # install still limits some manditory options.
603 $buildonly = 2;
604 }
Steven Rostedtbb8474b2011-11-23 15:58:00 -0500605 }
606
Steven Rostedta57419b2010-11-02 15:13:54 -0400607 if (defined($opt{$lvalue})) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400608 if (!$override || defined(${$overrides}{$lvalue})) {
609 my $extra = "";
610 if ($override) {
611 $extra = "In the same override section!\n";
612 }
613 die "$name: $.: Option $lvalue defined more than once!\n$extra";
614 }
Steven Rostedtcad96662011-12-22 11:32:52 -0500615 ${$overrides}{$lvalue} = $prvalue;
Steven Rostedta57419b2010-11-02 15:13:54 -0400616 }
Steven Rostedt21a96792010-11-08 16:45:50 -0500617 if ($rvalue =~ /^\s*$/) {
618 delete $opt{$lvalue};
619 } else {
Steven Rostedtcad96662011-12-22 11:32:52 -0500620 $opt{$lvalue} = $prvalue;
Steven Rostedt21a96792010-11-08 16:45:50 -0500621 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400622}
623
Steven Rostedt77d942c2011-05-20 13:36:58 -0400624sub set_variable {
625 my ($lvalue, $rvalue) = @_;
626
627 if ($rvalue =~ /^\s*$/) {
628 delete $variable{$lvalue};
629 } else {
630 $rvalue = process_variables($rvalue);
631 $variable{$lvalue} = $rvalue;
632 }
633}
634
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400635sub process_compare {
636 my ($lval, $cmp, $rval) = @_;
637
638 # remove whitespace
639
640 $lval =~ s/^\s*//;
641 $lval =~ s/\s*$//;
642
643 $rval =~ s/^\s*//;
644 $rval =~ s/\s*$//;
645
646 if ($cmp eq "==") {
647 return $lval eq $rval;
648 } elsif ($cmp eq "!=") {
649 return $lval ne $rval;
Steven Rostedt8fddbe92012-07-30 14:37:01 -0400650 } elsif ($cmp eq "=~") {
651 return $lval =~ m/$rval/;
652 } elsif ($cmp eq "!~") {
653 return $lval !~ m/$rval/;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400654 }
655
656 my $statement = "$lval $cmp $rval";
657 my $ret = eval $statement;
658
659 # $@ stores error of eval
660 if ($@) {
661 return -1;
662 }
663
664 return $ret;
665}
666
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400667sub value_defined {
668 my ($val) = @_;
669
670 return defined($variable{$2}) ||
671 defined($opt{$2});
672}
673
Steven Rostedt8d735212011-10-17 11:36:44 -0400674my $d = 0;
675sub process_expression {
676 my ($name, $val) = @_;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400677
Steven Rostedt8d735212011-10-17 11:36:44 -0400678 my $c = $d++;
679
680 while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
681 my $express = $1;
682
683 if (process_expression($name, $express)) {
684 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
685 } else {
686 $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
687 }
688 }
689
690 $d--;
691 my $OR = "\\|\\|";
692 my $AND = "\\&\\&";
693
694 while ($val =~ s/^(.*?)($OR|$AND)//) {
695 my $express = $1;
696 my $op = $2;
697
698 if (process_expression($name, $express)) {
699 if ($op eq "||") {
700 return 1;
701 }
702 } else {
703 if ($op eq "&&") {
704 return 0;
705 }
706 }
707 }
Steven Rostedt45d73a52011-09-30 19:44:53 -0400708
Steven Rostedt8fddbe92012-07-30 14:37:01 -0400709 if ($val =~ /(.*)(==|\!=|>=|<=|>|<|=~|\!~)(.*)/) {
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400710 my $ret = process_compare($1, $2, $3);
711 if ($ret < 0) {
712 die "$name: $.: Unable to process comparison\n";
713 }
714 return $ret;
715 }
716
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400717 if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
718 if (defined $1) {
719 return !value_defined($2);
720 } else {
721 return value_defined($2);
722 }
723 }
724
Steven Rostedt45d73a52011-09-30 19:44:53 -0400725 if ($val =~ /^\s*0\s*$/) {
726 return 0;
727 } elsif ($val =~ /^\s*\d+\s*$/) {
728 return 1;
729 }
730
Steven Rostedt9900b5d2011-09-30 22:41:14 -0400731 die ("$name: $.: Undefined content $val in if statement\n");
Steven Rostedt8d735212011-10-17 11:36:44 -0400732}
733
734sub process_if {
735 my ($name, $value) = @_;
736
737 # Convert variables and replace undefined ones with 0
738 my $val = process_variables($value, 1);
739 my $ret = process_expression $name, $val;
740
741 return $ret;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400742}
743
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400744sub __read_config {
745 my ($config, $current_test_num) = @_;
Steven Rostedt2545eb62010-11-02 15:01:32 -0400746
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400747 my $in;
748 open($in, $config) || die "can't read file $config";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400749
Steven Rostedta57419b2010-11-02 15:13:54 -0400750 my $name = $config;
751 $name =~ s,.*/(.*),$1,;
752
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400753 my $test_num = $$current_test_num;
Steven Rostedta57419b2010-11-02 15:13:54 -0400754 my $default = 1;
755 my $repeat = 1;
756 my $num_tests_set = 0;
757 my $skip = 0;
758 my $rest;
Steven Rostedta9f84422011-10-17 11:06:29 -0400759 my $line;
Steven Rostedt0df213c2011-06-14 20:51:37 -0400760 my $test_case = 0;
Steven Rostedt45d73a52011-09-30 19:44:53 -0400761 my $if = 0;
762 my $if_set = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400763 my $override = 0;
764
765 my %overrides;
Steven Rostedta57419b2010-11-02 15:13:54 -0400766
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400767 while (<$in>) {
Steven Rostedt2545eb62010-11-02 15:01:32 -0400768
769 # ignore blank lines and comments
770 next if (/^\s*$/ || /\s*\#/);
771
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400772 if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400773
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400774 my $type = $1;
775 $rest = $2;
Steven Rostedta9f84422011-10-17 11:06:29 -0400776 $line = $2;
Steven Rostedta57419b2010-11-02 15:13:54 -0400777
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400778 my $old_test_num;
779 my $old_repeat;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400780 $override = 0;
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400781
782 if ($type eq "TEST_START") {
783
784 if ($num_tests_set) {
785 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
786 }
787
788 $old_test_num = $test_num;
789 $old_repeat = $repeat;
790
791 $test_num += $repeat;
792 $default = 0;
793 $repeat = 1;
794 } else {
795 $default = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400796 }
797
Steven Rostedta9f84422011-10-17 11:06:29 -0400798 # If SKIP is anywhere in the line, the command will be skipped
799 if ($rest =~ s/\s+SKIP\b//) {
Steven Rostedta57419b2010-11-02 15:13:54 -0400800 $skip = 1;
801 } else {
Steven Rostedt0df213c2011-06-14 20:51:37 -0400802 $test_case = 1;
Steven Rostedta57419b2010-11-02 15:13:54 -0400803 $skip = 0;
804 }
805
Steven Rostedta9f84422011-10-17 11:06:29 -0400806 if ($rest =~ s/\sELSE\b//) {
807 if (!$if) {
808 die "$name: $.: ELSE found with out matching IF section\n$_";
809 }
810 $if = 0;
811
812 if ($if_set) {
813 $skip = 1;
814 } else {
815 $skip = 0;
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400816 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400817 }
818
Steven Rostedta9f84422011-10-17 11:06:29 -0400819 if ($rest =~ s/\sIF\s+(.*)//) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400820 if (process_if($name, $1)) {
821 $if_set = 1;
822 } else {
823 $skip = 1;
824 }
825 $if = 1;
826 } else {
827 $if = 0;
Steven Rostedta9f84422011-10-17 11:06:29 -0400828 $if_set = 0;
Steven Rostedta57419b2010-11-02 15:13:54 -0400829 }
830
Steven Rostedta9f84422011-10-17 11:06:29 -0400831 if (!$skip) {
832 if ($type eq "TEST_START") {
833 if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
834 $repeat = $1;
835 $repeat_tests{"$test_num"} = $repeat;
836 }
837 } elsif ($rest =~ s/\sOVERRIDE\b//) {
838 # DEFAULT only
839 $override = 1;
840 # Clear previous overrides
841 %overrides = ();
842 }
843 }
844
845 if (!$skip && $rest !~ /^\s*$/) {
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400846 die "$name: $.: Gargbage found after $type\n$_";
Steven Rostedta57419b2010-11-02 15:13:54 -0400847 }
848
Steven Rostedt0050b6b2011-09-30 21:10:30 -0400849 if ($skip && $type eq "TEST_START") {
Steven Rostedta57419b2010-11-02 15:13:54 -0400850 $test_num = $old_test_num;
Steven Rostedte48c5292010-11-02 14:35:37 -0400851 $repeat = $old_repeat;
Steven Rostedta57419b2010-11-02 15:13:54 -0400852 }
853
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400854 } elsif (/^\s*ELSE\b(.*)$/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400855 if (!$if) {
856 die "$name: $.: ELSE found with out matching IF section\n$_";
857 }
858 $rest = $1;
859 if ($if_set) {
860 $skip = 1;
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400861 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400862 } else {
863 $skip = 0;
864
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400865 if ($rest =~ /\sIF\s+(.*)/) {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400866 # May be a ELSE IF section.
Steven Rostedt95f57832012-09-26 14:48:17 -0400867 if (process_if($name, $1)) {
868 $if_set = 1;
869 } else {
Steven Rostedt45d73a52011-09-30 19:44:53 -0400870 $skip = 1;
871 }
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400872 $rest = "";
Steven Rostedt45d73a52011-09-30 19:44:53 -0400873 } else {
874 $if = 0;
875 }
876 }
877
Steven Rostedtab7a3f52011-09-30 20:24:07 -0400878 if ($rest !~ /^\s*$/) {
879 die "$name: $.: Gargbage found after DEFAULTS\n$_";
880 }
881
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400882 } elsif (/^\s*INCLUDE\s+(\S+)/) {
883
884 next if ($skip);
885
886 if (!$default) {
887 die "$name: $.: INCLUDE can only be done in default sections\n$_";
888 }
889
890 my $file = process_variables($1);
891
892 if ($file !~ m,^/,) {
893 # check the path of the config file first
894 if ($config =~ m,(.*)/,) {
895 if (-f "$1/$file") {
896 $file = "$1/$file";
897 }
898 }
899 }
900
901 if ( ! -r $file ) {
902 die "$name: $.: Can't read file $file\n$_";
903 }
904
905 if (__read_config($file, \$test_num)) {
906 $test_case = 1;
907 }
908
Steven Rostedta57419b2010-11-02 15:13:54 -0400909 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
910
911 next if ($skip);
912
Steven Rostedt2545eb62010-11-02 15:01:32 -0400913 my $lvalue = $1;
914 my $rvalue = $2;
915
Steven Rostedta57419b2010-11-02 15:13:54 -0400916 if (!$default &&
917 ($lvalue eq "NUM_TESTS" ||
918 $lvalue eq "LOG_FILE" ||
919 $lvalue eq "CLEAR_LOG")) {
920 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
Steven Rostedta75fece2010-11-02 14:58:27 -0400921 }
Steven Rostedta57419b2010-11-02 15:13:54 -0400922
923 if ($lvalue eq "NUM_TESTS") {
924 if ($test_num) {
925 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
926 }
927 if (!$default) {
928 die "$name: $.: NUM_TESTS must be set in default section\n";
929 }
930 $num_tests_set = 1;
931 }
932
933 if ($default || $lvalue =~ /\[\d+\]$/) {
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400934 set_value($lvalue, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400935 } else {
936 my $val = "$lvalue\[$test_num\]";
Steven Rostedt3d1cc412011-09-30 22:14:21 -0400937 set_value($val, $rvalue, $override, \%overrides, $name);
Steven Rostedta57419b2010-11-02 15:13:54 -0400938
939 if ($repeat > 1) {
940 $repeats{$val} = $repeat;
941 }
942 }
Steven Rostedt77d942c2011-05-20 13:36:58 -0400943 } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
944 next if ($skip);
945
946 my $lvalue = $1;
947 my $rvalue = $2;
948
949 # process config variables.
950 # Config variables are only active while reading the
951 # config and can be defined anywhere. They also ignore
952 # TEST_START and DEFAULTS, but are skipped if they are in
953 # on of these sections that have SKIP defined.
954 # The save variable can be
955 # defined multiple times and the new one simply overrides
956 # the prevous one.
957 set_variable($lvalue, $rvalue);
958
Steven Rostedta57419b2010-11-02 15:13:54 -0400959 } else {
960 die "$name: $.: Garbage found in config\n$_";
Steven Rostedt2545eb62010-11-02 15:01:32 -0400961 }
962 }
963
Steven Rostedta57419b2010-11-02 15:13:54 -0400964 if ($test_num) {
965 $test_num += $repeat - 1;
966 $opt{"NUM_TESTS"} = $test_num;
967 }
968
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400969 close($in);
970
971 $$current_test_num = $test_num;
972
973 return $test_case;
974}
975
Steven Rostedtc4261d02011-11-23 13:41:18 -0500976sub get_test_case {
977 print "What test case would you like to run?\n";
978 print " (build, install or boot)\n";
979 print " Other tests are available but require editing the config file\n";
980 my $ans = <STDIN>;
981 chomp $ans;
982 $default{"TEST_TYPE"} = $ans;
983}
984
Steven Rostedt2ed3b162011-09-30 21:00:00 -0400985sub read_config {
986 my ($config) = @_;
987
988 my $test_case;
989 my $test_num = 0;
990
991 $test_case = __read_config $config, \$test_num;
992
Steven Rostedt8d1491b2010-11-18 15:39:48 -0500993 # make sure we have all mandatory configs
994 get_ktest_configs;
995
Steven Rostedt0df213c2011-06-14 20:51:37 -0400996 # was a test specified?
997 if (!$test_case) {
998 print "No test case specified.\n";
Steven Rostedtc4261d02011-11-23 13:41:18 -0500999 get_test_case;
Steven Rostedt0df213c2011-06-14 20:51:37 -04001000 }
1001
Steven Rostedta75fece2010-11-02 14:58:27 -04001002 # set any defaults
1003
1004 foreach my $default (keys %default) {
1005 if (!defined($opt{$default})) {
1006 $opt{$default} = $default{$default};
1007 }
1008 }
Steven Rostedt9cc9e092011-12-22 21:37:22 -05001009
1010 if ($opt{"IGNORE_UNUSED"} == 1) {
1011 return;
1012 }
1013
1014 my %not_used;
1015
1016 # check if there are any stragglers (typos?)
1017 foreach my $option (keys %opt) {
1018 my $op = $option;
1019 # remove per test labels.
1020 $op =~ s/\[.*\]//;
1021 if (!exists($option_map{$op}) &&
1022 !exists($default{$op}) &&
1023 !exists($used_options{$op})) {
1024 $not_used{$op} = 1;
1025 }
1026 }
1027
1028 if (%not_used) {
1029 my $s = "s are";
1030 $s = " is" if (keys %not_used == 1);
1031 print "The following option$s not used; could be a typo:\n";
1032 foreach my $option (keys %not_used) {
1033 print "$option\n";
1034 }
1035 print "Set IGRNORE_UNUSED = 1 to have ktest ignore unused variables\n";
1036 if (!read_yn "Do you want to continue?") {
1037 exit -1;
1038 }
1039 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001040}
1041
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001042sub __eval_option {
1043 my ($option, $i) = @_;
1044
1045 # Add space to evaluate the character before $
1046 $option = " $option";
1047 my $retval = "";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301048 my $repeated = 0;
1049 my $parent = 0;
1050
1051 foreach my $test (keys %repeat_tests) {
1052 if ($i >= $test &&
1053 $i < $test + $repeat_tests{$test}) {
1054
1055 $repeated = 1;
1056 $parent = $test;
1057 last;
1058 }
1059 }
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001060
1061 while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
1062 my $start = $1;
1063 my $var = $2;
1064 my $end = $3;
1065
1066 # Append beginning of line
1067 $retval = "$retval$start";
1068
1069 # If the iteration option OPT[$i] exists, then use that.
1070 # otherwise see if the default OPT (without [$i]) exists.
1071
1072 my $o = "$var\[$i\]";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301073 my $parento = "$var\[$parent\]";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001074
1075 if (defined($opt{$o})) {
1076 $o = $opt{$o};
1077 $retval = "$retval$o";
Rabin Vincentf9dfb652011-11-18 17:05:30 +05301078 } elsif ($repeated && defined($opt{$parento})) {
1079 $o = $opt{$parento};
1080 $retval = "$retval$o";
Steven Rostedt23715c3c2011-06-13 11:03:34 -04001081 } elsif (defined($opt{$var})) {
1082 $o = $opt{$var};
1083 $retval = "$retval$o";
1084 } else {
1085 $retval = "$retval\$\{$var\}";
1086 }
1087
1088 $option = $end;
1089 }
1090
1091 $retval = "$retval$option";
1092
1093 $retval =~ s/^ //;
1094
1095 return $retval;
1096}
1097
1098sub eval_option {
1099 my ($option, $i) = @_;
1100
1101 my $prev = "";
1102
1103 # Since an option can evaluate to another option,
1104 # keep iterating until we do not evaluate any more
1105 # options.
1106 my $r = 0;
1107 while ($prev ne $option) {
1108 # Check for recursive evaluations.
1109 # 100 deep should be more than enough.
1110 if ($r++ > 100) {
1111 die "Over 100 evaluations accurred with $option\n" .
1112 "Check for recursive variables\n";
1113 }
1114 $prev = $option;
1115 $option = __eval_option($option, $i);
1116 }
1117
1118 return $option;
1119}
1120
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001121sub _logit {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001122 if (defined($opt{"LOG_FILE"})) {
1123 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
1124 print OUT @_;
1125 close(OUT);
1126 }
1127}
1128
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001129sub logit {
1130 if (defined($opt{"LOG_FILE"})) {
1131 _logit @_;
1132 } else {
1133 print @_;
1134 }
1135}
1136
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001137sub doprint {
1138 print @_;
Steven Rostedtd1e2f222010-11-08 16:39:57 -05001139 _logit @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001140}
1141
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001142sub run_command;
Andrew Jones2728be42011-08-12 15:32:05 +02001143sub start_monitor;
1144sub end_monitor;
1145sub wait_for_monitor;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001146
1147sub reboot {
Andrew Jones2728be42011-08-12 15:32:05 +02001148 my ($time) = @_;
1149
Steven Rostedt2b803362011-09-30 18:00:23 -04001150 if (defined($time)) {
1151 start_monitor;
1152 # flush out current monitor
1153 # May contain the reboot success line
1154 wait_for_monitor 1;
1155 }
1156
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001157 # try to reboot normally
Steven Rostedte48c5292010-11-02 14:35:37 -04001158 if (run_command $reboot) {
Steven Rostedt576f6272010-11-02 14:58:38 -04001159 if (defined($powercycle_after_reboot)) {
1160 sleep $powercycle_after_reboot;
1161 run_command "$power_cycle";
1162 }
1163 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001164 # nope? power cycle it.
Steven Rostedta75fece2010-11-02 14:58:27 -04001165 run_command "$power_cycle";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001166 }
Andrew Jones2728be42011-08-12 15:32:05 +02001167
1168 if (defined($time)) {
Steven Rostedt407b95b2012-07-19 16:05:42 -04001169 if (wait_for_monitor($time, $reboot_success_line)) {
1170 # reboot got stuck?
Steven Rostedt8a80c722012-07-19 16:08:33 -04001171 doprint "Reboot did not finish. Forcing power cycle\n";
Steven Rostedt407b95b2012-07-19 16:05:42 -04001172 run_command "$power_cycle";
1173 }
Andrew Jones2728be42011-08-12 15:32:05 +02001174 end_monitor;
1175 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001176}
1177
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001178sub reboot_to_good {
1179 my ($time) = @_;
1180
1181 if (defined($switch_to_good)) {
1182 run_command $switch_to_good;
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001183 }
1184
1185 reboot $time;
1186}
1187
Steven Rostedt576f6272010-11-02 14:58:38 -04001188sub do_not_reboot {
1189 my $i = $iteration;
1190
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001191 return $test_type eq "build" || $no_reboot ||
Steven Rostedt576f6272010-11-02 14:58:38 -04001192 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
1193 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
1194}
1195
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001196sub dodie {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001197 doprint "CRITICAL FAILURE... ", @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001198
Steven Rostedt576f6272010-11-02 14:58:38 -04001199 my $i = $iteration;
1200
1201 if ($reboot_on_error && !do_not_reboot) {
1202
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001203 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001204 reboot_to_good;
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001205
Steven Rostedta75fece2010-11-02 14:58:27 -04001206 } elsif ($poweroff_on_error && defined($power_off)) {
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001207 doprint "POWERING OFF\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001208 `$power_off`;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001209 }
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001210
Steven Rostedtf80802c2011-03-07 13:18:47 -05001211 if (defined($opt{"LOG_FILE"})) {
1212 print " See $opt{LOG_FILE} for more info.\n";
1213 }
1214
Steven Rostedt576f6272010-11-02 14:58:38 -04001215 die @_, "\n";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001216}
1217
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001218sub open_console {
1219 my ($fp) = @_;
1220
1221 my $flags;
1222
Steven Rostedta75fece2010-11-02 14:58:27 -04001223 my $pid = open($fp, "$console|") or
1224 dodie "Can't open console $console";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001225
1226 $flags = fcntl($fp, F_GETFL, 0) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001227 dodie "Can't get flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001228 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
Steven Rostedt576f6272010-11-02 14:58:38 -04001229 dodie "Can't set flags for the socket: $!";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001230
1231 return $pid;
1232}
1233
1234sub close_console {
1235 my ($fp, $pid) = @_;
1236
1237 doprint "kill child process $pid\n";
1238 kill 2, $pid;
1239
1240 print "closing!\n";
1241 close($fp);
1242}
1243
1244sub start_monitor {
1245 if ($monitor_cnt++) {
1246 return;
1247 }
1248 $monitor_fp = \*MONFD;
1249 $monitor_pid = open_console $monitor_fp;
Steven Rostedta75fece2010-11-02 14:58:27 -04001250
1251 return;
1252
1253 open(MONFD, "Stop perl from warning about single use of MONFD");
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001254}
1255
1256sub end_monitor {
1257 if (--$monitor_cnt) {
1258 return;
1259 }
1260 close_console($monitor_fp, $monitor_pid);
1261}
1262
1263sub wait_for_monitor {
Steven Rostedt2b803362011-09-30 18:00:23 -04001264 my ($time, $stop) = @_;
1265 my $full_line = "";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001266 my $line;
Steven Rostedt2b803362011-09-30 18:00:23 -04001267 my $booted = 0;
Steven Rostedt407b95b2012-07-19 16:05:42 -04001268 my $start_time = time;
Steven Rostedt8a80c722012-07-19 16:08:33 -04001269 my $skip_call_trace = 0;
1270 my $bug = 0;
1271 my $bug_ignored = 0;
Steven Rostedt407b95b2012-07-19 16:05:42 -04001272 my $now;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001273
Steven Rostedta75fece2010-11-02 14:58:27 -04001274 doprint "** Wait for monitor to settle down **\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001275
1276 # read the monitor and wait for the system to calm down
Steven Rostedt2b803362011-09-30 18:00:23 -04001277 while (!$booted) {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001278 $line = wait_for_input($monitor_fp, $time);
Steven Rostedt2b803362011-09-30 18:00:23 -04001279 last if (!defined($line));
1280 print "$line";
1281 $full_line .= $line;
1282
1283 if (defined($stop) && $full_line =~ /$stop/) {
1284 doprint "wait for monitor detected $stop\n";
1285 $booted = 1;
1286 }
1287
Steven Rostedt8a80c722012-07-19 16:08:33 -04001288 if ($full_line =~ /\[ backtrace testing \]/) {
1289 $skip_call_trace = 1;
1290 }
1291
1292 if ($full_line =~ /call trace:/i) {
1293 if (!$bug && !$skip_call_trace) {
1294 if ($ignore_errors) {
1295 $bug_ignored = 1;
1296 } else {
1297 $bug = 1;
1298 }
1299 }
1300 }
1301
1302 if ($full_line =~ /\[ end of backtrace testing \]/) {
1303 $skip_call_trace = 0;
1304 }
1305
1306 if ($full_line =~ /Kernel panic -/) {
1307 $bug = 1;
1308 }
1309
Steven Rostedt2b803362011-09-30 18:00:23 -04001310 if ($line =~ /\n/) {
1311 $full_line = "";
1312 }
Steven Rostedt407b95b2012-07-19 16:05:42 -04001313 $now = time;
1314 if ($now - $start_time >= $max_monitor_wait) {
1315 doprint "Exiting monitor flush due to hitting MAX_MONITOR_WAIT\n";
1316 return 1;
1317 }
Steven Rostedt2b803362011-09-30 18:00:23 -04001318 }
Steven Rostedta75fece2010-11-02 14:58:27 -04001319 print "** Monitor flushed **\n";
Steven Rostedt8a80c722012-07-19 16:08:33 -04001320 return $bug;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001321}
1322
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301323sub save_logs {
1324 my ($result, $basedir) = @_;
1325 my @t = localtime;
1326 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
1327 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
1328
1329 my $type = $build_type;
1330 if ($type =~ /useconfig/) {
1331 $type = "useconfig";
1332 }
1333
1334 my $dir = "$machine-$test_type-$type-$result-$date";
1335
1336 $dir = "$basedir/$dir";
1337
1338 if (!-d $dir) {
1339 mkpath($dir) or
1340 die "can't create $dir";
1341 }
1342
1343 my %files = (
1344 "config" => $output_config,
1345 "buildlog" => $buildlog,
1346 "dmesg" => $dmesg,
1347 "testlog" => $testlog,
1348 );
1349
1350 while (my ($name, $source) = each(%files)) {
1351 if (-f "$source") {
1352 cp "$source", "$dir/$name" or
1353 die "failed to copy $source";
1354 }
1355 }
1356
1357 doprint "*** Saved info to $dir ***\n";
1358}
1359
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001360sub fail {
1361
Steven Rostedt921ed4c2012-07-19 15:18:27 -04001362 if (defined($post_test)) {
1363 run_command $post_test;
1364 }
1365
Steven Rostedta75fece2010-11-02 14:58:27 -04001366 if ($die_on_failure) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001367 dodie @_;
1368 }
1369
Steven Rostedta75fece2010-11-02 14:58:27 -04001370 doprint "FAILED\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001371
Steven Rostedt576f6272010-11-02 14:58:38 -04001372 my $i = $iteration;
1373
Steven Rostedta75fece2010-11-02 14:58:27 -04001374 # no need to reboot for just building.
Steven Rostedt576f6272010-11-02 14:58:38 -04001375 if (!do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001376 doprint "REBOOTING\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001377 reboot_to_good $sleep_time;
Steven Rostedta75fece2010-11-02 14:58:27 -04001378 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001379
Steven Rostedt9064af52011-06-13 10:38:48 -04001380 my $name = "";
1381
1382 if (defined($test_name)) {
1383 $name = " ($test_name)";
1384 }
1385
Steven Rostedt576f6272010-11-02 14:58:38 -04001386 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1387 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04001388 doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
Steven Rostedt576f6272010-11-02 14:58:38 -04001389 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
1390 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
Steven Rostedta75fece2010-11-02 14:58:27 -04001391
Rabin Vincentde5b6e32011-11-18 17:05:31 +05301392 if (defined($store_failures)) {
1393 save_logs "fail", $store_failures;
1394 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001395
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001396 return 1;
1397}
1398
Steven Rostedt2545eb62010-11-02 15:01:32 -04001399sub run_command {
1400 my ($command) = @_;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001401 my $dolog = 0;
1402 my $dord = 0;
1403 my $pid;
1404
Steven Rostedte48c5292010-11-02 14:35:37 -04001405 $command =~ s/\$SSH_USER/$ssh_user/g;
1406 $command =~ s/\$MACHINE/$machine/g;
1407
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001408 doprint("$command ... ");
1409
1410 $pid = open(CMD, "$command 2>&1 |") or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001411 (fail "unable to exec $command" and return 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001412
1413 if (defined($opt{"LOG_FILE"})) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001414 open(LOG, ">>$opt{LOG_FILE}") or
1415 dodie "failed to write to log";
1416 $dolog = 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001417 }
1418
1419 if (defined($redirect)) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001420 open (RD, ">$redirect") or
1421 dodie "failed to write to redirect $redirect";
1422 $dord = 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001423 }
1424
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001425 while (<CMD>) {
1426 print LOG if ($dolog);
1427 print RD if ($dord);
1428 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001429
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001430 waitpid($pid, 0);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001431 my $failed = $?;
1432
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04001433 close(CMD);
1434 close(LOG) if ($dolog);
1435 close(RD) if ($dord);
1436
Steven Rostedt2545eb62010-11-02 15:01:32 -04001437 if ($failed) {
1438 doprint "FAILED!\n";
1439 } else {
1440 doprint "SUCCESS\n";
1441 }
1442
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001443 return !$failed;
1444}
1445
Steven Rostedte48c5292010-11-02 14:35:37 -04001446sub run_ssh {
1447 my ($cmd) = @_;
1448 my $cp_exec = $ssh_exec;
1449
1450 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1451 return run_command "$cp_exec";
1452}
1453
1454sub run_scp {
Steven Rostedt02ad2612012-03-21 08:21:24 -04001455 my ($src, $dst, $cp_scp) = @_;
Steven Rostedte48c5292010-11-02 14:35:37 -04001456
1457 $cp_scp =~ s/\$SRC_FILE/$src/g;
1458 $cp_scp =~ s/\$DST_FILE/$dst/g;
1459
1460 return run_command "$cp_scp";
1461}
1462
Steven Rostedt02ad2612012-03-21 08:21:24 -04001463sub run_scp_install {
1464 my ($src, $dst) = @_;
1465
1466 my $cp_scp = $scp_to_target_install;
1467
1468 return run_scp($src, $dst, $cp_scp);
1469}
1470
1471sub run_scp_mod {
1472 my ($src, $dst) = @_;
1473
1474 my $cp_scp = $scp_to_target;
1475
1476 return run_scp($src, $dst, $cp_scp);
1477}
1478
Steven Rostedta15ba912012-11-13 14:30:37 -05001479sub get_grub2_index {
1480
1481 return if (defined($grub_number));
1482
1483 doprint "Find grub2 menu ... ";
1484 $grub_number = -1;
1485
1486 my $ssh_grub = $ssh_exec;
1487 $ssh_grub =~ s,\$SSH_COMMAND,cat $grub_file,g;
1488
1489 open(IN, "$ssh_grub |")
1490 or die "unable to get $grub_file";
1491
1492 my $found = 0;
1493
1494 while (<IN>) {
1495 if (/^menuentry.*$grub_menu/) {
1496 $grub_number++;
1497 $found = 1;
1498 last;
1499 } elsif (/^menuentry\s/) {
1500 $grub_number++;
1501 }
1502 }
1503 close(IN);
1504
1505 die "Could not find '$grub_menu' in $grub_file on $machine"
1506 if (!$found);
1507 doprint "$grub_number\n";
1508}
1509
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001510sub get_grub_index {
1511
Steven Rostedta15ba912012-11-13 14:30:37 -05001512 if ($reboot_type eq "grub2") {
1513 get_grub2_index;
1514 return;
1515 }
1516
Steven Rostedta75fece2010-11-02 14:58:27 -04001517 if ($reboot_type ne "grub") {
1518 return;
1519 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001520 return if (defined($grub_number));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001521
1522 doprint "Find grub menu ... ";
1523 $grub_number = -1;
Steven Rostedte48c5292010-11-02 14:35:37 -04001524
1525 my $ssh_grub = $ssh_exec;
1526 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
1527
1528 open(IN, "$ssh_grub |")
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001529 or die "unable to get menu.lst";
Steven Rostedte48c5292010-11-02 14:35:37 -04001530
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001531 my $found = 0;
1532
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001533 while (<IN>) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001534 if (/^\s*title\s+$grub_menu\s*$/) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001535 $grub_number++;
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001536 $found = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001537 last;
1538 } elsif (/^\s*title\s/) {
1539 $grub_number++;
1540 }
1541 }
1542 close(IN);
1543
Steven Rostedta75fece2010-11-02 14:58:27 -04001544 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
Steven Rostedteaa1fe22011-09-14 17:20:39 -04001545 if (!$found);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001546 doprint "$grub_number\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001547}
1548
Steven Rostedt2545eb62010-11-02 15:01:32 -04001549sub wait_for_input
1550{
1551 my ($fp, $time) = @_;
1552 my $rin;
1553 my $ready;
1554 my $line;
1555 my $ch;
1556
1557 if (!defined($time)) {
1558 $time = $timeout;
1559 }
1560
1561 $rin = '';
1562 vec($rin, fileno($fp), 1) = 1;
1563 $ready = select($rin, undef, undef, $time);
1564
1565 $line = "";
1566
1567 # try to read one char at a time
1568 while (sysread $fp, $ch, 1) {
1569 $line .= $ch;
1570 last if ($ch eq "\n");
1571 }
1572
1573 if (!length($line)) {
1574 return undef;
1575 }
1576
1577 return $line;
1578}
1579
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001580sub reboot_to {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05001581 if (defined($switch_to_test)) {
1582 run_command $switch_to_test;
1583 }
1584
Steven Rostedta75fece2010-11-02 14:58:27 -04001585 if ($reboot_type eq "grub") {
Steven Rostedtc54367f2011-10-20 09:56:41 -04001586 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
Steven Rostedta15ba912012-11-13 14:30:37 -05001587 } elsif ($reboot_type eq "grub2") {
1588 run_ssh "$grub_reboot $grub_number";
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001589 } elsif (defined $reboot_script) {
1590 run_command "$reboot_script";
Steven Rostedta75fece2010-11-02 14:58:27 -04001591 }
Steven Rostedt96f6a0d2011-12-23 00:24:51 -05001592 reboot;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001593}
1594
Steven Rostedta57419b2010-11-02 15:13:54 -04001595sub get_sha1 {
1596 my ($commit) = @_;
1597
1598 doprint "git rev-list --max-count=1 $commit ... ";
1599 my $sha1 = `git rev-list --max-count=1 $commit`;
1600 my $ret = $?;
1601
1602 logit $sha1;
1603
1604 if ($ret) {
1605 doprint "FAILED\n";
1606 dodie "Failed to get git $commit";
1607 }
1608
1609 print "SUCCESS\n";
1610
1611 chomp $sha1;
1612
1613 return $sha1;
1614}
1615
Steven Rostedt5a391fb2010-11-02 14:57:43 -04001616sub monitor {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001617 my $booted = 0;
1618 my $bug = 0;
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001619 my $bug_ignored = 0;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001620 my $skip_call_trace = 0;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001621 my $loops;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001622
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001623 wait_for_monitor 5;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001624
1625 my $line;
1626 my $full_line = "";
1627
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001628 open(DMESG, "> $dmesg") or
1629 die "unable to write to $dmesg";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001630
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001631 reboot_to;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001632
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001633 my $success_start;
1634 my $failure_start;
Steven Rostedt2d01b262011-03-08 09:47:54 -05001635 my $monitor_start = time;
1636 my $done = 0;
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001637 my $version_found = 0;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001638
Steven Rostedt2d01b262011-03-08 09:47:54 -05001639 while (!$done) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001640
Steven Rostedtecaf8e52011-06-13 10:48:10 -04001641 if ($bug && defined($stop_after_failure) &&
1642 $stop_after_failure >= 0) {
1643 my $time = $stop_after_failure - (time - $failure_start);
1644 $line = wait_for_input($monitor_fp, $time);
1645 if (!defined($line)) {
1646 doprint "bug timed out after $booted_timeout seconds\n";
1647 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1648 last;
1649 }
1650 } elsif ($booted) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001651 $line = wait_for_input($monitor_fp, $booted_timeout);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001652 if (!defined($line)) {
1653 my $s = $booted_timeout == 1 ? "" : "s";
1654 doprint "Successful boot found: break after $booted_timeout second$s\n";
1655 last;
1656 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001657 } else {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001658 $line = wait_for_input($monitor_fp);
Steven Rostedtcd4f1d52011-06-13 10:26:27 -04001659 if (!defined($line)) {
1660 my $s = $timeout == 1 ? "" : "s";
1661 doprint "Timed out after $timeout second$s\n";
1662 last;
1663 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001664 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001665
Steven Rostedt2545eb62010-11-02 15:01:32 -04001666 doprint $line;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001667 print DMESG $line;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001668
1669 # we are not guaranteed to get a full line
1670 $full_line .= $line;
1671
Steven Rostedta75fece2010-11-02 14:58:27 -04001672 if ($full_line =~ /$success_line/) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04001673 $booted = 1;
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001674 $success_start = time;
1675 }
1676
1677 if ($booted && defined($stop_after_success) &&
1678 $stop_after_success >= 0) {
1679 my $now = time;
1680 if ($now - $success_start >= $stop_after_success) {
1681 doprint "Test forced to stop after $stop_after_success seconds after success\n";
1682 last;
1683 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001684 }
1685
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001686 if ($full_line =~ /\[ backtrace testing \]/) {
1687 $skip_call_trace = 1;
1688 }
1689
Steven Rostedt2545eb62010-11-02 15:01:32 -04001690 if ($full_line =~ /call trace:/i) {
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001691 if (!$bug && !$skip_call_trace) {
1692 if ($ignore_errors) {
1693 $bug_ignored = 1;
1694 } else {
1695 $bug = 1;
1696 $failure_start = time;
1697 }
Steven Rostedt1c8a6172010-11-09 12:55:40 -05001698 }
1699 }
1700
1701 if ($bug && defined($stop_after_failure) &&
1702 $stop_after_failure >= 0) {
1703 my $now = time;
1704 if ($now - $failure_start >= $stop_after_failure) {
1705 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
1706 last;
1707 }
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001708 }
1709
1710 if ($full_line =~ /\[ end of backtrace testing \]/) {
1711 $skip_call_trace = 0;
1712 }
1713
1714 if ($full_line =~ /Kernel panic -/) {
Steven Rostedt10abf112011-03-07 13:21:00 -05001715 $failure_start = time;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001716 $bug = 1;
1717 }
1718
Steven Rostedtf1a5b962011-06-13 10:30:00 -04001719 # Detect triple faults by testing the banner
1720 if ($full_line =~ /\bLinux version (\S+).*\n/) {
1721 if ($1 eq $version) {
1722 $version_found = 1;
1723 } elsif ($version_found && $detect_triplefault) {
1724 # We already booted into the kernel we are testing,
1725 # but now we booted into another kernel?
1726 # Consider this a triple fault.
1727 doprint "Aleady booted in Linux kernel $version, but now\n";
1728 doprint "we booted into Linux kernel $1.\n";
1729 doprint "Assuming that this is a triple fault.\n";
1730 doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
1731 last;
1732 }
1733 }
1734
Steven Rostedt2545eb62010-11-02 15:01:32 -04001735 if ($line =~ /\n/) {
1736 $full_line = "";
1737 }
Steven Rostedt2d01b262011-03-08 09:47:54 -05001738
1739 if ($stop_test_after > 0 && !$booted && !$bug) {
1740 if (time - $monitor_start > $stop_test_after) {
Steven Rostedt4d62bf52011-05-20 09:14:35 -04001741 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
Steven Rostedt2d01b262011-03-08 09:47:54 -05001742 $done = 1;
1743 }
1744 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04001745 }
1746
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001747 close(DMESG);
Steven Rostedt2545eb62010-11-02 15:01:32 -04001748
Steven Rostedt2545eb62010-11-02 15:01:32 -04001749 if ($bug) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001750 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001751 fail "failed - got a bug report" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001752 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001753
Steven Rostedta75fece2010-11-02 14:58:27 -04001754 if (!$booted) {
1755 return 0 if ($in_bisect);
Steven Rostedt576f6272010-11-02 14:58:38 -04001756 fail "failed - never got a boot prompt." and return 0;
Steven Rostedta75fece2010-11-02 14:58:27 -04001757 }
1758
Steven Rostedt6ca996c2012-03-21 08:18:35 -04001759 if ($bug_ignored) {
1760 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
1761 }
1762
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001763 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001764}
1765
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001766sub eval_kernel_version {
1767 my ($option) = @_;
1768
1769 $option =~ s/\$KERNEL_VERSION/$version/g;
1770
1771 return $option;
1772}
1773
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001774sub do_post_install {
1775
1776 return if (!defined($post_install));
1777
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001778 my $cp_post_install = eval_kernel_version $post_install;
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001779 run_command "$cp_post_install" or
1780 dodie "Failed to run post install";
1781}
1782
Steven Rostedt2545eb62010-11-02 15:01:32 -04001783sub install {
1784
Steven Rostedte0a87422011-09-30 17:50:48 -04001785 return if ($no_install);
1786
Steven Rostedte5c2ec12012-07-19 15:22:05 -04001787 if (defined($pre_install)) {
1788 my $cp_pre_install = eval_kernel_version $pre_install;
1789 run_command "$cp_pre_install" or
1790 dodie "Failed to run pre install";
1791 }
1792
Steven Rostedt2b29b2f2011-12-22 11:25:46 -05001793 my $cp_target = eval_kernel_version $target_image;
1794
Steven Rostedt02ad2612012-03-21 08:21:24 -04001795 run_scp_install "$outputdir/$build_target", "$cp_target" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001796 dodie "failed to copy image";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001797
1798 my $install_mods = 0;
1799
1800 # should we process modules?
1801 $install_mods = 0;
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001802 open(IN, "$output_config") or dodie("Can't read config file");
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001803 while (<IN>) {
1804 if (/CONFIG_MODULES(=y)?/) {
Steven Rostedt8bc5e4e2012-10-26 00:10:32 -04001805 if (defined($1)) {
1806 $install_mods = 1;
1807 last;
1808 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001809 }
1810 }
1811 close(IN);
1812
1813 if (!$install_mods) {
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001814 do_post_install;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001815 doprint "No modules needed\n";
1816 return;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001817 }
1818
Steven Rostedt627977d2012-03-21 08:16:15 -04001819 run_command "$make INSTALL_MOD_STRIP=1 INSTALL_MOD_PATH=$tmpdir modules_install" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001820 dodie "Failed to install modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001821
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001822 my $modlib = "/lib/modules/$version";
Steven Rostedta57419b2010-11-02 15:13:54 -04001823 my $modtar = "ktest-mods.tar.bz2";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001824
Steven Rostedte48c5292010-11-02 14:35:37 -04001825 run_ssh "rm -rf $modlib" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001826 dodie "failed to remove old mods: $modlib";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001827
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001828 # would be nice if scp -r did not follow symbolic links
Steven Rostedta75fece2010-11-02 14:58:27 -04001829 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001830 dodie "making tarball";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001831
Steven Rostedt02ad2612012-03-21 08:21:24 -04001832 run_scp_mod "$tmpdir/$modtar", "/tmp" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001833 dodie "failed to copy modules";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001834
Steven Rostedta75fece2010-11-02 14:58:27 -04001835 unlink "$tmpdir/$modtar";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001836
Steven Rostedte7b13442011-06-14 20:44:36 -04001837 run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001838 dodie "failed to tar modules";
Steven Rostedt2545eb62010-11-02 15:01:32 -04001839
Steven Rostedte48c5292010-11-02 14:35:37 -04001840 run_ssh "rm -f /tmp/$modtar";
Steven Rostedt8b37ca82010-11-02 14:58:33 -04001841
Steven Rostedtdb05cfe2011-06-13 11:09:22 -04001842 do_post_install;
Steven Rostedt2545eb62010-11-02 15:01:32 -04001843}
1844
Steven Rostedtddf607e2011-06-14 20:49:13 -04001845sub get_version {
1846 # get the release name
Steven Rostedt683a3e62012-05-18 13:34:35 -04001847 return if ($have_version);
Steven Rostedtddf607e2011-06-14 20:49:13 -04001848 doprint "$make kernelrelease ... ";
1849 $version = `$make kernelrelease | tail -1`;
1850 chomp($version);
1851 doprint "$version\n";
Steven Rostedt683a3e62012-05-18 13:34:35 -04001852 $have_version = 1;
Steven Rostedtddf607e2011-06-14 20:49:13 -04001853}
1854
1855sub start_monitor_and_boot {
Steven Rostedt9f7424c2011-10-22 08:58:19 -04001856 # Make sure the stable kernel has finished booting
1857 start_monitor;
1858 wait_for_monitor 5;
1859 end_monitor;
1860
Steven Rostedtddf607e2011-06-14 20:49:13 -04001861 get_grub_index;
1862 get_version;
1863 install;
1864
1865 start_monitor;
1866 return monitor;
1867}
1868
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001869sub check_buildlog {
1870 my ($patch) = @_;
1871
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001872 my @files = `git show $patch | diffstat -l`;
1873
1874 open(IN, "git show $patch |") or
1875 dodie "failed to show $patch";
1876 while (<IN>) {
1877 if (m,^--- a/(.*),) {
1878 chomp $1;
1879 $files[$#files] = $1;
1880 }
1881 }
1882 close(IN);
1883
1884 open(IN, $buildlog) or dodie "Can't open $buildlog";
1885 while (<IN>) {
1886 if (/^\s*(.*?):.*(warning|error)/) {
1887 my $err = $1;
1888 foreach my $file (@files) {
Steven Rostedta75fece2010-11-02 14:58:27 -04001889 my $fullpath = "$builddir/$file";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001890 if ($file eq $err || $fullpath eq $err) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001891 fail "$file built with warnings" and return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001892 }
1893 }
1894 }
1895 }
1896 close(IN);
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04001897
1898 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04001899}
1900
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001901sub apply_min_config {
1902 my $outconfig = "$output_config.new";
Steven Rostedt612b9e92011-03-07 13:27:43 -05001903
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001904 # Read the config file and remove anything that
1905 # is in the force_config hash (from minconfig and others)
1906 # then add the force config back.
1907
1908 doprint "Applying minimum configurations into $output_config.new\n";
1909
1910 open (OUT, ">$outconfig") or
1911 dodie "Can't create $outconfig";
1912
1913 if (-f $output_config) {
1914 open (IN, $output_config) or
1915 dodie "Failed to open $output_config";
1916 while (<IN>) {
1917 if (/^(# )?(CONFIG_[^\s=]*)/) {
1918 next if (defined($force_config{$2}));
1919 }
1920 print OUT;
1921 }
1922 close IN;
1923 }
1924 foreach my $config (keys %force_config) {
1925 print OUT "$force_config{$config}\n";
1926 }
1927 close OUT;
1928
1929 run_command "mv $outconfig $output_config";
1930}
1931
1932sub make_oldconfig {
1933
Steven Rostedt4c4ab122011-07-15 21:16:17 -04001934 my @force_list = keys %force_config;
1935
1936 if ($#force_list >= 0) {
1937 apply_min_config;
1938 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001939
Adam Leefb16d892012-09-01 01:05:17 +08001940 if (!run_command "$make olddefconfig") {
1941 # Perhaps olddefconfig doesn't exist in this version of the kernel
Steven Rostedt612b9e92011-03-07 13:27:43 -05001942 # try a yes '' | oldconfig
Adam Leefb16d892012-09-01 01:05:17 +08001943 doprint "olddefconfig failed, trying yes '' | make oldconfig\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001944 run_command "yes '' | $make oldconfig" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05001945 dodie "failed make config oldconfig";
1946 }
1947}
1948
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001949# read a config file and use this to force new configs.
1950sub load_force_config {
1951 my ($config) = @_;
1952
Steven Rostedtcf79fab2012-07-19 15:29:43 -04001953 doprint "Loading force configs from $config\n";
Steven Rostedtfcb3f162011-06-13 10:40:58 -04001954 open(IN, $config) or
1955 dodie "failed to read $config";
1956 while (<IN>) {
1957 chomp;
1958 if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
1959 $force_config{$1} = $_;
1960 } elsif (/^# (CONFIG_\S*) is not set/) {
1961 $force_config{$1} = $_;
1962 }
1963 }
1964 close IN;
1965}
1966
Steven Rostedt2545eb62010-11-02 15:01:32 -04001967sub build {
1968 my ($type) = @_;
1969
Steven Rostedt7faafbd2010-11-02 14:58:22 -04001970 unlink $buildlog;
1971
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04001972 # Failed builds should not reboot the target
1973 my $save_no_reboot = $no_reboot;
1974 $no_reboot = 1;
1975
Steven Rostedt683a3e62012-05-18 13:34:35 -04001976 # Calculate a new version from here.
1977 $have_version = 0;
1978
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04001979 if (defined($pre_build)) {
1980 my $ret = run_command $pre_build;
1981 if (!$ret && defined($pre_build_die) &&
1982 $pre_build_die) {
1983 dodie "failed to pre_build\n";
1984 }
1985 }
1986
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001987 if ($type =~ /^useconfig:(.*)/) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001988 run_command "cp $1 $output_config" or
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001989 dodie "could not copy $1 to .config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04001990
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001991 $type = "oldconfig";
1992 }
1993
Steven Rostedt5c42fc52010-11-02 14:57:01 -04001994 # old config can ask questions
1995 if ($type eq "oldconfig") {
Adam Leefb16d892012-09-01 01:05:17 +08001996 $type = "olddefconfig";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04001997
1998 # allow for empty configs
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05001999 run_command "touch $output_config";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002000
Andrew Jones13488232011-08-12 15:32:04 +02002001 if (!$noclean) {
2002 run_command "mv $output_config $outputdir/config_temp" or
2003 dodie "moving .config";
Steven Rostedt2545eb62010-11-02 15:01:32 -04002004
Andrew Jones13488232011-08-12 15:32:04 +02002005 run_command "$make mrproper" or dodie "make mrproper";
2006
2007 run_command "mv $outputdir/config_temp $output_config" or
2008 dodie "moving config_temp";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002009 }
2010
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002011 } elsif (!$noclean) {
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05002012 unlink "$output_config";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002013 run_command "$make mrproper" or
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002014 dodie "make mrproper";
Steven Rostedt5c42fc52010-11-02 14:57:01 -04002015 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04002016
2017 # add something to distinguish this build
Steven Rostedta75fece2010-11-02 14:58:27 -04002018 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
2019 print OUT "$localversion\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04002020 close(OUT);
2021
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002022 if (defined($minconfig)) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002023 load_force_config($minconfig);
Steven Rostedt2545eb62010-11-02 15:01:32 -04002024 }
2025
Adam Leefb16d892012-09-01 01:05:17 +08002026 if ($type ne "olddefconfig") {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002027 run_command "$make $type" or
Steven Rostedt612b9e92011-03-07 13:27:43 -05002028 dodie "failed make config";
2029 }
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002030 # Run old config regardless, to enforce min configurations
2031 make_oldconfig;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002032
Steven Rostedta75fece2010-11-02 14:58:27 -04002033 $redirect = "$buildlog";
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04002034 my $build_ret = run_command "$make $build_options";
2035 undef $redirect;
2036
2037 if (defined($post_build)) {
Steven Rostedt683a3e62012-05-18 13:34:35 -04002038 # Because a post build may change the kernel version
2039 # do it now.
2040 get_version;
Steven Rostedt0bd6c1a2011-06-14 20:39:31 -04002041 my $ret = run_command $post_build;
2042 if (!$ret && defined($post_build_die) &&
2043 $post_build_die) {
2044 dodie "failed to post_build\n";
2045 }
2046 }
2047
2048 if (!$build_ret) {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002049 # bisect may need this to pass
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04002050 if ($in_bisect) {
2051 $no_reboot = $save_no_reboot;
2052 return 0;
2053 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002054 fail "failed build" and return 0;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002055 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002056
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04002057 $no_reboot = $save_no_reboot;
2058
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002059 return 1;
Steven Rostedt2545eb62010-11-02 15:01:32 -04002060}
2061
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002062sub halt {
Steven Rostedte48c5292010-11-02 14:35:37 -04002063 if (!run_ssh "halt" or defined($power_off)) {
Steven Rostedt576f6272010-11-02 14:58:38 -04002064 if (defined($poweroff_after_halt)) {
2065 sleep $poweroff_after_halt;
2066 run_command "$power_off";
2067 }
2068 } else {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002069 # nope? the zap it!
Steven Rostedta75fece2010-11-02 14:58:27 -04002070 run_command "$power_off";
Steven Rostedt75c3fda72010-11-02 14:57:21 -04002071 }
2072}
2073
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002074sub success {
2075 my ($i) = @_;
2076
Steven Rostedt921ed4c2012-07-19 15:18:27 -04002077 if (defined($post_test)) {
2078 run_command $post_test;
2079 }
2080
Steven Rostedte48c5292010-11-02 14:35:37 -04002081 $successes++;
2082
Steven Rostedt9064af52011-06-13 10:38:48 -04002083 my $name = "";
2084
2085 if (defined($test_name)) {
2086 $name = " ($test_name)";
2087 }
2088
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002089 doprint "\n\n*******************************************\n";
2090 doprint "*******************************************\n";
Steven Rostedt9064af52011-06-13 10:38:48 -04002091 doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002092 doprint "*******************************************\n";
2093 doprint "*******************************************\n";
2094
Rabin Vincentde5b6e32011-11-18 17:05:31 +05302095 if (defined($store_successes)) {
2096 save_logs "success", $store_successes;
2097 }
2098
Steven Rostedt576f6272010-11-02 14:58:38 -04002099 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002100 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002101 reboot_to_good $sleep_time;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002102 }
2103}
2104
Steven Rostedtc960bb92011-03-08 09:22:39 -05002105sub answer_bisect {
2106 for (;;) {
2107 doprint "Pass or fail? [p/f]";
2108 my $ans = <STDIN>;
2109 chomp $ans;
2110 if ($ans eq "p" || $ans eq "P") {
2111 return 1;
2112 } elsif ($ans eq "f" || $ans eq "F") {
2113 return 0;
2114 } else {
2115 print "Please answer 'P' or 'F'\n";
2116 }
2117 }
2118}
2119
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002120sub child_run_test {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002121 my $failed = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002122
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002123 # child should have no power
Steven Rostedta75fece2010-11-02 14:58:27 -04002124 $reboot_on_error = 0;
2125 $poweroff_on_error = 0;
2126 $die_on_failure = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002127
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05302128 $redirect = "$testlog";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002129 run_command $run_test or $failed = 1;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05302130 undef $redirect;
2131
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002132 exit $failed;
2133}
2134
2135my $child_done;
2136
2137sub child_finished {
2138 $child_done = 1;
2139}
2140
2141sub do_run_test {
2142 my $child_pid;
2143 my $child_exit;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002144 my $line;
2145 my $full_line;
2146 my $bug = 0;
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002147 my $bug_ignored = 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002148
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002149 wait_for_monitor 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002150
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002151 doprint "run test $run_test\n";
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002152
2153 $child_done = 0;
2154
2155 $SIG{CHLD} = qw(child_finished);
2156
2157 $child_pid = fork;
2158
2159 child_run_test if (!$child_pid);
2160
2161 $full_line = "";
2162
2163 do {
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002164 $line = wait_for_input($monitor_fp, 1);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002165 if (defined($line)) {
2166
2167 # we are not guaranteed to get a full line
2168 $full_line .= $line;
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002169 doprint $line;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002170
2171 if ($full_line =~ /call trace:/i) {
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002172 if ($ignore_errors) {
2173 $bug_ignored = 1;
2174 } else {
2175 $bug = 1;
2176 }
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002177 }
2178
2179 if ($full_line =~ /Kernel panic -/) {
2180 $bug = 1;
2181 }
2182
2183 if ($line =~ /\n/) {
2184 $full_line = "";
2185 }
2186 }
2187 } while (!$child_done && !$bug);
2188
Steven Rostedt9b1d3672012-07-30 14:30:53 -04002189 if (!$bug && $bug_ignored) {
2190 doprint "WARNING: Call Trace detected but ignored due to IGNORE_ERRORS=1\n";
2191 }
2192
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002193 if ($bug) {
Steven Rostedt8ea0e062011-03-08 09:44:35 -05002194 my $failure_start = time;
2195 my $now;
2196 do {
2197 $line = wait_for_input($monitor_fp, 1);
2198 if (defined($line)) {
2199 doprint $line;
2200 }
2201 $now = time;
2202 if ($now - $failure_start >= $stop_after_failure) {
2203 last;
2204 }
2205 } while (defined($line));
2206
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002207 doprint "Detected kernel crash!\n";
2208 # kill the child with extreme prejudice
2209 kill 9, $child_pid;
2210 }
2211
2212 waitpid $child_pid, 0;
2213 $child_exit = $?;
2214
Steven Rostedtc5dacb82011-12-22 12:43:57 -05002215 if (!$bug && $in_bisect) {
2216 if (defined($bisect_ret_good)) {
2217 if ($child_exit == $bisect_ret_good) {
2218 return 1;
2219 }
2220 }
2221 if (defined($bisect_ret_skip)) {
2222 if ($child_exit == $bisect_ret_skip) {
2223 return -1;
2224 }
2225 }
2226 if (defined($bisect_ret_abort)) {
2227 if ($child_exit == $bisect_ret_abort) {
2228 fail "test abort" and return -2;
2229 }
2230 }
2231 if (defined($bisect_ret_bad)) {
2232 if ($child_exit == $bisect_ret_skip) {
2233 return 0;
2234 }
2235 }
2236 if (defined($bisect_ret_default)) {
2237 if ($bisect_ret_default eq "good") {
2238 return 1;
2239 } elsif ($bisect_ret_default eq "bad") {
2240 return 0;
2241 } elsif ($bisect_ret_default eq "skip") {
2242 return -1;
2243 } elsif ($bisect_ret_default eq "abort") {
2244 return -2;
2245 } else {
2246 fail "unknown default action: $bisect_ret_default"
2247 and return -2;
2248 }
2249 }
2250 }
2251
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002252 if ($bug || $child_exit) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002253 return 0 if $in_bisect;
2254 fail "test failed" and return 0;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002255 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002256 return 1;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002257}
2258
Steven Rostedta75fece2010-11-02 14:58:27 -04002259sub run_git_bisect {
2260 my ($command) = @_;
2261
2262 doprint "$command ... ";
2263
2264 my $output = `$command 2>&1`;
2265 my $ret = $?;
2266
2267 logit $output;
2268
2269 if ($ret) {
2270 doprint "FAILED\n";
2271 dodie "Failed to git bisect";
2272 }
2273
2274 doprint "SUCCESS\n";
2275 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
2276 doprint "$1 [$2]\n";
2277 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002278 $bisect_bad_commit = $1;
Steven Rostedta75fece2010-11-02 14:58:27 -04002279 doprint "Found bad commit... $1\n";
2280 return 0;
2281 } else {
2282 # we already logged it, just print it now.
2283 print $output;
2284 }
2285
2286 return 1;
2287}
2288
Steven Rostedtc23dca72011-03-08 09:26:31 -05002289sub bisect_reboot {
2290 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002291 reboot_to_good $bisect_sleep_time;
Steven Rostedtc23dca72011-03-08 09:26:31 -05002292}
2293
2294# returns 1 on success, 0 on failure, -1 on skip
Steven Rostedt0a05c762010-11-08 11:14:10 -05002295sub run_bisect_test {
2296 my ($type, $buildtype) = @_;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002297
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002298 my $failed = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002299 my $result;
2300 my $output;
2301 my $ret;
2302
Steven Rostedt0a05c762010-11-08 11:14:10 -05002303 $in_bisect = 1;
2304
2305 build $buildtype or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002306
2307 if ($type ne "build") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002308 if ($failed && $bisect_skip) {
2309 $in_bisect = 0;
2310 return -1;
2311 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002312 dodie "Failed on build" if $failed;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002313
2314 # Now boot the box
Steven Rostedtddf607e2011-06-14 20:49:13 -04002315 start_monitor_and_boot or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002316
2317 if ($type ne "boot") {
Steven Rostedtc23dca72011-03-08 09:26:31 -05002318 if ($failed && $bisect_skip) {
2319 end_monitor;
2320 bisect_reboot;
2321 $in_bisect = 0;
2322 return -1;
2323 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002324 dodie "Failed on boot" if $failed;
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002325
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002326 do_run_test or $failed = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002327 }
Steven Rostedt7faafbd2010-11-02 14:58:22 -04002328 end_monitor;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002329 }
2330
2331 if ($failed) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002332 $result = 0;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002333 } else {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002334 $result = 1;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002335 }
Steven Rostedt4025bc62011-05-20 09:16:29 -04002336
2337 # reboot the box to a kernel we can ssh to
2338 if ($type ne "build") {
2339 bisect_reboot;
2340 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002341 $in_bisect = 0;
2342
2343 return $result;
2344}
2345
2346sub run_bisect {
2347 my ($type) = @_;
2348 my $buildtype = "oldconfig";
2349
2350 # We should have a minconfig to use?
2351 if (defined($minconfig)) {
2352 $buildtype = "useconfig:$minconfig";
2353 }
2354
2355 my $ret = run_bisect_test $type, $buildtype;
2356
Steven Rostedtc960bb92011-03-08 09:22:39 -05002357 if ($bisect_manual) {
2358 $ret = answer_bisect;
2359 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002360
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002361 # Are we looking for where it worked, not failed?
Russ Dill5158ba3e2012-04-23 19:43:00 -07002362 if ($reverse_bisect && $ret >= 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002363 $ret = !$ret;
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002364 }
2365
Steven Rostedtc23dca72011-03-08 09:26:31 -05002366 if ($ret > 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002367 return "good";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002368 } elsif ($ret == 0) {
Steven Rostedt0a05c762010-11-08 11:14:10 -05002369 return "bad";
Steven Rostedtc23dca72011-03-08 09:26:31 -05002370 } elsif ($bisect_skip) {
2371 doprint "HIT A BAD COMMIT ... SKIPPING\n";
2372 return "skip";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002373 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002374}
2375
Steven Rostedtdad98752011-11-22 20:48:57 -05002376sub update_bisect_replay {
2377 my $tmp_log = "$tmpdir/ktest_bisect_log";
2378 run_command "git bisect log > $tmp_log" or
2379 die "can't create bisect log";
2380 return $tmp_log;
2381}
2382
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002383sub bisect {
2384 my ($i) = @_;
2385
2386 my $result;
2387
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002388 die "BISECT_GOOD[$i] not defined\n" if (!defined($bisect_good));
2389 die "BISECT_BAD[$i] not defined\n" if (!defined($bisect_bad));
2390 die "BISECT_TYPE[$i] not defined\n" if (!defined($bisect_type));
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002391
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002392 my $good = $bisect_good;
2393 my $bad = $bisect_bad;
2394 my $type = $bisect_type;
2395 my $start = $bisect_start;
2396 my $replay = $bisect_replay;
2397 my $start_files = $bisect_files;
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002398
2399 if (defined($start_files)) {
2400 $start_files = " -- " . $start_files;
2401 } else {
2402 $start_files = "";
2403 }
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002404
Steven Rostedta57419b2010-11-02 15:13:54 -04002405 # convert to true sha1's
2406 $good = get_sha1($good);
2407 $bad = get_sha1($bad);
2408
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002409 if (defined($bisect_reverse) && $bisect_reverse == 1) {
Steven Rostedtd6ce2a02010-11-02 14:58:05 -04002410 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
2411 $reverse_bisect = 1;
2412 } else {
2413 $reverse_bisect = 0;
2414 }
2415
Steven Rostedt5a391fb2010-11-02 14:57:43 -04002416 # Can't have a test without having a test to run
2417 if ($type eq "test" && !defined($run_test)) {
2418 $type = "boot";
2419 }
2420
Steven Rostedtdad98752011-11-22 20:48:57 -05002421 # Check if a bisect was running
2422 my $bisect_start_file = "$builddir/.git/BISECT_START";
2423
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002424 my $check = $bisect_check;
Steven Rostedtdad98752011-11-22 20:48:57 -05002425 my $do_check = defined($check) && $check ne "0";
2426
2427 if ( -f $bisect_start_file ) {
2428 print "Bisect in progress found\n";
2429 if ($do_check) {
2430 print " If you say yes, then no checks of good or bad will be done\n";
2431 }
2432 if (defined($replay)) {
2433 print "** BISECT_REPLAY is defined in config file **";
2434 print " Ignore config option and perform new git bisect log?\n";
2435 if (read_ync " (yes, no, or cancel) ") {
2436 $replay = update_bisect_replay;
2437 $do_check = 0;
2438 }
2439 } elsif (read_yn "read git log and continue?") {
2440 $replay = update_bisect_replay;
2441 $do_check = 0;
2442 }
2443 }
2444
2445 if ($do_check) {
Steven Rostedta75fece2010-11-02 14:58:27 -04002446
2447 # get current HEAD
Steven Rostedta57419b2010-11-02 15:13:54 -04002448 my $head = get_sha1("HEAD");
Steven Rostedta75fece2010-11-02 14:58:27 -04002449
2450 if ($check ne "good") {
2451 doprint "TESTING BISECT BAD [$bad]\n";
2452 run_command "git checkout $bad" or
2453 die "Failed to checkout $bad";
2454
2455 $result = run_bisect $type;
2456
2457 if ($result ne "bad") {
2458 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
2459 }
2460 }
2461
2462 if ($check ne "bad") {
2463 doprint "TESTING BISECT GOOD [$good]\n";
2464 run_command "git checkout $good" or
2465 die "Failed to checkout $good";
2466
2467 $result = run_bisect $type;
2468
2469 if ($result ne "good") {
2470 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
2471 }
2472 }
2473
2474 # checkout where we started
2475 run_command "git checkout $head" or
2476 die "Failed to checkout $head";
2477 }
2478
Steven Rostedt3410f6f2011-03-08 09:38:12 -05002479 run_command "git bisect start$start_files" or
Steven Rostedta75fece2010-11-02 14:58:27 -04002480 dodie "could not start bisect";
2481
2482 run_command "git bisect good $good" or
2483 dodie "could not set bisect good to $good";
2484
2485 run_git_bisect "git bisect bad $bad" or
2486 dodie "could not set bisect bad to $bad";
2487
2488 if (defined($replay)) {
2489 run_command "git bisect replay $replay" or
2490 dodie "failed to run replay";
2491 }
2492
2493 if (defined($start)) {
2494 run_command "git checkout $start" or
2495 dodie "failed to checkout $start";
2496 }
2497
2498 my $test;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002499 do {
2500 $result = run_bisect $type;
Steven Rostedta75fece2010-11-02 14:58:27 -04002501 $test = run_git_bisect "git bisect $result";
2502 } while ($test);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002503
2504 run_command "git bisect log" or
2505 dodie "could not capture git bisect log";
2506
2507 run_command "git bisect reset" or
2508 dodie "could not reset git bisect";
2509
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002510 doprint "Bad commit was [$bisect_bad_commit]\n";
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002511
Steven Rostedt0a05c762010-11-08 11:14:10 -05002512 success $i;
2513}
2514
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002515# config_ignore holds the configs that were set (or unset) for
2516# a good config and we will ignore these configs for the rest
2517# of a config bisect. These configs stay as they were.
Steven Rostedt0a05c762010-11-08 11:14:10 -05002518my %config_ignore;
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002519
2520# config_set holds what all configs were set as.
Steven Rostedt0a05c762010-11-08 11:14:10 -05002521my %config_set;
2522
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002523# config_off holds the set of configs that the bad config had disabled.
2524# We need to record them and set them in the .config when running
Adam Leefb16d892012-09-01 01:05:17 +08002525# olddefconfig, because olddefconfig keeps the defaults.
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002526my %config_off;
2527
2528# config_off_tmp holds a set of configs to turn off for now
2529my @config_off_tmp;
2530
2531# config_list is the set of configs that are being tested
Steven Rostedt0a05c762010-11-08 11:14:10 -05002532my %config_list;
2533my %null_config;
2534
2535my %dependency;
2536
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002537sub assign_configs {
2538 my ($hash, $config) = @_;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002539
2540 open (IN, $config)
2541 or dodie "Failed to read $config";
2542
2543 while (<IN>) {
Steven Rostedt9bf71742011-06-01 23:27:19 -04002544 if (/^((CONFIG\S*)=.*)/) {
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002545 ${$hash}{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002546 }
2547 }
2548
2549 close(IN);
2550}
2551
Steven Rostedt4c4ab122011-07-15 21:16:17 -04002552sub process_config_ignore {
2553 my ($config) = @_;
2554
2555 assign_configs \%config_ignore, $config;
2556}
2557
Steven Rostedt0a05c762010-11-08 11:14:10 -05002558sub read_current_config {
2559 my ($config_ref) = @_;
2560
2561 %{$config_ref} = ();
2562 undef %{$config_ref};
2563
2564 my @key = keys %{$config_ref};
2565 if ($#key >= 0) {
2566 print "did not delete!\n";
2567 exit;
2568 }
2569 open (IN, "$output_config");
2570
2571 while (<IN>) {
2572 if (/^(CONFIG\S+)=(.*)/) {
2573 ${$config_ref}{$1} = $2;
2574 }
2575 }
2576 close(IN);
2577}
2578
2579sub get_dependencies {
2580 my ($config) = @_;
2581
2582 my $arr = $dependency{$config};
2583 if (!defined($arr)) {
2584 return ();
2585 }
2586
2587 my @deps = @{$arr};
2588
2589 foreach my $dep (@{$arr}) {
2590 print "ADD DEP $dep\n";
2591 @deps = (@deps, get_dependencies $dep);
2592 }
2593
2594 return @deps;
2595}
2596
2597sub create_config {
2598 my @configs = @_;
2599
2600 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
2601
2602 foreach my $config (@configs) {
2603 print OUT "$config_set{$config}\n";
2604 my @deps = get_dependencies $config;
2605 foreach my $dep (@deps) {
2606 print OUT "$config_set{$dep}\n";
2607 }
2608 }
2609
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002610 # turn off configs to keep off
2611 foreach my $config (keys %config_off) {
2612 print OUT "# $config is not set\n";
2613 }
2614
2615 # turn off configs that should be off for now
2616 foreach my $config (@config_off_tmp) {
2617 print OUT "# $config is not set\n";
2618 }
2619
Steven Rostedt0a05c762010-11-08 11:14:10 -05002620 foreach my $config (keys %config_ignore) {
2621 print OUT "$config_ignore{$config}\n";
2622 }
2623 close(OUT);
2624
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002625 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002626}
2627
2628sub compare_configs {
2629 my (%a, %b) = @_;
2630
2631 foreach my $item (keys %a) {
2632 if (!defined($b{$item})) {
2633 print "diff $item\n";
2634 return 1;
2635 }
2636 delete $b{$item};
2637 }
2638
2639 my @keys = keys %b;
2640 if ($#keys) {
2641 print "diff2 $keys[0]\n";
2642 }
2643 return -1 if ($#keys >= 0);
2644
2645 return 0;
2646}
2647
2648sub run_config_bisect_test {
2649 my ($type) = @_;
2650
2651 return run_bisect_test $type, "oldconfig";
2652}
2653
2654sub process_passed {
2655 my (%configs) = @_;
2656
2657 doprint "These configs had no failure: (Enabling them for further compiles)\n";
2658 # Passed! All these configs are part of a good compile.
2659 # Add them to the min options.
2660 foreach my $config (keys %configs) {
2661 if (defined($config_list{$config})) {
2662 doprint " removing $config\n";
2663 $config_ignore{$config} = $config_list{$config};
2664 delete $config_list{$config};
2665 }
2666 }
Steven Rostedtf1a27852010-11-11 11:34:38 -05002667 doprint "config copied to $outputdir/config_good\n";
2668 run_command "cp -f $output_config $outputdir/config_good";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002669}
2670
2671sub process_failed {
2672 my ($config) = @_;
2673
2674 doprint "\n\n***************************************\n";
2675 doprint "Found bad config: $config\n";
2676 doprint "***************************************\n\n";
2677}
2678
2679sub run_config_bisect {
2680
2681 my @start_list = keys %config_list;
2682
2683 if ($#start_list < 0) {
2684 doprint "No more configs to test!!!\n";
2685 return -1;
2686 }
2687
2688 doprint "***** RUN TEST ***\n";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002689 my $type = $config_bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002690 my $ret;
2691 my %current_config;
2692
2693 my $count = $#start_list + 1;
2694 doprint " $count configs to test\n";
2695
2696 my $half = int($#start_list / 2);
2697
2698 do {
2699 my @tophalf = @start_list[0 .. $half];
2700
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002701 # keep the bottom half off
2702 if ($half < $#start_list) {
2703 @config_off_tmp = @start_list[$half + 1 .. $#start_list];
2704 } else {
2705 @config_off_tmp = ();
2706 }
2707
Steven Rostedt0a05c762010-11-08 11:14:10 -05002708 create_config @tophalf;
2709 read_current_config \%current_config;
2710
2711 $count = $#tophalf + 1;
2712 doprint "Testing $count configs\n";
2713 my $found = 0;
2714 # make sure we test something
2715 foreach my $config (@tophalf) {
2716 if (defined($current_config{$config})) {
2717 logit " $config\n";
2718 $found = 1;
2719 }
2720 }
2721 if (!$found) {
2722 # try the other half
2723 doprint "Top half produced no set configs, trying bottom half\n";
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002724
2725 # keep the top half off
2726 @config_off_tmp = @tophalf;
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002727 @tophalf = @start_list[$half + 1 .. $#start_list];
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002728
Steven Rostedt0a05c762010-11-08 11:14:10 -05002729 create_config @tophalf;
2730 read_current_config \%current_config;
2731 foreach my $config (@tophalf) {
2732 if (defined($current_config{$config})) {
2733 logit " $config\n";
2734 $found = 1;
2735 }
2736 }
2737 if (!$found) {
2738 doprint "Failed: Can't make new config with current configs\n";
2739 foreach my $config (@start_list) {
2740 doprint " CONFIG: $config\n";
2741 }
2742 return -1;
2743 }
2744 $count = $#tophalf + 1;
2745 doprint "Testing $count configs\n";
2746 }
2747
2748 $ret = run_config_bisect_test $type;
Steven Rostedtc960bb92011-03-08 09:22:39 -05002749 if ($bisect_manual) {
2750 $ret = answer_bisect;
2751 }
Steven Rostedt0a05c762010-11-08 11:14:10 -05002752 if ($ret) {
2753 process_passed %current_config;
2754 return 0;
2755 }
2756
2757 doprint "This config had a failure.\n";
2758 doprint "Removing these configs that were not set in this config:\n";
Steven Rostedtf1a27852010-11-11 11:34:38 -05002759 doprint "config copied to $outputdir/config_bad\n";
2760 run_command "cp -f $output_config $outputdir/config_bad";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002761
2762 # A config exists in this group that was bad.
2763 foreach my $config (keys %config_list) {
2764 if (!defined($current_config{$config})) {
2765 doprint " removing $config\n";
2766 delete $config_list{$config};
2767 }
2768 }
2769
2770 @start_list = @tophalf;
2771
2772 if ($#start_list == 0) {
2773 process_failed $start_list[0];
2774 return 1;
2775 }
2776
2777 # remove half the configs we are looking at and see if
2778 # they are good.
2779 $half = int($#start_list / 2);
Steven Rostedt4c8cc552011-06-01 23:22:30 -04002780 } while ($#start_list > 0);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002781
Steven Rostedtc960bb92011-03-08 09:22:39 -05002782 # we found a single config, try it again unless we are running manually
2783
2784 if ($bisect_manual) {
2785 process_failed $start_list[0];
2786 return 1;
2787 }
2788
Steven Rostedt0a05c762010-11-08 11:14:10 -05002789 my @tophalf = @start_list[0 .. 0];
2790
2791 $ret = run_config_bisect_test $type;
2792 if ($ret) {
2793 process_passed %current_config;
2794 return 0;
2795 }
2796
2797 process_failed $start_list[0];
2798 return 1;
2799}
2800
2801sub config_bisect {
2802 my ($i) = @_;
2803
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002804 my $start_config = $config_bisect;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002805
2806 my $tmpconfig = "$tmpdir/use_config";
2807
Steven Rostedt30f75da2011-06-13 10:35:35 -04002808 if (defined($config_bisect_good)) {
2809 process_config_ignore $config_bisect_good;
2810 }
2811
Steven Rostedt0a05c762010-11-08 11:14:10 -05002812 # Make the file with the bad config and the min config
2813 if (defined($minconfig)) {
2814 # read the min config for things to ignore
2815 run_command "cp $minconfig $tmpconfig" or
2816 dodie "failed to copy $minconfig to $tmpconfig";
2817 } else {
2818 unlink $tmpconfig;
2819 }
2820
Steven Rostedt0a05c762010-11-08 11:14:10 -05002821 if (-f $tmpconfig) {
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002822 load_force_config($tmpconfig);
Steven Rostedt0a05c762010-11-08 11:14:10 -05002823 process_config_ignore $tmpconfig;
2824 }
2825
2826 # now process the start config
2827 run_command "cp $start_config $output_config" or
2828 dodie "failed to copy $start_config to $output_config";
2829
2830 # read directly what we want to check
2831 my %config_check;
2832 open (IN, $output_config)
Masanari Iidaf9dee312012-02-11 21:46:56 +09002833 or dodie "failed to open $output_config";
Steven Rostedt0a05c762010-11-08 11:14:10 -05002834
2835 while (<IN>) {
2836 if (/^((CONFIG\S*)=.*)/) {
2837 $config_check{$2} = $1;
2838 }
2839 }
2840 close(IN);
2841
Steven Rostedt250bae82011-07-15 22:05:59 -04002842 # Now run oldconfig with the minconfig
Steven Rostedtfcb3f162011-06-13 10:40:58 -04002843 make_oldconfig;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002844
2845 # check to see what we lost (or gained)
2846 open (IN, $output_config)
2847 or dodie "Failed to read $start_config";
2848
2849 my %removed_configs;
2850 my %added_configs;
2851
2852 while (<IN>) {
2853 if (/^((CONFIG\S*)=.*)/) {
2854 # save off all options
2855 $config_set{$2} = $1;
2856 if (defined($config_check{$2})) {
2857 if (defined($config_ignore{$2})) {
2858 $removed_configs{$2} = $1;
2859 } else {
2860 $config_list{$2} = $1;
2861 }
2862 } elsif (!defined($config_ignore{$2})) {
2863 $added_configs{$2} = $1;
2864 $config_list{$2} = $1;
2865 }
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002866 } elsif (/^# ((CONFIG\S*).*)/) {
2867 # Keep these configs disabled
2868 $config_set{$2} = $1;
2869 $config_off{$2} = $1;
Steven Rostedt0a05c762010-11-08 11:14:10 -05002870 }
2871 }
2872 close(IN);
2873
2874 my @confs = keys %removed_configs;
2875 if ($#confs >= 0) {
2876 doprint "Configs overridden by default configs and removed from check:\n";
2877 foreach my $config (@confs) {
2878 doprint " $config\n";
2879 }
2880 }
2881 @confs = keys %added_configs;
2882 if ($#confs >= 0) {
2883 doprint "Configs appearing in make oldconfig and added:\n";
2884 foreach my $config (@confs) {
2885 doprint " $config\n";
2886 }
2887 }
2888
2889 my %config_test;
2890 my $once = 0;
2891
Steven Rostedtcf79fab2012-07-19 15:29:43 -04002892 @config_off_tmp = ();
2893
Steven Rostedt0a05c762010-11-08 11:14:10 -05002894 # Sometimes kconfig does weird things. We must make sure
2895 # that the config we autocreate has everything we need
2896 # to test, otherwise we may miss testing configs, or
2897 # may not be able to create a new config.
2898 # Here we create a config with everything set.
2899 create_config (keys %config_list);
2900 read_current_config \%config_test;
2901 foreach my $config (keys %config_list) {
2902 if (!defined($config_test{$config})) {
2903 if (!$once) {
2904 $once = 1;
2905 doprint "Configs not produced by kconfig (will not be checked):\n";
2906 }
2907 doprint " $config\n";
2908 delete $config_list{$config};
2909 }
2910 }
2911 my $ret;
Steven Rostedtb0918612012-07-19 15:26:00 -04002912
2913 if (defined($config_bisect_check) && $config_bisect_check) {
2914 doprint " Checking to make sure bad config with min config fails\n";
2915 create_config keys %config_list;
2916 $ret = run_config_bisect_test $config_bisect_type;
2917 if ($ret) {
2918 doprint " FAILED! Bad config with min config boots fine\n";
2919 return -1;
2920 }
2921 doprint " Bad config with min config fails as expected\n";
2922 }
2923
Steven Rostedt0a05c762010-11-08 11:14:10 -05002924 do {
2925 $ret = run_config_bisect;
2926 } while (!$ret);
2927
2928 return $ret if ($ret < 0);
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04002929
2930 success $i;
2931}
2932
Steven Rostedt27d934b2011-05-20 09:18:18 -04002933sub patchcheck_reboot {
2934 doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05002935 reboot_to_good $patchcheck_sleep_time;
Steven Rostedt27d934b2011-05-20 09:18:18 -04002936}
2937
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002938sub patchcheck {
2939 my ($i) = @_;
2940
2941 die "PATCHCHECK_START[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002942 if (!defined($patchcheck_start));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002943 die "PATCHCHECK_TYPE[$i] not defined\n"
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002944 if (!defined($patchcheck_type));
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002945
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002946 my $start = $patchcheck_start;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002947
2948 my $end = "HEAD";
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002949 if (defined($patchcheck_end)) {
2950 $end = $patchcheck_end;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002951 }
2952
Steven Rostedta57419b2010-11-02 15:13:54 -04002953 # Get the true sha1's since we can use things like HEAD~3
2954 $start = get_sha1($start);
2955 $end = get_sha1($end);
2956
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05002957 my $type = $patchcheck_type;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002958
2959 # Can't have a test without having a test to run
2960 if ($type eq "test" && !defined($run_test)) {
2961 $type = "boot";
2962 }
2963
2964 open (IN, "git log --pretty=oneline $end|") or
2965 dodie "could not get git list";
2966
2967 my @list;
2968
2969 while (<IN>) {
2970 chomp;
2971 $list[$#list+1] = $_;
2972 last if (/^$start/);
2973 }
2974 close(IN);
2975
2976 if ($list[$#list] !~ /^$start/) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04002977 fail "SHA1 $start not found";
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002978 }
2979
2980 # go backwards in the list
2981 @list = reverse @list;
2982
2983 my $save_clean = $noclean;
Steven Rostedt19902072011-06-14 20:46:25 -04002984 my %ignored_warnings;
2985
2986 if (defined($ignore_warnings)) {
2987 foreach my $sha1 (split /\s+/, $ignore_warnings) {
2988 $ignored_warnings{$sha1} = 1;
2989 }
2990 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04002991
2992 $in_patchcheck = 1;
2993 foreach my $item (@list) {
2994 my $sha1 = $item;
2995 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
2996
2997 doprint "\nProcessing commit $item\n\n";
2998
2999 run_command "git checkout $sha1" or
3000 die "Failed to checkout $sha1";
3001
3002 # only clean on the first and last patch
3003 if ($item eq $list[0] ||
3004 $item eq $list[$#list]) {
3005 $noclean = $save_clean;
3006 } else {
3007 $noclean = 1;
3008 }
3009
3010 if (defined($minconfig)) {
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003011 build "useconfig:$minconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003012 } else {
3013 # ?? no config to use?
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003014 build "oldconfig" or return 0;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003015 }
3016
Steven Rostedt19902072011-06-14 20:46:25 -04003017
3018 if (!defined($ignored_warnings{$sha1})) {
3019 check_buildlog $sha1 or return 0;
3020 }
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003021
3022 next if ($type eq "build");
3023
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003024 my $failed = 0;
3025
Steven Rostedtddf607e2011-06-14 20:49:13 -04003026 start_monitor_and_boot or $failed = 1;
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003027
3028 if (!$failed && $type ne "boot"){
3029 do_run_test or $failed = 1;
3030 }
3031 end_monitor;
3032 return 0 if ($failed);
3033
Steven Rostedt27d934b2011-05-20 09:18:18 -04003034 patchcheck_reboot;
3035
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003036 }
3037 $in_patchcheck = 0;
3038 success $i;
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003039
3040 return 1;
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003041}
3042
Steven Rostedtb9066f62011-07-15 21:25:24 -04003043my %depends;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003044my %depcount;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003045my $iflevel = 0;
3046my @ifdeps;
3047
3048# prevent recursion
3049my %read_kconfigs;
3050
Steven Rostedtac6974c2011-10-04 09:40:17 -04003051sub add_dep {
3052 # $config depends on $dep
3053 my ($config, $dep) = @_;
3054
3055 if (defined($depends{$config})) {
3056 $depends{$config} .= " " . $dep;
3057 } else {
3058 $depends{$config} = $dep;
3059 }
3060
3061 # record the number of configs depending on $dep
3062 if (defined $depcount{$dep}) {
3063 $depcount{$dep}++;
3064 } else {
3065 $depcount{$dep} = 1;
3066 }
3067}
3068
Steven Rostedtb9066f62011-07-15 21:25:24 -04003069# taken from streamline_config.pl
3070sub read_kconfig {
3071 my ($kconfig) = @_;
3072
3073 my $state = "NONE";
3074 my $config;
3075 my @kconfigs;
3076
3077 my $cont = 0;
3078 my $line;
3079
3080
3081 if (! -f $kconfig) {
3082 doprint "file $kconfig does not exist, skipping\n";
3083 return;
3084 }
3085
3086 open(KIN, "$kconfig")
3087 or die "Can't open $kconfig";
3088 while (<KIN>) {
3089 chomp;
3090
3091 # Make sure that lines ending with \ continue
3092 if ($cont) {
3093 $_ = $line . " " . $_;
3094 }
3095
3096 if (s/\\$//) {
3097 $cont = 1;
3098 $line = $_;
3099 next;
3100 }
3101
3102 $cont = 0;
3103
3104 # collect any Kconfig sources
3105 if (/^source\s*"(.*)"/) {
3106 $kconfigs[$#kconfigs+1] = $1;
3107 }
3108
3109 # configs found
3110 if (/^\s*(menu)?config\s+(\S+)\s*$/) {
3111 $state = "NEW";
3112 $config = $2;
3113
3114 for (my $i = 0; $i < $iflevel; $i++) {
Steven Rostedtac6974c2011-10-04 09:40:17 -04003115 add_dep $config, $ifdeps[$i];
Steven Rostedtb9066f62011-07-15 21:25:24 -04003116 }
3117
3118 # collect the depends for the config
3119 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
3120
Steven Rostedtac6974c2011-10-04 09:40:17 -04003121 add_dep $config, $1;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003122
3123 # Get the configs that select this config
Steven Rostedtac6974c2011-10-04 09:40:17 -04003124 } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
3125
3126 # selected by depends on config
3127 add_dep $1, $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003128
3129 # Check for if statements
3130 } elsif (/^if\s+(.*\S)\s*$/) {
3131 my $deps = $1;
3132 # remove beginning and ending non text
3133 $deps =~ s/^[^a-zA-Z0-9_]*//;
3134 $deps =~ s/[^a-zA-Z0-9_]*$//;
3135
3136 my @deps = split /[^a-zA-Z0-9_]+/, $deps;
3137
3138 $ifdeps[$iflevel++] = join ':', @deps;
3139
3140 } elsif (/^endif/) {
3141
3142 $iflevel-- if ($iflevel);
3143
3144 # stop on "help"
3145 } elsif (/^\s*help\s*$/) {
3146 $state = "NONE";
3147 }
3148 }
3149 close(KIN);
3150
3151 # read in any configs that were found.
3152 foreach $kconfig (@kconfigs) {
3153 if (!defined($read_kconfigs{$kconfig})) {
3154 $read_kconfigs{$kconfig} = 1;
3155 read_kconfig("$builddir/$kconfig");
3156 }
3157 }
3158}
3159
3160sub read_depends {
3161 # find out which arch this is by the kconfig file
3162 open (IN, $output_config)
3163 or dodie "Failed to read $output_config";
3164 my $arch;
3165 while (<IN>) {
3166 if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
3167 $arch = $1;
3168 last;
3169 }
3170 }
3171 close IN;
3172
3173 if (!defined($arch)) {
3174 doprint "Could not find arch from config file\n";
3175 doprint "no dependencies used\n";
3176 return;
3177 }
3178
3179 # arch is really the subarch, we need to know
3180 # what directory to look at.
3181 if ($arch eq "i386" || $arch eq "x86_64") {
3182 $arch = "x86";
3183 } elsif ($arch =~ /^tile/) {
3184 $arch = "tile";
3185 }
3186
3187 my $kconfig = "$builddir/arch/$arch/Kconfig";
3188
3189 if (! -f $kconfig && $arch =~ /\d$/) {
3190 my $orig = $arch;
3191 # some subarchs have numbers, truncate them
3192 $arch =~ s/\d*$//;
3193 $kconfig = "$builddir/arch/$arch/Kconfig";
3194 if (! -f $kconfig) {
3195 doprint "No idea what arch dir $orig is for\n";
3196 doprint "no dependencies used\n";
3197 return;
3198 }
3199 }
3200
3201 read_kconfig($kconfig);
3202}
3203
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003204sub read_config_list {
3205 my ($config) = @_;
3206
3207 open (IN, $config)
3208 or dodie "Failed to read $config";
3209
3210 while (<IN>) {
3211 if (/^((CONFIG\S*)=.*)/) {
3212 if (!defined($config_ignore{$2})) {
3213 $config_list{$2} = $1;
3214 }
3215 }
3216 }
3217
3218 close(IN);
3219}
3220
3221sub read_output_config {
3222 my ($config) = @_;
3223
3224 assign_configs \%config_ignore, $config;
3225}
3226
3227sub make_new_config {
3228 my @configs = @_;
3229
3230 open (OUT, ">$output_config")
3231 or dodie "Failed to write $output_config";
3232
3233 foreach my $config (@configs) {
3234 print OUT "$config\n";
3235 }
3236 close OUT;
3237}
3238
Steven Rostedtac6974c2011-10-04 09:40:17 -04003239sub chomp_config {
3240 my ($config) = @_;
3241
3242 $config =~ s/CONFIG_//;
3243
3244 return $config;
3245}
3246
Steven Rostedtb9066f62011-07-15 21:25:24 -04003247sub get_depends {
3248 my ($dep) = @_;
3249
Steven Rostedtac6974c2011-10-04 09:40:17 -04003250 my $kconfig = chomp_config $dep;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003251
3252 $dep = $depends{"$kconfig"};
3253
3254 # the dep string we have saves the dependencies as they
3255 # were found, including expressions like ! && ||. We
3256 # want to split this out into just an array of configs.
3257
3258 my $valid = "A-Za-z_0-9";
3259
3260 my @configs;
3261
3262 while ($dep =~ /[$valid]/) {
3263
3264 if ($dep =~ /^[^$valid]*([$valid]+)/) {
3265 my $conf = "CONFIG_" . $1;
3266
3267 $configs[$#configs + 1] = $conf;
3268
3269 $dep =~ s/^[^$valid]*[$valid]+//;
3270 } else {
3271 die "this should never happen";
3272 }
3273 }
3274
3275 return @configs;
3276}
3277
3278my %min_configs;
3279my %keep_configs;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003280my %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003281my %processed_configs;
3282my %nochange_config;
3283
3284sub test_this_config {
3285 my ($config) = @_;
3286
3287 my $found;
3288
3289 # if we already processed this config, skip it
3290 if (defined($processed_configs{$config})) {
3291 return undef;
3292 }
3293 $processed_configs{$config} = 1;
3294
3295 # if this config failed during this round, skip it
3296 if (defined($nochange_config{$config})) {
3297 return undef;
3298 }
3299
Steven Rostedtac6974c2011-10-04 09:40:17 -04003300 my $kconfig = chomp_config $config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003301
3302 # Test dependencies first
3303 if (defined($depends{"$kconfig"})) {
3304 my @parents = get_depends $config;
3305 foreach my $parent (@parents) {
3306 # if the parent is in the min config, check it first
3307 next if (!defined($min_configs{$parent}));
3308 $found = test_this_config($parent);
3309 if (defined($found)) {
3310 return $found;
3311 }
3312 }
3313 }
3314
3315 # Remove this config from the list of configs
Adam Leefb16d892012-09-01 01:05:17 +08003316 # do a make olddefconfig and then read the resulting
Steven Rostedtb9066f62011-07-15 21:25:24 -04003317 # .config to make sure it is missing the config that
3318 # we had before
3319 my %configs = %min_configs;
3320 delete $configs{$config};
3321 make_new_config ((values %configs), (values %keep_configs));
3322 make_oldconfig;
3323 undef %configs;
3324 assign_configs \%configs, $output_config;
3325
3326 return $config if (!defined($configs{$config}));
3327
3328 doprint "disabling config $config did not change .config\n";
3329
3330 $nochange_config{$config} = 1;
3331
3332 return undef;
3333}
3334
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003335sub make_min_config {
3336 my ($i) = @_;
3337
Steven Rostedtccc513b2012-05-21 17:13:40 -04003338 my $type = $minconfig_type;
3339 if ($type ne "boot" && $type ne "test") {
3340 fail "Invalid MIN_CONFIG_TYPE '$minconfig_type'\n" .
3341 " make_min_config works only with 'boot' and 'test'\n" and return;
3342 }
3343
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003344 if (!defined($output_minconfig)) {
3345 fail "OUTPUT_MIN_CONFIG not defined" and return;
3346 }
Steven Rostedt35ce5952011-07-15 21:57:25 -04003347
3348 # If output_minconfig exists, and the start_minconfig
3349 # came from min_config, than ask if we should use
3350 # that instead.
3351 if (-f $output_minconfig && !$start_minconfig_defined) {
3352 print "$output_minconfig exists\n";
Steven Rostedt43de3312012-05-21 23:35:12 -04003353 if (!defined($use_output_minconfig)) {
3354 if (read_yn " Use it as minconfig?") {
3355 $start_minconfig = $output_minconfig;
3356 }
3357 } elsif ($use_output_minconfig > 0) {
3358 doprint "Using $output_minconfig as MIN_CONFIG\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003359 $start_minconfig = $output_minconfig;
Steven Rostedt43de3312012-05-21 23:35:12 -04003360 } else {
3361 doprint "Set to still use MIN_CONFIG as starting point\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003362 }
3363 }
3364
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003365 if (!defined($start_minconfig)) {
3366 fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
3367 }
3368
Steven Rostedt35ce5952011-07-15 21:57:25 -04003369 my $temp_config = "$tmpdir/temp_config";
3370
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003371 # First things first. We build an allnoconfig to find
3372 # out what the defaults are that we can't touch.
3373 # Some are selections, but we really can't handle selections.
3374
3375 my $save_minconfig = $minconfig;
3376 undef $minconfig;
3377
3378 run_command "$make allnoconfig" or return 0;
3379
Steven Rostedtb9066f62011-07-15 21:25:24 -04003380 read_depends;
3381
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003382 process_config_ignore $output_config;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003383
Steven Rostedt43d1b652011-07-15 22:01:56 -04003384 undef %save_configs;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003385 undef %min_configs;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003386
3387 if (defined($ignore_config)) {
3388 # make sure the file exists
3389 `touch $ignore_config`;
Steven Rostedt43d1b652011-07-15 22:01:56 -04003390 assign_configs \%save_configs, $ignore_config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003391 }
3392
Steven Rostedt43d1b652011-07-15 22:01:56 -04003393 %keep_configs = %save_configs;
3394
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003395 doprint "Load initial configs from $start_minconfig\n";
3396
3397 # Look at the current min configs, and save off all the
3398 # ones that were set via the allnoconfig
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003399 assign_configs \%min_configs, $start_minconfig;
3400
3401 my @config_keys = keys %min_configs;
3402
Steven Rostedtac6974c2011-10-04 09:40:17 -04003403 # All configs need a depcount
3404 foreach my $config (@config_keys) {
3405 my $kconfig = chomp_config $config;
3406 if (!defined $depcount{$kconfig}) {
3407 $depcount{$kconfig} = 0;
3408 }
3409 }
3410
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003411 # Remove anything that was set by the make allnoconfig
3412 # we shouldn't need them as they get set for us anyway.
3413 foreach my $config (@config_keys) {
3414 # Remove anything in the ignore_config
3415 if (defined($keep_configs{$config})) {
3416 my $file = $ignore_config;
3417 $file =~ s,.*/(.*?)$,$1,;
3418 doprint "$config set by $file ... ignored\n";
3419 delete $min_configs{$config};
3420 next;
3421 }
3422 # But make sure the settings are the same. If a min config
3423 # sets a selection, we do not want to get rid of it if
3424 # it is not the same as what we have. Just move it into
3425 # the keep configs.
3426 if (defined($config_ignore{$config})) {
3427 if ($config_ignore{$config} ne $min_configs{$config}) {
3428 doprint "$config is in allnoconfig as '$config_ignore{$config}'";
3429 doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
3430 $keep_configs{$config} = $min_configs{$config};
3431 } else {
3432 doprint "$config set by allnoconfig ... ignored\n";
3433 }
3434 delete $min_configs{$config};
3435 }
3436 }
3437
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003438 my $done = 0;
Steven Rostedtb9066f62011-07-15 21:25:24 -04003439 my $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003440
3441 while (!$done) {
3442
3443 my $config;
3444 my $found;
3445
3446 # Now disable each config one by one and do a make oldconfig
3447 # till we find a config that changes our list.
3448
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003449 my @test_configs = keys %min_configs;
Steven Rostedtac6974c2011-10-04 09:40:17 -04003450
3451 # Sort keys by who is most dependent on
3452 @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
3453 @test_configs ;
3454
3455 # Put configs that did not modify the config at the end.
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003456 my $reset = 1;
3457 for (my $i = 0; $i < $#test_configs; $i++) {
3458 if (!defined($nochange_config{$test_configs[0]})) {
3459 $reset = 0;
3460 last;
3461 }
3462 # This config didn't change the .config last time.
3463 # Place it at the end
3464 my $config = shift @test_configs;
3465 push @test_configs, $config;
3466 }
3467
3468 # if every test config has failed to modify the .config file
3469 # in the past, then reset and start over.
3470 if ($reset) {
3471 undef %nochange_config;
3472 }
3473
Steven Rostedtb9066f62011-07-15 21:25:24 -04003474 undef %processed_configs;
3475
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003476 foreach my $config (@test_configs) {
3477
Steven Rostedtb9066f62011-07-15 21:25:24 -04003478 $found = test_this_config $config;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003479
Steven Rostedtb9066f62011-07-15 21:25:24 -04003480 last if (defined($found));
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003481
3482 # oh well, try another config
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003483 }
3484
3485 if (!defined($found)) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003486 # we could have failed due to the nochange_config hash
3487 # reset and try again
3488 if (!$take_two) {
3489 undef %nochange_config;
3490 $take_two = 1;
3491 next;
3492 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003493 doprint "No more configs found that we can disable\n";
3494 $done = 1;
3495 last;
3496 }
Steven Rostedtb9066f62011-07-15 21:25:24 -04003497 $take_two = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003498
3499 $config = $found;
3500
3501 doprint "Test with $config disabled\n";
3502
3503 # set in_bisect to keep build and monitor from dieing
3504 $in_bisect = 1;
3505
3506 my $failed = 0;
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003507 build "oldconfig" or $failed = 1;
3508 if (!$failed) {
3509 start_monitor_and_boot or $failed = 1;
Steven Rostedtccc513b2012-05-21 17:13:40 -04003510
3511 if ($type eq "test" && !$failed) {
3512 do_run_test or $failed = 1;
3513 }
3514
Steven Rostedtbf1c95a2012-02-27 13:58:49 -05003515 end_monitor;
3516 }
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003517
3518 $in_bisect = 0;
3519
3520 if ($failed) {
Steven Rostedtb9066f62011-07-15 21:25:24 -04003521 doprint "$min_configs{$config} is needed to boot the box... keeping\n";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003522 # this config is needed, add it to the ignore list.
3523 $keep_configs{$config} = $min_configs{$config};
Steven Rostedt43d1b652011-07-15 22:01:56 -04003524 $save_configs{$config} = $min_configs{$config};
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003525 delete $min_configs{$config};
Steven Rostedt35ce5952011-07-15 21:57:25 -04003526
3527 # update new ignore configs
3528 if (defined($ignore_config)) {
3529 open (OUT, ">$temp_config")
3530 or die "Can't write to $temp_config";
Steven Rostedt43d1b652011-07-15 22:01:56 -04003531 foreach my $config (keys %save_configs) {
3532 print OUT "$save_configs{$config}\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003533 }
3534 close OUT;
3535 run_command "mv $temp_config $ignore_config" or
3536 dodie "failed to copy update to $ignore_config";
3537 }
3538
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003539 } else {
3540 # We booted without this config, remove it from the minconfigs.
3541 doprint "$config is not needed, disabling\n";
3542
3543 delete $min_configs{$config};
3544
3545 # Also disable anything that is not enabled in this config
3546 my %configs;
3547 assign_configs \%configs, $output_config;
3548 my @config_keys = keys %min_configs;
3549 foreach my $config (@config_keys) {
3550 if (!defined($configs{$config})) {
3551 doprint "$config is not set, disabling\n";
3552 delete $min_configs{$config};
3553 }
3554 }
3555
3556 # Save off all the current mandidory configs
Steven Rostedt35ce5952011-07-15 21:57:25 -04003557 open (OUT, ">$temp_config")
3558 or die "Can't write to $temp_config";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003559 foreach my $config (keys %keep_configs) {
3560 print OUT "$keep_configs{$config}\n";
3561 }
3562 foreach my $config (keys %min_configs) {
3563 print OUT "$min_configs{$config}\n";
3564 }
3565 close OUT;
Steven Rostedt35ce5952011-07-15 21:57:25 -04003566
3567 run_command "mv $temp_config $output_minconfig" or
3568 dodie "failed to copy update to $output_minconfig";
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003569 }
3570
3571 doprint "Reboot and wait $sleep_time seconds\n";
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003572 reboot_to_good $sleep_time;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003573 }
3574
3575 success $i;
3576 return 1;
3577}
3578
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003579$#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
Steven Rostedt2545eb62010-11-02 15:01:32 -04003580
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003581if ($#ARGV == 0) {
3582 $ktest_config = $ARGV[0];
3583 if (! -f $ktest_config) {
3584 print "$ktest_config does not exist.\n";
Steven Rostedt35ce5952011-07-15 21:57:25 -04003585 if (!read_yn "Create it?") {
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003586 exit 0;
3587 }
3588 }
3589} else {
3590 $ktest_config = "ktest.conf";
3591}
3592
3593if (! -f $ktest_config) {
Steven Rostedtdbd37832011-11-23 16:00:48 -05003594 $newconfig = 1;
Steven Rostedtc4261d02011-11-23 13:41:18 -05003595 get_test_case;
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003596 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
3597 print OUT << "EOF"
3598# Generated by ktest.pl
3599#
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003600
3601# PWD is a ktest.pl variable that will result in the process working
3602# directory that ktest.pl is executed in.
3603
3604# THIS_DIR is automatically assigned the PWD of the path that generated
3605# the config file. It is best to use this variable when assigning other
3606# directory paths within this directory. This allows you to easily
3607# move the test cases to other locations or to other machines.
3608#
3609THIS_DIR := $variable{"PWD"}
3610
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003611# Define each test with TEST_START
3612# The config options below it will override the defaults
3613TEST_START
Steven Rostedtc4261d02011-11-23 13:41:18 -05003614TEST_TYPE = $default{"TEST_TYPE"}
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003615
3616DEFAULTS
3617EOF
3618;
3619 close(OUT);
3620}
3621read_config $ktest_config;
3622
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003623if (defined($opt{"LOG_FILE"})) {
3624 $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
3625}
3626
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003627# Append any configs entered in manually to the config file.
3628my @new_configs = keys %entered_configs;
3629if ($#new_configs >= 0) {
3630 print "\nAppending entered in configs to $ktest_config\n";
3631 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
3632 foreach my $config (@new_configs) {
3633 print OUT "$config = $entered_configs{$config}\n";
Steven Rostedt0e7a22d2011-11-21 20:39:33 -05003634 $opt{$config} = process_variables($entered_configs{$config});
Steven Rostedt8d1491b2010-11-18 15:39:48 -05003635 }
3636}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003637
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003638if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
3639 unlink $opt{"LOG_FILE"};
3640}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003641
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003642doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
3643
Steven Rostedta57419b2010-11-02 15:13:54 -04003644for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
3645
3646 if (!$i) {
3647 doprint "DEFAULT OPTIONS:\n";
3648 } else {
3649 doprint "\nTEST $i OPTIONS";
3650 if (defined($repeat_tests{$i})) {
3651 $repeat = $repeat_tests{$i};
3652 doprint " ITERATE $repeat";
3653 }
3654 doprint "\n";
3655 }
3656
3657 foreach my $option (sort keys %opt) {
3658
3659 if ($option =~ /\[(\d+)\]$/) {
3660 next if ($i != $1);
3661 } else {
3662 next if ($i);
3663 }
3664
3665 doprint "$option = $opt{$option}\n";
3666 }
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003667}
Steven Rostedt2545eb62010-11-02 15:01:32 -04003668
Steven Rostedt2a625122011-05-20 15:48:59 -04003669sub __set_test_option {
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003670 my ($name, $i) = @_;
3671
3672 my $option = "$name\[$i\]";
3673
3674 if (defined($opt{$option})) {
3675 return $opt{$option};
3676 }
3677
Steven Rostedta57419b2010-11-02 15:13:54 -04003678 foreach my $test (keys %repeat_tests) {
3679 if ($i >= $test &&
3680 $i < $test + $repeat_tests{$test}) {
3681 $option = "$name\[$test\]";
3682 if (defined($opt{$option})) {
3683 return $opt{$option};
3684 }
3685 }
3686 }
3687
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003688 if (defined($opt{$name})) {
3689 return $opt{$name};
3690 }
3691
3692 return undef;
3693}
3694
Steven Rostedt2a625122011-05-20 15:48:59 -04003695sub set_test_option {
3696 my ($name, $i) = @_;
3697
3698 my $option = __set_test_option($name, $i);
3699 return $option if (!defined($option));
3700
Steven Rostedt23715c3c2011-06-13 11:03:34 -04003701 return eval_option($option, $i);
Steven Rostedt2a625122011-05-20 15:48:59 -04003702}
3703
Steven Rostedt2545eb62010-11-02 15:01:32 -04003704# First we need to do is the builds
Steven Rostedta75fece2010-11-02 14:58:27 -04003705for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
Steven Rostedt2545eb62010-11-02 15:01:32 -04003706
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003707 # Do not reboot on failing test options
3708 $no_reboot = 1;
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003709 $reboot_success = 0;
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003710
Steven Rostedt683a3e62012-05-18 13:34:35 -04003711 $have_version = 0;
3712
Steven Rostedt576f6272010-11-02 14:58:38 -04003713 $iteration = $i;
3714
Steven Rostedtc1434dc2012-07-20 22:39:16 -04003715 undef %force_config;
3716
Steven Rostedta75fece2010-11-02 14:58:27 -04003717 my $makecmd = set_test_option("MAKE_CMD", $i);
3718
Steven Rostedt9cc9e092011-12-22 21:37:22 -05003719 # Load all the options into their mapped variable names
3720 foreach my $opt (keys %option_map) {
3721 ${$option_map{$opt}} = set_test_option($opt, $i);
3722 }
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003723
Steven Rostedt35ce5952011-07-15 21:57:25 -04003724 $start_minconfig_defined = 1;
3725
Steven Rostedt921ed4c2012-07-19 15:18:27 -04003726 # The first test may override the PRE_KTEST option
3727 if (defined($pre_ktest) && $i == 1) {
3728 doprint "\n";
3729 run_command $pre_ktest;
3730 }
3731
3732 # Any test can override the POST_KTEST option
3733 # The last test takes precedence.
3734 if (defined($post_ktest)) {
3735 $final_post_ktest = $post_ktest;
3736 }
3737
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003738 if (!defined($start_minconfig)) {
Steven Rostedt35ce5952011-07-15 21:57:25 -04003739 $start_minconfig_defined = 0;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003740 $start_minconfig = $minconfig;
3741 }
3742
Steven Rostedta75fece2010-11-02 14:58:27 -04003743 chdir $builddir || die "can't change directory to $builddir";
3744
Andrew Jonesa908a662011-08-12 15:32:03 +02003745 foreach my $dir ($tmpdir, $outputdir) {
3746 if (!-d $dir) {
3747 mkpath($dir) or
3748 die "can't create $dir";
3749 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003750 }
3751
Steven Rostedte48c5292010-11-02 14:35:37 -04003752 $ENV{"SSH_USER"} = $ssh_user;
3753 $ENV{"MACHINE"} = $machine;
3754
Steven Rostedta75fece2010-11-02 14:58:27 -04003755 $buildlog = "$tmpdir/buildlog-$machine";
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303756 $testlog = "$tmpdir/testlog-$machine";
Steven Rostedta75fece2010-11-02 14:58:27 -04003757 $dmesg = "$tmpdir/dmesg-$machine";
3758 $make = "$makecmd O=$outputdir";
Steven Rostedt51ad1dd2010-11-08 16:43:21 -05003759 $output_config = "$outputdir/.config";
Steven Rostedta75fece2010-11-02 14:58:27 -04003760
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003761 if (!$buildonly) {
3762 $target = "$ssh_user\@$machine";
3763 if ($reboot_type eq "grub") {
3764 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
Steven Rostedta15ba912012-11-13 14:30:37 -05003765 } elsif ($reboot_type eq "grub2") {
3766 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
3767 dodie "GRUB_FILE not defined" if (!defined($grub_file));
Steven Rostedtbb8474b2011-11-23 15:58:00 -05003768 }
Steven Rostedta75fece2010-11-02 14:58:27 -04003769 }
3770
3771 my $run_type = $build_type;
3772 if ($test_type eq "patchcheck") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003773 $run_type = $patchcheck_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003774 } elsif ($test_type eq "bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003775 $run_type = $bisect_type;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003776 } elsif ($test_type eq "config_bisect") {
Steven Rostedtb5f4aea2011-12-22 21:33:55 -05003777 $run_type = $config_bisect_type;
Steven Rostedta75fece2010-11-02 14:58:27 -04003778 }
3779
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003780 if ($test_type eq "make_min_config") {
3781 $run_type = "";
3782 }
3783
Steven Rostedta75fece2010-11-02 14:58:27 -04003784 # mistake in config file?
3785 if (!defined($run_type)) {
3786 $run_type = "ERROR";
3787 }
Steven Rostedt2545eb62010-11-02 15:01:32 -04003788
Steven Rostedte0a87422011-09-30 17:50:48 -04003789 my $installme = "";
3790 $installme = " no_install" if ($no_install);
3791
Steven Rostedt2545eb62010-11-02 15:01:32 -04003792 doprint "\n\n";
Steven Rostedte0a87422011-09-30 17:50:48 -04003793 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003794
Steven Rostedt921ed4c2012-07-19 15:18:27 -04003795 if (defined($pre_test)) {
3796 run_command $pre_test;
3797 }
3798
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003799 unlink $dmesg;
3800 unlink $buildlog;
Rabin Vincenta9dd5d62011-11-18 17:05:29 +05303801 unlink $testlog;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003802
Steven Rostedt250bae82011-07-15 22:05:59 -04003803 if (defined($addconfig)) {
3804 my $min = $minconfig;
3805 if (!defined($minconfig)) {
3806 $min = "";
3807 }
3808 run_command "cat $addconfig $min > $tmpdir/add_config" or
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003809 dodie "Failed to create temp config";
Steven Rostedt9be2e6b2010-11-09 12:20:21 -05003810 $minconfig = "$tmpdir/add_config";
Steven Rostedt2b7d9b22010-11-02 14:58:15 -04003811 }
3812
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003813 if (defined($checkout)) {
3814 run_command "git checkout $checkout" or
3815 die "failed to checkout $checkout";
3816 }
3817
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003818 $no_reboot = 0;
3819
Steven Rostedt648a1822012-03-21 11:18:27 -04003820 # A test may opt to not reboot the box
3821 if ($reboot_on_success) {
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003822 $reboot_success = 1;
Steven Rostedt648a1822012-03-21 11:18:27 -04003823 }
Steven Rostedt4ab1cce2011-09-30 18:12:20 -04003824
Steven Rostedta75fece2010-11-02 14:58:27 -04003825 if ($test_type eq "bisect") {
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003826 bisect $i;
3827 next;
Steven Rostedt0a05c762010-11-08 11:14:10 -05003828 } elsif ($test_type eq "config_bisect") {
3829 config_bisect $i;
3830 next;
Steven Rostedta75fece2010-11-02 14:58:27 -04003831 } elsif ($test_type eq "patchcheck") {
Steven Rostedt6c5ee0b2010-11-02 14:57:58 -04003832 patchcheck $i;
3833 next;
Steven Rostedt4c4ab122011-07-15 21:16:17 -04003834 } elsif ($test_type eq "make_min_config") {
3835 make_min_config $i;
3836 next;
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003837 }
3838
Steven Rostedt7faafbd2010-11-02 14:58:22 -04003839 if ($build_type ne "nobuild") {
3840 build $build_type or next;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003841 }
3842
Steven Rostedtcd8e3682011-08-18 16:35:44 -04003843 if ($test_type eq "install") {
3844 get_version;
3845 install;
3846 success $i;
3847 next;
3848 }
3849
Steven Rostedta75fece2010-11-02 14:58:27 -04003850 if ($test_type ne "build") {
Steven Rostedta75fece2010-11-02 14:58:27 -04003851 my $failed = 0;
Steven Rostedtddf607e2011-06-14 20:49:13 -04003852 start_monitor_and_boot or $failed = 1;
Steven Rostedta75fece2010-11-02 14:58:27 -04003853
3854 if (!$failed && $test_type ne "boot" && defined($run_test)) {
3855 do_run_test or $failed = 1;
3856 }
3857 end_monitor;
3858 next if ($failed);
Steven Rostedt5a391fb2010-11-02 14:57:43 -04003859 }
3860
Steven Rostedt5f9b6ce2010-11-02 14:57:33 -04003861 success $i;
Steven Rostedt2545eb62010-11-02 15:01:32 -04003862}
3863
Steven Rostedt921ed4c2012-07-19 15:18:27 -04003864if (defined($final_post_ktest)) {
3865 run_command $final_post_ktest;
3866}
3867
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003868if ($opt{"POWEROFF_ON_SUCCESS"}) {
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003869 halt;
Steven Rostedt759a3cc2012-05-01 08:20:12 -04003870} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot && $reboot_success) {
Steven Rostedtbc7c5802011-12-22 16:29:10 -05003871 reboot_to_good;
Steven Rostedt648a1822012-03-21 11:18:27 -04003872} elsif (defined($switch_to_good)) {
3873 # still need to get to the good kernel
3874 run_command $switch_to_good;
Steven Rostedt5c42fc52010-11-02 14:57:01 -04003875}
Steven Rostedt75c3fda72010-11-02 14:57:21 -04003876
Steven Rostedt648a1822012-03-21 11:18:27 -04003877
Steven Rostedte48c5292010-11-02 14:35:37 -04003878doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
3879
Steven Rostedt2545eb62010-11-02 15:01:32 -04003880exit 0;