perf data: Add perf data to CTF conversion support
[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_usage[] = {
26         "perf data [<common options>] <command> [<options>]",
27         NULL
28 };
29
30 static void print_usage(void)
31 {
32         struct data_cmd *cmd;
33
34         printf("Usage:\n");
35         printf("\t%s\n\n", data_usage[0]);
36         printf("\tAvailable commands:\n");
37
38         for_each_cmd(cmd) {
39                 printf("\t %s\t- %s\n", cmd->name, cmd->summary);
40         }
41
42         printf("\n");
43 }
44
45 static const char * const data_convert_usage[] = {
46         "perf data convert [<options>]",
47         NULL
48 };
49
50 static int cmd_data_convert(int argc, const char **argv,
51                             const char *prefix __maybe_unused)
52 {
53         const char *to_ctf     = NULL;
54         const struct option options[] = {
55                 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
56                 OPT_STRING('i', "input", &input_name, "file", "input file name"),
57 #ifdef HAVE_LIBBABELTRACE_SUPPORT
58                 OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"),
59 #endif
60                 OPT_END()
61         };
62
63 #ifndef HAVE_LIBBABELTRACE_SUPPORT
64         pr_err("No conversion support compiled in.\n");
65         return -1;
66 #endif
67
68         argc = parse_options(argc, argv, options,
69                              data_convert_usage, 0);
70         if (argc) {
71                 usage_with_options(data_convert_usage, options);
72                 return -1;
73         }
74
75         if (to_ctf) {
76 #ifdef HAVE_LIBBABELTRACE_SUPPORT
77                 return bt_convert__perf2ctf(input_name, to_ctf);
78 #else
79                 pr_err("The libbabeltrace support is not compiled in.\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         { NULL },
90 };
91
92 int cmd_data(int argc, const char **argv, const char *prefix)
93 {
94         struct data_cmd *cmd;
95         const char *cmdstr;
96
97         /* No command specified. */
98         if (argc < 2)
99                 goto usage;
100
101         argc = parse_options(argc, argv, data_options, data_usage,
102                              PARSE_OPT_STOP_AT_NON_OPTION);
103         if (argc < 1)
104                 goto usage;
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, prefix);
113         }
114
115         pr_err("Unknown command: %s\n", cmdstr);
116 usage:
117         print_usage();
118         return -1;
119 }