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> |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 6 | #include "data-convert-bt.h" |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 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 | |
Yunlong Song | 01b7160 | 2015-03-18 21:35:52 +0800 | [diff] [blame] | 25 | static const char * const data_subcommands[] = { "convert", NULL }; |
| 26 | |
| 27 | static const char *data_usage[] = { |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 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 | |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 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; |
Yunlong Song | bd05954 | 2015-04-02 21:47:19 +0800 | [diff] [blame] | 56 | bool force = false; |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 57 | const struct option options[] = { |
| 58 | OPT_INCR('v', "verbose", &verbose, "be more verbose"), |
| 59 | OPT_STRING('i', "input", &input_name, "file", "input file name"), |
| 60 | #ifdef HAVE_LIBBABELTRACE_SUPPORT |
| 61 | OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"), |
| 62 | #endif |
Yunlong Song | bd05954 | 2015-04-02 21:47:19 +0800 | [diff] [blame] | 63 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 64 | OPT_END() |
| 65 | }; |
| 66 | |
| 67 | #ifndef HAVE_LIBBABELTRACE_SUPPORT |
| 68 | pr_err("No conversion support compiled in.\n"); |
| 69 | return -1; |
| 70 | #endif |
| 71 | |
| 72 | argc = parse_options(argc, argv, options, |
| 73 | data_convert_usage, 0); |
| 74 | if (argc) { |
| 75 | usage_with_options(data_convert_usage, options); |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | if (to_ctf) { |
| 80 | #ifdef HAVE_LIBBABELTRACE_SUPPORT |
Yunlong Song | bd05954 | 2015-04-02 21:47:19 +0800 | [diff] [blame] | 81 | return bt_convert__perf2ctf(input_name, to_ctf, force); |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 82 | #else |
| 83 | pr_err("The libbabeltrace support is not compiled in.\n"); |
| 84 | return -1; |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 91 | static struct data_cmd data_cmds[] = { |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 92 | { "convert", "converts data file between formats", cmd_data_convert }, |
Yunlong Song | 1f924c2 | 2015-02-27 19:53:46 +0800 | [diff] [blame] | 93 | { .name = NULL, }, |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | int cmd_data(int argc, const char **argv, const char *prefix) |
| 97 | { |
| 98 | struct data_cmd *cmd; |
| 99 | const char *cmdstr; |
| 100 | |
| 101 | /* No command specified. */ |
| 102 | if (argc < 2) |
| 103 | goto usage; |
| 104 | |
Yunlong Song | 01b7160 | 2015-03-18 21:35:52 +0800 | [diff] [blame] | 105 | argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage, |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 106 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 107 | if (argc < 1) |
| 108 | goto usage; |
| 109 | |
| 110 | cmdstr = argv[0]; |
| 111 | |
| 112 | for_each_cmd(cmd) { |
| 113 | if (strcmp(cmd->name, cmdstr)) |
| 114 | continue; |
| 115 | |
| 116 | return cmd->fn(argc, argv, prefix); |
| 117 | } |
| 118 | |
| 119 | pr_err("Unknown command: %s\n", cmdstr); |
| 120 | usage: |
| 121 | print_usage(); |
| 122 | return -1; |
| 123 | } |