1 /* Routines required for instrumenting a program. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989-2014 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* A utility function for outputing errors. */
28 static int __attribute__((format(printf
, 1, 2)))
29 gcov_error (const char *fmt
, ...)
34 ret
= vfprintf (stderr
, fmt
, argp
);
39 /* Make sure path component of the given FILENAME exists, create
40 missing directories. FILENAME must be writable.
41 Returns zero on success, or -1 if an error occurred. */
44 create_file_directory (char *filename
)
46 #if !defined(TARGET_POSIX_IO) && !defined(_WIN32)
54 if (HAS_DRIVE_SPEC(s
))
56 if (IS_DIR_SEPARATOR(*s
))
58 for (; *s
!= '\0'; s
++)
59 if (IS_DIR_SEPARATOR(*s
))
64 /* Try to make directory if it doesn't already exist. */
65 if (access (filename
, F_OK
) == -1
66 #ifdef TARGET_POSIX_IO
67 && mkdir (filename
, 0755) == -1
69 && mkdir (filename
) == -1
71 /* The directory might have been made by another process. */
74 gcov_error ("profiling:%s:Cannot create directory\n", filename
);
86 allocate_filename_struct (struct gcov_filename
*gf
)
88 const char *gcov_prefix
;
93 /* Check if the level of dirs to strip off specified. */
94 char *tmp
= getenv("GCOV_PREFIX_STRIP");
98 /* Do not consider negative values. */
105 /* Get file name relocation prefix. Non-absolute values are ignored. */
106 gcov_prefix
= getenv("GCOV_PREFIX");
107 prefix_length
= gcov_prefix
? strlen (gcov_prefix
) : 0;
109 /* Remove an unnecessary trailing '/' */
110 if (prefix_length
&& IS_DIR_SEPARATOR (gcov_prefix
[prefix_length
- 1]))
113 /* If no prefix was specified and a prefix stip, then we assume
115 if (!prefix_length
&& gf
->strip
)
120 gf
->prefix
= prefix_length
;
122 /* Allocate and initialize the filename scratch space. */
123 gf
->filename
= (char *) xmalloc (gf
->max_length
+ prefix_length
+ 2);
125 memcpy (gf
->filename
, gcov_prefix
, prefix_length
);
129 gcov_open_by_filename (char *gi_filename
)
131 if (!gcov_open (gi_filename
))
133 /* Open failed likely due to missed directory.
134 Create directory and retry to open file. */
135 if (create_file_directory (gi_filename
))
137 fprintf (stderr
, "profiling:%s:Skip\n", gi_filename
);
140 if (!gcov_open (gi_filename
))
142 fprintf (stderr
, "profiling:%s:Cannot open\n", gi_filename
);
149 #define GCOV_GET_FILENAME gcov_strip_leading_dirs
151 /* Strip GCOV_PREFIX_STRIP levels of leading '/' from FILENAME and
152 put the result into GI_FILENAME_UP. */
155 gcov_strip_leading_dirs (int prefix_length
, int gcov_prefix_strip
,
156 const char *filename
, char *gi_filename_up
)
158 /* Avoid to add multiple drive letters into combined path. */
159 if (prefix_length
!= 0 && HAS_DRIVE_SPEC(filename
))
162 /* Build relocated filename, stripping off leading
163 directories from the initial filename if requested. */
164 if (gcov_prefix_strip
> 0)
166 const char *probe
= filename
;
169 /* Remove a leading separator, without counting it. */
170 if (IS_DIR_SEPARATOR (*probe
))
173 /* Skip selected directory levels. If we fall off the end, we
174 keep the final part. */
175 for (level
= gcov_prefix_strip
; *probe
&& level
; probe
++)
176 if (IS_DIR_SEPARATOR (*probe
))
183 /* Update complete filename with stripped original. */
184 if (prefix_length
!= 0 && !IS_DIR_SEPARATOR (*filename
))
186 /* If prefix is given, add directory separator. */
187 strcpy (gi_filename_up
, "/");
188 strcpy (gi_filename_up
+ 1, filename
);
191 strcpy (gi_filename_up
, filename
);
194 /* Open a gcda file specified by GI_FILENAME.
195 Return -1 on error. Return 0 on success. */
198 gcov_exit_open_gcda_file (struct gcov_info
*gi_ptr
, struct gcov_filename
*gf
)
200 int gcov_prefix_strip
;
201 size_t prefix_length
;
202 char *gi_filename_up
;
204 gcov_prefix_strip
= gf
->strip
;
205 gi_filename_up
= gf
->filename
+ gf
->prefix
;
206 prefix_length
= gf
->prefix
;
208 GCOV_GET_FILENAME (prefix_length
, gcov_prefix_strip
, gi_ptr
->filename
,
211 return gcov_open_by_filename (gf
->filename
);