blob: b7f9dc85a4071615a818f5869baa0e4f513ecb04 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jiri Olsa2245bf12015-02-20 23:16:59 +01002#include <linux/compiler.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -03003#include <stdio.h>
4#include <string.h>
Jiri Olsa2245bf12015-02-20 23:16:59 +01005#include "builtin.h"
6#include "perf.h"
7#include "debug.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06008#include <subcmd/parse-options.h>
Wang Nan3275f682016-06-24 11:22:07 +00009#include "data-convert.h"
Jiri Olsa2245bf12015-02-20 23:16:59 +010010
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -030011typedef int (*data_cmd_fn_t)(int argc, const char **argv);
Jiri Olsa2245bf12015-02-20 23:16:59 +010012
13struct data_cmd {
14 const char *name;
15 const char *summary;
16 data_cmd_fn_t fn;
17};
18
19static struct data_cmd data_cmds[];
20
21#define for_each_cmd(cmd) \
22 for (cmd = data_cmds; cmd && cmd->name; cmd++)
23
Yunlong Song01b71602015-03-18 21:35:52 +080024static const char * const data_subcommands[] = { "convert", NULL };
25
26static const char *data_usage[] = {
Jiri Olsaedbe9812015-02-20 23:17:00 +010027 "perf data convert [<options>]",
28 NULL
29};
30
Joshua Martinez760f5e72021-08-24 13:58:29 -070031const char *to_json;
32const char *to_ctf;
33struct perf_data_convert_opts opts = {
34 .force = false,
35 .all = false,
36};
37
38const struct option data_options[] = {
Jiri Olsaedbe9812015-02-20 23:17:00 +010039 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
40 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Nicholas Fraserd0713d42021-04-26 10:47:16 -040041 OPT_STRING(0, "to-json", &to_json, NULL, "Convert to JSON format"),
Jiri Olsaedbe9812015-02-20 23:17:00 +010042#ifdef HAVE_LIBBABELTRACE_SUPPORT
43 OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"),
Jiri Olsa88371c52020-08-05 11:34:42 +020044 OPT_BOOLEAN(0, "tod", &opts.tod, "Convert time to wall clock time"),
Jiri Olsaedbe9812015-02-20 23:17:00 +010045#endif
Wang Nan3275f682016-06-24 11:22:07 +000046 OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
Wang Nan9e1a7ea2016-06-24 11:22:11 +000047 OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
Jiri Olsaedbe9812015-02-20 23:17:00 +010048 OPT_END()
49 };
50
Joshua Martinez760f5e72021-08-24 13:58:29 -070051static int cmd_data_convert(int argc, const char **argv)
52{
53
54 argc = parse_options(argc, argv, data_options,
55 data_usage, 0);
Jiri Olsaedbe9812015-02-20 23:17:00 +010056 if (argc) {
Joshua Martinez760f5e72021-08-24 13:58:29 -070057 usage_with_options(data_usage, data_options);
Jiri Olsaedbe9812015-02-20 23:17:00 +010058 return -1;
59 }
60
Nicholas Fraserd0713d42021-04-26 10:47:16 -040061 if (to_json && to_ctf) {
62 pr_err("You cannot specify both --to-ctf and --to-json.\n");
63 return -1;
64 }
65 if (!to_json && !to_ctf) {
66 pr_err("You must specify one of --to-ctf or --to-json.\n");
67 return -1;
68 }
69
70 if (to_json)
71 return bt_convert__perf2json(input_name, to_json, &opts);
72
Jiri Olsaedbe9812015-02-20 23:17:00 +010073 if (to_ctf) {
74#ifdef HAVE_LIBBABELTRACE_SUPPORT
Wang Nan3275f682016-06-24 11:22:07 +000075 return bt_convert__perf2ctf(input_name, to_ctf, &opts);
Jiri Olsaedbe9812015-02-20 23:17:00 +010076#else
Nicholas Fraserd0713d42021-04-26 10:47:16 -040077 pr_err("The libbabeltrace support is not compiled in. perf should be "
78 "compiled with environment variables LIBBABELTRACE=1 and "
79 "LIBBABELTRACE_DIR=/path/to/libbabeltrace/\n");
Jiri Olsaedbe9812015-02-20 23:17:00 +010080 return -1;
81#endif
82 }
83
84 return 0;
85}
86
Jiri Olsa2245bf12015-02-20 23:16:59 +010087static struct data_cmd data_cmds[] = {
Jiri Olsaedbe9812015-02-20 23:17:00 +010088 { "convert", "converts data file between formats", cmd_data_convert },
Yunlong Song1f924c22015-02-27 19:53:46 +080089 { .name = NULL, },
Jiri Olsa2245bf12015-02-20 23:16:59 +010090};
91
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -030092int cmd_data(int argc, const char **argv)
Jiri Olsa2245bf12015-02-20 23:16:59 +010093{
94 struct data_cmd *cmd;
95 const char *cmdstr;
96
Yunlong Song01b71602015-03-18 21:35:52 +080097 argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage,
Jiri Olsa2245bf12015-02-20 23:16:59 +010098 PARSE_OPT_STOP_AT_NON_OPTION);
Joshua Martinez760f5e72021-08-24 13:58:29 -070099
100 if (!argc) {
101 usage_with_options(data_usage, data_options);
102 return -1;
103 }
Jiri Olsa2245bf12015-02-20 23:16:59 +0100104
105 cmdstr = argv[0];
106
107 for_each_cmd(cmd) {
108 if (strcmp(cmd->name, cmdstr))
109 continue;
110
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300111 return cmd->fn(argc, argv);
Jiri Olsa2245bf12015-02-20 23:16:59 +0100112 }
113
114 pr_err("Unknown command: %s\n", cmdstr);
Joshua Martinez760f5e72021-08-24 13:58:29 -0700115 usage_with_options(data_usage, data_options);
Jiri Olsa2245bf12015-02-20 23:16:59 +0100116 return -1;
117}