Commit 75cc66f2 by Martin Liska Committed by Martin Liska

gcov: add new option (--hash-filenames) (PR

	PR gcov-profile/36412
	* doc/gcov.texi: Document --hash-filenames(-x).
	* gcov.c (print_usage): Add the option.
	(process_args): Process the option, sort options alphabetically.
	(md5sum_to_hex): New function.
	(make_gcov_file_name): Do the md5sum and append it to a
	filename.

From-SVN: r239503
parent 3c556bc4
2016-08-16 Martin Liska <mliska@suse.cz>
PR gcov-profile/36412
* doc/gcov.texi: Document --hash-filenames(-x).
* gcov.c (print_usage): Add the option.
(process_args): Process the option, sort options alphabetically.
(md5sum_to_hex): New function.
(make_gcov_file_name): Do the md5sum and append it to a
filename.
2016-08-16 Bin Cheng <bin.cheng@arm.com> 2016-08-16 Bin Cheng <bin.cheng@arm.com>
PR tree-optimization/69848 PR tree-optimization/69848
......
...@@ -133,6 +133,7 @@ gcov [@option{-v}|@option{--version}] [@option{-h}|@option{--help}] ...@@ -133,6 +133,7 @@ gcov [@option{-v}|@option{--version}] [@option{-h}|@option{--help}]
[@option{-r}|@option{--relative-only}] [@option{-r}|@option{--relative-only}]
[@option{-s}|@option{--source-prefix} @var{directory}] [@option{-s}|@option{--source-prefix} @var{directory}]
[@option{-u}|@option{--unconditional-branches}] [@option{-u}|@option{--unconditional-branches}]
[@option{-x}|@option{--hash-filenames}]
@var{files} @var{files}
@c man end @c man end
@c man begin SEEALSO @c man begin SEEALSO
...@@ -278,6 +279,16 @@ branch:28,nottaken ...@@ -278,6 +279,16 @@ branch:28,nottaken
Display demangled function names in output. The default is to show Display demangled function names in output. The default is to show
mangled function names. mangled function names.
@item -x
@itemx --hash-filenames
By default, gcov uses the full pathname of the source files to to create
an output filename. This can lead to long filenames that can overflow
filesystem limits. This option creates names of the form
@file{@var{source-file}##@var{md5}.gcov},
where the @var{source-file} component is the final filename part and
the @var{md5} component is calculated from the full mangled name that
would have been used otherwise.
@end table @end table
@command{gcov} should be run with the current directory the same as that @command{gcov} should be run with the current directory the same as that
......
...@@ -43,6 +43,7 @@ along with Gcov; see the file COPYING3. If not see ...@@ -43,6 +43,7 @@ along with Gcov; see the file COPYING3. If not see
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include "md5.h"
using namespace std; using namespace std;
...@@ -359,6 +360,11 @@ static int flag_demangled_names = 0; ...@@ -359,6 +360,11 @@ static int flag_demangled_names = 0;
static int flag_long_names = 0; static int flag_long_names = 0;
/* For situations when a long name can potentially hit filesystem path limit,
let's calculate md5sum of the path and append it to a file name. */
static int flag_hash_filenames = 0;
/* Output count information for every basic block, not merely those /* Output count information for every basic block, not merely those
that contain line number information. */ that contain line number information. */
...@@ -667,6 +673,7 @@ print_usage (int error_p) ...@@ -667,6 +673,7 @@ print_usage (int error_p)
fnotice (file, " -s, --source-prefix DIR Source prefix to elide\n"); fnotice (file, " -s, --source-prefix DIR Source prefix to elide\n");
fnotice (file, " -u, --unconditional-branches Show unconditional branch counts too\n"); fnotice (file, " -u, --unconditional-branches Show unconditional branch counts too\n");
fnotice (file, " -v, --version Print version number, then exit\n"); fnotice (file, " -v, --version Print version number, then exit\n");
fnotice (file, " -x, --hash-filenames Hash long pathnames\n");
fnotice (file, "\nFor bug reporting instructions, please see:\n%s.\n", fnotice (file, "\nFor bug reporting instructions, please see:\n%s.\n",
bug_report_url); bug_report_url);
exit (status); exit (status);
...@@ -706,6 +713,7 @@ static const struct option options[] = ...@@ -706,6 +713,7 @@ static const struct option options[] =
{ "source-prefix", required_argument, NULL, 's' }, { "source-prefix", required_argument, NULL, 's' },
{ "unconditional-branches", no_argument, NULL, 'u' }, { "unconditional-branches", no_argument, NULL, 'u' },
{ "display-progress", no_argument, NULL, 'd' }, { "display-progress", no_argument, NULL, 'd' },
{ "hash-filenames", no_argument, NULL, 'x' },
{ 0, 0, 0, 0 } { 0, 0, 0, 0 }
}; };
...@@ -716,8 +724,8 @@ process_args (int argc, char **argv) ...@@ -716,8 +724,8 @@ process_args (int argc, char **argv)
{ {
int opt; int opt;
while ((opt = getopt_long (argc, argv, "abcdfhilmno:s:pruv", options, NULL)) != const char *opts = "abcdfhilmno:prs:uvx";
-1) while ((opt = getopt_long (argc, argv, opts, options, NULL)) != -1)
{ {
switch (opt) switch (opt)
{ {
...@@ -770,6 +778,9 @@ process_args (int argc, char **argv) ...@@ -770,6 +778,9 @@ process_args (int argc, char **argv)
break; break;
case 'v': case 'v':
print_version (); print_version ();
case 'x':
flag_hash_filenames = 1;
break;
/* print_version will exit. */ /* print_version will exit. */
default: default:
print_usage (true); print_usage (true);
...@@ -2147,6 +2158,15 @@ canonicalize_name (const char *name) ...@@ -2147,6 +2158,15 @@ canonicalize_name (const char *name)
return result; return result;
} }
/* Print hex representation of 16 bytes from SUM and write it to BUFFER. */
static void
md5sum_to_hex (const char *sum, char *buffer)
{
for (unsigned i = 0; i < 16; i++)
sprintf (buffer + (2 * i), "%02x", sum[i]);
}
/* Generate an output file name. INPUT_NAME is the canonicalized main /* Generate an output file name. INPUT_NAME is the canonicalized main
input file and SRC_NAME is the canonicalized file name. input file and SRC_NAME is the canonicalized file name.
LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation. With LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation. With
...@@ -2184,6 +2204,30 @@ make_gcov_file_name (const char *input_name, const char *src_name) ...@@ -2184,6 +2204,30 @@ make_gcov_file_name (const char *input_name, const char *src_name)
ptr = mangle_name (src_name, ptr); ptr = mangle_name (src_name, ptr);
strcpy (ptr, ".gcov"); strcpy (ptr, ".gcov");
/* When hashing filenames, we shorten them by only using the filename
component and appending a hash of the full (mangled) pathname. */
if (flag_hash_filenames)
{
md5_ctx ctx;
char md5sum[16];
char md5sum_hex[33];
md5_init_ctx (&ctx);
md5_process_bytes (result, strlen (result), &ctx);
md5_finish_ctx (&ctx, md5sum);
md5sum_to_hex (md5sum, md5sum_hex);
free (result);
result = XNEWVEC (char, strlen (src_name) + 50);
ptr = result;
ptr = mangle_name (src_name, ptr);
ptr[0] = ptr[1] = '#';
ptr += 2;
memcpy (ptr, md5sum_hex, 32);
ptr += 32;
strcpy (ptr, ".gcov");
}
return result; return result;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment