blob: a347faced9595092464744b9729c80145ea8e0c8 [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[]"),
81 /* Basic types */
82 ASSIGN_FETCH_TYPE(u8, u8, 0),
83 ASSIGN_FETCH_TYPE(u16, u16, 0),
84 ASSIGN_FETCH_TYPE(u32, u32, 0),
85 ASSIGN_FETCH_TYPE(u64, u64, 0),
86 ASSIGN_FETCH_TYPE(s8, u8, 1),
87 ASSIGN_FETCH_TYPE(s16, u16, 1),
88 ASSIGN_FETCH_TYPE(s32, u32, 1),
89 ASSIGN_FETCH_TYPE(s64, u64, 1),
90 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
91 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
92 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
93 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
Masami Hiramatsu60c2e0c2018-04-25 21:20:28 +090094 ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
Masami Hiramatsuf451bc82018-04-25 21:18:32 +090095
96 ASSIGN_FETCH_TYPE_END
97};
98
99static const struct fetch_type *find_fetch_type(const char *type)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530100{
101 int i;
102
103 if (!type)
104 type = DEFAULT_FETCH_TYPE_STR;
105
106 /* Special case: bitfield */
107 if (*type == 'b') {
108 unsigned long bs;
109
110 type = strchr(type, '/');
111 if (!type)
112 goto fail;
113
114 type++;
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200115 if (kstrtoul(type, 0, &bs))
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530116 goto fail;
117
118 switch (bs) {
119 case 8:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900120 return find_fetch_type("u8");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530121 case 16:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900122 return find_fetch_type("u16");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530123 case 32:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900124 return find_fetch_type("u32");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530125 case 64:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900126 return find_fetch_type("u64");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530127 default:
128 goto fail;
129 }
130 }
131
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900132 for (i = 0; probe_fetch_types[i].name; i++) {
133 if (strcmp(type, probe_fetch_types[i].name) == 0)
134 return &probe_fetch_types[i];
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900135 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530136
137fail:
138 return NULL;
139}
140
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500141static struct trace_probe_log trace_probe_log;
142
143void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
144{
145 trace_probe_log.subsystem = subsystem;
146 trace_probe_log.argc = argc;
147 trace_probe_log.argv = argv;
148 trace_probe_log.index = 0;
149}
150
151void trace_probe_log_clear(void)
152{
153 memset(&trace_probe_log, 0, sizeof(trace_probe_log));
154}
155
156void trace_probe_log_set_index(int index)
157{
158 trace_probe_log.index = index;
159}
160
161void __trace_probe_log_err(int offset, int err_type)
162{
163 char *command, *p;
164 int i, len = 0, pos = 0;
165
166 if (!trace_probe_log.argv)
167 return;
168
169 /* Recalcurate the length and allocate buffer */
170 for (i = 0; i < trace_probe_log.argc; i++) {
171 if (i == trace_probe_log.index)
172 pos = len;
173 len += strlen(trace_probe_log.argv[i]) + 1;
174 }
175 command = kzalloc(len, GFP_KERNEL);
176 if (!command)
177 return;
178
179 /* And make a command string from argv array */
180 p = command;
181 for (i = 0; i < trace_probe_log.argc; i++) {
182 len = strlen(trace_probe_log.argv[i]);
183 strcpy(p, trace_probe_log.argv[i]);
184 p[len] = ' ';
185 p += len + 1;
186 }
187 *(p - 1) = '\0';
188
Steven Rostedt (VMware)2f754e72019-04-01 22:52:21 -0400189 tracing_log_err(NULL, trace_probe_log.subsystem, command,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500190 trace_probe_err_text, err_type, pos + offset);
191
192 kfree(command);
193}
194
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530195/* Split symbol and offset. */
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900196int traceprobe_split_symbol_offset(char *symbol, long *offset)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530197{
198 char *tmp;
199 int ret;
200
201 if (!offset)
202 return -EINVAL;
203
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900204 tmp = strpbrk(symbol, "+-");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530205 if (tmp) {
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900206 ret = kstrtol(tmp, 0, offset);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530207 if (ret)
208 return ret;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530209 *tmp = '\0';
210 } else
211 *offset = 0;
212
213 return 0;
214}
215
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900216/* @buf must has MAX_EVENT_NAME_LEN size */
217int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500218 char *buf, int offset)
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900219{
220 const char *slash, *event = *pevent;
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900221 int len;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900222
223 slash = strchr(event, '/');
224 if (slash) {
225 if (slash == event) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500226 trace_probe_log_err(offset, NO_GROUP_NAME);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900227 return -EINVAL;
228 }
229 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500230 trace_probe_log_err(offset, GROUP_TOO_LONG);
231 return -EINVAL;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900232 }
233 strlcpy(buf, event, slash - event + 1);
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900234 if (!is_good_name(buf)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500235 trace_probe_log_err(offset, BAD_GROUP_NAME);
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900236 return -EINVAL;
237 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900238 *pgroup = buf;
239 *pevent = slash + 1;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500240 offset += slash - event + 1;
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900241 event = *pevent;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900242 }
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900243 len = strlen(event);
244 if (len == 0) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500245 trace_probe_log_err(offset, NO_EVENT_NAME);
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900246 return -EINVAL;
Masami Hiramatsudec65d792019-03-14 13:30:20 +0900247 } else if (len > MAX_EVENT_NAME_LEN) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500248 trace_probe_log_err(offset, EVENT_TOO_LONG);
249 return -EINVAL;
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900250 }
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900251 if (!is_good_name(event)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500252 trace_probe_log_err(offset, BAD_EVENT_NAME);
Masami Hiramatsu5b7a9622019-03-14 13:30:40 +0900253 return -EINVAL;
254 }
Masami Hiramatsu6212dd22018-11-05 18:02:36 +0900255 return 0;
256}
257
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530258#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
259
260static int parse_probe_vars(char *arg, const struct fetch_type *t,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500261 struct fetch_insn *code, unsigned int flags, int offs)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530262{
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530263 unsigned long param;
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500264 int ret = 0;
265 int len;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530266
267 if (strcmp(arg, "retval") == 0) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500268 if (flags & TPARG_FL_RETURN) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900269 code->op = FETCH_OP_RETVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500270 } else {
271 trace_probe_log_err(offs, RETVAL_ON_PROBE);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530272 ret = -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500273 }
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500274 } else if ((len = str_has_prefix(arg, "stack"))) {
275 if (arg[len] == '\0') {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900276 code->op = FETCH_OP_STACKP;
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500277 } else if (isdigit(arg[len])) {
278 ret = kstrtoul(arg + len, 10, &param);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500279 if (ret) {
280 goto inval_var;
281 } else if ((flags & TPARG_FL_KERNEL) &&
282 param > PARAM_MAX_STACK) {
283 trace_probe_log_err(offs, BAD_STACK_NUM);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530284 ret = -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500285 } else {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900286 code->op = FETCH_OP_STACK;
287 code->param = (unsigned int)param;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530288 }
289 } else
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500290 goto inval_var;
Omar Sandoval35abb672016-06-08 18:38:02 -0700291 } else if (strcmp(arg, "comm") == 0) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900292 code->op = FETCH_OP_COMM;
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900293#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
294 } else if (((flags & TPARG_FL_MASK) ==
295 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500296 (len = str_has_prefix(arg, "arg"))) {
Steven Rostedt (VMware)3d739c12018-12-21 23:10:26 -0500297 ret = kstrtoul(arg + len, 10, &param);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500298 if (ret) {
299 goto inval_var;
300 } else if (!param || param > PARAM_MAX_STACK) {
301 trace_probe_log_err(offs, BAD_ARG_NUM);
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900302 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500303 }
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900304 code->op = FETCH_OP_ARG;
305 code->param = (unsigned int)param - 1;
306#endif
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530307 } else
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500308 goto inval_var;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530309
310 return ret;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500311
312inval_var:
313 trace_probe_log_err(offs, BAD_VAR);
314 return -EINVAL;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530315}
316
317/* Recursive argument parser */
Masami Hiramatsu53305922018-04-25 21:18:03 +0900318static int
319parse_probe_arg(char *arg, const struct fetch_type *type,
320 struct fetch_insn **pcode, struct fetch_insn *end,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500321 unsigned int flags, int offs)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530322{
Masami Hiramatsu53305922018-04-25 21:18:03 +0900323 struct fetch_insn *code = *pcode;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530324 unsigned long param;
Steven Rostedt (VMware)bf173ca2018-10-12 12:50:22 -0400325 long offset = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530326 char *tmp;
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900327 int ret = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530328
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530329 switch (arg[0]) {
330 case '$':
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500331 ret = parse_probe_vars(arg + 1, type, code, flags, offs);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530332 break;
333
334 case '%': /* named register */
335 ret = regs_query_register_offset(arg + 1);
336 if (ret >= 0) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900337 code->op = FETCH_OP_REG;
338 code->param = (unsigned int)ret;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530339 ret = 0;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500340 } else
341 trace_probe_log_err(offs, BAD_REG_NAME);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530342 break;
343
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900344 case '@': /* memory, file-offset or symbol */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530345 if (isdigit(arg[1])) {
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200346 ret = kstrtoul(arg + 1, 0, &param);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500347 if (ret) {
348 trace_probe_log_err(offs, BAD_MEM_ADDR);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530349 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500350 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900351 /* load address */
352 code->op = FETCH_OP_IMM;
353 code->immediate = param;
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900354 } else if (arg[1] == '+') {
355 /* kprobes don't support file offsets */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500356 if (flags & TPARG_FL_KERNEL) {
357 trace_probe_log_err(offs, FILE_ON_KPROBE);
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900358 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500359 }
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900360 ret = kstrtol(arg + 2, 0, &offset);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500361 if (ret) {
362 trace_probe_log_err(offs, BAD_FILE_OFFS);
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900363 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500364 }
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900365
Masami Hiramatsu53305922018-04-25 21:18:03 +0900366 code->op = FETCH_OP_FOFFS;
367 code->immediate = (unsigned long)offset; // imm64?
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530368 } else {
Namhyung Kimb079d372013-07-03 18:34:23 +0900369 /* uprobes don't support symbols */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500370 if (!(flags & TPARG_FL_KERNEL)) {
371 trace_probe_log_err(offs, SYM_ON_UPROBE);
Namhyung Kimb079d372013-07-03 18:34:23 +0900372 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500373 }
Masami Hiramatsua6682812018-08-29 01:18:43 +0900374 /* Preserve symbol for updating */
375 code->op = FETCH_NOP_SYMBOL;
376 code->data = kstrdup(arg + 1, GFP_KERNEL);
377 if (!code->data)
378 return -ENOMEM;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500379 if (++code == end) {
380 trace_probe_log_err(offs, TOO_MANY_OPS);
381 return -EINVAL;
382 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900383 code->op = FETCH_OP_IMM;
Masami Hiramatsua6682812018-08-29 01:18:43 +0900384 code->immediate = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530385 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900386 /* These are fetching from memory */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500387 if (++code == end) {
388 trace_probe_log_err(offs, TOO_MANY_OPS);
389 return -EINVAL;
390 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900391 *pcode = code;
392 code->op = FETCH_OP_DEREF;
393 code->offset = offset;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530394 break;
395
396 case '+': /* deref memory */
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200397 arg++; /* Skip '+', because kstrtol() rejects it. */
Mathieu Malaterre91457c02019-01-14 21:30:37 +0100398 /* fall through */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530399 case '-':
400 tmp = strchr(arg, '(');
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500401 if (!tmp) {
402 trace_probe_log_err(offs, DEREF_NEED_BRACE);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900403 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500404 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530405 *tmp = '\0';
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200406 ret = kstrtol(arg, 0, &offset);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500407 if (ret) {
408 trace_probe_log_err(offs, BAD_DEREF_OFFS);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530409 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500410 }
411 offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530412 arg = tmp + 1;
413 tmp = strrchr(arg, ')');
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500414 if (!tmp) {
415 trace_probe_log_err(offs + strlen(arg),
416 DEREF_OPEN_BRACE);
417 return -EINVAL;
418 } else {
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900419 const struct fetch_type *t2 = find_fetch_type(NULL);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530420
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530421 *tmp = '\0';
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500422 ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530423 if (ret)
Masami Hiramatsu53305922018-04-25 21:18:03 +0900424 break;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500425 if (code->op == FETCH_OP_COMM) {
426 trace_probe_log_err(offs, COMM_CANT_DEREF);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900427 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500428 }
429 if (++code == end) {
430 trace_probe_log_err(offs, TOO_MANY_OPS);
431 return -EINVAL;
432 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900433 *pcode = code;
434
435 code->op = FETCH_OP_DEREF;
436 code->offset = offset;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530437 }
438 break;
439 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900440 if (!ret && code->op == FETCH_OP_NOP) {
441 /* Parsed, but do not find fetch method */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500442 trace_probe_log_err(offs, BAD_FETCH_ARG);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530443 ret = -EINVAL;
444 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530445 return ret;
446}
447
448#define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
449
450/* Bitfield type needs to be parsed into a fetch function */
451static int __parse_bitfield_probe_arg(const char *bf,
452 const struct fetch_type *t,
Masami Hiramatsu53305922018-04-25 21:18:03 +0900453 struct fetch_insn **pcode)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530454{
Masami Hiramatsu53305922018-04-25 21:18:03 +0900455 struct fetch_insn *code = *pcode;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530456 unsigned long bw, bo;
457 char *tail;
458
459 if (*bf != 'b')
460 return 0;
461
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530462 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
463
464 if (bw == 0 || *tail != '@')
465 return -EINVAL;
466
467 bf = tail + 1;
468 bo = simple_strtoul(bf, &tail, 0);
469
470 if (tail == bf || *tail != '/')
471 return -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900472 code++;
473 if (code->op != FETCH_OP_NOP)
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500474 return -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900475 *pcode = code;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530476
Masami Hiramatsu53305922018-04-25 21:18:03 +0900477 code->op = FETCH_OP_MOD_BF;
478 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
479 code->rshift = BYTES_TO_BITS(t->size) - bw;
480 code->basesize = t->size;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530481
482 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
483}
484
485/* String length checking wrapper */
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900486static int traceprobe_parse_probe_arg_body(char *arg, ssize_t *size,
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500487 struct probe_arg *parg, unsigned int flags, int offset)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530488{
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900489 struct fetch_insn *code, *scode, *tmp = NULL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500490 char *t, *t2, *t3;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900491 int ret, len;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530492
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500493 len = strlen(arg);
494 if (len > MAX_ARGSTR_LEN) {
495 trace_probe_log_err(offset, ARG_TOO_LONG);
496 return -EINVAL;
497 } else if (len == 0) {
498 trace_probe_log_err(offset, NO_ARG_BODY);
499 return -EINVAL;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530500 }
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500501
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530502 parg->comm = kstrdup(arg, GFP_KERNEL);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500503 if (!parg->comm)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530504 return -ENOMEM;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500505
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900506 t = strchr(arg, ':');
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530507 if (t) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900508 *t = '\0';
509 t2 = strchr(++t, '[');
510 if (t2) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500511 *t2++ = '\0';
512 t3 = strchr(t2, ']');
513 if (!t3) {
514 offset += t2 + strlen(t2) - arg;
515 trace_probe_log_err(offset,
516 ARRAY_NO_CLOSE);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900517 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500518 } else if (t3[1] != '\0') {
519 trace_probe_log_err(offset + t3 + 1 - arg,
520 BAD_ARRAY_SUFFIX);
521 return -EINVAL;
522 }
523 *t3 = '\0';
524 if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
525 trace_probe_log_err(offset + t2 - arg,
526 BAD_ARRAY_NUM);
527 return -EINVAL;
528 }
529 if (parg->count > MAX_ARRAY_LEN) {
530 trace_probe_log_err(offset + t2 - arg,
531 ARRAY_TOO_BIG);
532 return -EINVAL;
533 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900534 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530535 }
Masami Hiramatsu3dd1f7f2019-05-07 22:56:02 +0900536
537 /* Since $comm can not be dereferred, we can find $comm by strcmp */
538 if (strcmp(arg, "$comm") == 0) {
539 /* The type of $comm must be "string", and not an array. */
540 if (parg->count || (t && strcmp(t, "string")))
541 return -EINVAL;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900542 parg->type = find_fetch_type("string");
Masami Hiramatsu3dd1f7f2019-05-07 22:56:02 +0900543 } else
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900544 parg->type = find_fetch_type(t);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530545 if (!parg->type) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500546 trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530547 return -EINVAL;
548 }
549 parg->offset = *size;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900550 *size += parg->type->size * (parg->count ?: 1);
551
552 if (parg->count) {
553 len = strlen(parg->type->fmttype) + 6;
554 parg->fmt = kmalloc(len, GFP_KERNEL);
555 if (!parg->fmt)
556 return -ENOMEM;
557 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
558 parg->count);
559 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530560
Gustavo A. R. Silva8623b002019-01-14 22:34:08 -0600561 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900562 if (!code)
563 return -ENOMEM;
564 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530565
Masami Hiramatsu53305922018-04-25 21:18:03 +0900566 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500567 flags, offset);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900568 if (ret)
569 goto fail;
570
571 /* Store operation */
572 if (!strcmp(parg->type->name, "string")) {
573 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_IMM &&
574 code->op != FETCH_OP_COMM) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500575 trace_probe_log_err(offset + (t ? (t - arg) : 0),
576 BAD_STRING);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900577 ret = -EINVAL;
578 goto fail;
579 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900580 if (code->op != FETCH_OP_DEREF || parg->count) {
581 /*
582 * IMM and COMM is pointing actual address, those must
583 * be kept, and if parg->count != 0, this is an array
584 * of string pointers instead of string address itself.
585 */
Masami Hiramatsu53305922018-04-25 21:18:03 +0900586 code++;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900587 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500588 trace_probe_log_err(offset, TOO_MANY_OPS);
589 ret = -EINVAL;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900590 goto fail;
591 }
592 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900593 code->op = FETCH_OP_ST_STRING; /* In DEREF case, replace it */
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900594 code->size = parg->type->size;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900595 parg->dynamic = true;
596 } else if (code->op == FETCH_OP_DEREF) {
597 code->op = FETCH_OP_ST_MEM;
598 code->size = parg->type->size;
599 } else {
600 code++;
601 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500602 trace_probe_log_err(offset, TOO_MANY_OPS);
603 ret = -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900604 goto fail;
605 }
606 code->op = FETCH_OP_ST_RAW;
607 code->size = parg->type->size;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530608 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900609 scode = code;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900610 /* Modify operation */
611 if (t != NULL) {
612 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500613 if (ret) {
614 trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900615 goto fail;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500616 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900617 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900618 /* Loop(Array) operation */
619 if (parg->count) {
620 if (scode->op != FETCH_OP_ST_MEM &&
621 scode->op != FETCH_OP_ST_STRING) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500622 trace_probe_log_err(offset + (t ? (t - arg) : 0),
623 BAD_STRING);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900624 ret = -EINVAL;
625 goto fail;
626 }
627 code++;
628 if (code->op != FETCH_OP_NOP) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500629 trace_probe_log_err(offset, TOO_MANY_OPS);
630 ret = -EINVAL;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900631 goto fail;
632 }
633 code->op = FETCH_OP_LP_ARRAY;
634 code->param = parg->count;
635 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900636 code++;
637 code->op = FETCH_OP_END;
638
639 /* Shrink down the code buffer */
Gustavo A. R. Silva8623b002019-01-14 22:34:08 -0600640 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900641 if (!parg->code)
642 ret = -ENOMEM;
643 else
644 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
645
646fail:
Masami Hiramatsua6682812018-08-29 01:18:43 +0900647 if (ret) {
648 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
649 if (code->op == FETCH_NOP_SYMBOL)
650 kfree(code->data);
651 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900652 kfree(tmp);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530653
654 return ret;
655}
656
657/* Return 1 if name is reserved or already used by another argument */
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900658static int traceprobe_conflict_field_name(const char *name,
659 struct probe_arg *args, int narg)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530660{
661 int i;
662
663 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
664 if (strcmp(reserved_field_names[i], name) == 0)
665 return 1;
666
667 for (i = 0; i < narg; i++)
668 if (strcmp(args[i].name, name) == 0)
669 return 1;
670
671 return 0;
672}
673
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900674int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, char *arg,
675 unsigned int flags)
676{
677 struct probe_arg *parg = &tp->args[i];
678 char *body;
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900679
680 /* Increment count for freeing args in error case */
681 tp->nr_args++;
682
683 body = strchr(arg, '=');
684 if (body) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500685 if (body - arg > MAX_ARG_NAME_LEN) {
686 trace_probe_log_err(0, ARG_NAME_TOO_LONG);
Masami Hiramatsub4443c12019-03-14 13:30:30 +0900687 return -EINVAL;
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500688 } else if (body == arg) {
689 trace_probe_log_err(0, NO_ARG_NAME);
690 return -EINVAL;
691 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900692 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
693 body++;
694 } else {
695 /* If argument name is omitted, set "argN" */
696 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
697 body = arg;
698 }
699 if (!parg->name)
700 return -ENOMEM;
701
702 if (!is_good_name(parg->name)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500703 trace_probe_log_err(0, BAD_ARG_NAME);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900704 return -EINVAL;
705 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900706 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500707 trace_probe_log_err(0, USED_ARG_NAME);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900708 return -EINVAL;
709 }
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900710 /* Parse fetch argument */
Masami Hiramatsuab105a42019-03-31 18:48:19 -0500711 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
712 body - arg);
Masami Hiramatsud00bbea92018-11-05 18:01:40 +0900713}
714
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530715void traceprobe_free_probe_arg(struct probe_arg *arg)
716{
Masami Hiramatsua6682812018-08-29 01:18:43 +0900717 struct fetch_insn *code = arg->code;
718
719 while (code && code->op != FETCH_OP_END) {
720 if (code->op == FETCH_NOP_SYMBOL)
721 kfree(code->data);
722 code++;
723 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900724 kfree(arg->code);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530725 kfree(arg->name);
726 kfree(arg->comm);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900727 kfree(arg->fmt);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530728}
729
Masami Hiramatsua6682812018-08-29 01:18:43 +0900730int traceprobe_update_arg(struct probe_arg *arg)
731{
732 struct fetch_insn *code = arg->code;
733 long offset;
734 char *tmp;
735 char c;
736 int ret = 0;
737
738 while (code && code->op != FETCH_OP_END) {
739 if (code->op == FETCH_NOP_SYMBOL) {
740 if (code[1].op != FETCH_OP_IMM)
741 return -EINVAL;
742
Masami Hiramatsuee474b82018-11-01 23:29:28 +0900743 tmp = strpbrk(code->data, "+-");
Masami Hiramatsua6682812018-08-29 01:18:43 +0900744 if (tmp)
745 c = *tmp;
746 ret = traceprobe_split_symbol_offset(code->data,
747 &offset);
748 if (ret)
749 return ret;
750
751 code[1].immediate =
752 (unsigned long)kallsyms_lookup_name(code->data);
753 if (tmp)
754 *tmp = c;
755 if (!code[1].immediate)
756 return -ENOENT;
757 code[1].immediate += offset;
758 }
759 code++;
760 }
761 return 0;
762}
763
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900764/* When len=0, we just calculate the needed length */
765#define LEN_OR_ZERO (len ? len - pos : 0)
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900766static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
767 bool is_return)
768{
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900769 struct probe_arg *parg;
770 int i, j;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900771 int pos = 0;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900772 const char *fmt, *arg;
773
774 if (!is_return) {
775 fmt = "(%lx)";
776 arg = "REC->" FIELD_STRING_IP;
777 } else {
778 fmt = "(%lx <- %lx)";
779 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
780 }
781
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900782 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
783
784 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900785 parg = tp->args + i;
786 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
787 if (parg->count) {
788 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
789 parg->type->fmt);
790 for (j = 1; j < parg->count; j++)
791 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
792 parg->type->fmt);
793 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
794 } else
795 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
796 parg->type->fmt);
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900797 }
798
799 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
800
801 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900802 parg = tp->args + i;
803 if (parg->count) {
804 if (strcmp(parg->type->name, "string") == 0)
805 fmt = ", __get_str(%s[%d])";
806 else
807 fmt = ", REC->%s[%d]";
808 for (j = 0; j < parg->count; j++)
809 pos += snprintf(buf + pos, LEN_OR_ZERO,
810 fmt, parg->name, j);
811 } else {
812 if (strcmp(parg->type->name, "string") == 0)
813 fmt = ", __get_str(%s)";
814 else
815 fmt = ", REC->%s";
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900816 pos += snprintf(buf + pos, LEN_OR_ZERO,
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900817 fmt, parg->name);
818 }
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900819 }
820
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900821 /* return the length of print_fmt */
822 return pos;
823}
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900824#undef LEN_OR_ZERO
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900825
Masami Hiramatsu0a46c852018-04-25 21:19:30 +0900826int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return)
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900827{
828 int len;
829 char *print_fmt;
830
831 /* First: called with 0 length to calculate the needed length */
832 len = __set_print_fmt(tp, NULL, 0, is_return);
833 print_fmt = kmalloc(len + 1, GFP_KERNEL);
834 if (!print_fmt)
835 return -ENOMEM;
836
837 /* Second: actually write the @print_fmt */
838 __set_print_fmt(tp, print_fmt, len + 1, is_return);
839 tp->call.print_fmt = print_fmt;
840
841 return 0;
842}
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900843
844int traceprobe_define_arg_fields(struct trace_event_call *event_call,
845 size_t offset, struct trace_probe *tp)
846{
847 int ret, i;
848
849 /* Set argument names as fields */
850 for (i = 0; i < tp->nr_args; i++) {
851 struct probe_arg *parg = &tp->args[i];
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900852 const char *fmt = parg->type->fmttype;
853 int size = parg->type->size;
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900854
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900855 if (parg->fmt)
856 fmt = parg->fmt;
857 if (parg->count)
858 size *= parg->count;
859 ret = trace_define_field(event_call, fmt, parg->name,
860 offset + parg->offset, size,
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900861 parg->type->is_signed,
862 FILTER_OTHER);
863 if (ret)
864 return ret;
865 }
866 return 0;
867}