summaryrefslogtreecommitdiffstats
path: root/usr.sbin/kernbb/kernbb.c
blob: 83ddd536b521809907002152e428bca0665bff7d (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
/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
 * ----------------------------------------------------------------------------
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <err.h>
#include <fcntl.h>
#include <kvm.h>
#include <nlist.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

struct bb {
	u_long	zero_one;
	u_long	filename;
	u_long	counts;
	u_long	ncounts;
	u_long	next;
	u_long	addr;
	u_long	nwords;
	u_long	func;
	u_long	lineno;
	u_long	file;
};

struct nlist namelist[] = {
	{ "bbhead", 0, 0, 0, 0 },
	{ NULL, 0, 0, 0, 0 }
};

kvm_t	*kv;

int
main(int argc __unused, char **argv __unused)
{
	int i;
	u_long l1,l2,l4;
	struct bb bb;
	char buf[BUFSIZ], *p;
	FILE *f;

	kv = kvm_open(NULL,NULL,NULL,O_RDWR,"dnc");
	if (!kv) 
		err(1,"kvm_open");
	i = kvm_nlist(kv,namelist);
	if (i)
		err(1,"kvm_nlist");

	l1 = namelist[0].n_value;
	kvm_read(kv,l1,&l2,sizeof l2);
	while(l2) {
		l1 += sizeof l1;
		kvm_read(kv,l2,&bb,sizeof bb);
		l2 = bb.next;
		kvm_read(kv, bb.filename, buf, sizeof(buf));
		p = buf;
		f = fopen(p, "w");
		if (f != NULL) {
			printf("Writing \"%s\"\n", p);
		} else {
			p = strrchr(buf, '/');
			if (p == NULL)
				p = buf;
			else
				p++;
			printf("Writing \"%s\" (spec \"%s\")\n", p, buf);
			f = fopen(p, "w");
		}
		if (f == NULL)
			err(1,"%s", p);
		fwrite(&bb.ncounts, 4, 1, f);
		l4 = 0;
		fwrite(&l4, 4, 1, f);
		p = malloc(bb.ncounts * 8);
		kvm_read(kv, bb.counts, p, bb.ncounts * 8);
		fwrite(p, 8, bb.ncounts, f);
		fclose(f);
		free(p);
	}
	return 0;
}
OpenPOWER on IntegriCloud