ktest.pl 78 KB

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