Zth (libzth)
Loading...
Searching...
No Matches
zth-vcd.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2019-2026 Jochem Rutgers
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 */
6
12#include <zth>
13
14static void show_help(char const* prog, FILE* out = stdout)
15{
16 fprintf(out, "Convert Zth perf output to VCD format.\n\n");
17
18 fprintf(out, "Usage: %s [-h|--help] [--] <perf-file> [<vcd-file>]\n\n", prog);
19
20 fprintf(out, "Options:\n");
21 fprintf(out, " -h, --help Show this help message and exit\n");
22 fprintf(out, "Arguments:\n");
23 fprintf(out, " <perf-file> Path to the input perf file (required)\n");
24 fprintf(out,
25 " <vcd-file> Path to the output VCD file (optional, defaults to stdout)\n");
26}
27
28int main(int argc, char* argv[])
29{
30 if(argc < 2) {
31 show_help(argv[0], stderr);
32 return 1;
33 }
34
35 char const* perf_path = nullptr;
36 char const* vcd_path = nullptr;
37 bool options = true;
38
39 for(int i = 1; i < argc; ++i) {
40 if(options && strcmp(argv[i], "--") == 0) {
41 options = false;
42 } else if(
43 options && (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)) {
44 show_help(argv[0]);
45 return 0;
46 } else if(!perf_path) {
47 perf_path = argv[i];
48 } else if(!vcd_path) {
49 vcd_path = argv[i];
50 } else {
51 fprintf(stderr, "Unexpected argument: %s\n", argv[i]);
52 show_help(argv[0], stderr);
53 return 1;
54 }
55 }
56
57 if(!perf_path) {
58 fprintf(stderr, "Error: <perf-file> is required\n");
59 show_help(argv[0], stderr);
60 return 1;
61 }
62
63 FILE* fperf = fopen(perf_path, "rb");
64 if(!fperf) {
65 fprintf(stderr, "Error opening perf file '%s': %s\n", perf_path, strerror(errno));
66 return 2;
67 }
68
69 FILE* fvcd = stdout;
70 if(vcd_path) {
71 fvcd = fopen(vcd_path, "wb");
72 if(!fvcd) {
73 fprintf(stderr, "Error opening VCD file '%s': %s\n", vcd_path,
74 strerror(errno));
75 (void)fclose(fperf);
76 return 2;
77 }
78 }
79
80 int res = zth::perf_vcdf(fperf, fvcd);
81
82 (void)fclose(fperf);
83 (void)fclose(fvcd);
84
85 if(res) {
86 fprintf(stderr, "Error converting perf to VCD: %s\n", strerror(res));
87 return 3;
88 }
89
90 return 0;
91}
int perf_vcdf(FILE *perf, FILE *vcd) noexcept
Convert a perf file into VCD.
Definition perf.cpp:1440
int main(int argc, char *argv[])
Definition zth-vcd.cpp:28