aboutsummaryrefslogtreecommitdiff
path: root/malloc/mtrace-ctl.c
diff options
context:
space:
mode:
authorDJ Delorie <dj@delorie.com>2016-07-11 20:50:34 -0400
committerDJ Delorie <dj@delorie.com>2016-07-12 22:00:44 -0400
commitb856f645a2f58a3e224976167b4b1fb030d7967b (patch)
treec15f3ce4b4d8b2f99babaae0481c601786eb452a /malloc/mtrace-ctl.c
parente4cff5fd83963c426e3015d3595dfc69a63f1aca (diff)
downloadglibc-b856f645a2f58a3e224976167b4b1fb030d7967b.tar
glibc-b856f645a2f58a3e224976167b4b1fb030d7967b.tar.gz
glibc-b856f645a2f58a3e224976167b4b1fb030d7967b.tar.bz2
glibc-b856f645a2f58a3e224976167b4b1fb030d7967b.zip
Update to new binary file-based trace file.
In order to not lose records, or need to guess ahead of time how many records you need, this switches to a mmap'd file for the trace buffer, and grows it as needed. The trace2dat perl script is replaced with a trace2wl C++ program that runs a lot faster and can handle the binary format.
Diffstat (limited to 'malloc/mtrace-ctl.c')
-rw-r--r--malloc/mtrace-ctl.c101
1 files changed, 82 insertions, 19 deletions
diff --git a/malloc/mtrace-ctl.c b/malloc/mtrace-ctl.c
index e1446ac1df..31adb46928 100644
--- a/malloc/mtrace-ctl.c
+++ b/malloc/mtrace-ctl.c
@@ -4,6 +4,8 @@
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
+#include <fcntl.h>
+#include <errno.h>
/* This module is a stand-alone control program for malloc's internal
trace buffer. It is intended to be preloaded like this:
@@ -30,37 +32,101 @@
#include "mtrace.h"
+#define estr(str) write (2, str, strlen (str))
+
+#if 0
static void
err(const char *str)
{
- write (2, str, strlen(str));
- write (2, "\n", 1);
- exit(1);
+ estr (str);
+ estr ("\n");
}
+#endif
-void __attribute__((constructor))
-djmain(void)
+/*
+ * mtrace_start - checks for buffer, allocates one if needed, starts trace.
+ * mtrace_stop - stops tracing
+ * mtrace_sync - syncs the buffer
+ * mtrace_reset - resets buffer state to intial state
+ */
+
+struct _malloc_trace_buffer_s *mtrace_buffer = NULL;
+size_t mtrace_buffer_bytesize = 0;
+
+int
+mtrace_start (void)
{
const char *e;
- size_t sz;
+ char *fname;
+ int sequence = 0;
e = getenv("MTRACE_CTL_COUNT");
if (!e)
e = "1000";
- sz = (size_t) atol(e) * sizeof(struct __malloc_trace_buffer_s);
- char *buf = mmap (NULL, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
- if (buf == NULL || buf == (char *)(-1))
- err("Cannot mmap");
+ e = getenv("MTRACE_CTL_FILE");
+ if (!e)
+ e = "/tmp/mtrace.out";
+
+ fname = alloca (strlen(e) + 30);
+ sprintf(fname, "%s.%d", e, getpid());
+ while (access (fname, F_OK) == 0)
+ {
+ sequence ++;
+ sprintf(fname, "%s.%d.%d", e, getpid(), sequence);
+ }
+
+ estr ("mtrace-ctl: writing to ");
+ estr (fname);
+ estr ("\n");
- buf[0] = 1;
- buf[sz-1] = 1;
+ __malloc_trace_init (fname);
+ return 0;
+}
- /* This must be the last thing we do. */
- __malloc_set_trace_buffer ((void *)buf, sz);
- return;
+void
+mtrace_stop (void)
+{
+ size_t count;
+ char line[100];
+
+ count = __malloc_trace_stop ();
+ sprintf (line, "mtrace-ctl: %lld entries recorded\n", (long long)count);
+ estr (line);
}
+void
+mtrace_sync (void)
+{
+ __malloc_trace_sync ();
+ // __malloc_trace_buffer_ptr buf = __malloc_get_trace_buffer (&size, &head);
+ // msync (buf, size * sizeof(struct __malloc_trace_buffer_s), MS_SYNC | MS_INVALIDATE);
+}
+
+void
+mtrace_reset (void)
+{
+
+ __malloc_trace_stop ();
+ mtrace_start ();
+}
+
+void __attribute__((constructor))
+mtrace_ctor(void)
+{
+ if (mtrace_start ())
+ exit (1);
+}
+
+void __attribute__((destructor))
+mtrace_dtor(void)
+{
+ mtrace_stop ();
+ mtrace_sync ();
+}
+
+#if 0
+
const char * const typenames[] = {
"unused ",
"malloc ",
@@ -79,10 +145,6 @@ djend(void)
FILE *outf;
size_t head, size, i;
- /* Prevent problems with recursion etc by shutting off trace right away. */
- __malloc_trace_buffer_ptr buf = __malloc_get_trace_buffer (&size, &head);
- __malloc_set_trace_buffer (NULL, 0);
-
e = getenv("MTRACE_CTL_FILE");
if (!e)
{
@@ -130,3 +192,4 @@ djend(void)
munmap (buf, size * sizeof(struct __malloc_trace_buffer_s));
return;
}
+#endif