blob: 7ad6e17ac6b362cadb02e453d36ce5bb288ff2e1 [file] [log] [blame]
Jiri Olsa2245bf12015-02-20 23:16:59 +01001#include <linux/compiler.h>
2#include "builtin.h"
3#include "perf.h"
4#include "debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06005#include <subcmd/parse-options.h>
Wang Nan3275f682016-06-24 11:22:07 +00006#include "data-convert.h"
Jiri Olsaedbe9812015-02-20 23:17:00 +01007#include "data-convert-bt.h"
Jiri Olsa2245bf12015-02-20 23:16:59 +01008
9typedef int (*data_cmd_fn_t)(int argc, const char **argv, const char *prefix);
10
11struct data_cmd {
12 const char *name;
13 const char *summary;
14 data_cmd_fn_t fn;
15};
16
17static struct data_cmd data_cmds[];
18
19#define for_each_cmd(cmd) \
20 for (cmd = data_cmds; cmd && cmd->name; cmd++)
21
22static const struct option data_options[] = {
23 OPT_END()
24};
25
Yunlong Song01b71602015-03-18 21:35:52 +080026static const char * const data_subcommands[] = { "convert", NULL };
27
28static const char *data_usage[] = {
Jiri Olsa2245bf12015-02-20 23:16:59 +010029 "perf data [<common options>] <command> [<options>]",
30 NULL
31};
32
33static void print_usage(void)
34{
35 struct data_cmd *cmd;
36
37 printf("Usage:\n");
38 printf("\t%s\n\n", data_usage[0]);
39 printf("\tAvailable commands:\n");
40
41 for_each_cmd(cmd) {
42 printf("\t %s\t- %s\n", cmd->name, cmd->summary);
43 }
44
45 printf("\n");
46}
47
Jiri Olsaedbe9812015-02-20 23:17:00 +010048static const char * const data_convert_usage[] = {
49 "perf data convert [<options>]",
50 NULL
51};
52
53static int cmd_data_convert(int argc, const char **argv,
54 const char *prefix __maybe_unused)
55{
56 const char *to_ctf = NULL;
Wang Nan3275f682016-06-24 11:22:07 +000057 struct perf_data_convert_opts opts = {
58 .force = false,
Wang Nanf02a6482016-06-24 11:22:08 +000059 .all = false,
Wang Nan3275f682016-06-24 11:22:07 +000060 };
Jiri Olsaedbe9812015-02-20 23:17:00 +010061 const struct option options[] = {
62 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
63 OPT_STRING('i', "input", &input_name, "file", "input file name"),
64#ifdef HAVE_LIBBABELTRACE_SUPPORT
65 OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"),
66#endif
Wang Nan3275f682016-06-24 11:22:07 +000067 OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
Wang Nan9e1a7ea2016-06-24 11:22:11 +000068 OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
Jiri Olsaedbe9812015-02-20 23:17:00 +010069 OPT_END()
70 };
71
72#ifndef HAVE_LIBBABELTRACE_SUPPORT
73 pr_err("No conversion support compiled in.\n");
74 return -1;
75#endif
76
77 argc = parse_options(argc, argv, options,
78 data_convert_usage, 0);
79 if (argc) {
80 usage_with_options(data_convert_usage, options);
81 return -1;
82 }
83
84 if (to_ctf) {
85#ifdef HAVE_LIBBABELTRACE_SUPPORT
Wang Nan3275f682016-06-24 11:22:07 +000086 return bt_convert__perf2ctf(input_name, to_ctf, &opts);
Jiri Olsaedbe9812015-02-20 23:17:00 +010087#else
88 pr_err("The libbabeltrace support is not compiled in.\n");
89 return -1;
90#endif
91 }
92
93 return 0;
94}
95
Jiri Olsa2245bf12015-02-20 23:16:59 +010096static struct data_cmd data_cmds[] = {
Jiri Olsaedbe9812015-02-20 23:17:00 +010097 { "convert", "converts data file between formats", cmd_data_convert },
Yunlong Song1f924c22015-02-27 19:53:46 +080098 { .name = NULL, },
Jiri Olsa2245bf12015-02-20 23:16:59 +010099};
100
101int cmd_data(int argc, const char **argv, const char *prefix)
102{
103 struct data_cmd *cmd;
104 const char *cmdstr;
105
106 /* No command specified. */
107 if (argc < 2)
108 goto usage;
109
Yunlong Song01b71602015-03-18 21:35:52 +0800110 argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage,
Jiri Olsa2245bf12015-02-20 23:16:59 +0100111 PARSE_OPT_STOP_AT_NON_OPTION);
112 if (argc < 1)
113 goto usage;
114
115 cmdstr = argv[0];
116
117 for_each_cmd(cmd) {
118 if (strcmp(cmd->name, cmdstr))
119 continue;
120
121 return cmd->fn(argc, argv, prefix);
122 }
123
124 pr_err("Unknown command: %s\n", cmdstr);
125usage:
126 print_usage();
127 return -1;
128}