parse-branch-options.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "perf.h"
  3. #include "util/util.h"
  4. #include "util/debug.h"
  5. #include <subcmd/parse-options.h>
  6. #include "util/parse-branch-options.h"
  7. #define BRANCH_OPT(n, m) \
  8. { .name = n, .mode = (m) }
  9. #define BRANCH_END { .name = NULL }
  10. struct branch_mode {
  11. const char *name;
  12. int mode;
  13. };
  14. static const struct branch_mode branch_modes[] = {
  15. BRANCH_OPT("u", PERF_SAMPLE_BRANCH_USER),
  16. BRANCH_OPT("k", PERF_SAMPLE_BRANCH_KERNEL),
  17. BRANCH_OPT("hv", PERF_SAMPLE_BRANCH_HV),
  18. BRANCH_OPT("any", PERF_SAMPLE_BRANCH_ANY),
  19. BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL),
  20. BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN),
  21. BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL),
  22. BRANCH_OPT("abort_tx", PERF_SAMPLE_BRANCH_ABORT_TX),
  23. BRANCH_OPT("in_tx", PERF_SAMPLE_BRANCH_IN_TX),
  24. BRANCH_OPT("no_tx", PERF_SAMPLE_BRANCH_NO_TX),
  25. BRANCH_OPT("cond", PERF_SAMPLE_BRANCH_COND),
  26. BRANCH_OPT("ind_jmp", PERF_SAMPLE_BRANCH_IND_JUMP),
  27. BRANCH_OPT("call", PERF_SAMPLE_BRANCH_CALL),
  28. BRANCH_OPT("save_type", PERF_SAMPLE_BRANCH_TYPE_SAVE),
  29. BRANCH_END
  30. };
  31. int parse_branch_str(const char *str, __u64 *mode)
  32. {
  33. #define ONLY_PLM \
  34. (PERF_SAMPLE_BRANCH_USER |\
  35. PERF_SAMPLE_BRANCH_KERNEL |\
  36. PERF_SAMPLE_BRANCH_HV)
  37. int ret = 0;
  38. char *p, *s;
  39. char *os = NULL;
  40. const struct branch_mode *br;
  41. if (str == NULL) {
  42. *mode = PERF_SAMPLE_BRANCH_ANY;
  43. return 0;
  44. }
  45. /* because str is read-only */
  46. s = os = strdup(str);
  47. if (!s)
  48. return -1;
  49. for (;;) {
  50. p = strchr(s, ',');
  51. if (p)
  52. *p = '\0';
  53. for (br = branch_modes; br->name; br++) {
  54. if (!strcasecmp(s, br->name))
  55. break;
  56. }
  57. if (!br->name) {
  58. ret = -1;
  59. pr_warning("unknown branch filter %s,"
  60. " check man page\n", s);
  61. goto error;
  62. }
  63. *mode |= br->mode;
  64. if (!p)
  65. break;
  66. s = p + 1;
  67. }
  68. /* default to any branch */
  69. if ((*mode & ~ONLY_PLM) == 0) {
  70. *mode = PERF_SAMPLE_BRANCH_ANY;
  71. }
  72. error:
  73. free(os);
  74. return ret;
  75. }
  76. int
  77. parse_branch_stack(const struct option *opt, const char *str, int unset)
  78. {
  79. __u64 *mode = (__u64 *)opt->value;
  80. if (unset)
  81. return 0;
  82. /*
  83. * cannot set it twice, -b + --branch-filter for instance
  84. */
  85. if (*mode)
  86. return -1;
  87. return parse_branch_str(str, mode);
  88. }