parse-branch-options.c 2.0 KB

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