-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmm.c
79 lines (61 loc) · 2.03 KB
/
dmm.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
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "hooks.h"
#include "config.h"
#define FNAME_MAX_LEN 256
static char *dumps_location;
static pid_t pid;
static int dump_seq = 0;
static void dmm_make_snapshot(int sig) {
char fn[FNAME_MAX_LEN];
FILE *fd;
int save_error = 0;
snprintf(fn, FNAME_MAX_LEN, "%s/dmm_%d_%04d.jdmm", dumps_location, pid, dump_seq);
printf("Saving allocation snapshot to: %s\n", fn);
fd = fopen(fn, "w");
if (fd == NULL) {
printf("ERROR: failed to open snapshot file: %s\n", strerror(errno));
return;
}
if (stdlib_snapshot(fd) != 0) {
printf("Fail to save STDLIB allocations snapshot: %s\n", strerror(errno));
save_error = 1;
}
if (fclose(fd) != 0) {
printf("Fail to close file: %s\n", strerror(errno));
}
if (!save_error) {
printf("Allocation snapshot succesfully saved to: %s\n", fn);
} else {
printf("Allocation snapshot NOT saved\n");
}
dump_seq++;
}
static void __attribute__((constructor)) dmm_construct(void) {
printf("DMM initializing...\n");
//TODO: redesign if glib support will be addded.
printf("If you are debugging glib based application please export G_SLICE=always-malloc\n");
dumps_location = getenv("DMM_DUMP_LOCATION");
if (dumps_location == NULL) {
dumps_location = DMM_DEFAULT_DUMP_LOCATION;
}
pid = getpid();
//TODO: add check if SIGRTMIN + 5 exceeds SIGRTMAX
if (signal(DMM_DUMP_SIGNALNO, dmm_make_snapshot) == SIG_ERR) {
printf ("WARNING: fail to registers signal handler, only resulting dump will be generated\n");
} else {
printf("To generate allocation snapshot please execute: kill -%d %d\n", DMM_DUMP_SIGNALNO, pid);
}
stdlib_init();
printf("DMM initialized!\n");
}
static void __attribute__((destructor)) dmm_destruct(void) {
printf("DMM exiting...\n");
dmm_make_snapshot(0);
stdlib_release();
}