File: mcelog.c

package info (click to toggle)
mcelog 0.4-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 112 kB
  • ctags: 112
  • sloc: ansic: 750; makefile: 65; sh: 26
file content (346 lines) | stat: -rw-r--r-- 7,149 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* Copyright (C) 2004,2005 Andi Kleen, SuSE Labs.
   Decode IA32/x86-64 machine check events in /dev/mcelog. 

   mcelog is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; version
   2.

   mcelog is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should find a copy of v2 of the GNU General Public License somewhere
   on your Linux system; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

#define _GNU_SOURCE 1
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <asm/types.h>
#include <asm/ioctls.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
#include "mcelog.h"
#include "k8.h"
#include "p4.h"

enum {
	CPU_GENERIC,
	CPU_K8,
	CPU_P4
} cpu = CPU_GENERIC;	

char *logfn = "/dev/mcelog";

int use_syslog;

void Wprintf(char *fmt, ...)
{
	va_list ap;
	va_start(ap,fmt);
	if (use_syslog) 
		vsyslog(LOG_NOTICE, fmt, ap);
	else
		vprintf(fmt, ap);
	va_end(ap);
}

char *bankname(unsigned bank) 
{ 
	static char numeric[64];
	switch (cpu) { 
	case CPU_K8:
		return k8_bank_name(bank);
	case CPU_P4:
		return p4_bank_name(bank);
	/* add banks of other cpu types here */
	default:
		sprintf(numeric, "BANK %d", bank); 
		return numeric;
	}
} 

void dump_mce(struct mce *m) 
{
	/* should not happen */
	if (!m->finished)
		Wprintf("not finished?\n");
	Wprintf("CPU %d %s ", m->cpu, bankname(m->bank));
	if (m->tsc)
		Wprintf("TSC %Lx %s\n", 
			m->tsc, 
			(m->mcgstatus & MCI_STATUS_UC) ? 
			"(upper bound, found by polled driver)" : "");
	else
		Wprintf("from boot or resume\n");
	if (m->rip) 
		Wprintf("RIP%s %02x:%Lx ", 
		       !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
		       m->cs, m->rip);
	if (m->misc)
		Wprintf("MISC %Lx ", m->misc);
	if (m->addr)
		Wprintf("ADDR %Lx ", m->addr);
	if (m->rip | m->misc | m->addr)	
		Wprintf("\n");
	switch (cpu) { 
	case CPU_K8:
		decode_k8_mc(m); 
		break;
	case CPU_P4:
		decode_p4_mc(m);
		break;
	/* add handlers for other CPUs here */
	default:
		break;
	} 
	/* decode all status bits here */
	Wprintf("STATUS %Lx MCGSTATUS %Lx\n", m->status, m->mcgstatus);
}

void check_cpu(void)
{ 
	FILE *f;
	f = fopen("/proc/cpuinfo","r");
	if (f != NULL) { 
		int found = 0; 
		int family; 
		char vendor[64];
		char *line = NULL;
		size_t linelen = 0; 
		while (getdelim(&line, &linelen, '\n', f) > 0 && found < 2) { 
			if (sscanf(line, "vendor_id : %63[^\n]", vendor) == 1) 
				found++; 
			if (sscanf(line, "cpu family : %d", &family) == 1)
				found++;
		} 
		if (found == 2) {
			if (!strcmp(vendor,"AuthenticAMD") && family == 15)
				cpu = CPU_K8;
			if (!strcmp(vendor,"GenuineIntel") && family == 15)
				cpu = CPU_P4;
			/* Add checks for other CPUs here */	
		} else {
			fprintf(stderr, "mcelog: warning: Cannot parse /proc/cpuinfo\n"); 
		} 
		fclose(f);
		free(line);
	} else
		fprintf(stderr, "mcelog: warning: Cannot open /proc/cpuinfo\n");
} 

char *skipgunk(char *s)
{
	while (isspace(*s))
		++s; 
	if (*s == '<') { 
		s += strcspn(s, ">"); 
		if (*s == '>') 
			++s; 
	}
	while (isspace(*s))
		++s; 
	return s;
}

void dump_mce_final(struct mce *m, char *symbol, int missing)
{
	m->finished = 1;
	dump_mce(m);
	if (symbol[0])
		Wprintf("RIP: %s\n", symbol);
}

/* Decode ASCII input for fatal messages */
void decodefatal(FILE *inf)
{
	struct mce m;
	char *line = NULL; 
	size_t linelen = 0;
	int k;
	int missing = 0; 
	char symbol[100];
	int data = 0;

	k = 0;
	memset(&m, 0, sizeof(struct mce));
	symbol[0] = '\0';
	while (getdelim(&line, &linelen, '\n', inf) > 0) { 
		int n = 0;
		char *s = skipgunk(line);

		if (!strncmp(s, "CPU", 3)) { 
			unsigned cpu = 0, bank = 0;
			n = sscanf(s,
	       "CPU %u: Machine Check Exception: %16Lx Bank %d: %016Lx",
			       &cpu,
			       &m.mcgstatus,
			       &bank,
			       &m.status
			       );
			m.cpu = cpu;
			m.bank = bank;
			if (n != 4) 
				missing++; 
		} 

		else if (!strncmp(s, "RIP", 3)) { 
			unsigned cs = 0; 

			if (!strncmp(s, "RIP!INEXACT!", 12))
				s += 12; 
			else
				s += 3; 

			n = sscanf(s, "%02x:<%016Lx> {%100s}",
				   &cs,
				   &m.rip, 
				   symbol); 
			m.cs = cs;
			if (n < 2) 
				missing++; 
		} 

		else if (!strncmp(s, "TSC",3)) { 
			if ((n = sscanf(s, "TSC %Lx", &m.tsc)) != 1) 
				missing++; 
		}
		else if (!strncmp(s, "ADDR",4)) { 
			if ((n = sscanf(s, "ADDR %Lx", &m.addr)) != 1) 
				missing++; 
		}
		else if (!strncmp(s, "MISC",4)) { 
			if ((n = sscanf(s, "MISC %Lx", &m.misc)) != 1) 
				missing++; 
		}
		else { 
			if (*s && data) { 
				dump_mce_final(&m, symbol, missing); 
				data = 0;
			} 
			Wprintf("%s", line); 
		} 
		if (n > 0) 
			data = 1;
	} 
	free(line);
	if (data)
		dump_mce_final(&m, symbol, missing);
}

void usage(void)
{
	fprintf(stderr, 
		"Usage:\n"
		"  mcelog [--k8|--p4|--generic] [--syslog] [mcelogdevice]\n"
		"  mcelog [--k8|--p4|--generic] --ascii\n"
		"Decode machine check error records\n");	
	exit(1);
}

void test_device(void)
{
	int fd;

	fd = open(logfn, O_RDONLY);
	if (fd < 0) {
		printf("%s is NOT usable (%s)\n", logfn, strerror(errno));
		exit(1);
	}
	printf("%s is usable\n", logfn);
	close(fd);
	exit(0);
}

int main(int ac, char **av) 
{ 
	int recordlen = 0;
	int loglen = 0;

	if (av[1] && !strcmp(av[1],"-t")) {
	        test_device();
		/* NOT REACHED */
	}

	if (av[1] && !strcmp(av[1],"--syslog")) { 
		av++;
		openlog("mcelog", 0, LOG_DAEMON);
		use_syslog = 1;
	}

	check_cpu();
	if (av[1]) { 
		if (!strcmp(av[1], "--k8")) {
			cpu = CPU_K8;
			av++;
		} else if (!strcmp(av[1], "--p4")) {
			cpu = CPU_P4;
			av++;
		} else if (!strcmp(av[1], "--generic")) { 
			cpu = CPU_GENERIC;
			av++;
		}
	}		
	
	if (av[1] && !strcmp(av[1], "--ascii")) { 
		av++;
		if (av[1]) 
			usage();
		decodefatal(stdin); 
		exit(0);
	} 

	if (av[1]) {
		logfn = av[1];
		av++;
	}
	if (av[1])
		usage();
		
	int fd = open(logfn, O_RDONLY); 
	if (fd < 0) {
		fprintf(stderr, "Cannot open %s\n", logfn); 
		exit(1);
	}
	
	if (ioctl(fd, MCE_GET_RECORD_LEN, &recordlen) < 0)
		err("MCE_GET_RECORD_LEN");
	if (ioctl(fd, MCE_GET_LOG_LEN, &loglen) < 0)
		err("MCE_GET_LOG_LEN");

	if (recordlen > sizeof(struct mce))
		fprintf(stderr, 
    "mcelog: warning: record length longer than expected. Consider update.\n");

	char *buf = calloc(recordlen, loglen); 
	if (!buf) 
		exit(100);
	
	int len = read(fd, buf, recordlen * loglen); 
	if (len < 0) 
		err("read"); 

	int i; 
	for (i = 0; i < len / recordlen; i++) { 
		struct mce *mce = (struct mce *)(buf + i*recordlen);
		printf("MCE %d\n", i); 
		dump_mce(mce); 
	}

	if (recordlen < sizeof(struct mce))  {
		fprintf(stderr, 
			"mcelog: warning: %lu bytes ignored in each record\n",
				(unsigned long)recordlen - sizeof(struct mce)); 
		fprintf(stderr, "mcelog: consider an update\n"); 
	}	

	exit(0); 
}