/
malloc-trace.c
355 lines (260 loc) · 6.05 KB
/
malloc-trace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* malloc-trace.c
*
* by Wolfram Gloger 1995.
*
* $Id$
* Copyright (C) 2002-2007, The Perl Foundation.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/time.h>
#ifdef __linux__
# include <signal.h>
# include <asm/signal.h>
#endif
#include "malloc-trace.h"
#define ACTION_BUF_SIZE 1024
#define TIMESTAMP_FREQ 50
#define LOG_NAME "/tmp/mtrace"
static void malloc_trace_destructor(void);
static ACTION buffer[ACTION_BUF_SIZE+1];
static int buffer_i = 0;
static int fd = -1;
static int tracing = 1;
static int pid = 0;
static char *initial_brk = 0;
/*
=head1 NAME
src/malloc-trace.c
=head1 DESCRIPTION
RT#48264
=head2 Functions
=over 4
=cut
*/
/*
=item C<static void
open_log_file()>
RT#48260: Not yet documented!!!
=cut
*/
static void
open_log_file()
{
char name[128];
const unsigned version = MTRACE_VERSION;
#ifdef __linux__
char cmdline[128], *s;
int count, fd2;
#endif
tracing = 0;
pid = getpid();
#ifdef __linux__
sprintf(name, "/proc/%d/cmdline", pid);
fd2 = open(name, O_RDONLY);
if (fd2>=0 && (count = read(fd2, cmdline, 127)) > 0) {
close(fd2);
cmdline[count] = 0;
for (s=cmdline; *s++;); s--;
while (--s>cmdline && *s!='/');
if (*s == '/') s++;
sprintf(name, LOG_NAME ".%.12s.%d", s, pid);
}
else {
sprintf(name, LOG_NAME ".%d", pid);
}
#else
sprintf(name, LOG_NAME ".%d", pid);
#endif
fd = open(name, O_WRONLY|O_CREAT, 0600);
if (fd >= 0) {
write(fd, &version, sizeof (version));
write(fd, &initial_brk, sizeof (initial_brk));
}
tracing = 1;
}
/*
=item C<static void
malloc_trace_destructor(void)>
RT#48260: Not yet documented!!!
=cut
*/
static void
malloc_trace_destructor(void)
{
struct timeval t;
/* produce final timestamp */
gettimeofday(&t, NULL);
buffer[buffer_i].code = CODE_TIMESTAMP;
buffer[buffer_i].size = t.tv_sec;
buffer[buffer_i].ptr = (void *)t.tv_usec;
buffer[buffer_i].ptr2 = NULL;
buffer_i++;
if (fd < 0) open_log_file();
if (getpid() != pid) { /* Oops, must have forked... */
if (fd >= 0) close(fd);
return;
}
if (fd >= 0) write(fd, buffer, buffer_i*sizeof (ACTION));
write(2, "<end malloc trace>\n", 19);
if (fd >= 0) close(fd);
}
#ifdef __linux__
/*
=item C<static void
malloc_segv_handler(int i, struct sigcontext_struct sc)>
RT#48260: Not yet documented!!!
=cut
*/
static void
malloc_segv_handler(int i, struct sigcontext_struct sc)
{
tracing = 0;
fprintf(stderr, "malloc-trace: caught SEGV signal.\n");
fprintf(stderr, "sc.cr2 = %8lx\n", (unsigned long)sc.cr2);
malloc_trace_destructor();
}
#endif
/*
=item C<static void
malloc_record(int code, size_t size, void *ptr, void *ptr2)>
RT#48260: Not yet documented!!!
=cut
*/
static void
malloc_record(int code, size_t size, void *ptr, void *ptr2)
{
static long count = 0;
struct timeval t;
if (!tracing) return;
#ifdef __linux__
if (count == 0) signal(SIGSEGV, (void (*)(int))malloc_segv_handler);
#endif
if ((count++ % TIMESTAMP_FREQ) == 0) {
gettimeofday(&t, NULL);
buffer[buffer_i].code = CODE_TIMESTAMP;
buffer[buffer_i].size = t.tv_sec;
buffer[buffer_i].ptr = (void *)t.tv_usec;
buffer[buffer_i].ptr2 = NULL;
buffer_i++;
}
buffer[buffer_i].code = code;
buffer[buffer_i].size = size;
buffer[buffer_i].ptr = ptr;
buffer[buffer_i].ptr2 = ptr2;
if (++buffer_i >= ACTION_BUF_SIZE) {
if (fd < 0) open_log_file();
if (getpid() != pid) { /* Oops, must have forked... */
tracing = 0;
return;
}
if (fd >= 0) write(fd, buffer, buffer_i*sizeof (ACTION));
buffer_i = 0;
}
}
void* _real_malloc(size_t bytes);
/*
=item C<void* malloc(size_t bytes)>
RT#48260: Not yet documented!!!
=cut
*/
void* malloc(size_t bytes)
{
void *ptr;
if (initial_brk == 0) { /* Must be the first time. */
initial_brk = sbrk(0);
atexit(malloc_trace_destructor);
}
ptr = _real_malloc(bytes);
malloc_record(CODE_MALLOC, bytes, ptr, 0);
return ptr;
}
#define malloc _real_malloc
void _real_free(void* mem);
/*
=item C<void free(void* mem)>
RT#48260: Not yet documented!!!
=cut
*/
void free(void* mem)
{
malloc_record(CODE_FREE, 0, mem, 0);
_real_free(mem);
}
#define free _real_free
void* _real_realloc(void* mem, size_t bytes);
/*
=item C<void* realloc(void* mem, size_t bytes)>
RT#48260: Not yet documented!!!
=cut
*/
void* realloc(void* mem, size_t bytes)
{
void *ptr;
ptr = _real_realloc(mem, bytes);
malloc_record(CODE_REALLOC, bytes, mem, ptr);
return ptr;
}
#define realloc _real_realloc
void* _real_memalign(size_t alignment, size_t bytes);
/*
=item C<void* memalign(size_t alignment, size_t bytes)>
RT#48260: Not yet documented!!!
=cut
*/
void* memalign(size_t alignment, size_t bytes)
{
void *ptr;
if (initial_brk == 0) { /* Must be the first time. */
initial_brk = sbrk(0);
atexit(malloc_trace_destructor);
}
ptr = _real_memalign(alignment, bytes);
malloc_record(CODE_MEMALIGN, bytes, ptr, (void*)alignment);
return ptr;
}
#define memalign _real_memalign
void* _real_calloc(size_t n, size_t elem_size);
/*
=item C<void* calloc(size_t n, size_t elem_size)>
RT#48260: Not yet documented!!!
=cut
*/
void* calloc(size_t n, size_t elem_size)
{
void *ptr;
if (initial_brk == 0) { /* Must be the first time. */
initial_brk = sbrk(0);
atexit(malloc_trace_destructor);
}
ptr = _real_calloc(n, elem_size);
malloc_record(CODE_CALLOC, n*elem_size, ptr, 0);
return ptr;
}
#define calloc _real_calloc
void _real_cfree(void *mem);
/*
=item C<void cfree(void *mem)>
RT#48260: Not yet documented!!!
=cut
*/
void cfree(void *mem)
{
malloc_record(CODE_CFREE, 0, mem, 0);
_real_cfree(mem);
}
#define cfree _real_cfree
#include "malloc.c"
/*
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/