builtin-data.c 2.9 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. OPT_BOOLEAN(0, "tod", &opts.tod, "Convert time to wall clock time"),
  56. #endif
  57. OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"),
  58. OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"),
  59. OPT_END()
  60. };
  61. #ifndef HAVE_LIBBABELTRACE_SUPPORT
  62. pr_err("No conversion support compiled in. perf should be compiled with environment variables LIBBABELTRACE=1 and LIBBABELTRACE_DIR=/path/to/libbabeltrace/\n");
  63. return -1;
  64. #endif
  65. argc = parse_options(argc, argv, options,
  66. data_convert_usage, 0);
  67. if (argc) {
  68. usage_with_options(data_convert_usage, options);
  69. return -1;
  70. }
  71. if (to_ctf) {
  72. #ifdef HAVE_LIBBABELTRACE_SUPPORT
  73. return bt_convert__perf2ctf(input_name, to_ctf, &opts);
  74. #else
  75. pr_err("The libbabeltrace support is not compiled in.\n");
  76. return -1;
  77. #endif
  78. }
  79. return 0;
  80. }
  81. static struct data_cmd data_cmds[] = {
  82. { "convert", "converts data file between formats", cmd_data_convert },
  83. { .name = NULL, },
  84. };
  85. int cmd_data(int argc, const char **argv)
  86. {
  87. struct data_cmd *cmd;
  88. const char *cmdstr;
  89. /* No command specified. */
  90. if (argc < 2)
  91. goto usage;
  92. argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage,
  93. PARSE_OPT_STOP_AT_NON_OPTION);
  94. if (argc < 1)
  95. goto usage;
  96. cmdstr = argv[0];
  97. for_each_cmd(cmd) {
  98. if (strcmp(cmd->name, cmdstr))
  99. continue;
  100. return cmd->fn(argc, argv);
  101. }
  102. pr_err("Unknown command: %s\n", cmdstr);
  103. usage:
  104. print_usage();
  105. return -1;
  106. }