6a459a
allow customizing the trace file name
6a459a
6a459a
Message-id: <1438961446-14046-1-git-send-email-pbonzini@redhat.com>
6a459a
Patchwork-id: 67396
6a459a
O-Subject: [RHEL7.2 PATCH gperftools] allow customizing the trace file name
6a459a
Bugzilla: 1232702
6a459a
RH-Acked-by: Thomas Huth <thuth@redhat.com>
6a459a
RH-Acked-by: Jeff Nelson <jenelson@redhat.com>
6a459a
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
6a459a
6a459a
The debug version of tcmalloc opens a hard-coded file in /tmp.  Arguably,
6a459a
this is not a vulnerability because it just affects debugging, but we
6a459a
should still fix this before shipping gperftools.  The file is still
6a459a
hard-coded, but at least multiple developers can use tcmalloc tracing
6a459a
at the same time.
6a459a
6a459a
(cherry picked from upstream commit 36066b8df4bc)
6a459a
6a459a
6a459a
6a459a
6a459a
diff --git a/src/debugallocation.cc b/src/debugallocation.cc
6a459a
index 2a8a20e..1f6296b 100644
6a459a
--- a/src/debugallocation.cc
6a459a
+++ b/src/debugallocation.cc
6a459a
@@ -884,6 +884,9 @@ static void TracePrintf(int fd, const char *fmt, ...) {
6a459a
   va_start(ap, fmt);
6a459a
   const char *p = fmt;
6a459a
   char numbuf[25];
6a459a
+  if (fd < 0) {
6a459a
+    return;
6a459a
+  }
6a459a
   numbuf[sizeof(numbuf)-1] = 0;
6a459a
   while (*p != '\0') {              // until end of format string
6a459a
     char *s = &numbuf[sizeof(numbuf)-1];
6a459a
@@ -955,11 +958,20 @@ static void TracePrintf(int fd, const char *fmt, ...) {
6a459a
 static int TraceFd() {
6a459a
   static int trace_fd = -1;
6a459a
   if (trace_fd == -1) {            // Open the trace file on the first call
6a459a
-    trace_fd = open("/tmp/google.alloc", O_CREAT|O_TRUNC|O_WRONLY, 0666);
6a459a
+    const char *val = getenv("TCMALLOC_TRACE_FILE");
6a459a
+    bool fallback_to_stderr = false;
6a459a
+    if (!val) {
6a459a
+      val = "/tmp/google.alloc";
6a459a
+      fallback_to_stderr = true;
6a459a
+    }
6a459a
+    trace_fd = open(val, O_CREAT|O_TRUNC|O_WRONLY, 0666);
6a459a
     if (trace_fd == -1) {
6a459a
-      trace_fd = 2;
6a459a
-      TracePrintf(trace_fd,
6a459a
-                  "Can't open /tmp/google.alloc.  Logging to stderr.\n");
6a459a
+      if (fallback_to_stderr) {
6a459a
+        trace_fd = 2;
6a459a
+        TracePrintf(trace_fd, "Can't open %s.  Logging to stderr.\n", val);
6a459a
+      } else {
6a459a
+        TracePrintf(2, "Can't open %s.  Logging disabled.\n", val);
6a459a
+      }
6a459a
     }
6a459a
     // Add a header to the log.
6a459a
     TracePrintf(trace_fd, "Trace started: %lu\n",