ktest.pl 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374
  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
  4. # Licensed under the terms of the GNU GPL License version 2
  5. #
  6. use strict;
  7. use IPC::Open2;
  8. use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
  9. use File::Path qw(mkpath);
  10. use File::Copy qw(cp);
  11. use FileHandle;
  12. my $VERSION = "0.2";
  13. $| = 1;
  14. my %opt;
  15. my %repeat_tests;
  16. my %repeats;
  17. my %default;
  18. #default opts
  19. $default{"NUM_TESTS"} = 1;
  20. $default{"TEST_TYPE"} = "test";
  21. $default{"BUILD_TYPE"} = "randconfig";
  22. $default{"MAKE_CMD"} = "make";
  23. $default{"TIMEOUT"} = 120;
  24. $default{"TMP_DIR"} = "/tmp/ktest/\${MACHINE}";
  25. $default{"SLEEP_TIME"} = 60; # sleep time between tests
  26. $default{"BUILD_NOCLEAN"} = 0;
  27. $default{"REBOOT_ON_ERROR"} = 0;
  28. $default{"POWEROFF_ON_ERROR"} = 0;
  29. $default{"REBOOT_ON_SUCCESS"} = 1;
  30. $default{"POWEROFF_ON_SUCCESS"} = 0;
  31. $default{"BUILD_OPTIONS"} = "";
  32. $default{"BISECT_SLEEP_TIME"} = 60; # sleep time between bisects
  33. $default{"PATCHCHECK_SLEEP_TIME"} = 60; # sleep time between patch checks
  34. $default{"CLEAR_LOG"} = 0;
  35. $default{"BISECT_MANUAL"} = 0;
  36. $default{"BISECT_SKIP"} = 1;
  37. $default{"SUCCESS_LINE"} = "login:";
  38. $default{"DETECT_TRIPLE_FAULT"} = 1;
  39. $default{"NO_INSTALL"} = 0;
  40. $default{"BOOTED_TIMEOUT"} = 1;
  41. $default{"DIE_ON_FAILURE"} = 1;
  42. $default{"SSH_EXEC"} = "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND";
  43. $default{"SCP_TO_TARGET"} = "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE";
  44. $default{"REBOOT"} = "ssh \$SSH_USER\@\$MACHINE reboot";
  45. $default{"STOP_AFTER_SUCCESS"} = 10;
  46. $default{"STOP_AFTER_FAILURE"} = 60;
  47. $default{"STOP_TEST_AFTER"} = 600;
  48. # required, and we will ask users if they don't have them but we keep the default
  49. # value something that is common.
  50. $default{"REBOOT_TYPE"} = "grub";
  51. $default{"LOCALVERSION"} = "-test";
  52. $default{"SSH_USER"} = "root";
  53. $default{"BUILD_TARGET"} = "arch/x86/boot/bzImage";
  54. $default{"TARGET_IMAGE"} = "/boot/vmlinuz-test";
  55. my $ktest_config;
  56. my $version;
  57. my $machine;
  58. my $ssh_user;
  59. my $tmpdir;
  60. my $builddir;
  61. my $outputdir;
  62. my $output_config;
  63. my $test_type;
  64. my $build_type;
  65. my $build_options;
  66. my $pre_build;
  67. my $post_build;
  68. my $pre_build_die;
  69. my $post_build_die;
  70. my $reboot_type;
  71. my $reboot_script;
  72. my $power_cycle;
  73. my $reboot;
  74. my $reboot_on_error;
  75. my $poweroff_on_error;
  76. my $die_on_failure;
  77. my $powercycle_after_reboot;
  78. my $poweroff_after_halt;
  79. my $ssh_exec;
  80. my $scp_to_target;
  81. my $power_off;
  82. my $grub_menu;
  83. my $grub_number;
  84. my $target;
  85. my $make;
  86. my $post_install;
  87. my $no_install;
  88. my $noclean;
  89. my $minconfig;
  90. my $start_minconfig;
  91. my $start_minconfig_defined;
  92. my $output_minconfig;
  93. my $ignore_config;
  94. my $addconfig;
  95. my $in_bisect = 0;
  96. my $bisect_bad = "";
  97. my $reverse_bisect;
  98. my $bisect_manual;
  99. my $bisect_skip;
  100. my $config_bisect_good;
  101. my $in_patchcheck = 0;
  102. my $run_test;
  103. my $redirect;
  104. my $buildlog;
  105. my $testlog;
  106. my $dmesg;
  107. my $monitor_fp;
  108. my $monitor_pid;
  109. my $monitor_cnt = 0;
  110. my $sleep_time;
  111. my $bisect_sleep_time;
  112. my $patchcheck_sleep_time;
  113. my $ignore_warnings;
  114. my $store_failures;
  115. my $store_successes;
  116. my $test_name;
  117. my $timeout;
  118. my $booted_timeout;
  119. my $detect_triplefault;
  120. my $console;
  121. my $reboot_success_line;
  122. my $success_line;
  123. my $stop_after_success;
  124. my $stop_after_failure;
  125. my $stop_test_after;
  126. my $build_target;
  127. my $target_image;
  128. my $localversion;
  129. my $iteration = 0;
  130. my $successes = 0;
  131. my %entered_configs;
  132. my %config_help;
  133. my %variable;
  134. my %force_config;
  135. # do not force reboots on config problems
  136. my $no_reboot = 1;
  137. # default variables that can be used
  138. chomp ($variable{"PWD"} = `pwd`);
  139. $config_help{"MACHINE"} = << "EOF"
  140. The machine hostname that you will test.
  141. EOF
  142. ;
  143. $config_help{"SSH_USER"} = << "EOF"
  144. The box is expected to have ssh on normal bootup, provide the user
  145. (most likely root, since you need privileged operations)
  146. EOF
  147. ;
  148. $config_help{"BUILD_DIR"} = << "EOF"
  149. The directory that contains the Linux source code (full path).
  150. You can use \${PWD} that will be the path where ktest.pl is run, or use
  151. \${THIS_DIR} which is assigned \${PWD} but may be changed later.
  152. EOF
  153. ;
  154. $config_help{"OUTPUT_DIR"} = << "EOF"
  155. The directory that the objects will be built (full path).
  156. (can not be same as BUILD_DIR)
  157. You can use \${PWD} that will be the path where ktest.pl is run, or use
  158. \${THIS_DIR} which is assigned \${PWD} but may be changed later.
  159. EOF
  160. ;
  161. $config_help{"BUILD_TARGET"} = << "EOF"
  162. The location of the compiled file to copy to the target.
  163. (relative to OUTPUT_DIR)
  164. EOF
  165. ;
  166. $config_help{"TARGET_IMAGE"} = << "EOF"
  167. The place to put your image on the test machine.
  168. EOF
  169. ;
  170. $config_help{"POWER_CYCLE"} = << "EOF"
  171. A script or command to reboot the box.
  172. Here is a digital loggers power switch example
  173. POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
  174. Here is an example to reboot a virtual box on the current host
  175. with the name "Guest".
  176. POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
  177. EOF
  178. ;
  179. $config_help{"CONSOLE"} = << "EOF"
  180. The script or command that reads the console
  181. If you use ttywatch server, something like the following would work.
  182. CONSOLE = nc -d localhost 3001
  183. For a virtual machine with guest name "Guest".
  184. CONSOLE = virsh console Guest
  185. EOF
  186. ;
  187. $config_help{"LOCALVERSION"} = << "EOF"
  188. Required version ending to differentiate the test
  189. from other linux builds on the system.
  190. EOF
  191. ;
  192. $config_help{"REBOOT_TYPE"} = << "EOF"
  193. Way to reboot the box to the test kernel.
  194. Only valid options so far are "grub" and "script".
  195. If you specify grub, it will assume grub version 1
  196. and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
  197. and select that target to reboot to the kernel. If this is not
  198. your setup, then specify "script" and have a command or script
  199. specified in REBOOT_SCRIPT to boot to the target.
  200. The entry in /boot/grub/menu.lst must be entered in manually.
  201. The test will not modify that file.
  202. EOF
  203. ;
  204. $config_help{"GRUB_MENU"} = << "EOF"
  205. The grub title name for the test kernel to boot
  206. (Only mandatory if REBOOT_TYPE = grub)
  207. Note, ktest.pl will not update the grub menu.lst, you need to
  208. manually add an option for the test. ktest.pl will search
  209. the grub menu.lst for this option to find what kernel to
  210. reboot into.
  211. For example, if in the /boot/grub/menu.lst the test kernel title has:
  212. title Test Kernel
  213. kernel vmlinuz-test
  214. GRUB_MENU = Test Kernel
  215. EOF
  216. ;
  217. $config_help{"REBOOT_SCRIPT"} = << "EOF"
  218. A script to reboot the target into the test kernel
  219. (Only mandatory if REBOOT_TYPE = script)
  220. EOF
  221. ;
  222. sub read_prompt {
  223. my ($cancel, $prompt) = @_;
  224. my $ans;
  225. for (;;) {
  226. if ($cancel) {
  227. print "$prompt [y/n/C] ";
  228. } else {
  229. print "$prompt [Y/n] ";
  230. }
  231. $ans = <STDIN>;
  232. chomp $ans;
  233. if ($ans =~ /^\s*$/) {
  234. if ($cancel) {
  235. $ans = "c";
  236. } else {
  237. $ans = "y";
  238. }
  239. }
  240. last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
  241. if ($cancel) {
  242. last if ($ans =~ /^c$/i);
  243. print "Please answer either 'y', 'n' or 'c'.\n";
  244. } else {
  245. print "Please answer either 'y' or 'n'.\n";
  246. }
  247. }
  248. if ($ans =~ /^c/i) {
  249. exit;
  250. }
  251. if ($ans !~ /^y$/i) {
  252. return 0;
  253. }
  254. return 1;
  255. }
  256. sub read_yn {
  257. my ($prompt) = @_;
  258. return read_prompt 0, $prompt;
  259. }
  260. sub read_ync {
  261. my ($prompt) = @_;
  262. return read_prompt 1, $prompt;
  263. }
  264. sub get_ktest_config {
  265. my ($config) = @_;
  266. my $ans;
  267. return if (defined($opt{$config}));
  268. if (defined($config_help{$config})) {
  269. print "\n";
  270. print $config_help{$config};
  271. }
  272. for (;;) {
  273. print "$config = ";
  274. if (defined($default{$config})) {
  275. print "\[$default{$config}\] ";
  276. }
  277. $ans = <STDIN>;
  278. $ans =~ s/^\s*(.*\S)\s*$/$1/;
  279. if ($ans =~ /^\s*$/) {
  280. if ($default{$config}) {
  281. $ans = $default{$config};
  282. } else {
  283. print "Your answer can not be blank\n";
  284. next;
  285. }
  286. }
  287. $entered_configs{$config} = ${ans};
  288. last;
  289. }
  290. }
  291. sub get_ktest_configs {
  292. get_ktest_config("MACHINE");
  293. get_ktest_config("SSH_USER");
  294. get_ktest_config("BUILD_DIR");
  295. get_ktest_config("OUTPUT_DIR");
  296. get_ktest_config("BUILD_TARGET");
  297. get_ktest_config("TARGET_IMAGE");
  298. get_ktest_config("POWER_CYCLE");
  299. get_ktest_config("CONSOLE");
  300. get_ktest_config("LOCALVERSION");
  301. my $rtype = $opt{"REBOOT_TYPE"};
  302. if (!defined($rtype)) {
  303. if (!defined($opt{"GRUB_MENU"})) {
  304. get_ktest_config("REBOOT_TYPE");
  305. $rtype = $entered_configs{"REBOOT_TYPE"};
  306. } else {
  307. $rtype = "grub";
  308. }
  309. }
  310. if ($rtype eq "grub") {
  311. get_ktest_config("GRUB_MENU");
  312. } else {
  313. get_ktest_config("REBOOT_SCRIPT");
  314. }
  315. }
  316. sub process_variables {
  317. my ($value, $remove_undef) = @_;
  318. my $retval = "";
  319. # We want to check for '\', and it is just easier
  320. # to check the previous characet of '$' and not need
  321. # to worry if '$' is the first character. By adding
  322. # a space to $value, we can just check [^\\]\$ and
  323. # it will still work.
  324. $value = " $value";
  325. while ($value =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
  326. my $begin = $1;
  327. my $var = $2;
  328. my $end = $3;
  329. # append beginning of value to retval
  330. $retval = "$retval$begin";
  331. if (defined($variable{$var})) {
  332. $retval = "$retval$variable{$var}";
  333. } elsif (defined($remove_undef) && $remove_undef) {
  334. # for if statements, any variable that is not defined,
  335. # we simple convert to 0
  336. $retval = "${retval}0";
  337. } else {
  338. # put back the origin piece.
  339. $retval = "$retval\$\{$var\}";
  340. }
  341. $value = $end;
  342. }
  343. $retval = "$retval$value";
  344. # remove the space added in the beginning
  345. $retval =~ s/ //;
  346. return "$retval"
  347. }
  348. sub set_value {
  349. my ($lvalue, $rvalue, $override, $overrides, $name) = @_;
  350. if (defined($opt{$lvalue})) {
  351. if (!$override || defined(${$overrides}{$lvalue})) {
  352. my $extra = "";
  353. if ($override) {
  354. $extra = "In the same override section!\n";
  355. }
  356. die "$name: $.: Option $lvalue defined more than once!\n$extra";
  357. }
  358. ${$overrides}{$lvalue} = $rvalue;
  359. }
  360. if ($rvalue =~ /^\s*$/) {
  361. delete $opt{$lvalue};
  362. } else {
  363. $rvalue = process_variables($rvalue);
  364. $opt{$lvalue} = $rvalue;
  365. }
  366. }
  367. sub set_variable {
  368. my ($lvalue, $rvalue) = @_;
  369. if ($rvalue =~ /^\s*$/) {
  370. delete $variable{$lvalue};
  371. } else {
  372. $rvalue = process_variables($rvalue);
  373. $variable{$lvalue} = $rvalue;
  374. }
  375. }
  376. sub process_compare {
  377. my ($lval, $cmp, $rval) = @_;
  378. # remove whitespace
  379. $lval =~ s/^\s*//;
  380. $lval =~ s/\s*$//;
  381. $rval =~ s/^\s*//;
  382. $rval =~ s/\s*$//;
  383. if ($cmp eq "==") {
  384. return $lval eq $rval;
  385. } elsif ($cmp eq "!=") {
  386. return $lval ne $rval;
  387. }
  388. my $statement = "$lval $cmp $rval";
  389. my $ret = eval $statement;
  390. # $@ stores error of eval
  391. if ($@) {
  392. return -1;
  393. }
  394. return $ret;
  395. }
  396. sub value_defined {
  397. my ($val) = @_;
  398. return defined($variable{$2}) ||
  399. defined($opt{$2});
  400. }
  401. my $d = 0;
  402. sub process_expression {
  403. my ($name, $val) = @_;
  404. my $c = $d++;
  405. while ($val =~ s/\(([^\(]*?)\)/\&\&\&\&VAL\&\&\&\&/) {
  406. my $express = $1;
  407. if (process_expression($name, $express)) {
  408. $val =~ s/\&\&\&\&VAL\&\&\&\&/ 1 /;
  409. } else {
  410. $val =~ s/\&\&\&\&VAL\&\&\&\&/ 0 /;
  411. }
  412. }
  413. $d--;
  414. my $OR = "\\|\\|";
  415. my $AND = "\\&\\&";
  416. while ($val =~ s/^(.*?)($OR|$AND)//) {
  417. my $express = $1;
  418. my $op = $2;
  419. if (process_expression($name, $express)) {
  420. if ($op eq "||") {
  421. return 1;
  422. }
  423. } else {
  424. if ($op eq "&&") {
  425. return 0;
  426. }
  427. }
  428. }
  429. if ($val =~ /(.*)(==|\!=|>=|<=|>|<)(.*)/) {
  430. my $ret = process_compare($1, $2, $3);
  431. if ($ret < 0) {
  432. die "$name: $.: Unable to process comparison\n";
  433. }
  434. return $ret;
  435. }
  436. if ($val =~ /^\s*(NOT\s*)?DEFINED\s+(\S+)\s*$/) {
  437. if (defined $1) {
  438. return !value_defined($2);
  439. } else {
  440. return value_defined($2);
  441. }
  442. }
  443. if ($val =~ /^\s*0\s*$/) {
  444. return 0;
  445. } elsif ($val =~ /^\s*\d+\s*$/) {
  446. return 1;
  447. }
  448. die ("$name: $.: Undefined content $val in if statement\n");
  449. }
  450. sub process_if {
  451. my ($name, $value) = @_;
  452. # Convert variables and replace undefined ones with 0
  453. my $val = process_variables($value, 1);
  454. my $ret = process_expression $name, $val;
  455. return $ret;
  456. }
  457. sub __read_config {
  458. my ($config, $current_test_num) = @_;
  459. my $in;
  460. open($in, $config) || die "can't read file $config";
  461. my $name = $config;
  462. $name =~ s,.*/(.*),$1,;
  463. my $test_num = $$current_test_num;
  464. my $default = 1;
  465. my $repeat = 1;
  466. my $num_tests_set = 0;
  467. my $skip = 0;
  468. my $rest;
  469. my $line;
  470. my $test_case = 0;
  471. my $if = 0;
  472. my $if_set = 0;
  473. my $override = 0;
  474. my %overrides;
  475. while (<$in>) {
  476. # ignore blank lines and comments
  477. next if (/^\s*$/ || /\s*\#/);
  478. if (/^\s*(TEST_START|DEFAULTS)\b(.*)/) {
  479. my $type = $1;
  480. $rest = $2;
  481. $line = $2;
  482. my $old_test_num;
  483. my $old_repeat;
  484. $override = 0;
  485. if ($type eq "TEST_START") {
  486. if ($num_tests_set) {
  487. die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
  488. }
  489. $old_test_num = $test_num;
  490. $old_repeat = $repeat;
  491. $test_num += $repeat;
  492. $default = 0;
  493. $repeat = 1;
  494. } else {
  495. $default = 1;
  496. }
  497. # If SKIP is anywhere in the line, the command will be skipped
  498. if ($rest =~ s/\s+SKIP\b//) {
  499. $skip = 1;
  500. } else {
  501. $test_case = 1;
  502. $skip = 0;
  503. }
  504. if ($rest =~ s/\sELSE\b//) {
  505. if (!$if) {
  506. die "$name: $.: ELSE found with out matching IF section\n$_";
  507. }
  508. $if = 0;
  509. if ($if_set) {
  510. $skip = 1;
  511. } else {
  512. $skip = 0;
  513. }
  514. }
  515. if ($rest =~ s/\sIF\s+(.*)//) {
  516. if (process_if($name, $1)) {
  517. $if_set = 1;
  518. } else {
  519. $skip = 1;
  520. }
  521. $if = 1;
  522. } else {
  523. $if = 0;
  524. $if_set = 0;
  525. }
  526. if (!$skip) {
  527. if ($type eq "TEST_START") {
  528. if ($rest =~ s/\s+ITERATE\s+(\d+)//) {
  529. $repeat = $1;
  530. $repeat_tests{"$test_num"} = $repeat;
  531. }
  532. } elsif ($rest =~ s/\sOVERRIDE\b//) {
  533. # DEFAULT only
  534. $override = 1;
  535. # Clear previous overrides
  536. %overrides = ();
  537. }
  538. }
  539. if (!$skip && $rest !~ /^\s*$/) {
  540. die "$name: $.: Gargbage found after $type\n$_";
  541. }
  542. if ($skip && $type eq "TEST_START") {
  543. $test_num = $old_test_num;
  544. $repeat = $old_repeat;
  545. }
  546. } elsif (/^\s*ELSE\b(.*)$/) {
  547. if (!$if) {
  548. die "$name: $.: ELSE found with out matching IF section\n$_";
  549. }
  550. $rest = $1;
  551. if ($if_set) {
  552. $skip = 1;
  553. $rest = "";
  554. } else {
  555. $skip = 0;
  556. if ($rest =~ /\sIF\s+(.*)/) {
  557. # May be a ELSE IF section.
  558. if (!process_if($name, $1)) {
  559. $skip = 1;
  560. }
  561. $rest = "";
  562. } else {
  563. $if = 0;
  564. }
  565. }
  566. if ($rest !~ /^\s*$/) {
  567. die "$name: $.: Gargbage found after DEFAULTS\n$_";
  568. }
  569. } elsif (/^\s*INCLUDE\s+(\S+)/) {
  570. next if ($skip);
  571. if (!$default) {
  572. die "$name: $.: INCLUDE can only be done in default sections\n$_";
  573. }
  574. my $file = process_variables($1);
  575. if ($file !~ m,^/,) {
  576. # check the path of the config file first
  577. if ($config =~ m,(.*)/,) {
  578. if (-f "$1/$file") {
  579. $file = "$1/$file";
  580. }
  581. }
  582. }
  583. if ( ! -r $file ) {
  584. die "$name: $.: Can't read file $file\n$_";
  585. }
  586. if (__read_config($file, \$test_num)) {
  587. $test_case = 1;
  588. }
  589. } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
  590. next if ($skip);
  591. my $lvalue = $1;
  592. my $rvalue = $2;
  593. if (!$default &&
  594. ($lvalue eq "NUM_TESTS" ||
  595. $lvalue eq "LOG_FILE" ||
  596. $lvalue eq "CLEAR_LOG")) {
  597. die "$name: $.: $lvalue must be set in DEFAULTS section\n";
  598. }
  599. if ($lvalue eq "NUM_TESTS") {
  600. if ($test_num) {
  601. die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
  602. }
  603. if (!$default) {
  604. die "$name: $.: NUM_TESTS must be set in default section\n";
  605. }
  606. $num_tests_set = 1;
  607. }
  608. if ($default || $lvalue =~ /\[\d+\]$/) {
  609. set_value($lvalue, $rvalue, $override, \%overrides, $name);
  610. } else {
  611. my $val = "$lvalue\[$test_num\]";
  612. set_value($val, $rvalue, $override, \%overrides, $name);
  613. if ($repeat > 1) {
  614. $repeats{$val} = $repeat;
  615. }
  616. }
  617. } elsif (/^\s*([A-Z_\[\]\d]+)\s*:=\s*(.*?)\s*$/) {
  618. next if ($skip);
  619. my $lvalue = $1;
  620. my $rvalue = $2;
  621. # process config variables.
  622. # Config variables are only active while reading the
  623. # config and can be defined anywhere. They also ignore
  624. # TEST_START and DEFAULTS, but are skipped if they are in
  625. # on of these sections that have SKIP defined.
  626. # The save variable can be
  627. # defined multiple times and the new one simply overrides
  628. # the prevous one.
  629. set_variable($lvalue, $rvalue);
  630. } else {
  631. die "$name: $.: Garbage found in config\n$_";
  632. }
  633. }
  634. if ($test_num) {
  635. $test_num += $repeat - 1;
  636. $opt{"NUM_TESTS"} = $test_num;
  637. }
  638. close($in);
  639. $$current_test_num = $test_num;
  640. return $test_case;
  641. }
  642. sub read_config {
  643. my ($config) = @_;
  644. my $test_case;
  645. my $test_num = 0;
  646. $test_case = __read_config $config, \$test_num;
  647. # make sure we have all mandatory configs
  648. get_ktest_configs;
  649. # was a test specified?
  650. if (!$test_case) {
  651. print "No test case specified.\n";
  652. print "What test case would you like to run?\n";
  653. my $ans = <STDIN>;
  654. chomp $ans;
  655. $default{"TEST_TYPE"} = $ans;
  656. }
  657. # set any defaults
  658. foreach my $default (keys %default) {
  659. if (!defined($opt{$default})) {
  660. $opt{$default} = $default{$default};
  661. }
  662. }
  663. }
  664. sub __eval_option {
  665. my ($option, $i) = @_;
  666. # Add space to evaluate the character before $
  667. $option = " $option";
  668. my $retval = "";
  669. my $repeated = 0;
  670. my $parent = 0;
  671. foreach my $test (keys %repeat_tests) {
  672. if ($i >= $test &&
  673. $i < $test + $repeat_tests{$test}) {
  674. $repeated = 1;
  675. $parent = $test;
  676. last;
  677. }
  678. }
  679. while ($option =~ /(.*?[^\\])\$\{(.*?)\}(.*)/) {
  680. my $start = $1;
  681. my $var = $2;
  682. my $end = $3;
  683. # Append beginning of line
  684. $retval = "$retval$start";
  685. # If the iteration option OPT[$i] exists, then use that.
  686. # otherwise see if the default OPT (without [$i]) exists.
  687. my $o = "$var\[$i\]";
  688. my $parento = "$var\[$parent\]";
  689. if (defined($opt{$o})) {
  690. $o = $opt{$o};
  691. $retval = "$retval$o";
  692. } elsif ($repeated && defined($opt{$parento})) {
  693. $o = $opt{$parento};
  694. $retval = "$retval$o";
  695. } elsif (defined($opt{$var})) {
  696. $o = $opt{$var};
  697. $retval = "$retval$o";
  698. } else {
  699. $retval = "$retval\$\{$var\}";
  700. }
  701. $option = $end;
  702. }
  703. $retval = "$retval$option";
  704. $retval =~ s/^ //;
  705. return $retval;
  706. }
  707. sub eval_option {
  708. my ($option, $i) = @_;
  709. my $prev = "";
  710. # Since an option can evaluate to another option,
  711. # keep iterating until we do not evaluate any more
  712. # options.
  713. my $r = 0;
  714. while ($prev ne $option) {
  715. # Check for recursive evaluations.
  716. # 100 deep should be more than enough.
  717. if ($r++ > 100) {
  718. die "Over 100 evaluations accurred with $option\n" .
  719. "Check for recursive variables\n";
  720. }
  721. $prev = $option;
  722. $option = __eval_option($option, $i);
  723. }
  724. return $option;
  725. }
  726. sub _logit {
  727. if (defined($opt{"LOG_FILE"})) {
  728. open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
  729. print OUT @_;
  730. close(OUT);
  731. }
  732. }
  733. sub logit {
  734. if (defined($opt{"LOG_FILE"})) {
  735. _logit @_;
  736. } else {
  737. print @_;
  738. }
  739. }
  740. sub doprint {
  741. print @_;
  742. _logit @_;
  743. }
  744. sub run_command;
  745. sub start_monitor;
  746. sub end_monitor;
  747. sub wait_for_monitor;
  748. sub reboot {
  749. my ($time) = @_;
  750. if (defined($time)) {
  751. start_monitor;
  752. # flush out current monitor
  753. # May contain the reboot success line
  754. wait_for_monitor 1;
  755. }
  756. # try to reboot normally
  757. if (run_command $reboot) {
  758. if (defined($powercycle_after_reboot)) {
  759. sleep $powercycle_after_reboot;
  760. run_command "$power_cycle";
  761. }
  762. } else {
  763. # nope? power cycle it.
  764. run_command "$power_cycle";
  765. }
  766. if (defined($time)) {
  767. wait_for_monitor($time, $reboot_success_line);
  768. end_monitor;
  769. }
  770. }
  771. sub do_not_reboot {
  772. my $i = $iteration;
  773. return $test_type eq "build" || $no_reboot ||
  774. ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
  775. ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
  776. }
  777. sub dodie {
  778. doprint "CRITICAL FAILURE... ", @_, "\n";
  779. my $i = $iteration;
  780. if ($reboot_on_error && !do_not_reboot) {
  781. doprint "REBOOTING\n";
  782. reboot;
  783. } elsif ($poweroff_on_error && defined($power_off)) {
  784. doprint "POWERING OFF\n";
  785. `$power_off`;
  786. }
  787. if (defined($opt{"LOG_FILE"})) {
  788. print " See $opt{LOG_FILE} for more info.\n";
  789. }
  790. die @_, "\n";
  791. }
  792. sub open_console {
  793. my ($fp) = @_;
  794. my $flags;
  795. my $pid = open($fp, "$console|") or
  796. dodie "Can't open console $console";
  797. $flags = fcntl($fp, F_GETFL, 0) or
  798. dodie "Can't get flags for the socket: $!";
  799. $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
  800. dodie "Can't set flags for the socket: $!";
  801. return $pid;
  802. }
  803. sub close_console {
  804. my ($fp, $pid) = @_;
  805. doprint "kill child process $pid\n";
  806. kill 2, $pid;
  807. print "closing!\n";
  808. close($fp);
  809. }
  810. sub start_monitor {
  811. if ($monitor_cnt++) {
  812. return;
  813. }
  814. $monitor_fp = \*MONFD;
  815. $monitor_pid = open_console $monitor_fp;
  816. return;
  817. open(MONFD, "Stop perl from warning about single use of MONFD");
  818. }
  819. sub end_monitor {
  820. if (--$monitor_cnt) {
  821. return;
  822. }
  823. close_console($monitor_fp, $monitor_pid);
  824. }
  825. sub wait_for_monitor {
  826. my ($time, $stop) = @_;
  827. my $full_line = "";
  828. my $line;
  829. my $booted = 0;
  830. doprint "** Wait for monitor to settle down **\n";
  831. # read the monitor and wait for the system to calm down
  832. while (!$booted) {
  833. $line = wait_for_input($monitor_fp, $time);
  834. last if (!defined($line));
  835. print "$line";
  836. $full_line .= $line;
  837. if (defined($stop) && $full_line =~ /$stop/) {
  838. doprint "wait for monitor detected $stop\n";
  839. $booted = 1;
  840. }
  841. if ($line =~ /\n/) {
  842. $full_line = "";
  843. }
  844. }
  845. print "** Monitor flushed **\n";
  846. }
  847. sub save_logs {
  848. my ($result, $basedir) = @_;
  849. my @t = localtime;
  850. my $date = sprintf "%04d%02d%02d%02d%02d%02d",
  851. 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
  852. my $type = $build_type;
  853. if ($type =~ /useconfig/) {
  854. $type = "useconfig";
  855. }
  856. my $dir = "$machine-$test_type-$type-$result-$date";
  857. $dir = "$basedir/$dir";
  858. if (!-d $dir) {
  859. mkpath($dir) or
  860. die "can't create $dir";
  861. }
  862. my %files = (
  863. "config" => $output_config,
  864. "buildlog" => $buildlog,
  865. "dmesg" => $dmesg,
  866. "testlog" => $testlog,
  867. );
  868. while (my ($name, $source) = each(%files)) {
  869. if (-f "$source") {
  870. cp "$source", "$dir/$name" or
  871. die "failed to copy $source";
  872. }
  873. }
  874. doprint "*** Saved info to $dir ***\n";
  875. }
  876. sub fail {
  877. if ($die_on_failure) {
  878. dodie @_;
  879. }
  880. doprint "FAILED\n";
  881. my $i = $iteration;
  882. # no need to reboot for just building.
  883. if (!do_not_reboot) {
  884. doprint "REBOOTING\n";
  885. reboot $sleep_time;
  886. }
  887. my $name = "";
  888. if (defined($test_name)) {
  889. $name = " ($test_name)";
  890. }
  891. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  892. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  893. doprint "KTEST RESULT: TEST $i$name Failed: ", @_, "\n";
  894. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  895. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  896. if (defined($store_failures)) {
  897. save_logs "fail", $store_failures;
  898. }
  899. return 1;
  900. }
  901. sub run_command {
  902. my ($command) = @_;
  903. my $dolog = 0;
  904. my $dord = 0;
  905. my $pid;
  906. $command =~ s/\$SSH_USER/$ssh_user/g;
  907. $command =~ s/\$MACHINE/$machine/g;
  908. doprint("$command ... ");
  909. $pid = open(CMD, "$command 2>&1 |") or
  910. (fail "unable to exec $command" and return 0);
  911. if (defined($opt{"LOG_FILE"})) {
  912. open(LOG, ">>$opt{LOG_FILE}") or
  913. dodie "failed to write to log";
  914. $dolog = 1;
  915. }
  916. if (defined($redirect)) {
  917. open (RD, ">$redirect") or
  918. dodie "failed to write to redirect $redirect";
  919. $dord = 1;
  920. }
  921. while (<CMD>) {
  922. print LOG if ($dolog);
  923. print RD if ($dord);
  924. }
  925. waitpid($pid, 0);
  926. my $failed = $?;
  927. close(CMD);
  928. close(LOG) if ($dolog);
  929. close(RD) if ($dord);
  930. if ($failed) {
  931. doprint "FAILED!\n";
  932. } else {
  933. doprint "SUCCESS\n";
  934. }
  935. return !$failed;
  936. }
  937. sub run_ssh {
  938. my ($cmd) = @_;
  939. my $cp_exec = $ssh_exec;
  940. $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
  941. return run_command "$cp_exec";
  942. }
  943. sub run_scp {
  944. my ($src, $dst) = @_;
  945. my $cp_scp = $scp_to_target;
  946. $cp_scp =~ s/\$SRC_FILE/$src/g;
  947. $cp_scp =~ s/\$DST_FILE/$dst/g;
  948. return run_command "$cp_scp";
  949. }
  950. sub get_grub_index {
  951. if ($reboot_type ne "grub") {
  952. return;
  953. }
  954. return if (defined($grub_number));
  955. doprint "Find grub menu ... ";
  956. $grub_number = -1;
  957. my $ssh_grub = $ssh_exec;
  958. $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
  959. open(IN, "$ssh_grub |")
  960. or die "unable to get menu.lst";
  961. my $found = 0;
  962. while (<IN>) {
  963. if (/^\s*title\s+$grub_menu\s*$/) {
  964. $grub_number++;
  965. $found = 1;
  966. last;
  967. } elsif (/^\s*title\s/) {
  968. $grub_number++;
  969. }
  970. }
  971. close(IN);
  972. die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
  973. if (!$found);
  974. doprint "$grub_number\n";
  975. }
  976. sub wait_for_input
  977. {
  978. my ($fp, $time) = @_;
  979. my $rin;
  980. my $ready;
  981. my $line;
  982. my $ch;
  983. if (!defined($time)) {
  984. $time = $timeout;
  985. }
  986. $rin = '';
  987. vec($rin, fileno($fp), 1) = 1;
  988. $ready = select($rin, undef, undef, $time);
  989. $line = "";
  990. # try to read one char at a time
  991. while (sysread $fp, $ch, 1) {
  992. $line .= $ch;
  993. last if ($ch eq "\n");
  994. }
  995. if (!length($line)) {
  996. return undef;
  997. }
  998. return $line;
  999. }
  1000. sub reboot_to {
  1001. if ($reboot_type eq "grub") {
  1002. run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch)'";
  1003. reboot;
  1004. return;
  1005. }
  1006. run_command "$reboot_script";
  1007. }
  1008. sub get_sha1 {
  1009. my ($commit) = @_;
  1010. doprint "git rev-list --max-count=1 $commit ... ";
  1011. my $sha1 = `git rev-list --max-count=1 $commit`;
  1012. my $ret = $?;
  1013. logit $sha1;
  1014. if ($ret) {
  1015. doprint "FAILED\n";
  1016. dodie "Failed to get git $commit";
  1017. }
  1018. print "SUCCESS\n";
  1019. chomp $sha1;
  1020. return $sha1;
  1021. }
  1022. sub monitor {
  1023. my $booted = 0;
  1024. my $bug = 0;
  1025. my $skip_call_trace = 0;
  1026. my $loops;
  1027. wait_for_monitor 5;
  1028. my $line;
  1029. my $full_line = "";
  1030. open(DMESG, "> $dmesg") or
  1031. die "unable to write to $dmesg";
  1032. reboot_to;
  1033. my $success_start;
  1034. my $failure_start;
  1035. my $monitor_start = time;
  1036. my $done = 0;
  1037. my $version_found = 0;
  1038. while (!$done) {
  1039. if ($bug && defined($stop_after_failure) &&
  1040. $stop_after_failure >= 0) {
  1041. my $time = $stop_after_failure - (time - $failure_start);
  1042. $line = wait_for_input($monitor_fp, $time);
  1043. if (!defined($line)) {
  1044. doprint "bug timed out after $booted_timeout seconds\n";
  1045. doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
  1046. last;
  1047. }
  1048. } elsif ($booted) {
  1049. $line = wait_for_input($monitor_fp, $booted_timeout);
  1050. if (!defined($line)) {
  1051. my $s = $booted_timeout == 1 ? "" : "s";
  1052. doprint "Successful boot found: break after $booted_timeout second$s\n";
  1053. last;
  1054. }
  1055. } else {
  1056. $line = wait_for_input($monitor_fp);
  1057. if (!defined($line)) {
  1058. my $s = $timeout == 1 ? "" : "s";
  1059. doprint "Timed out after $timeout second$s\n";
  1060. last;
  1061. }
  1062. }
  1063. doprint $line;
  1064. print DMESG $line;
  1065. # we are not guaranteed to get a full line
  1066. $full_line .= $line;
  1067. if ($full_line =~ /$success_line/) {
  1068. $booted = 1;
  1069. $success_start = time;
  1070. }
  1071. if ($booted && defined($stop_after_success) &&
  1072. $stop_after_success >= 0) {
  1073. my $now = time;
  1074. if ($now - $success_start >= $stop_after_success) {
  1075. doprint "Test forced to stop after $stop_after_success seconds after success\n";
  1076. last;
  1077. }
  1078. }
  1079. if ($full_line =~ /\[ backtrace testing \]/) {
  1080. $skip_call_trace = 1;
  1081. }
  1082. if ($full_line =~ /call trace:/i) {
  1083. if (!$bug && !$skip_call_trace) {
  1084. $bug = 1;
  1085. $failure_start = time;
  1086. }
  1087. }
  1088. if ($bug && defined($stop_after_failure) &&
  1089. $stop_after_failure >= 0) {
  1090. my $now = time;
  1091. if ($now - $failure_start >= $stop_after_failure) {
  1092. doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
  1093. last;
  1094. }
  1095. }
  1096. if ($full_line =~ /\[ end of backtrace testing \]/) {
  1097. $skip_call_trace = 0;
  1098. }
  1099. if ($full_line =~ /Kernel panic -/) {
  1100. $failure_start = time;
  1101. $bug = 1;
  1102. }
  1103. # Detect triple faults by testing the banner
  1104. if ($full_line =~ /\bLinux version (\S+).*\n/) {
  1105. if ($1 eq $version) {
  1106. $version_found = 1;
  1107. } elsif ($version_found && $detect_triplefault) {
  1108. # We already booted into the kernel we are testing,
  1109. # but now we booted into another kernel?
  1110. # Consider this a triple fault.
  1111. doprint "Aleady booted in Linux kernel $version, but now\n";
  1112. doprint "we booted into Linux kernel $1.\n";
  1113. doprint "Assuming that this is a triple fault.\n";
  1114. doprint "To disable this: set DETECT_TRIPLE_FAULT to 0\n";
  1115. last;
  1116. }
  1117. }
  1118. if ($line =~ /\n/) {
  1119. $full_line = "";
  1120. }
  1121. if ($stop_test_after > 0 && !$booted && !$bug) {
  1122. if (time - $monitor_start > $stop_test_after) {
  1123. doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
  1124. $done = 1;
  1125. }
  1126. }
  1127. }
  1128. close(DMESG);
  1129. if ($bug) {
  1130. return 0 if ($in_bisect);
  1131. fail "failed - got a bug report" and return 0;
  1132. }
  1133. if (!$booted) {
  1134. return 0 if ($in_bisect);
  1135. fail "failed - never got a boot prompt." and return 0;
  1136. }
  1137. return 1;
  1138. }
  1139. sub do_post_install {
  1140. return if (!defined($post_install));
  1141. my $cp_post_install = $post_install;
  1142. $cp_post_install =~ s/\$KERNEL_VERSION/$version/g;
  1143. run_command "$cp_post_install" or
  1144. dodie "Failed to run post install";
  1145. }
  1146. sub install {
  1147. return if ($no_install);
  1148. run_scp "$outputdir/$build_target", "$target_image" or
  1149. dodie "failed to copy image";
  1150. my $install_mods = 0;
  1151. # should we process modules?
  1152. $install_mods = 0;
  1153. open(IN, "$output_config") or dodie("Can't read config file");
  1154. while (<IN>) {
  1155. if (/CONFIG_MODULES(=y)?/) {
  1156. $install_mods = 1 if (defined($1));
  1157. last;
  1158. }
  1159. }
  1160. close(IN);
  1161. if (!$install_mods) {
  1162. do_post_install;
  1163. doprint "No modules needed\n";
  1164. return;
  1165. }
  1166. run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
  1167. dodie "Failed to install modules";
  1168. my $modlib = "/lib/modules/$version";
  1169. my $modtar = "ktest-mods.tar.bz2";
  1170. run_ssh "rm -rf $modlib" or
  1171. dodie "failed to remove old mods: $modlib";
  1172. # would be nice if scp -r did not follow symbolic links
  1173. run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
  1174. dodie "making tarball";
  1175. run_scp "$tmpdir/$modtar", "/tmp" or
  1176. dodie "failed to copy modules";
  1177. unlink "$tmpdir/$modtar";
  1178. run_ssh "'(cd / && tar xjf /tmp/$modtar)'" or
  1179. dodie "failed to tar modules";
  1180. run_ssh "rm -f /tmp/$modtar";
  1181. do_post_install;
  1182. }
  1183. sub get_version {
  1184. # get the release name
  1185. doprint "$make kernelrelease ... ";
  1186. $version = `$make kernelrelease | tail -1`;
  1187. chomp($version);
  1188. doprint "$version\n";
  1189. }
  1190. sub start_monitor_and_boot {
  1191. # Make sure the stable kernel has finished booting
  1192. start_monitor;
  1193. wait_for_monitor 5;
  1194. end_monitor;
  1195. get_grub_index;
  1196. get_version;
  1197. install;
  1198. start_monitor;
  1199. return monitor;
  1200. }
  1201. sub check_buildlog {
  1202. my ($patch) = @_;
  1203. my @files = `git show $patch | diffstat -l`;
  1204. open(IN, "git show $patch |") or
  1205. dodie "failed to show $patch";
  1206. while (<IN>) {
  1207. if (m,^--- a/(.*),) {
  1208. chomp $1;
  1209. $files[$#files] = $1;
  1210. }
  1211. }
  1212. close(IN);
  1213. open(IN, $buildlog) or dodie "Can't open $buildlog";
  1214. while (<IN>) {
  1215. if (/^\s*(.*?):.*(warning|error)/) {
  1216. my $err = $1;
  1217. foreach my $file (@files) {
  1218. my $fullpath = "$builddir/$file";
  1219. if ($file eq $err || $fullpath eq $err) {
  1220. fail "$file built with warnings" and return 0;
  1221. }
  1222. }
  1223. }
  1224. }
  1225. close(IN);
  1226. return 1;
  1227. }
  1228. sub apply_min_config {
  1229. my $outconfig = "$output_config.new";
  1230. # Read the config file and remove anything that
  1231. # is in the force_config hash (from minconfig and others)
  1232. # then add the force config back.
  1233. doprint "Applying minimum configurations into $output_config.new\n";
  1234. open (OUT, ">$outconfig") or
  1235. dodie "Can't create $outconfig";
  1236. if (-f $output_config) {
  1237. open (IN, $output_config) or
  1238. dodie "Failed to open $output_config";
  1239. while (<IN>) {
  1240. if (/^(# )?(CONFIG_[^\s=]*)/) {
  1241. next if (defined($force_config{$2}));
  1242. }
  1243. print OUT;
  1244. }
  1245. close IN;
  1246. }
  1247. foreach my $config (keys %force_config) {
  1248. print OUT "$force_config{$config}\n";
  1249. }
  1250. close OUT;
  1251. run_command "mv $outconfig $output_config";
  1252. }
  1253. sub make_oldconfig {
  1254. my @force_list = keys %force_config;
  1255. if ($#force_list >= 0) {
  1256. apply_min_config;
  1257. }
  1258. if (!run_command "$make oldnoconfig") {
  1259. # Perhaps oldnoconfig doesn't exist in this version of the kernel
  1260. # try a yes '' | oldconfig
  1261. doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
  1262. run_command "yes '' | $make oldconfig" or
  1263. dodie "failed make config oldconfig";
  1264. }
  1265. }
  1266. # read a config file and use this to force new configs.
  1267. sub load_force_config {
  1268. my ($config) = @_;
  1269. open(IN, $config) or
  1270. dodie "failed to read $config";
  1271. while (<IN>) {
  1272. chomp;
  1273. if (/^(CONFIG[^\s=]*)(\s*=.*)/) {
  1274. $force_config{$1} = $_;
  1275. } elsif (/^# (CONFIG_\S*) is not set/) {
  1276. $force_config{$1} = $_;
  1277. }
  1278. }
  1279. close IN;
  1280. }
  1281. sub build {
  1282. my ($type) = @_;
  1283. unlink $buildlog;
  1284. # Failed builds should not reboot the target
  1285. my $save_no_reboot = $no_reboot;
  1286. $no_reboot = 1;
  1287. if (defined($pre_build)) {
  1288. my $ret = run_command $pre_build;
  1289. if (!$ret && defined($pre_build_die) &&
  1290. $pre_build_die) {
  1291. dodie "failed to pre_build\n";
  1292. }
  1293. }
  1294. if ($type =~ /^useconfig:(.*)/) {
  1295. run_command "cp $1 $output_config" or
  1296. dodie "could not copy $1 to .config";
  1297. $type = "oldconfig";
  1298. }
  1299. # old config can ask questions
  1300. if ($type eq "oldconfig") {
  1301. $type = "oldnoconfig";
  1302. # allow for empty configs
  1303. run_command "touch $output_config";
  1304. if (!$noclean) {
  1305. run_command "mv $output_config $outputdir/config_temp" or
  1306. dodie "moving .config";
  1307. run_command "$make mrproper" or dodie "make mrproper";
  1308. run_command "mv $outputdir/config_temp $output_config" or
  1309. dodie "moving config_temp";
  1310. }
  1311. } elsif (!$noclean) {
  1312. unlink "$output_config";
  1313. run_command "$make mrproper" or
  1314. dodie "make mrproper";
  1315. }
  1316. # add something to distinguish this build
  1317. open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
  1318. print OUT "$localversion\n";
  1319. close(OUT);
  1320. if (defined($minconfig)) {
  1321. load_force_config($minconfig);
  1322. }
  1323. if ($type ne "oldnoconfig") {
  1324. run_command "$make $type" or
  1325. dodie "failed make config";
  1326. }
  1327. # Run old config regardless, to enforce min configurations
  1328. make_oldconfig;
  1329. $redirect = "$buildlog";
  1330. my $build_ret = run_command "$make $build_options";
  1331. undef $redirect;
  1332. if (defined($post_build)) {
  1333. my $ret = run_command $post_build;
  1334. if (!$ret && defined($post_build_die) &&
  1335. $post_build_die) {
  1336. dodie "failed to post_build\n";
  1337. }
  1338. }
  1339. if (!$build_ret) {
  1340. # bisect may need this to pass
  1341. if ($in_bisect) {
  1342. $no_reboot = $save_no_reboot;
  1343. return 0;
  1344. }
  1345. fail "failed build" and return 0;
  1346. }
  1347. $no_reboot = $save_no_reboot;
  1348. return 1;
  1349. }
  1350. sub halt {
  1351. if (!run_ssh "halt" or defined($power_off)) {
  1352. if (defined($poweroff_after_halt)) {
  1353. sleep $poweroff_after_halt;
  1354. run_command "$power_off";
  1355. }
  1356. } else {
  1357. # nope? the zap it!
  1358. run_command "$power_off";
  1359. }
  1360. }
  1361. sub success {
  1362. my ($i) = @_;
  1363. $successes++;
  1364. my $name = "";
  1365. if (defined($test_name)) {
  1366. $name = " ($test_name)";
  1367. }
  1368. doprint "\n\n*******************************************\n";
  1369. doprint "*******************************************\n";
  1370. doprint "KTEST RESULT: TEST $i$name SUCCESS!!!! **\n";
  1371. doprint "*******************************************\n";
  1372. doprint "*******************************************\n";
  1373. if (defined($store_successes)) {
  1374. save_logs "success", $store_successes;
  1375. }
  1376. if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
  1377. doprint "Reboot and wait $sleep_time seconds\n";
  1378. reboot $sleep_time;
  1379. }
  1380. }
  1381. sub answer_bisect {
  1382. for (;;) {
  1383. doprint "Pass or fail? [p/f]";
  1384. my $ans = <STDIN>;
  1385. chomp $ans;
  1386. if ($ans eq "p" || $ans eq "P") {
  1387. return 1;
  1388. } elsif ($ans eq "f" || $ans eq "F") {
  1389. return 0;
  1390. } else {
  1391. print "Please answer 'P' or 'F'\n";
  1392. }
  1393. }
  1394. }
  1395. sub child_run_test {
  1396. my $failed = 0;
  1397. # child should have no power
  1398. $reboot_on_error = 0;
  1399. $poweroff_on_error = 0;
  1400. $die_on_failure = 1;
  1401. $redirect = "$testlog";
  1402. run_command $run_test or $failed = 1;
  1403. undef $redirect;
  1404. exit $failed;
  1405. }
  1406. my $child_done;
  1407. sub child_finished {
  1408. $child_done = 1;
  1409. }
  1410. sub do_run_test {
  1411. my $child_pid;
  1412. my $child_exit;
  1413. my $line;
  1414. my $full_line;
  1415. my $bug = 0;
  1416. wait_for_monitor 1;
  1417. doprint "run test $run_test\n";
  1418. $child_done = 0;
  1419. $SIG{CHLD} = qw(child_finished);
  1420. $child_pid = fork;
  1421. child_run_test if (!$child_pid);
  1422. $full_line = "";
  1423. do {
  1424. $line = wait_for_input($monitor_fp, 1);
  1425. if (defined($line)) {
  1426. # we are not guaranteed to get a full line
  1427. $full_line .= $line;
  1428. doprint $line;
  1429. if ($full_line =~ /call trace:/i) {
  1430. $bug = 1;
  1431. }
  1432. if ($full_line =~ /Kernel panic -/) {
  1433. $bug = 1;
  1434. }
  1435. if ($line =~ /\n/) {
  1436. $full_line = "";
  1437. }
  1438. }
  1439. } while (!$child_done && !$bug);
  1440. if ($bug) {
  1441. my $failure_start = time;
  1442. my $now;
  1443. do {
  1444. $line = wait_for_input($monitor_fp, 1);
  1445. if (defined($line)) {
  1446. doprint $line;
  1447. }
  1448. $now = time;
  1449. if ($now - $failure_start >= $stop_after_failure) {
  1450. last;
  1451. }
  1452. } while (defined($line));
  1453. doprint "Detected kernel crash!\n";
  1454. # kill the child with extreme prejudice
  1455. kill 9, $child_pid;
  1456. }
  1457. waitpid $child_pid, 0;
  1458. $child_exit = $?;
  1459. if ($bug || $child_exit) {
  1460. return 0 if $in_bisect;
  1461. fail "test failed" and return 0;
  1462. }
  1463. return 1;
  1464. }
  1465. sub run_git_bisect {
  1466. my ($command) = @_;
  1467. doprint "$command ... ";
  1468. my $output = `$command 2>&1`;
  1469. my $ret = $?;
  1470. logit $output;
  1471. if ($ret) {
  1472. doprint "FAILED\n";
  1473. dodie "Failed to git bisect";
  1474. }
  1475. doprint "SUCCESS\n";
  1476. if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
  1477. doprint "$1 [$2]\n";
  1478. } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
  1479. $bisect_bad = $1;
  1480. doprint "Found bad commit... $1\n";
  1481. return 0;
  1482. } else {
  1483. # we already logged it, just print it now.
  1484. print $output;
  1485. }
  1486. return 1;
  1487. }
  1488. sub bisect_reboot {
  1489. doprint "Reboot and sleep $bisect_sleep_time seconds\n";
  1490. reboot $bisect_sleep_time;
  1491. }
  1492. # returns 1 on success, 0 on failure, -1 on skip
  1493. sub run_bisect_test {
  1494. my ($type, $buildtype) = @_;
  1495. my $failed = 0;
  1496. my $result;
  1497. my $output;
  1498. my $ret;
  1499. $in_bisect = 1;
  1500. build $buildtype or $failed = 1;
  1501. if ($type ne "build") {
  1502. if ($failed && $bisect_skip) {
  1503. $in_bisect = 0;
  1504. return -1;
  1505. }
  1506. dodie "Failed on build" if $failed;
  1507. # Now boot the box
  1508. start_monitor_and_boot or $failed = 1;
  1509. if ($type ne "boot") {
  1510. if ($failed && $bisect_skip) {
  1511. end_monitor;
  1512. bisect_reboot;
  1513. $in_bisect = 0;
  1514. return -1;
  1515. }
  1516. dodie "Failed on boot" if $failed;
  1517. do_run_test or $failed = 1;
  1518. }
  1519. end_monitor;
  1520. }
  1521. if ($failed) {
  1522. $result = 0;
  1523. } else {
  1524. $result = 1;
  1525. }
  1526. # reboot the box to a kernel we can ssh to
  1527. if ($type ne "build") {
  1528. bisect_reboot;
  1529. }
  1530. $in_bisect = 0;
  1531. return $result;
  1532. }
  1533. sub run_bisect {
  1534. my ($type) = @_;
  1535. my $buildtype = "oldconfig";
  1536. # We should have a minconfig to use?
  1537. if (defined($minconfig)) {
  1538. $buildtype = "useconfig:$minconfig";
  1539. }
  1540. my $ret = run_bisect_test $type, $buildtype;
  1541. if ($bisect_manual) {
  1542. $ret = answer_bisect;
  1543. }
  1544. # Are we looking for where it worked, not failed?
  1545. if ($reverse_bisect) {
  1546. $ret = !$ret;
  1547. }
  1548. if ($ret > 0) {
  1549. return "good";
  1550. } elsif ($ret == 0) {
  1551. return "bad";
  1552. } elsif ($bisect_skip) {
  1553. doprint "HIT A BAD COMMIT ... SKIPPING\n";
  1554. return "skip";
  1555. }
  1556. }
  1557. sub update_bisect_replay {
  1558. my $tmp_log = "$tmpdir/ktest_bisect_log";
  1559. run_command "git bisect log > $tmp_log" or
  1560. die "can't create bisect log";
  1561. return $tmp_log;
  1562. }
  1563. sub bisect {
  1564. my ($i) = @_;
  1565. my $result;
  1566. die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
  1567. die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
  1568. die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
  1569. my $good = $opt{"BISECT_GOOD[$i]"};
  1570. my $bad = $opt{"BISECT_BAD[$i]"};
  1571. my $type = $opt{"BISECT_TYPE[$i]"};
  1572. my $start = $opt{"BISECT_START[$i]"};
  1573. my $replay = $opt{"BISECT_REPLAY[$i]"};
  1574. my $start_files = $opt{"BISECT_FILES[$i]"};
  1575. if (defined($start_files)) {
  1576. $start_files = " -- " . $start_files;
  1577. } else {
  1578. $start_files = "";
  1579. }
  1580. # convert to true sha1's
  1581. $good = get_sha1($good);
  1582. $bad = get_sha1($bad);
  1583. if (defined($opt{"BISECT_REVERSE[$i]"}) &&
  1584. $opt{"BISECT_REVERSE[$i]"} == 1) {
  1585. doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
  1586. $reverse_bisect = 1;
  1587. } else {
  1588. $reverse_bisect = 0;
  1589. }
  1590. # Can't have a test without having a test to run
  1591. if ($type eq "test" && !defined($run_test)) {
  1592. $type = "boot";
  1593. }
  1594. # Check if a bisect was running
  1595. my $bisect_start_file = "$builddir/.git/BISECT_START";
  1596. my $check = $opt{"BISECT_CHECK[$i]"};
  1597. my $do_check = defined($check) && $check ne "0";
  1598. if ( -f $bisect_start_file ) {
  1599. print "Bisect in progress found\n";
  1600. if ($do_check) {
  1601. print " If you say yes, then no checks of good or bad will be done\n";
  1602. }
  1603. if (defined($replay)) {
  1604. print "** BISECT_REPLAY is defined in config file **";
  1605. print " Ignore config option and perform new git bisect log?\n";
  1606. if (read_ync " (yes, no, or cancel) ") {
  1607. $replay = update_bisect_replay;
  1608. $do_check = 0;
  1609. }
  1610. } elsif (read_yn "read git log and continue?") {
  1611. $replay = update_bisect_replay;
  1612. $do_check = 0;
  1613. }
  1614. }
  1615. if ($do_check) {
  1616. # get current HEAD
  1617. my $head = get_sha1("HEAD");
  1618. if ($check ne "good") {
  1619. doprint "TESTING BISECT BAD [$bad]\n";
  1620. run_command "git checkout $bad" or
  1621. die "Failed to checkout $bad";
  1622. $result = run_bisect $type;
  1623. if ($result ne "bad") {
  1624. fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
  1625. }
  1626. }
  1627. if ($check ne "bad") {
  1628. doprint "TESTING BISECT GOOD [$good]\n";
  1629. run_command "git checkout $good" or
  1630. die "Failed to checkout $good";
  1631. $result = run_bisect $type;
  1632. if ($result ne "good") {
  1633. fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
  1634. }
  1635. }
  1636. # checkout where we started
  1637. run_command "git checkout $head" or
  1638. die "Failed to checkout $head";
  1639. }
  1640. run_command "git bisect start$start_files" or
  1641. dodie "could not start bisect";
  1642. run_command "git bisect good $good" or
  1643. dodie "could not set bisect good to $good";
  1644. run_git_bisect "git bisect bad $bad" or
  1645. dodie "could not set bisect bad to $bad";
  1646. if (defined($replay)) {
  1647. run_command "git bisect replay $replay" or
  1648. dodie "failed to run replay";
  1649. }
  1650. if (defined($start)) {
  1651. run_command "git checkout $start" or
  1652. dodie "failed to checkout $start";
  1653. }
  1654. my $test;
  1655. do {
  1656. $result = run_bisect $type;
  1657. $test = run_git_bisect "git bisect $result";
  1658. } while ($test);
  1659. run_command "git bisect log" or
  1660. dodie "could not capture git bisect log";
  1661. run_command "git bisect reset" or
  1662. dodie "could not reset git bisect";
  1663. doprint "Bad commit was [$bisect_bad]\n";
  1664. success $i;
  1665. }
  1666. my %config_ignore;
  1667. my %config_set;
  1668. my %config_list;
  1669. my %null_config;
  1670. my %dependency;
  1671. sub assign_configs {
  1672. my ($hash, $config) = @_;
  1673. open (IN, $config)
  1674. or dodie "Failed to read $config";
  1675. while (<IN>) {
  1676. if (/^((CONFIG\S*)=.*)/) {
  1677. ${$hash}{$2} = $1;
  1678. }
  1679. }
  1680. close(IN);
  1681. }
  1682. sub process_config_ignore {
  1683. my ($config) = @_;
  1684. assign_configs \%config_ignore, $config;
  1685. }
  1686. sub read_current_config {
  1687. my ($config_ref) = @_;
  1688. %{$config_ref} = ();
  1689. undef %{$config_ref};
  1690. my @key = keys %{$config_ref};
  1691. if ($#key >= 0) {
  1692. print "did not delete!\n";
  1693. exit;
  1694. }
  1695. open (IN, "$output_config");
  1696. while (<IN>) {
  1697. if (/^(CONFIG\S+)=(.*)/) {
  1698. ${$config_ref}{$1} = $2;
  1699. }
  1700. }
  1701. close(IN);
  1702. }
  1703. sub get_dependencies {
  1704. my ($config) = @_;
  1705. my $arr = $dependency{$config};
  1706. if (!defined($arr)) {
  1707. return ();
  1708. }
  1709. my @deps = @{$arr};
  1710. foreach my $dep (@{$arr}) {
  1711. print "ADD DEP $dep\n";
  1712. @deps = (@deps, get_dependencies $dep);
  1713. }
  1714. return @deps;
  1715. }
  1716. sub create_config {
  1717. my @configs = @_;
  1718. open(OUT, ">$output_config") or dodie "Can not write to $output_config";
  1719. foreach my $config (@configs) {
  1720. print OUT "$config_set{$config}\n";
  1721. my @deps = get_dependencies $config;
  1722. foreach my $dep (@deps) {
  1723. print OUT "$config_set{$dep}\n";
  1724. }
  1725. }
  1726. foreach my $config (keys %config_ignore) {
  1727. print OUT "$config_ignore{$config}\n";
  1728. }
  1729. close(OUT);
  1730. # exit;
  1731. make_oldconfig;
  1732. }
  1733. sub compare_configs {
  1734. my (%a, %b) = @_;
  1735. foreach my $item (keys %a) {
  1736. if (!defined($b{$item})) {
  1737. print "diff $item\n";
  1738. return 1;
  1739. }
  1740. delete $b{$item};
  1741. }
  1742. my @keys = keys %b;
  1743. if ($#keys) {
  1744. print "diff2 $keys[0]\n";
  1745. }
  1746. return -1 if ($#keys >= 0);
  1747. return 0;
  1748. }
  1749. sub run_config_bisect_test {
  1750. my ($type) = @_;
  1751. return run_bisect_test $type, "oldconfig";
  1752. }
  1753. sub process_passed {
  1754. my (%configs) = @_;
  1755. doprint "These configs had no failure: (Enabling them for further compiles)\n";
  1756. # Passed! All these configs are part of a good compile.
  1757. # Add them to the min options.
  1758. foreach my $config (keys %configs) {
  1759. if (defined($config_list{$config})) {
  1760. doprint " removing $config\n";
  1761. $config_ignore{$config} = $config_list{$config};
  1762. delete $config_list{$config};
  1763. }
  1764. }
  1765. doprint "config copied to $outputdir/config_good\n";
  1766. run_command "cp -f $output_config $outputdir/config_good";
  1767. }
  1768. sub process_failed {
  1769. my ($config) = @_;
  1770. doprint "\n\n***************************************\n";
  1771. doprint "Found bad config: $config\n";
  1772. doprint "***************************************\n\n";
  1773. }
  1774. sub run_config_bisect {
  1775. my @start_list = keys %config_list;
  1776. if ($#start_list < 0) {
  1777. doprint "No more configs to test!!!\n";
  1778. return -1;
  1779. }
  1780. doprint "***** RUN TEST ***\n";
  1781. my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
  1782. my $ret;
  1783. my %current_config;
  1784. my $count = $#start_list + 1;
  1785. doprint " $count configs to test\n";
  1786. my $half = int($#start_list / 2);
  1787. do {
  1788. my @tophalf = @start_list[0 .. $half];
  1789. create_config @tophalf;
  1790. read_current_config \%current_config;
  1791. $count = $#tophalf + 1;
  1792. doprint "Testing $count configs\n";
  1793. my $found = 0;
  1794. # make sure we test something
  1795. foreach my $config (@tophalf) {
  1796. if (defined($current_config{$config})) {
  1797. logit " $config\n";
  1798. $found = 1;
  1799. }
  1800. }
  1801. if (!$found) {
  1802. # try the other half
  1803. doprint "Top half produced no set configs, trying bottom half\n";
  1804. @tophalf = @start_list[$half + 1 .. $#start_list];
  1805. create_config @tophalf;
  1806. read_current_config \%current_config;
  1807. foreach my $config (@tophalf) {
  1808. if (defined($current_config{$config})) {
  1809. logit " $config\n";
  1810. $found = 1;
  1811. }
  1812. }
  1813. if (!$found) {
  1814. doprint "Failed: Can't make new config with current configs\n";
  1815. foreach my $config (@start_list) {
  1816. doprint " CONFIG: $config\n";
  1817. }
  1818. return -1;
  1819. }
  1820. $count = $#tophalf + 1;
  1821. doprint "Testing $count configs\n";
  1822. }
  1823. $ret = run_config_bisect_test $type;
  1824. if ($bisect_manual) {
  1825. $ret = answer_bisect;
  1826. }
  1827. if ($ret) {
  1828. process_passed %current_config;
  1829. return 0;
  1830. }
  1831. doprint "This config had a failure.\n";
  1832. doprint "Removing these configs that were not set in this config:\n";
  1833. doprint "config copied to $outputdir/config_bad\n";
  1834. run_command "cp -f $output_config $outputdir/config_bad";
  1835. # A config exists in this group that was bad.
  1836. foreach my $config (keys %config_list) {
  1837. if (!defined($current_config{$config})) {
  1838. doprint " removing $config\n";
  1839. delete $config_list{$config};
  1840. }
  1841. }
  1842. @start_list = @tophalf;
  1843. if ($#start_list == 0) {
  1844. process_failed $start_list[0];
  1845. return 1;
  1846. }
  1847. # remove half the configs we are looking at and see if
  1848. # they are good.
  1849. $half = int($#start_list / 2);
  1850. } while ($#start_list > 0);
  1851. # we found a single config, try it again unless we are running manually
  1852. if ($bisect_manual) {
  1853. process_failed $start_list[0];
  1854. return 1;
  1855. }
  1856. my @tophalf = @start_list[0 .. 0];
  1857. $ret = run_config_bisect_test $type;
  1858. if ($ret) {
  1859. process_passed %current_config;
  1860. return 0;
  1861. }
  1862. process_failed $start_list[0];
  1863. return 1;
  1864. }
  1865. sub config_bisect {
  1866. my ($i) = @_;
  1867. my $start_config = $opt{"CONFIG_BISECT[$i]"};
  1868. my $tmpconfig = "$tmpdir/use_config";
  1869. if (defined($config_bisect_good)) {
  1870. process_config_ignore $config_bisect_good;
  1871. }
  1872. # Make the file with the bad config and the min config
  1873. if (defined($minconfig)) {
  1874. # read the min config for things to ignore
  1875. run_command "cp $minconfig $tmpconfig" or
  1876. dodie "failed to copy $minconfig to $tmpconfig";
  1877. } else {
  1878. unlink $tmpconfig;
  1879. }
  1880. if (-f $tmpconfig) {
  1881. load_force_config($tmpconfig);
  1882. process_config_ignore $tmpconfig;
  1883. }
  1884. # now process the start config
  1885. run_command "cp $start_config $output_config" or
  1886. dodie "failed to copy $start_config to $output_config";
  1887. # read directly what we want to check
  1888. my %config_check;
  1889. open (IN, $output_config)
  1890. or dodie "faied to open $output_config";
  1891. while (<IN>) {
  1892. if (/^((CONFIG\S*)=.*)/) {
  1893. $config_check{$2} = $1;
  1894. }
  1895. }
  1896. close(IN);
  1897. # Now run oldconfig with the minconfig
  1898. make_oldconfig;
  1899. # check to see what we lost (or gained)
  1900. open (IN, $output_config)
  1901. or dodie "Failed to read $start_config";
  1902. my %removed_configs;
  1903. my %added_configs;
  1904. while (<IN>) {
  1905. if (/^((CONFIG\S*)=.*)/) {
  1906. # save off all options
  1907. $config_set{$2} = $1;
  1908. if (defined($config_check{$2})) {
  1909. if (defined($config_ignore{$2})) {
  1910. $removed_configs{$2} = $1;
  1911. } else {
  1912. $config_list{$2} = $1;
  1913. }
  1914. } elsif (!defined($config_ignore{$2})) {
  1915. $added_configs{$2} = $1;
  1916. $config_list{$2} = $1;
  1917. }
  1918. }
  1919. }
  1920. close(IN);
  1921. my @confs = keys %removed_configs;
  1922. if ($#confs >= 0) {
  1923. doprint "Configs overridden by default configs and removed from check:\n";
  1924. foreach my $config (@confs) {
  1925. doprint " $config\n";
  1926. }
  1927. }
  1928. @confs = keys %added_configs;
  1929. if ($#confs >= 0) {
  1930. doprint "Configs appearing in make oldconfig and added:\n";
  1931. foreach my $config (@confs) {
  1932. doprint " $config\n";
  1933. }
  1934. }
  1935. my %config_test;
  1936. my $once = 0;
  1937. # Sometimes kconfig does weird things. We must make sure
  1938. # that the config we autocreate has everything we need
  1939. # to test, otherwise we may miss testing configs, or
  1940. # may not be able to create a new config.
  1941. # Here we create a config with everything set.
  1942. create_config (keys %config_list);
  1943. read_current_config \%config_test;
  1944. foreach my $config (keys %config_list) {
  1945. if (!defined($config_test{$config})) {
  1946. if (!$once) {
  1947. $once = 1;
  1948. doprint "Configs not produced by kconfig (will not be checked):\n";
  1949. }
  1950. doprint " $config\n";
  1951. delete $config_list{$config};
  1952. }
  1953. }
  1954. my $ret;
  1955. do {
  1956. $ret = run_config_bisect;
  1957. } while (!$ret);
  1958. return $ret if ($ret < 0);
  1959. success $i;
  1960. }
  1961. sub patchcheck_reboot {
  1962. doprint "Reboot and sleep $patchcheck_sleep_time seconds\n";
  1963. reboot $patchcheck_sleep_time;
  1964. }
  1965. sub patchcheck {
  1966. my ($i) = @_;
  1967. die "PATCHCHECK_START[$i] not defined\n"
  1968. if (!defined($opt{"PATCHCHECK_START[$i]"}));
  1969. die "PATCHCHECK_TYPE[$i] not defined\n"
  1970. if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
  1971. my $start = $opt{"PATCHCHECK_START[$i]"};
  1972. my $end = "HEAD";
  1973. if (defined($opt{"PATCHCHECK_END[$i]"})) {
  1974. $end = $opt{"PATCHCHECK_END[$i]"};
  1975. }
  1976. # Get the true sha1's since we can use things like HEAD~3
  1977. $start = get_sha1($start);
  1978. $end = get_sha1($end);
  1979. my $type = $opt{"PATCHCHECK_TYPE[$i]"};
  1980. # Can't have a test without having a test to run
  1981. if ($type eq "test" && !defined($run_test)) {
  1982. $type = "boot";
  1983. }
  1984. open (IN, "git log --pretty=oneline $end|") or
  1985. dodie "could not get git list";
  1986. my @list;
  1987. while (<IN>) {
  1988. chomp;
  1989. $list[$#list+1] = $_;
  1990. last if (/^$start/);
  1991. }
  1992. close(IN);
  1993. if ($list[$#list] !~ /^$start/) {
  1994. fail "SHA1 $start not found";
  1995. }
  1996. # go backwards in the list
  1997. @list = reverse @list;
  1998. my $save_clean = $noclean;
  1999. my %ignored_warnings;
  2000. if (defined($ignore_warnings)) {
  2001. foreach my $sha1 (split /\s+/, $ignore_warnings) {
  2002. $ignored_warnings{$sha1} = 1;
  2003. }
  2004. }
  2005. $in_patchcheck = 1;
  2006. foreach my $item (@list) {
  2007. my $sha1 = $item;
  2008. $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
  2009. doprint "\nProcessing commit $item\n\n";
  2010. run_command "git checkout $sha1" or
  2011. die "Failed to checkout $sha1";
  2012. # only clean on the first and last patch
  2013. if ($item eq $list[0] ||
  2014. $item eq $list[$#list]) {
  2015. $noclean = $save_clean;
  2016. } else {
  2017. $noclean = 1;
  2018. }
  2019. if (defined($minconfig)) {
  2020. build "useconfig:$minconfig" or return 0;
  2021. } else {
  2022. # ?? no config to use?
  2023. build "oldconfig" or return 0;
  2024. }
  2025. if (!defined($ignored_warnings{$sha1})) {
  2026. check_buildlog $sha1 or return 0;
  2027. }
  2028. next if ($type eq "build");
  2029. my $failed = 0;
  2030. start_monitor_and_boot or $failed = 1;
  2031. if (!$failed && $type ne "boot"){
  2032. do_run_test or $failed = 1;
  2033. }
  2034. end_monitor;
  2035. return 0 if ($failed);
  2036. patchcheck_reboot;
  2037. }
  2038. $in_patchcheck = 0;
  2039. success $i;
  2040. return 1;
  2041. }
  2042. my %depends;
  2043. my %depcount;
  2044. my $iflevel = 0;
  2045. my @ifdeps;
  2046. # prevent recursion
  2047. my %read_kconfigs;
  2048. sub add_dep {
  2049. # $config depends on $dep
  2050. my ($config, $dep) = @_;
  2051. if (defined($depends{$config})) {
  2052. $depends{$config} .= " " . $dep;
  2053. } else {
  2054. $depends{$config} = $dep;
  2055. }
  2056. # record the number of configs depending on $dep
  2057. if (defined $depcount{$dep}) {
  2058. $depcount{$dep}++;
  2059. } else {
  2060. $depcount{$dep} = 1;
  2061. }
  2062. }
  2063. # taken from streamline_config.pl
  2064. sub read_kconfig {
  2065. my ($kconfig) = @_;
  2066. my $state = "NONE";
  2067. my $config;
  2068. my @kconfigs;
  2069. my $cont = 0;
  2070. my $line;
  2071. if (! -f $kconfig) {
  2072. doprint "file $kconfig does not exist, skipping\n";
  2073. return;
  2074. }
  2075. open(KIN, "$kconfig")
  2076. or die "Can't open $kconfig";
  2077. while (<KIN>) {
  2078. chomp;
  2079. # Make sure that lines ending with \ continue
  2080. if ($cont) {
  2081. $_ = $line . " " . $_;
  2082. }
  2083. if (s/\\$//) {
  2084. $cont = 1;
  2085. $line = $_;
  2086. next;
  2087. }
  2088. $cont = 0;
  2089. # collect any Kconfig sources
  2090. if (/^source\s*"(.*)"/) {
  2091. $kconfigs[$#kconfigs+1] = $1;
  2092. }
  2093. # configs found
  2094. if (/^\s*(menu)?config\s+(\S+)\s*$/) {
  2095. $state = "NEW";
  2096. $config = $2;
  2097. for (my $i = 0; $i < $iflevel; $i++) {
  2098. add_dep $config, $ifdeps[$i];
  2099. }
  2100. # collect the depends for the config
  2101. } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
  2102. add_dep $config, $1;
  2103. # Get the configs that select this config
  2104. } elsif ($state eq "NEW" && /^\s*select\s+(\S+)/) {
  2105. # selected by depends on config
  2106. add_dep $1, $config;
  2107. # Check for if statements
  2108. } elsif (/^if\s+(.*\S)\s*$/) {
  2109. my $deps = $1;
  2110. # remove beginning and ending non text
  2111. $deps =~ s/^[^a-zA-Z0-9_]*//;
  2112. $deps =~ s/[^a-zA-Z0-9_]*$//;
  2113. my @deps = split /[^a-zA-Z0-9_]+/, $deps;
  2114. $ifdeps[$iflevel++] = join ':', @deps;
  2115. } elsif (/^endif/) {
  2116. $iflevel-- if ($iflevel);
  2117. # stop on "help"
  2118. } elsif (/^\s*help\s*$/) {
  2119. $state = "NONE";
  2120. }
  2121. }
  2122. close(KIN);
  2123. # read in any configs that were found.
  2124. foreach $kconfig (@kconfigs) {
  2125. if (!defined($read_kconfigs{$kconfig})) {
  2126. $read_kconfigs{$kconfig} = 1;
  2127. read_kconfig("$builddir/$kconfig");
  2128. }
  2129. }
  2130. }
  2131. sub read_depends {
  2132. # find out which arch this is by the kconfig file
  2133. open (IN, $output_config)
  2134. or dodie "Failed to read $output_config";
  2135. my $arch;
  2136. while (<IN>) {
  2137. if (m,Linux/(\S+)\s+\S+\s+Kernel Configuration,) {
  2138. $arch = $1;
  2139. last;
  2140. }
  2141. }
  2142. close IN;
  2143. if (!defined($arch)) {
  2144. doprint "Could not find arch from config file\n";
  2145. doprint "no dependencies used\n";
  2146. return;
  2147. }
  2148. # arch is really the subarch, we need to know
  2149. # what directory to look at.
  2150. if ($arch eq "i386" || $arch eq "x86_64") {
  2151. $arch = "x86";
  2152. } elsif ($arch =~ /^tile/) {
  2153. $arch = "tile";
  2154. }
  2155. my $kconfig = "$builddir/arch/$arch/Kconfig";
  2156. if (! -f $kconfig && $arch =~ /\d$/) {
  2157. my $orig = $arch;
  2158. # some subarchs have numbers, truncate them
  2159. $arch =~ s/\d*$//;
  2160. $kconfig = "$builddir/arch/$arch/Kconfig";
  2161. if (! -f $kconfig) {
  2162. doprint "No idea what arch dir $orig is for\n";
  2163. doprint "no dependencies used\n";
  2164. return;
  2165. }
  2166. }
  2167. read_kconfig($kconfig);
  2168. }
  2169. sub read_config_list {
  2170. my ($config) = @_;
  2171. open (IN, $config)
  2172. or dodie "Failed to read $config";
  2173. while (<IN>) {
  2174. if (/^((CONFIG\S*)=.*)/) {
  2175. if (!defined($config_ignore{$2})) {
  2176. $config_list{$2} = $1;
  2177. }
  2178. }
  2179. }
  2180. close(IN);
  2181. }
  2182. sub read_output_config {
  2183. my ($config) = @_;
  2184. assign_configs \%config_ignore, $config;
  2185. }
  2186. sub make_new_config {
  2187. my @configs = @_;
  2188. open (OUT, ">$output_config")
  2189. or dodie "Failed to write $output_config";
  2190. foreach my $config (@configs) {
  2191. print OUT "$config\n";
  2192. }
  2193. close OUT;
  2194. }
  2195. sub chomp_config {
  2196. my ($config) = @_;
  2197. $config =~ s/CONFIG_//;
  2198. return $config;
  2199. }
  2200. sub get_depends {
  2201. my ($dep) = @_;
  2202. my $kconfig = chomp_config $dep;
  2203. $dep = $depends{"$kconfig"};
  2204. # the dep string we have saves the dependencies as they
  2205. # were found, including expressions like ! && ||. We
  2206. # want to split this out into just an array of configs.
  2207. my $valid = "A-Za-z_0-9";
  2208. my @configs;
  2209. while ($dep =~ /[$valid]/) {
  2210. if ($dep =~ /^[^$valid]*([$valid]+)/) {
  2211. my $conf = "CONFIG_" . $1;
  2212. $configs[$#configs + 1] = $conf;
  2213. $dep =~ s/^[^$valid]*[$valid]+//;
  2214. } else {
  2215. die "this should never happen";
  2216. }
  2217. }
  2218. return @configs;
  2219. }
  2220. my %min_configs;
  2221. my %keep_configs;
  2222. my %save_configs;
  2223. my %processed_configs;
  2224. my %nochange_config;
  2225. sub test_this_config {
  2226. my ($config) = @_;
  2227. my $found;
  2228. # if we already processed this config, skip it
  2229. if (defined($processed_configs{$config})) {
  2230. return undef;
  2231. }
  2232. $processed_configs{$config} = 1;
  2233. # if this config failed during this round, skip it
  2234. if (defined($nochange_config{$config})) {
  2235. return undef;
  2236. }
  2237. my $kconfig = chomp_config $config;
  2238. # Test dependencies first
  2239. if (defined($depends{"$kconfig"})) {
  2240. my @parents = get_depends $config;
  2241. foreach my $parent (@parents) {
  2242. # if the parent is in the min config, check it first
  2243. next if (!defined($min_configs{$parent}));
  2244. $found = test_this_config($parent);
  2245. if (defined($found)) {
  2246. return $found;
  2247. }
  2248. }
  2249. }
  2250. # Remove this config from the list of configs
  2251. # do a make oldnoconfig and then read the resulting
  2252. # .config to make sure it is missing the config that
  2253. # we had before
  2254. my %configs = %min_configs;
  2255. delete $configs{$config};
  2256. make_new_config ((values %configs), (values %keep_configs));
  2257. make_oldconfig;
  2258. undef %configs;
  2259. assign_configs \%configs, $output_config;
  2260. return $config if (!defined($configs{$config}));
  2261. doprint "disabling config $config did not change .config\n";
  2262. $nochange_config{$config} = 1;
  2263. return undef;
  2264. }
  2265. sub make_min_config {
  2266. my ($i) = @_;
  2267. if (!defined($output_minconfig)) {
  2268. fail "OUTPUT_MIN_CONFIG not defined" and return;
  2269. }
  2270. # If output_minconfig exists, and the start_minconfig
  2271. # came from min_config, than ask if we should use
  2272. # that instead.
  2273. if (-f $output_minconfig && !$start_minconfig_defined) {
  2274. print "$output_minconfig exists\n";
  2275. if (read_yn " Use it as minconfig?") {
  2276. $start_minconfig = $output_minconfig;
  2277. }
  2278. }
  2279. if (!defined($start_minconfig)) {
  2280. fail "START_MIN_CONFIG or MIN_CONFIG not defined" and return;
  2281. }
  2282. my $temp_config = "$tmpdir/temp_config";
  2283. # First things first. We build an allnoconfig to find
  2284. # out what the defaults are that we can't touch.
  2285. # Some are selections, but we really can't handle selections.
  2286. my $save_minconfig = $minconfig;
  2287. undef $minconfig;
  2288. run_command "$make allnoconfig" or return 0;
  2289. read_depends;
  2290. process_config_ignore $output_config;
  2291. undef %save_configs;
  2292. undef %min_configs;
  2293. if (defined($ignore_config)) {
  2294. # make sure the file exists
  2295. `touch $ignore_config`;
  2296. assign_configs \%save_configs, $ignore_config;
  2297. }
  2298. %keep_configs = %save_configs;
  2299. doprint "Load initial configs from $start_minconfig\n";
  2300. # Look at the current min configs, and save off all the
  2301. # ones that were set via the allnoconfig
  2302. assign_configs \%min_configs, $start_minconfig;
  2303. my @config_keys = keys %min_configs;
  2304. # All configs need a depcount
  2305. foreach my $config (@config_keys) {
  2306. my $kconfig = chomp_config $config;
  2307. if (!defined $depcount{$kconfig}) {
  2308. $depcount{$kconfig} = 0;
  2309. }
  2310. }
  2311. # Remove anything that was set by the make allnoconfig
  2312. # we shouldn't need them as they get set for us anyway.
  2313. foreach my $config (@config_keys) {
  2314. # Remove anything in the ignore_config
  2315. if (defined($keep_configs{$config})) {
  2316. my $file = $ignore_config;
  2317. $file =~ s,.*/(.*?)$,$1,;
  2318. doprint "$config set by $file ... ignored\n";
  2319. delete $min_configs{$config};
  2320. next;
  2321. }
  2322. # But make sure the settings are the same. If a min config
  2323. # sets a selection, we do not want to get rid of it if
  2324. # it is not the same as what we have. Just move it into
  2325. # the keep configs.
  2326. if (defined($config_ignore{$config})) {
  2327. if ($config_ignore{$config} ne $min_configs{$config}) {
  2328. doprint "$config is in allnoconfig as '$config_ignore{$config}'";
  2329. doprint " but it is '$min_configs{$config}' in minconfig .. keeping\n";
  2330. $keep_configs{$config} = $min_configs{$config};
  2331. } else {
  2332. doprint "$config set by allnoconfig ... ignored\n";
  2333. }
  2334. delete $min_configs{$config};
  2335. }
  2336. }
  2337. my $done = 0;
  2338. my $take_two = 0;
  2339. while (!$done) {
  2340. my $config;
  2341. my $found;
  2342. # Now disable each config one by one and do a make oldconfig
  2343. # till we find a config that changes our list.
  2344. my @test_configs = keys %min_configs;
  2345. # Sort keys by who is most dependent on
  2346. @test_configs = sort { $depcount{chomp_config($b)} <=> $depcount{chomp_config($a)} }
  2347. @test_configs ;
  2348. # Put configs that did not modify the config at the end.
  2349. my $reset = 1;
  2350. for (my $i = 0; $i < $#test_configs; $i++) {
  2351. if (!defined($nochange_config{$test_configs[0]})) {
  2352. $reset = 0;
  2353. last;
  2354. }
  2355. # This config didn't change the .config last time.
  2356. # Place it at the end
  2357. my $config = shift @test_configs;
  2358. push @test_configs, $config;
  2359. }
  2360. # if every test config has failed to modify the .config file
  2361. # in the past, then reset and start over.
  2362. if ($reset) {
  2363. undef %nochange_config;
  2364. }
  2365. undef %processed_configs;
  2366. foreach my $config (@test_configs) {
  2367. $found = test_this_config $config;
  2368. last if (defined($found));
  2369. # oh well, try another config
  2370. }
  2371. if (!defined($found)) {
  2372. # we could have failed due to the nochange_config hash
  2373. # reset and try again
  2374. if (!$take_two) {
  2375. undef %nochange_config;
  2376. $take_two = 1;
  2377. next;
  2378. }
  2379. doprint "No more configs found that we can disable\n";
  2380. $done = 1;
  2381. last;
  2382. }
  2383. $take_two = 0;
  2384. $config = $found;
  2385. doprint "Test with $config disabled\n";
  2386. # set in_bisect to keep build and monitor from dieing
  2387. $in_bisect = 1;
  2388. my $failed = 0;
  2389. build "oldconfig";
  2390. start_monitor_and_boot or $failed = 1;
  2391. end_monitor;
  2392. $in_bisect = 0;
  2393. if ($failed) {
  2394. doprint "$min_configs{$config} is needed to boot the box... keeping\n";
  2395. # this config is needed, add it to the ignore list.
  2396. $keep_configs{$config} = $min_configs{$config};
  2397. $save_configs{$config} = $min_configs{$config};
  2398. delete $min_configs{$config};
  2399. # update new ignore configs
  2400. if (defined($ignore_config)) {
  2401. open (OUT, ">$temp_config")
  2402. or die "Can't write to $temp_config";
  2403. foreach my $config (keys %save_configs) {
  2404. print OUT "$save_configs{$config}\n";
  2405. }
  2406. close OUT;
  2407. run_command "mv $temp_config $ignore_config" or
  2408. dodie "failed to copy update to $ignore_config";
  2409. }
  2410. } else {
  2411. # We booted without this config, remove it from the minconfigs.
  2412. doprint "$config is not needed, disabling\n";
  2413. delete $min_configs{$config};
  2414. # Also disable anything that is not enabled in this config
  2415. my %configs;
  2416. assign_configs \%configs, $output_config;
  2417. my @config_keys = keys %min_configs;
  2418. foreach my $config (@config_keys) {
  2419. if (!defined($configs{$config})) {
  2420. doprint "$config is not set, disabling\n";
  2421. delete $min_configs{$config};
  2422. }
  2423. }
  2424. # Save off all the current mandidory configs
  2425. open (OUT, ">$temp_config")
  2426. or die "Can't write to $temp_config";
  2427. foreach my $config (keys %keep_configs) {
  2428. print OUT "$keep_configs{$config}\n";
  2429. }
  2430. foreach my $config (keys %min_configs) {
  2431. print OUT "$min_configs{$config}\n";
  2432. }
  2433. close OUT;
  2434. run_command "mv $temp_config $output_minconfig" or
  2435. dodie "failed to copy update to $output_minconfig";
  2436. }
  2437. doprint "Reboot and wait $sleep_time seconds\n";
  2438. reboot $sleep_time;
  2439. }
  2440. success $i;
  2441. return 1;
  2442. }
  2443. $#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
  2444. if ($#ARGV == 0) {
  2445. $ktest_config = $ARGV[0];
  2446. if (! -f $ktest_config) {
  2447. print "$ktest_config does not exist.\n";
  2448. if (!read_yn "Create it?") {
  2449. exit 0;
  2450. }
  2451. }
  2452. } else {
  2453. $ktest_config = "ktest.conf";
  2454. }
  2455. if (! -f $ktest_config) {
  2456. open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
  2457. print OUT << "EOF"
  2458. # Generated by ktest.pl
  2459. #
  2460. # PWD is a ktest.pl variable that will result in the process working
  2461. # directory that ktest.pl is executed in.
  2462. # THIS_DIR is automatically assigned the PWD of the path that generated
  2463. # the config file. It is best to use this variable when assigning other
  2464. # directory paths within this directory. This allows you to easily
  2465. # move the test cases to other locations or to other machines.
  2466. #
  2467. THIS_DIR := $variable{"PWD"}
  2468. # Define each test with TEST_START
  2469. # The config options below it will override the defaults
  2470. TEST_START
  2471. DEFAULTS
  2472. EOF
  2473. ;
  2474. close(OUT);
  2475. }
  2476. read_config $ktest_config;
  2477. if (defined($opt{"LOG_FILE"})) {
  2478. $opt{"LOG_FILE"} = eval_option($opt{"LOG_FILE"}, -1);
  2479. }
  2480. # Append any configs entered in manually to the config file.
  2481. my @new_configs = keys %entered_configs;
  2482. if ($#new_configs >= 0) {
  2483. print "\nAppending entered in configs to $ktest_config\n";
  2484. open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
  2485. foreach my $config (@new_configs) {
  2486. print OUT "$config = $entered_configs{$config}\n";
  2487. $opt{$config} = process_variables($entered_configs{$config});
  2488. }
  2489. }
  2490. if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
  2491. unlink $opt{"LOG_FILE"};
  2492. }
  2493. doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
  2494. for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
  2495. if (!$i) {
  2496. doprint "DEFAULT OPTIONS:\n";
  2497. } else {
  2498. doprint "\nTEST $i OPTIONS";
  2499. if (defined($repeat_tests{$i})) {
  2500. $repeat = $repeat_tests{$i};
  2501. doprint " ITERATE $repeat";
  2502. }
  2503. doprint "\n";
  2504. }
  2505. foreach my $option (sort keys %opt) {
  2506. if ($option =~ /\[(\d+)\]$/) {
  2507. next if ($i != $1);
  2508. } else {
  2509. next if ($i);
  2510. }
  2511. doprint "$option = $opt{$option}\n";
  2512. }
  2513. }
  2514. sub __set_test_option {
  2515. my ($name, $i) = @_;
  2516. my $option = "$name\[$i\]";
  2517. if (defined($opt{$option})) {
  2518. return $opt{$option};
  2519. }
  2520. foreach my $test (keys %repeat_tests) {
  2521. if ($i >= $test &&
  2522. $i < $test + $repeat_tests{$test}) {
  2523. $option = "$name\[$test\]";
  2524. if (defined($opt{$option})) {
  2525. return $opt{$option};
  2526. }
  2527. }
  2528. }
  2529. if (defined($opt{$name})) {
  2530. return $opt{$name};
  2531. }
  2532. return undef;
  2533. }
  2534. sub set_test_option {
  2535. my ($name, $i) = @_;
  2536. my $option = __set_test_option($name, $i);
  2537. return $option if (!defined($option));
  2538. return eval_option($option, $i);
  2539. }
  2540. # First we need to do is the builds
  2541. for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
  2542. # Do not reboot on failing test options
  2543. $no_reboot = 1;
  2544. $iteration = $i;
  2545. my $makecmd = set_test_option("MAKE_CMD", $i);
  2546. $machine = set_test_option("MACHINE", $i);
  2547. $ssh_user = set_test_option("SSH_USER", $i);
  2548. $tmpdir = set_test_option("TMP_DIR", $i);
  2549. $outputdir = set_test_option("OUTPUT_DIR", $i);
  2550. $builddir = set_test_option("BUILD_DIR", $i);
  2551. $test_type = set_test_option("TEST_TYPE", $i);
  2552. $build_type = set_test_option("BUILD_TYPE", $i);
  2553. $build_options = set_test_option("BUILD_OPTIONS", $i);
  2554. $pre_build = set_test_option("PRE_BUILD", $i);
  2555. $post_build = set_test_option("POST_BUILD", $i);
  2556. $pre_build_die = set_test_option("PRE_BUILD_DIE", $i);
  2557. $post_build_die = set_test_option("POST_BUILD_DIE", $i);
  2558. $power_cycle = set_test_option("POWER_CYCLE", $i);
  2559. $reboot = set_test_option("REBOOT", $i);
  2560. $noclean = set_test_option("BUILD_NOCLEAN", $i);
  2561. $minconfig = set_test_option("MIN_CONFIG", $i);
  2562. $output_minconfig = set_test_option("OUTPUT_MIN_CONFIG", $i);
  2563. $start_minconfig = set_test_option("START_MIN_CONFIG", $i);
  2564. $ignore_config = set_test_option("IGNORE_CONFIG", $i);
  2565. $run_test = set_test_option("TEST", $i);
  2566. $addconfig = set_test_option("ADD_CONFIG", $i);
  2567. $reboot_type = set_test_option("REBOOT_TYPE", $i);
  2568. $grub_menu = set_test_option("GRUB_MENU", $i);
  2569. $post_install = set_test_option("POST_INSTALL", $i);
  2570. $no_install = set_test_option("NO_INSTALL", $i);
  2571. $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
  2572. $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
  2573. $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
  2574. $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
  2575. $power_off = set_test_option("POWER_OFF", $i);
  2576. $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
  2577. $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
  2578. $sleep_time = set_test_option("SLEEP_TIME", $i);
  2579. $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
  2580. $patchcheck_sleep_time = set_test_option("PATCHCHECK_SLEEP_TIME", $i);
  2581. $ignore_warnings = set_test_option("IGNORE_WARNINGS", $i);
  2582. $bisect_manual = set_test_option("BISECT_MANUAL", $i);
  2583. $bisect_skip = set_test_option("BISECT_SKIP", $i);
  2584. $config_bisect_good = set_test_option("CONFIG_BISECT_GOOD", $i);
  2585. $store_failures = set_test_option("STORE_FAILURES", $i);
  2586. $store_successes = set_test_option("STORE_SUCCESSES", $i);
  2587. $test_name = set_test_option("TEST_NAME", $i);
  2588. $timeout = set_test_option("TIMEOUT", $i);
  2589. $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
  2590. $console = set_test_option("CONSOLE", $i);
  2591. $detect_triplefault = set_test_option("DETECT_TRIPLE_FAULT", $i);
  2592. $success_line = set_test_option("SUCCESS_LINE", $i);
  2593. $reboot_success_line = set_test_option("REBOOT_SUCCESS_LINE", $i);
  2594. $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
  2595. $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
  2596. $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
  2597. $build_target = set_test_option("BUILD_TARGET", $i);
  2598. $ssh_exec = set_test_option("SSH_EXEC", $i);
  2599. $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
  2600. $target_image = set_test_option("TARGET_IMAGE", $i);
  2601. $localversion = set_test_option("LOCALVERSION", $i);
  2602. $start_minconfig_defined = 1;
  2603. if (!defined($start_minconfig)) {
  2604. $start_minconfig_defined = 0;
  2605. $start_minconfig = $minconfig;
  2606. }
  2607. chdir $builddir || die "can't change directory to $builddir";
  2608. foreach my $dir ($tmpdir, $outputdir) {
  2609. if (!-d $dir) {
  2610. mkpath($dir) or
  2611. die "can't create $dir";
  2612. }
  2613. }
  2614. $ENV{"SSH_USER"} = $ssh_user;
  2615. $ENV{"MACHINE"} = $machine;
  2616. $target = "$ssh_user\@$machine";
  2617. $buildlog = "$tmpdir/buildlog-$machine";
  2618. $testlog = "$tmpdir/testlog-$machine";
  2619. $dmesg = "$tmpdir/dmesg-$machine";
  2620. $make = "$makecmd O=$outputdir";
  2621. $output_config = "$outputdir/.config";
  2622. if ($reboot_type eq "grub") {
  2623. dodie "GRUB_MENU not defined" if (!defined($grub_menu));
  2624. } elsif (!defined($reboot_script)) {
  2625. dodie "REBOOT_SCRIPT not defined"
  2626. }
  2627. my $run_type = $build_type;
  2628. if ($test_type eq "patchcheck") {
  2629. $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
  2630. } elsif ($test_type eq "bisect") {
  2631. $run_type = $opt{"BISECT_TYPE[$i]"};
  2632. } elsif ($test_type eq "config_bisect") {
  2633. $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
  2634. }
  2635. if ($test_type eq "make_min_config") {
  2636. $run_type = "";
  2637. }
  2638. # mistake in config file?
  2639. if (!defined($run_type)) {
  2640. $run_type = "ERROR";
  2641. }
  2642. my $installme = "";
  2643. $installme = " no_install" if ($no_install);
  2644. doprint "\n\n";
  2645. doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type$installme\n\n";
  2646. unlink $dmesg;
  2647. unlink $buildlog;
  2648. unlink $testlog;
  2649. if (defined($addconfig)) {
  2650. my $min = $minconfig;
  2651. if (!defined($minconfig)) {
  2652. $min = "";
  2653. }
  2654. run_command "cat $addconfig $min > $tmpdir/add_config" or
  2655. dodie "Failed to create temp config";
  2656. $minconfig = "$tmpdir/add_config";
  2657. }
  2658. my $checkout = $opt{"CHECKOUT[$i]"};
  2659. if (defined($checkout)) {
  2660. run_command "git checkout $checkout" or
  2661. die "failed to checkout $checkout";
  2662. }
  2663. $no_reboot = 0;
  2664. if ($test_type eq "bisect") {
  2665. bisect $i;
  2666. next;
  2667. } elsif ($test_type eq "config_bisect") {
  2668. config_bisect $i;
  2669. next;
  2670. } elsif ($test_type eq "patchcheck") {
  2671. patchcheck $i;
  2672. next;
  2673. } elsif ($test_type eq "make_min_config") {
  2674. make_min_config $i;
  2675. next;
  2676. }
  2677. if ($build_type ne "nobuild") {
  2678. build $build_type or next;
  2679. }
  2680. if ($test_type eq "install") {
  2681. get_version;
  2682. install;
  2683. success $i;
  2684. next;
  2685. }
  2686. if ($test_type ne "build") {
  2687. my $failed = 0;
  2688. start_monitor_and_boot or $failed = 1;
  2689. if (!$failed && $test_type ne "boot" && defined($run_test)) {
  2690. do_run_test or $failed = 1;
  2691. }
  2692. end_monitor;
  2693. next if ($failed);
  2694. }
  2695. success $i;
  2696. }
  2697. if ($opt{"POWEROFF_ON_SUCCESS"}) {
  2698. halt;
  2699. } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
  2700. reboot;
  2701. }
  2702. doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
  2703. exit 0;