0888f0644d2575f56ed2dbb90f7b0e3e82b01e7c
[releases.git] / 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 #undef C
17 #define C(a, b)         b
18
19 static const char *trace_probe_err_text[] = { ERRORS };
20
21 static const char *reserved_field_names[] = {
22         "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
32 /* Printing  in basic type function template */
33 #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt)                  \
34 int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
35 {                                                                       \
36         trace_seq_printf(s, fmt, *(type *)data);                        \
37         return !trace_seq_has_overflowed(s);                            \
38 }                                                                       \
39 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
40
41 DEFINE_BASIC_PRINT_TYPE_FUNC(u8,  u8,  "%u")
42 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
43 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
44 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
45 DEFINE_BASIC_PRINT_TYPE_FUNC(s8,  s8,  "%d")
46 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
47 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
48 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
49 DEFINE_BASIC_PRINT_TYPE_FUNC(x8,  u8,  "0x%x")
50 DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
53
54 int 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 }
59 const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
60
61 /* Print type function for string type */
62 int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
63 {
64         int len = *(u32 *)data >> 16;
65
66         if (!len)
67                 trace_seq_puts(s, FAULT_STRING);
68         else
69                 trace_seq_printf(s, "\"%s\"",
70                                  (const char *)get_loc_data(data, ent));
71         return !trace_seq_has_overflowed(s);
72 }
73
74 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
75
76 /* Fetch type information table */
77 static const struct fetch_type probe_fetch_types[] = {
78         /* Special types */
79         __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, 1,
80                             "__data_loc char[]"),
81         __ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, 1,
82                             "__data_loc char[]"),
83         __ASSIGN_FETCH_TYPE("symstr", string, string, sizeof(u32), 1, 1,
84                             "__data_loc char[]"),
85         /* Basic types */
86         ASSIGN_FETCH_TYPE(u8,  u8,  0),
87         ASSIGN_FETCH_TYPE(u16, u16, 0),
88         ASSIGN_FETCH_TYPE(u32, u32, 0),
89         ASSIGN_FETCH_TYPE(u64, u64, 0),
90         ASSIGN_FETCH_TYPE(s8,  u8,  1),
91         ASSIGN_FETCH_TYPE(s16, u16, 1),
92         ASSIGN_FETCH_TYPE(s32, u32, 1),
93         ASSIGN_FETCH_TYPE(s64, u64, 1),
94         ASSIGN_FETCH_TYPE_ALIAS(x8,  u8,  u8,  0),
95         ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
96         ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
97         ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
98         ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
99
100         ASSIGN_FETCH_TYPE_END
101 };
102
103 static const struct fetch_type *find_fetch_type(const char *type)
104 {
105         int i;
106
107         if (!type)
108                 type = DEFAULT_FETCH_TYPE_STR;
109
110         /* Special case: bitfield */
111         if (*type == 'b') {
112                 unsigned long bs;
113
114                 type = strchr(type, '/');
115                 if (!type)
116                         goto fail;
117
118                 type++;
119                 if (kstrtoul(type, 0, &bs))
120                         goto fail;
121
122                 switch (bs) {
123                 case 8:
124                         return find_fetch_type("u8");
125                 case 16:
126                         return find_fetch_type("u16");
127                 case 32:
128                         return find_fetch_type("u32");
129                 case 64:
130                         return find_fetch_type("u64");
131                 default:
132                         goto fail;
133                 }
134         }
135
136         for (i = 0; probe_fetch_types[i].name; i++) {
137                 if (strcmp(type, probe_fetch_types[i].name) == 0)
138                         return &probe_fetch_types[i];
139         }
140
141 fail:
142         return NULL;
143 }
144
145 static struct trace_probe_log trace_probe_log;
146
147 void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
148 {
149         trace_probe_log.subsystem = subsystem;
150         trace_probe_log.argc = argc;
151         trace_probe_log.argv = argv;
152         trace_probe_log.index = 0;
153 }
154
155 void trace_probe_log_clear(void)
156 {
157         memset(&trace_probe_log, 0, sizeof(trace_probe_log));
158 }
159
160 void trace_probe_log_set_index(int index)
161 {
162         trace_probe_log.index = index;
163 }
164
165 void __trace_probe_log_err(int offset, int err_type)
166 {
167         char *command, *p;
168         int i, len = 0, pos = 0;
169
170         if (!trace_probe_log.argv)
171                 return;
172
173         /* Recalculate the length and allocate buffer */
174         for (i = 0; i < trace_probe_log.argc; i++) {
175                 if (i == trace_probe_log.index)
176                         pos = len;
177                 len += strlen(trace_probe_log.argv[i]) + 1;
178         }
179         command = kzalloc(len, GFP_KERNEL);
180         if (!command)
181                 return;
182
183         if (trace_probe_log.index >= trace_probe_log.argc) {
184                 /**
185                  * Set the error position is next to the last arg + space.
186                  * Note that len includes the terminal null and the cursor
187                  * appears at pos + 1.
188                  */
189                 pos = len;
190                 offset = 0;
191         }
192
193         /* And make a command string from argv array */
194         p = command;
195         for (i = 0; i < trace_probe_log.argc; i++) {
196                 len = strlen(trace_probe_log.argv[i]);
197                 strcpy(p, trace_probe_log.argv[i]);
198                 p[len] = ' ';
199                 p += len + 1;
200         }
201         *(p - 1) = '\0';
202
203         tracing_log_err(NULL, trace_probe_log.subsystem, command,
204                         trace_probe_err_text, err_type, pos + offset);
205
206         kfree(command);
207 }
208
209 /* Split symbol and offset. */
210 int traceprobe_split_symbol_offset(char *symbol, long *offset)
211 {
212         char *tmp;
213         int ret;
214
215         if (!offset)
216                 return -EINVAL;
217
218         tmp = strpbrk(symbol, "+-");
219         if (tmp) {
220                 ret = kstrtol(tmp, 0, offset);
221                 if (ret)
222                         return ret;
223                 *tmp = '\0';
224         } else
225                 *offset = 0;
226
227         return 0;
228 }
229
230 /* @buf must has MAX_EVENT_NAME_LEN size */
231 int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
232                                 char *buf, int offset)
233 {
234         const char *slash, *event = *pevent;
235         int len;
236
237         slash = strchr(event, '/');
238         if (!slash)
239                 slash = strchr(event, '.');
240
241         if (slash) {
242                 if (slash == event) {
243                         trace_probe_log_err(offset, NO_GROUP_NAME);
244                         return -EINVAL;
245                 }
246                 if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
247                         trace_probe_log_err(offset, GROUP_TOO_LONG);
248                         return -EINVAL;
249                 }
250                 strlcpy(buf, event, slash - event + 1);
251                 if (!is_good_system_name(buf)) {
252                         trace_probe_log_err(offset, BAD_GROUP_NAME);
253                         return -EINVAL;
254                 }
255                 *pgroup = buf;
256                 *pevent = slash + 1;
257                 offset += slash - event + 1;
258                 event = *pevent;
259         }
260         len = strlen(event);
261         if (len == 0) {
262                 trace_probe_log_err(offset, NO_EVENT_NAME);
263                 return -EINVAL;
264         } else if (len > MAX_EVENT_NAME_LEN) {
265                 trace_probe_log_err(offset, EVENT_TOO_LONG);
266                 return -EINVAL;
267         }
268         if (!is_good_name(event)) {
269                 trace_probe_log_err(offset, BAD_EVENT_NAME);
270                 return -EINVAL;
271         }
272         return 0;
273 }
274
275 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
276
277 static int parse_probe_vars(char *arg, const struct fetch_type *t,
278                         struct fetch_insn *code, unsigned int flags, int offs)
279 {
280         unsigned long param;
281         int ret = 0;
282         int len;
283
284         if (flags & TPARG_FL_TPOINT) {
285                 if (code->data)
286                         return -EFAULT;
287                 code->data = kstrdup(arg, GFP_KERNEL);
288                 if (!code->data)
289                         return -ENOMEM;
290                 code->op = FETCH_OP_TP_ARG;
291         } else if (strcmp(arg, "retval") == 0) {
292                 if (flags & TPARG_FL_RETURN) {
293                         code->op = FETCH_OP_RETVAL;
294                 } else {
295                         trace_probe_log_err(offs, RETVAL_ON_PROBE);
296                         ret = -EINVAL;
297                 }
298         } else if ((len = str_has_prefix(arg, "stack"))) {
299                 if (arg[len] == '\0') {
300                         code->op = FETCH_OP_STACKP;
301                 } else if (isdigit(arg[len])) {
302                         ret = kstrtoul(arg + len, 10, &param);
303                         if (ret) {
304                                 goto inval_var;
305                         } else if ((flags & TPARG_FL_KERNEL) &&
306                                     param > PARAM_MAX_STACK) {
307                                 trace_probe_log_err(offs, BAD_STACK_NUM);
308                                 ret = -EINVAL;
309                         } else {
310                                 code->op = FETCH_OP_STACK;
311                                 code->param = (unsigned int)param;
312                         }
313                 } else
314                         goto inval_var;
315         } else if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
316                 code->op = FETCH_OP_COMM;
317 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
318         } else if (((flags & TPARG_FL_MASK) ==
319                     (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) &&
320                    (len = str_has_prefix(arg, "arg"))) {
321                 ret = kstrtoul(arg + len, 10, &param);
322                 if (ret) {
323                         goto inval_var;
324                 } else if (!param || param > PARAM_MAX_STACK) {
325                         trace_probe_log_err(offs, BAD_ARG_NUM);
326                         return -EINVAL;
327                 }
328                 code->op = FETCH_OP_ARG;
329                 code->param = (unsigned int)param - 1;
330 #endif
331         } else
332                 goto inval_var;
333
334         return ret;
335
336 inval_var:
337         trace_probe_log_err(offs, BAD_VAR);
338         return -EINVAL;
339 }
340
341 static int str_to_immediate(char *str, unsigned long *imm)
342 {
343         if (isdigit(str[0]))
344                 return kstrtoul(str, 0, imm);
345         else if (str[0] == '-')
346                 return kstrtol(str, 0, (long *)imm);
347         else if (str[0] == '+')
348                 return kstrtol(str + 1, 0, (long *)imm);
349         return -EINVAL;
350 }
351
352 static int __parse_imm_string(char *str, char **pbuf, int offs)
353 {
354         size_t len = strlen(str);
355
356         if (str[len - 1] != '"') {
357                 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
358                 return -EINVAL;
359         }
360         *pbuf = kstrndup(str, len - 1, GFP_KERNEL);
361         if (!*pbuf)
362                 return -ENOMEM;
363         return 0;
364 }
365
366 /* Recursive argument parser */
367 static int
368 parse_probe_arg(char *arg, const struct fetch_type *type,
369                 struct fetch_insn **pcode, struct fetch_insn *end,
370                 unsigned int flags, int offs)
371 {
372         struct fetch_insn *code = *pcode;
373         unsigned long param;
374         int deref = FETCH_OP_DEREF;
375         long offset = 0;
376         char *tmp;
377         int ret = 0;
378
379         switch (arg[0]) {
380         case '$':
381                 ret = parse_probe_vars(arg + 1, type, code, flags, offs);
382                 break;
383
384         case '%':       /* named register */
385                 if (flags & TPARG_FL_TPOINT) {
386                         /* eprobes do not handle registers */
387                         trace_probe_log_err(offs, BAD_VAR);
388                         break;
389                 }
390                 ret = regs_query_register_offset(arg + 1);
391                 if (ret >= 0) {
392                         code->op = FETCH_OP_REG;
393                         code->param = (unsigned int)ret;
394                         ret = 0;
395                 } else
396                         trace_probe_log_err(offs, BAD_REG_NAME);
397                 break;
398
399         case '@':       /* memory, file-offset or symbol */
400                 if (isdigit(arg[1])) {
401                         ret = kstrtoul(arg + 1, 0, &param);
402                         if (ret) {
403                                 trace_probe_log_err(offs, BAD_MEM_ADDR);
404                                 break;
405                         }
406                         /* load address */
407                         code->op = FETCH_OP_IMM;
408                         code->immediate = param;
409                 } else if (arg[1] == '+') {
410                         /* kprobes don't support file offsets */
411                         if (flags & TPARG_FL_KERNEL) {
412                                 trace_probe_log_err(offs, FILE_ON_KPROBE);
413                                 return -EINVAL;
414                         }
415                         ret = kstrtol(arg + 2, 0, &offset);
416                         if (ret) {
417                                 trace_probe_log_err(offs, BAD_FILE_OFFS);
418                                 break;
419                         }
420
421                         code->op = FETCH_OP_FOFFS;
422                         code->immediate = (unsigned long)offset;  // imm64?
423                 } else {
424                         /* uprobes don't support symbols */
425                         if (!(flags & TPARG_FL_KERNEL)) {
426                                 trace_probe_log_err(offs, SYM_ON_UPROBE);
427                                 return -EINVAL;
428                         }
429                         /* Preserve symbol for updating */
430                         code->op = FETCH_NOP_SYMBOL;
431                         code->data = kstrdup(arg + 1, GFP_KERNEL);
432                         if (!code->data)
433                                 return -ENOMEM;
434                         if (++code == end) {
435                                 trace_probe_log_err(offs, TOO_MANY_OPS);
436                                 return -EINVAL;
437                         }
438                         code->op = FETCH_OP_IMM;
439                         code->immediate = 0;
440                 }
441                 /* These are fetching from memory */
442                 if (++code == end) {
443                         trace_probe_log_err(offs, TOO_MANY_OPS);
444                         return -EINVAL;
445                 }
446                 *pcode = code;
447                 code->op = FETCH_OP_DEREF;
448                 code->offset = offset;
449                 break;
450
451         case '+':       /* deref memory */
452         case '-':
453                 if (arg[1] == 'u') {
454                         deref = FETCH_OP_UDEREF;
455                         arg[1] = arg[0];
456                         arg++;
457                 }
458                 if (arg[0] == '+')
459                         arg++;  /* Skip '+', because kstrtol() rejects it. */
460                 tmp = strchr(arg, '(');
461                 if (!tmp) {
462                         trace_probe_log_err(offs, DEREF_NEED_BRACE);
463                         return -EINVAL;
464                 }
465                 *tmp = '\0';
466                 ret = kstrtol(arg, 0, &offset);
467                 if (ret) {
468                         trace_probe_log_err(offs, BAD_DEREF_OFFS);
469                         break;
470                 }
471                 offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
472                 arg = tmp + 1;
473                 tmp = strrchr(arg, ')');
474                 if (!tmp) {
475                         trace_probe_log_err(offs + strlen(arg),
476                                             DEREF_OPEN_BRACE);
477                         return -EINVAL;
478                 } else {
479                         const struct fetch_type *t2 = find_fetch_type(NULL);
480
481                         *tmp = '\0';
482                         ret = parse_probe_arg(arg, t2, &code, end, flags, offs);
483                         if (ret)
484                                 break;
485                         if (code->op == FETCH_OP_COMM ||
486                             code->op == FETCH_OP_DATA) {
487                                 trace_probe_log_err(offs, COMM_CANT_DEREF);
488                                 return -EINVAL;
489                         }
490                         if (++code == end) {
491                                 trace_probe_log_err(offs, TOO_MANY_OPS);
492                                 return -EINVAL;
493                         }
494                         *pcode = code;
495
496                         code->op = deref;
497                         code->offset = offset;
498                 }
499                 break;
500         case '\\':      /* Immediate value */
501                 if (arg[1] == '"') {    /* Immediate string */
502                         ret = __parse_imm_string(arg + 2, &tmp, offs + 2);
503                         if (ret)
504                                 break;
505                         code->op = FETCH_OP_DATA;
506                         code->data = tmp;
507                 } else {
508                         ret = str_to_immediate(arg + 1, &code->immediate);
509                         if (ret)
510                                 trace_probe_log_err(offs + 1, BAD_IMM);
511                         else
512                                 code->op = FETCH_OP_IMM;
513                 }
514                 break;
515         }
516         if (!ret && code->op == FETCH_OP_NOP) {
517                 /* Parsed, but do not find fetch method */
518                 trace_probe_log_err(offs, BAD_FETCH_ARG);
519                 ret = -EINVAL;
520         }
521         return ret;
522 }
523
524 #define BYTES_TO_BITS(nb)       ((BITS_PER_LONG * (nb)) / sizeof(long))
525
526 /* Bitfield type needs to be parsed into a fetch function */
527 static int __parse_bitfield_probe_arg(const char *bf,
528                                       const struct fetch_type *t,
529                                       struct fetch_insn **pcode)
530 {
531         struct fetch_insn *code = *pcode;
532         unsigned long bw, bo;
533         char *tail;
534
535         if (*bf != 'b')
536                 return 0;
537
538         bw = simple_strtoul(bf + 1, &tail, 0);  /* Use simple one */
539
540         if (bw == 0 || *tail != '@')
541                 return -EINVAL;
542
543         bf = tail + 1;
544         bo = simple_strtoul(bf, &tail, 0);
545
546         if (tail == bf || *tail != '/')
547                 return -EINVAL;
548         code++;
549         if (code->op != FETCH_OP_NOP)
550                 return -EINVAL;
551         *pcode = code;
552
553         code->op = FETCH_OP_MOD_BF;
554         code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
555         code->rshift = BYTES_TO_BITS(t->size) - bw;
556         code->basesize = t->size;
557
558         return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
559 }
560
561 /* String length checking wrapper */
562 static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
563                 struct probe_arg *parg, unsigned int flags, int offset)
564 {
565         struct fetch_insn *code, *scode, *tmp = NULL;
566         char *t, *t2, *t3;
567         char *arg;
568         int ret, len;
569
570         arg = kstrdup(argv, GFP_KERNEL);
571         if (!arg)
572                 return -ENOMEM;
573
574         ret = -EINVAL;
575         len = strlen(arg);
576         if (len > MAX_ARGSTR_LEN) {
577                 trace_probe_log_err(offset, ARG_TOO_LONG);
578                 goto out;
579         } else if (len == 0) {
580                 trace_probe_log_err(offset, NO_ARG_BODY);
581                 goto out;
582         }
583
584         ret = -ENOMEM;
585         parg->comm = kstrdup(arg, GFP_KERNEL);
586         if (!parg->comm)
587                 goto out;
588
589         ret = -EINVAL;
590         t = strchr(arg, ':');
591         if (t) {
592                 *t = '\0';
593                 t2 = strchr(++t, '[');
594                 if (t2) {
595                         *t2++ = '\0';
596                         t3 = strchr(t2, ']');
597                         if (!t3) {
598                                 offset += t2 + strlen(t2) - arg;
599                                 trace_probe_log_err(offset,
600                                                     ARRAY_NO_CLOSE);
601                                 goto out;
602                         } else if (t3[1] != '\0') {
603                                 trace_probe_log_err(offset + t3 + 1 - arg,
604                                                     BAD_ARRAY_SUFFIX);
605                                 goto out;
606                         }
607                         *t3 = '\0';
608                         if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
609                                 trace_probe_log_err(offset + t2 - arg,
610                                                     BAD_ARRAY_NUM);
611                                 goto out;
612                         }
613                         if (parg->count > MAX_ARRAY_LEN) {
614                                 trace_probe_log_err(offset + t2 - arg,
615                                                     ARRAY_TOO_BIG);
616                                 goto out;
617                         }
618                 }
619         }
620
621         /*
622          * Since $comm and immediate string can not be dereferenced,
623          * we can find those by strcmp. But ignore for eprobes.
624          */
625         if (!(flags & TPARG_FL_TPOINT) &&
626             (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
627              strncmp(arg, "\\\"", 2) == 0)) {
628                 /* The type of $comm must be "string", and not an array. */
629                 if (parg->count || (t && strcmp(t, "string")))
630                         goto out;
631                 parg->type = find_fetch_type("string");
632         } else
633                 parg->type = find_fetch_type(t);
634         if (!parg->type) {
635                 trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE);
636                 goto out;
637         }
638         parg->offset = *size;
639         *size += parg->type->size * (parg->count ?: 1);
640
641         ret = -ENOMEM;
642         if (parg->count) {
643                 len = strlen(parg->type->fmttype) + 6;
644                 parg->fmt = kmalloc(len, GFP_KERNEL);
645                 if (!parg->fmt)
646                         goto out;
647                 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
648                          parg->count);
649         }
650
651         code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
652         if (!code)
653                 goto out;
654         code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
655
656         ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
657                               flags, offset);
658         if (ret)
659                 goto fail;
660
661         ret = -EINVAL;
662         /* Store operation */
663         if (parg->type->is_string) {
664                 if (!strcmp(parg->type->name, "symstr")) {
665                         if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
666                             code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG &&
667                             code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) {
668                                 trace_probe_log_err(offset + (t ? (t - arg) : 0),
669                                                     BAD_SYMSTRING);
670                                 goto fail;
671                         }
672                 } else {
673                         if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
674                             code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
675                             code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
676                                 trace_probe_log_err(offset + (t ? (t - arg) : 0),
677                                                     BAD_STRING);
678                                 goto fail;
679                         }
680                 }
681                 if (!strcmp(parg->type->name, "symstr") ||
682                     (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
683                      code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
684                      parg->count) {
685                         /*
686                          * IMM, DATA and COMM is pointing actual address, those
687                          * must be kept, and if parg->count != 0, this is an
688                          * array of string pointers instead of string address
689                          * itself.
690                          * For the symstr, it doesn't need to dereference, thus
691                          * it just get the value.
692                          */
693                         code++;
694                         if (code->op != FETCH_OP_NOP) {
695                                 trace_probe_log_err(offset, TOO_MANY_OPS);
696                                 goto fail;
697                         }
698                 }
699                 /* If op == DEREF, replace it with STRING */
700                 if (!strcmp(parg->type->name, "ustring") ||
701                     code->op == FETCH_OP_UDEREF)
702                         code->op = FETCH_OP_ST_USTRING;
703                 else if (!strcmp(parg->type->name, "symstr"))
704                         code->op = FETCH_OP_ST_SYMSTR;
705                 else
706                         code->op = FETCH_OP_ST_STRING;
707                 code->size = parg->type->size;
708                 parg->dynamic = true;
709         } else if (code->op == FETCH_OP_DEREF) {
710                 code->op = FETCH_OP_ST_MEM;
711                 code->size = parg->type->size;
712         } else if (code->op == FETCH_OP_UDEREF) {
713                 code->op = FETCH_OP_ST_UMEM;
714                 code->size = parg->type->size;
715         } else {
716                 code++;
717                 if (code->op != FETCH_OP_NOP) {
718                         trace_probe_log_err(offset, TOO_MANY_OPS);
719                         goto fail;
720                 }
721                 code->op = FETCH_OP_ST_RAW;
722                 code->size = parg->type->size;
723         }
724         scode = code;
725         /* Modify operation */
726         if (t != NULL) {
727                 ret = __parse_bitfield_probe_arg(t, parg->type, &code);
728                 if (ret) {
729                         trace_probe_log_err(offset + t - arg, BAD_BITFIELD);
730                         goto fail;
731                 }
732         }
733         ret = -EINVAL;
734         /* Loop(Array) operation */
735         if (parg->count) {
736                 if (scode->op != FETCH_OP_ST_MEM &&
737                     scode->op != FETCH_OP_ST_STRING &&
738                     scode->op != FETCH_OP_ST_USTRING) {
739                         trace_probe_log_err(offset + (t ? (t - arg) : 0),
740                                             BAD_STRING);
741                         goto fail;
742                 }
743                 code++;
744                 if (code->op != FETCH_OP_NOP) {
745                         trace_probe_log_err(offset, TOO_MANY_OPS);
746                         goto fail;
747                 }
748                 code->op = FETCH_OP_LP_ARRAY;
749                 code->param = parg->count;
750         }
751         code++;
752         code->op = FETCH_OP_END;
753
754         ret = 0;
755         /* Shrink down the code buffer */
756         parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
757         if (!parg->code)
758                 ret = -ENOMEM;
759         else
760                 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
761
762 fail:
763         if (ret) {
764                 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
765                         if (code->op == FETCH_NOP_SYMBOL ||
766                             code->op == FETCH_OP_DATA)
767                                 kfree(code->data);
768         }
769         kfree(tmp);
770 out:
771         kfree(arg);
772
773         return ret;
774 }
775
776 /* Return 1 if name is reserved or already used by another argument */
777 static int traceprobe_conflict_field_name(const char *name,
778                                           struct probe_arg *args, int narg)
779 {
780         int i;
781
782         for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
783                 if (strcmp(reserved_field_names[i], name) == 0)
784                         return 1;
785
786         for (i = 0; i < narg; i++)
787                 if (strcmp(args[i].name, name) == 0)
788                         return 1;
789
790         return 0;
791 }
792
793 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
794                                 unsigned int flags)
795 {
796         struct probe_arg *parg = &tp->args[i];
797         const char *body;
798
799         /* Increment count for freeing args in error case */
800         tp->nr_args++;
801
802         body = strchr(arg, '=');
803         if (body) {
804                 if (body - arg > MAX_ARG_NAME_LEN) {
805                         trace_probe_log_err(0, ARG_NAME_TOO_LONG);
806                         return -EINVAL;
807                 } else if (body == arg) {
808                         trace_probe_log_err(0, NO_ARG_NAME);
809                         return -EINVAL;
810                 }
811                 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
812                 body++;
813         } else {
814                 /* If argument name is omitted, set "argN" */
815                 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1);
816                 body = arg;
817         }
818         if (!parg->name)
819                 return -ENOMEM;
820
821         if (!is_good_name(parg->name)) {
822                 trace_probe_log_err(0, BAD_ARG_NAME);
823                 return -EINVAL;
824         }
825         if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
826                 trace_probe_log_err(0, USED_ARG_NAME);
827                 return -EINVAL;
828         }
829         /* Parse fetch argument */
830         return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags,
831                                                body - arg);
832 }
833
834 void traceprobe_free_probe_arg(struct probe_arg *arg)
835 {
836         struct fetch_insn *code = arg->code;
837
838         while (code && code->op != FETCH_OP_END) {
839                 if (code->op == FETCH_NOP_SYMBOL ||
840                     code->op == FETCH_OP_DATA)
841                         kfree(code->data);
842                 code++;
843         }
844         kfree(arg->code);
845         kfree(arg->name);
846         kfree(arg->comm);
847         kfree(arg->fmt);
848 }
849
850 int traceprobe_update_arg(struct probe_arg *arg)
851 {
852         struct fetch_insn *code = arg->code;
853         long offset;
854         char *tmp;
855         char c;
856         int ret = 0;
857
858         while (code && code->op != FETCH_OP_END) {
859                 if (code->op == FETCH_NOP_SYMBOL) {
860                         if (code[1].op != FETCH_OP_IMM)
861                                 return -EINVAL;
862
863                         tmp = strpbrk(code->data, "+-");
864                         if (tmp)
865                                 c = *tmp;
866                         ret = traceprobe_split_symbol_offset(code->data,
867                                                              &offset);
868                         if (ret)
869                                 return ret;
870
871                         code[1].immediate =
872                                 (unsigned long)kallsyms_lookup_name(code->data);
873                         if (tmp)
874                                 *tmp = c;
875                         if (!code[1].immediate)
876                                 return -ENOENT;
877                         code[1].immediate += offset;
878                 }
879                 code++;
880         }
881         return 0;
882 }
883
884 /* When len=0, we just calculate the needed length */
885 #define LEN_OR_ZERO (len ? len - pos : 0)
886 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
887                            enum probe_print_type ptype)
888 {
889         struct probe_arg *parg;
890         int i, j;
891         int pos = 0;
892         const char *fmt, *arg;
893
894         switch (ptype) {
895         case PROBE_PRINT_NORMAL:
896                 fmt = "(%lx)";
897                 arg = "REC->" FIELD_STRING_IP;
898                 break;
899         case PROBE_PRINT_RETURN:
900                 fmt = "(%lx <- %lx)";
901                 arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
902                 break;
903         case PROBE_PRINT_EVENT:
904                 fmt = "(%u)";
905                 arg = "REC->" FIELD_STRING_TYPE;
906                 break;
907         default:
908                 WARN_ON_ONCE(1);
909                 return 0;
910         }
911
912         pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
913
914         for (i = 0; i < tp->nr_args; i++) {
915                 parg = tp->args + i;
916                 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
917                 if (parg->count) {
918                         pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
919                                         parg->type->fmt);
920                         for (j = 1; j < parg->count; j++)
921                                 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
922                                                 parg->type->fmt);
923                         pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
924                 } else
925                         pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
926                                         parg->type->fmt);
927         }
928
929         pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
930
931         for (i = 0; i < tp->nr_args; i++) {
932                 parg = tp->args + i;
933                 if (parg->count) {
934                         if (parg->type->is_string)
935                                 fmt = ", __get_str(%s[%d])";
936                         else
937                                 fmt = ", REC->%s[%d]";
938                         for (j = 0; j < parg->count; j++)
939                                 pos += snprintf(buf + pos, LEN_OR_ZERO,
940                                                 fmt, parg->name, j);
941                 } else {
942                         if (parg->type->is_string)
943                                 fmt = ", __get_str(%s)";
944                         else
945                                 fmt = ", REC->%s";
946                         pos += snprintf(buf + pos, LEN_OR_ZERO,
947                                         fmt, parg->name);
948                 }
949         }
950
951         /* return the length of print_fmt */
952         return pos;
953 }
954 #undef LEN_OR_ZERO
955
956 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
957 {
958         struct trace_event_call *call = trace_probe_event_call(tp);
959         int len;
960         char *print_fmt;
961
962         /* First: called with 0 length to calculate the needed length */
963         len = __set_print_fmt(tp, NULL, 0, ptype);
964         print_fmt = kmalloc(len + 1, GFP_KERNEL);
965         if (!print_fmt)
966                 return -ENOMEM;
967
968         /* Second: actually write the @print_fmt */
969         __set_print_fmt(tp, print_fmt, len + 1, ptype);
970         call->print_fmt = print_fmt;
971
972         return 0;
973 }
974
975 int traceprobe_define_arg_fields(struct trace_event_call *event_call,
976                                  size_t offset, struct trace_probe *tp)
977 {
978         int ret, i;
979
980         /* Set argument names as fields */
981         for (i = 0; i < tp->nr_args; i++) {
982                 struct probe_arg *parg = &tp->args[i];
983                 const char *fmt = parg->type->fmttype;
984                 int size = parg->type->size;
985
986                 if (parg->fmt)
987                         fmt = parg->fmt;
988                 if (parg->count)
989                         size *= parg->count;
990                 ret = trace_define_field(event_call, fmt, parg->name,
991                                          offset + parg->offset, size,
992                                          parg->type->is_signed,
993                                          FILTER_OTHER);
994                 if (ret)
995                         return ret;
996         }
997         return 0;
998 }
999
1000 static void trace_probe_event_free(struct trace_probe_event *tpe)
1001 {
1002         kfree(tpe->class.system);
1003         kfree(tpe->call.name);
1004         kfree(tpe->call.print_fmt);
1005         kfree(tpe);
1006 }
1007
1008 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
1009 {
1010         if (trace_probe_has_sibling(tp))
1011                 return -EBUSY;
1012
1013         list_del_init(&tp->list);
1014         trace_probe_event_free(tp->event);
1015
1016         tp->event = to->event;
1017         list_add_tail(&tp->list, trace_probe_probe_list(to));
1018
1019         return 0;
1020 }
1021
1022 void trace_probe_unlink(struct trace_probe *tp)
1023 {
1024         list_del_init(&tp->list);
1025         if (list_empty(trace_probe_probe_list(tp)))
1026                 trace_probe_event_free(tp->event);
1027         tp->event = NULL;
1028 }
1029
1030 void trace_probe_cleanup(struct trace_probe *tp)
1031 {
1032         int i;
1033
1034         for (i = 0; i < tp->nr_args; i++)
1035                 traceprobe_free_probe_arg(&tp->args[i]);
1036
1037         if (tp->event)
1038                 trace_probe_unlink(tp);
1039 }
1040
1041 int trace_probe_init(struct trace_probe *tp, const char *event,
1042                      const char *group, bool alloc_filter)
1043 {
1044         struct trace_event_call *call;
1045         size_t size = sizeof(struct trace_probe_event);
1046         int ret = 0;
1047
1048         if (!event || !group)
1049                 return -EINVAL;
1050
1051         if (alloc_filter)
1052                 size += sizeof(struct trace_uprobe_filter);
1053
1054         tp->event = kzalloc(size, GFP_KERNEL);
1055         if (!tp->event)
1056                 return -ENOMEM;
1057
1058         INIT_LIST_HEAD(&tp->event->files);
1059         INIT_LIST_HEAD(&tp->event->class.fields);
1060         INIT_LIST_HEAD(&tp->event->probes);
1061         INIT_LIST_HEAD(&tp->list);
1062         list_add(&tp->list, &tp->event->probes);
1063
1064         call = trace_probe_event_call(tp);
1065         call->class = &tp->event->class;
1066         call->name = kstrdup(event, GFP_KERNEL);
1067         if (!call->name) {
1068                 ret = -ENOMEM;
1069                 goto error;
1070         }
1071
1072         tp->event->class.system = kstrdup(group, GFP_KERNEL);
1073         if (!tp->event->class.system) {
1074                 ret = -ENOMEM;
1075                 goto error;
1076         }
1077
1078         return 0;
1079
1080 error:
1081         trace_probe_cleanup(tp);
1082         return ret;
1083 }
1084
1085 static struct trace_event_call *
1086 find_trace_event_call(const char *system, const char *event_name)
1087 {
1088         struct trace_event_call *tp_event;
1089         const char *name;
1090
1091         list_for_each_entry(tp_event, &ftrace_events, list) {
1092                 if (!tp_event->class->system ||
1093                     strcmp(system, tp_event->class->system))
1094                         continue;
1095                 name = trace_event_name(tp_event);
1096                 if (!name || strcmp(event_name, name))
1097                         continue;
1098                 return tp_event;
1099         }
1100
1101         return NULL;
1102 }
1103
1104 int trace_probe_register_event_call(struct trace_probe *tp)
1105 {
1106         struct trace_event_call *call = trace_probe_event_call(tp);
1107         int ret;
1108
1109         lockdep_assert_held(&event_mutex);
1110
1111         if (find_trace_event_call(trace_probe_group_name(tp),
1112                                   trace_probe_name(tp)))
1113                 return -EEXIST;
1114
1115         ret = register_trace_event(&call->event);
1116         if (!ret)
1117                 return -ENODEV;
1118
1119         ret = trace_add_event_call(call);
1120         if (ret)
1121                 unregister_trace_event(&call->event);
1122
1123         return ret;
1124 }
1125
1126 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1127 {
1128         struct event_file_link *link;
1129
1130         link = kmalloc(sizeof(*link), GFP_KERNEL);
1131         if (!link)
1132                 return -ENOMEM;
1133
1134         link->file = file;
1135         INIT_LIST_HEAD(&link->list);
1136         list_add_tail_rcu(&link->list, &tp->event->files);
1137         trace_probe_set_flag(tp, TP_FLAG_TRACE);
1138         return 0;
1139 }
1140
1141 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1142                                                   struct trace_event_file *file)
1143 {
1144         struct event_file_link *link;
1145
1146         trace_probe_for_each_link(link, tp) {
1147                 if (link->file == file)
1148                         return link;
1149         }
1150
1151         return NULL;
1152 }
1153
1154 int trace_probe_remove_file(struct trace_probe *tp,
1155                             struct trace_event_file *file)
1156 {
1157         struct event_file_link *link;
1158
1159         link = trace_probe_get_file_link(tp, file);
1160         if (!link)
1161                 return -ENOENT;
1162
1163         list_del_rcu(&link->list);
1164         synchronize_rcu();
1165         kfree(link);
1166
1167         if (list_empty(&tp->event->files))
1168                 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
1169
1170         return 0;
1171 }
1172
1173 /*
1174  * Return the smallest index of different type argument (start from 1).
1175  * If all argument types and name are same, return 0.
1176  */
1177 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1178 {
1179         int i;
1180
1181         /* In case of more arguments */
1182         if (a->nr_args < b->nr_args)
1183                 return a->nr_args + 1;
1184         if (a->nr_args > b->nr_args)
1185                 return b->nr_args + 1;
1186
1187         for (i = 0; i < a->nr_args; i++) {
1188                 if ((b->nr_args <= i) ||
1189                     ((a->args[i].type != b->args[i].type) ||
1190                      (a->args[i].count != b->args[i].count) ||
1191                      strcmp(a->args[i].name, b->args[i].name)))
1192                         return i + 1;
1193         }
1194
1195         return 0;
1196 }
1197
1198 bool trace_probe_match_command_args(struct trace_probe *tp,
1199                                     int argc, const char **argv)
1200 {
1201         char buf[MAX_ARGSTR_LEN + 1];
1202         int i;
1203
1204         if (tp->nr_args < argc)
1205                 return false;
1206
1207         for (i = 0; i < argc; i++) {
1208                 snprintf(buf, sizeof(buf), "%s=%s",
1209                          tp->args[i].name, tp->args[i].comm);
1210                 if (strcmp(buf, argv[i]))
1211                         return false;
1212         }
1213         return true;
1214 }
1215
1216 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
1217 {
1218         int argc = 0, ret = 0;
1219         char **argv;
1220
1221         argv = argv_split(GFP_KERNEL, raw_command, &argc);
1222         if (!argv)
1223                 return -ENOMEM;
1224
1225         if (argc)
1226                 ret = createfn(argc, (const char **)argv);
1227
1228         argv_free(argv);
1229
1230         return ret;
1231 }