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}
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Common code for probe-based Dynamic events.
   4 *
 
 
 
 
 
 
 
 
 
 
 
 
 
   5 * This code was copied from kernel/trace/trace_kprobe.c written by
   6 * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
   7 *
   8 * Updates to make this generic:
   9 * Copyright (C) IBM Corporation, 2010-2011
  10 * Author:     Srikar Dronamraju
  11 */
  12#define pr_fmt(fmt)	"trace_probe: " fmt
  13
  14#include <linux/bpf.h>
  15#include "trace_btf.h"
  16
  17#include "trace_probe.h"
  18
  19#undef C
  20#define C(a, b)		b
  21
  22static const char *trace_probe_err_text[] = { ERRORS };
  23
  24static const char *reserved_field_names[] = {
  25	"common_type",
  26	"common_flags",
  27	"common_preempt_count",
  28	"common_pid",
  29	"common_tgid",
  30	FIELD_STRING_IP,
  31	FIELD_STRING_RETIP,
  32	FIELD_STRING_FUNC,
  33};
  34
  35/* Printing  in basic type function template */
  36#define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt)			\
  37int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\
 
  38{									\
  39	trace_seq_printf(s, fmt, *(type *)data);			\
  40	return !trace_seq_has_overflowed(s);				\
  41}									\
  42const char PRINT_TYPE_FMT_NAME(tname)[] = fmt;
  43
  44DEFINE_BASIC_PRINT_TYPE_FUNC(u8,  u8,  "%u")
  45DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u")
  46DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u")
  47DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu")
  48DEFINE_BASIC_PRINT_TYPE_FUNC(s8,  s8,  "%d")
  49DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d")
  50DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d")
  51DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld")
  52DEFINE_BASIC_PRINT_TYPE_FUNC(x8,  u8,  "0x%x")
  53DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
  54DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
  55DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
  56DEFINE_BASIC_PRINT_TYPE_FUNC(char, u8, "'%c'")
  57
  58int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
  59{
  60	trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data);
  61	return !trace_seq_has_overflowed(s);
  62}
  63const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS";
 
 
  64
  65/* Print type function for string type */
  66int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent)
 
  67{
  68	int len = *(u32 *)data >> 16;
  69
  70	if (!len)
  71		trace_seq_puts(s, FAULT_STRING);
  72	else
  73		trace_seq_printf(s, "\"%s\"",
  74				 (const char *)get_loc_data(data, ent));
  75	return !trace_seq_has_overflowed(s);
  76}
 
  77
  78const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\"";
  79
  80/* Fetch type information table */
  81static const struct fetch_type probe_fetch_types[] = {
  82	/* Special types */
  83	__ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, 1,
  84			    "__data_loc char[]"),
  85	__ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, 1,
  86			    "__data_loc char[]"),
  87	__ASSIGN_FETCH_TYPE("symstr", string, string, sizeof(u32), 1, 1,
  88			    "__data_loc char[]"),
  89	/* Basic types */
  90	ASSIGN_FETCH_TYPE(u8,  u8,  0),
  91	ASSIGN_FETCH_TYPE(u16, u16, 0),
  92	ASSIGN_FETCH_TYPE(u32, u32, 0),
  93	ASSIGN_FETCH_TYPE(u64, u64, 0),
  94	ASSIGN_FETCH_TYPE(s8,  u8,  1),
  95	ASSIGN_FETCH_TYPE(s16, u16, 1),
  96	ASSIGN_FETCH_TYPE(s32, u32, 1),
  97	ASSIGN_FETCH_TYPE(s64, u64, 1),
  98	ASSIGN_FETCH_TYPE_ALIAS(x8,  u8,  u8,  0),
  99	ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
 100	ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
 101	ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
 102	ASSIGN_FETCH_TYPE_ALIAS(char, u8, u8,  0),
 103	ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 104
 105	ASSIGN_FETCH_TYPE_END
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 106};
 107
 108static const struct fetch_type *find_fetch_type(const char *type, unsigned long flags)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 109{
 110	int i;
 111
 112	/* Reject the symbol/symstr for uprobes */
 113	if (type && (flags & TPARG_FL_USER) &&
 114	    (!strcmp(type, "symbol") || !strcmp(type, "symstr")))
 115		return NULL;
 116
 117	if (!type)
 118		type = DEFAULT_FETCH_TYPE_STR;
 119
 120	/* Special case: bitfield */
 121	if (*type == 'b') {
 122		unsigned long bs;
 123
 124		type = strchr(type, '/');
 125		if (!type)
 126			goto fail;
 127
 128		type++;
 129		if (kstrtoul(type, 0, &bs))
 130			goto fail;
 131
 132		switch (bs) {
 133		case 8:
 134			return find_fetch_type("u8", flags);
 135		case 16:
 136			return find_fetch_type("u16", flags);
 137		case 32:
 138			return find_fetch_type("u32", flags);
 139		case 64:
 140			return find_fetch_type("u64", flags);
 141		default:
 142			goto fail;
 143		}
 144	}
 145
 146	for (i = 0; probe_fetch_types[i].name; i++) {
 147		if (strcmp(type, probe_fetch_types[i].name) == 0)
 148			return &probe_fetch_types[i];
 149	}
 150
 151fail:
 152	return NULL;
 153}
 154
 155static struct trace_probe_log trace_probe_log;
 156
 157void trace_probe_log_init(const char *subsystem, int argc, const char **argv)
 158{
 159	trace_probe_log.subsystem = subsystem;
 160	trace_probe_log.argc = argc;
 161	trace_probe_log.argv = argv;
 162	trace_probe_log.index = 0;
 163}
 
 164
 165void trace_probe_log_clear(void)
 166{
 167	memset(&trace_probe_log, 0, sizeof(trace_probe_log));
 168}
 
 169
 170void trace_probe_log_set_index(int index)
 
 
 171{
 172	trace_probe_log.index = index;
 173}
 174
 175void __trace_probe_log_err(int offset, int err_type)
 176{
 177	char *command, *p;
 178	int i, len = 0, pos = 0;
 179
 180	if (!trace_probe_log.argv)
 181		return;
 
 182
 183	/* Recalculate the length and allocate buffer */
 184	for (i = 0; i < trace_probe_log.argc; i++) {
 185		if (i == trace_probe_log.index)
 186			pos = len;
 187		len += strlen(trace_probe_log.argv[i]) + 1;
 188	}
 189	command = kzalloc(len, GFP_KERNEL);
 190	if (!command)
 191		return;
 192
 193	if (trace_probe_log.index >= trace_probe_log.argc) {
 194		/**
 195		 * Set the error position is next to the last arg + space.
 196		 * Note that len includes the terminal null and the cursor
 197		 * appears at pos + 1.
 198		 */
 199		pos = len;
 200		offset = 0;
 201	}
 202
 203	/* And make a command string from argv array */
 204	p = command;
 205	for (i = 0; i < trace_probe_log.argc; i++) {
 206		len = strlen(trace_probe_log.argv[i]);
 207		strcpy(p, trace_probe_log.argv[i]);
 208		p[len] = ' ';
 209		p += len + 1;
 210	}
 211	*(p - 1) = '\0';
 212
 213	tracing_log_err(NULL, trace_probe_log.subsystem, command,
 214			trace_probe_err_text, err_type, pos + offset);
 215
 216	kfree(command);
 217}
 218
 219/* Split symbol and offset. */
 220int traceprobe_split_symbol_offset(char *symbol, long *offset)
 221{
 222	char *tmp;
 223	int ret;
 224
 225	if (!offset)
 226		return -EINVAL;
 227
 228	tmp = strpbrk(symbol, "+-");
 229	if (tmp) {
 230		ret = kstrtol(tmp, 0, offset);
 
 231		if (ret)
 232			return ret;
 
 233		*tmp = '\0';
 234	} else
 235		*offset = 0;
 236
 237	return 0;
 238}
 239
 240/* @buf must has MAX_EVENT_NAME_LEN size */
 241int traceprobe_parse_event_name(const char **pevent, const char **pgroup,
 242				char *buf, int offset)
 243{
 244	const char *slash, *event = *pevent;
 245	int len;
 246
 247	slash = strchr(event, '/');
 248	if (!slash)
 249		slash = strchr(event, '.');
 250
 251	if (slash) {
 252		if (slash == event) {
 253			trace_probe_log_err(offset, NO_GROUP_NAME);
 254			return -EINVAL;
 255		}
 256		if (slash - event + 1 > MAX_EVENT_NAME_LEN) {
 257			trace_probe_log_err(offset, GROUP_TOO_LONG);
 258			return -EINVAL;
 259		}
 260		strscpy(buf, event, slash - event + 1);
 261		if (!is_good_system_name(buf)) {
 262			trace_probe_log_err(offset, BAD_GROUP_NAME);
 263			return -EINVAL;
 264		}
 265		*pgroup = buf;
 266		*pevent = slash + 1;
 267		offset += slash - event + 1;
 268		event = *pevent;
 269	}
 270	len = strlen(event);
 271	if (len == 0) {
 272		if (slash) {
 273			*pevent = NULL;
 274			return 0;
 275		}
 276		trace_probe_log_err(offset, NO_EVENT_NAME);
 277		return -EINVAL;
 278	} else if (len > MAX_EVENT_NAME_LEN) {
 279		trace_probe_log_err(offset, EVENT_TOO_LONG);
 280		return -EINVAL;
 281	}
 282	if (!is_good_name(event)) {
 283		trace_probe_log_err(offset, BAD_EVENT_NAME);
 284		return -EINVAL;
 285	}
 286	return 0;
 287}
 288
 289static int parse_trace_event_arg(char *arg, struct fetch_insn *code,
 290				 struct traceprobe_parse_context *ctx)
 291{
 292	struct ftrace_event_field *field;
 293	struct list_head *head;
 294
 295	head = trace_get_fields(ctx->event);
 296	list_for_each_entry(field, head, link) {
 297		if (!strcmp(arg, field->name)) {
 298			code->op = FETCH_OP_TP_ARG;
 299			code->data = field;
 300			return 0;
 301		}
 302	}
 303	return -ENOENT;
 304}
 305
 306#ifdef CONFIG_PROBE_EVENTS_BTF_ARGS
 307
 308static u32 btf_type_int(const struct btf_type *t)
 309{
 310	return *(u32 *)(t + 1);
 311}
 312
 313static bool btf_type_is_char_ptr(struct btf *btf, const struct btf_type *type)
 314{
 315	const struct btf_type *real_type;
 316	u32 intdata;
 317	s32 tid;
 318
 319	real_type = btf_type_skip_modifiers(btf, type->type, &tid);
 320	if (!real_type)
 321		return false;
 322
 323	if (BTF_INFO_KIND(real_type->info) != BTF_KIND_INT)
 324		return false;
 325
 326	intdata = btf_type_int(real_type);
 327	return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED)
 328		&& BTF_INT_BITS(intdata) == 8;
 329}
 330
 331static bool btf_type_is_char_array(struct btf *btf, const struct btf_type *type)
 332{
 333	const struct btf_type *real_type;
 334	const struct btf_array *array;
 335	u32 intdata;
 336	s32 tid;
 337
 338	if (BTF_INFO_KIND(type->info) != BTF_KIND_ARRAY)
 339		return false;
 340
 341	array = (const struct btf_array *)(type + 1);
 342
 343	real_type = btf_type_skip_modifiers(btf, array->type, &tid);
 344
 345	intdata = btf_type_int(real_type);
 346	return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED)
 347		&& BTF_INT_BITS(intdata) == 8;
 348}
 349
 350static int check_prepare_btf_string_fetch(char *typename,
 351				struct fetch_insn **pcode,
 352				struct traceprobe_parse_context *ctx)
 353{
 354	struct btf *btf = ctx->btf;
 355
 356	if (!btf || !ctx->last_type)
 357		return 0;
 358
 359	/* char [] does not need any change. */
 360	if (btf_type_is_char_array(btf, ctx->last_type))
 361		return 0;
 362
 363	/* char * requires dereference the pointer. */
 364	if (btf_type_is_char_ptr(btf, ctx->last_type)) {
 365		struct fetch_insn *code = *pcode + 1;
 366
 367		if (code->op == FETCH_OP_END) {
 368			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
 369			return -E2BIG;
 370		}
 371		if (typename[0] == 'u')
 372			code->op = FETCH_OP_UDEREF;
 373		else
 374			code->op = FETCH_OP_DEREF;
 375		code->offset = 0;
 376		*pcode = code;
 377		return 0;
 378	}
 379	/* Other types are not available for string */
 380	trace_probe_log_err(ctx->offset, BAD_TYPE4STR);
 381	return -EINVAL;
 382}
 383
 384static const char *fetch_type_from_btf_type(struct btf *btf,
 385					const struct btf_type *type,
 386					struct traceprobe_parse_context *ctx)
 387{
 388	u32 intdata;
 389
 390	/* TODO: const char * could be converted as a string */
 391	switch (BTF_INFO_KIND(type->info)) {
 392	case BTF_KIND_ENUM:
 393		/* enum is "int", so convert to "s32" */
 394		return "s32";
 395	case BTF_KIND_ENUM64:
 396		return "s64";
 397	case BTF_KIND_PTR:
 398		/* pointer will be converted to "x??" */
 399		if (IS_ENABLED(CONFIG_64BIT))
 400			return "x64";
 401		else
 402			return "x32";
 403	case BTF_KIND_INT:
 404		intdata = btf_type_int(type);
 405		if (BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) {
 406			switch (BTF_INT_BITS(intdata)) {
 407			case 8:
 408				return "s8";
 409			case 16:
 410				return "s16";
 411			case 32:
 412				return "s32";
 413			case 64:
 414				return "s64";
 415			}
 416		} else {	/* unsigned */
 417			switch (BTF_INT_BITS(intdata)) {
 418			case 8:
 419				return "u8";
 420			case 16:
 421				return "u16";
 422			case 32:
 423				return "u32";
 424			case 64:
 425				return "u64";
 426			}
 427			/* bitfield, size is encoded in the type */
 428			ctx->last_bitsize = BTF_INT_BITS(intdata);
 429			ctx->last_bitoffs += BTF_INT_OFFSET(intdata);
 430			return "u64";
 431		}
 432	}
 433	/* TODO: support other types */
 434
 435	return NULL;
 436}
 437
 438static int query_btf_context(struct traceprobe_parse_context *ctx)
 439{
 440	const struct btf_param *param;
 441	const struct btf_type *type;
 442	struct btf *btf;
 443	s32 nr;
 444
 445	if (ctx->btf)
 446		return 0;
 447
 448	if (!ctx->funcname)
 449		return -EINVAL;
 450
 451	type = btf_find_func_proto(ctx->funcname, &btf);
 452	if (!type)
 453		return -ENOENT;
 454
 455	ctx->btf = btf;
 456	ctx->proto = type;
 457
 458	/* ctx->params is optional, since func(void) will not have params. */
 459	nr = 0;
 460	param = btf_get_func_param(type, &nr);
 461	if (!IS_ERR_OR_NULL(param)) {
 462		/* Hide the first 'data' argument of tracepoint */
 463		if (ctx->flags & TPARG_FL_TPOINT) {
 464			nr--;
 465			param++;
 466		}
 467	}
 468
 469	if (nr > 0) {
 470		ctx->nr_params = nr;
 471		ctx->params = param;
 472	} else {
 473		ctx->nr_params = 0;
 474		ctx->params = NULL;
 475	}
 476
 477	return 0;
 478}
 479
 480static void clear_btf_context(struct traceprobe_parse_context *ctx)
 481{
 482	if (ctx->btf) {
 483		btf_put(ctx->btf);
 484		ctx->btf = NULL;
 485		ctx->proto = NULL;
 486		ctx->params = NULL;
 487		ctx->nr_params = 0;
 488	}
 489}
 490
 491/* Return 1 if the field separater is arrow operator ('->') */
 492static int split_next_field(char *varname, char **next_field,
 493			    struct traceprobe_parse_context *ctx)
 494{
 495	char *field;
 496	int ret = 0;
 497
 498	field = strpbrk(varname, ".-");
 499	if (field) {
 500		if (field[0] == '-' && field[1] == '>') {
 501			field[0] = '\0';
 502			field += 2;
 503			ret = 1;
 504		} else if (field[0] == '.') {
 505			field[0] = '\0';
 506			field += 1;
 507		} else {
 508			trace_probe_log_err(ctx->offset + field - varname, BAD_HYPHEN);
 509			return -EINVAL;
 510		}
 511		*next_field = field;
 512	}
 513
 514	return ret;
 515}
 516
 517/*
 518 * Parse the field of data structure. The @type must be a pointer type
 519 * pointing the target data structure type.
 520 */
 521static int parse_btf_field(char *fieldname, const struct btf_type *type,
 522			   struct fetch_insn **pcode, struct fetch_insn *end,
 523			   struct traceprobe_parse_context *ctx)
 524{
 525	struct fetch_insn *code = *pcode;
 526	const struct btf_member *field;
 527	u32 bitoffs, anon_offs;
 528	char *next;
 529	int is_ptr;
 530	s32 tid;
 531
 532	do {
 533		/* Outer loop for solving arrow operator ('->') */
 534		if (BTF_INFO_KIND(type->info) != BTF_KIND_PTR) {
 535			trace_probe_log_err(ctx->offset, NO_PTR_STRCT);
 536			return -EINVAL;
 537		}
 538		/* Convert a struct pointer type to a struct type */
 539		type = btf_type_skip_modifiers(ctx->btf, type->type, &tid);
 540		if (!type) {
 541			trace_probe_log_err(ctx->offset, BAD_BTF_TID);
 542			return -EINVAL;
 543		}
 544
 545		bitoffs = 0;
 546		do {
 547			/* Inner loop for solving dot operator ('.') */
 548			next = NULL;
 549			is_ptr = split_next_field(fieldname, &next, ctx);
 550			if (is_ptr < 0)
 551				return is_ptr;
 552
 553			anon_offs = 0;
 554			field = btf_find_struct_member(ctx->btf, type, fieldname,
 555						       &anon_offs);
 556			if (!field) {
 557				trace_probe_log_err(ctx->offset, NO_BTF_FIELD);
 558				return -ENOENT;
 559			}
 560			/* Add anonymous structure/union offset */
 561			bitoffs += anon_offs;
 562
 563			/* Accumulate the bit-offsets of the dot-connected fields */
 564			if (btf_type_kflag(type)) {
 565				bitoffs += BTF_MEMBER_BIT_OFFSET(field->offset);
 566				ctx->last_bitsize = BTF_MEMBER_BITFIELD_SIZE(field->offset);
 567			} else {
 568				bitoffs += field->offset;
 569				ctx->last_bitsize = 0;
 570			}
 571
 572			type = btf_type_skip_modifiers(ctx->btf, field->type, &tid);
 573			if (!type) {
 574				trace_probe_log_err(ctx->offset, BAD_BTF_TID);
 575				return -EINVAL;
 576			}
 577
 578			ctx->offset += next - fieldname;
 579			fieldname = next;
 580		} while (!is_ptr && fieldname);
 581
 582		if (++code == end) {
 583			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
 584			return -EINVAL;
 585		}
 586		code->op = FETCH_OP_DEREF;	/* TODO: user deref support */
 587		code->offset = bitoffs / 8;
 588		*pcode = code;
 589
 590		ctx->last_bitoffs = bitoffs % 8;
 591		ctx->last_type = type;
 592	} while (fieldname);
 593
 594	return 0;
 595}
 596
 597static int parse_btf_arg(char *varname,
 598			 struct fetch_insn **pcode, struct fetch_insn *end,
 599			 struct traceprobe_parse_context *ctx)
 600{
 601	struct fetch_insn *code = *pcode;
 602	const struct btf_param *params;
 603	const struct btf_type *type;
 604	char *field = NULL;
 605	int i, is_ptr, ret;
 606	u32 tid;
 607
 608	if (WARN_ON_ONCE(!ctx->funcname))
 609		return -EINVAL;
 610
 611	is_ptr = split_next_field(varname, &field, ctx);
 612	if (is_ptr < 0)
 613		return is_ptr;
 614	if (!is_ptr && field) {
 615		/* dot-connected field on an argument is not supported. */
 616		trace_probe_log_err(ctx->offset + field - varname,
 617				    NOSUP_DAT_ARG);
 618		return -EOPNOTSUPP;
 619	}
 620
 621	if (ctx->flags & TPARG_FL_RETURN) {
 622		if (strcmp(varname, "$retval") != 0) {
 623			trace_probe_log_err(ctx->offset, NO_BTFARG);
 624			return -ENOENT;
 625		}
 626		code->op = FETCH_OP_RETVAL;
 627		/* Check whether the function return type is not void */
 628		if (query_btf_context(ctx) == 0) {
 629			if (ctx->proto->type == 0) {
 630				trace_probe_log_err(ctx->offset, NO_RETVAL);
 631				return -ENOENT;
 632			}
 633			tid = ctx->proto->type;
 634			goto found;
 635		}
 636		if (field) {
 637			trace_probe_log_err(ctx->offset + field - varname,
 638					    NO_BTF_ENTRY);
 639			return -ENOENT;
 640		}
 641		return 0;
 642	}
 643
 644	if (!ctx->btf) {
 645		ret = query_btf_context(ctx);
 646		if (ret < 0 || ctx->nr_params == 0) {
 647			trace_probe_log_err(ctx->offset, NO_BTF_ENTRY);
 648			return PTR_ERR(params);
 649		}
 650	}
 651	params = ctx->params;
 652
 653	for (i = 0; i < ctx->nr_params; i++) {
 654		const char *name = btf_name_by_offset(ctx->btf, params[i].name_off);
 655
 656		if (name && !strcmp(name, varname)) {
 657			code->op = FETCH_OP_ARG;
 658			if (ctx->flags & TPARG_FL_TPOINT)
 659				code->param = i + 1;
 660			else
 661				code->param = i;
 662			tid = params[i].type;
 663			goto found;
 664		}
 665	}
 666	trace_probe_log_err(ctx->offset, NO_BTFARG);
 667	return -ENOENT;
 668
 669found:
 670	type = btf_type_skip_modifiers(ctx->btf, tid, &tid);
 671	if (!type) {
 672		trace_probe_log_err(ctx->offset, BAD_BTF_TID);
 673		return -EINVAL;
 674	}
 675	/* Initialize the last type information */
 676	ctx->last_type = type;
 677	ctx->last_bitoffs = 0;
 678	ctx->last_bitsize = 0;
 679	if (field) {
 680		ctx->offset += field - varname;
 681		return parse_btf_field(field, type, pcode, end, ctx);
 682	}
 683	return 0;
 684}
 685
 686static const struct fetch_type *find_fetch_type_from_btf_type(
 687					struct traceprobe_parse_context *ctx)
 688{
 689	struct btf *btf = ctx->btf;
 690	const char *typestr = NULL;
 691
 692	if (btf && ctx->last_type)
 693		typestr = fetch_type_from_btf_type(btf, ctx->last_type, ctx);
 694
 695	return find_fetch_type(typestr, ctx->flags);
 696}
 697
 698static int parse_btf_bitfield(struct fetch_insn **pcode,
 699			      struct traceprobe_parse_context *ctx)
 700{
 701	struct fetch_insn *code = *pcode;
 702
 703	if ((ctx->last_bitsize % 8 == 0) && ctx->last_bitoffs == 0)
 704		return 0;
 705
 706	code++;
 707	if (code->op != FETCH_OP_NOP) {
 708		trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
 709		return -EINVAL;
 710	}
 711	*pcode = code;
 712
 713	code->op = FETCH_OP_MOD_BF;
 714	code->lshift = 64 - (ctx->last_bitsize + ctx->last_bitoffs);
 715	code->rshift = 64 - ctx->last_bitsize;
 716	code->basesize = 64 / 8;
 717	return 0;
 718}
 719
 720#else
 721static void clear_btf_context(struct traceprobe_parse_context *ctx)
 722{
 723	ctx->btf = NULL;
 724}
 725
 726static int query_btf_context(struct traceprobe_parse_context *ctx)
 727{
 728	return -EOPNOTSUPP;
 729}
 730
 731static int parse_btf_arg(char *varname,
 732			 struct fetch_insn **pcode, struct fetch_insn *end,
 733			 struct traceprobe_parse_context *ctx)
 734{
 735	trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
 736	return -EOPNOTSUPP;
 737}
 738
 739static int parse_btf_bitfield(struct fetch_insn **pcode,
 740			      struct traceprobe_parse_context *ctx)
 741{
 742	trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
 743	return -EOPNOTSUPP;
 744}
 745
 746#define find_fetch_type_from_btf_type(ctx)		\
 747	find_fetch_type(NULL, ctx->flags)
 748
 749static int check_prepare_btf_string_fetch(char *typename,
 750				struct fetch_insn **pcode,
 751				struct traceprobe_parse_context *ctx)
 752{
 753	return 0;
 754}
 755
 756#endif
 757
 758#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long))
 759
 760/* Parse $vars. @orig_arg points '$', which syncs to @ctx->offset */
 761static int parse_probe_vars(char *orig_arg, const struct fetch_type *t,
 762			    struct fetch_insn **pcode,
 763			    struct fetch_insn *end,
 764			    struct traceprobe_parse_context *ctx)
 765{
 766	struct fetch_insn *code = *pcode;
 767	int err = TP_ERR_BAD_VAR;
 768	char *arg = orig_arg + 1;
 769	unsigned long param;
 770	int ret = 0;
 771	int len;
 772
 773	if (ctx->flags & TPARG_FL_TEVENT) {
 774		if (code->data)
 775			return -EFAULT;
 776		ret = parse_trace_event_arg(arg, code, ctx);
 777		if (!ret)
 778			return 0;
 779		if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
 780			code->op = FETCH_OP_COMM;
 781			return 0;
 782		}
 783		/* backward compatibility */
 784		ctx->offset = 0;
 785		goto inval;
 786	}
 787
 788	if (str_has_prefix(arg, "retval")) {
 789		if (!(ctx->flags & TPARG_FL_RETURN)) {
 790			err = TP_ERR_RETVAL_ON_PROBE;
 791			goto inval;
 792		}
 793		if (!(ctx->flags & TPARG_FL_KERNEL) ||
 794		    !IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
 795			code->op = FETCH_OP_RETVAL;
 796			return 0;
 797		}
 798		return parse_btf_arg(orig_arg, pcode, end, ctx);
 799	}
 800
 801	len = str_has_prefix(arg, "stack");
 802	if (len) {
 803
 804		if (arg[len] == '\0') {
 805			code->op = FETCH_OP_STACKP;
 806			return 0;
 807		}
 808
 809		if (isdigit(arg[len])) {
 810			ret = kstrtoul(arg + len, 10, &param);
 811			if (ret)
 812				goto inval;
 813
 814			if ((ctx->flags & TPARG_FL_KERNEL) &&
 815			    param > PARAM_MAX_STACK) {
 816				err = TP_ERR_BAD_STACK_NUM;
 817				goto inval;
 818			}
 819			code->op = FETCH_OP_STACK;
 820			code->param = (unsigned int)param;
 821			return 0;
 822		}
 823		goto inval;
 824	}
 825
 826	if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) {
 827		code->op = FETCH_OP_COMM;
 828		return 0;
 829	}
 830
 831#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
 832	len = str_has_prefix(arg, "arg");
 833	if (len && tparg_is_function_entry(ctx->flags)) {
 834		ret = kstrtoul(arg + len, 10, &param);
 835		if (ret)
 836			goto inval;
 837
 838		if (!param || param > PARAM_MAX_STACK) {
 839			err = TP_ERR_BAD_ARG_NUM;
 840			goto inval;
 841		}
 842
 843		code->op = FETCH_OP_ARG;
 844		code->param = (unsigned int)param - 1;
 845		/*
 846		 * The tracepoint probe will probe a stub function, and the
 847		 * first parameter of the stub is a dummy and should be ignored.
 848		 */
 849		if (ctx->flags & TPARG_FL_TPOINT)
 850			code->param++;
 851		return 0;
 852	}
 853#endif
 854
 855inval:
 856	__trace_probe_log_err(ctx->offset, err);
 857	return -EINVAL;
 858}
 859
 860static int str_to_immediate(char *str, unsigned long *imm)
 861{
 862	if (isdigit(str[0]))
 863		return kstrtoul(str, 0, imm);
 864	else if (str[0] == '-')
 865		return kstrtol(str, 0, (long *)imm);
 866	else if (str[0] == '+')
 867		return kstrtol(str + 1, 0, (long *)imm);
 868	return -EINVAL;
 869}
 870
 871static int __parse_imm_string(char *str, char **pbuf, int offs)
 872{
 873	size_t len = strlen(str);
 874
 875	if (str[len - 1] != '"') {
 876		trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE);
 877		return -EINVAL;
 878	}
 879	*pbuf = kstrndup(str, len - 1, GFP_KERNEL);
 880	if (!*pbuf)
 881		return -ENOMEM;
 882	return 0;
 883}
 884
 885/* Recursive argument parser */
 886static int
 887parse_probe_arg(char *arg, const struct fetch_type *type,
 888		struct fetch_insn **pcode, struct fetch_insn *end,
 889		struct traceprobe_parse_context *ctx)
 890{
 891	struct fetch_insn *code = *pcode;
 892	unsigned long param;
 893	int deref = FETCH_OP_DEREF;
 894	long offset = 0;
 895	char *tmp;
 896	int ret = 0;
 897
 898	switch (arg[0]) {
 899	case '$':
 900		ret = parse_probe_vars(arg, type, pcode, end, ctx);
 901		break;
 902
 903	case '%':	/* named register */
 904		if (ctx->flags & (TPARG_FL_TEVENT | TPARG_FL_FPROBE)) {
 905			/* eprobe and fprobe do not handle registers */
 906			trace_probe_log_err(ctx->offset, BAD_VAR);
 907			break;
 908		}
 909		ret = regs_query_register_offset(arg + 1);
 910		if (ret >= 0) {
 911			code->op = FETCH_OP_REG;
 912			code->param = (unsigned int)ret;
 913			ret = 0;
 914		} else
 915			trace_probe_log_err(ctx->offset, BAD_REG_NAME);
 916		break;
 917
 918	case '@':	/* memory, file-offset or symbol */
 919		if (isdigit(arg[1])) {
 920			ret = kstrtoul(arg + 1, 0, &param);
 921			if (ret) {
 922				trace_probe_log_err(ctx->offset, BAD_MEM_ADDR);
 923				break;
 924			}
 925			/* load address */
 926			code->op = FETCH_OP_IMM;
 927			code->immediate = param;
 928		} else if (arg[1] == '+') {
 929			/* kprobes don't support file offsets */
 930			if (ctx->flags & TPARG_FL_KERNEL) {
 931				trace_probe_log_err(ctx->offset, FILE_ON_KPROBE);
 932				return -EINVAL;
 933			}
 934			ret = kstrtol(arg + 2, 0, &offset);
 935			if (ret) {
 936				trace_probe_log_err(ctx->offset, BAD_FILE_OFFS);
 937				break;
 938			}
 939
 940			code->op = FETCH_OP_FOFFS;
 941			code->immediate = (unsigned long)offset;  // imm64?
 942		} else {
 943			/* uprobes don't support symbols */
 944			if (!(ctx->flags & TPARG_FL_KERNEL)) {
 945				trace_probe_log_err(ctx->offset, SYM_ON_UPROBE);
 946				return -EINVAL;
 947			}
 948			/* Preserve symbol for updating */
 949			code->op = FETCH_NOP_SYMBOL;
 950			code->data = kstrdup(arg + 1, GFP_KERNEL);
 951			if (!code->data)
 952				return -ENOMEM;
 953			if (++code == end) {
 954				trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
 955				return -EINVAL;
 956			}
 957			code->op = FETCH_OP_IMM;
 958			code->immediate = 0;
 959		}
 960		/* These are fetching from memory */
 961		if (++code == end) {
 962			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
 963			return -EINVAL;
 964		}
 965		*pcode = code;
 966		code->op = FETCH_OP_DEREF;
 967		code->offset = offset;
 968		break;
 969
 970	case '+':	/* deref memory */
 
 971	case '-':
 972		if (arg[1] == 'u') {
 973			deref = FETCH_OP_UDEREF;
 974			arg[1] = arg[0];
 975			arg++;
 976		}
 977		if (arg[0] == '+')
 978			arg++;	/* Skip '+', because kstrtol() rejects it. */
 979		tmp = strchr(arg, '(');
 980		if (!tmp) {
 981			trace_probe_log_err(ctx->offset, DEREF_NEED_BRACE);
 982			return -EINVAL;
 983		}
 984		*tmp = '\0';
 985		ret = kstrtol(arg, 0, &offset);
 986		if (ret) {
 987			trace_probe_log_err(ctx->offset, BAD_DEREF_OFFS);
 988			break;
 989		}
 990		ctx->offset += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0);
 991		arg = tmp + 1;
 992		tmp = strrchr(arg, ')');
 993		if (!tmp) {
 994			trace_probe_log_err(ctx->offset + strlen(arg),
 995					    DEREF_OPEN_BRACE);
 996			return -EINVAL;
 997		} else {
 998			const struct fetch_type *t2 = find_fetch_type(NULL, ctx->flags);
 999			int cur_offs = ctx->offset;
1000
 
 
 
 
 
1001			*tmp = '\0';
1002			ret = parse_probe_arg(arg, t2, &code, end, ctx);
 
 
 
 
 
 
 
 
 
 
1003			if (ret)
1004				break;
1005			ctx->offset = cur_offs;
1006			if (code->op == FETCH_OP_COMM ||
1007			    code->op == FETCH_OP_DATA) {
1008				trace_probe_log_err(ctx->offset, COMM_CANT_DEREF);
1009				return -EINVAL;
1010			}
1011			if (++code == end) {
1012				trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1013				return -EINVAL;
1014			}
1015			*pcode = code;
1016
1017			code->op = deref;
1018			code->offset = offset;
1019			/* Reset the last type if used */
1020			ctx->last_type = NULL;
1021		}
1022		break;
1023	case '\\':	/* Immediate value */
1024		if (arg[1] == '"') {	/* Immediate string */
1025			ret = __parse_imm_string(arg + 2, &tmp, ctx->offset + 2);
1026			if (ret)
1027				break;
1028			code->op = FETCH_OP_DATA;
1029			code->data = tmp;
1030		} else {
1031			ret = str_to_immediate(arg + 1, &code->immediate);
1032			if (ret)
1033				trace_probe_log_err(ctx->offset + 1, BAD_IMM);
1034			else
1035				code->op = FETCH_OP_IMM;
1036		}
1037		break;
1038	default:
1039		if (isalpha(arg[0]) || arg[0] == '_') {	/* BTF variable */
1040			if (!tparg_is_function_entry(ctx->flags)) {
1041				trace_probe_log_err(ctx->offset, NOSUP_BTFARG);
1042				return -EINVAL;
1043			}
1044			ret = parse_btf_arg(arg, pcode, end, ctx);
1045			break;
1046		}
1047	}
1048	if (!ret && code->op == FETCH_OP_NOP) {
1049		/* Parsed, but do not find fetch method */
1050		trace_probe_log_err(ctx->offset, BAD_FETCH_ARG);
1051		ret = -EINVAL;
1052	}
 
1053	return ret;
1054}
1055
1056#define BYTES_TO_BITS(nb)	((BITS_PER_LONG * (nb)) / sizeof(long))
1057
1058/* Bitfield type needs to be parsed into a fetch function */
1059static int __parse_bitfield_probe_arg(const char *bf,
1060				      const struct fetch_type *t,
1061				      struct fetch_insn **pcode)
1062{
1063	struct fetch_insn *code = *pcode;
1064	unsigned long bw, bo;
1065	char *tail;
1066
1067	if (*bf != 'b')
1068		return 0;
1069
 
 
 
 
 
 
 
1070	bw = simple_strtoul(bf + 1, &tail, 0);	/* Use simple one */
1071
1072	if (bw == 0 || *tail != '@')
1073		return -EINVAL;
1074
1075	bf = tail + 1;
1076	bo = simple_strtoul(bf, &tail, 0);
1077
1078	if (tail == bf || *tail != '/')
1079		return -EINVAL;
1080	code++;
1081	if (code->op != FETCH_OP_NOP)
1082		return -EINVAL;
1083	*pcode = code;
1084
1085	code->op = FETCH_OP_MOD_BF;
1086	code->lshift = BYTES_TO_BITS(t->size) - (bw + bo);
1087	code->rshift = BYTES_TO_BITS(t->size) - bw;
1088	code->basesize = t->size;
1089
1090	return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0;
1091}
1092
1093/* String length checking wrapper */
1094static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size,
1095					   struct probe_arg *parg,
1096					   struct traceprobe_parse_context *ctx)
1097{
1098	struct fetch_insn *code, *scode, *tmp = NULL;
1099	char *t, *t2, *t3;
1100	int ret, len;
1101	char *arg;
1102
1103	arg = kstrdup(argv, GFP_KERNEL);
1104	if (!arg)
 
 
 
 
 
1105		return -ENOMEM;
1106
1107	ret = -EINVAL;
1108	len = strlen(arg);
1109	if (len > MAX_ARGSTR_LEN) {
1110		trace_probe_log_err(ctx->offset, ARG_TOO_LONG);
1111		goto out;
1112	} else if (len == 0) {
1113		trace_probe_log_err(ctx->offset, NO_ARG_BODY);
1114		goto out;
1115	}
1116
1117	ret = -ENOMEM;
1118	parg->comm = kstrdup(arg, GFP_KERNEL);
1119	if (!parg->comm)
1120		goto out;
1121
1122	ret = -EINVAL;
1123	t = strchr(arg, ':');
1124	if (t) {
1125		*t = '\0';
1126		t2 = strchr(++t, '[');
1127		if (t2) {
1128			*t2++ = '\0';
1129			t3 = strchr(t2, ']');
1130			if (!t3) {
1131				int offs = t2 + strlen(t2) - arg;
1132
1133				trace_probe_log_err(ctx->offset + offs,
1134						    ARRAY_NO_CLOSE);
1135				goto out;
1136			} else if (t3[1] != '\0') {
1137				trace_probe_log_err(ctx->offset + t3 + 1 - arg,
1138						    BAD_ARRAY_SUFFIX);
1139				goto out;
1140			}
1141			*t3 = '\0';
1142			if (kstrtouint(t2, 0, &parg->count) || !parg->count) {
1143				trace_probe_log_err(ctx->offset + t2 - arg,
1144						    BAD_ARRAY_NUM);
1145				goto out;
1146			}
1147			if (parg->count > MAX_ARRAY_LEN) {
1148				trace_probe_log_err(ctx->offset + t2 - arg,
1149						    ARRAY_TOO_BIG);
1150				goto out;
1151			}
1152		}
1153	}
1154
1155	/*
1156	 * Since $comm and immediate string can not be dereferenced,
1157	 * we can find those by strcmp. But ignore for eprobes.
1158	 */
1159	if (!(ctx->flags & TPARG_FL_TEVENT) &&
1160	    (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 ||
1161	     strncmp(arg, "\\\"", 2) == 0)) {
1162		/* The type of $comm must be "string", and not an array type. */
1163		if (parg->count || (t && strcmp(t, "string"))) {
1164			trace_probe_log_err(ctx->offset + (t ? (t - arg) : 0),
1165					NEED_STRING_TYPE);
1166			goto out;
1167		}
1168		parg->type = find_fetch_type("string", ctx->flags);
1169	} else
1170		parg->type = find_fetch_type(t, ctx->flags);
1171	if (!parg->type) {
1172		trace_probe_log_err(ctx->offset + (t ? (t - arg) : 0), BAD_TYPE);
1173		goto out;
1174	}
1175
1176	code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL);
1177	if (!code)
1178		goto out;
1179	code[FETCH_INSN_MAX - 1].op = FETCH_OP_END;
1180
1181	ctx->last_type = NULL;
1182	ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1],
1183			      ctx);
1184	if (ret)
1185		goto fail;
1186
1187	/* Update storing type if BTF is available */
1188	if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) &&
1189	    ctx->last_type) {
1190		if (!t) {
1191			parg->type = find_fetch_type_from_btf_type(ctx);
1192		} else if (strstr(t, "string")) {
1193			ret = check_prepare_btf_string_fetch(t, &code, ctx);
1194			if (ret)
1195				goto fail;
1196		}
1197	}
1198	parg->offset = *size;
1199	*size += parg->type->size * (parg->count ?: 1);
1200
1201	if (parg->count) {
1202		len = strlen(parg->type->fmttype) + 6;
1203		parg->fmt = kmalloc(len, GFP_KERNEL);
1204		if (!parg->fmt) {
1205			ret = -ENOMEM;
1206			goto out;
1207		}
1208		snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype,
1209			 parg->count);
 
1210	}
1211
1212	ret = -EINVAL;
1213	/* Store operation */
1214	if (parg->type->is_string) {
1215		if (!strcmp(parg->type->name, "symstr")) {
1216			if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK &&
1217			    code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG &&
1218			    code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) {
1219				trace_probe_log_err(ctx->offset + (t ? (t - arg) : 0),
1220						    BAD_SYMSTRING);
1221				goto fail;
1222			}
1223		} else {
1224			if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF &&
1225			    code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM &&
1226			    code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) {
1227				trace_probe_log_err(ctx->offset + (t ? (t - arg) : 0),
1228						    BAD_STRING);
1229				goto fail;
1230			}
1231		}
1232		if (!strcmp(parg->type->name, "symstr") ||
1233		    (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM ||
1234		     code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG ||
1235		     parg->count) {
1236			/*
1237			 * IMM, DATA and COMM is pointing actual address, those
1238			 * must be kept, and if parg->count != 0, this is an
1239			 * array of string pointers instead of string address
1240			 * itself.
1241			 * For the symstr, it doesn't need to dereference, thus
1242			 * it just get the value.
1243			 */
1244			code++;
1245			if (code->op != FETCH_OP_NOP) {
1246				trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1247				goto fail;
1248			}
1249		}
1250		/* If op == DEREF, replace it with STRING */
1251		if (!strcmp(parg->type->name, "ustring") ||
1252		    code->op == FETCH_OP_UDEREF)
1253			code->op = FETCH_OP_ST_USTRING;
1254		else if (!strcmp(parg->type->name, "symstr"))
1255			code->op = FETCH_OP_ST_SYMSTR;
1256		else
1257			code->op = FETCH_OP_ST_STRING;
1258		code->size = parg->type->size;
1259		parg->dynamic = true;
1260	} else if (code->op == FETCH_OP_DEREF) {
1261		code->op = FETCH_OP_ST_MEM;
1262		code->size = parg->type->size;
1263	} else if (code->op == FETCH_OP_UDEREF) {
1264		code->op = FETCH_OP_ST_UMEM;
1265		code->size = parg->type->size;
1266	} else {
1267		code++;
1268		if (code->op != FETCH_OP_NOP) {
1269			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1270			goto fail;
1271		}
1272		code->op = FETCH_OP_ST_RAW;
1273		code->size = parg->type->size;
1274	}
1275	scode = code;
1276	/* Modify operation */
1277	if (t != NULL) {
1278		ret = __parse_bitfield_probe_arg(t, parg->type, &code);
1279		if (ret) {
1280			trace_probe_log_err(ctx->offset + t - arg, BAD_BITFIELD);
1281			goto fail;
1282		}
1283	} else if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) &&
1284		   ctx->last_type) {
1285		ret = parse_btf_bitfield(&code, ctx);
1286		if (ret)
1287			goto fail;
1288	}
1289	ret = -EINVAL;
1290	/* Loop(Array) operation */
1291	if (parg->count) {
1292		if (scode->op != FETCH_OP_ST_MEM &&
1293		    scode->op != FETCH_OP_ST_STRING &&
1294		    scode->op != FETCH_OP_ST_USTRING) {
1295			trace_probe_log_err(ctx->offset + (t ? (t - arg) : 0),
1296					    BAD_STRING);
1297			goto fail;
1298		}
1299		code++;
1300		if (code->op != FETCH_OP_NOP) {
1301			trace_probe_log_err(ctx->offset, TOO_MANY_OPS);
1302			goto fail;
1303		}
1304		code->op = FETCH_OP_LP_ARRAY;
1305		code->param = parg->count;
1306	}
1307	code++;
1308	code->op = FETCH_OP_END;
1309
1310	ret = 0;
1311	/* Shrink down the code buffer */
1312	parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL);
1313	if (!parg->code)
1314		ret = -ENOMEM;
1315	else
1316		memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1));
1317
1318fail:
1319	if (ret) {
1320		for (code = tmp; code < tmp + FETCH_INSN_MAX; code++)
1321			if (code->op == FETCH_NOP_SYMBOL ||
1322			    code->op == FETCH_OP_DATA)
1323				kfree(code->data);
1324	}
1325	kfree(tmp);
1326out:
1327	kfree(arg);
1328
1329	return ret;
1330}
1331
1332/* Return 1 if name is reserved or already used by another argument */
1333static int traceprobe_conflict_field_name(const char *name,
1334					  struct probe_arg *args, int narg)
1335{
1336	int i;
1337
1338	for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++)
1339		if (strcmp(reserved_field_names[i], name) == 0)
1340			return 1;
1341
1342	for (i = 0; i < narg; i++)
1343		if (strcmp(args[i].name, name) == 0)
1344			return 1;
1345
1346	return 0;
1347}
1348
1349static char *generate_probe_arg_name(const char *arg, int idx)
1350{
1351	char *name = NULL;
1352	const char *end;
1353
1354	/*
1355	 * If argument name is omitted, try arg as a name (BTF variable)
1356	 * or "argN".
1357	 */
1358	if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) {
1359		end = strchr(arg, ':');
1360		if (!end)
1361			end = arg + strlen(arg);
1362
1363		name = kmemdup_nul(arg, end - arg, GFP_KERNEL);
1364		if (!name || !is_good_name(name)) {
1365			kfree(name);
1366			name = NULL;
1367		}
1368	}
1369
1370	if (!name)
1371		name = kasprintf(GFP_KERNEL, "arg%d", idx + 1);
1372
1373	return name;
1374}
1375
1376int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg,
1377			       struct traceprobe_parse_context *ctx)
1378{
1379	struct probe_arg *parg = &tp->args[i];
1380	const char *body;
1381
1382	/* Increment count for freeing args in error case */
1383	tp->nr_args++;
1384
1385	body = strchr(arg, '=');
1386	if (body) {
1387		if (body - arg > MAX_ARG_NAME_LEN) {
1388			trace_probe_log_err(0, ARG_NAME_TOO_LONG);
1389			return -EINVAL;
1390		} else if (body == arg) {
1391			trace_probe_log_err(0, NO_ARG_NAME);
1392			return -EINVAL;
1393		}
1394		parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL);
1395		body++;
1396	} else {
1397		parg->name = generate_probe_arg_name(arg, i);
1398		body = arg;
1399	}
1400	if (!parg->name)
1401		return -ENOMEM;
1402
1403	if (!is_good_name(parg->name)) {
1404		trace_probe_log_err(0, BAD_ARG_NAME);
1405		return -EINVAL;
1406	}
1407	if (traceprobe_conflict_field_name(parg->name, tp->args, i)) {
1408		trace_probe_log_err(0, USED_ARG_NAME);
1409		return -EINVAL;
1410	}
1411	ctx->offset = body - arg;
1412	/* Parse fetch argument */
1413	return traceprobe_parse_probe_arg_body(body, &tp->size, parg, ctx);
1414}
1415
1416void traceprobe_free_probe_arg(struct probe_arg *arg)
1417{
1418	struct fetch_insn *code = arg->code;
 
 
 
 
 
1419
1420	while (code && code->op != FETCH_OP_END) {
1421		if (code->op == FETCH_NOP_SYMBOL ||
1422		    code->op == FETCH_OP_DATA)
1423			kfree(code->data);
1424		code++;
1425	}
1426	kfree(arg->code);
1427	kfree(arg->name);
1428	kfree(arg->comm);
1429	kfree(arg->fmt);
1430}
1431
1432static int argv_has_var_arg(int argc, const char *argv[], int *args_idx,
1433			    struct traceprobe_parse_context *ctx)
1434{
1435	int i, found = 0;
 
1436
1437	for (i = 0; i < argc; i++)
1438		if (str_has_prefix(argv[i], "$arg")) {
1439			trace_probe_log_set_index(i + 2);
 
 
1440
1441			if (!tparg_is_function_entry(ctx->flags)) {
1442				trace_probe_log_err(0, NOFENTRY_ARGS);
1443				return -EINVAL;
1444			}
1445
1446			if (isdigit(argv[i][4])) {
1447				found = 1;
1448				continue;
1449			}
1450
1451			if (argv[i][4] != '*') {
1452				trace_probe_log_err(0, BAD_VAR);
1453				return -EINVAL;
1454			}
1455
1456			if (*args_idx >= 0 && *args_idx < argc) {
1457				trace_probe_log_err(0, DOUBLE_ARGS);
1458				return -EINVAL;
1459			}
1460			found = 1;
1461			*args_idx = i;
1462		}
1463
1464	return found;
1465}
 
 
 
 
 
 
1466
1467static int sprint_nth_btf_arg(int idx, const char *type,
1468			      char *buf, int bufsize,
1469			      struct traceprobe_parse_context *ctx)
1470{
1471	const char *name;
1472	int ret;
1473
1474	if (idx >= ctx->nr_params) {
1475		trace_probe_log_err(0, NO_BTFARG);
1476		return -ENOENT;
1477	}
1478	name = btf_name_by_offset(ctx->btf, ctx->params[idx].name_off);
1479	if (!name) {
1480		trace_probe_log_err(0, NO_BTF_ENTRY);
1481		return -ENOENT;
1482	}
1483	ret = snprintf(buf, bufsize, "%s%s", name, type);
1484	if (ret >= bufsize) {
1485		trace_probe_log_err(0, ARGS_2LONG);
1486		return -E2BIG;
1487	}
1488	return ret;
1489}
1490
1491/* Return new_argv which must be freed after use */
1492const char **traceprobe_expand_meta_args(int argc, const char *argv[],
1493					 int *new_argc, char *buf, int bufsize,
1494					 struct traceprobe_parse_context *ctx)
1495{
1496	const struct btf_param *params = NULL;
1497	int i, j, n, used, ret, args_idx = -1;
1498	const char **new_argv = NULL;
1499
1500	ret = argv_has_var_arg(argc, argv, &args_idx, ctx);
1501	if (ret < 0)
1502		return ERR_PTR(ret);
1503
1504	if (!ret) {
1505		*new_argc = argc;
1506		return NULL;
1507	}
1508
1509	ret = query_btf_context(ctx);
1510	if (ret < 0 || ctx->nr_params == 0) {
1511		if (args_idx != -1) {
1512			/* $arg* requires BTF info */
1513			trace_probe_log_err(0, NOSUP_BTFARG);
1514			return (const char **)params;
1515		}
1516		*new_argc = argc;
1517		return NULL;
1518	}
1519
1520	if (args_idx >= 0)
1521		*new_argc = argc + ctx->nr_params - 1;
1522	else
1523		*new_argc = argc;
1524
1525	new_argv = kcalloc(*new_argc, sizeof(char *), GFP_KERNEL);
1526	if (!new_argv)
1527		return ERR_PTR(-ENOMEM);
1528
1529	used = 0;
1530	for (i = 0, j = 0; i < argc; i++) {
1531		trace_probe_log_set_index(i + 2);
1532		if (i == args_idx) {
1533			for (n = 0; n < ctx->nr_params; n++) {
1534				ret = sprint_nth_btf_arg(n, "", buf + used,
1535							 bufsize - used, ctx);
1536				if (ret < 0)
1537					goto error;
1538
1539				new_argv[j++] = buf + used;
1540				used += ret + 1;
1541			}
1542			continue;
1543		}
 
 
 
1544
1545		if (str_has_prefix(argv[i], "$arg")) {
1546			char *type = NULL;
1547
1548			n = simple_strtoul(argv[i] + 4, &type, 10);
1549			if (type && !(*type == ':' || *type == '\0')) {
1550				trace_probe_log_err(0, BAD_VAR);
1551				ret = -ENOENT;
1552				goto error;
1553			}
1554			/* Note: $argN starts from $arg1 */
1555			ret = sprint_nth_btf_arg(n - 1, type, buf + used,
1556						 bufsize - used, ctx);
1557			if (ret < 0)
1558				goto error;
1559			new_argv[j++] = buf + used;
1560			used += ret + 1;
1561		} else
1562			new_argv[j++] = argv[i];
1563	}
 
1564
1565	return new_argv;
 
1566
1567error:
1568	kfree(new_argv);
1569	return ERR_PTR(ret);
1570}
1571
1572void traceprobe_finish_parse(struct traceprobe_parse_context *ctx)
1573{
1574	clear_btf_context(ctx);
1575}
1576
1577int traceprobe_update_arg(struct probe_arg *arg)
1578{
1579	struct fetch_insn *code = arg->code;
1580	long offset;
1581	char *tmp;
1582	char c;
1583	int ret = 0;
1584
1585	while (code && code->op != FETCH_OP_END) {
1586		if (code->op == FETCH_NOP_SYMBOL) {
1587			if (code[1].op != FETCH_OP_IMM)
1588				return -EINVAL;
1589
1590			tmp = strpbrk(code->data, "+-");
1591			if (tmp)
1592				c = *tmp;
1593			ret = traceprobe_split_symbol_offset(code->data,
1594							     &offset);
1595			if (ret)
1596				return ret;
1597
1598			code[1].immediate =
1599				(unsigned long)kallsyms_lookup_name(code->data);
1600			if (tmp)
1601				*tmp = c;
1602			if (!code[1].immediate)
1603				return -ENOENT;
1604			code[1].immediate += offset;
1605		}
1606		code++;
1607	}
1608	return 0;
1609}
1610
1611/* When len=0, we just calculate the needed length */
1612#define LEN_OR_ZERO (len ? len - pos : 0)
1613static int __set_print_fmt(struct trace_probe *tp, char *buf, int len,
1614			   enum probe_print_type ptype)
1615{
1616	struct probe_arg *parg;
1617	int i, j;
1618	int pos = 0;
 
1619	const char *fmt, *arg;
1620
1621	switch (ptype) {
1622	case PROBE_PRINT_NORMAL:
1623		fmt = "(%lx)";
1624		arg = ", REC->" FIELD_STRING_IP;
1625		break;
1626	case PROBE_PRINT_RETURN:
1627		fmt = "(%lx <- %lx)";
1628		arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP;
1629		break;
1630	case PROBE_PRINT_EVENT:
1631		fmt = "";
1632		arg = "";
1633		break;
1634	default:
1635		WARN_ON_ONCE(1);
1636		return 0;
1637	}
1638
 
 
 
1639	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
1640
1641	for (i = 0; i < tp->nr_args; i++) {
1642		parg = tp->args + i;
1643		pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name);
1644		if (parg->count) {
1645			pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s",
1646					parg->type->fmt);
1647			for (j = 1; j < parg->count; j++)
1648				pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s",
1649						parg->type->fmt);
1650			pos += snprintf(buf + pos, LEN_OR_ZERO, "}");
1651		} else
1652			pos += snprintf(buf + pos, LEN_OR_ZERO, "%s",
1653					parg->type->fmt);
1654	}
1655
1656	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg);
1657
1658	for (i = 0; i < tp->nr_args; i++) {
1659		parg = tp->args + i;
1660		if (parg->count) {
1661			if (parg->type->is_string)
1662				fmt = ", __get_str(%s[%d])";
1663			else
1664				fmt = ", REC->%s[%d]";
1665			for (j = 0; j < parg->count; j++)
1666				pos += snprintf(buf + pos, LEN_OR_ZERO,
1667						fmt, parg->name, j);
1668		} else {
1669			if (parg->type->is_string)
1670				fmt = ", __get_str(%s)";
1671			else
1672				fmt = ", REC->%s";
1673			pos += snprintf(buf + pos, LEN_OR_ZERO,
1674					fmt, parg->name);
1675		}
 
 
 
1676	}
1677
 
 
1678	/* return the length of print_fmt */
1679	return pos;
1680}
1681#undef LEN_OR_ZERO
1682
1683int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype)
1684{
1685	struct trace_event_call *call = trace_probe_event_call(tp);
1686	int len;
1687	char *print_fmt;
1688
1689	/* First: called with 0 length to calculate the needed length */
1690	len = __set_print_fmt(tp, NULL, 0, ptype);
1691	print_fmt = kmalloc(len + 1, GFP_KERNEL);
1692	if (!print_fmt)
1693		return -ENOMEM;
1694
1695	/* Second: actually write the @print_fmt */
1696	__set_print_fmt(tp, print_fmt, len + 1, ptype);
1697	call->print_fmt = print_fmt;
1698
1699	return 0;
1700}
1701
1702int traceprobe_define_arg_fields(struct trace_event_call *event_call,
1703				 size_t offset, struct trace_probe *tp)
1704{
1705	int ret, i;
1706
1707	/* Set argument names as fields */
1708	for (i = 0; i < tp->nr_args; i++) {
1709		struct probe_arg *parg = &tp->args[i];
1710		const char *fmt = parg->type->fmttype;
1711		int size = parg->type->size;
1712
1713		if (parg->fmt)
1714			fmt = parg->fmt;
1715		if (parg->count)
1716			size *= parg->count;
1717		ret = trace_define_field(event_call, fmt, parg->name,
1718					 offset + parg->offset, size,
1719					 parg->type->is_signed,
1720					 FILTER_OTHER);
1721		if (ret)
1722			return ret;
1723	}
1724	return 0;
1725}
1726
1727static void trace_probe_event_free(struct trace_probe_event *tpe)
1728{
1729	kfree(tpe->class.system);
1730	kfree(tpe->call.name);
1731	kfree(tpe->call.print_fmt);
1732	kfree(tpe);
1733}
1734
1735int trace_probe_append(struct trace_probe *tp, struct trace_probe *to)
1736{
1737	if (trace_probe_has_sibling(tp))
1738		return -EBUSY;
1739
1740	list_del_init(&tp->list);
1741	trace_probe_event_free(tp->event);
1742
1743	tp->event = to->event;
1744	list_add_tail(&tp->list, trace_probe_probe_list(to));
1745
1746	return 0;
1747}
1748
1749void trace_probe_unlink(struct trace_probe *tp)
1750{
1751	list_del_init(&tp->list);
1752	if (list_empty(trace_probe_probe_list(tp)))
1753		trace_probe_event_free(tp->event);
1754	tp->event = NULL;
1755}
1756
1757void trace_probe_cleanup(struct trace_probe *tp)
1758{
1759	int i;
1760
1761	for (i = 0; i < tp->nr_args; i++)
1762		traceprobe_free_probe_arg(&tp->args[i]);
1763
1764	if (tp->event)
1765		trace_probe_unlink(tp);
1766}
1767
1768int trace_probe_init(struct trace_probe *tp, const char *event,
1769		     const char *group, bool alloc_filter)
1770{
1771	struct trace_event_call *call;
1772	size_t size = sizeof(struct trace_probe_event);
1773	int ret = 0;
1774
1775	if (!event || !group)
1776		return -EINVAL;
1777
1778	if (alloc_filter)
1779		size += sizeof(struct trace_uprobe_filter);
1780
1781	tp->event = kzalloc(size, GFP_KERNEL);
1782	if (!tp->event)
1783		return -ENOMEM;
1784
1785	INIT_LIST_HEAD(&tp->event->files);
1786	INIT_LIST_HEAD(&tp->event->class.fields);
1787	INIT_LIST_HEAD(&tp->event->probes);
1788	INIT_LIST_HEAD(&tp->list);
1789	list_add(&tp->list, &tp->event->probes);
1790
1791	call = trace_probe_event_call(tp);
1792	call->class = &tp->event->class;
1793	call->name = kstrdup(event, GFP_KERNEL);
1794	if (!call->name) {
1795		ret = -ENOMEM;
1796		goto error;
1797	}
1798
1799	tp->event->class.system = kstrdup(group, GFP_KERNEL);
1800	if (!tp->event->class.system) {
1801		ret = -ENOMEM;
1802		goto error;
1803	}
1804
1805	return 0;
1806
1807error:
1808	trace_probe_cleanup(tp);
1809	return ret;
1810}
1811
1812static struct trace_event_call *
1813find_trace_event_call(const char *system, const char *event_name)
1814{
1815	struct trace_event_call *tp_event;
1816	const char *name;
1817
1818	list_for_each_entry(tp_event, &ftrace_events, list) {
1819		if (!tp_event->class->system ||
1820		    strcmp(system, tp_event->class->system))
1821			continue;
1822		name = trace_event_name(tp_event);
1823		if (!name || strcmp(event_name, name))
1824			continue;
1825		return tp_event;
1826	}
1827
1828	return NULL;
1829}
1830
1831int trace_probe_register_event_call(struct trace_probe *tp)
1832{
1833	struct trace_event_call *call = trace_probe_event_call(tp);
1834	int ret;
1835
1836	lockdep_assert_held(&event_mutex);
1837
1838	if (find_trace_event_call(trace_probe_group_name(tp),
1839				  trace_probe_name(tp)))
1840		return -EEXIST;
1841
1842	ret = register_trace_event(&call->event);
1843	if (!ret)
1844		return -ENODEV;
1845
1846	ret = trace_add_event_call(call);
1847	if (ret)
1848		unregister_trace_event(&call->event);
1849
1850	return ret;
1851}
1852
1853int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file)
1854{
1855	struct event_file_link *link;
1856
1857	link = kmalloc(sizeof(*link), GFP_KERNEL);
1858	if (!link)
1859		return -ENOMEM;
1860
1861	link->file = file;
1862	INIT_LIST_HEAD(&link->list);
1863	list_add_tail_rcu(&link->list, &tp->event->files);
1864	trace_probe_set_flag(tp, TP_FLAG_TRACE);
1865	return 0;
1866}
1867
1868struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp,
1869						  struct trace_event_file *file)
1870{
1871	struct event_file_link *link;
1872
1873	trace_probe_for_each_link(link, tp) {
1874		if (link->file == file)
1875			return link;
1876	}
1877
1878	return NULL;
1879}
1880
1881int trace_probe_remove_file(struct trace_probe *tp,
1882			    struct trace_event_file *file)
1883{
1884	struct event_file_link *link;
1885
1886	link = trace_probe_get_file_link(tp, file);
1887	if (!link)
1888		return -ENOENT;
1889
1890	list_del_rcu(&link->list);
1891	kvfree_rcu_mightsleep(link);
1892
1893	if (list_empty(&tp->event->files))
1894		trace_probe_clear_flag(tp, TP_FLAG_TRACE);
1895
1896	return 0;
1897}
1898
1899/*
1900 * Return the smallest index of different type argument (start from 1).
1901 * If all argument types and name are same, return 0.
1902 */
1903int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b)
1904{
1905	int i;
1906
1907	/* In case of more arguments */
1908	if (a->nr_args < b->nr_args)
1909		return a->nr_args + 1;
1910	if (a->nr_args > b->nr_args)
1911		return b->nr_args + 1;
1912
1913	for (i = 0; i < a->nr_args; i++) {
1914		if ((b->nr_args <= i) ||
1915		    ((a->args[i].type != b->args[i].type) ||
1916		     (a->args[i].count != b->args[i].count) ||
1917		     strcmp(a->args[i].name, b->args[i].name)))
1918			return i + 1;
1919	}
1920
1921	return 0;
1922}
1923
1924bool trace_probe_match_command_args(struct trace_probe *tp,
1925				    int argc, const char **argv)
1926{
1927	char buf[MAX_ARGSTR_LEN + 1];
1928	int i;
1929
1930	if (tp->nr_args < argc)
1931		return false;
1932
1933	for (i = 0; i < argc; i++) {
1934		snprintf(buf, sizeof(buf), "%s=%s",
1935			 tp->args[i].name, tp->args[i].comm);
1936		if (strcmp(buf, argv[i]))
1937			return false;
1938	}
1939	return true;
1940}
1941
1942int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **))
1943{
1944	int argc = 0, ret = 0;
1945	char **argv;
1946
1947	argv = argv_split(GFP_KERNEL, raw_command, &argc);
1948	if (!argv)
1949		return -ENOMEM;
1950
1951	if (argc)
1952		ret = createfn(argc, (const char **)argv);
1953
1954	argv_free(argv);
1955
1956	return ret;
1957}
1958
1959int trace_probe_print_args(struct trace_seq *s, struct probe_arg *args, int nr_args,
1960		 u8 *data, void *field)
1961{
1962	void *p;
1963	int i, j;
1964
1965	for (i = 0; i < nr_args; i++) {
1966		struct probe_arg *a = args + i;
1967
1968		trace_seq_printf(s, " %s=", a->name);
1969		if (likely(!a->count)) {
1970			if (!a->type->print(s, data + a->offset, field))
1971				return -ENOMEM;
1972			continue;
1973		}
1974		trace_seq_putc(s, '{');
1975		p = data + a->offset;
1976		for (j = 0; j < a->count; j++) {
1977			if (!a->type->print(s, p, field))
1978				return -ENOMEM;
1979			trace_seq_putc(s, j == a->count - 1 ? '}' : ',');
1980			p += a->type->size;
1981		}
1982	}
1983	return 0;
1984}