summaryrefslogtreecommitdiff
path: root/dmake/dbug/malloc/dump.c
blob: 70c8ac30c2d80505c96ad6c4fd69975058c4e4ab (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
96
97
98
99
100
101
102
103
/*
 * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
 * You may copy, distribute, and use this software as long as this
 * copyright statement is not removed.
 */
#include <stdio.h>
#include "malloc.h"
#include "tostring.h"

/*
 * Function:    malloc_dump()
 *
 * Purpose: to dump a printed copy of the malloc chain and
 *      associated data elements
 *
 * Arguments:   fd  - file descriptor to write data to
 *
 * Returns: nothing of any use
 *
 * Narrative:   Just print out all the junk
 *
 * Notes:   This function is implemented using low level calls because
 *      of the likelyhood that the malloc tree is damaged when it
 *      is called.  (Lots of things in the c library use malloc and
 *      we don't want to get into a catch-22).
 *
 */

#ifndef lint
static
char rcs_hdr[] = "$Id: dump.c,v 1.2 2006-07-25 10:07:38 rt Exp $";
#endif


#define ERRSTR  "I/O Error on malloc dump file descriptor\n"

#define WRITEOUT(fd,str,len)    if( write(fd,str,(unsigned)len) != len ) \
                { \
                    (void) write(2,ERRSTR,\
                             (unsigned)strlen(ERRSTR));\
                    exit(120); \
                }

void
malloc_dump(fd)
    int     fd;
{
    char              buffer[512];
    void              exit();
    int           i;
    extern char     * malloc_data_end;
    extern char     * malloc_data_start;
    extern struct mlist     * malloc_end;
    extern struct mlist   malloc_start;
    struct mlist        * ptr;

    WRITEOUT(fd,"MALLOC CHAIN:\n",14);
    WRITEOUT(fd,"-------------------- START ----------------\n",44);

    for(i=0; i < 80; i++)
    {
        buffer[i] = ' ';
    }

    for(ptr = &malloc_start; ptr; ptr = ptr->next)
    {
        (void) tostring(buffer,    (int)ptr,         8, B_HEX,  '0');
        (void) tostring(buffer+9,  (int)ptr->next,   8, B_HEX,  '0');
        (void) tostring(buffer+18, (int)ptr->prev,   8, B_HEX,  '0');
        (void) tostring(buffer+27, (int)ptr->flag,  10, B_HEX,  '0');
        (void) tostring(buffer+38, (int)ptr->s.size, 8, B_DEC,  ' ');
        (void) tostring(buffer+47, (int)ptr->s.size, 8, B_HEX,  '0');
        (void) tostring(buffer+57, (int)ptr->data,   8, B_HEX,  '0');
        buffer[46] = '(';
        buffer[55] = ')';
        buffer[65] = '\n';
        WRITEOUT(fd,buffer,66);
    }
    WRITEOUT(fd,"-------------------- DONE -----------------\n",44);

    WRITEOUT(fd,"Malloc start:      ",19);
    (void) tostring(buffer, (int) &malloc_start, 8, B_HEX, '0');
    buffer[8] = '\n';
    WRITEOUT(fd,buffer,9);

    WRITEOUT(fd,"Malloc end:        ", 19);
    (void) tostring(buffer, (int) malloc_end, 8, B_HEX, '0');
    buffer[8] = '\n';
    WRITEOUT(fd,buffer,9);

    WRITEOUT(fd,"Malloc data start: ", 19);
    (void) tostring(buffer, (int) malloc_data_start, 8, B_HEX, '0');
    buffer[8] = '\n';
    WRITEOUT(fd,buffer,9);

    WRITEOUT(fd,"Malloc data end:   ", 19);
    (void) tostring(buffer, (int) malloc_data_end, 8, B_HEX, '0');
    buffer[8] = '\n';
    WRITEOUT(fd,buffer,9);

} /* malloc_dump(... */