ktest.pl 72 KB

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