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