gcov.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513
  1. /* Gcov.c: prepend line execution counts and branch probabilities to a
  2. source file.
  3. Copyright (C) 1990-2015 Free Software Foundation, Inc.
  4. Contributed by James E. Wilson of Cygnus Support.
  5. Mangled by Bob Manson of Cygnus Support.
  6. Mangled further by Nathan Sidwell <nathan@codesourcery.com>
  7. Gcov is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3, or (at your option)
  10. any later version.
  11. Gcov is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with Gcov; see the file COPYING3. If not see
  17. <http://www.gnu.org/licenses/>. */
  18. /* ??? Print a list of the ten blocks with the highest execution counts,
  19. and list the line numbers corresponding to those blocks. Also, perhaps
  20. list the line numbers with the highest execution counts, only printing
  21. the first if there are several which are all listed in the same block. */
  22. /* ??? Should have an option to print the number of basic blocks, and the
  23. percent of them that are covered. */
  24. /* Need an option to show individual block counts, and show
  25. probabilities of fall through arcs. */
  26. #include "config.h"
  27. #include "system.h"
  28. #include "coretypes.h"
  29. #include "tm.h"
  30. #include "intl.h"
  31. #include "diagnostic.h"
  32. #include "version.h"
  33. #include "demangle.h"
  34. #include <getopt.h>
  35. #define IN_GCOV 1
  36. #include "gcov-io.h"
  37. #include "gcov-io.c"
  38. /* The gcno file is generated by -ftest-coverage option. The gcda file is
  39. generated by a program compiled with -fprofile-arcs. Their formats
  40. are documented in gcov-io.h. */
  41. /* The functions in this file for creating and solution program flow graphs
  42. are very similar to functions in the gcc source file profile.c. In
  43. some places we make use of the knowledge of how profile.c works to
  44. select particular algorithms here. */
  45. /* The code validates that the profile information read in corresponds
  46. to the code currently being compiled. Rather than checking for
  47. identical files, the code below compares a checksum on the CFG
  48. (based on the order of basic blocks and the arcs in the CFG). If
  49. the CFG checksum in the gcda file match the CFG checksum in the
  50. gcno file, the profile data will be used. */
  51. /* This is the size of the buffer used to read in source file lines. */
  52. struct function_info;
  53. struct block_info;
  54. struct source_info;
  55. /* Describes an arc between two basic blocks. */
  56. typedef struct arc_info
  57. {
  58. /* source and destination blocks. */
  59. struct block_info *src;
  60. struct block_info *dst;
  61. /* transition counts. */
  62. gcov_type count;
  63. /* used in cycle search, so that we do not clobber original counts. */
  64. gcov_type cs_count;
  65. unsigned int count_valid : 1;
  66. unsigned int on_tree : 1;
  67. unsigned int fake : 1;
  68. unsigned int fall_through : 1;
  69. /* Arc to a catch handler. */
  70. unsigned int is_throw : 1;
  71. /* Arc is for a function that abnormally returns. */
  72. unsigned int is_call_non_return : 1;
  73. /* Arc is for catch/setjmp. */
  74. unsigned int is_nonlocal_return : 1;
  75. /* Is an unconditional branch. */
  76. unsigned int is_unconditional : 1;
  77. /* Loop making arc. */
  78. unsigned int cycle : 1;
  79. /* Next branch on line. */
  80. struct arc_info *line_next;
  81. /* Links to next arc on src and dst lists. */
  82. struct arc_info *succ_next;
  83. struct arc_info *pred_next;
  84. } arc_t;
  85. /* Describes a basic block. Contains lists of arcs to successor and
  86. predecessor blocks. */
  87. typedef struct block_info
  88. {
  89. /* Chain of exit and entry arcs. */
  90. arc_t *succ;
  91. arc_t *pred;
  92. /* Number of unprocessed exit and entry arcs. */
  93. gcov_type num_succ;
  94. gcov_type num_pred;
  95. /* Block execution count. */
  96. gcov_type count;
  97. unsigned flags : 12;
  98. unsigned count_valid : 1;
  99. unsigned valid_chain : 1;
  100. unsigned invalid_chain : 1;
  101. unsigned exceptional : 1;
  102. /* Block is a call instrumenting site. */
  103. unsigned is_call_site : 1; /* Does the call. */
  104. unsigned is_call_return : 1; /* Is the return. */
  105. /* Block is a landing pad for longjmp or throw. */
  106. unsigned is_nonlocal_return : 1;
  107. union
  108. {
  109. struct
  110. {
  111. /* Array of line numbers and source files. source files are
  112. introduced by a linenumber of zero, the next 'line number' is
  113. the number of the source file. Always starts with a source
  114. file. */
  115. unsigned *encoding;
  116. unsigned num;
  117. } line; /* Valid until blocks are linked onto lines */
  118. struct
  119. {
  120. /* Single line graph cycle workspace. Used for all-blocks
  121. mode. */
  122. arc_t *arc;
  123. unsigned ident;
  124. } cycle; /* Used in all-blocks mode, after blocks are linked onto
  125. lines. */
  126. } u;
  127. /* Temporary chain for solving graph, and for chaining blocks on one
  128. line. */
  129. struct block_info *chain;
  130. } block_t;
  131. /* Describes a single function. Contains an array of basic blocks. */
  132. typedef struct function_info
  133. {
  134. /* Name of function. */
  135. char *name;
  136. char *demangled_name;
  137. unsigned ident;
  138. unsigned lineno_checksum;
  139. unsigned cfg_checksum;
  140. /* The graph contains at least one fake incoming edge. */
  141. unsigned has_catch : 1;
  142. /* Array of basic blocks. Like in GCC, the entry block is
  143. at blocks[0] and the exit block is at blocks[1]. */
  144. #define ENTRY_BLOCK (0)
  145. #define EXIT_BLOCK (1)
  146. block_t *blocks;
  147. unsigned num_blocks;
  148. unsigned blocks_executed;
  149. /* Raw arc coverage counts. */
  150. gcov_type *counts;
  151. unsigned num_counts;
  152. /* First line number & file. */
  153. unsigned line;
  154. unsigned src;
  155. /* Next function in same source file. */
  156. struct function_info *line_next;
  157. /* Next function. */
  158. struct function_info *next;
  159. } function_t;
  160. /* Describes coverage of a file or function. */
  161. typedef struct coverage_info
  162. {
  163. int lines;
  164. int lines_executed;
  165. int branches;
  166. int branches_executed;
  167. int branches_taken;
  168. int calls;
  169. int calls_executed;
  170. char *name;
  171. } coverage_t;
  172. /* Describes a single line of source. Contains a chain of basic blocks
  173. with code on it. */
  174. typedef struct line_info
  175. {
  176. gcov_type count; /* execution count */
  177. union
  178. {
  179. arc_t *branches; /* branches from blocks that end on this
  180. line. Used for branch-counts when not
  181. all-blocks mode. */
  182. block_t *blocks; /* blocks which start on this line. Used
  183. in all-blocks mode. */
  184. } u;
  185. unsigned exists : 1;
  186. unsigned unexceptional : 1;
  187. } line_t;
  188. /* Describes a file mentioned in the block graph. Contains an array
  189. of line info. */
  190. typedef struct source_info
  191. {
  192. /* Canonical name of source file. */
  193. char *name;
  194. time_t file_time;
  195. /* Array of line information. */
  196. line_t *lines;
  197. unsigned num_lines;
  198. coverage_t coverage;
  199. /* Functions in this source file. These are in ascending line
  200. number order. */
  201. function_t *functions;
  202. } source_t;
  203. typedef struct name_map
  204. {
  205. char *name; /* Source file name */
  206. unsigned src; /* Source file */
  207. } name_map_t;
  208. /* Holds a list of function basic block graphs. */
  209. static function_t *functions;
  210. static function_t **fn_end = &functions;
  211. static source_t *sources; /* Array of source files */
  212. static unsigned n_sources; /* Number of sources */
  213. static unsigned a_sources; /* Allocated sources */
  214. static name_map_t *names; /* Mapping of file names to sources */
  215. static unsigned n_names; /* Number of names */
  216. static unsigned a_names; /* Allocated names */
  217. /* This holds data summary information. */
  218. static unsigned object_runs;
  219. static unsigned program_count;
  220. static unsigned total_lines;
  221. static unsigned total_executed;
  222. /* Modification time of graph file. */
  223. static time_t bbg_file_time;
  224. /* Name of the notes (gcno) output file. The "bbg" prefix is for
  225. historical reasons, when the notes file contained only the
  226. basic block graph notes. */
  227. static char *bbg_file_name;
  228. /* Stamp of the bbg file */
  229. static unsigned bbg_stamp;
  230. /* Name and file pointer of the input file for the count data (gcda). */
  231. static char *da_file_name;
  232. /* Data file is missing. */
  233. static int no_data_file;
  234. /* If there is several input files, compute and display results after
  235. reading all data files. This way if two or more gcda file refer to
  236. the same source file (eg inline subprograms in a .h file), the
  237. counts are added. */
  238. static int multiple_files = 0;
  239. /* Output branch probabilities. */
  240. static int flag_branches = 0;
  241. /* Show unconditional branches too. */
  242. static int flag_unconditional = 0;
  243. /* Output a gcov file if this is true. This is on by default, and can
  244. be turned off by the -n option. */
  245. static int flag_gcov_file = 1;
  246. /* Output progress indication if this is true. This is off by default
  247. and can be turned on by the -d option. */
  248. static int flag_display_progress = 0;
  249. /* Output *.gcov file in intermediate format used by 'lcov'. */
  250. static int flag_intermediate_format = 0;
  251. /* Output demangled function names. */
  252. static int flag_demangled_names = 0;
  253. /* For included files, make the gcov output file name include the name
  254. of the input source file. For example, if x.h is included in a.c,
  255. then the output file name is a.c##x.h.gcov instead of x.h.gcov. */
  256. static int flag_long_names = 0;
  257. /* Output count information for every basic block, not merely those
  258. that contain line number information. */
  259. static int flag_all_blocks = 0;
  260. /* Output summary info for each function. */
  261. static int flag_function_summary = 0;
  262. /* Object directory file prefix. This is the directory/file where the
  263. graph and data files are looked for, if nonzero. */
  264. static char *object_directory = 0;
  265. /* Source directory prefix. This is removed from source pathnames
  266. that match, when generating the output file name. */
  267. static char *source_prefix = 0;
  268. static size_t source_length = 0;
  269. /* Only show data for sources with relative pathnames. Absolute ones
  270. usually indicate a system header file, which although it may
  271. contain inline functions, is usually uninteresting. */
  272. static int flag_relative_only = 0;
  273. /* Preserve all pathname components. Needed when object files and
  274. source files are in subdirectories. '/' is mangled as '#', '.' is
  275. elided and '..' mangled to '^'. */
  276. static int flag_preserve_paths = 0;
  277. /* Output the number of times a branch was taken as opposed to the percentage
  278. of times it was taken. */
  279. static int flag_counts = 0;
  280. /* Forward declarations. */
  281. static int process_args (int, char **);
  282. static void print_usage (int) ATTRIBUTE_NORETURN;
  283. static void print_version (void) ATTRIBUTE_NORETURN;
  284. static void process_file (const char *);
  285. static void generate_results (const char *);
  286. static void create_file_names (const char *);
  287. static int name_search (const void *, const void *);
  288. static int name_sort (const void *, const void *);
  289. static char *canonicalize_name (const char *);
  290. static unsigned find_source (const char *);
  291. static function_t *read_graph_file (void);
  292. static int read_count_file (function_t *);
  293. static void solve_flow_graph (function_t *);
  294. static void find_exception_blocks (function_t *);
  295. static void add_branch_counts (coverage_t *, const arc_t *);
  296. static void add_line_counts (coverage_t *, function_t *);
  297. static void executed_summary (unsigned, unsigned);
  298. static void function_summary (const coverage_t *, const char *);
  299. static const char *format_gcov (gcov_type, gcov_type, int);
  300. static void accumulate_line_counts (source_t *);
  301. static void output_gcov_file (const char *, source_t *);
  302. static int output_branch_count (FILE *, int, const arc_t *);
  303. static void output_lines (FILE *, const source_t *);
  304. static char *make_gcov_file_name (const char *, const char *);
  305. static char *mangle_name (const char *, char *);
  306. static void release_structures (void);
  307. static void release_function (function_t *);
  308. extern int main (int, char **);
  309. int
  310. main (int argc, char **argv)
  311. {
  312. int argno;
  313. int first_arg;
  314. const char *p;
  315. p = argv[0] + strlen (argv[0]);
  316. while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
  317. --p;
  318. progname = p;
  319. xmalloc_set_program_name (progname);
  320. /* Unlock the stdio streams. */
  321. unlock_std_streams ();
  322. gcc_init_libintl ();
  323. diagnostic_initialize (global_dc, 0);
  324. /* Handle response files. */
  325. expandargv (&argc, &argv);
  326. a_names = 10;
  327. names = XNEWVEC (name_map_t, a_names);
  328. a_sources = 10;
  329. sources = XNEWVEC (source_t, a_sources);
  330. argno = process_args (argc, argv);
  331. if (optind == argc)
  332. print_usage (true);
  333. if (argc - argno > 1)
  334. multiple_files = 1;
  335. first_arg = argno;
  336. for (; argno != argc; argno++)
  337. {
  338. if (flag_display_progress)
  339. printf ("Processing file %d out of %d\n",
  340. argno - first_arg + 1, argc - first_arg);
  341. process_file (argv[argno]);
  342. }
  343. generate_results (multiple_files ? NULL : argv[argc - 1]);
  344. release_structures ();
  345. return 0;
  346. }
  347. /* Print a usage message and exit. If ERROR_P is nonzero, this is an error,
  348. otherwise the output of --help. */
  349. static void
  350. print_usage (int error_p)
  351. {
  352. FILE *file = error_p ? stderr : stdout;
  353. int status = error_p ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;
  354. fnotice (file, "Usage: gcov [OPTION]... SOURCE|OBJ...\n\n");
  355. fnotice (file, "Print code coverage information.\n\n");
  356. fnotice (file, " -h, --help Print this help, then exit\n");
  357. fnotice (file, " -a, --all-blocks Show information for every basic block\n");
  358. fnotice (file, " -b, --branch-probabilities Include branch probabilities in output\n");
  359. fnotice (file, " -c, --branch-counts Output counts of branches taken\n\
  360. rather than percentages\n");
  361. fnotice (file, " -d, --display-progress Display progress information\n");
  362. fnotice (file, " -f, --function-summaries Output summaries for each function\n");
  363. fnotice (file, " -i, --intermediate-format Output .gcov file in intermediate text format\n");
  364. fnotice (file, " -l, --long-file-names Use long output file names for included\n\
  365. source files\n");
  366. fnotice (file, " -m, --demangled-names Output demangled function names\n");
  367. fnotice (file, " -n, --no-output Do not create an output file\n");
  368. fnotice (file, " -o, --object-directory DIR|FILE Search for object files in DIR or called FILE\n");
  369. fnotice (file, " -p, --preserve-paths Preserve all pathname components\n");
  370. fnotice (file, " -r, --relative-only Only show data for relative sources\n");
  371. fnotice (file, " -s, --source-prefix DIR Source prefix to elide\n");
  372. fnotice (file, " -u, --unconditional-branches Show unconditional branch counts too\n");
  373. fnotice (file, " -v, --version Print version number, then exit\n");
  374. fnotice (file, "\nFor bug reporting instructions, please see:\n%s.\n",
  375. bug_report_url);
  376. exit (status);
  377. }
  378. /* Print version information and exit. */
  379. static void
  380. print_version (void)
  381. {
  382. fnotice (stdout, "gcov %s%s\n", pkgversion_string, version_string);
  383. fprintf (stdout, "Copyright %s 2015 Free Software Foundation, Inc.\n",
  384. _("(C)"));
  385. fnotice (stdout,
  386. _("This is free software; see the source for copying conditions.\n"
  387. "There is NO warranty; not even for MERCHANTABILITY or \n"
  388. "FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
  389. exit (SUCCESS_EXIT_CODE);
  390. }
  391. static const struct option options[] =
  392. {
  393. { "help", no_argument, NULL, 'h' },
  394. { "version", no_argument, NULL, 'v' },
  395. { "all-blocks", no_argument, NULL, 'a' },
  396. { "branch-probabilities", no_argument, NULL, 'b' },
  397. { "branch-counts", no_argument, NULL, 'c' },
  398. { "intermediate-format", no_argument, NULL, 'i' },
  399. { "no-output", no_argument, NULL, 'n' },
  400. { "long-file-names", no_argument, NULL, 'l' },
  401. { "function-summaries", no_argument, NULL, 'f' },
  402. { "demangled-names", no_argument, NULL, 'm' },
  403. { "preserve-paths", no_argument, NULL, 'p' },
  404. { "relative-only", no_argument, NULL, 'r' },
  405. { "object-directory", required_argument, NULL, 'o' },
  406. { "object-file", required_argument, NULL, 'o' },
  407. { "source-prefix", required_argument, NULL, 's' },
  408. { "unconditional-branches", no_argument, NULL, 'u' },
  409. { "display-progress", no_argument, NULL, 'd' },
  410. { 0, 0, 0, 0 }
  411. };
  412. /* Process args, return index to first non-arg. */
  413. static int
  414. process_args (int argc, char **argv)
  415. {
  416. int opt;
  417. while ((opt = getopt_long (argc, argv, "abcdfhilmno:s:pruv", options, NULL)) !=
  418. -1)
  419. {
  420. switch (opt)
  421. {
  422. case 'a':
  423. flag_all_blocks = 1;
  424. break;
  425. case 'b':
  426. flag_branches = 1;
  427. break;
  428. case 'c':
  429. flag_counts = 1;
  430. break;
  431. case 'f':
  432. flag_function_summary = 1;
  433. break;
  434. case 'h':
  435. print_usage (false);
  436. /* print_usage will exit. */
  437. case 'l':
  438. flag_long_names = 1;
  439. break;
  440. case 'm':
  441. flag_demangled_names = 1;
  442. break;
  443. case 'n':
  444. flag_gcov_file = 0;
  445. break;
  446. case 'o':
  447. object_directory = optarg;
  448. break;
  449. case 's':
  450. source_prefix = optarg;
  451. source_length = strlen (source_prefix);
  452. break;
  453. case 'r':
  454. flag_relative_only = 1;
  455. break;
  456. case 'p':
  457. flag_preserve_paths = 1;
  458. break;
  459. case 'u':
  460. flag_unconditional = 1;
  461. break;
  462. case 'i':
  463. flag_intermediate_format = 1;
  464. flag_gcov_file = 1;
  465. break;
  466. case 'd':
  467. flag_display_progress = 1;
  468. break;
  469. case 'v':
  470. print_version ();
  471. /* print_version will exit. */
  472. default:
  473. print_usage (true);
  474. /* print_usage will exit. */
  475. }
  476. }
  477. return optind;
  478. }
  479. /* Get the name of the gcov file. The return value must be free'd.
  480. It appends the '.gcov' extension to the *basename* of the file.
  481. The resulting file name will be in PWD.
  482. e.g.,
  483. input: foo.da, output: foo.da.gcov
  484. input: a/b/foo.cc, output: foo.cc.gcov */
  485. static char *
  486. get_gcov_intermediate_filename (const char *file_name)
  487. {
  488. const char *gcov = ".gcov";
  489. char *result;
  490. const char *cptr;
  491. /* Find the 'basename'. */
  492. cptr = lbasename (file_name);
  493. result = XNEWVEC (char, strlen (cptr) + strlen (gcov) + 1);
  494. sprintf (result, "%s%s", cptr, gcov);
  495. return result;
  496. }
  497. /* Output the result in intermediate format used by 'lcov'.
  498. The intermediate format contains a single file named 'foo.cc.gcov',
  499. with no source code included. A sample output is
  500. file:foo.cc
  501. function:5,1,_Z3foov
  502. function:13,1,main
  503. function:19,1,_GLOBAL__sub_I__Z3foov
  504. function:19,1,_Z41__static_initialization_and_destruction_0ii
  505. lcount:5,1
  506. lcount:7,9
  507. lcount:9,8
  508. lcount:11,1
  509. file:/.../iostream
  510. lcount:74,1
  511. file:/.../basic_ios.h
  512. file:/.../ostream
  513. file:/.../ios_base.h
  514. function:157,0,_ZStorSt12_Ios_IostateS_
  515. lcount:157,0
  516. file:/.../char_traits.h
  517. function:258,0,_ZNSt11char_traitsIcE6lengthEPKc
  518. lcount:258,0
  519. ...
  520. The default gcov outputs multiple files: 'foo.cc.gcov',
  521. 'iostream.gcov', 'ios_base.h.gcov', etc. with source code
  522. included. Instead the intermediate format here outputs only a single
  523. file 'foo.cc.gcov' similar to the above example. */
  524. static void
  525. output_intermediate_file (FILE *gcov_file, source_t *src)
  526. {
  527. unsigned line_num; /* current line number. */
  528. const line_t *line; /* current line info ptr. */
  529. function_t *fn; /* current function info ptr. */
  530. fprintf (gcov_file, "file:%s\n", src->name); /* source file name */
  531. for (fn = src->functions; fn; fn = fn->line_next)
  532. {
  533. /* function:<name>,<line_number>,<execution_count> */
  534. fprintf (gcov_file, "function:%d,%s,%s\n", fn->line,
  535. format_gcov (fn->blocks[0].count, 0, -1),
  536. flag_demangled_names ? fn->demangled_name : fn->name);
  537. }
  538. for (line_num = 1, line = &src->lines[line_num];
  539. line_num < src->num_lines;
  540. line_num++, line++)
  541. {
  542. arc_t *arc;
  543. if (line->exists)
  544. fprintf (gcov_file, "lcount:%u,%s\n", line_num,
  545. format_gcov (line->count, 0, -1));
  546. if (flag_branches)
  547. for (arc = line->u.branches; arc; arc = arc->line_next)
  548. {
  549. if (!arc->is_unconditional && !arc->is_call_non_return)
  550. {
  551. const char *branch_type;
  552. /* branch:<line_num>,<branch_coverage_type>
  553. branch_coverage_type
  554. : notexec (Branch not executed)
  555. : taken (Branch executed and taken)
  556. : nottaken (Branch executed, but not taken)
  557. */
  558. if (arc->src->count)
  559. branch_type = (arc->count > 0) ? "taken" : "nottaken";
  560. else
  561. branch_type = "notexec";
  562. fprintf (gcov_file, "branch:%d,%s\n", line_num, branch_type);
  563. }
  564. }
  565. }
  566. }
  567. /* Process a single input file. */
  568. static void
  569. process_file (const char *file_name)
  570. {
  571. function_t *fns;
  572. create_file_names (file_name);
  573. fns = read_graph_file ();
  574. if (!fns)
  575. return;
  576. read_count_file (fns);
  577. while (fns)
  578. {
  579. function_t *fn = fns;
  580. fns = fn->next;
  581. fn->next = NULL;
  582. if (fn->counts)
  583. {
  584. unsigned src = fn->src;
  585. unsigned line = fn->line;
  586. unsigned block_no;
  587. function_t *probe, **prev;
  588. /* Now insert it into the source file's list of
  589. functions. Normally functions will be encountered in
  590. ascending order, so a simple scan is quick. Note we're
  591. building this list in reverse order. */
  592. for (prev = &sources[src].functions;
  593. (probe = *prev); prev = &probe->line_next)
  594. if (probe->line <= line)
  595. break;
  596. fn->line_next = probe;
  597. *prev = fn;
  598. /* Mark last line in files touched by function. */
  599. for (block_no = 0; block_no != fn->num_blocks; block_no++)
  600. {
  601. unsigned *enc = fn->blocks[block_no].u.line.encoding;
  602. unsigned num = fn->blocks[block_no].u.line.num;
  603. for (; num--; enc++)
  604. if (!*enc)
  605. {
  606. if (enc[1] != src)
  607. {
  608. if (line >= sources[src].num_lines)
  609. sources[src].num_lines = line + 1;
  610. line = 0;
  611. src = enc[1];
  612. }
  613. enc++;
  614. num--;
  615. }
  616. else if (*enc > line)
  617. line = *enc;
  618. }
  619. if (line >= sources[src].num_lines)
  620. sources[src].num_lines = line + 1;
  621. solve_flow_graph (fn);
  622. if (fn->has_catch)
  623. find_exception_blocks (fn);
  624. *fn_end = fn;
  625. fn_end = &fn->next;
  626. }
  627. else
  628. /* The function was not in the executable -- some other
  629. instance must have been selected. */
  630. release_function (fn);
  631. }
  632. }
  633. static void
  634. output_gcov_file (const char *file_name, source_t *src)
  635. {
  636. char *gcov_file_name = make_gcov_file_name (file_name, src->coverage.name);
  637. if (src->coverage.lines)
  638. {
  639. FILE *gcov_file = fopen (gcov_file_name, "w");
  640. if (gcov_file)
  641. {
  642. fnotice (stdout, "Creating '%s'\n", gcov_file_name);
  643. output_lines (gcov_file, src);
  644. if (ferror (gcov_file))
  645. fnotice (stderr, "Error writing output file '%s'\n", gcov_file_name);
  646. fclose (gcov_file);
  647. }
  648. else
  649. fnotice (stderr, "Could not open output file '%s'\n", gcov_file_name);
  650. }
  651. else
  652. {
  653. unlink (gcov_file_name);
  654. fnotice (stdout, "Removing '%s'\n", gcov_file_name);
  655. }
  656. free (gcov_file_name);
  657. }
  658. static void
  659. generate_results (const char *file_name)
  660. {
  661. unsigned ix;
  662. source_t *src;
  663. function_t *fn;
  664. FILE *gcov_intermediate_file = NULL;
  665. char *gcov_intermediate_filename = NULL;
  666. for (ix = n_sources, src = sources; ix--; src++)
  667. if (src->num_lines)
  668. src->lines = XCNEWVEC (line_t, src->num_lines);
  669. for (fn = functions; fn; fn = fn->next)
  670. {
  671. coverage_t coverage;
  672. memset (&coverage, 0, sizeof (coverage));
  673. coverage.name = flag_demangled_names ? fn->demangled_name : fn->name;
  674. add_line_counts (flag_function_summary ? &coverage : NULL, fn);
  675. if (flag_function_summary)
  676. {
  677. function_summary (&coverage, "Function");
  678. fnotice (stdout, "\n");
  679. }
  680. }
  681. if (file_name)
  682. {
  683. name_map_t *name_map = (name_map_t *)bsearch
  684. (file_name, names, n_names, sizeof (*names), name_search);
  685. if (name_map)
  686. file_name = sources[name_map->src].coverage.name;
  687. else
  688. file_name = canonicalize_name (file_name);
  689. }
  690. if (flag_gcov_file && flag_intermediate_format)
  691. {
  692. /* Open the intermediate file. */
  693. gcov_intermediate_filename =
  694. get_gcov_intermediate_filename (file_name);
  695. gcov_intermediate_file = fopen (gcov_intermediate_filename, "w");
  696. if (!gcov_intermediate_file)
  697. {
  698. fnotice (stderr, "Cannot open intermediate output file %s\n",
  699. gcov_intermediate_filename);
  700. return;
  701. }
  702. }
  703. for (ix = n_sources, src = sources; ix--; src++)
  704. {
  705. if (flag_relative_only)
  706. {
  707. /* Ignore this source, if it is an absolute path (after
  708. source prefix removal). */
  709. char first = src->coverage.name[0];
  710. #if HAVE_DOS_BASED_FILE_SYSTEM
  711. if (first && src->coverage.name[1] == ':')
  712. first = src->coverage.name[2];
  713. #endif
  714. if (IS_DIR_SEPARATOR (first))
  715. continue;
  716. }
  717. accumulate_line_counts (src);
  718. function_summary (&src->coverage, "File");
  719. total_lines += src->coverage.lines;
  720. total_executed += src->coverage.lines_executed;
  721. if (flag_gcov_file)
  722. {
  723. if (flag_intermediate_format)
  724. /* Output the intermediate format without requiring source
  725. files. This outputs a section to a *single* file. */
  726. output_intermediate_file (gcov_intermediate_file, src);
  727. else
  728. output_gcov_file (file_name, src);
  729. fnotice (stdout, "\n");
  730. }
  731. }
  732. if (flag_gcov_file && flag_intermediate_format)
  733. {
  734. /* Now we've finished writing the intermediate file. */
  735. fclose (gcov_intermediate_file);
  736. XDELETEVEC (gcov_intermediate_filename);
  737. }
  738. if (!file_name)
  739. executed_summary (total_lines, total_executed);
  740. }
  741. /* Release a function structure */
  742. static void
  743. release_function (function_t *fn)
  744. {
  745. unsigned ix;
  746. block_t *block;
  747. for (ix = fn->num_blocks, block = fn->blocks; ix--; block++)
  748. {
  749. arc_t *arc, *arc_n;
  750. for (arc = block->succ; arc; arc = arc_n)
  751. {
  752. arc_n = arc->succ_next;
  753. free (arc);
  754. }
  755. }
  756. free (fn->blocks);
  757. free (fn->counts);
  758. if (flag_demangled_names && fn->demangled_name != fn->name)
  759. free (fn->demangled_name);
  760. free (fn->name);
  761. }
  762. /* Release all memory used. */
  763. static void
  764. release_structures (void)
  765. {
  766. unsigned ix;
  767. function_t *fn;
  768. for (ix = n_sources; ix--;)
  769. free (sources[ix].lines);
  770. free (sources);
  771. for (ix = n_names; ix--;)
  772. free (names[ix].name);
  773. free (names);
  774. while ((fn = functions))
  775. {
  776. functions = fn->next;
  777. release_function (fn);
  778. }
  779. }
  780. /* Generate the names of the graph and data files. If OBJECT_DIRECTORY
  781. is not specified, these are named from FILE_NAME sans extension. If
  782. OBJECT_DIRECTORY is specified and is a directory, the files are in that
  783. directory, but named from the basename of the FILE_NAME, sans extension.
  784. Otherwise OBJECT_DIRECTORY is taken to be the name of the object *file*
  785. and the data files are named from that. */
  786. static void
  787. create_file_names (const char *file_name)
  788. {
  789. char *cptr;
  790. char *name;
  791. int length = strlen (file_name);
  792. int base;
  793. /* Free previous file names. */
  794. free (bbg_file_name);
  795. free (da_file_name);
  796. da_file_name = bbg_file_name = NULL;
  797. bbg_file_time = 0;
  798. bbg_stamp = 0;
  799. if (object_directory && object_directory[0])
  800. {
  801. struct stat status;
  802. length += strlen (object_directory) + 2;
  803. name = XNEWVEC (char, length);
  804. name[0] = 0;
  805. base = !stat (object_directory, &status) && S_ISDIR (status.st_mode);
  806. strcat (name, object_directory);
  807. if (base && (! IS_DIR_SEPARATOR (name[strlen (name) - 1])))
  808. strcat (name, "/");
  809. }
  810. else
  811. {
  812. name = XNEWVEC (char, length + 1);
  813. strcpy (name, file_name);
  814. base = 0;
  815. }
  816. if (base)
  817. {
  818. /* Append source file name. */
  819. const char *cptr = lbasename (file_name);
  820. strcat (name, cptr ? cptr : file_name);
  821. }
  822. /* Remove the extension. */
  823. cptr = strrchr (CONST_CAST (char *, lbasename (name)), '.');
  824. if (cptr)
  825. *cptr = 0;
  826. length = strlen (name);
  827. bbg_file_name = XNEWVEC (char, length + strlen (GCOV_NOTE_SUFFIX) + 1);
  828. strcpy (bbg_file_name, name);
  829. strcpy (bbg_file_name + length, GCOV_NOTE_SUFFIX);
  830. da_file_name = XNEWVEC (char, length + strlen (GCOV_DATA_SUFFIX) + 1);
  831. strcpy (da_file_name, name);
  832. strcpy (da_file_name + length, GCOV_DATA_SUFFIX);
  833. free (name);
  834. return;
  835. }
  836. /* A is a string and B is a pointer to name_map_t. Compare for file
  837. name orderability. */
  838. static int
  839. name_search (const void *a_, const void *b_)
  840. {
  841. const char *a = (const char *)a_;
  842. const name_map_t *b = (const name_map_t *)b_;
  843. #if HAVE_DOS_BASED_FILE_SYSTEM
  844. return strcasecmp (a, b->name);
  845. #else
  846. return strcmp (a, b->name);
  847. #endif
  848. }
  849. /* A and B are a pointer to name_map_t. Compare for file name
  850. orderability. */
  851. static int
  852. name_sort (const void *a_, const void *b_)
  853. {
  854. const name_map_t *a = (const name_map_t *)a_;
  855. return name_search (a->name, b_);
  856. }
  857. /* Find or create a source file structure for FILE_NAME. Copies
  858. FILE_NAME on creation */
  859. static unsigned
  860. find_source (const char *file_name)
  861. {
  862. name_map_t *name_map;
  863. char *canon;
  864. unsigned idx;
  865. struct stat status;
  866. if (!file_name)
  867. file_name = "<unknown>";
  868. name_map = (name_map_t *)bsearch
  869. (file_name, names, n_names, sizeof (*names), name_search);
  870. if (name_map)
  871. {
  872. idx = name_map->src;
  873. goto check_date;
  874. }
  875. if (n_names + 2 > a_names)
  876. {
  877. /* Extend the name map array -- we'll be inserting one or two
  878. entries. */
  879. a_names *= 2;
  880. name_map = XNEWVEC (name_map_t, a_names);
  881. memcpy (name_map, names, n_names * sizeof (*names));
  882. free (names);
  883. names = name_map;
  884. }
  885. /* Not found, try the canonical name. */
  886. canon = canonicalize_name (file_name);
  887. name_map = (name_map_t *)bsearch
  888. (canon, names, n_names, sizeof (*names), name_search);
  889. if (!name_map)
  890. {
  891. /* Not found with canonical name, create a new source. */
  892. source_t *src;
  893. if (n_sources == a_sources)
  894. {
  895. a_sources *= 2;
  896. src = XNEWVEC (source_t, a_sources);
  897. memcpy (src, sources, n_sources * sizeof (*sources));
  898. free (sources);
  899. sources = src;
  900. }
  901. idx = n_sources;
  902. name_map = &names[n_names++];
  903. name_map->name = canon;
  904. name_map->src = idx;
  905. src = &sources[n_sources++];
  906. memset (src, 0, sizeof (*src));
  907. src->name = canon;
  908. src->coverage.name = src->name;
  909. if (source_length
  910. #if HAVE_DOS_BASED_FILE_SYSTEM
  911. /* You lose if separators don't match exactly in the
  912. prefix. */
  913. && !strncasecmp (source_prefix, src->coverage.name, source_length)
  914. #else
  915. && !strncmp (source_prefix, src->coverage.name, source_length)
  916. #endif
  917. && IS_DIR_SEPARATOR (src->coverage.name[source_length]))
  918. src->coverage.name += source_length + 1;
  919. if (!stat (src->name, &status))
  920. src->file_time = status.st_mtime;
  921. }
  922. else
  923. idx = name_map->src;
  924. if (name_search (file_name, name_map))
  925. {
  926. /* Append the non-canonical name. */
  927. name_map = &names[n_names++];
  928. name_map->name = xstrdup (file_name);
  929. name_map->src = idx;
  930. }
  931. /* Resort the name map. */
  932. qsort (names, n_names, sizeof (*names), name_sort);
  933. check_date:
  934. if (sources[idx].file_time > bbg_file_time)
  935. {
  936. static int info_emitted;
  937. fnotice (stderr, "%s:source file is newer than notes file '%s'\n",
  938. file_name, bbg_file_name);
  939. if (!info_emitted)
  940. {
  941. fnotice (stderr,
  942. "(the message is displayed only once per source file)\n");
  943. info_emitted = 1;
  944. }
  945. sources[idx].file_time = 0;
  946. }
  947. return idx;
  948. }
  949. /* Read the notes file. Return list of functions read -- in reverse order. */
  950. static function_t *
  951. read_graph_file (void)
  952. {
  953. unsigned version;
  954. unsigned current_tag = 0;
  955. function_t *fn = NULL;
  956. function_t *fns = NULL;
  957. function_t **fns_end = &fns;
  958. unsigned src_idx = 0;
  959. unsigned ix;
  960. unsigned tag;
  961. if (!gcov_open (bbg_file_name, 1))
  962. {
  963. fnotice (stderr, "%s:cannot open notes file\n", bbg_file_name);
  964. return fns;
  965. }
  966. bbg_file_time = gcov_time ();
  967. if (!gcov_magic (gcov_read_unsigned (), GCOV_NOTE_MAGIC))
  968. {
  969. fnotice (stderr, "%s:not a gcov notes file\n", bbg_file_name);
  970. gcov_close ();
  971. return fns;
  972. }
  973. version = gcov_read_unsigned ();
  974. if (version != GCOV_VERSION)
  975. {
  976. char v[4], e[4];
  977. GCOV_UNSIGNED2STRING (v, version);
  978. GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
  979. fnotice (stderr, "%s:version '%.4s', prefer '%.4s'\n",
  980. bbg_file_name, v, e);
  981. }
  982. bbg_stamp = gcov_read_unsigned ();
  983. while ((tag = gcov_read_unsigned ()))
  984. {
  985. unsigned length = gcov_read_unsigned ();
  986. gcov_position_t base = gcov_position ();
  987. if (tag == GCOV_TAG_FUNCTION)
  988. {
  989. char *function_name;
  990. unsigned ident, lineno;
  991. unsigned lineno_checksum, cfg_checksum;
  992. ident = gcov_read_unsigned ();
  993. lineno_checksum = gcov_read_unsigned ();
  994. cfg_checksum = gcov_read_unsigned ();
  995. function_name = xstrdup (gcov_read_string ());
  996. src_idx = find_source (gcov_read_string ());
  997. lineno = gcov_read_unsigned ();
  998. fn = XCNEW (function_t);
  999. fn->name = function_name;
  1000. if (flag_demangled_names)
  1001. {
  1002. fn->demangled_name = cplus_demangle (fn->name, DMGL_PARAMS);
  1003. if (!fn->demangled_name)
  1004. fn->demangled_name = fn->name;
  1005. }
  1006. fn->ident = ident;
  1007. fn->lineno_checksum = lineno_checksum;
  1008. fn->cfg_checksum = cfg_checksum;
  1009. fn->src = src_idx;
  1010. fn->line = lineno;
  1011. fn->line_next = NULL;
  1012. fn->next = NULL;
  1013. *fns_end = fn;
  1014. fns_end = &fn->next;
  1015. current_tag = tag;
  1016. }
  1017. else if (fn && tag == GCOV_TAG_BLOCKS)
  1018. {
  1019. if (fn->blocks)
  1020. fnotice (stderr, "%s:already seen blocks for '%s'\n",
  1021. bbg_file_name, fn->name);
  1022. else
  1023. {
  1024. unsigned ix, num_blocks = GCOV_TAG_BLOCKS_NUM (length);
  1025. fn->num_blocks = num_blocks;
  1026. fn->blocks = XCNEWVEC (block_t, fn->num_blocks);
  1027. for (ix = 0; ix != num_blocks; ix++)
  1028. fn->blocks[ix].flags = gcov_read_unsigned ();
  1029. }
  1030. }
  1031. else if (fn && tag == GCOV_TAG_ARCS)
  1032. {
  1033. unsigned src = gcov_read_unsigned ();
  1034. unsigned num_dests = GCOV_TAG_ARCS_NUM (length);
  1035. block_t *src_blk = &fn->blocks[src];
  1036. unsigned mark_catches = 0;
  1037. struct arc_info *arc;
  1038. if (src >= fn->num_blocks || fn->blocks[src].succ)
  1039. goto corrupt;
  1040. while (num_dests--)
  1041. {
  1042. unsigned dest = gcov_read_unsigned ();
  1043. unsigned flags = gcov_read_unsigned ();
  1044. if (dest >= fn->num_blocks)
  1045. goto corrupt;
  1046. arc = XCNEW (arc_t);
  1047. arc->dst = &fn->blocks[dest];
  1048. arc->src = src_blk;
  1049. arc->count = 0;
  1050. arc->count_valid = 0;
  1051. arc->on_tree = !!(flags & GCOV_ARC_ON_TREE);
  1052. arc->fake = !!(flags & GCOV_ARC_FAKE);
  1053. arc->fall_through = !!(flags & GCOV_ARC_FALLTHROUGH);
  1054. arc->succ_next = src_blk->succ;
  1055. src_blk->succ = arc;
  1056. src_blk->num_succ++;
  1057. arc->pred_next = fn->blocks[dest].pred;
  1058. fn->blocks[dest].pred = arc;
  1059. fn->blocks[dest].num_pred++;
  1060. if (arc->fake)
  1061. {
  1062. if (src)
  1063. {
  1064. /* Exceptional exit from this function, the
  1065. source block must be a call. */
  1066. fn->blocks[src].is_call_site = 1;
  1067. arc->is_call_non_return = 1;
  1068. mark_catches = 1;
  1069. }
  1070. else
  1071. {
  1072. /* Non-local return from a callee of this
  1073. function. The destination block is a setjmp. */
  1074. arc->is_nonlocal_return = 1;
  1075. fn->blocks[dest].is_nonlocal_return = 1;
  1076. }
  1077. }
  1078. if (!arc->on_tree)
  1079. fn->num_counts++;
  1080. }
  1081. if (mark_catches)
  1082. {
  1083. /* We have a fake exit from this block. The other
  1084. non-fall through exits must be to catch handlers.
  1085. Mark them as catch arcs. */
  1086. for (arc = src_blk->succ; arc; arc = arc->succ_next)
  1087. if (!arc->fake && !arc->fall_through)
  1088. {
  1089. arc->is_throw = 1;
  1090. fn->has_catch = 1;
  1091. }
  1092. }
  1093. }
  1094. else if (fn && tag == GCOV_TAG_LINES)
  1095. {
  1096. unsigned blockno = gcov_read_unsigned ();
  1097. unsigned *line_nos = XCNEWVEC (unsigned, length - 1);
  1098. if (blockno >= fn->num_blocks || fn->blocks[blockno].u.line.encoding)
  1099. goto corrupt;
  1100. for (ix = 0; ; )
  1101. {
  1102. unsigned lineno = gcov_read_unsigned ();
  1103. if (lineno)
  1104. {
  1105. if (!ix)
  1106. {
  1107. line_nos[ix++] = 0;
  1108. line_nos[ix++] = src_idx;
  1109. }
  1110. line_nos[ix++] = lineno;
  1111. }
  1112. else
  1113. {
  1114. const char *file_name = gcov_read_string ();
  1115. if (!file_name)
  1116. break;
  1117. src_idx = find_source (file_name);
  1118. line_nos[ix++] = 0;
  1119. line_nos[ix++] = src_idx;
  1120. }
  1121. }
  1122. fn->blocks[blockno].u.line.encoding = line_nos;
  1123. fn->blocks[blockno].u.line.num = ix;
  1124. }
  1125. else if (current_tag && !GCOV_TAG_IS_SUBTAG (current_tag, tag))
  1126. {
  1127. fn = NULL;
  1128. current_tag = 0;
  1129. }
  1130. gcov_sync (base, length);
  1131. if (gcov_is_error ())
  1132. {
  1133. corrupt:;
  1134. fnotice (stderr, "%s:corrupted\n", bbg_file_name);
  1135. break;
  1136. }
  1137. }
  1138. gcov_close ();
  1139. if (!fns)
  1140. fnotice (stderr, "%s:no functions found\n", bbg_file_name);
  1141. return fns;
  1142. }
  1143. /* Reads profiles from the count file and attach to each
  1144. function. Return nonzero if fatal error. */
  1145. static int
  1146. read_count_file (function_t *fns)
  1147. {
  1148. unsigned ix;
  1149. unsigned version;
  1150. unsigned tag;
  1151. function_t *fn = NULL;
  1152. int error = 0;
  1153. if (!gcov_open (da_file_name, 1))
  1154. {
  1155. fnotice (stderr, "%s:cannot open data file, assuming not executed\n",
  1156. da_file_name);
  1157. no_data_file = 1;
  1158. return 0;
  1159. }
  1160. if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
  1161. {
  1162. fnotice (stderr, "%s:not a gcov data file\n", da_file_name);
  1163. cleanup:;
  1164. gcov_close ();
  1165. return 1;
  1166. }
  1167. version = gcov_read_unsigned ();
  1168. if (version != GCOV_VERSION)
  1169. {
  1170. char v[4], e[4];
  1171. GCOV_UNSIGNED2STRING (v, version);
  1172. GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
  1173. fnotice (stderr, "%s:version '%.4s', prefer version '%.4s'\n",
  1174. da_file_name, v, e);
  1175. }
  1176. tag = gcov_read_unsigned ();
  1177. if (tag != bbg_stamp)
  1178. {
  1179. fnotice (stderr, "%s:stamp mismatch with notes file\n", da_file_name);
  1180. goto cleanup;
  1181. }
  1182. while ((tag = gcov_read_unsigned ()))
  1183. {
  1184. unsigned length = gcov_read_unsigned ();
  1185. unsigned long base = gcov_position ();
  1186. if (tag == GCOV_TAG_PROGRAM_SUMMARY)
  1187. {
  1188. struct gcov_summary summary;
  1189. gcov_read_summary (&summary);
  1190. object_runs += summary.ctrs[GCOV_COUNTER_ARCS].runs;
  1191. program_count++;
  1192. }
  1193. else if (tag == GCOV_TAG_FUNCTION && !length)
  1194. ; /* placeholder */
  1195. else if (tag == GCOV_TAG_FUNCTION && length == GCOV_TAG_FUNCTION_LENGTH)
  1196. {
  1197. unsigned ident;
  1198. struct function_info *fn_n;
  1199. /* Try to find the function in the list. To speed up the
  1200. search, first start from the last function found. */
  1201. ident = gcov_read_unsigned ();
  1202. fn_n = fns;
  1203. for (fn = fn ? fn->next : NULL; ; fn = fn->next)
  1204. {
  1205. if (fn)
  1206. ;
  1207. else if ((fn = fn_n))
  1208. fn_n = NULL;
  1209. else
  1210. {
  1211. fnotice (stderr, "%s:unknown function '%u'\n",
  1212. da_file_name, ident);
  1213. break;
  1214. }
  1215. if (fn->ident == ident)
  1216. break;
  1217. }
  1218. if (!fn)
  1219. ;
  1220. else if (gcov_read_unsigned () != fn->lineno_checksum
  1221. || gcov_read_unsigned () != fn->cfg_checksum)
  1222. {
  1223. mismatch:;
  1224. fnotice (stderr, "%s:profile mismatch for '%s'\n",
  1225. da_file_name, fn->name);
  1226. goto cleanup;
  1227. }
  1228. }
  1229. else if (tag == GCOV_TAG_FOR_COUNTER (GCOV_COUNTER_ARCS) && fn)
  1230. {
  1231. if (length != GCOV_TAG_COUNTER_LENGTH (fn->num_counts))
  1232. goto mismatch;
  1233. if (!fn->counts)
  1234. fn->counts = XCNEWVEC (gcov_type, fn->num_counts);
  1235. for (ix = 0; ix != fn->num_counts; ix++)
  1236. fn->counts[ix] += gcov_read_counter ();
  1237. }
  1238. gcov_sync (base, length);
  1239. if ((error = gcov_is_error ()))
  1240. {
  1241. fnotice (stderr, error < 0 ? "%s:overflowed\n" : "%s:corrupted\n",
  1242. da_file_name);
  1243. goto cleanup;
  1244. }
  1245. }
  1246. gcov_close ();
  1247. return 0;
  1248. }
  1249. /* Solve the flow graph. Propagate counts from the instrumented arcs
  1250. to the blocks and the uninstrumented arcs. */
  1251. static void
  1252. solve_flow_graph (function_t *fn)
  1253. {
  1254. unsigned ix;
  1255. arc_t *arc;
  1256. gcov_type *count_ptr = fn->counts;
  1257. block_t *blk;
  1258. block_t *valid_blocks = NULL; /* valid, but unpropagated blocks. */
  1259. block_t *invalid_blocks = NULL; /* invalid, but inferable blocks. */
  1260. /* The arcs were built in reverse order. Fix that now. */
  1261. for (ix = fn->num_blocks; ix--;)
  1262. {
  1263. arc_t *arc_p, *arc_n;
  1264. for (arc_p = NULL, arc = fn->blocks[ix].succ; arc;
  1265. arc_p = arc, arc = arc_n)
  1266. {
  1267. arc_n = arc->succ_next;
  1268. arc->succ_next = arc_p;
  1269. }
  1270. fn->blocks[ix].succ = arc_p;
  1271. for (arc_p = NULL, arc = fn->blocks[ix].pred; arc;
  1272. arc_p = arc, arc = arc_n)
  1273. {
  1274. arc_n = arc->pred_next;
  1275. arc->pred_next = arc_p;
  1276. }
  1277. fn->blocks[ix].pred = arc_p;
  1278. }
  1279. if (fn->num_blocks < 2)
  1280. fnotice (stderr, "%s:'%s' lacks entry and/or exit blocks\n",
  1281. bbg_file_name, fn->name);
  1282. else
  1283. {
  1284. if (fn->blocks[ENTRY_BLOCK].num_pred)
  1285. fnotice (stderr, "%s:'%s' has arcs to entry block\n",
  1286. bbg_file_name, fn->name);
  1287. else
  1288. /* We can't deduce the entry block counts from the lack of
  1289. predecessors. */
  1290. fn->blocks[ENTRY_BLOCK].num_pred = ~(unsigned)0;
  1291. if (fn->blocks[EXIT_BLOCK].num_succ)
  1292. fnotice (stderr, "%s:'%s' has arcs from exit block\n",
  1293. bbg_file_name, fn->name);
  1294. else
  1295. /* Likewise, we can't deduce exit block counts from the lack
  1296. of its successors. */
  1297. fn->blocks[EXIT_BLOCK].num_succ = ~(unsigned)0;
  1298. }
  1299. /* Propagate the measured counts, this must be done in the same
  1300. order as the code in profile.c */
  1301. for (ix = 0, blk = fn->blocks; ix != fn->num_blocks; ix++, blk++)
  1302. {
  1303. block_t const *prev_dst = NULL;
  1304. int out_of_order = 0;
  1305. int non_fake_succ = 0;
  1306. for (arc = blk->succ; arc; arc = arc->succ_next)
  1307. {
  1308. if (!arc->fake)
  1309. non_fake_succ++;
  1310. if (!arc->on_tree)
  1311. {
  1312. if (count_ptr)
  1313. arc->count = *count_ptr++;
  1314. arc->count_valid = 1;
  1315. blk->num_succ--;
  1316. arc->dst->num_pred--;
  1317. }
  1318. if (prev_dst && prev_dst > arc->dst)
  1319. out_of_order = 1;
  1320. prev_dst = arc->dst;
  1321. }
  1322. if (non_fake_succ == 1)
  1323. {
  1324. /* If there is only one non-fake exit, it is an
  1325. unconditional branch. */
  1326. for (arc = blk->succ; arc; arc = arc->succ_next)
  1327. if (!arc->fake)
  1328. {
  1329. arc->is_unconditional = 1;
  1330. /* If this block is instrumenting a call, it might be
  1331. an artificial block. It is not artificial if it has
  1332. a non-fallthrough exit, or the destination of this
  1333. arc has more than one entry. Mark the destination
  1334. block as a return site, if none of those conditions
  1335. hold. */
  1336. if (blk->is_call_site && arc->fall_through
  1337. && arc->dst->pred == arc && !arc->pred_next)
  1338. arc->dst->is_call_return = 1;
  1339. }
  1340. }
  1341. /* Sort the successor arcs into ascending dst order. profile.c
  1342. normally produces arcs in the right order, but sometimes with
  1343. one or two out of order. We're not using a particularly
  1344. smart sort. */
  1345. if (out_of_order)
  1346. {
  1347. arc_t *start = blk->succ;
  1348. unsigned changes = 1;
  1349. while (changes)
  1350. {
  1351. arc_t *arc, *arc_p, *arc_n;
  1352. changes = 0;
  1353. for (arc_p = NULL, arc = start; (arc_n = arc->succ_next);)
  1354. {
  1355. if (arc->dst > arc_n->dst)
  1356. {
  1357. changes = 1;
  1358. if (arc_p)
  1359. arc_p->succ_next = arc_n;
  1360. else
  1361. start = arc_n;
  1362. arc->succ_next = arc_n->succ_next;
  1363. arc_n->succ_next = arc;
  1364. arc_p = arc_n;
  1365. }
  1366. else
  1367. {
  1368. arc_p = arc;
  1369. arc = arc_n;
  1370. }
  1371. }
  1372. }
  1373. blk->succ = start;
  1374. }
  1375. /* Place it on the invalid chain, it will be ignored if that's
  1376. wrong. */
  1377. blk->invalid_chain = 1;
  1378. blk->chain = invalid_blocks;
  1379. invalid_blocks = blk;
  1380. }
  1381. while (invalid_blocks || valid_blocks)
  1382. {
  1383. while ((blk = invalid_blocks))
  1384. {
  1385. gcov_type total = 0;
  1386. const arc_t *arc;
  1387. invalid_blocks = blk->chain;
  1388. blk->invalid_chain = 0;
  1389. if (!blk->num_succ)
  1390. for (arc = blk->succ; arc; arc = arc->succ_next)
  1391. total += arc->count;
  1392. else if (!blk->num_pred)
  1393. for (arc = blk->pred; arc; arc = arc->pred_next)
  1394. total += arc->count;
  1395. else
  1396. continue;
  1397. blk->count = total;
  1398. blk->count_valid = 1;
  1399. blk->chain = valid_blocks;
  1400. blk->valid_chain = 1;
  1401. valid_blocks = blk;
  1402. }
  1403. while ((blk = valid_blocks))
  1404. {
  1405. gcov_type total;
  1406. arc_t *arc, *inv_arc;
  1407. valid_blocks = blk->chain;
  1408. blk->valid_chain = 0;
  1409. if (blk->num_succ == 1)
  1410. {
  1411. block_t *dst;
  1412. total = blk->count;
  1413. inv_arc = NULL;
  1414. for (arc = blk->succ; arc; arc = arc->succ_next)
  1415. {
  1416. total -= arc->count;
  1417. if (!arc->count_valid)
  1418. inv_arc = arc;
  1419. }
  1420. dst = inv_arc->dst;
  1421. inv_arc->count_valid = 1;
  1422. inv_arc->count = total;
  1423. blk->num_succ--;
  1424. dst->num_pred--;
  1425. if (dst->count_valid)
  1426. {
  1427. if (dst->num_pred == 1 && !dst->valid_chain)
  1428. {
  1429. dst->chain = valid_blocks;
  1430. dst->valid_chain = 1;
  1431. valid_blocks = dst;
  1432. }
  1433. }
  1434. else
  1435. {
  1436. if (!dst->num_pred && !dst->invalid_chain)
  1437. {
  1438. dst->chain = invalid_blocks;
  1439. dst->invalid_chain = 1;
  1440. invalid_blocks = dst;
  1441. }
  1442. }
  1443. }
  1444. if (blk->num_pred == 1)
  1445. {
  1446. block_t *src;
  1447. total = blk->count;
  1448. inv_arc = NULL;
  1449. for (arc = blk->pred; arc; arc = arc->pred_next)
  1450. {
  1451. total -= arc->count;
  1452. if (!arc->count_valid)
  1453. inv_arc = arc;
  1454. }
  1455. src = inv_arc->src;
  1456. inv_arc->count_valid = 1;
  1457. inv_arc->count = total;
  1458. blk->num_pred--;
  1459. src->num_succ--;
  1460. if (src->count_valid)
  1461. {
  1462. if (src->num_succ == 1 && !src->valid_chain)
  1463. {
  1464. src->chain = valid_blocks;
  1465. src->valid_chain = 1;
  1466. valid_blocks = src;
  1467. }
  1468. }
  1469. else
  1470. {
  1471. if (!src->num_succ && !src->invalid_chain)
  1472. {
  1473. src->chain = invalid_blocks;
  1474. src->invalid_chain = 1;
  1475. invalid_blocks = src;
  1476. }
  1477. }
  1478. }
  1479. }
  1480. }
  1481. /* If the graph has been correctly solved, every block will have a
  1482. valid count. */
  1483. for (ix = 0; ix < fn->num_blocks; ix++)
  1484. if (!fn->blocks[ix].count_valid)
  1485. {
  1486. fnotice (stderr, "%s:graph is unsolvable for '%s'\n",
  1487. bbg_file_name, fn->name);
  1488. break;
  1489. }
  1490. }
  1491. /* Mark all the blocks only reachable via an incoming catch. */
  1492. static void
  1493. find_exception_blocks (function_t *fn)
  1494. {
  1495. unsigned ix;
  1496. block_t **queue = XALLOCAVEC (block_t *, fn->num_blocks);
  1497. /* First mark all blocks as exceptional. */
  1498. for (ix = fn->num_blocks; ix--;)
  1499. fn->blocks[ix].exceptional = 1;
  1500. /* Now mark all the blocks reachable via non-fake edges */
  1501. queue[0] = fn->blocks;
  1502. queue[0]->exceptional = 0;
  1503. for (ix = 1; ix;)
  1504. {
  1505. block_t *block = queue[--ix];
  1506. const arc_t *arc;
  1507. for (arc = block->succ; arc; arc = arc->succ_next)
  1508. if (!arc->fake && !arc->is_throw && arc->dst->exceptional)
  1509. {
  1510. arc->dst->exceptional = 0;
  1511. queue[ix++] = arc->dst;
  1512. }
  1513. }
  1514. }
  1515. /* Increment totals in COVERAGE according to arc ARC. */
  1516. static void
  1517. add_branch_counts (coverage_t *coverage, const arc_t *arc)
  1518. {
  1519. if (arc->is_call_non_return)
  1520. {
  1521. coverage->calls++;
  1522. if (arc->src->count)
  1523. coverage->calls_executed++;
  1524. }
  1525. else if (!arc->is_unconditional)
  1526. {
  1527. coverage->branches++;
  1528. if (arc->src->count)
  1529. coverage->branches_executed++;
  1530. if (arc->count)
  1531. coverage->branches_taken++;
  1532. }
  1533. }
  1534. /* Format a GCOV_TYPE integer as either a percent ratio, or absolute
  1535. count. If dp >= 0, format TOP/BOTTOM * 100 to DP decimal places.
  1536. If DP is zero, no decimal point is printed. Only print 100% when
  1537. TOP==BOTTOM and only print 0% when TOP=0. If dp < 0, then simply
  1538. format TOP. Return pointer to a static string. */
  1539. static char const *
  1540. format_gcov (gcov_type top, gcov_type bottom, int dp)
  1541. {
  1542. static char buffer[20];
  1543. if (dp >= 0)
  1544. {
  1545. float ratio = bottom ? (float)top / bottom : 0;
  1546. int ix;
  1547. unsigned limit = 100;
  1548. unsigned percent;
  1549. for (ix = dp; ix--; )
  1550. limit *= 10;
  1551. percent = (unsigned) (ratio * limit + (float)0.5);
  1552. if (percent <= 0 && top)
  1553. percent = 1;
  1554. else if (percent >= limit && top != bottom)
  1555. percent = limit - 1;
  1556. ix = sprintf (buffer, "%.*u%%", dp + 1, percent);
  1557. if (dp)
  1558. {
  1559. dp++;
  1560. do
  1561. {
  1562. buffer[ix+1] = buffer[ix];
  1563. ix--;
  1564. }
  1565. while (dp--);
  1566. buffer[ix + 1] = '.';
  1567. }
  1568. }
  1569. else
  1570. sprintf (buffer, "%"PRId64, (int64_t)top);
  1571. return buffer;
  1572. }
  1573. /* Summary of execution */
  1574. static void
  1575. executed_summary (unsigned lines, unsigned executed)
  1576. {
  1577. if (lines)
  1578. fnotice (stdout, "Lines executed:%s of %d\n",
  1579. format_gcov (executed, lines, 2), lines);
  1580. else
  1581. fnotice (stdout, "No executable lines\n");
  1582. }
  1583. /* Output summary info for a function or file. */
  1584. static void
  1585. function_summary (const coverage_t *coverage, const char *title)
  1586. {
  1587. fnotice (stdout, "%s '%s'\n", title, coverage->name);
  1588. executed_summary (coverage->lines, coverage->lines_executed);
  1589. if (flag_branches)
  1590. {
  1591. if (coverage->branches)
  1592. {
  1593. fnotice (stdout, "Branches executed:%s of %d\n",
  1594. format_gcov (coverage->branches_executed,
  1595. coverage->branches, 2),
  1596. coverage->branches);
  1597. fnotice (stdout, "Taken at least once:%s of %d\n",
  1598. format_gcov (coverage->branches_taken,
  1599. coverage->branches, 2),
  1600. coverage->branches);
  1601. }
  1602. else
  1603. fnotice (stdout, "No branches\n");
  1604. if (coverage->calls)
  1605. fnotice (stdout, "Calls executed:%s of %d\n",
  1606. format_gcov (coverage->calls_executed, coverage->calls, 2),
  1607. coverage->calls);
  1608. else
  1609. fnotice (stdout, "No calls\n");
  1610. }
  1611. }
  1612. /* Canonicalize the filename NAME by canonicalizing directory
  1613. separators, eliding . components and resolving .. components
  1614. appropriately. Always returns a unique string. */
  1615. static char *
  1616. canonicalize_name (const char *name)
  1617. {
  1618. /* The canonical name cannot be longer than the incoming name. */
  1619. char *result = XNEWVEC (char, strlen (name) + 1);
  1620. const char *base = name, *probe;
  1621. char *ptr = result;
  1622. char *dd_base;
  1623. int slash = 0;
  1624. #if HAVE_DOS_BASED_FILE_SYSTEM
  1625. if (base[0] && base[1] == ':')
  1626. {
  1627. result[0] = base[0];
  1628. result[1] = ':';
  1629. base += 2;
  1630. ptr += 2;
  1631. }
  1632. #endif
  1633. for (dd_base = ptr; *base; base = probe)
  1634. {
  1635. size_t len;
  1636. for (probe = base; *probe; probe++)
  1637. if (IS_DIR_SEPARATOR (*probe))
  1638. break;
  1639. len = probe - base;
  1640. if (len == 1 && base[0] == '.')
  1641. /* Elide a '.' directory */
  1642. ;
  1643. else if (len == 2 && base[0] == '.' && base[1] == '.')
  1644. {
  1645. /* '..', we can only elide it and the previous directory, if
  1646. we're not a symlink. */
  1647. struct stat ATTRIBUTE_UNUSED buf;
  1648. *ptr = 0;
  1649. if (dd_base == ptr
  1650. #if defined (S_ISLNK)
  1651. /* S_ISLNK is not POSIX.1-1996. */
  1652. || stat (result, &buf) || S_ISLNK (buf.st_mode)
  1653. #endif
  1654. )
  1655. {
  1656. /* Cannot elide, or unreadable or a symlink. */
  1657. dd_base = ptr + 2 + slash;
  1658. goto regular;
  1659. }
  1660. while (ptr != dd_base && *ptr != '/')
  1661. ptr--;
  1662. slash = ptr != result;
  1663. }
  1664. else
  1665. {
  1666. regular:
  1667. /* Regular pathname component. */
  1668. if (slash)
  1669. *ptr++ = '/';
  1670. memcpy (ptr, base, len);
  1671. ptr += len;
  1672. slash = 1;
  1673. }
  1674. for (; IS_DIR_SEPARATOR (*probe); probe++)
  1675. continue;
  1676. }
  1677. *ptr = 0;
  1678. return result;
  1679. }
  1680. /* Generate an output file name. INPUT_NAME is the canonicalized main
  1681. input file and SRC_NAME is the canonicalized file name.
  1682. LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation. With
  1683. long_output_names we prepend the processed name of the input file
  1684. to each output name (except when the current source file is the
  1685. input file, so you don't get a double concatenation). The two
  1686. components are separated by '##'. With preserve_paths we create a
  1687. filename from all path components of the source file, replacing '/'
  1688. with '#', and .. with '^', without it we simply take the basename
  1689. component. (Remember, the canonicalized name will already have
  1690. elided '.' components and converted \\ separators.) */
  1691. static char *
  1692. make_gcov_file_name (const char *input_name, const char *src_name)
  1693. {
  1694. char *ptr;
  1695. char *result;
  1696. if (flag_long_names && input_name && strcmp (src_name, input_name))
  1697. {
  1698. /* Generate the input filename part. */
  1699. result = XNEWVEC (char, strlen (input_name) + strlen (src_name) + 10);
  1700. ptr = result;
  1701. ptr = mangle_name (input_name, ptr);
  1702. ptr[0] = ptr[1] = '#';
  1703. ptr += 2;
  1704. }
  1705. else
  1706. {
  1707. result = XNEWVEC (char, strlen (src_name) + 10);
  1708. ptr = result;
  1709. }
  1710. ptr = mangle_name (src_name, ptr);
  1711. strcpy (ptr, ".gcov");
  1712. return result;
  1713. }
  1714. static char *
  1715. mangle_name (char const *base, char *ptr)
  1716. {
  1717. size_t len;
  1718. /* Generate the source filename part. */
  1719. if (!flag_preserve_paths)
  1720. {
  1721. base = lbasename (base);
  1722. len = strlen (base);
  1723. memcpy (ptr, base, len);
  1724. ptr += len;
  1725. }
  1726. else
  1727. {
  1728. /* Convert '/' to '#', convert '..' to '^',
  1729. convert ':' to '~' on DOS based file system. */
  1730. const char *probe;
  1731. #if HAVE_DOS_BASED_FILE_SYSTEM
  1732. if (base[0] && base[1] == ':')
  1733. {
  1734. ptr[0] = base[0];
  1735. ptr[1] = '~';
  1736. ptr += 2;
  1737. base += 2;
  1738. }
  1739. #endif
  1740. for (; *base; base = probe)
  1741. {
  1742. size_t len;
  1743. for (probe = base; *probe; probe++)
  1744. if (*probe == '/')
  1745. break;
  1746. len = probe - base;
  1747. if (len == 2 && base[0] == '.' && base[1] == '.')
  1748. *ptr++ = '^';
  1749. else
  1750. {
  1751. memcpy (ptr, base, len);
  1752. ptr += len;
  1753. }
  1754. if (*probe)
  1755. {
  1756. *ptr++ = '#';
  1757. probe++;
  1758. }
  1759. }
  1760. }
  1761. return ptr;
  1762. }
  1763. /* Scan through the bb_data for each line in the block, increment
  1764. the line number execution count indicated by the execution count of
  1765. the appropriate basic block. */
  1766. static void
  1767. add_line_counts (coverage_t *coverage, function_t *fn)
  1768. {
  1769. unsigned ix;
  1770. line_t *line = NULL; /* This is propagated from one iteration to the
  1771. next. */
  1772. /* Scan each basic block. */
  1773. for (ix = 0; ix != fn->num_blocks; ix++)
  1774. {
  1775. block_t *block = &fn->blocks[ix];
  1776. unsigned *encoding;
  1777. const source_t *src = NULL;
  1778. unsigned jx;
  1779. if (block->count && ix && ix + 1 != fn->num_blocks)
  1780. fn->blocks_executed++;
  1781. for (jx = 0, encoding = block->u.line.encoding;
  1782. jx != block->u.line.num; jx++, encoding++)
  1783. if (!*encoding)
  1784. {
  1785. src = &sources[*++encoding];
  1786. jx++;
  1787. }
  1788. else
  1789. {
  1790. line = &src->lines[*encoding];
  1791. if (coverage)
  1792. {
  1793. if (!line->exists)
  1794. coverage->lines++;
  1795. if (!line->count && block->count)
  1796. coverage->lines_executed++;
  1797. }
  1798. line->exists = 1;
  1799. if (!block->exceptional)
  1800. line->unexceptional = 1;
  1801. line->count += block->count;
  1802. }
  1803. free (block->u.line.encoding);
  1804. block->u.cycle.arc = NULL;
  1805. block->u.cycle.ident = ~0U;
  1806. if (!ix || ix + 1 == fn->num_blocks)
  1807. /* Entry or exit block */;
  1808. else if (flag_all_blocks)
  1809. {
  1810. line_t *block_line = line;
  1811. if (!block_line)
  1812. block_line = &sources[fn->src].lines[fn->line];
  1813. block->chain = block_line->u.blocks;
  1814. block_line->u.blocks = block;
  1815. }
  1816. else if (flag_branches)
  1817. {
  1818. arc_t *arc;
  1819. for (arc = block->succ; arc; arc = arc->succ_next)
  1820. {
  1821. arc->line_next = line->u.branches;
  1822. line->u.branches = arc;
  1823. if (coverage && !arc->is_unconditional)
  1824. add_branch_counts (coverage, arc);
  1825. }
  1826. }
  1827. }
  1828. if (!line)
  1829. fnotice (stderr, "%s:no lines for '%s'\n", bbg_file_name, fn->name);
  1830. }
  1831. /* Accumulate the line counts of a file. */
  1832. static void
  1833. accumulate_line_counts (source_t *src)
  1834. {
  1835. line_t *line;
  1836. function_t *fn, *fn_p, *fn_n;
  1837. unsigned ix;
  1838. /* Reverse the function order. */
  1839. for (fn = src->functions, fn_p = NULL; fn;
  1840. fn_p = fn, fn = fn_n)
  1841. {
  1842. fn_n = fn->line_next;
  1843. fn->line_next = fn_p;
  1844. }
  1845. src->functions = fn_p;
  1846. for (ix = src->num_lines, line = src->lines; ix--; line++)
  1847. {
  1848. if (!flag_all_blocks)
  1849. {
  1850. arc_t *arc, *arc_p, *arc_n;
  1851. /* Total and reverse the branch information. */
  1852. for (arc = line->u.branches, arc_p = NULL; arc;
  1853. arc_p = arc, arc = arc_n)
  1854. {
  1855. arc_n = arc->line_next;
  1856. arc->line_next = arc_p;
  1857. add_branch_counts (&src->coverage, arc);
  1858. }
  1859. line->u.branches = arc_p;
  1860. }
  1861. else if (line->u.blocks)
  1862. {
  1863. /* The user expects the line count to be the number of times
  1864. a line has been executed. Simply summing the block count
  1865. will give an artificially high number. The Right Thing
  1866. is to sum the entry counts to the graph of blocks on this
  1867. line, then find the elementary cycles of the local graph
  1868. and add the transition counts of those cycles. */
  1869. block_t *block, *block_p, *block_n;
  1870. gcov_type count = 0;
  1871. /* Reverse the block information. */
  1872. for (block = line->u.blocks, block_p = NULL; block;
  1873. block_p = block, block = block_n)
  1874. {
  1875. block_n = block->chain;
  1876. block->chain = block_p;
  1877. block->u.cycle.ident = ix;
  1878. }
  1879. line->u.blocks = block_p;
  1880. /* Sum the entry arcs. */
  1881. for (block = line->u.blocks; block; block = block->chain)
  1882. {
  1883. arc_t *arc;
  1884. for (arc = block->pred; arc; arc = arc->pred_next)
  1885. {
  1886. if (arc->src->u.cycle.ident != ix)
  1887. count += arc->count;
  1888. if (flag_branches)
  1889. add_branch_counts (&src->coverage, arc);
  1890. }
  1891. /* Initialize the cs_count. */
  1892. for (arc = block->succ; arc; arc = arc->succ_next)
  1893. arc->cs_count = arc->count;
  1894. }
  1895. /* Find the loops. This uses the algorithm described in
  1896. Tiernan 'An Efficient Search Algorithm to Find the
  1897. Elementary Circuits of a Graph', CACM Dec 1970. We hold
  1898. the P array by having each block point to the arc that
  1899. connects to the previous block. The H array is implicitly
  1900. held because of the arc ordering, and the block's
  1901. previous arc pointer.
  1902. Although the algorithm is O(N^3) for highly connected
  1903. graphs, at worst we'll have O(N^2), as most blocks have
  1904. only one or two exits. Most graphs will be small.
  1905. For each loop we find, locate the arc with the smallest
  1906. transition count, and add that to the cumulative
  1907. count. Decrease flow over the cycle and remove the arc
  1908. from consideration. */
  1909. for (block = line->u.blocks; block; block = block->chain)
  1910. {
  1911. block_t *head = block;
  1912. arc_t *arc;
  1913. next_vertex:;
  1914. arc = head->succ;
  1915. current_vertex:;
  1916. while (arc)
  1917. {
  1918. block_t *dst = arc->dst;
  1919. if (/* Already used that arc. */
  1920. arc->cycle
  1921. /* Not to same graph, or before first vertex. */
  1922. || dst->u.cycle.ident != ix
  1923. /* Already in path. */
  1924. || dst->u.cycle.arc)
  1925. {
  1926. arc = arc->succ_next;
  1927. continue;
  1928. }
  1929. if (dst == block)
  1930. {
  1931. /* Found a closing arc. */
  1932. gcov_type cycle_count = arc->cs_count;
  1933. arc_t *cycle_arc = arc;
  1934. arc_t *probe_arc;
  1935. /* Locate the smallest arc count of the loop. */
  1936. for (dst = head; (probe_arc = dst->u.cycle.arc);
  1937. dst = probe_arc->src)
  1938. if (cycle_count > probe_arc->cs_count)
  1939. {
  1940. cycle_count = probe_arc->cs_count;
  1941. cycle_arc = probe_arc;
  1942. }
  1943. count += cycle_count;
  1944. cycle_arc->cycle = 1;
  1945. /* Remove the flow from the cycle. */
  1946. arc->cs_count -= cycle_count;
  1947. for (dst = head; (probe_arc = dst->u.cycle.arc);
  1948. dst = probe_arc->src)
  1949. probe_arc->cs_count -= cycle_count;
  1950. /* Unwind to the cyclic arc. */
  1951. while (head != cycle_arc->src)
  1952. {
  1953. arc = head->u.cycle.arc;
  1954. head->u.cycle.arc = NULL;
  1955. head = arc->src;
  1956. }
  1957. /* Move on. */
  1958. arc = arc->succ_next;
  1959. continue;
  1960. }
  1961. /* Add new block to chain. */
  1962. dst->u.cycle.arc = arc;
  1963. head = dst;
  1964. goto next_vertex;
  1965. }
  1966. /* We could not add another vertex to the path. Remove
  1967. the last vertex from the list. */
  1968. arc = head->u.cycle.arc;
  1969. if (arc)
  1970. {
  1971. /* It was not the first vertex. Move onto next arc. */
  1972. head->u.cycle.arc = NULL;
  1973. head = arc->src;
  1974. arc = arc->succ_next;
  1975. goto current_vertex;
  1976. }
  1977. /* Mark this block as unusable. */
  1978. block->u.cycle.ident = ~0U;
  1979. }
  1980. line->count = count;
  1981. }
  1982. if (line->exists)
  1983. {
  1984. src->coverage.lines++;
  1985. if (line->count)
  1986. src->coverage.lines_executed++;
  1987. }
  1988. }
  1989. }
  1990. /* Output information about ARC number IX. Returns nonzero if
  1991. anything is output. */
  1992. static int
  1993. output_branch_count (FILE *gcov_file, int ix, const arc_t *arc)
  1994. {
  1995. if (arc->is_call_non_return)
  1996. {
  1997. if (arc->src->count)
  1998. {
  1999. fnotice (gcov_file, "call %2d returned %s\n", ix,
  2000. format_gcov (arc->src->count - arc->count,
  2001. arc->src->count, -flag_counts));
  2002. }
  2003. else
  2004. fnotice (gcov_file, "call %2d never executed\n", ix);
  2005. }
  2006. else if (!arc->is_unconditional)
  2007. {
  2008. if (arc->src->count)
  2009. fnotice (gcov_file, "branch %2d taken %s%s\n", ix,
  2010. format_gcov (arc->count, arc->src->count, -flag_counts),
  2011. arc->fall_through ? " (fallthrough)"
  2012. : arc->is_throw ? " (throw)" : "");
  2013. else
  2014. fnotice (gcov_file, "branch %2d never executed\n", ix);
  2015. }
  2016. else if (flag_unconditional && !arc->dst->is_call_return)
  2017. {
  2018. if (arc->src->count)
  2019. fnotice (gcov_file, "unconditional %2d taken %s\n", ix,
  2020. format_gcov (arc->count, arc->src->count, -flag_counts));
  2021. else
  2022. fnotice (gcov_file, "unconditional %2d never executed\n", ix);
  2023. }
  2024. else
  2025. return 0;
  2026. return 1;
  2027. }
  2028. static const char *
  2029. read_line (FILE *file)
  2030. {
  2031. static char *string;
  2032. static size_t string_len;
  2033. size_t pos = 0;
  2034. char *ptr;
  2035. if (!string_len)
  2036. {
  2037. string_len = 200;
  2038. string = XNEWVEC (char, string_len);
  2039. }
  2040. while ((ptr = fgets (string + pos, string_len - pos, file)))
  2041. {
  2042. size_t len = strlen (string + pos);
  2043. if (string[pos + len - 1] == '\n')
  2044. {
  2045. string[pos + len - 1] = 0;
  2046. return string;
  2047. }
  2048. pos += len;
  2049. string = XRESIZEVEC (char, string, string_len * 2);
  2050. string_len *= 2;
  2051. }
  2052. return pos ? string : NULL;
  2053. }
  2054. /* Read in the source file one line at a time, and output that line to
  2055. the gcov file preceded by its execution count and other
  2056. information. */
  2057. static void
  2058. output_lines (FILE *gcov_file, const source_t *src)
  2059. {
  2060. FILE *source_file;
  2061. unsigned line_num; /* current line number. */
  2062. const line_t *line; /* current line info ptr. */
  2063. const char *retval = ""; /* status of source file reading. */
  2064. function_t *fn = NULL;
  2065. fprintf (gcov_file, "%9s:%5d:Source:%s\n", "-", 0, src->coverage.name);
  2066. if (!multiple_files)
  2067. {
  2068. fprintf (gcov_file, "%9s:%5d:Graph:%s\n", "-", 0, bbg_file_name);
  2069. fprintf (gcov_file, "%9s:%5d:Data:%s\n", "-", 0,
  2070. no_data_file ? "-" : da_file_name);
  2071. fprintf (gcov_file, "%9s:%5d:Runs:%u\n", "-", 0, object_runs);
  2072. }
  2073. fprintf (gcov_file, "%9s:%5d:Programs:%u\n", "-", 0, program_count);
  2074. source_file = fopen (src->name, "r");
  2075. if (!source_file)
  2076. {
  2077. fnotice (stderr, "Cannot open source file %s\n", src->name);
  2078. retval = NULL;
  2079. }
  2080. else if (src->file_time == 0)
  2081. fprintf (gcov_file, "%9s:%5d:Source is newer than graph\n", "-", 0);
  2082. if (flag_branches)
  2083. fn = src->functions;
  2084. for (line_num = 1, line = &src->lines[line_num];
  2085. line_num < src->num_lines; line_num++, line++)
  2086. {
  2087. for (; fn && fn->line == line_num; fn = fn->line_next)
  2088. {
  2089. arc_t *arc = fn->blocks[EXIT_BLOCK].pred;
  2090. gcov_type return_count = fn->blocks[EXIT_BLOCK].count;
  2091. gcov_type called_count = fn->blocks[ENTRY_BLOCK].count;
  2092. for (; arc; arc = arc->pred_next)
  2093. if (arc->fake)
  2094. return_count -= arc->count;
  2095. fprintf (gcov_file, "function %s", flag_demangled_names ?
  2096. fn->demangled_name : fn->name);
  2097. fprintf (gcov_file, " called %s",
  2098. format_gcov (called_count, 0, -1));
  2099. fprintf (gcov_file, " returned %s",
  2100. format_gcov (return_count, called_count, 0));
  2101. fprintf (gcov_file, " blocks executed %s",
  2102. format_gcov (fn->blocks_executed, fn->num_blocks - 2, 0));
  2103. fprintf (gcov_file, "\n");
  2104. }
  2105. if (retval)
  2106. retval = read_line (source_file);
  2107. /* For lines which don't exist in the .bb file, print '-' before
  2108. the source line. For lines which exist but were never
  2109. executed, print '#####' or '=====' before the source line.
  2110. Otherwise, print the execution count before the source line.
  2111. There are 16 spaces of indentation added before the source
  2112. line so that tabs won't be messed up. */
  2113. fprintf (gcov_file, "%9s:%5u:%s\n",
  2114. !line->exists ? "-" : line->count
  2115. ? format_gcov (line->count, 0, -1)
  2116. : line->unexceptional ? "#####" : "=====", line_num,
  2117. retval ? retval : "/*EOF*/");
  2118. if (flag_all_blocks)
  2119. {
  2120. block_t *block;
  2121. arc_t *arc;
  2122. int ix, jx;
  2123. for (ix = jx = 0, block = line->u.blocks; block;
  2124. block = block->chain)
  2125. {
  2126. if (!block->is_call_return)
  2127. fprintf (gcov_file, "%9s:%5u-block %2d\n",
  2128. !line->exists ? "-" : block->count
  2129. ? format_gcov (block->count, 0, -1)
  2130. : block->exceptional ? "%%%%%" : "$$$$$",
  2131. line_num, ix++);
  2132. if (flag_branches)
  2133. for (arc = block->succ; arc; arc = arc->succ_next)
  2134. jx += output_branch_count (gcov_file, jx, arc);
  2135. }
  2136. }
  2137. else if (flag_branches)
  2138. {
  2139. int ix;
  2140. arc_t *arc;
  2141. for (ix = 0, arc = line->u.branches; arc; arc = arc->line_next)
  2142. ix += output_branch_count (gcov_file, ix, arc);
  2143. }
  2144. }
  2145. /* Handle all remaining source lines. There may be lines after the
  2146. last line of code. */
  2147. if (retval)
  2148. {
  2149. for (; (retval = read_line (source_file)); line_num++)
  2150. fprintf (gcov_file, "%9s:%5u:%s\n", "-", line_num, retval);
  2151. }
  2152. if (source_file)
  2153. fclose (source_file);
  2154. }