|
From: <sv...@va...> - 2006-09-20 21:29:48
|
Author: weidendo
Date: 2006-09-20 22:29:39 +0100 (Wed, 20 Sep 2006)
New Revision: 6082
Log:
Callgrind: fix interactive control after fork()
This fixes bug 134316: when an program in callgrind does
a fork, callgrind_control does show both now, and they
can be controlled separately.
However, missing in this patch is zeroing of cost centers
directly after the clone syscall in the child.
Modified:
trunk/callgrind/command.c
trunk/callgrind/dump.c
trunk/callgrind/global.h
trunk/callgrind/main.c
Modified: trunk/callgrind/command.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/callgrind/command.c 2006-09-19 13:29:36 UTC (rev 6081)
+++ trunk/callgrind/command.c 2006-09-20 21:29:39 UTC (rev 6082)
@@ -48,20 +48,35 @@
static Char* info_file =3D 0;
static Char* dump_base =3D 0;
=20
-static Bool command_inited =3D False;
+static Int thisPID =3D 0;
=20
-void CLG_(init_command)(Char* dir, Char* dumps)
+/**
+ * Setup for interactive control of a callgrind run
+ */
+static void setup_control(void)
{
- Int fd =3D -1, size;
+ Int fd, size;
SysRes res;
+ Char* dir, *dump_filename;
=20
- dump_base =3D dumps;
+ CLG_ASSERT(thisPID !=3D 0);
=20
+ fd =3D -1;
+ dir =3D CLG_(get_base_directory)();
+ dump_base =3D CLG_(get_dump_file_base)();
+
+ /* base name of dump files with PID ending */
+ size =3D VG_(strlen)(dump_base) + 10;
+ dump_filename =3D (char*) CLG_MALLOC(size);
+ CLG_ASSERT(dump_filename !=3D 0);
+ VG_(sprintf)(dump_filename, "%s.%d", dump_base, thisPID);
+
+ /* name of command file */
size =3D VG_(strlen)(dir) + VG_(strlen)(DEFAULT_COMMANDNAME) +10;
command_file =3D (char*) CLG_MALLOC(size);
CLG_ASSERT(command_file !=3D 0);
VG_(sprintf)(command_file, "%s/%s.%d",
- dir, DEFAULT_COMMANDNAME, VG_(getpid)());
+ dir, DEFAULT_COMMANDNAME, thisPID);
=20
/* This is for compatibility with the "Force Now" Button of current
* KCachegrind releases, as it doesn't use ".pid" to distinguish
@@ -76,7 +91,7 @@
result_file =3D (char*) CLG_MALLOC(size);
CLG_ASSERT(result_file !=3D 0);
VG_(sprintf)(result_file, "%s/%s.%d",
- dir, DEFAULT_RESULTNAME, VG_(getpid)());
+ dir, DEFAULT_RESULTNAME, thisPID);
=20
/* If we get a command from a command file without .pid, use
* a result file without .pid suffix
@@ -88,9 +103,10 @@
=20
info_file =3D (char*) CLG_MALLOC(VG_(strlen)(DEFAULT_INFONAME) + 10);
CLG_ASSERT(info_file !=3D 0);
- VG_(sprintf)(info_file, "%s.%d", DEFAULT_INFONAME, VG_(getpid)());
+ VG_(sprintf)(info_file, "%s.%d", DEFAULT_INFONAME, thisPID);
=20
- CLG_DEBUG(1, " dump file base: '%s'\n", dump_base);
+ CLG_DEBUG(1, "Setup for interactive control (PID: %d):\n", thisPID);
+ CLG_DEBUG(1, " dump file base: '%s'\n", dump_filename);
CLG_DEBUG(1, " command file: '%s'\n", command_file);
CLG_DEBUG(1, " result file: '%s'\n", result_file);
CLG_DEBUG(1, " info file: '%s'\n", info_file);
@@ -128,7 +144,7 @@
VG_(sprintf)(buf, "base: %s\n", dir);
VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
=20
- VG_(sprintf)(buf, "dumps: %s\n", dump_base);
+ VG_(sprintf)(buf, "dumps: %s\n", dump_filename);
VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
=20
VG_(sprintf)(buf, "control: %s\n", command_file);
@@ -149,8 +165,12 @@
VG_(write)(fd, "\n", 1);
VG_(close)(fd);
}
+}
=20
- command_inited =3D True;
+void CLG_(init_command)()
+{
+ thisPID =3D VG_(getpid)();
+ setup_control();
}
=20
void CLG_(finish_command)()
@@ -355,14 +375,31 @@
Char *cmdPos =3D 0, *cmdNextLine =3D 0;
Int fd, bytesRead =3D 0, do_kill =3D 0;
SysRes res;
+ Int currentPID;
+ static Int check_counter =3D 0;
=20
- if (!command_inited) return;
+ /* Check for PID change, i.e. whether we run as child after a fork.
+ * If yes, we setup interactive control for the new process
+ */
+ currentPID =3D VG_(getpid)();
+ if (thisPID !=3D currentPID) {
+ thisPID =3D currentPID;
+ setup_control();
+ }
=20
- /* toggle between 2 command files, with/without ".pid" postfix */
- current_command_file =3D (current_command_file =3D=3D command_file2)=
?=20
- command_file : command_file2;
- current_result_file =3D (current_command_file =3D=3D command_file2)=
?
- result_file2 : result_file; =20
+ /* Toggle between 2 command files, with/without ".pid" postfix
+ * (needed for compatibility with KCachegrind, which wants to trigge=
r
+ * a dump by writing into a command file without the ".pid" postfix=
)
+ */
+ check_counter++;
+ if (check_counter % 2) {
+ current_command_file =3D command_file;
+ current_result_file =3D result_file;
+ }
+ else {
+ current_command_file =3D command_file2;
+ current_result_file =3D result_file2;
+ }
=20
res =3D VG_(open)(current_command_file, VKI_O_RDONLY,0);
if (!res.isError) {
Modified: trunk/callgrind/dump.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/callgrind/dump.c 2006-09-19 13:29:36 UTC (rev 6081)
+++ trunk/callgrind/dump.c 2006-09-20 21:29:39 UTC (rev 6082)
@@ -32,15 +32,13 @@
#include <pub_tool_threadstate.h>
#include <pub_tool_libcfile.h>
=20
-/*------------------------------------------------------------*/
-/*--- Support for signal handlers and multi-threading ---*/
-/*------------------------------------------------------------*/
=20
/* Dump Part Counter */
static Int out_counter =3D 0;
=20
static Char* dump_file_base =3D 0;
static Char* base_directory =3D 0;
+static Bool dumps_initialized =3D False;
=20
/* Command */
static Char cmdbuf[BUF_LEN];
@@ -66,9 +64,16 @@
=20
Char* CLG_(get_dump_file_base)()
{
- return dump_file_base;
+ CLG_ASSERT(dumps_initialized);
+ return dump_file_base;
}
=20
+Char* CLG_(get_base_directory)()
+{
+ CLG_ASSERT(dumps_initialized);
+ return base_directory;
+}
+
/*------------------------------------------------------------*/
/*--- Output file related stuff ---*/
/*------------------------------------------------------------*/
@@ -1264,6 +1269,7 @@
FullCost sum =3D 0;
SysRes res;
=20
+ CLG_ASSERT(dumps_initialized);
CLG_ASSERT(filename !=3D 0);
=20
if (!CLG_(clo).combine_dumps) {
@@ -1641,10 +1647,20 @@
cmdbuf[size] =3D 0;
}
=20
-void CLG_(init_files)(Char** dir, Char** file)
+/*
+ * Set up file names for dump output: base_directory, dump_file_base
+ * The final filename of a dump is constructed at dump time from
+ * the PID, thread ID and dump counter.
+ *
+ * These always will contain a full absolute path.
+ * If no prefix is given (via option "--base=3D<prefix>"), the current
+ * working directory at program start is used, otherwise <prefix> can
+ * be relative to cwd or absolute.
+ */
+void CLG_(init_dumps)()
{
- Int size;
- SysRes res;
+ Int size;
+ SysRes res;
=20
if (!CLG_(clo).filename_base)
CLG_(clo).filename_base =3D DEFAULT_DUMPNAME;
@@ -1709,8 +1725,7 @@
}
if (!res.isError) VG_(close)( (Int)res.val );
=20
- *dir =3D base_directory;
- *file =3D filename;
+ init_cmdbuf();
=20
- init_cmdbuf();
+ dumps_initialized =3D True;
}
Modified: trunk/callgrind/global.h
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/callgrind/global.h 2006-09-19 13:29:36 UTC (rev 6081)
+++ trunk/callgrind/global.h 2006-09-20 21:29:39 UTC (rev 6082)
@@ -675,7 +675,7 @@
void CLG_(fini)(Int exitcode);
=20
/* from command.c */
-void CLG_(init_command)(Char* dir, Char* dumps);
+void CLG_(init_command)(void);
void CLG_(check_command)(void);
void CLG_(finish_command)(void);
=20
@@ -761,10 +761,10 @@
=20
/* from dump.c */
extern FullCost CLG_(total_cost);
-void CLG_(init_files)(Char** dir, Char** file);
+void CLG_(init_dumps)(void);
Char* CLG_(get_dump_file_base)(void);
+Char* CLG_(get_base_directory)(void);
=20
-
/*------------------------------------------------------------*/
/*--- Exported global variables ---*/
/*------------------------------------------------------------*/
Modified: trunk/callgrind/main.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/callgrind/main.c 2006-09-19 13:29:36 UTC (rev 6081)
+++ trunk/callgrind/main.c 2006-09-20 21:29:39 UTC (rev 6082)
@@ -1028,8 +1028,6 @@
static
void CLG_(post_clo_init)(void)
{
- Char *dir =3D 0, *fname =3D 0;
-
VG_(clo_vex_control).iropt_unroll_thresh =3D 0;
VG_(clo_vex_control).guest_chase_thresh =3D 0;
=20
@@ -1042,8 +1040,8 @@
CLG_(clo).dump_line =3D True;
}
=20
- CLG_(init_files)(&dir,&fname);
- CLG_(init_command)(dir,fname);
+ CLG_(init_dumps)();
+ CLG_(init_command)();
=20
(*CLG_(cachesim).post_clo_init)();
=20
|