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