blob: 333cda6d263364f3f5525d281e6ec294220d96f3 [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
16const char *reserved_field_names[] = {
17 "common_type",
18 "common_flags",
19 "common_preempt_count",
20 "common_pid",
21 "common_tgid",
22 FIELD_STRING_IP,
23 FIELD_STRING_RETIP,
24 FIELD_STRING_FUNC,
25};
26
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053027/* Printing in basic type function template */
Masami Hiramatsu17ce3dc2016-08-18 17:57:50 +090028#define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \
Masami Hiramatsu56de7632018-04-25 21:16:36 +090029int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053030{ \
Masami Hiramatsu56de7632018-04-25 21:16:36 +090031 trace_seq_printf(s, fmt, *(type *)data); \
Steven Rostedt (Red Hat)d2b01912014-11-12 17:19:51 -050032 return !trace_seq_has_overflowed(s); \
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053033} \
Masami Hiramatsu7bfbc632018-04-25 21:17:34 +090034const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053035
Masami Hiramatsubdca79c22016-08-18 17:59:21 +090036DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u")
37DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
38DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
39DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
Masami Hiramatsu17ce3dc2016-08-18 17:57:50 +090040DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d")
41DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
42DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
43DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
44DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x")
45DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
46DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
47DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053048
Masami Hiramatsu60c2e0c2018-04-25 21:20:28 +090049int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
50{
51 trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
52 return !trace_seq_has_overflowed(s);
53}
54const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
55
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053056/* Print type function for string type */
Masami Hiramatsu56de7632018-04-25 21:16:36 +090057int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053058{
59 int len = *(u32 *)data >> 16;
60
61 if (!len)
Masami Hiramatsu56de7632018-04-25 21:16:36 +090062 trace_seq_puts(s, "(fault)");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053063 else
Masami Hiramatsu56de7632018-04-25 21:16:36 +090064 trace_seq_printf(s, "\"%s\"",
Steven Rostedt (Red Hat)d2b01912014-11-12 17:19:51 -050065 (const char *)get_loc_data(data, ent));
66 return !trace_seq_has_overflowed(s);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053067}
68
Namhyung Kimb26c74e2013-11-26 14:19:59 +090069const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053070
Masami Hiramatsuf451bc82018-04-25 21:18:32 +090071/* Fetch type information table */
72static const struct fetch_type probe_fetch_types[] = {
73 /* Special types */
74 __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1,
75 "__data_loc char[]"),
76 /* Basic types */
77 ASSIGN_FETCH_TYPE(u8, u8, 0),
78 ASSIGN_FETCH_TYPE(u16, u16, 0),
79 ASSIGN_FETCH_TYPE(u32, u32, 0),
80 ASSIGN_FETCH_TYPE(u64, u64, 0),
81 ASSIGN_FETCH_TYPE(s8, u8, 1),
82 ASSIGN_FETCH_TYPE(s16, u16, 1),
83 ASSIGN_FETCH_TYPE(s32, u32, 1),
84 ASSIGN_FETCH_TYPE(s64, u64, 1),
85 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
86 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
87 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
88 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
Masami Hiramatsu60c2e0c2018-04-25 21:20:28 +090089 ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
Masami Hiramatsuf451bc82018-04-25 21:18:32 +090090
91 ASSIGN_FETCH_TYPE_END
92};
93
94static const struct fetch_type *find_fetch_type(const char *type)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +053095{
96 int i;
97
98 if (!type)
99 type = DEFAULT_FETCH_TYPE_STR;
100
101 /* Special case: bitfield */
102 if (*type == 'b') {
103 unsigned long bs;
104
105 type = strchr(type, '/');
106 if (!type)
107 goto fail;
108
109 type++;
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200110 if (kstrtoul(type, 0, &bs))
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530111 goto fail;
112
113 switch (bs) {
114 case 8:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900115 return find_fetch_type("u8");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530116 case 16:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900117 return find_fetch_type("u16");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530118 case 32:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900119 return find_fetch_type("u32");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530120 case 64:
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900121 return find_fetch_type("u64");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530122 default:
123 goto fail;
124 }
125 }
126
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900127 for (i = 0; probe_fetch_types[i].name; i++) {
128 if (strcmp(type, probe_fetch_types[i].name) == 0)
129 return &probe_fetch_types[i];
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900130 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530131
132fail:
133 return NULL;
134}
135
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530136/* Split symbol and offset. */
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900137int traceprobe_split_symbol_offset(char *symbol, long *offset)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530138{
139 char *tmp;
140 int ret;
141
142 if (!offset)
143 return -EINVAL;
144
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900145 tmp = strpbrk(symbol, "+-");
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530146 if (tmp) {
Masami Hiramatsuc5d343b2018-03-17 21:38:10 +0900147 ret = kstrtol(tmp, 0, offset);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530148 if (ret)
149 return ret;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530150 *tmp = '\0';
151 } else
152 *offset = 0;
153
154 return 0;
155}
156
157#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
158
159static int parse_probe_vars(char *arg, const struct fetch_type *t,
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900160 struct fetch_insn *code, unsigned int flags)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530161{
162 int ret = 0;
163 unsigned long param;
164
165 if (strcmp(arg, "retval") == 0) {
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900166 if (flags & TPARG_FL_RETURN)
Masami Hiramatsu53305922018-04-25 21:18:03 +0900167 code->op = FETCH_OP_RETVAL;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530168 else
169 ret = -EINVAL;
170 } else if (strncmp(arg, "stack", 5) == 0) {
171 if (arg[5] == '\0') {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900172 code->op = FETCH_OP_STACKP;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530173 } else if (isdigit(arg[5])) {
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200174 ret = kstrtoul(arg + 5, 10, &param);
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900175 if (ret || ((flags & TPARG_FL_KERNEL) &&
176 param > PARAM_MAX_STACK))
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530177 ret = -EINVAL;
178 else {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900179 code->op = FETCH_OP_STACK;
180 code->param = (unsigned int)param;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530181 }
182 } else
183 ret = -EINVAL;
Omar Sandoval35abb672016-06-08 18:38:02 -0700184 } else if (strcmp(arg, "comm") == 0) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900185 code->op = FETCH_OP_COMM;
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900186#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
187 } else if (((flags & TPARG_FL_MASK) ==
188 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
189 strncmp(arg, "arg", 3) == 0) {
190 if (!isdigit(arg[3]))
191 return -EINVAL;
192 ret = kstrtoul(arg + 3, 10, &param);
193 if (ret || !param || param > PARAM_MAX_STACK)
194 return -EINVAL;
195 code->op = FETCH_OP_ARG;
196 code->param = (unsigned int)param - 1;
197#endif
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530198 } else
199 ret = -EINVAL;
200
201 return ret;
202}
203
204/* Recursive argument parser */
Masami Hiramatsu53305922018-04-25 21:18:03 +0900205static int
206parse_probe_arg(char *arg, const struct fetch_type *type,
207 struct fetch_insn **pcode, struct fetch_insn *end,
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900208 unsigned int flags)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530209{
Masami Hiramatsu53305922018-04-25 21:18:03 +0900210 struct fetch_insn *code = *pcode;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530211 unsigned long param;
212 long offset;
213 char *tmp;
Namhyung Kim34fee3a2013-11-26 14:56:28 +0900214 int ret = 0;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530215
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530216 switch (arg[0]) {
217 case '$':
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900218 ret = parse_probe_vars(arg + 1, type, code, flags);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530219 break;
220
221 case '%': /* named register */
222 ret = regs_query_register_offset(arg + 1);
223 if (ret >= 0) {
Masami Hiramatsu53305922018-04-25 21:18:03 +0900224 code->op = FETCH_OP_REG;
225 code->param = (unsigned int)ret;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530226 ret = 0;
227 }
228 break;
229
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900230 case '@': /* memory, file-offset or symbol */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530231 if (isdigit(arg[1])) {
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200232 ret = kstrtoul(arg + 1, 0, &param);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530233 if (ret)
234 break;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900235 /* load address */
236 code->op = FETCH_OP_IMM;
237 code->immediate = param;
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900238 } else if (arg[1] == '+') {
239 /* kprobes don't support file offsets */
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900240 if (flags & TPARG_FL_KERNEL)
Namhyung Kimb7e0bf32013-11-25 13:42:47 +0900241 return -EINVAL;
242
243 ret = kstrtol(arg + 2, 0, &offset);
244 if (ret)
245 break;
246
Masami Hiramatsu53305922018-04-25 21:18:03 +0900247 code->op = FETCH_OP_FOFFS;
248 code->immediate = (unsigned long)offset; // imm64?
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530249 } else {
Namhyung Kimb079d372013-07-03 18:34:23 +0900250 /* uprobes don't support symbols */
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900251 if (!(flags & TPARG_FL_KERNEL))
Namhyung Kimb079d372013-07-03 18:34:23 +0900252 return -EINVAL;
253
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530254 ret = traceprobe_split_symbol_offset(arg + 1, &offset);
255 if (ret)
256 break;
257
Masami Hiramatsu53305922018-04-25 21:18:03 +0900258 code->op = FETCH_OP_IMM;
259 code->immediate =
260 (unsigned long)kallsyms_lookup_name(arg + 1);
261 if (!code->immediate)
262 return -ENOENT;
263 code->immediate += offset;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530264 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900265 /* These are fetching from memory */
266 if (++code == end)
267 return -E2BIG;
268 *pcode = code;
269 code->op = FETCH_OP_DEREF;
270 code->offset = offset;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530271 break;
272
273 case '+': /* deref memory */
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200274 arg++; /* Skip '+', because kstrtol() rejects it. */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530275 case '-':
276 tmp = strchr(arg, '(');
277 if (!tmp)
Masami Hiramatsu53305922018-04-25 21:18:03 +0900278 return -EINVAL;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530279
280 *tmp = '\0';
Daniel Walterbcd83ea2012-09-26 22:08:38 +0200281 ret = kstrtol(arg, 0, &offset);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530282 if (ret)
283 break;
284
285 arg = tmp + 1;
286 tmp = strrchr(arg, ')');
287
288 if (tmp) {
Masami Hiramatsuf451bc82018-04-25 21:18:32 +0900289 const struct fetch_type *t2 = find_fetch_type(NULL);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530290
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530291 *tmp = '\0';
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900292 ret = parse_probe_arg(arg, t2, &code, end, flags);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530293 if (ret)
Masami Hiramatsu53305922018-04-25 21:18:03 +0900294 break;
295 if (code->op == FETCH_OP_COMM)
296 return -EINVAL;
297 if (++code == end)
298 return -E2BIG;
299 *pcode = code;
300
301 code->op = FETCH_OP_DEREF;
302 code->offset = offset;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530303 }
304 break;
305 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900306 if (!ret && code->op == FETCH_OP_NOP) {
307 /* Parsed, but do not find fetch method */
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530308 ret = -EINVAL;
309 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530310 return ret;
311}
312
313#define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long))
314
315/* Bitfield type needs to be parsed into a fetch function */
316static int __parse_bitfield_probe_arg(const char *bf,
317 const struct fetch_type *t,
Masami Hiramatsu53305922018-04-25 21:18:03 +0900318 struct fetch_insn **pcode)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530319{
Masami Hiramatsu53305922018-04-25 21:18:03 +0900320 struct fetch_insn *code = *pcode;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530321 unsigned long bw, bo;
322 char *tail;
323
324 if (*bf != 'b')
325 return 0;
326
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530327 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */
328
329 if (bw == 0 || *tail != '@')
330 return -EINVAL;
331
332 bf = tail + 1;
333 bo = simple_strtoul(bf, &tail, 0);
334
335 if (tail == bf || *tail != '/')
336 return -EINVAL;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900337 code++;
338 if (code->op != FETCH_OP_NOP)
339 return -E2BIG;
340 *pcode = code;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530341
Masami Hiramatsu53305922018-04-25 21:18:03 +0900342 code->op = FETCH_OP_MOD_BF;
343 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
344 code->rshift = BYTES_TO_BITS(t->size) - bw;
345 code->basesize = t->size;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530346
347 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
348}
349
350/* String length checking wrapper */
351int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900352 struct probe_arg *parg, unsigned int flags)
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530353{
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900354 struct fetch_insn *code, *scode, *tmp = NULL;
355 char *t, *t2;
356 int ret, len;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530357
358 if (strlen(arg) > MAX_ARGSTR_LEN) {
359 pr_info("Argument is too long.: %s\n", arg);
360 return -ENOSPC;
361 }
362 parg->comm = kstrdup(arg, GFP_KERNEL);
363 if (!parg->comm) {
364 pr_info("Failed to allocate memory for command '%s'.\n", arg);
365 return -ENOMEM;
366 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900367 t = strchr(arg, ':');
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530368 if (t) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900369 *t = '\0';
370 t2 = strchr(++t, '[');
371 if (t2) {
372 *t2 = '\0';
373 parg->count = simple_strtoul(t2 + 1, &t2, 0);
374 if (strcmp(t2, "]") || parg->count == 0)
375 return -EINVAL;
376 if (parg->count > MAX_ARRAY_LEN)
377 return -E2BIG;
378 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530379 }
Omar Sandoval35abb672016-06-08 18:38:02 -0700380 /*
381 * The default type of $comm should be "string", and it can't be
382 * dereferenced.
383 */
384 if (!t && strcmp(arg, "$comm") == 0)
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900385 parg->type = find_fetch_type("string");
386 else
387 parg->type = find_fetch_type(t);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530388 if (!parg->type) {
389 pr_info("Unsupported type: %s\n", t);
390 return -EINVAL;
391 }
392 parg->offset = *size;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900393 *size += parg->type->size * (parg->count ?: 1);
394
395 if (parg->count) {
396 len = strlen(parg->type->fmttype) + 6;
397 parg->fmt = kmalloc(len, GFP_KERNEL);
398 if (!parg->fmt)
399 return -ENOMEM;
400 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
401 parg->count);
402 }
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530403
Masami Hiramatsu53305922018-04-25 21:18:03 +0900404 code = tmp = kzalloc(sizeof(*code) * FETCH_INSN_MAX, GFP_KERNEL);
405 if (!code)
406 return -ENOMEM;
407 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530408
Masami Hiramatsu53305922018-04-25 21:18:03 +0900409 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
Masami Hiramatsua1303af2018-04-25 21:21:26 +0900410 flags);
Masami Hiramatsu53305922018-04-25 21:18:03 +0900411 if (ret)
412 goto fail;
413
414 /* Store operation */
415 if (!strcmp(parg->type->name, "string")) {
416 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_IMM &&
417 code->op != FETCH_OP_COMM) {
418 pr_info("string only accepts memory or address.\n");
419 ret = -EINVAL;
420 goto fail;
421 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900422 if (code->op != FETCH_OP_DEREF || parg->count) {
423 /*
424 * IMM and COMM is pointing actual address, those must
425 * be kept, and if parg->count != 0, this is an array
426 * of string pointers instead of string address itself.
427 */
Masami Hiramatsu53305922018-04-25 21:18:03 +0900428 code++;
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900429 if (code->op != FETCH_OP_NOP) {
430 ret = -E2BIG;
431 goto fail;
432 }
433 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900434 code->op = FETCH_OP_ST_STRING; /* In DEREF case, replace it */
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900435 code->size = parg->type->size;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900436 parg->dynamic = true;
437 } else if (code->op == FETCH_OP_DEREF) {
438 code->op = FETCH_OP_ST_MEM;
439 code->size = parg->type->size;
440 } else {
441 code++;
442 if (code->op != FETCH_OP_NOP) {
443 ret = -E2BIG;
444 goto fail;
445 }
446 code->op = FETCH_OP_ST_RAW;
447 code->size = parg->type->size;
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530448 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900449 scode = code;
Masami Hiramatsu53305922018-04-25 21:18:03 +0900450 /* Modify operation */
451 if (t != NULL) {
452 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
453 if (ret)
454 goto fail;
455 }
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900456 /* Loop(Array) operation */
457 if (parg->count) {
458 if (scode->op != FETCH_OP_ST_MEM &&
459 scode->op != FETCH_OP_ST_STRING) {
460 pr_info("array only accepts memory or address\n");
461 ret = -EINVAL;
462 goto fail;
463 }
464 code++;
465 if (code->op != FETCH_OP_NOP) {
466 ret = -E2BIG;
467 goto fail;
468 }
469 code->op = FETCH_OP_LP_ARRAY;
470 code->param = parg->count;
471 }
Masami Hiramatsu53305922018-04-25 21:18:03 +0900472 code++;
473 code->op = FETCH_OP_END;
474
475 /* Shrink down the code buffer */
476 parg->code = kzalloc(sizeof(*code) * (code - tmp + 1), GFP_KERNEL);
477 if (!parg->code)
478 ret = -ENOMEM;
479 else
480 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
481
482fail:
483 kfree(tmp);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530484
485 return ret;
486}
487
488/* Return 1 if name is reserved or already used by another argument */
489int traceprobe_conflict_field_name(const char *name,
490 struct probe_arg *args, int narg)
491{
492 int i;
493
494 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
495 if (strcmp(reserved_field_names[i], name) == 0)
496 return 1;
497
498 for (i = 0; i < narg; i++)
499 if (strcmp(args[i].name, name) == 0)
500 return 1;
501
502 return 0;
503}
504
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530505void traceprobe_free_probe_arg(struct probe_arg *arg)
506{
Masami Hiramatsu53305922018-04-25 21:18:03 +0900507 kfree(arg->code);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530508 kfree(arg->name);
509 kfree(arg->comm);
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900510 kfree(arg->fmt);
Srikar Dronamraju8ab83f52012-04-09 14:41:44 +0530511}
512
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900513/* When len=0, we just calculate the needed length */
514#define LEN_OR_ZERO (len ? len - pos : 0)
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900515static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
516 bool is_return)
517{
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900518 struct probe_arg *parg;
519 int i, j;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900520 int pos = 0;
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900521 const char *fmt, *arg;
522
523 if (!is_return) {
524 fmt = "(%lx)";
525 arg = "REC->" FIELD_STRING_IP;
526 } else {
527 fmt = "(%lx <- %lx)";
528 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
529 }
530
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900531 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
532
533 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900534 parg = tp->args + i;
535 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
536 if (parg->count) {
537 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
538 parg->type->fmt);
539 for (j = 1; j < parg->count; j++)
540 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
541 parg->type->fmt);
542 pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
543 } else
544 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
545 parg->type->fmt);
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900546 }
547
548 pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
549
550 for (i = 0; i < tp->nr_args; i++) {
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900551 parg = tp->args + i;
552 if (parg->count) {
553 if (strcmp(parg->type->name, "string") == 0)
554 fmt = ", __get_str(%s[%d])";
555 else
556 fmt = ", REC->%s[%d]";
557 for (j = 0; j < parg->count; j++)
558 pos += snprintf(buf + pos, LEN_OR_ZERO,
559 fmt, parg->name, j);
560 } else {
561 if (strcmp(parg->type->name, "string") == 0)
562 fmt = ", __get_str(%s)";
563 else
564 fmt = ", REC->%s";
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900565 pos += snprintf(buf + pos, LEN_OR_ZERO,
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900566 fmt, parg->name);
567 }
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900568 }
569
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900570 /* return the length of print_fmt */
571 return pos;
572}
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900573#undef LEN_OR_ZERO
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900574
Masami Hiramatsu0a46c852018-04-25 21:19:30 +0900575int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return)
Namhyung Kim5bf652a2013-07-03 16:09:02 +0900576{
577 int len;
578 char *print_fmt;
579
580 /* First: called with 0 length to calculate the needed length */
581 len = __set_print_fmt(tp, NULL, 0, is_return);
582 print_fmt = kmalloc(len + 1, GFP_KERNEL);
583 if (!print_fmt)
584 return -ENOMEM;
585
586 /* Second: actually write the @print_fmt */
587 __set_print_fmt(tp, print_fmt, len + 1, is_return);
588 tp->call.print_fmt = print_fmt;
589
590 return 0;
591}
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900592
593int traceprobe_define_arg_fields(struct trace_event_call *event_call,
594 size_t offset, struct trace_probe *tp)
595{
596 int ret, i;
597
598 /* Set argument names as fields */
599 for (i = 0; i < tp->nr_args; i++) {
600 struct probe_arg *parg = &tp->args[i];
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900601 const char *fmt = parg->type->fmttype;
602 int size = parg->type->size;
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900603
Masami Hiramatsu40b53b72018-04-25 21:21:55 +0900604 if (parg->fmt)
605 fmt = parg->fmt;
606 if (parg->count)
607 size *= parg->count;
608 ret = trace_define_field(event_call, fmt, parg->name,
609 offset + parg->offset, size,
Masami Hiramatsueeb07b02018-04-25 21:17:05 +0900610 parg->type->is_signed,
611 FILTER_OTHER);
612 if (ret)
613 return ret;
614 }
615 return 0;
616}