builtin-data.c 2.6 KB

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