-
Notifications
You must be signed in to change notification settings - Fork 6
/
mx.c
259 lines (238 loc) · 5.69 KB
/
mx.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
/* malloc tracer for memory leak tracking
* This fragment actually prints the leak report
* -- Howard Chu, [email protected] 2015-03-24
*/
#define IFDECO if (options & MD_NO_DECORATION)
#define IFNDECO if (!(options & MD_NO_DECORATION))
#define MD_TTL_MEM "Memory blocks:\n"
#define MD_TTL_MEM2 "Memory leaks (%ld total):\n", md_nb_mem_used
void md_display_stack(int nb_stack, MD_Loc *stack, int options)
{
int l;
if (nb_stack == 0)
return;
IFDECO
printf("stack ");
for(l=0; l<nb_stack; l++)
{
if (stack[l].addr == NULL)
{
IFDECO
printf("\"*\" ");
}
else
{
IFDECO
{
if (options & MD_MEMORY_LINE)
printf("\"%s\" \"%s:%d\" \"%s\" ", MD_DNAME(stack[l].name), MD_DFILE(stack[l].file),
stack[l].line, MD_DOBJ(stack[l].object));
printf("\"%s\" ", MD_DNAME(stack[l].name));
}
else
{
if (!l)
printf(" stack: ");
if (options & MD_MEMORY_LINE)
{
if (l)
printf(" ");
printf("%20s : %s (%s:%d)\n", MD_DOBJ(stack[l].object),
MD_DNAME(stack[l].name), MD_DFILE(stack[l].file), stack[l].line);
}else
{
if (l)
printf(" <- ");
printf("%s", MD_DNAME(stack[l].name));
if (l+1 == nb_stack || stack[l+1].addr == NULL)
printf("\n");
}
}
}
}
IFDECO
printf("\n");
}
/* sub-functions used by memory_leaks and memory_blocks */
void md_display_invalid_block(MD_Mem *blk, int options)
{
if (blk->anext && blk->anext->where_f)
{
IFDECO
{
printf("still %p \"%s\" \"%s:%d\" \"%s\" \"%s:%d\"\n",
blk->ptr, MD_DNAME(blk->func_f),
MD_DFILE(blk->file_f), blk->line_f,
MD_DNAME(blk->anext->func_f),
MD_DFILE(blk->anext->file_f),
blk->anext->line_f);
}
else
printf("Manipulation of address %p at %s (%s:%d),"
" which was freed at %s (%s:%d)\n",
blk->ptr, MD_DNAME(blk->func_f),
MD_DFILE(blk->file_f), blk->line_f,
MD_DNAME(blk->anext->func_f),
MD_DFILE(blk->anext->file_f),
blk->anext->line_f);
}
else
if (blk->func_a == NULL)
{/* invalid free */
IFDECO
printf("free %p \"%s\" \"%s:%d\"\n",
blk->ptr, MD_DNAME(blk->func_f),
MD_DFILE( blk->file_f), blk->line_f);
else
printf(" unreferenced address (%p) for 'free' at %s (%s:%d)\n",
blk->ptr, MD_DNAME(blk->func_f),
MD_DFILE( blk->file_f), blk->line_f);
}
else
{/* invalid realloc */
IFDECO
printf("realloc %p \"%s\" \"%s:%d\"\n",
blk->ptr, MD_DNAME(blk->func_f),
MD_DFILE(blk->file_f), blk->line_f);
else
printf(" unreferenced address (%p) for 'realloc' at %s (%s:%d)\n",
blk->ptr, MD_DNAME(blk->func_f),
MD_DFILE(blk->file_f), blk->line_f);
}
}
void md_display_valid_leak(MD_Mem *blk, int options)
{
int j;
MD_Mem *m;
for (j=0,m=blk->rnext;m;m=m->rnext,j++);
/* normal behavior */
IFDECO
{
printf("%p %u \"%s\" \"%s:%d\" %d\n",blk->ptr, blk->size_a,
MD_DNAME(blk->func_a), MD_DFILE(blk->file_a), blk->line_a,
j);
}
else
{
printf(" Leak, blocks, size: %p,%d,%-8u ", blk->ptr, blk->block, blk->size_a);
printf(" %s (%s:%d)\n", MD_DNAME(blk->func_a),
MD_DFILE(blk->file_a), blk->line_a);
}
/*call-stack */
md_display_stack(blk->nb_stack_a, blk->stack_a, options);
for (j=0,m=blk->rnext;m;m=m->rnext,j++)
{
IFDECO
{
printf("%p %u \"%s\" \"%s:%d\"\n", m->ptr, m->size_a,
MD_DNAME(m->func_a),MD_DFILE(m->file_a),m->line_a);
}
else
{
printf(" realloc(%3d): %p,%-8u ", j+1, m->ptr, m->size_a);
printf(" %s (%s:%d)\n",
MD_DNAME(m->func_a),MD_DFILE(m->file_a),m->line_a);
}
/* new: call-stack */
md_display_stack(m->nb_stack_a, m->stack_a, options);
}
if (j > 0)
IFDECO
printf("\n");
}
static int md_found_leak;
int md_display_leak1(MD_Mem *me, int options)
{
for (;me;me=me->anext)
{
if ((options & (MD_NO_UNRES_MALLOC|MD_NO_UNRES_REALLOC))&&
(me->where_a) && (!me->valid_a))
continue;
if ((options & MD_NO_UNRES_FREE)&&
(me->where_f) && (!me->valid_f))
continue;
if (me->where_a)
{
if (!me->where_f && !me->rnext)
{
if (!md_found_leak)
{
md_found_leak = 1;
IFNDECO
{
printf("\n");
printf(MD_TTL_MEM2);
printf("\n");
}
}
md_display_valid_leak(me, options);
}
}
else
{
if (!md_found_leak)
{
md_found_leak = 1;
IFNDECO
{
printf("\n");
printf(MD_TTL_MEM2);
printf("\n");
}
}
md_display_invalid_block(me, options);
}
}
return 0;
}
MD_Mem **leaks;
int lcnt;
int md_linearize_leaks(MD_Mem *me, int foo)
{
leaks[lcnt++] = me;
return 0;
}
/* sort in descending order of size, #blocks */
int md_sort_leaks(const void *v1, const void *v2)
{
MD_Mem **p1 = (MD_Mem **)v1, **p2 = (MD_Mem **)v2;
MD_Mem *m1 = *p1, *m2 = *p2;
long l;
l = (long)m2->size_a - (long)m1->size_a;
if (l)
return l < 0 ? -1 : l > 0;
return m2->block - m1->block;
}
int md_display_leaks(int options)
{
md_found_leak = 0;
int i;
leaks = malloc(blocks * sizeof(MD_Mem));
avl_apply(md_mems, (AVL_APPLY)md_linearize_leaks, NULL, -1, AVL_INORDER);
qsort(leaks, blocks, sizeof(MD_Mem *), md_sort_leaks);
if (!md_mems)
{
IFDECO
{
printf(">leaks\n");
printf("<leaks\n");
}
else
printf("\nNo memory leaks (no memory block referenced).\n\n");
return(1);
}
IFDECO
printf(">leaks\n");
for (i=0; i<blocks; i++)
md_display_leak1(leaks[i], options);
IFNDECO
{
if (!md_found_leak)
{
printf("\nNo memory leaks.\n\n");
}
}
IFDECO
printf("<leaks\n");
return(1);
}