⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gcov.c

📁 GCC编译器源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Gcov.c: prepend line execution counts and branch probabilities to a   source file.   Copyright (C) 1990, 91, 92, 93, 94, 96, 1997 Free Software Foundation, Inc.   Contributed by James E. Wilson of Cygnus Support.   Mangled by Bob Manson of Cygnus Support.Gcov is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.Gcov is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with Gcov; see the file COPYING.  If not, write tothe Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  *//* ??? The code in final.c that produces the struct bb assumes that there is   no padding between the fields.  This is not necessary true.  The current   code can only be trusted if longs and pointers are the same size.  *//* ??? No need to print an execution count on every line, could just print   it on the first line of each block, and only print it on a subsequent   line in the same block if the count changes.  *//* ??? Print a list of the ten blocks with the highest execution counts,   and list the line numbers corresponding to those blocks.  Also, perhaps   list the line numbers with the highest execution counts, only printing   the first if there are several which are all listed in the same block.  *//* ??? Should have an option to print the number of basic blocks, and the   percent of them that are covered.  *//* ??? Does not correctly handle the case where two .bb files refer to the   same included source file.  For example, if one has a short file containing   only inline functions, which is then included in two other files, then   there will be two .bb files which refer to the include file, but there   is no way to get the total execution counts for the included file, can   only get execution counts for one or the other of the including files.  */#include "config.h"#include <stdio.h>#include "gansidecl.h"#include <sys/types.h>#include <sys/stat.h>#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif#ifdef HAVE_STRING_H#include <string.h>#else#ifdef HAVE_STRINGS_H#include <strings.h>#endif#endif#include "gcov-io.h"#ifdef NEED_DECLARATION_RINDEXextern char *rindex ();#endif/* The .bb file format consists of several lists of 4-byte integers   which are the line numbers of each basic block in the file.  Each   list is terminated by a zero.  These lists correspond to the basic   blocks in the reconstructed program flow graph.   A line number of -1 indicates that a source file name (padded to a   long boundary) follows.  The padded file name is followed by   another -1 to make it easy to scan past file names.  A -2 indicates   that a function name (padded to a long boundary) follows; the name   is followed by another -2 to make it easy to scan past the function   name.   The .bbg file contains enough info to enable gcov to reconstruct the   program flow graph.  The first word is the number of basic blocks,   the second word is the number of arcs, followed by the list of arcs   (source bb, dest bb pairs), then a -1, then the number of instrumented   arcs followed by the instrumented arcs, followed by another -1.  This   is repeated for each function.   The .da file contains the execution count for each instrumented branch.   The .bb and .bbg files are created by giving GCC the -ftest-coverage option,   and the .da files are created when an executable compiled with   -fprofile-arcs is run.  *//* The functions in this file for creating and solution program flow graphs   are very similar to functions in the gcc source file profile.c.  */char gcov_version_string[] = "GNU gcov version 1.5\n";/* This is the size of the buffer used to read in source file lines.  */#define STRING_SIZE 200/* One copy of this structure is created for each source file mentioned in the   .bb file.  */struct sourcefile{  char *name;  int maxlineno;  struct sourcefile *next;};/* This points to the head of the sourcefile structure list.  */struct sourcefile *sources;/* One of these is dynamically created whenever we identify an arc in the   function.  */struct adj_list {  int source;  int target;  int arc_count;  unsigned int count_valid : 1;  unsigned int on_tree : 1;  unsigned int fake : 1;  unsigned int fall_through : 1;#if 0  /* Not needed for gcov, but defined in profile.c.  */  rtx branch_insn;#endif  struct adj_list *pred_next;  struct adj_list *succ_next;};/* Count the number of basic blocks, and create an array of these structures,   one for each bb in the function.  */struct bb_info {  struct adj_list *succ;  struct adj_list *pred;  int succ_count;  int pred_count;  int exec_count;  unsigned int count_valid : 1;  unsigned int on_tree : 1;#if 0  /* Not needed for gcov, but defined in profile.c.  */  rtx first_insn;#endif};/* When outputting branch probabilities, one of these structures is created   for each branch/call.  */struct arcdata{  int prob;  int call_insn;  struct arcdata *next;};/* Used to save the list of bb_graphs, one per function.  */struct bb_info_list {  /* Indexed by block number, holds the basic block graph for one function.  */  struct bb_info *bb_graph;  int num_blocks;  struct bb_info_list *next;};/* Holds a list of function basic block graphs.  */static struct bb_info_list *bb_graph_list = 0;/* Name and file pointer of the input file for the basic block graph.  */static char *bbg_file_name;static FILE *bbg_file;/* Name and file pointer of the input file for the arc count data.  */static char *da_file_name;static FILE *da_file;/* Name and file pointer of the input file for the basic block line counts.  */static char *bb_file_name;static FILE *bb_file;/* Holds the entire contents of the bb_file read into memory.  */static char *bb_data;/* Size of bb_data array in longs.  */static long bb_data_size;/* Name and file pointer of the output file.  */static char *gcov_file_name;static FILE *gcov_file;/* Name of the file mentioned on the command line.  */static char *input_file_name = 0;/* Output branch probabilities if true.  */static int output_branch_probs = 0;/* Output a gcov file if this is true.  This is on by default, and can   be turned off by the -n option.  */static int output_gcov_file = 1;/* For included files, make the gcov output file name include the name of   the input source file.  For example, if x.h is included in a.c, then the   output file name is a.c.x.h.gcov instead of x.h.gcov.  This works only   when a single source file is specified.  */static int output_long_names = 0;/* Output summary info for each function.  */static int output_function_summary = 0;/* Object directory file prefix.  This is the directory where .bb and .bbg   files are looked for, if non-zero.  */static char *object_directory = 0;/* Forward declarations.  */static void process_args ();static void open_files ();static void read_files ();static void scan_for_source_files ();static void output_data ();char * xmalloc ();intmain (argc, argv)     int argc;     char **argv;{  process_args (argc, argv);  open_files ();  read_files ();  scan_for_source_files ();  output_data ();  return 0;}char *xmalloc (size)     unsigned size;{  register char *value = (char *) malloc (size);  if (value == 0)    {      fprintf (stderr, "error: virtual memory exhausted");      exit (FATAL_EXIT_CODE);    }  return value;}/* More 'friendly' abort that prints the line and file.   config.h can #define abort fancy_abort if you like that sort of thing.  */voidfancy_abort (){  fprintf (stderr, "Internal gcc abort.\n");  exit (FATAL_EXIT_CODE);}/* Print a usage message and exit.  */static voidprint_usage (){  fprintf (stderr, "gcov [-b] [-v] [-n] [-l] [-f] [-o OBJDIR] file\n");  exit (FATAL_EXIT_CODE);}/* Parse the command line.  */static voidprocess_args (argc, argv)     int argc;     char **argv;{  int i;  for (i = 1; i < argc; i++)    {      if (argv[i][0] == '-')	{	  if (argv[i][1] == 'b')	    output_branch_probs = 1;	  else if (argv[i][1] == 'v')	    fputs (gcov_version_string, stderr);	  else if (argv[i][1] == 'n')	    output_gcov_file = 0;	  else if (argv[i][1] == 'l')	    output_long_names = 1;	  else if (argv[i][1] == 'f')	    output_function_summary = 1;	  else if (argv[i][1] == 'o' && argv[i][2] == '\0')	    object_directory = argv[++i];	  else	    print_usage ();	}      else if (! input_file_name)	input_file_name = argv[i];      else	print_usage ();    }  if (! input_file_name)    print_usage ();}/* Find and open the .bb, .da, and .bbg files.  */static voidopen_files (){  int count, objdir_count;  char *cptr;  /* Determine the names of the .bb, .bbg, and .da files.  Strip off the     extension, if any, and append the new extensions.  */  count = strlen (input_file_name);  if (object_directory)    objdir_count = strlen (object_directory);  else    objdir_count = 0;  da_file_name = xmalloc (count + objdir_count + 4);  bb_file_name = xmalloc (count + objdir_count + 4);  bbg_file_name = xmalloc (count + objdir_count + 5);  if (object_directory)    {      strcpy (da_file_name, object_directory);      strcpy (bb_file_name, object_directory);      strcpy (bbg_file_name, object_directory);      if (object_directory[objdir_count - 1] != '/')	{	  strcat (da_file_name, "/");	  strcat (bb_file_name, "/");	  strcat (bbg_file_name, "/");	}      cptr = rindex (input_file_name, '/');      if (cptr)	{	  strcat (da_file_name, cptr + 1);	  strcat (bb_file_name, cptr + 1);	  strcat (bbg_file_name, cptr + 1);	}      else	{	  strcat (da_file_name, input_file_name);	  strcat (bb_file_name, input_file_name);	  strcat (bbg_file_name, input_file_name);	}    }  else    {      strcpy (da_file_name, input_file_name);      strcpy (bb_file_name, input_file_name);      strcpy (bbg_file_name, input_file_name);    }  cptr = rindex (bb_file_name, '.');  if (cptr)    strcpy (cptr, ".bb");  else    strcat (bb_file_name, ".bb");  cptr = rindex (da_file_name, '.');  if (cptr)    strcpy (cptr, ".da");  else    strcat (da_file_name, ".da");  cptr = rindex (bbg_file_name, '.');  if (cptr)    strcpy (cptr, ".bbg");  else    strcat (bbg_file_name, ".bbg");  bb_file = fopen (bb_file_name, "r");  if (bb_file == NULL)    {      fprintf (stderr, "Could not open basic block file %s.\n", bb_file_name);      exit (FATAL_EXIT_CODE);    }  /* If none of the functions in the file were executed, then there won't     be a .da file.  Just assume that all counts are zero in this case.  */  da_file = fopen (da_file_name, "r");  if (da_file == NULL)    {      fprintf (stderr, "Could not open data file %s.\n", da_file_name);      fprintf (stderr, "Assuming that all execution counts are zero.\n");    }      bbg_file = fopen (bbg_file_name, "r");  if (bbg_file == NULL)    {      fprintf (stderr, "Could not open program flow graph file %s.\n",	       bbg_file_name);      exit (FATAL_EXIT_CODE);    }  /* Check for empty .bbg file.  This indicates that there is no executable     code in this source file.  */  /* Set the EOF condition if at the end of file.  */  ungetc (getc (bbg_file), bbg_file);  if (feof (bbg_file))    {      fprintf (stderr, "No executable code associated with file %s.\n",	       input_file_name);      exit (FATAL_EXIT_CODE);    }}/* Initialize a new arc.  */static voidinit_arc (arcptr, source, target, bb_graph)     struct adj_list *arcptr;     int source, target;     struct bb_info *bb_graph;{  arcptr->target = target;  arcptr->source = source;  arcptr->arc_count = 0;  arcptr->count_valid = 0;  arcptr->on_tree = 0;  arcptr->fake = 0;  arcptr->fall_through = 0;  arcptr->succ_next = bb_graph[source].succ;  bb_graph[source].succ = arcptr;  bb_graph[source].succ_count++;  arcptr->pred_next = bb_graph[target].pred;  bb_graph[target].pred = arcptr;  bb_graph[target].pred_count++;}/* Reverse the arcs on a arc list.  */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -