aboutsummaryrefslogtreecommitdiff
path: root/chattymalloc.c
blob: 301e0f248370f0fd9b19b4737611aca96457d099 (plain)
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <sys/syscall.h>
#include <unistd.h>

static void * (*myfn_malloc)(size_t size);
static void * (*myfn_free)(void* ptr);
static void * (*myfn_calloc)(size_t nmemb, size_t size);
static void * (*myfn_realloc)(void *ptr, size_t size);
static void * (*myfn_memalign)(size_t blocksize, size_t bytes);

static int initializing = 0;

// Memory we give out during initialisation
static char tmpbuff[4096];
static unsigned long tmppos = 0;
static unsigned long tmpallocs = 0;

/* Options that can be set in the CHATTYMALLOC_OPTS environment var.
 *
 * store: store call data in memory or dump it with each allocator call */
static bool store = true;
/* sigusr1: dump in memory log in SIGUSR1 handler */
static bool sigusr1 = false;
/* timestamp: get timestamp of each allocator call */
static bool timestamp = false;
/* timestamp: get timestamp of each allocator call */
static char* path = "chattymalloc.data";

void parse_options()
{
	char* options = getenv("CHATTYMALLOC_OPTS");
	if (!options)
		return;

	char* opt = options;
	char* val;
	char c;

	for (int i = 0;; i++)
	{
		c = options[i];
		if (c == ':' || c == '\0')
		{
			if (strncmp(opt, "store", 5) == 0)
			{
				if (strncmp(val, "false", 5) == 0)
					store = false;
			}
			else if (strncmp(opt, "sigusr1", 7) == 0)
			{
				if (strncmp(val, "true", 4) == 0)
					sigusr1 = true;
			}
			else if (strncmp(opt, "timestamp", 9) == 0)
			{
				if (strncmp(val, "true", 4) == 0)
					timestamp = true;
			}
			else
				fprintf(stderr, "Unknown option \n");

			if (c == '\0')
				return;

			opt = options + i + 1;
		}
		else if (c == '=')
		{
			val = options + i + 1;
		}
	}
}

typedef enum { MALLOC, FREE, CALLOC, REALLOC, MEMALIGN } Func;

// Entry in our in memory log
typedef struct Log_entry
{
	time_t time;
	Func function;
	uintptr_t args[3];
} Log_entry;

// Chunk of thread local log entries
typedef struct Log_chunk
{
	struct Log_chunk* next;
	pid_t tid;
	size_t n;
	Log_entry entries[10000];
} Log_chunk;

// List of chunks
static struct Log_chunk* log_start = NULL;
static struct Log_chunk* log_end = NULL;

static __thread pid_t tid;
static __thread Log_chunk* cur_log;

// Flag to prevent recursion
static __thread int prevent_recursion = 0;

static void (*old_signalhandler)(int);

FILE* out;

#ifdef SYS_gettid
static pid_t gettid()
{
	if (!tid)
		tid = syscall(SYS_gettid);
	return tid;
}
#else
#error "SYS_gettid unavailable on this system"
#endif

static void open_output()
{
	out = fopen(path, "w");
	if (!out)
	{
		perror("failed to open output");
		exit(1);
	}
}

static void new_log_chunk (void)
{
	cur_log = myfn_malloc(sizeof(Log_chunk));
	if (!cur_log)
	{
		perror("can't malloc chunk");
		exit(1);
	}

	cur_log->tid = gettid();
	cur_log->n = 0;
	cur_log->next = NULL;

chain: ;
	Log_chunk* old_end = log_end;
	if (!__sync_bool_compare_and_swap(&log_end, old_end, cur_log))
		goto chain;

	if (old_end)
		old_end->next = cur_log;
}

static void write_log (void)
{
	Log_chunk* chunk = log_start;

	prevent_recursion = 1;

	open_output();

	while (chunk)
	{
		for (size_t i = 0; i < chunk->n; i++)
		{
			Log_entry entry = chunk->entries[i];
			fprintf(out, "%lu %d ", entry.time, chunk->tid);
			switch (entry.function)
			{
			case MALLOC:
				fprintf(out, "ma %lu %p\n", entry.args[0], entry.args[1]);
				break;
			case FREE:
				fprintf(out, "f %p\n", entry.args[0]);
				break;
			case CALLOC:
				fprintf(out, "c %lu %lu %p\n", entry.args[0], entry.args[1], entry.args[2]);
				break;
			case REALLOC:
				fprintf(out, "r %p %lu %p\n", entry.args[0], entry.args[1], entry.args[2]);
				break;
			case MEMALIGN:
				fprintf(out, "mm %lu %lu %p\n", entry.args[0], entry.args[1], entry.args[2]);
				break;
			}
		}
		chunk = chunk->next;
	}

	fclose(out);
}

static void sigusr1_handler
(int __attribute__((__unused__)) sig)
{
	write_log();
	pid_t ppid = getppid();
	kill(ppid, SIGUSR1);

	// we are done writing the log
	prevent_recursion = 0;
}

static void init (void)
{
	initializing = 1;

	myfn_malloc     = dlsym(RTLD_NEXT, "malloc");
	myfn_free       = dlsym(RTLD_NEXT, "free");
	myfn_calloc     = dlsym(RTLD_NEXT, "calloc");
	myfn_realloc    = dlsym(RTLD_NEXT, "realloc");
	myfn_memalign   = dlsym(RTLD_NEXT, "memalign");

	if (!myfn_malloc || !myfn_free || !myfn_calloc || !myfn_realloc || !myfn_memalign) 
	{
		fprintf(stderr, "Error in `dlsym`: %s\n", dlerror());
		exit(1);
	}

	initializing = 0;
	// now we can use the real allocator
	prevent_recursion = 1;

	// parse chattymalloc options
	parse_options();

	if (store)
	{
		new_log_chunk();
		log_start = cur_log;
		atexit(write_log);
	}
	else
		open_output();

	if (sigusr1)
	{
		// register USR1 signal handler to dump log
		struct sigaction old;
		struct sigaction sa;

		sigaction(SIGUSR1, NULL, &old);
		old_signalhandler = old.sa_handler;

		memset(&sa, 0, sizeof(sa));
		sa.sa_handler = sigusr1_handler;
		sa.sa_mask = old.sa_mask;

		if (sigaction(SIGUSR1, &sa, NULL) == -1)
		{
			perror("Can't register our SIGUSR1 handler");
			exit(1);
		}
	}

	prevent_recursion = 0;
}

void *malloc (size_t size)
{

	if (myfn_malloc == NULL)
	{
		if (!initializing)
			init();
		else
		{
			if (tmppos + size < sizeof(tmpbuff))
			{
				/* fprintf(stderr, "jcheck: %lu requested during init\n", size); */
				void *retptr = tmpbuff + tmppos;
				tmppos += size;
				++tmpallocs;
				return retptr;
			}
			else
			{
				fprintf(stderr, "jcheck: too much memory requested during initialisation - increase tmpbuff size\n");
				*((int*) NULL) = 1;
				exit(1);
			}
		}
	}

	void *ptr = myfn_malloc(size);

	if (!prevent_recursion)
	{
		struct timespec ts;
		ts.tv_sec = 0;
		ts.tv_nsec = 0;

		if (timestamp)
			clock_gettime(CLOCK_MONOTONIC, &ts);

		if (store)
		{
			if (cur_log == NULL || cur_log->n == 100)
				 new_log_chunk();

			Log_entry* cur_entry = &cur_log->entries[cur_log->n];

			cur_entry->time = ts.tv_nsec;
			cur_entry->function = MALLOC;
			cur_entry->args[0] = (uintptr_t)size;
			cur_entry->args[1] = (uintptr_t)ptr;
			cur_log->n++;
		}
		else
		{
			prevent_recursion = 1;
			fprintf(out, "%lu %d ma %u %p\n", ts.tv_nsec, gettid(), size, ptr);
			prevent_recursion = 0;
		}
	}

	return ptr;
}

void free (void *ptr)
{
	if (myfn_free == NULL)
		init();
	if (!(ptr >= (void*) tmpbuff && ptr <= (void*)(tmpbuff + tmppos)))
	{
		if (!prevent_recursion)
		{
			struct timespec ts;
			ts.tv_sec = 0;
			ts.tv_nsec = 0;

			if (timestamp)
				clock_gettime(CLOCK_MONOTONIC, &ts);

			if (store)
			{
				if (cur_log == NULL || cur_log->n == 100)
					 new_log_chunk();

				Log_entry* cur_entry = &cur_log->entries[cur_log->n];

				cur_entry->time = ts.tv_nsec;
				cur_entry->function = FREE;
				cur_entry->args[0] = (uintptr_t)ptr;
				cur_log->n++;
			}
			else
			{
				prevent_recursion = 1;
				fprintf(out, "%lu %d f %p\n", ts.tv_nsec, gettid(), ptr);
				prevent_recursion = 0;
			}
		}

		myfn_free(ptr);
	}
}

void *realloc (void *ptr, size_t size)
{
	if (myfn_realloc == NULL)
	{
		void *nptr = malloc(size);
		if (nptr && ptr)
		{
			memmove(nptr, ptr, size);
			free(ptr);
		}
		return nptr;
	}

	void *nptr = myfn_realloc(ptr, size);

	if (!prevent_recursion)
	{
		struct timespec ts;
		ts.tv_sec = 0;
		ts.tv_nsec = 0;

		if (timestamp)
			clock_gettime(CLOCK_MONOTONIC, &ts);

		if (store)
		{
			if (cur_log == NULL || cur_log->n == 100)
				 new_log_chunk();

			Log_entry* cur_entry = &cur_log->entries[cur_log->n];

			cur_entry->time = ts.tv_nsec;
			cur_entry->function = REALLOC;
			cur_entry->args[0] = (uintptr_t)ptr;
			cur_entry->args[1] = (uintptr_t)size;
			cur_entry->args[2] = (uintptr_t)nptr;
			cur_log->n++;
		}
		else
		{
			prevent_recursion = 1;
			fprintf(out, "%lu %d r %p %u %p\n", ts.tv_nsec, gettid(), ptr, size, nptr);
			prevent_recursion = 0;
		}
	}

	return nptr;
}

void *calloc (size_t nmemb, size_t size)
{
	if (myfn_calloc == NULL)
	{
		void *ptr = malloc(nmemb*size);
		if (ptr)
			memset(ptr, 0, nmemb*size);
		return ptr;
	}

	void *ptr = myfn_calloc(nmemb, size);

	if (!prevent_recursion)
	{
		struct timespec ts;
		ts.tv_sec = 0;
		ts.tv_nsec = 0;

		if (timestamp)
			clock_gettime(CLOCK_MONOTONIC, &ts);

		if (store)
		{
			if (cur_log == NULL || cur_log->n == 100)
				 new_log_chunk();

			Log_entry* cur_entry = &cur_log->entries[cur_log->n];

			cur_entry->time = ts.tv_nsec;
			cur_entry->function = CALLOC;
			cur_entry->args[0] = (uintptr_t)nmemb;
			cur_entry->args[1] = (uintptr_t)size;
			cur_entry->args[2] = (uintptr_t)ptr;
			cur_log->n++;
		}
		else
		{
			prevent_recursion = 1;
			fprintf(out, "%lu %d c %u %u %p\n", ts.tv_nsec, gettid(), nmemb, size, ptr);
			prevent_recursion = 0;
		}
	}

	return ptr;
}

void *memalign (size_t alignment, size_t size)
{
	// Hopefully this gets never called during init()
	void *ptr = myfn_memalign(alignment, size);

	if (!prevent_recursion)
	{
		struct timespec ts;
		ts.tv_sec = 0;
		ts.tv_nsec = 0;

		if (timestamp)
			clock_gettime(CLOCK_MONOTONIC, &ts);

		if (store)
		{
			if (cur_log == NULL || cur_log->n == 100)
				 new_log_chunk();

			Log_entry* cur_entry = &cur_log->entries[cur_log->n];

			cur_entry->time = ts.tv_nsec;
			cur_entry->function = MEMALIGN;
			cur_entry->args[0] = (uintptr_t)alignment;
			cur_entry->args[1] = (uintptr_t)size;
			cur_entry->args[2] = (uintptr_t)ptr;
			cur_log->n++;
		}
		else
		{
			prevent_recursion = 1;
			fprintf(out, "%lu %d mm %u %u %p\n", ts.tv_nsec, gettid(), alignment, size, ptr);
			prevent_recursion = 0;
		}
	}

	return ptr;
}