xref: /plan9/sys/lib/acid/coverage (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
1// Coverage library
2
3defn coverage()
4{
5	local lmap, lp, e, pc, n, l;
6
7	new();
8
9	bblock = {};
10
11	// find the first location in the text
12	e = (map()[0][1])\i;
13
14	while e < etext-4 do {
15		l = follow(e);
16		if tail l != {} then {
17			if match(l[0], bblock) < 0 then
18				bblock = append bblock, l[0];
19			if match(l[1], bblock) < 0 then
20				bblock = append bblock, l[1];
21		}
22		e++;
23	}
24
25	l = bblock;
26	while l != {} do {
27		*fmt(head l, bpfmt) = bpinst;
28		l = tail l;
29	}
30
31	while 1 do {
32		cont();
33		pc = *PC;
34		n = match(pc, bblock);
35		if n >= 0 then {
36			pc = fmt(pc, bpfmt);
37			*pc = @pc;
38			bblock = delete bblock, n;
39		}
40		else {
41			pstop(pid);
42			return {};
43		}
44	}
45}
46
47defn eblock(addr)
48{
49	addr = addr\i;
50
51	while addr < etext do {
52		if (tail follow(addr)) != {} then
53			return pcline(addr);
54		addr++;
55	}
56	return 0;
57}
58
59defn basic(stsrc, ensrc, file)
60{
61	local src, text;
62
63	if stsrc >= ensrc then
64		return {};
65
66	print(file, ":", stsrc, ",", ensrc, "\n");
67	src = match(file, srcfiles);
68
69	if src >= 0 then
70		src = srctext[src];
71	else
72		src = findsrc(file);
73
74	if src == {} then
75		print("no source for ", file, "\n");
76	else {
77		while stsrc <= ensrc do {
78			text = src[stsrc];
79			if text != {} then
80				print("\t", stsrc, ":", text, "\n");
81			stsrc = stsrc+1;
82		}
83	}
84}
85
86defn analyse(fnaddr)
87{
88	local addr, l, tfn;
89
90	new();
91
92	tfn = fnbound(fnaddr);
93
94	l = bblock;
95	while l do {
96		addr = head l;
97
98		if addr >= tfn[0] && addr < tfn[1] then
99			basic(pcline(addr), eblock(addr), pcfile(addr));
100
101		l = tail l;
102	}
103	kill(pid);
104}
105
106defn report()
107{
108	local addr, l;
109
110	new();
111
112	l = bblock;
113	while l do {
114		addr = head l;
115
116		basic(pcline(addr), eblock(addr), pcfile(addr));
117
118		l = tail l;
119	}
120	kill(pid);
121}
122
123defn stopped(pid)
124{
125	return {};
126}
127
128print("/sys/lib/acid/coverage");
129