config-bisect.pl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. #!/usr/bin/perl -w
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. # Copyright 2015 - Steven Rostedt, Red Hat Inc.
  5. # Copyright 2017 - Steven Rostedt, VMware, Inc.
  6. #
  7. # usage:
  8. # config-bisect.pl [options] good-config bad-config [good|bad]
  9. #
  10. # Compares a good config to a bad config, then takes half of the diffs
  11. # and produces a config that is somewhere between the good config and
  12. # the bad config. That is, the resulting config will start with the
  13. # good config and will try to make half of the differences of between
  14. # the good and bad configs match the bad config. It tries because of
  15. # dependencies between the two configs it may not be able to change
  16. # exactly half of the configs that are different between the two config
  17. # files.
  18. # Here's a normal way to use it:
  19. #
  20. # $ cd /path/to/linux/kernel
  21. # $ config-bisect.pl /path/to/good/config /path/to/bad/config
  22. # This will now pull in good config (blowing away .config in that directory
  23. # so do not make that be one of the good or bad configs), and then
  24. # build the config with "make oldconfig" to make sure it matches the
  25. # current kernel. It will then store the configs in that result for
  26. # the good config. It does the same for the bad config as well.
  27. # The algorithm will run, merging half of the differences between
  28. # the two configs and building them with "make oldconfig" to make sure
  29. # the result changes (dependencies may reset changes the tool had made).
  30. # It then copies the result of its good config to /path/to/good/config.tmp
  31. # and the bad config to /path/to/bad/config.tmp (just appends ".tmp" to the
  32. # files passed in). And the ".config" that you should test will be in
  33. # directory
  34. # After the first run, determine if the result is good or bad then
  35. # run the same command appending the result
  36. # For good results:
  37. # $ config-bisect.pl /path/to/good/config /path/to/bad/config good
  38. # For bad results:
  39. # $ config-bisect.pl /path/to/good/config /path/to/bad/config bad
  40. # Do not change the good-config or bad-config, config-bisect.pl will
  41. # copy the good-config to a temp file with the same name as good-config
  42. # but with a ".tmp" after it. It will do the same with the bad-config.
  43. # If "good" or "bad" is not stated at the end, it will copy the good and
  44. # bad configs to the .tmp versions. If a .tmp version already exists, it will
  45. # warn before writing over them (-r will not warn, and just write over them).
  46. # If the last config is labeled "good", then it will copy it to the good .tmp
  47. # version. If the last config is labeled "bad", it will copy it to the bad
  48. # .tmp version. It will continue this until it can not merge the two any more
  49. # without the result being equal to either the good or bad .tmp configs.
  50. my $start = 0;
  51. my $val = "";
  52. my $pwd = `pwd`;
  53. chomp $pwd;
  54. my $tree = $pwd;
  55. my $build;
  56. my $output_config;
  57. my $reset_bisect;
  58. sub usage {
  59. print << "EOF"
  60. usage: config-bisect.pl [-l linux-tree][-b build-dir] good-config bad-config [good|bad]
  61. -l [optional] define location of linux-tree (default is current directory)
  62. -b [optional] define location to build (O=build-dir) (default is linux-tree)
  63. good-config the config that is considered good
  64. bad-config the config that does not work
  65. "good" add this if the last run produced a good config
  66. "bad" add this if the last run produced a bad config
  67. If "good" or "bad" is not specified, then it is the start of a new bisect
  68. Note, each run will create copy of good and bad configs with ".tmp" appended.
  69. EOF
  70. ;
  71. exit(-1);
  72. }
  73. sub doprint {
  74. print @_;
  75. }
  76. sub dodie {
  77. doprint "CRITICAL FAILURE... ", @_, "\n";
  78. die @_, "\n";
  79. }
  80. sub expand_path {
  81. my ($file) = @_;
  82. if ($file =~ m,^/,) {
  83. return $file;
  84. }
  85. return "$pwd/$file";
  86. }
  87. sub read_prompt {
  88. my ($cancel, $prompt) = @_;
  89. my $ans;
  90. for (;;) {
  91. if ($cancel) {
  92. print "$prompt [y/n/C] ";
  93. } else {
  94. print "$prompt [y/N] ";
  95. }
  96. $ans = <STDIN>;
  97. chomp $ans;
  98. if ($ans =~ /^\s*$/) {
  99. if ($cancel) {
  100. $ans = "c";
  101. } else {
  102. $ans = "n";
  103. }
  104. }
  105. last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
  106. if ($cancel) {
  107. last if ($ans =~ /^c$/i);
  108. print "Please answer either 'y', 'n' or 'c'.\n";
  109. } else {
  110. print "Please answer either 'y' or 'n'.\n";
  111. }
  112. }
  113. if ($ans =~ /^c/i) {
  114. exit;
  115. }
  116. if ($ans !~ /^y$/i) {
  117. return 0;
  118. }
  119. return 1;
  120. }
  121. sub read_yn {
  122. my ($prompt) = @_;
  123. return read_prompt 0, $prompt;
  124. }
  125. sub read_ync {
  126. my ($prompt) = @_;
  127. return read_prompt 1, $prompt;
  128. }
  129. sub run_command {
  130. my ($command, $redirect) = @_;
  131. my $start_time;
  132. my $end_time;
  133. my $dord = 0;
  134. my $pid;
  135. $start_time = time;
  136. doprint("$command ... ");
  137. $pid = open(CMD, "$command 2>&1 |") or
  138. dodie "unable to exec $command";
  139. if (defined($redirect)) {
  140. open (RD, ">$redirect") or
  141. dodie "failed to write to redirect $redirect";
  142. $dord = 1;
  143. }
  144. while (<CMD>) {
  145. print RD if ($dord);
  146. }
  147. waitpid($pid, 0);
  148. my $failed = $?;
  149. close(CMD);
  150. close(RD) if ($dord);
  151. $end_time = time;
  152. my $delta = $end_time - $start_time;
  153. if ($delta == 1) {
  154. doprint "[1 second] ";
  155. } else {
  156. doprint "[$delta seconds] ";
  157. }
  158. if ($failed) {
  159. doprint "FAILED!\n";
  160. } else {
  161. doprint "SUCCESS\n";
  162. }
  163. return !$failed;
  164. }
  165. ###### CONFIG BISECT ######
  166. # config_ignore holds the configs that were set (or unset) for
  167. # a good config and we will ignore these configs for the rest
  168. # of a config bisect. These configs stay as they were.
  169. my %config_ignore;
  170. # config_set holds what all configs were set as.
  171. my %config_set;
  172. # config_off holds the set of configs that the bad config had disabled.
  173. # We need to record them and set them in the .config when running
  174. # olddefconfig, because olddefconfig keeps the defaults.
  175. my %config_off;
  176. # config_off_tmp holds a set of configs to turn off for now
  177. my @config_off_tmp;
  178. # config_list is the set of configs that are being tested
  179. my %config_list;
  180. my %null_config;
  181. my %dependency;
  182. my $make;
  183. sub make_oldconfig {
  184. if (!run_command "$make olddefconfig") {
  185. # Perhaps olddefconfig doesn't exist in this version of the kernel
  186. # try oldnoconfig
  187. doprint "olddefconfig failed, trying make oldnoconfig\n";
  188. if (!run_command "$make oldnoconfig") {
  189. doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
  190. # try a yes '' | oldconfig
  191. run_command "yes '' | $make oldconfig" or
  192. dodie "failed make config oldconfig";
  193. }
  194. }
  195. }
  196. sub assign_configs {
  197. my ($hash, $config) = @_;
  198. doprint "Reading configs from $config\n";
  199. open (IN, $config)
  200. or dodie "Failed to read $config";
  201. while (<IN>) {
  202. chomp;
  203. if (/^((CONFIG\S*)=.*)/) {
  204. ${$hash}{$2} = $1;
  205. } elsif (/^(# (CONFIG\S*) is not set)/) {
  206. ${$hash}{$2} = $1;
  207. }
  208. }
  209. close(IN);
  210. }
  211. sub process_config_ignore {
  212. my ($config) = @_;
  213. assign_configs \%config_ignore, $config;
  214. }
  215. sub get_dependencies {
  216. my ($config) = @_;
  217. my $arr = $dependency{$config};
  218. if (!defined($arr)) {
  219. return ();
  220. }
  221. my @deps = @{$arr};
  222. foreach my $dep (@{$arr}) {
  223. print "ADD DEP $dep\n";
  224. @deps = (@deps, get_dependencies $dep);
  225. }
  226. return @deps;
  227. }
  228. sub save_config {
  229. my ($pc, $file) = @_;
  230. my %configs = %{$pc};
  231. doprint "Saving configs into $file\n";
  232. open(OUT, ">$file") or dodie "Can not write to $file";
  233. foreach my $config (keys %configs) {
  234. print OUT "$configs{$config}\n";
  235. }
  236. close(OUT);
  237. }
  238. sub create_config {
  239. my ($name, $pc) = @_;
  240. doprint "Creating old config from $name configs\n";
  241. save_config $pc, $output_config;
  242. make_oldconfig;
  243. }
  244. # compare two config hashes, and return configs with different vals.
  245. # It returns B's config values, but you can use A to see what A was.
  246. sub diff_config_vals {
  247. my ($pa, $pb) = @_;
  248. # crappy Perl way to pass in hashes.
  249. my %a = %{$pa};
  250. my %b = %{$pb};
  251. my %ret;
  252. foreach my $item (keys %a) {
  253. if (defined($b{$item}) && $b{$item} ne $a{$item}) {
  254. $ret{$item} = $b{$item};
  255. }
  256. }
  257. return %ret;
  258. }
  259. # compare two config hashes and return the configs in B but not A
  260. sub diff_configs {
  261. my ($pa, $pb) = @_;
  262. my %ret;
  263. # crappy Perl way to pass in hashes.
  264. my %a = %{$pa};
  265. my %b = %{$pb};
  266. foreach my $item (keys %b) {
  267. if (!defined($a{$item})) {
  268. $ret{$item} = $b{$item};
  269. }
  270. }
  271. return %ret;
  272. }
  273. # return if two configs are equal or not
  274. # 0 is equal +1 b has something a does not
  275. # +1 if a and b have a different item.
  276. # -1 if a has something b does not
  277. sub compare_configs {
  278. my ($pa, $pb) = @_;
  279. my %ret;
  280. # crappy Perl way to pass in hashes.
  281. my %a = %{$pa};
  282. my %b = %{$pb};
  283. foreach my $item (keys %b) {
  284. if (!defined($a{$item})) {
  285. return 1;
  286. }
  287. if ($a{$item} ne $b{$item}) {
  288. return 1;
  289. }
  290. }
  291. foreach my $item (keys %a) {
  292. if (!defined($b{$item})) {
  293. return -1;
  294. }
  295. }
  296. return 0;
  297. }
  298. sub process_failed {
  299. my ($config) = @_;
  300. doprint "\n\n***************************************\n";
  301. doprint "Found bad config: $config\n";
  302. doprint "***************************************\n\n";
  303. }
  304. sub process_new_config {
  305. my ($tc, $nc, $gc, $bc) = @_;
  306. my %tmp_config = %{$tc};
  307. my %good_configs = %{$gc};
  308. my %bad_configs = %{$bc};
  309. my %new_configs;
  310. my $runtest = 1;
  311. my $ret;
  312. create_config "tmp_configs", \%tmp_config;
  313. assign_configs \%new_configs, $output_config;
  314. $ret = compare_configs \%new_configs, \%bad_configs;
  315. if (!$ret) {
  316. doprint "New config equals bad config, try next test\n";
  317. $runtest = 0;
  318. }
  319. if ($runtest) {
  320. $ret = compare_configs \%new_configs, \%good_configs;
  321. if (!$ret) {
  322. doprint "New config equals good config, try next test\n";
  323. $runtest = 0;
  324. }
  325. }
  326. %{$nc} = %new_configs;
  327. return $runtest;
  328. }
  329. sub convert_config {
  330. my ($config) = @_;
  331. if ($config =~ /^# (.*) is not set/) {
  332. $config = "$1=n";
  333. }
  334. $config =~ s/^CONFIG_//;
  335. return $config;
  336. }
  337. sub print_config {
  338. my ($sym, $config) = @_;
  339. $config = convert_config $config;
  340. doprint "$sym$config\n";
  341. }
  342. sub print_config_compare {
  343. my ($good_config, $bad_config) = @_;
  344. $good_config = convert_config $good_config;
  345. $bad_config = convert_config $bad_config;
  346. my $good_value = $good_config;
  347. my $bad_value = $bad_config;
  348. $good_value =~ s/(.*)=//;
  349. my $config = $1;
  350. $bad_value =~ s/.*=//;
  351. doprint " $config $good_value -> $bad_value\n";
  352. }
  353. # Pass in:
  354. # $phalf: half of the configs names you want to add
  355. # $oconfigs: The orginial configs to start with
  356. # $sconfigs: The source to update $oconfigs with (from $phalf)
  357. # $which: The name of which half that is updating (top / bottom)
  358. # $type: The name of the source type (good / bad)
  359. sub make_half {
  360. my ($phalf, $oconfigs, $sconfigs, $which, $type) = @_;
  361. my @half = @{$phalf};
  362. my %orig_configs = %{$oconfigs};
  363. my %source_configs = %{$sconfigs};
  364. my %tmp_config = %orig_configs;
  365. doprint "Settings bisect with $which half of $type configs:\n";
  366. foreach my $item (@half) {
  367. doprint "Updating $item to $source_configs{$item}\n";
  368. $tmp_config{$item} = $source_configs{$item};
  369. }
  370. return %tmp_config;
  371. }
  372. sub run_config_bisect {
  373. my ($pgood, $pbad) = @_;
  374. my %good_configs = %{$pgood};
  375. my %bad_configs = %{$pbad};
  376. my %diff_configs = diff_config_vals \%good_configs, \%bad_configs;
  377. my %b_configs = diff_configs \%good_configs, \%bad_configs;
  378. my %g_configs = diff_configs \%bad_configs, \%good_configs;
  379. # diff_arr is what is in both good and bad but are different (y->n)
  380. my @diff_arr = keys %diff_configs;
  381. my $len_diff = $#diff_arr + 1;
  382. # b_arr is what is in bad but not in good (has depends)
  383. my @b_arr = keys %b_configs;
  384. my $len_b = $#b_arr + 1;
  385. # g_arr is what is in good but not in bad
  386. my @g_arr = keys %g_configs;
  387. my $len_g = $#g_arr + 1;
  388. my $runtest = 0;
  389. my %new_configs;
  390. my $ret;
  391. # Look at the configs that are different between good and bad.
  392. # This does not include those that depend on other configs
  393. # (configs depending on other configs that are not set would
  394. # not show up even as a "# CONFIG_FOO is not set"
  395. doprint "# of configs to check: $len_diff\n";
  396. doprint "# of configs showing only in good: $len_g\n";
  397. doprint "# of configs showing only in bad: $len_b\n";
  398. if ($len_diff > 0) {
  399. # Now test for different values
  400. doprint "Configs left to check:\n";
  401. doprint " Good Config\t\t\tBad Config\n";
  402. doprint " -----------\t\t\t----------\n";
  403. foreach my $item (@diff_arr) {
  404. doprint " $good_configs{$item}\t$bad_configs{$item}\n";
  405. }
  406. my $half = int($#diff_arr / 2);
  407. my @tophalf = @diff_arr[0 .. $half];
  408. doprint "Set tmp config to be good config with some bad config values\n";
  409. my %tmp_config = make_half \@tophalf, \%good_configs,
  410. \%bad_configs, "top", "bad";
  411. $runtest = process_new_config \%tmp_config, \%new_configs,
  412. \%good_configs, \%bad_configs;
  413. if (!$runtest) {
  414. doprint "Set tmp config to be bad config with some good config values\n";
  415. my %tmp_config = make_half \@tophalf, \%bad_configs,
  416. \%good_configs, "top", "good";
  417. $runtest = process_new_config \%tmp_config, \%new_configs,
  418. \%good_configs, \%bad_configs;
  419. }
  420. }
  421. if (!$runtest && $len_diff > 0) {
  422. # do the same thing, but this time with bottom half
  423. my $half = int($#diff_arr / 2);
  424. my @bottomhalf = @diff_arr[$half+1 .. $#diff_arr];
  425. doprint "Set tmp config to be good config with some bad config values\n";
  426. my %tmp_config = make_half \@bottomhalf, \%good_configs,
  427. \%bad_configs, "bottom", "bad";
  428. $runtest = process_new_config \%tmp_config, \%new_configs,
  429. \%good_configs, \%bad_configs;
  430. if (!$runtest) {
  431. doprint "Set tmp config to be bad config with some good config values\n";
  432. my %tmp_config = make_half \@bottomhalf, \%bad_configs,
  433. \%good_configs, "bottom", "good";
  434. $runtest = process_new_config \%tmp_config, \%new_configs,
  435. \%good_configs, \%bad_configs;
  436. }
  437. }
  438. if ($runtest) {
  439. make_oldconfig;
  440. doprint "READY TO TEST .config IN $build\n";
  441. return 0;
  442. }
  443. doprint "\n%%%%%%%% FAILED TO FIND SINGLE BAD CONFIG %%%%%%%%\n";
  444. doprint "Hmm, can't make any more changes without making good == bad?\n";
  445. doprint "Difference between good (+) and bad (-)\n";
  446. foreach my $item (keys %bad_configs) {
  447. if (!defined($good_configs{$item})) {
  448. print_config "-", $bad_configs{$item};
  449. }
  450. }
  451. foreach my $item (keys %good_configs) {
  452. next if (!defined($bad_configs{$item}));
  453. if ($good_configs{$item} ne $bad_configs{$item}) {
  454. print_config_compare $good_configs{$item}, $bad_configs{$item};
  455. }
  456. }
  457. foreach my $item (keys %good_configs) {
  458. if (!defined($bad_configs{$item})) {
  459. print_config "+", $good_configs{$item};
  460. }
  461. }
  462. return -1;
  463. }
  464. sub config_bisect {
  465. my ($good_config, $bad_config) = @_;
  466. my $ret;
  467. my %good_configs;
  468. my %bad_configs;
  469. my %tmp_configs;
  470. doprint "Run good configs through make oldconfig\n";
  471. assign_configs \%tmp_configs, $good_config;
  472. create_config "$good_config", \%tmp_configs;
  473. assign_configs \%good_configs, $output_config;
  474. doprint "Run bad configs through make oldconfig\n";
  475. assign_configs \%tmp_configs, $bad_config;
  476. create_config "$bad_config", \%tmp_configs;
  477. assign_configs \%bad_configs, $output_config;
  478. save_config \%good_configs, $good_config;
  479. save_config \%bad_configs, $bad_config;
  480. return run_config_bisect \%good_configs, \%bad_configs;
  481. }
  482. while ($#ARGV >= 0) {
  483. if ($ARGV[0] !~ m/^-/) {
  484. last;
  485. }
  486. my $opt = shift @ARGV;
  487. if ($opt eq "-b") {
  488. $val = shift @ARGV;
  489. if (!defined($val)) {
  490. die "-b requires value\n";
  491. }
  492. $build = $val;
  493. }
  494. elsif ($opt eq "-l") {
  495. $val = shift @ARGV;
  496. if (!defined($val)) {
  497. die "-l requires value\n";
  498. }
  499. $tree = $val;
  500. }
  501. elsif ($opt eq "-r") {
  502. $reset_bisect = 1;
  503. }
  504. elsif ($opt eq "-h") {
  505. usage;
  506. }
  507. else {
  508. die "Unknown option $opt\n";
  509. }
  510. }
  511. $build = $tree if (!defined($build));
  512. $tree = expand_path $tree;
  513. $build = expand_path $build;
  514. if ( ! -d $tree ) {
  515. die "$tree not a directory\n";
  516. }
  517. if ( ! -d $build ) {
  518. die "$build not a directory\n";
  519. }
  520. usage if $#ARGV < 1;
  521. if ($#ARGV == 1) {
  522. $start = 1;
  523. } elsif ($#ARGV == 2) {
  524. $val = $ARGV[2];
  525. if ($val ne "good" && $val ne "bad") {
  526. die "Unknown command '$val', bust be either \"good\" or \"bad\"\n";
  527. }
  528. } else {
  529. usage;
  530. }
  531. my $good_start = expand_path $ARGV[0];
  532. my $bad_start = expand_path $ARGV[1];
  533. my $good = "$good_start.tmp";
  534. my $bad = "$bad_start.tmp";
  535. $make = "make";
  536. if ($build ne $tree) {
  537. $make = "make O=$build"
  538. }
  539. $output_config = "$build/.config";
  540. if ($start) {
  541. if ( ! -f $good_start ) {
  542. die "$good_start not found\n";
  543. }
  544. if ( ! -f $bad_start ) {
  545. die "$bad_start not found\n";
  546. }
  547. if ( -f $good || -f $bad ) {
  548. my $p = "";
  549. if ( -f $good ) {
  550. $p = "$good exists\n";
  551. }
  552. if ( -f $bad ) {
  553. $p = "$p$bad exists\n";
  554. }
  555. if (!defined($reset_bisect)) {
  556. if (!read_yn "${p}Overwrite and start new bisect anyway?") {
  557. exit (-1);
  558. }
  559. }
  560. }
  561. run_command "cp $good_start $good" or die "failed to copy to $good\n";
  562. run_command "cp $bad_start $bad" or die "failed to copy to $bad\n";
  563. } else {
  564. if ( ! -f $good ) {
  565. die "Can not find file $good\n";
  566. }
  567. if ( ! -f $bad ) {
  568. die "Can not find file $bad\n";
  569. }
  570. if ($val eq "good") {
  571. run_command "cp $output_config $good" or die "failed to copy $config to $good\n";
  572. } elsif ($val eq "bad") {
  573. run_command "cp $output_config $bad" or die "failed to copy $config to $bad\n";
  574. }
  575. }
  576. chdir $tree || die "can't change directory to $tree";
  577. my $ret = config_bisect $good, $bad;
  578. if (!$ret) {
  579. exit(0);
  580. }
  581. if ($ret > 0) {
  582. doprint "Cleaning temp files\n";
  583. run_command "rm $good";
  584. run_command "rm $bad";
  585. exit(1);
  586. } else {
  587. doprint "See good and bad configs for details:\n";
  588. doprint "good: $good\n";
  589. doprint "bad: $bad\n";
  590. doprint "%%%%%%%% FAILED TO FIND SINGLE BAD CONFIG %%%%%%%%\n";
  591. }
  592. exit(2);