Prepend output file with 'malloctrace.' even when env var is not set.
authorMilian Wolff <mail@milianw.de>
Sat, 31 May 2014 22:58:21 +0000 (00:58 +0200)
committerMilian Wolff <mail@milianw.de>
Mon, 2 Jun 2014 09:31:36 +0000 (11:31 +0200)
Also add some code comments on future things to do.

malloctrace.cpp

index 3577430..70a41b8 100644 (file)
@@ -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;