]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - kernel/trace/trace_probe.c
tracing/probe: Check event name length correctly
[zynq/linux.git] / kernel / trace / trace_probe.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Common code for probe-based Dynamic events.
4  *
5  * 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  */
12 #define pr_fmt(fmt)     "trace_probe: " fmt
13
14 #include "trace_probe.h"
15
16 static const 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
27 /* Printing  in basic type function template */
28 #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt)                  \
29 int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
30 {                                                                       \
31         trace_seq_printf(s, fmt, *(type *)data);                        \
32         return !trace_seq_has_overflowed(s);                            \
33 }                                                                       \
34 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
35
36 DEFINE_BASIC_PRINT_TYPE_FUNC(u8,  u8,  "%u")
37 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
38 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
39 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
40 DEFINE_BASIC_PRINT_TYPE_FUNC(s8,  s8,  "%d")
41 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
42 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
43 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
44 DEFINE_BASIC_PRINT_TYPE_FUNC(x8,  u8,  "0x%x")
45 DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
46 DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
47 DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
48
49 int 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 }
54 const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
55
56 /* Print type function for string type */
57 int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
58 {
59         int len = *(u32 *)data >> 16;
60
61         if (!len)
62                 trace_seq_puts(s, "(fault)");
63         else
64                 trace_seq_printf(s, "\"%s\"",
65                                  (const char *)get_loc_data(data, ent));
66         return !trace_seq_has_overflowed(s);
67 }
68
69 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
70
71 /* Fetch type information table */
72 static 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),
89         ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
90
91         ASSIGN_FETCH_TYPE_END
92 };
93
94 static const struct fetch_type *find_fetch_type(const char *type)
95 {
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++;
110                 if (kstrtoul(type, 0, &bs))
111                         goto fail;
112
113                 switch (bs) {
114                 case 8:
115                         return find_fetch_type("u8");
116                 case 16:
117                         return find_fetch_type("u16");
118                 case 32:
119                         return find_fetch_type("u32");
120                 case 64:
121                         return find_fetch_type("u64");
122                 default:
123                         goto fail;
124                 }
125         }
126
127         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];
130         }
131
132 fail:
133         return NULL;
134 }
135
136 /* Split symbol and offset. */
137 int traceprobe_split_symbol_offset(char *symbol, long *offset)
138 {
139         char *tmp;
140         int ret;
141
142         if (!offset)
143                 return -EINVAL;
144
145         tmp = strpbrk(symbol, "+-");
146         if (tmp) {
147                 ret = kstrtol(tmp, 0, offset);
148                 if (ret)
149                         return ret;
150                 *tmp = '\0';
151         } else
152                 *offset = 0;
153
154         return 0;
155 }
156
157 /* @buf must has MAX_EVENT_NAME_LEN size */
158 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
159                                 char *buf)
160 {
161         const char *slash, *event = *pevent;
162         int len;
163
164         slash = strchr(event, '/');
165         if (slash) {
166                 if (slash == event) {
167                         pr_info("Group name is not specified\n");
168                         return -EINVAL;
169                 }
170                 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
171                         pr_info("Group name is too long\n");
172                         return -E2BIG;
173                 }
174                 strlcpy(buf, event, slash - event + 1);
175                 *pgroup = buf;
176                 *pevent = slash + 1;
177                 event = *pevent;
178         }
179         len = strlen(event);
180         if (len == 0) {
181                 pr_info("Event name is not specified\n");
182                 return -EINVAL;
183         } else if (len > MAX_EVENT_NAME_LEN) {
184                 pr_info("Event name is too long\n");
185                 return -E2BIG;
186         }
187         return 0;
188 }
189
190 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
191
192 static int parse_probe_vars(char *arg, const struct fetch_type *t,
193                             struct fetch_insn *code, unsigned int flags)
194 {
195         unsigned long param;
196         int ret = 0;
197         int len;
198
199         if (strcmp(arg, "retval") == 0) {
200                 if (flags & TPARG_FL_RETURN)
201                         code->op = FETCH_OP_RETVAL;
202                 else
203                         ret = -EINVAL;
204         } else if ((len = str_has_prefix(arg, "stack"))) {
205                 if (arg[len] == '\0') {
206                         code->op = FETCH_OP_STACKP;
207                 } else if (isdigit(arg[len])) {
208                         ret = kstrtoul(arg + len, 10, &param);
209                         if (ret || ((flags & TPARG_FL_KERNEL) &&
210                                     param > PARAM_MAX_STACK))
211                                 ret = -EINVAL;
212                         else {
213                                 code->op = FETCH_OP_STACK;
214                                 code->param = (unsigned int)param;
215                         }
216                 } else
217                         ret = -EINVAL;
218         } else if (strcmp(arg, "comm") == 0) {
219                 code->op = FETCH_OP_COMM;
220 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
221         } else if (((flags & TPARG_FL_MASK) ==
222                     (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
223                    (len = str_has_prefix(arg, "arg"))) {
224                 if (!isdigit(arg[len]))
225                         return -EINVAL;
226                 ret = kstrtoul(arg + len, 10, &param);
227                 if (ret || !param || param > PARAM_MAX_STACK)
228                         return -EINVAL;
229                 code->op = FETCH_OP_ARG;
230                 code->param = (unsigned int)param - 1;
231 #endif
232         } else
233                 ret = -EINVAL;
234
235         return ret;
236 }
237
238 /* Recursive argument parser */
239 static int
240 parse_probe_arg(char *arg, const struct fetch_type *type,
241                 struct fetch_insn **pcode, struct fetch_insn *end,
242                 unsigned int flags)
243 {
244         struct fetch_insn *code = *pcode;
245         unsigned long param;
246         long offset = 0;
247         char *tmp;
248         int ret = 0;
249
250         switch (arg[0]) {
251         case '$':
252                 ret = parse_probe_vars(arg + 1, type, code, flags);
253                 break;
254
255         case '%':       /* named register */
256                 ret = regs_query_register_offset(arg + 1);
257                 if (ret >= 0) {
258                         code->op = FETCH_OP_REG;
259                         code->param = (unsigned int)ret;
260                         ret = 0;
261                 }
262                 break;
263
264         case '@':       /* memory, file-offset or symbol */
265                 if (isdigit(arg[1])) {
266                         ret = kstrtoul(arg + 1, 0, &param);
267                         if (ret)
268                                 break;
269                         /* load address */
270                         code->op = FETCH_OP_IMM;
271                         code->immediate = param;
272                 } else if (arg[1] == '+') {
273                         /* kprobes don't support file offsets */
274                         if (flags & TPARG_FL_KERNEL)
275                                 return -EINVAL;
276
277                         ret = kstrtol(arg + 2, 0, &offset);
278                         if (ret)
279                                 break;
280
281                         code->op = FETCH_OP_FOFFS;
282                         code->immediate = (unsigned long)offset;  // imm64?
283                 } else {
284                         /* uprobes don't support symbols */
285                         if (!(flags & TPARG_FL_KERNEL))
286                                 return -EINVAL;
287
288                         /* Preserve symbol for updating */
289                         code->op = FETCH_NOP_SYMBOL;
290                         code->data = kstrdup(arg + 1, GFP_KERNEL);
291                         if (!code->data)
292                                 return -ENOMEM;
293                         if (++code == end)
294                                 return -E2BIG;
295
296                         code->op = FETCH_OP_IMM;
297                         code->immediate = 0;
298                 }
299                 /* These are fetching from memory */
300                 if (++code == end)
301                         return -E2BIG;
302                 *pcode = code;
303                 code->op = FETCH_OP_DEREF;
304                 code->offset = offset;
305                 break;
306
307         case '+':       /* deref memory */
308                 arg++;  /* Skip '+', because kstrtol() rejects it. */
309                 /* fall through */
310         case '-':
311                 tmp = strchr(arg, '(');
312                 if (!tmp)
313                         return -EINVAL;
314
315                 *tmp = '\0';
316                 ret = kstrtol(arg, 0, &offset);
317                 if (ret)
318                         break;
319
320                 arg = tmp + 1;
321                 tmp = strrchr(arg, ')');
322
323                 if (tmp) {
324                         const struct fetch_type *t2 = find_fetch_type(NULL);
325
326                         *tmp = '\0';
327                         ret = parse_probe_arg(arg, t2, &code, end, flags);
328                         if (ret)
329                                 break;
330                         if (code->op == FETCH_OP_COMM)
331                                 return -EINVAL;
332                         if (++code == end)
333                                 return -E2BIG;
334                         *pcode = code;
335
336                         code->op = FETCH_OP_DEREF;
337                         code->offset = offset;
338                 }
339                 break;
340         }
341         if (!ret && code->op == FETCH_OP_NOP) {
342                 /* Parsed, but do not find fetch method */
343                 ret = -EINVAL;
344         }
345         return ret;
346 }
347
348 #define BYTES_TO_BITS(nb)       ((BITS_PER_LONG * (nb)) / sizeof(long))
349
350 /* Bitfield type needs to be parsed into a fetch function */
351 static int __parse_bitfield_probe_arg(const char *bf,
352                                       const struct fetch_type *t,
353                                       struct fetch_insn **pcode)
354 {
355         struct fetch_insn *code = *pcode;
356         unsigned long bw, bo;
357         char *tail;
358
359         if (*bf != 'b')
360                 return 0;
361
362         bw = simple_strtoul(bf + 1, &tail, 0);  /* Use simple one */
363
364         if (bw == 0 || *tail != '@')
365                 return -EINVAL;
366
367         bf = tail + 1;
368         bo = simple_strtoul(bf, &tail, 0);
369
370         if (tail == bf || *tail != '/')
371                 return -EINVAL;
372         code++;
373         if (code->op != FETCH_OP_NOP)
374                 return -E2BIG;
375         *pcode = code;
376
377         code->op = FETCH_OP_MOD_BF;
378         code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
379         code->rshift = BYTES_TO_BITS(t->size) - bw;
380         code->basesize = t->size;
381
382         return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
383 }
384
385 /* String length checking wrapper */
386 static int traceprobe_parse_probe_arg_body(char *arg, ssize_t *size,
387                 struct probe_arg *parg, unsigned int flags)
388 {
389         struct fetch_insn *code, *scode, *tmp = NULL;
390         char *t, *t2;
391         int ret, len;
392
393         if (strlen(arg) > MAX_ARGSTR_LEN) {
394                 pr_info("Argument is too long.: %s\n",  arg);
395                 return -ENOSPC;
396         }
397         parg->comm = kstrdup(arg, GFP_KERNEL);
398         if (!parg->comm) {
399                 pr_info("Failed to allocate memory for command '%s'.\n", arg);
400                 return -ENOMEM;
401         }
402         t = strchr(arg, ':');
403         if (t) {
404                 *t = '\0';
405                 t2 = strchr(++t, '[');
406                 if (t2) {
407                         *t2 = '\0';
408                         parg->count = simple_strtoul(t2 + 1, &t2, 0);
409                         if (strcmp(t2, "]") || parg->count == 0)
410                                 return -EINVAL;
411                         if (parg->count > MAX_ARRAY_LEN)
412                                 return -E2BIG;
413                 }
414         }
415         /*
416          * The default type of $comm should be "string", and it can't be
417          * dereferenced.
418          */
419         if (!t && strcmp(arg, "$comm") == 0)
420                 parg->type = find_fetch_type("string");
421         else
422                 parg->type = find_fetch_type(t);
423         if (!parg->type) {
424                 pr_info("Unsupported type: %s\n", t);
425                 return -EINVAL;
426         }
427         parg->offset = *size;
428         *size += parg->type->size * (parg->count ?: 1);
429
430         if (parg->count) {
431                 len = strlen(parg->type->fmttype) + 6;
432                 parg->fmt = kmalloc(len, GFP_KERNEL);
433                 if (!parg->fmt)
434                         return -ENOMEM;
435                 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
436                          parg->count);
437         }
438
439         code = tmp = kzalloc(sizeof(*code) * FETCH_INSN_MAX, GFP_KERNEL);
440         if (!code)
441                 return -ENOMEM;
442         code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
443
444         ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
445                               flags);
446         if (ret)
447                 goto fail;
448
449         /* Store operation */
450         if (!strcmp(parg->type->name, "string")) {
451                 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_IMM &&
452                     code->op != FETCH_OP_COMM) {
453                         pr_info("string only accepts memory or address.\n");
454                         ret = -EINVAL;
455                         goto fail;
456                 }
457                 if (code->op != FETCH_OP_DEREF || parg->count) {
458                         /*
459                          * IMM and COMM is pointing actual address, those must
460                          * be kept, and if parg->count != 0, this is an array
461                          * of string pointers instead of string address itself.
462                          */
463                         code++;
464                         if (code->op != FETCH_OP_NOP) {
465                                 ret = -E2BIG;
466                                 goto fail;
467                         }
468                 }
469                 code->op = FETCH_OP_ST_STRING;  /* In DEREF case, replace it */
470                 code->size = parg->type->size;
471                 parg->dynamic = true;
472         } else if (code->op == FETCH_OP_DEREF) {
473                 code->op = FETCH_OP_ST_MEM;
474                 code->size = parg->type->size;
475         } else {
476                 code++;
477                 if (code->op != FETCH_OP_NOP) {
478                         ret = -E2BIG;
479                         goto fail;
480                 }
481                 code->op = FETCH_OP_ST_RAW;
482                 code->size = parg->type->size;
483         }
484         scode = code;
485         /* Modify operation */
486         if (t != NULL) {
487                 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
488                 if (ret)
489                         goto fail;
490         }
491         /* Loop(Array) operation */
492         if (parg->count) {
493                 if (scode->op != FETCH_OP_ST_MEM &&
494                     scode->op != FETCH_OP_ST_STRING) {
495                         pr_info("array only accepts memory or address\n");
496                         ret = -EINVAL;
497                         goto fail;
498                 }
499                 code++;
500                 if (code->op != FETCH_OP_NOP) {
501                         ret = -E2BIG;
502                         goto fail;
503                 }
504                 code->op = FETCH_OP_LP_ARRAY;
505                 code->param = parg->count;
506         }
507         code++;
508         code->op = FETCH_OP_END;
509
510         /* Shrink down the code buffer */
511         parg->code = kzalloc(sizeof(*code) * (code - tmp + 1), GFP_KERNEL);
512         if (!parg->code)
513                 ret = -ENOMEM;
514         else
515                 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
516
517 fail:
518         if (ret) {
519                 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
520                         if (code->op == FETCH_NOP_SYMBOL)
521                                 kfree(code->data);
522         }
523         kfree(tmp);
524
525         return ret;
526 }
527
528 /* Return 1 if name is reserved or already used by another argument */
529 static int traceprobe_conflict_field_name(const char *name,
530                                           struct probe_arg *args, int narg)
531 {
532         int i;
533
534         for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
535                 if (strcmp(reserved_field_names[i], name) == 0)
536                         return 1;
537
538         for (i = 0; i < narg; i++)
539                 if (strcmp(args[i].name, name) == 0)
540                         return 1;
541
542         return 0;
543 }
544
545 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, char *arg,
546                                 unsigned int flags)
547 {
548         struct probe_arg *parg = &tp->args[i];
549         char *body;
550         int ret;
551
552         /* Increment count for freeing args in error case */
553         tp->nr_args++;
554
555         body = strchr(arg, '=');
556         if (body) {
557                 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
558                 body++;
559         } else {
560                 /* If argument name is omitted, set "argN" */
561                 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
562                 body = arg;
563         }
564         if (!parg->name)
565                 return -ENOMEM;
566
567         if (!is_good_name(parg->name)) {
568                 pr_info("Invalid argument[%d] name: %s\n",
569                         i, parg->name);
570                 return -EINVAL;
571         }
572
573         if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
574                 pr_info("Argument[%d]: '%s' conflicts with another field.\n",
575                         i, parg->name);
576                 return -EINVAL;
577         }
578
579         /* Parse fetch argument */
580         ret = traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags);
581         if (ret)
582                 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
583         return ret;
584 }
585
586 void traceprobe_free_probe_arg(struct probe_arg *arg)
587 {
588         struct fetch_insn *code = arg->code;
589
590         while (code && code->op != FETCH_OP_END) {
591                 if (code->op == FETCH_NOP_SYMBOL)
592                         kfree(code->data);
593                 code++;
594         }
595         kfree(arg->code);
596         kfree(arg->name);
597         kfree(arg->comm);
598         kfree(arg->fmt);
599 }
600
601 int traceprobe_update_arg(struct probe_arg *arg)
602 {
603         struct fetch_insn *code = arg->code;
604         long offset;
605         char *tmp;
606         char c;
607         int ret = 0;
608
609         while (code && code->op != FETCH_OP_END) {
610                 if (code->op == FETCH_NOP_SYMBOL) {
611                         if (code[1].op != FETCH_OP_IMM)
612                                 return -EINVAL;
613
614                         tmp = strpbrk(code->data, "+-");
615                         if (tmp)
616                                 c = *tmp;
617                         ret = traceprobe_split_symbol_offset(code->data,
618                                                              &offset);
619                         if (ret)
620                                 return ret;
621
622                         code[1].immediate =
623                                 (unsigned long)kallsyms_lookup_name(code->data);
624                         if (tmp)
625                                 *tmp = c;
626                         if (!code[1].immediate)
627                                 return -ENOENT;
628                         code[1].immediate += offset;
629                 }
630                 code++;
631         }
632         return 0;
633 }
634
635 /* When len=0, we just calculate the needed length */
636 #define LEN_OR_ZERO (len ? len - pos : 0)
637 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
638                            bool is_return)
639 {
640         struct probe_arg *parg;
641         int i, j;
642         int pos = 0;
643         const char *fmt, *arg;
644
645         if (!is_return) {
646                 fmt = "(%lx)";
647                 arg = "REC->" FIELD_STRING_IP;
648         } else {
649                 fmt = "(%lx <- %lx)";
650                 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
651         }
652
653         pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
654
655         for (i = 0; i < tp->nr_args; i++) {
656                 parg = tp->args + i;
657                 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
658                 if (parg->count) {
659                         pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
660                                         parg->type->fmt);
661                         for (j = 1; j < parg->count; j++)
662                                 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
663                                                 parg->type->fmt);
664                         pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
665                 } else
666                         pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
667                                         parg->type->fmt);
668         }
669
670         pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
671
672         for (i = 0; i < tp->nr_args; i++) {
673                 parg = tp->args + i;
674                 if (parg->count) {
675                         if (strcmp(parg->type->name, "string") == 0)
676                                 fmt = ", __get_str(%s[%d])";
677                         else
678                                 fmt = ", REC->%s[%d]";
679                         for (j = 0; j < parg->count; j++)
680                                 pos += snprintf(buf + pos, LEN_OR_ZERO,
681                                                 fmt, parg->name, j);
682                 } else {
683                         if (strcmp(parg->type->name, "string") == 0)
684                                 fmt = ", __get_str(%s)";
685                         else
686                                 fmt = ", REC->%s";
687                         pos += snprintf(buf + pos, LEN_OR_ZERO,
688                                         fmt, parg->name);
689                 }
690         }
691
692         /* return the length of print_fmt */
693         return pos;
694 }
695 #undef LEN_OR_ZERO
696
697 int traceprobe_set_print_fmt(struct trace_probe *tp, bool is_return)
698 {
699         int len;
700         char *print_fmt;
701
702         /* First: called with 0 length to calculate the needed length */
703         len = __set_print_fmt(tp, NULL, 0, is_return);
704         print_fmt = kmalloc(len + 1, GFP_KERNEL);
705         if (!print_fmt)
706                 return -ENOMEM;
707
708         /* Second: actually write the @print_fmt */
709         __set_print_fmt(tp, print_fmt, len + 1, is_return);
710         tp->call.print_fmt = print_fmt;
711
712         return 0;
713 }
714
715 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
716                                  size_t offset, struct trace_probe *tp)
717 {
718         int ret, i;
719
720         /* Set argument names as fields */
721         for (i = 0; i < tp->nr_args; i++) {
722                 struct probe_arg *parg = &tp->args[i];
723                 const char *fmt = parg->type->fmttype;
724                 int size = parg->type->size;
725
726                 if (parg->fmt)
727                         fmt = parg->fmt;
728                 if (parg->count)
729                         size *= parg->count;
730                 ret = trace_define_field(event_call, fmt, parg->name,
731                                          offset + parg->offset, size,
732                                          parg->type->is_signed,
733                                          FILTER_OTHER);
734                 if (ret)
735                         return ret;
736         }
737         return 0;
738 }