3 * by Wolfram Gloger 1995.
6 * Copyright (C) 2002-2007, The Perl Foundation.
17 # include <asm/signal.h>
20 #include "malloc-trace.h"
22 #define ACTION_BUF_SIZE 1024
23 #define TIMESTAMP_FREQ 50
24 #define LOG_NAME "/tmp/mtrace"
26 static void malloc_trace_destructor(void);
28 static ACTION buffer
[ACTION_BUF_SIZE
+1];
29 static int buffer_i
= 0;
31 static int tracing
= 1;
33 static char *initial_brk
= 0;
58 RT#48260: Not yet documented!!!
68 const unsigned version
= MTRACE_VERSION
;
70 char cmdline
[128], *s
;
77 sprintf(name
, "/proc/%d/cmdline", pid
);
78 fd2
= open(name
, O_RDONLY
);
79 if (fd2
>=0 && (count
= read(fd2
, cmdline
, 127)) > 0) {
82 for (s
=cmdline
; *s
++;); s
--;
83 while (--s
>cmdline
&& *s
!='/');
85 sprintf(name
, LOG_NAME
".%.12s.%d", s
, pid
);
88 sprintf(name
, LOG_NAME
".%d", pid
);
91 sprintf(name
, LOG_NAME
".%d", pid
);
93 fd
= open(name
, O_WRONLY
|O_CREAT
, 0600);
95 write(fd
, &version
, sizeof (version
));
96 write(fd
, &initial_brk
, sizeof (initial_brk
));
104 malloc_trace_destructor(void)>
106 RT#48260: Not yet documented!!!
113 malloc_trace_destructor(void)
117 /* produce final timestamp */
118 gettimeofday(&t
, NULL
);
119 buffer
[buffer_i
].code
= CODE_TIMESTAMP
;
120 buffer
[buffer_i
].size
= t
.tv_sec
;
121 buffer
[buffer_i
].ptr
= (void *)t
.tv_usec
;
122 buffer
[buffer_i
].ptr2
= NULL
;
124 if (fd
< 0) open_log_file();
125 if (getpid() != pid
) { /* Oops, must have forked... */
126 if (fd
>= 0) close(fd
);
129 if (fd
>= 0) write(fd
, buffer
, buffer_i
*sizeof (ACTION
));
130 write(2, "<end malloc trace>\n", 19);
131 if (fd
>= 0) close(fd
);
139 malloc_segv_handler(int i, struct sigcontext_struct sc)>
141 RT#48260: Not yet documented!!!
148 malloc_segv_handler(int i
, struct sigcontext_struct sc
)
151 fprintf(stderr
, "malloc-trace: caught SEGV signal.\n");
152 fprintf(stderr
, "sc.cr2 = %8lx\n", (unsigned long)sc
.cr2
);
153 malloc_trace_destructor();
161 malloc_record(int code, size_t size, void *ptr, void *ptr2)>
163 RT#48260: Not yet documented!!!
170 malloc_record(int code
, size_t size
, void *ptr
, void *ptr2
)
172 static long count
= 0;
175 if (!tracing
) return;
177 if (count
== 0) signal(SIGSEGV
, (void (*)(int))malloc_segv_handler
);
179 if ((count
++ % TIMESTAMP_FREQ
) == 0) {
180 gettimeofday(&t
, NULL
);
181 buffer
[buffer_i
].code
= CODE_TIMESTAMP
;
182 buffer
[buffer_i
].size
= t
.tv_sec
;
183 buffer
[buffer_i
].ptr
= (void *)t
.tv_usec
;
184 buffer
[buffer_i
].ptr2
= NULL
;
187 buffer
[buffer_i
].code
= code
;
188 buffer
[buffer_i
].size
= size
;
189 buffer
[buffer_i
].ptr
= ptr
;
190 buffer
[buffer_i
].ptr2
= ptr2
;
191 if (++buffer_i
>= ACTION_BUF_SIZE
) {
192 if (fd
< 0) open_log_file();
193 if (getpid() != pid
) { /* Oops, must have forked... */
197 if (fd
>= 0) write(fd
, buffer
, buffer_i
*sizeof (ACTION
));
202 void* _real_malloc(size_t bytes
);
206 =item C<void* malloc(size_t bytes)>
208 RT#48260: Not yet documented!!!
214 void* malloc(size_t bytes
)
218 if (initial_brk
== 0) { /* Must be the first time. */
219 initial_brk
= sbrk(0);
220 atexit(malloc_trace_destructor
);
222 ptr
= _real_malloc(bytes
);
223 malloc_record(CODE_MALLOC
, bytes
, ptr
, 0);
226 #define malloc _real_malloc
228 void _real_free(void* mem
);
232 =item C<void free(void* mem)>
234 RT#48260: Not yet documented!!!
242 malloc_record(CODE_FREE
, 0, mem
, 0);
245 #define free _real_free
247 void* _real_realloc(void* mem
, size_t bytes
);
251 =item C<void* realloc(void* mem, size_t bytes)>
253 RT#48260: Not yet documented!!!
259 void* realloc(void* mem
, size_t bytes
)
263 ptr
= _real_realloc(mem
, bytes
);
264 malloc_record(CODE_REALLOC
, bytes
, mem
, ptr
);
267 #define realloc _real_realloc
269 void* _real_memalign(size_t alignment
, size_t bytes
);
273 =item C<void* memalign(size_t alignment, size_t bytes)>
275 RT#48260: Not yet documented!!!
281 void* memalign(size_t alignment
, size_t bytes
)
285 if (initial_brk
== 0) { /* Must be the first time. */
286 initial_brk
= sbrk(0);
287 atexit(malloc_trace_destructor
);
289 ptr
= _real_memalign(alignment
, bytes
);
290 malloc_record(CODE_MEMALIGN
, bytes
, ptr
, (void*)alignment
);
293 #define memalign _real_memalign
295 void* _real_calloc(size_t n
, size_t elem_size
);
299 =item C<void* calloc(size_t n, size_t elem_size)>
301 RT#48260: Not yet documented!!!
307 void* calloc(size_t n
, size_t elem_size
)
311 if (initial_brk
== 0) { /* Must be the first time. */
312 initial_brk
= sbrk(0);
313 atexit(malloc_trace_destructor
);
315 ptr
= _real_calloc(n
, elem_size
);
316 malloc_record(CODE_CALLOC
, n
*elem_size
, ptr
, 0);
319 #define calloc _real_calloc
321 void _real_cfree(void *mem
);
325 =item C<void cfree(void *mem)>
327 RT#48260: Not yet documented!!!
333 void cfree(void *mem
)
335 malloc_record(CODE_CFREE
, 0, mem
, 0);
338 #define cfree _real_cfree
352 * c-file-style: "parrot"
354 * vim: expandtab shiftwidth=4: