[PATCH] D27912: [XRay] [compiler-rt] Include argv[0] in the log file name.

Martin Pelikán via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 18 22:12:02 PST 2016


pelikan created this revision.
pelikan added a reviewer: dberris.
pelikan added a subscriber: llvm-commits.

If you decide to recompile parts of your Linux distro with XRay, it may
be useful to know which trace belongs to which binary.  While there, get
rid of the incorrect strncat() usage; it always returns a pointer to the
start which makes that if() always true.  Replace with snprintf which is
bounded so that enough from both strings fits nicely.


https://reviews.llvm.org/D27912

Files:
  lib/xray/xray_inmemory_log.cc


Index: lib/xray/xray_inmemory_log.cc
===================================================================
--- lib/xray/xray_inmemory_log.cc
+++ lib/xray/xray_inmemory_log.cc
@@ -112,14 +112,22 @@
   // Open a temporary file once for the log.
   static char TmpFilename[256] = {};
   static char TmpWildcardPattern[] = "XXXXXX";
-  auto E = internal_strncat(TmpFilename, flags()->xray_logfile_base,
-                            sizeof(TmpFilename) - 10);
-  if (static_cast<size_t>((E + 6) - TmpFilename) > (sizeof(TmpFilename) - 1)) {
-    Report("XRay log file base too long: %s\n", flags()->xray_logfile_base);
+  auto Argv = GetArgv();
+  const char *Progname = Argv[0] == nullptr ? "UNKNOWN_PROGNAME" : Argv[0];
+  const char *LastSlash = internal_strrchr(Progname, '/');
+
+  if (LastSlash != nullptr) {
+    Progname = LastSlash + 1;
+  }
+
+  int Need = internal_snprintf(TmpFilename, sizeof(TmpFilename), "%.*s%.*s.%s",
+                               120, flags()->xray_logfile_base,
+                               120, Progname,
+                               TmpWildcardPattern);
+  if (Need > int(sizeof(TmpFilename))) {
+    Report("XRay log file name too long (%d): %s\n", Need, TmpFilename);
     return -1;
   }
-  internal_strncat(TmpFilename, TmpWildcardPattern,
-                   sizeof(TmpWildcardPattern) - 1);
   int Fd = mkstemp(TmpFilename);
   if (Fd == -1) {
     Report("XRay: Failed opening temporary file '%s'; not logging events.\n",


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27912.81915.patch
Type: text/x-patch
Size: 1467 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161219/906fcc6c/attachment.bin>


More information about the llvm-commits mailing list