parse-branch-options.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "perf.h"
  2. #include "util/util.h"
  3. #include "util/debug.h"
  4. #include "util/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_END
  27. };
  28. int
  29. parse_branch_stack(const struct option *opt, const char *str, int unset)
  30. {
  31. #define ONLY_PLM \
  32. (PERF_SAMPLE_BRANCH_USER |\
  33. PERF_SAMPLE_BRANCH_KERNEL |\
  34. PERF_SAMPLE_BRANCH_HV)
  35. uint64_t *mode = (uint64_t *)opt->value;
  36. const struct branch_mode *br;
  37. char *s, *os = NULL, *p;
  38. int ret = -1;
  39. if (unset)
  40. return 0;
  41. /*
  42. * cannot set it twice, -b + --branch-filter for instance
  43. */
  44. if (*mode)
  45. return -1;
  46. /* str may be NULL in case no arg is passed to -b */
  47. if (str) {
  48. /* because str is read-only */
  49. s = os = strdup(str);
  50. if (!s)
  51. return -1;
  52. for (;;) {
  53. p = strchr(s, ',');
  54. if (p)
  55. *p = '\0';
  56. for (br = branch_modes; br->name; br++) {
  57. if (!strcasecmp(s, br->name))
  58. break;
  59. }
  60. if (!br->name) {
  61. ui__warning("unknown branch filter %s,"
  62. " check man page\n", s);
  63. goto error;
  64. }
  65. *mode |= br->mode;
  66. if (!p)
  67. break;
  68. s = p + 1;
  69. }
  70. }
  71. ret = 0;
  72. /* default to any branch */
  73. if ((*mode & ~ONLY_PLM) == 0) {
  74. *mode = PERF_SAMPLE_BRANCH_ANY;
  75. }
  76. error:
  77. free(os);
  78. return ret;
  79. }