ktest.pl 86 KB

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