Linux Audio

Check our new training course

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