summaryrefslogtreecommitdiff
path: root/passes/cmds/cover.cc
blob: b8792a05f185df89e63155e64a6df68bcfcec912 (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
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
/*
 *  yosys -- Yosys Open SYnthesis Suite
 *
 *  Copyright (C) 2014  Clifford Wolf <clifford@clifford.at>
 *
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include <sys/types.h>
#include <unistd.h>
#include "kernel/yosys.h"

USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN

struct CoverPass : public Pass {
	CoverPass() : Pass("cover", "print code coverage counters") { }
	virtual void help()
	{
		//   |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
		log("\n");
		log("    cover [options] [pattern]\n");
		log("\n");
		log("Print the code coverage counters collected using the cover() macro in the Yosys\n");
		log("C++ code. This is useful to figure out what parts of Yosys are utilized by a\n");
		log("test bench.\n");
		log("\n");
		log("    -q\n");
		log("        Do not print output to the normal destination (console and/or log file)\n");
		log("\n");
		log("    -o file\n");
		log("        Write output to this file, truncate if exists.\n");
		log("\n");
		log("    -a file\n");
		log("        Write output to this file, append if exists.\n");
		log("\n");
		log("    -d dir\n");
		log("        Write output to a newly created file in the specified directory.\n");
		log("\n");
		log("When one or more pattern (shell wildcards) are specified, then only counters\n");
		log("matching at least one pattern are printed.\n");
		log("\n");
		log("\n");
		log("It is also possible to instruct Yosys to print the coverage counters on program\n");
		log("exit to a file using environment variables:\n");
		log("\n");
		log("    YOSYS_COVER_DIR=\"{dir-name}\" yosys {args}\n");
		log("\n");
		log("        This will create a file (with an auto-generated name) in this\n");
		log("        directory and write the coverage counters to it.\n");
		log("\n");
		log("    YOSYS_COVER_FILE=\"{file-name}\" yosys {args}\n");
		log("\n");
		log("        This will append the coverage counters to the specified file.\n");
		log("\n");
		log("\n");
		log("Hint: Use the following AWK command to consolidate Yosys coverage files:\n");
		log("\n");
		log("    gawk '{ p[$3] = $1; c[$3] += $2; } END { for (i in p)\n");
		log("      printf \"%%-60s %%10d %%s\\n\", p[i], c[i], i; }' {files} | sort -k3\n");
		log("\n");
		log("\n");
		log("Coverage counters are only available in debug builds of Yosys for Linux.\n");
		log("\n");
	}
	virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
	{
		std::vector<FILE*> out_files;
		std::vector<std::string> patterns;
		bool do_log = true;

		size_t argidx;
		for (argidx = 1; argidx < args.size(); argidx++)
		{
			if (args[argidx] == "-q") {
				do_log = false;
				continue;
			}
			if ((args[argidx] == "-o" || args[argidx] == "-a" || args[argidx] == "-d") && argidx+1 < args.size()) {
				const char *open_mode = args[argidx] == "-a" ? "a+" : "w";
				std::string filename = args[++argidx];
				if (args[argidx-1] == "-d") {
					char filename_buffer[4096];
					snprintf(filename_buffer, 4096, "%s/yosys_cover_%d_XXXXXX.txt", filename.c_str(), getpid());
					filename = mkstemps(filename_buffer, 4);
				}
				FILE *f = fopen(filename.c_str(), open_mode);
				if (f == NULL) {
					for (auto f : out_files)
						fclose(f);
					log_cmd_error("Can't create file %s.\n", args[argidx].c_str());
				}
				out_files.push_back(f);
				continue;
			}
			break;
		}
		while (argidx < args.size() && args[argidx].substr(0, 1) != "-")
			patterns.push_back(args[argidx++]);
		extra_args(args, argidx, design);

		if (do_log) {
			log_header("Printing code coverage counters.\n");
			log("\n");
		}

#ifdef COVER_ACTIVE
		for (auto &it : get_coverage_data()) {
			if (!patterns.empty()) {
				for (auto &p : patterns)
					if (patmatch(p.c_str(), it.first.c_str()))
						goto pattern_match;
				continue;
			}
		pattern_match:
			for (auto f : out_files)
				fprintf(f, "%-60s %10d %s\n", it.second.first.c_str(), it.second.second, it.first.c_str());
			if (do_log)
				log("%-60s %10d %s\n", it.second.first.c_str(), it.second.second, it.first.c_str());
		}
#else
		for (auto f : out_files)
			fclose(f);

		log_cmd_error("Coverage counters are only available in debug builds of Yosys for Linux.\n");
#endif

		for (auto f : out_files)
			fclose(f);
	}
} CoverPass;

PRIVATE_NAMESPACE_END