blob: 0916a99647191389a17e84e555bec02531e31cd7 [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
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530322 } else
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500323 goto inval_var;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530324
325 return ret;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500326
327inval_var:
328 trace_probe_log_err(offs, BAD_VAR);
329 return -EINVAL;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530330}
331
Masami Hiramatsu6218bf92019-06-20 00:08:27 +0900332static int str_to_immediate(char *str, unsigned long *imm)
333{
334 if (isdigit(str[0]))
335 return kstrtoul(str, 0, imm);
336 else if (str[0] == '-')
337 return kstrtol(str, 0, (long *)imm);
338 else if (str[0] == '+')
339 return kstrtol(str + 1, 0, (long *)imm);
340 return -EINVAL;
341}
342
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900343static int __parse_imm_string(char *str, char **pbuf, int offs)
344{
345 size_t len = strlen(str);
346
347 if (str[len - 1] != '"') {
348 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
349 return -EINVAL;
350 }
351 *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
352 return 0;
353}
354
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530355/* Recursive argument parser */
Masami Hiramatsu53305922018-04-25 21:18:03 +0900356static int
357parse_probe_arg(char *arg, const struct fetch_type *type,
358 struct fetch_insn **pcode, struct fetch_insn *end,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500359 unsigned int flags, int offs)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530360{
Masami Hiramatsu53305922018-04-25 21:18:03 +0900361 struct fetch_insn *code = *pcode;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530362 unsigned long param;
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900363 int deref = FETCH_OP_DEREF;
Steven Rostedt (VMware)bf173ca2018-10-12 12:50:22 -0400364 long offset = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530365 char *tmp;
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900366 int ret = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530367
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530368 switch (arg[0]) {
369 case '$':
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500370 ret = parse_probe_vars(arg + 1, type, code, flags, offs);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530371 break;
372
373 case '%': /* named register */
374 ret = regs_query_register_offset(arg + 1);
375 if (ret >= 0) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900376 code->op = FETCH_OP_REG;
377 code->param = (unsigned int)ret;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530378 ret = 0;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500379 } else
380 trace_probe_log_err(offs, BAD_REG_NAME);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530381 break;
382
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900383 case '@': /* memory, file-offset or symbol */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530384 if (isdigit(arg[1])) {
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200385 ret = kstrtoul(arg + 1, 0, &param);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500386 if (ret) {
387 trace_probe_log_err(offs, BAD_MEM_ADDR);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530388 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500389 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900390 /* load address */
391 code->op = FETCH_OP_IMM;
392 code->immediate = param;
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900393 } else if (arg[1] == '+') {
394 /* kprobes don't support file offsets */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500395 if (flags & TPARG_FL_KERNEL) {
396 trace_probe_log_err(offs, FILE_ON_KPROBE);
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900397 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500398 }
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900399 ret = kstrtol(arg + 2, 0, &offset);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500400 if (ret) {
401 trace_probe_log_err(offs, BAD_FILE_OFFS);
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900402 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500403 }
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900404
Masami Hiramatsu53305922018-04-25 21:18:03 +0900405 code->op = FETCH_OP_FOFFS;
406 code->immediate = (unsigned long)offset; // imm64?
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530407 } else {
Namhyung Kimb079d372013-07-03 18:34:23 +0900408 /* uprobes don't support symbols */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500409 if (!(flags & TPARG_FL_KERNEL)) {
410 trace_probe_log_err(offs, SYM_ON_UPROBE);
Namhyung Kimb079d372013-07-03 18:34:23 +0900411 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500412 }
Masami Hiramatsua6682812018-08-29 01:18:43 +0900413 /* Preserve symbol for updating */
414 code->op = FETCH_NOP_SYMBOL;
415 code->data = kstrdup(arg + 1, GFP_KERNEL);
416 if (!code->data)
417 return -ENOMEM;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500418 if (++code == end) {
419 trace_probe_log_err(offs, TOO_MANY_OPS);
420 return -EINVAL;
421 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900422 code->op = FETCH_OP_IMM;
Masami Hiramatsua6682812018-08-29 01:18:43 +0900423 code->immediate = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530424 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900425 /* These are fetching from memory */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500426 if (++code == end) {
427 trace_probe_log_err(offs, TOO_MANY_OPS);
428 return -EINVAL;
429 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900430 *pcode = code;
431 code->op = FETCH_OP_DEREF;
432 code->offset = offset;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530433 break;
434
435 case '+': /* deref memory */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530436 case '-':
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900437 if (arg[1] == 'u') {
438 deref = FETCH_OP_UDEREF;
439 arg[1] = arg[0];
440 arg++;
441 }
442 if (arg[0] == '+')
443 arg++; /* Skip '+', because kstrtol() rejects it. */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530444 tmp = strchr(arg, '(');
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500445 if (!tmp) {
446 trace_probe_log_err(offs, DEREF_NEED_BRACE);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900447 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500448 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530449 *tmp = '\0';
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200450 ret = kstrtol(arg, 0, &offset);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500451 if (ret) {
452 trace_probe_log_err(offs, BAD_DEREF_OFFS);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530453 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500454 }
455 offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530456 arg = tmp + 1;
457 tmp = strrchr(arg, ')');
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500458 if (!tmp) {
459 trace_probe_log_err(offs + strlen(arg),
460 DEREF_OPEN_BRACE);
461 return -EINVAL;
462 } else {
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900463 const struct fetch_type *t2 = find_fetch_type(NULL);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530464
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530465 *tmp = '\0';
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500466 ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530467 if (ret)
Masami Hiramatsu53305922018-04-25 21:18:03 +0900468 break;
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900469 if (code->op == FETCH_OP_COMM ||
470 code->op == FETCH_OP_DATA) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500471 trace_probe_log_err(offs, COMM_CANT_DEREF);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900472 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500473 }
474 if (++code == end) {
475 trace_probe_log_err(offs, TOO_MANY_OPS);
476 return -EINVAL;
477 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900478 *pcode = code;
479
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900480 code->op = deref;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900481 code->offset = offset;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530482 }
483 break;
Masami Hiramatsu6218bf92019-06-20 00:08:27 +0900484 case '\\': /* Immediate value */
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900485 if (arg[1] == '"') { /* Immediate string */
486 ret = __parse_imm_string(arg + 2, &tmp, offs + 2);
487 if (ret)
488 break;
489 code->op = FETCH_OP_DATA;
490 code->data = tmp;
491 } else {
492 ret = str_to_immediate(arg + 1, &code->immediate);
493 if (ret)
494 trace_probe_log_err(offs + 1, BAD_IMM);
495 else
496 code->op = FETCH_OP_IMM;
497 }
Masami Hiramatsu6218bf92019-06-20 00:08:27 +0900498 break;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530499 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900500 if (!ret && code->op == FETCH_OP_NOP) {
501 /* Parsed, but do not find fetch method */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500502 trace_probe_log_err(offs, BAD_FETCH_ARG);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530503 ret = -EINVAL;
504 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530505 return ret;
506}
507
508#define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
509
510/* Bitfield type needs to be parsed into a fetch function */
511static int __parse_bitfield_probe_arg(const char *bf,
512 const struct fetch_type *t,
Masami Hiramatsu53305922018-04-25 21:18:03 +0900513 struct fetch_insn **pcode)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530514{
Masami Hiramatsu53305922018-04-25 21:18:03 +0900515 struct fetch_insn *code = *pcode;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530516 unsigned long bw, bo;
517 char *tail;
518
519 if (*bf != 'b')
520 return 0;
521
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530522 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
523
524 if (bw == 0 || *tail != '@')
525 return -EINVAL;
526
527 bf = tail + 1;
528 bo = simple_strtoul(bf, &tail, 0);
529
530 if (tail == bf || *tail != '/')
531 return -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900532 code++;
533 if (code->op != FETCH_OP_NOP)
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500534 return -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900535 *pcode = code;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530536
Masami Hiramatsu53305922018-04-25 21:18:03 +0900537 code->op = FETCH_OP_MOD_BF;
538 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
539 code->rshift = BYTES_TO_BITS(t->size) - bw;
540 code->basesize = t->size;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530541
542 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
543}
544
545/* String length checking wrapper */
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400546static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500547 struct probe_arg *parg, unsigned int flags, int offset)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530548{
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900549 struct fetch_insn *code, *scode, *tmp = NULL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500550 char *t, *t2, *t3;
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400551 char *arg;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900552 int ret, len;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530553
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400554 arg = kstrdup(argv, GFP_KERNEL);
555 if (!arg)
556 return -ENOMEM;
557
558 ret = -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500559 len = strlen(arg);
560 if (len > MAX_ARGSTR_LEN) {
561 trace_probe_log_err(offset, ARG_TOO_LONG);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400562 goto out;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500563 } else if (len == 0) {
564 trace_probe_log_err(offset, NO_ARG_BODY);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400565 goto out;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530566 }
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500567
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400568 ret = -ENOMEM;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530569 parg->comm = kstrdup(arg, GFP_KERNEL);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500570 if (!parg->comm)
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400571 goto out;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500572
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400573 ret = -EINVAL;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900574 t = strchr(arg, ':');
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530575 if (t) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900576 *t = '\0';
577 t2 = strchr(++t, '[');
578 if (t2) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500579 *t2++ = '\0';
580 t3 = strchr(t2, ']');
581 if (!t3) {
582 offset += t2 + strlen(t2) - arg;
583 trace_probe_log_err(offset,
584 ARRAY_NO_CLOSE);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400585 goto out;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500586 } else if (t3[1] != '\0') {
587 trace_probe_log_err(offset + t3 + 1 - arg,
588 BAD_ARRAY_SUFFIX);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400589 goto out;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500590 }
591 *t3 = '\0';
592 if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
593 trace_probe_log_err(offset + t2 - arg,
594 BAD_ARRAY_NUM);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400595 goto out;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500596 }
597 if (parg->count > MAX_ARRAY_LEN) {
598 trace_probe_log_err(offset + t2 - arg,
599 ARRAY_TOO_BIG);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400600 goto out;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500601 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900602 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530603 }
Masami Hiramatsu3dd1f7f2019-05-07 22:56:02 +0900604
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900605 /*
Ingo Molnarf2cc0202021-03-23 18:49:35 +0100606 * Since $comm and immediate string can not be dereferenced,
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900607 * we can find those by strcmp.
608 */
609 if (strcmp(arg, "$comm") == 0 || strncmp(arg, "\\\"", 2) == 0) {
Masami Hiramatsu3dd1f7f2019-05-07 22:56:02 +0900610 /* The type of $comm must be "string", and not an array. */
611 if (parg->count || (t && strcmp(t, "string")))
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400612 goto out;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900613 parg->type = find_fetch_type("string");
Masami Hiramatsu3dd1f7f2019-05-07 22:56:02 +0900614 } else
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900615 parg->type = find_fetch_type(t);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530616 if (!parg->type) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500617 trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400618 goto out;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530619 }
620 parg->offset = *size;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900621 *size += parg->type->size * (parg->count ?: 1);
622
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400623 ret = -ENOMEM;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900624 if (parg->count) {
625 len = strlen(parg->type->fmttype) + 6;
626 parg->fmt = kmalloc(len, GFP_KERNEL);
627 if (!parg->fmt)
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400628 goto out;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900629 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
630 parg->count);
631 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530632
Gustavo A. R. Silva8623b002019-01-14 22:34:08 -0600633 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900634 if (!code)
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400635 goto out;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900636 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530637
Masami Hiramatsu53305922018-04-25 21:18:03 +0900638 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500639 flags, offset);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900640 if (ret)
641 goto fail;
642
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400643 ret = -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900644 /* Store operation */
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900645 if (!strcmp(parg->type->name, "string") ||
646 !strcmp(parg->type->name, "ustring")) {
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900647 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900648 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
649 code->op != FETCH_OP_DATA) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500650 trace_probe_log_err(offset + (t ? (t - arg) : 0),
651 BAD_STRING);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900652 goto fail;
653 }
Vamshi K Sthambamkadi3aa8fdc2020-06-15 20:00:38 +0530654 if ((code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
655 code->op == FETCH_OP_DATA) || parg->count) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900656 /*
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900657 * IMM, DATA and COMM is pointing actual address, those
658 * must be kept, and if parg->count != 0, this is an
659 * array of string pointers instead of string address
660 * itself.
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900661 */
Masami Hiramatsu53305922018-04-25 21:18:03 +0900662 code++;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900663 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500664 trace_probe_log_err(offset, TOO_MANY_OPS);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900665 goto fail;
666 }
667 }
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900668 /* If op == DEREF, replace it with STRING */
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900669 if (!strcmp(parg->type->name, "ustring") ||
670 code->op == FETCH_OP_UDEREF)
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900671 code->op = FETCH_OP_ST_USTRING;
672 else
673 code->op = FETCH_OP_ST_STRING;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900674 code->size = parg->type->size;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900675 parg->dynamic = true;
676 } else if (code->op == FETCH_OP_DEREF) {
677 code->op = FETCH_OP_ST_MEM;
678 code->size = parg->type->size;
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900679 } else if (code->op == FETCH_OP_UDEREF) {
680 code->op = FETCH_OP_ST_UMEM;
681 code->size = parg->type->size;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900682 } else {
683 code++;
684 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500685 trace_probe_log_err(offset, TOO_MANY_OPS);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900686 goto fail;
687 }
688 code->op = FETCH_OP_ST_RAW;
689 code->size = parg->type->size;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530690 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900691 scode = code;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900692 /* Modify operation */
693 if (t != NULL) {
694 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500695 if (ret) {
696 trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900697 goto fail;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500698 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900699 }
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400700 ret = -EINVAL;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900701 /* Loop(Array) operation */
702 if (parg->count) {
703 if (scode->op != FETCH_OP_ST_MEM &&
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900704 scode->op != FETCH_OP_ST_STRING &&
705 scode->op != FETCH_OP_ST_USTRING) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500706 trace_probe_log_err(offset + (t ? (t - arg) : 0),
707 BAD_STRING);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900708 goto fail;
709 }
710 code++;
711 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500712 trace_probe_log_err(offset, TOO_MANY_OPS);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900713 goto fail;
714 }
715 code->op = FETCH_OP_LP_ARRAY;
716 code->param = parg->count;
717 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900718 code++;
719 code->op = FETCH_OP_END;
720
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400721 ret = 0;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900722 /* Shrink down the code buffer */
Gustavo A. R. Silva8623b002019-01-14 22:34:08 -0600723 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900724 if (!parg->code)
725 ret = -ENOMEM;
726 else
727 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
728
729fail:
Masami Hiramatsua6682812018-08-29 01:18:43 +0900730 if (ret) {
731 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900732 if (code->op == FETCH_NOP_SYMBOL ||
733 code->op == FETCH_OP_DATA)
Masami Hiramatsua6682812018-08-29 01:18:43 +0900734 kfree(code->data);
735 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900736 kfree(tmp);
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400737out:
738 kfree(arg);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530739
740 return ret;
741}
742
743/* Return 1 if name is reserved or already used by another argument */
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900744static int traceprobe_conflict_field_name(const char *name,
745 struct probe_arg *args, int narg)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530746{
747 int i;
748
749 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
750 if (strcmp(reserved_field_names[i], name) == 0)
751 return 1;
752
753 for (i = 0; i < narg; i++)
754 if (strcmp(args[i].name, name) == 0)
755 return 1;
756
757 return 0;
758}
759
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400760int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900761 unsigned int flags)
762{
763 struct probe_arg *parg = &tp->args[i];
Steven Rostedt (VMware)fcd9db52021-08-16 23:42:58 -0400764 const char *body;
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900765
766 /* Increment count for freeing args in error case */
767 tp->nr_args++;
768
769 body = strchr(arg, '=');
770 if (body) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500771 if (body - arg > MAX_ARG_NAME_LEN) {
772 trace_probe_log_err(0, ARG_NAME_TOO_LONG);
Masami Hiramatsub4443c12019-03-14 13:30:30 +0900773 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500774 } else if (body == arg) {
775 trace_probe_log_err(0, NO_ARG_NAME);
776 return -EINVAL;
777 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900778 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
779 body++;
780 } else {
781 /* If argument name is omitted, set "argN" */
782 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
783 body = arg;
784 }
785 if (!parg->name)
786 return -ENOMEM;
787
788 if (!is_good_name(parg->name)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500789 trace_probe_log_err(0, BAD_ARG_NAME);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900790 return -EINVAL;
791 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900792 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500793 trace_probe_log_err(0, USED_ARG_NAME);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900794 return -EINVAL;
795 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900796 /* Parse fetch argument */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500797 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
798 body - arg);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900799}
800
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530801void traceprobe_free_probe_arg(struct probe_arg *arg)
802{
Masami Hiramatsua6682812018-08-29 01:18:43 +0900803 struct fetch_insn *code = arg->code;
804
805 while (code && code->op != FETCH_OP_END) {
Masami Hiramatsua42e3c42019-06-20 00:08:37 +0900806 if (code->op == FETCH_NOP_SYMBOL ||
807 code->op == FETCH_OP_DATA)
Masami Hiramatsua6682812018-08-29 01:18:43 +0900808 kfree(code->data);
809 code++;
810 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900811 kfree(arg->code);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530812 kfree(arg->name);
813 kfree(arg->comm);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900814 kfree(arg->fmt);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530815}
816
Masami Hiramatsua6682812018-08-29 01:18:43 +0900817int traceprobe_update_arg(struct probe_arg *arg)
818{
819 struct fetch_insn *code = arg->code;
820 long offset;
821 char *tmp;
822 char c;
823 int ret = 0;
824
825 while (code && code->op != FETCH_OP_END) {
826 if (code->op == FETCH_NOP_SYMBOL) {
827 if (code[1].op != FETCH_OP_IMM)
828 return -EINVAL;
829
Masami Hiramatsuee474b82018-11-01 23:29:28 +0900830 tmp = strpbrk(code->data, "+-");
Masami Hiramatsua6682812018-08-29 01:18:43 +0900831 if (tmp)
832 c = *tmp;
833 ret = traceprobe_split_symbol_offset(code->data,
834 &offset);
835 if (ret)
836 return ret;
837
838 code[1].immediate =
839 (unsigned long)kallsyms_lookup_name(code->data);
840 if (tmp)
841 *tmp = c;
842 if (!code[1].immediate)
843 return -ENOENT;
844 code[1].immediate += offset;
845 }
846 code++;
847 }
848 return 0;
849}
850
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900851/* When len=0, we just calculate the needed length */
852#define LEN_OR_ZERO (len ? len - pos : 0)
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900853static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
854 bool is_return)
855{
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900856 struct probe_arg *parg;
857 int i, j;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900858 int pos = 0;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900859 const char *fmt, *arg;
860
861 if (!is_return) {
862 fmt = "(%lx)";
863 arg = "REC->" FIELD_STRING_IP;
864 } else {
865 fmt = "(%lx <- %lx)";
866 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
867 }
868
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900869 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
870
871 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900872 parg = tp->args + i;
873 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
874 if (parg->count) {
875 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
876 parg->type->fmt);
877 for (j = 1; j < parg->count; j++)
878 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
879 parg->type->fmt);
880 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
881 } else
882 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
883 parg->type->fmt);
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900884 }
885
886 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
887
888 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900889 parg = tp->args + i;
890 if (parg->count) {
Steven Rostedt (VMware)20279422020-01-24 10:07:42 -0500891 if ((strcmp(parg->type->name, "string") == 0) ||
892 (strcmp(parg->type->name, "ustring") == 0))
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900893 fmt = ", __get_str(%s[%d])";
894 else
895 fmt = ", REC->%s[%d]";
896 for (j = 0; j < parg->count; j++)
897 pos += snprintf(buf + pos, LEN_OR_ZERO,
898 fmt, parg->name, j);
899 } else {
Steven Rostedt (VMware)20279422020-01-24 10:07:42 -0500900 if ((strcmp(parg->type->name, "string") == 0) ||
901 (strcmp(parg->type->name, "ustring") == 0))
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900902 fmt = ", __get_str(%s)";
903 else
904 fmt = ", REC->%s";
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900905 pos += snprintf(buf + pos, LEN_OR_ZERO,
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900906 fmt, parg->name);
907 }
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900908 }
909
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900910 /* return the length of print_fmt */
911 return pos;
912}
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900913#undef LEN_OR_ZERO
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900914
Masami Hiramatsu0a46c852018-04-25 21:19:30 +0900915int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return)
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900916{
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +0900917 struct trace_event_call *call = trace_probe_event_call(tp);
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900918 int len;
919 char *print_fmt;
920
921 /* First: called with 0 length to calculate the needed length */
922 len = __set_print_fmt(tp, NULL, 0, is_return);
923 print_fmt = kmalloc(len + 1, GFP_KERNEL);
924 if (!print_fmt)
925 return -ENOMEM;
926
927 /* Second: actually write the @print_fmt */
928 __set_print_fmt(tp, print_fmt, len + 1, is_return);
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +0900929 call->print_fmt = print_fmt;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900930
931 return 0;
932}
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900933
934int traceprobe_define_arg_fields(struct trace_event_call *event_call,
935 size_t offset, struct trace_probe *tp)
936{
937 int ret, i;
938
939 /* Set argument names as fields */
940 for (i = 0; i < tp->nr_args; i++) {
941 struct probe_arg *parg = &tp->args[i];
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900942 const char *fmt = parg->type->fmttype;
943 int size = parg->type->size;
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900944
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900945 if (parg->fmt)
946 fmt = parg->fmt;
947 if (parg->count)
948 size *= parg->count;
949 ret = trace_define_field(event_call, fmt, parg->name,
950 offset + parg->offset, size,
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900951 parg->type->is_signed,
952 FILTER_OTHER);
953 if (ret)
954 return ret;
955 }
956 return 0;
957}
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900958
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900959static void trace_probe_event_free(struct trace_probe_event *tpe)
960{
961 kfree(tpe->class.system);
962 kfree(tpe->call.name);
963 kfree(tpe->call.print_fmt);
964 kfree(tpe);
965}
966
967int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
968{
969 if (trace_probe_has_sibling(tp))
970 return -EBUSY;
971
972 list_del_init(&tp->list);
973 trace_probe_event_free(tp->event);
974
975 tp->event = to->event;
976 list_add_tail(&tp->list, trace_probe_probe_list(to));
977
978 return 0;
979}
980
981void trace_probe_unlink(struct trace_probe *tp)
982{
983 list_del_init(&tp->list);
984 if (list_empty(trace_probe_probe_list(tp)))
985 trace_probe_event_free(tp->event);
986 tp->event = NULL;
987}
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900988
989void trace_probe_cleanup(struct trace_probe *tp)
990{
991 int i;
992
993 for (i = 0; i < tp->nr_args; i++)
994 traceprobe_free_probe_arg(&tp->args[i]);
995
Masami Hiramatsuca89bc02019-06-20 00:07:49 +0900996 if (tp->event)
997 trace_probe_unlink(tp);
Masami Hiramatsu455b2892019-06-01 00:17:06 +0900998}
999
1000int trace_probe_init(struct trace_probe *tp, const char *event,
Masami Hiramatsub61387c2020-01-22 12:23:25 +09001001 const char *group, bool alloc_filter)
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001002{
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001003 struct trace_event_call *call;
Masami Hiramatsub61387c2020-01-22 12:23:25 +09001004 size_t size = sizeof(struct trace_probe_event);
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001005 int ret = 0;
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001006
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001007 if (!event || !group)
1008 return -EINVAL;
1009
Masami Hiramatsub61387c2020-01-22 12:23:25 +09001010 if (alloc_filter)
1011 size += sizeof(struct trace_uprobe_filter);
1012
1013 tp->event = kzalloc(size, GFP_KERNEL);
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001014 if (!tp->event)
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001015 return -ENOMEM;
1016
Masami Hiramatsud59fae62019-09-17 14:11:37 +09001017 INIT_LIST_HEAD(&tp->event->files);
1018 INIT_LIST_HEAD(&tp->event->class.fields);
1019 INIT_LIST_HEAD(&tp->event->probes);
1020 INIT_LIST_HEAD(&tp->list);
Julia Lawallfc9d2762020-05-07 21:30:08 +02001021 list_add(&tp->list, &tp->event->probes);
Masami Hiramatsud59fae62019-09-17 14:11:37 +09001022
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001023 call = trace_probe_event_call(tp);
1024 call->class = &tp->event->class;
1025 call->name = kstrdup(event, GFP_KERNEL);
1026 if (!call->name) {
1027 ret = -ENOMEM;
1028 goto error;
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001029 }
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001030
1031 tp->event->class.system = kstrdup(group, GFP_KERNEL);
1032 if (!tp->event->class.system) {
1033 ret = -ENOMEM;
1034 goto error;
1035 }
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001036
1037 return 0;
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001038
1039error:
1040 trace_probe_cleanup(tp);
1041 return ret;
Masami Hiramatsu455b2892019-06-01 00:17:06 +09001042}
Masami Hiramatsu46e53762019-06-01 00:17:16 +09001043
1044int trace_probe_register_event_call(struct trace_probe *tp)
1045{
Masami Hiramatsue3dc9f82019-06-01 00:17:57 +09001046 struct trace_event_call *call = trace_probe_event_call(tp);
Masami Hiramatsu46e53762019-06-01 00:17:16 +09001047 int ret;
1048
1049 ret = register_trace_event(&call->event);
1050 if (!ret)
1051 return -ENODEV;
1052
1053 ret = trace_add_event_call(call);
1054 if (ret)
1055 unregister_trace_event(&call->event);
1056
1057 return ret;
1058}
Masami Hiramatsub5f935e2019-06-01 00:17:26 +09001059
1060int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1061{
1062 struct event_file_link *link;
1063
1064 link = kmalloc(sizeof(*link), GFP_KERNEL);
1065 if (!link)
1066 return -ENOMEM;
1067
1068 link->file = file;
1069 INIT_LIST_HEAD(&link->list);
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001070 list_add_tail_rcu(&link->list, &tp->event->files);
Masami Hiramatsu747774d2019-06-01 00:17:37 +09001071 trace_probe_set_flag(tp, TP_FLAG_TRACE);
Masami Hiramatsub5f935e2019-06-01 00:17:26 +09001072 return 0;
1073}
1074
1075struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1076 struct trace_event_file *file)
1077{
1078 struct event_file_link *link;
1079
1080 trace_probe_for_each_link(link, tp) {
1081 if (link->file == file)
1082 return link;
1083 }
1084
1085 return NULL;
1086}
1087
1088int trace_probe_remove_file(struct trace_probe *tp,
1089 struct trace_event_file *file)
1090{
1091 struct event_file_link *link;
1092
1093 link = trace_probe_get_file_link(tp, file);
1094 if (!link)
1095 return -ENOENT;
1096
1097 list_del_rcu(&link->list);
1098 synchronize_rcu();
1099 kfree(link);
1100
Masami Hiramatsu60d53e22019-06-20 00:07:20 +09001101 if (list_empty(&tp->event->files))
Masami Hiramatsu747774d2019-06-01 00:17:37 +09001102 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
Masami Hiramatsub5f935e2019-06-01 00:17:26 +09001103
1104 return 0;
1105}
Masami Hiramatsuca89bc02019-06-20 00:07:49 +09001106
1107/*
1108 * Return the smallest index of different type argument (start from 1).
1109 * If all argument types and name are same, return 0.
1110 */
1111int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1112{
1113 int i;
1114
Masami Hiramatsud2aea952019-09-28 05:53:29 -04001115 /* In case of more arguments */
1116 if (a->nr_args < b->nr_args)
1117 return a->nr_args + 1;
1118 if (a->nr_args > b->nr_args)
1119 return b->nr_args + 1;
1120
Masami Hiramatsuca89bc02019-06-20 00:07:49 +09001121 for (i = 0; i < a->nr_args; i++) {
1122 if ((b->nr_args <= i) ||
1123 ((a->args[i].type != b->args[i].type) ||
1124 (a->args[i].count != b->args[i].count) ||
1125 strcmp(a->args[i].name, b->args[i].name)))
1126 return i + 1;
1127 }
1128
1129 return 0;
1130}
Masami Hiramatsueb5bf812019-06-20 00:08:08 +09001131
1132bool trace_probe_match_command_args(struct trace_probe *tp,
1133 int argc, const char **argv)
1134{
1135 char buf[MAX_ARGSTR_LEN + 1];
1136 int i;
1137
1138 if (tp->nr_args < argc)
1139 return false;
1140
1141 for (i = 0; i < argc; i++) {
1142 snprintf(buf, sizeof(buf), "%s=%s",
1143 tp->args[i].name, tp->args[i].comm);
1144 if (strcmp(buf, argv[i]))
1145 return false;
1146 }
1147 return true;
1148}
Masami Hiramatsud2622712021-02-01 13:48:11 -06001149
1150int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
1151{
1152 int argc = 0, ret = 0;
1153 char **argv;
1154
1155 argv = argv_split(GFP_KERNEL, raw_command, &argc);
1156 if (!argv)
1157 return -ENOMEM;
1158
1159 if (argc)
1160 ret = createfn(argc, (const char **)argv);
1161
1162 argv_free(argv);
1163
1164 return ret;
1165}