blob: b8a928e925c70bf1a01f007b44f561f07ad74a44 [file] [log] [blame]
Steven Rostedt (VMware)bcea3f92018-08-16 11:23:53 -04001// SPDX-License-Identifier: GPL-2.0
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +05302/*
3 * Common code for probe-based Dynamic events.
4 *
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +05305 * This code was copied from kernel/trace/trace_kprobe.c written by
6 * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
7 *
8 * Updates to make this generic:
9 * Copyright (C) IBM Corporation, 2010-2011
10 * Author: Srikar Dronamraju
11 */
Masami Hiramatsu72576342017-02-07 20:21:28 +090012#define pr_fmt(fmt) "trace_probe: " fmt
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053013
14#include "trace_probe.h"
15
Masami Hiramatsuab105a42019-03-31 18:48:19 -050016#undef C
17#define C(a, b) b
18
19static const char *trace_probe_err_text[] = { ERRORS };
20
Valdis Klētnieks08416252019-03-12 04:52:58 -040021static const char *reserved_field_names[] = {
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053022 "common_type",
23 "common_flags",
24 "common_preempt_count",
25 "common_pid",
26 "common_tgid",
27 FIELD_STRING_IP,
28 FIELD_STRING_RETIP,
29 FIELD_STRING_FUNC,
30};
31
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053032/* Printing in basic type function template */
Masami Hiramatsu17ce3dc2016-08-18 17:57:50 +090033#define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \
Masami Hiramatsu56de7632018-04-25 21:16:36 +090034int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053035{ \
Masami Hiramatsu56de7632018-04-25 21:16:36 +090036 trace_seq_printf(s, fmt, *(type *)data); \
Steven Rostedt (Red Hat)d2b01912014-11-12 17:19:51 -050037 return !trace_seq_has_overflowed(s); \
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053038} \
Masami Hiramatsu7bfbc632018-04-25 21:17:34 +090039const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053040
Masami Hiramatsubdca79c22016-08-18 17:59:21 +090041DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u")
42DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
43DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
44DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
Masami Hiramatsu17ce3dc2016-08-18 17:57:50 +090045DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d")
46DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
47DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
48DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
49DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x")
50DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
51DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
52DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053053
Masami Hiramatsu60c2e0c2018-04-25 21:20:28 +090054int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
55{
56 trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
57 return !trace_seq_has_overflowed(s);
58}
59const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
60
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053061/* Print type function for string type */
Masami Hiramatsu56de7632018-04-25 21:16:36 +090062int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053063{
64 int len = *(u32 *)data >> 16;
65
66 if (!len)
Masami Hiramatsu56de7632018-04-25 21:16:36 +090067 trace_seq_puts(s, "(fault)");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053068 else
Masami Hiramatsu56de7632018-04-25 21:16:36 +090069 trace_seq_printf(s, "\"%s\"",
Steven Rostedt (Red Hat)d2b01912014-11-12 17:19:51 -050070 (const char *)get_loc_data(data, ent));
71 return !trace_seq_has_overflowed(s);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053072}
73
Namhyung Kimb26c74e2013-11-26 14:19:59 +090074const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053075
Masami Hiramatsuf451bc82018-04-25 21:18:32 +090076/* Fetch type information table */
77static const struct fetch_type probe_fetch_types[] = {
78 /* Special types */
79 __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1,
80 "__data_loc char[]"),
Masami Hiramatsu88903c42019-05-15 14:38:30 +090081 __ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1,
82 "__data_loc char[]"),
Masami Hiramatsuf451bc82018-04-25 21:18:32 +090083 /* Basic types */
84 ASSIGN_FETCH_TYPE(u8, u8, 0),
85 ASSIGN_FETCH_TYPE(u16, u16, 0),
86 ASSIGN_FETCH_TYPE(u32, u32, 0),
87 ASSIGN_FETCH_TYPE(u64, u64, 0),
88 ASSIGN_FETCH_TYPE(s8, u8, 1),
89 ASSIGN_FETCH_TYPE(s16, u16, 1),
90 ASSIGN_FETCH_TYPE(s32, u32, 1),
91 ASSIGN_FETCH_TYPE(s64, u64, 1),
92 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
93 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
94 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
95 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
Masami Hiramatsu60c2e0c2018-04-25 21:20:28 +090096 ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
Masami Hiramatsuf451bc82018-04-25 21:18:32 +090097
98 ASSIGN_FETCH_TYPE_END
99};
100
101static const struct fetch_type *find_fetch_type(const char *type)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530102{
103 int i;
104
105 if (!type)
106 type = DEFAULT_FETCH_TYPE_STR;
107
108 /* Special case: bitfield */
109 if (*type == 'b') {
110 unsigned long bs;
111
112 type = strchr(type, '/');
113 if (!type)
114 goto fail;
115
116 type++;
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200117 if (kstrtoul(type, 0, &bs))
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530118 goto fail;
119
120 switch (bs) {
121 case 8:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900122 return find_fetch_type("u8");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530123 case 16:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900124 return find_fetch_type("u16");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530125 case 32:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900126 return find_fetch_type("u32");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530127 case 64:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900128 return find_fetch_type("u64");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530129 default:
130 goto fail;
131 }
132 }
133
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900134 for (i = 0; probe_fetch_types[i].name; i++) {
135 if (strcmp(type, probe_fetch_types[i].name) == 0)
136 return &probe_fetch_types[i];
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900137 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530138
139fail:
140 return NULL;
141}
142
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500143static struct trace_probe_log trace_probe_log;
144
145void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
146{
147 trace_probe_log.subsystem = subsystem;
148 trace_probe_log.argc = argc;
149 trace_probe_log.argv = argv;
150 trace_probe_log.index = 0;
151}
152
153void trace_probe_log_clear(void)
154{
155 memset(&trace_probe_log, 0, sizeof(trace_probe_log));
156}
157
158void trace_probe_log_set_index(int index)
159{
160 trace_probe_log.index = index;
161}
162
163void __trace_probe_log_err(int offset, int err_type)
164{
165 char *command, *p;
166 int i, len = 0, pos = 0;
167
168 if (!trace_probe_log.argv)
169 return;
170
171 /* Recalcurate the length and allocate buffer */
172 for (i = 0; i < trace_probe_log.argc; i++) {
173 if (i == trace_probe_log.index)
174 pos = len;
175 len += strlen(trace_probe_log.argv[i]) + 1;
176 }
177 command = kzalloc(len, GFP_KERNEL);
178 if (!command)
179 return;
180
Masami Hiramatsud2aea952019-09-28 05:53:29 -0400181 if (trace_probe_log.index >= trace_probe_log.argc) {
182 /**
183 * Set the error position is next to the last arg + space.
184 * Note that len includes the terminal null and the cursor
185 * appaers at pos + 1.
186 */
187 pos = len;
188 offset = 0;
189 }
190
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500191 /* And make a command string from argv array */
192 p = command;
193 for (i = 0; i < trace_probe_log.argc; i++) {
194 len = strlen(trace_probe_log.argv[i]);
195 strcpy(p, trace_probe_log.argv[i]);
196 p[len] = ' ';
197 p += len + 1;
198 }
199 *(p - 1) = '\0';
200
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -0400201 tracing_log_err(NULL, trace_probe_log.subsystem, command,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500202 trace_probe_err_text, err_type, pos + offset);
203
204 kfree(command);
205}
206
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530207/* Split symbol and offset. */
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900208int traceprobe_split_symbol_offset(char *symbol, long *offset)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530209{
210 char *tmp;
211 int ret;
212
213 if (!offset)
214 return -EINVAL;
215
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900216 tmp = strpbrk(symbol, "+-");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530217 if (tmp) {
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900218 ret = kstrtol(tmp, 0, offset);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530219 if (ret)
220 return ret;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530221 *tmp = '\0';
222 } else
223 *offset = 0;
224
225 return 0;
226}
227
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900228/* @buf must has MAX_EVENT_NAME_LEN size */
229int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500230 char *buf, int offset)
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900231{
232 const char *slash, *event = *pevent;
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900233 int len;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900234
235 slash = strchr(event, '/');
236 if (slash) {
237 if (slash == event) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500238 trace_probe_log_err(offset, NO_GROUP_NAME);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900239 return -EINVAL;
240 }
241 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500242 trace_probe_log_err(offset, GROUP_TOO_LONG);
243 return -EINVAL;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900244 }
245 strlcpy(buf, event, slash - event + 1);
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900246 if (!is_good_name(buf)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500247 trace_probe_log_err(offset, BAD_GROUP_NAME);
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900248 return -EINVAL;
249 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900250 *pgroup = buf;
251 *pevent = slash + 1;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500252 offset += slash - event + 1;
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900253 event = *pevent;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900254 }
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900255 len = strlen(event);
256 if (len == 0) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500257 trace_probe_log_err(offset, NO_EVENT_NAME);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900258 return -EINVAL;
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900259 } else if (len > MAX_EVENT_NAME_LEN) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500260 trace_probe_log_err(offset, EVENT_TOO_LONG);
261 return -EINVAL;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900262 }
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900263 if (!is_good_name(event)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500264 trace_probe_log_err(offset, BAD_EVENT_NAME);
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900265 return -EINVAL;
266 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900267 return 0;
268}
269
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530270#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
271
272static int parse_probe_vars(char *arg, const struct fetch_type *t,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500273 struct fetch_insn *code, unsigned int flags, int offs)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530274{
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530275 unsigned long param;
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500276 int ret = 0;
277 int len;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530278
279 if (strcmp(arg, "retval") == 0) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500280 if (flags & TPARG_FL_RETURN) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900281 code->op = FETCH_OP_RETVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500282 } else {
283 trace_probe_log_err(offs, RETVAL_ON_PROBE);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530284 ret = -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500285 }
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500286 } else if ((len = str_has_prefix(arg, "stack"))) {
287 if (arg[len] == '\0') {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900288 code->op = FETCH_OP_STACKP;
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500289 } else if (isdigit(arg[len])) {
290 ret = kstrtoul(arg + len, 10, &param);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500291 if (ret) {
292 goto inval_var;
293 } else if ((flags & TPARG_FL_KERNEL) &&
294 param > PARAM_MAX_STACK) {
295 trace_probe_log_err(offs, BAD_STACK_NUM);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530296 ret = -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500297 } else {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900298 code->op = FETCH_OP_STACK;
299 code->param = (unsigned int)param;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530300 }
301 } else
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500302 goto inval_var;
Omar Sandoval35abb672016-06-08 18:38:02 -0700303 } else if (strcmp(arg, "comm") == 0) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900304 code->op = FETCH_OP_COMM;
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900305#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
306 } else if (((flags & TPARG_FL_MASK) ==
307 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500308 (len = str_has_prefix(arg, "arg"))) {
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500309 ret = kstrtoul(arg + len, 10, &param);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500310 if (ret) {
311 goto inval_var;
312 } else if (!param || param > PARAM_MAX_STACK) {
313 trace_probe_log_err(offs, BAD_ARG_NUM);
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900314 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500315 }
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900316 code->op = FETCH_OP_ARG;
317 code->param = (unsigned int)param - 1;
318#endif
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530319 } else
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500320 goto inval_var;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530321
322 return ret;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500323
324inval_var:
325 trace_probe_log_err(offs, BAD_VAR);
326 return -EINVAL;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530327}
328
Masami Hiramatsu6218bf92019-06-20 00:08:27 +0900329static int str_to_immediate(char *str, unsigned long *imm)
330{
331 if (isdigit(str[0]))
332 return kstrtoul(str, 0, imm);
333 else if (str[0] == '-')
334 return kstrtol(str, 0, (long *)imm);
335 else if (str[0] == '+')
336 return kstrtol(str + 1, 0, (long *)imm);
337 return -EINVAL;
338}
339
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900340static int __parse_imm_string(char *str, char **pbuf, int offs)
341{
342 size_t len = strlen(str);
343
344 if (str[len - 1] != '"') {
345 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
346 return -EINVAL;
347 }
348 *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
349 return 0;
350}
351
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530352/* Recursive argument parser */
Masami Hiramatsu53305922018-04-25 21:18:03 +0900353static int
354parse_probe_arg(char *arg, const struct fetch_type *type,
355 struct fetch_insn **pcode, struct fetch_insn *end,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500356 unsigned int flags, int offs)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530357{
Masami Hiramatsu53305922018-04-25 21:18:03 +0900358 struct fetch_insn *code = *pcode;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530359 unsigned long param;
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900360 int deref = FETCH_OP_DEREF;
Steven Rostedt (VMware)bf173ca2018-10-12 12:50:22 -0400361 long offset = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530362 char *tmp;
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900363 int ret = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530364
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530365 switch (arg[0]) {
366 case '$':
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500367 ret = parse_probe_vars(arg + 1, type, code, flags, offs);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530368 break;
369
370 case '%': /* named register */
371 ret = regs_query_register_offset(arg + 1);
372 if (ret >= 0) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900373 code->op = FETCH_OP_REG;
374 code->param = (unsigned int)ret;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530375 ret = 0;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500376 } else
377 trace_probe_log_err(offs, BAD_REG_NAME);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530378 break;
379
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900380 case '@': /* memory, file-offset or symbol */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530381 if (isdigit(arg[1])) {
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200382 ret = kstrtoul(arg + 1, 0, &param);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500383 if (ret) {
384 trace_probe_log_err(offs, BAD_MEM_ADDR);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530385 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500386 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900387 /* load address */
388 code->op = FETCH_OP_IMM;
389 code->immediate = param;
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900390 } else if (arg[1] == '+') {
391 /* kprobes don't support file offsets */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500392 if (flags & TPARG_FL_KERNEL) {
393 trace_probe_log_err(offs, FILE_ON_KPROBE);
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900394 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500395 }
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900396 ret = kstrtol(arg + 2, 0, &offset);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500397 if (ret) {
398 trace_probe_log_err(offs, BAD_FILE_OFFS);
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900399 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500400 }
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900401
Masami Hiramatsu53305922018-04-25 21:18:03 +0900402 code->op = FETCH_OP_FOFFS;
403 code->immediate = (unsigned long)offset; // imm64?
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530404 } else {
Namhyung Kimb079d372013-07-03 18:34:23 +0900405 /* uprobes don't support symbols */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500406 if (!(flags & TPARG_FL_KERNEL)) {
407 trace_probe_log_err(offs, SYM_ON_UPROBE);
Namhyung Kimb079d372013-07-03 18:34:23 +0900408 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500409 }
Masami Hiramatsua6682812018-08-29 01:18:43 +0900410 /* Preserve symbol for updating */
411 code->op = FETCH_NOP_SYMBOL;
412 code->data = kstrdup(arg + 1, GFP_KERNEL);
413 if (!code->data)
414 return -ENOMEM;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500415 if (++code == end) {
416 trace_probe_log_err(offs, TOO_MANY_OPS);
417 return -EINVAL;
418 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900419 code->op = FETCH_OP_IMM;
Masami Hiramatsua6682812018-08-29 01:18:43 +0900420 code->immediate = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530421 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900422 /* These are fetching from memory */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500423 if (++code == end) {
424 trace_probe_log_err(offs, TOO_MANY_OPS);
425 return -EINVAL;
426 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900427 *pcode = code;
428 code->op = FETCH_OP_DEREF;
429 code->offset = offset;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530430 break;
431
432 case '+': /* deref memory */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530433 case '-':
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900434 if (arg[1] == 'u') {
435 deref = FETCH_OP_UDEREF;
436 arg[1] = arg[0];
437 arg++;
438 }
439 if (arg[0] == '+')
440 arg++; /* Skip '+', because kstrtol() rejects it. */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530441 tmp = strchr(arg, '(');
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500442 if (!tmp) {
443 trace_probe_log_err(offs, DEREF_NEED_BRACE);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900444 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500445 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530446 *tmp = '\0';
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200447 ret = kstrtol(arg, 0, &offset);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500448 if (ret) {
449 trace_probe_log_err(offs, BAD_DEREF_OFFS);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530450 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500451 }
452 offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530453 arg = tmp + 1;
454 tmp = strrchr(arg, ')');
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500455 if (!tmp) {
456 trace_probe_log_err(offs + strlen(arg),
457 DEREF_OPEN_BRACE);
458 return -EINVAL;
459 } else {
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900460 const struct fetch_type *t2 = find_fetch_type(NULL);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530461
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530462 *tmp = '\0';
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500463 ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530464 if (ret)
Masami Hiramatsu53305922018-04-25 21:18:03 +0900465 break;
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900466 if (code->op == FETCH_OP_COMM ||
467 code->op == FETCH_OP_DATA) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500468 trace_probe_log_err(offs, COMM_CANT_DEREF);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900469 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500470 }
471 if (++code == end) {
472 trace_probe_log_err(offs, TOO_MANY_OPS);
473 return -EINVAL;
474 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900475 *pcode = code;
476
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900477 code->op = deref;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900478 code->offset = offset;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530479 }
480 break;
Masami Hiramatsu6218bf92019-06-20 00:08:27 +0900481 case '\\': /* Immediate value */
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900482 if (arg[1] == '"') { /* Immediate string */
483 ret = __parse_imm_string(arg + 2, &tmp, offs + 2);
484 if (ret)
485 break;
486 code->op = FETCH_OP_DATA;
487 code->data = tmp;
488 } else {
489 ret = str_to_immediate(arg + 1, &code->immediate);
490 if (ret)
491 trace_probe_log_err(offs + 1, BAD_IMM);
492 else
493 code->op = FETCH_OP_IMM;
494 }
Masami Hiramatsu6218bf92019-06-20 00:08:27 +0900495 break;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530496 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900497 if (!ret && code->op == FETCH_OP_NOP) {
498 /* Parsed, but do not find fetch method */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500499 trace_probe_log_err(offs, BAD_FETCH_ARG);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530500 ret = -EINVAL;
501 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530502 return ret;
503}
504
505#define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
506
507/* Bitfield type needs to be parsed into a fetch function */
508static int __parse_bitfield_probe_arg(const char *bf,
509 const struct fetch_type *t,
Masami Hiramatsu53305922018-04-25 21:18:03 +0900510 struct fetch_insn **pcode)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530511{
Masami Hiramatsu53305922018-04-25 21:18:03 +0900512 struct fetch_insn *code = *pcode;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530513 unsigned long bw, bo;
514 char *tail;
515
516 if (*bf != 'b')
517 return 0;
518
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530519 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
520
521 if (bw == 0 || *tail != '@')
522 return -EINVAL;
523
524 bf = tail + 1;
525 bo = simple_strtoul(bf, &tail, 0);
526
527 if (tail == bf || *tail != '/')
528 return -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900529 code++;
530 if (code->op != FETCH_OP_NOP)
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500531 return -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900532 *pcode = code;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530533
Masami Hiramatsu53305922018-04-25 21:18:03 +0900534 code->op = FETCH_OP_MOD_BF;
535 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
536 code->rshift = BYTES_TO_BITS(t->size) - bw;
537 code->basesize = t->size;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530538
539 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
540}
541
542/* String length checking wrapper */
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900543static int traceprobe_parse_probe_arg_body(char *arg, ssize_t *size,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500544 struct probe_arg *parg, unsigned int flags, int offset)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530545{
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900546 struct fetch_insn *code, *scode, *tmp = NULL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500547 char *t, *t2, *t3;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900548 int ret, len;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530549
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500550 len = strlen(arg);
551 if (len > MAX_ARGSTR_LEN) {
552 trace_probe_log_err(offset, ARG_TOO_LONG);
553 return -EINVAL;
554 } else if (len == 0) {
555 trace_probe_log_err(offset, NO_ARG_BODY);
556 return -EINVAL;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530557 }
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500558
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530559 parg->comm = kstrdup(arg, GFP_KERNEL);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500560 if (!parg->comm)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530561 return -ENOMEM;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500562
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900563 t = strchr(arg, ':');
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530564 if (t) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900565 *t = '\0';
566 t2 = strchr(++t, '[');
567 if (t2) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500568 *t2++ = '\0';
569 t3 = strchr(t2, ']');
570 if (!t3) {
571 offset += t2 + strlen(t2) - arg;
572 trace_probe_log_err(offset,
573 ARRAY_NO_CLOSE);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900574 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500575 } else if (t3[1] != '\0') {
576 trace_probe_log_err(offset + t3 + 1 - arg,
577 BAD_ARRAY_SUFFIX);
578 return -EINVAL;
579 }
580 *t3 = '\0';
581 if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
582 trace_probe_log_err(offset + t2 - arg,
583 BAD_ARRAY_NUM);
584 return -EINVAL;
585 }
586 if (parg->count > MAX_ARRAY_LEN) {
587 trace_probe_log_err(offset + t2 - arg,
588 ARRAY_TOO_BIG);
589 return -EINVAL;
590 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900591 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530592 }
Masami Hiramatsu3dd1f7f2019-05-07 22:56:02 +0900593
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900594 /*
595 * Since $comm and immediate string can not be dereferred,
596 * we can find those by strcmp.
597 */
598 if (strcmp(arg, "$comm") == 0 || strncmp(arg, "\\\"", 2) == 0) {
Masami Hiramatsu3dd1f7f2019-05-07 22:56:02 +0900599 /* The type of $comm must be "string", and not an array. */
600 if (parg->count || (t && strcmp(t, "string")))
601 return -EINVAL;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900602 parg->type = find_fetch_type("string");
Masami Hiramatsu3dd1f7f2019-05-07 22:56:02 +0900603 } else
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900604 parg->type = find_fetch_type(t);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530605 if (!parg->type) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500606 trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530607 return -EINVAL;
608 }
609 parg->offset = *size;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900610 *size += parg->type->size * (parg->count ?: 1);
611
612 if (parg->count) {
613 len = strlen(parg->type->fmttype) + 6;
614 parg->fmt = kmalloc(len, GFP_KERNEL);
615 if (!parg->fmt)
616 return -ENOMEM;
617 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
618 parg->count);
619 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530620
Gustavo A. R. Silva8623b002019-01-14 22:34:08 -0600621 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900622 if (!code)
623 return -ENOMEM;
624 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530625
Masami Hiramatsu53305922018-04-25 21:18:03 +0900626 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500627 flags, offset);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900628 if (ret)
629 goto fail;
630
631 /* Store operation */
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900632 if (!strcmp(parg->type->name, "string") ||
633 !strcmp(parg->type->name, "ustring")) {
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900634 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900635 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
636 code->op != FETCH_OP_DATA) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500637 trace_probe_log_err(offset + (t ? (t - arg) : 0),
638 BAD_STRING);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900639 ret = -EINVAL;
640 goto fail;
641 }
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900642 if ((code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM) ||
643 parg->count) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900644 /*
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900645 * IMM, DATA and COMM is pointing actual address, those
646 * must be kept, and if parg->count != 0, this is an
647 * array of string pointers instead of string address
648 * itself.
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900649 */
Masami Hiramatsu53305922018-04-25 21:18:03 +0900650 code++;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900651 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500652 trace_probe_log_err(offset, TOO_MANY_OPS);
653 ret = -EINVAL;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900654 goto fail;
655 }
656 }
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900657 /* If op == DEREF, replace it with STRING */
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900658 if (!strcmp(parg->type->name, "ustring") ||
659 code->op == FETCH_OP_UDEREF)
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900660 code->op = FETCH_OP_ST_USTRING;
661 else
662 code->op = FETCH_OP_ST_STRING;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900663 code->size = parg->type->size;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900664 parg->dynamic = true;
665 } else if (code->op == FETCH_OP_DEREF) {
666 code->op = FETCH_OP_ST_MEM;
667 code->size = parg->type->size;
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900668 } else if (code->op == FETCH_OP_UDEREF) {
669 code->op = FETCH_OP_ST_UMEM;
670 code->size = parg->type->size;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900671 } else {
672 code++;
673 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500674 trace_probe_log_err(offset, TOO_MANY_OPS);
675 ret = -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900676 goto fail;
677 }
678 code->op = FETCH_OP_ST_RAW;
679 code->size = parg->type->size;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530680 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900681 scode = code;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900682 /* Modify operation */
683 if (t != NULL) {
684 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500685 if (ret) {
686 trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900687 goto fail;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500688 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900689 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900690 /* Loop(Array) operation */
691 if (parg->count) {
692 if (scode->op != FETCH_OP_ST_MEM &&
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900693 scode->op != FETCH_OP_ST_STRING &&
694 scode->op != FETCH_OP_ST_USTRING) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500695 trace_probe_log_err(offset + (t ? (t - arg) : 0),
696 BAD_STRING);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900697 ret = -EINVAL;
698 goto fail;
699 }
700 code++;
701 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500702 trace_probe_log_err(offset, TOO_MANY_OPS);
703 ret = -EINVAL;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900704 goto fail;
705 }
706 code->op = FETCH_OP_LP_ARRAY;
707 code->param = parg->count;
708 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900709 code++;
710 code->op = FETCH_OP_END;
711
712 /* Shrink down the code buffer */
Gustavo A. R. Silva8623b002019-01-14 22:34:08 -0600713 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900714 if (!parg->code)
715 ret = -ENOMEM;
716 else
717 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
718
719fail:
Masami Hiramatsua6682812018-08-29 01:18:43 +0900720 if (ret) {
721 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900722 if (code->op == FETCH_NOP_SYMBOL ||
723 code->op == FETCH_OP_DATA)
Masami Hiramatsua6682812018-08-29 01:18:43 +0900724 kfree(code->data);
725 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900726 kfree(tmp);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530727
728 return ret;
729}
730
731/* Return 1 if name is reserved or already used by another argument */
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900732static int traceprobe_conflict_field_name(const char *name,
733 struct probe_arg *args, int narg)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530734{
735 int i;
736
737 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
738 if (strcmp(reserved_field_names[i], name) == 0)
739 return 1;
740
741 for (i = 0; i < narg; i++)
742 if (strcmp(args[i].name, name) == 0)
743 return 1;
744
745 return 0;
746}
747
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900748int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, char *arg,
749 unsigned int flags)
750{
751 struct probe_arg *parg = &tp->args[i];
752 char *body;
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900753
754 /* Increment count for freeing args in error case */
755 tp->nr_args++;
756
757 body = strchr(arg, '=');
758 if (body) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500759 if (body - arg > MAX_ARG_NAME_LEN) {
760 trace_probe_log_err(0, ARG_NAME_TOO_LONG);
Masami Hiramatsub4443c12019-03-14 13:30:30 +0900761 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500762 } else if (body == arg) {
763 trace_probe_log_err(0, NO_ARG_NAME);
764 return -EINVAL;
765 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900766 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
767 body++;
768 } else {
769 /* If argument name is omitted, set "argN" */
770 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
771 body = arg;
772 }
773 if (!parg->name)
774 return -ENOMEM;
775
776 if (!is_good_name(parg->name)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500777 trace_probe_log_err(0, BAD_ARG_NAME);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900778 return -EINVAL;
779 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900780 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500781 trace_probe_log_err(0, USED_ARG_NAME);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900782 return -EINVAL;
783 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900784 /* Parse fetch argument */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500785 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
786 body - arg);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900787}
788
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530789void traceprobe_free_probe_arg(struct probe_arg *arg)
790{
Masami Hiramatsua6682812018-08-29 01:18:43 +0900791 struct fetch_insn *code = arg->code;
792
793 while (code && code->op != FETCH_OP_END) {
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900794 if (code->op == FETCH_NOP_SYMBOL ||
795 code->op == FETCH_OP_DATA)
Masami Hiramatsua6682812018-08-29 01:18:43 +0900796 kfree(code->data);
797 code++;
798 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900799 kfree(arg->code);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530800 kfree(arg->name);
801 kfree(arg->comm);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900802 kfree(arg->fmt);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530803}
804
Masami Hiramatsua6682812018-08-29 01:18:43 +0900805int traceprobe_update_arg(struct probe_arg *arg)
806{
807 struct fetch_insn *code = arg->code;
808 long offset;
809 char *tmp;
810 char c;
811 int ret = 0;
812
813 while (code && code->op != FETCH_OP_END) {
814 if (code->op == FETCH_NOP_SYMBOL) {
815 if (code[1].op != FETCH_OP_IMM)
816 return -EINVAL;
817
Masami Hiramatsuee474b82018-11-01 23:29:28 +0900818 tmp = strpbrk(code->data, "+-");
Masami Hiramatsua6682812018-08-29 01:18:43 +0900819 if (tmp)
820 c = *tmp;
821 ret = traceprobe_split_symbol_offset(code->data,
822 &offset);
823 if (ret)
824 return ret;
825
826 code[1].immediate =
827 (unsigned long)kallsyms_lookup_name(code->data);
828 if (tmp)
829 *tmp = c;
830 if (!code[1].immediate)
831 return -ENOENT;
832 code[1].immediate += offset;
833 }
834 code++;
835 }
836 return 0;
837}
838
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900839/* When len=0, we just calculate the needed length */
840#define LEN_OR_ZERO (len ? len - pos : 0)
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900841static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
842 bool is_return)
843{
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900844 struct probe_arg *parg;
845 int i, j;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900846 int pos = 0;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900847 const char *fmt, *arg;
848
849 if (!is_return) {
850 fmt = "(%lx)";
851 arg = "REC->" FIELD_STRING_IP;
852 } else {
853 fmt = "(%lx <- %lx)";
854 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
855 }
856
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900857 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
858
859 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900860 parg = tp->args + i;
861 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
862 if (parg->count) {
863 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
864 parg->type->fmt);
865 for (j = 1; j < parg->count; j++)
866 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
867 parg->type->fmt);
868 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
869 } else
870 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
871 parg->type->fmt);
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900872 }
873
874 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
875
876 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900877 parg = tp->args + i;
878 if (parg->count) {
Steven Rostedt (VMware)20279422020-01-24 10:07:42 -0500879 if ((strcmp(parg->type->name, "string") == 0) ||
880 (strcmp(parg->type->name, "ustring") == 0))
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900881 fmt = ", __get_str(%s[%d])";
882 else
883 fmt = ", REC->%s[%d]";
884 for (j = 0; j < parg->count; j++)
885 pos += snprintf(buf + pos, LEN_OR_ZERO,
886 fmt, parg->name, j);
887 } else {
Steven Rostedt (VMware)20279422020-01-24 10:07:42 -0500888 if ((strcmp(parg->type->name, "string") == 0) ||
889 (strcmp(parg->type->name, "ustring") == 0))
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900890 fmt = ", __get_str(%s)";
891 else
892 fmt = ", REC->%s";
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900893 pos += snprintf(buf + pos, LEN_OR_ZERO,
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900894 fmt, parg->name);
895 }
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900896 }
897
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900898 /* return the length of print_fmt */
899 return pos;
900}
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900901#undef LEN_OR_ZERO
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900902
Masami Hiramatsu0a46c852018-04-25 21:19:30 +0900903int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return)
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900904{
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +0900905 struct trace_event_call *call = trace_probe_event_call(tp);
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900906 int len;
907 char *print_fmt;
908
909 /* First: called with 0 length to calculate the needed length */
910 len = __set_print_fmt(tp, NULL, 0, is_return);
911 print_fmt = kmalloc(len + 1, GFP_KERNEL);
912 if (!print_fmt)
913 return -ENOMEM;
914
915 /* Second: actually write the @print_fmt */
916 __set_print_fmt(tp, print_fmt, len + 1, is_return);
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +0900917 call->print_fmt = print_fmt;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900918
919 return 0;
920}
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900921
922int traceprobe_define_arg_fields(struct trace_event_call *event_call,
923 size_t offset, struct trace_probe *tp)
924{
925 int ret, i;
926
927 /* Set argument names as fields */
928 for (i = 0; i < tp->nr_args; i++) {
929 struct probe_arg *parg = &tp->args[i];
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900930 const char *fmt = parg->type->fmttype;
931 int size = parg->type->size;
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900932
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900933 if (parg->fmt)
934 fmt = parg->fmt;
935 if (parg->count)
936 size *= parg->count;
937 ret = trace_define_field(event_call, fmt, parg->name,
938 offset + parg->offset, size,
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900939 parg->type->is_signed,
940 FILTER_OTHER);
941 if (ret)
942 return ret;
943 }
944 return 0;
945}
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900946
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900947static void trace_probe_event_free(struct trace_probe_event *tpe)
948{
949 kfree(tpe->class.system);
950 kfree(tpe->call.name);
951 kfree(tpe->call.print_fmt);
952 kfree(tpe);
953}
954
955int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
956{
957 if (trace_probe_has_sibling(tp))
958 return -EBUSY;
959
960 list_del_init(&tp->list);
961 trace_probe_event_free(tp->event);
962
963 tp->event = to->event;
964 list_add_tail(&tp->list, trace_probe_probe_list(to));
965
966 return 0;
967}
968
969void trace_probe_unlink(struct trace_probe *tp)
970{
971 list_del_init(&tp->list);
972 if (list_empty(trace_probe_probe_list(tp)))
973 trace_probe_event_free(tp->event);
974 tp->event = NULL;
975}
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900976
977void trace_probe_cleanup(struct trace_probe *tp)
978{
979 int i;
980
981 for (i = 0; i < tp->nr_args; i++)
982 traceprobe_free_probe_arg(&tp->args[i]);
983
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900984 if (tp->event)
985 trace_probe_unlink(tp);
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900986}
987
988int trace_probe_init(struct trace_probe *tp, const char *event,
Masami Hiramatsub61387c2020-01-22 12:23:25 +0900989 const char *group, bool alloc_filter)
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900990{
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900991 struct trace_event_call *call;
Masami Hiramatsub61387c2020-01-22 12:23:25 +0900992 size_t size = sizeof(struct trace_probe_event);
Masami Hiramatsu60d53e22019-06-20 00:07:20 +0900993 int ret = 0;
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +0900994
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900995 if (!event || !group)
996 return -EINVAL;
997
Masami Hiramatsub61387c2020-01-22 12:23:25 +0900998 if (alloc_filter)
999 size += sizeof(struct trace_uprobe_filter);
1000
1001 tp->event = kzalloc(size, GFP_KERNEL);
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001002 if (!tp->event)
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001003 return -ENOMEM;
1004
Masami Hiramatsud59fae62019-09-17 14:11:37 +09001005 INIT_LIST_HEAD(&tp->event->files);
1006 INIT_LIST_HEAD(&tp->event->class.fields);
1007 INIT_LIST_HEAD(&tp->event->probes);
1008 INIT_LIST_HEAD(&tp->list);
Julia Lawallfc9d2762020-05-07 21:30:08 +02001009 list_add(&tp->list, &tp->event->probes);
Masami Hiramatsud59fae62019-09-17 14:11:37 +09001010
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001011 call = trace_probe_event_call(tp);
1012 call->class = &tp->event->class;
1013 call->name = kstrdup(event, GFP_KERNEL);
1014 if (!call->name) {
1015 ret = -ENOMEM;
1016 goto error;
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001017 }
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001018
1019 tp->event->class.system = kstrdup(group, GFP_KERNEL);
1020 if (!tp->event->class.system) {
1021 ret = -ENOMEM;
1022 goto error;
1023 }
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001024
1025 return 0;
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001026
1027error:
1028 trace_probe_cleanup(tp);
1029 return ret;
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001030}
Masami Hiramatsu46e53762019-06-01 00:17:16 +09001031
1032int trace_probe_register_event_call(struct trace_probe *tp)
1033{
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001034 struct trace_event_call *call = trace_probe_event_call(tp);
Masami Hiramatsu46e53762019-06-01 00:17:16 +09001035 int ret;
1036
1037 ret = register_trace_event(&call->event);
1038 if (!ret)
1039 return -ENODEV;
1040
1041 ret = trace_add_event_call(call);
1042 if (ret)
1043 unregister_trace_event(&call->event);
1044
1045 return ret;
1046}
Masami Hiramatsub5f935e2019-06-01 00:17:26 +09001047
1048int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1049{
1050 struct event_file_link *link;
1051
1052 link = kmalloc(sizeof(*link), GFP_KERNEL);
1053 if (!link)
1054 return -ENOMEM;
1055
1056 link->file = file;
1057 INIT_LIST_HEAD(&link->list);
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001058 list_add_tail_rcu(&link->list, &tp->event->files);
Masami Hiramatsu747774d2019-06-01 00:17:37 +09001059 trace_probe_set_flag(tp, TP_FLAG_TRACE);
Masami Hiramatsub5f935e2019-06-01 00:17:26 +09001060 return 0;
1061}
1062
1063struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1064 struct trace_event_file *file)
1065{
1066 struct event_file_link *link;
1067
1068 trace_probe_for_each_link(link, tp) {
1069 if (link->file == file)
1070 return link;
1071 }
1072
1073 return NULL;
1074}
1075
1076int trace_probe_remove_file(struct trace_probe *tp,
1077 struct trace_event_file *file)
1078{
1079 struct event_file_link *link;
1080
1081 link = trace_probe_get_file_link(tp, file);
1082 if (!link)
1083 return -ENOENT;
1084
1085 list_del_rcu(&link->list);
1086 synchronize_rcu();
1087 kfree(link);
1088
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001089 if (list_empty(&tp->event->files))
Masami Hiramatsu747774d2019-06-01 00:17:37 +09001090 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
Masami Hiramatsub5f935e2019-06-01 00:17:26 +09001091
1092 return 0;
1093}
Masami Hiramatsuca89bc02019-06-20 00:07:49 +09001094
1095/*
1096 * Return the smallest index of different type argument (start from 1).
1097 * If all argument types and name are same, return 0.
1098 */
1099int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1100{
1101 int i;
1102
Masami Hiramatsud2aea952019-09-28 05:53:29 -04001103 /* In case of more arguments */
1104 if (a->nr_args < b->nr_args)
1105 return a->nr_args + 1;
1106 if (a->nr_args > b->nr_args)
1107 return b->nr_args + 1;
1108
Masami Hiramatsuca89bc02019-06-20 00:07:49 +09001109 for (i = 0; i < a->nr_args; i++) {
1110 if ((b->nr_args <= i) ||
1111 ((a->args[i].type != b->args[i].type) ||
1112 (a->args[i].count != b->args[i].count) ||
1113 strcmp(a->args[i].name, b->args[i].name)))
1114 return i + 1;
1115 }
1116
1117 return 0;
1118}
Masami Hiramatsueb5bf812019-06-20 00:08:08 +09001119
1120bool trace_probe_match_command_args(struct trace_probe *tp,
1121 int argc, const char **argv)
1122{
1123 char buf[MAX_ARGSTR_LEN + 1];
1124 int i;
1125
1126 if (tp->nr_args < argc)
1127 return false;
1128
1129 for (i = 0; i < argc; i++) {
1130 snprintf(buf, sizeof(buf), "%s=%s",
1131 tp->args[i].name, tp->args[i].comm);
1132 if (strcmp(buf, argv[i]))
1133 return false;
1134 }
1135 return true;
1136}