perf tools: Add the bash completion for listing subsubcommands of perf data
[firefly-linux-kernel-4.4.55.git] / tools / perf / builtin-data.c
1 #include <linux/compiler.h>
2 #include "builtin.h"
3 #include "perf.h"
4 #include "debug.h"
5 #include "parse-options.h"
6 #include "data-convert-bt.h"
7
8 typedef int (*data_cmd_fn_t)(int argc, const char **argv, const char *prefix);
9
10 struct data_cmd {
11         const char      *name;
12         const char      *summary;
13         data_cmd_fn_t   fn;
14 };
15
16 static struct data_cmd data_cmds[];
17
18 #define for_each_cmd(cmd) \
19         for (cmd = data_cmds; cmd && cmd->name; cmd++)
20
21 static const struct option data_options[] = {
22         OPT_END()
23 };
24
25 static const char * const data_subcommands[] = { "convert", NULL };
26
27 static const char *data_usage[] = {
28         "perf data [<common options>] <command> [<options>]",
29         NULL
30 };
31
32 static void print_usage(void)
33 {
34         struct data_cmd *cmd;
35
36         printf("Usage:\n");
37         printf("\t%s\n\n", data_usage[0]);
38         printf("\tAvailable commands:\n");
39
40         for_each_cmd(cmd) {
41                 printf("\t %s\t- %s\n", cmd->name, cmd->summary);
42         }
43
44         printf("\n");
45 }
46
47 static const char * const data_convert_usage[] = {
48         "perf data convert [<options>]",
49         NULL
50 };
51
52 static int cmd_data_convert(int argc, const char **argv,
53                             const char *prefix __maybe_unused)
54 {
55         const char *to_ctf     = NULL;
56         const struct option options[] = {
57                 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
58                 OPT_STRING('i', "input", &input_name, "file", "input file name"),
59 #ifdef HAVE_LIBBABELTRACE_SUPPORT
60                 OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"),
61 #endif
62                 OPT_END()
63         };
64
65 #ifndef HAVE_LIBBABELTRACE_SUPPORT
66         pr_err("No conversion support compiled in.\n");
67         return -1;
68 #endif
69
70         argc = parse_options(argc, argv, options,
71                              data_convert_usage, 0);
72         if (argc) {
73                 usage_with_options(data_convert_usage, options);
74                 return -1;
75         }
76
77         if (to_ctf) {
78 #ifdef HAVE_LIBBABELTRACE_SUPPORT
79                 return bt_convert__perf2ctf(input_name, to_ctf);
80 #else
81                 pr_err("The libbabeltrace support is not compiled in.\n");
82                 return -1;
83 #endif
84         }
85
86         return 0;
87 }
88
89 static struct data_cmd data_cmds[] = {
90         { "convert", "converts data file between formats", cmd_data_convert },
91         { .name = NULL, },
92 };
93
94 int cmd_data(int argc, const char **argv, const char *prefix)
95 {
96         struct data_cmd *cmd;
97         const char *cmdstr;
98
99         /* No command specified. */
100         if (argc < 2)
101                 goto usage;
102
103         argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage,
104                              PARSE_OPT_STOP_AT_NON_OPTION);
105         if (argc < 1)
106                 goto usage;
107
108         cmdstr = argv[0];
109
110         for_each_cmd(cmd) {
111                 if (strcmp(cmd->name, cmdstr))
112                         continue;
113
114                 return cmd->fn(argc, argv, prefix);
115         }
116
117         pr_err("Unknown command: %s\n", cmdstr);
118 usage:
119         print_usage();
120         return -1;
121 }