builtin-probe.c 8.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * builtin-probe.c
 *
 * Builtin probe command: Set up probe events by C expression
 *
 * Written by Masami Hiramatsu <mhiramat@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */
#define _GNU_SOURCE
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#undef _GNU_SOURCE
#include "perf.h"
#include "builtin.h"
#include "util/util.h"
38
#include "util/strlist.h"
39
#include "util/symbol.h"
40
#include "util/debug.h"
41
#include "util/debugfs.h"
42 43
#include "util/parse-options.h"
#include "util/probe-finder.h"
44
#include "util/probe-event.h"
45 46 47 48 49

#define MAX_PATH_LEN 256

/* Session management structure */
static struct {
50
	bool list_events;
51
	bool force_add;
52
	bool show_lines;
53
	bool show_vars;
54
	bool show_ext_vars;
55
	bool mod_events;
56 57
	int nevents;
	struct perf_probe_event events[MAX_PROBES];
58
	struct strlist *dellist;
59
	struct line_range line_range;
60
	const char *target_module;
61
	int max_probe_points;
62
} params;
63

64
/* Parse an event definition. Note that any error must die. */
65
static int parse_probe_event(const char *str)
66
{
67
	struct perf_probe_event *pev = &params.events[params.nevents];
68
	int ret;
69

70
	pr_debug("probe-definition(%d): %s\n", params.nevents, str);
71 72 73 74
	if (++params.nevents == MAX_PROBES) {
		pr_err("Too many probes (> %d) were specified.", MAX_PROBES);
		return -1;
	}
75

76
	/* Parse a perf-probe command into event */
77
	ret = parse_perf_probe_command(str, pev);
78
	pr_debug("%d arguments\n", pev->nargs);
79 80

	return ret;
81 82
}

83
static int parse_probe_event_argv(int argc, const char **argv)
84
{
85
	int i, len, ret;
86 87 88 89 90 91
	char *buf;

	/* Bind up rest arguments */
	len = 0;
	for (i = 0; i < argc; i++)
		len += strlen(argv[i]) + 1;
92 93 94
	buf = zalloc(len + 1);
	if (buf == NULL)
		return -ENOMEM;
95 96 97
	len = 0;
	for (i = 0; i < argc; i++)
		len += sprintf(&buf[len], "%s ", argv[i]);
98
	params.mod_events = true;
99
	ret = parse_probe_event(buf);
100
	free(buf);
101
	return ret;
102 103
}

104
static int opt_add_probe_event(const struct option *opt __used,
105 106
			      const char *str, int unset __used)
{
107 108
	if (str) {
		params.mod_events = true;
109
		return parse_probe_event(str);
110
	} else
111
		return 0;
112 113
}

114 115 116 117
static int opt_del_probe_event(const struct option *opt __used,
			       const char *str, int unset __used)
{
	if (str) {
118
		params.mod_events = true;
119 120 121
		if (!params.dellist)
			params.dellist = strlist__new(true, NULL);
		strlist__add(params.dellist, str);
122 123 124 125
	}
	return 0;
}

126
#ifdef DWARF_SUPPORT
127 128 129
static int opt_show_lines(const struct option *opt __used,
			  const char *str, int unset __used)
{
130 131
	int ret = 0;

132
	if (str)
133
		ret = parse_line_range_desc(str, &params.line_range);
134 135
	INIT_LIST_HEAD(&params.line_range.line_list);
	params.show_lines = true;
136 137

	return ret;
138
}
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

static int opt_show_vars(const struct option *opt __used,
			 const char *str, int unset __used)
{
	struct perf_probe_event *pev = &params.events[params.nevents];
	int ret;

	if (!str)
		return 0;

	ret = parse_probe_event(str);
	if (!ret && pev->nargs != 0) {
		pr_err("  Error: '--vars' doesn't accept arguments.\n");
		return -EINVAL;
	}
	params.show_vars = true;

	return ret;
}
158
#endif
159 160

static const char * const probe_usage[] = {
161 162
	"perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
	"perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
163
	"perf probe [<options>] --del '[GROUP:]EVENT' ...",
164
	"perf probe --list",
165
#ifdef DWARF_SUPPORT
166 167
	"perf probe [<options>] --line 'LINEDESC'",
	"perf probe [<options>] --vars 'PROBEPOINT'",
168
#endif
169 170 171 172
	NULL
};

static const struct option options[] = {
173
	OPT_INCR('v', "verbose", &verbose,
174
		    "be more verbose (show parsed arguments, etc)"),
175
	OPT_BOOLEAN('l', "list", &params.list_events,
176
		    "list up current probe events"),
177 178
	OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
		opt_del_probe_event),
179
	OPT_CALLBACK('a', "add", NULL,
180
#ifdef DWARF_SUPPORT
181
		"[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
182
		" [[NAME=]ARG ...]",
183
#else
184
		"[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]",
185
#endif
186
		"probe point definition, where\n"
187 188
		"\t\tGROUP:\tGroup name (optional)\n"
		"\t\tEVENT:\tEvent name\n"
189
		"\t\tFUNC:\tFunction name\n"
190
		"\t\tOFF:\tOffset from function entry (in byte)\n"
191
		"\t\t%return:\tPut the probe at function return\n"
192
#ifdef DWARF_SUPPORT
193
		"\t\tSRC:\tSource code path\n"
194 195 196
		"\t\tRL:\tRelative line number from function entry.\n"
		"\t\tAL:\tAbsolute line number in file.\n"
		"\t\tPT:\tLazy expression of line code.\n"
197
		"\t\tARG:\tProbe argument (local variable name or\n"
198
		"\t\t\tkprobe-tracer argument format.)\n",
199 200 201
#else
		"\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
#endif
202
		opt_add_probe_event),
203
	OPT_BOOLEAN('f', "force", &params.force_add, "forcibly add events"
204
		    " with existing name"),
205
#ifdef DWARF_SUPPORT
206
	OPT_CALLBACK('L', "line", NULL,
207
		     "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]",
208
		     "Show source code lines.", opt_show_lines),
209 210 211
	OPT_CALLBACK('V', "vars", NULL,
		     "FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT",
		     "Show accessible variables on PROBEDEF", opt_show_vars),
212 213
	OPT_BOOLEAN('\0', "externs", &params.show_ext_vars,
		    "Show external variables too (with --vars only)"),
214 215
	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
		   "file", "vmlinux pathname"),
216 217
	OPT_STRING('s', "source", &symbol_conf.source_prefix,
		   "directory", "path to kernel source"),
218 219
	OPT_STRING('m', "module", &params.target_module,
		   "modname", "target module name"),
220
#endif
221
	OPT__DRY_RUN(&probe_event_dry_run),
222 223
	OPT_INTEGER('\0', "max-probes", &params.max_probe_points,
		 "Set how many probe points can be found for a probe."),
224 225 226 227 228
	OPT_END()
};

int cmd_probe(int argc, const char **argv, const char *prefix __used)
{
229 230
	int ret;

231
	argc = parse_options(argc, argv, options, probe_usage,
232
			     PARSE_OPT_STOP_AT_NON_OPTION);
233 234 235 236 237
	if (argc > 0) {
		if (strcmp(argv[0], "-") == 0) {
			pr_warning("  Error: '-' is not supported.\n");
			usage_with_options(probe_usage, options);
		}
238 239 240 241 242
		ret = parse_probe_event_argv(argc, argv);
		if (ret < 0) {
			pr_err("  Error: Parse Error.  (%d)\n", ret);
			return ret;
		}
243
	}
244

245 246 247
	if (params.max_probe_points == 0)
		params.max_probe_points = MAX_PROBES;

248
	if ((!params.nevents && !params.dellist && !params.list_events &&
249
	     !params.show_lines))
250 251
		usage_with_options(probe_usage, options);

252 253 254 255 256
	/*
	 * Only consider the user's kernel image path if given.
	 */
	symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);

257
	if (params.list_events) {
258
		if (params.mod_events) {
259
			pr_err("  Error: Don't use --list with --add/--del.\n");
260 261
			usage_with_options(probe_usage, options);
		}
262
		if (params.show_lines) {
263
			pr_err("  Error: Don't use --list with --line.\n");
264 265
			usage_with_options(probe_usage, options);
		}
266 267 268 269
		if (params.show_vars) {
			pr_err(" Error: Don't use --list with --vars.\n");
			usage_with_options(probe_usage, options);
		}
270 271 272 273 274
		ret = show_perf_probe_events();
		if (ret < 0)
			pr_err("  Error: Failed to show event list. (%d)\n",
			       ret);
		return ret;
275 276
	}

277
#ifdef DWARF_SUPPORT
278
	if (params.show_lines) {
279 280 281 282 283 284 285
		if (params.mod_events) {
			pr_err("  Error: Don't use --line with"
			       " --add/--del.\n");
			usage_with_options(probe_usage, options);
		}
		if (params.show_vars) {
			pr_err(" Error: Don't use --line with --vars.\n");
286 287
			usage_with_options(probe_usage, options);
		}
288

289
		ret = show_line_range(&params.line_range, params.target_module);
290 291 292
		if (ret < 0)
			pr_err("  Error: Failed to show lines. (%d)\n", ret);
		return ret;
293
	}
294 295 296 297 298 299 300
	if (params.show_vars) {
		if (params.mod_events) {
			pr_err("  Error: Don't use --vars with"
			       " --add/--del.\n");
			usage_with_options(probe_usage, options);
		}
		ret = show_available_vars(params.events, params.nevents,
301
					  params.max_probe_points,
302
					  params.target_module,
303
					  params.show_ext_vars);
304 305 306 307
		if (ret < 0)
			pr_err("  Error: Failed to show vars. (%d)\n", ret);
		return ret;
	}
308 309
#endif

310
	if (params.dellist) {
311
		ret = del_perf_probe_events(params.dellist);
312
		strlist__delete(params.dellist);
313 314 315 316
		if (ret < 0) {
			pr_err("  Error: Failed to delete events. (%d)\n", ret);
			return ret;
		}
317 318
	}

319 320
	if (params.nevents) {
		ret = add_perf_probe_events(params.events, params.nevents,
M
Masami Hiramatsu 已提交
321
					    params.max_probe_points,
322
					    params.target_module,
M
Masami Hiramatsu 已提交
323
					    params.force_add);
324 325 326 327 328
		if (ret < 0) {
			pr_err("  Error: Failed to add events. (%d)\n", ret);
			return ret;
		}
	}
329 330
	return 0;
}