| 1 | |
| 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 <dwarf-regs.h>
|
| 9 | #include <subcmd/parse-options.h>
|
| 10 | #include "util/perf_regs.h"
|
| 11 | #include "util/parse-regs-options.h"
|
| 12 |
|
| 13 | static void list_perf_regs(FILE *fp, uint64_t mask)
|
| 14 | {
|
| 15 | const char *last_name = NULL;
|
| 16 |
|
| 17 | fprintf(fp, "available registers: ");
|
| 18 | for (int reg = 0; reg < 64; reg++) {
|
| 19 | const char *name;
|
| 20 |
|
| 21 | if (((1ULL << reg) & mask) == 0)
|
| 22 | continue;
|
| 23 |
|
| 24 | name = perf_reg_name(reg, EM_HOST, EF_HOST);
|
| 25 | if (name && (!last_name || strcmp(last_name, name)))
|
| 26 | fprintf(fp, "%s%s", reg > 0 ? " " : "", name);
|
| 27 | last_name = name;
|
| 28 | }
|
| 29 | fputc('\n', fp);
|
| 30 | }
|
| 31 |
|
| 32 | static uint64_t name_to_perf_reg_mask(const char *to_match, uint64_t mask)
|
| 33 | {
|
| 34 | uint64_t reg_mask = 0;
|
| 35 |
|
| 36 | for (int reg = 0; reg < 64; reg++) {
|
| 37 | const char *name;
|
| 38 |
|
| 39 | if (((1ULL << reg) & mask) == 0)
|
| 40 | continue;
|
| 41 |
|
| 42 | name = perf_reg_name(reg, EM_HOST, EF_HOST);
|
| 43 | if (!name)
|
| 44 | continue;
|
| 45 |
|
| 46 | if (!strcasecmp(to_match, name))
|
| 47 | reg_mask |= 1ULL << reg;
|
| 48 | }
|
| 49 | return reg_mask;
|
| 50 | }
|
| 51 |
|
| 52 | static int
|
| 53 | __parse_regs(const struct option *opt, const char *str, int unset, bool intr)
|
| 54 | {
|
| 55 | uint64_t *mode = (uint64_t *)opt->value;
|
| 56 | char *s, *os = NULL, *p;
|
| 57 | int ret = -1;
|
| 58 | uint64_t mask;
|
| 59 |
|
| 60 | if (unset)
|
| 61 | return 0;
|
| 62 |
|
| 63 | |
| 64 | * cannot set it twice
|
| 65 |
|
| 66 | if (*mode)
|
| 67 | return -1;
|
| 68 |
|
| 69 | mask = intr ? perf_intr_reg_mask(EM_HOST) : perf_user_reg_mask(EM_HOST);
|
| 70 |
|
| 71 |
|
| 72 | if (!str) {
|
| 73 | *mode = mask;
|
| 74 | return 0;
|
| 75 | }
|
| 76 |
|
| 77 |
|
| 78 | s = os = strdup(str);
|
| 79 | if (!s)
|
| 80 | return -1;
|
| 81 |
|
| 82 | for (;;) {
|
| 83 | uint64_t reg_mask;
|
| 84 |
|
| 85 | p = strchr(s, ',');
|
| 86 | if (p)
|
| 87 | *p = '\0';
|
| 88 |
|
| 89 | if (!strcmp(s, "?")) {
|
| 90 | list_perf_regs(stderr, mask);
|
| 91 | goto error;
|
| 92 | }
|
| 93 |
|
| 94 | reg_mask = name_to_perf_reg_mask(s, mask);
|
| 95 | if (reg_mask == 0) {
|
| 96 | ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
|
| 97 | s, intr ? "-I" : "--user-regs=");
|
| 98 | goto error;
|
| 99 | }
|
| 100 | *mode |= reg_mask;
|
| 101 |
|
| 102 | if (!p)
|
| 103 | break;
|
| 104 |
|
| 105 | s = p + 1;
|
| 106 | }
|
| 107 | ret = 0;
|
| 108 |
|
| 109 | error:
|
| 110 | free(os);
|
| 111 | return ret;
|
| 112 | }
|
| 113 |
|
| 114 | int
|
| 115 | parse_user_regs(const struct option *opt, const char *str, int unset)
|
| 116 | {
|
| 117 | return __parse_regs(opt, str, unset, false);
|
| 118 | }
|
| 119 |
|
| 120 | int
|
| 121 | parse_intr_regs(const struct option *opt, const char *str, int unset)
|
| 122 | {
|
| 123 | return __parse_regs(opt, str, unset, true);
|
| 124 | }
|