xref: /linux/tools/perf/util/parse-regs-options.c (revision 7ce6dfc603ed01044ebe58472a584d9995281ca2)
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 <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 	/* str may be NULL in case no arg is passed to -I */
70 	if (!str)
71 		return -1;
72 
73 	mask = intr ? arch__intr_reg_mask() : arch__user_reg_mask();
74 
75 	/* because str is read-only */
76 	s = os = strdup(str);
77 	if (!s)
78 		return -1;
79 
80 	for (;;) {
81 		uint64_t reg_mask;
82 
83 		p = strchr(s, ',');
84 		if (p)
85 			*p = '\0';
86 
87 		if (!strcmp(s, "?")) {
88 			list_perf_regs(stderr, mask);
89 			goto error;
90 		}
91 
92 		reg_mask = name_to_perf_reg_mask(s, mask);
93 		if (reg_mask == 0) {
94 			ui__warning("Unknown register \"%s\", check man page or run \"perf record %s?\"\n",
95 				s, intr ? "-I" : "--user-regs=");
96 			goto error;
97 		}
98 		*mode |= reg_mask;
99 
100 		if (!p)
101 			break;
102 
103 		s = p + 1;
104 	}
105 	ret = 0;
106 
107 	/* default to all possible regs */
108 	if (*mode == 0)
109 		*mode = mask;
110 error:
111 	free(os);
112 	return ret;
113 }
114 
115 int
116 parse_user_regs(const struct option *opt, const char *str, int unset)
117 {
118 	return __parse_regs(opt, str, unset, false);
119 }
120 
121 int
122 parse_intr_regs(const struct option *opt, const char *str, int unset)
123 {
124 	return __parse_regs(opt, str, unset, true);
125 }
126