xref: /openbmc/linux/kernel/trace/trace_probe.c (revision e5c86679)
1 /*
2  * Common code for probe-based Dynamic events.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  * This code was copied from kernel/trace/trace_kprobe.c written by
18  * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
19  *
20  * Updates to make this generic:
21  * Copyright (C) IBM Corporation, 2010-2011
22  * Author:     Srikar Dronamraju
23  */
24 #define pr_fmt(fmt)	"trace_probe: " fmt
25 
26 #include "trace_probe.h"
27 
28 const char *reserved_field_names[] = {
29 	"common_type",
30 	"common_flags",
31 	"common_preempt_count",
32 	"common_pid",
33 	"common_tgid",
34 	FIELD_STRING_IP,
35 	FIELD_STRING_RETIP,
36 	FIELD_STRING_FUNC,
37 };
38 
39 /* Printing  in basic type function template */
40 #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt)			\
41 int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, const char *name,	\
42 				void *data, void *ent)			\
43 {									\
44 	trace_seq_printf(s, " %s=" fmt, name, *(type *)data);		\
45 	return !trace_seq_has_overflowed(s);				\
46 }									\
47 const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;				\
48 NOKPROBE_SYMBOL(PRINT_TYPE_FUNC_NAME(tname));
49 
50 DEFINE_BASIC_PRINT_TYPE_FUNC(u8,  u8,  "%u")
51 DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
52 DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
53 DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
54 DEFINE_BASIC_PRINT_TYPE_FUNC(s8,  s8,  "%d")
55 DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
56 DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
57 DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
58 DEFINE_BASIC_PRINT_TYPE_FUNC(x8,  u8,  "0x%x")
59 DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
60 DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
61 DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
62 
63 /* Print type function for string type */
64 int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, const char *name,
65 				 void *data, void *ent)
66 {
67 	int len = *(u32 *)data >> 16;
68 
69 	if (!len)
70 		trace_seq_printf(s, " %s=(fault)", name);
71 	else
72 		trace_seq_printf(s, " %s=\"%s\"", name,
73 				 (const char *)get_loc_data(data, ent));
74 	return !trace_seq_has_overflowed(s);
75 }
76 NOKPROBE_SYMBOL(PRINT_TYPE_FUNC_NAME(string));
77 
78 const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
79 
80 #define CHECK_FETCH_FUNCS(method, fn)			\
81 	(((FETCH_FUNC_NAME(method, u8) == fn) ||	\
82 	  (FETCH_FUNC_NAME(method, u16) == fn) ||	\
83 	  (FETCH_FUNC_NAME(method, u32) == fn) ||	\
84 	  (FETCH_FUNC_NAME(method, u64) == fn) ||	\
85 	  (FETCH_FUNC_NAME(method, string) == fn) ||	\
86 	  (FETCH_FUNC_NAME(method, string_size) == fn)) \
87 	 && (fn != NULL))
88 
89 /* Data fetch function templates */
90 #define DEFINE_FETCH_reg(type)						\
91 void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, void *offset, void *dest)	\
92 {									\
93 	*(type *)dest = (type)regs_get_register(regs,			\
94 				(unsigned int)((unsigned long)offset));	\
95 }									\
96 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(reg, type));
97 DEFINE_BASIC_FETCH_FUNCS(reg)
98 /* No string on the register */
99 #define fetch_reg_string	NULL
100 #define fetch_reg_string_size	NULL
101 
102 #define DEFINE_FETCH_retval(type)					\
103 void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs,		\
104 				   void *dummy, void *dest)		\
105 {									\
106 	*(type *)dest = (type)regs_return_value(regs);			\
107 }									\
108 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(retval, type));
109 DEFINE_BASIC_FETCH_FUNCS(retval)
110 /* No string on the retval */
111 #define fetch_retval_string		NULL
112 #define fetch_retval_string_size	NULL
113 
114 /* Dereference memory access function */
115 struct deref_fetch_param {
116 	struct fetch_param	orig;
117 	long			offset;
118 	fetch_func_t		fetch;
119 	fetch_func_t		fetch_size;
120 };
121 
122 #define DEFINE_FETCH_deref(type)					\
123 void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs,			\
124 				  void *data, void *dest)		\
125 {									\
126 	struct deref_fetch_param *dprm = data;				\
127 	unsigned long addr;						\
128 	call_fetch(&dprm->orig, regs, &addr);				\
129 	if (addr) {							\
130 		addr += dprm->offset;					\
131 		dprm->fetch(regs, (void *)addr, dest);			\
132 	} else								\
133 		*(type *)dest = 0;					\
134 }									\
135 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(deref, type));
136 DEFINE_BASIC_FETCH_FUNCS(deref)
137 DEFINE_FETCH_deref(string)
138 
139 void FETCH_FUNC_NAME(deref, string_size)(struct pt_regs *regs,
140 					 void *data, void *dest)
141 {
142 	struct deref_fetch_param *dprm = data;
143 	unsigned long addr;
144 
145 	call_fetch(&dprm->orig, regs, &addr);
146 	if (addr && dprm->fetch_size) {
147 		addr += dprm->offset;
148 		dprm->fetch_size(regs, (void *)addr, dest);
149 	} else
150 		*(string_size *)dest = 0;
151 }
152 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(deref, string_size));
153 
154 static void update_deref_fetch_param(struct deref_fetch_param *data)
155 {
156 	if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
157 		update_deref_fetch_param(data->orig.data);
158 	else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
159 		update_symbol_cache(data->orig.data);
160 }
161 NOKPROBE_SYMBOL(update_deref_fetch_param);
162 
163 static void free_deref_fetch_param(struct deref_fetch_param *data)
164 {
165 	if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
166 		free_deref_fetch_param(data->orig.data);
167 	else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
168 		free_symbol_cache(data->orig.data);
169 	kfree(data);
170 }
171 NOKPROBE_SYMBOL(free_deref_fetch_param);
172 
173 /* Bitfield fetch function */
174 struct bitfield_fetch_param {
175 	struct fetch_param	orig;
176 	unsigned char		hi_shift;
177 	unsigned char		low_shift;
178 };
179 
180 #define DEFINE_FETCH_bitfield(type)					\
181 void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs,		\
182 				     void *data, void *dest)		\
183 {									\
184 	struct bitfield_fetch_param *bprm = data;			\
185 	type buf = 0;							\
186 	call_fetch(&bprm->orig, regs, &buf);				\
187 	if (buf) {							\
188 		buf <<= bprm->hi_shift;					\
189 		buf >>= bprm->low_shift;				\
190 	}								\
191 	*(type *)dest = buf;						\
192 }									\
193 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(bitfield, type));
194 DEFINE_BASIC_FETCH_FUNCS(bitfield)
195 #define fetch_bitfield_string		NULL
196 #define fetch_bitfield_string_size	NULL
197 
198 static void
199 update_bitfield_fetch_param(struct bitfield_fetch_param *data)
200 {
201 	/*
202 	 * Don't check the bitfield itself, because this must be the
203 	 * last fetch function.
204 	 */
205 	if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
206 		update_deref_fetch_param(data->orig.data);
207 	else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
208 		update_symbol_cache(data->orig.data);
209 }
210 
211 static void
212 free_bitfield_fetch_param(struct bitfield_fetch_param *data)
213 {
214 	/*
215 	 * Don't check the bitfield itself, because this must be the
216 	 * last fetch function.
217 	 */
218 	if (CHECK_FETCH_FUNCS(deref, data->orig.fn))
219 		free_deref_fetch_param(data->orig.data);
220 	else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn))
221 		free_symbol_cache(data->orig.data);
222 
223 	kfree(data);
224 }
225 
226 void FETCH_FUNC_NAME(comm, string)(struct pt_regs *regs,
227 					  void *data, void *dest)
228 {
229 	int maxlen = get_rloc_len(*(u32 *)dest);
230 	u8 *dst = get_rloc_data(dest);
231 	long ret;
232 
233 	if (!maxlen)
234 		return;
235 
236 	ret = strlcpy(dst, current->comm, maxlen);
237 	*(u32 *)dest = make_data_rloc(ret, get_rloc_offs(*(u32 *)dest));
238 }
239 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(comm, string));
240 
241 void FETCH_FUNC_NAME(comm, string_size)(struct pt_regs *regs,
242 					       void *data, void *dest)
243 {
244 	*(u32 *)dest = strlen(current->comm) + 1;
245 }
246 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(comm, string_size));
247 
248 static const struct fetch_type *find_fetch_type(const char *type,
249 						const struct fetch_type *ftbl)
250 {
251 	int i;
252 
253 	if (!type)
254 		type = DEFAULT_FETCH_TYPE_STR;
255 
256 	/* Special case: bitfield */
257 	if (*type == 'b') {
258 		unsigned long bs;
259 
260 		type = strchr(type, '/');
261 		if (!type)
262 			goto fail;
263 
264 		type++;
265 		if (kstrtoul(type, 0, &bs))
266 			goto fail;
267 
268 		switch (bs) {
269 		case 8:
270 			return find_fetch_type("u8", ftbl);
271 		case 16:
272 			return find_fetch_type("u16", ftbl);
273 		case 32:
274 			return find_fetch_type("u32", ftbl);
275 		case 64:
276 			return find_fetch_type("u64", ftbl);
277 		default:
278 			goto fail;
279 		}
280 	}
281 
282 	for (i = 0; ftbl[i].name; i++) {
283 		if (strcmp(type, ftbl[i].name) == 0)
284 			return &ftbl[i];
285 	}
286 
287 fail:
288 	return NULL;
289 }
290 
291 /* Special function : only accept unsigned long */
292 static void fetch_kernel_stack_address(struct pt_regs *regs, void *dummy, void *dest)
293 {
294 	*(unsigned long *)dest = kernel_stack_pointer(regs);
295 }
296 NOKPROBE_SYMBOL(fetch_kernel_stack_address);
297 
298 static void fetch_user_stack_address(struct pt_regs *regs, void *dummy, void *dest)
299 {
300 	*(unsigned long *)dest = user_stack_pointer(regs);
301 }
302 NOKPROBE_SYMBOL(fetch_user_stack_address);
303 
304 static fetch_func_t get_fetch_size_function(const struct fetch_type *type,
305 					    fetch_func_t orig_fn,
306 					    const struct fetch_type *ftbl)
307 {
308 	int i;
309 
310 	if (type != &ftbl[FETCH_TYPE_STRING])
311 		return NULL;	/* Only string type needs size function */
312 
313 	for (i = 0; i < FETCH_MTD_END; i++)
314 		if (type->fetch[i] == orig_fn)
315 			return ftbl[FETCH_TYPE_STRSIZE].fetch[i];
316 
317 	WARN_ON(1);	/* This should not happen */
318 
319 	return NULL;
320 }
321 
322 /* Split symbol and offset. */
323 int traceprobe_split_symbol_offset(char *symbol, unsigned long *offset)
324 {
325 	char *tmp;
326 	int ret;
327 
328 	if (!offset)
329 		return -EINVAL;
330 
331 	tmp = strchr(symbol, '+');
332 	if (tmp) {
333 		/* skip sign because kstrtoul doesn't accept '+' */
334 		ret = kstrtoul(tmp + 1, 0, offset);
335 		if (ret)
336 			return ret;
337 
338 		*tmp = '\0';
339 	} else
340 		*offset = 0;
341 
342 	return 0;
343 }
344 
345 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
346 
347 static int parse_probe_vars(char *arg, const struct fetch_type *t,
348 			    struct fetch_param *f, bool is_return,
349 			    bool is_kprobe)
350 {
351 	int ret = 0;
352 	unsigned long param;
353 
354 	if (strcmp(arg, "retval") == 0) {
355 		if (is_return)
356 			f->fn = t->fetch[FETCH_MTD_retval];
357 		else
358 			ret = -EINVAL;
359 	} else if (strncmp(arg, "stack", 5) == 0) {
360 		if (arg[5] == '\0') {
361 			if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR))
362 				return -EINVAL;
363 
364 			if (is_kprobe)
365 				f->fn = fetch_kernel_stack_address;
366 			else
367 				f->fn = fetch_user_stack_address;
368 		} else if (isdigit(arg[5])) {
369 			ret = kstrtoul(arg + 5, 10, &param);
370 			if (ret || (is_kprobe && param > PARAM_MAX_STACK))
371 				ret = -EINVAL;
372 			else {
373 				f->fn = t->fetch[FETCH_MTD_stack];
374 				f->data = (void *)param;
375 			}
376 		} else
377 			ret = -EINVAL;
378 	} else if (strcmp(arg, "comm") == 0) {
379 		if (strcmp(t->name, "string") != 0 &&
380 		    strcmp(t->name, "string_size") != 0)
381 			return -EINVAL;
382 		f->fn = t->fetch[FETCH_MTD_comm];
383 	} else
384 		ret = -EINVAL;
385 
386 	return ret;
387 }
388 
389 /* Recursive argument parser */
390 static int parse_probe_arg(char *arg, const struct fetch_type *t,
391 		     struct fetch_param *f, bool is_return, bool is_kprobe,
392 		     const struct fetch_type *ftbl)
393 {
394 	unsigned long param;
395 	long offset;
396 	char *tmp;
397 	int ret = 0;
398 
399 	switch (arg[0]) {
400 	case '$':
401 		ret = parse_probe_vars(arg + 1, t, f, is_return, is_kprobe);
402 		break;
403 
404 	case '%':	/* named register */
405 		ret = regs_query_register_offset(arg + 1);
406 		if (ret >= 0) {
407 			f->fn = t->fetch[FETCH_MTD_reg];
408 			f->data = (void *)(unsigned long)ret;
409 			ret = 0;
410 		}
411 		break;
412 
413 	case '@':	/* memory, file-offset or symbol */
414 		if (isdigit(arg[1])) {
415 			ret = kstrtoul(arg + 1, 0, &param);
416 			if (ret)
417 				break;
418 
419 			f->fn = t->fetch[FETCH_MTD_memory];
420 			f->data = (void *)param;
421 		} else if (arg[1] == '+') {
422 			/* kprobes don't support file offsets */
423 			if (is_kprobe)
424 				return -EINVAL;
425 
426 			ret = kstrtol(arg + 2, 0, &offset);
427 			if (ret)
428 				break;
429 
430 			f->fn = t->fetch[FETCH_MTD_file_offset];
431 			f->data = (void *)offset;
432 		} else {
433 			/* uprobes don't support symbols */
434 			if (!is_kprobe)
435 				return -EINVAL;
436 
437 			ret = traceprobe_split_symbol_offset(arg + 1, &offset);
438 			if (ret)
439 				break;
440 
441 			f->data = alloc_symbol_cache(arg + 1, offset);
442 			if (f->data)
443 				f->fn = t->fetch[FETCH_MTD_symbol];
444 		}
445 		break;
446 
447 	case '+':	/* deref memory */
448 		arg++;	/* Skip '+', because kstrtol() rejects it. */
449 	case '-':
450 		tmp = strchr(arg, '(');
451 		if (!tmp)
452 			break;
453 
454 		*tmp = '\0';
455 		ret = kstrtol(arg, 0, &offset);
456 
457 		if (ret)
458 			break;
459 
460 		arg = tmp + 1;
461 		tmp = strrchr(arg, ')');
462 
463 		if (tmp) {
464 			struct deref_fetch_param	*dprm;
465 			const struct fetch_type		*t2;
466 
467 			t2 = find_fetch_type(NULL, ftbl);
468 			*tmp = '\0';
469 			dprm = kzalloc(sizeof(struct deref_fetch_param), GFP_KERNEL);
470 
471 			if (!dprm)
472 				return -ENOMEM;
473 
474 			dprm->offset = offset;
475 			dprm->fetch = t->fetch[FETCH_MTD_memory];
476 			dprm->fetch_size = get_fetch_size_function(t,
477 							dprm->fetch, ftbl);
478 			ret = parse_probe_arg(arg, t2, &dprm->orig, is_return,
479 							is_kprobe, ftbl);
480 			if (ret)
481 				kfree(dprm);
482 			else {
483 				f->fn = t->fetch[FETCH_MTD_deref];
484 				f->data = (void *)dprm;
485 			}
486 		}
487 		break;
488 	}
489 	if (!ret && !f->fn) {	/* Parsed, but do not find fetch method */
490 		pr_info("%s type has no corresponding fetch method.\n", t->name);
491 		ret = -EINVAL;
492 	}
493 
494 	return ret;
495 }
496 
497 #define BYTES_TO_BITS(nb)	((BITS_PER_LONG * (nb)) / sizeof(long))
498 
499 /* Bitfield type needs to be parsed into a fetch function */
500 static int __parse_bitfield_probe_arg(const char *bf,
501 				      const struct fetch_type *t,
502 				      struct fetch_param *f)
503 {
504 	struct bitfield_fetch_param *bprm;
505 	unsigned long bw, bo;
506 	char *tail;
507 
508 	if (*bf != 'b')
509 		return 0;
510 
511 	bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
512 	if (!bprm)
513 		return -ENOMEM;
514 
515 	bprm->orig = *f;
516 	f->fn = t->fetch[FETCH_MTD_bitfield];
517 	f->data = (void *)bprm;
518 	bw = simple_strtoul(bf + 1, &tail, 0);	/* Use simple one */
519 
520 	if (bw == 0 || *tail != '@')
521 		return -EINVAL;
522 
523 	bf = tail + 1;
524 	bo = simple_strtoul(bf, &tail, 0);
525 
526 	if (tail == bf || *tail != '/')
527 		return -EINVAL;
528 
529 	bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo);
530 	bprm->low_shift = bprm->hi_shift + bo;
531 
532 	return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
533 }
534 
535 /* String length checking wrapper */
536 int traceprobe_parse_probe_arg(char *arg, ssize_t *size,
537 		struct probe_arg *parg, bool is_return, bool is_kprobe,
538 		const struct fetch_type *ftbl)
539 {
540 	const char *t;
541 	int ret;
542 
543 	if (strlen(arg) > MAX_ARGSTR_LEN) {
544 		pr_info("Argument is too long.: %s\n",  arg);
545 		return -ENOSPC;
546 	}
547 	parg->comm = kstrdup(arg, GFP_KERNEL);
548 	if (!parg->comm) {
549 		pr_info("Failed to allocate memory for command '%s'.\n", arg);
550 		return -ENOMEM;
551 	}
552 	t = strchr(parg->comm, ':');
553 	if (t) {
554 		arg[t - parg->comm] = '\0';
555 		t++;
556 	}
557 	/*
558 	 * The default type of $comm should be "string", and it can't be
559 	 * dereferenced.
560 	 */
561 	if (!t && strcmp(arg, "$comm") == 0)
562 		t = "string";
563 	parg->type = find_fetch_type(t, ftbl);
564 	if (!parg->type) {
565 		pr_info("Unsupported type: %s\n", t);
566 		return -EINVAL;
567 	}
568 	parg->offset = *size;
569 	*size += parg->type->size;
570 	ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return,
571 			      is_kprobe, ftbl);
572 
573 	if (ret >= 0 && t != NULL)
574 		ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch);
575 
576 	if (ret >= 0) {
577 		parg->fetch_size.fn = get_fetch_size_function(parg->type,
578 							      parg->fetch.fn,
579 							      ftbl);
580 		parg->fetch_size.data = parg->fetch.data;
581 	}
582 
583 	return ret;
584 }
585 
586 /* Return 1 if name is reserved or already used by another argument */
587 int traceprobe_conflict_field_name(const char *name,
588 			       struct probe_arg *args, int narg)
589 {
590 	int i;
591 
592 	for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
593 		if (strcmp(reserved_field_names[i], name) == 0)
594 			return 1;
595 
596 	for (i = 0; i < narg; i++)
597 		if (strcmp(args[i].name, name) == 0)
598 			return 1;
599 
600 	return 0;
601 }
602 
603 void traceprobe_update_arg(struct probe_arg *arg)
604 {
605 	if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
606 		update_bitfield_fetch_param(arg->fetch.data);
607 	else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
608 		update_deref_fetch_param(arg->fetch.data);
609 	else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
610 		update_symbol_cache(arg->fetch.data);
611 }
612 
613 void traceprobe_free_probe_arg(struct probe_arg *arg)
614 {
615 	if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn))
616 		free_bitfield_fetch_param(arg->fetch.data);
617 	else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn))
618 		free_deref_fetch_param(arg->fetch.data);
619 	else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn))
620 		free_symbol_cache(arg->fetch.data);
621 
622 	kfree(arg->name);
623 	kfree(arg->comm);
624 }
625 
626 int traceprobe_command(const char *buf, int (*createfn)(int, char **))
627 {
628 	char **argv;
629 	int argc, ret;
630 
631 	argc = 0;
632 	ret = 0;
633 	argv = argv_split(GFP_KERNEL, buf, &argc);
634 	if (!argv)
635 		return -ENOMEM;
636 
637 	if (argc)
638 		ret = createfn(argc, argv);
639 
640 	argv_free(argv);
641 
642 	return ret;
643 }
644 
645 #define WRITE_BUFSIZE  4096
646 
647 ssize_t traceprobe_probes_write(struct file *file, const char __user *buffer,
648 				size_t count, loff_t *ppos,
649 				int (*createfn)(int, char **))
650 {
651 	char *kbuf, *buf, *tmp;
652 	int ret = 0;
653 	size_t done = 0;
654 	size_t size;
655 
656 	kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
657 	if (!kbuf)
658 		return -ENOMEM;
659 
660 	while (done < count) {
661 		size = count - done;
662 
663 		if (size >= WRITE_BUFSIZE)
664 			size = WRITE_BUFSIZE - 1;
665 
666 		if (copy_from_user(kbuf, buffer + done, size)) {
667 			ret = -EFAULT;
668 			goto out;
669 		}
670 		kbuf[size] = '\0';
671 		buf = kbuf;
672 		do {
673 			tmp = strchr(buf, '\n');
674 			if (tmp) {
675 				*tmp = '\0';
676 				size = tmp - buf + 1;
677 			} else {
678 				size = strlen(buf);
679 				if (done + size < count) {
680 					if (buf != kbuf)
681 						break;
682 					/* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */
683 					pr_warn("Line length is too long: Should be less than %d\n",
684 						WRITE_BUFSIZE - 2);
685 					ret = -EINVAL;
686 					goto out;
687 				}
688 			}
689 			done += size;
690 
691 			/* Remove comments */
692 			tmp = strchr(buf, '#');
693 
694 			if (tmp)
695 				*tmp = '\0';
696 
697 			ret = traceprobe_command(buf, createfn);
698 			if (ret)
699 				goto out;
700 			buf += size;
701 
702 		} while (done < count);
703 	}
704 	ret = done;
705 
706 out:
707 	kfree(kbuf);
708 
709 	return ret;
710 }
711 
712 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
713 			   bool is_return)
714 {
715 	int i;
716 	int pos = 0;
717 
718 	const char *fmt, *arg;
719 
720 	if (!is_return) {
721 		fmt = "(%lx)";
722 		arg = "REC->" FIELD_STRING_IP;
723 	} else {
724 		fmt = "(%lx <- %lx)";
725 		arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
726 	}
727 
728 	/* When len=0, we just calculate the needed length */
729 #define LEN_OR_ZERO (len ? len - pos : 0)
730 
731 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
732 
733 	for (i = 0; i < tp->nr_args; i++) {
734 		pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
735 				tp->args[i].name, tp->args[i].type->fmt);
736 	}
737 
738 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
739 
740 	for (i = 0; i < tp->nr_args; i++) {
741 		if (strcmp(tp->args[i].type->name, "string") == 0)
742 			pos += snprintf(buf + pos, LEN_OR_ZERO,
743 					", __get_str(%s)",
744 					tp->args[i].name);
745 		else
746 			pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
747 					tp->args[i].name);
748 	}
749 
750 #undef LEN_OR_ZERO
751 
752 	/* return the length of print_fmt */
753 	return pos;
754 }
755 
756 int set_print_fmt(struct trace_probe *tp, bool is_return)
757 {
758 	int len;
759 	char *print_fmt;
760 
761 	/* First: called with 0 length to calculate the needed length */
762 	len = __set_print_fmt(tp, NULL, 0, is_return);
763 	print_fmt = kmalloc(len + 1, GFP_KERNEL);
764 	if (!print_fmt)
765 		return -ENOMEM;
766 
767 	/* Second: actually write the @print_fmt */
768 	__set_print_fmt(tp, print_fmt, len + 1, is_return);
769 	tp->call.print_fmt = print_fmt;
770 
771 	return 0;
772 }
773