From e3b1fbbc440f491893934f0755dabc53e31ed475 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Sun, 1 Jun 2014 00:58:21 +0200 Subject: [PATCH] Prepend output file with 'malloctrace.' even when env var is not set. Also add some code comments on future things to do. --- malloctrace.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/malloctrace.cpp b/malloctrace.cpp index 3577430..70a41b8 100644 --- a/malloctrace.cpp +++ b/malloctrace.cpp @@ -206,13 +206,22 @@ struct Data traceCache.reserve(16384); allocationInfo.reserve(16384); - string outputFileName = env("DUMP_MALLOC_TRACE_OUTPUT") + to_string(getpid()); + string outputFileName = env("DUMP_MALLOC_TRACE_OUTPUT"); + if (outputFileName.empty()) { + // env var might not be set when linked directly into an executable + outputFileName = "malloctrace."; + } + outputFileName += to_string(getpid()); + out = fopen(outputFileName.c_str(), "wa"); if (!out) { fprintf(stderr, "Failed to open output file: %s\n", outputFileName.c_str()); exit(1); } + // TODO: remember meta data about host application, such as cmdline, date of run, ... + + // cleanup environment to prevent tracing of child apps unsetenv("DUMP_MALLOC_TRACE_OUTPUT"); unsetenv("LD_PRELOAD"); } @@ -245,7 +254,7 @@ struct Data if (!fileName || !fileName[0]) { if (modules.empty()) { isExe = true; - ssize_t ret = readlink("/proc/self/exe", buf, sizeof(buf)); + ssize_t ret = readlink("/proc/self/exe", buf, sizeof(buf)); if ((ret > 0) && (ret < (ssize_t)sizeof(buf))) { buf[ret] = 0; fileName = buf; -- 2.7.4