Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/compiler.h>
3#include <stdio.h>
4#include <string.h>
5#include "builtin.h"
6#include "perf.h"
7#include "debug.h"
8#include <subcmd/parse-options.h>
9#include "data-convert.h"
10#include "data-convert-bt.h"
11
12typedef int (*data_cmd_fn_t)(int argc, const char **argv);
13
14struct data_cmd {
15 const char *name;
16 const char *summary;
17 data_cmd_fn_t fn;
18};
19
20static struct data_cmd data_cmds[];
21
22#define for_each_cmd(cmd) \
23 for (cmd = data_cmds; cmd && cmd->name; cmd++)
24
25static const struct option data_options[] = {
26 OPT_END()
27};
28
29static const char * const data_subcommands[] = { "convert", NULL };
30
31static const char *data_usage[] = {
32 "perf data [<common options>] <command> [<options>]",
33 NULL
34};
35
36static void print_usage(void)
37{
38 struct data_cmd *cmd;
39
40 printf("Usage:\n");
41 printf("\t%s\n\n", data_usage[0]);
42 printf("\tAvailable commands:\n");
43
44 for_each_cmd(cmd) {
45 printf("\t %s\t- %s\n", cmd->name, cmd->summary);
46 }
47
48 printf("\n");
49}
50
51static const char * const data_convert_usage[] = {
52 "perf data convert [<options>]",
53 NULL
54};
55
56static int cmd_data_convert(int argc, const char **argv)
57{
58 const char *to_ctf = NULL;
59 struct perf_data_convert_opts opts = {
60 .force = false,
61 .all = false,
62 };
63 const struct option options[] = {
64 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
65 OPT_STRING('i', "input", &input_name, "file", "input file name"),
66#ifdef HAVE_LIBBABELTRACE_SUPPORT
67 OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"),
68#endif
69 OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
70 OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
71 OPT_END()
72 };
73
74#ifndef HAVE_LIBBABELTRACE_SUPPORT
75 pr_err("No conversion support compiled in. perf should be compiled with environment variables LIBBABELTRACE=1 and LIBBABELTRACE_DIR=/path/to/libbabeltrace/\n");
76 return -1;
77#endif
78
79 argc = parse_options(argc, argv, options,
80 data_convert_usage, 0);
81 if (argc) {
82 usage_with_options(data_convert_usage, options);
83 return -1;
84 }
85
86 if (to_ctf) {
87#ifdef HAVE_LIBBABELTRACE_SUPPORT
88 return bt_convert__perf2ctf(input_name, to_ctf, &opts);
89#else
90 pr_err("The libbabeltrace support is not compiled in.\n");
91 return -1;
92#endif
93 }
94
95 return 0;
96}
97
98static struct data_cmd data_cmds[] = {
99 { "convert", "converts data file between formats", cmd_data_convert },
100 { .name = NULL, },
101};
102
103int cmd_data(int argc, const char **argv)
104{
105 struct data_cmd *cmd;
106 const char *cmdstr;
107
108 /* No command specified. */
109 if (argc < 2)
110 goto usage;
111
112 argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage,
113 PARSE_OPT_STOP_AT_NON_OPTION);
114 if (argc < 1)
115 goto usage;
116
117 cmdstr = argv[0];
118
119 for_each_cmd(cmd) {
120 if (strcmp(cmd->name, cmdstr))
121 continue;
122
123 return cmd->fn(argc, argv);
124 }
125
126 pr_err("Unknown command: %s\n", cmdstr);
127usage:
128 print_usage();
129 return -1;
130}
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/compiler.h>
3#include <stdio.h>
4#include <string.h>
5#include "builtin.h"
6#include "debug.h"
7#include <subcmd/parse-options.h>
8#include "data-convert.h"
9#include "util/util.h"
10
11typedef int (*data_cmd_fn_t)(int argc, const char **argv);
12
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
24static const char * const data_subcommands[] = { "convert", NULL };
25
26static const char *data_usage[] = {
27 "perf data convert [<options>]",
28 NULL
29};
30
31const 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[] = {
39 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
40 OPT_STRING('i', "input", &input_name, "file", "input file name"),
41 OPT_STRING(0, "to-json", &to_json, NULL, "Convert to JSON format"),
42#ifdef HAVE_LIBBABELTRACE_SUPPORT
43 OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"),
44 OPT_BOOLEAN(0, "tod", &opts.tod, "Convert time to wall clock time"),
45#endif
46 OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
47 OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
48 OPT_END()
49 };
50
51static int cmd_data_convert(int argc, const char **argv)
52{
53
54 argc = parse_options(argc, argv, data_options,
55 data_usage, 0);
56 if (argc) {
57 usage_with_options(data_usage, data_options);
58 return -1;
59 }
60
61 if (to_json && to_ctf) {
62 pr_err("You cannot specify both --to-ctf and --to-json.\n");
63 return -1;
64 }
65#ifdef HAVE_LIBBABELTRACE_SUPPORT
66 if (!to_json && !to_ctf) {
67 pr_err("You must specify one of --to-ctf or --to-json.\n");
68 return -1;
69 }
70#else
71 if (!to_json) {
72 pr_err("You must specify --to-json.\n");
73 return -1;
74}
75#endif
76
77 if (to_json)
78 return bt_convert__perf2json(input_name, to_json, &opts);
79
80 if (to_ctf) {
81#if defined(HAVE_LIBBABELTRACE_SUPPORT) && defined(HAVE_LIBTRACEEVENT)
82 return bt_convert__perf2ctf(input_name, to_ctf, &opts);
83#else
84 pr_err("The libbabeltrace support is not compiled in. perf should be "
85 "compiled with environment variables LIBBABELTRACE=1 and "
86 "LIBBABELTRACE_DIR=/path/to/libbabeltrace/.\n"
87 "Check also if libbtraceevent devel files are available.\n");
88 return -1;
89#endif
90 }
91
92 return 0;
93}
94
95static struct data_cmd data_cmds[] = {
96 { "convert", "converts data file between formats", cmd_data_convert },
97 { .name = NULL, },
98};
99
100int cmd_data(int argc, const char **argv)
101{
102 struct data_cmd *cmd;
103 const char *cmdstr;
104
105 argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage,
106 PARSE_OPT_STOP_AT_NON_OPTION);
107
108 if (!argc) {
109 usage_with_options(data_usage, data_options);
110 return -1;
111 }
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);
120 }
121
122 pr_err("Unknown command: %s\n", cmdstr);
123 usage_with_options(data_usage, data_options);
124 return -1;
125}