config-bisect.pl 18 KB

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