libgcov-driver-system.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* Routines required for instrumenting a program. */
  2. /* Compile this one with gcc. */
  3. /* Copyright (C) 1989-2022 Free Software Foundation, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #if !IN_GCOV_TOOL
  21. /* Configured via the GCOV_ERROR_FILE environment variable;
  22. it will either be stderr, or a file of the user's choosing.
  23. Non-static to prevent multiple gcov-aware shared objects from
  24. instantiating their own copies. */
  25. FILE *__gcov_error_file = NULL;
  26. #endif
  27. /* A utility function to populate the __gcov_error_file pointer.
  28. This should NOT be called outside of the gcov system driver code. */
  29. static FILE *
  30. get_gcov_error_file (void)
  31. {
  32. #if IN_GCOV_TOOL
  33. return stderr;
  34. #else
  35. if (!__gcov_error_file)
  36. {
  37. const char *gcov_error_filename = getenv ("GCOV_ERROR_FILE");
  38. if (gcov_error_filename)
  39. __gcov_error_file = fopen (gcov_error_filename, "a");
  40. if (!__gcov_error_file)
  41. __gcov_error_file = stderr;
  42. }
  43. return __gcov_error_file;
  44. #endif
  45. }
  46. /* A utility function for outputting errors. */
  47. static int __attribute__((format(printf, 1, 2)))
  48. gcov_error (const char *fmt, ...)
  49. {
  50. int ret;
  51. va_list argp;
  52. va_start (argp, fmt);
  53. FILE *f = get_gcov_error_file ();
  54. ret = vfprintf (f, fmt, argp);
  55. va_end (argp);
  56. if (getenv ("GCOV_EXIT_AT_ERROR"))
  57. {
  58. fprintf (f, "profiling:exiting after an error\n");
  59. exit (1);
  60. }
  61. return ret;
  62. }
  63. #if !IN_GCOV_TOOL
  64. static void
  65. gcov_error_exit (void)
  66. {
  67. if (__gcov_error_file && __gcov_error_file != stderr)
  68. {
  69. fclose (__gcov_error_file);
  70. __gcov_error_file = NULL;
  71. }
  72. }
  73. #endif
  74. /* Make sure path component of the given FILENAME exists, create
  75. missing directories. FILENAME must be writable.
  76. Returns zero on success, or -1 if an error occurred. */
  77. static int
  78. create_file_directory (char *filename)
  79. {
  80. #if !defined(TARGET_POSIX_IO) && !defined(_WIN32)
  81. (void) filename;
  82. return -1;
  83. #else
  84. char *s;
  85. s = filename;
  86. if (HAS_DRIVE_SPEC(s))
  87. s += 2;
  88. if (IS_DIR_SEPARATOR(*s))
  89. ++s;
  90. for (; *s != '\0'; s++)
  91. if (IS_DIR_SEPARATOR(*s))
  92. {
  93. char sep = *s;
  94. *s = '\0';
  95. /* Try to make directory if it doesn't already exist. */
  96. if (access (filename, F_OK) == -1
  97. #ifdef TARGET_POSIX_IO
  98. && mkdir (filename, 0777) == -1
  99. #else
  100. #ifdef mkdir
  101. #undef mkdir
  102. #endif
  103. && mkdir (filename) == -1
  104. #endif
  105. /* The directory might have been made by another process. */
  106. && errno != EEXIST)
  107. {
  108. gcov_error ("profiling:%s:Cannot create directory\n", filename);
  109. *s = sep;
  110. return -1;
  111. };
  112. *s = sep;
  113. };
  114. return 0;
  115. #endif
  116. }
  117. /* Replace filename variables in FILENAME. We currently support expansion:
  118. %p - process ID
  119. %q{ENV} - value of environment variable ENV
  120. */
  121. static char *
  122. replace_filename_variables (char *filename)
  123. {
  124. char buffer[16];
  125. char empty[] = "";
  126. for (char *p = filename; *p != '\0'; p++)
  127. {
  128. unsigned length = strlen (filename);
  129. if (*p == '%' && *(p + 1) != '\0')
  130. {
  131. unsigned start = p - filename;
  132. p++;
  133. char *replacement = NULL;
  134. switch (*p)
  135. {
  136. case 'p':
  137. sprintf (buffer, "%d", getpid ());
  138. replacement = buffer;
  139. p++;
  140. break;
  141. case 'q':
  142. if (*(p + 1) == '{')
  143. {
  144. p += 2;
  145. char *e = strchr (p, '}');
  146. if (e)
  147. {
  148. *e = '\0';
  149. replacement = getenv (p);
  150. if (replacement == NULL)
  151. replacement = empty;
  152. p = e + 1;
  153. }
  154. else
  155. return filename;
  156. }
  157. break;
  158. default:
  159. return filename;
  160. }
  161. /* Concat beginning of the path, replacement and
  162. ending of the path. */
  163. unsigned end = length - (p - filename);
  164. unsigned repl_length = replacement != NULL ? strlen (replacement) : 0;
  165. char *buffer = (char *)xmalloc (start + end + repl_length + 1);
  166. char *buffer_ptr = buffer;
  167. buffer_ptr = (char *)memcpy (buffer_ptr, filename, start);
  168. buffer_ptr += start;
  169. if (replacement != NULL)
  170. buffer_ptr = (char *)memcpy (buffer_ptr, replacement, repl_length);
  171. buffer_ptr += repl_length;
  172. buffer_ptr = (char *)memcpy (buffer_ptr, p, end);
  173. buffer_ptr += end;
  174. *buffer_ptr = '\0';
  175. free (filename);
  176. filename = buffer;
  177. p = buffer + start + repl_length;
  178. }
  179. }
  180. return filename;
  181. }
  182. static void
  183. allocate_filename_struct (struct gcov_filename *gf)
  184. {
  185. const char *gcov_prefix;
  186. size_t prefix_length;
  187. int strip = 0;
  188. gf->filename = NULL;
  189. {
  190. /* Check if the level of dirs to strip off specified. */
  191. char *tmp = getenv("GCOV_PREFIX_STRIP");
  192. if (tmp)
  193. {
  194. strip = atoi (tmp);
  195. /* Do not consider negative values. */
  196. if (strip < 0)
  197. strip = 0;
  198. }
  199. }
  200. gf->strip = strip;
  201. /* Get file name relocation prefix. Non-absolute values are ignored. */
  202. gcov_prefix = getenv("GCOV_PREFIX");
  203. prefix_length = gcov_prefix ? strlen (gcov_prefix) : 0;
  204. /* Remove an unnecessary trailing '/' */
  205. if (prefix_length && IS_DIR_SEPARATOR (gcov_prefix[prefix_length - 1]))
  206. prefix_length--;
  207. /* If no prefix was specified and a prefix stip, then we assume
  208. relative. */
  209. if (!prefix_length && gf->strip)
  210. {
  211. gcov_prefix = ".";
  212. prefix_length = 1;
  213. }
  214. /* Allocate and initialize the filename scratch space. */
  215. if (prefix_length)
  216. {
  217. gf->prefix = (char *) xmalloc (prefix_length + 1);
  218. char *p = (char *) memcpy (gf->prefix, gcov_prefix, prefix_length);
  219. *(p + prefix_length) = '\0';
  220. }
  221. else
  222. gf->prefix = NULL;
  223. }
  224. /* Open a gcda file specified by GI_FILENAME.
  225. Return -1 on error. Return 0 on success. */
  226. static int
  227. gcov_exit_open_gcda_file (struct gcov_info *gi_ptr,
  228. struct gcov_filename *gf)
  229. {
  230. int append_slash = 0;
  231. const char *fname = gi_ptr->filename;
  232. /* Build relocated filename, stripping off leading
  233. directories from the initial filename if requested. */
  234. if (gf->strip > 0)
  235. {
  236. const char *probe = fname;
  237. int level;
  238. /* Remove a leading separator, without counting it. */
  239. if (IS_DIR_SEPARATOR (*probe))
  240. probe++;
  241. /* Skip selected directory levels. If we fall off the end, we
  242. keep the final part. */
  243. for (level = gf->strip; *probe && level; probe++)
  244. if (IS_DIR_SEPARATOR (*probe))
  245. {
  246. fname = probe;
  247. level--;
  248. }
  249. }
  250. /* Update complete filename with stripped original. */
  251. if (gf->prefix)
  252. {
  253. /* Avoid to add multiple drive letters into combined path. */
  254. if (HAS_DRIVE_SPEC(fname))
  255. fname += 2;
  256. if (!IS_DIR_SEPARATOR (*fname))
  257. append_slash = 1;
  258. }
  259. size_t prefix_length = gf->prefix ? strlen (gf->prefix) : 0;
  260. gf->filename = (char *) xmalloc (prefix_length + strlen (fname) + 2);
  261. *gf->filename = '\0';
  262. if (prefix_length)
  263. strcat (gf->filename, gf->prefix);
  264. if (append_slash)
  265. *gf->filename++ = '/';
  266. strcat (gf->filename, fname);
  267. gf->filename = replace_filename_variables (gf->filename);
  268. if (!gcov_open (gf->filename))
  269. {
  270. /* Open failed likely due to missed directory.
  271. Create directory and retry to open file. */
  272. if (create_file_directory (gf->filename))
  273. {
  274. fprintf (stderr, "profiling:%s:Skip\n", gf->filename);
  275. return -1;
  276. }
  277. if (!gcov_open (gf->filename))
  278. {
  279. fprintf (stderr, "profiling:%s:Cannot open\n", gf->filename);
  280. return -1;
  281. }
  282. }
  283. return 0;
  284. }