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 "perf.h"
7 #include "debug.h"
8 #include <subcmd/parse-options.h>
9 #include "data-convert.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 };
37
38 const struct option data_options[] = {
39 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
40 OPT_STRING('i', "input", &input_name, "file", "input file name"),
41 OPT_STRING(0, "to-json", &to_json, NULL, "Convert to JSON format"),
42 #ifdef HAVE_LIBBABELTRACE_SUPPORT
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 #endif
46 OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
47 OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
48 OPT_END()
49 };
50
cmd_data_convert(int argc,const char ** argv)51 static int cmd_data_convert(int argc, const char **argv)
52 {
53
54 argc = parse_options(argc, argv, data_options,
55 data_usage, 0);
56 if (argc) {
57 usage_with_options(data_usage, data_options);
58 return -1;
59 }
60
61 if (to_json && to_ctf) {
62 pr_err("You cannot specify both --to-ctf and --to-json.\n");
63 return -1;
64 }
65 if (!to_json && !to_ctf) {
66 pr_err("You must specify one of --to-ctf or --to-json.\n");
67 return -1;
68 }
69
70 if (to_json)
71 return bt_convert__perf2json(input_name, to_json, &opts);
72
73 if (to_ctf) {
74 #ifdef HAVE_LIBBABELTRACE_SUPPORT
75 return bt_convert__perf2ctf(input_name, to_ctf, &opts);
76 #else
77 pr_err("The libbabeltrace support is not compiled in. perf should be "
78 "compiled with environment variables LIBBABELTRACE=1 and "
79 "LIBBABELTRACE_DIR=/path/to/libbabeltrace/\n");
80 return -1;
81 #endif
82 }
83
84 return 0;
85 }
86
87 static struct data_cmd data_cmds[] = {
88 { "convert", "converts data file between formats", cmd_data_convert },
89 { .name = NULL, },
90 };
91
cmd_data(int argc,const char ** argv)92 int cmd_data(int argc, const char **argv)
93 {
94 struct data_cmd *cmd;
95 const char *cmdstr;
96
97 argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage,
98 PARSE_OPT_STOP_AT_NON_OPTION);
99
100 if (!argc) {
101 usage_with_options(data_usage, data_options);
102 return -1;
103 }
104
105 cmdstr = argv[0];
106
107 for_each_cmd(cmd) {
108 if (strcmp(cmd->name, cmdstr))
109 continue;
110
111 return cmd->fn(argc, argv);
112 }
113
114 pr_err("Unknown command: %s\n", cmdstr);
115 usage_with_options(data_usage, data_options);
116 return -1;
117 }
118