Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2245bf14 | 2 | #include <linux/compiler.h> |
8520a98d ACM |
3 | #include <stdio.h> |
4 | #include <string.h> | |
2245bf14 JO |
5 | #include "builtin.h" |
6 | #include "perf.h" | |
7 | #include "debug.h" | |
4b6ab94e | 8 | #include <subcmd/parse-options.h> |
3275f68e | 9 | #include "data-convert.h" |
edbe9817 | 10 | #include "data-convert-bt.h" |
2245bf14 | 11 | |
b0ad8ea6 | 12 | typedef int (*data_cmd_fn_t)(int argc, const char **argv); |
2245bf14 JO |
13 | |
14 | struct data_cmd { | |
15 | const char *name; | |
16 | const char *summary; | |
17 | data_cmd_fn_t fn; | |
18 | }; | |
19 | ||
20 | static struct data_cmd data_cmds[]; | |
21 | ||
22 | #define for_each_cmd(cmd) \ | |
23 | for (cmd = data_cmds; cmd && cmd->name; cmd++) | |
24 | ||
25 | static const struct option data_options[] = { | |
26 | OPT_END() | |
27 | }; | |
28 | ||
01b7160b YS |
29 | static const char * const data_subcommands[] = { "convert", NULL }; |
30 | ||
31 | static const char *data_usage[] = { | |
2245bf14 JO |
32 | "perf data [<common options>] <command> [<options>]", |
33 | NULL | |
34 | }; | |
35 | ||
36 | static void print_usage(void) | |
37 | { | |
38 | struct data_cmd *cmd; | |
39 | ||
40 | printf("Usage:\n"); | |
41 | printf("\t%s\n\n", data_usage[0]); | |
42 | printf("\tAvailable commands:\n"); | |
43 | ||
44 | for_each_cmd(cmd) { | |
45 | printf("\t %s\t- %s\n", cmd->name, cmd->summary); | |
46 | } | |
47 | ||
48 | printf("\n"); | |
49 | } | |
50 | ||
edbe9817 JO |
51 | static const char * const data_convert_usage[] = { |
52 | "perf data convert [<options>]", | |
53 | NULL | |
54 | }; | |
55 | ||
b0ad8ea6 | 56 | static int cmd_data_convert(int argc, const char **argv) |
edbe9817 JO |
57 | { |
58 | const char *to_ctf = NULL; | |
3275f68e WN |
59 | struct perf_data_convert_opts opts = { |
60 | .force = false, | |
f02a6489 | 61 | .all = false, |
3275f68e | 62 | }; |
edbe9817 JO |
63 | const struct option options[] = { |
64 | OPT_INCR('v', "verbose", &verbose, "be more verbose"), | |
65 | OPT_STRING('i', "input", &input_name, "file", "input file name"), | |
66 | #ifdef HAVE_LIBBABELTRACE_SUPPORT | |
67 | OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"), | |
68 | #endif | |
3275f68e | 69 | OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"), |
9e1a7ea1 | 70 | OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"), |
edbe9817 JO |
71 | OPT_END() |
72 | }; | |
73 | ||
74 | #ifndef HAVE_LIBBABELTRACE_SUPPORT | |
6b7007af | 75 | pr_err("No conversion support compiled in. perf should be compiled with environment variables LIBBABELTRACE=1 and LIBBABELTRACE_DIR=/path/to/libbabeltrace/\n"); |
edbe9817 JO |
76 | return -1; |
77 | #endif | |
78 | ||
79 | argc = parse_options(argc, argv, options, | |
80 | data_convert_usage, 0); | |
81 | if (argc) { | |
82 | usage_with_options(data_convert_usage, options); | |
83 | return -1; | |
84 | } | |
85 | ||
86 | if (to_ctf) { | |
87 | #ifdef HAVE_LIBBABELTRACE_SUPPORT | |
3275f68e | 88 | return bt_convert__perf2ctf(input_name, to_ctf, &opts); |
edbe9817 JO |
89 | #else |
90 | pr_err("The libbabeltrace support is not compiled in.\n"); | |
91 | return -1; | |
92 | #endif | |
93 | } | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
2245bf14 | 98 | static struct data_cmd data_cmds[] = { |
edbe9817 | 99 | { "convert", "converts data file between formats", cmd_data_convert }, |
1f924c29 | 100 | { .name = NULL, }, |
2245bf14 JO |
101 | }; |
102 | ||
b0ad8ea6 | 103 | int cmd_data(int argc, const char **argv) |
2245bf14 JO |
104 | { |
105 | struct data_cmd *cmd; | |
106 | const char *cmdstr; | |
107 | ||
108 | /* No command specified. */ | |
109 | if (argc < 2) | |
110 | goto usage; | |
111 | ||
01b7160b | 112 | argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage, |
2245bf14 JO |
113 | PARSE_OPT_STOP_AT_NON_OPTION); |
114 | if (argc < 1) | |
115 | goto usage; | |
116 | ||
117 | cmdstr = argv[0]; | |
118 | ||
119 | for_each_cmd(cmd) { | |
120 | if (strcmp(cmd->name, cmdstr)) | |
121 | continue; | |
122 | ||
b0ad8ea6 | 123 | return cmd->fn(argc, argv); |
2245bf14 JO |
124 | } |
125 | ||
126 | pr_err("Unknown command: %s\n", cmdstr); | |
127 | usage: | |
128 | print_usage(); | |
129 | return -1; | |
130 | } |