xref: /linux/tools/perf/builtin-data.c (revision 536e81f07450562caba8e62022763a78fd00a0a7)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "builtin.h"
6 #include "debug.h"
7 #include <subcmd/parse-options.h>
8 #include "data-convert.h"
9 #include "util/util.h"
10 
11 typedef int (*data_cmd_fn_t)(int argc, const char **argv);
12 
13 struct data_cmd {
14 	const char	*name;
15 	const char	*summary;
16 	data_cmd_fn_t	fn;
17 };
18 
19 static struct data_cmd data_cmds[];
20 
21 #define for_each_cmd(cmd) \
22 	for (cmd = data_cmds; cmd && cmd->name; cmd++)
23 
24 static const char * const data_subcommands[] = { "convert", NULL };
25 
26 static const char *data_usage[] = {
27 	"perf data convert [<options>]",
28 	NULL
29 };
30 
31 static const char *to_json;
32 static const char *to_ctf;
33 static struct perf_data_convert_opts opts = {
34 	.force = false,
35 	.all = false,
36 	.time_str = NULL,
37 };
38 
39 static const struct option data_options[] = {
40 		OPT_INCR('v', "verbose", &verbose, "be more verbose"),
41 		OPT_STRING('i', "input", &input_name, "file", "input file name"),
42 		OPT_STRING(0, "to-json", &to_json, NULL, "Convert to JSON format"),
43 		OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"),
44 		OPT_BOOLEAN(0, "tod", &opts.tod, "Convert time to wall clock time"),
45 		OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
46 		OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
47 		OPT_STRING(0, "time", &opts.time_str, "str",
48 			   "Time span of interest (start,stop)"),
49 		OPT_END()
50 	};
51 
52 static int cmd_data_convert(int argc, const char **argv)
53 {
54 
55 	argc = parse_options(argc, argv, data_options,
56 			     data_usage, 0);
57 	if (argc) {
58 		usage_with_options(data_usage, data_options);
59 		return -1;
60 	}
61 
62 	if (to_json && to_ctf) {
63 		pr_err("You cannot specify both --to-ctf and --to-json.\n");
64 		return -1;
65 	}
66 	if (!to_json && !to_ctf) {
67 		pr_err("You must specify one of --to-ctf or --to-json.\n");
68 		return -1;
69 	}
70 
71 	if (to_json)
72 		return bt_convert__perf2json(input_name, to_json, &opts);
73 
74 	if (to_ctf) {
75 #if defined(HAVE_BABELTRACE2_CTF_WRITER_SUPPORT) && defined(HAVE_LIBTRACEEVENT)
76 		return bt_convert__perf2ctf(input_name, to_ctf, &opts);
77 #else
78 		pr_err("The babeltrace2 ctf support is not compiled in. Ensure you have both\n"
79 			"libbabeltrace2-dev[el] and libtraceevent-dev[el] installed or set\n"
80 			"PKG_CONFIG_PATH to find a local installation of those libraries.\n");
81 		return -1;
82 #endif
83 	}
84 
85 	return 0;
86 }
87 
88 static struct data_cmd data_cmds[] = {
89 	{ "convert", "converts data file between formats", cmd_data_convert },
90 	{ .name = NULL, },
91 };
92 
93 int cmd_data(int argc, const char **argv)
94 {
95 	struct data_cmd *cmd;
96 	const char *cmdstr;
97 
98 	argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage,
99 			     PARSE_OPT_STOP_AT_NON_OPTION);
100 
101 	if (!argc) {
102 		usage_with_options(data_usage, data_options);
103 		return -1;
104 	}
105 
106 	cmdstr = argv[0];
107 
108 	for_each_cmd(cmd) {
109 		if (strcmp(cmd->name, cmdstr))
110 			continue;
111 
112 		return cmd->fn(argc, argv);
113 	}
114 
115 	pr_err("Unknown command: %s\n", cmdstr);
116 	usage_with_options(data_usage, data_options);
117 	return -1;
118 }
119