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