Bug 1274041 - Make child process write its memory map to a different file name. r?jesup
This makes the child process write its memory map to a different file
name, just like it does for the jprof log. It tries to share the
variables between the two so that they're both connected in the code and
consistent with each other.
Note that I haven't yet written the patch to make jprof.cpp *read* the
map from that file, so this currently requires manually renaming the
generated map with the numeric suffix to jprof-map in order to run
jprof. I should probably write that patch eventually, but I haven't
actually needed to.
This at least prevents the child process's map file from being
overwritten by the parent's a fraction of a second later.
MozReview-Commit-ID: 7L4wT9BdQYI
--- a/tools/jprof/stub/libmalloc.cpp
+++ b/tools/jprof/stub/libmalloc.cpp
@@ -57,16 +57,19 @@ extern r_debug _r_debug;
#define USE_GLIBC_BACKTRACE 1
// To debug, use #define JPROF_STATIC
#define JPROF_STATIC static
static int gLogFD = -1;
static pthread_t main_thread;
+static bool gIsSlave = false;
+static int gFilenamePID;
+
static void startSignalCounter(unsigned long millisec);
static int enableRTCSignals(bool enable);
//----------------------------------------------------------------------
// replace use of atexit()
static void DumpAddressMap();
@@ -161,17 +164,23 @@ static void DumpAddressMap()
if (rtcHz) {
enableRTCSignals(false);
} else
#endif
{
startSignalCounter(0);
}
- int mfd = open(M_MAPFILE, O_CREAT|O_WRONLY|O_TRUNC, 0666);
+ char filename[2048];
+ if (gIsSlave)
+ snprintf(filename, sizeof(filename), "%s-%d", M_MAPFILE, gFilenamePID);
+ else
+ snprintf(filename, sizeof(filename), "%s", M_MAPFILE);
+
+ int mfd = open(filename, O_CREAT|O_WRONLY|O_TRUNC, 0666);
if (mfd >= 0) {
malloc_map_entry mme;
link_map* map = _r_debug.r_map;
while (nullptr != map) {
if (map->l_name && *map->l_name) {
mme.nameLen = strlen(map->l_name);
mme.address = map->l_addr;
write(mfd, &mme, sizeof(mme));
@@ -671,20 +680,21 @@ NS_EXPORT_(void) setupProfilingStuff(voi
if (f)
f = f + strlen("JP_FILENAME=");
else
f = M_LOGFILE;
char *is_slave = getenv("JPROF_SLAVE");
if (!is_slave)
setenv("JPROF_SLAVE","", 0);
+ gIsSlave = !!is_slave;
- int pid = syscall(SYS_gettid); //gettid();
+ gFilenamePID = syscall(SYS_gettid); //gettid();
if (is_slave)
- snprintf(filename,sizeof(filename),"%s-%d",f,pid);
+ snprintf(filename,sizeof(filename),"%s-%d",f,gFilenamePID);
else
snprintf(filename,sizeof(filename),"%s",f);
// XXX FIX! inherit current capture state!
}
if(!doNotStart) {