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