Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 2 | #include <linux/compiler.h> |
| 3 | #include "builtin.h" |
| 4 | #include "perf.h" |
| 5 | #include "debug.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 6 | #include <subcmd/parse-options.h> |
Wang Nan | 3275f68 | 2016-06-24 11:22:07 +0000 | [diff] [blame] | 7 | #include "data-convert.h" |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 8 | #include "data-convert-bt.h" |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 9 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 10 | typedef int (*data_cmd_fn_t)(int argc, const char **argv); |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 11 | |
| 12 | struct data_cmd { |
| 13 | const char *name; |
| 14 | const char *summary; |
| 15 | data_cmd_fn_t fn; |
| 16 | }; |
| 17 | |
| 18 | static struct data_cmd data_cmds[]; |
| 19 | |
| 20 | #define for_each_cmd(cmd) \ |
| 21 | for (cmd = data_cmds; cmd && cmd->name; cmd++) |
| 22 | |
| 23 | static const struct option data_options[] = { |
| 24 | OPT_END() |
| 25 | }; |
| 26 | |
Yunlong Song | 01b7160 | 2015-03-18 21:35:52 +0800 | [diff] [blame] | 27 | static const char * const data_subcommands[] = { "convert", NULL }; |
| 28 | |
| 29 | static const char *data_usage[] = { |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 30 | "perf data [<common options>] <command> [<options>]", |
| 31 | NULL |
| 32 | }; |
| 33 | |
| 34 | static void print_usage(void) |
| 35 | { |
| 36 | struct data_cmd *cmd; |
| 37 | |
| 38 | printf("Usage:\n"); |
| 39 | printf("\t%s\n\n", data_usage[0]); |
| 40 | printf("\tAvailable commands:\n"); |
| 41 | |
| 42 | for_each_cmd(cmd) { |
| 43 | printf("\t %s\t- %s\n", cmd->name, cmd->summary); |
| 44 | } |
| 45 | |
| 46 | printf("\n"); |
| 47 | } |
| 48 | |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 49 | static const char * const data_convert_usage[] = { |
| 50 | "perf data convert [<options>]", |
| 51 | NULL |
| 52 | }; |
| 53 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 54 | static int cmd_data_convert(int argc, const char **argv) |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 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, |
Wang Nan | f02a648 | 2016-06-24 11:22:08 +0000 | [diff] [blame] | 59 | .all = false, |
Wang Nan | 3275f68 | 2016-06-24 11:22:07 +0000 | [diff] [blame] | 60 | }; |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 61 | 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 Nan | 3275f68 | 2016-06-24 11:22:07 +0000 | [diff] [blame] | 67 | OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"), |
Wang Nan | 9e1a7ea | 2016-06-24 11:22:11 +0000 | [diff] [blame] | 68 | OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"), |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 69 | OPT_END() |
| 70 | }; |
| 71 | |
| 72 | #ifndef HAVE_LIBBABELTRACE_SUPPORT |
Geneviève Bastien | 6b7007a | 2017-07-27 14:12:05 -0400 | [diff] [blame] | 73 | pr_err("No conversion support compiled in. perf should be compiled with environment variables LIBBABELTRACE=1 and LIBBABELTRACE_DIR=/path/to/libbabeltrace/\n"); |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 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 Nan | 3275f68 | 2016-06-24 11:22:07 +0000 | [diff] [blame] | 86 | return bt_convert__perf2ctf(input_name, to_ctf, &opts); |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 87 | #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 Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 96 | static struct data_cmd data_cmds[] = { |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 97 | { "convert", "converts data file between formats", cmd_data_convert }, |
Yunlong Song | 1f924c2 | 2015-02-27 19:53:46 +0800 | [diff] [blame] | 98 | { .name = NULL, }, |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 99 | }; |
| 100 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 101 | int cmd_data(int argc, const char **argv) |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 102 | { |
| 103 | struct data_cmd *cmd; |
| 104 | const char *cmdstr; |
| 105 | |
| 106 | /* No command specified. */ |
| 107 | if (argc < 2) |
| 108 | goto usage; |
| 109 | |
Yunlong Song | 01b7160 | 2015-03-18 21:35:52 +0800 | [diff] [blame] | 110 | argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage, |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 111 | 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 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 121 | return cmd->fn(argc, argv); |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | pr_err("Unknown command: %s\n", cmdstr); |
| 125 | usage: |
| 126 | print_usage(); |
| 127 | return -1; |
| 128 | } |