summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAliaksey Kandratsenka <alk@tut.by>2015-06-27 21:15:30 -0700
committerAliaksey Kandratsenka <alk@tut.by>2015-06-27 21:15:30 -0700
commit36066b8df4bc516ade5209a1f60bd84d6448b531 (patch)
treef030202c792deb13d207b4704512b449d74da06e
parentc4069d2d37b67296d675c2d1de42a46dc6d43efc (diff)
downloadgperftools-36066b8df4bc516ade5209a1f60bd84d6448b531.tar.gz
issue-695: implementated TCMALLOC_TRACE_FILE variable
This is contributed by Paolo Bonzini. This commit adds TCMALLOC_TRACE_FILE environment variable, which if defined overrides location of malloc trace file.
-rw-r--r--src/debugallocation.cc20
1 files changed, 16 insertions, 4 deletions
diff --git a/src/debugallocation.cc b/src/debugallocation.cc
index 2a8a20e..1f6296b 100644
--- a/src/debugallocation.cc
+++ b/src/debugallocation.cc
@@ -884,6 +884,9 @@ static void TracePrintf(int fd, const char *fmt, ...) {
va_start(ap, fmt);
const char *p = fmt;
char numbuf[25];
+ if (fd < 0) {
+ return;
+ }
numbuf[sizeof(numbuf)-1] = 0;
while (*p != '\0') { // until end of format string
char *s = &numbuf[sizeof(numbuf)-1];
@@ -955,11 +958,20 @@ static void TracePrintf(int fd, const char *fmt, ...) {
static int TraceFd() {
static int trace_fd = -1;
if (trace_fd == -1) { // Open the trace file on the first call
- trace_fd = open("/tmp/google.alloc", O_CREAT|O_TRUNC|O_WRONLY, 0666);
+ const char *val = getenv("TCMALLOC_TRACE_FILE");
+ bool fallback_to_stderr = false;
+ if (!val) {
+ val = "/tmp/google.alloc";
+ fallback_to_stderr = true;
+ }
+ trace_fd = open(val, O_CREAT|O_TRUNC|O_WRONLY, 0666);
if (trace_fd == -1) {
- trace_fd = 2;
- TracePrintf(trace_fd,
- "Can't open /tmp/google.alloc. Logging to stderr.\n");
+ if (fallback_to_stderr) {
+ trace_fd = 2;
+ TracePrintf(trace_fd, "Can't open %s. Logging to stderr.\n", val);
+ } else {
+ TracePrintf(2, "Can't open %s. Logging disabled.\n", val);
+ }
}
// Add a header to the log.
TracePrintf(trace_fd, "Trace started: %lu\n",