summaryrefslogtreecommitdiffstats
path: root/vmcore-dmesg/vmcore-dmesg.c
blob: fe7df8ec372cc6f8c4bfc11a32dc50644cb7a859 (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
#include <elf_info.h>

/* The 32bit and 64bit note headers make it clear we don't care */
typedef Elf32_Nhdr Elf_Nhdr;

extern const char *fname;

static void write_to_stdout(char *buf, unsigned int nr)
{
	ssize_t ret;

	ret = write(STDOUT_FILENO, buf, nr);
	if (ret != nr) {
		fprintf(stderr, "Failed to write out the dmesg log buffer!:"
			" %s\n", strerror(errno));
		exit(54);
	}
}

static int read_vmcore_dmesg(int fd, void (*handler)(char*, unsigned int))
{
	int ret;

	ret = read_elf(fd);
	if (ret > 0) {
		fprintf(stderr, "Unable to read ELF information"
			" from vmcore\n");
		return ret;
	}

	dump_dmesg(fd, handler);

	return 0;
}

int main(int argc, char **argv)
{
	ssize_t ret;
	int fd;

	if (argc != 2) {
		fprintf(stderr, "usage: %s <kernel core file>\n", argv[0]);
		return 1;
	}
	fname = argv[1];

	fd = open(fname, O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "Cannot open %s: %s\n",
			fname, strerror(errno));
		return 2;
	}

	ret = read_vmcore_dmesg(fd, write_to_stdout);
	
	close(fd);

	return ret;
}