blob: b6b0593844cde765deab726d33c90a811c562b25 [file] [log] [blame]
Steven Rostedt (VMware)bcea3f92018-08-16 11:23:53 -04001// SPDX-License-Identifier: GPL-2.0
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +05302/*
3 * Common code for probe-based Dynamic events.
4 *
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +05305 * This code was copied from kernel/trace/trace_kprobe.c written by
6 * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
7 *
8 * Updates to make this generic:
9 * Copyright (C) IBM Corporation, 2010-2011
10 * Author: Srikar Dronamraju
11 */
Masami Hiramatsu72576342017-02-07 20:21:28 +090012#define pr_fmt(fmt) "trace_probe: " fmt
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053013
14#include "trace_probe.h"
15
Masami Hiramatsuab105a42019-03-31 18:48:19 -050016#undef C
17#define C(a, b) b
18
19static const char *trace_probe_err_text[] = { ERRORS };
20
Valdis Klētnieks08416252019-03-12 04:52:58 -040021static const char *reserved_field_names[] = {
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053022 "common_type",
23 "common_flags",
24 "common_preempt_count",
25 "common_pid",
26 "common_tgid",
27 FIELD_STRING_IP,
28 FIELD_STRING_RETIP,
29 FIELD_STRING_FUNC,
30};
31
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053032/* Printing in basic type function template */
Masami Hiramatsu17ce3dc2016-08-18 17:57:50 +090033#define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \
Masami Hiramatsu56de7632018-04-25 21:16:36 +090034int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053035{ \
Masami Hiramatsu56de7632018-04-25 21:16:36 +090036 trace_seq_printf(s, fmt, *(type *)data); \
Steven Rostedt (Red Hat)d2b01912014-11-12 17:19:51 -050037 return !trace_seq_has_overflowed(s); \
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053038} \
Masami Hiramatsu7bfbc632018-04-25 21:17:34 +090039const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053040
Masami Hiramatsubdca79c22016-08-18 17:59:21 +090041DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u")
42DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
43DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
44DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
Masami Hiramatsu17ce3dc2016-08-18 17:57:50 +090045DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d")
46DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
47DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
48DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
49DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x")
50DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
51DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
52DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053053
Masami Hiramatsu60c2e0c2018-04-25 21:20:28 +090054int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
55{
56 trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
57 return !trace_seq_has_overflowed(s);
58}
59const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
60
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053061/* Print type function for string type */
Masami Hiramatsu56de7632018-04-25 21:16:36 +090062int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053063{
64 int len = *(u32 *)data >> 16;
65
66 if (!len)
Masami Hiramatsu56de7632018-04-25 21:16:36 +090067 trace_seq_puts(s, "(fault)");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053068 else
Masami Hiramatsu56de7632018-04-25 21:16:36 +090069 trace_seq_printf(s, "\"%s\"",
Steven Rostedt (Red Hat)d2b01912014-11-12 17:19:51 -050070 (const char *)get_loc_data(data, ent));
71 return !trace_seq_has_overflowed(s);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053072}
73
Namhyung Kimb26c74e2013-11-26 14:19:59 +090074const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053075
Masami Hiramatsuf451bc82018-04-25 21:18:32 +090076/* Fetch type information table */
77static const struct fetch_type probe_fetch_types[] = {
78 /* Special types */
79 __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1,
80 "__data_loc char[]"),
Masami Hiramatsu88903c42019-05-15 14:38:30 +090081 __ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1,
82 "__data_loc char[]"),
Masami Hiramatsuf451bc82018-04-25 21:18:32 +090083 /* Basic types */
84 ASSIGN_FETCH_TYPE(u8, u8, 0),
85 ASSIGN_FETCH_TYPE(u16, u16, 0),
86 ASSIGN_FETCH_TYPE(u32, u32, 0),
87 ASSIGN_FETCH_TYPE(u64, u64, 0),
88 ASSIGN_FETCH_TYPE(s8, u8, 1),
89 ASSIGN_FETCH_TYPE(s16, u16, 1),
90 ASSIGN_FETCH_TYPE(s32, u32, 1),
91 ASSIGN_FETCH_TYPE(s64, u64, 1),
92 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
93 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
94 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
95 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
Masami Hiramatsu60c2e0c2018-04-25 21:20:28 +090096 ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
Masami Hiramatsuf451bc82018-04-25 21:18:32 +090097
98 ASSIGN_FETCH_TYPE_END
99};
100
101static const struct fetch_type *find_fetch_type(const char *type)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530102{
103 int i;
104
105 if (!type)
106 type = DEFAULT_FETCH_TYPE_STR;
107
108 /* Special case: bitfield */
109 if (*type == 'b') {
110 unsigned long bs;
111
112 type = strchr(type, '/');
113 if (!type)
114 goto fail;
115
116 type++;
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200117 if (kstrtoul(type, 0, &bs))
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530118 goto fail;
119
120 switch (bs) {
121 case 8:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900122 return find_fetch_type("u8");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530123 case 16:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900124 return find_fetch_type("u16");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530125 case 32:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900126 return find_fetch_type("u32");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530127 case 64:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900128 return find_fetch_type("u64");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530129 default:
130 goto fail;
131 }
132 }
133
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900134 for (i = 0; probe_fetch_types[i].name; i++) {
135 if (strcmp(type, probe_fetch_types[i].name) == 0)
136 return &probe_fetch_types[i];
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900137 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530138
139fail:
140 return NULL;
141}
142
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500143static struct trace_probe_log trace_probe_log;
144
145void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
146{
147 trace_probe_log.subsystem = subsystem;
148 trace_probe_log.argc = argc;
149 trace_probe_log.argv = argv;
150 trace_probe_log.index = 0;
151}
152
153void trace_probe_log_clear(void)
154{
155 memset(&trace_probe_log, 0, sizeof(trace_probe_log));
156}
157
158void trace_probe_log_set_index(int index)
159{
160 trace_probe_log.index = index;
161}
162
163void __trace_probe_log_err(int offset, int err_type)
164{
165 char *command, *p;
166 int i, len = 0, pos = 0;
167
168 if (!trace_probe_log.argv)
169 return;
170
171 /* Recalcurate the length and allocate buffer */
172 for (i = 0; i < trace_probe_log.argc; i++) {
173 if (i == trace_probe_log.index)
174 pos = len;
175 len += strlen(trace_probe_log.argv[i]) + 1;
176 }
177 command = kzalloc(len, GFP_KERNEL);
178 if (!command)
179 return;
180
181 /* And make a command string from argv array */
182 p = command;
183 for (i = 0; i < trace_probe_log.argc; i++) {
184 len = strlen(trace_probe_log.argv[i]);
185 strcpy(p, trace_probe_log.argv[i]);
186 p[len] = ' ';
187 p += len + 1;
188 }
189 *(p - 1) = '\0';
190
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -0400191 tracing_log_err(NULL, trace_probe_log.subsystem, command,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500192 trace_probe_err_text, err_type, pos + offset);
193
194 kfree(command);
195}
196
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530197/* Split symbol and offset. */
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900198int traceprobe_split_symbol_offset(char *symbol, long *offset)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530199{
200 char *tmp;
201 int ret;
202
203 if (!offset)
204 return -EINVAL;
205
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900206 tmp = strpbrk(symbol, "+-");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530207 if (tmp) {
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900208 ret = kstrtol(tmp, 0, offset);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530209 if (ret)
210 return ret;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530211 *tmp = '\0';
212 } else
213 *offset = 0;
214
215 return 0;
216}
217
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900218/* @buf must has MAX_EVENT_NAME_LEN size */
219int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500220 char *buf, int offset)
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900221{
222 const char *slash, *event = *pevent;
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900223 int len;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900224
225 slash = strchr(event, '/');
226 if (slash) {
227 if (slash == event) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500228 trace_probe_log_err(offset, NO_GROUP_NAME);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900229 return -EINVAL;
230 }
231 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500232 trace_probe_log_err(offset, GROUP_TOO_LONG);
233 return -EINVAL;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900234 }
235 strlcpy(buf, event, slash - event + 1);
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900236 if (!is_good_name(buf)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500237 trace_probe_log_err(offset, BAD_GROUP_NAME);
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900238 return -EINVAL;
239 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900240 *pgroup = buf;
241 *pevent = slash + 1;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500242 offset += slash - event + 1;
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900243 event = *pevent;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900244 }
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900245 len = strlen(event);
246 if (len == 0) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500247 trace_probe_log_err(offset, NO_EVENT_NAME);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900248 return -EINVAL;
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900249 } else if (len > MAX_EVENT_NAME_LEN) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500250 trace_probe_log_err(offset, EVENT_TOO_LONG);
251 return -EINVAL;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900252 }
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900253 if (!is_good_name(event)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500254 trace_probe_log_err(offset, BAD_EVENT_NAME);
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900255 return -EINVAL;
256 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900257 return 0;
258}
259
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530260#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
261
262static int parse_probe_vars(char *arg, const struct fetch_type *t,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500263 struct fetch_insn *code, unsigned int flags, int offs)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530264{
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530265 unsigned long param;
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500266 int ret = 0;
267 int len;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530268
269 if (strcmp(arg, "retval") == 0) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500270 if (flags & TPARG_FL_RETURN) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900271 code->op = FETCH_OP_RETVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500272 } else {
273 trace_probe_log_err(offs, RETVAL_ON_PROBE);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530274 ret = -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500275 }
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500276 } else if ((len = str_has_prefix(arg, "stack"))) {
277 if (arg[len] == '\0') {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900278 code->op = FETCH_OP_STACKP;
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500279 } else if (isdigit(arg[len])) {
280 ret = kstrtoul(arg + len, 10, &param);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500281 if (ret) {
282 goto inval_var;
283 } else if ((flags & TPARG_FL_KERNEL) &&
284 param > PARAM_MAX_STACK) {
285 trace_probe_log_err(offs, BAD_STACK_NUM);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530286 ret = -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500287 } else {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900288 code->op = FETCH_OP_STACK;
289 code->param = (unsigned int)param;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530290 }
291 } else
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500292 goto inval_var;
Omar Sandoval35abb672016-06-08 18:38:02 -0700293 } else if (strcmp(arg, "comm") == 0) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900294 code->op = FETCH_OP_COMM;
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900295#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
296 } else if (((flags & TPARG_FL_MASK) ==
297 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500298 (len = str_has_prefix(arg, "arg"))) {
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500299 ret = kstrtoul(arg + len, 10, &param);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500300 if (ret) {
301 goto inval_var;
302 } else if (!param || param > PARAM_MAX_STACK) {
303 trace_probe_log_err(offs, BAD_ARG_NUM);
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900304 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500305 }
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900306 code->op = FETCH_OP_ARG;
307 code->param = (unsigned int)param - 1;
308#endif
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530309 } else
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500310 goto inval_var;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530311
312 return ret;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500313
314inval_var:
315 trace_probe_log_err(offs, BAD_VAR);
316 return -EINVAL;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530317}
318
319/* Recursive argument parser */
Masami Hiramatsu53305922018-04-25 21:18:03 +0900320static int
321parse_probe_arg(char *arg, const struct fetch_type *type,
322 struct fetch_insn **pcode, struct fetch_insn *end,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500323 unsigned int flags, int offs)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530324{
Masami Hiramatsu53305922018-04-25 21:18:03 +0900325 struct fetch_insn *code = *pcode;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530326 unsigned long param;
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900327 int deref = FETCH_OP_DEREF;
Steven Rostedt (VMware)bf173ca2018-10-12 12:50:22 -0400328 long offset = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530329 char *tmp;
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900330 int ret = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530331
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530332 switch (arg[0]) {
333 case '$':
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500334 ret = parse_probe_vars(arg + 1, type, code, flags, offs);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530335 break;
336
337 case '%': /* named register */
338 ret = regs_query_register_offset(arg + 1);
339 if (ret >= 0) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900340 code->op = FETCH_OP_REG;
341 code->param = (unsigned int)ret;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530342 ret = 0;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500343 } else
344 trace_probe_log_err(offs, BAD_REG_NAME);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530345 break;
346
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900347 case '@': /* memory, file-offset or symbol */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530348 if (isdigit(arg[1])) {
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200349 ret = kstrtoul(arg + 1, 0, &param);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500350 if (ret) {
351 trace_probe_log_err(offs, BAD_MEM_ADDR);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530352 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500353 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900354 /* load address */
355 code->op = FETCH_OP_IMM;
356 code->immediate = param;
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900357 } else if (arg[1] == '+') {
358 /* kprobes don't support file offsets */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500359 if (flags & TPARG_FL_KERNEL) {
360 trace_probe_log_err(offs, FILE_ON_KPROBE);
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900361 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500362 }
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900363 ret = kstrtol(arg + 2, 0, &offset);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500364 if (ret) {
365 trace_probe_log_err(offs, BAD_FILE_OFFS);
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900366 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500367 }
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900368
Masami Hiramatsu53305922018-04-25 21:18:03 +0900369 code->op = FETCH_OP_FOFFS;
370 code->immediate = (unsigned long)offset; // imm64?
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530371 } else {
Namhyung Kimb079d372013-07-03 18:34:23 +0900372 /* uprobes don't support symbols */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500373 if (!(flags & TPARG_FL_KERNEL)) {
374 trace_probe_log_err(offs, SYM_ON_UPROBE);
Namhyung Kimb079d372013-07-03 18:34:23 +0900375 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500376 }
Masami Hiramatsua6682812018-08-29 01:18:43 +0900377 /* Preserve symbol for updating */
378 code->op = FETCH_NOP_SYMBOL;
379 code->data = kstrdup(arg + 1, GFP_KERNEL);
380 if (!code->data)
381 return -ENOMEM;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500382 if (++code == end) {
383 trace_probe_log_err(offs, TOO_MANY_OPS);
384 return -EINVAL;
385 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900386 code->op = FETCH_OP_IMM;
Masami Hiramatsua6682812018-08-29 01:18:43 +0900387 code->immediate = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530388 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900389 /* These are fetching from memory */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500390 if (++code == end) {
391 trace_probe_log_err(offs, TOO_MANY_OPS);
392 return -EINVAL;
393 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900394 *pcode = code;
395 code->op = FETCH_OP_DEREF;
396 code->offset = offset;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530397 break;
398
399 case '+': /* deref memory */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530400 case '-':
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900401 if (arg[1] == 'u') {
402 deref = FETCH_OP_UDEREF;
403 arg[1] = arg[0];
404 arg++;
405 }
406 if (arg[0] == '+')
407 arg++; /* Skip '+', because kstrtol() rejects it. */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530408 tmp = strchr(arg, '(');
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500409 if (!tmp) {
410 trace_probe_log_err(offs, DEREF_NEED_BRACE);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900411 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500412 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530413 *tmp = '\0';
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200414 ret = kstrtol(arg, 0, &offset);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500415 if (ret) {
416 trace_probe_log_err(offs, BAD_DEREF_OFFS);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530417 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500418 }
419 offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530420 arg = tmp + 1;
421 tmp = strrchr(arg, ')');
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500422 if (!tmp) {
423 trace_probe_log_err(offs + strlen(arg),
424 DEREF_OPEN_BRACE);
425 return -EINVAL;
426 } else {
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900427 const struct fetch_type *t2 = find_fetch_type(NULL);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530428
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530429 *tmp = '\0';
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500430 ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530431 if (ret)
Masami Hiramatsu53305922018-04-25 21:18:03 +0900432 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500433 if (code->op == FETCH_OP_COMM) {
434 trace_probe_log_err(offs, COMM_CANT_DEREF);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900435 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500436 }
437 if (++code == end) {
438 trace_probe_log_err(offs, TOO_MANY_OPS);
439 return -EINVAL;
440 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900441 *pcode = code;
442
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900443 code->op = deref;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900444 code->offset = offset;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530445 }
446 break;
447 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900448 if (!ret && code->op == FETCH_OP_NOP) {
449 /* Parsed, but do not find fetch method */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500450 trace_probe_log_err(offs, BAD_FETCH_ARG);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530451 ret = -EINVAL;
452 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530453 return ret;
454}
455
456#define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
457
458/* Bitfield type needs to be parsed into a fetch function */
459static int __parse_bitfield_probe_arg(const char *bf,
460 const struct fetch_type *t,
Masami Hiramatsu53305922018-04-25 21:18:03 +0900461 struct fetch_insn **pcode)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530462{
Masami Hiramatsu53305922018-04-25 21:18:03 +0900463 struct fetch_insn *code = *pcode;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530464 unsigned long bw, bo;
465 char *tail;
466
467 if (*bf != 'b')
468 return 0;
469
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530470 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
471
472 if (bw == 0 || *tail != '@')
473 return -EINVAL;
474
475 bf = tail + 1;
476 bo = simple_strtoul(bf, &tail, 0);
477
478 if (tail == bf || *tail != '/')
479 return -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900480 code++;
481 if (code->op != FETCH_OP_NOP)
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500482 return -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900483 *pcode = code;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530484
Masami Hiramatsu53305922018-04-25 21:18:03 +0900485 code->op = FETCH_OP_MOD_BF;
486 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
487 code->rshift = BYTES_TO_BITS(t->size) - bw;
488 code->basesize = t->size;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530489
490 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
491}
492
493/* String length checking wrapper */
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900494static int traceprobe_parse_probe_arg_body(char *arg, ssize_t *size,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500495 struct probe_arg *parg, unsigned int flags, int offset)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530496{
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900497 struct fetch_insn *code, *scode, *tmp = NULL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500498 char *t, *t2, *t3;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900499 int ret, len;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530500
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500501 len = strlen(arg);
502 if (len > MAX_ARGSTR_LEN) {
503 trace_probe_log_err(offset, ARG_TOO_LONG);
504 return -EINVAL;
505 } else if (len == 0) {
506 trace_probe_log_err(offset, NO_ARG_BODY);
507 return -EINVAL;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530508 }
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500509
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530510 parg->comm = kstrdup(arg, GFP_KERNEL);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500511 if (!parg->comm)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530512 return -ENOMEM;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500513
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900514 t = strchr(arg, ':');
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530515 if (t) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900516 *t = '\0';
517 t2 = strchr(++t, '[');
518 if (t2) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500519 *t2++ = '\0';
520 t3 = strchr(t2, ']');
521 if (!t3) {
522 offset += t2 + strlen(t2) - arg;
523 trace_probe_log_err(offset,
524 ARRAY_NO_CLOSE);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900525 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500526 } else if (t3[1] != '\0') {
527 trace_probe_log_err(offset + t3 + 1 - arg,
528 BAD_ARRAY_SUFFIX);
529 return -EINVAL;
530 }
531 *t3 = '\0';
532 if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
533 trace_probe_log_err(offset + t2 - arg,
534 BAD_ARRAY_NUM);
535 return -EINVAL;
536 }
537 if (parg->count > MAX_ARRAY_LEN) {
538 trace_probe_log_err(offset + t2 - arg,
539 ARRAY_TOO_BIG);
540 return -EINVAL;
541 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900542 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530543 }
Masami Hiramatsu3dd1f7f2019-05-07 22:56:02 +0900544
545 /* Since $comm can not be dereferred, we can find $comm by strcmp */
546 if (strcmp(arg, "$comm") == 0) {
547 /* The type of $comm must be "string", and not an array. */
548 if (parg->count || (t && strcmp(t, "string")))
549 return -EINVAL;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900550 parg->type = find_fetch_type("string");
Masami Hiramatsu3dd1f7f2019-05-07 22:56:02 +0900551 } else
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900552 parg->type = find_fetch_type(t);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530553 if (!parg->type) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500554 trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530555 return -EINVAL;
556 }
557 parg->offset = *size;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900558 *size += parg->type->size * (parg->count ?: 1);
559
560 if (parg->count) {
561 len = strlen(parg->type->fmttype) + 6;
562 parg->fmt = kmalloc(len, GFP_KERNEL);
563 if (!parg->fmt)
564 return -ENOMEM;
565 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
566 parg->count);
567 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530568
Gustavo A. R. Silva8623b002019-01-14 22:34:08 -0600569 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900570 if (!code)
571 return -ENOMEM;
572 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530573
Masami Hiramatsu53305922018-04-25 21:18:03 +0900574 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500575 flags, offset);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900576 if (ret)
577 goto fail;
578
579 /* Store operation */
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900580 if (!strcmp(parg->type->name, "string") ||
581 !strcmp(parg->type->name, "ustring")) {
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900582 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
583 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500584 trace_probe_log_err(offset + (t ? (t - arg) : 0),
585 BAD_STRING);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900586 ret = -EINVAL;
587 goto fail;
588 }
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900589 if ((code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM) ||
590 parg->count) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900591 /*
592 * IMM and COMM is pointing actual address, those must
593 * be kept, and if parg->count != 0, this is an array
594 * of string pointers instead of string address itself.
595 */
Masami Hiramatsu53305922018-04-25 21:18:03 +0900596 code++;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900597 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500598 trace_probe_log_err(offset, TOO_MANY_OPS);
599 ret = -EINVAL;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900600 goto fail;
601 }
602 }
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900603 /* If op == DEREF, replace it with STRING */
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900604 if (!strcmp(parg->type->name, "ustring") ||
605 code->op == FETCH_OP_UDEREF)
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900606 code->op = FETCH_OP_ST_USTRING;
607 else
608 code->op = FETCH_OP_ST_STRING;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900609 code->size = parg->type->size;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900610 parg->dynamic = true;
611 } else if (code->op == FETCH_OP_DEREF) {
612 code->op = FETCH_OP_ST_MEM;
613 code->size = parg->type->size;
Masami Hiramatsue65f7ae2019-05-15 14:38:42 +0900614 } else if (code->op == FETCH_OP_UDEREF) {
615 code->op = FETCH_OP_ST_UMEM;
616 code->size = parg->type->size;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900617 } else {
618 code++;
619 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500620 trace_probe_log_err(offset, TOO_MANY_OPS);
621 ret = -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900622 goto fail;
623 }
624 code->op = FETCH_OP_ST_RAW;
625 code->size = parg->type->size;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530626 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900627 scode = code;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900628 /* Modify operation */
629 if (t != NULL) {
630 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500631 if (ret) {
632 trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900633 goto fail;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500634 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900635 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900636 /* Loop(Array) operation */
637 if (parg->count) {
638 if (scode->op != FETCH_OP_ST_MEM &&
Masami Hiramatsu88903c42019-05-15 14:38:30 +0900639 scode->op != FETCH_OP_ST_STRING &&
640 scode->op != FETCH_OP_ST_USTRING) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500641 trace_probe_log_err(offset + (t ? (t - arg) : 0),
642 BAD_STRING);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900643 ret = -EINVAL;
644 goto fail;
645 }
646 code++;
647 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500648 trace_probe_log_err(offset, TOO_MANY_OPS);
649 ret = -EINVAL;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900650 goto fail;
651 }
652 code->op = FETCH_OP_LP_ARRAY;
653 code->param = parg->count;
654 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900655 code++;
656 code->op = FETCH_OP_END;
657
658 /* Shrink down the code buffer */
Gustavo A. R. Silva8623b002019-01-14 22:34:08 -0600659 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900660 if (!parg->code)
661 ret = -ENOMEM;
662 else
663 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
664
665fail:
Masami Hiramatsua6682812018-08-29 01:18:43 +0900666 if (ret) {
667 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
668 if (code->op == FETCH_NOP_SYMBOL)
669 kfree(code->data);
670 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900671 kfree(tmp);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530672
673 return ret;
674}
675
676/* Return 1 if name is reserved or already used by another argument */
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900677static int traceprobe_conflict_field_name(const char *name,
678 struct probe_arg *args, int narg)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530679{
680 int i;
681
682 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
683 if (strcmp(reserved_field_names[i], name) == 0)
684 return 1;
685
686 for (i = 0; i < narg; i++)
687 if (strcmp(args[i].name, name) == 0)
688 return 1;
689
690 return 0;
691}
692
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900693int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, char *arg,
694 unsigned int flags)
695{
696 struct probe_arg *parg = &tp->args[i];
697 char *body;
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900698
699 /* Increment count for freeing args in error case */
700 tp->nr_args++;
701
702 body = strchr(arg, '=');
703 if (body) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500704 if (body - arg > MAX_ARG_NAME_LEN) {
705 trace_probe_log_err(0, ARG_NAME_TOO_LONG);
Masami Hiramatsub4443c12019-03-14 13:30:30 +0900706 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500707 } else if (body == arg) {
708 trace_probe_log_err(0, NO_ARG_NAME);
709 return -EINVAL;
710 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900711 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
712 body++;
713 } else {
714 /* If argument name is omitted, set "argN" */
715 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
716 body = arg;
717 }
718 if (!parg->name)
719 return -ENOMEM;
720
721 if (!is_good_name(parg->name)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500722 trace_probe_log_err(0, BAD_ARG_NAME);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900723 return -EINVAL;
724 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900725 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500726 trace_probe_log_err(0, USED_ARG_NAME);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900727 return -EINVAL;
728 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900729 /* Parse fetch argument */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500730 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
731 body - arg);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900732}
733
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530734void traceprobe_free_probe_arg(struct probe_arg *arg)
735{
Masami Hiramatsua6682812018-08-29 01:18:43 +0900736 struct fetch_insn *code = arg->code;
737
738 while (code && code->op != FETCH_OP_END) {
739 if (code->op == FETCH_NOP_SYMBOL)
740 kfree(code->data);
741 code++;
742 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900743 kfree(arg->code);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530744 kfree(arg->name);
745 kfree(arg->comm);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900746 kfree(arg->fmt);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530747}
748
Masami Hiramatsua6682812018-08-29 01:18:43 +0900749int traceprobe_update_arg(struct probe_arg *arg)
750{
751 struct fetch_insn *code = arg->code;
752 long offset;
753 char *tmp;
754 char c;
755 int ret = 0;
756
757 while (code && code->op != FETCH_OP_END) {
758 if (code->op == FETCH_NOP_SYMBOL) {
759 if (code[1].op != FETCH_OP_IMM)
760 return -EINVAL;
761
Masami Hiramatsuee474b82018-11-01 23:29:28 +0900762 tmp = strpbrk(code->data, "+-");
Masami Hiramatsua6682812018-08-29 01:18:43 +0900763 if (tmp)
764 c = *tmp;
765 ret = traceprobe_split_symbol_offset(code->data,
766 &offset);
767 if (ret)
768 return ret;
769
770 code[1].immediate =
771 (unsigned long)kallsyms_lookup_name(code->data);
772 if (tmp)
773 *tmp = c;
774 if (!code[1].immediate)
775 return -ENOENT;
776 code[1].immediate += offset;
777 }
778 code++;
779 }
780 return 0;
781}
782
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900783/* When len=0, we just calculate the needed length */
784#define LEN_OR_ZERO (len ? len - pos : 0)
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900785static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
786 bool is_return)
787{
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900788 struct probe_arg *parg;
789 int i, j;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900790 int pos = 0;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900791 const char *fmt, *arg;
792
793 if (!is_return) {
794 fmt = "(%lx)";
795 arg = "REC->" FIELD_STRING_IP;
796 } else {
797 fmt = "(%lx <- %lx)";
798 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
799 }
800
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900801 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
802
803 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900804 parg = tp->args + i;
805 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
806 if (parg->count) {
807 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
808 parg->type->fmt);
809 for (j = 1; j < parg->count; j++)
810 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
811 parg->type->fmt);
812 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
813 } else
814 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
815 parg->type->fmt);
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900816 }
817
818 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
819
820 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900821 parg = tp->args + i;
822 if (parg->count) {
823 if (strcmp(parg->type->name, "string") == 0)
824 fmt = ", __get_str(%s[%d])";
825 else
826 fmt = ", REC->%s[%d]";
827 for (j = 0; j < parg->count; j++)
828 pos += snprintf(buf + pos, LEN_OR_ZERO,
829 fmt, parg->name, j);
830 } else {
831 if (strcmp(parg->type->name, "string") == 0)
832 fmt = ", __get_str(%s)";
833 else
834 fmt = ", REC->%s";
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900835 pos += snprintf(buf + pos, LEN_OR_ZERO,
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900836 fmt, parg->name);
837 }
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900838 }
839
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900840 /* return the length of print_fmt */
841 return pos;
842}
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900843#undef LEN_OR_ZERO
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900844
Masami Hiramatsu0a46c852018-04-25 21:19:30 +0900845int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return)
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900846{
847 int len;
848 char *print_fmt;
849
850 /* First: called with 0 length to calculate the needed length */
851 len = __set_print_fmt(tp, NULL, 0, is_return);
852 print_fmt = kmalloc(len + 1, GFP_KERNEL);
853 if (!print_fmt)
854 return -ENOMEM;
855
856 /* Second: actually write the @print_fmt */
857 __set_print_fmt(tp, print_fmt, len + 1, is_return);
858 tp->call.print_fmt = print_fmt;
859
860 return 0;
861}
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900862
863int traceprobe_define_arg_fields(struct trace_event_call *event_call,
864 size_t offset, struct trace_probe *tp)
865{
866 int ret, i;
867
868 /* Set argument names as fields */
869 for (i = 0; i < tp->nr_args; i++) {
870 struct probe_arg *parg = &tp->args[i];
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900871 const char *fmt = parg->type->fmttype;
872 int size = parg->type->size;
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900873
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900874 if (parg->fmt)
875 fmt = parg->fmt;
876 if (parg->count)
877 size *= parg->count;
878 ret = trace_define_field(event_call, fmt, parg->name,
879 offset + parg->offset, size,
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900880 parg->type->is_signed,
881 FILTER_OTHER);
882 if (ret)
883 return ret;
884 }
885 return 0;
886}