Linux Audio

Check our new training course

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