parse-regs-options.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include "util/debug.h"
  8. #include <subcmd/parse-options.h>
  9. #include "util/perf_regs.h"
  10. #include "util/parse-regs-options.h"
  11. static int
  12. __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
  13. {
  14. uint64_t *mode = (uint64_t *)opt->value;
  15. const struct sample_reg *r = NULL;
  16. char *s, *os = NULL, *p;
  17. int ret = -1;
  18. uint64_t mask;
  19. if (unset)
  20. return 0;
  21. /*
  22. * cannot set it twice
  23. */
  24. if (*mode)
  25. return -1;
  26. if (intr)
  27. mask = arch__intr_reg_mask();
  28. else
  29. mask = arch__user_reg_mask();
  30. /* str may be NULL in case no arg is passed to -I */
  31. if (str) {
  32. /* because str is read-only */
  33. s = os = strdup(str);
  34. if (!s)
  35. return -1;
  36. for (;;) {
  37. p = strchr(s, ',');
  38. if (p)
  39. *p = '\0';
  40. if (!strcmp(s, "?")) {
  41. fprintf(stderr, "available registers: ");
  42. #ifdef HAVE_PERF_REGS_SUPPORT
  43. for (r = sample_reg_masks; r->name; r++) {
  44. if (r->mask & mask)
  45. fprintf(stderr, "%s ", r->name);
  46. }
  47. #endif
  48. fputc('\n', stderr);
  49. /* just printing available regs */
  50. goto error;
  51. }
  52. #ifdef HAVE_PERF_REGS_SUPPORT
  53. for (r = sample_reg_masks; r->name; r++) {
  54. if ((r->mask & mask) && !strcasecmp(s, r->name))
  55. break;
  56. }
  57. #endif
  58. if (!r || !r->name) {
  59. ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
  60. s, intr ? "-I" : "--user-regs=");
  61. goto error;
  62. }
  63. *mode |= r->mask;
  64. if (!p)
  65. break;
  66. s = p + 1;
  67. }
  68. }
  69. ret = 0;
  70. /* default to all possible regs */
  71. if (*mode == 0)
  72. *mode = mask;
  73. error:
  74. free(os);
  75. return ret;
  76. }
  77. int
  78. parse_user_regs(const struct option *opt, const char *str, int unset)
  79. {
  80. return __parse_regs(opt, str, unset, false);
  81. }
  82. int
  83. parse_intr_regs(const struct option *opt, const char *str, int unset)
  84. {
  85. return __parse_regs(opt, str, unset, true);
  86. }