1 #include <errno.h>
2 #include <string.h>
3 #include <regex.h>
4 
5 #include "../../perf.h"
6 #include "../../util/util.h"
7 #include "../../util/perf_regs.h"
8 #include "../../util/debug.h"
9 
10 const struct sample_reg sample_reg_masks[] = {
11 	SMPL_REG(r0, PERF_REG_POWERPC_R0),
12 	SMPL_REG(r1, PERF_REG_POWERPC_R1),
13 	SMPL_REG(r2, PERF_REG_POWERPC_R2),
14 	SMPL_REG(r3, PERF_REG_POWERPC_R3),
15 	SMPL_REG(r4, PERF_REG_POWERPC_R4),
16 	SMPL_REG(r5, PERF_REG_POWERPC_R5),
17 	SMPL_REG(r6, PERF_REG_POWERPC_R6),
18 	SMPL_REG(r7, PERF_REG_POWERPC_R7),
19 	SMPL_REG(r8, PERF_REG_POWERPC_R8),
20 	SMPL_REG(r9, PERF_REG_POWERPC_R9),
21 	SMPL_REG(r10, PERF_REG_POWERPC_R10),
22 	SMPL_REG(r11, PERF_REG_POWERPC_R11),
23 	SMPL_REG(r12, PERF_REG_POWERPC_R12),
24 	SMPL_REG(r13, PERF_REG_POWERPC_R13),
25 	SMPL_REG(r14, PERF_REG_POWERPC_R14),
26 	SMPL_REG(r15, PERF_REG_POWERPC_R15),
27 	SMPL_REG(r16, PERF_REG_POWERPC_R16),
28 	SMPL_REG(r17, PERF_REG_POWERPC_R17),
29 	SMPL_REG(r18, PERF_REG_POWERPC_R18),
30 	SMPL_REG(r19, PERF_REG_POWERPC_R19),
31 	SMPL_REG(r20, PERF_REG_POWERPC_R20),
32 	SMPL_REG(r21, PERF_REG_POWERPC_R21),
33 	SMPL_REG(r22, PERF_REG_POWERPC_R22),
34 	SMPL_REG(r23, PERF_REG_POWERPC_R23),
35 	SMPL_REG(r24, PERF_REG_POWERPC_R24),
36 	SMPL_REG(r25, PERF_REG_POWERPC_R25),
37 	SMPL_REG(r26, PERF_REG_POWERPC_R26),
38 	SMPL_REG(r27, PERF_REG_POWERPC_R27),
39 	SMPL_REG(r28, PERF_REG_POWERPC_R28),
40 	SMPL_REG(r29, PERF_REG_POWERPC_R29),
41 	SMPL_REG(r30, PERF_REG_POWERPC_R30),
42 	SMPL_REG(r31, PERF_REG_POWERPC_R31),
43 	SMPL_REG(nip, PERF_REG_POWERPC_NIP),
44 	SMPL_REG(msr, PERF_REG_POWERPC_MSR),
45 	SMPL_REG(orig_r3, PERF_REG_POWERPC_ORIG_R3),
46 	SMPL_REG(ctr, PERF_REG_POWERPC_CTR),
47 	SMPL_REG(link, PERF_REG_POWERPC_LINK),
48 	SMPL_REG(xer, PERF_REG_POWERPC_XER),
49 	SMPL_REG(ccr, PERF_REG_POWERPC_CCR),
50 	SMPL_REG(softe, PERF_REG_POWERPC_SOFTE),
51 	SMPL_REG(trap, PERF_REG_POWERPC_TRAP),
52 	SMPL_REG(dar, PERF_REG_POWERPC_DAR),
53 	SMPL_REG(dsisr, PERF_REG_POWERPC_DSISR),
54 	SMPL_REG_END
55 };
56 
57 /* REG or %rREG */
58 #define SDT_OP_REGEX1  "^(%r)?([1-2]?[0-9]|3[0-1])$"
59 
60 /* -NUM(REG) or NUM(REG) or -NUM(%rREG) or NUM(%rREG) */
61 #define SDT_OP_REGEX2  "^(\\-)?([0-9]+)\\((%r)?([1-2]?[0-9]|3[0-1])\\)$"
62 
63 static regex_t sdt_op_regex1, sdt_op_regex2;
64 
65 static int sdt_init_op_regex(void)
66 {
67 	static int initialized;
68 	int ret = 0;
69 
70 	if (initialized)
71 		return 0;
72 
73 	ret = regcomp(&sdt_op_regex1, SDT_OP_REGEX1, REG_EXTENDED);
74 	if (ret)
75 		goto error;
76 
77 	ret = regcomp(&sdt_op_regex2, SDT_OP_REGEX2, REG_EXTENDED);
78 	if (ret)
79 		goto free_regex1;
80 
81 	initialized = 1;
82 	return 0;
83 
84 free_regex1:
85 	regfree(&sdt_op_regex1);
86 error:
87 	pr_debug4("Regex compilation error.\n");
88 	return ret;
89 }
90 
91 /*
92  * Parse OP and convert it into uprobe format, which is, +/-NUM(%gprREG).
93  * Possible variants of OP are:
94  *	Format		Example
95  *	-------------------------
96  *	NUM(REG)	48(18)
97  *	-NUM(REG)	-48(18)
98  *	NUM(%rREG)	48(%r18)
99  *	-NUM(%rREG)	-48(%r18)
100  *	REG		18
101  *	%rREG		%r18
102  *	iNUM		i0
103  *	i-NUM		i-1
104  *
105  * SDT marker arguments on Powerpc uses %rREG form with -mregnames flag
106  * and REG form with -mno-regnames. Here REG is general purpose register,
107  * which is in 0 to 31 range.
108  */
109 int arch_sdt_arg_parse_op(char *old_op, char **new_op)
110 {
111 	int ret, new_len;
112 	regmatch_t rm[5];
113 	char prefix;
114 
115 	/* Constant argument. Uprobe does not support it */
116 	if (old_op[0] == 'i') {
117 		pr_debug4("Skipping unsupported SDT argument: %s\n", old_op);
118 		return SDT_ARG_SKIP;
119 	}
120 
121 	ret = sdt_init_op_regex();
122 	if (ret < 0)
123 		return ret;
124 
125 	if (!regexec(&sdt_op_regex1, old_op, 3, rm, 0)) {
126 		/* REG or %rREG --> %gprREG */
127 
128 		new_len = 5;	/* % g p r NULL */
129 		new_len += (int)(rm[2].rm_eo - rm[2].rm_so);
130 
131 		*new_op = zalloc(new_len);
132 		if (!*new_op)
133 			return -ENOMEM;
134 
135 		scnprintf(*new_op, new_len, "%%gpr%.*s",
136 			(int)(rm[2].rm_eo - rm[2].rm_so), old_op + rm[2].rm_so);
137 	} else if (!regexec(&sdt_op_regex2, old_op, 5, rm, 0)) {
138 		/*
139 		 * -NUM(REG) or NUM(REG) or -NUM(%rREG) or NUM(%rREG) -->
140 		 *	+/-NUM(%gprREG)
141 		 */
142 		prefix = (rm[1].rm_so == -1) ? '+' : '-';
143 
144 		new_len = 8;	/* +/- ( % g p r ) NULL */
145 		new_len += (int)(rm[2].rm_eo - rm[2].rm_so);
146 		new_len += (int)(rm[4].rm_eo - rm[4].rm_so);
147 
148 		*new_op = zalloc(new_len);
149 		if (!*new_op)
150 			return -ENOMEM;
151 
152 		scnprintf(*new_op, new_len, "%c%.*s(%%gpr%.*s)", prefix,
153 			(int)(rm[2].rm_eo - rm[2].rm_so), old_op + rm[2].rm_so,
154 			(int)(rm[4].rm_eo - rm[4].rm_so), old_op + rm[4].rm_so);
155 	} else {
156 		pr_debug4("Skipping unsupported SDT argument: %s\n", old_op);
157 		return SDT_ARG_SKIP;
158 	}
159 
160 	return SDT_ARG_VALID;
161 }
162