builtin-data.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. typedef int (*data_cmd_fn_t)(int argc, const char **argv);
  11. struct data_cmd {
  12. const char *name;
  13. const char *summary;
  14. data_cmd_fn_t fn;
  15. };
  16. static struct data_cmd data_cmds[];
  17. #define for_each_cmd(cmd) \
  18. for (cmd = data_cmds; cmd && cmd->name; cmd++)
  19. static const char * const data_subcommands[] = { "convert", NULL };
  20. static const char *data_usage[] = {
  21. "perf data convert [<options>]",
  22. NULL
  23. };
  24. const char *to_json;
  25. const char *to_ctf;
  26. struct perf_data_convert_opts opts = {
  27. .force = false,
  28. .all = false,
  29. .time_str = NULL,
  30. };
  31. const struct option data_options[] = {
  32. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  33. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  34. OPT_STRING(0, "to-json", &to_json, NULL, "Convert to JSON format"),
  35. #ifdef HAVE_LIBBABELTRACE_SUPPORT
  36. OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"),
  37. OPT_BOOLEAN(0, "tod", &opts.tod, "Convert time to wall clock time"),
  38. #endif
  39. OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
  40. OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
  41. OPT_STRING(0, "time", &opts.time_str, "str",
  42. "Time span of interest (start,stop)"),
  43. OPT_END()
  44. };
  45. static int cmd_data_convert(int argc, const char **argv)
  46. {
  47. argc = parse_options(argc, argv, data_options,
  48. data_usage, 0);
  49. if (argc) {
  50. usage_with_options(data_usage, data_options);
  51. return -1;
  52. }
  53. if (to_json && to_ctf) {
  54. pr_err("You cannot specify both --to-ctf and --to-json.\n");
  55. return -1;
  56. }
  57. #ifdef HAVE_LIBBABELTRACE_SUPPORT
  58. if (!to_json && !to_ctf) {
  59. pr_err("You must specify one of --to-ctf or --to-json.\n");
  60. return -1;
  61. }
  62. #else
  63. if (!to_json) {
  64. pr_err("You must specify --to-json.\n");
  65. return -1;
  66. }
  67. #endif
  68. if (to_json)
  69. return bt_convert__perf2json(input_name, to_json, &opts);
  70. if (to_ctf) {
  71. #if defined(HAVE_LIBBABELTRACE_SUPPORT) && defined(HAVE_LIBTRACEEVENT)
  72. return bt_convert__perf2ctf(input_name, to_ctf, &opts);
  73. #else
  74. pr_err("The libbabeltrace support is not compiled in. perf should be "
  75. "compiled with environment variables LIBBABELTRACE=1 and "
  76. "LIBBABELTRACE_DIR=/path/to/libbabeltrace/.\n"
  77. "Check also if libbtraceevent devel files are available.\n");
  78. return -1;
  79. #endif
  80. }
  81. return 0;
  82. }
  83. static struct data_cmd data_cmds[] = {
  84. { "convert", "converts data file between formats", cmd_data_convert },
  85. { .name = NULL, },
  86. };
  87. int cmd_data(int argc, const char **argv)
  88. {
  89. struct data_cmd *cmd;
  90. const char *cmdstr;
  91. argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage,
  92. PARSE_OPT_STOP_AT_NON_OPTION);
  93. if (!argc) {
  94. usage_with_options(data_usage, data_options);
  95. return -1;
  96. }
  97. cmdstr = argv[0];
  98. for_each_cmd(cmd) {
  99. if (strcmp(cmd->name, cmdstr))
  100. continue;
  101. return cmd->fn(argc, argv);
  102. }
  103. pr_err("Unknown command: %s\n", cmdstr);
  104. usage_with_options(data_usage, data_options);
  105. return -1;
  106. }