aboutsummaryrefslogtreecommitdiff
path: root/sys/memdbg/memlog.c
blob: c8e84281dcc94a5ca2967371a9bdd1e03ce02f90 (plain) (blame)
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
#include <stdio.h>

/* MEMLOG -- SPP callable routines for logging MEMIO debug messages and
 * user application messages in sequence to the mem.log file.
 *
 *		 memlog (message)
 *		memlog1 (message, arg1)
 *		memlog2 (message, arg1, arg2)
 *		memlogs (message, strarg)
 *		 memlev (loglevel)
 *
 * Memlog logs a simple message string.  Memlog[12] allow one or two integer
 * arguments.  Memlogs allows one string argument.
 */

#define	FNAME	"mem.log"
#define	XCHAR	short

static	FILE *fp = NULL;
static	int loglevel = 3;
static 	int number = 0;

#define	LOG_MALLOC	0001
#define	LOG_SALLOC	0002

static void memput();


/* MEMLOG -- User routine to log a message in sequence to the memio debug
 * log file.
 */
memlog_ (message)
XCHAR	*message;
{
	register XCHAR *ip;
	register char *op;
	char	p_message[1024];

	for (ip=message, op=p_message;  *op++ = *ip++;  )
	    ;
	memput (p_message);
}


/* MEMLEV -- Set the logging level.
 */
memlev_ (level)
int	*level;
{
	loglevel = *level;
}


/* MEMLOG1 -- User routine to log a message in sequence to the memio debug
 * log file.
 */
memlo1_ (format, arg1)
XCHAR	*format;
int	*arg1;
{
	register XCHAR *ip;
	register char *op;
	char	p_format[1024];
	char	message[1024];

	/* Output user message. */
	for (ip=format, op=p_format;  *op++ = *ip++;  )
	    ;
	sprintf (message, p_format, *arg1);
	memput (message);
}


/* MEMLOG2 -- User routine to log a message in sequence to the memio debug
 * log file.
 */
memlo2_ (format, arg1, arg2)
XCHAR	*format;
int	*arg1;
int	*arg2;
{
	register XCHAR *ip;
	register char *op;
	char	p_format[1024];
	char	message[1024];

	/* Output user message. */
	for (ip=format, op=p_format;  *op++ = *ip++;  )
	    ;
	sprintf (message, p_format, *arg1, *arg2);
	memput (message);
}


/* MEMLOGS -- User routine to log a message in sequence to the memio debug
 * log file.
 */
memlos_ (format, strarg)
XCHAR	*format;
XCHAR	*strarg;
{
	register XCHAR *ip;
	register char *op;
	char	p_format[1024];
	char	p_strarg[1024];
	char	message[1024];

	/* Output user message. */
	for (ip=format, op=p_format;  *op++ = *ip++;  )
	    ;
	for (ip=strarg, op=p_strarg;  *op++ = *ip++;  )
	    ;
	sprintf (message, p_format, p_strarg);
	memput (message);
}


/* MEMPUT -- Log a message in sequence to the memio debug log file.
 */
static void
memput (message)
char	*message;
{
	/* Open logfile. */
	if (fp == NULL) {
	    unlink (FNAME);
	    if ((fp = fopen (FNAME, "a")) == NULL)
		return;
	}

	/* Output sequence number. */
	fprintf (fp, "%10s  %08d %8s  - -  ",
	    "------", number++, "------");

	/* Output message. */
	fprintf (fp, message);
	fprintf (fp, "\n");

	fflush (fp);
}


/* ZMEMLG -- Used internally by the MEMIO routines.
 */
zmemlg_ (addr, retaddr, action, class, format, arg1, arg2)
int	*addr, *retaddr;
int	*action, *class;
XCHAR	*format;
int	*arg1, *arg2;
{
	register XCHAR *ip;
	register char *op;
	char	p_format[1024];
	char	s_action[2];

	if (!(loglevel & *class))
	    return;

	for (ip=format, op=p_format;  *op++ = *ip++;  )
	    ;
	s_action[0] = *action;
	s_action[1] = '\0';

	if (fp == NULL) {
	    unlink (FNAME);
	    if ((fp = fopen (FNAME, "a")) == NULL)
		return;
	}

	fprintf (fp, "%10x  %08d %8x  %s %d  ",
	    *addr, number++, *retaddr, s_action, *class);
	fprintf (fp, p_format, *arg1, *arg2);
	fprintf (fp, "\n");
	fflush (fp);
}