From 97a53d1d04d5dc36f64c0897ff0c49c78ef83ffc Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Tue, 5 Jun 2018 14:10:22 +0200 Subject: [PATCH] Support variables in expansion of -fprofile-generate option (PR gcov-profile/47618). 2018-06-05 Martin Liska PR gcov-profile/47618 * doc/invoke.texi: Document how -fprofile-dir format is extended. 2018-06-05 Martin Liska PR gcov-profile/47618 * libgcov-driver-system.c (replace_filename_variables): New function. (gcov_exit_open_gcda_file): Use it. From-SVN: r261199 --- gcc/ChangeLog | 6 +++ gcc/doc/invoke.texi | 14 +++++++ libgcc/ChangeLog | 7 ++++ libgcc/libgcov-driver-system.c | 70 ++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 7f5f4e407d1..25ab6bded67 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2018-06-05 Martin Liska + + PR gcov-profile/47618 + * doc/invoke.texi: Document how -fprofile-dir format + is extended. + 2018-06-05 Richard Biener * tree-cfgcleanup.c (cleanup_control_flow_pre): For edge diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 169dd440059..3d767b64e85 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -11343,6 +11343,20 @@ and its related options. Both absolute and relative paths can be used. By default, GCC uses the current directory as @var{path}, thus the profile data file appears in the same directory as the object file. +When an executable is run in a massive parallel environment, it is recommended +to save profile to different folders. That can be done with variables +in @var{path} that are exported during run-time: + +@table @gcctabopt + +@item %p +process ID. + +@item %q@{VAR@} +value of environment variable @var{VAR} + +@end table + @item -fprofile-generate @itemx -fprofile-generate=@var{path} @opindex fprofile-generate diff --git a/libgcc/ChangeLog b/libgcc/ChangeLog index f2297c0a369..5389999cd8c 100644 --- a/libgcc/ChangeLog +++ b/libgcc/ChangeLog @@ -1,3 +1,10 @@ +2018-06-05 Martin Liska + + PR gcov-profile/47618 + * libgcov-driver-system.c (replace_filename_variables): New + function. + (gcov_exit_open_gcda_file): Use it. + 2018-06-05 Martin Liska * libgcov-driver.c (gcov_compute_histogram): Remove usage diff --git a/libgcc/libgcov-driver-system.c b/libgcc/libgcov-driver-system.c index bf125869dc0..7f3de631701 100644 --- a/libgcc/libgcov-driver-system.c +++ b/libgcc/libgcov-driver-system.c @@ -136,6 +136,74 @@ create_file_directory (char *filename) #endif } +/* Replace filename variables in FILENAME. We currently support expansion: + + %p - process ID + %q{ENV} - value of environment variable ENV + */ + +static char * +replace_filename_variables (char *filename) +{ + char buffer[16]; + char empty[] = ""; + for (char *p = filename; *p != '\0'; p++) + { + unsigned length = strlen (filename); + if (*p == '%' && *(p + 1) != '\0') + { + unsigned start = p - filename; + p++; + char *replacement = NULL; + switch (*p) + { + case 'p': + sprintf (buffer, "%d", getpid ()); + replacement = buffer; + p++; + break; + case 'q': + if (*(p + 1) == '{') + { + p += 2; + char *e = strchr (p, '}'); + if (e) + { + *e = '\0'; + replacement = getenv (p); + if (replacement == NULL) + replacement = empty; + p = e + 1; + } + else + return filename; + } + break; + default: + return filename; + } + + /* Concat beginning of the path, replacement and + ending of the path. */ + unsigned end = length - (p - filename); + unsigned repl_length = strlen (replacement); + + char *buffer = (char *)xmalloc (start + end + repl_length + 1); + char *buffer_ptr = buffer; + buffer_ptr = (char *)mempcpy (buffer_ptr, filename, start); + buffer_ptr = (char *)mempcpy (buffer_ptr, replacement, repl_length); + buffer_ptr = (char *)mempcpy (buffer_ptr, p, end); + *buffer_ptr = '\0'; + + free (filename); + filename = buffer; + p = buffer + start + repl_length; + } + } + + return filename; +} + static void allocate_filename_struct (struct gcov_filename *gf) { @@ -224,6 +292,8 @@ gcov_exit_open_gcda_file (struct gcov_info *gi_ptr, } strcpy (dst, fname); + gf->filename = replace_filename_variables (gf->filename); + if (!gcov_open (gf->filename)) { /* Open failed likely due to missed directory. -- 2.30.2