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