| [10] | 1 | /* source.c - Keep track of source files.
|
|---|
| 2 |
|
|---|
| 3 | Copyright 2000, 2001 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 2 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., 59 Temple Place - Suite 330, Boston, MA
|
|---|
| 20 | 02111-1307, USA. */
|
|---|
| 21 | |
|---|
| 22 |
|
|---|
| 23 | #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 | DEFUN (source_file_lookup_path, (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 | DEFUN (source_file_lookup_name, (filename), 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 | DEFUN (annotate_source, (sf, max_width, annote, arg),
|
|---|
| 94 | Source_File * sf AND int max_width
|
|---|
| 95 | AND void (*annote) PARAMS ((char *buf, int w, int l, void *arg))
|
|---|
| 96 | AND void *arg)
|
|---|
| 97 | {
|
|---|
| 98 | static bool first_file = TRUE;
|
|---|
| 99 | int i, line_num, nread;
|
|---|
| 100 | bool new_line;
|
|---|
| 101 | char buf[8192];
|
|---|
| 102 | char fname[PATH_MAX];
|
|---|
| 103 | char *annotation, *name_only;
|
|---|
| 104 | FILE *ifp, *ofp;
|
|---|
| 105 | Search_List_Elem *sle = src_search_list.head;
|
|---|
| 106 |
|
|---|
| 107 | /* Open input file. If open fails, walk along search-list until
|
|---|
| 108 | open succeeds or reaching end of list. */
|
|---|
| 109 | strcpy (fname, sf->name);
|
|---|
| 110 |
|
|---|
| 111 | if (IS_ABSOLUTE_PATH (sf->name))
|
|---|
| 112 | sle = 0; /* Don't use search list for absolute paths. */
|
|---|
| 113 |
|
|---|
| 114 | name_only = 0;
|
|---|
| 115 | while (TRUE)
|
|---|
| 116 | {
|
|---|
| 117 | DBG (SRCDEBUG, printf ("[annotate_source]: looking for %s, trying %s\n",
|
|---|
| 118 | sf->name, fname));
|
|---|
| 119 |
|
|---|
| 120 | ifp = fopen (fname, FOPEN_RB);
|
|---|
| 121 | if (ifp)
|
|---|
| 122 | break;
|
|---|
| 123 |
|
|---|
| 124 | if (!sle && !name_only)
|
|---|
| 125 | {
|
|---|
| 126 | name_only = strrchr (sf->name, '/');
|
|---|
| 127 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM
|
|---|
| 128 | {
|
|---|
| 129 | char *bslash = strrchr (sf->name, '\\');
|
|---|
| 130 | if (name_only == NULL || (bslash != NULL && bslash > name_only))
|
|---|
| 131 | name_only = bslash;
|
|---|
| 132 | if (name_only == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
|
|---|
| 133 | name_only = (char *)sf->name + 1;
|
|---|
| 134 | }
|
|---|
| 135 | #endif
|
|---|
| 136 | if (name_only)
|
|---|
| 137 | {
|
|---|
| |
|---|