parse-regs-options.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "perf.h"
  2. #include "util/util.h"
  3. #include "util/debug.h"
  4. #include <subcmd/parse-options.h>
  5. #include "util/parse-regs-options.h"
  6. int
  7. parse_regs(const struct option *opt, const char *str, int unset)
  8. {
  9. uint64_t *mode = (uint64_t *)opt->value;
  10. const struct sample_reg *r;
  11. char *s, *os = NULL, *p;
  12. int ret = -1;
  13. if (unset)
  14. return 0;
  15. /*
  16. * cannot set it twice
  17. */
  18. if (*mode)
  19. return -1;
  20. /* str may be NULL in case no arg is passed to -I */
  21. if (str) {
  22. /* because str is read-only */
  23. s = os = strdup(str);
  24. if (!s)
  25. return -1;
  26. for (;;) {
  27. p = strchr(s, ',');
  28. if (p)
  29. *p = '\0';
  30. if (!strcmp(s, "?")) {
  31. fprintf(stderr, "available registers: ");
  32. for (r = sample_reg_masks; r->name; r++) {
  33. fprintf(stderr, "%s ", r->name);
  34. }
  35. fputc('\n', stderr);
  36. /* just printing available regs */
  37. return -1;
  38. }
  39. for (r = sample_reg_masks; r->name; r++) {
  40. if (!strcasecmp(s, r->name))
  41. break;
  42. }
  43. if (!r->name) {
  44. ui__warning("unknown register %s,"
  45. " check man page\n", s);
  46. goto error;
  47. }
  48. *mode |= r->mask;
  49. if (!p)
  50. break;
  51. s = p + 1;
  52. }
  53. }
  54. ret = 0;
  55. /* default to all possible regs */
  56. if (*mode == 0)
  57. *mode = PERF_REGS_MASK;
  58. error:
  59. free(os);
  60. return ret;
  61. }