Home | History | Annotate | Line # | Download | only in gprof
source.c revision 1.8
      1 /* source.c - Keep track of source files.
      2 
      3    Copyright (C) 2000-2022 Free Software Foundation, Inc.
      4 
      5    This file is part of GNU Binutils.
      6 
      7    This program 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 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the Free Software
     19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
     20    02110-1301, USA.  */
     21 
     22 #include "gprof.h"
     24 #include "libiberty.h"
     25 #include "filenames.h"
     26 #include "search_list.h"
     27 #include "source.h"
     28 
     29 #define EXT_ANNO "-ann"		/* Postfix of annotated files.  */
     30 
     31 /* Default option values.  */
     32 bool create_annotation_files = false;
     33 
     34 Search_List src_search_list = {0, 0};
     35 Source_File *first_src_file = 0;
     36 
     37 
     38 Source_File *
     39 source_file_lookup_path (const char *path)
     40 {
     41   Source_File *sf;
     42 
     43   for (sf = first_src_file; sf; sf = sf->next)
     44     {
     45       if (FILENAME_CMP (path, sf->name) == 0)
     46 	break;
     47     }
     48 
     49   if (!sf)
     50     {
     51       /* Create a new source file descriptor.  */
     52       sf = (Source_File *) xmalloc (sizeof (*sf));
     53 
     54       memset (sf, 0, sizeof (*sf));
     55 
     56       sf->name = xstrdup (path);
     57       sf->next = first_src_file;
     58       first_src_file = sf;
     59     }
     60 
     61   return sf;
     62 }
     63 
     64 
     65 Source_File *
     66 source_file_lookup_name (const char *filename)
     67 {
     68   const char *fname;
     69   Source_File *sf;
     70 
     71   /* The user cannot know exactly how a filename will be stored in
     72      the debugging info (e.g., ../include/foo.h
     73      vs. /usr/include/foo.h).  So we simply compare the filename
     74      component of a path only.  */
     75   for (sf = first_src_file; sf; sf = sf->next)
     76     {
     77       fname = strrchr (sf->name, '/');
     78 
     79       if (fname)
     80 	++fname;
     81       else
     82 	fname = sf->name;
     83 
     84       if (FILENAME_CMP (filename, fname) == 0)
     85 	break;
     86     }
     87 
     88   return sf;
     89 }
     90 
     91 
     92 FILE *
     93 annotate_source (Source_File *sf, unsigned int max_width,
     94      void (*annote) (char *, unsigned int, int, void *),
     95      void *arg)
     96 {
     97   static bool first_file = true;
     98   int i, line_num, nread;
     99   bool new_line;
    100   char buf[8192];
    101   char *fname;
    102   char *annotation, *name_only;
    103   FILE *ifp, *ofp;
    104   Search_List_Elem *sle = src_search_list.head;
    105 
    106   /* Open input file.  If open fails, walk along search-list until
    107      open succeeds or reaching end of list.  */
    108   fname = (char *) sf->name;
    109 
    110   if (IS_ABSOLUTE_PATH (sf->name))
    111     sle = 0;			/* Don't use search list for absolute paths.  */
    112 
    113   name_only = 0;
    114   while (true)
    115     {
    116       DBG (SRCDEBUG, printf ("[annotate_source]: looking for %s, trying %s\n",
    117 			     sf->name, fname));
    118 
    119       ifp = fopen (fname, FOPEN_RB);
    120       if (fname != sf->name)
    121 	free (fname);
    122       if (ifp)
    123 	break;
    124 
    125       if (!sle && !name_only)
    126 	{
    127 	  name_only = strrchr (sf->name, '/');
    128 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
    129 	  {
    130 	    char *bslash = strrchr (sf->name, '\\');
    131 	    if (name_only == NULL || (bslash != NULL && bslash > name_only))
    132 	      name_only = bslash;
    133 	    if (name_only == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
    134 	      name_only = (char *)sf->name + 1;
    135 	  }
    136 #endif
    137 	  if (name_only)
    138 	    {
    139 	      /* Try search-list again, but this time with name only.  */
    140 	      ++name_only;
    141 	      sle = src_search_list.head;
    142 	    }
    143 	}
    144 
    145       if (sle)
    146 	{
    147 	  fname = xmalloc (strlen (sle->path) + 3
    148 			   + strlen (name_only ? name_only : sf->name));
    149 	  strcpy (fname, sle->path);
    150 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
    151 	  /* d:foo is not the same thing as d:/foo!  */
    152 	  if (fname[strlen (fname) - 1] == ':')
    153 	    strcat (fname, ".");
    154 #endif
    155 	  strcat (fname, "/");
    156 
    157 	  if (name_only)
    158 	    strcat (fname, name_only);
    159 	  else
    160 	    strcat (fname, sf->name);
    161 
    162 	  sle = sle->next;
    163 	}
    164       else
    165 	{
    166 	  if (errno == ENOENT)
    167 	    fprintf (stderr, _("%s: could not locate `%s'\n"),
    168 		     whoami, sf->name);
    169 	  else
    170 	    perror (sf->name);
    171 
    172 	  return 0;
    173 	}
    174     }
    175 
    176   ofp = stdout;
    177 
    178   if (create_annotation_files)
    179     {
    180       /* Try to create annotated source file.  */
    181       const char *filename;
    182 
    183       /* Create annotation files in the current working directory.  */
    184       filename = strrchr (sf->name, '/');
    185 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
    186 	{
    187 	  char *bslash = strrchr (sf->name, '\\');
    188 	  if (filename == NULL || (bslash != NULL && bslash > filename))
    189 	    filename = bslash;
    190 	  if (filename == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
    191 	    filename = sf->name + 1;
    192 	}
    193 #endif
    194       if (filename)
    195 	++filename;
    196       else
    197 	filename = sf->name;
    198 
    199       fname = xmalloc (strlen (filename) + strlen (EXT_ANNO) + 1);
    200       strcpy (fname, filename);
    201       strcat (fname, EXT_ANNO);
    202 #ifdef __MSDOS__
    203       {
    204 	/* foo.cpp-ann can overwrite foo.cpp due to silent truncation of
    205 	   file names on 8+3 filesystems.  Their `stat' better be good...  */
    206 	struct stat buf1, buf2;
    207 
    208 	if (stat (filename, &buf1) == 0
    209 	    && stat (fname, &buf2) == 0
    210 	    && buf1.st_ino == buf2.st_ino)
    211 	  {
    212 	    char *dot = strrchr (fname, '.');
    213 
    214 	    if (!dot)
    215 	      dot = fname + strlen (filename);
    216 	    strcpy (dot, ".ann");
    217 	  }
    218       }
    219 #endif
    220       ofp = fopen (fname, "w");
    221 
    222       if (!ofp)
    223 	{
    224 	  perror (fname);
    225 	  free (fname);
    226 	  return 0;
    227 	}
    228       free (fname);
    229     }
    230 
    231   /* Print file names if output goes to stdout
    232      and there are more than one source file.  */
    233   if (ofp == stdout)
    234     {
    235       if (first_file)
    236 	first_file = false;
    237       else
    238 	fputc ('\n', ofp);
    239 
    240       if (first_output)
    241 	first_output = false;
    242       else
    243 	fprintf (ofp, "\f\n");
    244 
    245       fprintf (ofp, _("*** File %s:\n"), sf->name);
    246     }
    247 
    248   annotation = (char *) xmalloc (max_width + 1);
    249   line_num = 1;
    250   new_line = true;
    251 
    252   while ((nread = fread (buf, 1, sizeof (buf), ifp)) > 0)
    253     {
    254       for (i = 0; i < nread; ++i)
    255 	{
    256 	  if (new_line)
    257 	    {
    258 	      (*annote) (annotation, max_width, line_num, arg);
    259 	      fputs (annotation, ofp);
    260 	      ++line_num;
    261 	    }
    262 
    263 	  new_line = (buf[i] == '\n');
    264 	  fputc (buf[i], ofp);
    265 	}
    266     }
    267 
    268   free (annotation);
    269   fclose (ifp);
    270   return ofp;
    271 }
    272