builtin-data.c 2.8 KB

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