Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 1 | #include <linux/compiler.h> |
| 2 | #include "builtin.h" |
| 3 | #include "perf.h" |
| 4 | #include "debug.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 5 | #include <subcmd/parse-options.h> |
Wang Nan | 3275f68 | 2016-06-24 11:22:07 +0000 | [diff] [blame^] | 6 | #include "data-convert.h" |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 7 | #include "data-convert-bt.h" |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 8 | |
| 9 | typedef int (*data_cmd_fn_t)(int argc, const char **argv, const char *prefix); |
| 10 | |
| 11 | struct data_cmd { |
| 12 | const char *name; |
| 13 | const char *summary; |
| 14 | data_cmd_fn_t fn; |
| 15 | }; |
| 16 | |
| 17 | static struct data_cmd data_cmds[]; |
| 18 | |
| 19 | #define for_each_cmd(cmd) \ |
| 20 | for (cmd = data_cmds; cmd && cmd->name; cmd++) |
| 21 | |
| 22 | static const struct option data_options[] = { |
| 23 | OPT_END() |
| 24 | }; |
| 25 | |
Yunlong Song | 01b7160 | 2015-03-18 21:35:52 +0800 | [diff] [blame] | 26 | static const char * const data_subcommands[] = { "convert", NULL }; |
| 27 | |
| 28 | static const char *data_usage[] = { |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 29 | "perf data [<common options>] <command> [<options>]", |
| 30 | NULL |
| 31 | }; |
| 32 | |
| 33 | static 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 Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 48 | static const char * const data_convert_usage[] = { |
| 49 | "perf data convert [<options>]", |
| 50 | NULL |
| 51 | }; |
| 52 | |
| 53 | static int cmd_data_convert(int argc, const char **argv, |
| 54 | const char *prefix __maybe_unused) |
| 55 | { |
| 56 | const char *to_ctf = NULL; |
Wang Nan | 3275f68 | 2016-06-24 11:22:07 +0000 | [diff] [blame^] | 57 | struct perf_data_convert_opts opts = { |
| 58 | .force = false, |
| 59 | }; |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 60 | const struct option options[] = { |
| 61 | OPT_INCR('v', "verbose", &verbose, "be more verbose"), |
| 62 | OPT_STRING('i', "input", &input_name, "file", "input file name"), |
| 63 | #ifdef HAVE_LIBBABELTRACE_SUPPORT |
| 64 | OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"), |
| 65 | #endif |
Wang Nan | 3275f68 | 2016-06-24 11:22:07 +0000 | [diff] [blame^] | 66 | OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"), |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 67 | OPT_END() |
| 68 | }; |
| 69 | |
| 70 | #ifndef HAVE_LIBBABELTRACE_SUPPORT |
| 71 | pr_err("No conversion support compiled in.\n"); |
| 72 | return -1; |
| 73 | #endif |
| 74 | |
| 75 | argc = parse_options(argc, argv, options, |
| 76 | data_convert_usage, 0); |
| 77 | if (argc) { |
| 78 | usage_with_options(data_convert_usage, options); |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | if (to_ctf) { |
| 83 | #ifdef HAVE_LIBBABELTRACE_SUPPORT |
Wang Nan | 3275f68 | 2016-06-24 11:22:07 +0000 | [diff] [blame^] | 84 | return bt_convert__perf2ctf(input_name, to_ctf, &opts); |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 85 | #else |
| 86 | pr_err("The libbabeltrace support is not compiled in.\n"); |
| 87 | return -1; |
| 88 | #endif |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 94 | static struct data_cmd data_cmds[] = { |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 95 | { "convert", "converts data file between formats", cmd_data_convert }, |
Yunlong Song | 1f924c2 | 2015-02-27 19:53:46 +0800 | [diff] [blame] | 96 | { .name = NULL, }, |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | int cmd_data(int argc, const char **argv, const char *prefix) |
| 100 | { |
| 101 | struct data_cmd *cmd; |
| 102 | const char *cmdstr; |
| 103 | |
| 104 | /* No command specified. */ |
| 105 | if (argc < 2) |
| 106 | goto usage; |
| 107 | |
Yunlong Song | 01b7160 | 2015-03-18 21:35:52 +0800 | [diff] [blame] | 108 | argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage, |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 109 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 110 | if (argc < 1) |
| 111 | goto usage; |
| 112 | |
| 113 | cmdstr = argv[0]; |
| 114 | |
| 115 | for_each_cmd(cmd) { |
| 116 | if (strcmp(cmd->name, cmdstr)) |
| 117 | continue; |
| 118 | |
| 119 | return cmd->fn(argc, argv, prefix); |
| 120 | } |
| 121 | |
| 122 | pr_err("Unknown command: %s\n", cmdstr); |
| 123 | usage: |
| 124 | print_usage(); |
| 125 | return -1; |
| 126 | } |