libgcov-driver-system.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Routines required for instrumenting a program. */
  2. /* Compile this one with gcc. */
  3. /* Copyright (C) 1989-2015 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. /* A utility function for outputing errors. */
  21. static int __attribute__((format(printf, 1, 2)))
  22. gcov_error (const char *fmt, ...)
  23. {
  24. int ret;
  25. va_list argp;
  26. va_start (argp, fmt);
  27. ret = vfprintf (stderr, fmt, argp);
  28. va_end (argp);
  29. return ret;
  30. }
  31. /* Make sure path component of the given FILENAME exists, create
  32. missing directories. FILENAME must be writable.
  33. Returns zero on success, or -1 if an error occurred. */
  34. static int
  35. create_file_directory (char *filename)
  36. {
  37. #if !defined(TARGET_POSIX_IO) && !defined(_WIN32)
  38. (void) filename;
  39. return -1;
  40. #else
  41. char *s;
  42. s = filename;
  43. if (HAS_DRIVE_SPEC(s))
  44. s += 2;
  45. if (IS_DIR_SEPARATOR(*s))
  46. ++s;
  47. for (; *s != '\0'; s++)
  48. if (IS_DIR_SEPARATOR(*s))
  49. {
  50. char sep = *s;
  51. *s = '\0';
  52. /* Try to make directory if it doesn't already exist. */
  53. if (access (filename, F_OK) == -1
  54. #ifdef TARGET_POSIX_IO
  55. && mkdir (filename, 0755) == -1
  56. #else
  57. #ifdef mkdir
  58. #undef mkdir
  59. #endif
  60. && mkdir (filename) == -1
  61. #endif
  62. /* The directory might have been made by another process. */
  63. && errno != EEXIST)
  64. {
  65. gcov_error ("profiling:%s:Cannot create directory\n", filename);
  66. *s = sep;
  67. return -1;
  68. };
  69. *s = sep;
  70. };
  71. return 0;
  72. #endif
  73. }
  74. static void
  75. allocate_filename_struct (struct gcov_filename *gf)
  76. {
  77. const char *gcov_prefix;
  78. size_t prefix_length;
  79. int strip = 0;
  80. {
  81. /* Check if the level of dirs to strip off specified. */
  82. char *tmp = getenv("GCOV_PREFIX_STRIP");
  83. if (tmp)
  84. {
  85. strip = atoi (tmp);
  86. /* Do not consider negative values. */
  87. if (strip < 0)
  88. strip = 0;
  89. }
  90. }
  91. gf->strip = strip;
  92. /* Get file name relocation prefix. Non-absolute values are ignored. */
  93. gcov_prefix = getenv("GCOV_PREFIX");
  94. prefix_length = gcov_prefix ? strlen (gcov_prefix) : 0;
  95. /* Remove an unnecessary trailing '/' */
  96. if (prefix_length && IS_DIR_SEPARATOR (gcov_prefix[prefix_length - 1]))
  97. prefix_length--;
  98. /* If no prefix was specified and a prefix stip, then we assume
  99. relative. */
  100. if (!prefix_length && gf->strip)
  101. {
  102. gcov_prefix = ".";
  103. prefix_length = 1;
  104. }
  105. gf->prefix = prefix_length;
  106. /* Allocate and initialize the filename scratch space. */
  107. gf->filename = (char *) xmalloc (gf->max_length + prefix_length + 2);
  108. if (prefix_length)
  109. memcpy (gf->filename, gcov_prefix, prefix_length);
  110. }
  111. /* Open a gcda file specified by GI_FILENAME.
  112. Return -1 on error. Return 0 on success. */
  113. static int
  114. gcov_exit_open_gcda_file (struct gcov_info *gi_ptr,
  115. struct gcov_filename *gf)
  116. {
  117. const char *fname = gi_ptr->filename;
  118. char *dst = gf->filename + gf->prefix;
  119. fname = gi_ptr->filename;
  120. /* Build relocated filename, stripping off leading
  121. directories from the initial filename if requested. */
  122. if (gf->strip > 0)
  123. {
  124. const char *probe = fname;
  125. int level;
  126. /* Remove a leading separator, without counting it. */
  127. if (IS_DIR_SEPARATOR (*probe))
  128. probe++;
  129. /* Skip selected directory levels. If we fall off the end, we
  130. keep the final part. */
  131. for (level = gf->strip; *probe && level; probe++)
  132. if (IS_DIR_SEPARATOR (*probe))
  133. {
  134. fname = probe;
  135. level--;
  136. }
  137. }
  138. /* Update complete filename with stripped original. */
  139. if (gf->prefix)
  140. {
  141. /* Avoid to add multiple drive letters into combined path. */
  142. if (HAS_DRIVE_SPEC(fname))
  143. fname += 2;
  144. if (!IS_DIR_SEPARATOR (*fname))
  145. *dst++ = '/';
  146. }
  147. strcpy (dst, fname);
  148. if (!gcov_open (gf->filename))
  149. {
  150. /* Open failed likely due to missed directory.
  151. Create directory and retry to open file. */
  152. if (create_file_directory (gf->filename))
  153. {
  154. fprintf (stderr, "profiling:%s:Skip\n", gf->filename);
  155. return -1;
  156. }
  157. if (!gcov_open (gf->filename))
  158. {
  159. fprintf (stderr, "profiling:%s:Cannot open\n", gf->filename);
  160. return -1;
  161. }
  162. }
  163. return 0;
  164. }