builtin-data.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compiler.h>
  3. #include "builtin.h"
  4. #include "perf.h"
  5. #include "debug.h"
  6. #include <subcmd/parse-options.h>
  7. #include "data-convert.h"
  8. #include "data-convert-bt.h"
  9. typedef int (*data_cmd_fn_t)(int argc, const char **argv);
  10. struct data_cmd {
  11. const char *name;
  12. const char *summary;
  13. data_cmd_fn_t fn;
  14. };
  15. static struct data_cmd data_cmds[];
  16. #define for_each_cmd(cmd) \
  17. for (cmd = data_cmds; cmd && cmd->name; cmd++)
  18. static const struct option data_options[] = {
  19. OPT_END()
  20. };
  21. static const char * const data_subcommands[] = { "convert", NULL };
  22. static const char *data_usage[] = {
  23. "perf data [<common options>] <command> [<options>]",
  24. NULL
  25. };
  26. static void print_usage(void)
  27. {
  28. struct data_cmd *cmd;
  29. printf("Usage:\n");
  30. printf("\t%s\n\n", data_usage[0]);
  31. printf("\tAvailable commands:\n");
  32. for_each_cmd(cmd) {
  33. printf("\t %s\t- %s\n", cmd->name, cmd->summary);
  34. }
  35. printf("\n");
  36. }
  37. static const char * const data_convert_usage[] = {
  38. "perf data convert [<options>]",
  39. NULL
  40. };
  41. static int cmd_data_convert(int argc, const char **argv)
  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. perf should be compiled with environment variables LIBBABELTRACE=1 and LIBBABELTRACE_DIR=/path/to/libbabeltrace/\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)
  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);
  98. }
  99. pr_err("Unknown command: %s\n", cmdstr);
  100. usage:
  101. print_usage();
  102. return -1;
  103. }