builtin-data.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <linux/compiler.h>
  2. #include "builtin.h"
  3. #include "perf.h"
  4. #include "debug.h"
  5. #include <subcmd/parse-options.h>
  6. #include "data-convert.h"
  7. #include "data-convert-bt.h"
  8. typedef int (*data_cmd_fn_t)(int argc, const char **argv, const char *prefix);
  9. struct data_cmd {
  10. const char *name;
  11. const char *summary;
  12. data_cmd_fn_t fn;
  13. };
  14. static struct data_cmd data_cmds[];
  15. #define for_each_cmd(cmd) \
  16. for (cmd = data_cmds; cmd && cmd->name; cmd++)
  17. static const struct option data_options[] = {
  18. OPT_END()
  19. };
  20. static const char * const data_subcommands[] = { "convert", NULL };
  21. static const char *data_usage[] = {
  22. "perf data [<common options>] <command> [<options>]",
  23. NULL
  24. };
  25. static void print_usage(void)
  26. {
  27. struct data_cmd *cmd;
  28. printf("Usage:\n");
  29. printf("\t%s\n\n", data_usage[0]);
  30. printf("\tAvailable commands:\n");
  31. for_each_cmd(cmd) {
  32. printf("\t %s\t- %s\n", cmd->name, cmd->summary);
  33. }
  34. printf("\n");
  35. }
  36. static const char * const data_convert_usage[] = {
  37. "perf data convert [<options>]",
  38. NULL
  39. };
  40. static int cmd_data_convert(int argc, const char **argv,
  41. const char *prefix __maybe_unused)
  42. {
  43. const char *to_ctf = NULL;
  44. struct perf_data_convert_opts opts = {
  45. .force = false,
  46. .all = false,
  47. };
  48. const struct option options[] = {
  49. OPT_INCR('v', "verbose", &verbose, "be more verbose"),
  50. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  51. #ifdef HAVE_LIBBABELTRACE_SUPPORT
  52. OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"),
  53. #endif
  54. OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
  55. OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
  56. OPT_END()
  57. };
  58. #ifndef HAVE_LIBBABELTRACE_SUPPORT
  59. pr_err("No conversion support compiled in.\n");
  60. return -1;
  61. #endif
  62. argc = parse_options(argc, argv, options,
  63. data_convert_usage, 0);
  64. if (argc) {
  65. usage_with_options(data_convert_usage, options);
  66. return -1;
  67. }
  68. if (to_ctf) {
  69. #ifdef HAVE_LIBBABELTRACE_SUPPORT
  70. return bt_convert__perf2ctf(input_name, to_ctf, &opts);
  71. #else
  72. pr_err("The libbabeltrace support is not compiled in.\n");
  73. return -1;
  74. #endif
  75. }
  76. return 0;
  77. }
  78. static struct data_cmd data_cmds[] = {
  79. { "convert", "converts data file between formats", cmd_data_convert },
  80. { .name = NULL, },
  81. };
  82. int cmd_data(int argc, const char **argv, const char *prefix)
  83. {
  84. struct data_cmd *cmd;
  85. const char *cmdstr;
  86. /* No command specified. */
  87. if (argc < 2)
  88. goto usage;
  89. argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage,
  90. PARSE_OPT_STOP_AT_NON_OPTION);
  91. if (argc < 1)
  92. goto usage;
  93. cmdstr = argv[0];
  94. for_each_cmd(cmd) {
  95. if (strcmp(cmd->name, cmdstr))
  96. continue;
  97. return cmd->fn(argc, argv, prefix);
  98. }
  99. pr_err("Unknown command: %s\n", cmdstr);
  100. usage:
  101. print_usage();
  102. return -1;
  103. }