parse-branch-options.c 2.1 KB

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