builtin-data.c 3.0 KB

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