Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/compiler.h>
0003 #include <stdio.h>
0004 #include <string.h>
0005 #include "builtin.h"
0006 #include "perf.h"
0007 #include "debug.h"
0008 #include <subcmd/parse-options.h>
0009 #include "data-convert.h"
0010 
0011 typedef int (*data_cmd_fn_t)(int argc, const char **argv);
0012 
0013 struct data_cmd {
0014     const char  *name;
0015     const char  *summary;
0016     data_cmd_fn_t   fn;
0017 };
0018 
0019 static struct data_cmd data_cmds[];
0020 
0021 #define for_each_cmd(cmd) \
0022     for (cmd = data_cmds; cmd && cmd->name; cmd++)
0023 
0024 static const char * const data_subcommands[] = { "convert", NULL };
0025 
0026 static const char *data_usage[] = {
0027     "perf data convert [<options>]",
0028     NULL
0029 };
0030 
0031 const char *to_json;
0032 const char *to_ctf;
0033 struct perf_data_convert_opts opts = {
0034     .force = false,
0035     .all = false,
0036 };
0037 
0038 const struct option data_options[] = {
0039         OPT_INCR('v', "verbose", &verbose, "be more verbose"),
0040         OPT_STRING('i', "input", &input_name, "file", "input file name"),
0041         OPT_STRING(0, "to-json", &to_json, NULL, "Convert to JSON format"),
0042 #ifdef HAVE_LIBBABELTRACE_SUPPORT
0043         OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"),
0044         OPT_BOOLEAN(0, "tod", &opts.tod, "Convert time to wall clock time"),
0045 #endif
0046         OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
0047         OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
0048         OPT_END()
0049     };
0050 
0051 static int cmd_data_convert(int argc, const char **argv)
0052 {
0053 
0054     argc = parse_options(argc, argv, data_options,
0055                  data_usage, 0);
0056     if (argc) {
0057         usage_with_options(data_usage, data_options);
0058         return -1;
0059     }
0060 
0061     if (to_json && to_ctf) {
0062         pr_err("You cannot specify both --to-ctf and --to-json.\n");
0063         return -1;
0064     }
0065 #ifdef HAVE_LIBBABELTRACE_SUPPORT
0066     if (!to_json && !to_ctf) {
0067         pr_err("You must specify one of --to-ctf or --to-json.\n");
0068         return -1;
0069     }
0070 #else
0071     if (!to_json) {
0072         pr_err("You must specify --to-json.\n");
0073     return -1;
0074 }
0075 #endif
0076 
0077     if (to_json)
0078         return bt_convert__perf2json(input_name, to_json, &opts);
0079 
0080     if (to_ctf) {
0081 #ifdef HAVE_LIBBABELTRACE_SUPPORT
0082         return bt_convert__perf2ctf(input_name, to_ctf, &opts);
0083 #else
0084         pr_err("The libbabeltrace support is not compiled in. perf should be "
0085                "compiled with environment variables LIBBABELTRACE=1 and "
0086                "LIBBABELTRACE_DIR=/path/to/libbabeltrace/\n");
0087         return -1;
0088 #endif
0089     }
0090 
0091     return 0;
0092 }
0093 
0094 static struct data_cmd data_cmds[] = {
0095     { "convert", "converts data file between formats", cmd_data_convert },
0096     { .name = NULL, },
0097 };
0098 
0099 int cmd_data(int argc, const char **argv)
0100 {
0101     struct data_cmd *cmd;
0102     const char *cmdstr;
0103 
0104     argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage,
0105                  PARSE_OPT_STOP_AT_NON_OPTION);
0106 
0107     if (!argc) {
0108         usage_with_options(data_usage, data_options);
0109         return -1;
0110     }
0111 
0112     cmdstr = argv[0];
0113 
0114     for_each_cmd(cmd) {
0115         if (strcmp(cmd->name, cmdstr))
0116             continue;
0117 
0118         return cmd->fn(argc, argv);
0119     }
0120 
0121     pr_err("Unknown command: %s\n", cmdstr);
0122     usage_with_options(data_usage, data_options);
0123     return -1;
0124 }