xref: /netbsd-src/external/gpl3/binutils.old/dist/gprof/source.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos /* source.c - Keep track of source files.
275fd0b74Schristos 
3*e992f068Schristos    Copyright (C) 2000-2022 Free Software Foundation, Inc.
475fd0b74Schristos 
575fd0b74Schristos    This file is part of GNU Binutils.
675fd0b74Schristos 
775fd0b74Schristos    This program is free software; you can redistribute it and/or modify
875fd0b74Schristos    it under the terms of the GNU General Public License as published by
975fd0b74Schristos    the Free Software Foundation; either version 3 of the License, or
1075fd0b74Schristos    (at your option) any later version.
1175fd0b74Schristos 
1275fd0b74Schristos    This program is distributed in the hope that it will be useful,
1375fd0b74Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1475fd0b74Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1575fd0b74Schristos    GNU General Public License for more details.
1675fd0b74Schristos 
1775fd0b74Schristos    You should have received a copy of the GNU General Public License
1875fd0b74Schristos    along with this program; if not, write to the Free Software
1975fd0b74Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
2075fd0b74Schristos    02110-1301, USA.  */
2175fd0b74Schristos 
2275fd0b74Schristos #include "gprof.h"
2375fd0b74Schristos #include "libiberty.h"
2475fd0b74Schristos #include "filenames.h"
2575fd0b74Schristos #include "search_list.h"
2675fd0b74Schristos #include "source.h"
2775fd0b74Schristos 
2875fd0b74Schristos #define EXT_ANNO "-ann"		/* Postfix of annotated files.  */
2975fd0b74Schristos 
3075fd0b74Schristos /* Default option values.  */
31*e992f068Schristos bool create_annotation_files = false;
3275fd0b74Schristos 
3375fd0b74Schristos Search_List src_search_list = {0, 0};
3475fd0b74Schristos Source_File *first_src_file = 0;
3575fd0b74Schristos 
3675fd0b74Schristos 
3775fd0b74Schristos Source_File *
source_file_lookup_path(const char * path)3875fd0b74Schristos source_file_lookup_path (const char *path)
3975fd0b74Schristos {
4075fd0b74Schristos   Source_File *sf;
4175fd0b74Schristos 
4275fd0b74Schristos   for (sf = first_src_file; sf; sf = sf->next)
4375fd0b74Schristos     {
4475fd0b74Schristos       if (FILENAME_CMP (path, sf->name) == 0)
4575fd0b74Schristos 	break;
4675fd0b74Schristos     }
4775fd0b74Schristos 
4875fd0b74Schristos   if (!sf)
4975fd0b74Schristos     {
5075fd0b74Schristos       /* Create a new source file descriptor.  */
5175fd0b74Schristos       sf = (Source_File *) xmalloc (sizeof (*sf));
5275fd0b74Schristos 
5375fd0b74Schristos       memset (sf, 0, sizeof (*sf));
5475fd0b74Schristos 
5575fd0b74Schristos       sf->name = xstrdup (path);
5675fd0b74Schristos       sf->next = first_src_file;
5775fd0b74Schristos       first_src_file = sf;
5875fd0b74Schristos     }
5975fd0b74Schristos 
6075fd0b74Schristos   return sf;
6175fd0b74Schristos }
6275fd0b74Schristos 
6375fd0b74Schristos 
6475fd0b74Schristos Source_File *
source_file_lookup_name(const char * filename)6575fd0b74Schristos source_file_lookup_name (const char *filename)
6675fd0b74Schristos {
6775fd0b74Schristos   const char *fname;
6875fd0b74Schristos   Source_File *sf;
6975fd0b74Schristos 
7075fd0b74Schristos   /* The user cannot know exactly how a filename will be stored in
7175fd0b74Schristos      the debugging info (e.g., ../include/foo.h
7275fd0b74Schristos      vs. /usr/include/foo.h).  So we simply compare the filename
7375fd0b74Schristos      component of a path only.  */
7475fd0b74Schristos   for (sf = first_src_file; sf; sf = sf->next)
7575fd0b74Schristos     {
7675fd0b74Schristos       fname = strrchr (sf->name, '/');
7775fd0b74Schristos 
7875fd0b74Schristos       if (fname)
7975fd0b74Schristos 	++fname;
8075fd0b74Schristos       else
8175fd0b74Schristos 	fname = sf->name;
8275fd0b74Schristos 
8375fd0b74Schristos       if (FILENAME_CMP (filename, fname) == 0)
8475fd0b74Schristos 	break;
8575fd0b74Schristos     }
8675fd0b74Schristos 
8775fd0b74Schristos   return sf;
8875fd0b74Schristos }
8975fd0b74Schristos 
9075fd0b74Schristos 
9175fd0b74Schristos FILE *
annotate_source(Source_File * sf,unsigned int max_width,void (* annote)(char *,unsigned int,int,void *),void * arg)9275fd0b74Schristos annotate_source (Source_File *sf, unsigned int max_width,
9375fd0b74Schristos      void (*annote) (char *, unsigned int, int, void *),
9475fd0b74Schristos      void *arg)
9575fd0b74Schristos {
96*e992f068Schristos   static bool first_file = true;
9775fd0b74Schristos   int i, line_num, nread;
98*e992f068Schristos   bool new_line;
9975fd0b74Schristos   char buf[8192];
100*e992f068Schristos   char *fname;
10175fd0b74Schristos   char *annotation, *name_only;
10275fd0b74Schristos   FILE *ifp, *ofp;
10375fd0b74Schristos   Search_List_Elem *sle = src_search_list.head;
10475fd0b74Schristos 
10575fd0b74Schristos   /* Open input file.  If open fails, walk along search-list until
10675fd0b74Schristos      open succeeds or reaching end of list.  */
107*e992f068Schristos   fname = (char *) sf->name;
10875fd0b74Schristos 
10975fd0b74Schristos   if (IS_ABSOLUTE_PATH (sf->name))
11075fd0b74Schristos     sle = 0;			/* Don't use search list for absolute paths.  */
11175fd0b74Schristos 
11275fd0b74Schristos   name_only = 0;
113*e992f068Schristos   while (true)
11475fd0b74Schristos     {
11575fd0b74Schristos       DBG (SRCDEBUG, printf ("[annotate_source]: looking for %s, trying %s\n",
11675fd0b74Schristos 			     sf->name, fname));
11775fd0b74Schristos 
11875fd0b74Schristos       ifp = fopen (fname, FOPEN_RB);
119*e992f068Schristos       if (fname != sf->name)
120*e992f068Schristos 	free (fname);
12175fd0b74Schristos       if (ifp)
12275fd0b74Schristos 	break;
12375fd0b74Schristos 
12475fd0b74Schristos       if (!sle && !name_only)
12575fd0b74Schristos 	{
12675fd0b74Schristos 	  name_only = strrchr (sf->name, '/');
12775fd0b74Schristos #ifdef HAVE_DOS_BASED_FILE_SYSTEM
12875fd0b74Schristos 	  {
12975fd0b74Schristos 	    char *bslash = strrchr (sf->name, '\\');
13075fd0b74Schristos 	    if (name_only == NULL || (bslash != NULL && bslash > name_only))
13175fd0b74Schristos 	      name_only = bslash;
13275fd0b74Schristos 	    if (name_only == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
13375fd0b74Schristos 	      name_only = (char *)sf->name + 1;
13475fd0b74Schristos 	  }
13575fd0b74Schristos #endif
13675fd0b74Schristos 	  if (name_only)
13775fd0b74Schristos 	    {
13875fd0b74Schristos 	      /* Try search-list again, but this time with name only.  */
13975fd0b74Schristos 	      ++name_only;
14075fd0b74Schristos 	      sle = src_search_list.head;
14175fd0b74Schristos 	    }
14275fd0b74Schristos 	}
14375fd0b74Schristos 
14475fd0b74Schristos       if (sle)
14575fd0b74Schristos 	{
146*e992f068Schristos 	  fname = xmalloc (strlen (sle->path) + 3
147*e992f068Schristos 			   + strlen (name_only ? name_only : sf->name));
14875fd0b74Schristos 	  strcpy (fname, sle->path);
14975fd0b74Schristos #ifdef HAVE_DOS_BASED_FILE_SYSTEM
15075fd0b74Schristos 	  /* d:foo is not the same thing as d:/foo!  */
15175fd0b74Schristos 	  if (fname[strlen (fname) - 1] == ':')
15275fd0b74Schristos 	    strcat (fname, ".");
15375fd0b74Schristos #endif
15475fd0b74Schristos 	  strcat (fname, "/");
15575fd0b74Schristos 
15675fd0b74Schristos 	  if (name_only)
15775fd0b74Schristos 	    strcat (fname, name_only);
15875fd0b74Schristos 	  else
15975fd0b74Schristos 	    strcat (fname, sf->name);
16075fd0b74Schristos 
16175fd0b74Schristos 	  sle = sle->next;
16275fd0b74Schristos 	}
16375fd0b74Schristos       else
16475fd0b74Schristos 	{
16575fd0b74Schristos 	  if (errno == ENOENT)
16675fd0b74Schristos 	    fprintf (stderr, _("%s: could not locate `%s'\n"),
16775fd0b74Schristos 		     whoami, sf->name);
16875fd0b74Schristos 	  else
16975fd0b74Schristos 	    perror (sf->name);
17075fd0b74Schristos 
17175fd0b74Schristos 	  return 0;
17275fd0b74Schristos 	}
17375fd0b74Schristos     }
17475fd0b74Schristos 
17575fd0b74Schristos   ofp = stdout;
17675fd0b74Schristos 
17775fd0b74Schristos   if (create_annotation_files)
17875fd0b74Schristos     {
17975fd0b74Schristos       /* Try to create annotated source file.  */
18075fd0b74Schristos       const char *filename;
18175fd0b74Schristos 
18275fd0b74Schristos       /* Create annotation files in the current working directory.  */
18375fd0b74Schristos       filename = strrchr (sf->name, '/');
18475fd0b74Schristos #ifdef HAVE_DOS_BASED_FILE_SYSTEM
18575fd0b74Schristos 	{
18675fd0b74Schristos 	  char *bslash = strrchr (sf->name, '\\');
18775fd0b74Schristos 	  if (filename == NULL || (bslash != NULL && bslash > filename))
18875fd0b74Schristos 	    filename = bslash;
18975fd0b74Schristos 	  if (filename == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
19075fd0b74Schristos 	    filename = sf->name + 1;
19175fd0b74Schristos 	}
19275fd0b74Schristos #endif
19375fd0b74Schristos       if (filename)
19475fd0b74Schristos 	++filename;
19575fd0b74Schristos       else
19675fd0b74Schristos 	filename = sf->name;
19775fd0b74Schristos 
198*e992f068Schristos       fname = xmalloc (strlen (filename) + strlen (EXT_ANNO) + 1);
19975fd0b74Schristos       strcpy (fname, filename);
20075fd0b74Schristos       strcat (fname, EXT_ANNO);
20175fd0b74Schristos #ifdef __MSDOS__
20275fd0b74Schristos       {
20375fd0b74Schristos 	/* foo.cpp-ann can overwrite foo.cpp due to silent truncation of
20475fd0b74Schristos 	   file names on 8+3 filesystems.  Their `stat' better be good...  */
20575fd0b74Schristos 	struct stat buf1, buf2;
20675fd0b74Schristos 
20775fd0b74Schristos 	if (stat (filename, &buf1) == 0
20875fd0b74Schristos 	    && stat (fname, &buf2) == 0
20975fd0b74Schristos 	    && buf1.st_ino == buf2.st_ino)
21075fd0b74Schristos 	  {
21175fd0b74Schristos 	    char *dot = strrchr (fname, '.');
21275fd0b74Schristos 
213*e992f068Schristos 	    if (!dot)
214*e992f068Schristos 	      dot = fname + strlen (filename);
215*e992f068Schristos 	    strcpy (dot, ".ann");
21675fd0b74Schristos 	  }
21775fd0b74Schristos       }
21875fd0b74Schristos #endif
21975fd0b74Schristos       ofp = fopen (fname, "w");
22075fd0b74Schristos 
22175fd0b74Schristos       if (!ofp)
22275fd0b74Schristos 	{
22375fd0b74Schristos 	  perror (fname);
224*e992f068Schristos 	  free (fname);
22575fd0b74Schristos 	  return 0;
22675fd0b74Schristos 	}
227*e992f068Schristos       free (fname);
22875fd0b74Schristos     }
22975fd0b74Schristos 
23075fd0b74Schristos   /* Print file names if output goes to stdout
23175fd0b74Schristos      and there are more than one source file.  */
23275fd0b74Schristos   if (ofp == stdout)
23375fd0b74Schristos     {
23475fd0b74Schristos       if (first_file)
235*e992f068Schristos 	first_file = false;
23675fd0b74Schristos       else
23775fd0b74Schristos 	fputc ('\n', ofp);
23875fd0b74Schristos 
23975fd0b74Schristos       if (first_output)
240*e992f068Schristos 	first_output = false;
24175fd0b74Schristos       else
24275fd0b74Schristos 	fprintf (ofp, "\f\n");
24375fd0b74Schristos 
24475fd0b74Schristos       fprintf (ofp, _("*** File %s:\n"), sf->name);
24575fd0b74Schristos     }
24675fd0b74Schristos 
24775fd0b74Schristos   annotation = (char *) xmalloc (max_width + 1);
24875fd0b74Schristos   line_num = 1;
249*e992f068Schristos   new_line = true;
25075fd0b74Schristos 
25175fd0b74Schristos   while ((nread = fread (buf, 1, sizeof (buf), ifp)) > 0)
25275fd0b74Schristos     {
25375fd0b74Schristos       for (i = 0; i < nread; ++i)
25475fd0b74Schristos 	{
25575fd0b74Schristos 	  if (new_line)
25675fd0b74Schristos 	    {
25775fd0b74Schristos 	      (*annote) (annotation, max_width, line_num, arg);
25875fd0b74Schristos 	      fputs (annotation, ofp);
25975fd0b74Schristos 	      ++line_num;
26075fd0b74Schristos 	    }
26175fd0b74Schristos 
26275fd0b74Schristos 	  new_line = (buf[i] == '\n');
26375fd0b74Schristos 	  fputc (buf[i], ofp);
26475fd0b74Schristos 	}
26575fd0b74Schristos     }
26675fd0b74Schristos 
26775fd0b74Schristos   free (annotation);
26875fd0b74Schristos   fclose (ifp);
26975fd0b74Schristos   return ofp;
27075fd0b74Schristos }
271