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 const char *to_json;
32 const char *to_ctf;
33 struct perf_data_convert_opts opts = {
34 .force = false,
35 .all = false,
36 .time_str = NULL,
37 };
38
39 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 #ifdef HAVE_LIBBABELTRACE_SUPPORT
44 OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"),
45 OPT_BOOLEAN(0, "tod", &opts.tod, "Convert time to wall clock time"),
46 #endif
47 OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
48 OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
49 OPT_STRING(0, "time", &opts.time_str, "str",
50 "Time span of interest (start,stop)"),
51 OPT_END()
52 };
53
cmd_data_convert(int argc,const char ** argv)54 static int cmd_data_convert(int argc, const char **argv)
55 {
56
57 argc = parse_options(argc, argv, data_options,
58 data_usage, 0);
59 if (argc) {
60 usage_with_options(data_usage, data_options);
61 return -1;
62 }
63
64 if (to_json && to_ctf) {
65 pr_err("You cannot specify both --to-ctf and --to-json.\n");
66 return -1;
67 }
68 #ifdef HAVE_LIBBABELTRACE_SUPPORT
69 if (!to_json && !to_ctf) {
70 pr_err("You must specify one of --to-ctf or --to-json.\n");
71 return -1;
72 }
73 #else
74 if (!to_json) {
75 pr_err("You must specify --to-json.\n");
76 return -1;
77 }
78 #endif
79
80 if (to_json)
81 return bt_convert__perf2json(input_name, to_json, &opts);
82
83 if (to_ctf) {
84 #if defined(HAVE_LIBBABELTRACE_SUPPORT) && defined(HAVE_LIBTRACEEVENT)
85 return bt_convert__perf2ctf(input_name, to_ctf, &opts);
86 #else
87 pr_err("The libbabeltrace support is not compiled in. perf should be "
88 "compiled with environment variables LIBBABELTRACE=1 and "
89 "LIBBABELTRACE_DIR=/path/to/libbabeltrace/.\n"
90 "Check also if libbtraceevent devel files are available.\n");
91 return -1;
92 #endif
93 }
94
95 return 0;
96 }
97
98 static struct data_cmd data_cmds[] = {
99 { "convert", "converts data file between formats", cmd_data_convert },
100 { .name = NULL, },
101 };
102
cmd_data(int argc,const char ** argv)103 int cmd_data(int argc, const char **argv)
104 {
105 struct data_cmd *cmd;
106 const char *cmdstr;
107
108 argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage,
109 PARSE_OPT_STOP_AT_NON_OPTION);
110
111 if (!argc) {
112 usage_with_options(data_usage, data_options);
113 return -1;
114 }
115
116 cmdstr = argv[0];
117
118 for_each_cmd(cmd) {
119 if (strcmp(cmd->name, cmdstr))
120 continue;
121
122 return cmd->fn(argc, argv);
123 }
124
125 pr_err("Unknown command: %s\n", cmdstr);
126 usage_with_options(data_usage, data_options);
127 return -1;
128 }
129