]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Fix for bug 166581: use correct output file name after PID change
authorJosef Weidendorfer <Josef.Weidendorfer@gmx.de>
Fri, 24 Oct 2008 18:50:00 +0000 (18:50 +0000)
committerJosef Weidendorfer <Josef.Weidendorfer@gmx.de>
Fri, 24 Oct 2008 18:50:00 +0000 (18:50 +0000)
This is a little tricky because
* we want to check directly at startup whether the output file
  can be written, thus the file name is set at beginning.
* a fork changes the PID in the child, and thus (potentially) the
  output file name has to be updated. This best is directly before
  generating the profile dump.
* the child after fork needs to be controllable via callgrind_control.
  The setup of the control interface needs the new file name, too.
The fix is to allow multiple calls of CLG(init_dumps), everytime the
output file name is needed.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@8704

callgrind/dump.c

index 6fda4596440ac5f0f1f6b2d0853de2eb89b31b6c..44fb276a53cde8ac7dbe31c5e309334730100561 100644 (file)
@@ -64,13 +64,13 @@ Int CLG_(get_dump_counter)(void)
 
 Char* CLG_(get_out_file)()
 {
-    CLG_ASSERT(dumps_initialized);
+    CLG_(init_dumps)();
     return out_file;
 }
 
 Char* CLG_(get_out_directory)()
 {
-    CLG_ASSERT(dumps_initialized);
+    CLG_(init_dumps)();
     return out_directory;
 }
 
@@ -1616,6 +1616,8 @@ void CLG_(dump_profile)(Char* trigger, Bool only_current_thread)
    CLG_DEBUG(2, "+ dump_profile(Trigger '%s')\n",
            trigger ? trigger : (Char*)"Prg.Term.");
 
+   CLG_(init_dumps)();
+
    if (VG_(clo_verbosity) > 1)
        VG_(message)(Vg_DebugMsg, "Start dumping at BB %llu (%s)...",
                    CLG_(stat).bb_executions,
@@ -1673,15 +1675,35 @@ void init_cmdbuf(void)
  * <out_file> always starts with a full absolute path.
  * If the output format string represents a relative path, the current
  * working directory at program start is used.
+ *
+ * This function has to be called every time a profile dump is generated
+ * to be able to react on PID changes.
  */
 void CLG_(init_dumps)()
 {
    Int lastSlash, i;
    SysRes res;
 
+   static int thisPID = 0;
+   int currentPID = VG_(getpid)();
+   if (currentPID == thisPID) {
+       /* already initialized, and no PID change */
+       CLG_ASSERT(out_file != 0);
+       return;
+   }
+   thisPID = currentPID;
+   
    if (!CLG_(clo).out_format)
      CLG_(clo).out_format = DEFAULT_OUTFORMAT;
 
+   /* If a file name was already set, clean up before */
+   if (out_file) {
+       VG_(free)(out_file);
+       VG_(free)(out_directory);
+       VG_(free)(filename);
+       out_counter = 0;
+   }
+
    // Setup output filename.
    out_file =
        VG_(expand_file_name)("--callgrind-out-file", CLG_(clo).out_format);
@@ -1721,7 +1743,8 @@ void CLG_(init_dumps)()
     }
     if (!res.isError) VG_(close)( (Int)res.res );
 
-    init_cmdbuf();
+    if (!dumps_initialized)
+       init_cmdbuf();
 
     dumps_initialized = True;
 }