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