blob: 3ed2a3f372972cd3fe9e83fb142c1a608a57be9f [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
Ingo Molnarf2cc0202021-03-23 18:49:35 +0100171 /* Recalculate the length and allocate buffer */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500172 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
Ingo Molnarf2cc0202021-03-23 18:49:35 +0100185 * appears at pos + 1.
Masami Hiramatsud2aea952019-09-28 05:53:29 -0400186 */
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, '/');
Steven Rostedt (VMware)bc1b9732021-08-16 23:42:59 -0400236 if (!slash)
237 slash = strchr(event, '.');
238
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900239 if (slash) {
240 if (slash == event) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500241 trace_probe_log_err(offset, NO_GROUP_NAME);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900242 return -EINVAL;
243 }
244 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500245 trace_probe_log_err(offset, GROUP_TOO_LONG);
246 return -EINVAL;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900247 }
248 strlcpy(buf, event, slash - event + 1);
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900249 if (!is_good_name(buf)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500250 trace_probe_log_err(offset, BAD_GROUP_NAME);
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900251 return -EINVAL;
252 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900253 *pgroup = buf;
254 *pevent = slash + 1;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500255 offset += slash - event + 1;
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900256 event = *pevent;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900257 }
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900258 len = strlen(event);
259 if (len == 0) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500260 trace_probe_log_err(offset, NO_EVENT_NAME);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900261 return -EINVAL;
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900262 } else if (len > MAX_EVENT_NAME_LEN) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500263 trace_probe_log_err(offset, EVENT_TOO_LONG);
264 return -EINVAL;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900265 }
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900266 if (!is_good_name(event)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500267 trace_probe_log_err(offset, BAD_EVENT_NAME);
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900268 return -EINVAL;
269 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900270 return 0;
271}
272
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530273#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
274
275static int parse_probe_vars(char *arg, const struct fetch_type *t,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500276 struct fetch_insn *code, unsigned int flags, int offs)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530277{
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530278 unsigned long param;
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500279 int ret = 0;
280 int len;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530281
282 if (strcmp(arg, "retval") == 0) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500283 if (flags & TPARG_FL_RETURN) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900284 code->op = FETCH_OP_RETVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500285 } else {
286 trace_probe_log_err(offs, RETVAL_ON_PROBE);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530287 ret = -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500288 }
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500289 } else if ((len = str_has_prefix(arg, "stack"))) {
290 if (arg[len] == '\0') {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900291 code->op = FETCH_OP_STACKP;
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500292 } else if (isdigit(arg[len])) {
293 ret = kstrtoul(arg + len, 10, &param);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500294 if (ret) {
295 goto inval_var;
296 } else if ((flags & TPARG_FL_KERNEL) &&
297 param > PARAM_MAX_STACK) {
298 trace_probe_log_err(offs, BAD_STACK_NUM);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530299 ret = -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500300 } else {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900301 code->op = FETCH_OP_STACK;
302 code->param = (unsigned int)param;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530303 }
304 } else
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500305 goto inval_var;
Omar Sandoval35abb672016-06-08 18:38:02 -0700306 } else if (strcmp(arg, "comm") == 0) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900307 code->op = FETCH_OP_COMM;
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900308#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
309 } else if (((flags & TPARG_FL_MASK) ==
310 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500311 (len = str_has_prefix(arg, "arg"))) {
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500312 ret = kstrtoul(arg + len, 10, &param);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500313 if (ret) {
314 goto inval_var;
315 } else if (!param || param > PARAM_MAX_STACK) {
316 trace_probe_log_err(offs, BAD_ARG_NUM);
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900317 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500318 }
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900319 code->op = FETCH_OP_ARG;
320 code->param = (unsigned int)param - 1;
321#endif
Tzvetomir Stoyanov (VMware)7491e2c2021-08-19 11:26:06 -0400322 } else if (flags & TPARG_FL_TPOINT) {
323 if (code->data)
324 return -EFAULT;
325 code->data = kstrdup(arg, GFP_KERNEL);
326 if (!code->data)
327 return -ENOMEM;
328 code->op = FETCH_OP_TP_ARG;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530329 } else
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500330 goto inval_var;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530331
332 return ret;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500333
334inval_var:
335 trace_probe_log_err(offs, BAD_VAR);
336 return -EINVAL;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530337}
338
Masami Hiramatsu6218bf92019-06-20 00:08:27 +0900339static int str_to_immediate(char *str, unsigned long *imm)
340{
341 if (isdigit(str[0]))
342 return kstrtoul(str, 0, imm);
343 else if (str[0] == '-')
344 return kstrtol(str, 0, (long *)imm);
345 else if (str[0] == '+')
346 return kstrtol(str + 1, 0, (long *)imm);
347 return -EINVAL;
348}
349
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900350static int __parse_imm_string(char *str, char **pbuf, int offs)
351{
352 size_t len = strlen(str);
353
354 if (str[len - 1] != '"') {
355 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
356 return -EINVAL;
357 }
358 *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
359 return 0;
360}
361
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530362/* Recursive argument parser */
Masami Hiramatsu53305922018-04-25 21:18:03 +0900363static int
364parse_probe_arg(char *arg, const struct fetch_type *type,
365 struct fetch_insn **pcode, struct fetch_insn *end,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500366 unsigned int flags, int offs)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530367{
Masami Hiramatsu53305922018-04-25 21:18:03 +0900368 struct fetch_insn *code = *pcode;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530369 unsigned long param;
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900370 int deref = FETCH_OP_DEREF;
Steven Rostedt (VMware)bf173ca2018-10-12 12:50:22 -0400371 long offset = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530372 char *tmp;
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900373 int ret = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530374
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530375 switch (arg[0]) {
376 case '$':
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500377 ret = parse_probe_vars(arg + 1, type, code, flags, offs);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530378 break;
379
380 case '%': /* named register */
381 ret = regs_query_register_offset(arg + 1);
382 if (ret >= 0) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900383 code->op = FETCH_OP_REG;
384 code->param = (unsigned int)ret;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530385 ret = 0;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500386 } else
387 trace_probe_log_err(offs, BAD_REG_NAME);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530388 break;
389
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900390 case '@': /* memory, file-offset or symbol */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530391 if (isdigit(arg[1])) {
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200392 ret = kstrtoul(arg + 1, 0, &param);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500393 if (ret) {
394 trace_probe_log_err(offs, BAD_MEM_ADDR);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530395 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500396 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900397 /* load address */
398 code->op = FETCH_OP_IMM;
399 code->immediate = param;
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900400 } else if (arg[1] == '+') {
401 /* kprobes don't support file offsets */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500402 if (flags & TPARG_FL_KERNEL) {
403 trace_probe_log_err(offs, FILE_ON_KPROBE);
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900404 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500405 }
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900406 ret = kstrtol(arg + 2, 0, &offset);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500407 if (ret) {
408 trace_probe_log_err(offs, BAD_FILE_OFFS);
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900409 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500410 }
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900411
Masami Hiramatsu53305922018-04-25 21:18:03 +0900412 code->op = FETCH_OP_FOFFS;
413 code->immediate = (unsigned long)offset; // imm64?
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530414 } else {
Namhyung Kimb079d372013-07-03 18:34:23 +0900415 /* uprobes don't support symbols */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500416 if (!(flags & TPARG_FL_KERNEL)) {
417 trace_probe_log_err(offs, SYM_ON_UPROBE);
Namhyung Kimb079d372013-07-03 18:34:23 +0900418 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500419 }
Masami Hiramatsua6682812018-08-29 01:18:43 +0900420 /* Preserve symbol for updating */
421 code->op = FETCH_NOP_SYMBOL;
422 code->data = kstrdup(arg + 1, GFP_KERNEL);
423 if (!code->data)
424 return -ENOMEM;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500425 if (++code == end) {
426 trace_probe_log_err(offs, TOO_MANY_OPS);
427 return -EINVAL;
428 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900429 code->op = FETCH_OP_IMM;
Masami Hiramatsua6682812018-08-29 01:18:43 +0900430 code->immediate = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530431 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900432 /* These are fetching from memory */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500433 if (++code == end) {
434 trace_probe_log_err(offs, TOO_MANY_OPS);
435 return -EINVAL;
436 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900437 *pcode = code;
438 code->op = FETCH_OP_DEREF;
439 code->offset = offset;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530440 break;
441
442 case '+': /* deref memory */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530443 case '-':
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900444 if (arg[1] == 'u') {
445 deref = FETCH_OP_UDEREF;
446 arg[1] = arg[0];
447 arg++;
448 }
449 if (arg[0] == '+')
450 arg++; /* Skip '+', because kstrtol() rejects it. */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530451 tmp = strchr(arg, '(');
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500452 if (!tmp) {
453 trace_probe_log_err(offs, DEREF_NEED_BRACE);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900454 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500455 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530456 *tmp = '\0';
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200457 ret = kstrtol(arg, 0, &offset);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500458 if (ret) {
459 trace_probe_log_err(offs, BAD_DEREF_OFFS);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530460 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500461 }
462 offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530463 arg = tmp + 1;
464 tmp = strrchr(arg, ')');
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500465 if (!tmp) {
466 trace_probe_log_err(offs + strlen(arg),
467 DEREF_OPEN_BRACE);
468 return -EINVAL;
469 } else {
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900470 const struct fetch_type *t2 = find_fetch_type(NULL);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530471
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530472 *tmp = '\0';
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500473 ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530474 if (ret)
Masami Hiramatsu53305922018-04-25 21:18:03 +0900475 break;
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900476 if (code->op == FETCH_OP_COMM ||
477 code->op == FETCH_OP_DATA) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500478 trace_probe_log_err(offs, COMM_CANT_DEREF);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900479 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500480 }
481 if (++code == end) {
482 trace_probe_log_err(offs, TOO_MANY_OPS);
483 return -EINVAL;
484 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900485 *pcode = code;
486
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900487 code->op = deref;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900488 code->offset = offset;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530489 }
490 break;
Masami Hiramatsu6218bf92019-06-20 00:08:27 +0900491 case '\\': /* Immediate value */
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900492 if (arg[1] == '"') { /* Immediate string */
493 ret = __parse_imm_string(arg + 2, &tmp, offs + 2);
494 if (ret)
495 break;
496 code->op = FETCH_OP_DATA;
497 code->data = tmp;
498 } else {
499 ret = str_to_immediate(arg + 1, &code->immediate);
500 if (ret)
501 trace_probe_log_err(offs + 1, BAD_IMM);
502 else
503 code->op = FETCH_OP_IMM;
504 }
Masami Hiramatsu6218bf92019-06-20 00:08:27 +0900505 break;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530506 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900507 if (!ret && code->op == FETCH_OP_NOP) {
508 /* Parsed, but do not find fetch method */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500509 trace_probe_log_err(offs, BAD_FETCH_ARG);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530510 ret = -EINVAL;
511 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530512 return ret;
513}
514
515#define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
516
517/* Bitfield type needs to be parsed into a fetch function */
518static int __parse_bitfield_probe_arg(const char *bf,
519 const struct fetch_type *t,
Masami Hiramatsu53305922018-04-25 21:18:03 +0900520 struct fetch_insn **pcode)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530521{
Masami Hiramatsu53305922018-04-25 21:18:03 +0900522 struct fetch_insn *code = *pcode;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530523 unsigned long bw, bo;
524 char *tail;
525
526 if (*bf != 'b')
527 return 0;
528
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530529 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
530
531 if (bw == 0 || *tail != '@')
532 return -EINVAL;
533
534 bf = tail + 1;
535 bo = simple_strtoul(bf, &tail, 0);
536
537 if (tail == bf || *tail != '/')
538 return -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900539 code++;
540 if (code->op != FETCH_OP_NOP)
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500541 return -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900542 *pcode = code;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530543
Masami Hiramatsu53305922018-04-25 21:18:03 +0900544 code->op = FETCH_OP_MOD_BF;
545 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
546 code->rshift = BYTES_TO_BITS(t->size) - bw;
547 code->basesize = t->size;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530548
549 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
550}
551
552/* String length checking wrapper */
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400553static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500554 struct probe_arg *parg, unsigned int flags, int offset)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530555{
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900556 struct fetch_insn *code, *scode, *tmp = NULL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500557 char *t, *t2, *t3;
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400558 char *arg;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900559 int ret, len;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530560
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400561 arg = kstrdup(argv, GFP_KERNEL);
562 if (!arg)
563 return -ENOMEM;
564
565 ret = -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500566 len = strlen(arg);
567 if (len > MAX_ARGSTR_LEN) {
568 trace_probe_log_err(offset, ARG_TOO_LONG);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400569 goto out;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500570 } else if (len == 0) {
571 trace_probe_log_err(offset, NO_ARG_BODY);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400572 goto out;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530573 }
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500574
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400575 ret = -ENOMEM;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530576 parg->comm = kstrdup(arg, GFP_KERNEL);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500577 if (!parg->comm)
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400578 goto out;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500579
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400580 ret = -EINVAL;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900581 t = strchr(arg, ':');
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530582 if (t) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900583 *t = '\0';
584 t2 = strchr(++t, '[');
585 if (t2) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500586 *t2++ = '\0';
587 t3 = strchr(t2, ']');
588 if (!t3) {
589 offset += t2 + strlen(t2) - arg;
590 trace_probe_log_err(offset,
591 ARRAY_NO_CLOSE);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400592 goto out;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500593 } else if (t3[1] != '\0') {
594 trace_probe_log_err(offset + t3 + 1 - arg,
595 BAD_ARRAY_SUFFIX);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400596 goto out;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500597 }
598 *t3 = '\0';
599 if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
600 trace_probe_log_err(offset + t2 - arg,
601 BAD_ARRAY_NUM);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400602 goto out;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500603 }
604 if (parg->count > MAX_ARRAY_LEN) {
605 trace_probe_log_err(offset + t2 - arg,
606 ARRAY_TOO_BIG);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400607 goto out;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500608 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900609 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530610 }
Masami Hiramatsu3dd1f7f2019-05-07 22:56:02 +0900611
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900612 /*
Ingo Molnarf2cc0202021-03-23 18:49:35 +0100613 * Since $comm and immediate string can not be dereferenced,
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900614 * we can find those by strcmp.
615 */
616 if (strcmp(arg, "$comm") == 0 || strncmp(arg, "\\\"", 2) == 0) {
Masami Hiramatsu3dd1f7f2019-05-07 22:56:02 +0900617 /* The type of $comm must be "string", and not an array. */
618 if (parg->count || (t && strcmp(t, "string")))
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400619 goto out;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900620 parg->type = find_fetch_type("string");
Masami Hiramatsu3dd1f7f2019-05-07 22:56:02 +0900621 } else
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900622 parg->type = find_fetch_type(t);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530623 if (!parg->type) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500624 trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400625 goto out;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530626 }
627 parg->offset = *size;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900628 *size += parg->type->size * (parg->count ?: 1);
629
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400630 ret = -ENOMEM;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900631 if (parg->count) {
632 len = strlen(parg->type->fmttype) + 6;
633 parg->fmt = kmalloc(len, GFP_KERNEL);
634 if (!parg->fmt)
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400635 goto out;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900636 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
637 parg->count);
638 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530639
Gustavo A. R. Silva8623b002019-01-14 22:34:08 -0600640 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900641 if (!code)
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400642 goto out;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900643 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530644
Masami Hiramatsu53305922018-04-25 21:18:03 +0900645 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500646 flags, offset);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900647 if (ret)
648 goto fail;
649
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400650 ret = -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900651 /* Store operation */
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900652 if (!strcmp(parg->type->name, "string") ||
653 !strcmp(parg->type->name, "ustring")) {
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900654 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900655 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
Tzvetomir Stoyanov (VMware)7491e2c2021-08-19 11:26:06 -0400656 code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500657 trace_probe_log_err(offset + (t ? (t - arg) : 0),
658 BAD_STRING);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900659 goto fail;
660 }
Vamshi K Sthambamkadi3aa8fdc2020-06-15 20:00:38 +0530661 if ((code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
Tzvetomir Stoyanov (VMware)7491e2c2021-08-19 11:26:06 -0400662 code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
663 parg->count) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900664 /*
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900665 * IMM, DATA and COMM is pointing actual address, those
666 * must be kept, and if parg->count != 0, this is an
667 * array of string pointers instead of string address
668 * itself.
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900669 */
Masami Hiramatsu53305922018-04-25 21:18:03 +0900670 code++;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900671 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500672 trace_probe_log_err(offset, TOO_MANY_OPS);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900673 goto fail;
674 }
675 }
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900676 /* If op == DEREF, replace it with STRING */
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900677 if (!strcmp(parg->type->name, "ustring") ||
678 code->op == FETCH_OP_UDEREF)
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900679 code->op = FETCH_OP_ST_USTRING;
680 else
681 code->op = FETCH_OP_ST_STRING;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900682 code->size = parg->type->size;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900683 parg->dynamic = true;
684 } else if (code->op == FETCH_OP_DEREF) {
685 code->op = FETCH_OP_ST_MEM;
686 code->size = parg->type->size;
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900687 } else if (code->op == FETCH_OP_UDEREF) {
688 code->op = FETCH_OP_ST_UMEM;
689 code->size = parg->type->size;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900690 } else {
691 code++;
692 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500693 trace_probe_log_err(offset, TOO_MANY_OPS);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900694 goto fail;
695 }
696 code->op = FETCH_OP_ST_RAW;
697 code->size = parg->type->size;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530698 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900699 scode = code;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900700 /* Modify operation */
701 if (t != NULL) {
702 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500703 if (ret) {
704 trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900705 goto fail;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500706 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900707 }
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400708 ret = -EINVAL;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900709 /* Loop(Array) operation */
710 if (parg->count) {
711 if (scode->op != FETCH_OP_ST_MEM &&
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900712 scode->op != FETCH_OP_ST_STRING &&
713 scode->op != FETCH_OP_ST_USTRING) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500714 trace_probe_log_err(offset + (t ? (t - arg) : 0),
715 BAD_STRING);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900716 goto fail;
717 }
718 code++;
719 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500720 trace_probe_log_err(offset, TOO_MANY_OPS);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900721 goto fail;
722 }
723 code->op = FETCH_OP_LP_ARRAY;
724 code->param = parg->count;
725 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900726 code++;
727 code->op = FETCH_OP_END;
728
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400729 ret = 0;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900730 /* Shrink down the code buffer */
Gustavo A. R. Silva8623b002019-01-14 22:34:08 -0600731 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900732 if (!parg->code)
733 ret = -ENOMEM;
734 else
735 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
736
737fail:
Masami Hiramatsua6682812018-08-29 01:18:43 +0900738 if (ret) {
739 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900740 if (code->op == FETCH_NOP_SYMBOL ||
741 code->op == FETCH_OP_DATA)
Masami Hiramatsua6682812018-08-29 01:18:43 +0900742 kfree(code->data);
743 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900744 kfree(tmp);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400745out:
746 kfree(arg);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530747
748 return ret;
749}
750
751/* Return 1 if name is reserved or already used by another argument */
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900752static int traceprobe_conflict_field_name(const char *name,
753 struct probe_arg *args, int narg)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530754{
755 int i;
756
757 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
758 if (strcmp(reserved_field_names[i], name) == 0)
759 return 1;
760
761 for (i = 0; i < narg; i++)
762 if (strcmp(args[i].name, name) == 0)
763 return 1;
764
765 return 0;
766}
767
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400768int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900769 unsigned int flags)
770{
771 struct probe_arg *parg = &tp->args[i];
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400772 const char *body;
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900773
774 /* Increment count for freeing args in error case */
775 tp->nr_args++;
776
777 body = strchr(arg, '=');
778 if (body) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500779 if (body - arg > MAX_ARG_NAME_LEN) {
780 trace_probe_log_err(0, ARG_NAME_TOO_LONG);
Masami Hiramatsub4443c12019-03-14 13:30:30 +0900781 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500782 } else if (body == arg) {
783 trace_probe_log_err(0, NO_ARG_NAME);
784 return -EINVAL;
785 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900786 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
787 body++;
788 } else {
789 /* If argument name is omitted, set "argN" */
790 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
791 body = arg;
792 }
793 if (!parg->name)
794 return -ENOMEM;
795
796 if (!is_good_name(parg->name)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500797 trace_probe_log_err(0, BAD_ARG_NAME);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900798 return -EINVAL;
799 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900800 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500801 trace_probe_log_err(0, USED_ARG_NAME);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900802 return -EINVAL;
803 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900804 /* Parse fetch argument */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500805 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
806 body - arg);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900807}
808
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530809void traceprobe_free_probe_arg(struct probe_arg *arg)
810{
Masami Hiramatsua6682812018-08-29 01:18:43 +0900811 struct fetch_insn *code = arg->code;
812
813 while (code && code->op != FETCH_OP_END) {
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900814 if (code->op == FETCH_NOP_SYMBOL ||
815 code->op == FETCH_OP_DATA)
Masami Hiramatsua6682812018-08-29 01:18:43 +0900816 kfree(code->data);
817 code++;
818 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900819 kfree(arg->code);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530820 kfree(arg->name);
821 kfree(arg->comm);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900822 kfree(arg->fmt);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530823}
824
Masami Hiramatsua6682812018-08-29 01:18:43 +0900825int traceprobe_update_arg(struct probe_arg *arg)
826{
827 struct fetch_insn *code = arg->code;
828 long offset;
829 char *tmp;
830 char c;
831 int ret = 0;
832
833 while (code && code->op != FETCH_OP_END) {
834 if (code->op == FETCH_NOP_SYMBOL) {
835 if (code[1].op != FETCH_OP_IMM)
836 return -EINVAL;
837
Masami Hiramatsuee474b82018-11-01 23:29:28 +0900838 tmp = strpbrk(code->data, "+-");
Masami Hiramatsua6682812018-08-29 01:18:43 +0900839 if (tmp)
840 c = *tmp;
841 ret = traceprobe_split_symbol_offset(code->data,
842 &offset);
843 if (ret)
844 return ret;
845
846 code[1].immediate =
847 (unsigned long)kallsyms_lookup_name(code->data);
848 if (tmp)
849 *tmp = c;
850 if (!code[1].immediate)
851 return -ENOENT;
852 code[1].immediate += offset;
853 }
854 code++;
855 }
856 return 0;
857}
858
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900859/* When len=0, we just calculate the needed length */
860#define LEN_OR_ZERO (len ? len - pos : 0)
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900861static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
Steven Rostedt (VMware)007517a2021-08-19 00:13:27 -0400862 enum probe_print_type ptype)
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900863{
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900864 struct probe_arg *parg;
865 int i, j;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900866 int pos = 0;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900867 const char *fmt, *arg;
868
Steven Rostedt (VMware)007517a2021-08-19 00:13:27 -0400869 switch (ptype) {
870 case PROBE_PRINT_NORMAL:
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900871 fmt = "(%lx)";
872 arg = "REC->" FIELD_STRING_IP;
Steven Rostedt (VMware)007517a2021-08-19 00:13:27 -0400873 break;
874 case PROBE_PRINT_RETURN:
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900875 fmt = "(%lx <- %lx)";
876 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
Steven Rostedt (VMware)007517a2021-08-19 00:13:27 -0400877 break;
Tzvetomir Stoyanov (VMware)7491e2c2021-08-19 11:26:06 -0400878 case PROBE_PRINT_EVENT:
879 fmt = "(%u)";
880 arg = "REC->" FIELD_STRING_TYPE;
881 break;
Steven Rostedt (VMware)007517a2021-08-19 00:13:27 -0400882 default:
883 WARN_ON_ONCE(1);
884 return 0;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900885 }
886
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900887 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
888
889 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900890 parg = tp->args + i;
891 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
892 if (parg->count) {
893 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
894 parg->type->fmt);
895 for (j = 1; j < parg->count; j++)
896 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
897 parg->type->fmt);
898 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
899 } else
900 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
901 parg->type->fmt);
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900902 }
903
904 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
905
906 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900907 parg = tp->args + i;
908 if (parg->count) {
Steven Rostedt (VMware)20279422020-01-24 10:07:42 -0500909 if ((strcmp(parg->type->name, "string") == 0) ||
910 (strcmp(parg->type->name, "ustring") == 0))
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900911 fmt = ", __get_str(%s[%d])";
912 else
913 fmt = ", REC->%s[%d]";
914 for (j = 0; j < parg->count; j++)
915 pos += snprintf(buf + pos, LEN_OR_ZERO,
916 fmt, parg->name, j);
917 } else {
Steven Rostedt (VMware)20279422020-01-24 10:07:42 -0500918 if ((strcmp(parg->type->name, "string") == 0) ||
919 (strcmp(parg->type->name, "ustring") == 0))
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900920 fmt = ", __get_str(%s)";
921 else
922 fmt = ", REC->%s";
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900923 pos += snprintf(buf + pos, LEN_OR_ZERO,
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900924 fmt, parg->name);
925 }
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900926 }
927
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900928 /* return the length of print_fmt */
929 return pos;
930}
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900931#undef LEN_OR_ZERO
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900932
Steven Rostedt (VMware)007517a2021-08-19 00:13:27 -0400933int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900934{
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +0900935 struct trace_event_call *call = trace_probe_event_call(tp);
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900936 int len;
937 char *print_fmt;
938
939 /* First: called with 0 length to calculate the needed length */
Steven Rostedt (VMware)007517a2021-08-19 00:13:27 -0400940 len = __set_print_fmt(tp, NULL, 0, ptype);
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900941 print_fmt = kmalloc(len + 1, GFP_KERNEL);
942 if (!print_fmt)
943 return -ENOMEM;
944
945 /* Second: actually write the @print_fmt */
Steven Rostedt (VMware)007517a2021-08-19 00:13:27 -0400946 __set_print_fmt(tp, print_fmt, len + 1, ptype);
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +0900947 call->print_fmt = print_fmt;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900948
949 return 0;
950}
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900951
952int traceprobe_define_arg_fields(struct trace_event_call *event_call,
953 size_t offset, struct trace_probe *tp)
954{
955 int ret, i;
956
957 /* Set argument names as fields */
958 for (i = 0; i < tp->nr_args; i++) {
959 struct probe_arg *parg = &tp->args[i];
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900960 const char *fmt = parg->type->fmttype;
961 int size = parg->type->size;
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900962
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900963 if (parg->fmt)
964 fmt = parg->fmt;
965 if (parg->count)
966 size *= parg->count;
967 ret = trace_define_field(event_call, fmt, parg->name,
968 offset + parg->offset, size,
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900969 parg->type->is_signed,
970 FILTER_OTHER);
971 if (ret)
972 return ret;
973 }
974 return 0;
975}
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900976
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900977static void trace_probe_event_free(struct trace_probe_event *tpe)
978{
979 kfree(tpe->class.system);
980 kfree(tpe->call.name);
981 kfree(tpe->call.print_fmt);
982 kfree(tpe);
983}
984
985int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
986{
987 if (trace_probe_has_sibling(tp))
988 return -EBUSY;
989
990 list_del_init(&tp->list);
991 trace_probe_event_free(tp->event);
992
993 tp->event = to->event;
994 list_add_tail(&tp->list, trace_probe_probe_list(to));
995
996 return 0;
997}
998
999void trace_probe_unlink(struct trace_probe *tp)
1000{
1001 list_del_init(&tp->list);
1002 if (list_empty(trace_probe_probe_list(tp)))
1003 trace_probe_event_free(tp->event);
1004 tp->event = NULL;
1005}
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001006
1007void trace_probe_cleanup(struct trace_probe *tp)
1008{
1009 int i;
1010
1011 for (i = 0; i < tp->nr_args; i++)
1012 traceprobe_free_probe_arg(&tp->args[i]);
1013
Masami Hiramatsuca89bc02019-06-20 00:07:49 +09001014 if (tp->event)
1015 trace_probe_unlink(tp);
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001016}
1017
1018int trace_probe_init(struct trace_probe *tp, const char *event,
Masami Hiramatsub61387c2020-01-22 12:23:25 +09001019 const char *group, bool alloc_filter)
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001020{
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001021 struct trace_event_call *call;
Masami Hiramatsub61387c2020-01-22 12:23:25 +09001022 size_t size = sizeof(struct trace_probe_event);
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001023 int ret = 0;
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001024
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001025 if (!event || !group)
1026 return -EINVAL;
1027
Masami Hiramatsub61387c2020-01-22 12:23:25 +09001028 if (alloc_filter)
1029 size += sizeof(struct trace_uprobe_filter);
1030
1031 tp->event = kzalloc(size, GFP_KERNEL);
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001032 if (!tp->event)
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001033 return -ENOMEM;
1034
Masami Hiramatsud59fae62019-09-17 14:11:37 +09001035 INIT_LIST_HEAD(&tp->event->files);
1036 INIT_LIST_HEAD(&tp->event->class.fields);
1037 INIT_LIST_HEAD(&tp->event->probes);
1038 INIT_LIST_HEAD(&tp->list);
Julia Lawallfc9d2762020-05-07 21:30:08 +02001039 list_add(&tp->list, &tp->event->probes);
Masami Hiramatsud59fae62019-09-17 14:11:37 +09001040
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001041 call = trace_probe_event_call(tp);
1042 call->class = &tp->event->class;
1043 call->name = kstrdup(event, GFP_KERNEL);
1044 if (!call->name) {
1045 ret = -ENOMEM;
1046 goto error;
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001047 }
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001048
1049 tp->event->class.system = kstrdup(group, GFP_KERNEL);
1050 if (!tp->event->class.system) {
1051 ret = -ENOMEM;
1052 goto error;
1053 }
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001054
1055 return 0;
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001056
1057error:
1058 trace_probe_cleanup(tp);
1059 return ret;
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001060}
Masami Hiramatsu46e53762019-06-01 00:17:16 +09001061
Masami Hiramatsu8e242062021-08-19 19:26:02 +09001062static struct trace_event_call *
1063find_trace_event_call(const char *system, const char *event_name)
1064{
1065 struct trace_event_call *tp_event;
1066 const char *name;
1067
1068 list_for_each_entry(tp_event, &ftrace_events, list) {
1069 if (!tp_event->class->system ||
1070 strcmp(system, tp_event->class->system))
1071 continue;
1072 name = trace_event_name(tp_event);
1073 if (!name || strcmp(event_name, name))
1074 continue;
1075 return tp_event;
1076 }
1077
1078 return NULL;
1079}
1080
Masami Hiramatsu46e53762019-06-01 00:17:16 +09001081int trace_probe_register_event_call(struct trace_probe *tp)
1082{
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001083 struct trace_event_call *call = trace_probe_event_call(tp);
Masami Hiramatsu46e53762019-06-01 00:17:16 +09001084 int ret;
1085
Masami Hiramatsu8e242062021-08-19 19:26:02 +09001086 lockdep_assert_held(&event_mutex);
1087
1088 if (find_trace_event_call(trace_probe_group_name(tp),
1089 trace_probe_name(tp)))
1090 return -EEXIST;
1091
Masami Hiramatsu46e53762019-06-01 00:17:16 +09001092 ret = register_trace_event(&call->event);
1093 if (!ret)
1094 return -ENODEV;
1095
1096 ret = trace_add_event_call(call);
1097 if (ret)
1098 unregister_trace_event(&call->event);
1099
1100 return ret;
1101}
Masami Hiramatsub5f935e2019-06-01 00:17:26 +09001102
1103int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1104{
1105 struct event_file_link *link;
1106
1107 link = kmalloc(sizeof(*link), GFP_KERNEL);
1108 if (!link)
1109 return -ENOMEM;
1110
1111 link->file = file;
1112 INIT_LIST_HEAD(&link->list);
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001113 list_add_tail_rcu(&link->list, &tp->event->files);
Masami Hiramatsu747774d2019-06-01 00:17:37 +09001114 trace_probe_set_flag(tp, TP_FLAG_TRACE);
Masami Hiramatsub5f935e2019-06-01 00:17:26 +09001115 return 0;
1116}
1117
1118struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1119 struct trace_event_file *file)
1120{
1121 struct event_file_link *link;
1122
1123 trace_probe_for_each_link(link, tp) {
1124 if (link->file == file)
1125 return link;
1126 }
1127
1128 return NULL;
1129}
1130
1131int trace_probe_remove_file(struct trace_probe *tp,
1132 struct trace_event_file *file)
1133{
1134 struct event_file_link *link;
1135
1136 link = trace_probe_get_file_link(tp, file);
1137 if (!link)
1138 return -ENOENT;
1139
1140 list_del_rcu(&link->list);
1141 synchronize_rcu();
1142 kfree(link);
1143
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001144 if (list_empty(&tp->event->files))
Masami Hiramatsu747774d2019-06-01 00:17:37 +09001145 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
Masami Hiramatsub5f935e2019-06-01 00:17:26 +09001146
1147 return 0;
1148}
Masami Hiramatsuca89bc02019-06-20 00:07:49 +09001149
1150/*
1151 * Return the smallest index of different type argument (start from 1).
1152 * If all argument types and name are same, return 0.
1153 */
1154int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1155{
1156 int i;
1157
Masami Hiramatsud2aea952019-09-28 05:53:29 -04001158 /* In case of more arguments */
1159 if (a->nr_args < b->nr_args)
1160 return a->nr_args + 1;
1161 if (a->nr_args > b->nr_args)
1162 return b->nr_args + 1;
1163
Masami Hiramatsuca89bc02019-06-20 00:07:49 +09001164 for (i = 0; i < a->nr_args; i++) {
1165 if ((b->nr_args <= i) ||
1166 ((a->args[i].type != b->args[i].type) ||
1167 (a->args[i].count != b->args[i].count) ||
1168 strcmp(a->args[i].name, b->args[i].name)))
1169 return i + 1;
1170 }
1171
1172 return 0;
1173}
Masami Hiramatsueb5bf812019-06-20 00:08:08 +09001174
1175bool trace_probe_match_command_args(struct trace_probe *tp,
1176 int argc, const char **argv)
1177{
1178 char buf[MAX_ARGSTR_LEN + 1];
1179 int i;
1180
1181 if (tp->nr_args < argc)
1182 return false;
1183
1184 for (i = 0; i < argc; i++) {
1185 snprintf(buf, sizeof(buf), "%s=%s",
1186 tp->args[i].name, tp->args[i].comm);
1187 if (strcmp(buf, argv[i]))
1188 return false;
1189 }
1190 return true;
1191}
Masami Hiramatsud2622712021-02-01 13:48:11 -06001192
1193int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
1194{
1195 int argc = 0, ret = 0;
1196 char **argv;
1197
1198 argv = argv_split(GFP_KERNEL, raw_command, &argc);
1199 if (!argv)
1200 return -ENOMEM;
1201
1202 if (argc)
1203 ret = createfn(argc, (const char **)argv);
1204
1205 argv_free(argv);
1206
1207 return ret;
1208}