blob: a4a100425b3a29bcb11765e833d9fb9e2d66da38 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
David Brazdil0f672f62019-12-10 10:32:29 +00002#include <stdbool.h>
3#include <stdlib.h>
4#include <stdint.h>
5#include <string.h>
6#include <stdio.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007#include "util/debug.h"
8#include <subcmd/parse-options.h>
David Brazdil0f672f62019-12-10 10:32:29 +00009#include "util/perf_regs.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000010#include "util/parse-regs-options.h"
11
David Brazdil0f672f62019-12-10 10:32:29 +000012static int
13__parse_regs(const struct option *opt, const char *str, int unset, bool intr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014{
15 uint64_t *mode = (uint64_t *)opt->value;
Olivier Deprez157378f2022-04-04 15:47:50 +020016 const struct sample_reg *r = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017 char *s, *os = NULL, *p;
18 int ret = -1;
David Brazdil0f672f62019-12-10 10:32:29 +000019 uint64_t mask;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020
21 if (unset)
22 return 0;
23
24 /*
25 * cannot set it twice
26 */
27 if (*mode)
28 return -1;
29
David Brazdil0f672f62019-12-10 10:32:29 +000030 if (intr)
31 mask = arch__intr_reg_mask();
32 else
33 mask = arch__user_reg_mask();
34
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035 /* str may be NULL in case no arg is passed to -I */
36 if (str) {
37 /* because str is read-only */
38 s = os = strdup(str);
39 if (!s)
40 return -1;
41
42 for (;;) {
43 p = strchr(s, ',');
44 if (p)
45 *p = '\0';
46
47 if (!strcmp(s, "?")) {
48 fprintf(stderr, "available registers: ");
Olivier Deprez157378f2022-04-04 15:47:50 +020049#ifdef HAVE_PERF_REGS_SUPPORT
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050 for (r = sample_reg_masks; r->name; r++) {
David Brazdil0f672f62019-12-10 10:32:29 +000051 if (r->mask & mask)
52 fprintf(stderr, "%s ", r->name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053 }
Olivier Deprez157378f2022-04-04 15:47:50 +020054#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055 fputc('\n', stderr);
56 /* just printing available regs */
Olivier Deprez0e641232021-09-23 10:07:05 +020057 goto error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000058 }
Olivier Deprez157378f2022-04-04 15:47:50 +020059#ifdef HAVE_PERF_REGS_SUPPORT
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060 for (r = sample_reg_masks; r->name; r++) {
David Brazdil0f672f62019-12-10 10:32:29 +000061 if ((r->mask & mask) && !strcasecmp(s, r->name))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062 break;
63 }
Olivier Deprez157378f2022-04-04 15:47:50 +020064#endif
65 if (!r || !r->name) {
David Brazdil0f672f62019-12-10 10:32:29 +000066 ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
67 s, intr ? "-I" : "--user-regs=");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000068 goto error;
69 }
70
71 *mode |= r->mask;
72
73 if (!p)
74 break;
75
76 s = p + 1;
77 }
78 }
79 ret = 0;
80
81 /* default to all possible regs */
82 if (*mode == 0)
David Brazdil0f672f62019-12-10 10:32:29 +000083 *mode = mask;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084error:
85 free(os);
86 return ret;
87}
David Brazdil0f672f62019-12-10 10:32:29 +000088
89int
90parse_user_regs(const struct option *opt, const char *str, int unset)
91{
92 return __parse_regs(opt, str, unset, false);
93}
94
95int
96parse_intr_regs(const struct option *opt, const char *str, int unset)
97{
98 return __parse_regs(opt, str, unset, true);
99}