3 # Copyright 2015 - Steven Rostedt, Red Hat Inc.
4 # Copyright 2017 - Steven Rostedt, VMware, Inc.
6 # Licensed under the terms of the GNU GPL License version 2
10 # config-bisect.pl [options] good-config bad-config [good|bad]
13 # Compares a good config to a bad config, then takes half of the diffs
14 # and produces a config that is somewhere between the good config and
15 # the bad config. That is, the resulting config will start with the
16 # good config and will try to make half of the differences of between
17 # the good and bad configs match the bad config. It tries because of
18 # dependencies between the two configs it may not be able to change
19 # exactly half of the configs that are different between the two config
22 # Here's a normal way to use it:
24 # $ cd /path/to/linux/kernel
25 # $ config-bisect.pl /path/to/good/config /path/to/bad/config
27 # This will now pull in good config (blowing away .config in that directory
28 # so do not make that be one of the good or bad configs), and then
29 # build the config with "make oldconfig" to make sure it matches the
30 # current kernel. It will then store the configs in that result for
31 # the good config. It does the same for the bad config as well.
32 # The algorithm will run, merging half of the differences between
33 # the two configs and building them with "make oldconfig" to make sure
34 # the result changes (dependencies may reset changes the tool had made).
35 # It then copies the result of its good config to /path/to/good/config.tmp
36 # and the bad config to /path/to/bad/config.tmp (just appends ".tmp" to the
37 # files passed in). And the ".config" that you should test will be in
40 # After the first run, determine if the result is good or bad then
41 # run the same command appending the result
44 # $ config-bisect.pl /path/to/good/config /path/to/bad/config good
47 # $ config-bisect.pl /path/to/good/config /path/to/bad/config bad
49 # Do not change the good-config or bad-config, config-bisect.pl will
50 # copy the good-config to a temp file with the same name as good-config
51 # but with a ".tmp" after it. It will do the same with the bad-config.
53 # If "good" or "bad" is not stated at the end, it will copy the good and
54 # bad configs to the .tmp versions. If a .tmp version already exists, it will
55 # warn before writing over them (-r will not warn, and just write over them).
56 # If the last config is labeled "good", then it will copy it to the good .tmp
57 # version. If the last config is labeled "bad", it will copy it to the bad
58 # .tmp version. It will continue this until it can not merge the two any more
59 # without the result being equal to either the good or bad .tmp configs.
75 usage
: config
-bisect
.pl
[-l linux
-tree
][-b build
-dir
] good
-config bad
-config
[good
|bad
]
76 -l
[optional
] define location of linux
-tree
(default is current directory
)
77 -b
[optional
] define location to build
(O
=build
-dir
) (default is linux
-tree
)
78 good
-config the config that is considered good
79 bad
-config the config that does
not work
80 "good" add this
if the
last run produced a good config
81 "bad" add this
if the
last run produced a bad config
82 If
"good" or "bad" is
not specified
, then it is the start of a new bisect
84 Note
, each run will create copy of good
and bad configs with
".tmp" appended
.
97 doprint
"CRITICAL FAILURE... ", @_, "\n";
105 if ($file =~ m
,^/,) {
112 my ($cancel, $prompt) = @_;
118 print "$prompt [y/n/C] ";
120 print "$prompt [y/N] ";
124 if ($ans =~ /^\s*$/) {
131 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
133 last if ($ans =~ /^c$/i);
134 print "Please answer either 'y', 'n' or 'c'.\n";
136 print "Please answer either 'y' or 'n'.\n";
142 if ($ans !~ /^y$/i) {
151 return read_prompt
0, $prompt;
157 return read_prompt
1, $prompt;
161 my ($command, $redirect) = @_;
169 doprint
("$command ... ");
171 $pid = open(CMD
, "$command 2>&1 |") or
172 dodie
"unable to exec $command";
174 if (defined($redirect)) {
175 open (RD
, ">$redirect") or
176 dodie
"failed to write to redirect $redirect";
188 close(RD
) if ($dord);
191 my $delta = $end_time - $start_time;
194 doprint
"[1 second] ";
196 doprint
"[$delta seconds] ";
208 ###### CONFIG BISECT ######
210 # config_ignore holds the configs that were set (or unset) for
211 # a good config and we will ignore these configs for the rest
212 # of a config bisect. These configs stay as they were.
215 # config_set holds what all configs were set as.
218 # config_off holds the set of configs that the bad config had disabled.
219 # We need to record them and set them in the .config when running
220 # olddefconfig, because olddefconfig keeps the defaults.
223 # config_off_tmp holds a set of configs to turn off for now
226 # config_list is the set of configs that are being tested
236 if (!run_command
"$make olddefconfig") {
237 # Perhaps olddefconfig doesn't exist in this version of the kernel
239 doprint
"olddefconfig failed, trying make oldnoconfig\n";
240 if (!run_command
"$make oldnoconfig") {
241 doprint
"oldnoconfig failed, trying yes '' | make oldconfig\n";
242 # try a yes '' | oldconfig
243 run_command
"yes '' | $make oldconfig" or
244 dodie
"failed make config oldconfig";
250 my ($hash, $config) = @_;
252 doprint
"Reading configs from $config\n";
255 or dodie
"Failed to read $config";
259 if (/^((CONFIG\S*)=.*)/) {
261 } elsif (/^(# (CONFIG\S*) is not set)/) {
269 sub process_config_ignore
{
272 assign_configs \
%config_ignore, $config;
275 sub get_dependencies
{
278 my $arr = $dependency{$config};
279 if (!defined($arr)) {
285 foreach my $dep (@
{$arr}) {
286 print "ADD DEP $dep\n";
287 @deps = (@deps, get_dependencies
$dep);
294 my ($pc, $file) = @_;
296 my %configs = %{$pc};
298 doprint
"Saving configs into $file\n";
300 open(OUT
, ">$file") or dodie
"Can not write to $file";
302 foreach my $config (keys %configs) {
303 print OUT
"$configs{$config}\n";
309 my ($name, $pc) = @_;
311 doprint
"Creating old config from $name configs\n";
313 save_config
$pc, $output_config;
318 # compare two config hashes, and return configs with different vals.
319 # It returns B's config values, but you can use A to see what A was.
320 sub diff_config_vals
{
323 # crappy Perl way to pass in hashes.
329 foreach my $item (keys %a) {
330 if (defined($b{$item}) && $b{$item} ne $a{$item}) {
331 $ret{$item} = $b{$item};
338 # compare two config hashes and return the configs in B but not A
344 # crappy Perl way to pass in hashes.
348 foreach my $item (keys %b) {
349 if (!defined($a{$item})) {
350 $ret{$item} = $b{$item};
357 # return if two configs are equal or not
358 # 0 is equal +1 b has something a does not
359 # +1 if a and b have a different item.
360 # -1 if a has something b does not
361 sub compare_configs
{
366 # crappy Perl way to pass in hashes.
370 foreach my $item (keys %b) {
371 if (!defined($a{$item})) {
374 if ($a{$item} ne $b{$item}) {
379 foreach my $item (keys %a) {
380 if (!defined($b{$item})) {
391 doprint
"\n\n***************************************\n";
392 doprint
"Found bad config: $config\n";
393 doprint
"***************************************\n\n";
396 sub process_new_config
{
397 my ($tc, $nc, $gc, $bc) = @_;
399 my %tmp_config = %{$tc};
400 my %good_configs = %{$gc};
401 my %bad_configs = %{$bc};
408 create_config
"tmp_configs", \
%tmp_config;
409 assign_configs \
%new_configs, $output_config;
411 $ret = compare_configs \
%new_configs, \
%bad_configs;
413 doprint
"New config equals bad config, try next test\n";
418 $ret = compare_configs \
%new_configs, \
%good_configs;
420 doprint
"New config equals good config, try next test\n";
425 %{$nc} = %new_configs;
433 if ($config =~ /^# (.*) is not set/) {
437 $config =~ s/^CONFIG_//;
442 my ($sym, $config) = @_;
444 $config = convert_config
$config;
445 doprint
"$sym$config\n";
448 sub print_config_compare
{
449 my ($good_config, $bad_config) = @_;
451 $good_config = convert_config
$good_config;
452 $bad_config = convert_config
$bad_config;
454 my $good_value = $good_config;
455 my $bad_value = $bad_config;
456 $good_value =~ s/(.*)=//;
459 $bad_value =~ s/.*=//;
461 doprint
" $config $good_value -> $bad_value\n";
465 # $phalf: half of the configs names you want to add
466 # $oconfigs: The orginial configs to start with
467 # $sconfigs: The source to update $oconfigs with (from $phalf)
468 # $which: The name of which half that is updating (top / bottom)
469 # $type: The name of the source type (good / bad)
471 my ($phalf, $oconfigs, $sconfigs, $which, $type) = @_;
473 my @half = @
{$phalf};
474 my %orig_configs = %{$oconfigs};
475 my %source_configs = %{$sconfigs};
477 my %tmp_config = %orig_configs;
479 doprint
"Settings bisect with $which half of $type configs:\n";
480 foreach my $item (@half) {
481 doprint
"Updating $item to $source_configs{$item}\n";
482 $tmp_config{$item} = $source_configs{$item};
488 sub run_config_bisect
{
489 my ($pgood, $pbad) = @_;
491 my %good_configs = %{$pgood};
492 my %bad_configs = %{$pbad};
494 my %diff_configs = diff_config_vals \
%good_configs, \
%bad_configs;
495 my %b_configs = diff_configs \
%good_configs, \
%bad_configs;
496 my %g_configs = diff_configs \
%bad_configs, \
%good_configs;
498 # diff_arr is what is in both good and bad but are different (y->n)
499 my @diff_arr = keys %diff_configs;
500 my $len_diff = $#diff_arr + 1;
502 # b_arr is what is in bad but not in good (has depends)
503 my @b_arr = keys %b_configs;
504 my $len_b = $#b_arr + 1;
506 # g_arr is what is in good but not in bad
507 my @g_arr = keys %g_configs;
508 my $len_g = $#g_arr + 1;
514 # Look at the configs that are different between good and bad.
515 # This does not include those that depend on other configs
516 # (configs depending on other configs that are not set would
517 # not show up even as a "# CONFIG_FOO is not set"
520 doprint
"# of configs to check: $len_diff\n";
521 doprint
"# of configs showing only in good: $len_g\n";
522 doprint
"# of configs showing only in bad: $len_b\n";
525 # Now test for different values
527 doprint
"Configs left to check:\n";
528 doprint
" Good Config\t\t\tBad Config\n";
529 doprint
" -----------\t\t\t----------\n";
530 foreach my $item (@diff_arr) {
531 doprint
" $good_configs{$item}\t$bad_configs{$item}\n";
534 my $half = int($#diff_arr / 2);
535 my @tophalf = @diff_arr[0 .. $half];
537 doprint
"Set tmp config to be good config with some bad config values\n";
539 my %tmp_config = make_half \
@tophalf, \
%good_configs,
540 \
%bad_configs, "top", "bad";
542 $runtest = process_new_config \
%tmp_config, \
%new_configs,
543 \
%good_configs, \
%bad_configs;
546 doprint
"Set tmp config to be bad config with some good config values\n";
548 my %tmp_config = make_half \
@tophalf, \
%bad_configs,
549 \
%good_configs, "top", "good";
551 $runtest = process_new_config \
%tmp_config, \
%new_configs,
552 \
%good_configs, \
%bad_configs;
556 if (!$runtest && $len_diff > 0) {
557 # do the same thing, but this time with bottom half
559 my $half = int($#diff_arr / 2);
560 my @bottomhalf = @diff_arr[$half+1 .. $#diff_arr];
562 doprint
"Set tmp config to be good config with some bad config values\n";
564 my %tmp_config = make_half \
@bottomhalf, \
%good_configs,
565 \
%bad_configs, "bottom", "bad";
567 $runtest = process_new_config \
%tmp_config, \
%new_configs,
568 \
%good_configs, \
%bad_configs;
571 doprint
"Set tmp config to be bad config with some good config values\n";
573 my %tmp_config = make_half \
@bottomhalf, \
%bad_configs,
574 \
%good_configs, "bottom", "good";
576 $runtest = process_new_config \
%tmp_config, \
%new_configs,
577 \
%good_configs, \
%bad_configs;
583 doprint
"READY TO TEST .config IN $build\n";
587 doprint
"\n%%%%%%%% FAILED TO FIND SINGLE BAD CONFIG %%%%%%%%\n";
588 doprint
"Hmm, can't make any more changes without making good == bad?\n";
589 doprint
"Difference between good (+) and bad (-)\n";
591 foreach my $item (keys %bad_configs) {
592 if (!defined($good_configs{$item})) {
593 print_config
"-", $bad_configs{$item};
597 foreach my $item (keys %good_configs) {
598 next if (!defined($bad_configs{$item}));
599 if ($good_configs{$item} ne $bad_configs{$item}) {
600 print_config_compare
$good_configs{$item}, $bad_configs{$item};
604 foreach my $item (keys %good_configs) {
605 if (!defined($bad_configs{$item})) {
606 print_config
"+", $good_configs{$item};
613 my ($good_config, $bad_config) = @_;
620 doprint
"Run good configs through make oldconfig\n";
621 assign_configs \
%tmp_configs, $good_config;
622 create_config
"$good_config", \
%tmp_configs;
623 assign_configs \
%good_configs, $output_config;
625 doprint
"Run bad configs through make oldconfig\n";
626 assign_configs \
%tmp_configs, $bad_config;
627 create_config
"$bad_config", \
%tmp_configs;
628 assign_configs \
%bad_configs, $output_config;
630 save_config \
%good_configs, $good_config;
631 save_config \
%bad_configs, $bad_config;
633 return run_config_bisect \
%good_configs, \
%bad_configs;
636 while ($#ARGV >= 0) {
637 if ($ARGV[0] !~ m/^-/) {
640 my $opt = shift @ARGV;
644 if (!defined($val)) {
645 die "-b requires value\n";
650 elsif ($opt eq "-l") {
652 if (!defined($val)) {
653 die "-l requires value\n";
658 elsif ($opt eq "-r") {
662 elsif ($opt eq "-h") {
667 die "Unknow option $opt\n";
671 $build = $tree if (!defined($build));
673 $tree = expand_path
$tree;
674 $build = expand_path
$build;
677 die "$tree not a directory\n";
681 die "$build not a directory\n";
688 } elsif ($#ARGV == 2) {
690 if ($val ne "good" && $val ne "bad") {
691 die "Unknown command '$val', bust be either \"good\" or \"bad\"\n";
697 my $good_start = expand_path
$ARGV[0];
698 my $bad_start = expand_path
$ARGV[1];
700 my $good = "$good_start.tmp";
701 my $bad = "$bad_start.tmp";
705 if ($build ne $tree) {
706 $make = "make O=$build"
709 $output_config = "$build/.config";
712 if ( ! -f
$good_start ) {
713 die "$good_start not found\n";
715 if ( ! -f
$bad_start ) {
716 die "$bad_start not found\n";
718 if ( -f
$good || -f
$bad ) {
722 $p = "$good exists\n";
726 $p = "$p$bad exists\n";
729 if (!defined($reset_bisect)) {
730 if (!read_yn
"${p}Overwrite and start new bisect anyway?") {
735 run_command
"cp $good_start $good" or die "failed to copy to $good\n";
736 run_command
"cp $bad_start $bad" or die "faield to copy to $bad\n";
739 die "Can not find file $good\n";
742 die "Can not find file $bad\n";
744 if ($val eq "good") {
745 run_command
"cp $output_config $good" or die "failed to copy $config to $good\n";
746 } elsif ($val eq "bad") {
747 run_command
"cp $output_config $bad" or die "failed to copy $config to $bad\n";
751 chdir $tree || die "can't change directory to $tree";
753 my $ret = config_bisect
$good, $bad;
760 doprint
"Cleaning temp files\n";
761 run_command
"rm $good";
762 run_command
"rm $bad";
765 doprint
"See good and bad configs for details:\n";
766 doprint
"good: $good\n";
767 doprint
"bad: $bad\n";
768 doprint
"%%%%%%%% FAILED TO FIND SINGLE BAD CONFIG %%%%%%%%\n";