aboutsummaryrefslogtreecommitdiff
path: root/print_status_on_exit.c
blob: 23df6bc922a48a996d4fb286363633a8462153fc (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
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

char tmpbuff[1024];
unsigned long tmppos = 0;
unsigned long tmpallocs = 0;

/*=========================================================
 *  * interception points
 *  */

static void * (*myfn_malloc)(size_t size);
static void   (*myfn_free)(void *ptr);

static void print_status(void)
{
	char buf[4096];

	FILE* status = fopen("/proc/self/status", "r");
	if (status == NULL)
	{
		perror("fopen status");
		exit(1);
	}

	FILE* output = fopen("status", "a");
	if (output == NULL)
	{
		perror("fopen output file");
		exit(1);
	}

	while (!feof(status))
	{
		fgets(&buf, 4096, status);
		fprintf(output, "%s", buf);
	}
	fclose(status);
}

static void init()
{
	myfn_malloc     = dlsym(RTLD_NEXT, "malloc");
	myfn_free       = dlsym(RTLD_NEXT, "free");

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

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

void free(void *ptr)
{
	// something wrong if we call free before one of the allocators!
	if (myfn_malloc == NULL)
		init();
	if (!(ptr >= (void*) tmpbuff && ptr <= (void*)(tmpbuff + tmppos)))
		myfn_free(ptr);
}