lcov 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303
  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright (c) International Business Machines Corp., 2002,2012
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or (at
  8. # your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. #
  19. #
  20. # lcov
  21. #
  22. # This is a wrapper script which provides a single interface for accessing
  23. # LCOV coverage data.
  24. #
  25. #
  26. # History:
  27. # 2002-08-29 created by Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
  28. # IBM Lab Boeblingen
  29. # 2002-09-05 / Peter Oberparleiter: implemented --kernel-directory +
  30. # multiple directories
  31. # 2002-10-16 / Peter Oberparleiter: implemented --add-tracefile option
  32. # 2002-10-17 / Peter Oberparleiter: implemented --extract option
  33. # 2002-11-04 / Peter Oberparleiter: implemented --list option
  34. # 2003-03-07 / Paul Larson: Changed to make it work with the latest gcov
  35. # kernel patch. This will break it with older gcov-kernel
  36. # patches unless you change the value of $gcovmod in this script
  37. # 2003-04-07 / Peter Oberparleiter: fixed bug which resulted in an error
  38. # when trying to combine .info files containing data without
  39. # a test name
  40. # 2003-04-10 / Peter Oberparleiter: extended Paul's change so that LCOV
  41. # works both with the new and the old gcov-kernel patch
  42. # 2003-04-10 / Peter Oberparleiter: added $gcov_dir constant in anticipation
  43. # of a possible move of the gcov kernel directory to another
  44. # file system in a future version of the gcov-kernel patch
  45. # 2003-04-15 / Paul Larson: make info write to STDERR, not STDOUT
  46. # 2003-04-15 / Paul Larson: added --remove option
  47. # 2003-04-30 / Peter Oberparleiter: renamed --reset to --zerocounters
  48. # to remove naming ambiguity with --remove
  49. # 2003-04-30 / Peter Oberparleiter: adjusted help text to include --remove
  50. # 2003-06-27 / Peter Oberparleiter: implemented --diff
  51. # 2003-07-03 / Peter Oberparleiter: added line checksum support, added
  52. # --no-checksum
  53. # 2003-12-11 / Laurent Deniel: added --follow option
  54. # 2004-03-29 / Peter Oberparleiter: modified --diff option to better cope with
  55. # ambiguous patch file entries, modified --capture option to use
  56. # modprobe before insmod (needed for 2.6)
  57. # 2004-03-30 / Peter Oberparleiter: added --path option
  58. # 2004-08-09 / Peter Oberparleiter: added configuration file support
  59. # 2008-08-13 / Peter Oberparleiter: added function coverage support
  60. #
  61. use strict;
  62. use File::Basename;
  63. use File::Path;
  64. use File::Find;
  65. use File::Temp qw /tempdir/;
  66. use File::Spec::Functions qw /abs2rel canonpath catdir catfile catpath
  67. file_name_is_absolute rootdir splitdir splitpath/;
  68. use Getopt::Long;
  69. use Cwd qw /abs_path getcwd/;
  70. # Global constants
  71. our $lcov_version = 'LCOV version 1.10';
  72. our $lcov_url = "http://ltp.sourceforge.net/coverage/lcov.php";
  73. our $tool_name = basename($0);
  74. # Directory containing gcov kernel files
  75. our $gcov_dir;
  76. # Where to create temporary directories
  77. our $tmp_dir;
  78. # Internal constants
  79. our $GKV_PROC = 0; # gcov-kernel data in /proc via external patch
  80. our $GKV_SYS = 1; # gcov-kernel data in /sys via vanilla 2.6.31+
  81. our @GKV_NAME = ( "external", "upstream" );
  82. our $pkg_gkv_file = ".gcov_kernel_version";
  83. our $pkg_build_file = ".build_directory";
  84. our $BR_BLOCK = 0;
  85. our $BR_BRANCH = 1;
  86. our $BR_TAKEN = 2;
  87. our $BR_VEC_ENTRIES = 3;
  88. our $BR_VEC_WIDTH = 32;
  89. # Branch data combination types
  90. our $BR_SUB = 0;
  91. our $BR_ADD = 1;
  92. # Prototypes
  93. sub print_usage(*);
  94. sub check_options();
  95. sub userspace_reset();
  96. sub userspace_capture();
  97. sub kernel_reset();
  98. sub kernel_capture();
  99. sub kernel_capture_initial();
  100. sub package_capture();
  101. sub add_traces();
  102. sub read_info_file($);
  103. sub get_info_entry($);
  104. sub set_info_entry($$$$$$$$$;$$$$$$);
  105. sub add_counts($$);
  106. sub merge_checksums($$$);
  107. sub combine_info_entries($$$);
  108. sub combine_info_files($$);
  109. sub write_info_file(*$);
  110. sub extract();
  111. sub remove();
  112. sub list();
  113. sub get_common_filename($$);
  114. sub read_diff($);
  115. sub diff();
  116. sub system_no_output($@);
  117. sub read_config($);
  118. sub apply_config($);
  119. sub info(@);
  120. sub create_temp_dir();
  121. sub transform_pattern($);
  122. sub warn_handler($);
  123. sub die_handler($);
  124. sub abort_handler($);
  125. sub temp_cleanup();
  126. sub setup_gkv();
  127. sub get_overall_line($$$$);
  128. sub print_overall_rate($$$$$$$$$);
  129. sub lcov_geninfo(@);
  130. sub create_package($$$;$);
  131. sub get_func_found_and_hit($);
  132. sub br_ivec_get($$);
  133. sub summary();
  134. sub rate($$;$$$);
  135. # Global variables & initialization
  136. our @directory; # Specifies where to get coverage data from
  137. our @kernel_directory; # If set, captures only from specified kernel subdirs
  138. our @add_tracefile; # If set, reads in and combines all files in list
  139. our $list; # If set, list contents of tracefile
  140. our $extract; # If set, extracts parts of tracefile
  141. our $remove; # If set, removes parts of tracefile
  142. our $diff; # If set, modifies tracefile according to diff
  143. our $reset; # If set, reset all coverage data to zero
  144. our $capture; # If set, capture data
  145. our $output_filename; # Name for file to write coverage data to
  146. our $test_name = ""; # Test case name
  147. our $quiet = ""; # If set, suppress information messages
  148. our $help; # Help option flag
  149. our $version; # Version option flag
  150. our $convert_filenames; # If set, convert filenames when applying diff
  151. our $strip; # If set, strip leading directories when applying diff
  152. our $temp_dir_name; # Name of temporary directory
  153. our $cwd = `pwd`; # Current working directory
  154. our $to_file; # If set, indicates that output is written to a file
  155. our $follow; # If set, indicates that find shall follow links
  156. our $diff_path = ""; # Path removed from tracefile when applying diff
  157. our $base_directory; # Base directory (cwd of gcc during compilation)
  158. our $checksum; # If set, calculate a checksum for each line
  159. our $no_checksum; # If set, don't calculate a checksum for each line
  160. our $compat_libtool; # If set, indicates that libtool mode is to be enabled
  161. our $no_compat_libtool; # If set, indicates that libtool mode is to be disabled
  162. our $gcov_tool;
  163. our @opt_ignore_errors;
  164. our $initial;
  165. our $no_recursion = 0;
  166. our $to_package;
  167. our $from_package;
  168. our $maxdepth;
  169. our $no_markers;
  170. our $config; # Configuration file contents
  171. chomp($cwd);
  172. our $tool_dir = dirname($0); # Directory where genhtml tool is installed
  173. our @temp_dirs;
  174. our $gcov_gkv; # gcov kernel support version found on machine
  175. our $opt_derive_func_data;
  176. our $opt_debug;
  177. our $opt_list_full_path;
  178. our $opt_no_list_full_path;
  179. our $opt_list_width = 80;
  180. our $opt_list_truncate_max = 20;
  181. our $opt_external;
  182. our $opt_no_external;
  183. our $opt_config_file;
  184. our %opt_rc;
  185. our @opt_summary;
  186. our $opt_compat;
  187. our $ln_overall_found;
  188. our $ln_overall_hit;
  189. our $fn_overall_found;
  190. our $fn_overall_hit;
  191. our $br_overall_found;
  192. our $br_overall_hit;
  193. our $func_coverage = 1;
  194. our $br_coverage = 0;
  195. #
  196. # Code entry point
  197. #
  198. $SIG{__WARN__} = \&warn_handler;
  199. $SIG{__DIE__} = \&die_handler;
  200. $SIG{'INT'} = \&abort_handler;
  201. $SIG{'QUIT'} = \&abort_handler;
  202. # Prettify version string
  203. $lcov_version =~ s/\$\s*Revision\s*:?\s*(\S+)\s*\$/$1/;
  204. # Add current working directory if $tool_dir is not already an absolute path
  205. if (! ($tool_dir =~ /^\/(.*)$/))
  206. {
  207. $tool_dir = "$cwd/$tool_dir";
  208. }
  209. # Check command line for a configuration file name
  210. Getopt::Long::Configure("pass_through", "no_auto_abbrev");
  211. GetOptions("config-file=s" => \$opt_config_file,
  212. "rc=s%" => \%opt_rc);
  213. Getopt::Long::Configure("default");
  214. # Read configuration file if available
  215. if (defined($opt_config_file)) {
  216. $config = read_config($opt_config_file);
  217. } elsif (defined($ENV{"HOME"}) && (-r $ENV{"HOME"}."/.lcovrc"))
  218. {
  219. $config = read_config($ENV{"HOME"}."/.lcovrc");
  220. }
  221. elsif (-r "/etc/lcovrc")
  222. {
  223. $config = read_config("/etc/lcovrc");
  224. }
  225. if ($config || %opt_rc)
  226. {
  227. # Copy configuration file and --rc values to variables
  228. apply_config({
  229. "lcov_gcov_dir" => \$gcov_dir,
  230. "lcov_tmp_dir" => \$tmp_dir,
  231. "lcov_list_full_path" => \$opt_list_full_path,
  232. "lcov_list_width" => \$opt_list_width,
  233. "lcov_list_truncate_max"=> \$opt_list_truncate_max,
  234. "lcov_branch_coverage" => \$br_coverage,
  235. "lcov_function_coverage"=> \$func_coverage,
  236. });
  237. }
  238. # Parse command line options
  239. if (!GetOptions("directory|d|di=s" => \@directory,
  240. "add-tracefile|a=s" => \@add_tracefile,
  241. "list|l=s" => \$list,
  242. "kernel-directory|k=s" => \@kernel_directory,
  243. "extract|e=s" => \$extract,
  244. "remove|r=s" => \$remove,
  245. "diff=s" => \$diff,
  246. "convert-filenames" => \$convert_filenames,
  247. "strip=i" => \$strip,
  248. "capture|c" => \$capture,
  249. "output-file|o=s" => \$output_filename,
  250. "test-name|t=s" => \$test_name,
  251. "zerocounters|z" => \$reset,
  252. "quiet|q" => \$quiet,
  253. "help|h|?" => \$help,
  254. "version|v" => \$version,
  255. "follow|f" => \$follow,
  256. "path=s" => \$diff_path,
  257. "base-directory|b=s" => \$base_directory,
  258. "checksum" => \$checksum,
  259. "no-checksum" => \$no_checksum,
  260. "compat-libtool" => \$compat_libtool,
  261. "no-compat-libtool" => \$no_compat_libtool,
  262. "gcov-tool=s" => \$gcov_tool,
  263. "ignore-errors=s" => \@opt_ignore_errors,
  264. "initial|i" => \$initial,
  265. "no-recursion" => \$no_recursion,
  266. "to-package=s" => \$to_package,
  267. "from-package=s" => \$from_package,
  268. "no-markers" => \$no_markers,
  269. "derive-func-data" => \$opt_derive_func_data,
  270. "debug" => \$opt_debug,
  271. "list-full-path" => \$opt_list_full_path,
  272. "no-list-full-path" => \$opt_no_list_full_path,
  273. "external" => \$opt_external,
  274. "no-external" => \$opt_no_external,
  275. "summary=s" => \@opt_summary,
  276. "compat=s" => \$opt_compat,
  277. "config-file=s" => \$opt_config_file,
  278. "rc=s%" => \%opt_rc,
  279. ))
  280. {
  281. print(STDERR "Use $tool_name --help to get usage information\n");
  282. exit(1);
  283. }
  284. else
  285. {
  286. # Merge options
  287. if (defined($no_checksum))
  288. {
  289. $checksum = ($no_checksum ? 0 : 1);
  290. $no_checksum = undef;
  291. }
  292. if (defined($no_compat_libtool))
  293. {
  294. $compat_libtool = ($no_compat_libtool ? 0 : 1);
  295. $no_compat_libtool = undef;
  296. }
  297. if (defined($opt_no_list_full_path))
  298. {
  299. $opt_list_full_path = ($opt_no_list_full_path ? 0 : 1);
  300. $opt_no_list_full_path = undef;
  301. }
  302. if (defined($opt_no_external)) {
  303. $opt_external = 0;
  304. $opt_no_external = undef;
  305. }
  306. }
  307. # Check for help option
  308. if ($help)
  309. {
  310. print_usage(*STDOUT);
  311. exit(0);
  312. }
  313. # Check for version option
  314. if ($version)
  315. {
  316. print("$tool_name: $lcov_version\n");
  317. exit(0);
  318. }
  319. # Check list width option
  320. if ($opt_list_width <= 40) {
  321. die("ERROR: lcov_list_width parameter out of range (needs to be ".
  322. "larger than 40)\n");
  323. }
  324. # Normalize --path text
  325. $diff_path =~ s/\/$//;
  326. if ($follow)
  327. {
  328. $follow = "-follow";
  329. }
  330. else
  331. {
  332. $follow = "";
  333. }
  334. if ($no_recursion)
  335. {
  336. $maxdepth = "-maxdepth 1";
  337. }
  338. else
  339. {
  340. $maxdepth = "";
  341. }
  342. # Check for valid options
  343. check_options();
  344. # Only --extract, --remove and --diff allow unnamed parameters
  345. if (@ARGV && !($extract || $remove || $diff || @opt_summary))
  346. {
  347. die("Extra parameter found: '".join(" ", @ARGV)."'\n".
  348. "Use $tool_name --help to get usage information\n");
  349. }
  350. # Check for output filename
  351. $to_file = ($output_filename && ($output_filename ne "-"));
  352. if ($capture)
  353. {
  354. if (!$to_file)
  355. {
  356. # Option that tells geninfo to write to stdout
  357. $output_filename = "-";
  358. }
  359. }
  360. # Determine kernel directory for gcov data
  361. if (!$from_package && !@directory && ($capture || $reset)) {
  362. ($gcov_gkv, $gcov_dir) = setup_gkv();
  363. }
  364. # Check for requested functionality
  365. if ($reset)
  366. {
  367. # Differentiate between user space and kernel reset
  368. if (@directory)
  369. {
  370. userspace_reset();
  371. }
  372. else
  373. {
  374. kernel_reset();
  375. }
  376. }
  377. elsif ($capture)
  378. {
  379. # Capture source can be user space, kernel or package
  380. if ($from_package) {
  381. package_capture();
  382. } elsif (@directory) {
  383. userspace_capture();
  384. } else {
  385. if ($initial) {
  386. if (defined($to_package)) {
  387. die("ERROR: --initial cannot be used together ".
  388. "with --to-package\n");
  389. }
  390. kernel_capture_initial();
  391. } else {
  392. kernel_capture();
  393. }
  394. }
  395. }
  396. elsif (@add_tracefile)
  397. {
  398. ($ln_overall_found, $ln_overall_hit,
  399. $fn_overall_found, $fn_overall_hit,
  400. $br_overall_found, $br_overall_hit) = add_traces();
  401. }
  402. elsif ($remove)
  403. {
  404. ($ln_overall_found, $ln_overall_hit,
  405. $fn_overall_found, $fn_overall_hit,
  406. $br_overall_found, $br_overall_hit) = remove();
  407. }
  408. elsif ($extract)
  409. {
  410. ($ln_overall_found, $ln_overall_hit,
  411. $fn_overall_found, $fn_overall_hit,
  412. $br_overall_found, $br_overall_hit) = extract();
  413. }
  414. elsif ($list)
  415. {
  416. list();
  417. }
  418. elsif ($diff)
  419. {
  420. if (scalar(@ARGV) != 1)
  421. {
  422. die("ERROR: option --diff requires one additional argument!\n".
  423. "Use $tool_name --help to get usage information\n");
  424. }
  425. ($ln_overall_found, $ln_overall_hit,
  426. $fn_overall_found, $fn_overall_hit,
  427. $br_overall_found, $br_overall_hit) = diff();
  428. }
  429. elsif (@opt_summary)
  430. {
  431. ($ln_overall_found, $ln_overall_hit,
  432. $fn_overall_found, $fn_overall_hit,
  433. $br_overall_found, $br_overall_hit) = summary();
  434. }
  435. temp_cleanup();
  436. if (defined($ln_overall_found)) {
  437. print_overall_rate(1, $ln_overall_found, $ln_overall_hit,
  438. 1, $fn_overall_found, $fn_overall_hit,
  439. 1, $br_overall_found, $br_overall_hit);
  440. } else {
  441. info("Done.\n") if (!$list && !$capture);
  442. }
  443. exit(0);
  444. #
  445. # print_usage(handle)
  446. #
  447. # Print usage information.
  448. #
  449. sub print_usage(*)
  450. {
  451. local *HANDLE = $_[0];
  452. print(HANDLE <<END_OF_USAGE);
  453. Usage: $tool_name [OPTIONS]
  454. Use lcov to collect coverage data from either the currently running Linux
  455. kernel or from a user space application. Specify the --directory option to
  456. get coverage data for a user space program.
  457. Misc:
  458. -h, --help Print this help, then exit
  459. -v, --version Print version number, then exit
  460. -q, --quiet Do not print progress messages
  461. Operation:
  462. -z, --zerocounters Reset all execution counts to zero
  463. -c, --capture Capture coverage data
  464. -a, --add-tracefile FILE Add contents of tracefiles
  465. -e, --extract FILE PATTERN Extract files matching PATTERN from FILE
  466. -r, --remove FILE PATTERN Remove files matching PATTERN from FILE
  467. -l, --list FILE List contents of tracefile FILE
  468. --diff FILE DIFF Transform tracefile FILE according to DIFF
  469. --summary FILE Show summary coverage data for tracefiles
  470. Options:
  471. -i, --initial Capture initial zero coverage data
  472. -t, --test-name NAME Specify test name to be stored with data
  473. -o, --output-file FILENAME Write data to FILENAME instead of stdout
  474. -d, --directory DIR Use .da files in DIR instead of kernel
  475. -f, --follow Follow links when searching .da files
  476. -k, --kernel-directory KDIR Capture kernel coverage data only from KDIR
  477. -b, --base-directory DIR Use DIR as base directory for relative paths
  478. --convert-filenames Convert filenames when applying diff
  479. --strip DEPTH Strip initial DEPTH directory levels in diff
  480. --path PATH Strip PATH from tracefile when applying diff
  481. --(no-)checksum Enable (disable) line checksumming
  482. --(no-)compat-libtool Enable (disable) libtool compatibility mode
  483. --gcov-tool TOOL Specify gcov tool location
  484. --ignore-errors ERRORS Continue after ERRORS (gcov, source, graph)
  485. --no-recursion Exclude subdirectories from processing
  486. --to-package FILENAME Store unprocessed coverage data in FILENAME
  487. --from-package FILENAME Capture from unprocessed data in FILENAME
  488. --no-markers Ignore exclusion markers in source code
  489. --derive-func-data Generate function data from line data
  490. --list-full-path Print full path during a list operation
  491. --(no-)external Include (ignore) data for external files
  492. --config-file FILENAME Specify configuration file location
  493. --rc SETTING=VALUE Override configuration file setting
  494. --compat MODE=on|off|auto Set compat MODE (libtool, hammer, split_crc)
  495. For more information see: $lcov_url
  496. END_OF_USAGE
  497. ;
  498. }
  499. #
  500. # check_options()
  501. #
  502. # Check for valid combination of command line options. Die on error.
  503. #
  504. sub check_options()
  505. {
  506. my $i = 0;
  507. # Count occurrence of mutually exclusive options
  508. $reset && $i++;
  509. $capture && $i++;
  510. @add_tracefile && $i++;
  511. $extract && $i++;
  512. $remove && $i++;
  513. $list && $i++;
  514. $diff && $i++;
  515. @opt_summary && $i++;
  516. if ($i == 0)
  517. {
  518. die("Need one of options -z, -c, -a, -e, -r, -l, ".
  519. "--diff or --summary\n".
  520. "Use $tool_name --help to get usage information\n");
  521. }
  522. elsif ($i > 1)
  523. {
  524. die("ERROR: only one of -z, -c, -a, -e, -r, -l, ".
  525. "--diff or --summary allowed!\n".
  526. "Use $tool_name --help to get usage information\n");
  527. }
  528. }
  529. #
  530. # userspace_reset()
  531. #
  532. # Reset coverage data found in DIRECTORY by deleting all contained .da files.
  533. #
  534. # Die on error.
  535. #
  536. sub userspace_reset()
  537. {
  538. my $current_dir;
  539. my @file_list;
  540. foreach $current_dir (@directory)
  541. {
  542. info("Deleting all .da files in $current_dir".
  543. ($no_recursion?"\n":" and subdirectories\n"));
  544. @file_list = `find "$current_dir" $maxdepth $follow -name \\*\\.da -o -name \\*\\.gcda -type f 2>/dev/null`;
  545. chomp(@file_list);
  546. foreach (@file_list)
  547. {
  548. unlink($_) or die("ERROR: cannot remove file $_!\n");
  549. }
  550. }
  551. }
  552. #
  553. # userspace_capture()
  554. #
  555. # Capture coverage data found in DIRECTORY and write it to a package (if
  556. # TO_PACKAGE specified) or to OUTPUT_FILENAME or STDOUT.
  557. #
  558. # Die on error.
  559. #
  560. sub userspace_capture()
  561. {
  562. my $dir;
  563. my $build;
  564. if (!defined($to_package)) {
  565. lcov_geninfo(@directory);
  566. return;
  567. }
  568. if (scalar(@directory) != 1) {
  569. die("ERROR: -d may be specified only once with --to-package\n");
  570. }
  571. $dir = $directory[0];
  572. if (defined($base_directory)) {
  573. $build = $base_directory;
  574. } else {
  575. $build = $dir;
  576. }
  577. create_package($to_package, $dir, $build);
  578. }
  579. #
  580. # kernel_reset()
  581. #
  582. # Reset kernel coverage.
  583. #
  584. # Die on error.
  585. #
  586. sub kernel_reset()
  587. {
  588. local *HANDLE;
  589. my $reset_file;
  590. info("Resetting kernel execution counters\n");
  591. if (-e "$gcov_dir/vmlinux") {
  592. $reset_file = "$gcov_dir/vmlinux";
  593. } elsif (-e "$gcov_dir/reset") {
  594. $reset_file = "$gcov_dir/reset";
  595. } else {
  596. die("ERROR: no reset control found in $gcov_dir\n");
  597. }
  598. open(HANDLE, ">", $reset_file) or
  599. die("ERROR: cannot write to $reset_file!\n");
  600. print(HANDLE "0");
  601. close(HANDLE);
  602. }
  603. #
  604. # lcov_copy_single(from, to)
  605. #
  606. # Copy single regular file FROM to TO without checking its size. This is
  607. # required to work with special files generated by the kernel
  608. # seq_file-interface.
  609. #
  610. #
  611. sub lcov_copy_single($$)
  612. {
  613. my ($from, $to) = @_;
  614. my $content;
  615. local $/;
  616. local *HANDLE;
  617. open(HANDLE, "<", $from) or die("ERROR: cannot read $from: $!\n");
  618. $content = <HANDLE>;
  619. close(HANDLE);
  620. open(HANDLE, ">", $to) or die("ERROR: cannot write $from: $!\n");
  621. if (defined($content)) {
  622. print(HANDLE $content);
  623. }
  624. close(HANDLE);
  625. }
  626. #
  627. # lcov_find(dir, function, data[, extension, ...)])
  628. #
  629. # Search DIR for files and directories whose name matches PATTERN and run
  630. # FUNCTION for each match. If not pattern is specified, match all names.
  631. #
  632. # FUNCTION has the following prototype:
  633. # function(dir, relative_name, data)
  634. #
  635. # Where:
  636. # dir: the base directory for this search
  637. # relative_name: the name relative to the base directory of this entry
  638. # data: the DATA variable passed to lcov_find
  639. #
  640. sub lcov_find($$$;@)
  641. {
  642. my ($dir, $fn, $data, @pattern) = @_;
  643. my $result;
  644. my $_fn = sub {
  645. my $filename = $File::Find::name;
  646. if (defined($result)) {
  647. return;
  648. }
  649. $filename = abs2rel($filename, $dir);
  650. foreach (@pattern) {
  651. if ($filename =~ /$_/) {
  652. goto ok;
  653. }
  654. }
  655. return;
  656. ok:
  657. $result = &$fn($dir, $filename, $data);
  658. };
  659. if (scalar(@pattern) == 0) {
  660. @pattern = ".*";
  661. }
  662. find( { wanted => $_fn, no_chdir => 1 }, $dir);
  663. return $result;
  664. }
  665. #
  666. # lcov_copy_fn(from, rel, to)
  667. #
  668. # Copy directories, files and links from/rel to to/rel.
  669. #
  670. sub lcov_copy_fn($$$)
  671. {
  672. my ($from, $rel, $to) = @_;
  673. my $absfrom = canonpath(catfile($from, $rel));
  674. my $absto = canonpath(catfile($to, $rel));
  675. if (-d) {
  676. if (! -d $absto) {
  677. mkpath($absto) or
  678. die("ERROR: cannot create directory $absto\n");
  679. chmod(0700, $absto);
  680. }
  681. } elsif (-l) {
  682. # Copy symbolic link
  683. my $link = readlink($absfrom);
  684. if (!defined($link)) {
  685. die("ERROR: cannot read link $absfrom: $!\n");
  686. }
  687. symlink($link, $absto) or
  688. die("ERROR: cannot create link $absto: $!\n");
  689. } else {
  690. lcov_copy_single($absfrom, $absto);
  691. chmod(0600, $absto);
  692. }
  693. return undef;
  694. }
  695. #
  696. # lcov_copy(from, to, subdirs)
  697. #
  698. # Copy all specified SUBDIRS and files from directory FROM to directory TO. For
  699. # regular files, copy file contents without checking its size. This is required
  700. # to work with seq_file-generated files.
  701. #
  702. sub lcov_copy($$;@)
  703. {
  704. my ($from, $to, @subdirs) = @_;
  705. my @pattern;
  706. foreach (@subdirs) {
  707. push(@pattern, "^$_");
  708. }
  709. lcov_find($from, \&lcov_copy_fn, $to, @pattern);
  710. }
  711. #
  712. # lcov_geninfo(directory)
  713. #
  714. # Call geninfo for the specified directory and with the parameters specified
  715. # at the command line.
  716. #
  717. sub lcov_geninfo(@)
  718. {
  719. my (@dir) = @_;
  720. my @param;
  721. # Capture data
  722. info("Capturing coverage data from ".join(" ", @dir)."\n");
  723. @param = ("$tool_dir/geninfo", @dir);
  724. if ($output_filename)
  725. {
  726. @param = (@param, "--output-filename", $output_filename);
  727. }
  728. if ($test_name)
  729. {
  730. @param = (@param, "--test-name", $test_name);
  731. }
  732. if ($follow)
  733. {
  734. @param = (@param, "--follow");
  735. }
  736. if ($quiet)
  737. {
  738. @param = (@param, "--quiet");
  739. }
  740. if (defined($checksum))
  741. {
  742. if ($checksum)
  743. {
  744. @param = (@param, "--checksum");
  745. }
  746. else
  747. {
  748. @param = (@param, "--no-checksum");
  749. }
  750. }
  751. if ($base_directory)
  752. {
  753. @param = (@param, "--base-directory", $base_directory);
  754. }
  755. if ($no_compat_libtool)
  756. {
  757. @param = (@param, "--no-compat-libtool");
  758. }
  759. elsif ($compat_libtool)
  760. {
  761. @param = (@param, "--compat-libtool");
  762. }
  763. if ($gcov_tool)
  764. {
  765. @param = (@param, "--gcov-tool", $gcov_tool);
  766. }
  767. foreach (@opt_ignore_errors) {
  768. @param = (@param, "--ignore-errors", $_);
  769. }
  770. if ($no_recursion) {
  771. @param = (@param, "--no-recursion");
  772. }
  773. if ($initial)
  774. {
  775. @param = (@param, "--initial");
  776. }
  777. if ($no_markers)
  778. {
  779. @param = (@param, "--no-markers");
  780. }
  781. if ($opt_derive_func_data)
  782. {
  783. @param = (@param, "--derive-func-data");
  784. }
  785. if ($opt_debug)
  786. {
  787. @param = (@param, "--debug");
  788. }
  789. if (defined($opt_external) && $opt_external)
  790. {
  791. @param = (@param, "--external");
  792. }
  793. if (defined($opt_external) && !$opt_external)
  794. {
  795. @param = (@param, "--no-external");
  796. }
  797. if (defined($opt_compat)) {
  798. @param = (@param, "--compat", $opt_compat);
  799. }
  800. if (%opt_rc) {
  801. foreach my $key (keys(%opt_rc)) {
  802. @param = (@param, "--rc", "$key=".$opt_rc{$key});
  803. }
  804. }
  805. system(@param) and exit($? >> 8);
  806. }
  807. #
  808. # read_file(filename)
  809. #
  810. # Return the contents of the file defined by filename.
  811. #
  812. sub read_file($)
  813. {
  814. my ($filename) = @_;
  815. my $content;
  816. local $\;
  817. local *HANDLE;
  818. open(HANDLE, "<", $filename) || return undef;
  819. $content = <HANDLE>;
  820. close(HANDLE);
  821. return $content;
  822. }
  823. #
  824. # get_package(package_file)
  825. #
  826. # Unpack unprocessed coverage data files from package_file to a temporary
  827. # directory and return directory name, build directory and gcov kernel version
  828. # as found in package.
  829. #
  830. sub get_package($)
  831. {
  832. my ($file) = @_;
  833. my $dir = create_temp_dir();
  834. my $gkv;
  835. my $build;
  836. my $cwd = getcwd();
  837. my $count;
  838. local *HANDLE;
  839. info("Reading package $file:\n");
  840. info(" data directory .......: $dir\n");
  841. $file = abs_path($file);
  842. chdir($dir);
  843. open(HANDLE, "-|", "tar xvfz '$file' 2>/dev/null")
  844. or die("ERROR: could not process package $file\n");
  845. while (<HANDLE>) {
  846. if (/\.da$/ || /\.gcda$/) {
  847. $count++;
  848. }
  849. }
  850. close(HANDLE);
  851. $build = read_file("$dir/$pkg_build_file");
  852. if (defined($build)) {
  853. info(" build directory ......: $build\n");
  854. }
  855. $gkv = read_file("$dir/$pkg_gkv_file");
  856. if (defined($gkv)) {
  857. $gkv = int($gkv);
  858. if ($gkv != $GKV_PROC && $gkv != $GKV_SYS) {
  859. die("ERROR: unsupported gcov kernel version found ".
  860. "($gkv)\n");
  861. }
  862. info(" content type .........: kernel data\n");
  863. info(" gcov kernel version ..: %s\n", $GKV_NAME[$gkv]);
  864. } else {
  865. info(" content type .........: application data\n");
  866. }
  867. info(" data files ...........: $count\n");
  868. chdir($cwd);
  869. return ($dir, $build, $gkv);
  870. }
  871. #
  872. # write_file(filename, $content)
  873. #
  874. # Create a file named filename and write the specified content to it.
  875. #
  876. sub write_file($$)
  877. {
  878. my ($filename, $content) = @_;
  879. local *HANDLE;
  880. open(HANDLE, ">", $filename) || return 0;
  881. print(HANDLE $content);
  882. close(HANDLE) || return 0;
  883. return 1;
  884. }
  885. # count_package_data(filename)
  886. #
  887. # Count the number of coverage data files in the specified package file.
  888. #
  889. sub count_package_data($)
  890. {
  891. my ($filename) = @_;
  892. local *HANDLE;
  893. my $count = 0;
  894. open(HANDLE, "-|", "tar tfz '$filename'") or return undef;
  895. while (<HANDLE>) {
  896. if (/\.da$/ || /\.gcda$/) {
  897. $count++;
  898. }
  899. }
  900. close(HANDLE);
  901. return $count;
  902. }
  903. #
  904. # create_package(package_file, source_directory, build_directory[,
  905. # kernel_gcov_version])
  906. #
  907. # Store unprocessed coverage data files from source_directory to package_file.
  908. #
  909. sub create_package($$$;$)
  910. {
  911. my ($file, $dir, $build, $gkv) = @_;
  912. my $cwd = getcwd();
  913. # Print information about the package
  914. info("Creating package $file:\n");
  915. info(" data directory .......: $dir\n");
  916. # Handle build directory
  917. if (defined($build)) {
  918. info(" build directory ......: $build\n");
  919. write_file("$dir/$pkg_build_file", $build)
  920. or die("ERROR: could not write to ".
  921. "$dir/$pkg_build_file\n");
  922. }
  923. # Handle gcov kernel version data
  924. if (defined($gkv)) {
  925. info(" content type .........: kernel data\n");
  926. info(" gcov kernel version ..: %s\n", $GKV_NAME[$gkv]);
  927. write_file("$dir/$pkg_gkv_file", $gkv)
  928. or die("ERROR: could not write to ".
  929. "$dir/$pkg_gkv_file\n");
  930. } else {
  931. info(" content type .........: application data\n");
  932. }
  933. # Create package
  934. $file = abs_path($file);
  935. chdir($dir);
  936. system("tar cfz $file .")
  937. and die("ERROR: could not create package $file\n");
  938. # Remove temporary files
  939. unlink("$dir/$pkg_build_file");
  940. unlink("$dir/$pkg_gkv_file");
  941. # Show number of data files
  942. if (!$quiet) {
  943. my $count = count_package_data($file);
  944. if (defined($count)) {
  945. info(" data files ...........: $count\n");
  946. }
  947. }
  948. chdir($cwd);
  949. }
  950. sub find_link_fn($$$)
  951. {
  952. my ($from, $rel, $filename) = @_;
  953. my $absfile = catfile($from, $rel, $filename);
  954. if (-l $absfile) {
  955. return $absfile;
  956. }
  957. return undef;
  958. }
  959. #
  960. # get_base(dir)
  961. #
  962. # Return (BASE, OBJ), where
  963. # - BASE: is the path to the kernel base directory relative to dir
  964. # - OBJ: is the absolute path to the kernel build directory
  965. #
  966. sub get_base($)
  967. {
  968. my ($dir) = @_;
  969. my $marker = "kernel/gcov/base.gcno";
  970. my $markerfile;
  971. my $sys;
  972. my $obj;
  973. my $link;
  974. $markerfile = lcov_find($dir, \&find_link_fn, $marker);
  975. if (!defined($markerfile)) {
  976. return (undef, undef);
  977. }
  978. # sys base is parent of parent of markerfile.
  979. $sys = abs2rel(dirname(dirname(dirname($markerfile))), $dir);
  980. # obj base is parent of parent of markerfile link target.
  981. $link = readlink($markerfile);
  982. if (!defined($link)) {
  983. die("ERROR: could not read $markerfile\n");
  984. }
  985. $obj = dirname(dirname(dirname($link)));
  986. return ($sys, $obj);
  987. }
  988. #
  989. # apply_base_dir(data_dir, base_dir, build_dir, @directories)
  990. #
  991. # Make entries in @directories relative to data_dir.
  992. #
  993. sub apply_base_dir($$$@)
  994. {
  995. my ($data, $base, $build, @dirs) = @_;
  996. my $dir;
  997. my @result;
  998. foreach $dir (@dirs) {
  999. # Is directory path relative to data directory?
  1000. if (-d catdir($data, $dir)) {
  1001. push(@result, $dir);
  1002. next;
  1003. }
  1004. # Relative to the auto-detected base-directory?
  1005. if (defined($base)) {
  1006. if (-d catdir($data, $base, $dir)) {
  1007. push(@result, catdir($base, $dir));
  1008. next;
  1009. }
  1010. }
  1011. # Relative to the specified base-directory?
  1012. if (defined($base_directory)) {
  1013. if (file_name_is_absolute($base_directory)) {
  1014. $base = abs2rel($base_directory, rootdir());
  1015. } else {
  1016. $base = $base_directory;
  1017. }
  1018. if (-d catdir($data, $base, $dir)) {
  1019. push(@result, catdir($base, $dir));
  1020. next;
  1021. }
  1022. }
  1023. # Relative to the build directory?
  1024. if (defined($build)) {
  1025. if (file_name_is_absolute($build)) {
  1026. $base = abs2rel($build, rootdir());
  1027. } else {
  1028. $base = $build;
  1029. }
  1030. if (-d catdir($data, $base, $dir)) {
  1031. push(@result, catdir($base, $dir));
  1032. next;
  1033. }
  1034. }
  1035. die("ERROR: subdirectory $dir not found\n".
  1036. "Please use -b to specify the correct directory\n");
  1037. }
  1038. return @result;
  1039. }
  1040. #
  1041. # copy_gcov_dir(dir, [@subdirectories])
  1042. #
  1043. # Create a temporary directory and copy all or, if specified, only some
  1044. # subdirectories from dir to that directory. Return the name of the temporary
  1045. # directory.
  1046. #
  1047. sub copy_gcov_dir($;@)
  1048. {
  1049. my ($data, @dirs) = @_;
  1050. my $tempdir = create_temp_dir();
  1051. info("Copying data to temporary directory $tempdir\n");
  1052. lcov_copy($data, $tempdir, @dirs);
  1053. return $tempdir;
  1054. }
  1055. #
  1056. # kernel_capture_initial
  1057. #
  1058. # Capture initial kernel coverage data, i.e. create a coverage data file from
  1059. # static graph files which contains zero coverage data for all instrumented
  1060. # lines.
  1061. #
  1062. sub kernel_capture_initial()
  1063. {
  1064. my $build;
  1065. my $source;
  1066. my @params;
  1067. if (defined($base_directory)) {
  1068. $build = $base_directory;
  1069. $source = "specified";
  1070. } else {
  1071. (undef, $build) = get_base($gcov_dir);
  1072. if (!defined($build)) {
  1073. die("ERROR: could not auto-detect build directory.\n".
  1074. "Please use -b to specify the build directory\n");
  1075. }
  1076. $source = "auto-detected";
  1077. }
  1078. info("Using $build as kernel build directory ($source)\n");
  1079. # Build directory needs to be passed to geninfo
  1080. $base_directory = $build;
  1081. if (@kernel_directory) {
  1082. foreach my $dir (@kernel_directory) {
  1083. push(@params, "$build/$dir");
  1084. }
  1085. } else {
  1086. push(@params, $build);
  1087. }
  1088. lcov_geninfo(@params);
  1089. }
  1090. #
  1091. # kernel_capture_from_dir(directory, gcov_kernel_version, build)
  1092. #
  1093. # Perform the actual kernel coverage capturing from the specified directory
  1094. # assuming that the data was copied from the specified gcov kernel version.
  1095. #
  1096. sub kernel_capture_from_dir($$$)
  1097. {
  1098. my ($dir, $gkv, $build) = @_;
  1099. # Create package or coverage file
  1100. if (defined($to_package)) {
  1101. create_package($to_package, $dir, $build, $gkv);
  1102. } else {
  1103. # Build directory needs to be passed to geninfo
  1104. $base_directory = $build;
  1105. lcov_geninfo($dir);
  1106. }
  1107. }
  1108. #
  1109. # adjust_kernel_dir(dir, build)
  1110. #
  1111. # Adjust directories specified with -k so that they point to the directory
  1112. # relative to DIR. Return the build directory if specified or the auto-
  1113. # detected build-directory.
  1114. #
  1115. sub adjust_kernel_dir($$)
  1116. {
  1117. my ($dir, $build) = @_;
  1118. my ($sys_base, $build_auto) = get_base($dir);
  1119. if (!defined($build)) {
  1120. $build = $build_auto;
  1121. }
  1122. if (!defined($build)) {
  1123. die("ERROR: could not auto-detect build directory.\n".
  1124. "Please use -b to specify the build directory\n");
  1125. }
  1126. # Make @kernel_directory relative to sysfs base
  1127. if (@kernel_directory) {
  1128. @kernel_directory = apply_base_dir($dir, $sys_base, $build,
  1129. @kernel_directory);
  1130. }
  1131. return $build;
  1132. }
  1133. sub kernel_capture()
  1134. {
  1135. my $data_dir;
  1136. my $build = $base_directory;
  1137. if ($gcov_gkv == $GKV_SYS) {
  1138. $build = adjust_kernel_dir($gcov_dir, $build);
  1139. }
  1140. $data_dir = copy_gcov_dir($gcov_dir, @kernel_directory);
  1141. kernel_capture_from_dir($data_dir, $gcov_gkv, $build);
  1142. }
  1143. #
  1144. # package_capture()
  1145. #
  1146. # Capture coverage data from a package of unprocessed coverage data files
  1147. # as generated by lcov --to-package.
  1148. #
  1149. sub package_capture()
  1150. {
  1151. my $dir;
  1152. my $build;
  1153. my $gkv;
  1154. ($dir, $build, $gkv) = get_package($from_package);
  1155. # Check for build directory
  1156. if (defined($base_directory)) {
  1157. if (defined($build)) {
  1158. info("Using build directory specified by -b.\n");
  1159. }
  1160. $build = $base_directory;
  1161. }
  1162. # Do the actual capture
  1163. if (defined($gkv)) {
  1164. if ($gkv == $GKV_SYS) {
  1165. $build = adjust_kernel_dir($dir, $build);
  1166. }
  1167. if (@kernel_directory) {
  1168. $dir = copy_gcov_dir($dir, @kernel_directory);
  1169. }
  1170. kernel_capture_from_dir($dir, $gkv, $build);
  1171. } else {
  1172. # Build directory needs to be passed to geninfo
  1173. $base_directory = $build;
  1174. lcov_geninfo($dir);
  1175. }
  1176. }
  1177. #
  1178. # info(printf_parameter)
  1179. #
  1180. # Use printf to write PRINTF_PARAMETER to stdout only when the $quiet flag
  1181. # is not set.
  1182. #
  1183. sub info(@)
  1184. {
  1185. if (!$quiet)
  1186. {
  1187. # Print info string
  1188. if ($to_file)
  1189. {
  1190. printf(@_)
  1191. }
  1192. else
  1193. {
  1194. # Don't interfere with the .info output to STDOUT
  1195. printf(STDERR @_);
  1196. }
  1197. }
  1198. }
  1199. #
  1200. # create_temp_dir()
  1201. #
  1202. # Create a temporary directory and return its path.
  1203. #
  1204. # Die on error.
  1205. #
  1206. sub create_temp_dir()
  1207. {
  1208. my $dir;
  1209. if (defined($tmp_dir)) {
  1210. $dir = tempdir(DIR => $tmp_dir, CLEANUP => 1);
  1211. } else {
  1212. $dir = tempdir(CLEANUP => 1);
  1213. }
  1214. if (!defined($dir)) {
  1215. die("ERROR: cannot create temporary directory\n");
  1216. }
  1217. push(@temp_dirs, $dir);
  1218. return $dir;
  1219. }
  1220. #
  1221. # br_taken_to_num(taken)
  1222. #
  1223. # Convert a branch taken value .info format to number format.
  1224. #
  1225. sub br_taken_to_num($)
  1226. {
  1227. my ($taken) = @_;
  1228. return 0 if ($taken eq '-');
  1229. return $taken + 1;
  1230. }
  1231. #
  1232. # br_num_to_taken(taken)
  1233. #
  1234. # Convert a branch taken value in number format to .info format.
  1235. #
  1236. sub br_num_to_taken($)
  1237. {
  1238. my ($taken) = @_;
  1239. return '-' if ($taken == 0);
  1240. return $taken - 1;
  1241. }
  1242. #
  1243. # br_taken_add(taken1, taken2)
  1244. #
  1245. # Return the result of taken1 + taken2 for 'branch taken' values.
  1246. #
  1247. sub br_taken_add($$)
  1248. {
  1249. my ($t1, $t2) = @_;
  1250. return $t1 if (!defined($t2));
  1251. return $t2 if (!defined($t1));
  1252. return $t1 if ($t2 eq '-');
  1253. return $t2 if ($t1 eq '-');
  1254. return $t1 + $t2;
  1255. }
  1256. #
  1257. # br_taken_sub(taken1, taken2)
  1258. #
  1259. # Return the result of taken1 - taken2 for 'branch taken' values. Return 0
  1260. # if the result would become negative.
  1261. #
  1262. sub br_taken_sub($$)
  1263. {
  1264. my ($t1, $t2) = @_;
  1265. return $t1 if (!defined($t2));
  1266. return undef if (!defined($t1));
  1267. return $t1 if ($t1 eq '-');
  1268. return $t1 if ($t2 eq '-');
  1269. return 0 if $t2 > $t1;
  1270. return $t1 - $t2;
  1271. }
  1272. #
  1273. #
  1274. # br_ivec_len(vector)
  1275. #
  1276. # Return the number of entries in the branch coverage vector.
  1277. #
  1278. sub br_ivec_len($)
  1279. {
  1280. my ($vec) = @_;
  1281. return 0 if (!defined($vec));
  1282. return (length($vec) * 8 / $BR_VEC_WIDTH) / $BR_VEC_ENTRIES;
  1283. }
  1284. #
  1285. # br_ivec_push(vector, block, branch, taken)
  1286. #
  1287. # Add an entry to the branch coverage vector. If an entry with the same
  1288. # branch ID already exists, add the corresponding taken values.
  1289. #
  1290. sub br_ivec_push($$$$)
  1291. {
  1292. my ($vec, $block, $branch, $taken) = @_;
  1293. my $offset;
  1294. my $num = br_ivec_len($vec);
  1295. my $i;
  1296. $vec = "" if (!defined($vec));
  1297. # Check if branch already exists in vector
  1298. for ($i = 0; $i < $num; $i++) {
  1299. my ($v_block, $v_branch, $v_taken) = br_ivec_get($vec, $i);
  1300. next if ($v_block != $block || $v_branch != $branch);
  1301. # Add taken counts
  1302. $taken = br_taken_add($taken, $v_taken);
  1303. last;
  1304. }
  1305. $offset = $i * $BR_VEC_ENTRIES;
  1306. $taken = br_taken_to_num($taken);
  1307. # Add to vector
  1308. vec($vec, $offset + $BR_BLOCK, $BR_VEC_WIDTH) = $block;
  1309. vec($vec, $offset + $BR_BRANCH, $BR_VEC_WIDTH) = $branch;
  1310. vec($vec, $offset + $BR_TAKEN, $BR_VEC_WIDTH) = $taken;
  1311. return $vec;
  1312. }
  1313. #
  1314. # br_ivec_get(vector, number)
  1315. #
  1316. # Return an entry from the branch coverage vector.
  1317. #
  1318. sub br_ivec_get($$)
  1319. {
  1320. my ($vec, $num) = @_;
  1321. my $block;
  1322. my $branch;
  1323. my $taken;
  1324. my $offset = $num * $BR_VEC_ENTRIES;
  1325. # Retrieve data from vector
  1326. $block = vec($vec, $offset + $BR_BLOCK, $BR_VEC_WIDTH);
  1327. $branch = vec($vec, $offset + $BR_BRANCH, $BR_VEC_WIDTH);
  1328. $taken = vec($vec, $offset + $BR_TAKEN, $BR_VEC_WIDTH);
  1329. # Decode taken value from an integer
  1330. $taken = br_num_to_taken($taken);
  1331. return ($block, $branch, $taken);
  1332. }
  1333. #
  1334. # get_br_found_and_hit(brcount)
  1335. #
  1336. # Return (br_found, br_hit) for brcount
  1337. #
  1338. sub get_br_found_and_hit($)
  1339. {
  1340. my ($brcount) = @_;
  1341. my $line;
  1342. my $br_found = 0;
  1343. my $br_hit = 0;
  1344. foreach $line (keys(%{$brcount})) {
  1345. my $brdata = $brcount->{$line};
  1346. my $i;
  1347. my $num = br_ivec_len($brdata);
  1348. for ($i = 0; $i < $num; $i++) {
  1349. my $taken;
  1350. (undef, undef, $taken) = br_ivec_get($brdata, $i);
  1351. $br_found++;
  1352. $br_hit++ if ($taken ne "-" && $taken > 0);
  1353. }
  1354. }
  1355. return ($br_found, $br_hit);
  1356. }
  1357. #
  1358. # read_info_file(info_filename)
  1359. #
  1360. # Read in the contents of the .info file specified by INFO_FILENAME. Data will
  1361. # be returned as a reference to a hash containing the following mappings:
  1362. #
  1363. # %result: for each filename found in file -> \%data
  1364. #
  1365. # %data: "test" -> \%testdata
  1366. # "sum" -> \%sumcount
  1367. # "func" -> \%funcdata
  1368. # "found" -> $lines_found (number of instrumented lines found in file)
  1369. # "hit" -> $lines_hit (number of executed lines in file)
  1370. # "check" -> \%checkdata
  1371. # "testfnc" -> \%testfncdata
  1372. # "sumfnc" -> \%sumfnccount
  1373. # "testbr" -> \%testbrdata
  1374. # "sumbr" -> \%sumbrcount
  1375. #
  1376. # %testdata : name of test affecting this file -> \%testcount
  1377. # %testfncdata: name of test affecting this file -> \%testfnccount
  1378. # %testbrdata: name of test affecting this file -> \%testbrcount
  1379. #
  1380. # %testcount : line number -> execution count for a single test
  1381. # %testfnccount: function name -> execution count for a single test
  1382. # %testbrcount : line number -> branch coverage data for a single test
  1383. # %sumcount : line number -> execution count for all tests
  1384. # %sumfnccount : function name -> execution count for all tests
  1385. # %sumbrcount : line number -> branch coverage data for all tests
  1386. # %funcdata : function name -> line number
  1387. # %checkdata : line number -> checksum of source code line
  1388. # $brdata : vector of items: block, branch, taken
  1389. #
  1390. # Note that .info file sections referring to the same file and test name
  1391. # will automatically be combined by adding all execution counts.
  1392. #
  1393. # Note that if INFO_FILENAME ends with ".gz", it is assumed that the file
  1394. # is compressed using GZIP. If available, GUNZIP will be used to decompress
  1395. # this file.
  1396. #
  1397. # Die on error.
  1398. #
  1399. sub read_info_file($)
  1400. {
  1401. my $tracefile = $_[0]; # Name of tracefile
  1402. my %result; # Resulting hash: file -> data
  1403. my $data; # Data handle for current entry
  1404. my $testdata; # " "
  1405. my $testcount; # " "
  1406. my $sumcount; # " "
  1407. my $funcdata; # " "
  1408. my $checkdata; # " "
  1409. my $testfncdata;
  1410. my $testfnccount;
  1411. my $sumfnccount;
  1412. my $testbrdata;
  1413. my $testbrcount;
  1414. my $sumbrcount;
  1415. my $line; # Current line read from .info file
  1416. my $testname; # Current test name
  1417. my $filename; # Current filename
  1418. my $hitcount; # Count for lines hit
  1419. my $count; # Execution count of current line
  1420. my $negative; # If set, warn about negative counts
  1421. my $changed_testname; # If set, warn about changed testname
  1422. my $line_checksum; # Checksum of current line
  1423. local *INFO_HANDLE; # Filehandle for .info file
  1424. info("Reading tracefile $tracefile\n");
  1425. # Check if file exists and is readable
  1426. stat($_[0]);
  1427. if (!(-r _))
  1428. {
  1429. die("ERROR: cannot read file $_[0]!\n");
  1430. }
  1431. # Check if this is really a plain file
  1432. if (!(-f _))
  1433. {
  1434. die("ERROR: not a plain file: $_[0]!\n");
  1435. }
  1436. # Check for .gz extension
  1437. if ($_[0] =~ /\.gz$/)
  1438. {
  1439. # Check for availability of GZIP tool
  1440. system_no_output(1, "gunzip" ,"-h")
  1441. and die("ERROR: gunzip command not available!\n");
  1442. # Check integrity of compressed file
  1443. system_no_output(1, "gunzip", "-t", $_[0])
  1444. and die("ERROR: integrity check failed for ".
  1445. "compressed file $_[0]!\n");
  1446. # Open compressed file
  1447. open(INFO_HANDLE, "-|", "gunzip -c '$_[0]'")
  1448. or die("ERROR: cannot start gunzip to decompress ".
  1449. "file $_[0]!\n");
  1450. }
  1451. else
  1452. {
  1453. # Open decompressed file
  1454. open(INFO_HANDLE, "<", $_[0])
  1455. or die("ERROR: cannot read file $_[0]!\n");
  1456. }
  1457. $testname = "";
  1458. while (<INFO_HANDLE>)
  1459. {
  1460. chomp($_);
  1461. $line = $_;
  1462. # Switch statement
  1463. foreach ($line)
  1464. {
  1465. /^TN:([^,]*)(,diff)?/ && do
  1466. {
  1467. # Test name information found
  1468. $testname = defined($1) ? $1 : "";
  1469. if ($testname =~ s/\W/_/g)
  1470. {
  1471. $changed_testname = 1;
  1472. }
  1473. $testname .= $2 if (defined($2));
  1474. last;
  1475. };
  1476. /^[SK]F:(.*)/ && do
  1477. {
  1478. # Filename information found
  1479. # Retrieve data for new entry
  1480. $filename = $1;
  1481. $data = $result{$filename};
  1482. ($testdata, $sumcount, $funcdata, $checkdata,
  1483. $testfncdata, $sumfnccount, $testbrdata,
  1484. $sumbrcount) =
  1485. get_info_entry($data);
  1486. if (defined($testname))
  1487. {
  1488. $testcount = $testdata->{$testname};
  1489. $testfnccount = $testfncdata->{$testname};
  1490. $testbrcount = $testbrdata->{$testname};
  1491. }
  1492. else
  1493. {
  1494. $testcount = {};
  1495. $testfnccount = {};
  1496. $testbrcount = {};
  1497. }
  1498. last;
  1499. };
  1500. /^DA:(\d+),(-?\d+)(,[^,\s]+)?/ && do
  1501. {
  1502. # Fix negative counts
  1503. $count = $2 < 0 ? 0 : $2;
  1504. if ($2 < 0)
  1505. {
  1506. $negative = 1;
  1507. }
  1508. # Execution count found, add to structure
  1509. # Add summary counts
  1510. $sumcount->{$1} += $count;
  1511. # Add test-specific counts
  1512. if (defined($testname))
  1513. {
  1514. $testcount->{$1} += $count;
  1515. }
  1516. # Store line checksum if available
  1517. if (defined($3))
  1518. {
  1519. $line_checksum = substr($3, 1);
  1520. # Does it match a previous definition
  1521. if (defined($checkdata->{$1}) &&
  1522. ($checkdata->{$1} ne
  1523. $line_checksum))
  1524. {
  1525. die("ERROR: checksum mismatch ".
  1526. "at $filename:$1\n");
  1527. }
  1528. $checkdata->{$1} = $line_checksum;
  1529. }
  1530. last;
  1531. };
  1532. /^FN:(\d+),([^,]+)/ && do
  1533. {
  1534. last if (!$func_coverage);
  1535. # Function data found, add to structure
  1536. $funcdata->{$2} = $1;
  1537. # Also initialize function call data
  1538. if (!defined($sumfnccount->{$2})) {
  1539. $sumfnccount->{$2} = 0;
  1540. }
  1541. if (defined($testname))
  1542. {
  1543. if (!defined($testfnccount->{$2})) {
  1544. $testfnccount->{$2} = 0;
  1545. }
  1546. }
  1547. last;
  1548. };
  1549. /^FNDA:(\d+),([^,]+)/ && do
  1550. {
  1551. last if (!$func_coverage);
  1552. # Function call count found, add to structure
  1553. # Add summary counts
  1554. $sumfnccount->{$2} += $1;
  1555. # Add test-specific counts
  1556. if (defined($testname))
  1557. {
  1558. $testfnccount->{$2} += $1;
  1559. }
  1560. last;
  1561. };
  1562. /^BRDA:(\d+),(\d+),(\d+),(\d+|-)/ && do {
  1563. # Branch coverage data found
  1564. my ($line, $block, $branch, $taken) =
  1565. ($1, $2, $3, $4);
  1566. last if (!$br_coverage);
  1567. $sumbrcount->{$line} =
  1568. br_ivec_push($sumbrcount->{$line},
  1569. $block, $branch, $taken);
  1570. # Add test-specific counts
  1571. if (defined($testname)) {
  1572. $testbrcount->{$line} =
  1573. br_ivec_push(
  1574. $testbrcount->{$line},
  1575. $block, $branch,
  1576. $taken);
  1577. }
  1578. last;
  1579. };
  1580. /^end_of_record/ && do
  1581. {
  1582. # Found end of section marker
  1583. if ($filename)
  1584. {
  1585. # Store current section data
  1586. if (defined($testname))
  1587. {
  1588. $testdata->{$testname} =
  1589. $testcount;
  1590. $testfncdata->{$testname} =
  1591. $testfnccount;
  1592. $testbrdata->{$testname} =
  1593. $testbrcount;
  1594. }
  1595. set_info_entry($data, $testdata,
  1596. $sumcount, $funcdata,
  1597. $checkdata, $testfncdata,
  1598. $sumfnccount,
  1599. $testbrdata,
  1600. $sumbrcount);
  1601. $result{$filename} = $data;
  1602. last;
  1603. }
  1604. };
  1605. # default
  1606. last;
  1607. }
  1608. }
  1609. close(INFO_HANDLE);
  1610. # Calculate hit and found values for lines and functions of each file
  1611. foreach $filename (keys(%result))
  1612. {
  1613. $data = $result{$filename};
  1614. ($testdata, $sumcount, undef, undef, $testfncdata,
  1615. $sumfnccount, $testbrdata, $sumbrcount) =
  1616. get_info_entry($data);
  1617. # Filter out empty files
  1618. if (scalar(keys(%{$sumcount})) == 0)
  1619. {
  1620. delete($result{$filename});
  1621. next;
  1622. }
  1623. # Filter out empty test cases
  1624. foreach $testname (keys(%{$testdata}))
  1625. {
  1626. if (!defined($testdata->{$testname}) ||
  1627. scalar(keys(%{$testdata->{$testname}})) == 0)
  1628. {
  1629. delete($testdata->{$testname});
  1630. delete($testfncdata->{$testname});
  1631. }
  1632. }
  1633. $data->{"found"} = scalar(keys(%{$sumcount}));
  1634. $hitcount = 0;
  1635. foreach (keys(%{$sumcount}))
  1636. {
  1637. if ($sumcount->{$_} > 0) { $hitcount++; }
  1638. }
  1639. $data->{"hit"} = $hitcount;
  1640. # Get found/hit values for function call data
  1641. $data->{"f_found"} = scalar(keys(%{$sumfnccount}));
  1642. $hitcount = 0;
  1643. foreach (keys(%{$sumfnccount})) {
  1644. if ($sumfnccount->{$_} > 0) {
  1645. $hitcount++;
  1646. }
  1647. }
  1648. $data->{"f_hit"} = $hitcount;
  1649. # Get found/hit values for branch data
  1650. {
  1651. my ($br_found, $br_hit) = get_br_found_and_hit($sumbrcount);
  1652. $data->{"b_found"} = $br_found;
  1653. $data->{"b_hit"} = $br_hit;
  1654. }
  1655. }
  1656. if (scalar(keys(%result)) == 0)
  1657. {
  1658. die("ERROR: no valid records found in tracefile $tracefile\n");
  1659. }
  1660. if ($negative)
  1661. {
  1662. warn("WARNING: negative counts found in tracefile ".
  1663. "$tracefile\n");
  1664. }
  1665. if ($changed_testname)
  1666. {
  1667. warn("WARNING: invalid characters removed from testname in ".
  1668. "tracefile $tracefile\n");
  1669. }
  1670. return(\%result);
  1671. }
  1672. #
  1673. # get_info_entry(hash_ref)
  1674. #
  1675. # Retrieve data from an entry of the structure generated by read_info_file().
  1676. # Return a list of references to hashes:
  1677. # (test data hash ref, sum count hash ref, funcdata hash ref, checkdata hash
  1678. # ref, testfncdata hash ref, sumfnccount hash ref, testbrdata hash ref,
  1679. # sumbrcount hash ref, lines found, lines hit, functions found,
  1680. # functions hit, branches found, branches hit)
  1681. #
  1682. sub get_info_entry($)
  1683. {
  1684. my $testdata_ref = $_[0]->{"test"};
  1685. my $sumcount_ref = $_[0]->{"sum"};
  1686. my $funcdata_ref = $_[0]->{"func"};
  1687. my $checkdata_ref = $_[0]->{"check"};
  1688. my $testfncdata = $_[0]->{"testfnc"};
  1689. my $sumfnccount = $_[0]->{"sumfnc"};
  1690. my $testbrdata = $_[0]->{"testbr"};
  1691. my $sumbrcount = $_[0]->{"sumbr"};
  1692. my $lines_found = $_[0]->{"found"};
  1693. my $lines_hit = $_[0]->{"hit"};
  1694. my $f_found = $_[0]->{"f_found"};
  1695. my $f_hit = $_[0]->{"f_hit"};
  1696. my $br_found = $_[0]->{"b_found"};
  1697. my $br_hit = $_[0]->{"b_hit"};
  1698. return ($testdata_ref, $sumcount_ref, $funcdata_ref, $checkdata_ref,
  1699. $testfncdata, $sumfnccount, $testbrdata, $sumbrcount,
  1700. $lines_found, $lines_hit, $f_found, $f_hit,
  1701. $br_found, $br_hit);
  1702. }
  1703. #
  1704. # set_info_entry(hash_ref, testdata_ref, sumcount_ref, funcdata_ref,
  1705. # checkdata_ref, testfncdata_ref, sumfcncount_ref,
  1706. # testbrdata_ref, sumbrcount_ref[,lines_found,
  1707. # lines_hit, f_found, f_hit, $b_found, $b_hit])
  1708. #
  1709. # Update the hash referenced by HASH_REF with the provided data references.
  1710. #
  1711. sub set_info_entry($$$$$$$$$;$$$$$$)
  1712. {
  1713. my $data_ref = $_[0];
  1714. $data_ref->{"test"} = $_[1];
  1715. $data_ref->{"sum"} = $_[2];
  1716. $data_ref->{"func"} = $_[3];
  1717. $data_ref->{"check"} = $_[4];
  1718. $data_ref->{"testfnc"} = $_[5];
  1719. $data_ref->{"sumfnc"} = $_[6];
  1720. $data_ref->{"testbr"} = $_[7];
  1721. $data_ref->{"sumbr"} = $_[8];
  1722. if (defined($_[9])) { $data_ref->{"found"} = $_[9]; }
  1723. if (defined($_[10])) { $data_ref->{"hit"} = $_[10]; }
  1724. if (defined($_[11])) { $data_ref->{"f_found"} = $_[11]; }
  1725. if (defined($_[12])) { $data_ref->{"f_hit"} = $_[12]; }
  1726. if (defined($_[13])) { $data_ref->{"b_found"} = $_[13]; }
  1727. if (defined($_[14])) { $data_ref->{"b_hit"} = $_[14]; }
  1728. }
  1729. #
  1730. # add_counts(data1_ref, data2_ref)
  1731. #
  1732. # DATA1_REF and DATA2_REF are references to hashes containing a mapping
  1733. #
  1734. # line number -> execution count
  1735. #
  1736. # Return a list (RESULT_REF, LINES_FOUND, LINES_HIT) where RESULT_REF
  1737. # is a reference to a hash containing the combined mapping in which
  1738. # execution counts are added.
  1739. #
  1740. sub add_counts($$)
  1741. {
  1742. my %data1 = %{$_[0]}; # Hash 1
  1743. my %data2 = %{$_[1]}; # Hash 2
  1744. my %result; # Resulting hash
  1745. my $line; # Current line iteration scalar
  1746. my $data1_count; # Count of line in hash1
  1747. my $data2_count; # Count of line in hash2
  1748. my $found = 0; # Total number of lines found
  1749. my $hit = 0; # Number of lines with a count > 0
  1750. foreach $line (keys(%data1))
  1751. {
  1752. $data1_count = $data1{$line};
  1753. $data2_count = $data2{$line};
  1754. # Add counts if present in both hashes
  1755. if (defined($data2_count)) { $data1_count += $data2_count; }
  1756. # Store sum in %result
  1757. $result{$line} = $data1_count;
  1758. $found++;
  1759. if ($data1_count > 0) { $hit++; }
  1760. }
  1761. # Add lines unique to data2
  1762. foreach $line (keys(%data2))
  1763. {
  1764. # Skip lines already in data1
  1765. if (defined($data1{$line})) { next; }
  1766. # Copy count from data2
  1767. $result{$line} = $data2{$line};
  1768. $found++;
  1769. if ($result{$line} > 0) { $hit++; }
  1770. }
  1771. return (\%result, $found, $hit);
  1772. }
  1773. #
  1774. # merge_checksums(ref1, ref2, filename)
  1775. #
  1776. # REF1 and REF2 are references to hashes containing a mapping
  1777. #
  1778. # line number -> checksum
  1779. #
  1780. # Merge checksum lists defined in REF1 and REF2 and return reference to
  1781. # resulting hash. Die if a checksum for a line is defined in both hashes
  1782. # but does not match.
  1783. #
  1784. sub merge_checksums($$$)
  1785. {
  1786. my $ref1 = $_[0];
  1787. my $ref2 = $_[1];
  1788. my $filename = $_[2];
  1789. my %result;
  1790. my $line;
  1791. foreach $line (keys(%{$ref1}))
  1792. {
  1793. if (defined($ref2->{$line}) &&
  1794. ($ref1->{$line} ne $ref2->{$line}))
  1795. {
  1796. die("ERROR: checksum mismatch at $filename:$line\n");
  1797. }
  1798. $result{$line} = $ref1->{$line};
  1799. }
  1800. foreach $line (keys(%{$ref2}))
  1801. {
  1802. $result{$line} = $ref2->{$line};
  1803. }
  1804. return \%result;
  1805. }
  1806. #
  1807. # merge_func_data(funcdata1, funcdata2, filename)
  1808. #
  1809. sub merge_func_data($$$)
  1810. {
  1811. my ($funcdata1, $funcdata2, $filename) = @_;
  1812. my %result;
  1813. my $func;
  1814. if (defined($funcdata1)) {
  1815. %result = %{$funcdata1};
  1816. }
  1817. foreach $func (keys(%{$funcdata2})) {
  1818. my $line1 = $result{$func};
  1819. my $line2 = $funcdata2->{$func};
  1820. if (defined($line1) && ($line1 != $line2)) {
  1821. warn("WARNING: function data mismatch at ".
  1822. "$filename:$line2\n");
  1823. next;
  1824. }
  1825. $result{$func} = $line2;
  1826. }
  1827. return \%result;
  1828. }
  1829. #
  1830. # add_fnccount(fnccount1, fnccount2)
  1831. #
  1832. # Add function call count data. Return list (fnccount_added, f_found, f_hit)
  1833. #
  1834. sub add_fnccount($$)
  1835. {
  1836. my ($fnccount1, $fnccount2) = @_;
  1837. my %result;
  1838. my $f_found;
  1839. my $f_hit;
  1840. my $function;
  1841. if (defined($fnccount1)) {
  1842. %result = %{$fnccount1};
  1843. }
  1844. foreach $function (keys(%{$fnccount2})) {
  1845. $result{$function} += $fnccount2->{$function};
  1846. }
  1847. $f_found = scalar(keys(%result));
  1848. $f_hit = 0;
  1849. foreach $function (keys(%result)) {
  1850. if ($result{$function} > 0) {
  1851. $f_hit++;
  1852. }
  1853. }
  1854. return (\%result, $f_found, $f_hit);
  1855. }
  1856. #
  1857. # add_testfncdata(testfncdata1, testfncdata2)
  1858. #
  1859. # Add function call count data for several tests. Return reference to
  1860. # added_testfncdata.
  1861. #
  1862. sub add_testfncdata($$)
  1863. {
  1864. my ($testfncdata1, $testfncdata2) = @_;
  1865. my %result;
  1866. my $testname;
  1867. foreach $testname (keys(%{$testfncdata1})) {
  1868. if (defined($testfncdata2->{$testname})) {
  1869. my $fnccount;
  1870. # Function call count data for this testname exists
  1871. # in both data sets: merge
  1872. ($fnccount) = add_fnccount(
  1873. $testfncdata1->{$testname},
  1874. $testfncdata2->{$testname});
  1875. $result{$testname} = $fnccount;
  1876. next;
  1877. }
  1878. # Function call count data for this testname is unique to
  1879. # data set 1: copy
  1880. $result{$testname} = $testfncdata1->{$testname};
  1881. }
  1882. # Add count data for testnames unique to data set 2
  1883. foreach $testname (keys(%{$testfncdata2})) {
  1884. if (!defined($result{$testname})) {
  1885. $result{$testname} = $testfncdata2->{$testname};
  1886. }
  1887. }
  1888. return \%result;
  1889. }
  1890. #
  1891. # brcount_to_db(brcount)
  1892. #
  1893. # Convert brcount data to the following format:
  1894. #
  1895. # db: line number -> block hash
  1896. # block hash: block number -> branch hash
  1897. # branch hash: branch number -> taken value
  1898. #
  1899. sub brcount_to_db($)
  1900. {
  1901. my ($brcount) = @_;
  1902. my $line;
  1903. my $db;
  1904. # Add branches from first count to database
  1905. foreach $line (keys(%{$brcount})) {
  1906. my $brdata = $brcount->{$line};
  1907. my $i;
  1908. my $num = br_ivec_len($brdata);
  1909. for ($i = 0; $i < $num; $i++) {
  1910. my ($block, $branch, $taken) = br_ivec_get($brdata, $i);
  1911. $db->{$line}->{$block}->{$branch} = $taken;
  1912. }
  1913. }
  1914. return $db;
  1915. }
  1916. #
  1917. # db_to_brcount(db)
  1918. #
  1919. # Convert branch coverage data back to brcount format.
  1920. #
  1921. sub db_to_brcount($)
  1922. {
  1923. my ($db) = @_;
  1924. my $line;
  1925. my $brcount = {};
  1926. my $br_found = 0;
  1927. my $br_hit = 0;
  1928. # Convert database back to brcount format
  1929. foreach $line (sort({$a <=> $b} keys(%{$db}))) {
  1930. my $ldata = $db->{$line};
  1931. my $brdata;
  1932. my $block;
  1933. foreach $block (sort({$a <=> $b} keys(%{$ldata}))) {
  1934. my $bdata = $ldata->{$block};
  1935. my $branch;
  1936. foreach $branch (sort({$a <=> $b} keys(%{$bdata}))) {
  1937. my $taken = $bdata->{$branch};
  1938. $br_found++;
  1939. $br_hit++ if ($taken ne "-" && $taken > 0);
  1940. $brdata = br_ivec_push($brdata, $block,
  1941. $branch, $taken);
  1942. }
  1943. }
  1944. $brcount->{$line} = $brdata;
  1945. }
  1946. return ($brcount, $br_found, $br_hit);
  1947. }
  1948. # combine_brcount(brcount1, brcount2, type)
  1949. #
  1950. # If add is BR_ADD, add branch coverage data and return list (brcount_added,
  1951. # br_found, br_hit). If add is BR_SUB, subtract the taken values of brcount2
  1952. # from brcount1 and return (brcount_sub, br_found, br_hit).
  1953. #
  1954. sub combine_brcount($$$)
  1955. {
  1956. my ($brcount1, $brcount2, $type) = @_;
  1957. my $line;
  1958. my $block;
  1959. my $branch;
  1960. my $taken;
  1961. my $db;
  1962. my $br_found = 0;
  1963. my $br_hit = 0;
  1964. my $result;
  1965. # Convert branches from first count to database
  1966. $db = brcount_to_db($brcount1);
  1967. # Combine values from database and second count
  1968. foreach $line (keys(%{$brcount2})) {
  1969. my $brdata = $brcount2->{$line};
  1970. my $num = br_ivec_len($brdata);
  1971. my $i;
  1972. for ($i = 0; $i < $num; $i++) {
  1973. ($block, $branch, $taken) = br_ivec_get($brdata, $i);
  1974. my $new_taken = $db->{$line}->{$block}->{$branch};
  1975. if ($type == $BR_ADD) {
  1976. $new_taken = br_taken_add($new_taken, $taken);
  1977. } elsif ($type == $BR_SUB) {
  1978. $new_taken = br_taken_sub($new_taken, $taken);
  1979. }
  1980. $db->{$line}->{$block}->{$branch} = $new_taken
  1981. if (defined($new_taken));
  1982. }
  1983. }
  1984. # Convert database back to brcount format
  1985. ($result, $br_found, $br_hit) = db_to_brcount($db);
  1986. return ($result, $br_found, $br_hit);
  1987. }
  1988. #
  1989. # add_testbrdata(testbrdata1, testbrdata2)
  1990. #
  1991. # Add branch coverage data for several tests. Return reference to
  1992. # added_testbrdata.
  1993. #
  1994. sub add_testbrdata($$)
  1995. {
  1996. my ($testbrdata1, $testbrdata2) = @_;
  1997. my %result;
  1998. my $testname;
  1999. foreach $testname (keys(%{$testbrdata1})) {
  2000. if (defined($testbrdata2->{$testname})) {
  2001. my $brcount;
  2002. # Branch coverage data for this testname exists
  2003. # in both data sets: add
  2004. ($brcount) = combine_brcount(
  2005. $testbrdata1->{$testname},
  2006. $testbrdata2->{$testname}, $BR_ADD);
  2007. $result{$testname} = $brcount;
  2008. next;
  2009. }
  2010. # Branch coverage data for this testname is unique to
  2011. # data set 1: copy
  2012. $result{$testname} = $testbrdata1->{$testname};
  2013. }
  2014. # Add count data for testnames unique to data set 2
  2015. foreach $testname (keys(%{$testbrdata2})) {
  2016. if (!defined($result{$testname})) {
  2017. $result{$testname} = $testbrdata2->{$testname};
  2018. }
  2019. }
  2020. return \%result;
  2021. }
  2022. #
  2023. # combine_info_entries(entry_ref1, entry_ref2, filename)
  2024. #
  2025. # Combine .info data entry hashes referenced by ENTRY_REF1 and ENTRY_REF2.
  2026. # Return reference to resulting hash.
  2027. #
  2028. sub combine_info_entries($$$)
  2029. {
  2030. my $entry1 = $_[0]; # Reference to hash containing first entry
  2031. my $testdata1;
  2032. my $sumcount1;
  2033. my $funcdata1;
  2034. my $checkdata1;
  2035. my $testfncdata1;
  2036. my $sumfnccount1;
  2037. my $testbrdata1;
  2038. my $sumbrcount1;
  2039. my $entry2 = $_[1]; # Reference to hash containing second entry
  2040. my $testdata2;
  2041. my $sumcount2;
  2042. my $funcdata2;
  2043. my $checkdata2;
  2044. my $testfncdata2;
  2045. my $sumfnccount2;
  2046. my $testbrdata2;
  2047. my $sumbrcount2;
  2048. my %result; # Hash containing combined entry
  2049. my %result_testdata;
  2050. my $result_sumcount = {};
  2051. my $result_funcdata;
  2052. my $result_testfncdata;
  2053. my $result_sumfnccount;
  2054. my $result_testbrdata;
  2055. my $result_sumbrcount;
  2056. my $lines_found;
  2057. my $lines_hit;
  2058. my $f_found;
  2059. my $f_hit;
  2060. my $br_found;
  2061. my $br_hit;
  2062. my $testname;
  2063. my $filename = $_[2];
  2064. # Retrieve data
  2065. ($testdata1, $sumcount1, $funcdata1, $checkdata1, $testfncdata1,
  2066. $sumfnccount1, $testbrdata1, $sumbrcount1) = get_info_entry($entry1);
  2067. ($testdata2, $sumcount2, $funcdata2, $checkdata2, $testfncdata2,
  2068. $sumfnccount2, $testbrdata2, $sumbrcount2) = get_info_entry($entry2);
  2069. # Merge checksums
  2070. $checkdata1 = merge_checksums($checkdata1, $checkdata2, $filename);
  2071. # Combine funcdata
  2072. $result_funcdata = merge_func_data($funcdata1, $funcdata2, $filename);
  2073. # Combine function call count data
  2074. $result_testfncdata = add_testfncdata($testfncdata1, $testfncdata2);
  2075. ($result_sumfnccount, $f_found, $f_hit) =
  2076. add_fnccount($sumfnccount1, $sumfnccount2);
  2077. # Combine branch coverage data
  2078. $result_testbrdata = add_testbrdata($testbrdata1, $testbrdata2);
  2079. ($result_sumbrcount, $br_found, $br_hit) =
  2080. combine_brcount($sumbrcount1, $sumbrcount2, $BR_ADD);
  2081. # Combine testdata
  2082. foreach $testname (keys(%{$testdata1}))
  2083. {
  2084. if (defined($testdata2->{$testname}))
  2085. {
  2086. # testname is present in both entries, requires
  2087. # combination
  2088. ($result_testdata{$testname}) =
  2089. add_counts($testdata1->{$testname},
  2090. $testdata2->{$testname});
  2091. }
  2092. else
  2093. {
  2094. # testname only present in entry1, add to result
  2095. $result_testdata{$testname} = $testdata1->{$testname};
  2096. }
  2097. # update sum count hash
  2098. ($result_sumcount, $lines_found, $lines_hit) =
  2099. add_counts($result_sumcount,
  2100. $result_testdata{$testname});
  2101. }
  2102. foreach $testname (keys(%{$testdata2}))
  2103. {
  2104. # Skip testnames already covered by previous iteration
  2105. if (defined($testdata1->{$testname})) { next; }
  2106. # testname only present in entry2, add to result hash
  2107. $result_testdata{$testname} = $testdata2->{$testname};
  2108. # update sum count hash
  2109. ($result_sumcount, $lines_found, $lines_hit) =
  2110. add_counts($result_sumcount,
  2111. $result_testdata{$testname});
  2112. }
  2113. # Calculate resulting sumcount
  2114. # Store result
  2115. set_info_entry(\%result, \%result_testdata, $result_sumcount,
  2116. $result_funcdata, $checkdata1, $result_testfncdata,
  2117. $result_sumfnccount, $result_testbrdata,
  2118. $result_sumbrcount, $lines_found, $lines_hit,
  2119. $f_found, $f_hit, $br_found, $br_hit);
  2120. return(\%result);
  2121. }
  2122. #
  2123. # combine_info_files(info_ref1, info_ref2)
  2124. #
  2125. # Combine .info data in hashes referenced by INFO_REF1 and INFO_REF2. Return
  2126. # reference to resulting hash.
  2127. #
  2128. sub combine_info_files($$)
  2129. {
  2130. my %hash1 = %{$_[0]};
  2131. my %hash2 = %{$_[1]};
  2132. my $filename;
  2133. foreach $filename (keys(%hash2))
  2134. {
  2135. if ($hash1{$filename})
  2136. {
  2137. # Entry already exists in hash1, combine them
  2138. $hash1{$filename} =
  2139. combine_info_entries($hash1{$filename},
  2140. $hash2{$filename},
  2141. $filename);
  2142. }
  2143. else
  2144. {
  2145. # Entry is unique in both hashes, simply add to
  2146. # resulting hash
  2147. $hash1{$filename} = $hash2{$filename};
  2148. }
  2149. }
  2150. return(\%hash1);
  2151. }
  2152. #
  2153. # add_traces()
  2154. #
  2155. sub add_traces()
  2156. {
  2157. my $total_trace;
  2158. my $current_trace;
  2159. my $tracefile;
  2160. my @result;
  2161. local *INFO_HANDLE;
  2162. info("Combining tracefiles.\n");
  2163. foreach $tracefile (@add_tracefile)
  2164. {
  2165. $current_trace = read_info_file($tracefile);
  2166. if ($total_trace)
  2167. {
  2168. $total_trace = combine_info_files($total_trace,
  2169. $current_trace);
  2170. }
  2171. else
  2172. {
  2173. $total_trace = $current_trace;
  2174. }
  2175. }
  2176. # Write combined data
  2177. if ($to_file)
  2178. {
  2179. info("Writing data to $output_filename\n");
  2180. open(INFO_HANDLE, ">", $output_filename)
  2181. or die("ERROR: cannot write to $output_filename!\n");
  2182. @result = write_info_file(*INFO_HANDLE, $total_trace);
  2183. close(*INFO_HANDLE);
  2184. }
  2185. else
  2186. {
  2187. @result = write_info_file(*STDOUT, $total_trace);
  2188. }
  2189. return @result;
  2190. }
  2191. #
  2192. # write_info_file(filehandle, data)
  2193. #
  2194. sub write_info_file(*$)
  2195. {
  2196. local *INFO_HANDLE = $_[0];
  2197. my %data = %{$_[1]};
  2198. my $source_file;
  2199. my $entry;
  2200. my $testdata;
  2201. my $sumcount;
  2202. my $funcdata;
  2203. my $checkdata;
  2204. my $testfncdata;
  2205. my $sumfnccount;
  2206. my $testbrdata;
  2207. my $sumbrcount;
  2208. my $testname;
  2209. my $line;
  2210. my $func;
  2211. my $testcount;
  2212. my $testfnccount;
  2213. my $testbrcount;
  2214. my $found;
  2215. my $hit;
  2216. my $f_found;
  2217. my $f_hit;
  2218. my $br_found;
  2219. my $br_hit;
  2220. my $ln_total_found = 0;
  2221. my $ln_total_hit = 0;
  2222. my $fn_total_found = 0;
  2223. my $fn_total_hit = 0;
  2224. my $br_total_found = 0;
  2225. my $br_total_hit = 0;
  2226. foreach $source_file (sort(keys(%data)))
  2227. {
  2228. $entry = $data{$source_file};
  2229. ($testdata, $sumcount, $funcdata, $checkdata, $testfncdata,
  2230. $sumfnccount, $testbrdata, $sumbrcount, $found, $hit,
  2231. $f_found, $f_hit, $br_found, $br_hit) =
  2232. get_info_entry($entry);
  2233. # Add to totals
  2234. $ln_total_found += $found;
  2235. $ln_total_hit += $hit;
  2236. $fn_total_found += $f_found;
  2237. $fn_total_hit += $f_hit;
  2238. $br_total_found += $br_found;
  2239. $br_total_hit += $br_hit;
  2240. foreach $testname (sort(keys(%{$testdata})))
  2241. {
  2242. $testcount = $testdata->{$testname};
  2243. $testfnccount = $testfncdata->{$testname};
  2244. $testbrcount = $testbrdata->{$testname};
  2245. $found = 0;
  2246. $hit = 0;
  2247. print(INFO_HANDLE "TN:$testname\n");
  2248. print(INFO_HANDLE "SF:$source_file\n");
  2249. # Write function related data
  2250. foreach $func (
  2251. sort({$funcdata->{$a} <=> $funcdata->{$b}}
  2252. keys(%{$funcdata})))
  2253. {
  2254. print(INFO_HANDLE "FN:".$funcdata->{$func}.
  2255. ",$func\n");
  2256. }
  2257. foreach $func (keys(%{$testfnccount})) {
  2258. print(INFO_HANDLE "FNDA:".
  2259. $testfnccount->{$func}.
  2260. ",$func\n");
  2261. }
  2262. ($f_found, $f_hit) =
  2263. get_func_found_and_hit($testfnccount);
  2264. print(INFO_HANDLE "FNF:$f_found\n");
  2265. print(INFO_HANDLE "FNH:$f_hit\n");
  2266. # Write branch related data
  2267. $br_found = 0;
  2268. $br_hit = 0;
  2269. foreach $line (sort({$a <=> $b}
  2270. keys(%{$testbrcount}))) {
  2271. my $brdata = $testbrcount->{$line};
  2272. my $num = br_ivec_len($brdata);
  2273. my $i;
  2274. for ($i = 0; $i < $num; $i++) {
  2275. my ($block, $branch, $taken) =
  2276. br_ivec_get($brdata, $i);
  2277. print(INFO_HANDLE "BRDA:$line,$block,".
  2278. "$branch,$taken\n");
  2279. $br_found++;
  2280. $br_hit++ if ($taken ne '-' &&
  2281. $taken > 0);
  2282. }
  2283. }
  2284. if ($br_found > 0) {
  2285. print(INFO_HANDLE "BRF:$br_found\n");
  2286. print(INFO_HANDLE "BRH:$br_hit\n");
  2287. }
  2288. # Write line related data
  2289. foreach $line (sort({$a <=> $b} keys(%{$testcount})))
  2290. {
  2291. print(INFO_HANDLE "DA:$line,".
  2292. $testcount->{$line}.
  2293. (defined($checkdata->{$line}) &&
  2294. $checksum ?
  2295. ",".$checkdata->{$line} : "")."\n");
  2296. $found++;
  2297. if ($testcount->{$line} > 0)
  2298. {
  2299. $hit++;
  2300. }
  2301. }
  2302. print(INFO_HANDLE "LF:$found\n");
  2303. print(INFO_HANDLE "LH:$hit\n");
  2304. print(INFO_HANDLE "end_of_record\n");
  2305. }
  2306. }
  2307. return ($ln_total_found, $ln_total_hit, $fn_total_found, $fn_total_hit,
  2308. $br_total_found, $br_total_hit);
  2309. }
  2310. #
  2311. # transform_pattern(pattern)
  2312. #
  2313. # Transform shell wildcard expression to equivalent Perl regular expression.
  2314. # Return transformed pattern.
  2315. #
  2316. sub transform_pattern($)
  2317. {
  2318. my $pattern = $_[0];
  2319. # Escape special chars
  2320. $pattern =~ s/\\/\\\\/g;
  2321. $pattern =~ s/\//\\\//g;
  2322. $pattern =~ s/\^/\\\^/g;
  2323. $pattern =~ s/\$/\\\$/g;
  2324. $pattern =~ s/\(/\\\(/g;
  2325. $pattern =~ s/\)/\\\)/g;
  2326. $pattern =~ s/\[/\\\[/g;
  2327. $pattern =~ s/\]/\\\]/g;
  2328. $pattern =~ s/\{/\\\{/g;
  2329. $pattern =~ s/\}/\\\}/g;
  2330. $pattern =~ s/\./\\\./g;
  2331. $pattern =~ s/\,/\\\,/g;
  2332. $pattern =~ s/\|/\\\|/g;
  2333. $pattern =~ s/\+/\\\+/g;
  2334. $pattern =~ s/\!/\\\!/g;
  2335. # Transform ? => (.) and * => (.*)
  2336. $pattern =~ s/\*/\(\.\*\)/g;
  2337. $pattern =~ s/\?/\(\.\)/g;
  2338. return $pattern;
  2339. }
  2340. #
  2341. # extract()
  2342. #
  2343. sub extract()
  2344. {
  2345. my $data = read_info_file($extract);
  2346. my $filename;
  2347. my $keep;
  2348. my $pattern;
  2349. my @pattern_list;
  2350. my $extracted = 0;
  2351. my @result;
  2352. local *INFO_HANDLE;
  2353. # Need perlreg expressions instead of shell pattern
  2354. @pattern_list = map({ transform_pattern($_); } @ARGV);
  2355. # Filter out files which do not match any pattern
  2356. foreach $filename (sort(keys(%{$data})))
  2357. {
  2358. $keep = 0;
  2359. foreach $pattern (@pattern_list)
  2360. {
  2361. $keep ||= ($filename =~ (/^$pattern$/));
  2362. }
  2363. if (!$keep)
  2364. {
  2365. delete($data->{$filename});
  2366. }
  2367. else
  2368. {
  2369. info("Extracting $filename\n"),
  2370. $extracted++;
  2371. }
  2372. }
  2373. # Write extracted data
  2374. if ($to_file)
  2375. {
  2376. info("Extracted $extracted files\n");
  2377. info("Writing data to $output_filename\n");
  2378. open(INFO_HANDLE, ">", $output_filename)
  2379. or die("ERROR: cannot write to $output_filename!\n");
  2380. @result = write_info_file(*INFO_HANDLE, $data);
  2381. close(*INFO_HANDLE);
  2382. }
  2383. else
  2384. {
  2385. @result = write_info_file(*STDOUT, $data);
  2386. }
  2387. return @result;
  2388. }
  2389. #
  2390. # remove()
  2391. #
  2392. sub remove()
  2393. {
  2394. my $data = read_info_file($remove);
  2395. my $filename;
  2396. my $match_found;
  2397. my $pattern;
  2398. my @pattern_list;
  2399. my $removed = 0;
  2400. my @result;
  2401. local *INFO_HANDLE;
  2402. # Need perlreg expressions instead of shell pattern
  2403. @pattern_list = map({ transform_pattern($_); } @ARGV);
  2404. # Filter out files that match the pattern
  2405. foreach $filename (sort(keys(%{$data})))
  2406. {
  2407. $match_found = 0;
  2408. foreach $pattern (@pattern_list)
  2409. {
  2410. $match_found ||= ($filename =~ (/$pattern$/));
  2411. }
  2412. if ($match_found)
  2413. {
  2414. delete($data->{$filename});
  2415. info("Removing $filename\n"),
  2416. $removed++;
  2417. }
  2418. }
  2419. # Write data
  2420. if ($to_file)
  2421. {
  2422. info("Deleted $removed files\n");
  2423. info("Writing data to $output_filename\n");
  2424. open(INFO_HANDLE, ">", $output_filename)
  2425. or die("ERROR: cannot write to $output_filename!\n");
  2426. @result = write_info_file(*INFO_HANDLE, $data);
  2427. close(*INFO_HANDLE);
  2428. }
  2429. else
  2430. {
  2431. @result = write_info_file(*STDOUT, $data);
  2432. }
  2433. return @result;
  2434. }
  2435. # get_prefix(max_width, max_percentage_too_long, path_list)
  2436. #
  2437. # Return a path prefix that satisfies the following requirements:
  2438. # - is shared by more paths in path_list than any other prefix
  2439. # - the percentage of paths which would exceed the given max_width length
  2440. # after applying the prefix does not exceed max_percentage_too_long
  2441. #
  2442. # If multiple prefixes satisfy all requirements, the longest prefix is
  2443. # returned. Return an empty string if no prefix could be found.
  2444. sub get_prefix($$@)
  2445. {
  2446. my ($max_width, $max_long, @path_list) = @_;
  2447. my $path;
  2448. my $ENTRY_NUM = 0;
  2449. my $ENTRY_LONG = 1;
  2450. my %prefix;
  2451. # Build prefix hash
  2452. foreach $path (@path_list) {
  2453. my ($v, $d, $f) = splitpath($path);
  2454. my @dirs = splitdir($d);
  2455. my $p_len = length($path);
  2456. my $i;
  2457. # Remove trailing '/'
  2458. pop(@dirs) if ($dirs[scalar(@dirs) - 1] eq '');
  2459. for ($i = 0; $i < scalar(@dirs); $i++) {
  2460. my $subpath = catpath($v, catdir(@dirs[0..$i]), '');
  2461. my $entry = $prefix{$subpath};
  2462. $entry = [ 0, 0 ] if (!defined($entry));
  2463. $entry->[$ENTRY_NUM]++;
  2464. if (($p_len - length($subpath) - 1) > $max_width) {
  2465. $entry->[$ENTRY_LONG]++;
  2466. }
  2467. $prefix{$subpath} = $entry;
  2468. }
  2469. }
  2470. # Find suitable prefix (sort descending by two keys: 1. number of
  2471. # entries covered by a prefix, 2. length of prefix)
  2472. foreach $path (sort {($prefix{$a}->[$ENTRY_NUM] ==
  2473. $prefix{$b}->[$ENTRY_NUM]) ?
  2474. length($b) <=> length($a) :
  2475. $prefix{$b}->[$ENTRY_NUM] <=>
  2476. $prefix{$a}->[$ENTRY_NUM]}
  2477. keys(%prefix)) {
  2478. my ($num, $long) = @{$prefix{$path}};
  2479. # Check for additional requirement: number of filenames
  2480. # that would be too long may not exceed a certain percentage
  2481. if ($long <= $num * $max_long / 100) {
  2482. return $path;
  2483. }
  2484. }
  2485. return "";
  2486. }
  2487. #
  2488. # shorten_filename(filename, width)
  2489. #
  2490. # Truncate filename if it is longer than width characters.
  2491. #
  2492. sub shorten_filename($$)
  2493. {
  2494. my ($filename, $width) = @_;
  2495. my $l = length($filename);
  2496. my $s;
  2497. my $e;
  2498. return $filename if ($l <= $width);
  2499. $e = int(($width - 3) / 2);
  2500. $s = $width - 3 - $e;
  2501. return substr($filename, 0, $s).'...'.substr($filename, $l - $e);
  2502. }
  2503. sub shorten_number($$)
  2504. {
  2505. my ($number, $width) = @_;
  2506. my $result = sprintf("%*d", $width, $number);
  2507. return $result if (length($result) <= $width);
  2508. $number = $number / 1000;
  2509. return $result if (length($result) <= $width);
  2510. $result = sprintf("%*dk", $width - 1, $number);
  2511. return $result if (length($result) <= $width);
  2512. $number = $number / 1000;
  2513. $result = sprintf("%*dM", $width - 1, $number);
  2514. return $result if (length($result) <= $width);
  2515. return '#';
  2516. }
  2517. sub shorten_rate($$$)
  2518. {
  2519. my ($hit, $found, $width) = @_;
  2520. my $result = rate($hit, $found, "%", 1, $width);
  2521. return $result if (length($result) <= $width);
  2522. $result = rate($hit, $found, "%", 0, $width);
  2523. return $result if (length($result) <= $width);
  2524. return "#";
  2525. }
  2526. #
  2527. # list()
  2528. #
  2529. sub list()
  2530. {
  2531. my $data = read_info_file($list);
  2532. my $filename;
  2533. my $found;
  2534. my $hit;
  2535. my $entry;
  2536. my $fn_found;
  2537. my $fn_hit;
  2538. my $br_found;
  2539. my $br_hit;
  2540. my $total_found = 0;
  2541. my $total_hit = 0;
  2542. my $fn_total_found = 0;
  2543. my $fn_total_hit = 0;
  2544. my $br_total_found = 0;
  2545. my $br_total_hit = 0;
  2546. my $prefix;
  2547. my $strlen = length("Filename");
  2548. my $format;
  2549. my $heading1;
  2550. my $heading2;
  2551. my @footer;
  2552. my $barlen;
  2553. my $rate;
  2554. my $fnrate;
  2555. my $brrate;
  2556. my $lastpath;
  2557. my $F_LN_NUM = 0;
  2558. my $F_LN_RATE = 1;
  2559. my $F_FN_NUM = 2;
  2560. my $F_FN_RATE = 3;
  2561. my $F_BR_NUM = 4;
  2562. my $F_BR_RATE = 5;
  2563. my @fwidth_narrow = (5, 5, 3, 5, 4, 5);
  2564. my @fwidth_wide = (6, 5, 5, 5, 6, 5);
  2565. my @fwidth = @fwidth_wide;
  2566. my $w;
  2567. my $max_width = $opt_list_width;
  2568. my $max_long = $opt_list_truncate_max;
  2569. my $fwidth_narrow_length;
  2570. my $fwidth_wide_length;
  2571. my $got_prefix = 0;
  2572. my $root_prefix = 0;
  2573. # Calculate total width of narrow fields
  2574. $fwidth_narrow_length = 0;
  2575. foreach $w (@fwidth_narrow) {
  2576. $fwidth_narrow_length += $w + 1;
  2577. }
  2578. # Calculate total width of wide fields
  2579. $fwidth_wide_length = 0;
  2580. foreach $w (@fwidth_wide) {
  2581. $fwidth_wide_length += $w + 1;
  2582. }
  2583. # Get common file path prefix
  2584. $prefix = get_prefix($max_width - $fwidth_narrow_length, $max_long,
  2585. keys(%{$data}));
  2586. $root_prefix = 1 if ($prefix eq rootdir());
  2587. $got_prefix = 1 if (length($prefix) > 0);
  2588. $prefix =~ s/\/$//;
  2589. # Get longest filename length
  2590. foreach $filename (keys(%{$data})) {
  2591. if (!$opt_list_full_path) {
  2592. if (!$got_prefix || !$root_prefix &&
  2593. !($filename =~ s/^\Q$prefix\/\E//)) {
  2594. my ($v, $d, $f) = splitpath($filename);
  2595. $filename = $f;
  2596. }
  2597. }
  2598. # Determine maximum length of entries
  2599. if (length($filename) > $strlen) {
  2600. $strlen = length($filename)
  2601. }
  2602. }
  2603. if (!$opt_list_full_path) {
  2604. my $blanks;
  2605. $w = $fwidth_wide_length;
  2606. # Check if all columns fit into max_width characters
  2607. if ($strlen + $fwidth_wide_length > $max_width) {
  2608. # Use narrow fields
  2609. @fwidth = @fwidth_narrow;
  2610. $w = $fwidth_narrow_length;
  2611. if (($strlen + $fwidth_narrow_length) > $max_width) {
  2612. # Truncate filenames at max width
  2613. $strlen = $max_width - $fwidth_narrow_length;
  2614. }
  2615. }
  2616. # Add some blanks between filename and fields if possible
  2617. $blanks = int($strlen * 0.5);
  2618. $blanks = 4 if ($blanks < 4);
  2619. $blanks = 8 if ($blanks > 8);
  2620. if (($strlen + $w + $blanks) < $max_width) {
  2621. $strlen += $blanks;
  2622. } else {
  2623. $strlen = $max_width - $w;
  2624. }
  2625. }
  2626. # Filename
  2627. $w = $strlen;
  2628. $format = "%-${w}s|";
  2629. $heading1 = sprintf("%*s|", $w, "");
  2630. $heading2 = sprintf("%-*s|", $w, "Filename");
  2631. $barlen = $w + 1;
  2632. # Line coverage rate
  2633. $w = $fwidth[$F_LN_RATE];
  2634. $format .= "%${w}s ";
  2635. $heading1 .= sprintf("%-*s |", $w + $fwidth[$F_LN_NUM],
  2636. "Lines");
  2637. $heading2 .= sprintf("%-*s ", $w, "Rate");
  2638. $barlen += $w + 1;
  2639. # Number of lines
  2640. $w = $fwidth[$F_LN_NUM];
  2641. $format .= "%${w}s|";
  2642. $heading2 .= sprintf("%*s|", $w, "Num");
  2643. $barlen += $w + 1;
  2644. # Function coverage rate
  2645. $w = $fwidth[$F_FN_RATE];
  2646. $format .= "%${w}s ";
  2647. $heading1 .= sprintf("%-*s|", $w + $fwidth[$F_FN_NUM] + 1,
  2648. "Functions");
  2649. $heading2 .= sprintf("%-*s ", $w, "Rate");
  2650. $barlen += $w + 1;
  2651. # Number of functions
  2652. $w = $fwidth[$F_FN_NUM];
  2653. $format .= "%${w}s|";
  2654. $heading2 .= sprintf("%*s|", $w, "Num");
  2655. $barlen += $w + 1;
  2656. # Branch coverage rate
  2657. $w = $fwidth[$F_BR_RATE];
  2658. $format .= "%${w}s ";
  2659. $heading1 .= sprintf("%-*s", $w + $fwidth[$F_BR_NUM] + 1,
  2660. "Branches");
  2661. $heading2 .= sprintf("%-*s ", $w, "Rate");
  2662. $barlen += $w + 1;
  2663. # Number of branches
  2664. $w = $fwidth[$F_BR_NUM];
  2665. $format .= "%${w}s";
  2666. $heading2 .= sprintf("%*s", $w, "Num");
  2667. $barlen += $w;
  2668. # Line end
  2669. $format .= "\n";
  2670. $heading1 .= "\n";
  2671. $heading2 .= "\n";
  2672. # Print heading
  2673. print($heading1);
  2674. print($heading2);
  2675. print(("="x$barlen)."\n");
  2676. # Print per file information
  2677. foreach $filename (sort(keys(%{$data})))
  2678. {
  2679. my @file_data;
  2680. my $print_filename = $filename;
  2681. $entry = $data->{$filename};
  2682. if (!$opt_list_full_path) {
  2683. my $p;
  2684. $print_filename = $filename;
  2685. if (!$got_prefix || !$root_prefix &&
  2686. !($print_filename =~ s/^\Q$prefix\/\E//)) {
  2687. my ($v, $d, $f) = splitpath($filename);
  2688. $p = catpath($v, $d, "");
  2689. $p =~ s/\/$//;
  2690. $print_filename = $f;
  2691. } else {
  2692. $p = $prefix;
  2693. }
  2694. if (!defined($lastpath) || $lastpath ne $p) {
  2695. print("\n") if (defined($lastpath));
  2696. $lastpath = $p;
  2697. print("[$lastpath/]\n") if (!$root_prefix);
  2698. }
  2699. $print_filename = shorten_filename($print_filename,
  2700. $strlen);
  2701. }
  2702. (undef, undef, undef, undef, undef, undef, undef, undef,
  2703. $found, $hit, $fn_found, $fn_hit, $br_found, $br_hit) =
  2704. get_info_entry($entry);
  2705. # Assume zero count if there is no function data for this file
  2706. if (!defined($fn_found) || !defined($fn_hit)) {
  2707. $fn_found = 0;
  2708. $fn_hit = 0;
  2709. }
  2710. # Assume zero count if there is no branch data for this file
  2711. if (!defined($br_found) || !defined($br_hit)) {
  2712. $br_found = 0;
  2713. $br_hit = 0;
  2714. }
  2715. # Add line coverage totals
  2716. $total_found += $found;
  2717. $total_hit += $hit;
  2718. # Add function coverage totals
  2719. $fn_total_found += $fn_found;
  2720. $fn_total_hit += $fn_hit;
  2721. # Add branch coverage totals
  2722. $br_total_found += $br_found;
  2723. $br_total_hit += $br_hit;
  2724. # Determine line coverage rate for this file
  2725. $rate = shorten_rate($hit, $found, $fwidth[$F_LN_RATE]);
  2726. # Determine function coverage rate for this file
  2727. $fnrate = shorten_rate($fn_hit, $fn_found, $fwidth[$F_FN_RATE]);
  2728. # Determine branch coverage rate for this file
  2729. $brrate = shorten_rate($br_hit, $br_found, $fwidth[$F_BR_RATE]);
  2730. # Assemble line parameters
  2731. push(@file_data, $print_filename);
  2732. push(@file_data, $rate);
  2733. push(@file_data, shorten_number($found, $fwidth[$F_LN_NUM]));
  2734. push(@file_data, $fnrate);
  2735. push(@file_data, shorten_number($fn_found, $fwidth[$F_FN_NUM]));
  2736. push(@file_data, $brrate);
  2737. push(@file_data, shorten_number($br_found, $fwidth[$F_BR_NUM]));
  2738. # Print assembled line
  2739. printf($format, @file_data);
  2740. }
  2741. # Determine total line coverage rate
  2742. $rate = shorten_rate($total_hit, $total_found, $fwidth[$F_LN_RATE]);
  2743. # Determine total function coverage rate
  2744. $fnrate = shorten_rate($fn_total_hit, $fn_total_found,
  2745. $fwidth[$F_FN_RATE]);
  2746. # Determine total branch coverage rate
  2747. $brrate = shorten_rate($br_total_hit, $br_total_found,
  2748. $fwidth[$F_BR_RATE]);
  2749. # Print separator
  2750. print(("="x$barlen)."\n");
  2751. # Assemble line parameters
  2752. push(@footer, sprintf("%*s", $strlen, "Total:"));
  2753. push(@footer, $rate);
  2754. push(@footer, shorten_number($total_found, $fwidth[$F_LN_NUM]));
  2755. push(@footer, $fnrate);
  2756. push(@footer, shorten_number($fn_total_found, $fwidth[$F_FN_NUM]));
  2757. push(@footer, $brrate);
  2758. push(@footer, shorten_number($br_total_found, $fwidth[$F_BR_NUM]));
  2759. # Print assembled line
  2760. printf($format, @footer);
  2761. }
  2762. #
  2763. # get_common_filename(filename1, filename2)
  2764. #
  2765. # Check for filename components which are common to FILENAME1 and FILENAME2.
  2766. # Upon success, return
  2767. #
  2768. # (common, path1, path2)
  2769. #
  2770. # or 'undef' in case there are no such parts.
  2771. #
  2772. sub get_common_filename($$)
  2773. {
  2774. my @list1 = split("/", $_[0]);
  2775. my @list2 = split("/", $_[1]);
  2776. my @result;
  2777. # Work in reverse order, i.e. beginning with the filename itself
  2778. while (@list1 && @list2 && ($list1[$#list1] eq $list2[$#list2]))
  2779. {
  2780. unshift(@result, pop(@list1));
  2781. pop(@list2);
  2782. }
  2783. # Did we find any similarities?
  2784. if (scalar(@result) > 0)
  2785. {
  2786. return (join("/", @result), join("/", @list1),
  2787. join("/", @list2));
  2788. }
  2789. else
  2790. {
  2791. return undef;
  2792. }
  2793. }
  2794. #
  2795. # strip_directories($path, $depth)
  2796. #
  2797. # Remove DEPTH leading directory levels from PATH.
  2798. #
  2799. sub strip_directories($$)
  2800. {
  2801. my $filename = $_[0];
  2802. my $depth = $_[1];
  2803. my $i;
  2804. if (!defined($depth) || ($depth < 1))
  2805. {
  2806. return $filename;
  2807. }
  2808. for ($i = 0; $i < $depth; $i++)
  2809. {
  2810. $filename =~ s/^[^\/]*\/+(.*)$/$1/;
  2811. }
  2812. return $filename;
  2813. }
  2814. #
  2815. # read_diff(filename)
  2816. #
  2817. # Read diff output from FILENAME to memory. The diff file has to follow the
  2818. # format generated by 'diff -u'. Returns a list of hash references:
  2819. #
  2820. # (mapping, path mapping)
  2821. #
  2822. # mapping: filename -> reference to line hash
  2823. # line hash: line number in new file -> corresponding line number in old file
  2824. #
  2825. # path mapping: filename -> old filename
  2826. #
  2827. # Die in case of error.
  2828. #
  2829. sub read_diff($)
  2830. {
  2831. my $diff_file = $_[0]; # Name of diff file
  2832. my %diff; # Resulting mapping filename -> line hash
  2833. my %paths; # Resulting mapping old path -> new path
  2834. my $mapping; # Reference to current line hash
  2835. my $line; # Contents of current line
  2836. my $num_old; # Current line number in old file
  2837. my $num_new; # Current line number in new file
  2838. my $file_old; # Name of old file in diff section
  2839. my $file_new; # Name of new file in diff section
  2840. my $filename; # Name of common filename of diff section
  2841. my $in_block = 0; # Non-zero while we are inside a diff block
  2842. local *HANDLE; # File handle for reading the diff file
  2843. info("Reading diff $diff_file\n");
  2844. # Check if file exists and is readable
  2845. stat($diff_file);
  2846. if (!(-r _))
  2847. {
  2848. die("ERROR: cannot read file $diff_file!\n");
  2849. }
  2850. # Check if this is really a plain file
  2851. if (!(-f _))
  2852. {
  2853. die("ERROR: not a plain file: $diff_file!\n");
  2854. }
  2855. # Check for .gz extension
  2856. if ($diff_file =~ /\.gz$/)
  2857. {
  2858. # Check for availability of GZIP tool
  2859. system_no_output(1, "gunzip", "-h")
  2860. and die("ERROR: gunzip command not available!\n");
  2861. # Check integrity of compressed file
  2862. system_no_output(1, "gunzip", "-t", $diff_file)
  2863. and die("ERROR: integrity check failed for ".
  2864. "compressed file $diff_file!\n");
  2865. # Open compressed file
  2866. open(HANDLE, "-|", "gunzip -c '$diff_file'")
  2867. or die("ERROR: cannot start gunzip to decompress ".
  2868. "file $_[0]!\n");
  2869. }
  2870. else
  2871. {
  2872. # Open decompressed file
  2873. open(HANDLE, "<", $diff_file)
  2874. or die("ERROR: cannot read file $_[0]!\n");
  2875. }
  2876. # Parse diff file line by line
  2877. while (<HANDLE>)
  2878. {
  2879. chomp($_);
  2880. $line = $_;
  2881. foreach ($line)
  2882. {
  2883. # Filename of old file:
  2884. # --- <filename> <date>
  2885. /^--- (\S+)/ && do
  2886. {
  2887. $file_old = strip_directories($1, $strip);
  2888. last;
  2889. };
  2890. # Filename of new file:
  2891. # +++ <filename> <date>
  2892. /^\+\+\+ (\S+)/ && do
  2893. {
  2894. # Add last file to resulting hash
  2895. if ($filename)
  2896. {
  2897. my %new_hash;
  2898. $diff{$filename} = $mapping;
  2899. $mapping = \%new_hash;
  2900. }
  2901. $file_new = strip_directories($1, $strip);
  2902. $filename = $file_old;
  2903. $paths{$filename} = $file_new;
  2904. $num_old = 1;
  2905. $num_new = 1;
  2906. last;
  2907. };
  2908. # Start of diff block:
  2909. # @@ -old_start,old_num, +new_start,new_num @@
  2910. /^\@\@\s+-(\d+),(\d+)\s+\+(\d+),(\d+)\s+\@\@$/ && do
  2911. {
  2912. $in_block = 1;
  2913. while ($num_old < $1)
  2914. {
  2915. $mapping->{$num_new} = $num_old;
  2916. $num_old++;
  2917. $num_new++;
  2918. }
  2919. last;
  2920. };
  2921. # Unchanged line
  2922. # <line starts with blank>
  2923. /^ / && do
  2924. {
  2925. if ($in_block == 0)
  2926. {
  2927. last;
  2928. }
  2929. $mapping->{$num_new} = $num_old;
  2930. $num_old++;
  2931. $num_new++;
  2932. last;
  2933. };
  2934. # Line as seen in old file
  2935. # <line starts with '-'>
  2936. /^-/ && do
  2937. {
  2938. if ($in_block == 0)
  2939. {
  2940. last;
  2941. }
  2942. $num_old++;
  2943. last;
  2944. };
  2945. # Line as seen in new file
  2946. # <line starts with '+'>
  2947. /^\+/ && do
  2948. {
  2949. if ($in_block == 0)
  2950. {
  2951. last;
  2952. }
  2953. $num_new++;
  2954. last;
  2955. };
  2956. # Empty line
  2957. /^$/ && do
  2958. {
  2959. if ($in_block == 0)
  2960. {
  2961. last;
  2962. }
  2963. $mapping->{$num_new} = $num_old;
  2964. $num_old++;
  2965. $num_new++;
  2966. last;
  2967. };
  2968. }
  2969. }
  2970. close(HANDLE);
  2971. # Add final diff file section to resulting hash
  2972. if ($filename)
  2973. {
  2974. $diff{$filename} = $mapping;
  2975. }
  2976. if (!%diff)
  2977. {
  2978. die("ERROR: no valid diff data found in $diff_file!\n".
  2979. "Make sure to use 'diff -u' when generating the diff ".
  2980. "file.\n");
  2981. }
  2982. return (\%diff, \%paths);
  2983. }
  2984. #
  2985. # apply_diff($count_data, $line_hash)
  2986. #
  2987. # Transform count data using a mapping of lines:
  2988. #
  2989. # $count_data: reference to hash: line number -> data
  2990. # $line_hash: reference to hash: line number new -> line number old
  2991. #
  2992. # Return a reference to transformed count data.
  2993. #
  2994. sub apply_diff($$)
  2995. {
  2996. my $count_data = $_[0]; # Reference to data hash: line -> hash
  2997. my $line_hash = $_[1]; # Reference to line hash: new line -> old line
  2998. my %result; # Resulting hash
  2999. my $last_new = 0; # Last new line number found in line hash
  3000. my $last_old = 0; # Last old line number found in line hash
  3001. # Iterate all new line numbers found in the diff
  3002. foreach (sort({$a <=> $b} keys(%{$line_hash})))
  3003. {
  3004. $last_new = $_;
  3005. $last_old = $line_hash->{$last_new};
  3006. # Is there data associated with the corresponding old line?
  3007. if (defined($count_data->{$line_hash->{$_}}))
  3008. {
  3009. # Copy data to new hash with a new line number
  3010. $result{$_} = $count_data->{$line_hash->{$_}};
  3011. }
  3012. }
  3013. # Transform all other lines which come after the last diff entry
  3014. foreach (sort({$a <=> $b} keys(%{$count_data})))
  3015. {
  3016. if ($_ <= $last_old)
  3017. {
  3018. # Skip lines which were covered by line hash
  3019. next;
  3020. }
  3021. # Copy data to new hash with an offset
  3022. $result{$_ + ($last_new - $last_old)} = $count_data->{$_};
  3023. }
  3024. return \%result;
  3025. }
  3026. #
  3027. # apply_diff_to_brcount(brcount, linedata)
  3028. #
  3029. # Adjust line numbers of branch coverage data according to linedata.
  3030. #
  3031. sub apply_diff_to_brcount($$)
  3032. {
  3033. my ($brcount, $linedata) = @_;
  3034. my $db;
  3035. # Convert brcount to db format
  3036. $db = brcount_to_db($brcount);
  3037. # Apply diff to db format
  3038. $db = apply_diff($db, $linedata);
  3039. # Convert db format back to brcount format
  3040. ($brcount) = db_to_brcount($db);
  3041. return $brcount;
  3042. }
  3043. #
  3044. # get_hash_max(hash_ref)
  3045. #
  3046. # Return the highest integer key from hash.
  3047. #
  3048. sub get_hash_max($)
  3049. {
  3050. my ($hash) = @_;
  3051. my $max;
  3052. foreach (keys(%{$hash})) {
  3053. if (!defined($max)) {
  3054. $max = $_;
  3055. } elsif ($hash->{$_} > $max) {
  3056. $max = $_;
  3057. }
  3058. }
  3059. return $max;
  3060. }
  3061. sub get_hash_reverse($)
  3062. {
  3063. my ($hash) = @_;
  3064. my %result;
  3065. foreach (keys(%{$hash})) {
  3066. $result{$hash->{$_}} = $_;
  3067. }
  3068. return \%result;
  3069. }
  3070. #
  3071. # apply_diff_to_funcdata(funcdata, line_hash)
  3072. #
  3073. sub apply_diff_to_funcdata($$)
  3074. {
  3075. my ($funcdata, $linedata) = @_;
  3076. my $last_new = get_hash_max($linedata);
  3077. my $last_old = $linedata->{$last_new};
  3078. my $func;
  3079. my %result;
  3080. my $line_diff = get_hash_reverse($linedata);
  3081. foreach $func (keys(%{$funcdata})) {
  3082. my $line = $funcdata->{$func};
  3083. if (defined($line_diff->{$line})) {
  3084. $result{$func} = $line_diff->{$line};
  3085. } elsif ($line > $last_old) {
  3086. $result{$func} = $line + $last_new - $last_old;
  3087. }
  3088. }
  3089. return \%result;
  3090. }
  3091. #
  3092. # get_line_hash($filename, $diff_data, $path_data)
  3093. #
  3094. # Find line hash in DIFF_DATA which matches FILENAME. On success, return list
  3095. # line hash. or undef in case of no match. Die if more than one line hashes in
  3096. # DIFF_DATA match.
  3097. #
  3098. sub get_line_hash($$$)
  3099. {
  3100. my $filename = $_[0];
  3101. my $diff_data = $_[1];
  3102. my $path_data = $_[2];
  3103. my $conversion;
  3104. my $old_path;
  3105. my $new_path;
  3106. my $diff_name;
  3107. my $common;
  3108. my $old_depth;
  3109. my $new_depth;
  3110. # Remove trailing slash from diff path
  3111. $diff_path =~ s/\/$//;
  3112. foreach (keys(%{$diff_data}))
  3113. {
  3114. my $sep = "";
  3115. $sep = '/' if (!/^\//);
  3116. # Try to match diff filename with filename
  3117. if ($filename =~ /^\Q$diff_path$sep$_\E$/)
  3118. {
  3119. if ($diff_name)
  3120. {
  3121. # Two files match, choose the more specific one
  3122. # (the one with more path components)
  3123. $old_depth = ($diff_name =~ tr/\///);
  3124. $new_depth = (tr/\///);
  3125. if ($old_depth == $new_depth)
  3126. {
  3127. die("ERROR: diff file contains ".
  3128. "ambiguous entries for ".
  3129. "$filename\n");
  3130. }
  3131. elsif ($new_depth > $old_depth)
  3132. {
  3133. $diff_name = $_;
  3134. }
  3135. }
  3136. else
  3137. {
  3138. $diff_name = $_;
  3139. }
  3140. };
  3141. }
  3142. if ($diff_name)
  3143. {
  3144. # Get converted path
  3145. if ($filename =~ /^(.*)$diff_name$/)
  3146. {
  3147. ($common, $old_path, $new_path) =
  3148. get_common_filename($filename,
  3149. $1.$path_data->{$diff_name});
  3150. }
  3151. return ($diff_data->{$diff_name}, $old_path, $new_path);
  3152. }
  3153. else
  3154. {
  3155. return undef;
  3156. }
  3157. }
  3158. #
  3159. # convert_paths(trace_data, path_conversion_data)
  3160. #
  3161. # Rename all paths in TRACE_DATA which show up in PATH_CONVERSION_DATA.
  3162. #
  3163. sub convert_paths($$)
  3164. {
  3165. my $trace_data = $_[0];
  3166. my $path_conversion_data = $_[1];
  3167. my $filename;
  3168. my $new_path;
  3169. if (scalar(keys(%{$path_conversion_data})) == 0)
  3170. {
  3171. info("No path conversion data available.\n");
  3172. return;
  3173. }
  3174. # Expand path conversion list
  3175. foreach $filename (keys(%{$path_conversion_data}))
  3176. {
  3177. $new_path = $path_conversion_data->{$filename};
  3178. while (($filename =~ s/^(.*)\/[^\/]+$/$1/) &&
  3179. ($new_path =~ s/^(.*)\/[^\/]+$/$1/) &&
  3180. ($filename ne $new_path))
  3181. {
  3182. $path_conversion_data->{$filename} = $new_path;
  3183. }
  3184. }
  3185. # Adjust paths
  3186. FILENAME: foreach $filename (keys(%{$trace_data}))
  3187. {
  3188. # Find a path in our conversion table that matches, starting
  3189. # with the longest path
  3190. foreach (sort({length($b) <=> length($a)}
  3191. keys(%{$path_conversion_data})))
  3192. {
  3193. # Is this path a prefix of our filename?
  3194. if (!($filename =~ /^$_(.*)$/))
  3195. {
  3196. next;
  3197. }
  3198. $new_path = $path_conversion_data->{$_}.$1;
  3199. # Make sure not to overwrite an existing entry under
  3200. # that path name
  3201. if ($trace_data->{$new_path})
  3202. {
  3203. # Need to combine entries
  3204. $trace_data->{$new_path} =
  3205. combine_info_entries(
  3206. $trace_data->{$filename},
  3207. $trace_data->{$new_path},
  3208. $filename);
  3209. }
  3210. else
  3211. {
  3212. # Simply rename entry
  3213. $trace_data->{$new_path} =
  3214. $trace_data->{$filename};
  3215. }
  3216. delete($trace_data->{$filename});
  3217. next FILENAME;
  3218. }
  3219. info("No conversion available for filename $filename\n");
  3220. }
  3221. }
  3222. #
  3223. # sub adjust_fncdata(funcdata, testfncdata, sumfnccount)
  3224. #
  3225. # Remove function call count data from testfncdata and sumfnccount which
  3226. # is no longer present in funcdata.
  3227. #
  3228. sub adjust_fncdata($$$)
  3229. {
  3230. my ($funcdata, $testfncdata, $sumfnccount) = @_;
  3231. my $testname;
  3232. my $func;
  3233. my $f_found;
  3234. my $f_hit;
  3235. # Remove count data in testfncdata for functions which are no longer
  3236. # in funcdata
  3237. foreach $testname (%{$testfncdata}) {
  3238. my $fnccount = $testfncdata->{$testname};
  3239. foreach $func (%{$fnccount}) {
  3240. if (!defined($funcdata->{$func})) {
  3241. delete($fnccount->{$func});
  3242. }
  3243. }
  3244. }
  3245. # Remove count data in sumfnccount for functions which are no longer
  3246. # in funcdata
  3247. foreach $func (%{$sumfnccount}) {
  3248. if (!defined($funcdata->{$func})) {
  3249. delete($sumfnccount->{$func});
  3250. }
  3251. }
  3252. }
  3253. #
  3254. # get_func_found_and_hit(sumfnccount)
  3255. #
  3256. # Return (f_found, f_hit) for sumfnccount
  3257. #
  3258. sub get_func_found_and_hit($)
  3259. {
  3260. my ($sumfnccount) = @_;
  3261. my $function;
  3262. my $f_found;
  3263. my $f_hit;
  3264. $f_found = scalar(keys(%{$sumfnccount}));
  3265. $f_hit = 0;
  3266. foreach $function (keys(%{$sumfnccount})) {
  3267. if ($sumfnccount->{$function} > 0) {
  3268. $f_hit++;
  3269. }
  3270. }
  3271. return ($f_found, $f_hit);
  3272. }
  3273. #
  3274. # diff()
  3275. #
  3276. sub diff()
  3277. {
  3278. my $trace_data = read_info_file($diff);
  3279. my $diff_data;
  3280. my $path_data;
  3281. my $old_path;
  3282. my $new_path;
  3283. my %path_conversion_data;
  3284. my $filename;
  3285. my $line_hash;
  3286. my $new_name;
  3287. my $entry;
  3288. my $testdata;
  3289. my $testname;
  3290. my $sumcount;
  3291. my $funcdata;
  3292. my $checkdata;
  3293. my $testfncdata;
  3294. my $sumfnccount;
  3295. my $testbrdata;
  3296. my $sumbrcount;
  3297. my $found;
  3298. my $hit;
  3299. my $f_found;
  3300. my $f_hit;
  3301. my $br_found;
  3302. my $br_hit;
  3303. my $converted = 0;
  3304. my $unchanged = 0;
  3305. my @result;
  3306. local *INFO_HANDLE;
  3307. ($diff_data, $path_data) = read_diff($ARGV[0]);
  3308. foreach $filename (sort(keys(%{$trace_data})))
  3309. {
  3310. # Find a diff section corresponding to this file
  3311. ($line_hash, $old_path, $new_path) =
  3312. get_line_hash($filename, $diff_data, $path_data);
  3313. if (!$line_hash)
  3314. {
  3315. # There's no diff section for this file
  3316. $unchanged++;
  3317. next;
  3318. }
  3319. $converted++;
  3320. if ($old_path && $new_path && ($old_path ne $new_path))
  3321. {
  3322. $path_conversion_data{$old_path} = $new_path;
  3323. }
  3324. # Check for deleted files
  3325. if (scalar(keys(%{$line_hash})) == 0)
  3326. {
  3327. info("Removing $filename\n");
  3328. delete($trace_data->{$filename});
  3329. next;
  3330. }
  3331. info("Converting $filename\n");
  3332. $entry = $trace_data->{$filename};
  3333. ($testdata, $sumcount, $funcdata, $checkdata, $testfncdata,
  3334. $sumfnccount, $testbrdata, $sumbrcount) =
  3335. get_info_entry($entry);
  3336. # Convert test data
  3337. foreach $testname (keys(%{$testdata}))
  3338. {
  3339. # Adjust line numbers of line coverage data
  3340. $testdata->{$testname} =
  3341. apply_diff($testdata->{$testname}, $line_hash);
  3342. # Adjust line numbers of branch coverage data
  3343. $testbrdata->{$testname} =
  3344. apply_diff_to_brcount($testbrdata->{$testname},
  3345. $line_hash);
  3346. # Remove empty sets of test data
  3347. if (scalar(keys(%{$testdata->{$testname}})) == 0)
  3348. {
  3349. delete($testdata->{$testname});
  3350. delete($testfncdata->{$testname});
  3351. delete($testbrdata->{$testname});
  3352. }
  3353. }
  3354. # Rename test data to indicate conversion
  3355. foreach $testname (keys(%{$testdata}))
  3356. {
  3357. # Skip testnames which already contain an extension
  3358. if ($testname =~ /,[^,]+$/)
  3359. {
  3360. next;
  3361. }
  3362. # Check for name conflict
  3363. if (defined($testdata->{$testname.",diff"}))
  3364. {
  3365. # Add counts
  3366. ($testdata->{$testname}) = add_counts(
  3367. $testdata->{$testname},
  3368. $testdata->{$testname.",diff"});
  3369. delete($testdata->{$testname.",diff"});
  3370. # Add function call counts
  3371. ($testfncdata->{$testname}) = add_fnccount(
  3372. $testfncdata->{$testname},
  3373. $testfncdata->{$testname.",diff"});
  3374. delete($testfncdata->{$testname.",diff"});
  3375. # Add branch counts
  3376. ($testbrdata->{$testname}) = combine_brcount(
  3377. $testbrdata->{$testname},
  3378. $testbrdata->{$testname.",diff"},
  3379. $BR_ADD);
  3380. delete($testbrdata->{$testname.",diff"});
  3381. }
  3382. # Move test data to new testname
  3383. $testdata->{$testname.",diff"} = $testdata->{$testname};
  3384. delete($testdata->{$testname});
  3385. # Move function call count data to new testname
  3386. $testfncdata->{$testname.",diff"} =
  3387. $testfncdata->{$testname};
  3388. delete($testfncdata->{$testname});
  3389. # Move branch count data to new testname
  3390. $testbrdata->{$testname.",diff"} =
  3391. $testbrdata->{$testname};
  3392. delete($testbrdata->{$testname});
  3393. }
  3394. # Convert summary of test data
  3395. $sumcount = apply_diff($sumcount, $line_hash);
  3396. # Convert function data
  3397. $funcdata = apply_diff_to_funcdata($funcdata, $line_hash);
  3398. # Convert branch coverage data
  3399. $sumbrcount = apply_diff_to_brcount($sumbrcount, $line_hash);
  3400. # Update found/hit numbers
  3401. # Convert checksum data
  3402. $checkdata = apply_diff($checkdata, $line_hash);
  3403. # Convert function call count data
  3404. adjust_fncdata($funcdata, $testfncdata, $sumfnccount);
  3405. ($f_found, $f_hit) = get_func_found_and_hit($sumfnccount);
  3406. ($br_found, $br_hit) = get_br_found_and_hit($sumbrcount);
  3407. # Update found/hit numbers
  3408. $found = 0;
  3409. $hit = 0;
  3410. foreach (keys(%{$sumcount}))
  3411. {
  3412. $found++;
  3413. if ($sumcount->{$_} > 0)
  3414. {
  3415. $hit++;
  3416. }
  3417. }
  3418. if ($found > 0)
  3419. {
  3420. # Store converted entry
  3421. set_info_entry($entry, $testdata, $sumcount, $funcdata,
  3422. $checkdata, $testfncdata, $sumfnccount,
  3423. $testbrdata, $sumbrcount, $found, $hit,
  3424. $f_found, $f_hit, $br_found, $br_hit);
  3425. }
  3426. else
  3427. {
  3428. # Remove empty data set
  3429. delete($trace_data->{$filename});
  3430. }
  3431. }
  3432. # Convert filenames as well if requested
  3433. if ($convert_filenames)
  3434. {
  3435. convert_paths($trace_data, \%path_conversion_data);
  3436. }
  3437. info("$converted entr".($converted != 1 ? "ies" : "y")." converted, ".
  3438. "$unchanged entr".($unchanged != 1 ? "ies" : "y")." left ".
  3439. "unchanged.\n");
  3440. # Write data
  3441. if ($to_file)
  3442. {
  3443. info("Writing data to $output_filename\n");
  3444. open(INFO_HANDLE, ">", $output_filename)
  3445. or die("ERROR: cannot write to $output_filename!\n");
  3446. @result = write_info_file(*INFO_HANDLE, $trace_data);
  3447. close(*INFO_HANDLE);
  3448. }
  3449. else
  3450. {
  3451. @result = write_info_file(*STDOUT, $trace_data);
  3452. }
  3453. return @result;
  3454. }
  3455. #
  3456. # summary()
  3457. #
  3458. sub summary()
  3459. {
  3460. my $filename;
  3461. my $current;
  3462. my $total;
  3463. my $ln_total_found;
  3464. my $ln_total_hit;
  3465. my $fn_total_found;
  3466. my $fn_total_hit;
  3467. my $br_total_found;
  3468. my $br_total_hit;
  3469. # Read and combine trace files
  3470. foreach $filename (@opt_summary) {
  3471. $current = read_info_file($filename);
  3472. if (!defined($total)) {
  3473. $total = $current;
  3474. } else {
  3475. $total = combine_info_files($total, $current);
  3476. }
  3477. }
  3478. # Calculate coverage data
  3479. foreach $filename (keys(%{$total}))
  3480. {
  3481. my $entry = $total->{$filename};
  3482. my $ln_found;
  3483. my $ln_hit;
  3484. my $fn_found;
  3485. my $fn_hit;
  3486. my $br_found;
  3487. my $br_hit;
  3488. (undef, undef, undef, undef, undef, undef, undef, undef,
  3489. $ln_found, $ln_hit, $fn_found, $fn_hit, $br_found,
  3490. $br_hit) = get_info_entry($entry);
  3491. # Add to totals
  3492. $ln_total_found += $ln_found;
  3493. $ln_total_hit += $ln_hit;
  3494. $fn_total_found += $fn_found;
  3495. $fn_total_hit += $fn_hit;
  3496. $br_total_found += $br_found;
  3497. $br_total_hit += $br_hit;
  3498. }
  3499. return ($ln_total_found, $ln_total_hit, $fn_total_found, $fn_total_hit,
  3500. $br_total_found, $br_total_hit);
  3501. }
  3502. #
  3503. # system_no_output(mode, parameters)
  3504. #
  3505. # Call an external program using PARAMETERS while suppressing depending on
  3506. # the value of MODE:
  3507. #
  3508. # MODE & 1: suppress STDOUT
  3509. # MODE & 2: suppress STDERR
  3510. #
  3511. # Return 0 on success, non-zero otherwise.
  3512. #
  3513. sub system_no_output($@)
  3514. {
  3515. my $mode = shift;
  3516. my $result;
  3517. local *OLD_STDERR;
  3518. local *OLD_STDOUT;
  3519. # Save old stdout and stderr handles
  3520. ($mode & 1) && open(OLD_STDOUT, ">>&", "STDOUT");
  3521. ($mode & 2) && open(OLD_STDERR, ">>&", "STDERR");
  3522. # Redirect to /dev/null
  3523. ($mode & 1) && open(STDOUT, ">", "/dev/null");
  3524. ($mode & 2) && open(STDERR, ">", "/dev/null");
  3525. system(@_);
  3526. $result = $?;
  3527. # Close redirected handles
  3528. ($mode & 1) && close(STDOUT);
  3529. ($mode & 2) && close(STDERR);
  3530. # Restore old handles
  3531. ($mode & 1) && open(STDOUT, ">>&", "OLD_STDOUT");
  3532. ($mode & 2) && open(STDERR, ">>&", "OLD_STDERR");
  3533. return $result;
  3534. }
  3535. #
  3536. # read_config(filename)
  3537. #
  3538. # Read configuration file FILENAME and return a reference to a hash containing
  3539. # all valid key=value pairs found.
  3540. #
  3541. sub read_config($)
  3542. {
  3543. my $filename = $_[0];
  3544. my %result;
  3545. my $key;
  3546. my $value;
  3547. local *HANDLE;
  3548. if (!open(HANDLE, "<", $filename))
  3549. {
  3550. warn("WARNING: cannot read configuration file $filename\n");
  3551. return undef;
  3552. }
  3553. while (<HANDLE>)
  3554. {
  3555. chomp;
  3556. # Skip comments
  3557. s/#.*//;
  3558. # Remove leading blanks
  3559. s/^\s+//;
  3560. # Remove trailing blanks
  3561. s/\s+$//;
  3562. next unless length;
  3563. ($key, $value) = split(/\s*=\s*/, $_, 2);
  3564. if (defined($key) && defined($value))
  3565. {
  3566. $result{$key} = $value;
  3567. }
  3568. else
  3569. {
  3570. warn("WARNING: malformed statement in line $. ".
  3571. "of configuration file $filename\n");
  3572. }
  3573. }
  3574. close(HANDLE);
  3575. return \%result;
  3576. }
  3577. #
  3578. # apply_config(REF)
  3579. #
  3580. # REF is a reference to a hash containing the following mapping:
  3581. #
  3582. # key_string => var_ref
  3583. #
  3584. # where KEY_STRING is a keyword and VAR_REF is a reference to an associated
  3585. # variable. If the global configuration hashes CONFIG or OPT_RC contain a value
  3586. # for keyword KEY_STRING, VAR_REF will be assigned the value for that keyword.
  3587. #
  3588. sub apply_config($)
  3589. {
  3590. my $ref = $_[0];
  3591. foreach (keys(%{$ref}))
  3592. {
  3593. if (defined($opt_rc{$_})) {
  3594. ${$ref->{$_}} = $opt_rc{$_};
  3595. } elsif (defined($config->{$_})) {
  3596. ${$ref->{$_}} = $config->{$_};
  3597. }
  3598. }
  3599. }
  3600. sub warn_handler($)
  3601. {
  3602. my ($msg) = @_;
  3603. temp_cleanup();
  3604. warn("$tool_name: $msg");
  3605. }
  3606. sub die_handler($)
  3607. {
  3608. my ($msg) = @_;
  3609. temp_cleanup();
  3610. die("$tool_name: $msg");
  3611. }
  3612. sub abort_handler($)
  3613. {
  3614. temp_cleanup();
  3615. exit(1);
  3616. }
  3617. sub temp_cleanup()
  3618. {
  3619. if (@temp_dirs) {
  3620. info("Removing temporary directories.\n");
  3621. foreach (@temp_dirs) {
  3622. rmtree($_);
  3623. }
  3624. @temp_dirs = ();
  3625. }
  3626. }
  3627. sub setup_gkv_sys()
  3628. {
  3629. system_no_output(3, "mount", "-t", "debugfs", "nodev",
  3630. "/sys/kernel/debug");
  3631. }
  3632. sub setup_gkv_proc()
  3633. {
  3634. if (system_no_output(3, "modprobe", "gcov_proc")) {
  3635. system_no_output(3, "modprobe", "gcov_prof");
  3636. }
  3637. }
  3638. sub check_gkv_sys($)
  3639. {
  3640. my ($dir) = @_;
  3641. if (-e "$dir/reset") {
  3642. return 1;
  3643. }
  3644. return 0;
  3645. }
  3646. sub check_gkv_proc($)
  3647. {
  3648. my ($dir) = @_;
  3649. if (-e "$dir/vmlinux") {
  3650. return 1;
  3651. }
  3652. return 0;
  3653. }
  3654. sub setup_gkv()
  3655. {
  3656. my $dir;
  3657. my $sys_dir = "/sys/kernel/debug/gcov";
  3658. my $proc_dir = "/proc/gcov";
  3659. my @todo;
  3660. if (!defined($gcov_dir)) {
  3661. info("Auto-detecting gcov kernel support.\n");
  3662. @todo = ( "cs", "cp", "ss", "cs", "sp", "cp" );
  3663. } elsif ($gcov_dir =~ /proc/) {
  3664. info("Checking gcov kernel support at $gcov_dir ".
  3665. "(user-specified).\n");
  3666. @todo = ( "cp", "sp", "cp", "cs", "ss", "cs");
  3667. } else {
  3668. info("Checking gcov kernel support at $gcov_dir ".
  3669. "(user-specified).\n");
  3670. @todo = ( "cs", "ss", "cs", "cp", "sp", "cp", );
  3671. }
  3672. foreach (@todo) {
  3673. if ($_ eq "cs") {
  3674. # Check /sys
  3675. $dir = defined($gcov_dir) ? $gcov_dir : $sys_dir;
  3676. if (check_gkv_sys($dir)) {
  3677. info("Found ".$GKV_NAME[$GKV_SYS]." gcov ".
  3678. "kernel support at $dir\n");
  3679. return ($GKV_SYS, $dir);
  3680. }
  3681. } elsif ($_ eq "cp") {
  3682. # Check /proc
  3683. $dir = defined($gcov_dir) ? $gcov_dir : $proc_dir;
  3684. if (check_gkv_proc($dir)) {
  3685. info("Found ".$GKV_NAME[$GKV_PROC]." gcov ".
  3686. "kernel support at $dir\n");
  3687. return ($GKV_PROC, $dir);
  3688. }
  3689. } elsif ($_ eq "ss") {
  3690. # Setup /sys
  3691. setup_gkv_sys();
  3692. } elsif ($_ eq "sp") {
  3693. # Setup /proc
  3694. setup_gkv_proc();
  3695. }
  3696. }
  3697. if (defined($gcov_dir)) {
  3698. die("ERROR: could not find gcov kernel data at $gcov_dir\n");
  3699. } else {
  3700. die("ERROR: no gcov kernel data found\n");
  3701. }
  3702. }
  3703. #
  3704. # get_overall_line(found, hit, name_singular, name_plural)
  3705. #
  3706. # Return a string containing overall information for the specified
  3707. # found/hit data.
  3708. #
  3709. sub get_overall_line($$$$)
  3710. {
  3711. my ($found, $hit, $name_sn, $name_pl) = @_;
  3712. my $name;
  3713. return "no data found" if (!defined($found) || $found == 0);
  3714. $name = ($found == 1) ? $name_sn : $name_pl;
  3715. return rate($hit, $found, "% ($hit of $found $name)");
  3716. }
  3717. #
  3718. # print_overall_rate(ln_do, ln_found, ln_hit, fn_do, fn_found, fn_hit, br_do
  3719. # br_found, br_hit)
  3720. #
  3721. # Print overall coverage rates for the specified coverage types.
  3722. #
  3723. sub print_overall_rate($$$$$$$$$)
  3724. {
  3725. my ($ln_do, $ln_found, $ln_hit, $fn_do, $fn_found, $fn_hit,
  3726. $br_do, $br_found, $br_hit) = @_;
  3727. info("Summary coverage rate:\n");
  3728. info(" lines......: %s\n",
  3729. get_overall_line($ln_found, $ln_hit, "line", "lines"))
  3730. if ($ln_do);
  3731. info(" functions..: %s\n",
  3732. get_overall_line($fn_found, $fn_hit, "function", "functions"))
  3733. if ($fn_do);
  3734. info(" branches...: %s\n",
  3735. get_overall_line($br_found, $br_hit, "branch", "branches"))
  3736. if ($br_do);
  3737. }
  3738. #
  3739. # rate(hit, found[, suffix, precision, width])
  3740. #
  3741. # Return the coverage rate [0..100] for HIT and FOUND values. 0 is only
  3742. # returned when HIT is 0. 100 is only returned when HIT equals FOUND.
  3743. # PRECISION specifies the precision of the result. SUFFIX defines a
  3744. # string that is appended to the result if FOUND is non-zero. Spaces
  3745. # are added to the start of the resulting string until it is at least WIDTH
  3746. # characters wide.
  3747. #
  3748. sub rate($$;$$$)
  3749. {
  3750. my ($hit, $found, $suffix, $precision, $width) = @_;
  3751. my $rate;
  3752. # Assign defaults if necessary
  3753. $precision = 1 if (!defined($precision));
  3754. $suffix = "" if (!defined($suffix));
  3755. $width = 0 if (!defined($width));
  3756. return sprintf("%*s", $width, "-") if (!defined($found) || $found == 0);
  3757. $rate = sprintf("%.*f", $precision, $hit * 100 / $found);
  3758. # Adjust rates if necessary
  3759. if ($rate == 0 && $hit > 0) {
  3760. $rate = sprintf("%.*f", $precision, 1 / 10 ** $precision);
  3761. } elsif ($rate == 100 && $hit != $found) {
  3762. $rate = sprintf("%.*f", $precision, 100 - 1 / 10 ** $precision);
  3763. }
  3764. return sprintf("%*s", $width, $rate.$suffix);
  3765. }