1 /***********************************************************************
5 The following is a notice of limited availability of the code and
6 Government license and disclaimer which must be included in the
7 prologue of the code and in all source listings of the code.
10 (c) 1977 University of Chicago
12 Permission is hereby granted to use, reproduce, prepare
13 derivative works, and to redistribute to others at no charge. If
14 you distribute a copy or copies of the Software, or you modify a
15 copy or copies of the Software or any portion of it, thus forming
16 a work based on the Software and make and/or distribute copies of
17 such work, you must meet the following conditions:
19 a) If you make a copy of the Software (modified or verbatim)
20 it must include the copyright notice and Government
21 license and disclaimer.
23 b) You must cause the modified Software to carry prominent
24 notices stating that you changed specified portions of
27 This software was authored by:
29 Argonne National Laboratory
30 J. Michalakes: (630) 252-6646; email: michalak@mcs.anl.gov
31 Mathematics and Computer Science Division
32 Argonne National Laboratory, Argonne, IL 60439
34 ARGONNE NATIONAL LABORATORY (ANL), WITH FACILITIES IN THE STATES
35 OF ILLINOIS AND IDAHO, IS OWNED BY THE UNITED STATES GOVERNMENT,
36 AND OPERATED BY THE UNIVERSITY OF CHICAGO UNDER PROVISION OF A
37 CONTRACT WITH THE DEPARTMENT OF ENERGY.
39 GOVERNMENT LICENSE AND DISCLAIMER
41 This computer code material was prepared, in part, as an account
42 of work sponsored by an agency of the United States Government.
43 The Government is granted for itself and others acting on its
44 behalf a paid-up, nonexclusive, irrevocable worldwide license in
45 this data to reproduce, prepare derivative works, distribute
46 copies to the public, perform publicly and display publicly, and
47 to permit others to do so. NEITHER THE UNITED STATES GOVERNMENT
48 NOR ANY AGENCY THEREOF, NOR THE UNIVERSITY OF CHICAGO, NOR ANY OF
49 THEIR EMPLOYEES, MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR
50 ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE ACCURACY,
51 COMPLETENESS, OR USEFULNESS OF ANY INFORMATION, APPARATUS,
52 PRODUCT, OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD
53 NOT INFRINGE PRIVATELY OWNED RIGHTS.
55 ***************************************************************************/
60 * Various debugging things, including malloc debugging stuff
68 /* #define NEXUS_MALLOC_DEBUG */
69 #ifdef NEXUS_MALLOC_DEBUG
74 #include <sys/param.h>
75 #include <sys/access.h>
78 * Memory allocation debugging and diagnostics code.
81 #define NEXUS_MALLOC_PAD 512
82 #define NEXUS_N_MALLOC_RECS 20000
84 typedef struct _malloc_rec_t
101 static struct _malloc_rec_t malloc_recs
[NEXUS_N_MALLOC_RECS
];
102 static int next_malloc_rec
= 0;
104 static char last_successful_file
[1024];
105 static int last_successful_line
;
107 static int initialized
= 0;
110 #define START_MAGIC 0xf00dface
111 #define END_MAGIC 0xeeaaddff
114 * nexus_debug_malloc()
116 * Malloc wrapper that can print out the size and location
117 * of allocations when the -Dmalloc argument has been given.
119 * The intent is to define a macro of the form
122 * #define malloc(size) nexus_debug_malloc(size, __FILE__, __LINE__)
125 * in order to trace memory allocation in detail.
128 void *nexus_debug_malloc(int size
, char *file
, int line
)
134 nexus_debug_malloc_check(file
, line
);
136 while ((size
& 0x07) != 0)
139 if (next_malloc_rec
>= NEXUS_N_MALLOC_RECS
)
141 fprintf(stderr
,"Too many malloc recs\n");
146 rec
= &malloc_recs
[next_malloc_rec
];
148 addr
= malloc(size
+ 2 * NEXUS_MALLOC_PAD
);
149 rc
= (char *) addr
+ NEXUS_MALLOC_PAD
;
150 bzero( rc
, size
) ; /* zero storage */
153 printf("malloc(%d) at %s:%d returns %x idx=%d\n",
154 size
, file
, line
, rc
, next_malloc_rec
);
161 rec
->free_file
= (char *) NULL
;
165 if (NEXUS_MALLOC_PAD
>= 4)
167 *((int *) addr
) = next_malloc_rec
;
169 for (p
= (int *) addr
+ 1; p
< (int *) rc
; p
++)
173 for (p
= (int *) ((char *) addr
+ size
+ NEXUS_MALLOC_PAD
);
174 p
< (int *) ((char *) addr
+ size
+ 2 * NEXUS_MALLOC_PAD
); p
++)
184 } /* nexus_debug_malloc() */
188 * nexus_debug_malloc_check()
190 * Walk the list of allocated blocks looking for munged memory.
192 nexus_debug_malloc_check(char *file
, int line
)
198 if (NEXUS_MALLOC_PAD
< 4)
201 for (i
= 0; i
< next_malloc_rec
; i
++)
203 rec
= &malloc_recs
[i
];
208 if (*((int *) rec
->addr
) != i
)
210 fprintf(stderr
,"Malloc check (start) failed for idx %d at %s:%d for allocation at %s:%d of size %d. Last successful check was %s:%d\n",
213 rec
->file
, rec
->line
,
215 last_successful_file
,
216 last_successful_line
);
220 for (p
= (int *) rec
->addr
+ 1; p
< (int *) ((char *) rec
->addr
+ NEXUS_MALLOC_PAD
); p
++)
222 if (*p
!= START_MAGIC
)
224 fprintf(stderr
,"Malloc check (start) failed for idx %d at %s:%d for allocation at %s:%d of size %d Last successful check was %s:%d\n",
227 rec
->file
, rec
->line
,
229 last_successful_file
,
230 last_successful_line
);
235 for (p
= (int *) ((char *) rec
->addr
+ rec
->size
+ NEXUS_MALLOC_PAD
);
236 p
< (int *) ((char *) rec
->addr
+ rec
->size
+ 2 * NEXUS_MALLOC_PAD
); p
++)
240 fprintf(stderr
,"Malloc check (end) failed for idx %d at %s:%d for allocation at %s:%d of size %d Last successful check was %s:%d\n",
243 rec
->file
, rec
->line
,
245 last_successful_file
,
246 last_successful_line
);
251 strcpy(last_successful_file
, file
);
252 last_successful_line
= line
;
253 } /* nexus_debug_malloc_check() */
255 void nexus_debug_mem_check(int size
, void *address
)
259 char *pad1_start
, *pad1_end
, *pad2_start
, *pad2_end
, *a_start
, *a_end
;
261 for (i
= 0; i
< next_malloc_rec
; i
++)
263 rec
= &malloc_recs
[i
];
268 pad1_start
= rec
->addr
;
269 pad1_end
= pad1_start
+ NEXUS_MALLOC_PAD
;
271 pad2_start
= rec
->addr
+ rec
->size
+ NEXUS_MALLOC_PAD
;
272 pad2_end
= pad2_start
+ NEXUS_MALLOC_PAD
;
275 a_end
= a_start
+ size
- 1;
277 if ((a_start
>= pad1_start
&& a_start
< pad1_end
) ||
278 (a_end
>= pad1_start
&& a_end
< pad1_end
) ||
279 (a_start
>= pad2_start
&& a_start
< pad2_end
) ||
280 (a_end
>= pad2_start
&& a_end
< pad2_end
) ||
281 (a_start
< pad1_start
&& a_end
> pad1_end
) ||
282 (a_start
< pad2_start
&& a_end
> pad2_end
))
284 fprintf(stderr
,"Malloc memory check for address %x length %s failed for idx %d for allocation at %s:%d of size %d.\n",
288 rec
->file
, rec
->line
,
293 } /* nexus_debug_mem_check() */
297 * nexus_debug_show_freed_blocks()
299 * Walk the list of allocated blocks looking blocks that
302 void nexus_debug_show_freed_blocks()
307 for (i
= 0; i
< next_malloc_rec
; i
++)
309 rec
= &malloc_recs
[i
];
313 fprintf(stderr
,"Unfreed block %d size=%5d at %s:%d\n",
314 i
, rec
->size
, rec
->file
, rec
->line
);
317 } /* nexus_debug_show_freed_blocks() */
321 * nexus_debug_show_malloc_stats()
323 * Print a summary of memory allocation statistics.
325 void nexus_debug_show_malloc_stats()
329 int bytes
, bytes_freed
;
332 bytes
= bytes_freed
= n_blocks_freed
= 0;
334 for (i
= 0; i
< next_malloc_rec
; i
++)
336 rec
= &malloc_recs
[i
];
342 bytes_freed
+= rec
->size
;
347 fprintf(stderr
,"Malloc statistics:\n");
348 fprintf(stderr
,"\tbytes allocated: %d\n", bytes
);
349 fprintf(stderr
,"\tbytes freed: %d\n", bytes_freed
);
350 fprintf(stderr
,"\tbytes unfreed: %d\n", bytes
- bytes_freed
);
351 fprintf(stderr
,"\tblocks allocated: %d\n", next_malloc_rec
);
352 fprintf(stderr
,"\tblocks freed: %d\n", n_blocks_freed
);
353 fprintf(stderr
,"\tblocks unfreed: %d\n", next_malloc_rec
- n_blocks_freed
);
354 } /* nexus_debug_show_malloc_stats() */
360 void nexus_debug_free(void *ptr
, char *file
, int line
)
367 if (NEXUS_MALLOC_PAD
>= 4)
369 addr
= (char *) ptr
- NEXUS_MALLOC_PAD
;
371 idx
= *((int *) addr
);
373 if ( idx
< 0 || idx
>= NEXUS_N_MALLOC_RECS
)
375 fprintf(stderr
,"nexus_debug_free(): bad idx = %d; \n possibly corrupt ptr %08x at %s line %d\n",
376 idx
, ptr
, file
, line
) ;
379 rec
= &(malloc_recs
[idx
]);
381 if (rec
->freed
== 1 )
383 fprintf(stderr
,"nexus_debug_free(): block %x idx %d allocated at %s:%d was freed twice at %s:%d and %s:%d\n",
385 rec
->file
, rec
->line
,
386 rec
->free_file
, rec
->free_line
,
392 rec
->free_file
= file
;
393 rec
->free_line
= line
;
403 nexus_printf("free(%x) at %s:%d index=%d\n",
404 ptr
, file
, line
, idx
);
406 } /* nexus_debug_free() */
410 #endif /* NEXUS_DEBUG */