133965Sjdp// SPDX-License-Identifier: GPL-2.0
2218822Sdim#include <linux/compiler.h>
3218822Sdim#include <stdio.h>
478828Sobrien#include <string.h>
533965Sjdp#include "builtin.h"
633965Sjdp#include "debug.h"
733965Sjdp#include <subcmd/parse-options.h>
833965Sjdp#include "data-convert.h"
933965Sjdp#include "util/util.h"
1033965Sjdp
1133965Sjdptypedef int (*data_cmd_fn_t)(int argc, const char **argv);
1233965Sjdp
1333965Sjdpstruct data_cmd {
1433965Sjdp	const char	*name;
1533965Sjdp	const char	*summary;
1633965Sjdp	data_cmd_fn_t	fn;
1733965Sjdp};
1833965Sjdp
1933965Sjdpstatic struct data_cmd data_cmds[];
2033965Sjdp
2133965Sjdp#define for_each_cmd(cmd) \
2233965Sjdp	for (cmd = data_cmds; cmd && cmd->name; cmd++)
2333965Sjdp
24218822Sdimstatic const char * const data_subcommands[] = { "convert", NULL };
25218822Sdim
2633965Sjdpstatic const char *data_usage[] = {
2733965Sjdp	"perf data convert [<options>]",
2833965Sjdp	NULL
2933965Sjdp};
3033965Sjdp
3133965Sjdpconst char *to_json;
3233965Sjdpconst char *to_ctf;
3360484Sobrienstruct perf_data_convert_opts opts = {
3460484Sobrien	.force = false,
3533965Sjdp	.all = false,
3633965Sjdp};
3733965Sjdp
3833965Sjdpconst struct option data_options[] = {
3933965Sjdp		OPT_INCR('v', "verbose", &verbose, "be more verbose"),
4033965Sjdp		OPT_STRING('i', "input", &input_name, "file", "input file name"),
4189857Sobrien		OPT_STRING(0, "to-json", &to_json, NULL, "Convert to JSON format"),
4233965Sjdp#ifdef HAVE_LIBBABELTRACE_SUPPORT
4333965Sjdp		OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"),
4433965Sjdp		OPT_BOOLEAN(0, "tod", &opts.tod, "Convert time to wall clock time"),
4533965Sjdp#endif
4633965Sjdp		OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
4733965Sjdp		OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
4833965Sjdp		OPT_END()
4933965Sjdp	};
5033965Sjdp
5133965Sjdpstatic int cmd_data_convert(int argc, const char **argv)
5233965Sjdp{
5333965Sjdp
5433965Sjdp	argc = parse_options(argc, argv, data_options,
5533965Sjdp			     data_usage, 0);
5633965Sjdp	if (argc) {
5733965Sjdp		usage_with_options(data_usage, data_options);
5833965Sjdp		return -1;
5933965Sjdp	}
6033965Sjdp
6133965Sjdp	if (to_json && to_ctf) {
6233965Sjdp		pr_err("You cannot specify both --to-ctf and --to-json.\n");
6333965Sjdp		return -1;
6433965Sjdp	}
6533965Sjdp#ifdef HAVE_LIBBABELTRACE_SUPPORT
6633965Sjdp	if (!to_json && !to_ctf) {
6733965Sjdp		pr_err("You must specify one of --to-ctf or --to-json.\n");
6833965Sjdp		return -1;
6933965Sjdp	}
7033965Sjdp#else
7133965Sjdp	if (!to_json) {
7233965Sjdp		pr_err("You must specify --to-json.\n");
7333965Sjdp	return -1;
7433965Sjdp}
7533965Sjdp#endif
7633965Sjdp
7733965Sjdp	if (to_json)
7833965Sjdp		return bt_convert__perf2json(input_name, to_json, &opts);
7933965Sjdp
8033965Sjdp	if (to_ctf) {
8133965Sjdp#if defined(HAVE_LIBBABELTRACE_SUPPORT) && defined(HAVE_LIBTRACEEVENT)
8233965Sjdp		return bt_convert__perf2ctf(input_name, to_ctf, &opts);
8333965Sjdp#else
8433965Sjdp		pr_err("The libbabeltrace support is not compiled in. perf should be "
8533965Sjdp		       "compiled with environment variables LIBBABELTRACE=1 and "
8633965Sjdp		       "LIBBABELTRACE_DIR=/path/to/libbabeltrace/.\n"
8733965Sjdp		       "Check also if libbtraceevent devel files are available.\n");
8833965Sjdp		return -1;
8933965Sjdp#endif
9033965Sjdp	}
9133965Sjdp
9233965Sjdp	return 0;
9333965Sjdp}
9433965Sjdp
9533965Sjdpstatic struct data_cmd data_cmds[] = {
9633965Sjdp	{ "convert", "converts data file between formats", cmd_data_convert },
9733965Sjdp	{ .name = NULL, },
9833965Sjdp};
9933965Sjdp
10033965Sjdpint cmd_data(int argc, const char **argv)
10133965Sjdp{
10233965Sjdp	struct data_cmd *cmd;
10333965Sjdp	const char *cmdstr;
10433965Sjdp
10533965Sjdp	argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage,
10633965Sjdp			     PARSE_OPT_STOP_AT_NON_OPTION);
10733965Sjdp
10833965Sjdp	if (!argc) {
10933965Sjdp		usage_with_options(data_usage, data_options);
11033965Sjdp		return -1;
11133965Sjdp	}
11233965Sjdp
11333965Sjdp	cmdstr = argv[0];
11433965Sjdp
11533965Sjdp	for_each_cmd(cmd) {
11633965Sjdp		if (strcmp(cmd->name, cmdstr))
11733965Sjdp			continue;
11833965Sjdp
11933965Sjdp		return cmd->fn(argc, argv);
12033965Sjdp	}
12133965Sjdp
12233965Sjdp	pr_err("Unknown command: %s\n", cmdstr);
12333965Sjdp	usage_with_options(data_usage, data_options);
12433965Sjdp	return -1;
12533965Sjdp}
12633965Sjdp