Linux Audio

Check our new training course

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