File: dumpheap.c

package info (click to toggle)
zmailer 2.99.51.52pre3-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 16,596 kB
  • ctags: 7,422
  • sloc: ansic: 90,470; sh: 3,608; makefile: 2,784; perl: 1,585; python: 115; awk: 22
file content (167 lines) | stat: -rw-r--r-- 4,592 bytes parent folder | download
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
/*  Author: Mark Moraes <moraes@csri.toronto.edu> */

/*LINTLIBRARY*/

#include "defs.h"
#include "globals.h"

RCSID("$Id: dumpheap.c,v 1.1.1.1 1998/02/10 21:01:46 mea Exp $")

int
__m_prblock(p, is_end_ptr, fp)
univptr_t p;
int is_end_ptr;
FILE *fp;
{
	Word *blk = (Word *) p;
	Word *blkend;
	ulong tag;
	ulong blksize;
	char buf[512];	/* long enough for the sprintfs below */

	if (blk == NULL)
		return 0;

	if ( ! PTR_IN_HEAP(blk)) {
		sprintf(buf, "  ** pointer 0x%lx not in heap\n", (ulong) blk);
		fputs(buf, fp);
		return 0;
	}
	blksize = SIZE(blk);
	tag = TAG(blk);
	if (is_end_ptr) {
		blkend = blk;
		blk -= blksize - 1;
	} else {
		blkend = blk + blksize - 1;
	}
	(void) sprintf(buf, "  %s blk: 0x%lx to 0x%lx, %lu (0x%lx) words",
		       tag == FREE ? "Free" : "Allocated", (ulong) blk,
		       (ulong) blkend, blksize, blksize);
	(void) fputs(buf, fp);
	if ( is_end_ptr && ! PTR_IN_HEAP(blk)) {
		sprintf(buf, "  ** start pointer 0x%lx not in heap\n",
			(ulong) blk);
		fputs(buf, fp);
		return 0;
	}
	if ( !is_end_ptr && ! PTR_IN_HEAP(blkend)) {
		sprintf(buf, "  ** end pointer 0x%lx not in heap\n",
			(ulong) blk);
		fputs(buf, fp);
		return 0;
	}
	if (tag == FREE) {
		int i, n;
		char *cp;

		(void) sprintf(buf, " next=0x%lx, prev=0x%lx\n",
			       (ulong) NEXT(blkend), (ulong) PREV(blkend));
		(void) fputs(buf, fp);
		/* Make sure free block is filled with FREEMAGIC */
		n = (blksize - FREE_OVERHEAD) * sizeof(Word);
		cp = (char *) (blk + FREEHEADERWORDS);
#ifdef DEBUG
		for (i = 0; i < n; i++, cp++) {
			if (*cp != FREEMAGIC) {
				(void) fputs("  ** modified after free().\n",
					     fp);
				break;
			}
		}
#endif
	} else {
#ifdef DEBUG
		(void) sprintf(buf, " really %lu bytes\n",
			       (ulong) REALSIZE(blk));
		(void) fputs(buf, fp);
#else
		(void) fputs("\n", fp);
#endif
	}
	if (TAG(blk) == FREE) {
		if( ! VALID_NEXT_PTR(blkend))
			(void) fputs("  ** bad next pointer\n", fp);
		if( ! VALID_PREV_PTR(blkend))
			(void) fputs("  ** bad prev pointer\n", fp);
	} else {
		if ( ! VALID_MAGIC(blk))
			(void) fputs("  ** end of block overwritten\n", fp);
	}
	if ( ! VALID_START_SIZE_FIELD(blk)) {
		/* At some systems the  size_t  is 64 bits, while long is
		   "mere" 32 bits (FreeBSD, for example) */
		sprintf(buf, "  ** bad size field: tags = 0x%lx, 0x%lx\n",
			(unsigned long) SIZEFIELD(blk),
			(unsigned long) SIZEFIELD(blkend));
		(void) fputs(buf, fp);
		return 0;
	}
	(void) fflush(fp);
	return 1;
}

/*
 * Similar to malloc_verify except that it prints the heap as it goes along.
 * The only ASSERTs in this routine are those that would cause it to wander
 * off into the sunset because of corrupt tags.
 */
void
mal_heapdump(fp)
FILE *fp;
{
	REGISTER Word *ptr;
	REGISTER Word *blk;
	REGISTER Word *blkend;
	int i;
	char buf[512];	/* long enough for the sprintfs below */

	if (_malloc_loword == NULL) { /* Nothing malloc'ed yet */
		(void) fputs("Null heap - nothing malloc'ed yet\n", fp);
		return;
	}
		
	(void) fputs("Heap printout:\n", fp);
	(void) fputs("Free list rover pointers:\n", fp);
	sprintf(buf, "  First non-null bin is %d\n", _malloc_firstbin);
	(void) fputs(buf, fp);
	for (i = 0; i < MAXBINS; i++) {
		if ((ptr = _malloc_rovers[i]) == NULL)
			continue;
		(void) sprintf(buf, "  %d: 0x%lx\n", i, (ulong) ptr);
		(void) fputs(buf, fp);
		if ( ! PTR_IN_HEAP(ptr))
			(void) fputs("  ** not in heap\n", fp);
		if ( ! VALID_END_SIZE_FIELD(ptr))
			(void) fputs("  ** bad end size field\n", fp);
		if ( ! VALID_NEXT_PTR(ptr))
			(void) fputs("  ** bad next pointer\n", fp);
		if ( ! VALID_PREV_PTR(ptr))
			(void) fputs("  ** bad prev pointer\n", fp);
	}
	if (_malloc_rovers[MAXBINS] != NULL) {
		(void) sprintf(buf, "  ** rover terminator is 0x%lx, fixing\n",
			       (ulong) _malloc_rovers[MAXBINS]);
		(void) fputs(buf, fp);
		_malloc_rovers[MAXBINS] = NULL;
	}
	for (ptr = _malloc_mem; ptr != NULL; ptr = ptr->next) {
		/* print the arena */
		(void) sprintf(buf, "Arena from 0x%lx to 0x%lx, %lu (0x%lx) words\n",
			       (ulong) ptr, (ulong) (ptr + SIZE(ptr+1)),
			       (ulong) SIZE(ptr+1)+1, (ulong) SIZE(ptr+1)+1);
		(void) fputs(buf, fp);
		(void) sprintf(buf, "Next arena is 0x%lx\n", (ulong)ptr->next);
		(void) fputs(buf, fp);
		(void) fflush(fp);
		ASSERT(SIZEFIELD(ptr+1) == SIZEFIELD(ptr + SIZE(ptr+1)),
		       "mal_dumpheap: corrupt malloc arena");
		blkend = ptr + SIZE(ptr + 1);
		for(blk = ptr + ARENASTART; blk < blkend; blk += SIZE(blk)) {
			ASSERT(__m_prblock((univptr_t) blk, 0, fp),
				"mal_dumpheap: corrupt block");
		}
	}
	(void) fputs("==============\n", fp);
	(void) fflush(fp);
}